blob: 39eb3e8bf1bc841cbf1be5a782a70275a9a82a5d [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
Daniel Vetter618563e2012-04-01 13:38:50 +020027#include <linux/dmi.h>
Jesse Barnes23b2f8b2011-06-28 13:04:16 -070028#include <linux/cpufreq.h>
Jesse Barnesc1c7af62009-09-10 15:28:03 -070029#include <linux/module.h>
30#include <linux/input.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080031#include <linux/i2c.h>
Shaohua Li7662c8b2009-06-26 11:23:55 +080032#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Jesse Barnes9cce37f2010-08-13 15:11:26 -070034#include <linux/vgaarb.h>
Wu Fengguange0dac652011-09-05 14:25:34 +080035#include <drm/drm_edid.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include "drmP.h"
37#include "intel_drv.h"
38#include "i915_drm.h"
39#include "i915_drv.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070040#include "i915_trace.h"
Dave Airlieab2c0672009-12-04 10:55:24 +100041#include "drm_dp_helper.h"
Jesse Barnes79e53942008-11-07 14:24:08 -080042#include "drm_crtc_helper.h"
Keith Packardc0f372b32011-11-16 22:24:52 -080043#include <linux/dma_remapping.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080044
Zhenyu Wang32f9d652009-07-24 01:00:32 +080045#define HAS_eDP (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
46
Akshay Joshi0206e352011-08-16 15:34:10 -040047bool intel_pipe_has_type(struct drm_crtc *crtc, int type);
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
Jesse Barnes57f350b2012-03-28 13:39:25 -0700363u32 intel_dpio_read(struct drm_i915_private *dev_priv, int reg)
364{
365 unsigned long flags;
366 u32 val = 0;
367
368 spin_lock_irqsave(&dev_priv->dpio_lock, flags);
369 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
370 DRM_ERROR("DPIO idle wait timed out\n");
371 goto out_unlock;
372 }
373
374 I915_WRITE(DPIO_REG, reg);
375 I915_WRITE(DPIO_PKT, DPIO_RID | DPIO_OP_READ | DPIO_PORTID |
376 DPIO_BYTE);
377 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
378 DRM_ERROR("DPIO read wait timed out\n");
379 goto out_unlock;
380 }
381 val = I915_READ(DPIO_DATA);
382
383out_unlock:
384 spin_unlock_irqrestore(&dev_priv->dpio_lock, flags);
385 return val;
386}
387
388static void intel_dpio_write(struct drm_i915_private *dev_priv, int reg,
389 u32 val)
390{
391 unsigned long flags;
392
393 spin_lock_irqsave(&dev_priv->dpio_lock, flags);
394 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
395 DRM_ERROR("DPIO idle wait timed out\n");
396 goto out_unlock;
397 }
398
399 I915_WRITE(DPIO_DATA, val);
400 I915_WRITE(DPIO_REG, reg);
401 I915_WRITE(DPIO_PKT, DPIO_RID | DPIO_OP_WRITE | DPIO_PORTID |
402 DPIO_BYTE);
403 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100))
404 DRM_ERROR("DPIO write wait timed out\n");
405
406out_unlock:
407 spin_unlock_irqrestore(&dev_priv->dpio_lock, flags);
408}
409
410static void vlv_init_dpio(struct drm_device *dev)
411{
412 struct drm_i915_private *dev_priv = dev->dev_private;
413
414 /* Reset the DPIO config */
415 I915_WRITE(DPIO_CTL, 0);
416 POSTING_READ(DPIO_CTL);
417 I915_WRITE(DPIO_CTL, 1);
418 POSTING_READ(DPIO_CTL);
419}
420
Daniel Vetter618563e2012-04-01 13:38:50 +0200421static int intel_dual_link_lvds_callback(const struct dmi_system_id *id)
422{
423 DRM_INFO("Forcing lvds to dual link mode on %s\n", id->ident);
424 return 1;
425}
426
427static const struct dmi_system_id intel_dual_link_lvds[] = {
428 {
429 .callback = intel_dual_link_lvds_callback,
430 .ident = "Apple MacBook Pro (Core i5/i7 Series)",
431 .matches = {
432 DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
433 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro8,2"),
434 },
435 },
436 { } /* terminating entry */
437};
438
Takashi Iwaib0354382012-03-20 13:07:05 +0100439static bool is_dual_link_lvds(struct drm_i915_private *dev_priv,
440 unsigned int reg)
441{
442 unsigned int val;
443
Takashi Iwai121d5272012-03-20 13:07:06 +0100444 /* use the module option value if specified */
445 if (i915_lvds_channel_mode > 0)
446 return i915_lvds_channel_mode == 2;
447
Daniel Vetter618563e2012-04-01 13:38:50 +0200448 if (dmi_check_system(intel_dual_link_lvds))
449 return true;
450
Takashi Iwaib0354382012-03-20 13:07:05 +0100451 if (dev_priv->lvds_val)
452 val = dev_priv->lvds_val;
453 else {
454 /* BIOS should set the proper LVDS register value at boot, but
455 * in reality, it doesn't set the value when the lid is closed;
456 * we need to check "the value to be set" in VBT when LVDS
457 * register is uninitialized.
458 */
459 val = I915_READ(reg);
460 if (!(val & ~LVDS_DETECTED))
461 val = dev_priv->bios_lvds_val;
462 dev_priv->lvds_val = val;
463 }
464 return (val & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP;
465}
466
Chris Wilson1b894b52010-12-14 20:04:54 +0000467static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
468 int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800469{
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800470 struct drm_device *dev = crtc->dev;
471 struct drm_i915_private *dev_priv = dev->dev_private;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800472 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800473
474 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Takashi Iwaib0354382012-03-20 13:07:05 +0100475 if (is_dual_link_lvds(dev_priv, PCH_LVDS)) {
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800476 /* LVDS dual channel */
Chris Wilson1b894b52010-12-14 20:04:54 +0000477 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800478 limit = &intel_limits_ironlake_dual_lvds_100m;
479 else
480 limit = &intel_limits_ironlake_dual_lvds;
481 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000482 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800483 limit = &intel_limits_ironlake_single_lvds_100m;
484 else
485 limit = &intel_limits_ironlake_single_lvds;
486 }
487 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
Zhao Yakui45476682009-12-31 16:06:04 +0800488 HAS_eDP)
489 limit = &intel_limits_ironlake_display_port;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800490 else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800491 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800492
493 return limit;
494}
495
Ma Ling044c7c42009-03-18 20:13:23 +0800496static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
497{
498 struct drm_device *dev = crtc->dev;
499 struct drm_i915_private *dev_priv = dev->dev_private;
500 const intel_limit_t *limit;
501
502 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Takashi Iwaib0354382012-03-20 13:07:05 +0100503 if (is_dual_link_lvds(dev_priv, LVDS))
Ma Ling044c7c42009-03-18 20:13:23 +0800504 /* LVDS with dual channel */
Keith Packarde4b36692009-06-05 19:22:17 -0700505 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800506 else
507 /* LVDS with dual channel */
Keith Packarde4b36692009-06-05 19:22:17 -0700508 limit = &intel_limits_g4x_single_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800509 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
510 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700511 limit = &intel_limits_g4x_hdmi;
Ma Ling044c7c42009-03-18 20:13:23 +0800512 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700513 limit = &intel_limits_g4x_sdvo;
Akshay Joshi0206e352011-08-16 15:34:10 -0400514 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700515 limit = &intel_limits_g4x_display_port;
Ma Ling044c7c42009-03-18 20:13:23 +0800516 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700517 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800518
519 return limit;
520}
521
Chris Wilson1b894b52010-12-14 20:04:54 +0000522static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800523{
524 struct drm_device *dev = crtc->dev;
525 const intel_limit_t *limit;
526
Eric Anholtbad720f2009-10-22 16:11:14 -0700527 if (HAS_PCH_SPLIT(dev))
Chris Wilson1b894b52010-12-14 20:04:54 +0000528 limit = intel_ironlake_limit(crtc, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800529 else if (IS_G4X(dev)) {
Ma Ling044c7c42009-03-18 20:13:23 +0800530 limit = intel_g4x_limit(crtc);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500531 } else if (IS_PINEVIEW(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +0800532 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500533 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800534 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500535 limit = &intel_limits_pineview_sdvo;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100536 } else if (!IS_GEN2(dev)) {
537 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
538 limit = &intel_limits_i9xx_lvds;
539 else
540 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800541 } else {
542 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700543 limit = &intel_limits_i8xx_lvds;
Jesse Barnes79e53942008-11-07 14:24:08 -0800544 else
Keith Packarde4b36692009-06-05 19:22:17 -0700545 limit = &intel_limits_i8xx_dvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800546 }
547 return limit;
548}
549
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500550/* m1 is reserved as 0 in Pineview, n is a ring counter */
551static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800552{
Shaohua Li21778322009-02-23 15:19:16 +0800553 clock->m = clock->m2 + 2;
554 clock->p = clock->p1 * clock->p2;
555 clock->vco = refclk * clock->m / clock->n;
556 clock->dot = clock->vco / clock->p;
557}
558
559static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
560{
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500561 if (IS_PINEVIEW(dev)) {
562 pineview_clock(refclk, clock);
Shaohua Li21778322009-02-23 15:19:16 +0800563 return;
564 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800565 clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
566 clock->p = clock->p1 * clock->p2;
567 clock->vco = refclk * clock->m / (clock->n + 2);
568 clock->dot = clock->vco / clock->p;
569}
570
Jesse Barnes79e53942008-11-07 14:24:08 -0800571/**
572 * Returns whether any output on the specified pipe is of the specified type
573 */
Chris Wilson4ef69c72010-09-09 15:14:28 +0100574bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
Jesse Barnes79e53942008-11-07 14:24:08 -0800575{
Chris Wilson4ef69c72010-09-09 15:14:28 +0100576 struct drm_device *dev = crtc->dev;
577 struct drm_mode_config *mode_config = &dev->mode_config;
578 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -0800579
Chris Wilson4ef69c72010-09-09 15:14:28 +0100580 list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
581 if (encoder->base.crtc == crtc && encoder->type == type)
582 return true;
583
584 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -0800585}
586
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800587#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800588/**
589 * Returns whether the given set of divisors are valid for a given refclk with
590 * the given connectors.
591 */
592
Chris Wilson1b894b52010-12-14 20:04:54 +0000593static bool intel_PLL_is_valid(struct drm_device *dev,
594 const intel_limit_t *limit,
595 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800596{
Jesse Barnes79e53942008-11-07 14:24:08 -0800597 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400598 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800599 if (clock->p < limit->p.min || limit->p.max < clock->p)
Akshay Joshi0206e352011-08-16 15:34:10 -0400600 INTELPllInvalid("p out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800601 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400602 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800603 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400604 INTELPllInvalid("m1 out of range\n");
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500605 if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
Akshay Joshi0206e352011-08-16 15:34:10 -0400606 INTELPllInvalid("m1 <= m2\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800607 if (clock->m < limit->m.min || limit->m.max < clock->m)
Akshay Joshi0206e352011-08-16 15:34:10 -0400608 INTELPllInvalid("m out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800609 if (clock->n < limit->n.min || limit->n.max < clock->n)
Akshay Joshi0206e352011-08-16 15:34:10 -0400610 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800611 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400612 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800613 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
614 * connector, etc., rather than just a single range.
615 */
616 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400617 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800618
619 return true;
620}
621
Ma Lingd4906092009-03-18 20:13:27 +0800622static bool
623intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800624 int target, int refclk, intel_clock_t *match_clock,
625 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800626
Jesse Barnes79e53942008-11-07 14:24:08 -0800627{
628 struct drm_device *dev = crtc->dev;
629 struct drm_i915_private *dev_priv = dev->dev_private;
630 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800631 int err = target;
632
Bruno Prémontbc5e5712009-08-08 13:01:17 +0200633 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Florian Mickler832cc282009-07-13 18:40:32 +0800634 (I915_READ(LVDS)) != 0) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800635 /*
636 * For LVDS, if the panel is on, just rely on its current
637 * settings for dual-channel. We haven't figured out how to
638 * reliably set up different single/dual channel state, if we
639 * even can.
640 */
Takashi Iwaib0354382012-03-20 13:07:05 +0100641 if (is_dual_link_lvds(dev_priv, LVDS))
Jesse Barnes79e53942008-11-07 14:24:08 -0800642 clock.p2 = limit->p2.p2_fast;
643 else
644 clock.p2 = limit->p2.p2_slow;
645 } else {
646 if (target < limit->p2.dot_limit)
647 clock.p2 = limit->p2.p2_slow;
648 else
649 clock.p2 = limit->p2.p2_fast;
650 }
651
Akshay Joshi0206e352011-08-16 15:34:10 -0400652 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800653
Zhao Yakui42158662009-11-20 11:24:18 +0800654 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
655 clock.m1++) {
656 for (clock.m2 = limit->m2.min;
657 clock.m2 <= limit->m2.max; clock.m2++) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500658 /* m1 is always 0 in Pineview */
659 if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
Zhao Yakui42158662009-11-20 11:24:18 +0800660 break;
661 for (clock.n = limit->n.min;
662 clock.n <= limit->n.max; clock.n++) {
663 for (clock.p1 = limit->p1.min;
664 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800665 int this_err;
666
Shaohua Li21778322009-02-23 15:19:16 +0800667 intel_clock(dev, refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000668 if (!intel_PLL_is_valid(dev, limit,
669 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800670 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800671 if (match_clock &&
672 clock.p != match_clock->p)
673 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800674
675 this_err = abs(clock.dot - target);
676 if (this_err < err) {
677 *best_clock = clock;
678 err = this_err;
679 }
680 }
681 }
682 }
683 }
684
685 return (err != target);
686}
687
Ma Lingd4906092009-03-18 20:13:27 +0800688static bool
689intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800690 int target, int refclk, intel_clock_t *match_clock,
691 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800692{
693 struct drm_device *dev = crtc->dev;
694 struct drm_i915_private *dev_priv = dev->dev_private;
695 intel_clock_t clock;
696 int max_n;
697 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400698 /* approximately equals target * 0.00585 */
699 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800700 found = false;
701
702 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Zhao Yakui45476682009-12-31 16:06:04 +0800703 int lvds_reg;
704
Eric Anholtc619eed2010-01-28 16:45:52 -0800705 if (HAS_PCH_SPLIT(dev))
Zhao Yakui45476682009-12-31 16:06:04 +0800706 lvds_reg = PCH_LVDS;
707 else
708 lvds_reg = LVDS;
709 if ((I915_READ(lvds_reg) & LVDS_CLKB_POWER_MASK) ==
Ma Lingd4906092009-03-18 20:13:27 +0800710 LVDS_CLKB_POWER_UP)
711 clock.p2 = limit->p2.p2_fast;
712 else
713 clock.p2 = limit->p2.p2_slow;
714 } else {
715 if (target < limit->p2.dot_limit)
716 clock.p2 = limit->p2.p2_slow;
717 else
718 clock.p2 = limit->p2.p2_fast;
719 }
720
721 memset(best_clock, 0, sizeof(*best_clock));
722 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200723 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800724 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200725 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800726 for (clock.m1 = limit->m1.max;
727 clock.m1 >= limit->m1.min; clock.m1--) {
728 for (clock.m2 = limit->m2.max;
729 clock.m2 >= limit->m2.min; clock.m2--) {
730 for (clock.p1 = limit->p1.max;
731 clock.p1 >= limit->p1.min; clock.p1--) {
732 int this_err;
733
Shaohua Li21778322009-02-23 15:19:16 +0800734 intel_clock(dev, refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000735 if (!intel_PLL_is_valid(dev, limit,
736 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800737 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800738 if (match_clock &&
739 clock.p != match_clock->p)
740 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000741
742 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800743 if (this_err < err_most) {
744 *best_clock = clock;
745 err_most = this_err;
746 max_n = clock.n;
747 found = true;
748 }
749 }
750 }
751 }
752 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800753 return found;
754}
Ma Lingd4906092009-03-18 20:13:27 +0800755
Zhenyu Wang2c072452009-06-05 15:38:42 +0800756static bool
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500757intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800758 int target, int refclk, intel_clock_t *match_clock,
759 intel_clock_t *best_clock)
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800760{
761 struct drm_device *dev = crtc->dev;
762 intel_clock_t clock;
Zhao Yakui45476682009-12-31 16:06:04 +0800763
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800764 if (target < 200000) {
765 clock.n = 1;
766 clock.p1 = 2;
767 clock.p2 = 10;
768 clock.m1 = 12;
769 clock.m2 = 9;
770 } else {
771 clock.n = 2;
772 clock.p1 = 1;
773 clock.p2 = 10;
774 clock.m1 = 14;
775 clock.m2 = 8;
776 }
777 intel_clock(dev, refclk, &clock);
778 memcpy(best_clock, &clock, sizeof(intel_clock_t));
779 return true;
780}
781
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700782/* DisplayPort has only two frequencies, 162MHz and 270MHz */
783static bool
784intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800785 int target, int refclk, intel_clock_t *match_clock,
786 intel_clock_t *best_clock)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700787{
Chris Wilson5eddb702010-09-11 13:48:45 +0100788 intel_clock_t clock;
789 if (target < 200000) {
790 clock.p1 = 2;
791 clock.p2 = 10;
792 clock.n = 2;
793 clock.m1 = 23;
794 clock.m2 = 8;
795 } else {
796 clock.p1 = 1;
797 clock.p2 = 10;
798 clock.n = 1;
799 clock.m1 = 14;
800 clock.m2 = 2;
801 }
802 clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
803 clock.p = (clock.p1 * clock.p2);
804 clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
805 clock.vco = 0;
806 memcpy(best_clock, &clock, sizeof(intel_clock_t));
807 return true;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700808}
809
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700810/**
811 * intel_wait_for_vblank - wait for vblank on a given pipe
812 * @dev: drm device
813 * @pipe: pipe to wait for
814 *
815 * Wait for vblank to occur on a given pipe. Needed for various bits of
816 * mode setting code.
817 */
818void intel_wait_for_vblank(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -0800819{
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700820 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800821 int pipestat_reg = PIPESTAT(pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700822
Chris Wilson300387c2010-09-05 20:25:43 +0100823 /* Clear existing vblank status. Note this will clear any other
824 * sticky status fields as well.
825 *
826 * This races with i915_driver_irq_handler() with the result
827 * that either function could miss a vblank event. Here it is not
828 * fatal, as we will either wait upon the next vblank interrupt or
829 * timeout. Generally speaking intel_wait_for_vblank() is only
830 * called during modeset at which time the GPU should be idle and
831 * should *not* be performing page flips and thus not waiting on
832 * vblanks...
833 * Currently, the result of us stealing a vblank from the irq
834 * handler is that a single frame will be skipped during swapbuffers.
835 */
836 I915_WRITE(pipestat_reg,
837 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
838
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700839 /* Wait for vblank interrupt bit to set */
Chris Wilson481b6af2010-08-23 17:43:35 +0100840 if (wait_for(I915_READ(pipestat_reg) &
841 PIPE_VBLANK_INTERRUPT_STATUS,
842 50))
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700843 DRM_DEBUG_KMS("vblank wait timed out\n");
844}
845
Keith Packardab7ad7f2010-10-03 00:33:06 -0700846/*
847 * intel_wait_for_pipe_off - wait for pipe to turn off
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700848 * @dev: drm device
849 * @pipe: pipe to wait for
850 *
851 * After disabling a pipe, we can't wait for vblank in the usual way,
852 * spinning on the vblank interrupt status bit, since we won't actually
853 * see an interrupt when the pipe is disabled.
854 *
Keith Packardab7ad7f2010-10-03 00:33:06 -0700855 * On Gen4 and above:
856 * wait for the pipe register state bit to turn off
857 *
858 * Otherwise:
859 * wait for the display line value to settle (it usually
860 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +0100861 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700862 */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100863void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700864{
865 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700866
Keith Packardab7ad7f2010-10-03 00:33:06 -0700867 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson58e10eb2010-10-03 10:56:11 +0100868 int reg = PIPECONF(pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700869
Keith Packardab7ad7f2010-10-03 00:33:06 -0700870 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100871 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
872 100))
Keith Packardab7ad7f2010-10-03 00:33:06 -0700873 DRM_DEBUG_KMS("pipe_off wait timed out\n");
874 } else {
875 u32 last_line;
Chris Wilson58e10eb2010-10-03 10:56:11 +0100876 int reg = PIPEDSL(pipe);
Keith Packardab7ad7f2010-10-03 00:33:06 -0700877 unsigned long timeout = jiffies + msecs_to_jiffies(100);
878
879 /* Wait for the display line to settle */
880 do {
Chris Wilson58e10eb2010-10-03 10:56:11 +0100881 last_line = I915_READ(reg) & DSL_LINEMASK;
Keith Packardab7ad7f2010-10-03 00:33:06 -0700882 mdelay(5);
Chris Wilson58e10eb2010-10-03 10:56:11 +0100883 } while (((I915_READ(reg) & DSL_LINEMASK) != last_line) &&
Keith Packardab7ad7f2010-10-03 00:33:06 -0700884 time_after(timeout, jiffies));
885 if (time_after(jiffies, timeout))
886 DRM_DEBUG_KMS("pipe_off wait timed out\n");
887 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800888}
889
Jesse Barnesb24e7172011-01-04 15:09:30 -0800890static const char *state_string(bool enabled)
891{
892 return enabled ? "on" : "off";
893}
894
895/* Only for pre-ILK configs */
896static void assert_pll(struct drm_i915_private *dev_priv,
897 enum pipe pipe, bool state)
898{
899 int reg;
900 u32 val;
901 bool cur_state;
902
903 reg = DPLL(pipe);
904 val = I915_READ(reg);
905 cur_state = !!(val & DPLL_VCO_ENABLE);
906 WARN(cur_state != state,
907 "PLL state assertion failure (expected %s, current %s)\n",
908 state_string(state), state_string(cur_state));
909}
910#define assert_pll_enabled(d, p) assert_pll(d, p, true)
911#define assert_pll_disabled(d, p) assert_pll(d, p, false)
912
Jesse Barnes040484a2011-01-03 12:14:26 -0800913/* For ILK+ */
914static void assert_pch_pll(struct drm_i915_private *dev_priv,
915 enum pipe pipe, bool state)
916{
917 int reg;
918 u32 val;
919 bool cur_state;
920
Jesse Barnesd3ccbe82011-10-12 09:27:42 -0700921 if (HAS_PCH_CPT(dev_priv->dev)) {
922 u32 pch_dpll;
923
924 pch_dpll = I915_READ(PCH_DPLL_SEL);
925
926 /* Make sure the selected PLL is enabled to the transcoder */
927 WARN(!((pch_dpll >> (4 * pipe)) & 8),
928 "transcoder %d PLL not enabled\n", pipe);
929
930 /* Convert the transcoder pipe number to a pll pipe number */
931 pipe = (pch_dpll >> (4 * pipe)) & 1;
932 }
933
Jesse Barnes040484a2011-01-03 12:14:26 -0800934 reg = PCH_DPLL(pipe);
935 val = I915_READ(reg);
936 cur_state = !!(val & DPLL_VCO_ENABLE);
937 WARN(cur_state != state,
938 "PCH PLL state assertion failure (expected %s, current %s)\n",
939 state_string(state), state_string(cur_state));
940}
941#define assert_pch_pll_enabled(d, p) assert_pch_pll(d, p, true)
942#define assert_pch_pll_disabled(d, p) assert_pch_pll(d, p, false)
943
944static void assert_fdi_tx(struct drm_i915_private *dev_priv,
945 enum pipe pipe, bool state)
946{
947 int reg;
948 u32 val;
949 bool cur_state;
950
951 reg = FDI_TX_CTL(pipe);
952 val = I915_READ(reg);
953 cur_state = !!(val & FDI_TX_ENABLE);
954 WARN(cur_state != state,
955 "FDI TX state assertion failure (expected %s, current %s)\n",
956 state_string(state), state_string(cur_state));
957}
958#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
959#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
960
961static void assert_fdi_rx(struct drm_i915_private *dev_priv,
962 enum pipe pipe, bool state)
963{
964 int reg;
965 u32 val;
966 bool cur_state;
967
968 reg = FDI_RX_CTL(pipe);
969 val = I915_READ(reg);
970 cur_state = !!(val & FDI_RX_ENABLE);
971 WARN(cur_state != state,
972 "FDI RX state assertion failure (expected %s, current %s)\n",
973 state_string(state), state_string(cur_state));
974}
975#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
976#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
977
978static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
979 enum pipe pipe)
980{
981 int reg;
982 u32 val;
983
984 /* ILK FDI PLL is always enabled */
985 if (dev_priv->info->gen == 5)
986 return;
987
988 reg = FDI_TX_CTL(pipe);
989 val = I915_READ(reg);
990 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
991}
992
993static void assert_fdi_rx_pll_enabled(struct drm_i915_private *dev_priv,
994 enum pipe pipe)
995{
996 int reg;
997 u32 val;
998
999 reg = FDI_RX_CTL(pipe);
1000 val = I915_READ(reg);
1001 WARN(!(val & FDI_RX_PLL_ENABLE), "FDI RX PLL assertion failure, should be active but is disabled\n");
1002}
1003
Jesse Barnesea0760c2011-01-04 15:09:32 -08001004static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1005 enum pipe pipe)
1006{
1007 int pp_reg, lvds_reg;
1008 u32 val;
1009 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001010 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001011
1012 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1013 pp_reg = PCH_PP_CONTROL;
1014 lvds_reg = PCH_LVDS;
1015 } else {
1016 pp_reg = PP_CONTROL;
1017 lvds_reg = LVDS;
1018 }
1019
1020 val = I915_READ(pp_reg);
1021 if (!(val & PANEL_POWER_ON) ||
1022 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1023 locked = false;
1024
1025 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1026 panel_pipe = PIPE_B;
1027
1028 WARN(panel_pipe == pipe && locked,
1029 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001030 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001031}
1032
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001033void assert_pipe(struct drm_i915_private *dev_priv,
1034 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001035{
1036 int reg;
1037 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001038 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001039
Daniel Vetter8e636782012-01-22 01:36:48 +01001040 /* if we need the pipe A quirk it must be always on */
1041 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1042 state = true;
1043
Jesse Barnesb24e7172011-01-04 15:09:30 -08001044 reg = PIPECONF(pipe);
1045 val = I915_READ(reg);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001046 cur_state = !!(val & PIPECONF_ENABLE);
1047 WARN(cur_state != state,
1048 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001049 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001050}
1051
Chris Wilson931872f2012-01-16 23:01:13 +00001052static void assert_plane(struct drm_i915_private *dev_priv,
1053 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001054{
1055 int reg;
1056 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001057 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001058
1059 reg = DSPCNTR(plane);
1060 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001061 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1062 WARN(cur_state != state,
1063 "plane %c assertion failure (expected %s, current %s)\n",
1064 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001065}
1066
Chris Wilson931872f2012-01-16 23:01:13 +00001067#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1068#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1069
Jesse Barnesb24e7172011-01-04 15:09:30 -08001070static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1071 enum pipe pipe)
1072{
1073 int reg, i;
1074 u32 val;
1075 int cur_pipe;
1076
Jesse Barnes19ec1352011-02-02 12:28:02 -08001077 /* Planes are fixed to pipes on ILK+ */
Adam Jackson28c057942011-10-07 14:38:42 -04001078 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1079 reg = DSPCNTR(pipe);
1080 val = I915_READ(reg);
1081 WARN((val & DISPLAY_PLANE_ENABLE),
1082 "plane %c assertion failure, should be disabled but not\n",
1083 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001084 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001085 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001086
Jesse Barnesb24e7172011-01-04 15:09:30 -08001087 /* Need to check both planes against the pipe */
1088 for (i = 0; i < 2; i++) {
1089 reg = DSPCNTR(i);
1090 val = I915_READ(reg);
1091 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1092 DISPPLANE_SEL_PIPE_SHIFT;
1093 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001094 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1095 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001096 }
1097}
1098
Jesse Barnes92f25842011-01-04 15:09:34 -08001099static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1100{
1101 u32 val;
1102 bool enabled;
1103
1104 val = I915_READ(PCH_DREF_CONTROL);
1105 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1106 DREF_SUPERSPREAD_SOURCE_MASK));
1107 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1108}
1109
1110static void assert_transcoder_disabled(struct drm_i915_private *dev_priv,
1111 enum pipe pipe)
1112{
1113 int reg;
1114 u32 val;
1115 bool enabled;
1116
1117 reg = TRANSCONF(pipe);
1118 val = I915_READ(reg);
1119 enabled = !!(val & TRANS_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001120 WARN(enabled,
1121 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1122 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001123}
1124
Keith Packard4e634382011-08-06 10:39:45 -07001125static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1126 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001127{
1128 if ((val & DP_PORT_EN) == 0)
1129 return false;
1130
1131 if (HAS_PCH_CPT(dev_priv->dev)) {
1132 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1133 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1134 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1135 return false;
1136 } else {
1137 if ((val & DP_PIPE_MASK) != (pipe << 30))
1138 return false;
1139 }
1140 return true;
1141}
1142
Keith Packard1519b992011-08-06 10:35:34 -07001143static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1144 enum pipe pipe, u32 val)
1145{
1146 if ((val & PORT_ENABLE) == 0)
1147 return false;
1148
1149 if (HAS_PCH_CPT(dev_priv->dev)) {
1150 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1151 return false;
1152 } else {
1153 if ((val & TRANSCODER_MASK) != TRANSCODER(pipe))
1154 return false;
1155 }
1156 return true;
1157}
1158
1159static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1160 enum pipe pipe, u32 val)
1161{
1162 if ((val & LVDS_PORT_EN) == 0)
1163 return false;
1164
1165 if (HAS_PCH_CPT(dev_priv->dev)) {
1166 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1167 return false;
1168 } else {
1169 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1170 return false;
1171 }
1172 return true;
1173}
1174
1175static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1176 enum pipe pipe, u32 val)
1177{
1178 if ((val & ADPA_DAC_ENABLE) == 0)
1179 return false;
1180 if (HAS_PCH_CPT(dev_priv->dev)) {
1181 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1182 return false;
1183 } else {
1184 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1185 return false;
1186 }
1187 return true;
1188}
1189
Jesse Barnes291906f2011-02-02 12:28:03 -08001190static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001191 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001192{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001193 u32 val = I915_READ(reg);
Keith Packard4e634382011-08-06 10:39:45 -07001194 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001195 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001196 reg, pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001197}
1198
1199static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1200 enum pipe pipe, int reg)
1201{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001202 u32 val = I915_READ(reg);
Keith Packard1519b992011-08-06 10:35:34 -07001203 WARN(hdmi_pipe_enabled(dev_priv, val, pipe),
Adam Jackson23c99e72011-10-07 14:38:43 -04001204 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001205 reg, pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001206}
1207
1208static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1209 enum pipe pipe)
1210{
1211 int reg;
1212 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001213
Keith Packardf0575e92011-07-25 22:12:43 -07001214 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1215 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1216 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001217
1218 reg = PCH_ADPA;
1219 val = I915_READ(reg);
Keith Packard1519b992011-08-06 10:35:34 -07001220 WARN(adpa_pipe_enabled(dev_priv, val, pipe),
Jesse Barnes291906f2011-02-02 12:28:03 -08001221 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001222 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001223
1224 reg = PCH_LVDS;
1225 val = I915_READ(reg);
Keith Packard1519b992011-08-06 10:35:34 -07001226 WARN(lvds_pipe_enabled(dev_priv, val, pipe),
Jesse Barnes291906f2011-02-02 12:28:03 -08001227 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001228 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001229
1230 assert_pch_hdmi_disabled(dev_priv, pipe, HDMIB);
1231 assert_pch_hdmi_disabled(dev_priv, pipe, HDMIC);
1232 assert_pch_hdmi_disabled(dev_priv, pipe, HDMID);
1233}
1234
Jesse Barnesb24e7172011-01-04 15:09:30 -08001235/**
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001236 * intel_enable_pll - enable a PLL
1237 * @dev_priv: i915 private structure
1238 * @pipe: pipe PLL to enable
1239 *
1240 * Enable @pipe's PLL so we can start pumping pixels from a plane. Check to
1241 * make sure the PLL reg is writable first though, since the panel write
1242 * protect mechanism may be enabled.
1243 *
1244 * Note! This is for pre-ILK only.
1245 */
1246static void intel_enable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1247{
1248 int reg;
1249 u32 val;
1250
1251 /* No really, not for ILK+ */
1252 BUG_ON(dev_priv->info->gen >= 5);
1253
1254 /* PLL is protected by panel, make sure we can write it */
1255 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
1256 assert_panel_unlocked(dev_priv, pipe);
1257
1258 reg = DPLL(pipe);
1259 val = I915_READ(reg);
1260 val |= DPLL_VCO_ENABLE;
1261
1262 /* We do this three times for luck */
1263 I915_WRITE(reg, val);
1264 POSTING_READ(reg);
1265 udelay(150); /* wait for warmup */
1266 I915_WRITE(reg, val);
1267 POSTING_READ(reg);
1268 udelay(150); /* wait for warmup */
1269 I915_WRITE(reg, val);
1270 POSTING_READ(reg);
1271 udelay(150); /* wait for warmup */
1272}
1273
1274/**
1275 * intel_disable_pll - disable a PLL
1276 * @dev_priv: i915 private structure
1277 * @pipe: pipe PLL to disable
1278 *
1279 * Disable the PLL for @pipe, making sure the pipe is off first.
1280 *
1281 * Note! This is for pre-ILK only.
1282 */
1283static void intel_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1284{
1285 int reg;
1286 u32 val;
1287
1288 /* Don't disable pipe A or pipe A PLLs if needed */
1289 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1290 return;
1291
1292 /* Make sure the pipe isn't still relying on us */
1293 assert_pipe_disabled(dev_priv, pipe);
1294
1295 reg = DPLL(pipe);
1296 val = I915_READ(reg);
1297 val &= ~DPLL_VCO_ENABLE;
1298 I915_WRITE(reg, val);
1299 POSTING_READ(reg);
1300}
1301
1302/**
Jesse Barnes92f25842011-01-04 15:09:34 -08001303 * intel_enable_pch_pll - enable PCH PLL
1304 * @dev_priv: i915 private structure
1305 * @pipe: pipe PLL to enable
1306 *
1307 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1308 * drives the transcoder clock.
1309 */
1310static void intel_enable_pch_pll(struct drm_i915_private *dev_priv,
1311 enum pipe pipe)
1312{
1313 int reg;
1314 u32 val;
1315
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001316 if (pipe > 1)
1317 return;
1318
Jesse Barnes92f25842011-01-04 15:09:34 -08001319 /* PCH only available on ILK+ */
1320 BUG_ON(dev_priv->info->gen < 5);
1321
1322 /* PCH refclock must be enabled first */
1323 assert_pch_refclk_enabled(dev_priv);
1324
1325 reg = PCH_DPLL(pipe);
1326 val = I915_READ(reg);
1327 val |= DPLL_VCO_ENABLE;
1328 I915_WRITE(reg, val);
1329 POSTING_READ(reg);
1330 udelay(200);
1331}
1332
1333static void intel_disable_pch_pll(struct drm_i915_private *dev_priv,
1334 enum pipe pipe)
1335{
1336 int reg;
Jesse Barnes7a419862011-11-15 10:28:53 -08001337 u32 val, pll_mask = TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL,
1338 pll_sel = TRANSC_DPLL_ENABLE;
Jesse Barnes92f25842011-01-04 15:09:34 -08001339
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001340 if (pipe > 1)
1341 return;
1342
Jesse Barnes92f25842011-01-04 15:09:34 -08001343 /* PCH only available on ILK+ */
1344 BUG_ON(dev_priv->info->gen < 5);
1345
1346 /* Make sure transcoder isn't still depending on us */
1347 assert_transcoder_disabled(dev_priv, pipe);
1348
Jesse Barnes7a419862011-11-15 10:28:53 -08001349 if (pipe == 0)
1350 pll_sel |= TRANSC_DPLLA_SEL;
1351 else if (pipe == 1)
1352 pll_sel |= TRANSC_DPLLB_SEL;
1353
1354
1355 if ((I915_READ(PCH_DPLL_SEL) & pll_mask) == pll_sel)
1356 return;
1357
Jesse Barnes92f25842011-01-04 15:09:34 -08001358 reg = PCH_DPLL(pipe);
1359 val = I915_READ(reg);
1360 val &= ~DPLL_VCO_ENABLE;
1361 I915_WRITE(reg, val);
1362 POSTING_READ(reg);
1363 udelay(200);
1364}
1365
Jesse Barnes040484a2011-01-03 12:14:26 -08001366static void intel_enable_transcoder(struct drm_i915_private *dev_priv,
1367 enum pipe pipe)
1368{
1369 int reg;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001370 u32 val, pipeconf_val;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001371 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Jesse Barnes040484a2011-01-03 12:14:26 -08001372
1373 /* PCH only available on ILK+ */
1374 BUG_ON(dev_priv->info->gen < 5);
1375
1376 /* Make sure PCH DPLL is enabled */
1377 assert_pch_pll_enabled(dev_priv, pipe);
1378
1379 /* FDI must be feeding us bits for PCH ports */
1380 assert_fdi_tx_enabled(dev_priv, pipe);
1381 assert_fdi_rx_enabled(dev_priv, pipe);
1382
1383 reg = TRANSCONF(pipe);
1384 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001385 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001386
1387 if (HAS_PCH_IBX(dev_priv->dev)) {
1388 /*
1389 * make the BPC in transcoder be consistent with
1390 * that in pipeconf reg.
1391 */
1392 val &= ~PIPE_BPC_MASK;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001393 val |= pipeconf_val & PIPE_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001394 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001395
1396 val &= ~TRANS_INTERLACE_MASK;
1397 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001398 if (HAS_PCH_IBX(dev_priv->dev) &&
1399 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1400 val |= TRANS_LEGACY_INTERLACED_ILK;
1401 else
1402 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001403 else
1404 val |= TRANS_PROGRESSIVE;
1405
Jesse Barnes040484a2011-01-03 12:14:26 -08001406 I915_WRITE(reg, val | TRANS_ENABLE);
1407 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
1408 DRM_ERROR("failed to enable transcoder %d\n", pipe);
1409}
1410
1411static void intel_disable_transcoder(struct drm_i915_private *dev_priv,
1412 enum pipe pipe)
1413{
1414 int reg;
1415 u32 val;
1416
1417 /* FDI relies on the transcoder */
1418 assert_fdi_tx_disabled(dev_priv, pipe);
1419 assert_fdi_rx_disabled(dev_priv, pipe);
1420
Jesse Barnes291906f2011-02-02 12:28:03 -08001421 /* Ports must be off as well */
1422 assert_pch_ports_disabled(dev_priv, pipe);
1423
Jesse Barnes040484a2011-01-03 12:14:26 -08001424 reg = TRANSCONF(pipe);
1425 val = I915_READ(reg);
1426 val &= ~TRANS_ENABLE;
1427 I915_WRITE(reg, val);
1428 /* wait for PCH transcoder off, transcoder state */
1429 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Jesse Barnes4c9c18c2011-10-13 09:46:32 -07001430 DRM_ERROR("failed to disable transcoder %d\n", pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001431}
1432
Jesse Barnes92f25842011-01-04 15:09:34 -08001433/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001434 * intel_enable_pipe - enable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001435 * @dev_priv: i915 private structure
1436 * @pipe: pipe to enable
Jesse Barnes040484a2011-01-03 12:14:26 -08001437 * @pch_port: on ILK+, is this pipe driving a PCH port or not
Jesse Barnesb24e7172011-01-04 15:09:30 -08001438 *
1439 * Enable @pipe, making sure that various hardware specific requirements
1440 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1441 *
1442 * @pipe should be %PIPE_A or %PIPE_B.
1443 *
1444 * Will wait until the pipe is actually running (i.e. first vblank) before
1445 * returning.
1446 */
Jesse Barnes040484a2011-01-03 12:14:26 -08001447static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
1448 bool pch_port)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001449{
1450 int reg;
1451 u32 val;
1452
1453 /*
1454 * A pipe without a PLL won't actually be able to drive bits from
1455 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1456 * need the check.
1457 */
1458 if (!HAS_PCH_SPLIT(dev_priv->dev))
1459 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001460 else {
1461 if (pch_port) {
1462 /* if driving the PCH, we need FDI enabled */
1463 assert_fdi_rx_pll_enabled(dev_priv, pipe);
1464 assert_fdi_tx_pll_enabled(dev_priv, pipe);
1465 }
1466 /* FIXME: assert CPU port conditions for SNB+ */
1467 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001468
1469 reg = PIPECONF(pipe);
1470 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001471 if (val & PIPECONF_ENABLE)
1472 return;
1473
1474 I915_WRITE(reg, val | PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001475 intel_wait_for_vblank(dev_priv->dev, pipe);
1476}
1477
1478/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001479 * intel_disable_pipe - disable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001480 * @dev_priv: i915 private structure
1481 * @pipe: pipe to disable
1482 *
1483 * Disable @pipe, making sure that various hardware specific requirements
1484 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1485 *
1486 * @pipe should be %PIPE_A or %PIPE_B.
1487 *
1488 * Will wait until the pipe has shut down before returning.
1489 */
1490static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1491 enum pipe pipe)
1492{
1493 int reg;
1494 u32 val;
1495
1496 /*
1497 * Make sure planes won't keep trying to pump pixels to us,
1498 * or we might hang the display.
1499 */
1500 assert_planes_disabled(dev_priv, pipe);
1501
1502 /* Don't disable pipe A or pipe A PLLs if needed */
1503 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1504 return;
1505
1506 reg = PIPECONF(pipe);
1507 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001508 if ((val & PIPECONF_ENABLE) == 0)
1509 return;
1510
1511 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001512 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1513}
1514
Keith Packardd74362c2011-07-28 14:47:14 -07001515/*
1516 * Plane regs are double buffered, going from enabled->disabled needs a
1517 * trigger in order to latch. The display address reg provides this.
1518 */
1519static void intel_flush_display_plane(struct drm_i915_private *dev_priv,
1520 enum plane plane)
1521{
1522 I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
1523 I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
1524}
1525
Jesse Barnesb24e7172011-01-04 15:09:30 -08001526/**
1527 * intel_enable_plane - enable a display plane on a given pipe
1528 * @dev_priv: i915 private structure
1529 * @plane: plane to enable
1530 * @pipe: pipe being fed
1531 *
1532 * Enable @plane on @pipe, making sure that @pipe is running first.
1533 */
1534static void intel_enable_plane(struct drm_i915_private *dev_priv,
1535 enum plane plane, enum pipe pipe)
1536{
1537 int reg;
1538 u32 val;
1539
1540 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1541 assert_pipe_enabled(dev_priv, pipe);
1542
1543 reg = DSPCNTR(plane);
1544 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001545 if (val & DISPLAY_PLANE_ENABLE)
1546 return;
1547
1548 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
Keith Packardd74362c2011-07-28 14:47:14 -07001549 intel_flush_display_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001550 intel_wait_for_vblank(dev_priv->dev, pipe);
1551}
1552
Jesse Barnesb24e7172011-01-04 15:09:30 -08001553/**
1554 * intel_disable_plane - disable a display plane
1555 * @dev_priv: i915 private structure
1556 * @plane: plane to disable
1557 * @pipe: pipe consuming the data
1558 *
1559 * Disable @plane; should be an independent operation.
1560 */
1561static void intel_disable_plane(struct drm_i915_private *dev_priv,
1562 enum plane plane, enum pipe pipe)
1563{
1564 int reg;
1565 u32 val;
1566
1567 reg = DSPCNTR(plane);
1568 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001569 if ((val & DISPLAY_PLANE_ENABLE) == 0)
1570 return;
1571
1572 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001573 intel_flush_display_plane(dev_priv, plane);
1574 intel_wait_for_vblank(dev_priv->dev, pipe);
1575}
1576
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001577static void disable_pch_dp(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001578 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001579{
1580 u32 val = I915_READ(reg);
Keith Packard4e634382011-08-06 10:39:45 -07001581 if (dp_pipe_enabled(dev_priv, pipe, port_sel, val)) {
Keith Packardf0575e92011-07-25 22:12:43 -07001582 DRM_DEBUG_KMS("Disabling pch dp %x on pipe %d\n", reg, pipe);
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001583 I915_WRITE(reg, val & ~DP_PORT_EN);
Keith Packardf0575e92011-07-25 22:12:43 -07001584 }
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001585}
1586
1587static void disable_pch_hdmi(struct drm_i915_private *dev_priv,
1588 enum pipe pipe, int reg)
1589{
1590 u32 val = I915_READ(reg);
Keith Packard1519b992011-08-06 10:35:34 -07001591 if (hdmi_pipe_enabled(dev_priv, val, pipe)) {
Keith Packardf0575e92011-07-25 22:12:43 -07001592 DRM_DEBUG_KMS("Disabling pch HDMI %x on pipe %d\n",
1593 reg, pipe);
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001594 I915_WRITE(reg, val & ~PORT_ENABLE);
Keith Packardf0575e92011-07-25 22:12:43 -07001595 }
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001596}
1597
1598/* Disable any ports connected to this transcoder */
1599static void intel_disable_pch_ports(struct drm_i915_private *dev_priv,
1600 enum pipe pipe)
1601{
1602 u32 reg, val;
1603
1604 val = I915_READ(PCH_PP_CONTROL);
1605 I915_WRITE(PCH_PP_CONTROL, val | PANEL_UNLOCK_REGS);
1606
Keith Packardf0575e92011-07-25 22:12:43 -07001607 disable_pch_dp(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1608 disable_pch_dp(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1609 disable_pch_dp(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001610
1611 reg = PCH_ADPA;
1612 val = I915_READ(reg);
Keith Packard1519b992011-08-06 10:35:34 -07001613 if (adpa_pipe_enabled(dev_priv, val, pipe))
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001614 I915_WRITE(reg, val & ~ADPA_DAC_ENABLE);
1615
1616 reg = PCH_LVDS;
1617 val = I915_READ(reg);
Keith Packard1519b992011-08-06 10:35:34 -07001618 if (lvds_pipe_enabled(dev_priv, val, pipe)) {
1619 DRM_DEBUG_KMS("disable lvds on pipe %d val 0x%08x\n", pipe, val);
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001620 I915_WRITE(reg, val & ~LVDS_PORT_EN);
1621 POSTING_READ(reg);
1622 udelay(100);
1623 }
1624
1625 disable_pch_hdmi(dev_priv, pipe, HDMIB);
1626 disable_pch_hdmi(dev_priv, pipe, HDMIC);
1627 disable_pch_hdmi(dev_priv, pipe, HDMID);
1628}
1629
Chris Wilson43a95392011-07-08 12:22:36 +01001630static void i8xx_disable_fbc(struct drm_device *dev)
1631{
1632 struct drm_i915_private *dev_priv = dev->dev_private;
1633 u32 fbc_ctl;
1634
1635 /* Disable compression */
1636 fbc_ctl = I915_READ(FBC_CONTROL);
1637 if ((fbc_ctl & FBC_CTL_EN) == 0)
1638 return;
1639
1640 fbc_ctl &= ~FBC_CTL_EN;
1641 I915_WRITE(FBC_CONTROL, fbc_ctl);
1642
1643 /* Wait for compressing bit to clear */
1644 if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
1645 DRM_DEBUG_KMS("FBC idle timed out\n");
1646 return;
1647 }
1648
1649 DRM_DEBUG_KMS("disabled FBC\n");
1650}
1651
Jesse Barnes80824002009-09-10 15:28:06 -07001652static void i8xx_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
1653{
1654 struct drm_device *dev = crtc->dev;
1655 struct drm_i915_private *dev_priv = dev->dev_private;
1656 struct drm_framebuffer *fb = crtc->fb;
1657 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00001658 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes80824002009-09-10 15:28:06 -07001659 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson016b9b62011-07-08 12:22:43 +01001660 int cfb_pitch;
Jesse Barnes80824002009-09-10 15:28:06 -07001661 int plane, i;
1662 u32 fbc_ctl, fbc_ctl2;
1663
Chris Wilson016b9b62011-07-08 12:22:43 +01001664 cfb_pitch = dev_priv->cfb_size / FBC_LL_SIZE;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001665 if (fb->pitches[0] < cfb_pitch)
1666 cfb_pitch = fb->pitches[0];
Jesse Barnes80824002009-09-10 15:28:06 -07001667
1668 /* FBC_CTL wants 64B units */
Chris Wilson016b9b62011-07-08 12:22:43 +01001669 cfb_pitch = (cfb_pitch / 64) - 1;
1670 plane = intel_crtc->plane == 0 ? FBC_CTL_PLANEA : FBC_CTL_PLANEB;
Jesse Barnes80824002009-09-10 15:28:06 -07001671
1672 /* Clear old tags */
1673 for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
1674 I915_WRITE(FBC_TAG + (i * 4), 0);
1675
1676 /* Set it up... */
Chris Wilsonde568512011-07-08 12:22:39 +01001677 fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
1678 fbc_ctl2 |= plane;
Jesse Barnes80824002009-09-10 15:28:06 -07001679 I915_WRITE(FBC_CONTROL2, fbc_ctl2);
1680 I915_WRITE(FBC_FENCE_OFF, crtc->y);
1681
1682 /* enable it... */
1683 fbc_ctl = FBC_CTL_EN | FBC_CTL_PERIODIC;
Jesse Barnesee25df22010-02-06 10:41:53 -08001684 if (IS_I945GM(dev))
Priit Laes49677902010-03-02 11:37:00 +02001685 fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
Chris Wilson016b9b62011-07-08 12:22:43 +01001686 fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
Jesse Barnes80824002009-09-10 15:28:06 -07001687 fbc_ctl |= (interval & 0x2fff) << FBC_CTL_INTERVAL_SHIFT;
Chris Wilson016b9b62011-07-08 12:22:43 +01001688 fbc_ctl |= obj->fence_reg;
Jesse Barnes80824002009-09-10 15:28:06 -07001689 I915_WRITE(FBC_CONTROL, fbc_ctl);
1690
Chris Wilson016b9b62011-07-08 12:22:43 +01001691 DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %d, ",
1692 cfb_pitch, crtc->y, intel_crtc->plane);
Jesse Barnes80824002009-09-10 15:28:06 -07001693}
1694
Adam Jacksonee5382a2010-04-23 11:17:39 -04001695static bool i8xx_fbc_enabled(struct drm_device *dev)
Jesse Barnes80824002009-09-10 15:28:06 -07001696{
Jesse Barnes80824002009-09-10 15:28:06 -07001697 struct drm_i915_private *dev_priv = dev->dev_private;
1698
1699 return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
1700}
1701
Jesse Barnes74dff282009-09-14 15:39:40 -07001702static void g4x_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
1703{
1704 struct drm_device *dev = crtc->dev;
1705 struct drm_i915_private *dev_priv = dev->dev_private;
1706 struct drm_framebuffer *fb = crtc->fb;
1707 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00001708 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes74dff282009-09-14 15:39:40 -07001709 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5eddb702010-09-11 13:48:45 +01001710 int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
Jesse Barnes74dff282009-09-14 15:39:40 -07001711 unsigned long stall_watermark = 200;
1712 u32 dpfc_ctl;
1713
Jesse Barnes74dff282009-09-14 15:39:40 -07001714 dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;
Chris Wilson016b9b62011-07-08 12:22:43 +01001715 dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
Chris Wilsonde568512011-07-08 12:22:39 +01001716 I915_WRITE(DPFC_CHICKEN, DPFC_HT_MODIFY);
Jesse Barnes74dff282009-09-14 15:39:40 -07001717
Jesse Barnes74dff282009-09-14 15:39:40 -07001718 I915_WRITE(DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
1719 (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
1720 (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
1721 I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
1722
1723 /* enable it... */
1724 I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);
1725
Zhao Yakui28c97732009-10-09 11:39:41 +08001726 DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
Jesse Barnes74dff282009-09-14 15:39:40 -07001727}
1728
Chris Wilson43a95392011-07-08 12:22:36 +01001729static void g4x_disable_fbc(struct drm_device *dev)
Jesse Barnes74dff282009-09-14 15:39:40 -07001730{
1731 struct drm_i915_private *dev_priv = dev->dev_private;
1732 u32 dpfc_ctl;
1733
1734 /* Disable compression */
1735 dpfc_ctl = I915_READ(DPFC_CONTROL);
Chris Wilsonbed4a672010-09-11 10:47:47 +01001736 if (dpfc_ctl & DPFC_CTL_EN) {
1737 dpfc_ctl &= ~DPFC_CTL_EN;
1738 I915_WRITE(DPFC_CONTROL, dpfc_ctl);
Jesse Barnes74dff282009-09-14 15:39:40 -07001739
Chris Wilsonbed4a672010-09-11 10:47:47 +01001740 DRM_DEBUG_KMS("disabled FBC\n");
1741 }
Jesse Barnes74dff282009-09-14 15:39:40 -07001742}
1743
Adam Jacksonee5382a2010-04-23 11:17:39 -04001744static bool g4x_fbc_enabled(struct drm_device *dev)
Jesse Barnes74dff282009-09-14 15:39:40 -07001745{
Jesse Barnes74dff282009-09-14 15:39:40 -07001746 struct drm_i915_private *dev_priv = dev->dev_private;
1747
1748 return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
1749}
1750
Jesse Barnes4efe0702011-01-18 11:25:41 -08001751static void sandybridge_blit_fbc_update(struct drm_device *dev)
1752{
1753 struct drm_i915_private *dev_priv = dev->dev_private;
1754 u32 blt_ecoskpd;
1755
1756 /* Make sure blitter notifies FBC of writes */
Ben Widawskyfcca7922011-04-25 11:23:07 -07001757 gen6_gt_force_wake_get(dev_priv);
Jesse Barnes4efe0702011-01-18 11:25:41 -08001758 blt_ecoskpd = I915_READ(GEN6_BLITTER_ECOSKPD);
1759 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY <<
1760 GEN6_BLITTER_LOCK_SHIFT;
1761 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
1762 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY;
1763 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
1764 blt_ecoskpd &= ~(GEN6_BLITTER_FBC_NOTIFY <<
1765 GEN6_BLITTER_LOCK_SHIFT);
1766 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
1767 POSTING_READ(GEN6_BLITTER_ECOSKPD);
Ben Widawskyfcca7922011-04-25 11:23:07 -07001768 gen6_gt_force_wake_put(dev_priv);
Jesse Barnes4efe0702011-01-18 11:25:41 -08001769}
1770
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001771static void ironlake_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
1772{
1773 struct drm_device *dev = crtc->dev;
1774 struct drm_i915_private *dev_priv = dev->dev_private;
1775 struct drm_framebuffer *fb = crtc->fb;
1776 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00001777 struct drm_i915_gem_object *obj = intel_fb->obj;
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001778 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5eddb702010-09-11 13:48:45 +01001779 int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001780 unsigned long stall_watermark = 200;
1781 u32 dpfc_ctl;
1782
Chris Wilsonbed4a672010-09-11 10:47:47 +01001783 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001784 dpfc_ctl &= DPFC_RESERVED;
1785 dpfc_ctl |= (plane | DPFC_CTL_LIMIT_1X);
Chris Wilson9ce9d062011-07-08 12:22:40 +01001786 /* Set persistent mode for front-buffer rendering, ala X. */
1787 dpfc_ctl |= DPFC_CTL_PERSISTENT_MODE;
Chris Wilson016b9b62011-07-08 12:22:43 +01001788 dpfc_ctl |= (DPFC_CTL_FENCE_EN | obj->fence_reg);
Chris Wilsonde568512011-07-08 12:22:39 +01001789 I915_WRITE(ILK_DPFC_CHICKEN, DPFC_HT_MODIFY);
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001790
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001791 I915_WRITE(ILK_DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
1792 (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
1793 (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
1794 I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
Chris Wilson05394f32010-11-08 19:18:58 +00001795 I915_WRITE(ILK_FBC_RT_BASE, obj->gtt_offset | ILK_FBC_RT_VALID);
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001796 /* enable it... */
Chris Wilsonbed4a672010-09-11 10:47:47 +01001797 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001798
Yuanhan Liu9c04f012010-12-15 15:42:32 +08001799 if (IS_GEN6(dev)) {
1800 I915_WRITE(SNB_DPFC_CTL_SA,
Chris Wilson016b9b62011-07-08 12:22:43 +01001801 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
Yuanhan Liu9c04f012010-12-15 15:42:32 +08001802 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
Jesse Barnes4efe0702011-01-18 11:25:41 -08001803 sandybridge_blit_fbc_update(dev);
Yuanhan Liu9c04f012010-12-15 15:42:32 +08001804 }
1805
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001806 DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
1807}
1808
Chris Wilson43a95392011-07-08 12:22:36 +01001809static void ironlake_disable_fbc(struct drm_device *dev)
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001810{
1811 struct drm_i915_private *dev_priv = dev->dev_private;
1812 u32 dpfc_ctl;
1813
1814 /* Disable compression */
1815 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
Chris Wilsonbed4a672010-09-11 10:47:47 +01001816 if (dpfc_ctl & DPFC_CTL_EN) {
1817 dpfc_ctl &= ~DPFC_CTL_EN;
1818 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001819
Chris Wilsonbed4a672010-09-11 10:47:47 +01001820 DRM_DEBUG_KMS("disabled FBC\n");
1821 }
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001822}
1823
1824static bool ironlake_fbc_enabled(struct drm_device *dev)
1825{
1826 struct drm_i915_private *dev_priv = dev->dev_private;
1827
1828 return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
1829}
1830
Adam Jacksonee5382a2010-04-23 11:17:39 -04001831bool intel_fbc_enabled(struct drm_device *dev)
1832{
1833 struct drm_i915_private *dev_priv = dev->dev_private;
1834
1835 if (!dev_priv->display.fbc_enabled)
1836 return false;
1837
1838 return dev_priv->display.fbc_enabled(dev);
1839}
1840
Chris Wilson1630fe72011-07-08 12:22:42 +01001841static void intel_fbc_work_fn(struct work_struct *__work)
1842{
1843 struct intel_fbc_work *work =
1844 container_of(to_delayed_work(__work),
1845 struct intel_fbc_work, work);
1846 struct drm_device *dev = work->crtc->dev;
1847 struct drm_i915_private *dev_priv = dev->dev_private;
1848
1849 mutex_lock(&dev->struct_mutex);
1850 if (work == dev_priv->fbc_work) {
1851 /* Double check that we haven't switched fb without cancelling
1852 * the prior work.
1853 */
Chris Wilson016b9b62011-07-08 12:22:43 +01001854 if (work->crtc->fb == work->fb) {
Chris Wilson1630fe72011-07-08 12:22:42 +01001855 dev_priv->display.enable_fbc(work->crtc,
1856 work->interval);
1857
Chris Wilson016b9b62011-07-08 12:22:43 +01001858 dev_priv->cfb_plane = to_intel_crtc(work->crtc)->plane;
1859 dev_priv->cfb_fb = work->crtc->fb->base.id;
1860 dev_priv->cfb_y = work->crtc->y;
1861 }
1862
Chris Wilson1630fe72011-07-08 12:22:42 +01001863 dev_priv->fbc_work = NULL;
1864 }
1865 mutex_unlock(&dev->struct_mutex);
1866
1867 kfree(work);
1868}
1869
1870static void intel_cancel_fbc_work(struct drm_i915_private *dev_priv)
1871{
1872 if (dev_priv->fbc_work == NULL)
1873 return;
1874
1875 DRM_DEBUG_KMS("cancelling pending FBC enable\n");
1876
1877 /* Synchronisation is provided by struct_mutex and checking of
1878 * dev_priv->fbc_work, so we can perform the cancellation
1879 * entirely asynchronously.
1880 */
1881 if (cancel_delayed_work(&dev_priv->fbc_work->work))
1882 /* tasklet was killed before being run, clean up */
1883 kfree(dev_priv->fbc_work);
1884
1885 /* Mark the work as no longer wanted so that if it does
1886 * wake-up (because the work was already running and waiting
1887 * for our mutex), it will discover that is no longer
1888 * necessary to run.
1889 */
1890 dev_priv->fbc_work = NULL;
1891}
1892
Chris Wilson43a95392011-07-08 12:22:36 +01001893static void intel_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
Adam Jacksonee5382a2010-04-23 11:17:39 -04001894{
Chris Wilson1630fe72011-07-08 12:22:42 +01001895 struct intel_fbc_work *work;
1896 struct drm_device *dev = crtc->dev;
1897 struct drm_i915_private *dev_priv = dev->dev_private;
Adam Jacksonee5382a2010-04-23 11:17:39 -04001898
1899 if (!dev_priv->display.enable_fbc)
1900 return;
1901
Chris Wilson1630fe72011-07-08 12:22:42 +01001902 intel_cancel_fbc_work(dev_priv);
1903
1904 work = kzalloc(sizeof *work, GFP_KERNEL);
1905 if (work == NULL) {
1906 dev_priv->display.enable_fbc(crtc, interval);
1907 return;
1908 }
1909
1910 work->crtc = crtc;
1911 work->fb = crtc->fb;
1912 work->interval = interval;
1913 INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
1914
1915 dev_priv->fbc_work = work;
1916
1917 DRM_DEBUG_KMS("scheduling delayed FBC enable\n");
1918
1919 /* Delay the actual enabling to let pageflipping cease and the
Chris Wilson016b9b62011-07-08 12:22:43 +01001920 * display to settle before starting the compression. Note that
1921 * this delay also serves a second purpose: it allows for a
1922 * vblank to pass after disabling the FBC before we attempt
1923 * to modify the control registers.
Chris Wilson1630fe72011-07-08 12:22:42 +01001924 *
1925 * A more complicated solution would involve tracking vblanks
1926 * following the termination of the page-flipping sequence
1927 * and indeed performing the enable as a co-routine and not
1928 * waiting synchronously upon the vblank.
1929 */
1930 schedule_delayed_work(&work->work, msecs_to_jiffies(50));
Adam Jacksonee5382a2010-04-23 11:17:39 -04001931}
1932
1933void intel_disable_fbc(struct drm_device *dev)
1934{
1935 struct drm_i915_private *dev_priv = dev->dev_private;
1936
Chris Wilson1630fe72011-07-08 12:22:42 +01001937 intel_cancel_fbc_work(dev_priv);
1938
Adam Jacksonee5382a2010-04-23 11:17:39 -04001939 if (!dev_priv->display.disable_fbc)
1940 return;
1941
1942 dev_priv->display.disable_fbc(dev);
Chris Wilson016b9b62011-07-08 12:22:43 +01001943 dev_priv->cfb_plane = -1;
Adam Jacksonee5382a2010-04-23 11:17:39 -04001944}
1945
Jesse Barnes80824002009-09-10 15:28:06 -07001946/**
1947 * intel_update_fbc - enable/disable FBC as needed
Chris Wilsonbed4a672010-09-11 10:47:47 +01001948 * @dev: the drm_device
Jesse Barnes80824002009-09-10 15:28:06 -07001949 *
1950 * Set up the framebuffer compression hardware at mode set time. We
1951 * enable it if possible:
1952 * - plane A only (on pre-965)
1953 * - no pixel mulitply/line duplication
1954 * - no alpha buffer discard
1955 * - no dual wide
1956 * - framebuffer <= 2048 in width, 1536 in height
1957 *
1958 * We can't assume that any compression will take place (worst case),
1959 * so the compressed buffer has to be the same size as the uncompressed
1960 * one. It also must reside (along with the line length buffer) in
1961 * stolen memory.
1962 *
1963 * We need to enable/disable FBC on a global basis.
1964 */
Chris Wilsonbed4a672010-09-11 10:47:47 +01001965static void intel_update_fbc(struct drm_device *dev)
Jesse Barnes80824002009-09-10 15:28:06 -07001966{
Jesse Barnes80824002009-09-10 15:28:06 -07001967 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonbed4a672010-09-11 10:47:47 +01001968 struct drm_crtc *crtc = NULL, *tmp_crtc;
1969 struct intel_crtc *intel_crtc;
1970 struct drm_framebuffer *fb;
Jesse Barnes80824002009-09-10 15:28:06 -07001971 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00001972 struct drm_i915_gem_object *obj;
Keith Packardcd0de032011-09-19 21:34:19 -07001973 int enable_fbc;
Jesse Barnes9c928d12010-07-23 15:20:00 -07001974
1975 DRM_DEBUG_KMS("\n");
Jesse Barnes80824002009-09-10 15:28:06 -07001976
1977 if (!i915_powersave)
1978 return;
1979
Adam Jacksonee5382a2010-04-23 11:17:39 -04001980 if (!I915_HAS_FBC(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07001981 return;
1982
Jesse Barnes80824002009-09-10 15:28:06 -07001983 /*
1984 * If FBC is already on, we just have to verify that we can
1985 * keep it that way...
1986 * Need to disable if:
Jesse Barnes9c928d12010-07-23 15:20:00 -07001987 * - more than one pipe is active
Jesse Barnes80824002009-09-10 15:28:06 -07001988 * - changing FBC params (stride, fence, mode)
1989 * - new fb is too large to fit in compressed buffer
1990 * - going to an unsupported config (interlace, pixel multiply, etc.)
1991 */
Jesse Barnes9c928d12010-07-23 15:20:00 -07001992 list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head) {
Chris Wilsond2102462011-01-24 17:43:27 +00001993 if (tmp_crtc->enabled && tmp_crtc->fb) {
Chris Wilsonbed4a672010-09-11 10:47:47 +01001994 if (crtc) {
1995 DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
1996 dev_priv->no_fbc_reason = FBC_MULTIPLE_PIPES;
1997 goto out_disable;
1998 }
1999 crtc = tmp_crtc;
2000 }
Jesse Barnes9c928d12010-07-23 15:20:00 -07002001 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01002002
2003 if (!crtc || crtc->fb == NULL) {
2004 DRM_DEBUG_KMS("no output, disabling\n");
2005 dev_priv->no_fbc_reason = FBC_NO_OUTPUT;
Jesse Barnes9c928d12010-07-23 15:20:00 -07002006 goto out_disable;
2007 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01002008
2009 intel_crtc = to_intel_crtc(crtc);
2010 fb = crtc->fb;
2011 intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00002012 obj = intel_fb->obj;
Chris Wilsonbed4a672010-09-11 10:47:47 +01002013
Keith Packardcd0de032011-09-19 21:34:19 -07002014 enable_fbc = i915_enable_fbc;
2015 if (enable_fbc < 0) {
2016 DRM_DEBUG_KMS("fbc set to per-chip default\n");
2017 enable_fbc = 1;
Chris Wilsond56d8b22011-11-08 23:17:34 +00002018 if (INTEL_INFO(dev)->gen <= 6)
Keith Packardcd0de032011-09-19 21:34:19 -07002019 enable_fbc = 0;
2020 }
2021 if (!enable_fbc) {
2022 DRM_DEBUG_KMS("fbc disabled per module param\n");
Jesse Barnesc1a9f042011-05-05 15:24:21 -07002023 dev_priv->no_fbc_reason = FBC_MODULE_PARAM;
2024 goto out_disable;
2025 }
Chris Wilson05394f32010-11-08 19:18:58 +00002026 if (intel_fb->obj->base.size > dev_priv->cfb_size) {
Zhao Yakui28c97732009-10-09 11:39:41 +08002027 DRM_DEBUG_KMS("framebuffer too large, disabling "
Chris Wilson5eddb702010-09-11 13:48:45 +01002028 "compression\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08002029 dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
Jesse Barnes80824002009-09-10 15:28:06 -07002030 goto out_disable;
2031 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01002032 if ((crtc->mode.flags & DRM_MODE_FLAG_INTERLACE) ||
2033 (crtc->mode.flags & DRM_MODE_FLAG_DBLSCAN)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08002034 DRM_DEBUG_KMS("mode incompatible with compression, "
Chris Wilson5eddb702010-09-11 13:48:45 +01002035 "disabling\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08002036 dev_priv->no_fbc_reason = FBC_UNSUPPORTED_MODE;
Jesse Barnes80824002009-09-10 15:28:06 -07002037 goto out_disable;
2038 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01002039 if ((crtc->mode.hdisplay > 2048) ||
2040 (crtc->mode.vdisplay > 1536)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08002041 DRM_DEBUG_KMS("mode too large for compression, disabling\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08002042 dev_priv->no_fbc_reason = FBC_MODE_TOO_LARGE;
Jesse Barnes80824002009-09-10 15:28:06 -07002043 goto out_disable;
2044 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01002045 if ((IS_I915GM(dev) || IS_I945GM(dev)) && intel_crtc->plane != 0) {
Zhao Yakui28c97732009-10-09 11:39:41 +08002046 DRM_DEBUG_KMS("plane not 0, disabling compression\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08002047 dev_priv->no_fbc_reason = FBC_BAD_PLANE;
Jesse Barnes80824002009-09-10 15:28:06 -07002048 goto out_disable;
2049 }
Chris Wilsonde568512011-07-08 12:22:39 +01002050
2051 /* The use of a CPU fence is mandatory in order to detect writes
2052 * by the CPU to the scanout and trigger updates to the FBC.
2053 */
2054 if (obj->tiling_mode != I915_TILING_X ||
2055 obj->fence_reg == I915_FENCE_REG_NONE) {
2056 DRM_DEBUG_KMS("framebuffer not tiled or fenced, disabling compression\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08002057 dev_priv->no_fbc_reason = FBC_NOT_TILED;
Jesse Barnes80824002009-09-10 15:28:06 -07002058 goto out_disable;
2059 }
2060
Jason Wesselc924b932010-08-05 09:22:32 -05002061 /* If the kernel debugger is active, always disable compression */
2062 if (in_dbg_master())
2063 goto out_disable;
2064
Chris Wilson016b9b62011-07-08 12:22:43 +01002065 /* If the scanout has not changed, don't modify the FBC settings.
2066 * Note that we make the fundamental assumption that the fb->obj
2067 * cannot be unpinned (and have its GTT offset and fence revoked)
2068 * without first being decoupled from the scanout and FBC disabled.
2069 */
2070 if (dev_priv->cfb_plane == intel_crtc->plane &&
2071 dev_priv->cfb_fb == fb->base.id &&
2072 dev_priv->cfb_y == crtc->y)
2073 return;
2074
2075 if (intel_fbc_enabled(dev)) {
2076 /* We update FBC along two paths, after changing fb/crtc
2077 * configuration (modeswitching) and after page-flipping
2078 * finishes. For the latter, we know that not only did
2079 * we disable the FBC at the start of the page-flip
2080 * sequence, but also more than one vblank has passed.
2081 *
2082 * For the former case of modeswitching, it is possible
2083 * to switch between two FBC valid configurations
2084 * instantaneously so we do need to disable the FBC
2085 * before we can modify its control registers. We also
2086 * have to wait for the next vblank for that to take
2087 * effect. However, since we delay enabling FBC we can
2088 * assume that a vblank has passed since disabling and
2089 * that we can safely alter the registers in the deferred
2090 * callback.
2091 *
2092 * In the scenario that we go from a valid to invalid
2093 * and then back to valid FBC configuration we have
2094 * no strict enforcement that a vblank occurred since
2095 * disabling the FBC. However, along all current pipe
2096 * disabling paths we do need to wait for a vblank at
2097 * some point. And we wait before enabling FBC anyway.
2098 */
2099 DRM_DEBUG_KMS("disabling active FBC for update\n");
2100 intel_disable_fbc(dev);
2101 }
2102
Chris Wilsonbed4a672010-09-11 10:47:47 +01002103 intel_enable_fbc(crtc, 500);
Jesse Barnes80824002009-09-10 15:28:06 -07002104 return;
2105
2106out_disable:
Jesse Barnes80824002009-09-10 15:28:06 -07002107 /* Multiple disables should be harmless */
Chris Wilsona9394062010-05-27 13:18:16 +01002108 if (intel_fbc_enabled(dev)) {
2109 DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
Adam Jacksonee5382a2010-04-23 11:17:39 -04002110 intel_disable_fbc(dev);
Chris Wilsona9394062010-05-27 13:18:16 +01002111 }
Jesse Barnes80824002009-09-10 15:28:06 -07002112}
2113
Chris Wilson127bd2a2010-07-23 23:32:05 +01002114int
Chris Wilson48b956c2010-09-14 12:50:34 +01002115intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00002116 struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002117 struct intel_ring_buffer *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002118{
Chris Wilsonce453d82011-02-21 14:43:56 +00002119 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002120 u32 alignment;
2121 int ret;
2122
Chris Wilson05394f32010-11-08 19:18:58 +00002123 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002124 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01002125 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
2126 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002127 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01002128 alignment = 4 * 1024;
2129 else
2130 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002131 break;
2132 case I915_TILING_X:
2133 /* pin() will align the object as required by fence */
2134 alignment = 0;
2135 break;
2136 case I915_TILING_Y:
2137 /* FIXME: Is this true? */
2138 DRM_ERROR("Y tiled not allowed for scan out buffers\n");
2139 return -EINVAL;
2140 default:
2141 BUG();
2142 }
2143
Chris Wilsonce453d82011-02-21 14:43:56 +00002144 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01002145 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01002146 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002147 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002148
2149 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2150 * fence, whereas 965+ only requires a fence if using
2151 * framebuffer compression. For simplicity, we always install
2152 * a fence as the cost is not that onerous.
2153 */
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002154 ret = i915_gem_object_get_fence(obj, pipelined);
2155 if (ret)
2156 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002157
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002158 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002159
Chris Wilsonce453d82011-02-21 14:43:56 +00002160 dev_priv->mm.interruptible = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002161 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002162
2163err_unpin:
2164 i915_gem_object_unpin(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00002165err_interruptible:
2166 dev_priv->mm.interruptible = true;
Chris Wilson48b956c2010-09-14 12:50:34 +01002167 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002168}
2169
Chris Wilson1690e1e2011-12-14 13:57:08 +01002170void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
2171{
2172 i915_gem_object_unpin_fence(obj);
2173 i915_gem_object_unpin(obj);
2174}
2175
Jesse Barnes17638cd2011-06-24 12:19:23 -07002176static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2177 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002178{
2179 struct drm_device *dev = crtc->dev;
2180 struct drm_i915_private *dev_priv = dev->dev_private;
2181 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2182 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00002183 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002184 int plane = intel_crtc->plane;
2185 unsigned long Start, Offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002186 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01002187 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07002188
2189 switch (plane) {
2190 case 0:
2191 case 1:
2192 break;
2193 default:
2194 DRM_ERROR("Can't update plane %d in SAREA\n", plane);
2195 return -EINVAL;
2196 }
2197
2198 intel_fb = to_intel_framebuffer(fb);
2199 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002200
Chris Wilson5eddb702010-09-11 13:48:45 +01002201 reg = DSPCNTR(plane);
2202 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002203 /* Mask out pixel format bits in case we change it */
2204 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
2205 switch (fb->bits_per_pixel) {
2206 case 8:
2207 dspcntr |= DISPPLANE_8BPP;
2208 break;
2209 case 16:
2210 if (fb->depth == 15)
2211 dspcntr |= DISPPLANE_15_16BPP;
2212 else
2213 dspcntr |= DISPPLANE_16BPP;
2214 break;
2215 case 24:
2216 case 32:
2217 dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
2218 break;
2219 default:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002220 DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
Jesse Barnes81255562010-08-02 12:07:50 -07002221 return -EINVAL;
2222 }
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002223 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00002224 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07002225 dspcntr |= DISPPLANE_TILED;
2226 else
2227 dspcntr &= ~DISPPLANE_TILED;
2228 }
2229
Chris Wilson5eddb702010-09-11 13:48:45 +01002230 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07002231
Chris Wilson05394f32010-11-08 19:18:58 +00002232 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 Barnes81255562010-08-02 12:07:50 -07002234
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002235 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]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002238 if (INTEL_INFO(dev)->gen >= 4) {
Armin Reese446f2542012-03-30 16:20:16 -07002239 I915_MODIFY_DISPBASE(DSPSURF(plane), Start);
Chris Wilson5eddb702010-09-11 13:48:45 +01002240 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2241 I915_WRITE(DSPADDR(plane), Offset);
2242 } else
2243 I915_WRITE(DSPADDR(plane), Start + Offset);
2244 POSTING_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002245
Jesse Barnes17638cd2011-06-24 12:19:23 -07002246 return 0;
2247}
2248
2249static int ironlake_update_plane(struct drm_crtc *crtc,
2250 struct drm_framebuffer *fb, int x, int y)
2251{
2252 struct drm_device *dev = crtc->dev;
2253 struct drm_i915_private *dev_priv = dev->dev_private;
2254 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2255 struct intel_framebuffer *intel_fb;
2256 struct drm_i915_gem_object *obj;
2257 int plane = intel_crtc->plane;
2258 unsigned long Start, Offset;
2259 u32 dspcntr;
2260 u32 reg;
2261
2262 switch (plane) {
2263 case 0:
2264 case 1:
Jesse Barnes27f82272011-09-02 12:54:37 -07002265 case 2:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002266 break;
2267 default:
2268 DRM_ERROR("Can't update plane %d in SAREA\n", plane);
2269 return -EINVAL;
2270 }
2271
2272 intel_fb = to_intel_framebuffer(fb);
2273 obj = intel_fb->obj;
2274
2275 reg = DSPCNTR(plane);
2276 dspcntr = I915_READ(reg);
2277 /* Mask out pixel format bits in case we change it */
2278 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
2279 switch (fb->bits_per_pixel) {
2280 case 8:
2281 dspcntr |= DISPPLANE_8BPP;
2282 break;
2283 case 16:
2284 if (fb->depth != 16)
2285 return -EINVAL;
2286
2287 dspcntr |= DISPPLANE_16BPP;
2288 break;
2289 case 24:
2290 case 32:
2291 if (fb->depth == 24)
2292 dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
2293 else if (fb->depth == 30)
2294 dspcntr |= DISPPLANE_32BPP_30BIT_NO_ALPHA;
2295 else
2296 return -EINVAL;
2297 break;
2298 default:
2299 DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
2300 return -EINVAL;
2301 }
2302
2303 if (obj->tiling_mode != I915_TILING_NONE)
2304 dspcntr |= DISPPLANE_TILED;
2305 else
2306 dspcntr &= ~DISPPLANE_TILED;
2307
2308 /* must disable */
2309 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2310
2311 I915_WRITE(reg, dspcntr);
2312
2313 Start = obj->gtt_offset;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002314 Offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002315
2316 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002317 Start, Offset, x, y, fb->pitches[0]);
2318 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Armin Reese446f2542012-03-30 16:20:16 -07002319 I915_MODIFY_DISPBASE(DSPSURF(plane), Start);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002320 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2321 I915_WRITE(DSPADDR(plane), Offset);
2322 POSTING_READ(reg);
2323
2324 return 0;
2325}
2326
2327/* Assume fb object is pinned & idle & fenced and just update base pointers */
2328static int
2329intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2330 int x, int y, enum mode_set_atomic state)
2331{
2332 struct drm_device *dev = crtc->dev;
2333 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002334
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002335 if (dev_priv->display.disable_fbc)
2336 dev_priv->display.disable_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02002337 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07002338
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002339 return dev_priv->display.update_plane(crtc, fb, x, y);
Jesse Barnes81255562010-08-02 12:07:50 -07002340}
2341
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002342static int
Chris Wilson14667a42012-04-03 17:58:35 +01002343intel_finish_fb(struct drm_framebuffer *old_fb)
2344{
2345 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2346 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2347 bool was_interruptible = dev_priv->mm.interruptible;
2348 int ret;
2349
2350 wait_event(dev_priv->pending_flip_queue,
2351 atomic_read(&dev_priv->mm.wedged) ||
2352 atomic_read(&obj->pending_flip) == 0);
2353
2354 /* Big Hammer, we also need to ensure that any pending
2355 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2356 * current scanout is retired before unpinning the old
2357 * framebuffer.
2358 *
2359 * This should only fail upon a hung GPU, in which case we
2360 * can safely continue.
2361 */
2362 dev_priv->mm.interruptible = false;
2363 ret = i915_gem_object_finish_gpu(obj);
2364 dev_priv->mm.interruptible = was_interruptible;
2365
2366 return ret;
2367}
2368
2369static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002370intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
2371 struct drm_framebuffer *old_fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002372{
2373 struct drm_device *dev = crtc->dev;
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002374 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08002375 struct drm_i915_master_private *master_priv;
2376 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002377 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002378
2379 /* no fb bound */
2380 if (!crtc->fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002381 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002382 return 0;
2383 }
2384
Chris Wilson265db952010-09-20 15:41:01 +01002385 switch (intel_crtc->plane) {
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002386 case 0:
2387 case 1:
2388 break;
Jesse Barnes27f82272011-09-02 12:54:37 -07002389 case 2:
2390 if (IS_IVYBRIDGE(dev))
2391 break;
2392 /* fall through otherwise */
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002393 default:
Jesse Barnesa5071c22011-07-19 15:38:56 -07002394 DRM_ERROR("no plane for crtc\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002395 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002396 }
2397
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002398 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01002399 ret = intel_pin_and_fence_fb_obj(dev,
2400 to_intel_framebuffer(crtc->fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002401 NULL);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002402 if (ret != 0) {
2403 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002404 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002405 return ret;
2406 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002407
Chris Wilson14667a42012-04-03 17:58:35 +01002408 if (old_fb)
2409 intel_finish_fb(old_fb);
Chris Wilson265db952010-09-20 15:41:01 +01002410
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002411 ret = dev_priv->display.update_plane(crtc, crtc->fb, x, y);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002412 if (ret) {
Chris Wilson1690e1e2011-12-14 13:57:08 +01002413 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002414 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002415 DRM_ERROR("failed to update base address\n");
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002416 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002417 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002418
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002419 if (old_fb) {
2420 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002421 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002422 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002423
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002424 intel_update_fbc(dev);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002425 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002426
2427 if (!dev->primary->master)
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002428 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002429
2430 master_priv = dev->primary->master->driver_priv;
2431 if (!master_priv->sarea_priv)
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002432 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002433
Chris Wilson265db952010-09-20 15:41:01 +01002434 if (intel_crtc->pipe) {
Jesse Barnes79e53942008-11-07 14:24:08 -08002435 master_priv->sarea_priv->pipeB_x = x;
2436 master_priv->sarea_priv->pipeB_y = y;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002437 } else {
2438 master_priv->sarea_priv->pipeA_x = x;
2439 master_priv->sarea_priv->pipeA_y = y;
Jesse Barnes79e53942008-11-07 14:24:08 -08002440 }
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002441
2442 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002443}
2444
Chris Wilson5eddb702010-09-11 13:48:45 +01002445static void ironlake_set_pll_edp(struct drm_crtc *crtc, int clock)
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002446{
2447 struct drm_device *dev = crtc->dev;
2448 struct drm_i915_private *dev_priv = dev->dev_private;
2449 u32 dpa_ctl;
2450
Zhao Yakui28c97732009-10-09 11:39:41 +08002451 DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", clock);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002452 dpa_ctl = I915_READ(DP_A);
2453 dpa_ctl &= ~DP_PLL_FREQ_MASK;
2454
2455 if (clock < 200000) {
2456 u32 temp;
2457 dpa_ctl |= DP_PLL_FREQ_160MHZ;
2458 /* workaround for 160Mhz:
2459 1) program 0x4600c bits 15:0 = 0x8124
2460 2) program 0x46010 bit 0 = 1
2461 3) program 0x46034 bit 24 = 1
2462 4) program 0x64000 bit 14 = 1
2463 */
2464 temp = I915_READ(0x4600c);
2465 temp &= 0xffff0000;
2466 I915_WRITE(0x4600c, temp | 0x8124);
2467
2468 temp = I915_READ(0x46010);
2469 I915_WRITE(0x46010, temp | 1);
2470
2471 temp = I915_READ(0x46034);
2472 I915_WRITE(0x46034, temp | (1 << 24));
2473 } else {
2474 dpa_ctl |= DP_PLL_FREQ_270MHZ;
2475 }
2476 I915_WRITE(DP_A, dpa_ctl);
2477
Chris Wilson5eddb702010-09-11 13:48:45 +01002478 POSTING_READ(DP_A);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002479 udelay(500);
2480}
2481
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002482static void intel_fdi_normal_train(struct drm_crtc *crtc)
2483{
2484 struct drm_device *dev = crtc->dev;
2485 struct drm_i915_private *dev_priv = dev->dev_private;
2486 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2487 int pipe = intel_crtc->pipe;
2488 u32 reg, temp;
2489
2490 /* enable normal train */
2491 reg = FDI_TX_CTL(pipe);
2492 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002493 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002494 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2495 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002496 } else {
2497 temp &= ~FDI_LINK_TRAIN_NONE;
2498 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002499 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002500 I915_WRITE(reg, temp);
2501
2502 reg = FDI_RX_CTL(pipe);
2503 temp = I915_READ(reg);
2504 if (HAS_PCH_CPT(dev)) {
2505 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2506 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2507 } else {
2508 temp &= ~FDI_LINK_TRAIN_NONE;
2509 temp |= FDI_LINK_TRAIN_NONE;
2510 }
2511 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2512
2513 /* wait one idle pattern time */
2514 POSTING_READ(reg);
2515 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002516
2517 /* IVB wants error correction enabled */
2518 if (IS_IVYBRIDGE(dev))
2519 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2520 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002521}
2522
Jesse Barnes291427f2011-07-29 12:42:37 -07002523static void cpt_phase_pointer_enable(struct drm_device *dev, int pipe)
2524{
2525 struct drm_i915_private *dev_priv = dev->dev_private;
2526 u32 flags = I915_READ(SOUTH_CHICKEN1);
2527
2528 flags |= FDI_PHASE_SYNC_OVR(pipe);
2529 I915_WRITE(SOUTH_CHICKEN1, flags); /* once to unlock... */
2530 flags |= FDI_PHASE_SYNC_EN(pipe);
2531 I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to enable */
2532 POSTING_READ(SOUTH_CHICKEN1);
2533}
2534
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002535/* The FDI link training functions for ILK/Ibexpeak. */
2536static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2537{
2538 struct drm_device *dev = crtc->dev;
2539 struct drm_i915_private *dev_priv = dev->dev_private;
2540 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2541 int pipe = intel_crtc->pipe;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002542 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002543 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002544
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002545 /* FDI needs bits from pipe & plane first */
2546 assert_pipe_enabled(dev_priv, pipe);
2547 assert_plane_enabled(dev_priv, plane);
2548
Adam Jacksone1a44742010-06-25 15:32:14 -04002549 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2550 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002551 reg = FDI_RX_IMR(pipe);
2552 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002553 temp &= ~FDI_RX_SYMBOL_LOCK;
2554 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002555 I915_WRITE(reg, temp);
2556 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002557 udelay(150);
2558
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002559 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002560 reg = FDI_TX_CTL(pipe);
2561 temp = I915_READ(reg);
Adam Jackson77ffb592010-04-12 11:38:44 -04002562 temp &= ~(7 << 19);
2563 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002564 temp &= ~FDI_LINK_TRAIN_NONE;
2565 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002566 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002567
Chris Wilson5eddb702010-09-11 13:48:45 +01002568 reg = FDI_RX_CTL(pipe);
2569 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002570 temp &= ~FDI_LINK_TRAIN_NONE;
2571 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002572 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2573
2574 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002575 udelay(150);
2576
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002577 /* Ironlake workaround, enable clock pointer after FDI enable*/
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002578 if (HAS_PCH_IBX(dev)) {
2579 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2580 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2581 FDI_RX_PHASE_SYNC_POINTER_EN);
2582 }
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002583
Chris Wilson5eddb702010-09-11 13:48:45 +01002584 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002585 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002586 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002587 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2588
2589 if ((temp & FDI_RX_BIT_LOCK)) {
2590 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002591 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002592 break;
2593 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002594 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002595 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002596 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002597
2598 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002599 reg = FDI_TX_CTL(pipe);
2600 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002601 temp &= ~FDI_LINK_TRAIN_NONE;
2602 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002603 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002604
Chris Wilson5eddb702010-09-11 13:48:45 +01002605 reg = FDI_RX_CTL(pipe);
2606 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002607 temp &= ~FDI_LINK_TRAIN_NONE;
2608 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002609 I915_WRITE(reg, temp);
2610
2611 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002612 udelay(150);
2613
Chris Wilson5eddb702010-09-11 13:48:45 +01002614 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002615 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002616 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002617 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2618
2619 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002620 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002621 DRM_DEBUG_KMS("FDI train 2 done.\n");
2622 break;
2623 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002624 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002625 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002626 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002627
2628 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002629
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002630}
2631
Akshay Joshi0206e352011-08-16 15:34:10 -04002632static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002633 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2634 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2635 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2636 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2637};
2638
2639/* The FDI link training functions for SNB/Cougarpoint. */
2640static void gen6_fdi_link_train(struct drm_crtc *crtc)
2641{
2642 struct drm_device *dev = crtc->dev;
2643 struct drm_i915_private *dev_priv = dev->dev_private;
2644 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2645 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05002646 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002647
Adam Jacksone1a44742010-06-25 15:32:14 -04002648 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2649 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002650 reg = FDI_RX_IMR(pipe);
2651 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002652 temp &= ~FDI_RX_SYMBOL_LOCK;
2653 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002654 I915_WRITE(reg, temp);
2655
2656 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002657 udelay(150);
2658
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002659 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002660 reg = FDI_TX_CTL(pipe);
2661 temp = I915_READ(reg);
Adam Jackson77ffb592010-04-12 11:38:44 -04002662 temp &= ~(7 << 19);
2663 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002664 temp &= ~FDI_LINK_TRAIN_NONE;
2665 temp |= FDI_LINK_TRAIN_PATTERN_1;
2666 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2667 /* SNB-B */
2668 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002669 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002670
Chris Wilson5eddb702010-09-11 13:48:45 +01002671 reg = FDI_RX_CTL(pipe);
2672 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002673 if (HAS_PCH_CPT(dev)) {
2674 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2675 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2676 } else {
2677 temp &= ~FDI_LINK_TRAIN_NONE;
2678 temp |= FDI_LINK_TRAIN_PATTERN_1;
2679 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002680 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2681
2682 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002683 udelay(150);
2684
Jesse Barnes291427f2011-07-29 12:42:37 -07002685 if (HAS_PCH_CPT(dev))
2686 cpt_phase_pointer_enable(dev, pipe);
2687
Akshay Joshi0206e352011-08-16 15:34:10 -04002688 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002689 reg = FDI_TX_CTL(pipe);
2690 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002691 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2692 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002693 I915_WRITE(reg, temp);
2694
2695 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002696 udelay(500);
2697
Sean Paulfa37d392012-03-02 12:53:39 -05002698 for (retry = 0; retry < 5; retry++) {
2699 reg = FDI_RX_IIR(pipe);
2700 temp = I915_READ(reg);
2701 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2702 if (temp & FDI_RX_BIT_LOCK) {
2703 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2704 DRM_DEBUG_KMS("FDI train 1 done.\n");
2705 break;
2706 }
2707 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002708 }
Sean Paulfa37d392012-03-02 12:53:39 -05002709 if (retry < 5)
2710 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002711 }
2712 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002713 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002714
2715 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002716 reg = FDI_TX_CTL(pipe);
2717 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002718 temp &= ~FDI_LINK_TRAIN_NONE;
2719 temp |= FDI_LINK_TRAIN_PATTERN_2;
2720 if (IS_GEN6(dev)) {
2721 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2722 /* SNB-B */
2723 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2724 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002725 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002726
Chris Wilson5eddb702010-09-11 13:48:45 +01002727 reg = FDI_RX_CTL(pipe);
2728 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002729 if (HAS_PCH_CPT(dev)) {
2730 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2731 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2732 } else {
2733 temp &= ~FDI_LINK_TRAIN_NONE;
2734 temp |= FDI_LINK_TRAIN_PATTERN_2;
2735 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002736 I915_WRITE(reg, temp);
2737
2738 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002739 udelay(150);
2740
Akshay Joshi0206e352011-08-16 15:34:10 -04002741 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002742 reg = FDI_TX_CTL(pipe);
2743 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002744 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2745 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002746 I915_WRITE(reg, temp);
2747
2748 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002749 udelay(500);
2750
Sean Paulfa37d392012-03-02 12:53:39 -05002751 for (retry = 0; retry < 5; retry++) {
2752 reg = FDI_RX_IIR(pipe);
2753 temp = I915_READ(reg);
2754 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2755 if (temp & FDI_RX_SYMBOL_LOCK) {
2756 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2757 DRM_DEBUG_KMS("FDI train 2 done.\n");
2758 break;
2759 }
2760 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002761 }
Sean Paulfa37d392012-03-02 12:53:39 -05002762 if (retry < 5)
2763 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002764 }
2765 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002766 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002767
2768 DRM_DEBUG_KMS("FDI train done.\n");
2769}
2770
Jesse Barnes357555c2011-04-28 15:09:55 -07002771/* Manual link training for Ivy Bridge A0 parts */
2772static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2773{
2774 struct drm_device *dev = crtc->dev;
2775 struct drm_i915_private *dev_priv = dev->dev_private;
2776 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2777 int pipe = intel_crtc->pipe;
2778 u32 reg, temp, i;
2779
2780 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2781 for train result */
2782 reg = FDI_RX_IMR(pipe);
2783 temp = I915_READ(reg);
2784 temp &= ~FDI_RX_SYMBOL_LOCK;
2785 temp &= ~FDI_RX_BIT_LOCK;
2786 I915_WRITE(reg, temp);
2787
2788 POSTING_READ(reg);
2789 udelay(150);
2790
2791 /* enable CPU FDI TX and PCH FDI RX */
2792 reg = FDI_TX_CTL(pipe);
2793 temp = I915_READ(reg);
2794 temp &= ~(7 << 19);
2795 temp |= (intel_crtc->fdi_lanes - 1) << 19;
2796 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2797 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
2798 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2799 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002800 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002801 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2802
2803 reg = FDI_RX_CTL(pipe);
2804 temp = I915_READ(reg);
2805 temp &= ~FDI_LINK_TRAIN_AUTO;
2806 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2807 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002808 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002809 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2810
2811 POSTING_READ(reg);
2812 udelay(150);
2813
Jesse Barnes291427f2011-07-29 12:42:37 -07002814 if (HAS_PCH_CPT(dev))
2815 cpt_phase_pointer_enable(dev, pipe);
2816
Akshay Joshi0206e352011-08-16 15:34:10 -04002817 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002818 reg = FDI_TX_CTL(pipe);
2819 temp = I915_READ(reg);
2820 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2821 temp |= snb_b_fdi_train_param[i];
2822 I915_WRITE(reg, temp);
2823
2824 POSTING_READ(reg);
2825 udelay(500);
2826
2827 reg = FDI_RX_IIR(pipe);
2828 temp = I915_READ(reg);
2829 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2830
2831 if (temp & FDI_RX_BIT_LOCK ||
2832 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2833 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2834 DRM_DEBUG_KMS("FDI train 1 done.\n");
2835 break;
2836 }
2837 }
2838 if (i == 4)
2839 DRM_ERROR("FDI train 1 fail!\n");
2840
2841 /* Train 2 */
2842 reg = FDI_TX_CTL(pipe);
2843 temp = I915_READ(reg);
2844 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2845 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2846 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2847 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2848 I915_WRITE(reg, temp);
2849
2850 reg = FDI_RX_CTL(pipe);
2851 temp = I915_READ(reg);
2852 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2853 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2854 I915_WRITE(reg, temp);
2855
2856 POSTING_READ(reg);
2857 udelay(150);
2858
Akshay Joshi0206e352011-08-16 15:34:10 -04002859 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002860 reg = FDI_TX_CTL(pipe);
2861 temp = I915_READ(reg);
2862 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2863 temp |= snb_b_fdi_train_param[i];
2864 I915_WRITE(reg, temp);
2865
2866 POSTING_READ(reg);
2867 udelay(500);
2868
2869 reg = FDI_RX_IIR(pipe);
2870 temp = I915_READ(reg);
2871 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2872
2873 if (temp & FDI_RX_SYMBOL_LOCK) {
2874 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2875 DRM_DEBUG_KMS("FDI train 2 done.\n");
2876 break;
2877 }
2878 }
2879 if (i == 4)
2880 DRM_ERROR("FDI train 2 fail!\n");
2881
2882 DRM_DEBUG_KMS("FDI train done.\n");
2883}
2884
2885static void ironlake_fdi_pll_enable(struct drm_crtc *crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07002886{
2887 struct drm_device *dev = crtc->dev;
2888 struct drm_i915_private *dev_priv = dev->dev_private;
2889 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2890 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002891 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002892
Jesse Barnesc64e3112010-09-10 11:27:03 -07002893 /* Write the TU size bits so error detection works */
Chris Wilson5eddb702010-09-11 13:48:45 +01002894 I915_WRITE(FDI_RX_TUSIZE1(pipe),
2895 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
Jesse Barnesc64e3112010-09-10 11:27:03 -07002896
Jesse Barnes0e23b992010-09-10 11:10:00 -07002897 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01002898 reg = FDI_RX_CTL(pipe);
2899 temp = I915_READ(reg);
2900 temp &= ~((0x7 << 19) | (0x7 << 16));
Jesse Barnes0e23b992010-09-10 11:10:00 -07002901 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Chris Wilson5eddb702010-09-11 13:48:45 +01002902 temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2903 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2904
2905 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002906 udelay(200);
2907
2908 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002909 temp = I915_READ(reg);
2910 I915_WRITE(reg, temp | FDI_PCDCLK);
2911
2912 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002913 udelay(200);
2914
2915 /* Enable CPU FDI TX PLL, always on for Ironlake */
Chris Wilson5eddb702010-09-11 13:48:45 +01002916 reg = FDI_TX_CTL(pipe);
2917 temp = I915_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002918 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002919 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
2920
2921 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002922 udelay(100);
2923 }
2924}
2925
Jesse Barnes291427f2011-07-29 12:42:37 -07002926static void cpt_phase_pointer_disable(struct drm_device *dev, int pipe)
2927{
2928 struct drm_i915_private *dev_priv = dev->dev_private;
2929 u32 flags = I915_READ(SOUTH_CHICKEN1);
2930
2931 flags &= ~(FDI_PHASE_SYNC_EN(pipe));
2932 I915_WRITE(SOUTH_CHICKEN1, flags); /* once to disable... */
2933 flags &= ~(FDI_PHASE_SYNC_OVR(pipe));
2934 I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to lock */
2935 POSTING_READ(SOUTH_CHICKEN1);
2936}
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002937static void ironlake_fdi_disable(struct drm_crtc *crtc)
2938{
2939 struct drm_device *dev = crtc->dev;
2940 struct drm_i915_private *dev_priv = dev->dev_private;
2941 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2942 int pipe = intel_crtc->pipe;
2943 u32 reg, temp;
2944
2945 /* disable CPU FDI tx and PCH FDI rx */
2946 reg = FDI_TX_CTL(pipe);
2947 temp = I915_READ(reg);
2948 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2949 POSTING_READ(reg);
2950
2951 reg = FDI_RX_CTL(pipe);
2952 temp = I915_READ(reg);
2953 temp &= ~(0x7 << 16);
2954 temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2955 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2956
2957 POSTING_READ(reg);
2958 udelay(100);
2959
2960 /* Ironlake workaround, disable clock pointer after downing FDI */
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002961 if (HAS_PCH_IBX(dev)) {
2962 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002963 I915_WRITE(FDI_RX_CHICKEN(pipe),
2964 I915_READ(FDI_RX_CHICKEN(pipe) &
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002965 ~FDI_RX_PHASE_SYNC_POINTER_EN));
Jesse Barnes291427f2011-07-29 12:42:37 -07002966 } else if (HAS_PCH_CPT(dev)) {
2967 cpt_phase_pointer_disable(dev, pipe);
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002968 }
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002969
2970 /* still set train pattern 1 */
2971 reg = FDI_TX_CTL(pipe);
2972 temp = I915_READ(reg);
2973 temp &= ~FDI_LINK_TRAIN_NONE;
2974 temp |= FDI_LINK_TRAIN_PATTERN_1;
2975 I915_WRITE(reg, temp);
2976
2977 reg = FDI_RX_CTL(pipe);
2978 temp = I915_READ(reg);
2979 if (HAS_PCH_CPT(dev)) {
2980 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2981 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2982 } else {
2983 temp &= ~FDI_LINK_TRAIN_NONE;
2984 temp |= FDI_LINK_TRAIN_PATTERN_1;
2985 }
2986 /* BPC in FDI rx is consistent with that in PIPECONF */
2987 temp &= ~(0x07 << 16);
2988 temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2989 I915_WRITE(reg, temp);
2990
2991 POSTING_READ(reg);
2992 udelay(100);
2993}
2994
Chris Wilson6b383a72010-09-13 13:54:26 +01002995/*
2996 * When we disable a pipe, we need to clear any pending scanline wait events
2997 * to avoid hanging the ring, which we assume we are waiting on.
2998 */
2999static void intel_clear_scanline_wait(struct drm_device *dev)
3000{
3001 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson8168bd42010-11-11 17:54:52 +00003002 struct intel_ring_buffer *ring;
Chris Wilson6b383a72010-09-13 13:54:26 +01003003 u32 tmp;
3004
3005 if (IS_GEN2(dev))
3006 /* Can't break the hang on i8xx */
3007 return;
3008
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003009 ring = LP_RING(dev_priv);
Chris Wilson8168bd42010-11-11 17:54:52 +00003010 tmp = I915_READ_CTL(ring);
3011 if (tmp & RING_WAIT)
3012 I915_WRITE_CTL(ring, tmp);
Chris Wilson6b383a72010-09-13 13:54:26 +01003013}
3014
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003015static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
3016{
Chris Wilson0f911282012-04-17 10:05:38 +01003017 struct drm_device *dev = crtc->dev;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003018
3019 if (crtc->fb == NULL)
3020 return;
3021
Chris Wilson0f911282012-04-17 10:05:38 +01003022 mutex_lock(&dev->struct_mutex);
3023 intel_finish_fb(crtc->fb);
3024 mutex_unlock(&dev->struct_mutex);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003025}
3026
Jesse Barnes040484a2011-01-03 12:14:26 -08003027static bool intel_crtc_driving_pch(struct drm_crtc *crtc)
3028{
3029 struct drm_device *dev = crtc->dev;
3030 struct drm_mode_config *mode_config = &dev->mode_config;
3031 struct intel_encoder *encoder;
3032
3033 /*
3034 * If there's a non-PCH eDP on this crtc, it must be DP_A, and that
3035 * must be driven by its own crtc; no sharing is possible.
3036 */
3037 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
3038 if (encoder->base.crtc != crtc)
3039 continue;
3040
3041 switch (encoder->type) {
3042 case INTEL_OUTPUT_EDP:
3043 if (!intel_encoder_is_pch_edp(&encoder->base))
3044 return false;
3045 continue;
3046 }
3047 }
3048
3049 return true;
3050}
3051
Jesse Barnesf67a5592011-01-05 10:31:48 -08003052/*
3053 * Enable PCH resources required for PCH ports:
3054 * - PCH PLLs
3055 * - FDI training & RX/TX
3056 * - update transcoder timings
3057 * - DP transcoding bits
3058 * - transcoder
3059 */
3060static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003061{
3062 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003063 struct drm_i915_private *dev_priv = dev->dev_private;
3064 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3065 int pipe = intel_crtc->pipe;
Jesse Barnes4b645f12011-10-12 09:51:31 -07003066 u32 reg, temp, transc_sel;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003067
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003068 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07003069 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003070
Jesse Barnes92f25842011-01-04 15:09:34 -08003071 intel_enable_pch_pll(dev_priv, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003072
3073 if (HAS_PCH_CPT(dev)) {
Jesse Barnes4b645f12011-10-12 09:51:31 -07003074 transc_sel = intel_crtc->use_pll_a ? TRANSC_DPLLA_SEL :
3075 TRANSC_DPLLB_SEL;
3076
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003077 /* Be sure PCH DPLL SEL is set */
3078 temp = I915_READ(PCH_DPLL_SEL);
Jesse Barnesd64311a2011-10-12 15:01:33 -07003079 if (pipe == 0) {
3080 temp &= ~(TRANSA_DPLLB_SEL);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003081 temp |= (TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL);
Jesse Barnesd64311a2011-10-12 15:01:33 -07003082 } else if (pipe == 1) {
3083 temp &= ~(TRANSB_DPLLB_SEL);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003084 temp |= (TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
Jesse Barnesd64311a2011-10-12 15:01:33 -07003085 } else if (pipe == 2) {
3086 temp &= ~(TRANSC_DPLLB_SEL);
Jesse Barnes4b645f12011-10-12 09:51:31 -07003087 temp |= (TRANSC_DPLL_ENABLE | transc_sel);
Jesse Barnesd64311a2011-10-12 15:01:33 -07003088 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003089 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003090 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003091
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003092 /* set transcoder timing, panel must allow it */
3093 assert_panel_unlocked(dev_priv, pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01003094 I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
3095 I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
3096 I915_WRITE(TRANS_HSYNC(pipe), I915_READ(HSYNC(pipe)));
3097
3098 I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
3099 I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
3100 I915_WRITE(TRANS_VSYNC(pipe), I915_READ(VSYNC(pipe)));
Daniel Vetter0529a0d2012-01-28 14:49:24 +01003101 I915_WRITE(TRANS_VSYNCSHIFT(pipe), I915_READ(VSYNCSHIFT(pipe)));
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003102
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003103 intel_fdi_normal_train(crtc);
3104
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003105 /* For PCH DP, enable TRANS_DP_CTL */
3106 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07003107 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3108 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003109 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003110 reg = TRANS_DP_CTL(pipe);
3111 temp = I915_READ(reg);
3112 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003113 TRANS_DP_SYNC_MASK |
3114 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003115 temp |= (TRANS_DP_OUTPUT_ENABLE |
3116 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003117 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003118
3119 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003120 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003121 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003122 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003123
3124 switch (intel_trans_dp_port_sel(crtc)) {
3125 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003126 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003127 break;
3128 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003129 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003130 break;
3131 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003132 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003133 break;
3134 default:
3135 DRM_DEBUG_KMS("Wrong PCH DP port return. Guess port B\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003136 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003137 break;
3138 }
3139
Chris Wilson5eddb702010-09-11 13:48:45 +01003140 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003141 }
3142
Jesse Barnes040484a2011-01-03 12:14:26 -08003143 intel_enable_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003144}
3145
Jesse Barnesd4270e52011-10-11 10:43:02 -07003146void intel_cpt_verify_modeset(struct drm_device *dev, int pipe)
3147{
3148 struct drm_i915_private *dev_priv = dev->dev_private;
3149 int dslreg = PIPEDSL(pipe), tc2reg = TRANS_CHICKEN2(pipe);
3150 u32 temp;
3151
3152 temp = I915_READ(dslreg);
3153 udelay(500);
3154 if (wait_for(I915_READ(dslreg) != temp, 5)) {
3155 /* Without this, mode sets may fail silently on FDI */
3156 I915_WRITE(tc2reg, TRANS_AUTOTRAIN_GEN_STALL_DIS);
3157 udelay(250);
3158 I915_WRITE(tc2reg, 0);
3159 if (wait_for(I915_READ(dslreg) != temp, 5))
3160 DRM_ERROR("mode set failed: pipe %d stuck\n", pipe);
3161 }
3162}
3163
Jesse Barnesf67a5592011-01-05 10:31:48 -08003164static void ironlake_crtc_enable(struct drm_crtc *crtc)
3165{
3166 struct drm_device *dev = crtc->dev;
3167 struct drm_i915_private *dev_priv = dev->dev_private;
3168 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3169 int pipe = intel_crtc->pipe;
3170 int plane = intel_crtc->plane;
3171 u32 temp;
3172 bool is_pch_port;
3173
3174 if (intel_crtc->active)
3175 return;
3176
3177 intel_crtc->active = true;
3178 intel_update_watermarks(dev);
3179
3180 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
3181 temp = I915_READ(PCH_LVDS);
3182 if ((temp & LVDS_PORT_EN) == 0)
3183 I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
3184 }
3185
3186 is_pch_port = intel_crtc_driving_pch(crtc);
3187
3188 if (is_pch_port)
Jesse Barnes357555c2011-04-28 15:09:55 -07003189 ironlake_fdi_pll_enable(crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003190 else
3191 ironlake_fdi_disable(crtc);
3192
3193 /* Enable panel fitting for LVDS */
3194 if (dev_priv->pch_pf_size &&
3195 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) || HAS_eDP)) {
3196 /* Force use of hard-coded filter coefficients
3197 * as some pre-programmed values are broken,
3198 * e.g. x201.
3199 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003200 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3201 I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
3202 I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003203 }
3204
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02003205 /*
3206 * On ILK+ LUT must be loaded before the pipe is running but with
3207 * clocks enabled
3208 */
3209 intel_crtc_load_lut(crtc);
3210
Jesse Barnesf67a5592011-01-05 10:31:48 -08003211 intel_enable_pipe(dev_priv, pipe, is_pch_port);
3212 intel_enable_plane(dev_priv, plane, pipe);
3213
3214 if (is_pch_port)
3215 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003216
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003217 mutex_lock(&dev->struct_mutex);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003218 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003219 mutex_unlock(&dev->struct_mutex);
3220
Chris Wilson6b383a72010-09-13 13:54:26 +01003221 intel_crtc_update_cursor(crtc, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003222}
3223
3224static void ironlake_crtc_disable(struct drm_crtc *crtc)
3225{
3226 struct drm_device *dev = crtc->dev;
3227 struct drm_i915_private *dev_priv = dev->dev_private;
3228 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3229 int pipe = intel_crtc->pipe;
3230 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01003231 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003232
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003233 if (!intel_crtc->active)
3234 return;
3235
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003236 intel_crtc_wait_for_pending_flips(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003237 drm_vblank_off(dev, pipe);
Chris Wilson6b383a72010-09-13 13:54:26 +01003238 intel_crtc_update_cursor(crtc, false);
Chris Wilson5eddb702010-09-11 13:48:45 +01003239
Jesse Barnesb24e7172011-01-04 15:09:30 -08003240 intel_disable_plane(dev_priv, plane, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003241
Chris Wilson973d04f2011-07-08 12:22:37 +01003242 if (dev_priv->cfb_plane == plane)
3243 intel_disable_fbc(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003244
Jesse Barnesb24e7172011-01-04 15:09:30 -08003245 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003246
Jesse Barnes6be4a602010-09-10 10:26:01 -07003247 /* Disable PF */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003248 I915_WRITE(PF_CTL(pipe), 0);
3249 I915_WRITE(PF_WIN_SZ(pipe), 0);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003250
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003251 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003252
Jesse Barnes47a05ec2011-02-07 13:46:40 -08003253 /* This is a horrible layering violation; we should be doing this in
3254 * the connector/encoder ->prepare instead, but we don't always have
3255 * enough information there about the config to know whether it will
3256 * actually be necessary or just cause undesired flicker.
3257 */
3258 intel_disable_pch_ports(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003259
Jesse Barnes040484a2011-01-03 12:14:26 -08003260 intel_disable_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003261
Jesse Barnes6be4a602010-09-10 10:26:01 -07003262 if (HAS_PCH_CPT(dev)) {
3263 /* disable TRANS_DP_CTL */
Chris Wilson5eddb702010-09-11 13:48:45 +01003264 reg = TRANS_DP_CTL(pipe);
3265 temp = I915_READ(reg);
3266 temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
Eric Anholtcb3543c2011-02-02 12:08:07 -08003267 temp |= TRANS_DP_PORT_SEL_NONE;
Chris Wilson5eddb702010-09-11 13:48:45 +01003268 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003269
3270 /* disable DPLL_SEL */
3271 temp = I915_READ(PCH_DPLL_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003272 switch (pipe) {
3273 case 0:
Jesse Barnesd64311a2011-10-12 15:01:33 -07003274 temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003275 break;
3276 case 1:
Jesse Barnes6be4a602010-09-10 10:26:01 -07003277 temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003278 break;
3279 case 2:
Jesse Barnes4b645f12011-10-12 09:51:31 -07003280 /* C shares PLL A or B */
Jesse Barnesd64311a2011-10-12 15:01:33 -07003281 temp &= ~(TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003282 break;
3283 default:
3284 BUG(); /* wtf */
3285 }
Jesse Barnes6be4a602010-09-10 10:26:01 -07003286 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003287 }
3288
3289 /* disable PCH DPLL */
Jesse Barnes4b645f12011-10-12 09:51:31 -07003290 if (!intel_crtc->no_pll)
3291 intel_disable_pch_pll(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003292
3293 /* Switch from PCDclk to Rawclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003294 reg = FDI_RX_CTL(pipe);
3295 temp = I915_READ(reg);
3296 I915_WRITE(reg, temp & ~FDI_PCDCLK);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003297
3298 /* Disable CPU FDI TX PLL */
Chris Wilson5eddb702010-09-11 13:48:45 +01003299 reg = FDI_TX_CTL(pipe);
3300 temp = I915_READ(reg);
3301 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3302
3303 POSTING_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003304 udelay(100);
3305
Chris Wilson5eddb702010-09-11 13:48:45 +01003306 reg = FDI_RX_CTL(pipe);
3307 temp = I915_READ(reg);
3308 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003309
3310 /* Wait for the clocks to turn off. */
Chris Wilson5eddb702010-09-11 13:48:45 +01003311 POSTING_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003312 udelay(100);
Chris Wilson6b383a72010-09-13 13:54:26 +01003313
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003314 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003315 intel_update_watermarks(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003316
3317 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01003318 intel_update_fbc(dev);
3319 intel_clear_scanline_wait(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003320 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003321}
3322
3323static void ironlake_crtc_dpms(struct drm_crtc *crtc, int mode)
3324{
3325 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3326 int pipe = intel_crtc->pipe;
3327 int plane = intel_crtc->plane;
3328
Zhenyu Wang2c072452009-06-05 15:38:42 +08003329 /* XXX: When our outputs are all unaware of DPMS modes other than off
3330 * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
3331 */
3332 switch (mode) {
3333 case DRM_MODE_DPMS_ON:
3334 case DRM_MODE_DPMS_STANDBY:
3335 case DRM_MODE_DPMS_SUSPEND:
Chris Wilson868dc582010-08-07 11:01:31 +01003336 DRM_DEBUG_KMS("crtc %d/%d dpms on\n", pipe, plane);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003337 ironlake_crtc_enable(crtc);
Chris Wilson868dc582010-08-07 11:01:31 +01003338 break;
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08003339
Zhenyu Wang2c072452009-06-05 15:38:42 +08003340 case DRM_MODE_DPMS_OFF:
Chris Wilson868dc582010-08-07 11:01:31 +01003341 DRM_DEBUG_KMS("crtc %d/%d dpms off\n", pipe, plane);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003342 ironlake_crtc_disable(crtc);
Zhenyu Wang2c072452009-06-05 15:38:42 +08003343 break;
3344 }
3345}
3346
Daniel Vetter02e792f2009-09-15 22:57:34 +02003347static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3348{
Daniel Vetter02e792f2009-09-15 22:57:34 +02003349 if (!enable && intel_crtc->overlay) {
Chris Wilson23f09ce2010-08-12 13:53:37 +01003350 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00003351 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02003352
Chris Wilson23f09ce2010-08-12 13:53:37 +01003353 mutex_lock(&dev->struct_mutex);
Chris Wilsonce453d82011-02-21 14:43:56 +00003354 dev_priv->mm.interruptible = false;
3355 (void) intel_overlay_switch_off(intel_crtc->overlay);
3356 dev_priv->mm.interruptible = true;
Chris Wilson23f09ce2010-08-12 13:53:37 +01003357 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02003358 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02003359
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01003360 /* Let userspace switch the overlay on again. In most cases userspace
3361 * has to recompute where to put it anyway.
3362 */
Daniel Vetter02e792f2009-09-15 22:57:34 +02003363}
3364
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003365static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003366{
3367 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08003368 struct drm_i915_private *dev_priv = dev->dev_private;
3369 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3370 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07003371 int plane = intel_crtc->plane;
Jesse Barnes79e53942008-11-07 14:24:08 -08003372
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003373 if (intel_crtc->active)
3374 return;
3375
3376 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01003377 intel_update_watermarks(dev);
3378
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08003379 intel_enable_pll(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08003380 intel_enable_pipe(dev_priv, pipe, false);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003381 intel_enable_plane(dev_priv, plane, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003382
3383 intel_crtc_load_lut(crtc);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003384 intel_update_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003385
3386 /* Give the overlay scaler a chance to enable if it's on this pipe */
3387 intel_crtc_dpms_overlay(intel_crtc, true);
Chris Wilson6b383a72010-09-13 13:54:26 +01003388 intel_crtc_update_cursor(crtc, true);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003389}
3390
3391static void i9xx_crtc_disable(struct drm_crtc *crtc)
3392{
3393 struct drm_device *dev = crtc->dev;
3394 struct drm_i915_private *dev_priv = dev->dev_private;
3395 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3396 int pipe = intel_crtc->pipe;
3397 int plane = intel_crtc->plane;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003398
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003399 if (!intel_crtc->active)
3400 return;
3401
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003402 /* Give the overlay scaler a chance to disable if it's on this pipe */
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003403 intel_crtc_wait_for_pending_flips(crtc);
3404 drm_vblank_off(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003405 intel_crtc_dpms_overlay(intel_crtc, false);
Chris Wilson6b383a72010-09-13 13:54:26 +01003406 intel_crtc_update_cursor(crtc, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003407
Chris Wilson973d04f2011-07-08 12:22:37 +01003408 if (dev_priv->cfb_plane == plane)
3409 intel_disable_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003410
Jesse Barnesb24e7172011-01-04 15:09:30 -08003411 intel_disable_plane(dev_priv, plane, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003412 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08003413 intel_disable_pll(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003414
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003415 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003416 intel_update_fbc(dev);
3417 intel_update_watermarks(dev);
3418 intel_clear_scanline_wait(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003419}
3420
3421static void i9xx_crtc_dpms(struct drm_crtc *crtc, int mode)
3422{
Jesse Barnes79e53942008-11-07 14:24:08 -08003423 /* XXX: When our outputs are all unaware of DPMS modes other than off
3424 * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
3425 */
3426 switch (mode) {
3427 case DRM_MODE_DPMS_ON:
3428 case DRM_MODE_DPMS_STANDBY:
3429 case DRM_MODE_DPMS_SUSPEND:
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003430 i9xx_crtc_enable(crtc);
3431 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08003432 case DRM_MODE_DPMS_OFF:
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003433 i9xx_crtc_disable(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08003434 break;
3435 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08003436}
3437
3438/**
3439 * Sets the power management mode of the pipe and plane.
Zhenyu Wang2c072452009-06-05 15:38:42 +08003440 */
3441static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
3442{
3443 struct drm_device *dev = crtc->dev;
Jesse Barnese70236a2009-09-21 10:42:27 -07003444 struct drm_i915_private *dev_priv = dev->dev_private;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003445 struct drm_i915_master_private *master_priv;
3446 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3447 int pipe = intel_crtc->pipe;
3448 bool enabled;
3449
Chris Wilson032d2a02010-09-06 16:17:22 +01003450 if (intel_crtc->dpms_mode == mode)
3451 return;
3452
Chris Wilsondebcadd2010-08-07 11:01:33 +01003453 intel_crtc->dpms_mode = mode;
Chris Wilsondebcadd2010-08-07 11:01:33 +01003454
Jesse Barnese70236a2009-09-21 10:42:27 -07003455 dev_priv->display.dpms(crtc, mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08003456
3457 if (!dev->primary->master)
3458 return;
3459
3460 master_priv = dev->primary->master->driver_priv;
3461 if (!master_priv->sarea_priv)
3462 return;
3463
3464 enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
3465
3466 switch (pipe) {
3467 case 0:
3468 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
3469 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
3470 break;
3471 case 1:
3472 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
3473 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
3474 break;
3475 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003476 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08003477 break;
3478 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003479}
3480
Chris Wilsoncdd59982010-09-08 16:30:16 +01003481static void intel_crtc_disable(struct drm_crtc *crtc)
3482{
3483 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
3484 struct drm_device *dev = crtc->dev;
3485
3486 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
Chris Wilson931872f2012-01-16 23:01:13 +00003487 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
3488 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003489
3490 if (crtc->fb) {
3491 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01003492 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003493 mutex_unlock(&dev->struct_mutex);
3494 }
3495}
3496
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003497/* Prepare for a mode set.
3498 *
3499 * Note we could be a lot smarter here. We need to figure out which outputs
3500 * will be enabled, which disabled (in short, how the config will changes)
3501 * and perform the minimum necessary steps to accomplish that, e.g. updating
3502 * watermarks, FBC configuration, making sure PLLs are programmed correctly,
3503 * panel fitting is in the proper state, etc.
3504 */
3505static void i9xx_crtc_prepare(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003506{
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003507 i9xx_crtc_disable(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08003508}
3509
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003510static void i9xx_crtc_commit(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003511{
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003512 i9xx_crtc_enable(crtc);
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003513}
3514
3515static void ironlake_crtc_prepare(struct drm_crtc *crtc)
3516{
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003517 ironlake_crtc_disable(crtc);
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003518}
3519
3520static void ironlake_crtc_commit(struct drm_crtc *crtc)
3521{
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003522 ironlake_crtc_enable(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08003523}
3524
Akshay Joshi0206e352011-08-16 15:34:10 -04003525void intel_encoder_prepare(struct drm_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08003526{
3527 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
3528 /* lvds has its own version of prepare see intel_lvds_prepare */
3529 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
3530}
3531
Akshay Joshi0206e352011-08-16 15:34:10 -04003532void intel_encoder_commit(struct drm_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08003533{
3534 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
Jesse Barnesd4270e52011-10-11 10:43:02 -07003535 struct drm_device *dev = encoder->dev;
3536 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
3537 struct intel_crtc *intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
3538
Jesse Barnes79e53942008-11-07 14:24:08 -08003539 /* lvds has its own version of commit see intel_lvds_commit */
3540 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003541
3542 if (HAS_PCH_CPT(dev))
3543 intel_cpt_verify_modeset(dev, intel_crtc->pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08003544}
3545
Chris Wilsonea5b2132010-08-04 13:50:23 +01003546void intel_encoder_destroy(struct drm_encoder *encoder)
3547{
Chris Wilson4ef69c72010-09-09 15:14:28 +01003548 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01003549
Chris Wilsonea5b2132010-08-04 13:50:23 +01003550 drm_encoder_cleanup(encoder);
3551 kfree(intel_encoder);
3552}
3553
Jesse Barnes79e53942008-11-07 14:24:08 -08003554static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
3555 struct drm_display_mode *mode,
3556 struct drm_display_mode *adjusted_mode)
3557{
Zhenyu Wang2c072452009-06-05 15:38:42 +08003558 struct drm_device *dev = crtc->dev;
Chris Wilson89749352010-09-12 18:25:19 +01003559
Eric Anholtbad720f2009-10-22 16:11:14 -07003560 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08003561 /* FDI link clock is fixed at 2.7G */
Jesse Barnes2377b742010-07-07 14:06:43 -07003562 if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
3563 return false;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003564 }
Chris Wilson89749352010-09-12 18:25:19 +01003565
Daniel Vetterca9bfa72012-01-28 14:49:20 +01003566 /* All interlaced capable intel hw wants timings in frames. */
3567 drm_mode_set_crtcinfo(adjusted_mode, 0);
Chris Wilson89749352010-09-12 18:25:19 +01003568
Jesse Barnes79e53942008-11-07 14:24:08 -08003569 return true;
3570}
3571
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07003572static int valleyview_get_display_clock_speed(struct drm_device *dev)
3573{
3574 return 400000; /* FIXME */
3575}
3576
Jesse Barnese70236a2009-09-21 10:42:27 -07003577static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08003578{
Jesse Barnese70236a2009-09-21 10:42:27 -07003579 return 400000;
3580}
Jesse Barnes79e53942008-11-07 14:24:08 -08003581
Jesse Barnese70236a2009-09-21 10:42:27 -07003582static int i915_get_display_clock_speed(struct drm_device *dev)
3583{
3584 return 333000;
3585}
Jesse Barnes79e53942008-11-07 14:24:08 -08003586
Jesse Barnese70236a2009-09-21 10:42:27 -07003587static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
3588{
3589 return 200000;
3590}
Jesse Barnes79e53942008-11-07 14:24:08 -08003591
Jesse Barnese70236a2009-09-21 10:42:27 -07003592static int i915gm_get_display_clock_speed(struct drm_device *dev)
3593{
3594 u16 gcfgc = 0;
3595
3596 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
3597
3598 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08003599 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07003600 else {
3601 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
3602 case GC_DISPLAY_CLOCK_333_MHZ:
3603 return 333000;
3604 default:
3605 case GC_DISPLAY_CLOCK_190_200_MHZ:
3606 return 190000;
3607 }
3608 }
3609}
Jesse Barnes79e53942008-11-07 14:24:08 -08003610
Jesse Barnese70236a2009-09-21 10:42:27 -07003611static int i865_get_display_clock_speed(struct drm_device *dev)
3612{
3613 return 266000;
3614}
3615
3616static int i855_get_display_clock_speed(struct drm_device *dev)
3617{
3618 u16 hpllcc = 0;
3619 /* Assume that the hardware is in the high speed state. This
3620 * should be the default.
3621 */
3622 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
3623 case GC_CLOCK_133_200:
3624 case GC_CLOCK_100_200:
3625 return 200000;
3626 case GC_CLOCK_166_250:
3627 return 250000;
3628 case GC_CLOCK_100_133:
3629 return 133000;
3630 }
3631
3632 /* Shouldn't happen */
3633 return 0;
3634}
3635
3636static int i830_get_display_clock_speed(struct drm_device *dev)
3637{
3638 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08003639}
3640
Zhenyu Wang2c072452009-06-05 15:38:42 +08003641struct fdi_m_n {
3642 u32 tu;
3643 u32 gmch_m;
3644 u32 gmch_n;
3645 u32 link_m;
3646 u32 link_n;
3647};
3648
3649static void
3650fdi_reduce_ratio(u32 *num, u32 *den)
3651{
3652 while (*num > 0xffffff || *den > 0xffffff) {
3653 *num >>= 1;
3654 *den >>= 1;
3655 }
3656}
3657
Zhenyu Wang2c072452009-06-05 15:38:42 +08003658static void
Adam Jacksonf2b115e2009-12-03 17:14:42 -05003659ironlake_compute_m_n(int bits_per_pixel, int nlanes, int pixel_clock,
3660 int link_clock, struct fdi_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003661{
Zhenyu Wang2c072452009-06-05 15:38:42 +08003662 m_n->tu = 64; /* default size */
3663
Chris Wilson22ed1112010-12-04 01:01:29 +00003664 /* BUG_ON(pixel_clock > INT_MAX / 36); */
3665 m_n->gmch_m = bits_per_pixel * pixel_clock;
3666 m_n->gmch_n = link_clock * nlanes * 8;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003667 fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
3668
Chris Wilson22ed1112010-12-04 01:01:29 +00003669 m_n->link_m = pixel_clock;
3670 m_n->link_n = link_clock;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003671 fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
3672}
3673
3674
Shaohua Li7662c8b2009-06-26 11:23:55 +08003675struct intel_watermark_params {
3676 unsigned long fifo_size;
3677 unsigned long max_wm;
3678 unsigned long default_wm;
3679 unsigned long guard_size;
3680 unsigned long cacheline_size;
3681};
3682
Adam Jacksonf2b115e2009-12-03 17:14:42 -05003683/* Pineview has different values for various configs */
Chris Wilsond2102462011-01-24 17:43:27 +00003684static const struct intel_watermark_params pineview_display_wm = {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05003685 PINEVIEW_DISPLAY_FIFO,
3686 PINEVIEW_MAX_WM,
3687 PINEVIEW_DFT_WM,
3688 PINEVIEW_GUARD_WM,
3689 PINEVIEW_FIFO_LINE_SIZE
Shaohua Li7662c8b2009-06-26 11:23:55 +08003690};
Chris Wilsond2102462011-01-24 17:43:27 +00003691static const struct intel_watermark_params pineview_display_hplloff_wm = {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05003692 PINEVIEW_DISPLAY_FIFO,
3693 PINEVIEW_MAX_WM,
3694 PINEVIEW_DFT_HPLLOFF_WM,
3695 PINEVIEW_GUARD_WM,
3696 PINEVIEW_FIFO_LINE_SIZE
Shaohua Li7662c8b2009-06-26 11:23:55 +08003697};
Chris Wilsond2102462011-01-24 17:43:27 +00003698static const struct intel_watermark_params pineview_cursor_wm = {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05003699 PINEVIEW_CURSOR_FIFO,
3700 PINEVIEW_CURSOR_MAX_WM,
3701 PINEVIEW_CURSOR_DFT_WM,
3702 PINEVIEW_CURSOR_GUARD_WM,
3703 PINEVIEW_FIFO_LINE_SIZE,
Shaohua Li7662c8b2009-06-26 11:23:55 +08003704};
Chris Wilsond2102462011-01-24 17:43:27 +00003705static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05003706 PINEVIEW_CURSOR_FIFO,
3707 PINEVIEW_CURSOR_MAX_WM,
3708 PINEVIEW_CURSOR_DFT_WM,
3709 PINEVIEW_CURSOR_GUARD_WM,
3710 PINEVIEW_FIFO_LINE_SIZE
Shaohua Li7662c8b2009-06-26 11:23:55 +08003711};
Chris Wilsond2102462011-01-24 17:43:27 +00003712static const struct intel_watermark_params g4x_wm_info = {
Jesse Barnes0e442c62009-10-19 10:09:33 +09003713 G4X_FIFO_SIZE,
3714 G4X_MAX_WM,
3715 G4X_MAX_WM,
3716 2,
3717 G4X_FIFO_LINE_SIZE,
3718};
Chris Wilsond2102462011-01-24 17:43:27 +00003719static const struct intel_watermark_params g4x_cursor_wm_info = {
Zhao Yakui4fe5e612010-06-12 14:32:25 +08003720 I965_CURSOR_FIFO,
3721 I965_CURSOR_MAX_WM,
3722 I965_CURSOR_DFT_WM,
3723 2,
3724 G4X_FIFO_LINE_SIZE,
3725};
Jesse Barnesceb04242012-03-28 13:39:22 -07003726static const struct intel_watermark_params valleyview_wm_info = {
3727 VALLEYVIEW_FIFO_SIZE,
3728 VALLEYVIEW_MAX_WM,
3729 VALLEYVIEW_MAX_WM,
3730 2,
3731 G4X_FIFO_LINE_SIZE,
3732};
3733static const struct intel_watermark_params valleyview_cursor_wm_info = {
3734 I965_CURSOR_FIFO,
3735 VALLEYVIEW_CURSOR_MAX_WM,
3736 I965_CURSOR_DFT_WM,
3737 2,
3738 G4X_FIFO_LINE_SIZE,
3739};
Chris Wilsond2102462011-01-24 17:43:27 +00003740static const struct intel_watermark_params i965_cursor_wm_info = {
Zhao Yakui4fe5e612010-06-12 14:32:25 +08003741 I965_CURSOR_FIFO,
3742 I965_CURSOR_MAX_WM,
3743 I965_CURSOR_DFT_WM,
3744 2,
3745 I915_FIFO_LINE_SIZE,
3746};
Chris Wilsond2102462011-01-24 17:43:27 +00003747static const struct intel_watermark_params i945_wm_info = {
Shaohua Li7662c8b2009-06-26 11:23:55 +08003748 I945_FIFO_SIZE,
3749 I915_MAX_WM,
3750 1,
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003751 2,
3752 I915_FIFO_LINE_SIZE
3753};
Chris Wilsond2102462011-01-24 17:43:27 +00003754static const struct intel_watermark_params i915_wm_info = {
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003755 I915_FIFO_SIZE,
3756 I915_MAX_WM,
3757 1,
3758 2,
Shaohua Li7662c8b2009-06-26 11:23:55 +08003759 I915_FIFO_LINE_SIZE
3760};
Chris Wilsond2102462011-01-24 17:43:27 +00003761static const struct intel_watermark_params i855_wm_info = {
Shaohua Li7662c8b2009-06-26 11:23:55 +08003762 I855GM_FIFO_SIZE,
3763 I915_MAX_WM,
3764 1,
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003765 2,
Shaohua Li7662c8b2009-06-26 11:23:55 +08003766 I830_FIFO_LINE_SIZE
3767};
Chris Wilsond2102462011-01-24 17:43:27 +00003768static const struct intel_watermark_params i830_wm_info = {
Shaohua Li7662c8b2009-06-26 11:23:55 +08003769 I830_FIFO_SIZE,
3770 I915_MAX_WM,
3771 1,
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003772 2,
Shaohua Li7662c8b2009-06-26 11:23:55 +08003773 I830_FIFO_LINE_SIZE
3774};
3775
Chris Wilsond2102462011-01-24 17:43:27 +00003776static const struct intel_watermark_params ironlake_display_wm_info = {
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003777 ILK_DISPLAY_FIFO,
3778 ILK_DISPLAY_MAXWM,
3779 ILK_DISPLAY_DFTWM,
3780 2,
3781 ILK_FIFO_LINE_SIZE
3782};
Chris Wilsond2102462011-01-24 17:43:27 +00003783static const struct intel_watermark_params ironlake_cursor_wm_info = {
Zhao Yakuic936f442010-06-12 14:32:26 +08003784 ILK_CURSOR_FIFO,
3785 ILK_CURSOR_MAXWM,
3786 ILK_CURSOR_DFTWM,
3787 2,
3788 ILK_FIFO_LINE_SIZE
3789};
Chris Wilsond2102462011-01-24 17:43:27 +00003790static const struct intel_watermark_params ironlake_display_srwm_info = {
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003791 ILK_DISPLAY_SR_FIFO,
3792 ILK_DISPLAY_MAX_SRWM,
3793 ILK_DISPLAY_DFT_SRWM,
3794 2,
3795 ILK_FIFO_LINE_SIZE
3796};
Chris Wilsond2102462011-01-24 17:43:27 +00003797static const struct intel_watermark_params ironlake_cursor_srwm_info = {
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003798 ILK_CURSOR_SR_FIFO,
3799 ILK_CURSOR_MAX_SRWM,
3800 ILK_CURSOR_DFT_SRWM,
3801 2,
3802 ILK_FIFO_LINE_SIZE
3803};
3804
Chris Wilsond2102462011-01-24 17:43:27 +00003805static const struct intel_watermark_params sandybridge_display_wm_info = {
Yuanhan Liu13982612010-12-15 15:42:31 +08003806 SNB_DISPLAY_FIFO,
3807 SNB_DISPLAY_MAXWM,
3808 SNB_DISPLAY_DFTWM,
3809 2,
3810 SNB_FIFO_LINE_SIZE
3811};
Chris Wilsond2102462011-01-24 17:43:27 +00003812static const struct intel_watermark_params sandybridge_cursor_wm_info = {
Yuanhan Liu13982612010-12-15 15:42:31 +08003813 SNB_CURSOR_FIFO,
3814 SNB_CURSOR_MAXWM,
3815 SNB_CURSOR_DFTWM,
3816 2,
3817 SNB_FIFO_LINE_SIZE
3818};
Chris Wilsond2102462011-01-24 17:43:27 +00003819static const struct intel_watermark_params sandybridge_display_srwm_info = {
Yuanhan Liu13982612010-12-15 15:42:31 +08003820 SNB_DISPLAY_SR_FIFO,
3821 SNB_DISPLAY_MAX_SRWM,
3822 SNB_DISPLAY_DFT_SRWM,
3823 2,
3824 SNB_FIFO_LINE_SIZE
3825};
Chris Wilsond2102462011-01-24 17:43:27 +00003826static const struct intel_watermark_params sandybridge_cursor_srwm_info = {
Yuanhan Liu13982612010-12-15 15:42:31 +08003827 SNB_CURSOR_SR_FIFO,
3828 SNB_CURSOR_MAX_SRWM,
3829 SNB_CURSOR_DFT_SRWM,
3830 2,
3831 SNB_FIFO_LINE_SIZE
3832};
3833
3834
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003835/**
3836 * intel_calculate_wm - calculate watermark level
3837 * @clock_in_khz: pixel clock
3838 * @wm: chip FIFO params
3839 * @pixel_size: display pixel size
3840 * @latency_ns: memory latency for the platform
3841 *
3842 * Calculate the watermark level (the level at which the display plane will
3843 * start fetching from memory again). Each chip has a different display
3844 * FIFO size and allocation, so the caller needs to figure that out and pass
3845 * in the correct intel_watermark_params structure.
3846 *
3847 * As the pixel clock runs, the FIFO will be drained at a rate that depends
3848 * on the pixel size. When it reaches the watermark level, it'll start
3849 * fetching FIFO line sized based chunks from memory until the FIFO fills
3850 * past the watermark point. If the FIFO drains completely, a FIFO underrun
3851 * will occur, and a display engine hang could result.
3852 */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003853static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
Chris Wilsond2102462011-01-24 17:43:27 +00003854 const struct intel_watermark_params *wm,
3855 int fifo_size,
Shaohua Li7662c8b2009-06-26 11:23:55 +08003856 int pixel_size,
3857 unsigned long latency_ns)
3858{
Jesse Barnes390c4dd2009-07-16 13:01:01 -07003859 long entries_required, wm_size;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003860
Jesse Barnesd6604672009-09-11 12:25:56 -07003861 /*
3862 * Note: we need to make sure we don't overflow for various clock &
3863 * latency values.
3864 * clocks go from a few thousand to several hundred thousand.
3865 * latency is usually a few thousand
3866 */
3867 entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
3868 1000;
Chris Wilson8de9b312010-07-19 19:59:52 +01003869 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003870
Joe Perchesbbb0aef52011-04-17 20:35:52 -07003871 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003872
Chris Wilsond2102462011-01-24 17:43:27 +00003873 wm_size = fifo_size - (entries_required + wm->guard_size);
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003874
Joe Perchesbbb0aef52011-04-17 20:35:52 -07003875 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003876
Jesse Barnes390c4dd2009-07-16 13:01:01 -07003877 /* Don't promote wm_size to unsigned... */
3878 if (wm_size > (long)wm->max_wm)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003879 wm_size = wm->max_wm;
Chris Wilsonc3add4b2010-09-08 09:14:08 +01003880 if (wm_size <= 0)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003881 wm_size = wm->default_wm;
3882 return wm_size;
3883}
3884
3885struct cxsr_latency {
3886 int is_desktop;
Li Peng95534262010-05-18 18:58:44 +08003887 int is_ddr3;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003888 unsigned long fsb_freq;
3889 unsigned long mem_freq;
3890 unsigned long display_sr;
3891 unsigned long display_hpll_disable;
3892 unsigned long cursor_sr;
3893 unsigned long cursor_hpll_disable;
3894};
3895
Chris Wilson403c89f2010-08-04 15:25:31 +01003896static const struct cxsr_latency cxsr_latency_table[] = {
Li Peng95534262010-05-18 18:58:44 +08003897 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
3898 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
3899 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
3900 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
3901 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003902
Li Peng95534262010-05-18 18:58:44 +08003903 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
3904 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
3905 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
3906 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
3907 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003908
Li Peng95534262010-05-18 18:58:44 +08003909 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
3910 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
3911 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
3912 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
3913 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003914
Li Peng95534262010-05-18 18:58:44 +08003915 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
3916 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
3917 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
3918 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
3919 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003920
Li Peng95534262010-05-18 18:58:44 +08003921 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
3922 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
3923 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
3924 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
3925 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003926
Li Peng95534262010-05-18 18:58:44 +08003927 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
3928 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
3929 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
3930 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
3931 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003932};
3933
Chris Wilson403c89f2010-08-04 15:25:31 +01003934static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
3935 int is_ddr3,
3936 int fsb,
3937 int mem)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003938{
Chris Wilson403c89f2010-08-04 15:25:31 +01003939 const struct cxsr_latency *latency;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003940 int i;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003941
3942 if (fsb == 0 || mem == 0)
3943 return NULL;
3944
3945 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
3946 latency = &cxsr_latency_table[i];
3947 if (is_desktop == latency->is_desktop &&
Li Peng95534262010-05-18 18:58:44 +08003948 is_ddr3 == latency->is_ddr3 &&
Jaswinder Singh Rajputdecbbcd2009-09-12 23:15:07 +05303949 fsb == latency->fsb_freq && mem == latency->mem_freq)
3950 return latency;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003951 }
Jaswinder Singh Rajputdecbbcd2009-09-12 23:15:07 +05303952
Zhao Yakui28c97732009-10-09 11:39:41 +08003953 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Jaswinder Singh Rajputdecbbcd2009-09-12 23:15:07 +05303954
3955 return NULL;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003956}
3957
Adam Jacksonf2b115e2009-12-03 17:14:42 -05003958static void pineview_disable_cxsr(struct drm_device *dev)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003959{
3960 struct drm_i915_private *dev_priv = dev->dev_private;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003961
3962 /* deactivate cxsr */
Chris Wilson3e33d942010-08-04 11:17:25 +01003963 I915_WRITE(DSPFW3, I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003964}
3965
Jesse Barnesbcc24fb2009-08-31 10:24:31 -07003966/*
3967 * Latency for FIFO fetches is dependent on several factors:
3968 * - memory configuration (speed, channels)
3969 * - chipset
3970 * - current MCH state
3971 * It can be fairly high in some situations, so here we assume a fairly
3972 * pessimal value. It's a tradeoff between extra memory fetches (if we
3973 * set this value too high, the FIFO will fetch frequently to stay full)
3974 * and power consumption (set it too low to save power and we might see
3975 * FIFO underruns and display "flicker").
3976 *
3977 * A value of 5us seems to be a good balance; safe for very low end
3978 * platforms but not overly aggressive on lower latency configs.
3979 */
Tobias Klauser69e302a2009-12-23 14:14:34 +01003980static const int latency_ns = 5000;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003981
Jesse Barnese70236a2009-09-21 10:42:27 -07003982static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003983{
3984 struct drm_i915_private *dev_priv = dev->dev_private;
3985 uint32_t dsparb = I915_READ(DSPARB);
3986 int size;
3987
Chris Wilson8de9b312010-07-19 19:59:52 +01003988 size = dsparb & 0x7f;
3989 if (plane)
3990 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003991
Zhao Yakui28c97732009-10-09 11:39:41 +08003992 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
Chris Wilson5eddb702010-09-11 13:48:45 +01003993 plane ? "B" : "A", size);
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003994
3995 return size;
3996}
Shaohua Li7662c8b2009-06-26 11:23:55 +08003997
Jesse Barnese70236a2009-09-21 10:42:27 -07003998static int i85x_get_fifo_size(struct drm_device *dev, int plane)
3999{
4000 struct drm_i915_private *dev_priv = dev->dev_private;
4001 uint32_t dsparb = I915_READ(DSPARB);
4002 int size;
4003
Chris Wilson8de9b312010-07-19 19:59:52 +01004004 size = dsparb & 0x1ff;
4005 if (plane)
4006 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
Jesse Barnese70236a2009-09-21 10:42:27 -07004007 size >>= 1; /* Convert to cachelines */
4008
Zhao Yakui28c97732009-10-09 11:39:41 +08004009 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
Chris Wilson5eddb702010-09-11 13:48:45 +01004010 plane ? "B" : "A", size);
Jesse Barnese70236a2009-09-21 10:42:27 -07004011
4012 return size;
4013}
4014
4015static int i845_get_fifo_size(struct drm_device *dev, int plane)
4016{
4017 struct drm_i915_private *dev_priv = dev->dev_private;
4018 uint32_t dsparb = I915_READ(DSPARB);
4019 int size;
4020
4021 size = dsparb & 0x7f;
4022 size >>= 2; /* Convert to cachelines */
4023
Zhao Yakui28c97732009-10-09 11:39:41 +08004024 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
Chris Wilson5eddb702010-09-11 13:48:45 +01004025 plane ? "B" : "A",
4026 size);
Jesse Barnese70236a2009-09-21 10:42:27 -07004027
4028 return size;
4029}
4030
4031static int i830_get_fifo_size(struct drm_device *dev, int plane)
4032{
4033 struct drm_i915_private *dev_priv = dev->dev_private;
4034 uint32_t dsparb = I915_READ(DSPARB);
4035 int size;
4036
4037 size = dsparb & 0x7f;
4038 size >>= 1; /* Convert to cachelines */
4039
Zhao Yakui28c97732009-10-09 11:39:41 +08004040 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
Chris Wilson5eddb702010-09-11 13:48:45 +01004041 plane ? "B" : "A", size);
Jesse Barnese70236a2009-09-21 10:42:27 -07004042
4043 return size;
4044}
4045
Chris Wilsond2102462011-01-24 17:43:27 +00004046static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
4047{
4048 struct drm_crtc *crtc, *enabled = NULL;
4049
4050 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
4051 if (crtc->enabled && crtc->fb) {
4052 if (enabled)
4053 return NULL;
4054 enabled = crtc;
4055 }
4056 }
4057
4058 return enabled;
4059}
4060
4061static void pineview_update_wm(struct drm_device *dev)
Zhao Yakuid4294342010-03-22 22:45:36 +08004062{
4063 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond2102462011-01-24 17:43:27 +00004064 struct drm_crtc *crtc;
Chris Wilson403c89f2010-08-04 15:25:31 +01004065 const struct cxsr_latency *latency;
Zhao Yakuid4294342010-03-22 22:45:36 +08004066 u32 reg;
4067 unsigned long wm;
Zhao Yakuid4294342010-03-22 22:45:36 +08004068
Chris Wilson403c89f2010-08-04 15:25:31 +01004069 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
Li Peng95534262010-05-18 18:58:44 +08004070 dev_priv->fsb_freq, dev_priv->mem_freq);
Zhao Yakuid4294342010-03-22 22:45:36 +08004071 if (!latency) {
4072 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
4073 pineview_disable_cxsr(dev);
4074 return;
4075 }
4076
Chris Wilsond2102462011-01-24 17:43:27 +00004077 crtc = single_enabled_crtc(dev);
4078 if (crtc) {
4079 int clock = crtc->mode.clock;
4080 int pixel_size = crtc->fb->bits_per_pixel / 8;
Zhao Yakuid4294342010-03-22 22:45:36 +08004081
4082 /* Display SR */
Chris Wilsond2102462011-01-24 17:43:27 +00004083 wm = intel_calculate_wm(clock, &pineview_display_wm,
4084 pineview_display_wm.fifo_size,
Zhao Yakuid4294342010-03-22 22:45:36 +08004085 pixel_size, latency->display_sr);
4086 reg = I915_READ(DSPFW1);
4087 reg &= ~DSPFW_SR_MASK;
4088 reg |= wm << DSPFW_SR_SHIFT;
4089 I915_WRITE(DSPFW1, reg);
4090 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
4091
4092 /* cursor SR */
Chris Wilsond2102462011-01-24 17:43:27 +00004093 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
4094 pineview_display_wm.fifo_size,
Zhao Yakuid4294342010-03-22 22:45:36 +08004095 pixel_size, latency->cursor_sr);
4096 reg = I915_READ(DSPFW3);
4097 reg &= ~DSPFW_CURSOR_SR_MASK;
4098 reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
4099 I915_WRITE(DSPFW3, reg);
4100
4101 /* Display HPLL off SR */
Chris Wilsond2102462011-01-24 17:43:27 +00004102 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
4103 pineview_display_hplloff_wm.fifo_size,
Zhao Yakuid4294342010-03-22 22:45:36 +08004104 pixel_size, latency->display_hpll_disable);
4105 reg = I915_READ(DSPFW3);
4106 reg &= ~DSPFW_HPLL_SR_MASK;
4107 reg |= wm & DSPFW_HPLL_SR_MASK;
4108 I915_WRITE(DSPFW3, reg);
4109
4110 /* cursor HPLL off SR */
Chris Wilsond2102462011-01-24 17:43:27 +00004111 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
4112 pineview_display_hplloff_wm.fifo_size,
Zhao Yakuid4294342010-03-22 22:45:36 +08004113 pixel_size, latency->cursor_hpll_disable);
4114 reg = I915_READ(DSPFW3);
4115 reg &= ~DSPFW_HPLL_CURSOR_MASK;
4116 reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
4117 I915_WRITE(DSPFW3, reg);
4118 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
4119
4120 /* activate cxsr */
Chris Wilson3e33d942010-08-04 11:17:25 +01004121 I915_WRITE(DSPFW3,
4122 I915_READ(DSPFW3) | PINEVIEW_SELF_REFRESH_EN);
Zhao Yakuid4294342010-03-22 22:45:36 +08004123 DRM_DEBUG_KMS("Self-refresh is enabled\n");
4124 } else {
4125 pineview_disable_cxsr(dev);
4126 DRM_DEBUG_KMS("Self-refresh is disabled\n");
4127 }
4128}
4129
Chris Wilson417ae142011-01-19 15:04:42 +00004130static bool g4x_compute_wm0(struct drm_device *dev,
4131 int plane,
4132 const struct intel_watermark_params *display,
4133 int display_latency_ns,
4134 const struct intel_watermark_params *cursor,
4135 int cursor_latency_ns,
4136 int *plane_wm,
4137 int *cursor_wm)
Jesse Barnes652c3932009-08-17 13:31:43 -07004138{
Chris Wilson417ae142011-01-19 15:04:42 +00004139 struct drm_crtc *crtc;
4140 int htotal, hdisplay, clock, pixel_size;
4141 int line_time_us, line_count;
4142 int entries, tlb_miss;
Jesse Barnes652c3932009-08-17 13:31:43 -07004143
Chris Wilson417ae142011-01-19 15:04:42 +00004144 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilson5c72d062011-04-13 09:28:23 +01004145 if (crtc->fb == NULL || !crtc->enabled) {
4146 *cursor_wm = cursor->guard_size;
4147 *plane_wm = display->guard_size;
Chris Wilson417ae142011-01-19 15:04:42 +00004148 return false;
Chris Wilson5c72d062011-04-13 09:28:23 +01004149 }
Jesse Barnes0e442c62009-10-19 10:09:33 +09004150
Chris Wilson417ae142011-01-19 15:04:42 +00004151 htotal = crtc->mode.htotal;
4152 hdisplay = crtc->mode.hdisplay;
4153 clock = crtc->mode.clock;
4154 pixel_size = crtc->fb->bits_per_pixel / 8;
Jesse Barnes0e442c62009-10-19 10:09:33 +09004155
Chris Wilson417ae142011-01-19 15:04:42 +00004156 /* Use the small buffer method to calculate plane watermark */
4157 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
4158 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
4159 if (tlb_miss > 0)
4160 entries += tlb_miss;
4161 entries = DIV_ROUND_UP(entries, display->cacheline_size);
4162 *plane_wm = entries + display->guard_size;
4163 if (*plane_wm > (int)display->max_wm)
4164 *plane_wm = display->max_wm;
Jesse Barnes0e442c62009-10-19 10:09:33 +09004165
Chris Wilson417ae142011-01-19 15:04:42 +00004166 /* Use the large buffer method to calculate cursor watermark */
4167 line_time_us = ((htotal * 1000) / clock);
4168 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
4169 entries = line_count * 64 * pixel_size;
4170 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
4171 if (tlb_miss > 0)
4172 entries += tlb_miss;
4173 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
4174 *cursor_wm = entries + cursor->guard_size;
4175 if (*cursor_wm > (int)cursor->max_wm)
4176 *cursor_wm = (int)cursor->max_wm;
Jesse Barnes0e442c62009-10-19 10:09:33 +09004177
Chris Wilson417ae142011-01-19 15:04:42 +00004178 return true;
4179}
Jesse Barnes0e442c62009-10-19 10:09:33 +09004180
Chris Wilson417ae142011-01-19 15:04:42 +00004181/*
4182 * Check the wm result.
4183 *
4184 * If any calculated watermark values is larger than the maximum value that
4185 * can be programmed into the associated watermark register, that watermark
4186 * must be disabled.
4187 */
4188static bool g4x_check_srwm(struct drm_device *dev,
4189 int display_wm, int cursor_wm,
4190 const struct intel_watermark_params *display,
4191 const struct intel_watermark_params *cursor)
4192{
4193 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
4194 display_wm, cursor_wm);
Jesse Barnes0e442c62009-10-19 10:09:33 +09004195
Chris Wilson417ae142011-01-19 15:04:42 +00004196 if (display_wm > display->max_wm) {
Joe Perchesbbb0aef52011-04-17 20:35:52 -07004197 DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
Chris Wilson417ae142011-01-19 15:04:42 +00004198 display_wm, display->max_wm);
4199 return false;
Jesse Barnes0e442c62009-10-19 10:09:33 +09004200 }
4201
Chris Wilson417ae142011-01-19 15:04:42 +00004202 if (cursor_wm > cursor->max_wm) {
Joe Perchesbbb0aef52011-04-17 20:35:52 -07004203 DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
Chris Wilson417ae142011-01-19 15:04:42 +00004204 cursor_wm, cursor->max_wm);
4205 return false;
4206 }
Jesse Barnes0e442c62009-10-19 10:09:33 +09004207
Chris Wilson417ae142011-01-19 15:04:42 +00004208 if (!(display_wm || cursor_wm)) {
4209 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
4210 return false;
4211 }
Jesse Barnes0e442c62009-10-19 10:09:33 +09004212
Chris Wilson417ae142011-01-19 15:04:42 +00004213 return true;
4214}
4215
4216static bool g4x_compute_srwm(struct drm_device *dev,
Chris Wilsond2102462011-01-24 17:43:27 +00004217 int plane,
4218 int latency_ns,
Chris Wilson417ae142011-01-19 15:04:42 +00004219 const struct intel_watermark_params *display,
4220 const struct intel_watermark_params *cursor,
4221 int *display_wm, int *cursor_wm)
4222{
Chris Wilsond2102462011-01-24 17:43:27 +00004223 struct drm_crtc *crtc;
4224 int hdisplay, htotal, pixel_size, clock;
Chris Wilson417ae142011-01-19 15:04:42 +00004225 unsigned long line_time_us;
4226 int line_count, line_size;
4227 int small, large;
4228 int entries;
4229
4230 if (!latency_ns) {
4231 *display_wm = *cursor_wm = 0;
4232 return false;
4233 }
4234
Chris Wilsond2102462011-01-24 17:43:27 +00004235 crtc = intel_get_crtc_for_plane(dev, plane);
4236 hdisplay = crtc->mode.hdisplay;
4237 htotal = crtc->mode.htotal;
4238 clock = crtc->mode.clock;
4239 pixel_size = crtc->fb->bits_per_pixel / 8;
4240
Chris Wilson417ae142011-01-19 15:04:42 +00004241 line_time_us = (htotal * 1000) / clock;
4242 line_count = (latency_ns / line_time_us + 1000) / 1000;
4243 line_size = hdisplay * pixel_size;
4244
4245 /* Use the minimum of the small and large buffer method for primary */
4246 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
4247 large = line_count * line_size;
4248
4249 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
4250 *display_wm = entries + display->guard_size;
4251
4252 /* calculate the self-refresh watermark for display cursor */
4253 entries = line_count * pixel_size * 64;
4254 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
4255 *cursor_wm = entries + cursor->guard_size;
4256
4257 return g4x_check_srwm(dev,
4258 *display_wm, *cursor_wm,
4259 display, cursor);
4260}
4261
Gajanan Bhat12a3c052012-03-28 13:39:30 -07004262static bool vlv_compute_drain_latency(struct drm_device *dev,
4263 int plane,
4264 int *plane_prec_mult,
4265 int *plane_dl,
4266 int *cursor_prec_mult,
4267 int *cursor_dl)
4268{
4269 struct drm_crtc *crtc;
4270 int clock, pixel_size;
4271 int entries;
4272
4273 crtc = intel_get_crtc_for_plane(dev, plane);
4274 if (crtc->fb == NULL || !crtc->enabled)
4275 return false;
4276
4277 clock = crtc->mode.clock; /* VESA DOT Clock */
4278 pixel_size = crtc->fb->bits_per_pixel / 8; /* BPP */
4279
4280 entries = (clock / 1000) * pixel_size;
4281 *plane_prec_mult = (entries > 256) ?
4282 DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_16;
4283 *plane_dl = (64 * (*plane_prec_mult) * 4) / ((clock / 1000) *
4284 pixel_size);
4285
4286 entries = (clock / 1000) * 4; /* BPP is always 4 for cursor */
4287 *cursor_prec_mult = (entries > 256) ?
4288 DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_16;
4289 *cursor_dl = (64 * (*cursor_prec_mult) * 4) / ((clock / 1000) * 4);
4290
4291 return true;
4292}
4293
4294/*
4295 * Update drain latency registers of memory arbiter
4296 *
4297 * Valleyview SoC has a new memory arbiter and needs drain latency registers
4298 * to be programmed. Each plane has a drain latency multiplier and a drain
4299 * latency value.
4300 */
4301
4302static void vlv_update_drain_latency(struct drm_device *dev)
4303{
4304 struct drm_i915_private *dev_priv = dev->dev_private;
4305 int planea_prec, planea_dl, planeb_prec, planeb_dl;
4306 int cursora_prec, cursora_dl, cursorb_prec, cursorb_dl;
4307 int plane_prec_mult, cursor_prec_mult; /* Precision multiplier is
4308 either 16 or 32 */
4309
4310 /* For plane A, Cursor A */
4311 if (vlv_compute_drain_latency(dev, 0, &plane_prec_mult, &planea_dl,
4312 &cursor_prec_mult, &cursora_dl)) {
4313 cursora_prec = (cursor_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
4314 DDL_CURSORA_PRECISION_32 : DDL_CURSORA_PRECISION_16;
4315 planea_prec = (plane_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
4316 DDL_PLANEA_PRECISION_32 : DDL_PLANEA_PRECISION_16;
4317
4318 I915_WRITE(VLV_DDL1, cursora_prec |
4319 (cursora_dl << DDL_CURSORA_SHIFT) |
4320 planea_prec | planea_dl);
4321 }
4322
4323 /* For plane B, Cursor B */
4324 if (vlv_compute_drain_latency(dev, 1, &plane_prec_mult, &planeb_dl,
4325 &cursor_prec_mult, &cursorb_dl)) {
4326 cursorb_prec = (cursor_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
4327 DDL_CURSORB_PRECISION_32 : DDL_CURSORB_PRECISION_16;
4328 planeb_prec = (plane_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
4329 DDL_PLANEB_PRECISION_32 : DDL_PLANEB_PRECISION_16;
4330
4331 I915_WRITE(VLV_DDL2, cursorb_prec |
4332 (cursorb_dl << DDL_CURSORB_SHIFT) |
4333 planeb_prec | planeb_dl);
4334 }
4335}
4336
Yuanhan Liu7ccb4a52011-03-18 07:37:35 +00004337#define single_plane_enabled(mask) is_power_of_2(mask)
Chris Wilsond2102462011-01-24 17:43:27 +00004338
Jesse Barnesceb04242012-03-28 13:39:22 -07004339static void valleyview_update_wm(struct drm_device *dev)
4340{
4341 static const int sr_latency_ns = 12000;
4342 struct drm_i915_private *dev_priv = dev->dev_private;
4343 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
4344 int plane_sr, cursor_sr;
4345 unsigned int enabled = 0;
4346
Gajanan Bhat12a3c052012-03-28 13:39:30 -07004347 vlv_update_drain_latency(dev);
4348
Jesse Barnesceb04242012-03-28 13:39:22 -07004349 if (g4x_compute_wm0(dev, 0,
4350 &valleyview_wm_info, latency_ns,
4351 &valleyview_cursor_wm_info, latency_ns,
4352 &planea_wm, &cursora_wm))
4353 enabled |= 1;
4354
4355 if (g4x_compute_wm0(dev, 1,
4356 &valleyview_wm_info, latency_ns,
4357 &valleyview_cursor_wm_info, latency_ns,
4358 &planeb_wm, &cursorb_wm))
4359 enabled |= 2;
4360
4361 plane_sr = cursor_sr = 0;
4362 if (single_plane_enabled(enabled) &&
4363 g4x_compute_srwm(dev, ffs(enabled) - 1,
4364 sr_latency_ns,
4365 &valleyview_wm_info,
4366 &valleyview_cursor_wm_info,
4367 &plane_sr, &cursor_sr))
4368 I915_WRITE(FW_BLC_SELF_VLV, FW_CSPWRDWNEN);
4369 else
4370 I915_WRITE(FW_BLC_SELF_VLV,
4371 I915_READ(FW_BLC_SELF_VLV) & ~FW_CSPWRDWNEN);
4372
4373 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
4374 planea_wm, cursora_wm,
4375 planeb_wm, cursorb_wm,
4376 plane_sr, cursor_sr);
4377
4378 I915_WRITE(DSPFW1,
4379 (plane_sr << DSPFW_SR_SHIFT) |
4380 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
4381 (planeb_wm << DSPFW_PLANEB_SHIFT) |
4382 planea_wm);
4383 I915_WRITE(DSPFW2,
4384 (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
4385 (cursora_wm << DSPFW_CURSORA_SHIFT));
4386 I915_WRITE(DSPFW3,
4387 (I915_READ(DSPFW3) | (cursor_sr << DSPFW_CURSOR_SR_SHIFT)));
4388}
4389
Chris Wilsond2102462011-01-24 17:43:27 +00004390static void g4x_update_wm(struct drm_device *dev)
Chris Wilson417ae142011-01-19 15:04:42 +00004391{
4392 static const int sr_latency_ns = 12000;
4393 struct drm_i915_private *dev_priv = dev->dev_private;
4394 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
Chris Wilsond2102462011-01-24 17:43:27 +00004395 int plane_sr, cursor_sr;
4396 unsigned int enabled = 0;
Chris Wilson417ae142011-01-19 15:04:42 +00004397
4398 if (g4x_compute_wm0(dev, 0,
4399 &g4x_wm_info, latency_ns,
4400 &g4x_cursor_wm_info, latency_ns,
4401 &planea_wm, &cursora_wm))
Chris Wilsond2102462011-01-24 17:43:27 +00004402 enabled |= 1;
Chris Wilson417ae142011-01-19 15:04:42 +00004403
4404 if (g4x_compute_wm0(dev, 1,
4405 &g4x_wm_info, latency_ns,
4406 &g4x_cursor_wm_info, latency_ns,
4407 &planeb_wm, &cursorb_wm))
Chris Wilsond2102462011-01-24 17:43:27 +00004408 enabled |= 2;
Chris Wilson417ae142011-01-19 15:04:42 +00004409
4410 plane_sr = cursor_sr = 0;
Chris Wilsond2102462011-01-24 17:43:27 +00004411 if (single_plane_enabled(enabled) &&
4412 g4x_compute_srwm(dev, ffs(enabled) - 1,
4413 sr_latency_ns,
Chris Wilson417ae142011-01-19 15:04:42 +00004414 &g4x_wm_info,
4415 &g4x_cursor_wm_info,
4416 &plane_sr, &cursor_sr))
4417 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
4418 else
4419 I915_WRITE(FW_BLC_SELF,
4420 I915_READ(FW_BLC_SELF) & ~FW_BLC_SELF_EN);
4421
Chris Wilson308977a2011-02-02 10:41:20 +00004422 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
4423 planea_wm, cursora_wm,
4424 planeb_wm, cursorb_wm,
4425 plane_sr, cursor_sr);
Chris Wilson417ae142011-01-19 15:04:42 +00004426
4427 I915_WRITE(DSPFW1,
4428 (plane_sr << DSPFW_SR_SHIFT) |
Jesse Barnes0e442c62009-10-19 10:09:33 +09004429 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
Chris Wilson417ae142011-01-19 15:04:42 +00004430 (planeb_wm << DSPFW_PLANEB_SHIFT) |
4431 planea_wm);
4432 I915_WRITE(DSPFW2,
4433 (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
Jesse Barnes0e442c62009-10-19 10:09:33 +09004434 (cursora_wm << DSPFW_CURSORA_SHIFT));
4435 /* HPLL off in SR has some issues on G4x... disable it */
Chris Wilson417ae142011-01-19 15:04:42 +00004436 I915_WRITE(DSPFW3,
4437 (I915_READ(DSPFW3) & ~DSPFW_HPLL_SR_EN) |
Jesse Barnes0e442c62009-10-19 10:09:33 +09004438 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Jesse Barnes652c3932009-08-17 13:31:43 -07004439}
4440
Chris Wilsond2102462011-01-24 17:43:27 +00004441static void i965_update_wm(struct drm_device *dev)
Shaohua Li7662c8b2009-06-26 11:23:55 +08004442{
4443 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond2102462011-01-24 17:43:27 +00004444 struct drm_crtc *crtc;
4445 int srwm = 1;
Zhao Yakui4fe5e612010-06-12 14:32:25 +08004446 int cursor_sr = 16;
Shaohua Li7662c8b2009-06-26 11:23:55 +08004447
Jesse Barnes1dc75462009-10-19 10:08:17 +09004448 /* Calc sr entries for one plane configs */
Chris Wilsond2102462011-01-24 17:43:27 +00004449 crtc = single_enabled_crtc(dev);
4450 if (crtc) {
Jesse Barnes1dc75462009-10-19 10:08:17 +09004451 /* self-refresh has much higher latency */
Tobias Klauser69e302a2009-12-23 14:14:34 +01004452 static const int sr_latency_ns = 12000;
Chris Wilsond2102462011-01-24 17:43:27 +00004453 int clock = crtc->mode.clock;
4454 int htotal = crtc->mode.htotal;
4455 int hdisplay = crtc->mode.hdisplay;
4456 int pixel_size = crtc->fb->bits_per_pixel / 8;
4457 unsigned long line_time_us;
4458 int entries;
Jesse Barnes1dc75462009-10-19 10:08:17 +09004459
Chris Wilsond2102462011-01-24 17:43:27 +00004460 line_time_us = ((htotal * 1000) / clock);
Jesse Barnes1dc75462009-10-19 10:08:17 +09004461
4462 /* Use ns/us then divide to preserve precision */
Chris Wilsond2102462011-01-24 17:43:27 +00004463 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
4464 pixel_size * hdisplay;
4465 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
Chris Wilsond2102462011-01-24 17:43:27 +00004466 srwm = I965_FIFO_SIZE - entries;
Jesse Barnes1dc75462009-10-19 10:08:17 +09004467 if (srwm < 0)
4468 srwm = 1;
Zhao Yakui1b07e042010-06-12 14:32:24 +08004469 srwm &= 0x1ff;
Chris Wilson308977a2011-02-02 10:41:20 +00004470 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
4471 entries, srwm);
Zhao Yakui4fe5e612010-06-12 14:32:25 +08004472
Chris Wilsond2102462011-01-24 17:43:27 +00004473 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Chris Wilson5eddb702010-09-11 13:48:45 +01004474 pixel_size * 64;
Chris Wilsond2102462011-01-24 17:43:27 +00004475 entries = DIV_ROUND_UP(entries,
Chris Wilson8de9b312010-07-19 19:59:52 +01004476 i965_cursor_wm_info.cacheline_size);
Zhao Yakui4fe5e612010-06-12 14:32:25 +08004477 cursor_sr = i965_cursor_wm_info.fifo_size -
Chris Wilsond2102462011-01-24 17:43:27 +00004478 (entries + i965_cursor_wm_info.guard_size);
Zhao Yakui4fe5e612010-06-12 14:32:25 +08004479
4480 if (cursor_sr > i965_cursor_wm_info.max_wm)
4481 cursor_sr = i965_cursor_wm_info.max_wm;
4482
4483 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
4484 "cursor %d\n", srwm, cursor_sr);
4485
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004486 if (IS_CRESTLINE(dev))
Jesse Barnesadcdbc62010-06-30 13:49:37 -07004487 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
David John33c5fd12010-01-27 15:19:08 +05304488 } else {
4489 /* Turn off self refresh if both pipes are enabled */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004490 if (IS_CRESTLINE(dev))
Jesse Barnesadcdbc62010-06-30 13:49:37 -07004491 I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
4492 & ~FW_BLC_SELF_EN);
Jesse Barnes1dc75462009-10-19 10:08:17 +09004493 }
4494
4495 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
4496 srwm);
Shaohua Li7662c8b2009-06-26 11:23:55 +08004497
4498 /* 965 has limitations... */
Chris Wilson417ae142011-01-19 15:04:42 +00004499 I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
4500 (8 << 16) | (8 << 8) | (8 << 0));
Shaohua Li7662c8b2009-06-26 11:23:55 +08004501 I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
Zhao Yakui4fe5e612010-06-12 14:32:25 +08004502 /* update cursor SR watermark */
4503 I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Shaohua Li7662c8b2009-06-26 11:23:55 +08004504}
4505
Chris Wilsond2102462011-01-24 17:43:27 +00004506static void i9xx_update_wm(struct drm_device *dev)
Shaohua Li7662c8b2009-06-26 11:23:55 +08004507{
4508 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond2102462011-01-24 17:43:27 +00004509 const struct intel_watermark_params *wm_info;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07004510 uint32_t fwater_lo;
4511 uint32_t fwater_hi;
Chris Wilsond2102462011-01-24 17:43:27 +00004512 int cwm, srwm = 1;
4513 int fifo_size;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07004514 int planea_wm, planeb_wm;
Chris Wilsond2102462011-01-24 17:43:27 +00004515 struct drm_crtc *crtc, *enabled = NULL;
Shaohua Li7662c8b2009-06-26 11:23:55 +08004516
Chris Wilson72557b42011-01-31 10:29:55 +00004517 if (IS_I945GM(dev))
Chris Wilsond2102462011-01-24 17:43:27 +00004518 wm_info = &i945_wm_info;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004519 else if (!IS_GEN2(dev))
Chris Wilsond2102462011-01-24 17:43:27 +00004520 wm_info = &i915_wm_info;
Shaohua Li7662c8b2009-06-26 11:23:55 +08004521 else
Chris Wilsond2102462011-01-24 17:43:27 +00004522 wm_info = &i855_wm_info;
Shaohua Li7662c8b2009-06-26 11:23:55 +08004523
Chris Wilsond2102462011-01-24 17:43:27 +00004524 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
4525 crtc = intel_get_crtc_for_plane(dev, 0);
4526 if (crtc->enabled && crtc->fb) {
4527 planea_wm = intel_calculate_wm(crtc->mode.clock,
4528 wm_info, fifo_size,
4529 crtc->fb->bits_per_pixel / 8,
4530 latency_ns);
4531 enabled = crtc;
4532 } else
4533 planea_wm = fifo_size - wm_info->guard_size;
Shaohua Li7662c8b2009-06-26 11:23:55 +08004534
Chris Wilsond2102462011-01-24 17:43:27 +00004535 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
4536 crtc = intel_get_crtc_for_plane(dev, 1);
4537 if (crtc->enabled && crtc->fb) {
4538 planeb_wm = intel_calculate_wm(crtc->mode.clock,
4539 wm_info, fifo_size,
4540 crtc->fb->bits_per_pixel / 8,
4541 latency_ns);
4542 if (enabled == NULL)
4543 enabled = crtc;
4544 else
4545 enabled = NULL;
4546 } else
4547 planeb_wm = fifo_size - wm_info->guard_size;
Shaohua Li7662c8b2009-06-26 11:23:55 +08004548
Zhao Yakui28c97732009-10-09 11:39:41 +08004549 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
Shaohua Li7662c8b2009-06-26 11:23:55 +08004550
4551 /*
4552 * Overlay gets an aggressive default since video jitter is bad.
4553 */
4554 cwm = 2;
4555
Alexander Lam18b21902011-01-03 13:28:56 -05004556 /* Play safe and disable self-refresh before adjusting watermarks. */
4557 if (IS_I945G(dev) || IS_I945GM(dev))
4558 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | 0);
4559 else if (IS_I915GM(dev))
4560 I915_WRITE(INSTPM, I915_READ(INSTPM) & ~INSTPM_SELF_EN);
4561
Jesse Barnesdff33cf2009-07-14 10:15:56 -07004562 /* Calc sr entries for one plane configs */
Chris Wilsond2102462011-01-24 17:43:27 +00004563 if (HAS_FW_BLC(dev) && enabled) {
Jesse Barnesdff33cf2009-07-14 10:15:56 -07004564 /* self-refresh has much higher latency */
Tobias Klauser69e302a2009-12-23 14:14:34 +01004565 static const int sr_latency_ns = 6000;
Chris Wilsond2102462011-01-24 17:43:27 +00004566 int clock = enabled->mode.clock;
4567 int htotal = enabled->mode.htotal;
4568 int hdisplay = enabled->mode.hdisplay;
4569 int pixel_size = enabled->fb->bits_per_pixel / 8;
4570 unsigned long line_time_us;
4571 int entries;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07004572
Chris Wilsond2102462011-01-24 17:43:27 +00004573 line_time_us = (htotal * 1000) / clock;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07004574
4575 /* Use ns/us then divide to preserve precision */
Chris Wilsond2102462011-01-24 17:43:27 +00004576 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
4577 pixel_size * hdisplay;
4578 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
4579 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
4580 srwm = wm_info->fifo_size - entries;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07004581 if (srwm < 0)
4582 srwm = 1;
Li Pengee980b82010-01-27 19:01:11 +08004583
4584 if (IS_I945G(dev) || IS_I945GM(dev))
Alexander Lam18b21902011-01-03 13:28:56 -05004585 I915_WRITE(FW_BLC_SELF,
4586 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
4587 else if (IS_I915GM(dev))
Li Pengee980b82010-01-27 19:01:11 +08004588 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
Shaohua Li7662c8b2009-06-26 11:23:55 +08004589 }
4590
Zhao Yakui28c97732009-10-09 11:39:41 +08004591 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
Chris Wilson5eddb702010-09-11 13:48:45 +01004592 planea_wm, planeb_wm, cwm, srwm);
Shaohua Li7662c8b2009-06-26 11:23:55 +08004593
Jesse Barnesdff33cf2009-07-14 10:15:56 -07004594 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
4595 fwater_hi = (cwm & 0x1f);
4596
4597 /* Set request length to 8 cachelines per fetch */
4598 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
4599 fwater_hi = fwater_hi | (1 << 8);
Shaohua Li7662c8b2009-06-26 11:23:55 +08004600
4601 I915_WRITE(FW_BLC, fwater_lo);
4602 I915_WRITE(FW_BLC2, fwater_hi);
Alexander Lam18b21902011-01-03 13:28:56 -05004603
Chris Wilsond2102462011-01-24 17:43:27 +00004604 if (HAS_FW_BLC(dev)) {
4605 if (enabled) {
4606 if (IS_I945G(dev) || IS_I945GM(dev))
4607 I915_WRITE(FW_BLC_SELF,
4608 FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
4609 else if (IS_I915GM(dev))
4610 I915_WRITE(INSTPM, I915_READ(INSTPM) | INSTPM_SELF_EN);
4611 DRM_DEBUG_KMS("memory self refresh enabled\n");
4612 } else
4613 DRM_DEBUG_KMS("memory self refresh disabled\n");
4614 }
Shaohua Li7662c8b2009-06-26 11:23:55 +08004615}
4616
Chris Wilsond2102462011-01-24 17:43:27 +00004617static void i830_update_wm(struct drm_device *dev)
Shaohua Li7662c8b2009-06-26 11:23:55 +08004618{
4619 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond2102462011-01-24 17:43:27 +00004620 struct drm_crtc *crtc;
4621 uint32_t fwater_lo;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07004622 int planea_wm;
Shaohua Li7662c8b2009-06-26 11:23:55 +08004623
Chris Wilsond2102462011-01-24 17:43:27 +00004624 crtc = single_enabled_crtc(dev);
4625 if (crtc == NULL)
4626 return;
Shaohua Li7662c8b2009-06-26 11:23:55 +08004627
Chris Wilsond2102462011-01-24 17:43:27 +00004628 planea_wm = intel_calculate_wm(crtc->mode.clock, &i830_wm_info,
4629 dev_priv->display.get_fifo_size(dev, 0),
4630 crtc->fb->bits_per_pixel / 8,
4631 latency_ns);
4632 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
Jesse Barnesf3601322009-07-22 12:54:59 -07004633 fwater_lo |= (3<<8) | planea_wm;
4634
Zhao Yakui28c97732009-10-09 11:39:41 +08004635 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
Shaohua Li7662c8b2009-06-26 11:23:55 +08004636
4637 I915_WRITE(FW_BLC, fwater_lo);
4638}
4639
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08004640#define ILK_LP0_PLANE_LATENCY 700
Zhao Yakuic936f442010-06-12 14:32:26 +08004641#define ILK_LP0_CURSOR_LATENCY 1300
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08004642
Jesse Barnesb79d4992010-12-21 13:10:23 -08004643/*
4644 * Check the wm result.
4645 *
4646 * If any calculated watermark values is larger than the maximum value that
4647 * can be programmed into the associated watermark register, that watermark
4648 * must be disabled.
4649 */
4650static bool ironlake_check_srwm(struct drm_device *dev, int level,
4651 int fbc_wm, int display_wm, int cursor_wm,
4652 const struct intel_watermark_params *display,
4653 const struct intel_watermark_params *cursor)
4654{
4655 struct drm_i915_private *dev_priv = dev->dev_private;
4656
4657 DRM_DEBUG_KMS("watermark %d: display plane %d, fbc lines %d,"
4658 " cursor %d\n", level, display_wm, fbc_wm, cursor_wm);
4659
4660 if (fbc_wm > SNB_FBC_MAX_SRWM) {
4661 DRM_DEBUG_KMS("fbc watermark(%d) is too large(%d), disabling wm%d+\n",
4662 fbc_wm, SNB_FBC_MAX_SRWM, level);
4663
4664 /* fbc has it's own way to disable FBC WM */
4665 I915_WRITE(DISP_ARB_CTL,
4666 I915_READ(DISP_ARB_CTL) | DISP_FBC_WM_DIS);
4667 return false;
4668 }
4669
4670 if (display_wm > display->max_wm) {
4671 DRM_DEBUG_KMS("display watermark(%d) is too large(%d), disabling wm%d+\n",
4672 display_wm, SNB_DISPLAY_MAX_SRWM, level);
4673 return false;
4674 }
4675
4676 if (cursor_wm > cursor->max_wm) {
4677 DRM_DEBUG_KMS("cursor watermark(%d) is too large(%d), disabling wm%d+\n",
4678 cursor_wm, SNB_CURSOR_MAX_SRWM, level);
4679 return false;
4680 }
4681
4682 if (!(fbc_wm || display_wm || cursor_wm)) {
4683 DRM_DEBUG_KMS("latency %d is 0, disabling wm%d+\n", level, level);
4684 return false;
4685 }
4686
4687 return true;
4688}
4689
4690/*
4691 * Compute watermark values of WM[1-3],
4692 */
Chris Wilsond2102462011-01-24 17:43:27 +00004693static bool ironlake_compute_srwm(struct drm_device *dev, int level, int plane,
4694 int latency_ns,
Jesse Barnesb79d4992010-12-21 13:10:23 -08004695 const struct intel_watermark_params *display,
4696 const struct intel_watermark_params *cursor,
4697 int *fbc_wm, int *display_wm, int *cursor_wm)
4698{
Chris Wilsond2102462011-01-24 17:43:27 +00004699 struct drm_crtc *crtc;
Jesse Barnesb79d4992010-12-21 13:10:23 -08004700 unsigned long line_time_us;
Chris Wilsond2102462011-01-24 17:43:27 +00004701 int hdisplay, htotal, pixel_size, clock;
Jesse Barnesb79d4992010-12-21 13:10:23 -08004702 int line_count, line_size;
4703 int small, large;
4704 int entries;
4705
4706 if (!latency_ns) {
4707 *fbc_wm = *display_wm = *cursor_wm = 0;
4708 return false;
4709 }
4710
Chris Wilsond2102462011-01-24 17:43:27 +00004711 crtc = intel_get_crtc_for_plane(dev, plane);
4712 hdisplay = crtc->mode.hdisplay;
4713 htotal = crtc->mode.htotal;
4714 clock = crtc->mode.clock;
4715 pixel_size = crtc->fb->bits_per_pixel / 8;
4716
Jesse Barnesb79d4992010-12-21 13:10:23 -08004717 line_time_us = (htotal * 1000) / clock;
4718 line_count = (latency_ns / line_time_us + 1000) / 1000;
4719 line_size = hdisplay * pixel_size;
4720
4721 /* Use the minimum of the small and large buffer method for primary */
4722 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
4723 large = line_count * line_size;
4724
4725 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
4726 *display_wm = entries + display->guard_size;
4727
4728 /*
4729 * Spec says:
4730 * FBC WM = ((Final Primary WM * 64) / number of bytes per line) + 2
4731 */
4732 *fbc_wm = DIV_ROUND_UP(*display_wm * 64, line_size) + 2;
4733
4734 /* calculate the self-refresh watermark for display cursor */
4735 entries = line_count * pixel_size * 64;
4736 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
4737 *cursor_wm = entries + cursor->guard_size;
4738
4739 return ironlake_check_srwm(dev, level,
4740 *fbc_wm, *display_wm, *cursor_wm,
4741 display, cursor);
4742}
4743
Chris Wilsond2102462011-01-24 17:43:27 +00004744static void ironlake_update_wm(struct drm_device *dev)
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08004745{
4746 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond2102462011-01-24 17:43:27 +00004747 int fbc_wm, plane_wm, cursor_wm;
4748 unsigned int enabled;
Zhao Yakuic936f442010-06-12 14:32:26 +08004749
Chris Wilson4ed765f2010-09-11 10:46:47 +01004750 enabled = 0;
Chris Wilson9f405102011-05-12 22:17:14 +01004751 if (g4x_compute_wm0(dev, 0,
4752 &ironlake_display_wm_info,
4753 ILK_LP0_PLANE_LATENCY,
4754 &ironlake_cursor_wm_info,
4755 ILK_LP0_CURSOR_LATENCY,
4756 &plane_wm, &cursor_wm)) {
Chris Wilson4ed765f2010-09-11 10:46:47 +01004757 I915_WRITE(WM0_PIPEA_ILK,
4758 (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
4759 DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
4760 " plane %d, " "cursor: %d\n",
4761 plane_wm, cursor_wm);
Chris Wilsond2102462011-01-24 17:43:27 +00004762 enabled |= 1;
Zhao Yakuic936f442010-06-12 14:32:26 +08004763 }
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08004764
Chris Wilson9f405102011-05-12 22:17:14 +01004765 if (g4x_compute_wm0(dev, 1,
4766 &ironlake_display_wm_info,
4767 ILK_LP0_PLANE_LATENCY,
4768 &ironlake_cursor_wm_info,
4769 ILK_LP0_CURSOR_LATENCY,
4770 &plane_wm, &cursor_wm)) {
Chris Wilson4ed765f2010-09-11 10:46:47 +01004771 I915_WRITE(WM0_PIPEB_ILK,
4772 (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
4773 DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
4774 " plane %d, cursor: %d\n",
4775 plane_wm, cursor_wm);
Chris Wilsond2102462011-01-24 17:43:27 +00004776 enabled |= 2;
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08004777 }
4778
4779 /*
4780 * Calculate and update the self-refresh watermark only when one
4781 * display plane is used.
4782 */
Jesse Barnesb79d4992010-12-21 13:10:23 -08004783 I915_WRITE(WM3_LP_ILK, 0);
4784 I915_WRITE(WM2_LP_ILK, 0);
4785 I915_WRITE(WM1_LP_ILK, 0);
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08004786
Chris Wilsond2102462011-01-24 17:43:27 +00004787 if (!single_plane_enabled(enabled))
Jesse Barnesb79d4992010-12-21 13:10:23 -08004788 return;
Chris Wilsond2102462011-01-24 17:43:27 +00004789 enabled = ffs(enabled) - 1;
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08004790
Jesse Barnesb79d4992010-12-21 13:10:23 -08004791 /* WM1 */
Chris Wilsond2102462011-01-24 17:43:27 +00004792 if (!ironlake_compute_srwm(dev, 1, enabled,
4793 ILK_READ_WM1_LATENCY() * 500,
Jesse Barnesb79d4992010-12-21 13:10:23 -08004794 &ironlake_display_srwm_info,
4795 &ironlake_cursor_srwm_info,
4796 &fbc_wm, &plane_wm, &cursor_wm))
4797 return;
Chris Wilson4ed765f2010-09-11 10:46:47 +01004798
Jesse Barnesb79d4992010-12-21 13:10:23 -08004799 I915_WRITE(WM1_LP_ILK,
4800 WM1_LP_SR_EN |
4801 (ILK_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
4802 (fbc_wm << WM1_LP_FBC_SHIFT) |
4803 (plane_wm << WM1_LP_SR_SHIFT) |
4804 cursor_wm);
Chris Wilson4ed765f2010-09-11 10:46:47 +01004805
Jesse Barnesb79d4992010-12-21 13:10:23 -08004806 /* WM2 */
Chris Wilsond2102462011-01-24 17:43:27 +00004807 if (!ironlake_compute_srwm(dev, 2, enabled,
4808 ILK_READ_WM2_LATENCY() * 500,
Jesse Barnesb79d4992010-12-21 13:10:23 -08004809 &ironlake_display_srwm_info,
4810 &ironlake_cursor_srwm_info,
4811 &fbc_wm, &plane_wm, &cursor_wm))
4812 return;
Chris Wilson4ed765f2010-09-11 10:46:47 +01004813
Jesse Barnesb79d4992010-12-21 13:10:23 -08004814 I915_WRITE(WM2_LP_ILK,
4815 WM2_LP_EN |
4816 (ILK_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
4817 (fbc_wm << WM1_LP_FBC_SHIFT) |
4818 (plane_wm << WM1_LP_SR_SHIFT) |
4819 cursor_wm);
Yuanhan Liu13982612010-12-15 15:42:31 +08004820
4821 /*
Jesse Barnesb79d4992010-12-21 13:10:23 -08004822 * WM3 is unsupported on ILK, probably because we don't have latency
4823 * data for that power state
Yuanhan Liu13982612010-12-15 15:42:31 +08004824 */
Yuanhan Liu13982612010-12-15 15:42:31 +08004825}
4826
Chris Wilsonf681fa22012-04-14 21:56:08 +01004827static void sandybridge_update_wm(struct drm_device *dev)
Yuanhan Liu13982612010-12-15 15:42:31 +08004828{
4829 struct drm_i915_private *dev_priv = dev->dev_private;
Yuanhan Liua0fa62d2010-12-23 16:35:40 +08004830 int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
Jesse Barnes47842642012-01-16 11:57:54 -08004831 u32 val;
Chris Wilsond2102462011-01-24 17:43:27 +00004832 int fbc_wm, plane_wm, cursor_wm;
4833 unsigned int enabled;
Yuanhan Liu13982612010-12-15 15:42:31 +08004834
4835 enabled = 0;
Chris Wilson9f405102011-05-12 22:17:14 +01004836 if (g4x_compute_wm0(dev, 0,
4837 &sandybridge_display_wm_info, latency,
4838 &sandybridge_cursor_wm_info, latency,
4839 &plane_wm, &cursor_wm)) {
Jesse Barnes47842642012-01-16 11:57:54 -08004840 val = I915_READ(WM0_PIPEA_ILK);
4841 val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
4842 I915_WRITE(WM0_PIPEA_ILK, val |
4843 ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
Yuanhan Liu13982612010-12-15 15:42:31 +08004844 DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
4845 " plane %d, " "cursor: %d\n",
4846 plane_wm, cursor_wm);
Chris Wilsond2102462011-01-24 17:43:27 +00004847 enabled |= 1;
Yuanhan Liu13982612010-12-15 15:42:31 +08004848 }
4849
Chris Wilson9f405102011-05-12 22:17:14 +01004850 if (g4x_compute_wm0(dev, 1,
4851 &sandybridge_display_wm_info, latency,
4852 &sandybridge_cursor_wm_info, latency,
4853 &plane_wm, &cursor_wm)) {
Jesse Barnes47842642012-01-16 11:57:54 -08004854 val = I915_READ(WM0_PIPEB_ILK);
4855 val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
4856 I915_WRITE(WM0_PIPEB_ILK, val |
4857 ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
Yuanhan Liu13982612010-12-15 15:42:31 +08004858 DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
4859 " plane %d, cursor: %d\n",
4860 plane_wm, cursor_wm);
Chris Wilsond2102462011-01-24 17:43:27 +00004861 enabled |= 2;
Yuanhan Liu13982612010-12-15 15:42:31 +08004862 }
4863
Jesse Barnesd6c892d2011-10-12 15:36:42 -07004864 /* IVB has 3 pipes */
4865 if (IS_IVYBRIDGE(dev) &&
4866 g4x_compute_wm0(dev, 2,
4867 &sandybridge_display_wm_info, latency,
4868 &sandybridge_cursor_wm_info, latency,
4869 &plane_wm, &cursor_wm)) {
Jesse Barnes47842642012-01-16 11:57:54 -08004870 val = I915_READ(WM0_PIPEC_IVB);
4871 val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
4872 I915_WRITE(WM0_PIPEC_IVB, val |
4873 ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
Jesse Barnesd6c892d2011-10-12 15:36:42 -07004874 DRM_DEBUG_KMS("FIFO watermarks For pipe C -"
4875 " plane %d, cursor: %d\n",
4876 plane_wm, cursor_wm);
4877 enabled |= 3;
4878 }
4879
Yuanhan Liu13982612010-12-15 15:42:31 +08004880 /*
4881 * Calculate and update the self-refresh watermark only when one
4882 * display plane is used.
4883 *
4884 * SNB support 3 levels of watermark.
4885 *
4886 * WM1/WM2/WM2 watermarks have to be enabled in the ascending order,
4887 * and disabled in the descending order
4888 *
4889 */
4890 I915_WRITE(WM3_LP_ILK, 0);
4891 I915_WRITE(WM2_LP_ILK, 0);
4892 I915_WRITE(WM1_LP_ILK, 0);
4893
Jesse Barnesb840d907f2011-12-13 13:19:38 -08004894 if (!single_plane_enabled(enabled) ||
4895 dev_priv->sprite_scaling_enabled)
Yuanhan Liu13982612010-12-15 15:42:31 +08004896 return;
Chris Wilsond2102462011-01-24 17:43:27 +00004897 enabled = ffs(enabled) - 1;
Yuanhan Liu13982612010-12-15 15:42:31 +08004898
4899 /* WM1 */
Chris Wilsond2102462011-01-24 17:43:27 +00004900 if (!ironlake_compute_srwm(dev, 1, enabled,
4901 SNB_READ_WM1_LATENCY() * 500,
Jesse Barnesb79d4992010-12-21 13:10:23 -08004902 &sandybridge_display_srwm_info,
4903 &sandybridge_cursor_srwm_info,
4904 &fbc_wm, &plane_wm, &cursor_wm))
Yuanhan Liu13982612010-12-15 15:42:31 +08004905 return;
4906
4907 I915_WRITE(WM1_LP_ILK,
4908 WM1_LP_SR_EN |
4909 (SNB_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
4910 (fbc_wm << WM1_LP_FBC_SHIFT) |
4911 (plane_wm << WM1_LP_SR_SHIFT) |
4912 cursor_wm);
4913
4914 /* WM2 */
Chris Wilsond2102462011-01-24 17:43:27 +00004915 if (!ironlake_compute_srwm(dev, 2, enabled,
4916 SNB_READ_WM2_LATENCY() * 500,
Jesse Barnesb79d4992010-12-21 13:10:23 -08004917 &sandybridge_display_srwm_info,
4918 &sandybridge_cursor_srwm_info,
4919 &fbc_wm, &plane_wm, &cursor_wm))
Yuanhan Liu13982612010-12-15 15:42:31 +08004920 return;
4921
4922 I915_WRITE(WM2_LP_ILK,
4923 WM2_LP_EN |
4924 (SNB_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
4925 (fbc_wm << WM1_LP_FBC_SHIFT) |
4926 (plane_wm << WM1_LP_SR_SHIFT) |
4927 cursor_wm);
4928
4929 /* WM3 */
Chris Wilsond2102462011-01-24 17:43:27 +00004930 if (!ironlake_compute_srwm(dev, 3, enabled,
4931 SNB_READ_WM3_LATENCY() * 500,
Jesse Barnesb79d4992010-12-21 13:10:23 -08004932 &sandybridge_display_srwm_info,
4933 &sandybridge_cursor_srwm_info,
4934 &fbc_wm, &plane_wm, &cursor_wm))
Yuanhan Liu13982612010-12-15 15:42:31 +08004935 return;
4936
4937 I915_WRITE(WM3_LP_ILK,
4938 WM3_LP_EN |
4939 (SNB_READ_WM3_LATENCY() << WM1_LP_LATENCY_SHIFT) |
4940 (fbc_wm << WM1_LP_FBC_SHIFT) |
4941 (plane_wm << WM1_LP_SR_SHIFT) |
4942 cursor_wm);
4943}
4944
Jesse Barnesb840d907f2011-12-13 13:19:38 -08004945static bool
4946sandybridge_compute_sprite_wm(struct drm_device *dev, int plane,
4947 uint32_t sprite_width, int pixel_size,
4948 const struct intel_watermark_params *display,
4949 int display_latency_ns, int *sprite_wm)
4950{
4951 struct drm_crtc *crtc;
4952 int clock;
4953 int entries, tlb_miss;
4954
4955 crtc = intel_get_crtc_for_plane(dev, plane);
4956 if (crtc->fb == NULL || !crtc->enabled) {
4957 *sprite_wm = display->guard_size;
4958 return false;
4959 }
4960
4961 clock = crtc->mode.clock;
4962
4963 /* Use the small buffer method to calculate the sprite watermark */
4964 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
4965 tlb_miss = display->fifo_size*display->cacheline_size -
4966 sprite_width * 8;
4967 if (tlb_miss > 0)
4968 entries += tlb_miss;
4969 entries = DIV_ROUND_UP(entries, display->cacheline_size);
4970 *sprite_wm = entries + display->guard_size;
4971 if (*sprite_wm > (int)display->max_wm)
4972 *sprite_wm = display->max_wm;
4973
4974 return true;
4975}
4976
4977static bool
4978sandybridge_compute_sprite_srwm(struct drm_device *dev, int plane,
4979 uint32_t sprite_width, int pixel_size,
4980 const struct intel_watermark_params *display,
4981 int latency_ns, int *sprite_wm)
4982{
4983 struct drm_crtc *crtc;
4984 unsigned long line_time_us;
4985 int clock;
4986 int line_count, line_size;
4987 int small, large;
4988 int entries;
4989
4990 if (!latency_ns) {
4991 *sprite_wm = 0;
4992 return false;
4993 }
4994
4995 crtc = intel_get_crtc_for_plane(dev, plane);
4996 clock = crtc->mode.clock;
Hai Lan4e9bb472012-02-15 19:07:02 +08004997 if (!clock) {
4998 *sprite_wm = 0;
4999 return false;
5000 }
Jesse Barnesb840d907f2011-12-13 13:19:38 -08005001
5002 line_time_us = (sprite_width * 1000) / clock;
Hai Lan4e9bb472012-02-15 19:07:02 +08005003 if (!line_time_us) {
5004 *sprite_wm = 0;
5005 return false;
5006 }
5007
Jesse Barnesb840d907f2011-12-13 13:19:38 -08005008 line_count = (latency_ns / line_time_us + 1000) / 1000;
5009 line_size = sprite_width * pixel_size;
5010
5011 /* Use the minimum of the small and large buffer method for primary */
5012 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
5013 large = line_count * line_size;
5014
5015 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
5016 *sprite_wm = entries + display->guard_size;
5017
5018 return *sprite_wm > 0x3ff ? false : true;
5019}
5020
5021static void sandybridge_update_sprite_wm(struct drm_device *dev, int pipe,
5022 uint32_t sprite_width, int pixel_size)
5023{
5024 struct drm_i915_private *dev_priv = dev->dev_private;
5025 int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
Jesse Barnes47842642012-01-16 11:57:54 -08005026 u32 val;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08005027 int sprite_wm, reg;
5028 int ret;
5029
5030 switch (pipe) {
5031 case 0:
5032 reg = WM0_PIPEA_ILK;
5033 break;
5034 case 1:
5035 reg = WM0_PIPEB_ILK;
5036 break;
5037 case 2:
5038 reg = WM0_PIPEC_IVB;
5039 break;
5040 default:
5041 return; /* bad pipe */
5042 }
5043
5044 ret = sandybridge_compute_sprite_wm(dev, pipe, sprite_width, pixel_size,
5045 &sandybridge_display_wm_info,
5046 latency, &sprite_wm);
5047 if (!ret) {
5048 DRM_DEBUG_KMS("failed to compute sprite wm for pipe %d\n",
5049 pipe);
5050 return;
5051 }
5052
Jesse Barnes47842642012-01-16 11:57:54 -08005053 val = I915_READ(reg);
5054 val &= ~WM0_PIPE_SPRITE_MASK;
5055 I915_WRITE(reg, val | (sprite_wm << WM0_PIPE_SPRITE_SHIFT));
Jesse Barnesb840d907f2011-12-13 13:19:38 -08005056 DRM_DEBUG_KMS("sprite watermarks For pipe %d - %d\n", pipe, sprite_wm);
5057
5058
5059 ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
5060 pixel_size,
5061 &sandybridge_display_srwm_info,
5062 SNB_READ_WM1_LATENCY() * 500,
5063 &sprite_wm);
5064 if (!ret) {
5065 DRM_DEBUG_KMS("failed to compute sprite lp1 wm on pipe %d\n",
5066 pipe);
5067 return;
5068 }
5069 I915_WRITE(WM1S_LP_ILK, sprite_wm);
5070
5071 /* Only IVB has two more LP watermarks for sprite */
5072 if (!IS_IVYBRIDGE(dev))
5073 return;
5074
5075 ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
5076 pixel_size,
5077 &sandybridge_display_srwm_info,
5078 SNB_READ_WM2_LATENCY() * 500,
5079 &sprite_wm);
5080 if (!ret) {
5081 DRM_DEBUG_KMS("failed to compute sprite lp2 wm on pipe %d\n",
5082 pipe);
5083 return;
5084 }
5085 I915_WRITE(WM2S_LP_IVB, sprite_wm);
5086
5087 ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
5088 pixel_size,
5089 &sandybridge_display_srwm_info,
5090 SNB_READ_WM3_LATENCY() * 500,
5091 &sprite_wm);
5092 if (!ret) {
5093 DRM_DEBUG_KMS("failed to compute sprite lp3 wm on pipe %d\n",
5094 pipe);
5095 return;
5096 }
5097 I915_WRITE(WM3S_LP_IVB, sprite_wm);
5098}
5099
Shaohua Li7662c8b2009-06-26 11:23:55 +08005100/**
5101 * intel_update_watermarks - update FIFO watermark values based on current modes
5102 *
5103 * Calculate watermark values for the various WM regs based on current mode
5104 * and plane configuration.
5105 *
5106 * There are several cases to deal with here:
5107 * - normal (i.e. non-self-refresh)
5108 * - self-refresh (SR) mode
5109 * - lines are large relative to FIFO size (buffer can hold up to 2)
5110 * - lines are small relative to FIFO size (buffer can hold more than 2
5111 * lines), so need to account for TLB latency
5112 *
5113 * The normal calculation is:
5114 * watermark = dotclock * bytes per pixel * latency
5115 * where latency is platform & configuration dependent (we assume pessimal
5116 * values here).
5117 *
5118 * The SR calculation is:
5119 * watermark = (trunc(latency/line time)+1) * surface width *
5120 * bytes per pixel
5121 * where
5122 * line time = htotal / dotclock
Zhao Yakuifa143212010-06-12 14:32:23 +08005123 * surface width = hdisplay for normal plane and 64 for cursor
Shaohua Li7662c8b2009-06-26 11:23:55 +08005124 * and latency is assumed to be high, as above.
5125 *
5126 * The final value programmed to the register should always be rounded up,
5127 * and include an extra 2 entries to account for clock crossings.
5128 *
5129 * We don't use the sprite, so we can ignore that. And on Crestline we have
5130 * to set the non-SR watermarks to 8.
Chris Wilson5eddb702010-09-11 13:48:45 +01005131 */
Chris Wilsonf681fa22012-04-14 21:56:08 +01005132void intel_update_watermarks(struct drm_device *dev)
Shaohua Li7662c8b2009-06-26 11:23:55 +08005133{
Jesse Barnese70236a2009-09-21 10:42:27 -07005134 struct drm_i915_private *dev_priv = dev->dev_private;
Shaohua Li7662c8b2009-06-26 11:23:55 +08005135
Chris Wilsond2102462011-01-24 17:43:27 +00005136 if (dev_priv->display.update_wm)
5137 dev_priv->display.update_wm(dev);
Shaohua Li7662c8b2009-06-26 11:23:55 +08005138}
5139
Jesse Barnesb840d907f2011-12-13 13:19:38 -08005140void intel_update_sprite_watermarks(struct drm_device *dev, int pipe,
5141 uint32_t sprite_width, int pixel_size)
5142{
5143 struct drm_i915_private *dev_priv = dev->dev_private;
5144
5145 if (dev_priv->display.update_sprite_wm)
5146 dev_priv->display.update_sprite_wm(dev, pipe, sprite_width,
5147 pixel_size);
5148}
5149
Chris Wilsona7615032011-01-12 17:04:08 +00005150static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
5151{
Keith Packard72bbe58c2011-09-26 16:09:45 -07005152 if (i915_panel_use_ssc >= 0)
5153 return i915_panel_use_ssc != 0;
5154 return dev_priv->lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07005155 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00005156}
5157
Jesse Barnes5a354202011-06-24 12:19:22 -07005158/**
5159 * intel_choose_pipe_bpp_dither - figure out what color depth the pipe should send
5160 * @crtc: CRTC structure
Adam Jackson3b5c78a2011-12-13 15:41:00 -08005161 * @mode: requested mode
Jesse Barnes5a354202011-06-24 12:19:22 -07005162 *
5163 * A pipe may be connected to one or more outputs. Based on the depth of the
5164 * attached framebuffer, choose a good color depth to use on the pipe.
5165 *
5166 * If possible, match the pipe depth to the fb depth. In some cases, this
5167 * isn't ideal, because the connected output supports a lesser or restricted
5168 * set of depths. Resolve that here:
5169 * LVDS typically supports only 6bpc, so clamp down in that case
5170 * HDMI supports only 8bpc or 12bpc, so clamp to 8bpc with dither for 10bpc
5171 * Displays may support a restricted set as well, check EDID and clamp as
5172 * appropriate.
Adam Jackson3b5c78a2011-12-13 15:41:00 -08005173 * DP may want to dither down to 6bpc to fit larger modes
Jesse Barnes5a354202011-06-24 12:19:22 -07005174 *
5175 * RETURNS:
5176 * Dithering requirement (i.e. false if display bpc and pipe bpc match,
5177 * true if they don't match).
5178 */
5179static bool intel_choose_pipe_bpp_dither(struct drm_crtc *crtc,
Adam Jackson3b5c78a2011-12-13 15:41:00 -08005180 unsigned int *pipe_bpp,
5181 struct drm_display_mode *mode)
Jesse Barnes5a354202011-06-24 12:19:22 -07005182{
5183 struct drm_device *dev = crtc->dev;
5184 struct drm_i915_private *dev_priv = dev->dev_private;
5185 struct drm_encoder *encoder;
5186 struct drm_connector *connector;
5187 unsigned int display_bpc = UINT_MAX, bpc;
5188
5189 /* Walk the encoders & connectors on this crtc, get min bpc */
5190 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
5191 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5192
5193 if (encoder->crtc != crtc)
5194 continue;
5195
5196 if (intel_encoder->type == INTEL_OUTPUT_LVDS) {
5197 unsigned int lvds_bpc;
5198
5199 if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) ==
5200 LVDS_A3_POWER_UP)
5201 lvds_bpc = 8;
5202 else
5203 lvds_bpc = 6;
5204
5205 if (lvds_bpc < display_bpc) {
Adam Jackson82820492011-10-10 16:33:34 -04005206 DRM_DEBUG_KMS("clamping display bpc (was %d) to LVDS (%d)\n", display_bpc, lvds_bpc);
Jesse Barnes5a354202011-06-24 12:19:22 -07005207 display_bpc = lvds_bpc;
5208 }
5209 continue;
5210 }
5211
5212 if (intel_encoder->type == INTEL_OUTPUT_EDP) {
5213 /* Use VBT settings if we have an eDP panel */
5214 unsigned int edp_bpc = dev_priv->edp.bpp / 3;
5215
5216 if (edp_bpc < display_bpc) {
Adam Jackson82820492011-10-10 16:33:34 -04005217 DRM_DEBUG_KMS("clamping display bpc (was %d) to eDP (%d)\n", display_bpc, edp_bpc);
Jesse Barnes5a354202011-06-24 12:19:22 -07005218 display_bpc = edp_bpc;
5219 }
5220 continue;
5221 }
5222
5223 /* Not one of the known troublemakers, check the EDID */
5224 list_for_each_entry(connector, &dev->mode_config.connector_list,
5225 head) {
5226 if (connector->encoder != encoder)
5227 continue;
5228
Jesse Barnes62ac41a2011-07-28 12:55:14 -07005229 /* Don't use an invalid EDID bpc value */
5230 if (connector->display_info.bpc &&
5231 connector->display_info.bpc < display_bpc) {
Adam Jackson82820492011-10-10 16:33:34 -04005232 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 -07005233 display_bpc = connector->display_info.bpc;
5234 }
5235 }
5236
5237 /*
5238 * HDMI is either 12 or 8, so if the display lets 10bpc sneak
5239 * through, clamp it down. (Note: >12bpc will be caught below.)
5240 */
5241 if (intel_encoder->type == INTEL_OUTPUT_HDMI) {
5242 if (display_bpc > 8 && display_bpc < 12) {
Adam Jackson82820492011-10-10 16:33:34 -04005243 DRM_DEBUG_KMS("forcing bpc to 12 for HDMI\n");
Jesse Barnes5a354202011-06-24 12:19:22 -07005244 display_bpc = 12;
5245 } else {
Adam Jackson82820492011-10-10 16:33:34 -04005246 DRM_DEBUG_KMS("forcing bpc to 8 for HDMI\n");
Jesse Barnes5a354202011-06-24 12:19:22 -07005247 display_bpc = 8;
5248 }
5249 }
5250 }
5251
Adam Jackson3b5c78a2011-12-13 15:41:00 -08005252 if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
5253 DRM_DEBUG_KMS("Dithering DP to 6bpc\n");
5254 display_bpc = 6;
5255 }
5256
Jesse Barnes5a354202011-06-24 12:19:22 -07005257 /*
5258 * We could just drive the pipe at the highest bpc all the time and
5259 * enable dithering as needed, but that costs bandwidth. So choose
5260 * the minimum value that expresses the full color range of the fb but
5261 * also stays within the max display bpc discovered above.
5262 */
5263
5264 switch (crtc->fb->depth) {
5265 case 8:
5266 bpc = 8; /* since we go through a colormap */
5267 break;
5268 case 15:
5269 case 16:
5270 bpc = 6; /* min is 18bpp */
5271 break;
5272 case 24:
Keith Packard578393c2011-09-05 11:53:21 -07005273 bpc = 8;
Jesse Barnes5a354202011-06-24 12:19:22 -07005274 break;
5275 case 30:
Keith Packard578393c2011-09-05 11:53:21 -07005276 bpc = 10;
Jesse Barnes5a354202011-06-24 12:19:22 -07005277 break;
5278 case 48:
Keith Packard578393c2011-09-05 11:53:21 -07005279 bpc = 12;
Jesse Barnes5a354202011-06-24 12:19:22 -07005280 break;
5281 default:
5282 DRM_DEBUG("unsupported depth, assuming 24 bits\n");
5283 bpc = min((unsigned int)8, display_bpc);
5284 break;
5285 }
5286
Keith Packard578393c2011-09-05 11:53:21 -07005287 display_bpc = min(display_bpc, bpc);
5288
Adam Jackson82820492011-10-10 16:33:34 -04005289 DRM_DEBUG_KMS("setting pipe bpc to %d (max display bpc %d)\n",
5290 bpc, display_bpc);
Jesse Barnes5a354202011-06-24 12:19:22 -07005291
Keith Packard578393c2011-09-05 11:53:21 -07005292 *pipe_bpp = display_bpc * 3;
Jesse Barnes5a354202011-06-24 12:19:22 -07005293
5294 return display_bpc != bpc;
5295}
5296
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005297static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
5298{
5299 struct drm_device *dev = crtc->dev;
5300 struct drm_i915_private *dev_priv = dev->dev_private;
5301 int refclk;
5302
5303 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
5304 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
5305 refclk = dev_priv->lvds_ssc_freq * 1000;
5306 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
5307 refclk / 1000);
5308 } else if (!IS_GEN2(dev)) {
5309 refclk = 96000;
5310 } else {
5311 refclk = 48000;
5312 }
5313
5314 return refclk;
5315}
5316
5317static void i9xx_adjust_sdvo_tv_clock(struct drm_display_mode *adjusted_mode,
5318 intel_clock_t *clock)
5319{
5320 /* SDVO TV has fixed PLL values depend on its clock range,
5321 this mirrors vbios setting. */
5322 if (adjusted_mode->clock >= 100000
5323 && adjusted_mode->clock < 140500) {
5324 clock->p1 = 2;
5325 clock->p2 = 10;
5326 clock->n = 3;
5327 clock->m1 = 16;
5328 clock->m2 = 8;
5329 } else if (adjusted_mode->clock >= 140500
5330 && adjusted_mode->clock <= 200000) {
5331 clock->p1 = 1;
5332 clock->p2 = 10;
5333 clock->n = 6;
5334 clock->m1 = 12;
5335 clock->m2 = 8;
5336 }
5337}
5338
Jesse Barnesa7516a02011-12-15 12:30:37 -08005339static void i9xx_update_pll_dividers(struct drm_crtc *crtc,
5340 intel_clock_t *clock,
5341 intel_clock_t *reduced_clock)
5342{
5343 struct drm_device *dev = crtc->dev;
5344 struct drm_i915_private *dev_priv = dev->dev_private;
5345 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5346 int pipe = intel_crtc->pipe;
5347 u32 fp, fp2 = 0;
5348
5349 if (IS_PINEVIEW(dev)) {
5350 fp = (1 << clock->n) << 16 | clock->m1 << 8 | clock->m2;
5351 if (reduced_clock)
5352 fp2 = (1 << reduced_clock->n) << 16 |
5353 reduced_clock->m1 << 8 | reduced_clock->m2;
5354 } else {
5355 fp = clock->n << 16 | clock->m1 << 8 | clock->m2;
5356 if (reduced_clock)
5357 fp2 = reduced_clock->n << 16 | reduced_clock->m1 << 8 |
5358 reduced_clock->m2;
5359 }
5360
5361 I915_WRITE(FP0(pipe), fp);
5362
5363 intel_crtc->lowfreq_avail = false;
5364 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
5365 reduced_clock && i915_powersave) {
5366 I915_WRITE(FP1(pipe), fp2);
5367 intel_crtc->lowfreq_avail = true;
5368 } else {
5369 I915_WRITE(FP1(pipe), fp);
5370 }
5371}
5372
Daniel Vetter93e537a2012-03-28 23:11:26 +02005373static void intel_update_lvds(struct drm_crtc *crtc, intel_clock_t *clock,
5374 struct drm_display_mode *adjusted_mode)
5375{
5376 struct drm_device *dev = crtc->dev;
5377 struct drm_i915_private *dev_priv = dev->dev_private;
5378 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5379 int pipe = intel_crtc->pipe;
5380 u32 temp, lvds_sync = 0;
5381
5382 temp = I915_READ(LVDS);
5383 temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
5384 if (pipe == 1) {
5385 temp |= LVDS_PIPEB_SELECT;
5386 } else {
5387 temp &= ~LVDS_PIPEB_SELECT;
5388 }
5389 /* set the corresponsding LVDS_BORDER bit */
5390 temp |= dev_priv->lvds_border_bits;
5391 /* Set the B0-B3 data pairs corresponding to whether we're going to
5392 * set the DPLLs for dual-channel mode or not.
5393 */
5394 if (clock->p2 == 7)
5395 temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
5396 else
5397 temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
5398
5399 /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
5400 * appropriately here, but we need to look more thoroughly into how
5401 * panels behave in the two modes.
5402 */
5403 /* set the dithering flag on LVDS as needed */
5404 if (INTEL_INFO(dev)->gen >= 4) {
5405 if (dev_priv->lvds_dither)
5406 temp |= LVDS_ENABLE_DITHER;
5407 else
5408 temp &= ~LVDS_ENABLE_DITHER;
5409 }
5410 if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
5411 lvds_sync |= LVDS_HSYNC_POLARITY;
5412 if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
5413 lvds_sync |= LVDS_VSYNC_POLARITY;
5414 if ((temp & (LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY))
5415 != lvds_sync) {
5416 char flags[2] = "-+";
5417 DRM_INFO("Changing LVDS panel from "
5418 "(%chsync, %cvsync) to (%chsync, %cvsync)\n",
5419 flags[!(temp & LVDS_HSYNC_POLARITY)],
5420 flags[!(temp & LVDS_VSYNC_POLARITY)],
5421 flags[!(lvds_sync & LVDS_HSYNC_POLARITY)],
5422 flags[!(lvds_sync & LVDS_VSYNC_POLARITY)]);
5423 temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
5424 temp |= lvds_sync;
5425 }
5426 I915_WRITE(LVDS, temp);
5427}
5428
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005429static void i9xx_update_pll(struct drm_crtc *crtc,
5430 struct drm_display_mode *mode,
5431 struct drm_display_mode *adjusted_mode,
5432 intel_clock_t *clock, intel_clock_t *reduced_clock,
5433 int num_connectors)
5434{
5435 struct drm_device *dev = crtc->dev;
5436 struct drm_i915_private *dev_priv = dev->dev_private;
5437 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5438 int pipe = intel_crtc->pipe;
5439 u32 dpll;
5440 bool is_sdvo;
5441
5442 is_sdvo = intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO) ||
5443 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI);
5444
5445 dpll = DPLL_VGA_MODE_DIS;
5446
5447 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
5448 dpll |= DPLLB_MODE_LVDS;
5449 else
5450 dpll |= DPLLB_MODE_DAC_SERIAL;
5451 if (is_sdvo) {
5452 int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
5453 if (pixel_multiplier > 1) {
5454 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
5455 dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
5456 }
5457 dpll |= DPLL_DVO_HIGH_SPEED;
5458 }
5459 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
5460 dpll |= DPLL_DVO_HIGH_SPEED;
5461
5462 /* compute bitmask from p1 value */
5463 if (IS_PINEVIEW(dev))
5464 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
5465 else {
5466 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5467 if (IS_G4X(dev) && reduced_clock)
5468 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5469 }
5470 switch (clock->p2) {
5471 case 5:
5472 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5473 break;
5474 case 7:
5475 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5476 break;
5477 case 10:
5478 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5479 break;
5480 case 14:
5481 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5482 break;
5483 }
5484 if (INTEL_INFO(dev)->gen >= 4)
5485 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
5486
5487 if (is_sdvo && intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
5488 dpll |= PLL_REF_INPUT_TVCLKINBC;
5489 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
5490 /* XXX: just matching BIOS for now */
5491 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
5492 dpll |= 3;
5493 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
5494 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5495 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5496 else
5497 dpll |= PLL_REF_INPUT_DREFCLK;
5498
5499 dpll |= DPLL_VCO_ENABLE;
5500 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
5501 POSTING_READ(DPLL(pipe));
5502 udelay(150);
5503
5504 /* The LVDS pin pair needs to be on before the DPLLs are enabled.
5505 * This is an exception to the general rule that mode_set doesn't turn
5506 * things on.
5507 */
5508 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
5509 intel_update_lvds(crtc, clock, adjusted_mode);
5510
5511 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
5512 intel_dp_set_m_n(crtc, mode, adjusted_mode);
5513
5514 I915_WRITE(DPLL(pipe), dpll);
5515
5516 /* Wait for the clocks to stabilize. */
5517 POSTING_READ(DPLL(pipe));
5518 udelay(150);
5519
5520 if (INTEL_INFO(dev)->gen >= 4) {
5521 u32 temp = 0;
5522 if (is_sdvo) {
5523 temp = intel_mode_get_pixel_multiplier(adjusted_mode);
5524 if (temp > 1)
5525 temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
5526 else
5527 temp = 0;
5528 }
5529 I915_WRITE(DPLL_MD(pipe), temp);
5530 } else {
5531 /* The pixel multiplier can only be updated once the
5532 * DPLL is enabled and the clocks are stable.
5533 *
5534 * So write it again.
5535 */
5536 I915_WRITE(DPLL(pipe), dpll);
5537 }
5538}
5539
5540static void i8xx_update_pll(struct drm_crtc *crtc,
5541 struct drm_display_mode *adjusted_mode,
5542 intel_clock_t *clock,
5543 int num_connectors)
5544{
5545 struct drm_device *dev = crtc->dev;
5546 struct drm_i915_private *dev_priv = dev->dev_private;
5547 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5548 int pipe = intel_crtc->pipe;
5549 u32 dpll;
5550
5551 dpll = DPLL_VGA_MODE_DIS;
5552
5553 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
5554 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5555 } else {
5556 if (clock->p1 == 2)
5557 dpll |= PLL_P1_DIVIDE_BY_TWO;
5558 else
5559 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5560 if (clock->p2 == 4)
5561 dpll |= PLL_P2_DIVIDE_BY_4;
5562 }
5563
5564 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
5565 /* XXX: just matching BIOS for now */
5566 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
5567 dpll |= 3;
5568 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
5569 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5570 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5571 else
5572 dpll |= PLL_REF_INPUT_DREFCLK;
5573
5574 dpll |= DPLL_VCO_ENABLE;
5575 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
5576 POSTING_READ(DPLL(pipe));
5577 udelay(150);
5578
5579 I915_WRITE(DPLL(pipe), dpll);
5580
5581 /* Wait for the clocks to stabilize. */
5582 POSTING_READ(DPLL(pipe));
5583 udelay(150);
5584
5585 /* The LVDS pin pair needs to be on before the DPLLs are enabled.
5586 * This is an exception to the general rule that mode_set doesn't turn
5587 * things on.
5588 */
5589 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
5590 intel_update_lvds(crtc, clock, adjusted_mode);
5591
5592 /* The pixel multiplier can only be updated once the
5593 * DPLL is enabled and the clocks are stable.
5594 *
5595 * So write it again.
5596 */
5597 I915_WRITE(DPLL(pipe), dpll);
5598}
5599
Eric Anholtf564048e2011-03-30 13:01:02 -07005600static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
5601 struct drm_display_mode *mode,
5602 struct drm_display_mode *adjusted_mode,
5603 int x, int y,
5604 struct drm_framebuffer *old_fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005605{
5606 struct drm_device *dev = crtc->dev;
5607 struct drm_i915_private *dev_priv = dev->dev_private;
5608 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5609 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07005610 int plane = intel_crtc->plane;
Eric Anholtc751ce42010-03-25 11:48:48 -07005611 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07005612 intel_clock_t clock, reduced_clock;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005613 u32 dspcntr, pipeconf, vsyncshift;
5614 bool ok, has_reduced_clock = false, is_sdvo = false;
5615 bool is_lvds = false, is_tv = false, is_dp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08005616 struct drm_mode_config *mode_config = &dev->mode_config;
Chris Wilson5eddb702010-09-11 13:48:45 +01005617 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08005618 const intel_limit_t *limit;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00005619 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08005620
Chris Wilson5eddb702010-09-11 13:48:45 +01005621 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5622 if (encoder->base.crtc != crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08005623 continue;
5624
Chris Wilson5eddb702010-09-11 13:48:45 +01005625 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08005626 case INTEL_OUTPUT_LVDS:
5627 is_lvds = true;
5628 break;
5629 case INTEL_OUTPUT_SDVO:
Eric Anholt7d573822009-01-02 13:33:00 -08005630 case INTEL_OUTPUT_HDMI:
Jesse Barnes79e53942008-11-07 14:24:08 -08005631 is_sdvo = true;
Chris Wilson5eddb702010-09-11 13:48:45 +01005632 if (encoder->needs_tv_clock)
Jesse Barnese2f0ba92009-02-02 15:11:52 -08005633 is_tv = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08005634 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005635 case INTEL_OUTPUT_TVOUT:
5636 is_tv = true;
5637 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005638 case INTEL_OUTPUT_DISPLAYPORT:
5639 is_dp = true;
5640 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005641 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005642
Eric Anholtc751ce42010-03-25 11:48:48 -07005643 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08005644 }
5645
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005646 refclk = i9xx_get_refclk(crtc, num_connectors);
Jesse Barnes79e53942008-11-07 14:24:08 -08005647
Ma Lingd4906092009-03-18 20:13:27 +08005648 /*
5649 * Returns a set of divisors for the desired target clock with the given
5650 * refclk, or FALSE. The returned values represent the clock equation:
5651 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
5652 */
Chris Wilson1b894b52010-12-14 20:04:54 +00005653 limit = intel_limit(crtc, refclk);
Sean Paulcec2f352012-01-10 15:09:36 -08005654 ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
5655 &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08005656 if (!ok) {
5657 DRM_ERROR("Couldn't find PLL settings for mode!\n");
Eric Anholtf564048e2011-03-30 13:01:02 -07005658 return -EINVAL;
5659 }
5660
5661 /* Ensure that the cursor is valid for the new mode before changing... */
5662 intel_crtc_update_cursor(crtc, true);
5663
5664 if (is_lvds && dev_priv->lvds_downclock_avail) {
Sean Paulcec2f352012-01-10 15:09:36 -08005665 /*
5666 * Ensure we match the reduced clock's P to the target clock.
5667 * If the clocks don't match, we can't switch the display clock
5668 * by using the FP0/FP1. In such case we will disable the LVDS
5669 * downclock feature.
5670 */
Eric Anholtf564048e2011-03-30 13:01:02 -07005671 has_reduced_clock = limit->find_pll(limit, crtc,
5672 dev_priv->lvds_downclock,
5673 refclk,
Sean Paulcec2f352012-01-10 15:09:36 -08005674 &clock,
Eric Anholtf564048e2011-03-30 13:01:02 -07005675 &reduced_clock);
Eric Anholtf564048e2011-03-30 13:01:02 -07005676 }
5677
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005678 if (is_sdvo && is_tv)
5679 i9xx_adjust_sdvo_tv_clock(adjusted_mode, &clock);
Eric Anholtf564048e2011-03-30 13:01:02 -07005680
Jesse Barnesa7516a02011-12-15 12:30:37 -08005681 i9xx_update_pll_dividers(crtc, &clock, has_reduced_clock ?
5682 &reduced_clock : NULL);
Eric Anholtf564048e2011-03-30 13:01:02 -07005683
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005684 if (IS_GEN2(dev))
5685 i8xx_update_pll(crtc, adjusted_mode, &clock, num_connectors);
Eric Anholtf564048e2011-03-30 13:01:02 -07005686 else
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005687 i9xx_update_pll(crtc, mode, adjusted_mode, &clock,
5688 has_reduced_clock ? &reduced_clock : NULL,
5689 num_connectors);
Eric Anholtf564048e2011-03-30 13:01:02 -07005690
5691 /* setup pipeconf */
5692 pipeconf = I915_READ(PIPECONF(pipe));
5693
5694 /* Set up the display plane register */
5695 dspcntr = DISPPLANE_GAMMA_ENABLE;
5696
Eric Anholt929c77f2011-03-30 13:01:04 -07005697 if (pipe == 0)
5698 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
5699 else
5700 dspcntr |= DISPPLANE_SEL_PIPE_B;
Eric Anholtf564048e2011-03-30 13:01:02 -07005701
5702 if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
5703 /* Enable pixel doubling when the dot clock is > 90% of the (display)
5704 * core speed.
5705 *
5706 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
5707 * pipe == 0 check?
5708 */
5709 if (mode->clock >
5710 dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
5711 pipeconf |= PIPECONF_DOUBLE_WIDE;
5712 else
5713 pipeconf &= ~PIPECONF_DOUBLE_WIDE;
5714 }
5715
Adam Jackson3b5c78a2011-12-13 15:41:00 -08005716 /* default to 8bpc */
5717 pipeconf &= ~(PIPECONF_BPP_MASK | PIPECONF_DITHER_EN);
5718 if (is_dp) {
5719 if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
5720 pipeconf |= PIPECONF_BPP_6 |
5721 PIPECONF_DITHER_EN |
5722 PIPECONF_DITHER_TYPE_SP;
5723 }
5724 }
5725
Eric Anholtf564048e2011-03-30 13:01:02 -07005726 DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
5727 drm_mode_debug_printmodeline(mode);
5728
Jesse Barnesa7516a02011-12-15 12:30:37 -08005729 if (HAS_PIPE_CXSR(dev)) {
5730 if (intel_crtc->lowfreq_avail) {
Eric Anholtf564048e2011-03-30 13:01:02 -07005731 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
5732 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005733 } else {
Eric Anholtf564048e2011-03-30 13:01:02 -07005734 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
5735 pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
5736 }
5737 }
5738
Keith Packard617cf882012-02-08 13:53:38 -08005739 pipeconf &= ~PIPECONF_INTERLACE_MASK;
Daniel Vetterdbb02572012-01-28 14:49:23 +01005740 if (!IS_GEN2(dev) &&
5741 adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Eric Anholtf564048e2011-03-30 13:01:02 -07005742 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
5743 /* the chip adds 2 halflines automatically */
Eric Anholtf564048e2011-03-30 13:01:02 -07005744 adjusted_mode->crtc_vtotal -= 1;
Eric Anholtf564048e2011-03-30 13:01:02 -07005745 adjusted_mode->crtc_vblank_end -= 1;
Daniel Vetter0529a0d2012-01-28 14:49:24 +01005746 vsyncshift = adjusted_mode->crtc_hsync_start
5747 - adjusted_mode->crtc_htotal/2;
5748 } else {
Keith Packard617cf882012-02-08 13:53:38 -08005749 pipeconf |= PIPECONF_PROGRESSIVE;
Daniel Vetter0529a0d2012-01-28 14:49:24 +01005750 vsyncshift = 0;
5751 }
5752
5753 if (!IS_GEN3(dev))
5754 I915_WRITE(VSYNCSHIFT(pipe), vsyncshift);
Eric Anholtf564048e2011-03-30 13:01:02 -07005755
5756 I915_WRITE(HTOTAL(pipe),
5757 (adjusted_mode->crtc_hdisplay - 1) |
5758 ((adjusted_mode->crtc_htotal - 1) << 16));
5759 I915_WRITE(HBLANK(pipe),
5760 (adjusted_mode->crtc_hblank_start - 1) |
5761 ((adjusted_mode->crtc_hblank_end - 1) << 16));
5762 I915_WRITE(HSYNC(pipe),
5763 (adjusted_mode->crtc_hsync_start - 1) |
5764 ((adjusted_mode->crtc_hsync_end - 1) << 16));
5765
5766 I915_WRITE(VTOTAL(pipe),
5767 (adjusted_mode->crtc_vdisplay - 1) |
5768 ((adjusted_mode->crtc_vtotal - 1) << 16));
5769 I915_WRITE(VBLANK(pipe),
5770 (adjusted_mode->crtc_vblank_start - 1) |
5771 ((adjusted_mode->crtc_vblank_end - 1) << 16));
5772 I915_WRITE(VSYNC(pipe),
5773 (adjusted_mode->crtc_vsync_start - 1) |
5774 ((adjusted_mode->crtc_vsync_end - 1) << 16));
5775
5776 /* pipesrc and dspsize control the size that is scaled from,
5777 * which should always be the user's requested size.
5778 */
Eric Anholt929c77f2011-03-30 13:01:04 -07005779 I915_WRITE(DSPSIZE(plane),
5780 ((mode->vdisplay - 1) << 16) |
5781 (mode->hdisplay - 1));
5782 I915_WRITE(DSPPOS(plane), 0);
Eric Anholtf564048e2011-03-30 13:01:02 -07005783 I915_WRITE(PIPESRC(pipe),
5784 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
5785
Eric Anholtf564048e2011-03-30 13:01:02 -07005786 I915_WRITE(PIPECONF(pipe), pipeconf);
5787 POSTING_READ(PIPECONF(pipe));
Eric Anholt929c77f2011-03-30 13:01:04 -07005788 intel_enable_pipe(dev_priv, pipe, false);
Eric Anholtf564048e2011-03-30 13:01:02 -07005789
5790 intel_wait_for_vblank(dev, pipe);
5791
Eric Anholtf564048e2011-03-30 13:01:02 -07005792 I915_WRITE(DSPCNTR(plane), dspcntr);
5793 POSTING_READ(DSPCNTR(plane));
Keith Packard284d9522011-06-06 17:12:49 -07005794 intel_enable_plane(dev_priv, plane, pipe);
Eric Anholtf564048e2011-03-30 13:01:02 -07005795
5796 ret = intel_pipe_set_base(crtc, x, y, old_fb);
5797
5798 intel_update_watermarks(dev);
5799
Eric Anholtf564048e2011-03-30 13:01:02 -07005800 return ret;
5801}
5802
Keith Packard9fb526d2011-09-26 22:24:57 -07005803/*
5804 * Initialize reference clocks when the driver loads
5805 */
5806void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07005807{
5808 struct drm_i915_private *dev_priv = dev->dev_private;
5809 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005810 struct intel_encoder *encoder;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005811 u32 temp;
5812 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005813 bool has_cpu_edp = false;
5814 bool has_pch_edp = false;
5815 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07005816 bool has_ck505 = false;
5817 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005818
5819 /* We need to take the global config into account */
Keith Packard199e5d72011-09-22 12:01:57 -07005820 list_for_each_entry(encoder, &mode_config->encoder_list,
5821 base.head) {
5822 switch (encoder->type) {
5823 case INTEL_OUTPUT_LVDS:
5824 has_panel = true;
5825 has_lvds = true;
5826 break;
5827 case INTEL_OUTPUT_EDP:
5828 has_panel = true;
5829 if (intel_encoder_is_pch_edp(&encoder->base))
5830 has_pch_edp = true;
5831 else
5832 has_cpu_edp = true;
5833 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005834 }
5835 }
5836
Keith Packard99eb6a02011-09-26 14:29:12 -07005837 if (HAS_PCH_IBX(dev)) {
5838 has_ck505 = dev_priv->display_clock_mode;
5839 can_ssc = has_ck505;
5840 } else {
5841 has_ck505 = false;
5842 can_ssc = true;
5843 }
5844
5845 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_pch_edp %d has_cpu_edp %d has_ck505 %d\n",
5846 has_panel, has_lvds, has_pch_edp, has_cpu_edp,
5847 has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005848
5849 /* Ironlake: try to setup display ref clock before DPLL
5850 * enabling. This is only under driver's control after
5851 * PCH B stepping, previous chipset stepping should be
5852 * ignoring this setting.
5853 */
5854 temp = I915_READ(PCH_DREF_CONTROL);
5855 /* Always enable nonspread source */
5856 temp &= ~DREF_NONSPREAD_SOURCE_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005857
Keith Packard99eb6a02011-09-26 14:29:12 -07005858 if (has_ck505)
5859 temp |= DREF_NONSPREAD_CK505_ENABLE;
5860 else
5861 temp |= DREF_NONSPREAD_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005862
Keith Packard199e5d72011-09-22 12:01:57 -07005863 if (has_panel) {
5864 temp &= ~DREF_SSC_SOURCE_MASK;
5865 temp |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005866
Keith Packard199e5d72011-09-22 12:01:57 -07005867 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07005868 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005869 DRM_DEBUG_KMS("Using SSC on panel\n");
Jesse Barnes13d83a62011-08-03 12:59:20 -07005870 temp |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02005871 } else
5872 temp &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005873
5874 /* Get SSC going before enabling the outputs */
5875 I915_WRITE(PCH_DREF_CONTROL, temp);
5876 POSTING_READ(PCH_DREF_CONTROL);
5877 udelay(200);
5878
Jesse Barnes13d83a62011-08-03 12:59:20 -07005879 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5880
5881 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07005882 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07005883 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005884 DRM_DEBUG_KMS("Using SSC on eDP\n");
Jesse Barnes13d83a62011-08-03 12:59:20 -07005885 temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005886 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07005887 else
5888 temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005889 } else
5890 temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5891
5892 I915_WRITE(PCH_DREF_CONTROL, temp);
5893 POSTING_READ(PCH_DREF_CONTROL);
5894 udelay(200);
5895 } else {
5896 DRM_DEBUG_KMS("Disabling SSC entirely\n");
5897
5898 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5899
5900 /* Turn off CPU output */
5901 temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5902
5903 I915_WRITE(PCH_DREF_CONTROL, temp);
5904 POSTING_READ(PCH_DREF_CONTROL);
5905 udelay(200);
5906
5907 /* Turn off the SSC source */
5908 temp &= ~DREF_SSC_SOURCE_MASK;
5909 temp |= DREF_SSC_SOURCE_DISABLE;
5910
5911 /* Turn off SSC1 */
5912 temp &= ~ DREF_SSC1_ENABLE;
5913
Jesse Barnes13d83a62011-08-03 12:59:20 -07005914 I915_WRITE(PCH_DREF_CONTROL, temp);
5915 POSTING_READ(PCH_DREF_CONTROL);
5916 udelay(200);
5917 }
5918}
5919
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005920static int ironlake_get_refclk(struct drm_crtc *crtc)
5921{
5922 struct drm_device *dev = crtc->dev;
5923 struct drm_i915_private *dev_priv = dev->dev_private;
5924 struct intel_encoder *encoder;
5925 struct drm_mode_config *mode_config = &dev->mode_config;
5926 struct intel_encoder *edp_encoder = NULL;
5927 int num_connectors = 0;
5928 bool is_lvds = false;
5929
5930 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5931 if (encoder->base.crtc != crtc)
5932 continue;
5933
5934 switch (encoder->type) {
5935 case INTEL_OUTPUT_LVDS:
5936 is_lvds = true;
5937 break;
5938 case INTEL_OUTPUT_EDP:
5939 edp_encoder = encoder;
5940 break;
5941 }
5942 num_connectors++;
5943 }
5944
5945 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
5946 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
5947 dev_priv->lvds_ssc_freq);
5948 return dev_priv->lvds_ssc_freq * 1000;
5949 }
5950
5951 return 120000;
5952}
5953
Eric Anholtf564048e2011-03-30 13:01:02 -07005954static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
5955 struct drm_display_mode *mode,
5956 struct drm_display_mode *adjusted_mode,
5957 int x, int y,
5958 struct drm_framebuffer *old_fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005959{
5960 struct drm_device *dev = crtc->dev;
5961 struct drm_i915_private *dev_priv = dev->dev_private;
5962 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5963 int pipe = intel_crtc->pipe;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00005964 int plane = intel_crtc->plane;
Jesse Barnes79e53942008-11-07 14:24:08 -08005965 int refclk, num_connectors = 0;
5966 intel_clock_t clock, reduced_clock;
5967 u32 dpll, fp = 0, fp2 = 0, dspcntr, pipeconf;
Eric Anholta07d6782011-03-30 13:01:08 -07005968 bool ok, has_reduced_clock = false, is_sdvo = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08005969 bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08005970 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnese3aef172012-04-10 11:58:03 -07005971 struct intel_encoder *encoder, *edp_encoder = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08005972 const intel_limit_t *limit;
5973 int ret;
5974 struct fdi_m_n m_n = {0};
Eric Anholtfae14982011-03-30 13:01:09 -07005975 u32 temp;
Jesse Barnes79e53942008-11-07 14:24:08 -08005976 u32 lvds_sync = 0;
Jesse Barnes5a354202011-06-24 12:19:22 -07005977 int target_clock, pixel_multiplier, lane, link_bw, factor;
5978 unsigned int pipe_bpp;
5979 bool dither;
Jesse Barnese3aef172012-04-10 11:58:03 -07005980 bool is_cpu_edp = false, is_pch_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08005981
Jesse Barnes79e53942008-11-07 14:24:08 -08005982 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5983 if (encoder->base.crtc != crtc)
5984 continue;
5985
5986 switch (encoder->type) {
5987 case INTEL_OUTPUT_LVDS:
5988 is_lvds = true;
5989 break;
5990 case INTEL_OUTPUT_SDVO:
5991 case INTEL_OUTPUT_HDMI:
5992 is_sdvo = true;
5993 if (encoder->needs_tv_clock)
5994 is_tv = true;
5995 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005996 case INTEL_OUTPUT_TVOUT:
5997 is_tv = true;
5998 break;
5999 case INTEL_OUTPUT_ANALOG:
6000 is_crt = true;
6001 break;
6002 case INTEL_OUTPUT_DISPLAYPORT:
6003 is_dp = true;
6004 break;
6005 case INTEL_OUTPUT_EDP:
Jesse Barnese3aef172012-04-10 11:58:03 -07006006 is_dp = true;
6007 if (intel_encoder_is_pch_edp(&encoder->base))
6008 is_pch_edp = true;
6009 else
6010 is_cpu_edp = true;
6011 edp_encoder = encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006012 break;
6013 }
6014
Kristian Høgsberg43565a02009-02-13 20:56:52 -05006015 num_connectors++;
6016 }
6017
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006018 refclk = ironlake_get_refclk(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006019
6020 /*
6021 * Returns a set of divisors for the desired target clock with the given
6022 * refclk, or FALSE. The returned values represent the clock equation:
6023 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
6024 */
6025 limit = intel_limit(crtc, refclk);
Sean Paulcec2f352012-01-10 15:09:36 -08006026 ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
6027 &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006028 if (!ok) {
6029 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6030 return -EINVAL;
6031 }
6032
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006033 /* Ensure that the cursor is valid for the new mode before changing... */
Chris Wilson6b383a72010-09-13 13:54:26 +01006034 intel_crtc_update_cursor(crtc, true);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006035
Zhao Yakuiddc90032010-01-06 22:05:56 +08006036 if (is_lvds && dev_priv->lvds_downclock_avail) {
Sean Paulcec2f352012-01-10 15:09:36 -08006037 /*
6038 * Ensure we match the reduced clock's P to the target clock.
6039 * If the clocks don't match, we can't switch the display clock
6040 * by using the FP0/FP1. In such case we will disable the LVDS
6041 * downclock feature.
6042 */
Zhao Yakuiddc90032010-01-06 22:05:56 +08006043 has_reduced_clock = limit->find_pll(limit, crtc,
Chris Wilson5eddb702010-09-11 13:48:45 +01006044 dev_priv->lvds_downclock,
6045 refclk,
Sean Paulcec2f352012-01-10 15:09:36 -08006046 &clock,
Chris Wilson5eddb702010-09-11 13:48:45 +01006047 &reduced_clock);
Jesse Barnes652c3932009-08-17 13:31:43 -07006048 }
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08006049 /* SDVO TV has fixed PLL values depend on its clock range,
6050 this mirrors vbios setting. */
6051 if (is_sdvo && is_tv) {
6052 if (adjusted_mode->clock >= 100000
Chris Wilson5eddb702010-09-11 13:48:45 +01006053 && adjusted_mode->clock < 140500) {
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08006054 clock.p1 = 2;
6055 clock.p2 = 10;
6056 clock.n = 3;
6057 clock.m1 = 16;
6058 clock.m2 = 8;
6059 } else if (adjusted_mode->clock >= 140500
Chris Wilson5eddb702010-09-11 13:48:45 +01006060 && adjusted_mode->clock <= 200000) {
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08006061 clock.p1 = 1;
6062 clock.p2 = 10;
6063 clock.n = 6;
6064 clock.m1 = 12;
6065 clock.m2 = 8;
6066 }
6067 }
6068
Zhenyu Wang2c072452009-06-05 15:38:42 +08006069 /* FDI link */
Eric Anholt8febb292011-03-30 13:01:07 -07006070 pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
6071 lane = 0;
6072 /* CPU eDP doesn't require FDI link, so just set DP M/N
6073 according to current link config */
Jesse Barnese3aef172012-04-10 11:58:03 -07006074 if (is_cpu_edp) {
Eric Anholt8febb292011-03-30 13:01:07 -07006075 target_clock = mode->clock;
Jesse Barnese3aef172012-04-10 11:58:03 -07006076 intel_edp_link_config(edp_encoder, &lane, &link_bw);
Eric Anholt8febb292011-03-30 13:01:07 -07006077 } else {
6078 /* [e]DP over FDI requires target mode clock
6079 instead of link clock */
Jesse Barnese3aef172012-04-10 11:58:03 -07006080 if (is_dp)
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08006081 target_clock = mode->clock;
Eric Anholt8febb292011-03-30 13:01:07 -07006082 else
6083 target_clock = adjusted_mode->clock;
Chris Wilson021357a2010-09-07 20:54:59 +01006084
Eric Anholt8febb292011-03-30 13:01:07 -07006085 /* FDI is a binary signal running at ~2.7GHz, encoding
6086 * each output octet as 10 bits. The actual frequency
6087 * is stored as a divider into a 100MHz clock, and the
6088 * mode pixel clock is stored in units of 1KHz.
6089 * Hence the bw of each lane in terms of the mode signal
6090 * is:
6091 */
6092 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08006093 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08006094
Eric Anholt8febb292011-03-30 13:01:07 -07006095 /* determine panel color depth */
6096 temp = I915_READ(PIPECONF(pipe));
6097 temp &= ~PIPE_BPC_MASK;
Adam Jackson3b5c78a2011-12-13 15:41:00 -08006098 dither = intel_choose_pipe_bpp_dither(crtc, &pipe_bpp, mode);
Jesse Barnes5a354202011-06-24 12:19:22 -07006099 switch (pipe_bpp) {
6100 case 18:
6101 temp |= PIPE_6BPC;
6102 break;
6103 case 24:
Eric Anholt8febb292011-03-30 13:01:07 -07006104 temp |= PIPE_8BPC;
Eric Anholt8febb292011-03-30 13:01:07 -07006105 break;
Jesse Barnes5a354202011-06-24 12:19:22 -07006106 case 30:
6107 temp |= PIPE_10BPC;
Eric Anholt8febb292011-03-30 13:01:07 -07006108 break;
Jesse Barnes5a354202011-06-24 12:19:22 -07006109 case 36:
6110 temp |= PIPE_12BPC;
Eric Anholt8febb292011-03-30 13:01:07 -07006111 break;
6112 default:
Jesse Barnes62ac41a2011-07-28 12:55:14 -07006113 WARN(1, "intel_choose_pipe_bpp returned invalid value %d\n",
6114 pipe_bpp);
Jesse Barnes5a354202011-06-24 12:19:22 -07006115 temp |= PIPE_8BPC;
6116 pipe_bpp = 24;
6117 break;
Eric Anholt8febb292011-03-30 13:01:07 -07006118 }
6119
Jesse Barnes5a354202011-06-24 12:19:22 -07006120 intel_crtc->bpp = pipe_bpp;
6121 I915_WRITE(PIPECONF(pipe), temp);
6122
Eric Anholt8febb292011-03-30 13:01:07 -07006123 if (!lane) {
6124 /*
6125 * Account for spread spectrum to avoid
6126 * oversubscribing the link. Max center spread
6127 * is 2.5%; use 5% for safety's sake.
6128 */
Jesse Barnes5a354202011-06-24 12:19:22 -07006129 u32 bps = target_clock * intel_crtc->bpp * 21 / 20;
Eric Anholt8febb292011-03-30 13:01:07 -07006130 lane = bps / (link_bw * 8) + 1;
6131 }
6132
6133 intel_crtc->fdi_lanes = lane;
6134
6135 if (pixel_multiplier > 1)
6136 link_bw *= pixel_multiplier;
Jesse Barnes5a354202011-06-24 12:19:22 -07006137 ironlake_compute_m_n(intel_crtc->bpp, lane, target_clock, link_bw,
6138 &m_n);
Eric Anholt8febb292011-03-30 13:01:07 -07006139
Eric Anholta07d6782011-03-30 13:01:08 -07006140 fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
6141 if (has_reduced_clock)
6142 fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
6143 reduced_clock.m2;
Jesse Barnes79e53942008-11-07 14:24:08 -08006144
Chris Wilsonc1858122010-12-03 21:35:48 +00006145 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07006146 factor = 21;
6147 if (is_lvds) {
6148 if ((intel_panel_use_ssc(dev_priv) &&
6149 dev_priv->lvds_ssc_freq == 100) ||
6150 (I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP)
6151 factor = 25;
6152 } else if (is_sdvo && is_tv)
6153 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00006154
Jesse Barnescb0e0932011-07-28 14:50:30 -07006155 if (clock.m < factor * clock.n)
Eric Anholt8febb292011-03-30 13:01:07 -07006156 fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00006157
Chris Wilson5eddb702010-09-11 13:48:45 +01006158 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006159
Eric Anholta07d6782011-03-30 13:01:08 -07006160 if (is_lvds)
6161 dpll |= DPLLB_MODE_LVDS;
6162 else
6163 dpll |= DPLLB_MODE_DAC_SERIAL;
6164 if (is_sdvo) {
6165 int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
6166 if (pixel_multiplier > 1) {
6167 dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Jesse Barnes79e53942008-11-07 14:24:08 -08006168 }
Eric Anholta07d6782011-03-30 13:01:08 -07006169 dpll |= DPLL_DVO_HIGH_SPEED;
6170 }
Jesse Barnese3aef172012-04-10 11:58:03 -07006171 if (is_dp && !is_cpu_edp)
Eric Anholta07d6782011-03-30 13:01:08 -07006172 dpll |= DPLL_DVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08006173
Eric Anholta07d6782011-03-30 13:01:08 -07006174 /* compute bitmask from p1 value */
6175 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6176 /* also FPA1 */
6177 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
6178
6179 switch (clock.p2) {
6180 case 5:
6181 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6182 break;
6183 case 7:
6184 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6185 break;
6186 case 10:
6187 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6188 break;
6189 case 14:
6190 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6191 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08006192 }
6193
6194 if (is_sdvo && is_tv)
6195 dpll |= PLL_REF_INPUT_TVCLKINBC;
6196 else if (is_tv)
6197 /* XXX: just matching BIOS for now */
6198 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
6199 dpll |= 3;
Chris Wilsona7615032011-01-12 17:04:08 +00006200 else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Jesse Barnes79e53942008-11-07 14:24:08 -08006201 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6202 else
6203 dpll |= PLL_REF_INPUT_DREFCLK;
6204
6205 /* setup pipeconf */
Chris Wilson5eddb702010-09-11 13:48:45 +01006206 pipeconf = I915_READ(PIPECONF(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006207
6208 /* Set up the display plane register */
6209 dspcntr = DISPPLANE_GAMMA_ENABLE;
6210
Jesse Barnesf7cb34d2011-10-12 10:49:14 -07006211 DRM_DEBUG_KMS("Mode for pipe %d:\n", pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08006212 drm_mode_debug_printmodeline(mode);
6213
Jesse Barnes5c5313c2010-10-07 16:01:11 -07006214 /* PCH eDP needs FDI, but CPU eDP does not */
Jesse Barnes4b645f12011-10-12 09:51:31 -07006215 if (!intel_crtc->no_pll) {
Jesse Barnese3aef172012-04-10 11:58:03 -07006216 if (!is_cpu_edp) {
Jesse Barnes4b645f12011-10-12 09:51:31 -07006217 I915_WRITE(PCH_FP0(pipe), fp);
6218 I915_WRITE(PCH_DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01006219
Jesse Barnes4b645f12011-10-12 09:51:31 -07006220 POSTING_READ(PCH_DPLL(pipe));
6221 udelay(150);
6222 }
6223 } else {
6224 if (dpll == (I915_READ(PCH_DPLL(0)) & 0x7fffffff) &&
6225 fp == I915_READ(PCH_FP0(0))) {
6226 intel_crtc->use_pll_a = true;
6227 DRM_DEBUG_KMS("using pipe a dpll\n");
6228 } else if (dpll == (I915_READ(PCH_DPLL(1)) & 0x7fffffff) &&
6229 fp == I915_READ(PCH_FP0(1))) {
6230 intel_crtc->use_pll_a = false;
6231 DRM_DEBUG_KMS("using pipe b dpll\n");
6232 } else {
6233 DRM_DEBUG_KMS("no matching PLL configuration for pipe 2\n");
6234 return -EINVAL;
6235 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006236 }
6237
6238 /* The LVDS pin pair needs to be on before the DPLLs are enabled.
6239 * This is an exception to the general rule that mode_set doesn't turn
6240 * things on.
6241 */
6242 if (is_lvds) {
Eric Anholtfae14982011-03-30 13:01:09 -07006243 temp = I915_READ(PCH_LVDS);
Chris Wilson5eddb702010-09-11 13:48:45 +01006244 temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
Jesse Barnes7885d202012-01-12 14:51:17 -08006245 if (HAS_PCH_CPT(dev)) {
6246 temp &= ~PORT_TRANS_SEL_MASK;
Jesse Barnes4b645f12011-10-12 09:51:31 -07006247 temp |= PORT_TRANS_SEL_CPT(pipe);
Jesse Barnes7885d202012-01-12 14:51:17 -08006248 } else {
6249 if (pipe == 1)
6250 temp |= LVDS_PIPEB_SELECT;
6251 else
6252 temp &= ~LVDS_PIPEB_SELECT;
6253 }
Jesse Barnes4b645f12011-10-12 09:51:31 -07006254
Zhao Yakuia3e17eb2009-10-10 10:42:37 +08006255 /* set the corresponsding LVDS_BORDER bit */
Chris Wilson5eddb702010-09-11 13:48:45 +01006256 temp |= dev_priv->lvds_border_bits;
Jesse Barnes79e53942008-11-07 14:24:08 -08006257 /* Set the B0-B3 data pairs corresponding to whether we're going to
6258 * set the DPLLs for dual-channel mode or not.
6259 */
6260 if (clock.p2 == 7)
Chris Wilson5eddb702010-09-11 13:48:45 +01006261 temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
Jesse Barnes79e53942008-11-07 14:24:08 -08006262 else
Chris Wilson5eddb702010-09-11 13:48:45 +01006263 temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
Jesse Barnes79e53942008-11-07 14:24:08 -08006264
6265 /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
6266 * appropriately here, but we need to look more thoroughly into how
6267 * panels behave in the two modes.
6268 */
Bryan Freedaa9b5002011-01-12 13:43:19 -08006269 if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
6270 lvds_sync |= LVDS_HSYNC_POLARITY;
6271 if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
6272 lvds_sync |= LVDS_VSYNC_POLARITY;
6273 if ((temp & (LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY))
6274 != lvds_sync) {
6275 char flags[2] = "-+";
6276 DRM_INFO("Changing LVDS panel from "
6277 "(%chsync, %cvsync) to (%chsync, %cvsync)\n",
6278 flags[!(temp & LVDS_HSYNC_POLARITY)],
6279 flags[!(temp & LVDS_VSYNC_POLARITY)],
6280 flags[!(lvds_sync & LVDS_HSYNC_POLARITY)],
6281 flags[!(lvds_sync & LVDS_VSYNC_POLARITY)]);
6282 temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
6283 temp |= lvds_sync;
6284 }
Eric Anholtfae14982011-03-30 13:01:09 -07006285 I915_WRITE(PCH_LVDS, temp);
Jesse Barnes79e53942008-11-07 14:24:08 -08006286 }
Jesse Barnes434ed092010-09-07 14:48:06 -07006287
Eric Anholt8febb292011-03-30 13:01:07 -07006288 pipeconf &= ~PIPECONF_DITHER_EN;
6289 pipeconf &= ~PIPECONF_DITHER_TYPE_MASK;
Jesse Barnes5a354202011-06-24 12:19:22 -07006290 if ((is_lvds && dev_priv->lvds_dither) || dither) {
Eric Anholt8febb292011-03-30 13:01:07 -07006291 pipeconf |= PIPECONF_DITHER_EN;
Daniel Vetterf74974c2011-10-11 17:27:51 +02006292 pipeconf |= PIPECONF_DITHER_TYPE_SP;
Jesse Barnes434ed092010-09-07 14:48:06 -07006293 }
Jesse Barnese3aef172012-04-10 11:58:03 -07006294 if (is_dp && !is_cpu_edp) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07006295 intel_dp_set_m_n(crtc, mode, adjusted_mode);
Eric Anholt8febb292011-03-30 13:01:07 -07006296 } else {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08006297 /* For non-DP output, clear any trans DP clock recovery setting.*/
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006298 I915_WRITE(TRANSDATA_M1(pipe), 0);
6299 I915_WRITE(TRANSDATA_N1(pipe), 0);
6300 I915_WRITE(TRANSDPLINK_M1(pipe), 0);
6301 I915_WRITE(TRANSDPLINK_N1(pipe), 0);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08006302 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006303
Jesse Barnese3aef172012-04-10 11:58:03 -07006304 if (!intel_crtc->no_pll && (!edp_encoder || is_pch_edp)) {
Eric Anholtfae14982011-03-30 13:01:09 -07006305 I915_WRITE(PCH_DPLL(pipe), dpll);
Chris Wilson5eddb702010-09-11 13:48:45 +01006306
Zhenyu Wang32f9d652009-07-24 01:00:32 +08006307 /* Wait for the clocks to stabilize. */
Eric Anholtfae14982011-03-30 13:01:09 -07006308 POSTING_READ(PCH_DPLL(pipe));
Zhenyu Wang32f9d652009-07-24 01:00:32 +08006309 udelay(150);
6310
Eric Anholt8febb292011-03-30 13:01:07 -07006311 /* The pixel multiplier can only be updated once the
6312 * DPLL is enabled and the clocks are stable.
6313 *
6314 * So write it again.
6315 */
Eric Anholtfae14982011-03-30 13:01:09 -07006316 I915_WRITE(PCH_DPLL(pipe), dpll);
Jesse Barnes79e53942008-11-07 14:24:08 -08006317 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006318
Chris Wilson5eddb702010-09-11 13:48:45 +01006319 intel_crtc->lowfreq_avail = false;
Jesse Barnes4b645f12011-10-12 09:51:31 -07006320 if (!intel_crtc->no_pll) {
6321 if (is_lvds && has_reduced_clock && i915_powersave) {
6322 I915_WRITE(PCH_FP1(pipe), fp2);
6323 intel_crtc->lowfreq_avail = true;
6324 if (HAS_PIPE_CXSR(dev)) {
6325 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
6326 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
6327 }
6328 } else {
6329 I915_WRITE(PCH_FP1(pipe), fp);
6330 if (HAS_PIPE_CXSR(dev)) {
6331 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
6332 pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
6333 }
Jesse Barnes652c3932009-08-17 13:31:43 -07006334 }
6335 }
6336
Keith Packard617cf882012-02-08 13:53:38 -08006337 pipeconf &= ~PIPECONF_INTERLACE_MASK;
Krzysztof Halasa734b4152010-05-25 18:41:46 +02006338 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Daniel Vetter5def4742012-01-28 14:49:22 +01006339 pipeconf |= PIPECONF_INTERLACED_ILK;
Krzysztof Halasa734b4152010-05-25 18:41:46 +02006340 /* the chip adds 2 halflines automatically */
Krzysztof Halasa734b4152010-05-25 18:41:46 +02006341 adjusted_mode->crtc_vtotal -= 1;
Krzysztof Halasa734b4152010-05-25 18:41:46 +02006342 adjusted_mode->crtc_vblank_end -= 1;
Daniel Vetter0529a0d2012-01-28 14:49:24 +01006343 I915_WRITE(VSYNCSHIFT(pipe),
6344 adjusted_mode->crtc_hsync_start
6345 - adjusted_mode->crtc_htotal/2);
6346 } else {
Keith Packard617cf882012-02-08 13:53:38 -08006347 pipeconf |= PIPECONF_PROGRESSIVE;
Daniel Vetter0529a0d2012-01-28 14:49:24 +01006348 I915_WRITE(VSYNCSHIFT(pipe), 0);
6349 }
Krzysztof Halasa734b4152010-05-25 18:41:46 +02006350
Chris Wilson5eddb702010-09-11 13:48:45 +01006351 I915_WRITE(HTOTAL(pipe),
6352 (adjusted_mode->crtc_hdisplay - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08006353 ((adjusted_mode->crtc_htotal - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01006354 I915_WRITE(HBLANK(pipe),
6355 (adjusted_mode->crtc_hblank_start - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08006356 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01006357 I915_WRITE(HSYNC(pipe),
6358 (adjusted_mode->crtc_hsync_start - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08006359 ((adjusted_mode->crtc_hsync_end - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01006360
6361 I915_WRITE(VTOTAL(pipe),
6362 (adjusted_mode->crtc_vdisplay - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08006363 ((adjusted_mode->crtc_vtotal - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01006364 I915_WRITE(VBLANK(pipe),
6365 (adjusted_mode->crtc_vblank_start - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08006366 ((adjusted_mode->crtc_vblank_end - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01006367 I915_WRITE(VSYNC(pipe),
6368 (adjusted_mode->crtc_vsync_start - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08006369 ((adjusted_mode->crtc_vsync_end - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01006370
Eric Anholt8febb292011-03-30 13:01:07 -07006371 /* pipesrc controls the size that is scaled from, which should
6372 * always be the user's requested size.
Jesse Barnes79e53942008-11-07 14:24:08 -08006373 */
Chris Wilson5eddb702010-09-11 13:48:45 +01006374 I915_WRITE(PIPESRC(pipe),
6375 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
Zhenyu Wang2c072452009-06-05 15:38:42 +08006376
Eric Anholt8febb292011-03-30 13:01:07 -07006377 I915_WRITE(PIPE_DATA_M1(pipe), TU_SIZE(m_n.tu) | m_n.gmch_m);
6378 I915_WRITE(PIPE_DATA_N1(pipe), m_n.gmch_n);
6379 I915_WRITE(PIPE_LINK_M1(pipe), m_n.link_m);
6380 I915_WRITE(PIPE_LINK_N1(pipe), m_n.link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006381
Jesse Barnese3aef172012-04-10 11:58:03 -07006382 if (is_cpu_edp)
Eric Anholt8febb292011-03-30 13:01:07 -07006383 ironlake_set_pll_edp(crtc, adjusted_mode->clock);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006384
Chris Wilson5eddb702010-09-11 13:48:45 +01006385 I915_WRITE(PIPECONF(pipe), pipeconf);
6386 POSTING_READ(PIPECONF(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006387
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006388 intel_wait_for_vblank(dev, pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08006389
Chris Wilson5eddb702010-09-11 13:48:45 +01006390 I915_WRITE(DSPCNTR(plane), dspcntr);
Jesse Barnesb24e7172011-01-04 15:09:30 -08006391 POSTING_READ(DSPCNTR(plane));
Jesse Barnes79e53942008-11-07 14:24:08 -08006392
Chris Wilson5c3b82e2009-02-11 13:25:09 +00006393 ret = intel_pipe_set_base(crtc, x, y, old_fb);
Shaohua Li7662c8b2009-06-26 11:23:55 +08006394
6395 intel_update_watermarks(dev);
6396
Chris Wilson1f803ee2009-06-06 09:45:59 +01006397 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006398}
6399
Eric Anholtf564048e2011-03-30 13:01:02 -07006400static int intel_crtc_mode_set(struct drm_crtc *crtc,
6401 struct drm_display_mode *mode,
6402 struct drm_display_mode *adjusted_mode,
6403 int x, int y,
6404 struct drm_framebuffer *old_fb)
6405{
6406 struct drm_device *dev = crtc->dev;
6407 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt0b701d22011-03-30 13:01:03 -07006408 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6409 int pipe = intel_crtc->pipe;
Eric Anholtf564048e2011-03-30 13:01:02 -07006410 int ret;
6411
Eric Anholt0b701d22011-03-30 13:01:03 -07006412 drm_vblank_pre_modeset(dev, pipe);
6413
Eric Anholtf564048e2011-03-30 13:01:02 -07006414 ret = dev_priv->display.crtc_mode_set(crtc, mode, adjusted_mode,
6415 x, y, old_fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08006416 drm_vblank_post_modeset(dev, pipe);
6417
Jesse Barnesd8e70a22011-11-15 10:28:54 -08006418 if (ret)
6419 intel_crtc->dpms_mode = DRM_MODE_DPMS_OFF;
6420 else
6421 intel_crtc->dpms_mode = DRM_MODE_DPMS_ON;
Keith Packard120eced2011-07-27 01:21:40 -07006422
Jesse Barnes79e53942008-11-07 14:24:08 -08006423 return ret;
6424}
6425
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006426static bool intel_eld_uptodate(struct drm_connector *connector,
6427 int reg_eldv, uint32_t bits_eldv,
6428 int reg_elda, uint32_t bits_elda,
6429 int reg_edid)
6430{
6431 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6432 uint8_t *eld = connector->eld;
6433 uint32_t i;
6434
6435 i = I915_READ(reg_eldv);
6436 i &= bits_eldv;
6437
6438 if (!eld[0])
6439 return !i;
6440
6441 if (!i)
6442 return false;
6443
6444 i = I915_READ(reg_elda);
6445 i &= ~bits_elda;
6446 I915_WRITE(reg_elda, i);
6447
6448 for (i = 0; i < eld[2]; i++)
6449 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
6450 return false;
6451
6452 return true;
6453}
6454
Wu Fengguange0dac652011-09-05 14:25:34 +08006455static void g4x_write_eld(struct drm_connector *connector,
6456 struct drm_crtc *crtc)
6457{
6458 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6459 uint8_t *eld = connector->eld;
6460 uint32_t eldv;
6461 uint32_t len;
6462 uint32_t i;
6463
6464 i = I915_READ(G4X_AUD_VID_DID);
6465
6466 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
6467 eldv = G4X_ELDV_DEVCL_DEVBLC;
6468 else
6469 eldv = G4X_ELDV_DEVCTG;
6470
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006471 if (intel_eld_uptodate(connector,
6472 G4X_AUD_CNTL_ST, eldv,
6473 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
6474 G4X_HDMIW_HDMIEDID))
6475 return;
6476
Wu Fengguange0dac652011-09-05 14:25:34 +08006477 i = I915_READ(G4X_AUD_CNTL_ST);
6478 i &= ~(eldv | G4X_ELD_ADDR);
6479 len = (i >> 9) & 0x1f; /* ELD buffer size */
6480 I915_WRITE(G4X_AUD_CNTL_ST, i);
6481
6482 if (!eld[0])
6483 return;
6484
6485 len = min_t(uint8_t, eld[2], len);
6486 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6487 for (i = 0; i < len; i++)
6488 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
6489
6490 i = I915_READ(G4X_AUD_CNTL_ST);
6491 i |= eldv;
6492 I915_WRITE(G4X_AUD_CNTL_ST, i);
6493}
6494
6495static void ironlake_write_eld(struct drm_connector *connector,
6496 struct drm_crtc *crtc)
6497{
6498 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6499 uint8_t *eld = connector->eld;
6500 uint32_t eldv;
6501 uint32_t i;
6502 int len;
6503 int hdmiw_hdmiedid;
Wu Fengguangb6daa022012-01-06 14:41:31 -06006504 int aud_config;
Wu Fengguange0dac652011-09-05 14:25:34 +08006505 int aud_cntl_st;
6506 int aud_cntrl_st2;
6507
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08006508 if (HAS_PCH_IBX(connector->dev)) {
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006509 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID_A;
Wu Fengguangb6daa022012-01-06 14:41:31 -06006510 aud_config = IBX_AUD_CONFIG_A;
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006511 aud_cntl_st = IBX_AUD_CNTL_ST_A;
6512 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08006513 } else {
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006514 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID_A;
Wu Fengguangb6daa022012-01-06 14:41:31 -06006515 aud_config = CPT_AUD_CONFIG_A;
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006516 aud_cntl_st = CPT_AUD_CNTL_ST_A;
6517 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08006518 }
6519
6520 i = to_intel_crtc(crtc)->pipe;
6521 hdmiw_hdmiedid += i * 0x100;
6522 aud_cntl_st += i * 0x100;
Wu Fengguangb6daa022012-01-06 14:41:31 -06006523 aud_config += i * 0x100;
Wu Fengguange0dac652011-09-05 14:25:34 +08006524
6525 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(i));
6526
6527 i = I915_READ(aud_cntl_st);
6528 i = (i >> 29) & 0x3; /* DIP_Port_Select, 0x1 = PortB */
6529 if (!i) {
6530 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
6531 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006532 eldv = IBX_ELD_VALIDB;
6533 eldv |= IBX_ELD_VALIDB << 4;
6534 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08006535 } else {
6536 DRM_DEBUG_DRIVER("ELD on port %c\n", 'A' + i);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006537 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08006538 }
6539
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006540 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6541 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6542 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
Wu Fengguangb6daa022012-01-06 14:41:31 -06006543 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6544 } else
6545 I915_WRITE(aud_config, 0);
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006546
6547 if (intel_eld_uptodate(connector,
6548 aud_cntrl_st2, eldv,
6549 aud_cntl_st, IBX_ELD_ADDRESS,
6550 hdmiw_hdmiedid))
6551 return;
6552
Wu Fengguange0dac652011-09-05 14:25:34 +08006553 i = I915_READ(aud_cntrl_st2);
6554 i &= ~eldv;
6555 I915_WRITE(aud_cntrl_st2, i);
6556
6557 if (!eld[0])
6558 return;
6559
Wu Fengguange0dac652011-09-05 14:25:34 +08006560 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006561 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08006562 I915_WRITE(aud_cntl_st, i);
6563
6564 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6565 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6566 for (i = 0; i < len; i++)
6567 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6568
6569 i = I915_READ(aud_cntrl_st2);
6570 i |= eldv;
6571 I915_WRITE(aud_cntrl_st2, i);
6572}
6573
6574void intel_write_eld(struct drm_encoder *encoder,
6575 struct drm_display_mode *mode)
6576{
6577 struct drm_crtc *crtc = encoder->crtc;
6578 struct drm_connector *connector;
6579 struct drm_device *dev = encoder->dev;
6580 struct drm_i915_private *dev_priv = dev->dev_private;
6581
6582 connector = drm_select_eld(encoder, mode);
6583 if (!connector)
6584 return;
6585
6586 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6587 connector->base.id,
6588 drm_get_connector_name(connector),
6589 connector->encoder->base.id,
6590 drm_get_encoder_name(connector->encoder));
6591
6592 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
6593
6594 if (dev_priv->display.write_eld)
6595 dev_priv->display.write_eld(connector, crtc);
6596}
6597
Jesse Barnes79e53942008-11-07 14:24:08 -08006598/** Loads the palette/gamma unit for the CRTC with the prepared values */
6599void intel_crtc_load_lut(struct drm_crtc *crtc)
6600{
6601 struct drm_device *dev = crtc->dev;
6602 struct drm_i915_private *dev_priv = dev->dev_private;
6603 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006604 int palreg = PALETTE(intel_crtc->pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08006605 int i;
6606
6607 /* The clocks have to be on to load the palette. */
Alban Browaeysaed3f092012-02-24 17:12:45 +00006608 if (!crtc->enabled || !intel_crtc->active)
Jesse Barnes79e53942008-11-07 14:24:08 -08006609 return;
6610
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006611 /* use legacy palette for Ironlake */
Eric Anholtbad720f2009-10-22 16:11:14 -07006612 if (HAS_PCH_SPLIT(dev))
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006613 palreg = LGC_PALETTE(intel_crtc->pipe);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006614
Jesse Barnes79e53942008-11-07 14:24:08 -08006615 for (i = 0; i < 256; i++) {
6616 I915_WRITE(palreg + 4 * i,
6617 (intel_crtc->lut_r[i] << 16) |
6618 (intel_crtc->lut_g[i] << 8) |
6619 intel_crtc->lut_b[i]);
6620 }
6621}
6622
Chris Wilson560b85b2010-08-07 11:01:38 +01006623static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
6624{
6625 struct drm_device *dev = crtc->dev;
6626 struct drm_i915_private *dev_priv = dev->dev_private;
6627 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6628 bool visible = base != 0;
6629 u32 cntl;
6630
6631 if (intel_crtc->cursor_visible == visible)
6632 return;
6633
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006634 cntl = I915_READ(_CURACNTR);
Chris Wilson560b85b2010-08-07 11:01:38 +01006635 if (visible) {
6636 /* On these chipsets we can only modify the base whilst
6637 * the cursor is disabled.
6638 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006639 I915_WRITE(_CURABASE, base);
Chris Wilson560b85b2010-08-07 11:01:38 +01006640
6641 cntl &= ~(CURSOR_FORMAT_MASK);
6642 /* XXX width must be 64, stride 256 => 0x00 << 28 */
6643 cntl |= CURSOR_ENABLE |
6644 CURSOR_GAMMA_ENABLE |
6645 CURSOR_FORMAT_ARGB;
6646 } else
6647 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006648 I915_WRITE(_CURACNTR, cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01006649
6650 intel_crtc->cursor_visible = visible;
6651}
6652
6653static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
6654{
6655 struct drm_device *dev = crtc->dev;
6656 struct drm_i915_private *dev_priv = dev->dev_private;
6657 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6658 int pipe = intel_crtc->pipe;
6659 bool visible = base != 0;
6660
6661 if (intel_crtc->cursor_visible != visible) {
Jesse Barnes548f2452011-02-17 10:40:53 -08006662 uint32_t cntl = I915_READ(CURCNTR(pipe));
Chris Wilson560b85b2010-08-07 11:01:38 +01006663 if (base) {
6664 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
6665 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6666 cntl |= pipe << 28; /* Connect to correct pipe */
6667 } else {
6668 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6669 cntl |= CURSOR_MODE_DISABLE;
6670 }
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006671 I915_WRITE(CURCNTR(pipe), cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01006672
6673 intel_crtc->cursor_visible = visible;
6674 }
6675 /* and commit changes on next vblank */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006676 I915_WRITE(CURBASE(pipe), base);
Chris Wilson560b85b2010-08-07 11:01:38 +01006677}
6678
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006679static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
6680{
6681 struct drm_device *dev = crtc->dev;
6682 struct drm_i915_private *dev_priv = dev->dev_private;
6683 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6684 int pipe = intel_crtc->pipe;
6685 bool visible = base != 0;
6686
6687 if (intel_crtc->cursor_visible != visible) {
6688 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
6689 if (base) {
6690 cntl &= ~CURSOR_MODE;
6691 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6692 } else {
6693 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6694 cntl |= CURSOR_MODE_DISABLE;
6695 }
6696 I915_WRITE(CURCNTR_IVB(pipe), cntl);
6697
6698 intel_crtc->cursor_visible = visible;
6699 }
6700 /* and commit changes on next vblank */
6701 I915_WRITE(CURBASE_IVB(pipe), base);
6702}
6703
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006704/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01006705static void intel_crtc_update_cursor(struct drm_crtc *crtc,
6706 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006707{
6708 struct drm_device *dev = crtc->dev;
6709 struct drm_i915_private *dev_priv = dev->dev_private;
6710 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6711 int pipe = intel_crtc->pipe;
6712 int x = intel_crtc->cursor_x;
6713 int y = intel_crtc->cursor_y;
Chris Wilson560b85b2010-08-07 11:01:38 +01006714 u32 base, pos;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006715 bool visible;
6716
6717 pos = 0;
6718
Chris Wilson6b383a72010-09-13 13:54:26 +01006719 if (on && crtc->enabled && crtc->fb) {
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006720 base = intel_crtc->cursor_addr;
6721 if (x > (int) crtc->fb->width)
6722 base = 0;
6723
6724 if (y > (int) crtc->fb->height)
6725 base = 0;
6726 } else
6727 base = 0;
6728
6729 if (x < 0) {
6730 if (x + intel_crtc->cursor_width < 0)
6731 base = 0;
6732
6733 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
6734 x = -x;
6735 }
6736 pos |= x << CURSOR_X_SHIFT;
6737
6738 if (y < 0) {
6739 if (y + intel_crtc->cursor_height < 0)
6740 base = 0;
6741
6742 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
6743 y = -y;
6744 }
6745 pos |= y << CURSOR_Y_SHIFT;
6746
6747 visible = base != 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01006748 if (!visible && !intel_crtc->cursor_visible)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006749 return;
6750
Eugeni Dodonov0cd83aa2012-04-13 17:08:48 -03006751 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006752 I915_WRITE(CURPOS_IVB(pipe), pos);
6753 ivb_update_cursor(crtc, base);
6754 } else {
6755 I915_WRITE(CURPOS(pipe), pos);
6756 if (IS_845G(dev) || IS_I865G(dev))
6757 i845_update_cursor(crtc, base);
6758 else
6759 i9xx_update_cursor(crtc, base);
6760 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006761
6762 if (visible)
6763 intel_mark_busy(dev, to_intel_framebuffer(crtc->fb)->obj);
6764}
6765
Jesse Barnes79e53942008-11-07 14:24:08 -08006766static int intel_crtc_cursor_set(struct drm_crtc *crtc,
Chris Wilson05394f32010-11-08 19:18:58 +00006767 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08006768 uint32_t handle,
6769 uint32_t width, uint32_t height)
6770{
6771 struct drm_device *dev = crtc->dev;
6772 struct drm_i915_private *dev_priv = dev->dev_private;
6773 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00006774 struct drm_i915_gem_object *obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006775 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006776 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006777
Zhao Yakui28c97732009-10-09 11:39:41 +08006778 DRM_DEBUG_KMS("\n");
Jesse Barnes79e53942008-11-07 14:24:08 -08006779
6780 /* if we want to turn off the cursor ignore width and height */
6781 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08006782 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006783 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00006784 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10006785 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006786 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08006787 }
6788
6789 /* Currently we only support 64x64 cursors */
6790 if (width != 64 || height != 64) {
6791 DRM_ERROR("we currently only support 64x64 cursors\n");
6792 return -EINVAL;
6793 }
6794
Chris Wilson05394f32010-11-08 19:18:58 +00006795 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00006796 if (&obj->base == NULL)
Jesse Barnes79e53942008-11-07 14:24:08 -08006797 return -ENOENT;
6798
Chris Wilson05394f32010-11-08 19:18:58 +00006799 if (obj->base.size < width * height * 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006800 DRM_ERROR("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10006801 ret = -ENOMEM;
6802 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08006803 }
6804
Dave Airlie71acb5e2008-12-30 20:31:46 +10006805 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006806 mutex_lock(&dev->struct_mutex);
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05006807 if (!dev_priv->info->cursor_needs_physical) {
Chris Wilsond9e86c02010-11-10 16:40:20 +00006808 if (obj->tiling_mode) {
6809 DRM_ERROR("cursor cannot be tiled\n");
6810 ret = -EINVAL;
6811 goto fail_locked;
6812 }
6813
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006814 ret = i915_gem_object_pin_to_display_plane(obj, 0, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01006815 if (ret) {
6816 DRM_ERROR("failed to move cursor bo into the GTT\n");
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006817 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006818 }
6819
Chris Wilsond9e86c02010-11-10 16:40:20 +00006820 ret = i915_gem_object_put_fence(obj);
6821 if (ret) {
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006822 DRM_ERROR("failed to release fence for cursor");
Chris Wilsond9e86c02010-11-10 16:40:20 +00006823 goto fail_unpin;
6824 }
6825
Chris Wilson05394f32010-11-08 19:18:58 +00006826 addr = obj->gtt_offset;
Dave Airlie71acb5e2008-12-30 20:31:46 +10006827 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01006828 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson05394f32010-11-08 19:18:58 +00006829 ret = i915_gem_attach_phys_object(dev, obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01006830 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
6831 align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10006832 if (ret) {
6833 DRM_ERROR("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006834 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10006835 }
Chris Wilson05394f32010-11-08 19:18:58 +00006836 addr = obj->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006837 }
6838
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006839 if (IS_GEN2(dev))
Jesse Barnes14b603912009-05-20 16:47:08 -04006840 I915_WRITE(CURSIZE, (height << 12) | width);
6841
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006842 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006843 if (intel_crtc->cursor_bo) {
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05006844 if (dev_priv->info->cursor_needs_physical) {
Chris Wilson05394f32010-11-08 19:18:58 +00006845 if (intel_crtc->cursor_bo != obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10006846 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
6847 } else
6848 i915_gem_object_unpin(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00006849 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006850 }
Jesse Barnes80824002009-09-10 15:28:06 -07006851
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006852 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006853
6854 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00006855 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006856 intel_crtc->cursor_width = width;
6857 intel_crtc->cursor_height = height;
6858
Chris Wilson6b383a72010-09-13 13:54:26 +01006859 intel_crtc_update_cursor(crtc, true);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006860
Jesse Barnes79e53942008-11-07 14:24:08 -08006861 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006862fail_unpin:
Chris Wilson05394f32010-11-08 19:18:58 +00006863 i915_gem_object_unpin(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006864fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10006865 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00006866fail:
Chris Wilson05394f32010-11-08 19:18:58 +00006867 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10006868 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006869}
6870
6871static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
6872{
Jesse Barnes79e53942008-11-07 14:24:08 -08006873 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006874
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006875 intel_crtc->cursor_x = x;
6876 intel_crtc->cursor_y = y;
Jesse Barnes652c3932009-08-17 13:31:43 -07006877
Chris Wilson6b383a72010-09-13 13:54:26 +01006878 intel_crtc_update_cursor(crtc, true);
Jesse Barnes79e53942008-11-07 14:24:08 -08006879
6880 return 0;
6881}
6882
6883/** Sets the color ramps on behalf of RandR */
6884void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
6885 u16 blue, int regno)
6886{
6887 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6888
6889 intel_crtc->lut_r[regno] = red >> 8;
6890 intel_crtc->lut_g[regno] = green >> 8;
6891 intel_crtc->lut_b[regno] = blue >> 8;
6892}
6893
Dave Airlieb8c00ac2009-10-06 13:54:01 +10006894void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
6895 u16 *blue, int regno)
6896{
6897 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6898
6899 *red = intel_crtc->lut_r[regno] << 8;
6900 *green = intel_crtc->lut_g[regno] << 8;
6901 *blue = intel_crtc->lut_b[regno] << 8;
6902}
6903
Jesse Barnes79e53942008-11-07 14:24:08 -08006904static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01006905 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08006906{
James Simmons72034252010-08-03 01:33:19 +01006907 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08006908 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006909
James Simmons72034252010-08-03 01:33:19 +01006910 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006911 intel_crtc->lut_r[i] = red[i] >> 8;
6912 intel_crtc->lut_g[i] = green[i] >> 8;
6913 intel_crtc->lut_b[i] = blue[i] >> 8;
6914 }
6915
6916 intel_crtc_load_lut(crtc);
6917}
6918
6919/**
6920 * Get a pipe with a simple mode set on it for doing load-based monitor
6921 * detection.
6922 *
6923 * It will be up to the load-detect code to adjust the pipe as appropriate for
Eric Anholtc751ce42010-03-25 11:48:48 -07006924 * its requirements. The pipe will be connected to no other encoders.
Jesse Barnes79e53942008-11-07 14:24:08 -08006925 *
Eric Anholtc751ce42010-03-25 11:48:48 -07006926 * Currently this code will only succeed if there is a pipe with no encoders
Jesse Barnes79e53942008-11-07 14:24:08 -08006927 * configured for it. In the future, it could choose to temporarily disable
6928 * some outputs to free up a pipe for its use.
6929 *
6930 * \return crtc, or NULL if no pipes are available.
6931 */
6932
6933/* VESA 640x480x72Hz mode to set on the pipe */
6934static struct drm_display_mode load_detect_mode = {
6935 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
6936 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
6937};
6938
Chris Wilsond2dff872011-04-19 08:36:26 +01006939static struct drm_framebuffer *
6940intel_framebuffer_create(struct drm_device *dev,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006941 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilsond2dff872011-04-19 08:36:26 +01006942 struct drm_i915_gem_object *obj)
6943{
6944 struct intel_framebuffer *intel_fb;
6945 int ret;
6946
6947 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
6948 if (!intel_fb) {
6949 drm_gem_object_unreference_unlocked(&obj->base);
6950 return ERR_PTR(-ENOMEM);
6951 }
6952
6953 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
6954 if (ret) {
6955 drm_gem_object_unreference_unlocked(&obj->base);
6956 kfree(intel_fb);
6957 return ERR_PTR(ret);
6958 }
6959
6960 return &intel_fb->base;
6961}
6962
6963static u32
6964intel_framebuffer_pitch_for_width(int width, int bpp)
6965{
6966 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
6967 return ALIGN(pitch, 64);
6968}
6969
6970static u32
6971intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
6972{
6973 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
6974 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
6975}
6976
6977static struct drm_framebuffer *
6978intel_framebuffer_create_for_mode(struct drm_device *dev,
6979 struct drm_display_mode *mode,
6980 int depth, int bpp)
6981{
6982 struct drm_i915_gem_object *obj;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006983 struct drm_mode_fb_cmd2 mode_cmd;
Chris Wilsond2dff872011-04-19 08:36:26 +01006984
6985 obj = i915_gem_alloc_object(dev,
6986 intel_framebuffer_size_for_mode(mode, bpp));
6987 if (obj == NULL)
6988 return ERR_PTR(-ENOMEM);
6989
6990 mode_cmd.width = mode->hdisplay;
6991 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006992 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
6993 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00006994 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01006995
6996 return intel_framebuffer_create(dev, &mode_cmd, obj);
6997}
6998
6999static struct drm_framebuffer *
7000mode_fits_in_fbdev(struct drm_device *dev,
7001 struct drm_display_mode *mode)
7002{
7003 struct drm_i915_private *dev_priv = dev->dev_private;
7004 struct drm_i915_gem_object *obj;
7005 struct drm_framebuffer *fb;
7006
7007 if (dev_priv->fbdev == NULL)
7008 return NULL;
7009
7010 obj = dev_priv->fbdev->ifb.obj;
7011 if (obj == NULL)
7012 return NULL;
7013
7014 fb = &dev_priv->fbdev->ifb.base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007015 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
7016 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01007017 return NULL;
7018
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007019 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01007020 return NULL;
7021
7022 return fb;
7023}
7024
Chris Wilson71731882011-04-19 23:10:58 +01007025bool intel_get_load_detect_pipe(struct intel_encoder *intel_encoder,
7026 struct drm_connector *connector,
7027 struct drm_display_mode *mode,
Chris Wilson8261b192011-04-19 23:18:09 +01007028 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08007029{
7030 struct intel_crtc *intel_crtc;
7031 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01007032 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08007033 struct drm_crtc *crtc = NULL;
7034 struct drm_device *dev = encoder->dev;
Chris Wilsond2dff872011-04-19 08:36:26 +01007035 struct drm_framebuffer *old_fb;
Jesse Barnes79e53942008-11-07 14:24:08 -08007036 int i = -1;
7037
Chris Wilsond2dff872011-04-19 08:36:26 +01007038 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7039 connector->base.id, drm_get_connector_name(connector),
7040 encoder->base.id, drm_get_encoder_name(encoder));
7041
Jesse Barnes79e53942008-11-07 14:24:08 -08007042 /*
7043 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01007044 *
Jesse Barnes79e53942008-11-07 14:24:08 -08007045 * - if the connector already has an assigned crtc, use it (but make
7046 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01007047 *
Jesse Barnes79e53942008-11-07 14:24:08 -08007048 * - try to find the first unused crtc that can drive this connector,
7049 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08007050 */
7051
7052 /* See if we already have a CRTC for this connector */
7053 if (encoder->crtc) {
7054 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01007055
Jesse Barnes79e53942008-11-07 14:24:08 -08007056 intel_crtc = to_intel_crtc(crtc);
Chris Wilson8261b192011-04-19 23:18:09 +01007057 old->dpms_mode = intel_crtc->dpms_mode;
7058 old->load_detect_temp = false;
7059
7060 /* Make sure the crtc and connector are running */
Jesse Barnes79e53942008-11-07 14:24:08 -08007061 if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
Chris Wilson64927112011-04-20 07:25:26 +01007062 struct drm_encoder_helper_funcs *encoder_funcs;
7063 struct drm_crtc_helper_funcs *crtc_funcs;
7064
Jesse Barnes79e53942008-11-07 14:24:08 -08007065 crtc_funcs = crtc->helper_private;
7066 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
Chris Wilson64927112011-04-20 07:25:26 +01007067
7068 encoder_funcs = encoder->helper_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08007069 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
7070 }
Chris Wilson8261b192011-04-19 23:18:09 +01007071
Chris Wilson71731882011-04-19 23:10:58 +01007072 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08007073 }
7074
7075 /* Find an unused one (if possible) */
7076 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
7077 i++;
7078 if (!(encoder->possible_crtcs & (1 << i)))
7079 continue;
7080 if (!possible_crtc->enabled) {
7081 crtc = possible_crtc;
7082 break;
7083 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007084 }
7085
7086 /*
7087 * If we didn't find an unused CRTC, don't use any.
7088 */
7089 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01007090 DRM_DEBUG_KMS("no pipe available for load-detect\n");
7091 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007092 }
7093
7094 encoder->crtc = crtc;
Zhenyu Wangc1c43972010-03-30 14:39:30 +08007095 connector->encoder = encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08007096
7097 intel_crtc = to_intel_crtc(crtc);
Chris Wilson8261b192011-04-19 23:18:09 +01007098 old->dpms_mode = intel_crtc->dpms_mode;
7099 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01007100 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08007101
Chris Wilson64927112011-04-20 07:25:26 +01007102 if (!mode)
7103 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08007104
Chris Wilsond2dff872011-04-19 08:36:26 +01007105 old_fb = crtc->fb;
7106
7107 /* We need a framebuffer large enough to accommodate all accesses
7108 * that the plane may generate whilst we perform load detection.
7109 * We can not rely on the fbcon either being present (we get called
7110 * during its initialisation to detect all boot displays, or it may
7111 * not even exist) or that it is large enough to satisfy the
7112 * requested mode.
7113 */
7114 crtc->fb = mode_fits_in_fbdev(dev, mode);
7115 if (crtc->fb == NULL) {
7116 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
7117 crtc->fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
7118 old->release_fb = crtc->fb;
7119 } else
7120 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
7121 if (IS_ERR(crtc->fb)) {
7122 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
7123 crtc->fb = old_fb;
7124 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007125 }
Chris Wilsond2dff872011-04-19 08:36:26 +01007126
7127 if (!drm_crtc_helper_set_mode(crtc, mode, 0, 0, old_fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01007128 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01007129 if (old->release_fb)
7130 old->release_fb->funcs->destroy(old->release_fb);
7131 crtc->fb = old_fb;
Chris Wilson64927112011-04-20 07:25:26 +01007132 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007133 }
Chris Wilson71731882011-04-19 23:10:58 +01007134
Jesse Barnes79e53942008-11-07 14:24:08 -08007135 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007136 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08007137
Chris Wilson71731882011-04-19 23:10:58 +01007138 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08007139}
7140
Zhenyu Wangc1c43972010-03-30 14:39:30 +08007141void intel_release_load_detect_pipe(struct intel_encoder *intel_encoder,
Chris Wilson8261b192011-04-19 23:18:09 +01007142 struct drm_connector *connector,
7143 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08007144{
Chris Wilson4ef69c72010-09-09 15:14:28 +01007145 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08007146 struct drm_device *dev = encoder->dev;
7147 struct drm_crtc *crtc = encoder->crtc;
7148 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
7149 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
7150
Chris Wilsond2dff872011-04-19 08:36:26 +01007151 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7152 connector->base.id, drm_get_connector_name(connector),
7153 encoder->base.id, drm_get_encoder_name(encoder));
7154
Chris Wilson8261b192011-04-19 23:18:09 +01007155 if (old->load_detect_temp) {
Zhenyu Wangc1c43972010-03-30 14:39:30 +08007156 connector->encoder = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08007157 drm_helper_disable_unused_functions(dev);
Chris Wilsond2dff872011-04-19 08:36:26 +01007158
7159 if (old->release_fb)
7160 old->release_fb->funcs->destroy(old->release_fb);
7161
Chris Wilson0622a532011-04-21 09:32:11 +01007162 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08007163 }
7164
Eric Anholtc751ce42010-03-25 11:48:48 -07007165 /* Switch crtc and encoder back off if necessary */
Chris Wilson0622a532011-04-21 09:32:11 +01007166 if (old->dpms_mode != DRM_MODE_DPMS_ON) {
7167 encoder_funcs->dpms(encoder, old->dpms_mode);
Chris Wilson8261b192011-04-19 23:18:09 +01007168 crtc_funcs->dpms(crtc, old->dpms_mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08007169 }
7170}
7171
7172/* Returns the clock of the currently programmed mode of the given pipe. */
7173static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
7174{
7175 struct drm_i915_private *dev_priv = dev->dev_private;
7176 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7177 int pipe = intel_crtc->pipe;
Jesse Barnes548f2452011-02-17 10:40:53 -08007178 u32 dpll = I915_READ(DPLL(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08007179 u32 fp;
7180 intel_clock_t clock;
7181
7182 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Chris Wilson39adb7a2011-04-22 22:17:21 +01007183 fp = I915_READ(FP0(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08007184 else
Chris Wilson39adb7a2011-04-22 22:17:21 +01007185 fp = I915_READ(FP1(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08007186
7187 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05007188 if (IS_PINEVIEW(dev)) {
7189 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
7190 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08007191 } else {
7192 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
7193 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
7194 }
7195
Chris Wilsona6c45cf2010-09-17 00:32:17 +01007196 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05007197 if (IS_PINEVIEW(dev))
7198 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
7199 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08007200 else
7201 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08007202 DPLL_FPA01_P1_POST_DIV_SHIFT);
7203
7204 switch (dpll & DPLL_MODE_MASK) {
7205 case DPLLB_MODE_DAC_SERIAL:
7206 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
7207 5 : 10;
7208 break;
7209 case DPLLB_MODE_LVDS:
7210 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
7211 7 : 14;
7212 break;
7213 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08007214 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08007215 "mode\n", (int)(dpll & DPLL_MODE_MASK));
7216 return 0;
7217 }
7218
7219 /* XXX: Handle the 100Mhz refclk */
Shaohua Li21778322009-02-23 15:19:16 +08007220 intel_clock(dev, 96000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08007221 } else {
7222 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
7223
7224 if (is_lvds) {
7225 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
7226 DPLL_FPA01_P1_POST_DIV_SHIFT);
7227 clock.p2 = 14;
7228
7229 if ((dpll & PLL_REF_INPUT_MASK) ==
7230 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
7231 /* XXX: might not be 66MHz */
Shaohua Li21778322009-02-23 15:19:16 +08007232 intel_clock(dev, 66000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08007233 } else
Shaohua Li21778322009-02-23 15:19:16 +08007234 intel_clock(dev, 48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08007235 } else {
7236 if (dpll & PLL_P1_DIVIDE_BY_TWO)
7237 clock.p1 = 2;
7238 else {
7239 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
7240 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
7241 }
7242 if (dpll & PLL_P2_DIVIDE_BY_4)
7243 clock.p2 = 4;
7244 else
7245 clock.p2 = 2;
7246
Shaohua Li21778322009-02-23 15:19:16 +08007247 intel_clock(dev, 48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08007248 }
7249 }
7250
7251 /* XXX: It would be nice to validate the clocks, but we can't reuse
7252 * i830PllIsValid() because it relies on the xf86_config connector
7253 * configuration being accurate, which it isn't necessarily.
7254 */
7255
7256 return clock.dot;
7257}
7258
7259/** Returns the currently programmed mode of the given pipe. */
7260struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
7261 struct drm_crtc *crtc)
7262{
Jesse Barnes548f2452011-02-17 10:40:53 -08007263 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08007264 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7265 int pipe = intel_crtc->pipe;
7266 struct drm_display_mode *mode;
Jesse Barnes548f2452011-02-17 10:40:53 -08007267 int htot = I915_READ(HTOTAL(pipe));
7268 int hsync = I915_READ(HSYNC(pipe));
7269 int vtot = I915_READ(VTOTAL(pipe));
7270 int vsync = I915_READ(VSYNC(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08007271
7272 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
7273 if (!mode)
7274 return NULL;
7275
7276 mode->clock = intel_crtc_clock_get(dev, crtc);
7277 mode->hdisplay = (htot & 0xffff) + 1;
7278 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
7279 mode->hsync_start = (hsync & 0xffff) + 1;
7280 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
7281 mode->vdisplay = (vtot & 0xffff) + 1;
7282 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
7283 mode->vsync_start = (vsync & 0xffff) + 1;
7284 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
7285
7286 drm_mode_set_name(mode);
7287 drm_mode_set_crtcinfo(mode, 0);
7288
7289 return mode;
7290}
7291
Jesse Barnes652c3932009-08-17 13:31:43 -07007292#define GPU_IDLE_TIMEOUT 500 /* ms */
7293
7294/* When this timer fires, we've been idle for awhile */
7295static void intel_gpu_idle_timer(unsigned long arg)
7296{
7297 struct drm_device *dev = (struct drm_device *)arg;
7298 drm_i915_private_t *dev_priv = dev->dev_private;
7299
Chris Wilsonff7ea4c2010-12-08 09:43:41 +00007300 if (!list_empty(&dev_priv->mm.active_list)) {
7301 /* Still processing requests, so just re-arm the timer. */
7302 mod_timer(&dev_priv->idle_timer, jiffies +
7303 msecs_to_jiffies(GPU_IDLE_TIMEOUT));
7304 return;
7305 }
Jesse Barnes652c3932009-08-17 13:31:43 -07007306
Chris Wilsonff7ea4c2010-12-08 09:43:41 +00007307 dev_priv->busy = false;
Eric Anholt01dfba92009-09-06 15:18:53 -07007308 queue_work(dev_priv->wq, &dev_priv->idle_work);
Jesse Barnes652c3932009-08-17 13:31:43 -07007309}
7310
Jesse Barnes652c3932009-08-17 13:31:43 -07007311#define CRTC_IDLE_TIMEOUT 1000 /* ms */
7312
7313static void intel_crtc_idle_timer(unsigned long arg)
7314{
7315 struct intel_crtc *intel_crtc = (struct intel_crtc *)arg;
7316 struct drm_crtc *crtc = &intel_crtc->base;
7317 drm_i915_private_t *dev_priv = crtc->dev->dev_private;
Chris Wilsonff7ea4c2010-12-08 09:43:41 +00007318 struct intel_framebuffer *intel_fb;
7319
7320 intel_fb = to_intel_framebuffer(crtc->fb);
7321 if (intel_fb && intel_fb->obj->active) {
7322 /* The framebuffer is still being accessed by the GPU. */
7323 mod_timer(&intel_crtc->idle_timer, jiffies +
7324 msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
7325 return;
7326 }
Jesse Barnes652c3932009-08-17 13:31:43 -07007327
Jesse Barnes652c3932009-08-17 13:31:43 -07007328 intel_crtc->busy = false;
Eric Anholt01dfba92009-09-06 15:18:53 -07007329 queue_work(dev_priv->wq, &dev_priv->idle_work);
Jesse Barnes652c3932009-08-17 13:31:43 -07007330}
7331
Daniel Vetter3dec0092010-08-20 21:40:52 +02007332static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07007333{
7334 struct drm_device *dev = crtc->dev;
7335 drm_i915_private_t *dev_priv = dev->dev_private;
7336 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7337 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007338 int dpll_reg = DPLL(pipe);
7339 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07007340
Eric Anholtbad720f2009-10-22 16:11:14 -07007341 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07007342 return;
7343
7344 if (!dev_priv->lvds_downclock_avail)
7345 return;
7346
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007347 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07007348 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08007349 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007350
Sean Paul8ac5a6d2012-02-13 13:14:51 -05007351 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007352
7353 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
7354 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007355 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007356
Jesse Barnes652c3932009-08-17 13:31:43 -07007357 dpll = I915_READ(dpll_reg);
7358 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08007359 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007360 }
7361
7362 /* Schedule downclock */
Daniel Vetter3dec0092010-08-20 21:40:52 +02007363 mod_timer(&intel_crtc->idle_timer, jiffies +
7364 msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
Jesse Barnes652c3932009-08-17 13:31:43 -07007365}
7366
7367static void intel_decrease_pllclock(struct drm_crtc *crtc)
7368{
7369 struct drm_device *dev = crtc->dev;
7370 drm_i915_private_t *dev_priv = dev->dev_private;
7371 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7372 int pipe = intel_crtc->pipe;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007373 int dpll_reg = DPLL(pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007374 int dpll = I915_READ(dpll_reg);
7375
Eric Anholtbad720f2009-10-22 16:11:14 -07007376 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07007377 return;
7378
7379 if (!dev_priv->lvds_downclock_avail)
7380 return;
7381
7382 /*
7383 * Since this is called by a timer, we should never get here in
7384 * the manual case.
7385 */
7386 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08007387 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007388
Sean Paul8ac5a6d2012-02-13 13:14:51 -05007389 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007390
7391 dpll |= DISPLAY_RATE_SELECT_FPA1;
7392 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007393 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007394 dpll = I915_READ(dpll_reg);
7395 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08007396 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007397 }
7398
7399}
7400
7401/**
7402 * intel_idle_update - adjust clocks for idleness
7403 * @work: work struct
7404 *
7405 * Either the GPU or display (or both) went idle. Check the busy status
7406 * here and adjust the CRTC and GPU clocks as necessary.
7407 */
7408static void intel_idle_update(struct work_struct *work)
7409{
7410 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
7411 idle_work);
7412 struct drm_device *dev = dev_priv->dev;
7413 struct drm_crtc *crtc;
7414 struct intel_crtc *intel_crtc;
7415
7416 if (!i915_powersave)
7417 return;
7418
7419 mutex_lock(&dev->struct_mutex);
7420
Jesse Barnes7648fa92010-05-20 14:28:11 -07007421 i915_update_gfx_val(dev_priv);
7422
Jesse Barnes652c3932009-08-17 13:31:43 -07007423 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
7424 /* Skip inactive CRTCs */
7425 if (!crtc->fb)
7426 continue;
7427
7428 intel_crtc = to_intel_crtc(crtc);
7429 if (!intel_crtc->busy)
7430 intel_decrease_pllclock(crtc);
7431 }
7432
Li Peng45ac22c2010-06-12 23:38:35 +08007433
Jesse Barnes652c3932009-08-17 13:31:43 -07007434 mutex_unlock(&dev->struct_mutex);
7435}
7436
7437/**
7438 * intel_mark_busy - mark the GPU and possibly the display busy
7439 * @dev: drm device
7440 * @obj: object we're operating on
7441 *
7442 * Callers can use this function to indicate that the GPU is busy processing
7443 * commands. If @obj matches one of the CRTC objects (i.e. it's a scanout
7444 * buffer), we'll also mark the display as busy, so we know to increase its
7445 * clock frequency.
7446 */
Chris Wilson05394f32010-11-08 19:18:58 +00007447void intel_mark_busy(struct drm_device *dev, struct drm_i915_gem_object *obj)
Jesse Barnes652c3932009-08-17 13:31:43 -07007448{
7449 drm_i915_private_t *dev_priv = dev->dev_private;
7450 struct drm_crtc *crtc = NULL;
7451 struct intel_framebuffer *intel_fb;
7452 struct intel_crtc *intel_crtc;
7453
Zhenyu Wang5e17ee72009-09-03 09:30:06 +08007454 if (!drm_core_check_feature(dev, DRIVER_MODESET))
7455 return;
7456
Alexander Lam18b21902011-01-03 13:28:56 -05007457 if (!dev_priv->busy)
Chris Wilson28cf7982009-11-30 01:08:56 +00007458 dev_priv->busy = true;
Alexander Lam18b21902011-01-03 13:28:56 -05007459 else
Chris Wilson28cf7982009-11-30 01:08:56 +00007460 mod_timer(&dev_priv->idle_timer, jiffies +
7461 msecs_to_jiffies(GPU_IDLE_TIMEOUT));
Jesse Barnes652c3932009-08-17 13:31:43 -07007462
7463 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
7464 if (!crtc->fb)
7465 continue;
7466
7467 intel_crtc = to_intel_crtc(crtc);
7468 intel_fb = to_intel_framebuffer(crtc->fb);
7469 if (intel_fb->obj == obj) {
7470 if (!intel_crtc->busy) {
7471 /* Non-busy -> busy, upclock */
Daniel Vetter3dec0092010-08-20 21:40:52 +02007472 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07007473 intel_crtc->busy = true;
7474 } else {
7475 /* Busy -> busy, put off timer */
7476 mod_timer(&intel_crtc->idle_timer, jiffies +
7477 msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
7478 }
7479 }
7480 }
7481}
7482
Jesse Barnes79e53942008-11-07 14:24:08 -08007483static void intel_crtc_destroy(struct drm_crtc *crtc)
7484{
7485 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02007486 struct drm_device *dev = crtc->dev;
7487 struct intel_unpin_work *work;
7488 unsigned long flags;
7489
7490 spin_lock_irqsave(&dev->event_lock, flags);
7491 work = intel_crtc->unpin_work;
7492 intel_crtc->unpin_work = NULL;
7493 spin_unlock_irqrestore(&dev->event_lock, flags);
7494
7495 if (work) {
7496 cancel_work_sync(&work->work);
7497 kfree(work);
7498 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007499
7500 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02007501
Jesse Barnes79e53942008-11-07 14:24:08 -08007502 kfree(intel_crtc);
7503}
7504
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007505static void intel_unpin_work_fn(struct work_struct *__work)
7506{
7507 struct intel_unpin_work *work =
7508 container_of(__work, struct intel_unpin_work, work);
7509
7510 mutex_lock(&work->dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01007511 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00007512 drm_gem_object_unreference(&work->pending_flip_obj->base);
7513 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00007514
Chris Wilson7782de32011-07-08 12:22:41 +01007515 intel_update_fbc(work->dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007516 mutex_unlock(&work->dev->struct_mutex);
7517 kfree(work);
7518}
7519
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007520static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01007521 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007522{
7523 drm_i915_private_t *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007524 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7525 struct intel_unpin_work *work;
Chris Wilson05394f32010-11-08 19:18:58 +00007526 struct drm_i915_gem_object *obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007527 struct drm_pending_vblank_event *e;
Mario Kleiner49b14a52010-12-09 07:00:07 +01007528 struct timeval tnow, tvbl;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007529 unsigned long flags;
7530
7531 /* Ignore early vblank irqs */
7532 if (intel_crtc == NULL)
7533 return;
7534
Mario Kleiner49b14a52010-12-09 07:00:07 +01007535 do_gettimeofday(&tnow);
7536
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007537 spin_lock_irqsave(&dev->event_lock, flags);
7538 work = intel_crtc->unpin_work;
7539 if (work == NULL || !work->pending) {
7540 spin_unlock_irqrestore(&dev->event_lock, flags);
7541 return;
7542 }
7543
7544 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007545
7546 if (work->event) {
7547 e = work->event;
Mario Kleiner49b14a52010-12-09 07:00:07 +01007548 e->event.sequence = drm_vblank_count_and_time(dev, intel_crtc->pipe, &tvbl);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01007549
7550 /* Called before vblank count and timestamps have
7551 * been updated for the vblank interval of flip
7552 * completion? Need to increment vblank count and
7553 * add one videorefresh duration to returned timestamp
Mario Kleiner49b14a52010-12-09 07:00:07 +01007554 * to account for this. We assume this happened if we
7555 * get called over 0.9 frame durations after the last
7556 * timestamped vblank.
7557 *
7558 * This calculation can not be used with vrefresh rates
7559 * below 5Hz (10Hz to be on the safe side) without
7560 * promoting to 64 integers.
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01007561 */
Mario Kleiner49b14a52010-12-09 07:00:07 +01007562 if (10 * (timeval_to_ns(&tnow) - timeval_to_ns(&tvbl)) >
7563 9 * crtc->framedur_ns) {
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01007564 e->event.sequence++;
Mario Kleiner49b14a52010-12-09 07:00:07 +01007565 tvbl = ns_to_timeval(timeval_to_ns(&tvbl) +
7566 crtc->framedur_ns);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01007567 }
7568
Mario Kleiner49b14a52010-12-09 07:00:07 +01007569 e->event.tv_sec = tvbl.tv_sec;
7570 e->event.tv_usec = tvbl.tv_usec;
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01007571
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007572 list_add_tail(&e->base.link,
7573 &e->base.file_priv->event_list);
7574 wake_up_interruptible(&e->base.file_priv->event_wait);
7575 }
7576
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01007577 drm_vblank_put(dev, intel_crtc->pipe);
7578
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007579 spin_unlock_irqrestore(&dev->event_lock, flags);
7580
Chris Wilson05394f32010-11-08 19:18:58 +00007581 obj = work->old_fb_obj;
Chris Wilsond9e86c02010-11-10 16:40:20 +00007582
Chris Wilsone59f2ba2010-10-07 17:28:15 +01007583 atomic_clear_mask(1 << intel_crtc->plane,
Chris Wilson05394f32010-11-08 19:18:58 +00007584 &obj->pending_flip.counter);
7585 if (atomic_read(&obj->pending_flip) == 0)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01007586 wake_up(&dev_priv->pending_flip_queue);
Chris Wilsond9e86c02010-11-10 16:40:20 +00007587
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007588 schedule_work(&work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07007589
7590 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007591}
7592
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007593void intel_finish_page_flip(struct drm_device *dev, int pipe)
7594{
7595 drm_i915_private_t *dev_priv = dev->dev_private;
7596 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
7597
Mario Kleiner49b14a52010-12-09 07:00:07 +01007598 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007599}
7600
7601void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
7602{
7603 drm_i915_private_t *dev_priv = dev->dev_private;
7604 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
7605
Mario Kleiner49b14a52010-12-09 07:00:07 +01007606 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007607}
7608
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007609void intel_prepare_page_flip(struct drm_device *dev, int plane)
7610{
7611 drm_i915_private_t *dev_priv = dev->dev_private;
7612 struct intel_crtc *intel_crtc =
7613 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
7614 unsigned long flags;
7615
7616 spin_lock_irqsave(&dev->event_lock, flags);
Jesse Barnesde3f4402010-01-14 13:18:02 -08007617 if (intel_crtc->unpin_work) {
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01007618 if ((++intel_crtc->unpin_work->pending) > 1)
7619 DRM_ERROR("Prepared flip multiple times\n");
Jesse Barnesde3f4402010-01-14 13:18:02 -08007620 } else {
7621 DRM_DEBUG_DRIVER("preparing flip with no unpin work?\n");
7622 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007623 spin_unlock_irqrestore(&dev->event_lock, flags);
7624}
7625
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007626static int intel_gen2_queue_flip(struct drm_device *dev,
7627 struct drm_crtc *crtc,
7628 struct drm_framebuffer *fb,
7629 struct drm_i915_gem_object *obj)
7630{
7631 struct drm_i915_private *dev_priv = dev->dev_private;
7632 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7633 unsigned long offset;
7634 u32 flip_mask;
7635 int ret;
7636
7637 ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
7638 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007639 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007640
7641 /* Offset into the new buffer for cases of shared fbs between CRTCs */
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007642 offset = crtc->y * fb->pitches[0] + crtc->x * fb->bits_per_pixel/8;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007643
7644 ret = BEGIN_LP_RING(6);
7645 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007646 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007647
7648 /* Can't queue multiple flips, so wait for the previous
7649 * one to finish before executing the next.
7650 */
7651 if (intel_crtc->plane)
7652 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7653 else
7654 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
7655 OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);
7656 OUT_RING(MI_NOOP);
7657 OUT_RING(MI_DISPLAY_FLIP |
7658 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007659 OUT_RING(fb->pitches[0]);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007660 OUT_RING(obj->gtt_offset + offset);
Daniel Vetterc6a32fc2012-01-20 10:43:44 +01007661 OUT_RING(0); /* aux display base address, unused */
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007662 ADVANCE_LP_RING();
Chris Wilson83d40922012-04-17 19:35:53 +01007663 return 0;
7664
7665err_unpin:
7666 intel_unpin_fb_obj(obj);
7667err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007668 return ret;
7669}
7670
7671static int intel_gen3_queue_flip(struct drm_device *dev,
7672 struct drm_crtc *crtc,
7673 struct drm_framebuffer *fb,
7674 struct drm_i915_gem_object *obj)
7675{
7676 struct drm_i915_private *dev_priv = dev->dev_private;
7677 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7678 unsigned long offset;
7679 u32 flip_mask;
7680 int ret;
7681
7682 ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
7683 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007684 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007685
7686 /* Offset into the new buffer for cases of shared fbs between CRTCs */
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007687 offset = crtc->y * fb->pitches[0] + crtc->x * fb->bits_per_pixel/8;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007688
7689 ret = BEGIN_LP_RING(6);
7690 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007691 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007692
7693 if (intel_crtc->plane)
7694 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7695 else
7696 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
7697 OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);
7698 OUT_RING(MI_NOOP);
7699 OUT_RING(MI_DISPLAY_FLIP_I915 |
7700 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007701 OUT_RING(fb->pitches[0]);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007702 OUT_RING(obj->gtt_offset + offset);
7703 OUT_RING(MI_NOOP);
7704
7705 ADVANCE_LP_RING();
Chris Wilson83d40922012-04-17 19:35:53 +01007706 return 0;
7707
7708err_unpin:
7709 intel_unpin_fb_obj(obj);
7710err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007711 return ret;
7712}
7713
7714static int intel_gen4_queue_flip(struct drm_device *dev,
7715 struct drm_crtc *crtc,
7716 struct drm_framebuffer *fb,
7717 struct drm_i915_gem_object *obj)
7718{
7719 struct drm_i915_private *dev_priv = dev->dev_private;
7720 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7721 uint32_t pf, pipesrc;
7722 int ret;
7723
7724 ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
7725 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007726 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007727
7728 ret = BEGIN_LP_RING(4);
7729 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007730 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007731
7732 /* i965+ uses the linear or tiled offsets from the
7733 * Display Registers (which do not change across a page-flip)
7734 * so we need only reprogram the base address.
7735 */
7736 OUT_RING(MI_DISPLAY_FLIP |
7737 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007738 OUT_RING(fb->pitches[0]);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007739 OUT_RING(obj->gtt_offset | obj->tiling_mode);
7740
7741 /* XXX Enabling the panel-fitter across page-flip is so far
7742 * untested on non-native modes, so ignore it for now.
7743 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
7744 */
7745 pf = 0;
7746 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
7747 OUT_RING(pf | pipesrc);
7748 ADVANCE_LP_RING();
Chris Wilson83d40922012-04-17 19:35:53 +01007749 return 0;
7750
7751err_unpin:
7752 intel_unpin_fb_obj(obj);
7753err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007754 return ret;
7755}
7756
7757static int intel_gen6_queue_flip(struct drm_device *dev,
7758 struct drm_crtc *crtc,
7759 struct drm_framebuffer *fb,
7760 struct drm_i915_gem_object *obj)
7761{
7762 struct drm_i915_private *dev_priv = dev->dev_private;
7763 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7764 uint32_t pf, pipesrc;
7765 int ret;
7766
7767 ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
7768 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007769 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007770
7771 ret = BEGIN_LP_RING(4);
7772 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007773 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007774
7775 OUT_RING(MI_DISPLAY_FLIP |
7776 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007777 OUT_RING(fb->pitches[0] | obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007778 OUT_RING(obj->gtt_offset);
7779
7780 pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
7781 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
7782 OUT_RING(pf | pipesrc);
7783 ADVANCE_LP_RING();
Chris Wilson83d40922012-04-17 19:35:53 +01007784 return 0;
7785
7786err_unpin:
7787 intel_unpin_fb_obj(obj);
7788err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007789 return ret;
7790}
7791
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007792/*
7793 * On gen7 we currently use the blit ring because (in early silicon at least)
7794 * the render ring doesn't give us interrpts for page flip completion, which
7795 * means clients will hang after the first flip is queued. Fortunately the
7796 * blit ring generates interrupts properly, so use it instead.
7797 */
7798static int intel_gen7_queue_flip(struct drm_device *dev,
7799 struct drm_crtc *crtc,
7800 struct drm_framebuffer *fb,
7801 struct drm_i915_gem_object *obj)
7802{
7803 struct drm_i915_private *dev_priv = dev->dev_private;
7804 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7805 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
7806 int ret;
7807
7808 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7809 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007810 goto err;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007811
7812 ret = intel_ring_begin(ring, 4);
7813 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007814 goto err_unpin;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007815
7816 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | (intel_crtc->plane << 19));
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007817 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007818 intel_ring_emit(ring, (obj->gtt_offset));
7819 intel_ring_emit(ring, (MI_NOOP));
7820 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007821 return 0;
7822
7823err_unpin:
7824 intel_unpin_fb_obj(obj);
7825err:
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007826 return ret;
7827}
7828
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007829static int intel_default_queue_flip(struct drm_device *dev,
7830 struct drm_crtc *crtc,
7831 struct drm_framebuffer *fb,
7832 struct drm_i915_gem_object *obj)
7833{
7834 return -ENODEV;
7835}
7836
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007837static int intel_crtc_page_flip(struct drm_crtc *crtc,
7838 struct drm_framebuffer *fb,
7839 struct drm_pending_vblank_event *event)
7840{
7841 struct drm_device *dev = crtc->dev;
7842 struct drm_i915_private *dev_priv = dev->dev_private;
7843 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00007844 struct drm_i915_gem_object *obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007845 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7846 struct intel_unpin_work *work;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007847 unsigned long flags;
Chris Wilson52e68632010-08-08 10:15:59 +01007848 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007849
7850 work = kzalloc(sizeof *work, GFP_KERNEL);
7851 if (work == NULL)
7852 return -ENOMEM;
7853
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007854 work->event = event;
7855 work->dev = crtc->dev;
7856 intel_fb = to_intel_framebuffer(crtc->fb);
Jesse Barnesb1b87f62010-01-26 14:40:05 -08007857 work->old_fb_obj = intel_fb->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007858 INIT_WORK(&work->work, intel_unpin_work_fn);
7859
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007860 ret = drm_vblank_get(dev, intel_crtc->pipe);
7861 if (ret)
7862 goto free_work;
7863
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007864 /* We borrow the event spin lock for protecting unpin_work */
7865 spin_lock_irqsave(&dev->event_lock, flags);
7866 if (intel_crtc->unpin_work) {
7867 spin_unlock_irqrestore(&dev->event_lock, flags);
7868 kfree(work);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007869 drm_vblank_put(dev, intel_crtc->pipe);
Chris Wilson468f0b42010-05-27 13:18:13 +01007870
7871 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007872 return -EBUSY;
7873 }
7874 intel_crtc->unpin_work = work;
7875 spin_unlock_irqrestore(&dev->event_lock, flags);
7876
7877 intel_fb = to_intel_framebuffer(fb);
7878 obj = intel_fb->obj;
7879
Chris Wilson468f0b42010-05-27 13:18:13 +01007880 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007881
Jesse Barnes75dfca82010-02-10 15:09:44 -08007882 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00007883 drm_gem_object_reference(&work->old_fb_obj->base);
7884 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007885
7886 crtc->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01007887
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007888 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007889
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01007890 work->enable_stall_check = true;
7891
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007892 /* Block clients from rendering to the new back buffer until
7893 * the flip occurs and the object is no longer visible.
7894 */
Chris Wilson05394f32010-11-08 19:18:58 +00007895 atomic_add(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007896
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007897 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
7898 if (ret)
7899 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007900
Chris Wilson7782de32011-07-08 12:22:41 +01007901 intel_disable_fbc(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007902 mutex_unlock(&dev->struct_mutex);
7903
Jesse Barnese5510fa2010-07-01 16:48:37 -07007904 trace_i915_flip_request(intel_crtc->plane, obj);
7905
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007906 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01007907
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007908cleanup_pending:
7909 atomic_sub(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
Chris Wilson05394f32010-11-08 19:18:58 +00007910 drm_gem_object_unreference(&work->old_fb_obj->base);
7911 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01007912 mutex_unlock(&dev->struct_mutex);
7913
7914 spin_lock_irqsave(&dev->event_lock, flags);
7915 intel_crtc->unpin_work = NULL;
7916 spin_unlock_irqrestore(&dev->event_lock, flags);
7917
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007918 drm_vblank_put(dev, intel_crtc->pipe);
7919free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01007920 kfree(work);
7921
7922 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007923}
7924
Chris Wilson47f1c6c2010-12-03 15:37:31 +00007925static void intel_sanitize_modesetting(struct drm_device *dev,
7926 int pipe, int plane)
7927{
7928 struct drm_i915_private *dev_priv = dev->dev_private;
7929 u32 reg, val;
7930
Chris Wilsonf47166d2012-03-22 15:00:50 +00007931 /* Clear any frame start delays used for debugging left by the BIOS */
7932 for_each_pipe(pipe) {
7933 reg = PIPECONF(pipe);
7934 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
7935 }
7936
Chris Wilson47f1c6c2010-12-03 15:37:31 +00007937 if (HAS_PCH_SPLIT(dev))
7938 return;
7939
7940 /* Who knows what state these registers were left in by the BIOS or
7941 * grub?
7942 *
7943 * If we leave the registers in a conflicting state (e.g. with the
7944 * display plane reading from the other pipe than the one we intend
7945 * to use) then when we attempt to teardown the active mode, we will
7946 * not disable the pipes and planes in the correct order -- leaving
7947 * a plane reading from a disabled pipe and possibly leading to
7948 * undefined behaviour.
7949 */
7950
7951 reg = DSPCNTR(plane);
7952 val = I915_READ(reg);
7953
7954 if ((val & DISPLAY_PLANE_ENABLE) == 0)
7955 return;
7956 if (!!(val & DISPPLANE_SEL_PIPE_MASK) == pipe)
7957 return;
7958
7959 /* This display plane is active and attached to the other CPU pipe. */
7960 pipe = !pipe;
7961
7962 /* Disable the plane and wait for it to stop reading from the pipe. */
Jesse Barnesb24e7172011-01-04 15:09:30 -08007963 intel_disable_plane(dev_priv, plane, pipe);
7964 intel_disable_pipe(dev_priv, pipe);
Chris Wilson47f1c6c2010-12-03 15:37:31 +00007965}
Jesse Barnes79e53942008-11-07 14:24:08 -08007966
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007967static void intel_crtc_reset(struct drm_crtc *crtc)
7968{
7969 struct drm_device *dev = crtc->dev;
7970 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7971
7972 /* Reset flags back to the 'unknown' status so that they
7973 * will be correctly set on the initial modeset.
7974 */
7975 intel_crtc->dpms_mode = -1;
7976
7977 /* We need to fix up any BIOS configuration that conflicts with
7978 * our expectations.
7979 */
7980 intel_sanitize_modesetting(dev, intel_crtc->pipe, intel_crtc->plane);
7981}
7982
7983static struct drm_crtc_helper_funcs intel_helper_funcs = {
7984 .dpms = intel_crtc_dpms,
7985 .mode_fixup = intel_crtc_mode_fixup,
7986 .mode_set = intel_crtc_mode_set,
7987 .mode_set_base = intel_pipe_set_base,
7988 .mode_set_base_atomic = intel_pipe_set_base_atomic,
7989 .load_lut = intel_crtc_load_lut,
7990 .disable = intel_crtc_disable,
7991};
7992
7993static const struct drm_crtc_funcs intel_crtc_funcs = {
7994 .reset = intel_crtc_reset,
7995 .cursor_set = intel_crtc_cursor_set,
7996 .cursor_move = intel_crtc_cursor_move,
7997 .gamma_set = intel_crtc_gamma_set,
7998 .set_config = drm_crtc_helper_set_config,
7999 .destroy = intel_crtc_destroy,
8000 .page_flip = intel_crtc_page_flip,
8001};
8002
Hannes Ederb358d0a2008-12-18 21:18:47 +01008003static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -08008004{
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08008005 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08008006 struct intel_crtc *intel_crtc;
8007 int i;
8008
8009 intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
8010 if (intel_crtc == NULL)
8011 return;
8012
8013 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
8014
8015 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -08008016 for (i = 0; i < 256; i++) {
8017 intel_crtc->lut_r[i] = i;
8018 intel_crtc->lut_g[i] = i;
8019 intel_crtc->lut_b[i] = i;
8020 }
8021
Jesse Barnes80824002009-09-10 15:28:06 -07008022 /* Swap pipes & planes for FBC on pre-965 */
8023 intel_crtc->pipe = pipe;
8024 intel_crtc->plane = pipe;
Chris Wilsone2e767a2010-09-13 16:53:12 +01008025 if (IS_MOBILE(dev) && IS_GEN3(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08008026 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +01008027 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07008028 }
8029
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08008030 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
8031 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
8032 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
8033 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
8034
Chris Wilson5d1d0cc2011-01-24 15:02:15 +00008035 intel_crtc_reset(&intel_crtc->base);
Chris Wilson04dbff52011-02-10 17:38:35 +00008036 intel_crtc->active = true; /* force the pipe off on setup_init_config */
Jesse Barnes5a354202011-06-24 12:19:22 -07008037 intel_crtc->bpp = 24; /* default for pre-Ironlake */
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07008038
8039 if (HAS_PCH_SPLIT(dev)) {
Jesse Barnes4b645f12011-10-12 09:51:31 -07008040 if (pipe == 2 && IS_IVYBRIDGE(dev))
8041 intel_crtc->no_pll = true;
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07008042 intel_helper_funcs.prepare = ironlake_crtc_prepare;
8043 intel_helper_funcs.commit = ironlake_crtc_commit;
8044 } else {
8045 intel_helper_funcs.prepare = i9xx_crtc_prepare;
8046 intel_helper_funcs.commit = i9xx_crtc_commit;
8047 }
8048
Jesse Barnes79e53942008-11-07 14:24:08 -08008049 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
8050
Jesse Barnes652c3932009-08-17 13:31:43 -07008051 intel_crtc->busy = false;
8052
8053 setup_timer(&intel_crtc->idle_timer, intel_crtc_idle_timer,
8054 (unsigned long)intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08008055}
8056
Carl Worth08d7b3d2009-04-29 14:43:54 -07008057int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00008058 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -07008059{
8060 drm_i915_private_t *dev_priv = dev->dev_private;
8061 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +02008062 struct drm_mode_object *drmmode_obj;
8063 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -07008064
8065 if (!dev_priv) {
8066 DRM_ERROR("called with no initialization\n");
8067 return -EINVAL;
8068 }
8069
Daniel Vetterc05422d2009-08-11 16:05:30 +02008070 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
8071 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -07008072
Daniel Vetterc05422d2009-08-11 16:05:30 +02008073 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -07008074 DRM_ERROR("no such CRTC id\n");
8075 return -EINVAL;
8076 }
8077
Daniel Vetterc05422d2009-08-11 16:05:30 +02008078 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
8079 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -07008080
Daniel Vetterc05422d2009-08-11 16:05:30 +02008081 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -07008082}
8083
Zhenyu Wangc5e4df32010-03-30 14:39:27 +08008084static int intel_encoder_clones(struct drm_device *dev, int type_mask)
Jesse Barnes79e53942008-11-07 14:24:08 -08008085{
Chris Wilson4ef69c72010-09-09 15:14:28 +01008086 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08008087 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008088 int entry = 0;
8089
Chris Wilson4ef69c72010-09-09 15:14:28 +01008090 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
8091 if (type_mask & encoder->clone_mask)
Jesse Barnes79e53942008-11-07 14:24:08 -08008092 index_mask |= (1 << entry);
8093 entry++;
8094 }
Chris Wilson4ef69c72010-09-09 15:14:28 +01008095
Jesse Barnes79e53942008-11-07 14:24:08 -08008096 return index_mask;
8097}
8098
Chris Wilson4d302442010-12-14 19:21:29 +00008099static bool has_edp_a(struct drm_device *dev)
8100{
8101 struct drm_i915_private *dev_priv = dev->dev_private;
8102
8103 if (!IS_MOBILE(dev))
8104 return false;
8105
8106 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
8107 return false;
8108
8109 if (IS_GEN5(dev) &&
8110 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
8111 return false;
8112
8113 return true;
8114}
8115
Jesse Barnes79e53942008-11-07 14:24:08 -08008116static void intel_setup_outputs(struct drm_device *dev)
8117{
Eric Anholt725e30a2009-01-22 13:01:02 -08008118 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +01008119 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -04008120 bool dpd_is_edp = false;
Chris Wilsonf3cfcba2012-02-09 09:35:53 +00008121 bool has_lvds;
Jesse Barnes79e53942008-11-07 14:24:08 -08008122
Chris Wilsonf3cfcba2012-02-09 09:35:53 +00008123 has_lvds = intel_lvds_init(dev);
Chris Wilsonc5d1b512010-11-29 18:00:23 +00008124 if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
8125 /* disable the panel fitter on everything but LVDS */
8126 I915_WRITE(PFIT_CONTROL, 0);
8127 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008128
Eric Anholtbad720f2009-10-22 16:11:14 -07008129 if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -04008130 dpd_is_edp = intel_dpd_is_edp(dev);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008131
Chris Wilson4d302442010-12-14 19:21:29 +00008132 if (has_edp_a(dev))
Zhenyu Wang32f9d652009-07-24 01:00:32 +08008133 intel_dp_init(dev, DP_A);
8134
Adam Jacksoncb0953d2010-07-16 14:46:29 -04008135 if (dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
8136 intel_dp_init(dev, PCH_DP_D);
8137 }
8138
8139 intel_crt_init(dev);
8140
8141 if (HAS_PCH_SPLIT(dev)) {
8142 int found;
8143
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008144 if (I915_READ(HDMIB) & PORT_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +08008145 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +01008146 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008147 if (!found)
8148 intel_hdmi_init(dev, HDMIB);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08008149 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
8150 intel_dp_init(dev, PCH_DP_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008151 }
8152
8153 if (I915_READ(HDMIC) & PORT_DETECTED)
8154 intel_hdmi_init(dev, HDMIC);
8155
8156 if (I915_READ(HDMID) & PORT_DETECTED)
8157 intel_hdmi_init(dev, HDMID);
8158
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08008159 if (I915_READ(PCH_DP_C) & DP_DETECTED)
8160 intel_dp_init(dev, PCH_DP_C);
8161
Adam Jacksoncb0953d2010-07-16 14:46:29 -04008162 if (!dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08008163 intel_dp_init(dev, PCH_DP_D);
8164
Zhenyu Wang103a1962009-11-27 11:44:36 +08008165 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +08008166 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -08008167
Eric Anholt725e30a2009-01-22 13:01:02 -08008168 if (I915_READ(SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008169 DRM_DEBUG_KMS("probing SDVOB\n");
Daniel Vettereef4eac2012-03-23 23:43:35 +01008170 found = intel_sdvo_init(dev, SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008171 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
8172 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Eric Anholt725e30a2009-01-22 13:01:02 -08008173 intel_hdmi_init(dev, SDVOB);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008174 }
Ma Ling27185ae2009-08-24 13:50:23 +08008175
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008176 if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
8177 DRM_DEBUG_KMS("probing DP_B\n");
Keith Packarda4fc5ed2009-04-07 16:16:42 -07008178 intel_dp_init(dev, DP_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008179 }
Eric Anholt725e30a2009-01-22 13:01:02 -08008180 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -04008181
8182 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -04008183
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008184 if (I915_READ(SDVOB) & SDVO_DETECTED) {
8185 DRM_DEBUG_KMS("probing SDVOC\n");
Daniel Vettereef4eac2012-03-23 23:43:35 +01008186 found = intel_sdvo_init(dev, SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008187 }
Ma Ling27185ae2009-08-24 13:50:23 +08008188
8189 if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
8190
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008191 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
8192 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Eric Anholt725e30a2009-01-22 13:01:02 -08008193 intel_hdmi_init(dev, SDVOC);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008194 }
8195 if (SUPPORTS_INTEGRATED_DP(dev)) {
8196 DRM_DEBUG_KMS("probing DP_C\n");
Keith Packarda4fc5ed2009-04-07 16:16:42 -07008197 intel_dp_init(dev, DP_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008198 }
Eric Anholt725e30a2009-01-22 13:01:02 -08008199 }
Ma Ling27185ae2009-08-24 13:50:23 +08008200
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008201 if (SUPPORTS_INTEGRATED_DP(dev) &&
8202 (I915_READ(DP_D) & DP_DETECTED)) {
8203 DRM_DEBUG_KMS("probing DP_D\n");
Keith Packarda4fc5ed2009-04-07 16:16:42 -07008204 intel_dp_init(dev, DP_D);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008205 }
Eric Anholtbad720f2009-10-22 16:11:14 -07008206 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08008207 intel_dvo_init(dev);
8208
Zhenyu Wang103a1962009-11-27 11:44:36 +08008209 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08008210 intel_tv_init(dev);
8211
Chris Wilson4ef69c72010-09-09 15:14:28 +01008212 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
8213 encoder->base.possible_crtcs = encoder->crtc_mask;
8214 encoder->base.possible_clones =
8215 intel_encoder_clones(dev, encoder->clone_mask);
Jesse Barnes79e53942008-11-07 14:24:08 -08008216 }
Chris Wilson47356eb2011-01-11 17:06:04 +00008217
Chris Wilson2c7111d2011-03-29 10:40:27 +01008218 /* disable all the possible outputs/crtcs before entering KMS mode */
8219 drm_helper_disable_unused_functions(dev);
Keith Packard9fb526d2011-09-26 22:24:57 -07008220
8221 if (HAS_PCH_SPLIT(dev))
8222 ironlake_init_pch_refclk(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08008223}
8224
8225static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
8226{
8227 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08008228
8229 drm_framebuffer_cleanup(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00008230 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08008231
8232 kfree(intel_fb);
8233}
8234
8235static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +00008236 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08008237 unsigned int *handle)
8238{
8239 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00008240 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08008241
Chris Wilson05394f32010-11-08 19:18:58 +00008242 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -08008243}
8244
8245static const struct drm_framebuffer_funcs intel_fb_funcs = {
8246 .destroy = intel_user_framebuffer_destroy,
8247 .create_handle = intel_user_framebuffer_create_handle,
8248};
8249
Dave Airlie38651672010-03-30 05:34:13 +00008250int intel_framebuffer_init(struct drm_device *dev,
8251 struct intel_framebuffer *intel_fb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008252 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilson05394f32010-11-08 19:18:58 +00008253 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -08008254{
Jesse Barnes79e53942008-11-07 14:24:08 -08008255 int ret;
8256
Chris Wilson05394f32010-11-08 19:18:58 +00008257 if (obj->tiling_mode == I915_TILING_Y)
Chris Wilson57cd6502010-08-08 12:34:44 +01008258 return -EINVAL;
8259
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008260 if (mode_cmd->pitches[0] & 63)
Chris Wilson57cd6502010-08-08 12:34:44 +01008261 return -EINVAL;
8262
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008263 switch (mode_cmd->pixel_format) {
Ville Syrjälä04b39242011-11-17 18:05:13 +02008264 case DRM_FORMAT_RGB332:
8265 case DRM_FORMAT_RGB565:
8266 case DRM_FORMAT_XRGB8888:
Jesse Barnesb250da72012-03-07 08:49:29 -08008267 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +02008268 case DRM_FORMAT_ARGB8888:
8269 case DRM_FORMAT_XRGB2101010:
8270 case DRM_FORMAT_ARGB2101010:
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008271 /* RGB formats are common across chipsets */
Jesse Barnesb5626742011-06-24 12:19:27 -07008272 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02008273 case DRM_FORMAT_YUYV:
8274 case DRM_FORMAT_UYVY:
8275 case DRM_FORMAT_YVYU:
8276 case DRM_FORMAT_VYUY:
Chris Wilson57cd6502010-08-08 12:34:44 +01008277 break;
8278 default:
Eugeni Dodonovaca25842012-01-17 15:25:45 -02008279 DRM_DEBUG_KMS("unsupported pixel format %u\n",
8280 mode_cmd->pixel_format);
Chris Wilson57cd6502010-08-08 12:34:44 +01008281 return -EINVAL;
8282 }
8283
Jesse Barnes79e53942008-11-07 14:24:08 -08008284 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
8285 if (ret) {
8286 DRM_ERROR("framebuffer init failed %d\n", ret);
8287 return ret;
8288 }
8289
8290 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
Jesse Barnes79e53942008-11-07 14:24:08 -08008291 intel_fb->obj = obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08008292 return 0;
8293}
8294
Jesse Barnes79e53942008-11-07 14:24:08 -08008295static struct drm_framebuffer *
8296intel_user_framebuffer_create(struct drm_device *dev,
8297 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008298 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -08008299{
Chris Wilson05394f32010-11-08 19:18:58 +00008300 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08008301
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008302 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
8303 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +00008304 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +01008305 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -08008306
Chris Wilsond2dff872011-04-19 08:36:26 +01008307 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08008308}
8309
Jesse Barnes79e53942008-11-07 14:24:08 -08008310static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -08008311 .fb_create = intel_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +00008312 .output_poll_changed = intel_fb_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -08008313};
8314
Chris Wilson05394f32010-11-08 19:18:58 +00008315static struct drm_i915_gem_object *
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08008316intel_alloc_context_page(struct drm_device *dev)
Chris Wilson9ea8d052010-01-04 18:57:56 +00008317{
Chris Wilson05394f32010-11-08 19:18:58 +00008318 struct drm_i915_gem_object *ctx;
Chris Wilson9ea8d052010-01-04 18:57:56 +00008319 int ret;
8320
Ben Widawsky2c34b852011-03-19 18:14:26 -07008321 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
8322
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08008323 ctx = i915_gem_alloc_object(dev, 4096);
8324 if (!ctx) {
Chris Wilson9ea8d052010-01-04 18:57:56 +00008325 DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
8326 return NULL;
8327 }
8328
Daniel Vetter75e9e912010-11-04 17:11:09 +01008329 ret = i915_gem_object_pin(ctx, 4096, true);
Chris Wilson9ea8d052010-01-04 18:57:56 +00008330 if (ret) {
8331 DRM_ERROR("failed to pin power context: %d\n", ret);
8332 goto err_unref;
8333 }
8334
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08008335 ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
Chris Wilson9ea8d052010-01-04 18:57:56 +00008336 if (ret) {
8337 DRM_ERROR("failed to set-domain on power context: %d\n", ret);
8338 goto err_unpin;
8339 }
Chris Wilson9ea8d052010-01-04 18:57:56 +00008340
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08008341 return ctx;
Chris Wilson9ea8d052010-01-04 18:57:56 +00008342
8343err_unpin:
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08008344 i915_gem_object_unpin(ctx);
Chris Wilson9ea8d052010-01-04 18:57:56 +00008345err_unref:
Chris Wilson05394f32010-11-08 19:18:58 +00008346 drm_gem_object_unreference(&ctx->base);
Chris Wilson9ea8d052010-01-04 18:57:56 +00008347 mutex_unlock(&dev->struct_mutex);
8348 return NULL;
8349}
8350
Jesse Barnes7648fa92010-05-20 14:28:11 -07008351bool ironlake_set_drps(struct drm_device *dev, u8 val)
8352{
8353 struct drm_i915_private *dev_priv = dev->dev_private;
8354 u16 rgvswctl;
8355
8356 rgvswctl = I915_READ16(MEMSWCTL);
8357 if (rgvswctl & MEMCTL_CMD_STS) {
8358 DRM_DEBUG("gpu busy, RCS change rejected\n");
8359 return false; /* still busy with another command */
8360 }
8361
8362 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
8363 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
8364 I915_WRITE16(MEMSWCTL, rgvswctl);
8365 POSTING_READ16(MEMSWCTL);
8366
8367 rgvswctl |= MEMCTL_CMD_STS;
8368 I915_WRITE16(MEMSWCTL, rgvswctl);
8369
8370 return true;
8371}
8372
Jesse Barnesf97108d2010-01-29 11:27:07 -08008373void ironlake_enable_drps(struct drm_device *dev)
8374{
8375 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7648fa92010-05-20 14:28:11 -07008376 u32 rgvmodectl = I915_READ(MEMMODECTL);
Jesse Barnesf97108d2010-01-29 11:27:07 -08008377 u8 fmax, fmin, fstart, vstart;
Jesse Barnesf97108d2010-01-29 11:27:07 -08008378
Jesse Barnesea056c12010-09-10 10:02:13 -07008379 /* Enable temp reporting */
8380 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
8381 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
8382
Jesse Barnesf97108d2010-01-29 11:27:07 -08008383 /* 100ms RC evaluation intervals */
8384 I915_WRITE(RCUPEI, 100000);
8385 I915_WRITE(RCDNEI, 100000);
8386
8387 /* Set max/min thresholds to 90ms and 80ms respectively */
8388 I915_WRITE(RCBMAXAVG, 90000);
8389 I915_WRITE(RCBMINAVG, 80000);
8390
8391 I915_WRITE(MEMIHYST, 1);
8392
8393 /* Set up min, max, and cur for interrupt handling */
8394 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
8395 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
8396 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
8397 MEMMODE_FSTART_SHIFT;
Jesse Barnes7648fa92010-05-20 14:28:11 -07008398
Jesse Barnesf97108d2010-01-29 11:27:07 -08008399 vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
8400 PXVFREQ_PX_SHIFT;
8401
Jesse Barnes80dbf4b2010-11-01 14:12:01 -07008402 dev_priv->fmax = fmax; /* IPS callback will increase this */
Jesse Barnes7648fa92010-05-20 14:28:11 -07008403 dev_priv->fstart = fstart;
8404
Jesse Barnes80dbf4b2010-11-01 14:12:01 -07008405 dev_priv->max_delay = fstart;
Jesse Barnesf97108d2010-01-29 11:27:07 -08008406 dev_priv->min_delay = fmin;
8407 dev_priv->cur_delay = fstart;
8408
Jesse Barnes80dbf4b2010-11-01 14:12:01 -07008409 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
8410 fmax, fmin, fstart);
Jesse Barnes7648fa92010-05-20 14:28:11 -07008411
Jesse Barnesf97108d2010-01-29 11:27:07 -08008412 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
8413
8414 /*
8415 * Interrupts will be enabled in ironlake_irq_postinstall
8416 */
8417
8418 I915_WRITE(VIDSTART, vstart);
8419 POSTING_READ(VIDSTART);
8420
8421 rgvmodectl |= MEMMODE_SWMODE_EN;
8422 I915_WRITE(MEMMODECTL, rgvmodectl);
8423
Chris Wilson481b6af2010-08-23 17:43:35 +01008424 if (wait_for((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Chris Wilson913d8d12010-08-07 11:01:35 +01008425 DRM_ERROR("stuck trying to change perf mode\n");
Jesse Barnesf97108d2010-01-29 11:27:07 -08008426 msleep(1);
8427
Jesse Barnes7648fa92010-05-20 14:28:11 -07008428 ironlake_set_drps(dev, fstart);
Jesse Barnesf97108d2010-01-29 11:27:07 -08008429
Jesse Barnes7648fa92010-05-20 14:28:11 -07008430 dev_priv->last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
8431 I915_READ(0x112e0);
8432 dev_priv->last_time1 = jiffies_to_msecs(jiffies);
8433 dev_priv->last_count2 = I915_READ(0x112f4);
8434 getrawmonotonic(&dev_priv->last_time2);
Jesse Barnesf97108d2010-01-29 11:27:07 -08008435}
8436
8437void ironlake_disable_drps(struct drm_device *dev)
8438{
8439 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7648fa92010-05-20 14:28:11 -07008440 u16 rgvswctl = I915_READ16(MEMSWCTL);
Jesse Barnesf97108d2010-01-29 11:27:07 -08008441
8442 /* Ack interrupts, disable EFC interrupt */
8443 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
8444 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
8445 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
8446 I915_WRITE(DEIIR, DE_PCU_EVENT);
8447 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
8448
8449 /* Go back to the starting frequency */
Jesse Barnes7648fa92010-05-20 14:28:11 -07008450 ironlake_set_drps(dev, dev_priv->fstart);
Jesse Barnesf97108d2010-01-29 11:27:07 -08008451 msleep(1);
8452 rgvswctl |= MEMCTL_CMD_STS;
8453 I915_WRITE(MEMSWCTL, rgvswctl);
8454 msleep(1);
8455
8456}
8457
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08008458void gen6_set_rps(struct drm_device *dev, u8 val)
8459{
8460 struct drm_i915_private *dev_priv = dev->dev_private;
8461 u32 swreq;
8462
8463 swreq = (val & 0x3ff) << 25;
8464 I915_WRITE(GEN6_RPNSWREQ, swreq);
8465}
8466
8467void gen6_disable_rps(struct drm_device *dev)
8468{
8469 struct drm_i915_private *dev_priv = dev->dev_private;
8470
8471 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
8472 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
8473 I915_WRITE(GEN6_PMIER, 0);
Daniel Vetter6fdd4d92011-09-08 14:00:22 +02008474 /* Complete PM interrupt masking here doesn't race with the rps work
8475 * item again unmasking PM interrupts because that is using a different
8476 * register (PMIMR) to mask PM interrupts. The only risk is in leaving
8477 * stale bits in PMIIR and PMIMR which gen6_enable_rps will clean up. */
Ben Widawsky4912d042011-04-25 11:25:20 -07008478
8479 spin_lock_irq(&dev_priv->rps_lock);
8480 dev_priv->pm_iir = 0;
8481 spin_unlock_irq(&dev_priv->rps_lock);
8482
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08008483 I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
8484}
8485
Jesse Barnes7648fa92010-05-20 14:28:11 -07008486static unsigned long intel_pxfreq(u32 vidfreq)
8487{
8488 unsigned long freq;
8489 int div = (vidfreq & 0x3f0000) >> 16;
8490 int post = (vidfreq & 0x3000) >> 12;
8491 int pre = (vidfreq & 0x7);
8492
8493 if (!pre)
8494 return 0;
8495
8496 freq = ((div * 133333) / ((1<<post) * pre));
8497
8498 return freq;
8499}
8500
8501void intel_init_emon(struct drm_device *dev)
8502{
8503 struct drm_i915_private *dev_priv = dev->dev_private;
8504 u32 lcfuse;
8505 u8 pxw[16];
8506 int i;
8507
8508 /* Disable to program */
8509 I915_WRITE(ECR, 0);
8510 POSTING_READ(ECR);
8511
8512 /* Program energy weights for various events */
8513 I915_WRITE(SDEW, 0x15040d00);
8514 I915_WRITE(CSIEW0, 0x007f0000);
8515 I915_WRITE(CSIEW1, 0x1e220004);
8516 I915_WRITE(CSIEW2, 0x04000004);
8517
8518 for (i = 0; i < 5; i++)
8519 I915_WRITE(PEW + (i * 4), 0);
8520 for (i = 0; i < 3; i++)
8521 I915_WRITE(DEW + (i * 4), 0);
8522
8523 /* Program P-state weights to account for frequency power adjustment */
8524 for (i = 0; i < 16; i++) {
8525 u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
8526 unsigned long freq = intel_pxfreq(pxvidfreq);
8527 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
8528 PXVFREQ_PX_SHIFT;
8529 unsigned long val;
8530
8531 val = vid * vid;
8532 val *= (freq / 1000);
8533 val *= 255;
8534 val /= (127*127*900);
8535 if (val > 0xff)
8536 DRM_ERROR("bad pxval: %ld\n", val);
8537 pxw[i] = val;
8538 }
8539 /* Render standby states get 0 weight */
8540 pxw[14] = 0;
8541 pxw[15] = 0;
8542
8543 for (i = 0; i < 4; i++) {
8544 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
8545 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
8546 I915_WRITE(PXW + (i * 4), val);
8547 }
8548
8549 /* Adjust magic regs to magic values (more experimental results) */
8550 I915_WRITE(OGW0, 0);
8551 I915_WRITE(OGW1, 0);
8552 I915_WRITE(EG0, 0x00007f00);
8553 I915_WRITE(EG1, 0x0000000e);
8554 I915_WRITE(EG2, 0x000e0000);
8555 I915_WRITE(EG3, 0x68000300);
8556 I915_WRITE(EG4, 0x42000000);
8557 I915_WRITE(EG5, 0x00140031);
8558 I915_WRITE(EG6, 0);
8559 I915_WRITE(EG7, 0);
8560
8561 for (i = 0; i < 8; i++)
8562 I915_WRITE(PXWL + (i * 4), 0);
8563
8564 /* Enable PMON + select events */
8565 I915_WRITE(ECR, 0x80000019);
8566
8567 lcfuse = I915_READ(LCFUSE02);
8568
8569 dev_priv->corr = (lcfuse & LCFUSE_HIV_MASK);
8570}
8571
Ben Widawsky0136db52012-04-10 21:17:01 -07008572int intel_enable_rc6(const struct drm_device *dev)
Keith Packardc0f372b32011-11-16 22:24:52 -08008573{
8574 /*
8575 * Respect the kernel parameter if it is set
8576 */
8577 if (i915_enable_rc6 >= 0)
8578 return i915_enable_rc6;
8579
8580 /*
8581 * Disable RC6 on Ironlake
8582 */
8583 if (INTEL_INFO(dev)->gen == 5)
8584 return 0;
8585
Eugeni Dodonov83de97c2012-04-13 17:08:54 -03008586 /* Sorry Haswell, no RC6 for you for now. */
8587 if (IS_HASWELL(dev))
8588 return 0;
8589
Keith Packardc0f372b32011-11-16 22:24:52 -08008590 /*
Keith Packard371de6e2011-12-26 17:02:11 -08008591 * Disable rc6 on Sandybridge
Keith Packardc0f372b32011-11-16 22:24:52 -08008592 */
8593 if (INTEL_INFO(dev)->gen == 6) {
Eugeni Dodonovaa464192012-03-23 11:57:19 -03008594 DRM_DEBUG_DRIVER("Sandybridge: deep RC6 disabled\n");
8595 return INTEL_RC6_ENABLE;
Keith Packardc0f372b32011-11-16 22:24:52 -08008596 }
Eugeni Dodonovaa464192012-03-23 11:57:19 -03008597 DRM_DEBUG_DRIVER("RC6 and deep RC6 enabled\n");
8598 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Keith Packardc0f372b32011-11-16 22:24:52 -08008599}
8600
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08008601void gen6_enable_rps(struct drm_i915_private *dev_priv)
Chris Wilson8fd26852010-12-08 18:40:43 +00008602{
Jesse Barnesa6044e22010-12-20 11:34:20 -08008603 u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
8604 u32 gt_perf_status = I915_READ(GEN6_GT_PERF_STATUS);
Jesse Barnes7df87212011-03-30 14:08:56 -07008605 u32 pcu_mbox, rc6_mask = 0;
Ben Widawskydd202c62012-02-09 10:15:18 +01008606 u32 gtfifodbg;
Jesse Barnesa6044e22010-12-20 11:34:20 -08008607 int cur_freq, min_freq, max_freq;
Eugeni Dodonov83b7f9a2012-03-23 11:57:18 -03008608 int rc6_mode;
Chris Wilson8fd26852010-12-08 18:40:43 +00008609 int i;
8610
8611 /* Here begins a magic sequence of register writes to enable
8612 * auto-downclocking.
8613 *
8614 * Perhaps there might be some value in exposing these to
8615 * userspace...
8616 */
8617 I915_WRITE(GEN6_RC_STATE, 0);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01008618 mutex_lock(&dev_priv->dev->struct_mutex);
Ben Widawskydd202c62012-02-09 10:15:18 +01008619
8620 /* Clear the DBG now so we don't confuse earlier errors */
8621 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
8622 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
8623 I915_WRITE(GTFIFODBG, gtfifodbg);
8624 }
8625
Ben Widawskyfcca7922011-04-25 11:23:07 -07008626 gen6_gt_force_wake_get(dev_priv);
Chris Wilson8fd26852010-12-08 18:40:43 +00008627
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08008628 /* disable the counters and set deterministic thresholds */
Chris Wilson8fd26852010-12-08 18:40:43 +00008629 I915_WRITE(GEN6_RC_CONTROL, 0);
8630
8631 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
8632 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
8633 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
8634 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
8635 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
8636
8637 for (i = 0; i < I915_NUM_RINGS; i++)
8638 I915_WRITE(RING_MAX_IDLE(dev_priv->ring[i].mmio_base), 10);
8639
8640 I915_WRITE(GEN6_RC_SLEEP, 0);
8641 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
8642 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
8643 I915_WRITE(GEN6_RC6p_THRESHOLD, 100000);
8644 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
8645
Eugeni Dodonov83b7f9a2012-03-23 11:57:18 -03008646 rc6_mode = intel_enable_rc6(dev_priv->dev);
8647 if (rc6_mode & INTEL_RC6_ENABLE)
8648 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
8649
8650 if (rc6_mode & INTEL_RC6p_ENABLE)
8651 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
8652
8653 if (rc6_mode & INTEL_RC6pp_ENABLE)
8654 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
8655
8656 DRM_INFO("Enabling RC6 states: RC6 %s, RC6p %s, RC6pp %s\n",
8657 (rc6_mode & INTEL_RC6_ENABLE) ? "on" : "off",
8658 (rc6_mode & INTEL_RC6p_ENABLE) ? "on" : "off",
8659 (rc6_mode & INTEL_RC6pp_ENABLE) ? "on" : "off");
Jesse Barnes7df87212011-03-30 14:08:56 -07008660
Chris Wilson8fd26852010-12-08 18:40:43 +00008661 I915_WRITE(GEN6_RC_CONTROL,
Jesse Barnes7df87212011-03-30 14:08:56 -07008662 rc6_mask |
Chris Wilson9c3d2f72010-12-17 10:54:26 +00008663 GEN6_RC_CTL_EI_MODE(1) |
Chris Wilson8fd26852010-12-08 18:40:43 +00008664 GEN6_RC_CTL_HW_ENABLE);
8665
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08008666 I915_WRITE(GEN6_RPNSWREQ,
Chris Wilson8fd26852010-12-08 18:40:43 +00008667 GEN6_FREQUENCY(10) |
8668 GEN6_OFFSET(0) |
8669 GEN6_AGGRESSIVE_TURBO);
8670 I915_WRITE(GEN6_RC_VIDEO_FREQ,
8671 GEN6_FREQUENCY(12));
8672
8673 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
8674 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
8675 18 << 24 |
8676 6 << 16);
Jesse Barnesccab5c82011-01-18 15:49:25 -08008677 I915_WRITE(GEN6_RP_UP_THRESHOLD, 10000);
8678 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 1000000);
Chris Wilson8fd26852010-12-08 18:40:43 +00008679 I915_WRITE(GEN6_RP_UP_EI, 100000);
Jesse Barnesccab5c82011-01-18 15:49:25 -08008680 I915_WRITE(GEN6_RP_DOWN_EI, 5000000);
Chris Wilson8fd26852010-12-08 18:40:43 +00008681 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
8682 I915_WRITE(GEN6_RP_CONTROL,
8683 GEN6_RP_MEDIA_TURBO |
Ben Widawsky6ed55ee2011-12-12 19:21:59 -08008684 GEN6_RP_MEDIA_HW_MODE |
Chris Wilson8fd26852010-12-08 18:40:43 +00008685 GEN6_RP_MEDIA_IS_GFX |
8686 GEN6_RP_ENABLE |
Jesse Barnesccab5c82011-01-18 15:49:25 -08008687 GEN6_RP_UP_BUSY_AVG |
8688 GEN6_RP_DOWN_IDLE_CONT);
Chris Wilson8fd26852010-12-08 18:40:43 +00008689
8690 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
8691 500))
8692 DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
8693
8694 I915_WRITE(GEN6_PCODE_DATA, 0);
8695 I915_WRITE(GEN6_PCODE_MAILBOX,
8696 GEN6_PCODE_READY |
8697 GEN6_PCODE_WRITE_MIN_FREQ_TABLE);
8698 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
8699 500))
8700 DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
8701
Jesse Barnesa6044e22010-12-20 11:34:20 -08008702 min_freq = (rp_state_cap & 0xff0000) >> 16;
8703 max_freq = rp_state_cap & 0xff;
8704 cur_freq = (gt_perf_status & 0xff00) >> 8;
8705
8706 /* Check for overclock support */
8707 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
8708 500))
8709 DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
8710 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_READ_OC_PARAMS);
8711 pcu_mbox = I915_READ(GEN6_PCODE_DATA);
8712 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
8713 500))
8714 DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
8715 if (pcu_mbox & (1<<31)) { /* OC supported */
8716 max_freq = pcu_mbox & 0xff;
Jesse Barnese281fca2011-03-18 10:32:07 -07008717 DRM_DEBUG_DRIVER("overclocking supported, adjusting frequency max to %dMHz\n", pcu_mbox * 50);
Jesse Barnesa6044e22010-12-20 11:34:20 -08008718 }
8719
8720 /* In units of 100MHz */
8721 dev_priv->max_delay = max_freq;
8722 dev_priv->min_delay = min_freq;
8723 dev_priv->cur_delay = cur_freq;
8724
Chris Wilson8fd26852010-12-08 18:40:43 +00008725 /* requires MSI enabled */
8726 I915_WRITE(GEN6_PMIER,
8727 GEN6_PM_MBOX_EVENT |
8728 GEN6_PM_THERMAL_EVENT |
8729 GEN6_PM_RP_DOWN_TIMEOUT |
8730 GEN6_PM_RP_UP_THRESHOLD |
8731 GEN6_PM_RP_DOWN_THRESHOLD |
8732 GEN6_PM_RP_UP_EI_EXPIRED |
8733 GEN6_PM_RP_DOWN_EI_EXPIRED);
Ben Widawsky4912d042011-04-25 11:25:20 -07008734 spin_lock_irq(&dev_priv->rps_lock);
8735 WARN_ON(dev_priv->pm_iir != 0);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08008736 I915_WRITE(GEN6_PMIMR, 0);
Ben Widawsky4912d042011-04-25 11:25:20 -07008737 spin_unlock_irq(&dev_priv->rps_lock);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08008738 /* enable all PM interrupts */
8739 I915_WRITE(GEN6_PMINTRMSK, 0);
Chris Wilson8fd26852010-12-08 18:40:43 +00008740
Ben Widawskyfcca7922011-04-25 11:23:07 -07008741 gen6_gt_force_wake_put(dev_priv);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01008742 mutex_unlock(&dev_priv->dev->struct_mutex);
Chris Wilson8fd26852010-12-08 18:40:43 +00008743}
8744
Jesse Barnes23b2f8b2011-06-28 13:04:16 -07008745void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
8746{
8747 int min_freq = 15;
8748 int gpu_freq, ia_freq, max_ia_freq;
8749 int scaling_factor = 180;
8750
8751 max_ia_freq = cpufreq_quick_get_max(0);
8752 /*
8753 * Default to measured freq if none found, PCU will ensure we don't go
8754 * over
8755 */
8756 if (!max_ia_freq)
8757 max_ia_freq = tsc_khz;
8758
8759 /* Convert from kHz to MHz */
8760 max_ia_freq /= 1000;
8761
8762 mutex_lock(&dev_priv->dev->struct_mutex);
8763
8764 /*
8765 * For each potential GPU frequency, load a ring frequency we'd like
8766 * to use for memory access. We do this by specifying the IA frequency
8767 * the PCU should use as a reference to determine the ring frequency.
8768 */
8769 for (gpu_freq = dev_priv->max_delay; gpu_freq >= dev_priv->min_delay;
8770 gpu_freq--) {
8771 int diff = dev_priv->max_delay - gpu_freq;
8772
8773 /*
8774 * For GPU frequencies less than 750MHz, just use the lowest
8775 * ring freq.
8776 */
8777 if (gpu_freq < min_freq)
8778 ia_freq = 800;
8779 else
8780 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
8781 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
8782
8783 I915_WRITE(GEN6_PCODE_DATA,
8784 (ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT) |
8785 gpu_freq);
8786 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY |
8787 GEN6_PCODE_WRITE_MIN_FREQ_TABLE);
8788 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) &
8789 GEN6_PCODE_READY) == 0, 10)) {
8790 DRM_ERROR("pcode write of freq table timed out\n");
8791 continue;
8792 }
8793 }
8794
8795 mutex_unlock(&dev_priv->dev->struct_mutex);
8796}
8797
Jesse Barnes6067aae2011-04-28 15:04:31 -07008798static void ironlake_init_clock_gating(struct drm_device *dev)
8799{
8800 struct drm_i915_private *dev_priv = dev->dev_private;
8801 uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
8802
8803 /* Required for FBC */
8804 dspclk_gate |= DPFCUNIT_CLOCK_GATE_DISABLE |
8805 DPFCRUNIT_CLOCK_GATE_DISABLE |
8806 DPFDUNIT_CLOCK_GATE_DISABLE;
8807 /* Required for CxSR */
8808 dspclk_gate |= DPARBUNIT_CLOCK_GATE_DISABLE;
8809
8810 I915_WRITE(PCH_3DCGDIS0,
8811 MARIUNIT_CLOCK_GATE_DISABLE |
8812 SVSMUNIT_CLOCK_GATE_DISABLE);
8813 I915_WRITE(PCH_3DCGDIS1,
8814 VFMUNIT_CLOCK_GATE_DISABLE);
8815
8816 I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
8817
8818 /*
Jesse Barnes6067aae2011-04-28 15:04:31 -07008819 * According to the spec the following bits should be set in
8820 * order to enable memory self-refresh
8821 * The bit 22/21 of 0x42004
8822 * The bit 5 of 0x42020
8823 * The bit 15 of 0x45000
8824 */
8825 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8826 (I915_READ(ILK_DISPLAY_CHICKEN2) |
8827 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
8828 I915_WRITE(ILK_DSPCLK_GATE,
8829 (I915_READ(ILK_DSPCLK_GATE) |
8830 ILK_DPARB_CLK_GATE));
8831 I915_WRITE(DISP_ARB_CTL,
8832 (I915_READ(DISP_ARB_CTL) |
8833 DISP_FBC_WM_DIS));
8834 I915_WRITE(WM3_LP_ILK, 0);
8835 I915_WRITE(WM2_LP_ILK, 0);
8836 I915_WRITE(WM1_LP_ILK, 0);
8837
8838 /*
8839 * Based on the document from hardware guys the following bits
8840 * should be set unconditionally in order to enable FBC.
8841 * The bit 22 of 0x42000
8842 * The bit 22 of 0x42004
8843 * The bit 7,8,9 of 0x42020.
8844 */
8845 if (IS_IRONLAKE_M(dev)) {
8846 I915_WRITE(ILK_DISPLAY_CHICKEN1,
8847 I915_READ(ILK_DISPLAY_CHICKEN1) |
8848 ILK_FBCQ_DIS);
8849 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8850 I915_READ(ILK_DISPLAY_CHICKEN2) |
8851 ILK_DPARB_GATE);
8852 I915_WRITE(ILK_DSPCLK_GATE,
8853 I915_READ(ILK_DSPCLK_GATE) |
8854 ILK_DPFC_DIS1 |
8855 ILK_DPFC_DIS2 |
8856 ILK_CLK_FBC);
8857 }
8858
8859 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8860 I915_READ(ILK_DISPLAY_CHICKEN2) |
8861 ILK_ELPIN_409_SELECT);
8862 I915_WRITE(_3D_CHICKEN2,
8863 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
8864 _3D_CHICKEN2_WM_READ_PIPELINED);
8865}
8866
8867static void gen6_init_clock_gating(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07008868{
8869 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08008870 int pipe;
Jesse Barnes6067aae2011-04-28 15:04:31 -07008871 uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
8872
8873 I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
Jesse Barnes652c3932009-08-17 13:31:43 -07008874
Jesse Barnes6067aae2011-04-28 15:04:31 -07008875 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8876 I915_READ(ILK_DISPLAY_CHICKEN2) |
8877 ILK_ELPIN_409_SELECT);
Eric Anholt8956c8b2010-03-18 13:21:14 -07008878
Jesse Barnes6067aae2011-04-28 15:04:31 -07008879 I915_WRITE(WM3_LP_ILK, 0);
8880 I915_WRITE(WM2_LP_ILK, 0);
8881 I915_WRITE(WM1_LP_ILK, 0);
Eric Anholt8956c8b2010-03-18 13:21:14 -07008882
Daniel Vetter009be662012-04-11 20:42:42 +02008883 /* clear masked bit */
8884 I915_WRITE(CACHE_MODE_0,
8885 CM0_STC_EVICT_DISABLE_LRA_SNB << CM0_MASK_SHIFT);
8886
Daniel Vetter80e829f2012-03-31 11:21:57 +02008887 I915_WRITE(GEN6_UCGCTL1,
8888 I915_READ(GEN6_UCGCTL1) |
Daniel Vetterde4a8bd2012-04-11 20:42:38 +02008889 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
8890 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter80e829f2012-03-31 11:21:57 +02008891
Eric Anholt406478d2011-11-07 16:07:04 -08008892 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
8893 * gating disable must be set. Failure to set it results in
8894 * flickering pixels due to Z write ordering failures after
8895 * some amount of runtime in the Mesa "fire" demo, and Unigine
8896 * Sanctuary and Tropics, and apparently anything else with
8897 * alpha test or pixel discard.
Eric Anholt9ca1d102011-11-07 16:07:05 -08008898 *
8899 * According to the spec, bit 11 (RCCUNIT) must also be set,
8900 * but we didn't debug actual testcases to find it out.
Eric Anholt406478d2011-11-07 16:07:04 -08008901 */
Eric Anholt9ca1d102011-11-07 16:07:05 -08008902 I915_WRITE(GEN6_UCGCTL2,
8903 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
8904 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
Eric Anholt406478d2011-11-07 16:07:04 -08008905
Daniel Vetterbf97b272012-04-11 20:42:41 +02008906 /* Bspec says we need to always set all mask bits. */
8907 I915_WRITE(_3D_CHICKEN, (0xFFFF << 16) |
8908 _3D_CHICKEN_SF_DISABLE_FASTCLIP_CULL);
8909
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08008910 /*
Jesse Barnes6067aae2011-04-28 15:04:31 -07008911 * According to the spec the following bits should be
8912 * set in order to enable memory self-refresh and fbc:
8913 * The bit21 and bit22 of 0x42000
8914 * The bit21 and bit22 of 0x42004
8915 * The bit5 and bit7 of 0x42020
8916 * The bit14 of 0x70180
8917 * The bit14 of 0x71180
Jesse Barnes382b0932010-10-07 16:01:25 -07008918 */
Jesse Barnes6067aae2011-04-28 15:04:31 -07008919 I915_WRITE(ILK_DISPLAY_CHICKEN1,
8920 I915_READ(ILK_DISPLAY_CHICKEN1) |
8921 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
8922 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8923 I915_READ(ILK_DISPLAY_CHICKEN2) |
8924 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
8925 I915_WRITE(ILK_DSPCLK_GATE,
8926 I915_READ(ILK_DSPCLK_GATE) |
8927 ILK_DPARB_CLK_GATE |
8928 ILK_DPFD_CLK_GATE);
Jesse Barnes382b0932010-10-07 16:01:25 -07008929
Keith Packardd74362c2011-07-28 14:47:14 -07008930 for_each_pipe(pipe) {
Jesse Barnes6067aae2011-04-28 15:04:31 -07008931 I915_WRITE(DSPCNTR(pipe),
8932 I915_READ(DSPCNTR(pipe)) |
8933 DISPPLANE_TRICKLE_FEED_DISABLE);
Keith Packardd74362c2011-07-28 14:47:14 -07008934 intel_flush_display_plane(dev_priv, pipe);
8935 }
Jesse Barnes6067aae2011-04-28 15:04:31 -07008936}
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08008937
Ben Widawskya1e969e2012-04-14 18:41:32 -07008938static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
8939{
8940 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
8941
8942 reg &= ~GEN7_FF_SCHED_MASK;
8943 reg |= GEN7_FF_TS_SCHED_HW;
8944 reg |= GEN7_FF_VS_SCHED_HW;
8945 reg |= GEN7_FF_DS_SCHED_HW;
8946
8947 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
8948}
8949
Jesse Barnes28963a32011-05-11 09:42:30 -07008950static void ivybridge_init_clock_gating(struct drm_device *dev)
8951{
8952 struct drm_i915_private *dev_priv = dev->dev_private;
8953 int pipe;
8954 uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
Jesse Barnes652c3932009-08-17 13:31:43 -07008955
Jesse Barnes28963a32011-05-11 09:42:30 -07008956 I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08008957
Jesse Barnes28963a32011-05-11 09:42:30 -07008958 I915_WRITE(WM3_LP_ILK, 0);
8959 I915_WRITE(WM2_LP_ILK, 0);
8960 I915_WRITE(WM1_LP_ILK, 0);
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08008961
Eugeni Dodonoveae66b52012-02-08 12:53:49 -08008962 /* According to the spec, bit 13 (RCZUNIT) must be set on IVB.
8963 * This implements the WaDisableRCZUnitClockGating workaround.
8964 */
8965 I915_WRITE(GEN6_UCGCTL2, GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
8966
Jesse Barnes28963a32011-05-11 09:42:30 -07008967 I915_WRITE(ILK_DSPCLK_GATE, IVB_VRHUNIT_CLK_GATE);
Eric Anholtde6e2ea2010-11-06 14:53:32 -07008968
Eric Anholt116ac8d2011-12-21 10:31:09 -08008969 I915_WRITE(IVB_CHICKEN3,
8970 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
8971 CHICKEN3_DGMG_DONE_FIX_DISABLE);
8972
Kenneth Graunked71de142012-02-08 12:53:52 -08008973 /* Apply the WaDisableRHWOOptimizationForRenderHang workaround. */
8974 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
8975 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
8976
Eugeni Dodonove4e0c052012-02-08 12:53:50 -08008977 /* WaApplyL3ControlAndL3ChickenMode requires those two on Ivy Bridge */
8978 I915_WRITE(GEN7_L3CNTLREG1,
8979 GEN7_WA_FOR_GEN7_L3_CONTROL);
8980 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
8981 GEN7_WA_L3_CHICKEN_MODE);
8982
Eugeni Dodonovdb099c82012-02-08 12:53:51 -08008983 /* This is required by WaCatErrorRejectionIssue */
8984 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8985 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8986 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8987
Keith Packardd74362c2011-07-28 14:47:14 -07008988 for_each_pipe(pipe) {
Jesse Barnes28963a32011-05-11 09:42:30 -07008989 I915_WRITE(DSPCNTR(pipe),
8990 I915_READ(DSPCNTR(pipe)) |
8991 DISPPLANE_TRICKLE_FEED_DISABLE);
Keith Packardd74362c2011-07-28 14:47:14 -07008992 intel_flush_display_plane(dev_priv, pipe);
8993 }
Ben Widawskya1e969e2012-04-14 18:41:32 -07008994
8995 gen7_setup_fixed_func_scheduler(dev_priv);
Jesse Barnes28963a32011-05-11 09:42:30 -07008996}
Eric Anholt67e92af2010-11-06 14:53:33 -07008997
Jesse Barnesfb046852012-03-28 13:39:26 -07008998static void valleyview_init_clock_gating(struct drm_device *dev)
8999{
9000 struct drm_i915_private *dev_priv = dev->dev_private;
9001 int pipe;
9002 uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
9003
9004 I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
9005
9006 I915_WRITE(WM3_LP_ILK, 0);
9007 I915_WRITE(WM2_LP_ILK, 0);
9008 I915_WRITE(WM1_LP_ILK, 0);
9009
9010 /* According to the spec, bit 13 (RCZUNIT) must be set on IVB.
9011 * This implements the WaDisableRCZUnitClockGating workaround.
9012 */
9013 I915_WRITE(GEN6_UCGCTL2, GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
9014
9015 I915_WRITE(ILK_DSPCLK_GATE, IVB_VRHUNIT_CLK_GATE);
9016
9017 I915_WRITE(IVB_CHICKEN3,
9018 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
9019 CHICKEN3_DGMG_DONE_FIX_DISABLE);
9020
9021 /* Apply the WaDisableRHWOOptimizationForRenderHang workaround. */
9022 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
9023 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
9024
9025 /* WaApplyL3ControlAndL3ChickenMode requires those two on Ivy Bridge */
9026 I915_WRITE(GEN7_L3CNTLREG1, GEN7_WA_FOR_GEN7_L3_CONTROL);
9027 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER, GEN7_WA_L3_CHICKEN_MODE);
9028
9029 /* This is required by WaCatErrorRejectionIssue */
9030 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
9031 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
9032 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
9033
9034 for_each_pipe(pipe) {
9035 I915_WRITE(DSPCNTR(pipe),
9036 I915_READ(DSPCNTR(pipe)) |
9037 DISPPLANE_TRICKLE_FEED_DISABLE);
9038 intel_flush_display_plane(dev_priv, pipe);
9039 }
9040
9041 I915_WRITE(CACHE_MODE_1, I915_READ(CACHE_MODE_1) |
9042 (PIXEL_SUBSPAN_COLLECT_OPT_DISABLE << 16) |
9043 PIXEL_SUBSPAN_COLLECT_OPT_DISABLE);
9044}
9045
Jesse Barnes6067aae2011-04-28 15:04:31 -07009046static void g4x_init_clock_gating(struct drm_device *dev)
9047{
9048 struct drm_i915_private *dev_priv = dev->dev_private;
9049 uint32_t dspclk_gate;
Chris Wilson8fd26852010-12-08 18:40:43 +00009050
Jesse Barnes6067aae2011-04-28 15:04:31 -07009051 I915_WRITE(RENCLK_GATE_D1, 0);
9052 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
9053 GS_UNIT_CLOCK_GATE_DISABLE |
9054 CL_UNIT_CLOCK_GATE_DISABLE);
9055 I915_WRITE(RAMCLK_GATE_D, 0);
9056 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
9057 OVRUNIT_CLOCK_GATE_DISABLE |
9058 OVCUNIT_CLOCK_GATE_DISABLE;
9059 if (IS_GM45(dev))
9060 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
9061 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
9062}
Yuanhan Liu13982612010-12-15 15:42:31 +08009063
Jesse Barnes6067aae2011-04-28 15:04:31 -07009064static void crestline_init_clock_gating(struct drm_device *dev)
9065{
9066 struct drm_i915_private *dev_priv = dev->dev_private;
Yuanhan Liu13982612010-12-15 15:42:31 +08009067
Jesse Barnes6067aae2011-04-28 15:04:31 -07009068 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
9069 I915_WRITE(RENCLK_GATE_D2, 0);
9070 I915_WRITE(DSPCLK_GATE_D, 0);
9071 I915_WRITE(RAMCLK_GATE_D, 0);
9072 I915_WRITE16(DEUC, 0);
9073}
Jesse Barnes652c3932009-08-17 13:31:43 -07009074
Jesse Barnes6067aae2011-04-28 15:04:31 -07009075static void broadwater_init_clock_gating(struct drm_device *dev)
9076{
9077 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -07009078
Jesse Barnes6067aae2011-04-28 15:04:31 -07009079 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
9080 I965_RCC_CLOCK_GATE_DISABLE |
9081 I965_RCPB_CLOCK_GATE_DISABLE |
9082 I965_ISC_CLOCK_GATE_DISABLE |
9083 I965_FBC_CLOCK_GATE_DISABLE);
9084 I915_WRITE(RENCLK_GATE_D2, 0);
9085}
Jesse Barnes652c3932009-08-17 13:31:43 -07009086
Jesse Barnes6067aae2011-04-28 15:04:31 -07009087static void gen3_init_clock_gating(struct drm_device *dev)
9088{
9089 struct drm_i915_private *dev_priv = dev->dev_private;
9090 u32 dstate = I915_READ(D_STATE);
9091
9092 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
9093 DSTATE_DOT_CLOCK_GATING;
9094 I915_WRITE(D_STATE, dstate);
9095}
9096
9097static void i85x_init_clock_gating(struct drm_device *dev)
9098{
9099 struct drm_i915_private *dev_priv = dev->dev_private;
9100
9101 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
9102}
9103
9104static void i830_init_clock_gating(struct drm_device *dev)
9105{
9106 struct drm_i915_private *dev_priv = dev->dev_private;
9107
9108 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes652c3932009-08-17 13:31:43 -07009109}
9110
Jesse Barnes645c62a2011-05-11 09:49:31 -07009111static void ibx_init_clock_gating(struct drm_device *dev)
9112{
9113 struct drm_i915_private *dev_priv = dev->dev_private;
9114
9115 /*
9116 * On Ibex Peak and Cougar Point, we need to disable clock
9117 * gating for the panel power sequencer or it will fail to
9118 * start up when no ports are active.
9119 */
9120 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
9121}
9122
9123static void cpt_init_clock_gating(struct drm_device *dev)
9124{
9125 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes3bcf6032011-07-27 11:51:40 -07009126 int pipe;
Jesse Barnes645c62a2011-05-11 09:49:31 -07009127
9128 /*
9129 * On Ibex Peak and Cougar Point, we need to disable clock
9130 * gating for the panel power sequencer or it will fail to
9131 * start up when no ports are active.
9132 */
9133 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
9134 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
9135 DPLS_EDP_PPS_FIX_DIS);
Jesse Barnes3bcf6032011-07-27 11:51:40 -07009136 /* Without this, mode sets may fail silently on FDI */
9137 for_each_pipe(pipe)
9138 I915_WRITE(TRANS_CHICKEN2(pipe), TRANS_AUTOTRAIN_GEN_STALL_DIS);
Jesse Barnes79e53942008-11-07 14:24:08 -08009139}
9140
Chris Wilsonac668082011-02-09 16:15:32 +00009141static void ironlake_teardown_rc6(struct drm_device *dev)
Chris Wilson0cdab212010-12-05 17:27:06 +00009142{
9143 struct drm_i915_private *dev_priv = dev->dev_private;
9144
9145 if (dev_priv->renderctx) {
Chris Wilsonac668082011-02-09 16:15:32 +00009146 i915_gem_object_unpin(dev_priv->renderctx);
9147 drm_gem_object_unreference(&dev_priv->renderctx->base);
Chris Wilson0cdab212010-12-05 17:27:06 +00009148 dev_priv->renderctx = NULL;
9149 }
9150
9151 if (dev_priv->pwrctx) {
Chris Wilsonac668082011-02-09 16:15:32 +00009152 i915_gem_object_unpin(dev_priv->pwrctx);
9153 drm_gem_object_unreference(&dev_priv->pwrctx->base);
Chris Wilson0cdab212010-12-05 17:27:06 +00009154 dev_priv->pwrctx = NULL;
9155 }
9156}
9157
Jesse Barnesd5bb0812011-01-05 12:01:26 -08009158static void ironlake_disable_rc6(struct drm_device *dev)
9159{
9160 struct drm_i915_private *dev_priv = dev->dev_private;
9161
Chris Wilsonac668082011-02-09 16:15:32 +00009162 if (I915_READ(PWRCTXA)) {
9163 /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
9164 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
9165 wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
9166 50);
9167
9168 I915_WRITE(PWRCTXA, 0);
9169 POSTING_READ(PWRCTXA);
9170
9171 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
9172 POSTING_READ(RSTDBYCTL);
9173 }
9174
Chris Wilson99507302011-02-24 09:42:52 +00009175 ironlake_teardown_rc6(dev);
Chris Wilsonac668082011-02-09 16:15:32 +00009176}
9177
9178static int ironlake_setup_rc6(struct drm_device *dev)
9179{
9180 struct drm_i915_private *dev_priv = dev->dev_private;
9181
9182 if (dev_priv->renderctx == NULL)
9183 dev_priv->renderctx = intel_alloc_context_page(dev);
9184 if (!dev_priv->renderctx)
9185 return -ENOMEM;
9186
9187 if (dev_priv->pwrctx == NULL)
9188 dev_priv->pwrctx = intel_alloc_context_page(dev);
9189 if (!dev_priv->pwrctx) {
9190 ironlake_teardown_rc6(dev);
9191 return -ENOMEM;
9192 }
9193
9194 return 0;
Jesse Barnesd5bb0812011-01-05 12:01:26 -08009195}
9196
9197void ironlake_enable_rc6(struct drm_device *dev)
9198{
9199 struct drm_i915_private *dev_priv = dev->dev_private;
9200 int ret;
9201
Chris Wilsonac668082011-02-09 16:15:32 +00009202 /* rc6 disabled by default due to repeated reports of hanging during
9203 * boot and resume.
9204 */
Keith Packardc0f372b32011-11-16 22:24:52 -08009205 if (!intel_enable_rc6(dev))
Chris Wilsonac668082011-02-09 16:15:32 +00009206 return;
9207
Ben Widawsky2c34b852011-03-19 18:14:26 -07009208 mutex_lock(&dev->struct_mutex);
Chris Wilsonac668082011-02-09 16:15:32 +00009209 ret = ironlake_setup_rc6(dev);
Ben Widawsky2c34b852011-03-19 18:14:26 -07009210 if (ret) {
9211 mutex_unlock(&dev->struct_mutex);
Chris Wilsonac668082011-02-09 16:15:32 +00009212 return;
Ben Widawsky2c34b852011-03-19 18:14:26 -07009213 }
Chris Wilsonac668082011-02-09 16:15:32 +00009214
Jesse Barnesd5bb0812011-01-05 12:01:26 -08009215 /*
9216 * GPU can automatically power down the render unit if given a page
9217 * to save state.
9218 */
9219 ret = BEGIN_LP_RING(6);
9220 if (ret) {
Chris Wilsonac668082011-02-09 16:15:32 +00009221 ironlake_teardown_rc6(dev);
Ben Widawsky2c34b852011-03-19 18:14:26 -07009222 mutex_unlock(&dev->struct_mutex);
Jesse Barnesd5bb0812011-01-05 12:01:26 -08009223 return;
9224 }
Chris Wilsonac668082011-02-09 16:15:32 +00009225
Jesse Barnesd5bb0812011-01-05 12:01:26 -08009226 OUT_RING(MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
9227 OUT_RING(MI_SET_CONTEXT);
9228 OUT_RING(dev_priv->renderctx->gtt_offset |
9229 MI_MM_SPACE_GTT |
9230 MI_SAVE_EXT_STATE_EN |
9231 MI_RESTORE_EXT_STATE_EN |
9232 MI_RESTORE_INHIBIT);
9233 OUT_RING(MI_SUSPEND_FLUSH);
9234 OUT_RING(MI_NOOP);
9235 OUT_RING(MI_FLUSH);
9236 ADVANCE_LP_RING();
9237
Ben Widawsky4a246cf2011-03-19 18:14:28 -07009238 /*
9239 * Wait for the command parser to advance past MI_SET_CONTEXT. The HW
9240 * does an implicit flush, combined with MI_FLUSH above, it should be
9241 * safe to assume that renderctx is valid
9242 */
9243 ret = intel_wait_ring_idle(LP_RING(dev_priv));
9244 if (ret) {
9245 DRM_ERROR("failed to enable ironlake power power savings\n");
9246 ironlake_teardown_rc6(dev);
9247 mutex_unlock(&dev->struct_mutex);
9248 return;
9249 }
9250
Jesse Barnesd5bb0812011-01-05 12:01:26 -08009251 I915_WRITE(PWRCTXA, dev_priv->pwrctx->gtt_offset | PWRCTX_EN);
9252 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
Ben Widawsky2c34b852011-03-19 18:14:26 -07009253 mutex_unlock(&dev->struct_mutex);
Jesse Barnesd5bb0812011-01-05 12:01:26 -08009254}
9255
Jesse Barnes645c62a2011-05-11 09:49:31 -07009256void intel_init_clock_gating(struct drm_device *dev)
9257{
9258 struct drm_i915_private *dev_priv = dev->dev_private;
9259
9260 dev_priv->display.init_clock_gating(dev);
9261
9262 if (dev_priv->display.init_pch_clock_gating)
9263 dev_priv->display.init_pch_clock_gating(dev);
9264}
Chris Wilsonac668082011-02-09 16:15:32 +00009265
Jesse Barnese70236a2009-09-21 10:42:27 -07009266/* Set up chip specific display functions */
9267static void intel_init_display(struct drm_device *dev)
9268{
9269 struct drm_i915_private *dev_priv = dev->dev_private;
9270
9271 /* We always want a DPMS function */
Eric Anholtf564048e2011-03-30 13:01:02 -07009272 if (HAS_PCH_SPLIT(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05009273 dev_priv->display.dpms = ironlake_crtc_dpms;
Eric Anholtf564048e2011-03-30 13:01:02 -07009274 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Jesse Barnes17638cd2011-06-24 12:19:23 -07009275 dev_priv->display.update_plane = ironlake_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07009276 } else {
Jesse Barnese70236a2009-09-21 10:42:27 -07009277 dev_priv->display.dpms = i9xx_crtc_dpms;
Eric Anholtf564048e2011-03-30 13:01:02 -07009278 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Jesse Barnes17638cd2011-06-24 12:19:23 -07009279 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07009280 }
Jesse Barnese70236a2009-09-21 10:42:27 -07009281
Adam Jacksonee5382a2010-04-23 11:17:39 -04009282 if (I915_HAS_FBC(dev)) {
Yuanhan Liu9c04f012010-12-15 15:42:32 +08009283 if (HAS_PCH_SPLIT(dev)) {
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08009284 dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
9285 dev_priv->display.enable_fbc = ironlake_enable_fbc;
9286 dev_priv->display.disable_fbc = ironlake_disable_fbc;
9287 } else if (IS_GM45(dev)) {
Jesse Barnes74dff282009-09-14 15:39:40 -07009288 dev_priv->display.fbc_enabled = g4x_fbc_enabled;
9289 dev_priv->display.enable_fbc = g4x_enable_fbc;
9290 dev_priv->display.disable_fbc = g4x_disable_fbc;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009291 } else if (IS_CRESTLINE(dev)) {
Jesse Barnese70236a2009-09-21 10:42:27 -07009292 dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
9293 dev_priv->display.enable_fbc = i8xx_enable_fbc;
9294 dev_priv->display.disable_fbc = i8xx_disable_fbc;
9295 }
Jesse Barnes74dff282009-09-14 15:39:40 -07009296 /* 855GM needs testing */
Jesse Barnese70236a2009-09-21 10:42:27 -07009297 }
9298
9299 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07009300 if (IS_VALLEYVIEW(dev))
9301 dev_priv->display.get_display_clock_speed =
9302 valleyview_get_display_clock_speed;
9303 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -07009304 dev_priv->display.get_display_clock_speed =
9305 i945_get_display_clock_speed;
9306 else if (IS_I915G(dev))
9307 dev_priv->display.get_display_clock_speed =
9308 i915_get_display_clock_speed;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05009309 else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07009310 dev_priv->display.get_display_clock_speed =
9311 i9xx_misc_get_display_clock_speed;
9312 else if (IS_I915GM(dev))
9313 dev_priv->display.get_display_clock_speed =
9314 i915gm_get_display_clock_speed;
9315 else if (IS_I865G(dev))
9316 dev_priv->display.get_display_clock_speed =
9317 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +02009318 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07009319 dev_priv->display.get_display_clock_speed =
9320 i855_get_display_clock_speed;
9321 else /* 852, 830 */
9322 dev_priv->display.get_display_clock_speed =
9323 i830_get_display_clock_speed;
9324
9325 /* For FIFO watermark updates */
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08009326 if (HAS_PCH_SPLIT(dev)) {
Keith Packard8d715f02011-11-18 20:39:01 -08009327 dev_priv->display.force_wake_get = __gen6_gt_force_wake_get;
9328 dev_priv->display.force_wake_put = __gen6_gt_force_wake_put;
9329
9330 /* IVB configs may use multi-threaded forcewake */
Eugeni Dodonov246bdbe2012-04-13 17:08:44 -03009331 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Keith Packard8d715f02011-11-18 20:39:01 -08009332 u32 ecobus;
9333
Keith Packardc7dffff2011-12-09 11:33:00 -08009334 /* A small trick here - if the bios hasn't configured MT forcewake,
9335 * and if the device is in RC6, then force_wake_mt_get will not wake
9336 * the device and the ECOBUS read will return zero. Which will be
9337 * (correctly) interpreted by the test below as MT forcewake being
9338 * disabled.
9339 */
Keith Packard8d715f02011-11-18 20:39:01 -08009340 mutex_lock(&dev->struct_mutex);
9341 __gen6_gt_force_wake_mt_get(dev_priv);
Keith Packardc7dffff2011-12-09 11:33:00 -08009342 ecobus = I915_READ_NOTRACE(ECOBUS);
Keith Packard8d715f02011-11-18 20:39:01 -08009343 __gen6_gt_force_wake_mt_put(dev_priv);
9344 mutex_unlock(&dev->struct_mutex);
9345
9346 if (ecobus & FORCEWAKE_MT_ENABLE) {
9347 DRM_DEBUG_KMS("Using MT version of forcewake\n");
9348 dev_priv->display.force_wake_get =
9349 __gen6_gt_force_wake_mt_get;
9350 dev_priv->display.force_wake_put =
9351 __gen6_gt_force_wake_mt_put;
9352 }
9353 }
9354
Jesse Barnes645c62a2011-05-11 09:49:31 -07009355 if (HAS_PCH_IBX(dev))
9356 dev_priv->display.init_pch_clock_gating = ibx_init_clock_gating;
9357 else if (HAS_PCH_CPT(dev))
9358 dev_priv->display.init_pch_clock_gating = cpt_init_clock_gating;
9359
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01009360 if (IS_GEN5(dev)) {
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08009361 if (I915_READ(MLTR_ILK) & ILK_SRLT_MASK)
9362 dev_priv->display.update_wm = ironlake_update_wm;
9363 else {
9364 DRM_DEBUG_KMS("Failed to get proper latency. "
9365 "Disable CxSR\n");
9366 dev_priv->display.update_wm = NULL;
9367 }
Jesse Barnes674cf962011-04-28 14:27:04 -07009368 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Jesse Barnes6067aae2011-04-28 15:04:31 -07009369 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
Wu Fengguange0dac652011-09-05 14:25:34 +08009370 dev_priv->display.write_eld = ironlake_write_eld;
Yuanhan Liu13982612010-12-15 15:42:31 +08009371 } else if (IS_GEN6(dev)) {
9372 if (SNB_READ_WM0_LATENCY()) {
9373 dev_priv->display.update_wm = sandybridge_update_wm;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08009374 dev_priv->display.update_sprite_wm = sandybridge_update_sprite_wm;
Yuanhan Liu13982612010-12-15 15:42:31 +08009375 } else {
9376 DRM_DEBUG_KMS("Failed to read display plane latency. "
9377 "Disable CxSR\n");
9378 dev_priv->display.update_wm = NULL;
9379 }
Jesse Barnes674cf962011-04-28 14:27:04 -07009380 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Jesse Barnes6067aae2011-04-28 15:04:31 -07009381 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
Wu Fengguange0dac652011-09-05 14:25:34 +08009382 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnes357555c2011-04-28 15:09:55 -07009383 } else if (IS_IVYBRIDGE(dev)) {
9384 /* FIXME: detect B0+ stepping and use auto training */
9385 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Jesse Barnesfe100d42011-04-28 14:29:45 -07009386 if (SNB_READ_WM0_LATENCY()) {
9387 dev_priv->display.update_wm = sandybridge_update_wm;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08009388 dev_priv->display.update_sprite_wm = sandybridge_update_sprite_wm;
Jesse Barnesfe100d42011-04-28 14:29:45 -07009389 } else {
9390 DRM_DEBUG_KMS("Failed to read display plane latency. "
9391 "Disable CxSR\n");
9392 dev_priv->display.update_wm = NULL;
9393 }
Jesse Barnes28963a32011-05-11 09:42:30 -07009394 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
Wu Fengguange0dac652011-09-05 14:25:34 +08009395 dev_priv->display.write_eld = ironlake_write_eld;
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08009396 } else
9397 dev_priv->display.update_wm = NULL;
Jesse Barnesceb04242012-03-28 13:39:22 -07009398 } else if (IS_VALLEYVIEW(dev)) {
9399 dev_priv->display.update_wm = valleyview_update_wm;
Jesse Barnesfb046852012-03-28 13:39:26 -07009400 dev_priv->display.init_clock_gating =
9401 valleyview_init_clock_gating;
Jesse Barnes575155a2012-03-28 13:39:37 -07009402 dev_priv->display.force_wake_get = vlv_force_wake_get;
9403 dev_priv->display.force_wake_put = vlv_force_wake_put;
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08009404 } else if (IS_PINEVIEW(dev)) {
Zhao Yakuid4294342010-03-22 22:45:36 +08009405 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
Li Peng95534262010-05-18 18:58:44 +08009406 dev_priv->is_ddr3,
Zhao Yakuid4294342010-03-22 22:45:36 +08009407 dev_priv->fsb_freq,
9408 dev_priv->mem_freq)) {
9409 DRM_INFO("failed to find known CxSR latency "
Li Peng95534262010-05-18 18:58:44 +08009410 "(found ddr%s fsb freq %d, mem freq %d), "
Zhao Yakuid4294342010-03-22 22:45:36 +08009411 "disabling CxSR\n",
Akshay Joshi0206e352011-08-16 15:34:10 -04009412 (dev_priv->is_ddr3 == 1) ? "3" : "2",
Zhao Yakuid4294342010-03-22 22:45:36 +08009413 dev_priv->fsb_freq, dev_priv->mem_freq);
9414 /* Disable CxSR and never update its watermark again */
9415 pineview_disable_cxsr(dev);
9416 dev_priv->display.update_wm = NULL;
9417 } else
9418 dev_priv->display.update_wm = pineview_update_wm;
Jason Stubbs95e0ee92011-05-28 14:26:48 +10009419 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
Jesse Barnes6067aae2011-04-28 15:04:31 -07009420 } else if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +08009421 dev_priv->display.write_eld = g4x_write_eld;
Jesse Barnese70236a2009-09-21 10:42:27 -07009422 dev_priv->display.update_wm = g4x_update_wm;
Jesse Barnes6067aae2011-04-28 15:04:31 -07009423 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
9424 } else if (IS_GEN4(dev)) {
Jesse Barnese70236a2009-09-21 10:42:27 -07009425 dev_priv->display.update_wm = i965_update_wm;
Jesse Barnes6067aae2011-04-28 15:04:31 -07009426 if (IS_CRESTLINE(dev))
9427 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
9428 else if (IS_BROADWATER(dev))
9429 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
9430 } else if (IS_GEN3(dev)) {
Jesse Barnese70236a2009-09-21 10:42:27 -07009431 dev_priv->display.update_wm = i9xx_update_wm;
9432 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Jesse Barnes6067aae2011-04-28 15:04:31 -07009433 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
9434 } else if (IS_I865G(dev)) {
9435 dev_priv->display.update_wm = i830_update_wm;
9436 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
9437 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Adam Jackson8f4695e2010-04-16 18:20:57 -04009438 } else if (IS_I85X(dev)) {
9439 dev_priv->display.update_wm = i9xx_update_wm;
9440 dev_priv->display.get_fifo_size = i85x_get_fifo_size;
Jesse Barnes6067aae2011-04-28 15:04:31 -07009441 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
Jesse Barnese70236a2009-09-21 10:42:27 -07009442 } else {
Adam Jackson8f4695e2010-04-16 18:20:57 -04009443 dev_priv->display.update_wm = i830_update_wm;
Jesse Barnes6067aae2011-04-28 15:04:31 -07009444 dev_priv->display.init_clock_gating = i830_init_clock_gating;
Adam Jackson8f4695e2010-04-16 18:20:57 -04009445 if (IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07009446 dev_priv->display.get_fifo_size = i845_get_fifo_size;
9447 else
9448 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Jesse Barnese70236a2009-09-21 10:42:27 -07009449 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009450
9451 /* Default just returns -ENODEV to indicate unsupported */
9452 dev_priv->display.queue_flip = intel_default_queue_flip;
9453
9454 switch (INTEL_INFO(dev)->gen) {
9455 case 2:
9456 dev_priv->display.queue_flip = intel_gen2_queue_flip;
9457 break;
9458
9459 case 3:
9460 dev_priv->display.queue_flip = intel_gen3_queue_flip;
9461 break;
9462
9463 case 4:
9464 case 5:
9465 dev_priv->display.queue_flip = intel_gen4_queue_flip;
9466 break;
9467
9468 case 6:
9469 dev_priv->display.queue_flip = intel_gen6_queue_flip;
9470 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009471 case 7:
9472 dev_priv->display.queue_flip = intel_gen7_queue_flip;
9473 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009474 }
Jesse Barnese70236a2009-09-21 10:42:27 -07009475}
9476
Jesse Barnesb690e962010-07-19 13:53:12 -07009477/*
9478 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
9479 * resume, or other times. This quirk makes sure that's the case for
9480 * affected systems.
9481 */
Akshay Joshi0206e352011-08-16 15:34:10 -04009482static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -07009483{
9484 struct drm_i915_private *dev_priv = dev->dev_private;
9485
9486 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02009487 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -07009488}
9489
Keith Packard435793d2011-07-12 14:56:22 -07009490/*
9491 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
9492 */
9493static void quirk_ssc_force_disable(struct drm_device *dev)
9494{
9495 struct drm_i915_private *dev_priv = dev->dev_private;
9496 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02009497 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -07009498}
9499
Carsten Emde4dca20e2012-03-15 15:56:26 +01009500/*
Carsten Emde5a15ab52012-03-15 15:56:27 +01009501 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
9502 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +01009503 */
9504static void quirk_invert_brightness(struct drm_device *dev)
9505{
9506 struct drm_i915_private *dev_priv = dev->dev_private;
9507 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02009508 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -07009509}
9510
9511struct intel_quirk {
9512 int device;
9513 int subsystem_vendor;
9514 int subsystem_device;
9515 void (*hook)(struct drm_device *dev);
9516};
9517
Ben Widawskyc43b5632012-04-16 14:07:40 -07009518static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -07009519 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -04009520 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -07009521
9522 /* Thinkpad R31 needs pipe A force quirk */
9523 { 0x3577, 0x1014, 0x0505, quirk_pipea_force },
9524 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
9525 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
9526
9527 /* ThinkPad X30 needs pipe A force quirk (LP: #304614) */
9528 { 0x3577, 0x1014, 0x0513, quirk_pipea_force },
9529 /* ThinkPad X40 needs pipe A force quirk */
9530
9531 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
9532 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
9533
9534 /* 855 & before need to leave pipe A & dpll A up */
9535 { 0x3582, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
9536 { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Keith Packard435793d2011-07-12 14:56:22 -07009537
9538 /* Lenovo U160 cannot use SSC on LVDS */
9539 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +02009540
9541 /* Sony Vaio Y cannot use SSC on LVDS */
9542 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +01009543
9544 /* Acer Aspire 5734Z must invert backlight brightness */
9545 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
Jesse Barnesb690e962010-07-19 13:53:12 -07009546};
9547
9548static void intel_init_quirks(struct drm_device *dev)
9549{
9550 struct pci_dev *d = dev->pdev;
9551 int i;
9552
9553 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
9554 struct intel_quirk *q = &intel_quirks[i];
9555
9556 if (d->device == q->device &&
9557 (d->subsystem_vendor == q->subsystem_vendor ||
9558 q->subsystem_vendor == PCI_ANY_ID) &&
9559 (d->subsystem_device == q->subsystem_device ||
9560 q->subsystem_device == PCI_ANY_ID))
9561 q->hook(dev);
9562 }
9563}
9564
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009565/* Disable the VGA plane that we never use */
9566static void i915_disable_vga(struct drm_device *dev)
9567{
9568 struct drm_i915_private *dev_priv = dev->dev_private;
9569 u8 sr1;
9570 u32 vga_reg;
9571
9572 if (HAS_PCH_SPLIT(dev))
9573 vga_reg = CPU_VGACNTRL;
9574 else
9575 vga_reg = VGACNTRL;
9576
9577 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -07009578 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009579 sr1 = inb(VGA_SR_DATA);
9580 outb(sr1 | 1<<5, VGA_SR_DATA);
9581 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
9582 udelay(300);
9583
9584 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9585 POSTING_READ(vga_reg);
9586}
9587
Jesse Barnesf82cfb62012-04-11 09:23:35 -07009588static void ivb_pch_pwm_override(struct drm_device *dev)
9589{
9590 struct drm_i915_private *dev_priv = dev->dev_private;
9591
9592 /*
9593 * IVB has CPU eDP backlight regs too, set things up to let the
9594 * PCH regs control the backlight
9595 */
9596 I915_WRITE(BLC_PWM_CPU_CTL2, PWM_ENABLE);
9597 I915_WRITE(BLC_PWM_CPU_CTL, 0);
9598 I915_WRITE(BLC_PWM_PCH_CTL1, PWM_ENABLE | (1<<30));
9599}
9600
Daniel Vetterf8175862012-04-10 15:50:11 +02009601void intel_modeset_init_hw(struct drm_device *dev)
9602{
9603 struct drm_i915_private *dev_priv = dev->dev_private;
9604
9605 intel_init_clock_gating(dev);
9606
9607 if (IS_IRONLAKE_M(dev)) {
9608 ironlake_enable_drps(dev);
9609 intel_init_emon(dev);
9610 }
9611
Jesse Barnesb6834bd2012-04-11 09:23:33 -07009612 if ((IS_GEN6(dev) || IS_GEN7(dev)) && !IS_VALLEYVIEW(dev)) {
Daniel Vetterf8175862012-04-10 15:50:11 +02009613 gen6_enable_rps(dev_priv);
9614 gen6_update_ring_freq(dev_priv);
9615 }
Jesse Barnesf82cfb62012-04-11 09:23:35 -07009616
9617 if (IS_IVYBRIDGE(dev))
9618 ivb_pch_pwm_override(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +02009619}
9620
Jesse Barnes79e53942008-11-07 14:24:08 -08009621void intel_modeset_init(struct drm_device *dev)
9622{
Jesse Barnes652c3932009-08-17 13:31:43 -07009623 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08009624 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08009625
9626 drm_mode_config_init(dev);
9627
9628 dev->mode_config.min_width = 0;
9629 dev->mode_config.min_height = 0;
9630
Dave Airlie019d96c2011-09-29 16:20:42 +01009631 dev->mode_config.preferred_depth = 24;
9632 dev->mode_config.prefer_shadow = 1;
9633
Jesse Barnes79e53942008-11-07 14:24:08 -08009634 dev->mode_config.funcs = (void *)&intel_mode_funcs;
9635
Jesse Barnesb690e962010-07-19 13:53:12 -07009636 intel_init_quirks(dev);
9637
Jesse Barnese70236a2009-09-21 10:42:27 -07009638 intel_init_display(dev);
9639
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009640 if (IS_GEN2(dev)) {
9641 dev->mode_config.max_width = 2048;
9642 dev->mode_config.max_height = 2048;
9643 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -07009644 dev->mode_config.max_width = 4096;
9645 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -08009646 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009647 dev->mode_config.max_width = 8192;
9648 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -08009649 }
Chris Wilson35c30472010-12-22 14:07:12 +00009650 dev->mode_config.fb_base = dev->agp->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08009651
Zhao Yakui28c97732009-10-09 11:39:41 +08009652 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Dave Airliea3524f12010-06-06 18:59:41 +10009653 dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -08009654
Dave Airliea3524f12010-06-06 18:59:41 +10009655 for (i = 0; i < dev_priv->num_pipe; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009656 intel_crtc_init(dev, i);
Jesse Barnes00c2064b2012-01-13 15:48:39 -08009657 ret = intel_plane_init(dev, i);
9658 if (ret)
9659 DRM_DEBUG_KMS("plane %d init failed: %d\n", i, ret);
Jesse Barnes79e53942008-11-07 14:24:08 -08009660 }
9661
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009662 /* Just disable it once at startup */
9663 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009664 intel_setup_outputs(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -07009665
Daniel Vetterf8175862012-04-10 15:50:11 +02009666 intel_modeset_init_hw(dev);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08009667
Jesse Barnes652c3932009-08-17 13:31:43 -07009668 INIT_WORK(&dev_priv->idle_work, intel_idle_update);
9669 setup_timer(&dev_priv->idle_timer, intel_gpu_idle_timer,
9670 (unsigned long)dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01009671}
9672
9673void intel_modeset_gem_init(struct drm_device *dev)
9674{
9675 if (IS_IRONLAKE_M(dev))
9676 ironlake_enable_rc6(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02009677
9678 intel_setup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009679}
9680
9681void intel_modeset_cleanup(struct drm_device *dev)
9682{
Jesse Barnes652c3932009-08-17 13:31:43 -07009683 struct drm_i915_private *dev_priv = dev->dev_private;
9684 struct drm_crtc *crtc;
9685 struct intel_crtc *intel_crtc;
9686
Keith Packardf87ea762010-10-03 19:36:26 -07009687 drm_kms_helper_poll_fini(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -07009688 mutex_lock(&dev->struct_mutex);
9689
Jesse Barnes723bfd72010-10-07 16:01:13 -07009690 intel_unregister_dsm_handler();
9691
9692
Jesse Barnes652c3932009-08-17 13:31:43 -07009693 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
9694 /* Skip inactive CRTCs */
9695 if (!crtc->fb)
9696 continue;
9697
9698 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3dec0092010-08-20 21:40:52 +02009699 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07009700 }
9701
Chris Wilson973d04f2011-07-08 12:22:37 +01009702 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -07009703
Jesse Barnesf97108d2010-01-29 11:27:07 -08009704 if (IS_IRONLAKE_M(dev))
9705 ironlake_disable_drps(dev);
Jesse Barnesb6834bd2012-04-11 09:23:33 -07009706 if ((IS_GEN6(dev) || IS_GEN7(dev)) && !IS_VALLEYVIEW(dev))
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08009707 gen6_disable_rps(dev);
Jesse Barnesf97108d2010-01-29 11:27:07 -08009708
Jesse Barnesd5bb0812011-01-05 12:01:26 -08009709 if (IS_IRONLAKE_M(dev))
9710 ironlake_disable_rc6(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +00009711
Jesse Barnes57f350b2012-03-28 13:39:25 -07009712 if (IS_VALLEYVIEW(dev))
9713 vlv_init_dpio(dev);
9714
Kristian Høgsberg69341a52009-11-11 12:19:17 -05009715 mutex_unlock(&dev->struct_mutex);
9716
Daniel Vetter6c0d93502010-08-20 18:26:46 +02009717 /* Disable the irq before mode object teardown, for the irq might
9718 * enqueue unpin/hotplug work. */
9719 drm_irq_uninstall(dev);
9720 cancel_work_sync(&dev_priv->hotplug_work);
Daniel Vetter6fdd4d92011-09-08 14:00:22 +02009721 cancel_work_sync(&dev_priv->rps_work);
Daniel Vetter6c0d93502010-08-20 18:26:46 +02009722
Chris Wilson1630fe72011-07-08 12:22:42 +01009723 /* flush any delayed tasks or pending work */
9724 flush_scheduled_work();
9725
Daniel Vetter3dec0092010-08-20 21:40:52 +02009726 /* Shut off idle work before the crtcs get freed. */
9727 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
9728 intel_crtc = to_intel_crtc(crtc);
9729 del_timer_sync(&intel_crtc->idle_timer);
9730 }
9731 del_timer_sync(&dev_priv->idle_timer);
9732 cancel_work_sync(&dev_priv->idle_work);
9733
Jesse Barnes79e53942008-11-07 14:24:08 -08009734 drm_mode_config_cleanup(dev);
9735}
9736
Dave Airlie28d52042009-09-21 14:33:58 +10009737/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +08009738 * Return which encoder is currently attached for connector.
9739 */
Chris Wilsondf0e9242010-09-09 16:20:55 +01009740struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -08009741{
Chris Wilsondf0e9242010-09-09 16:20:55 +01009742 return &intel_attached_encoder(connector)->base;
9743}
Jesse Barnes79e53942008-11-07 14:24:08 -08009744
Chris Wilsondf0e9242010-09-09 16:20:55 +01009745void intel_connector_attach_encoder(struct intel_connector *connector,
9746 struct intel_encoder *encoder)
9747{
9748 connector->encoder = encoder;
9749 drm_mode_connector_attach_encoder(&connector->base,
9750 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08009751}
Dave Airlie28d52042009-09-21 14:33:58 +10009752
9753/*
9754 * set vga decode state - true == enable VGA decode
9755 */
9756int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
9757{
9758 struct drm_i915_private *dev_priv = dev->dev_private;
9759 u16 gmch_ctrl;
9760
9761 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
9762 if (state)
9763 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
9764 else
9765 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
9766 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
9767 return 0;
9768}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009769
9770#ifdef CONFIG_DEBUG_FS
9771#include <linux/seq_file.h>
9772
9773struct intel_display_error_state {
9774 struct intel_cursor_error_state {
9775 u32 control;
9776 u32 position;
9777 u32 base;
9778 u32 size;
9779 } cursor[2];
9780
9781 struct intel_pipe_error_state {
9782 u32 conf;
9783 u32 source;
9784
9785 u32 htotal;
9786 u32 hblank;
9787 u32 hsync;
9788 u32 vtotal;
9789 u32 vblank;
9790 u32 vsync;
9791 } pipe[2];
9792
9793 struct intel_plane_error_state {
9794 u32 control;
9795 u32 stride;
9796 u32 size;
9797 u32 pos;
9798 u32 addr;
9799 u32 surface;
9800 u32 tile_offset;
9801 } plane[2];
9802};
9803
9804struct intel_display_error_state *
9805intel_display_capture_error_state(struct drm_device *dev)
9806{
Akshay Joshi0206e352011-08-16 15:34:10 -04009807 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009808 struct intel_display_error_state *error;
9809 int i;
9810
9811 error = kmalloc(sizeof(*error), GFP_ATOMIC);
9812 if (error == NULL)
9813 return NULL;
9814
9815 for (i = 0; i < 2; i++) {
9816 error->cursor[i].control = I915_READ(CURCNTR(i));
9817 error->cursor[i].position = I915_READ(CURPOS(i));
9818 error->cursor[i].base = I915_READ(CURBASE(i));
9819
9820 error->plane[i].control = I915_READ(DSPCNTR(i));
9821 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
9822 error->plane[i].size = I915_READ(DSPSIZE(i));
Akshay Joshi0206e352011-08-16 15:34:10 -04009823 error->plane[i].pos = I915_READ(DSPPOS(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009824 error->plane[i].addr = I915_READ(DSPADDR(i));
9825 if (INTEL_INFO(dev)->gen >= 4) {
9826 error->plane[i].surface = I915_READ(DSPSURF(i));
9827 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
9828 }
9829
9830 error->pipe[i].conf = I915_READ(PIPECONF(i));
9831 error->pipe[i].source = I915_READ(PIPESRC(i));
9832 error->pipe[i].htotal = I915_READ(HTOTAL(i));
9833 error->pipe[i].hblank = I915_READ(HBLANK(i));
9834 error->pipe[i].hsync = I915_READ(HSYNC(i));
9835 error->pipe[i].vtotal = I915_READ(VTOTAL(i));
9836 error->pipe[i].vblank = I915_READ(VBLANK(i));
9837 error->pipe[i].vsync = I915_READ(VSYNC(i));
9838 }
9839
9840 return error;
9841}
9842
9843void
9844intel_display_print_error_state(struct seq_file *m,
9845 struct drm_device *dev,
9846 struct intel_display_error_state *error)
9847{
9848 int i;
9849
9850 for (i = 0; i < 2; i++) {
9851 seq_printf(m, "Pipe [%d]:\n", i);
9852 seq_printf(m, " CONF: %08x\n", error->pipe[i].conf);
9853 seq_printf(m, " SRC: %08x\n", error->pipe[i].source);
9854 seq_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
9855 seq_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
9856 seq_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
9857 seq_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
9858 seq_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
9859 seq_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
9860
9861 seq_printf(m, "Plane [%d]:\n", i);
9862 seq_printf(m, " CNTR: %08x\n", error->plane[i].control);
9863 seq_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
9864 seq_printf(m, " SIZE: %08x\n", error->plane[i].size);
9865 seq_printf(m, " POS: %08x\n", error->plane[i].pos);
9866 seq_printf(m, " ADDR: %08x\n", error->plane[i].addr);
9867 if (INTEL_INFO(dev)->gen >= 4) {
9868 seq_printf(m, " SURF: %08x\n", error->plane[i].surface);
9869 seq_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
9870 }
9871
9872 seq_printf(m, "Cursor [%d]:\n", i);
9873 seq_printf(m, " CNTR: %08x\n", error->cursor[i].control);
9874 seq_printf(m, " POS: %08x\n", error->cursor[i].position);
9875 seq_printf(m, " BASE: %08x\n", error->cursor[i].base);
9876 }
9877}
9878#endif