blob: 6ec81b43ffc0bfb1585438a654073c63d35870f1 [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);
Shaohua Li7662c8b2009-06-26 11:23:55 +080048static void intel_update_watermarks(struct drm_device *dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +020049static void intel_increase_pllclock(struct drm_crtc *crtc);
Chris Wilson6b383a72010-09-13 13:54:26 +010050static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080051
52typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040053 /* given values */
54 int n;
55 int m1, m2;
56 int p1, p2;
57 /* derived values */
58 int dot;
59 int vco;
60 int m;
61 int p;
Jesse Barnes79e53942008-11-07 14:24:08 -080062} intel_clock_t;
63
64typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040065 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -080066} intel_range_t;
67
68typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040069 int dot_limit;
70 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -080071} intel_p2_t;
72
73#define INTEL_P2_NUM 2
Ma Lingd4906092009-03-18 20:13:27 +080074typedef struct intel_limit intel_limit_t;
75struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -040076 intel_range_t dot, vco, n, m, m1, m2, p, p1;
77 intel_p2_t p2;
78 bool (* find_pll)(const intel_limit_t *, struct drm_crtc *,
Sean Paulcec2f352012-01-10 15:09:36 -080079 int, int, intel_clock_t *, intel_clock_t *);
Ma Lingd4906092009-03-18 20:13:27 +080080};
Jesse Barnes79e53942008-11-07 14:24:08 -080081
Jesse Barnes2377b742010-07-07 14:06:43 -070082/* FDI */
83#define IRONLAKE_FDI_FREQ 2700000 /* in kHz for mode->clock */
84
Ma Lingd4906092009-03-18 20:13:27 +080085static bool
86intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -080087 int target, int refclk, intel_clock_t *match_clock,
88 intel_clock_t *best_clock);
Ma Lingd4906092009-03-18 20:13:27 +080089static bool
90intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -080091 int target, int refclk, intel_clock_t *match_clock,
92 intel_clock_t *best_clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080093
Keith Packarda4fc5ed2009-04-07 16:16:42 -070094static bool
95intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -080096 int target, int refclk, intel_clock_t *match_clock,
97 intel_clock_t *best_clock);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080098static bool
Adam Jacksonf2b115e2009-12-03 17:14:42 -050099intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800100 int target, int refclk, intel_clock_t *match_clock,
101 intel_clock_t *best_clock);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700102
Chris Wilson021357a2010-09-07 20:54:59 +0100103static inline u32 /* units of 100MHz */
104intel_fdi_link_freq(struct drm_device *dev)
105{
Chris Wilson8b99e682010-10-13 09:59:17 +0100106 if (IS_GEN5(dev)) {
107 struct drm_i915_private *dev_priv = dev->dev_private;
108 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
109 } else
110 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100111}
112
Keith Packarde4b36692009-06-05 19:22:17 -0700113static const intel_limit_t intel_limits_i8xx_dvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400114 .dot = { .min = 25000, .max = 350000 },
115 .vco = { .min = 930000, .max = 1400000 },
116 .n = { .min = 3, .max = 16 },
117 .m = { .min = 96, .max = 140 },
118 .m1 = { .min = 18, .max = 26 },
119 .m2 = { .min = 6, .max = 16 },
120 .p = { .min = 4, .max = 128 },
121 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700122 .p2 = { .dot_limit = 165000,
123 .p2_slow = 4, .p2_fast = 2 },
Ma Lingd4906092009-03-18 20:13:27 +0800124 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700125};
126
127static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400128 .dot = { .min = 25000, .max = 350000 },
129 .vco = { .min = 930000, .max = 1400000 },
130 .n = { .min = 3, .max = 16 },
131 .m = { .min = 96, .max = 140 },
132 .m1 = { .min = 18, .max = 26 },
133 .m2 = { .min = 6, .max = 16 },
134 .p = { .min = 4, .max = 128 },
135 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700136 .p2 = { .dot_limit = 165000,
137 .p2_slow = 14, .p2_fast = 7 },
Ma Lingd4906092009-03-18 20:13:27 +0800138 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700139};
Eric Anholt273e27c2011-03-30 13:01:10 -0700140
Keith Packarde4b36692009-06-05 19:22:17 -0700141static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400142 .dot = { .min = 20000, .max = 400000 },
143 .vco = { .min = 1400000, .max = 2800000 },
144 .n = { .min = 1, .max = 6 },
145 .m = { .min = 70, .max = 120 },
146 .m1 = { .min = 10, .max = 22 },
147 .m2 = { .min = 5, .max = 9 },
148 .p = { .min = 5, .max = 80 },
149 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700150 .p2 = { .dot_limit = 200000,
151 .p2_slow = 10, .p2_fast = 5 },
Ma Lingd4906092009-03-18 20:13:27 +0800152 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700153};
154
155static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400156 .dot = { .min = 20000, .max = 400000 },
157 .vco = { .min = 1400000, .max = 2800000 },
158 .n = { .min = 1, .max = 6 },
159 .m = { .min = 70, .max = 120 },
160 .m1 = { .min = 10, .max = 22 },
161 .m2 = { .min = 5, .max = 9 },
162 .p = { .min = 7, .max = 98 },
163 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700164 .p2 = { .dot_limit = 112000,
165 .p2_slow = 14, .p2_fast = 7 },
Ma Lingd4906092009-03-18 20:13:27 +0800166 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700167};
168
Eric Anholt273e27c2011-03-30 13:01:10 -0700169
Keith Packarde4b36692009-06-05 19:22:17 -0700170static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700171 .dot = { .min = 25000, .max = 270000 },
172 .vco = { .min = 1750000, .max = 3500000},
173 .n = { .min = 1, .max = 4 },
174 .m = { .min = 104, .max = 138 },
175 .m1 = { .min = 17, .max = 23 },
176 .m2 = { .min = 5, .max = 11 },
177 .p = { .min = 10, .max = 30 },
178 .p1 = { .min = 1, .max = 3},
179 .p2 = { .dot_limit = 270000,
180 .p2_slow = 10,
181 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800182 },
Ma Lingd4906092009-03-18 20:13:27 +0800183 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700184};
185
186static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700187 .dot = { .min = 22000, .max = 400000 },
188 .vco = { .min = 1750000, .max = 3500000},
189 .n = { .min = 1, .max = 4 },
190 .m = { .min = 104, .max = 138 },
191 .m1 = { .min = 16, .max = 23 },
192 .m2 = { .min = 5, .max = 11 },
193 .p = { .min = 5, .max = 80 },
194 .p1 = { .min = 1, .max = 8},
195 .p2 = { .dot_limit = 165000,
196 .p2_slow = 10, .p2_fast = 5 },
Ma Lingd4906092009-03-18 20:13:27 +0800197 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700198};
199
200static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700201 .dot = { .min = 20000, .max = 115000 },
202 .vco = { .min = 1750000, .max = 3500000 },
203 .n = { .min = 1, .max = 3 },
204 .m = { .min = 104, .max = 138 },
205 .m1 = { .min = 17, .max = 23 },
206 .m2 = { .min = 5, .max = 11 },
207 .p = { .min = 28, .max = 112 },
208 .p1 = { .min = 2, .max = 8 },
209 .p2 = { .dot_limit = 0,
210 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800211 },
Ma Lingd4906092009-03-18 20:13:27 +0800212 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700213};
214
215static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700216 .dot = { .min = 80000, .max = 224000 },
217 .vco = { .min = 1750000, .max = 3500000 },
218 .n = { .min = 1, .max = 3 },
219 .m = { .min = 104, .max = 138 },
220 .m1 = { .min = 17, .max = 23 },
221 .m2 = { .min = 5, .max = 11 },
222 .p = { .min = 14, .max = 42 },
223 .p1 = { .min = 2, .max = 6 },
224 .p2 = { .dot_limit = 0,
225 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800226 },
Ma Lingd4906092009-03-18 20:13:27 +0800227 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700228};
229
230static const intel_limit_t intel_limits_g4x_display_port = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400231 .dot = { .min = 161670, .max = 227000 },
232 .vco = { .min = 1750000, .max = 3500000},
233 .n = { .min = 1, .max = 2 },
234 .m = { .min = 97, .max = 108 },
235 .m1 = { .min = 0x10, .max = 0x12 },
236 .m2 = { .min = 0x05, .max = 0x06 },
237 .p = { .min = 10, .max = 20 },
238 .p1 = { .min = 1, .max = 2},
239 .p2 = { .dot_limit = 0,
Eric Anholt273e27c2011-03-30 13:01:10 -0700240 .p2_slow = 10, .p2_fast = 10 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400241 .find_pll = intel_find_pll_g4x_dp,
Keith Packarde4b36692009-06-05 19:22:17 -0700242};
243
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500244static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400245 .dot = { .min = 20000, .max = 400000},
246 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700247 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400248 .n = { .min = 3, .max = 6 },
249 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700250 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400251 .m1 = { .min = 0, .max = 0 },
252 .m2 = { .min = 0, .max = 254 },
253 .p = { .min = 5, .max = 80 },
254 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700255 .p2 = { .dot_limit = 200000,
256 .p2_slow = 10, .p2_fast = 5 },
Shaohua Li61157072009-04-03 15:24:43 +0800257 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700258};
259
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500260static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400261 .dot = { .min = 20000, .max = 400000 },
262 .vco = { .min = 1700000, .max = 3500000 },
263 .n = { .min = 3, .max = 6 },
264 .m = { .min = 2, .max = 256 },
265 .m1 = { .min = 0, .max = 0 },
266 .m2 = { .min = 0, .max = 254 },
267 .p = { .min = 7, .max = 112 },
268 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700269 .p2 = { .dot_limit = 112000,
270 .p2_slow = 14, .p2_fast = 14 },
Shaohua Li61157072009-04-03 15:24:43 +0800271 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700272};
273
Eric Anholt273e27c2011-03-30 13:01:10 -0700274/* Ironlake / Sandybridge
275 *
276 * We calculate clock using (register_value + 2) for N/M1/M2, so here
277 * the range value for them is (actual_value - 2).
278 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800279static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700280 .dot = { .min = 25000, .max = 350000 },
281 .vco = { .min = 1760000, .max = 3510000 },
282 .n = { .min = 1, .max = 5 },
283 .m = { .min = 79, .max = 127 },
284 .m1 = { .min = 12, .max = 22 },
285 .m2 = { .min = 5, .max = 9 },
286 .p = { .min = 5, .max = 80 },
287 .p1 = { .min = 1, .max = 8 },
288 .p2 = { .dot_limit = 225000,
289 .p2_slow = 10, .p2_fast = 5 },
Zhao Yakui45476682009-12-31 16:06:04 +0800290 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700291};
292
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800293static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700294 .dot = { .min = 25000, .max = 350000 },
295 .vco = { .min = 1760000, .max = 3510000 },
296 .n = { .min = 1, .max = 3 },
297 .m = { .min = 79, .max = 118 },
298 .m1 = { .min = 12, .max = 22 },
299 .m2 = { .min = 5, .max = 9 },
300 .p = { .min = 28, .max = 112 },
301 .p1 = { .min = 2, .max = 8 },
302 .p2 = { .dot_limit = 225000,
303 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800304 .find_pll = intel_g4x_find_best_PLL,
305};
306
307static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700308 .dot = { .min = 25000, .max = 350000 },
309 .vco = { .min = 1760000, .max = 3510000 },
310 .n = { .min = 1, .max = 3 },
311 .m = { .min = 79, .max = 127 },
312 .m1 = { .min = 12, .max = 22 },
313 .m2 = { .min = 5, .max = 9 },
314 .p = { .min = 14, .max = 56 },
315 .p1 = { .min = 2, .max = 8 },
316 .p2 = { .dot_limit = 225000,
317 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800318 .find_pll = intel_g4x_find_best_PLL,
319};
320
Eric Anholt273e27c2011-03-30 13:01:10 -0700321/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800322static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700323 .dot = { .min = 25000, .max = 350000 },
324 .vco = { .min = 1760000, .max = 3510000 },
325 .n = { .min = 1, .max = 2 },
326 .m = { .min = 79, .max = 126 },
327 .m1 = { .min = 12, .max = 22 },
328 .m2 = { .min = 5, .max = 9 },
329 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400330 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700331 .p2 = { .dot_limit = 225000,
332 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800333 .find_pll = intel_g4x_find_best_PLL,
334};
335
336static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700337 .dot = { .min = 25000, .max = 350000 },
338 .vco = { .min = 1760000, .max = 3510000 },
339 .n = { .min = 1, .max = 3 },
340 .m = { .min = 79, .max = 126 },
341 .m1 = { .min = 12, .max = 22 },
342 .m2 = { .min = 5, .max = 9 },
343 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400344 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700345 .p2 = { .dot_limit = 225000,
346 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800347 .find_pll = intel_g4x_find_best_PLL,
348};
349
350static const intel_limit_t intel_limits_ironlake_display_port = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400351 .dot = { .min = 25000, .max = 350000 },
352 .vco = { .min = 1760000, .max = 3510000},
353 .n = { .min = 1, .max = 2 },
354 .m = { .min = 81, .max = 90 },
355 .m1 = { .min = 12, .max = 22 },
356 .m2 = { .min = 5, .max = 9 },
357 .p = { .min = 10, .max = 20 },
358 .p1 = { .min = 1, .max = 2},
359 .p2 = { .dot_limit = 0,
Eric Anholt273e27c2011-03-30 13:01:10 -0700360 .p2_slow = 10, .p2_fast = 10 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400361 .find_pll = intel_find_pll_ironlake_dp,
Jesse Barnes79e53942008-11-07 14:24:08 -0800362};
363
Jesse Barnes57f350b2012-03-28 13:39:25 -0700364u32 intel_dpio_read(struct drm_i915_private *dev_priv, int reg)
365{
366 unsigned long flags;
367 u32 val = 0;
368
369 spin_lock_irqsave(&dev_priv->dpio_lock, flags);
370 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
371 DRM_ERROR("DPIO idle wait timed out\n");
372 goto out_unlock;
373 }
374
375 I915_WRITE(DPIO_REG, reg);
376 I915_WRITE(DPIO_PKT, DPIO_RID | DPIO_OP_READ | DPIO_PORTID |
377 DPIO_BYTE);
378 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
379 DRM_ERROR("DPIO read wait timed out\n");
380 goto out_unlock;
381 }
382 val = I915_READ(DPIO_DATA);
383
384out_unlock:
385 spin_unlock_irqrestore(&dev_priv->dpio_lock, flags);
386 return val;
387}
388
389static void intel_dpio_write(struct drm_i915_private *dev_priv, int reg,
390 u32 val)
391{
392 unsigned long flags;
393
394 spin_lock_irqsave(&dev_priv->dpio_lock, flags);
395 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
396 DRM_ERROR("DPIO idle wait timed out\n");
397 goto out_unlock;
398 }
399
400 I915_WRITE(DPIO_DATA, val);
401 I915_WRITE(DPIO_REG, reg);
402 I915_WRITE(DPIO_PKT, DPIO_RID | DPIO_OP_WRITE | DPIO_PORTID |
403 DPIO_BYTE);
404 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100))
405 DRM_ERROR("DPIO write wait timed out\n");
406
407out_unlock:
408 spin_unlock_irqrestore(&dev_priv->dpio_lock, flags);
409}
410
411static void vlv_init_dpio(struct drm_device *dev)
412{
413 struct drm_i915_private *dev_priv = dev->dev_private;
414
415 /* Reset the DPIO config */
416 I915_WRITE(DPIO_CTL, 0);
417 POSTING_READ(DPIO_CTL);
418 I915_WRITE(DPIO_CTL, 1);
419 POSTING_READ(DPIO_CTL);
420}
421
Daniel Vetter618563e2012-04-01 13:38:50 +0200422static int intel_dual_link_lvds_callback(const struct dmi_system_id *id)
423{
424 DRM_INFO("Forcing lvds to dual link mode on %s\n", id->ident);
425 return 1;
426}
427
428static const struct dmi_system_id intel_dual_link_lvds[] = {
429 {
430 .callback = intel_dual_link_lvds_callback,
431 .ident = "Apple MacBook Pro (Core i5/i7 Series)",
432 .matches = {
433 DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
434 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro8,2"),
435 },
436 },
437 { } /* terminating entry */
438};
439
Takashi Iwaib0354382012-03-20 13:07:05 +0100440static bool is_dual_link_lvds(struct drm_i915_private *dev_priv,
441 unsigned int reg)
442{
443 unsigned int val;
444
Takashi Iwai121d5272012-03-20 13:07:06 +0100445 /* use the module option value if specified */
446 if (i915_lvds_channel_mode > 0)
447 return i915_lvds_channel_mode == 2;
448
Daniel Vetter618563e2012-04-01 13:38:50 +0200449 if (dmi_check_system(intel_dual_link_lvds))
450 return true;
451
Takashi Iwaib0354382012-03-20 13:07:05 +0100452 if (dev_priv->lvds_val)
453 val = dev_priv->lvds_val;
454 else {
455 /* BIOS should set the proper LVDS register value at boot, but
456 * in reality, it doesn't set the value when the lid is closed;
457 * we need to check "the value to be set" in VBT when LVDS
458 * register is uninitialized.
459 */
460 val = I915_READ(reg);
461 if (!(val & ~LVDS_DETECTED))
462 val = dev_priv->bios_lvds_val;
463 dev_priv->lvds_val = val;
464 }
465 return (val & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP;
466}
467
Chris Wilson1b894b52010-12-14 20:04:54 +0000468static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
469 int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800470{
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800471 struct drm_device *dev = crtc->dev;
472 struct drm_i915_private *dev_priv = dev->dev_private;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800473 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800474
475 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Takashi Iwaib0354382012-03-20 13:07:05 +0100476 if (is_dual_link_lvds(dev_priv, PCH_LVDS)) {
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800477 /* LVDS dual channel */
Chris Wilson1b894b52010-12-14 20:04:54 +0000478 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800479 limit = &intel_limits_ironlake_dual_lvds_100m;
480 else
481 limit = &intel_limits_ironlake_dual_lvds;
482 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000483 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800484 limit = &intel_limits_ironlake_single_lvds_100m;
485 else
486 limit = &intel_limits_ironlake_single_lvds;
487 }
488 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
Zhao Yakui45476682009-12-31 16:06:04 +0800489 HAS_eDP)
490 limit = &intel_limits_ironlake_display_port;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800491 else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800492 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800493
494 return limit;
495}
496
Ma Ling044c7c42009-03-18 20:13:23 +0800497static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
498{
499 struct drm_device *dev = crtc->dev;
500 struct drm_i915_private *dev_priv = dev->dev_private;
501 const intel_limit_t *limit;
502
503 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Takashi Iwaib0354382012-03-20 13:07:05 +0100504 if (is_dual_link_lvds(dev_priv, LVDS))
Ma Ling044c7c42009-03-18 20:13:23 +0800505 /* LVDS with dual channel */
Keith Packarde4b36692009-06-05 19:22:17 -0700506 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800507 else
508 /* LVDS with dual channel */
Keith Packarde4b36692009-06-05 19:22:17 -0700509 limit = &intel_limits_g4x_single_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800510 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
511 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700512 limit = &intel_limits_g4x_hdmi;
Ma Ling044c7c42009-03-18 20:13:23 +0800513 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700514 limit = &intel_limits_g4x_sdvo;
Akshay Joshi0206e352011-08-16 15:34:10 -0400515 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700516 limit = &intel_limits_g4x_display_port;
Ma Ling044c7c42009-03-18 20:13:23 +0800517 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700518 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800519
520 return limit;
521}
522
Chris Wilson1b894b52010-12-14 20:04:54 +0000523static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800524{
525 struct drm_device *dev = crtc->dev;
526 const intel_limit_t *limit;
527
Eric Anholtbad720f2009-10-22 16:11:14 -0700528 if (HAS_PCH_SPLIT(dev))
Chris Wilson1b894b52010-12-14 20:04:54 +0000529 limit = intel_ironlake_limit(crtc, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800530 else if (IS_G4X(dev)) {
Ma Ling044c7c42009-03-18 20:13:23 +0800531 limit = intel_g4x_limit(crtc);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500532 } else if (IS_PINEVIEW(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +0800533 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500534 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800535 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500536 limit = &intel_limits_pineview_sdvo;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100537 } else if (!IS_GEN2(dev)) {
538 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
539 limit = &intel_limits_i9xx_lvds;
540 else
541 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800542 } else {
543 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700544 limit = &intel_limits_i8xx_lvds;
Jesse Barnes79e53942008-11-07 14:24:08 -0800545 else
Keith Packarde4b36692009-06-05 19:22:17 -0700546 limit = &intel_limits_i8xx_dvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800547 }
548 return limit;
549}
550
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500551/* m1 is reserved as 0 in Pineview, n is a ring counter */
552static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800553{
Shaohua Li21778322009-02-23 15:19:16 +0800554 clock->m = clock->m2 + 2;
555 clock->p = clock->p1 * clock->p2;
556 clock->vco = refclk * clock->m / clock->n;
557 clock->dot = clock->vco / clock->p;
558}
559
560static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
561{
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500562 if (IS_PINEVIEW(dev)) {
563 pineview_clock(refclk, clock);
Shaohua Li21778322009-02-23 15:19:16 +0800564 return;
565 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800566 clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
567 clock->p = clock->p1 * clock->p2;
568 clock->vco = refclk * clock->m / (clock->n + 2);
569 clock->dot = clock->vco / clock->p;
570}
571
Jesse Barnes79e53942008-11-07 14:24:08 -0800572/**
573 * Returns whether any output on the specified pipe is of the specified type
574 */
Chris Wilson4ef69c72010-09-09 15:14:28 +0100575bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
Jesse Barnes79e53942008-11-07 14:24:08 -0800576{
Chris Wilson4ef69c72010-09-09 15:14:28 +0100577 struct drm_device *dev = crtc->dev;
578 struct drm_mode_config *mode_config = &dev->mode_config;
579 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -0800580
Chris Wilson4ef69c72010-09-09 15:14:28 +0100581 list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
582 if (encoder->base.crtc == crtc && encoder->type == type)
583 return true;
584
585 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -0800586}
587
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800588#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800589/**
590 * Returns whether the given set of divisors are valid for a given refclk with
591 * the given connectors.
592 */
593
Chris Wilson1b894b52010-12-14 20:04:54 +0000594static bool intel_PLL_is_valid(struct drm_device *dev,
595 const intel_limit_t *limit,
596 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800597{
Jesse Barnes79e53942008-11-07 14:24:08 -0800598 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400599 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800600 if (clock->p < limit->p.min || limit->p.max < clock->p)
Akshay Joshi0206e352011-08-16 15:34:10 -0400601 INTELPllInvalid("p out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800602 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400603 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800604 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400605 INTELPllInvalid("m1 out of range\n");
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500606 if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
Akshay Joshi0206e352011-08-16 15:34:10 -0400607 INTELPllInvalid("m1 <= m2\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800608 if (clock->m < limit->m.min || limit->m.max < clock->m)
Akshay Joshi0206e352011-08-16 15:34:10 -0400609 INTELPllInvalid("m out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800610 if (clock->n < limit->n.min || limit->n.max < clock->n)
Akshay Joshi0206e352011-08-16 15:34:10 -0400611 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800612 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400613 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800614 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
615 * connector, etc., rather than just a single range.
616 */
617 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400618 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800619
620 return true;
621}
622
Ma Lingd4906092009-03-18 20:13:27 +0800623static bool
624intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800625 int target, int refclk, intel_clock_t *match_clock,
626 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800627
Jesse Barnes79e53942008-11-07 14:24:08 -0800628{
629 struct drm_device *dev = crtc->dev;
630 struct drm_i915_private *dev_priv = dev->dev_private;
631 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800632 int err = target;
633
Bruno Prémontbc5e5712009-08-08 13:01:17 +0200634 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Florian Mickler832cc282009-07-13 18:40:32 +0800635 (I915_READ(LVDS)) != 0) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800636 /*
637 * For LVDS, if the panel is on, just rely on its current
638 * settings for dual-channel. We haven't figured out how to
639 * reliably set up different single/dual channel state, if we
640 * even can.
641 */
Takashi Iwaib0354382012-03-20 13:07:05 +0100642 if (is_dual_link_lvds(dev_priv, LVDS))
Jesse Barnes79e53942008-11-07 14:24:08 -0800643 clock.p2 = limit->p2.p2_fast;
644 else
645 clock.p2 = limit->p2.p2_slow;
646 } else {
647 if (target < limit->p2.dot_limit)
648 clock.p2 = limit->p2.p2_slow;
649 else
650 clock.p2 = limit->p2.p2_fast;
651 }
652
Akshay Joshi0206e352011-08-16 15:34:10 -0400653 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800654
Zhao Yakui42158662009-11-20 11:24:18 +0800655 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
656 clock.m1++) {
657 for (clock.m2 = limit->m2.min;
658 clock.m2 <= limit->m2.max; clock.m2++) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500659 /* m1 is always 0 in Pineview */
660 if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
Zhao Yakui42158662009-11-20 11:24:18 +0800661 break;
662 for (clock.n = limit->n.min;
663 clock.n <= limit->n.max; clock.n++) {
664 for (clock.p1 = limit->p1.min;
665 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800666 int this_err;
667
Shaohua Li21778322009-02-23 15:19:16 +0800668 intel_clock(dev, refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000669 if (!intel_PLL_is_valid(dev, limit,
670 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800671 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800672 if (match_clock &&
673 clock.p != match_clock->p)
674 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800675
676 this_err = abs(clock.dot - target);
677 if (this_err < err) {
678 *best_clock = clock;
679 err = this_err;
680 }
681 }
682 }
683 }
684 }
685
686 return (err != target);
687}
688
Ma Lingd4906092009-03-18 20:13:27 +0800689static bool
690intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800691 int target, int refclk, intel_clock_t *match_clock,
692 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800693{
694 struct drm_device *dev = crtc->dev;
695 struct drm_i915_private *dev_priv = dev->dev_private;
696 intel_clock_t clock;
697 int max_n;
698 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400699 /* approximately equals target * 0.00585 */
700 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800701 found = false;
702
703 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Zhao Yakui45476682009-12-31 16:06:04 +0800704 int lvds_reg;
705
Eric Anholtc619eed2010-01-28 16:45:52 -0800706 if (HAS_PCH_SPLIT(dev))
Zhao Yakui45476682009-12-31 16:06:04 +0800707 lvds_reg = PCH_LVDS;
708 else
709 lvds_reg = LVDS;
710 if ((I915_READ(lvds_reg) & LVDS_CLKB_POWER_MASK) ==
Ma Lingd4906092009-03-18 20:13:27 +0800711 LVDS_CLKB_POWER_UP)
712 clock.p2 = limit->p2.p2_fast;
713 else
714 clock.p2 = limit->p2.p2_slow;
715 } else {
716 if (target < limit->p2.dot_limit)
717 clock.p2 = limit->p2.p2_slow;
718 else
719 clock.p2 = limit->p2.p2_fast;
720 }
721
722 memset(best_clock, 0, sizeof(*best_clock));
723 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200724 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800725 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200726 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800727 for (clock.m1 = limit->m1.max;
728 clock.m1 >= limit->m1.min; clock.m1--) {
729 for (clock.m2 = limit->m2.max;
730 clock.m2 >= limit->m2.min; clock.m2--) {
731 for (clock.p1 = limit->p1.max;
732 clock.p1 >= limit->p1.min; clock.p1--) {
733 int this_err;
734
Shaohua Li21778322009-02-23 15:19:16 +0800735 intel_clock(dev, refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000736 if (!intel_PLL_is_valid(dev, limit,
737 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800738 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800739 if (match_clock &&
740 clock.p != match_clock->p)
741 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000742
743 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800744 if (this_err < err_most) {
745 *best_clock = clock;
746 err_most = this_err;
747 max_n = clock.n;
748 found = true;
749 }
750 }
751 }
752 }
753 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800754 return found;
755}
Ma Lingd4906092009-03-18 20:13:27 +0800756
Zhenyu Wang2c072452009-06-05 15:38:42 +0800757static bool
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500758intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800759 int target, int refclk, intel_clock_t *match_clock,
760 intel_clock_t *best_clock)
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800761{
762 struct drm_device *dev = crtc->dev;
763 intel_clock_t clock;
Zhao Yakui45476682009-12-31 16:06:04 +0800764
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800765 if (target < 200000) {
766 clock.n = 1;
767 clock.p1 = 2;
768 clock.p2 = 10;
769 clock.m1 = 12;
770 clock.m2 = 9;
771 } else {
772 clock.n = 2;
773 clock.p1 = 1;
774 clock.p2 = 10;
775 clock.m1 = 14;
776 clock.m2 = 8;
777 }
778 intel_clock(dev, refclk, &clock);
779 memcpy(best_clock, &clock, sizeof(intel_clock_t));
780 return true;
781}
782
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700783/* DisplayPort has only two frequencies, 162MHz and 270MHz */
784static bool
785intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800786 int target, int refclk, intel_clock_t *match_clock,
787 intel_clock_t *best_clock)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700788{
Chris Wilson5eddb702010-09-11 13:48:45 +0100789 intel_clock_t clock;
790 if (target < 200000) {
791 clock.p1 = 2;
792 clock.p2 = 10;
793 clock.n = 2;
794 clock.m1 = 23;
795 clock.m2 = 8;
796 } else {
797 clock.p1 = 1;
798 clock.p2 = 10;
799 clock.n = 1;
800 clock.m1 = 14;
801 clock.m2 = 2;
802 }
803 clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
804 clock.p = (clock.p1 * clock.p2);
805 clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
806 clock.vco = 0;
807 memcpy(best_clock, &clock, sizeof(intel_clock_t));
808 return true;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700809}
810
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700811/**
812 * intel_wait_for_vblank - wait for vblank on a given pipe
813 * @dev: drm device
814 * @pipe: pipe to wait for
815 *
816 * Wait for vblank to occur on a given pipe. Needed for various bits of
817 * mode setting code.
818 */
819void intel_wait_for_vblank(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -0800820{
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700821 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800822 int pipestat_reg = PIPESTAT(pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700823
Chris Wilson300387c2010-09-05 20:25:43 +0100824 /* Clear existing vblank status. Note this will clear any other
825 * sticky status fields as well.
826 *
827 * This races with i915_driver_irq_handler() with the result
828 * that either function could miss a vblank event. Here it is not
829 * fatal, as we will either wait upon the next vblank interrupt or
830 * timeout. Generally speaking intel_wait_for_vblank() is only
831 * called during modeset at which time the GPU should be idle and
832 * should *not* be performing page flips and thus not waiting on
833 * vblanks...
834 * Currently, the result of us stealing a vblank from the irq
835 * handler is that a single frame will be skipped during swapbuffers.
836 */
837 I915_WRITE(pipestat_reg,
838 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
839
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700840 /* Wait for vblank interrupt bit to set */
Chris Wilson481b6af2010-08-23 17:43:35 +0100841 if (wait_for(I915_READ(pipestat_reg) &
842 PIPE_VBLANK_INTERRUPT_STATUS,
843 50))
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700844 DRM_DEBUG_KMS("vblank wait timed out\n");
845}
846
Keith Packardab7ad7f2010-10-03 00:33:06 -0700847/*
848 * intel_wait_for_pipe_off - wait for pipe to turn off
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700849 * @dev: drm device
850 * @pipe: pipe to wait for
851 *
852 * After disabling a pipe, we can't wait for vblank in the usual way,
853 * spinning on the vblank interrupt status bit, since we won't actually
854 * see an interrupt when the pipe is disabled.
855 *
Keith Packardab7ad7f2010-10-03 00:33:06 -0700856 * On Gen4 and above:
857 * wait for the pipe register state bit to turn off
858 *
859 * Otherwise:
860 * wait for the display line value to settle (it usually
861 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +0100862 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700863 */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100864void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700865{
866 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700867
Keith Packardab7ad7f2010-10-03 00:33:06 -0700868 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson58e10eb2010-10-03 10:56:11 +0100869 int reg = PIPECONF(pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700870
Keith Packardab7ad7f2010-10-03 00:33:06 -0700871 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100872 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
873 100))
Keith Packardab7ad7f2010-10-03 00:33:06 -0700874 DRM_DEBUG_KMS("pipe_off wait timed out\n");
875 } else {
876 u32 last_line;
Chris Wilson58e10eb2010-10-03 10:56:11 +0100877 int reg = PIPEDSL(pipe);
Keith Packardab7ad7f2010-10-03 00:33:06 -0700878 unsigned long timeout = jiffies + msecs_to_jiffies(100);
879
880 /* Wait for the display line to settle */
881 do {
Chris Wilson58e10eb2010-10-03 10:56:11 +0100882 last_line = I915_READ(reg) & DSL_LINEMASK;
Keith Packardab7ad7f2010-10-03 00:33:06 -0700883 mdelay(5);
Chris Wilson58e10eb2010-10-03 10:56:11 +0100884 } while (((I915_READ(reg) & DSL_LINEMASK) != last_line) &&
Keith Packardab7ad7f2010-10-03 00:33:06 -0700885 time_after(timeout, jiffies));
886 if (time_after(jiffies, timeout))
887 DRM_DEBUG_KMS("pipe_off wait timed out\n");
888 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800889}
890
Jesse Barnesb24e7172011-01-04 15:09:30 -0800891static const char *state_string(bool enabled)
892{
893 return enabled ? "on" : "off";
894}
895
896/* Only for pre-ILK configs */
897static void assert_pll(struct drm_i915_private *dev_priv,
898 enum pipe pipe, bool state)
899{
900 int reg;
901 u32 val;
902 bool cur_state;
903
904 reg = DPLL(pipe);
905 val = I915_READ(reg);
906 cur_state = !!(val & DPLL_VCO_ENABLE);
907 WARN(cur_state != state,
908 "PLL state assertion failure (expected %s, current %s)\n",
909 state_string(state), state_string(cur_state));
910}
911#define assert_pll_enabled(d, p) assert_pll(d, p, true)
912#define assert_pll_disabled(d, p) assert_pll(d, p, false)
913
Jesse Barnes040484a2011-01-03 12:14:26 -0800914/* For ILK+ */
915static void assert_pch_pll(struct drm_i915_private *dev_priv,
916 enum pipe pipe, bool state)
917{
918 int reg;
919 u32 val;
920 bool cur_state;
921
Jesse Barnesd3ccbe82011-10-12 09:27:42 -0700922 if (HAS_PCH_CPT(dev_priv->dev)) {
923 u32 pch_dpll;
924
925 pch_dpll = I915_READ(PCH_DPLL_SEL);
926
927 /* Make sure the selected PLL is enabled to the transcoder */
928 WARN(!((pch_dpll >> (4 * pipe)) & 8),
929 "transcoder %d PLL not enabled\n", pipe);
930
931 /* Convert the transcoder pipe number to a pll pipe number */
932 pipe = (pch_dpll >> (4 * pipe)) & 1;
933 }
934
Jesse Barnes040484a2011-01-03 12:14:26 -0800935 reg = PCH_DPLL(pipe);
936 val = I915_READ(reg);
937 cur_state = !!(val & DPLL_VCO_ENABLE);
938 WARN(cur_state != state,
939 "PCH PLL state assertion failure (expected %s, current %s)\n",
940 state_string(state), state_string(cur_state));
941}
942#define assert_pch_pll_enabled(d, p) assert_pch_pll(d, p, true)
943#define assert_pch_pll_disabled(d, p) assert_pch_pll(d, p, false)
944
945static void assert_fdi_tx(struct drm_i915_private *dev_priv,
946 enum pipe pipe, bool state)
947{
948 int reg;
949 u32 val;
950 bool cur_state;
951
952 reg = FDI_TX_CTL(pipe);
953 val = I915_READ(reg);
954 cur_state = !!(val & FDI_TX_ENABLE);
955 WARN(cur_state != state,
956 "FDI TX state assertion failure (expected %s, current %s)\n",
957 state_string(state), state_string(cur_state));
958}
959#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
960#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
961
962static void assert_fdi_rx(struct drm_i915_private *dev_priv,
963 enum pipe pipe, bool state)
964{
965 int reg;
966 u32 val;
967 bool cur_state;
968
969 reg = FDI_RX_CTL(pipe);
970 val = I915_READ(reg);
971 cur_state = !!(val & FDI_RX_ENABLE);
972 WARN(cur_state != state,
973 "FDI RX state assertion failure (expected %s, current %s)\n",
974 state_string(state), state_string(cur_state));
975}
976#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
977#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
978
979static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
980 enum pipe pipe)
981{
982 int reg;
983 u32 val;
984
985 /* ILK FDI PLL is always enabled */
986 if (dev_priv->info->gen == 5)
987 return;
988
989 reg = FDI_TX_CTL(pipe);
990 val = I915_READ(reg);
991 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
992}
993
994static void assert_fdi_rx_pll_enabled(struct drm_i915_private *dev_priv,
995 enum pipe pipe)
996{
997 int reg;
998 u32 val;
999
1000 reg = FDI_RX_CTL(pipe);
1001 val = I915_READ(reg);
1002 WARN(!(val & FDI_RX_PLL_ENABLE), "FDI RX PLL assertion failure, should be active but is disabled\n");
1003}
1004
Jesse Barnesea0760c2011-01-04 15:09:32 -08001005static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1006 enum pipe pipe)
1007{
1008 int pp_reg, lvds_reg;
1009 u32 val;
1010 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001011 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001012
1013 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1014 pp_reg = PCH_PP_CONTROL;
1015 lvds_reg = PCH_LVDS;
1016 } else {
1017 pp_reg = PP_CONTROL;
1018 lvds_reg = LVDS;
1019 }
1020
1021 val = I915_READ(pp_reg);
1022 if (!(val & PANEL_POWER_ON) ||
1023 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1024 locked = false;
1025
1026 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1027 panel_pipe = PIPE_B;
1028
1029 WARN(panel_pipe == pipe && locked,
1030 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001031 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001032}
1033
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001034void assert_pipe(struct drm_i915_private *dev_priv,
1035 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001036{
1037 int reg;
1038 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001039 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001040
Daniel Vetter8e636782012-01-22 01:36:48 +01001041 /* if we need the pipe A quirk it must be always on */
1042 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1043 state = true;
1044
Jesse Barnesb24e7172011-01-04 15:09:30 -08001045 reg = PIPECONF(pipe);
1046 val = I915_READ(reg);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001047 cur_state = !!(val & PIPECONF_ENABLE);
1048 WARN(cur_state != state,
1049 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001050 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001051}
1052
Chris Wilson931872f2012-01-16 23:01:13 +00001053static void assert_plane(struct drm_i915_private *dev_priv,
1054 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001055{
1056 int reg;
1057 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001058 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001059
1060 reg = DSPCNTR(plane);
1061 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001062 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1063 WARN(cur_state != state,
1064 "plane %c assertion failure (expected %s, current %s)\n",
1065 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001066}
1067
Chris Wilson931872f2012-01-16 23:01:13 +00001068#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1069#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1070
Jesse Barnesb24e7172011-01-04 15:09:30 -08001071static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1072 enum pipe pipe)
1073{
1074 int reg, i;
1075 u32 val;
1076 int cur_pipe;
1077
Jesse Barnes19ec1352011-02-02 12:28:02 -08001078 /* Planes are fixed to pipes on ILK+ */
Adam Jackson28c057942011-10-07 14:38:42 -04001079 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1080 reg = DSPCNTR(pipe);
1081 val = I915_READ(reg);
1082 WARN((val & DISPLAY_PLANE_ENABLE),
1083 "plane %c assertion failure, should be disabled but not\n",
1084 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001085 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001086 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001087
Jesse Barnesb24e7172011-01-04 15:09:30 -08001088 /* Need to check both planes against the pipe */
1089 for (i = 0; i < 2; i++) {
1090 reg = DSPCNTR(i);
1091 val = I915_READ(reg);
1092 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1093 DISPPLANE_SEL_PIPE_SHIFT;
1094 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001095 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1096 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001097 }
1098}
1099
Jesse Barnes92f25842011-01-04 15:09:34 -08001100static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1101{
1102 u32 val;
1103 bool enabled;
1104
1105 val = I915_READ(PCH_DREF_CONTROL);
1106 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1107 DREF_SUPERSPREAD_SOURCE_MASK));
1108 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1109}
1110
1111static void assert_transcoder_disabled(struct drm_i915_private *dev_priv,
1112 enum pipe pipe)
1113{
1114 int reg;
1115 u32 val;
1116 bool enabled;
1117
1118 reg = TRANSCONF(pipe);
1119 val = I915_READ(reg);
1120 enabled = !!(val & TRANS_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001121 WARN(enabled,
1122 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1123 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001124}
1125
Keith Packard4e634382011-08-06 10:39:45 -07001126static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1127 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001128{
1129 if ((val & DP_PORT_EN) == 0)
1130 return false;
1131
1132 if (HAS_PCH_CPT(dev_priv->dev)) {
1133 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1134 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1135 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1136 return false;
1137 } else {
1138 if ((val & DP_PIPE_MASK) != (pipe << 30))
1139 return false;
1140 }
1141 return true;
1142}
1143
Keith Packard1519b992011-08-06 10:35:34 -07001144static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1145 enum pipe pipe, u32 val)
1146{
1147 if ((val & PORT_ENABLE) == 0)
1148 return false;
1149
1150 if (HAS_PCH_CPT(dev_priv->dev)) {
1151 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1152 return false;
1153 } else {
1154 if ((val & TRANSCODER_MASK) != TRANSCODER(pipe))
1155 return false;
1156 }
1157 return true;
1158}
1159
1160static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1161 enum pipe pipe, u32 val)
1162{
1163 if ((val & LVDS_PORT_EN) == 0)
1164 return false;
1165
1166 if (HAS_PCH_CPT(dev_priv->dev)) {
1167 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1168 return false;
1169 } else {
1170 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1171 return false;
1172 }
1173 return true;
1174}
1175
1176static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1177 enum pipe pipe, u32 val)
1178{
1179 if ((val & ADPA_DAC_ENABLE) == 0)
1180 return false;
1181 if (HAS_PCH_CPT(dev_priv->dev)) {
1182 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1183 return false;
1184 } else {
1185 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1186 return false;
1187 }
1188 return true;
1189}
1190
Jesse Barnes291906f2011-02-02 12:28:03 -08001191static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001192 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001193{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001194 u32 val = I915_READ(reg);
Keith Packard4e634382011-08-06 10:39:45 -07001195 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001196 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001197 reg, pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001198}
1199
1200static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1201 enum pipe pipe, int reg)
1202{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001203 u32 val = I915_READ(reg);
Keith Packard1519b992011-08-06 10:35:34 -07001204 WARN(hdmi_pipe_enabled(dev_priv, val, pipe),
Adam Jackson23c99e72011-10-07 14:38:43 -04001205 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001206 reg, pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001207}
1208
1209static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1210 enum pipe pipe)
1211{
1212 int reg;
1213 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001214
Keith Packardf0575e92011-07-25 22:12:43 -07001215 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1216 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1217 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001218
1219 reg = PCH_ADPA;
1220 val = I915_READ(reg);
Keith Packard1519b992011-08-06 10:35:34 -07001221 WARN(adpa_pipe_enabled(dev_priv, val, pipe),
Jesse Barnes291906f2011-02-02 12:28:03 -08001222 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001223 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001224
1225 reg = PCH_LVDS;
1226 val = I915_READ(reg);
Keith Packard1519b992011-08-06 10:35:34 -07001227 WARN(lvds_pipe_enabled(dev_priv, val, pipe),
Jesse Barnes291906f2011-02-02 12:28:03 -08001228 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001229 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001230
1231 assert_pch_hdmi_disabled(dev_priv, pipe, HDMIB);
1232 assert_pch_hdmi_disabled(dev_priv, pipe, HDMIC);
1233 assert_pch_hdmi_disabled(dev_priv, pipe, HDMID);
1234}
1235
Jesse Barnesb24e7172011-01-04 15:09:30 -08001236/**
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001237 * intel_enable_pll - enable a PLL
1238 * @dev_priv: i915 private structure
1239 * @pipe: pipe PLL to enable
1240 *
1241 * Enable @pipe's PLL so we can start pumping pixels from a plane. Check to
1242 * make sure the PLL reg is writable first though, since the panel write
1243 * protect mechanism may be enabled.
1244 *
1245 * Note! This is for pre-ILK only.
1246 */
1247static void intel_enable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1248{
1249 int reg;
1250 u32 val;
1251
1252 /* No really, not for ILK+ */
1253 BUG_ON(dev_priv->info->gen >= 5);
1254
1255 /* PLL is protected by panel, make sure we can write it */
1256 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
1257 assert_panel_unlocked(dev_priv, pipe);
1258
1259 reg = DPLL(pipe);
1260 val = I915_READ(reg);
1261 val |= DPLL_VCO_ENABLE;
1262
1263 /* We do this three times for luck */
1264 I915_WRITE(reg, val);
1265 POSTING_READ(reg);
1266 udelay(150); /* wait for warmup */
1267 I915_WRITE(reg, val);
1268 POSTING_READ(reg);
1269 udelay(150); /* wait for warmup */
1270 I915_WRITE(reg, val);
1271 POSTING_READ(reg);
1272 udelay(150); /* wait for warmup */
1273}
1274
1275/**
1276 * intel_disable_pll - disable a PLL
1277 * @dev_priv: i915 private structure
1278 * @pipe: pipe PLL to disable
1279 *
1280 * Disable the PLL for @pipe, making sure the pipe is off first.
1281 *
1282 * Note! This is for pre-ILK only.
1283 */
1284static void intel_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1285{
1286 int reg;
1287 u32 val;
1288
1289 /* Don't disable pipe A or pipe A PLLs if needed */
1290 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1291 return;
1292
1293 /* Make sure the pipe isn't still relying on us */
1294 assert_pipe_disabled(dev_priv, pipe);
1295
1296 reg = DPLL(pipe);
1297 val = I915_READ(reg);
1298 val &= ~DPLL_VCO_ENABLE;
1299 I915_WRITE(reg, val);
1300 POSTING_READ(reg);
1301}
1302
1303/**
Jesse Barnes92f25842011-01-04 15:09:34 -08001304 * intel_enable_pch_pll - enable PCH PLL
1305 * @dev_priv: i915 private structure
1306 * @pipe: pipe PLL to enable
1307 *
1308 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1309 * drives the transcoder clock.
1310 */
1311static void intel_enable_pch_pll(struct drm_i915_private *dev_priv,
1312 enum pipe pipe)
1313{
1314 int reg;
1315 u32 val;
1316
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001317 if (pipe > 1)
1318 return;
1319
Jesse Barnes92f25842011-01-04 15:09:34 -08001320 /* PCH only available on ILK+ */
1321 BUG_ON(dev_priv->info->gen < 5);
1322
1323 /* PCH refclock must be enabled first */
1324 assert_pch_refclk_enabled(dev_priv);
1325
1326 reg = PCH_DPLL(pipe);
1327 val = I915_READ(reg);
1328 val |= DPLL_VCO_ENABLE;
1329 I915_WRITE(reg, val);
1330 POSTING_READ(reg);
1331 udelay(200);
1332}
1333
1334static void intel_disable_pch_pll(struct drm_i915_private *dev_priv,
1335 enum pipe pipe)
1336{
1337 int reg;
Jesse Barnes7a419862011-11-15 10:28:53 -08001338 u32 val, pll_mask = TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL,
1339 pll_sel = TRANSC_DPLL_ENABLE;
Jesse Barnes92f25842011-01-04 15:09:34 -08001340
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001341 if (pipe > 1)
1342 return;
1343
Jesse Barnes92f25842011-01-04 15:09:34 -08001344 /* PCH only available on ILK+ */
1345 BUG_ON(dev_priv->info->gen < 5);
1346
1347 /* Make sure transcoder isn't still depending on us */
1348 assert_transcoder_disabled(dev_priv, pipe);
1349
Jesse Barnes7a419862011-11-15 10:28:53 -08001350 if (pipe == 0)
1351 pll_sel |= TRANSC_DPLLA_SEL;
1352 else if (pipe == 1)
1353 pll_sel |= TRANSC_DPLLB_SEL;
1354
1355
1356 if ((I915_READ(PCH_DPLL_SEL) & pll_mask) == pll_sel)
1357 return;
1358
Jesse Barnes92f25842011-01-04 15:09:34 -08001359 reg = PCH_DPLL(pipe);
1360 val = I915_READ(reg);
1361 val &= ~DPLL_VCO_ENABLE;
1362 I915_WRITE(reg, val);
1363 POSTING_READ(reg);
1364 udelay(200);
1365}
1366
Jesse Barnes040484a2011-01-03 12:14:26 -08001367static void intel_enable_transcoder(struct drm_i915_private *dev_priv,
1368 enum pipe pipe)
1369{
1370 int reg;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001371 u32 val, pipeconf_val;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001372 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Jesse Barnes040484a2011-01-03 12:14:26 -08001373
1374 /* PCH only available on ILK+ */
1375 BUG_ON(dev_priv->info->gen < 5);
1376
1377 /* Make sure PCH DPLL is enabled */
1378 assert_pch_pll_enabled(dev_priv, pipe);
1379
1380 /* FDI must be feeding us bits for PCH ports */
1381 assert_fdi_tx_enabled(dev_priv, pipe);
1382 assert_fdi_rx_enabled(dev_priv, pipe);
1383
1384 reg = TRANSCONF(pipe);
1385 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001386 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001387
1388 if (HAS_PCH_IBX(dev_priv->dev)) {
1389 /*
1390 * make the BPC in transcoder be consistent with
1391 * that in pipeconf reg.
1392 */
1393 val &= ~PIPE_BPC_MASK;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001394 val |= pipeconf_val & PIPE_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001395 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001396
1397 val &= ~TRANS_INTERLACE_MASK;
1398 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001399 if (HAS_PCH_IBX(dev_priv->dev) &&
1400 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1401 val |= TRANS_LEGACY_INTERLACED_ILK;
1402 else
1403 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001404 else
1405 val |= TRANS_PROGRESSIVE;
1406
Jesse Barnes040484a2011-01-03 12:14:26 -08001407 I915_WRITE(reg, val | TRANS_ENABLE);
1408 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
1409 DRM_ERROR("failed to enable transcoder %d\n", pipe);
1410}
1411
1412static void intel_disable_transcoder(struct drm_i915_private *dev_priv,
1413 enum pipe pipe)
1414{
1415 int reg;
1416 u32 val;
1417
1418 /* FDI relies on the transcoder */
1419 assert_fdi_tx_disabled(dev_priv, pipe);
1420 assert_fdi_rx_disabled(dev_priv, pipe);
1421
Jesse Barnes291906f2011-02-02 12:28:03 -08001422 /* Ports must be off as well */
1423 assert_pch_ports_disabled(dev_priv, pipe);
1424
Jesse Barnes040484a2011-01-03 12:14:26 -08001425 reg = TRANSCONF(pipe);
1426 val = I915_READ(reg);
1427 val &= ~TRANS_ENABLE;
1428 I915_WRITE(reg, val);
1429 /* wait for PCH transcoder off, transcoder state */
1430 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Jesse Barnes4c9c18c2011-10-13 09:46:32 -07001431 DRM_ERROR("failed to disable transcoder %d\n", pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001432}
1433
Jesse Barnes92f25842011-01-04 15:09:34 -08001434/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001435 * intel_enable_pipe - enable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001436 * @dev_priv: i915 private structure
1437 * @pipe: pipe to enable
Jesse Barnes040484a2011-01-03 12:14:26 -08001438 * @pch_port: on ILK+, is this pipe driving a PCH port or not
Jesse Barnesb24e7172011-01-04 15:09:30 -08001439 *
1440 * Enable @pipe, making sure that various hardware specific requirements
1441 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1442 *
1443 * @pipe should be %PIPE_A or %PIPE_B.
1444 *
1445 * Will wait until the pipe is actually running (i.e. first vblank) before
1446 * returning.
1447 */
Jesse Barnes040484a2011-01-03 12:14:26 -08001448static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
1449 bool pch_port)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001450{
1451 int reg;
1452 u32 val;
1453
1454 /*
1455 * A pipe without a PLL won't actually be able to drive bits from
1456 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1457 * need the check.
1458 */
1459 if (!HAS_PCH_SPLIT(dev_priv->dev))
1460 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001461 else {
1462 if (pch_port) {
1463 /* if driving the PCH, we need FDI enabled */
1464 assert_fdi_rx_pll_enabled(dev_priv, pipe);
1465 assert_fdi_tx_pll_enabled(dev_priv, pipe);
1466 }
1467 /* FIXME: assert CPU port conditions for SNB+ */
1468 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001469
1470 reg = PIPECONF(pipe);
1471 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001472 if (val & PIPECONF_ENABLE)
1473 return;
1474
1475 I915_WRITE(reg, val | PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001476 intel_wait_for_vblank(dev_priv->dev, pipe);
1477}
1478
1479/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001480 * intel_disable_pipe - disable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001481 * @dev_priv: i915 private structure
1482 * @pipe: pipe to disable
1483 *
1484 * Disable @pipe, making sure that various hardware specific requirements
1485 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1486 *
1487 * @pipe should be %PIPE_A or %PIPE_B.
1488 *
1489 * Will wait until the pipe has shut down before returning.
1490 */
1491static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1492 enum pipe pipe)
1493{
1494 int reg;
1495 u32 val;
1496
1497 /*
1498 * Make sure planes won't keep trying to pump pixels to us,
1499 * or we might hang the display.
1500 */
1501 assert_planes_disabled(dev_priv, pipe);
1502
1503 /* Don't disable pipe A or pipe A PLLs if needed */
1504 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1505 return;
1506
1507 reg = PIPECONF(pipe);
1508 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001509 if ((val & PIPECONF_ENABLE) == 0)
1510 return;
1511
1512 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001513 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1514}
1515
Keith Packardd74362c2011-07-28 14:47:14 -07001516/*
1517 * Plane regs are double buffered, going from enabled->disabled needs a
1518 * trigger in order to latch. The display address reg provides this.
1519 */
1520static void intel_flush_display_plane(struct drm_i915_private *dev_priv,
1521 enum plane plane)
1522{
1523 I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
1524 I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
1525}
1526
Jesse Barnesb24e7172011-01-04 15:09:30 -08001527/**
1528 * intel_enable_plane - enable a display plane on a given pipe
1529 * @dev_priv: i915 private structure
1530 * @plane: plane to enable
1531 * @pipe: pipe being fed
1532 *
1533 * Enable @plane on @pipe, making sure that @pipe is running first.
1534 */
1535static void intel_enable_plane(struct drm_i915_private *dev_priv,
1536 enum plane plane, enum pipe pipe)
1537{
1538 int reg;
1539 u32 val;
1540
1541 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1542 assert_pipe_enabled(dev_priv, pipe);
1543
1544 reg = DSPCNTR(plane);
1545 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001546 if (val & DISPLAY_PLANE_ENABLE)
1547 return;
1548
1549 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
Keith Packardd74362c2011-07-28 14:47:14 -07001550 intel_flush_display_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001551 intel_wait_for_vblank(dev_priv->dev, pipe);
1552}
1553
Jesse Barnesb24e7172011-01-04 15:09:30 -08001554/**
1555 * intel_disable_plane - disable a display plane
1556 * @dev_priv: i915 private structure
1557 * @plane: plane to disable
1558 * @pipe: pipe consuming the data
1559 *
1560 * Disable @plane; should be an independent operation.
1561 */
1562static void intel_disable_plane(struct drm_i915_private *dev_priv,
1563 enum plane plane, enum pipe pipe)
1564{
1565 int reg;
1566 u32 val;
1567
1568 reg = DSPCNTR(plane);
1569 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001570 if ((val & DISPLAY_PLANE_ENABLE) == 0)
1571 return;
1572
1573 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001574 intel_flush_display_plane(dev_priv, plane);
1575 intel_wait_for_vblank(dev_priv->dev, pipe);
1576}
1577
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001578static void disable_pch_dp(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001579 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001580{
1581 u32 val = I915_READ(reg);
Keith Packard4e634382011-08-06 10:39:45 -07001582 if (dp_pipe_enabled(dev_priv, pipe, port_sel, val)) {
Keith Packardf0575e92011-07-25 22:12:43 -07001583 DRM_DEBUG_KMS("Disabling pch dp %x on pipe %d\n", reg, pipe);
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001584 I915_WRITE(reg, val & ~DP_PORT_EN);
Keith Packardf0575e92011-07-25 22:12:43 -07001585 }
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001586}
1587
1588static void disable_pch_hdmi(struct drm_i915_private *dev_priv,
1589 enum pipe pipe, int reg)
1590{
1591 u32 val = I915_READ(reg);
Keith Packard1519b992011-08-06 10:35:34 -07001592 if (hdmi_pipe_enabled(dev_priv, val, pipe)) {
Keith Packardf0575e92011-07-25 22:12:43 -07001593 DRM_DEBUG_KMS("Disabling pch HDMI %x on pipe %d\n",
1594 reg, pipe);
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001595 I915_WRITE(reg, val & ~PORT_ENABLE);
Keith Packardf0575e92011-07-25 22:12:43 -07001596 }
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001597}
1598
1599/* Disable any ports connected to this transcoder */
1600static void intel_disable_pch_ports(struct drm_i915_private *dev_priv,
1601 enum pipe pipe)
1602{
1603 u32 reg, val;
1604
1605 val = I915_READ(PCH_PP_CONTROL);
1606 I915_WRITE(PCH_PP_CONTROL, val | PANEL_UNLOCK_REGS);
1607
Keith Packardf0575e92011-07-25 22:12:43 -07001608 disable_pch_dp(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1609 disable_pch_dp(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1610 disable_pch_dp(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001611
1612 reg = PCH_ADPA;
1613 val = I915_READ(reg);
Keith Packard1519b992011-08-06 10:35:34 -07001614 if (adpa_pipe_enabled(dev_priv, val, pipe))
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001615 I915_WRITE(reg, val & ~ADPA_DAC_ENABLE);
1616
1617 reg = PCH_LVDS;
1618 val = I915_READ(reg);
Keith Packard1519b992011-08-06 10:35:34 -07001619 if (lvds_pipe_enabled(dev_priv, val, pipe)) {
1620 DRM_DEBUG_KMS("disable lvds on pipe %d val 0x%08x\n", pipe, val);
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001621 I915_WRITE(reg, val & ~LVDS_PORT_EN);
1622 POSTING_READ(reg);
1623 udelay(100);
1624 }
1625
1626 disable_pch_hdmi(dev_priv, pipe, HDMIB);
1627 disable_pch_hdmi(dev_priv, pipe, HDMIC);
1628 disable_pch_hdmi(dev_priv, pipe, HDMID);
1629}
1630
Chris Wilson43a95392011-07-08 12:22:36 +01001631static void i8xx_disable_fbc(struct drm_device *dev)
1632{
1633 struct drm_i915_private *dev_priv = dev->dev_private;
1634 u32 fbc_ctl;
1635
1636 /* Disable compression */
1637 fbc_ctl = I915_READ(FBC_CONTROL);
1638 if ((fbc_ctl & FBC_CTL_EN) == 0)
1639 return;
1640
1641 fbc_ctl &= ~FBC_CTL_EN;
1642 I915_WRITE(FBC_CONTROL, fbc_ctl);
1643
1644 /* Wait for compressing bit to clear */
1645 if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
1646 DRM_DEBUG_KMS("FBC idle timed out\n");
1647 return;
1648 }
1649
1650 DRM_DEBUG_KMS("disabled FBC\n");
1651}
1652
Jesse Barnes80824002009-09-10 15:28:06 -07001653static void i8xx_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
1654{
1655 struct drm_device *dev = crtc->dev;
1656 struct drm_i915_private *dev_priv = dev->dev_private;
1657 struct drm_framebuffer *fb = crtc->fb;
1658 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00001659 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes80824002009-09-10 15:28:06 -07001660 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson016b9b62011-07-08 12:22:43 +01001661 int cfb_pitch;
Jesse Barnes80824002009-09-10 15:28:06 -07001662 int plane, i;
1663 u32 fbc_ctl, fbc_ctl2;
1664
Chris Wilson016b9b62011-07-08 12:22:43 +01001665 cfb_pitch = dev_priv->cfb_size / FBC_LL_SIZE;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001666 if (fb->pitches[0] < cfb_pitch)
1667 cfb_pitch = fb->pitches[0];
Jesse Barnes80824002009-09-10 15:28:06 -07001668
1669 /* FBC_CTL wants 64B units */
Chris Wilson016b9b62011-07-08 12:22:43 +01001670 cfb_pitch = (cfb_pitch / 64) - 1;
1671 plane = intel_crtc->plane == 0 ? FBC_CTL_PLANEA : FBC_CTL_PLANEB;
Jesse Barnes80824002009-09-10 15:28:06 -07001672
1673 /* Clear old tags */
1674 for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
1675 I915_WRITE(FBC_TAG + (i * 4), 0);
1676
1677 /* Set it up... */
Chris Wilsonde568512011-07-08 12:22:39 +01001678 fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
1679 fbc_ctl2 |= plane;
Jesse Barnes80824002009-09-10 15:28:06 -07001680 I915_WRITE(FBC_CONTROL2, fbc_ctl2);
1681 I915_WRITE(FBC_FENCE_OFF, crtc->y);
1682
1683 /* enable it... */
1684 fbc_ctl = FBC_CTL_EN | FBC_CTL_PERIODIC;
Jesse Barnesee25df22010-02-06 10:41:53 -08001685 if (IS_I945GM(dev))
Priit Laes49677902010-03-02 11:37:00 +02001686 fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
Chris Wilson016b9b62011-07-08 12:22:43 +01001687 fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
Jesse Barnes80824002009-09-10 15:28:06 -07001688 fbc_ctl |= (interval & 0x2fff) << FBC_CTL_INTERVAL_SHIFT;
Chris Wilson016b9b62011-07-08 12:22:43 +01001689 fbc_ctl |= obj->fence_reg;
Jesse Barnes80824002009-09-10 15:28:06 -07001690 I915_WRITE(FBC_CONTROL, fbc_ctl);
1691
Chris Wilson016b9b62011-07-08 12:22:43 +01001692 DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %d, ",
1693 cfb_pitch, crtc->y, intel_crtc->plane);
Jesse Barnes80824002009-09-10 15:28:06 -07001694}
1695
Adam Jacksonee5382a2010-04-23 11:17:39 -04001696static bool i8xx_fbc_enabled(struct drm_device *dev)
Jesse Barnes80824002009-09-10 15:28:06 -07001697{
Jesse Barnes80824002009-09-10 15:28:06 -07001698 struct drm_i915_private *dev_priv = dev->dev_private;
1699
1700 return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
1701}
1702
Jesse Barnes74dff282009-09-14 15:39:40 -07001703static void g4x_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
1704{
1705 struct drm_device *dev = crtc->dev;
1706 struct drm_i915_private *dev_priv = dev->dev_private;
1707 struct drm_framebuffer *fb = crtc->fb;
1708 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00001709 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes74dff282009-09-14 15:39:40 -07001710 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5eddb702010-09-11 13:48:45 +01001711 int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
Jesse Barnes74dff282009-09-14 15:39:40 -07001712 unsigned long stall_watermark = 200;
1713 u32 dpfc_ctl;
1714
Jesse Barnes74dff282009-09-14 15:39:40 -07001715 dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;
Chris Wilson016b9b62011-07-08 12:22:43 +01001716 dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
Chris Wilsonde568512011-07-08 12:22:39 +01001717 I915_WRITE(DPFC_CHICKEN, DPFC_HT_MODIFY);
Jesse Barnes74dff282009-09-14 15:39:40 -07001718
Jesse Barnes74dff282009-09-14 15:39:40 -07001719 I915_WRITE(DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
1720 (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
1721 (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
1722 I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
1723
1724 /* enable it... */
1725 I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);
1726
Zhao Yakui28c97732009-10-09 11:39:41 +08001727 DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
Jesse Barnes74dff282009-09-14 15:39:40 -07001728}
1729
Chris Wilson43a95392011-07-08 12:22:36 +01001730static void g4x_disable_fbc(struct drm_device *dev)
Jesse Barnes74dff282009-09-14 15:39:40 -07001731{
1732 struct drm_i915_private *dev_priv = dev->dev_private;
1733 u32 dpfc_ctl;
1734
1735 /* Disable compression */
1736 dpfc_ctl = I915_READ(DPFC_CONTROL);
Chris Wilsonbed4a672010-09-11 10:47:47 +01001737 if (dpfc_ctl & DPFC_CTL_EN) {
1738 dpfc_ctl &= ~DPFC_CTL_EN;
1739 I915_WRITE(DPFC_CONTROL, dpfc_ctl);
Jesse Barnes74dff282009-09-14 15:39:40 -07001740
Chris Wilsonbed4a672010-09-11 10:47:47 +01001741 DRM_DEBUG_KMS("disabled FBC\n");
1742 }
Jesse Barnes74dff282009-09-14 15:39:40 -07001743}
1744
Adam Jacksonee5382a2010-04-23 11:17:39 -04001745static bool g4x_fbc_enabled(struct drm_device *dev)
Jesse Barnes74dff282009-09-14 15:39:40 -07001746{
Jesse Barnes74dff282009-09-14 15:39:40 -07001747 struct drm_i915_private *dev_priv = dev->dev_private;
1748
1749 return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
1750}
1751
Jesse Barnes4efe0702011-01-18 11:25:41 -08001752static void sandybridge_blit_fbc_update(struct drm_device *dev)
1753{
1754 struct drm_i915_private *dev_priv = dev->dev_private;
1755 u32 blt_ecoskpd;
1756
1757 /* Make sure blitter notifies FBC of writes */
Ben Widawskyfcca7922011-04-25 11:23:07 -07001758 gen6_gt_force_wake_get(dev_priv);
Jesse Barnes4efe0702011-01-18 11:25:41 -08001759 blt_ecoskpd = I915_READ(GEN6_BLITTER_ECOSKPD);
1760 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY <<
1761 GEN6_BLITTER_LOCK_SHIFT;
1762 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
1763 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY;
1764 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
1765 blt_ecoskpd &= ~(GEN6_BLITTER_FBC_NOTIFY <<
1766 GEN6_BLITTER_LOCK_SHIFT);
1767 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
1768 POSTING_READ(GEN6_BLITTER_ECOSKPD);
Ben Widawskyfcca7922011-04-25 11:23:07 -07001769 gen6_gt_force_wake_put(dev_priv);
Jesse Barnes4efe0702011-01-18 11:25:41 -08001770}
1771
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001772static void ironlake_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
1773{
1774 struct drm_device *dev = crtc->dev;
1775 struct drm_i915_private *dev_priv = dev->dev_private;
1776 struct drm_framebuffer *fb = crtc->fb;
1777 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00001778 struct drm_i915_gem_object *obj = intel_fb->obj;
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001779 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5eddb702010-09-11 13:48:45 +01001780 int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001781 unsigned long stall_watermark = 200;
1782 u32 dpfc_ctl;
1783
Chris Wilsonbed4a672010-09-11 10:47:47 +01001784 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001785 dpfc_ctl &= DPFC_RESERVED;
1786 dpfc_ctl |= (plane | DPFC_CTL_LIMIT_1X);
Chris Wilson9ce9d062011-07-08 12:22:40 +01001787 /* Set persistent mode for front-buffer rendering, ala X. */
1788 dpfc_ctl |= DPFC_CTL_PERSISTENT_MODE;
Chris Wilson016b9b62011-07-08 12:22:43 +01001789 dpfc_ctl |= (DPFC_CTL_FENCE_EN | obj->fence_reg);
Chris Wilsonde568512011-07-08 12:22:39 +01001790 I915_WRITE(ILK_DPFC_CHICKEN, DPFC_HT_MODIFY);
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001791
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001792 I915_WRITE(ILK_DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
1793 (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
1794 (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
1795 I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
Chris Wilson05394f32010-11-08 19:18:58 +00001796 I915_WRITE(ILK_FBC_RT_BASE, obj->gtt_offset | ILK_FBC_RT_VALID);
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001797 /* enable it... */
Chris Wilsonbed4a672010-09-11 10:47:47 +01001798 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001799
Yuanhan Liu9c04f012010-12-15 15:42:32 +08001800 if (IS_GEN6(dev)) {
1801 I915_WRITE(SNB_DPFC_CTL_SA,
Chris Wilson016b9b62011-07-08 12:22:43 +01001802 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
Yuanhan Liu9c04f012010-12-15 15:42:32 +08001803 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
Jesse Barnes4efe0702011-01-18 11:25:41 -08001804 sandybridge_blit_fbc_update(dev);
Yuanhan Liu9c04f012010-12-15 15:42:32 +08001805 }
1806
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001807 DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
1808}
1809
Chris Wilson43a95392011-07-08 12:22:36 +01001810static void ironlake_disable_fbc(struct drm_device *dev)
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001811{
1812 struct drm_i915_private *dev_priv = dev->dev_private;
1813 u32 dpfc_ctl;
1814
1815 /* Disable compression */
1816 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
Chris Wilsonbed4a672010-09-11 10:47:47 +01001817 if (dpfc_ctl & DPFC_CTL_EN) {
1818 dpfc_ctl &= ~DPFC_CTL_EN;
1819 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001820
Chris Wilsonbed4a672010-09-11 10:47:47 +01001821 DRM_DEBUG_KMS("disabled FBC\n");
1822 }
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001823}
1824
1825static bool ironlake_fbc_enabled(struct drm_device *dev)
1826{
1827 struct drm_i915_private *dev_priv = dev->dev_private;
1828
1829 return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
1830}
1831
Adam Jacksonee5382a2010-04-23 11:17:39 -04001832bool intel_fbc_enabled(struct drm_device *dev)
1833{
1834 struct drm_i915_private *dev_priv = dev->dev_private;
1835
1836 if (!dev_priv->display.fbc_enabled)
1837 return false;
1838
1839 return dev_priv->display.fbc_enabled(dev);
1840}
1841
Chris Wilson1630fe72011-07-08 12:22:42 +01001842static void intel_fbc_work_fn(struct work_struct *__work)
1843{
1844 struct intel_fbc_work *work =
1845 container_of(to_delayed_work(__work),
1846 struct intel_fbc_work, work);
1847 struct drm_device *dev = work->crtc->dev;
1848 struct drm_i915_private *dev_priv = dev->dev_private;
1849
1850 mutex_lock(&dev->struct_mutex);
1851 if (work == dev_priv->fbc_work) {
1852 /* Double check that we haven't switched fb without cancelling
1853 * the prior work.
1854 */
Chris Wilson016b9b62011-07-08 12:22:43 +01001855 if (work->crtc->fb == work->fb) {
Chris Wilson1630fe72011-07-08 12:22:42 +01001856 dev_priv->display.enable_fbc(work->crtc,
1857 work->interval);
1858
Chris Wilson016b9b62011-07-08 12:22:43 +01001859 dev_priv->cfb_plane = to_intel_crtc(work->crtc)->plane;
1860 dev_priv->cfb_fb = work->crtc->fb->base.id;
1861 dev_priv->cfb_y = work->crtc->y;
1862 }
1863
Chris Wilson1630fe72011-07-08 12:22:42 +01001864 dev_priv->fbc_work = NULL;
1865 }
1866 mutex_unlock(&dev->struct_mutex);
1867
1868 kfree(work);
1869}
1870
1871static void intel_cancel_fbc_work(struct drm_i915_private *dev_priv)
1872{
1873 if (dev_priv->fbc_work == NULL)
1874 return;
1875
1876 DRM_DEBUG_KMS("cancelling pending FBC enable\n");
1877
1878 /* Synchronisation is provided by struct_mutex and checking of
1879 * dev_priv->fbc_work, so we can perform the cancellation
1880 * entirely asynchronously.
1881 */
1882 if (cancel_delayed_work(&dev_priv->fbc_work->work))
1883 /* tasklet was killed before being run, clean up */
1884 kfree(dev_priv->fbc_work);
1885
1886 /* Mark the work as no longer wanted so that if it does
1887 * wake-up (because the work was already running and waiting
1888 * for our mutex), it will discover that is no longer
1889 * necessary to run.
1890 */
1891 dev_priv->fbc_work = NULL;
1892}
1893
Chris Wilson43a95392011-07-08 12:22:36 +01001894static void intel_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
Adam Jacksonee5382a2010-04-23 11:17:39 -04001895{
Chris Wilson1630fe72011-07-08 12:22:42 +01001896 struct intel_fbc_work *work;
1897 struct drm_device *dev = crtc->dev;
1898 struct drm_i915_private *dev_priv = dev->dev_private;
Adam Jacksonee5382a2010-04-23 11:17:39 -04001899
1900 if (!dev_priv->display.enable_fbc)
1901 return;
1902
Chris Wilson1630fe72011-07-08 12:22:42 +01001903 intel_cancel_fbc_work(dev_priv);
1904
1905 work = kzalloc(sizeof *work, GFP_KERNEL);
1906 if (work == NULL) {
1907 dev_priv->display.enable_fbc(crtc, interval);
1908 return;
1909 }
1910
1911 work->crtc = crtc;
1912 work->fb = crtc->fb;
1913 work->interval = interval;
1914 INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
1915
1916 dev_priv->fbc_work = work;
1917
1918 DRM_DEBUG_KMS("scheduling delayed FBC enable\n");
1919
1920 /* Delay the actual enabling to let pageflipping cease and the
Chris Wilson016b9b62011-07-08 12:22:43 +01001921 * display to settle before starting the compression. Note that
1922 * this delay also serves a second purpose: it allows for a
1923 * vblank to pass after disabling the FBC before we attempt
1924 * to modify the control registers.
Chris Wilson1630fe72011-07-08 12:22:42 +01001925 *
1926 * A more complicated solution would involve tracking vblanks
1927 * following the termination of the page-flipping sequence
1928 * and indeed performing the enable as a co-routine and not
1929 * waiting synchronously upon the vblank.
1930 */
1931 schedule_delayed_work(&work->work, msecs_to_jiffies(50));
Adam Jacksonee5382a2010-04-23 11:17:39 -04001932}
1933
1934void intel_disable_fbc(struct drm_device *dev)
1935{
1936 struct drm_i915_private *dev_priv = dev->dev_private;
1937
Chris Wilson1630fe72011-07-08 12:22:42 +01001938 intel_cancel_fbc_work(dev_priv);
1939
Adam Jacksonee5382a2010-04-23 11:17:39 -04001940 if (!dev_priv->display.disable_fbc)
1941 return;
1942
1943 dev_priv->display.disable_fbc(dev);
Chris Wilson016b9b62011-07-08 12:22:43 +01001944 dev_priv->cfb_plane = -1;
Adam Jacksonee5382a2010-04-23 11:17:39 -04001945}
1946
Jesse Barnes80824002009-09-10 15:28:06 -07001947/**
1948 * intel_update_fbc - enable/disable FBC as needed
Chris Wilsonbed4a672010-09-11 10:47:47 +01001949 * @dev: the drm_device
Jesse Barnes80824002009-09-10 15:28:06 -07001950 *
1951 * Set up the framebuffer compression hardware at mode set time. We
1952 * enable it if possible:
1953 * - plane A only (on pre-965)
1954 * - no pixel mulitply/line duplication
1955 * - no alpha buffer discard
1956 * - no dual wide
1957 * - framebuffer <= 2048 in width, 1536 in height
1958 *
1959 * We can't assume that any compression will take place (worst case),
1960 * so the compressed buffer has to be the same size as the uncompressed
1961 * one. It also must reside (along with the line length buffer) in
1962 * stolen memory.
1963 *
1964 * We need to enable/disable FBC on a global basis.
1965 */
Chris Wilsonbed4a672010-09-11 10:47:47 +01001966static void intel_update_fbc(struct drm_device *dev)
Jesse Barnes80824002009-09-10 15:28:06 -07001967{
Jesse Barnes80824002009-09-10 15:28:06 -07001968 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonbed4a672010-09-11 10:47:47 +01001969 struct drm_crtc *crtc = NULL, *tmp_crtc;
1970 struct intel_crtc *intel_crtc;
1971 struct drm_framebuffer *fb;
Jesse Barnes80824002009-09-10 15:28:06 -07001972 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00001973 struct drm_i915_gem_object *obj;
Keith Packardcd0de032011-09-19 21:34:19 -07001974 int enable_fbc;
Jesse Barnes9c928d12010-07-23 15:20:00 -07001975
1976 DRM_DEBUG_KMS("\n");
Jesse Barnes80824002009-09-10 15:28:06 -07001977
1978 if (!i915_powersave)
1979 return;
1980
Adam Jacksonee5382a2010-04-23 11:17:39 -04001981 if (!I915_HAS_FBC(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07001982 return;
1983
Jesse Barnes80824002009-09-10 15:28:06 -07001984 /*
1985 * If FBC is already on, we just have to verify that we can
1986 * keep it that way...
1987 * Need to disable if:
Jesse Barnes9c928d12010-07-23 15:20:00 -07001988 * - more than one pipe is active
Jesse Barnes80824002009-09-10 15:28:06 -07001989 * - changing FBC params (stride, fence, mode)
1990 * - new fb is too large to fit in compressed buffer
1991 * - going to an unsupported config (interlace, pixel multiply, etc.)
1992 */
Jesse Barnes9c928d12010-07-23 15:20:00 -07001993 list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head) {
Chris Wilsond2102462011-01-24 17:43:27 +00001994 if (tmp_crtc->enabled && tmp_crtc->fb) {
Chris Wilsonbed4a672010-09-11 10:47:47 +01001995 if (crtc) {
1996 DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
1997 dev_priv->no_fbc_reason = FBC_MULTIPLE_PIPES;
1998 goto out_disable;
1999 }
2000 crtc = tmp_crtc;
2001 }
Jesse Barnes9c928d12010-07-23 15:20:00 -07002002 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01002003
2004 if (!crtc || crtc->fb == NULL) {
2005 DRM_DEBUG_KMS("no output, disabling\n");
2006 dev_priv->no_fbc_reason = FBC_NO_OUTPUT;
Jesse Barnes9c928d12010-07-23 15:20:00 -07002007 goto out_disable;
2008 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01002009
2010 intel_crtc = to_intel_crtc(crtc);
2011 fb = crtc->fb;
2012 intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00002013 obj = intel_fb->obj;
Chris Wilsonbed4a672010-09-11 10:47:47 +01002014
Keith Packardcd0de032011-09-19 21:34:19 -07002015 enable_fbc = i915_enable_fbc;
2016 if (enable_fbc < 0) {
2017 DRM_DEBUG_KMS("fbc set to per-chip default\n");
2018 enable_fbc = 1;
Chris Wilsond56d8b22011-11-08 23:17:34 +00002019 if (INTEL_INFO(dev)->gen <= 6)
Keith Packardcd0de032011-09-19 21:34:19 -07002020 enable_fbc = 0;
2021 }
2022 if (!enable_fbc) {
2023 DRM_DEBUG_KMS("fbc disabled per module param\n");
Jesse Barnesc1a9f042011-05-05 15:24:21 -07002024 dev_priv->no_fbc_reason = FBC_MODULE_PARAM;
2025 goto out_disable;
2026 }
Chris Wilson05394f32010-11-08 19:18:58 +00002027 if (intel_fb->obj->base.size > dev_priv->cfb_size) {
Zhao Yakui28c97732009-10-09 11:39:41 +08002028 DRM_DEBUG_KMS("framebuffer too large, disabling "
Chris Wilson5eddb702010-09-11 13:48:45 +01002029 "compression\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08002030 dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
Jesse Barnes80824002009-09-10 15:28:06 -07002031 goto out_disable;
2032 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01002033 if ((crtc->mode.flags & DRM_MODE_FLAG_INTERLACE) ||
2034 (crtc->mode.flags & DRM_MODE_FLAG_DBLSCAN)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08002035 DRM_DEBUG_KMS("mode incompatible with compression, "
Chris Wilson5eddb702010-09-11 13:48:45 +01002036 "disabling\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08002037 dev_priv->no_fbc_reason = FBC_UNSUPPORTED_MODE;
Jesse Barnes80824002009-09-10 15:28:06 -07002038 goto out_disable;
2039 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01002040 if ((crtc->mode.hdisplay > 2048) ||
2041 (crtc->mode.vdisplay > 1536)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08002042 DRM_DEBUG_KMS("mode too large for compression, disabling\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08002043 dev_priv->no_fbc_reason = FBC_MODE_TOO_LARGE;
Jesse Barnes80824002009-09-10 15:28:06 -07002044 goto out_disable;
2045 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01002046 if ((IS_I915GM(dev) || IS_I945GM(dev)) && intel_crtc->plane != 0) {
Zhao Yakui28c97732009-10-09 11:39:41 +08002047 DRM_DEBUG_KMS("plane not 0, disabling compression\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08002048 dev_priv->no_fbc_reason = FBC_BAD_PLANE;
Jesse Barnes80824002009-09-10 15:28:06 -07002049 goto out_disable;
2050 }
Chris Wilsonde568512011-07-08 12:22:39 +01002051
2052 /* The use of a CPU fence is mandatory in order to detect writes
2053 * by the CPU to the scanout and trigger updates to the FBC.
2054 */
2055 if (obj->tiling_mode != I915_TILING_X ||
2056 obj->fence_reg == I915_FENCE_REG_NONE) {
2057 DRM_DEBUG_KMS("framebuffer not tiled or fenced, disabling compression\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08002058 dev_priv->no_fbc_reason = FBC_NOT_TILED;
Jesse Barnes80824002009-09-10 15:28:06 -07002059 goto out_disable;
2060 }
2061
Jason Wesselc924b932010-08-05 09:22:32 -05002062 /* If the kernel debugger is active, always disable compression */
2063 if (in_dbg_master())
2064 goto out_disable;
2065
Chris Wilson016b9b62011-07-08 12:22:43 +01002066 /* If the scanout has not changed, don't modify the FBC settings.
2067 * Note that we make the fundamental assumption that the fb->obj
2068 * cannot be unpinned (and have its GTT offset and fence revoked)
2069 * without first being decoupled from the scanout and FBC disabled.
2070 */
2071 if (dev_priv->cfb_plane == intel_crtc->plane &&
2072 dev_priv->cfb_fb == fb->base.id &&
2073 dev_priv->cfb_y == crtc->y)
2074 return;
2075
2076 if (intel_fbc_enabled(dev)) {
2077 /* We update FBC along two paths, after changing fb/crtc
2078 * configuration (modeswitching) and after page-flipping
2079 * finishes. For the latter, we know that not only did
2080 * we disable the FBC at the start of the page-flip
2081 * sequence, but also more than one vblank has passed.
2082 *
2083 * For the former case of modeswitching, it is possible
2084 * to switch between two FBC valid configurations
2085 * instantaneously so we do need to disable the FBC
2086 * before we can modify its control registers. We also
2087 * have to wait for the next vblank for that to take
2088 * effect. However, since we delay enabling FBC we can
2089 * assume that a vblank has passed since disabling and
2090 * that we can safely alter the registers in the deferred
2091 * callback.
2092 *
2093 * In the scenario that we go from a valid to invalid
2094 * and then back to valid FBC configuration we have
2095 * no strict enforcement that a vblank occurred since
2096 * disabling the FBC. However, along all current pipe
2097 * disabling paths we do need to wait for a vblank at
2098 * some point. And we wait before enabling FBC anyway.
2099 */
2100 DRM_DEBUG_KMS("disabling active FBC for update\n");
2101 intel_disable_fbc(dev);
2102 }
2103
Chris Wilsonbed4a672010-09-11 10:47:47 +01002104 intel_enable_fbc(crtc, 500);
Jesse Barnes80824002009-09-10 15:28:06 -07002105 return;
2106
2107out_disable:
Jesse Barnes80824002009-09-10 15:28:06 -07002108 /* Multiple disables should be harmless */
Chris Wilsona9394062010-05-27 13:18:16 +01002109 if (intel_fbc_enabled(dev)) {
2110 DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
Adam Jacksonee5382a2010-04-23 11:17:39 -04002111 intel_disable_fbc(dev);
Chris Wilsona9394062010-05-27 13:18:16 +01002112 }
Jesse Barnes80824002009-09-10 15:28:06 -07002113}
2114
Chris Wilson127bd2a2010-07-23 23:32:05 +01002115int
Chris Wilson48b956c2010-09-14 12:50:34 +01002116intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00002117 struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002118 struct intel_ring_buffer *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002119{
Chris Wilsonce453d82011-02-21 14:43:56 +00002120 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002121 u32 alignment;
2122 int ret;
2123
Chris Wilson05394f32010-11-08 19:18:58 +00002124 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002125 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01002126 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
2127 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002128 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01002129 alignment = 4 * 1024;
2130 else
2131 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002132 break;
2133 case I915_TILING_X:
2134 /* pin() will align the object as required by fence */
2135 alignment = 0;
2136 break;
2137 case I915_TILING_Y:
2138 /* FIXME: Is this true? */
2139 DRM_ERROR("Y tiled not allowed for scan out buffers\n");
2140 return -EINVAL;
2141 default:
2142 BUG();
2143 }
2144
Chris Wilsonce453d82011-02-21 14:43:56 +00002145 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01002146 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01002147 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002148 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002149
2150 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2151 * fence, whereas 965+ only requires a fence if using
2152 * framebuffer compression. For simplicity, we always install
2153 * a fence as the cost is not that onerous.
2154 */
Chris Wilson05394f32010-11-08 19:18:58 +00002155 if (obj->tiling_mode != I915_TILING_NONE) {
Chris Wilsonce453d82011-02-21 14:43:56 +00002156 ret = i915_gem_object_get_fence(obj, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01002157 if (ret)
2158 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002159
2160 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002161 }
2162
Chris Wilsonce453d82011-02-21 14:43:56 +00002163 dev_priv->mm.interruptible = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002164 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002165
2166err_unpin:
2167 i915_gem_object_unpin(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00002168err_interruptible:
2169 dev_priv->mm.interruptible = true;
Chris Wilson48b956c2010-09-14 12:50:34 +01002170 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002171}
2172
Chris Wilson1690e1e2011-12-14 13:57:08 +01002173void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
2174{
2175 i915_gem_object_unpin_fence(obj);
2176 i915_gem_object_unpin(obj);
2177}
2178
Jesse Barnes17638cd2011-06-24 12:19:23 -07002179static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2180 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002181{
2182 struct drm_device *dev = crtc->dev;
2183 struct drm_i915_private *dev_priv = dev->dev_private;
2184 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2185 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00002186 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002187 int plane = intel_crtc->plane;
2188 unsigned long Start, Offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002189 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01002190 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07002191
2192 switch (plane) {
2193 case 0:
2194 case 1:
2195 break;
2196 default:
2197 DRM_ERROR("Can't update plane %d in SAREA\n", plane);
2198 return -EINVAL;
2199 }
2200
2201 intel_fb = to_intel_framebuffer(fb);
2202 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002203
Chris Wilson5eddb702010-09-11 13:48:45 +01002204 reg = DSPCNTR(plane);
2205 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002206 /* Mask out pixel format bits in case we change it */
2207 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
2208 switch (fb->bits_per_pixel) {
2209 case 8:
2210 dspcntr |= DISPPLANE_8BPP;
2211 break;
2212 case 16:
2213 if (fb->depth == 15)
2214 dspcntr |= DISPPLANE_15_16BPP;
2215 else
2216 dspcntr |= DISPPLANE_16BPP;
2217 break;
2218 case 24:
2219 case 32:
2220 dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
2221 break;
2222 default:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002223 DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
Jesse Barnes81255562010-08-02 12:07:50 -07002224 return -EINVAL;
2225 }
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002226 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00002227 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07002228 dspcntr |= DISPPLANE_TILED;
2229 else
2230 dspcntr &= ~DISPPLANE_TILED;
2231 }
2232
Chris Wilson5eddb702010-09-11 13:48:45 +01002233 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07002234
Chris Wilson05394f32010-11-08 19:18:58 +00002235 Start = obj->gtt_offset;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002236 Offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes81255562010-08-02 12:07:50 -07002237
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002238 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002239 Start, Offset, x, y, fb->pitches[0]);
2240 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002241 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002242 I915_WRITE(DSPSURF(plane), Start);
2243 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2244 I915_WRITE(DSPADDR(plane), Offset);
2245 } else
2246 I915_WRITE(DSPADDR(plane), Start + Offset);
2247 POSTING_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002248
Jesse Barnes17638cd2011-06-24 12:19:23 -07002249 return 0;
2250}
2251
2252static int ironlake_update_plane(struct drm_crtc *crtc,
2253 struct drm_framebuffer *fb, int x, int y)
2254{
2255 struct drm_device *dev = crtc->dev;
2256 struct drm_i915_private *dev_priv = dev->dev_private;
2257 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2258 struct intel_framebuffer *intel_fb;
2259 struct drm_i915_gem_object *obj;
2260 int plane = intel_crtc->plane;
2261 unsigned long Start, Offset;
2262 u32 dspcntr;
2263 u32 reg;
2264
2265 switch (plane) {
2266 case 0:
2267 case 1:
Jesse Barnes27f82272011-09-02 12:54:37 -07002268 case 2:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002269 break;
2270 default:
2271 DRM_ERROR("Can't update plane %d in SAREA\n", plane);
2272 return -EINVAL;
2273 }
2274
2275 intel_fb = to_intel_framebuffer(fb);
2276 obj = intel_fb->obj;
2277
2278 reg = DSPCNTR(plane);
2279 dspcntr = I915_READ(reg);
2280 /* Mask out pixel format bits in case we change it */
2281 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
2282 switch (fb->bits_per_pixel) {
2283 case 8:
2284 dspcntr |= DISPPLANE_8BPP;
2285 break;
2286 case 16:
2287 if (fb->depth != 16)
2288 return -EINVAL;
2289
2290 dspcntr |= DISPPLANE_16BPP;
2291 break;
2292 case 24:
2293 case 32:
2294 if (fb->depth == 24)
2295 dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
2296 else if (fb->depth == 30)
2297 dspcntr |= DISPPLANE_32BPP_30BIT_NO_ALPHA;
2298 else
2299 return -EINVAL;
2300 break;
2301 default:
2302 DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
2303 return -EINVAL;
2304 }
2305
2306 if (obj->tiling_mode != I915_TILING_NONE)
2307 dspcntr |= DISPPLANE_TILED;
2308 else
2309 dspcntr &= ~DISPPLANE_TILED;
2310
2311 /* must disable */
2312 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2313
2314 I915_WRITE(reg, dspcntr);
2315
2316 Start = obj->gtt_offset;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002317 Offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002318
2319 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002320 Start, Offset, x, y, fb->pitches[0]);
2321 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002322 I915_WRITE(DSPSURF(plane), Start);
2323 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2324 I915_WRITE(DSPADDR(plane), Offset);
2325 POSTING_READ(reg);
2326
2327 return 0;
2328}
2329
2330/* Assume fb object is pinned & idle & fenced and just update base pointers */
2331static int
2332intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2333 int x, int y, enum mode_set_atomic state)
2334{
2335 struct drm_device *dev = crtc->dev;
2336 struct drm_i915_private *dev_priv = dev->dev_private;
2337 int ret;
2338
2339 ret = dev_priv->display.update_plane(crtc, fb, x, y);
2340 if (ret)
2341 return ret;
2342
Chris Wilsonbed4a672010-09-11 10:47:47 +01002343 intel_update_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02002344 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07002345
2346 return 0;
2347}
2348
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002349static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002350intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
2351 struct drm_framebuffer *old_fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002352{
2353 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002354 struct drm_i915_master_private *master_priv;
2355 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002356 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002357
2358 /* no fb bound */
2359 if (!crtc->fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002360 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002361 return 0;
2362 }
2363
Chris Wilson265db952010-09-20 15:41:01 +01002364 switch (intel_crtc->plane) {
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002365 case 0:
2366 case 1:
2367 break;
Jesse Barnes27f82272011-09-02 12:54:37 -07002368 case 2:
2369 if (IS_IVYBRIDGE(dev))
2370 break;
2371 /* fall through otherwise */
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002372 default:
Jesse Barnesa5071c22011-07-19 15:38:56 -07002373 DRM_ERROR("no plane for crtc\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002374 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002375 }
2376
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002377 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01002378 ret = intel_pin_and_fence_fb_obj(dev,
2379 to_intel_framebuffer(crtc->fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002380 NULL);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002381 if (ret != 0) {
2382 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002383 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002384 return ret;
2385 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002386
Chris Wilson265db952010-09-20 15:41:01 +01002387 if (old_fb) {
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002388 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002389 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
Chris Wilson265db952010-09-20 15:41:01 +01002390
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002391 wait_event(dev_priv->pending_flip_queue,
Chris Wilson01eec722011-02-11 20:47:45 +00002392 atomic_read(&dev_priv->mm.wedged) ||
Chris Wilson05394f32010-11-08 19:18:58 +00002393 atomic_read(&obj->pending_flip) == 0);
Chris Wilson85345512010-11-13 09:49:11 +00002394
2395 /* Big Hammer, we also need to ensure that any pending
2396 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2397 * current scanout is retired before unpinning the old
2398 * framebuffer.
Chris Wilson01eec722011-02-11 20:47:45 +00002399 *
2400 * This should only fail upon a hung GPU, in which case we
2401 * can safely continue.
Chris Wilson85345512010-11-13 09:49:11 +00002402 */
Chris Wilsona8198ee2011-04-13 22:04:09 +01002403 ret = i915_gem_object_finish_gpu(obj);
Chris Wilson01eec722011-02-11 20:47:45 +00002404 (void) ret;
Chris Wilson265db952010-09-20 15:41:01 +01002405 }
2406
Jason Wessel21c74a82010-10-13 14:09:44 -05002407 ret = intel_pipe_set_base_atomic(crtc, crtc->fb, x, y,
2408 LEAVE_ATOMIC_MODE_SET);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002409 if (ret) {
Chris Wilson1690e1e2011-12-14 13:57:08 +01002410 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002411 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002412 DRM_ERROR("failed to update base address\n");
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002413 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002414 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002415
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002416 if (old_fb) {
2417 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002418 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002419 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002420
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002421 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002422
2423 if (!dev->primary->master)
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002424 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002425
2426 master_priv = dev->primary->master->driver_priv;
2427 if (!master_priv->sarea_priv)
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002428 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002429
Chris Wilson265db952010-09-20 15:41:01 +01002430 if (intel_crtc->pipe) {
Jesse Barnes79e53942008-11-07 14:24:08 -08002431 master_priv->sarea_priv->pipeB_x = x;
2432 master_priv->sarea_priv->pipeB_y = y;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002433 } else {
2434 master_priv->sarea_priv->pipeA_x = x;
2435 master_priv->sarea_priv->pipeA_y = y;
Jesse Barnes79e53942008-11-07 14:24:08 -08002436 }
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002437
2438 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002439}
2440
Chris Wilson5eddb702010-09-11 13:48:45 +01002441static void ironlake_set_pll_edp(struct drm_crtc *crtc, int clock)
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002442{
2443 struct drm_device *dev = crtc->dev;
2444 struct drm_i915_private *dev_priv = dev->dev_private;
2445 u32 dpa_ctl;
2446
Zhao Yakui28c97732009-10-09 11:39:41 +08002447 DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", clock);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002448 dpa_ctl = I915_READ(DP_A);
2449 dpa_ctl &= ~DP_PLL_FREQ_MASK;
2450
2451 if (clock < 200000) {
2452 u32 temp;
2453 dpa_ctl |= DP_PLL_FREQ_160MHZ;
2454 /* workaround for 160Mhz:
2455 1) program 0x4600c bits 15:0 = 0x8124
2456 2) program 0x46010 bit 0 = 1
2457 3) program 0x46034 bit 24 = 1
2458 4) program 0x64000 bit 14 = 1
2459 */
2460 temp = I915_READ(0x4600c);
2461 temp &= 0xffff0000;
2462 I915_WRITE(0x4600c, temp | 0x8124);
2463
2464 temp = I915_READ(0x46010);
2465 I915_WRITE(0x46010, temp | 1);
2466
2467 temp = I915_READ(0x46034);
2468 I915_WRITE(0x46034, temp | (1 << 24));
2469 } else {
2470 dpa_ctl |= DP_PLL_FREQ_270MHZ;
2471 }
2472 I915_WRITE(DP_A, dpa_ctl);
2473
Chris Wilson5eddb702010-09-11 13:48:45 +01002474 POSTING_READ(DP_A);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002475 udelay(500);
2476}
2477
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002478static void intel_fdi_normal_train(struct drm_crtc *crtc)
2479{
2480 struct drm_device *dev = crtc->dev;
2481 struct drm_i915_private *dev_priv = dev->dev_private;
2482 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2483 int pipe = intel_crtc->pipe;
2484 u32 reg, temp;
2485
2486 /* enable normal train */
2487 reg = FDI_TX_CTL(pipe);
2488 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002489 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002490 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2491 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002492 } else {
2493 temp &= ~FDI_LINK_TRAIN_NONE;
2494 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002495 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002496 I915_WRITE(reg, temp);
2497
2498 reg = FDI_RX_CTL(pipe);
2499 temp = I915_READ(reg);
2500 if (HAS_PCH_CPT(dev)) {
2501 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2502 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2503 } else {
2504 temp &= ~FDI_LINK_TRAIN_NONE;
2505 temp |= FDI_LINK_TRAIN_NONE;
2506 }
2507 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2508
2509 /* wait one idle pattern time */
2510 POSTING_READ(reg);
2511 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002512
2513 /* IVB wants error correction enabled */
2514 if (IS_IVYBRIDGE(dev))
2515 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2516 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002517}
2518
Jesse Barnes291427f2011-07-29 12:42:37 -07002519static void cpt_phase_pointer_enable(struct drm_device *dev, int pipe)
2520{
2521 struct drm_i915_private *dev_priv = dev->dev_private;
2522 u32 flags = I915_READ(SOUTH_CHICKEN1);
2523
2524 flags |= FDI_PHASE_SYNC_OVR(pipe);
2525 I915_WRITE(SOUTH_CHICKEN1, flags); /* once to unlock... */
2526 flags |= FDI_PHASE_SYNC_EN(pipe);
2527 I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to enable */
2528 POSTING_READ(SOUTH_CHICKEN1);
2529}
2530
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002531/* The FDI link training functions for ILK/Ibexpeak. */
2532static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2533{
2534 struct drm_device *dev = crtc->dev;
2535 struct drm_i915_private *dev_priv = dev->dev_private;
2536 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2537 int pipe = intel_crtc->pipe;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002538 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002539 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002540
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002541 /* FDI needs bits from pipe & plane first */
2542 assert_pipe_enabled(dev_priv, pipe);
2543 assert_plane_enabled(dev_priv, plane);
2544
Adam Jacksone1a44742010-06-25 15:32:14 -04002545 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2546 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002547 reg = FDI_RX_IMR(pipe);
2548 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002549 temp &= ~FDI_RX_SYMBOL_LOCK;
2550 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002551 I915_WRITE(reg, temp);
2552 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002553 udelay(150);
2554
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002555 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002556 reg = FDI_TX_CTL(pipe);
2557 temp = I915_READ(reg);
Adam Jackson77ffb592010-04-12 11:38:44 -04002558 temp &= ~(7 << 19);
2559 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002560 temp &= ~FDI_LINK_TRAIN_NONE;
2561 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002562 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002563
Chris Wilson5eddb702010-09-11 13:48:45 +01002564 reg = FDI_RX_CTL(pipe);
2565 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002566 temp &= ~FDI_LINK_TRAIN_NONE;
2567 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002568 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2569
2570 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002571 udelay(150);
2572
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002573 /* Ironlake workaround, enable clock pointer after FDI enable*/
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002574 if (HAS_PCH_IBX(dev)) {
2575 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2576 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2577 FDI_RX_PHASE_SYNC_POINTER_EN);
2578 }
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002579
Chris Wilson5eddb702010-09-11 13:48:45 +01002580 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002581 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002582 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002583 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2584
2585 if ((temp & FDI_RX_BIT_LOCK)) {
2586 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002587 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002588 break;
2589 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002590 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002591 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002592 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002593
2594 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002595 reg = FDI_TX_CTL(pipe);
2596 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002597 temp &= ~FDI_LINK_TRAIN_NONE;
2598 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002599 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002600
Chris Wilson5eddb702010-09-11 13:48:45 +01002601 reg = FDI_RX_CTL(pipe);
2602 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002603 temp &= ~FDI_LINK_TRAIN_NONE;
2604 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002605 I915_WRITE(reg, temp);
2606
2607 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002608 udelay(150);
2609
Chris Wilson5eddb702010-09-11 13:48:45 +01002610 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002611 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002612 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002613 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2614
2615 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002616 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002617 DRM_DEBUG_KMS("FDI train 2 done.\n");
2618 break;
2619 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002620 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002621 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002622 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002623
2624 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002625
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002626}
2627
Akshay Joshi0206e352011-08-16 15:34:10 -04002628static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002629 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2630 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2631 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2632 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2633};
2634
2635/* The FDI link training functions for SNB/Cougarpoint. */
2636static void gen6_fdi_link_train(struct drm_crtc *crtc)
2637{
2638 struct drm_device *dev = crtc->dev;
2639 struct drm_i915_private *dev_priv = dev->dev_private;
2640 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2641 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05002642 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002643
Adam Jacksone1a44742010-06-25 15:32:14 -04002644 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2645 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002646 reg = FDI_RX_IMR(pipe);
2647 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002648 temp &= ~FDI_RX_SYMBOL_LOCK;
2649 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002650 I915_WRITE(reg, temp);
2651
2652 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002653 udelay(150);
2654
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002655 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002656 reg = FDI_TX_CTL(pipe);
2657 temp = I915_READ(reg);
Adam Jackson77ffb592010-04-12 11:38:44 -04002658 temp &= ~(7 << 19);
2659 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002660 temp &= ~FDI_LINK_TRAIN_NONE;
2661 temp |= FDI_LINK_TRAIN_PATTERN_1;
2662 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2663 /* SNB-B */
2664 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002665 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002666
Chris Wilson5eddb702010-09-11 13:48:45 +01002667 reg = FDI_RX_CTL(pipe);
2668 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002669 if (HAS_PCH_CPT(dev)) {
2670 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2671 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2672 } else {
2673 temp &= ~FDI_LINK_TRAIN_NONE;
2674 temp |= FDI_LINK_TRAIN_PATTERN_1;
2675 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002676 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2677
2678 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002679 udelay(150);
2680
Jesse Barnes291427f2011-07-29 12:42:37 -07002681 if (HAS_PCH_CPT(dev))
2682 cpt_phase_pointer_enable(dev, pipe);
2683
Akshay Joshi0206e352011-08-16 15:34:10 -04002684 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002685 reg = FDI_TX_CTL(pipe);
2686 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002687 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2688 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002689 I915_WRITE(reg, temp);
2690
2691 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002692 udelay(500);
2693
Sean Paulfa37d392012-03-02 12:53:39 -05002694 for (retry = 0; retry < 5; retry++) {
2695 reg = FDI_RX_IIR(pipe);
2696 temp = I915_READ(reg);
2697 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2698 if (temp & FDI_RX_BIT_LOCK) {
2699 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2700 DRM_DEBUG_KMS("FDI train 1 done.\n");
2701 break;
2702 }
2703 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002704 }
Sean Paulfa37d392012-03-02 12:53:39 -05002705 if (retry < 5)
2706 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002707 }
2708 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002709 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002710
2711 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002712 reg = FDI_TX_CTL(pipe);
2713 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002714 temp &= ~FDI_LINK_TRAIN_NONE;
2715 temp |= FDI_LINK_TRAIN_PATTERN_2;
2716 if (IS_GEN6(dev)) {
2717 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2718 /* SNB-B */
2719 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2720 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002721 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002722
Chris Wilson5eddb702010-09-11 13:48:45 +01002723 reg = FDI_RX_CTL(pipe);
2724 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002725 if (HAS_PCH_CPT(dev)) {
2726 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2727 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2728 } else {
2729 temp &= ~FDI_LINK_TRAIN_NONE;
2730 temp |= FDI_LINK_TRAIN_PATTERN_2;
2731 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002732 I915_WRITE(reg, temp);
2733
2734 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002735 udelay(150);
2736
Akshay Joshi0206e352011-08-16 15:34:10 -04002737 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002738 reg = FDI_TX_CTL(pipe);
2739 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002740 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2741 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002742 I915_WRITE(reg, temp);
2743
2744 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002745 udelay(500);
2746
Sean Paulfa37d392012-03-02 12:53:39 -05002747 for (retry = 0; retry < 5; retry++) {
2748 reg = FDI_RX_IIR(pipe);
2749 temp = I915_READ(reg);
2750 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2751 if (temp & FDI_RX_SYMBOL_LOCK) {
2752 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2753 DRM_DEBUG_KMS("FDI train 2 done.\n");
2754 break;
2755 }
2756 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002757 }
Sean Paulfa37d392012-03-02 12:53:39 -05002758 if (retry < 5)
2759 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002760 }
2761 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002762 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002763
2764 DRM_DEBUG_KMS("FDI train done.\n");
2765}
2766
Jesse Barnes357555c2011-04-28 15:09:55 -07002767/* Manual link training for Ivy Bridge A0 parts */
2768static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2769{
2770 struct drm_device *dev = crtc->dev;
2771 struct drm_i915_private *dev_priv = dev->dev_private;
2772 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2773 int pipe = intel_crtc->pipe;
2774 u32 reg, temp, i;
2775
2776 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2777 for train result */
2778 reg = FDI_RX_IMR(pipe);
2779 temp = I915_READ(reg);
2780 temp &= ~FDI_RX_SYMBOL_LOCK;
2781 temp &= ~FDI_RX_BIT_LOCK;
2782 I915_WRITE(reg, temp);
2783
2784 POSTING_READ(reg);
2785 udelay(150);
2786
2787 /* enable CPU FDI TX and PCH FDI RX */
2788 reg = FDI_TX_CTL(pipe);
2789 temp = I915_READ(reg);
2790 temp &= ~(7 << 19);
2791 temp |= (intel_crtc->fdi_lanes - 1) << 19;
2792 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2793 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
2794 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2795 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002796 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002797 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2798
2799 reg = FDI_RX_CTL(pipe);
2800 temp = I915_READ(reg);
2801 temp &= ~FDI_LINK_TRAIN_AUTO;
2802 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2803 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002804 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002805 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2806
2807 POSTING_READ(reg);
2808 udelay(150);
2809
Jesse Barnes291427f2011-07-29 12:42:37 -07002810 if (HAS_PCH_CPT(dev))
2811 cpt_phase_pointer_enable(dev, pipe);
2812
Akshay Joshi0206e352011-08-16 15:34:10 -04002813 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002814 reg = FDI_TX_CTL(pipe);
2815 temp = I915_READ(reg);
2816 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2817 temp |= snb_b_fdi_train_param[i];
2818 I915_WRITE(reg, temp);
2819
2820 POSTING_READ(reg);
2821 udelay(500);
2822
2823 reg = FDI_RX_IIR(pipe);
2824 temp = I915_READ(reg);
2825 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2826
2827 if (temp & FDI_RX_BIT_LOCK ||
2828 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2829 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2830 DRM_DEBUG_KMS("FDI train 1 done.\n");
2831 break;
2832 }
2833 }
2834 if (i == 4)
2835 DRM_ERROR("FDI train 1 fail!\n");
2836
2837 /* Train 2 */
2838 reg = FDI_TX_CTL(pipe);
2839 temp = I915_READ(reg);
2840 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2841 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2842 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2843 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2844 I915_WRITE(reg, temp);
2845
2846 reg = FDI_RX_CTL(pipe);
2847 temp = I915_READ(reg);
2848 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2849 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2850 I915_WRITE(reg, temp);
2851
2852 POSTING_READ(reg);
2853 udelay(150);
2854
Akshay Joshi0206e352011-08-16 15:34:10 -04002855 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002856 reg = FDI_TX_CTL(pipe);
2857 temp = I915_READ(reg);
2858 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2859 temp |= snb_b_fdi_train_param[i];
2860 I915_WRITE(reg, temp);
2861
2862 POSTING_READ(reg);
2863 udelay(500);
2864
2865 reg = FDI_RX_IIR(pipe);
2866 temp = I915_READ(reg);
2867 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2868
2869 if (temp & FDI_RX_SYMBOL_LOCK) {
2870 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2871 DRM_DEBUG_KMS("FDI train 2 done.\n");
2872 break;
2873 }
2874 }
2875 if (i == 4)
2876 DRM_ERROR("FDI train 2 fail!\n");
2877
2878 DRM_DEBUG_KMS("FDI train done.\n");
2879}
2880
2881static void ironlake_fdi_pll_enable(struct drm_crtc *crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07002882{
2883 struct drm_device *dev = crtc->dev;
2884 struct drm_i915_private *dev_priv = dev->dev_private;
2885 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2886 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002887 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002888
Jesse Barnesc64e3112010-09-10 11:27:03 -07002889 /* Write the TU size bits so error detection works */
Chris Wilson5eddb702010-09-11 13:48:45 +01002890 I915_WRITE(FDI_RX_TUSIZE1(pipe),
2891 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
Jesse Barnesc64e3112010-09-10 11:27:03 -07002892
Jesse Barnes0e23b992010-09-10 11:10:00 -07002893 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01002894 reg = FDI_RX_CTL(pipe);
2895 temp = I915_READ(reg);
2896 temp &= ~((0x7 << 19) | (0x7 << 16));
Jesse Barnes0e23b992010-09-10 11:10:00 -07002897 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Chris Wilson5eddb702010-09-11 13:48:45 +01002898 temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2899 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2900
2901 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002902 udelay(200);
2903
2904 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002905 temp = I915_READ(reg);
2906 I915_WRITE(reg, temp | FDI_PCDCLK);
2907
2908 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002909 udelay(200);
2910
2911 /* Enable CPU FDI TX PLL, always on for Ironlake */
Chris Wilson5eddb702010-09-11 13:48:45 +01002912 reg = FDI_TX_CTL(pipe);
2913 temp = I915_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002914 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002915 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
2916
2917 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002918 udelay(100);
2919 }
2920}
2921
Jesse Barnes291427f2011-07-29 12:42:37 -07002922static void cpt_phase_pointer_disable(struct drm_device *dev, int pipe)
2923{
2924 struct drm_i915_private *dev_priv = dev->dev_private;
2925 u32 flags = I915_READ(SOUTH_CHICKEN1);
2926
2927 flags &= ~(FDI_PHASE_SYNC_EN(pipe));
2928 I915_WRITE(SOUTH_CHICKEN1, flags); /* once to disable... */
2929 flags &= ~(FDI_PHASE_SYNC_OVR(pipe));
2930 I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to lock */
2931 POSTING_READ(SOUTH_CHICKEN1);
2932}
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002933static void ironlake_fdi_disable(struct drm_crtc *crtc)
2934{
2935 struct drm_device *dev = crtc->dev;
2936 struct drm_i915_private *dev_priv = dev->dev_private;
2937 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2938 int pipe = intel_crtc->pipe;
2939 u32 reg, temp;
2940
2941 /* disable CPU FDI tx and PCH FDI rx */
2942 reg = FDI_TX_CTL(pipe);
2943 temp = I915_READ(reg);
2944 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2945 POSTING_READ(reg);
2946
2947 reg = FDI_RX_CTL(pipe);
2948 temp = I915_READ(reg);
2949 temp &= ~(0x7 << 16);
2950 temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2951 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2952
2953 POSTING_READ(reg);
2954 udelay(100);
2955
2956 /* Ironlake workaround, disable clock pointer after downing FDI */
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002957 if (HAS_PCH_IBX(dev)) {
2958 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002959 I915_WRITE(FDI_RX_CHICKEN(pipe),
2960 I915_READ(FDI_RX_CHICKEN(pipe) &
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002961 ~FDI_RX_PHASE_SYNC_POINTER_EN));
Jesse Barnes291427f2011-07-29 12:42:37 -07002962 } else if (HAS_PCH_CPT(dev)) {
2963 cpt_phase_pointer_disable(dev, pipe);
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002964 }
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002965
2966 /* still set train pattern 1 */
2967 reg = FDI_TX_CTL(pipe);
2968 temp = I915_READ(reg);
2969 temp &= ~FDI_LINK_TRAIN_NONE;
2970 temp |= FDI_LINK_TRAIN_PATTERN_1;
2971 I915_WRITE(reg, temp);
2972
2973 reg = FDI_RX_CTL(pipe);
2974 temp = I915_READ(reg);
2975 if (HAS_PCH_CPT(dev)) {
2976 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2977 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2978 } else {
2979 temp &= ~FDI_LINK_TRAIN_NONE;
2980 temp |= FDI_LINK_TRAIN_PATTERN_1;
2981 }
2982 /* BPC in FDI rx is consistent with that in PIPECONF */
2983 temp &= ~(0x07 << 16);
2984 temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2985 I915_WRITE(reg, temp);
2986
2987 POSTING_READ(reg);
2988 udelay(100);
2989}
2990
Chris Wilson6b383a72010-09-13 13:54:26 +01002991/*
2992 * When we disable a pipe, we need to clear any pending scanline wait events
2993 * to avoid hanging the ring, which we assume we are waiting on.
2994 */
2995static void intel_clear_scanline_wait(struct drm_device *dev)
2996{
2997 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson8168bd42010-11-11 17:54:52 +00002998 struct intel_ring_buffer *ring;
Chris Wilson6b383a72010-09-13 13:54:26 +01002999 u32 tmp;
3000
3001 if (IS_GEN2(dev))
3002 /* Can't break the hang on i8xx */
3003 return;
3004
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003005 ring = LP_RING(dev_priv);
Chris Wilson8168bd42010-11-11 17:54:52 +00003006 tmp = I915_READ_CTL(ring);
3007 if (tmp & RING_WAIT)
3008 I915_WRITE_CTL(ring, tmp);
Chris Wilson6b383a72010-09-13 13:54:26 +01003009}
3010
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003011static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
3012{
Chris Wilson05394f32010-11-08 19:18:58 +00003013 struct drm_i915_gem_object *obj;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003014 struct drm_i915_private *dev_priv;
3015
3016 if (crtc->fb == NULL)
3017 return;
3018
Chris Wilson05394f32010-11-08 19:18:58 +00003019 obj = to_intel_framebuffer(crtc->fb)->obj;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003020 dev_priv = crtc->dev->dev_private;
3021 wait_event(dev_priv->pending_flip_queue,
Chris Wilson05394f32010-11-08 19:18:58 +00003022 atomic_read(&obj->pending_flip) == 0);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003023}
3024
Jesse Barnes040484a2011-01-03 12:14:26 -08003025static bool intel_crtc_driving_pch(struct drm_crtc *crtc)
3026{
3027 struct drm_device *dev = crtc->dev;
3028 struct drm_mode_config *mode_config = &dev->mode_config;
3029 struct intel_encoder *encoder;
3030
3031 /*
3032 * If there's a non-PCH eDP on this crtc, it must be DP_A, and that
3033 * must be driven by its own crtc; no sharing is possible.
3034 */
3035 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
3036 if (encoder->base.crtc != crtc)
3037 continue;
3038
3039 switch (encoder->type) {
3040 case INTEL_OUTPUT_EDP:
3041 if (!intel_encoder_is_pch_edp(&encoder->base))
3042 return false;
3043 continue;
3044 }
3045 }
3046
3047 return true;
3048}
3049
Jesse Barnesf67a5592011-01-05 10:31:48 -08003050/*
3051 * Enable PCH resources required for PCH ports:
3052 * - PCH PLLs
3053 * - FDI training & RX/TX
3054 * - update transcoder timings
3055 * - DP transcoding bits
3056 * - transcoder
3057 */
3058static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003059{
3060 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003061 struct drm_i915_private *dev_priv = dev->dev_private;
3062 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3063 int pipe = intel_crtc->pipe;
Jesse Barnes4b645f12011-10-12 09:51:31 -07003064 u32 reg, temp, transc_sel;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003065
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003066 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07003067 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003068
Jesse Barnes92f25842011-01-04 15:09:34 -08003069 intel_enable_pch_pll(dev_priv, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003070
3071 if (HAS_PCH_CPT(dev)) {
Jesse Barnes4b645f12011-10-12 09:51:31 -07003072 transc_sel = intel_crtc->use_pll_a ? TRANSC_DPLLA_SEL :
3073 TRANSC_DPLLB_SEL;
3074
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003075 /* Be sure PCH DPLL SEL is set */
3076 temp = I915_READ(PCH_DPLL_SEL);
Jesse Barnesd64311a2011-10-12 15:01:33 -07003077 if (pipe == 0) {
3078 temp &= ~(TRANSA_DPLLB_SEL);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003079 temp |= (TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL);
Jesse Barnesd64311a2011-10-12 15:01:33 -07003080 } else if (pipe == 1) {
3081 temp &= ~(TRANSB_DPLLB_SEL);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003082 temp |= (TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
Jesse Barnesd64311a2011-10-12 15:01:33 -07003083 } else if (pipe == 2) {
3084 temp &= ~(TRANSC_DPLLB_SEL);
Jesse Barnes4b645f12011-10-12 09:51:31 -07003085 temp |= (TRANSC_DPLL_ENABLE | transc_sel);
Jesse Barnesd64311a2011-10-12 15:01:33 -07003086 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003087 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003088 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003089
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003090 /* set transcoder timing, panel must allow it */
3091 assert_panel_unlocked(dev_priv, pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01003092 I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
3093 I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
3094 I915_WRITE(TRANS_HSYNC(pipe), I915_READ(HSYNC(pipe)));
3095
3096 I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
3097 I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
3098 I915_WRITE(TRANS_VSYNC(pipe), I915_READ(VSYNC(pipe)));
Daniel Vetter0529a0d2012-01-28 14:49:24 +01003099 I915_WRITE(TRANS_VSYNCSHIFT(pipe), I915_READ(VSYNCSHIFT(pipe)));
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003100
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003101 intel_fdi_normal_train(crtc);
3102
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003103 /* For PCH DP, enable TRANS_DP_CTL */
3104 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07003105 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3106 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003107 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003108 reg = TRANS_DP_CTL(pipe);
3109 temp = I915_READ(reg);
3110 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003111 TRANS_DP_SYNC_MASK |
3112 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003113 temp |= (TRANS_DP_OUTPUT_ENABLE |
3114 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003115 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003116
3117 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003118 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003119 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003120 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003121
3122 switch (intel_trans_dp_port_sel(crtc)) {
3123 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003124 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003125 break;
3126 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003127 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003128 break;
3129 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003130 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003131 break;
3132 default:
3133 DRM_DEBUG_KMS("Wrong PCH DP port return. Guess port B\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003134 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003135 break;
3136 }
3137
Chris Wilson5eddb702010-09-11 13:48:45 +01003138 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003139 }
3140
Jesse Barnes040484a2011-01-03 12:14:26 -08003141 intel_enable_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003142}
3143
Jesse Barnesd4270e52011-10-11 10:43:02 -07003144void intel_cpt_verify_modeset(struct drm_device *dev, int pipe)
3145{
3146 struct drm_i915_private *dev_priv = dev->dev_private;
3147 int dslreg = PIPEDSL(pipe), tc2reg = TRANS_CHICKEN2(pipe);
3148 u32 temp;
3149
3150 temp = I915_READ(dslreg);
3151 udelay(500);
3152 if (wait_for(I915_READ(dslreg) != temp, 5)) {
3153 /* Without this, mode sets may fail silently on FDI */
3154 I915_WRITE(tc2reg, TRANS_AUTOTRAIN_GEN_STALL_DIS);
3155 udelay(250);
3156 I915_WRITE(tc2reg, 0);
3157 if (wait_for(I915_READ(dslreg) != temp, 5))
3158 DRM_ERROR("mode set failed: pipe %d stuck\n", pipe);
3159 }
3160}
3161
Jesse Barnesf67a5592011-01-05 10:31:48 -08003162static void ironlake_crtc_enable(struct drm_crtc *crtc)
3163{
3164 struct drm_device *dev = crtc->dev;
3165 struct drm_i915_private *dev_priv = dev->dev_private;
3166 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3167 int pipe = intel_crtc->pipe;
3168 int plane = intel_crtc->plane;
3169 u32 temp;
3170 bool is_pch_port;
3171
3172 if (intel_crtc->active)
3173 return;
3174
3175 intel_crtc->active = true;
3176 intel_update_watermarks(dev);
3177
3178 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
3179 temp = I915_READ(PCH_LVDS);
3180 if ((temp & LVDS_PORT_EN) == 0)
3181 I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
3182 }
3183
3184 is_pch_port = intel_crtc_driving_pch(crtc);
3185
3186 if (is_pch_port)
Jesse Barnes357555c2011-04-28 15:09:55 -07003187 ironlake_fdi_pll_enable(crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003188 else
3189 ironlake_fdi_disable(crtc);
3190
3191 /* Enable panel fitting for LVDS */
3192 if (dev_priv->pch_pf_size &&
3193 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) || HAS_eDP)) {
3194 /* Force use of hard-coded filter coefficients
3195 * as some pre-programmed values are broken,
3196 * e.g. x201.
3197 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003198 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3199 I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
3200 I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003201 }
3202
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02003203 /*
3204 * On ILK+ LUT must be loaded before the pipe is running but with
3205 * clocks enabled
3206 */
3207 intel_crtc_load_lut(crtc);
3208
Jesse Barnesf67a5592011-01-05 10:31:48 -08003209 intel_enable_pipe(dev_priv, pipe, is_pch_port);
3210 intel_enable_plane(dev_priv, plane, pipe);
3211
3212 if (is_pch_port)
3213 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003214
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003215 mutex_lock(&dev->struct_mutex);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003216 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003217 mutex_unlock(&dev->struct_mutex);
3218
Chris Wilson6b383a72010-09-13 13:54:26 +01003219 intel_crtc_update_cursor(crtc, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003220}
3221
3222static void ironlake_crtc_disable(struct drm_crtc *crtc)
3223{
3224 struct drm_device *dev = crtc->dev;
3225 struct drm_i915_private *dev_priv = dev->dev_private;
3226 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3227 int pipe = intel_crtc->pipe;
3228 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01003229 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003230
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003231 if (!intel_crtc->active)
3232 return;
3233
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003234 intel_crtc_wait_for_pending_flips(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003235 drm_vblank_off(dev, pipe);
Chris Wilson6b383a72010-09-13 13:54:26 +01003236 intel_crtc_update_cursor(crtc, false);
Chris Wilson5eddb702010-09-11 13:48:45 +01003237
Jesse Barnesb24e7172011-01-04 15:09:30 -08003238 intel_disable_plane(dev_priv, plane, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003239
Chris Wilson973d04f2011-07-08 12:22:37 +01003240 if (dev_priv->cfb_plane == plane)
3241 intel_disable_fbc(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003242
Jesse Barnesb24e7172011-01-04 15:09:30 -08003243 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003244
Jesse Barnes6be4a602010-09-10 10:26:01 -07003245 /* Disable PF */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003246 I915_WRITE(PF_CTL(pipe), 0);
3247 I915_WRITE(PF_WIN_SZ(pipe), 0);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003248
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003249 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003250
Jesse Barnes47a05ec2011-02-07 13:46:40 -08003251 /* This is a horrible layering violation; we should be doing this in
3252 * the connector/encoder ->prepare instead, but we don't always have
3253 * enough information there about the config to know whether it will
3254 * actually be necessary or just cause undesired flicker.
3255 */
3256 intel_disable_pch_ports(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003257
Jesse Barnes040484a2011-01-03 12:14:26 -08003258 intel_disable_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003259
Jesse Barnes6be4a602010-09-10 10:26:01 -07003260 if (HAS_PCH_CPT(dev)) {
3261 /* disable TRANS_DP_CTL */
Chris Wilson5eddb702010-09-11 13:48:45 +01003262 reg = TRANS_DP_CTL(pipe);
3263 temp = I915_READ(reg);
3264 temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
Eric Anholtcb3543c2011-02-02 12:08:07 -08003265 temp |= TRANS_DP_PORT_SEL_NONE;
Chris Wilson5eddb702010-09-11 13:48:45 +01003266 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003267
3268 /* disable DPLL_SEL */
3269 temp = I915_READ(PCH_DPLL_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003270 switch (pipe) {
3271 case 0:
Jesse Barnesd64311a2011-10-12 15:01:33 -07003272 temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003273 break;
3274 case 1:
Jesse Barnes6be4a602010-09-10 10:26:01 -07003275 temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003276 break;
3277 case 2:
Jesse Barnes4b645f12011-10-12 09:51:31 -07003278 /* C shares PLL A or B */
Jesse Barnesd64311a2011-10-12 15:01:33 -07003279 temp &= ~(TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003280 break;
3281 default:
3282 BUG(); /* wtf */
3283 }
Jesse Barnes6be4a602010-09-10 10:26:01 -07003284 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003285 }
3286
3287 /* disable PCH DPLL */
Jesse Barnes4b645f12011-10-12 09:51:31 -07003288 if (!intel_crtc->no_pll)
3289 intel_disable_pch_pll(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003290
3291 /* Switch from PCDclk to Rawclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003292 reg = FDI_RX_CTL(pipe);
3293 temp = I915_READ(reg);
3294 I915_WRITE(reg, temp & ~FDI_PCDCLK);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003295
3296 /* Disable CPU FDI TX PLL */
Chris Wilson5eddb702010-09-11 13:48:45 +01003297 reg = FDI_TX_CTL(pipe);
3298 temp = I915_READ(reg);
3299 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3300
3301 POSTING_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003302 udelay(100);
3303
Chris Wilson5eddb702010-09-11 13:48:45 +01003304 reg = FDI_RX_CTL(pipe);
3305 temp = I915_READ(reg);
3306 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003307
3308 /* Wait for the clocks to turn off. */
Chris Wilson5eddb702010-09-11 13:48:45 +01003309 POSTING_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003310 udelay(100);
Chris Wilson6b383a72010-09-13 13:54:26 +01003311
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003312 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003313 intel_update_watermarks(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003314
3315 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01003316 intel_update_fbc(dev);
3317 intel_clear_scanline_wait(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003318 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003319}
3320
3321static void ironlake_crtc_dpms(struct drm_crtc *crtc, int mode)
3322{
3323 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3324 int pipe = intel_crtc->pipe;
3325 int plane = intel_crtc->plane;
3326
Zhenyu Wang2c072452009-06-05 15:38:42 +08003327 /* XXX: When our outputs are all unaware of DPMS modes other than off
3328 * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
3329 */
3330 switch (mode) {
3331 case DRM_MODE_DPMS_ON:
3332 case DRM_MODE_DPMS_STANDBY:
3333 case DRM_MODE_DPMS_SUSPEND:
Chris Wilson868dc582010-08-07 11:01:31 +01003334 DRM_DEBUG_KMS("crtc %d/%d dpms on\n", pipe, plane);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003335 ironlake_crtc_enable(crtc);
Chris Wilson868dc582010-08-07 11:01:31 +01003336 break;
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08003337
Zhenyu Wang2c072452009-06-05 15:38:42 +08003338 case DRM_MODE_DPMS_OFF:
Chris Wilson868dc582010-08-07 11:01:31 +01003339 DRM_DEBUG_KMS("crtc %d/%d dpms off\n", pipe, plane);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003340 ironlake_crtc_disable(crtc);
Zhenyu Wang2c072452009-06-05 15:38:42 +08003341 break;
3342 }
3343}
3344
Daniel Vetter02e792f2009-09-15 22:57:34 +02003345static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3346{
Daniel Vetter02e792f2009-09-15 22:57:34 +02003347 if (!enable && intel_crtc->overlay) {
Chris Wilson23f09ce2010-08-12 13:53:37 +01003348 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00003349 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02003350
Chris Wilson23f09ce2010-08-12 13:53:37 +01003351 mutex_lock(&dev->struct_mutex);
Chris Wilsonce453d82011-02-21 14:43:56 +00003352 dev_priv->mm.interruptible = false;
3353 (void) intel_overlay_switch_off(intel_crtc->overlay);
3354 dev_priv->mm.interruptible = true;
Chris Wilson23f09ce2010-08-12 13:53:37 +01003355 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02003356 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02003357
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01003358 /* Let userspace switch the overlay on again. In most cases userspace
3359 * has to recompute where to put it anyway.
3360 */
Daniel Vetter02e792f2009-09-15 22:57:34 +02003361}
3362
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003363static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003364{
3365 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08003366 struct drm_i915_private *dev_priv = dev->dev_private;
3367 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3368 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07003369 int plane = intel_crtc->plane;
Jesse Barnes79e53942008-11-07 14:24:08 -08003370
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003371 if (intel_crtc->active)
3372 return;
3373
3374 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01003375 intel_update_watermarks(dev);
3376
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08003377 intel_enable_pll(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08003378 intel_enable_pipe(dev_priv, pipe, false);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003379 intel_enable_plane(dev_priv, plane, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003380
3381 intel_crtc_load_lut(crtc);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003382 intel_update_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003383
3384 /* Give the overlay scaler a chance to enable if it's on this pipe */
3385 intel_crtc_dpms_overlay(intel_crtc, true);
Chris Wilson6b383a72010-09-13 13:54:26 +01003386 intel_crtc_update_cursor(crtc, true);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003387}
3388
3389static void i9xx_crtc_disable(struct drm_crtc *crtc)
3390{
3391 struct drm_device *dev = crtc->dev;
3392 struct drm_i915_private *dev_priv = dev->dev_private;
3393 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3394 int pipe = intel_crtc->pipe;
3395 int plane = intel_crtc->plane;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003396
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003397 if (!intel_crtc->active)
3398 return;
3399
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003400 /* Give the overlay scaler a chance to disable if it's on this pipe */
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003401 intel_crtc_wait_for_pending_flips(crtc);
3402 drm_vblank_off(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003403 intel_crtc_dpms_overlay(intel_crtc, false);
Chris Wilson6b383a72010-09-13 13:54:26 +01003404 intel_crtc_update_cursor(crtc, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003405
Chris Wilson973d04f2011-07-08 12:22:37 +01003406 if (dev_priv->cfb_plane == plane)
3407 intel_disable_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003408
Jesse Barnesb24e7172011-01-04 15:09:30 -08003409 intel_disable_plane(dev_priv, plane, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003410 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08003411 intel_disable_pll(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003412
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003413 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003414 intel_update_fbc(dev);
3415 intel_update_watermarks(dev);
3416 intel_clear_scanline_wait(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003417}
3418
3419static void i9xx_crtc_dpms(struct drm_crtc *crtc, int mode)
3420{
Jesse Barnes79e53942008-11-07 14:24:08 -08003421 /* XXX: When our outputs are all unaware of DPMS modes other than off
3422 * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
3423 */
3424 switch (mode) {
3425 case DRM_MODE_DPMS_ON:
3426 case DRM_MODE_DPMS_STANDBY:
3427 case DRM_MODE_DPMS_SUSPEND:
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003428 i9xx_crtc_enable(crtc);
3429 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08003430 case DRM_MODE_DPMS_OFF:
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003431 i9xx_crtc_disable(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08003432 break;
3433 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08003434}
3435
3436/**
3437 * Sets the power management mode of the pipe and plane.
Zhenyu Wang2c072452009-06-05 15:38:42 +08003438 */
3439static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
3440{
3441 struct drm_device *dev = crtc->dev;
Jesse Barnese70236a2009-09-21 10:42:27 -07003442 struct drm_i915_private *dev_priv = dev->dev_private;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003443 struct drm_i915_master_private *master_priv;
3444 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3445 int pipe = intel_crtc->pipe;
3446 bool enabled;
3447
Chris Wilson032d2a02010-09-06 16:17:22 +01003448 if (intel_crtc->dpms_mode == mode)
3449 return;
3450
Chris Wilsondebcadd2010-08-07 11:01:33 +01003451 intel_crtc->dpms_mode = mode;
Chris Wilsondebcadd2010-08-07 11:01:33 +01003452
Jesse Barnese70236a2009-09-21 10:42:27 -07003453 dev_priv->display.dpms(crtc, mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08003454
3455 if (!dev->primary->master)
3456 return;
3457
3458 master_priv = dev->primary->master->driver_priv;
3459 if (!master_priv->sarea_priv)
3460 return;
3461
3462 enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
3463
3464 switch (pipe) {
3465 case 0:
3466 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
3467 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
3468 break;
3469 case 1:
3470 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
3471 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
3472 break;
3473 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003474 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08003475 break;
3476 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003477}
3478
Chris Wilsoncdd59982010-09-08 16:30:16 +01003479static void intel_crtc_disable(struct drm_crtc *crtc)
3480{
3481 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
3482 struct drm_device *dev = crtc->dev;
3483
3484 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
Chris Wilson931872f2012-01-16 23:01:13 +00003485 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
3486 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003487
3488 if (crtc->fb) {
3489 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01003490 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003491 mutex_unlock(&dev->struct_mutex);
3492 }
3493}
3494
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003495/* Prepare for a mode set.
3496 *
3497 * Note we could be a lot smarter here. We need to figure out which outputs
3498 * will be enabled, which disabled (in short, how the config will changes)
3499 * and perform the minimum necessary steps to accomplish that, e.g. updating
3500 * watermarks, FBC configuration, making sure PLLs are programmed correctly,
3501 * panel fitting is in the proper state, etc.
3502 */
3503static void i9xx_crtc_prepare(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003504{
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003505 i9xx_crtc_disable(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08003506}
3507
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003508static void i9xx_crtc_commit(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003509{
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003510 i9xx_crtc_enable(crtc);
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003511}
3512
3513static void ironlake_crtc_prepare(struct drm_crtc *crtc)
3514{
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003515 ironlake_crtc_disable(crtc);
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003516}
3517
3518static void ironlake_crtc_commit(struct drm_crtc *crtc)
3519{
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003520 ironlake_crtc_enable(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08003521}
3522
Akshay Joshi0206e352011-08-16 15:34:10 -04003523void intel_encoder_prepare(struct drm_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08003524{
3525 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
3526 /* lvds has its own version of prepare see intel_lvds_prepare */
3527 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
3528}
3529
Akshay Joshi0206e352011-08-16 15:34:10 -04003530void intel_encoder_commit(struct drm_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08003531{
3532 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
Jesse Barnesd4270e52011-10-11 10:43:02 -07003533 struct drm_device *dev = encoder->dev;
3534 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
3535 struct intel_crtc *intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
3536
Jesse Barnes79e53942008-11-07 14:24:08 -08003537 /* lvds has its own version of commit see intel_lvds_commit */
3538 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003539
3540 if (HAS_PCH_CPT(dev))
3541 intel_cpt_verify_modeset(dev, intel_crtc->pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08003542}
3543
Chris Wilsonea5b2132010-08-04 13:50:23 +01003544void intel_encoder_destroy(struct drm_encoder *encoder)
3545{
Chris Wilson4ef69c72010-09-09 15:14:28 +01003546 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01003547
Chris Wilsonea5b2132010-08-04 13:50:23 +01003548 drm_encoder_cleanup(encoder);
3549 kfree(intel_encoder);
3550}
3551
Jesse Barnes79e53942008-11-07 14:24:08 -08003552static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
3553 struct drm_display_mode *mode,
3554 struct drm_display_mode *adjusted_mode)
3555{
Zhenyu Wang2c072452009-06-05 15:38:42 +08003556 struct drm_device *dev = crtc->dev;
Chris Wilson89749352010-09-12 18:25:19 +01003557
Eric Anholtbad720f2009-10-22 16:11:14 -07003558 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08003559 /* FDI link clock is fixed at 2.7G */
Jesse Barnes2377b742010-07-07 14:06:43 -07003560 if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
3561 return false;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003562 }
Chris Wilson89749352010-09-12 18:25:19 +01003563
Daniel Vetterca9bfa72012-01-28 14:49:20 +01003564 /* All interlaced capable intel hw wants timings in frames. */
3565 drm_mode_set_crtcinfo(adjusted_mode, 0);
Chris Wilson89749352010-09-12 18:25:19 +01003566
Jesse Barnes79e53942008-11-07 14:24:08 -08003567 return true;
3568}
3569
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07003570static int valleyview_get_display_clock_speed(struct drm_device *dev)
3571{
3572 return 400000; /* FIXME */
3573}
3574
Jesse Barnese70236a2009-09-21 10:42:27 -07003575static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08003576{
Jesse Barnese70236a2009-09-21 10:42:27 -07003577 return 400000;
3578}
Jesse Barnes79e53942008-11-07 14:24:08 -08003579
Jesse Barnese70236a2009-09-21 10:42:27 -07003580static int i915_get_display_clock_speed(struct drm_device *dev)
3581{
3582 return 333000;
3583}
Jesse Barnes79e53942008-11-07 14:24:08 -08003584
Jesse Barnese70236a2009-09-21 10:42:27 -07003585static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
3586{
3587 return 200000;
3588}
Jesse Barnes79e53942008-11-07 14:24:08 -08003589
Jesse Barnese70236a2009-09-21 10:42:27 -07003590static int i915gm_get_display_clock_speed(struct drm_device *dev)
3591{
3592 u16 gcfgc = 0;
3593
3594 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
3595
3596 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08003597 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07003598 else {
3599 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
3600 case GC_DISPLAY_CLOCK_333_MHZ:
3601 return 333000;
3602 default:
3603 case GC_DISPLAY_CLOCK_190_200_MHZ:
3604 return 190000;
3605 }
3606 }
3607}
Jesse Barnes79e53942008-11-07 14:24:08 -08003608
Jesse Barnese70236a2009-09-21 10:42:27 -07003609static int i865_get_display_clock_speed(struct drm_device *dev)
3610{
3611 return 266000;
3612}
3613
3614static int i855_get_display_clock_speed(struct drm_device *dev)
3615{
3616 u16 hpllcc = 0;
3617 /* Assume that the hardware is in the high speed state. This
3618 * should be the default.
3619 */
3620 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
3621 case GC_CLOCK_133_200:
3622 case GC_CLOCK_100_200:
3623 return 200000;
3624 case GC_CLOCK_166_250:
3625 return 250000;
3626 case GC_CLOCK_100_133:
3627 return 133000;
3628 }
3629
3630 /* Shouldn't happen */
3631 return 0;
3632}
3633
3634static int i830_get_display_clock_speed(struct drm_device *dev)
3635{
3636 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08003637}
3638
Zhenyu Wang2c072452009-06-05 15:38:42 +08003639struct fdi_m_n {
3640 u32 tu;
3641 u32 gmch_m;
3642 u32 gmch_n;
3643 u32 link_m;
3644 u32 link_n;
3645};
3646
3647static void
3648fdi_reduce_ratio(u32 *num, u32 *den)
3649{
3650 while (*num > 0xffffff || *den > 0xffffff) {
3651 *num >>= 1;
3652 *den >>= 1;
3653 }
3654}
3655
Zhenyu Wang2c072452009-06-05 15:38:42 +08003656static void
Adam Jacksonf2b115e2009-12-03 17:14:42 -05003657ironlake_compute_m_n(int bits_per_pixel, int nlanes, int pixel_clock,
3658 int link_clock, struct fdi_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003659{
Zhenyu Wang2c072452009-06-05 15:38:42 +08003660 m_n->tu = 64; /* default size */
3661
Chris Wilson22ed1112010-12-04 01:01:29 +00003662 /* BUG_ON(pixel_clock > INT_MAX / 36); */
3663 m_n->gmch_m = bits_per_pixel * pixel_clock;
3664 m_n->gmch_n = link_clock * nlanes * 8;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003665 fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
3666
Chris Wilson22ed1112010-12-04 01:01:29 +00003667 m_n->link_m = pixel_clock;
3668 m_n->link_n = link_clock;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003669 fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
3670}
3671
3672
Shaohua Li7662c8b2009-06-26 11:23:55 +08003673struct intel_watermark_params {
3674 unsigned long fifo_size;
3675 unsigned long max_wm;
3676 unsigned long default_wm;
3677 unsigned long guard_size;
3678 unsigned long cacheline_size;
3679};
3680
Adam Jacksonf2b115e2009-12-03 17:14:42 -05003681/* Pineview has different values for various configs */
Chris Wilsond2102462011-01-24 17:43:27 +00003682static const struct intel_watermark_params pineview_display_wm = {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05003683 PINEVIEW_DISPLAY_FIFO,
3684 PINEVIEW_MAX_WM,
3685 PINEVIEW_DFT_WM,
3686 PINEVIEW_GUARD_WM,
3687 PINEVIEW_FIFO_LINE_SIZE
Shaohua Li7662c8b2009-06-26 11:23:55 +08003688};
Chris Wilsond2102462011-01-24 17:43:27 +00003689static const struct intel_watermark_params pineview_display_hplloff_wm = {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05003690 PINEVIEW_DISPLAY_FIFO,
3691 PINEVIEW_MAX_WM,
3692 PINEVIEW_DFT_HPLLOFF_WM,
3693 PINEVIEW_GUARD_WM,
3694 PINEVIEW_FIFO_LINE_SIZE
Shaohua Li7662c8b2009-06-26 11:23:55 +08003695};
Chris Wilsond2102462011-01-24 17:43:27 +00003696static const struct intel_watermark_params pineview_cursor_wm = {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05003697 PINEVIEW_CURSOR_FIFO,
3698 PINEVIEW_CURSOR_MAX_WM,
3699 PINEVIEW_CURSOR_DFT_WM,
3700 PINEVIEW_CURSOR_GUARD_WM,
3701 PINEVIEW_FIFO_LINE_SIZE,
Shaohua Li7662c8b2009-06-26 11:23:55 +08003702};
Chris Wilsond2102462011-01-24 17:43:27 +00003703static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05003704 PINEVIEW_CURSOR_FIFO,
3705 PINEVIEW_CURSOR_MAX_WM,
3706 PINEVIEW_CURSOR_DFT_WM,
3707 PINEVIEW_CURSOR_GUARD_WM,
3708 PINEVIEW_FIFO_LINE_SIZE
Shaohua Li7662c8b2009-06-26 11:23:55 +08003709};
Chris Wilsond2102462011-01-24 17:43:27 +00003710static const struct intel_watermark_params g4x_wm_info = {
Jesse Barnes0e442c62009-10-19 10:09:33 +09003711 G4X_FIFO_SIZE,
3712 G4X_MAX_WM,
3713 G4X_MAX_WM,
3714 2,
3715 G4X_FIFO_LINE_SIZE,
3716};
Chris Wilsond2102462011-01-24 17:43:27 +00003717static const struct intel_watermark_params g4x_cursor_wm_info = {
Zhao Yakui4fe5e612010-06-12 14:32:25 +08003718 I965_CURSOR_FIFO,
3719 I965_CURSOR_MAX_WM,
3720 I965_CURSOR_DFT_WM,
3721 2,
3722 G4X_FIFO_LINE_SIZE,
3723};
Jesse Barnesceb04242012-03-28 13:39:22 -07003724static const struct intel_watermark_params valleyview_wm_info = {
3725 VALLEYVIEW_FIFO_SIZE,
3726 VALLEYVIEW_MAX_WM,
3727 VALLEYVIEW_MAX_WM,
3728 2,
3729 G4X_FIFO_LINE_SIZE,
3730};
3731static const struct intel_watermark_params valleyview_cursor_wm_info = {
3732 I965_CURSOR_FIFO,
3733 VALLEYVIEW_CURSOR_MAX_WM,
3734 I965_CURSOR_DFT_WM,
3735 2,
3736 G4X_FIFO_LINE_SIZE,
3737};
Chris Wilsond2102462011-01-24 17:43:27 +00003738static const struct intel_watermark_params i965_cursor_wm_info = {
Zhao Yakui4fe5e612010-06-12 14:32:25 +08003739 I965_CURSOR_FIFO,
3740 I965_CURSOR_MAX_WM,
3741 I965_CURSOR_DFT_WM,
3742 2,
3743 I915_FIFO_LINE_SIZE,
3744};
Chris Wilsond2102462011-01-24 17:43:27 +00003745static const struct intel_watermark_params i945_wm_info = {
Shaohua Li7662c8b2009-06-26 11:23:55 +08003746 I945_FIFO_SIZE,
3747 I915_MAX_WM,
3748 1,
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003749 2,
3750 I915_FIFO_LINE_SIZE
3751};
Chris Wilsond2102462011-01-24 17:43:27 +00003752static const struct intel_watermark_params i915_wm_info = {
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003753 I915_FIFO_SIZE,
3754 I915_MAX_WM,
3755 1,
3756 2,
Shaohua Li7662c8b2009-06-26 11:23:55 +08003757 I915_FIFO_LINE_SIZE
3758};
Chris Wilsond2102462011-01-24 17:43:27 +00003759static const struct intel_watermark_params i855_wm_info = {
Shaohua Li7662c8b2009-06-26 11:23:55 +08003760 I855GM_FIFO_SIZE,
3761 I915_MAX_WM,
3762 1,
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003763 2,
Shaohua Li7662c8b2009-06-26 11:23:55 +08003764 I830_FIFO_LINE_SIZE
3765};
Chris Wilsond2102462011-01-24 17:43:27 +00003766static const struct intel_watermark_params i830_wm_info = {
Shaohua Li7662c8b2009-06-26 11:23:55 +08003767 I830_FIFO_SIZE,
3768 I915_MAX_WM,
3769 1,
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003770 2,
Shaohua Li7662c8b2009-06-26 11:23:55 +08003771 I830_FIFO_LINE_SIZE
3772};
3773
Chris Wilsond2102462011-01-24 17:43:27 +00003774static const struct intel_watermark_params ironlake_display_wm_info = {
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003775 ILK_DISPLAY_FIFO,
3776 ILK_DISPLAY_MAXWM,
3777 ILK_DISPLAY_DFTWM,
3778 2,
3779 ILK_FIFO_LINE_SIZE
3780};
Chris Wilsond2102462011-01-24 17:43:27 +00003781static const struct intel_watermark_params ironlake_cursor_wm_info = {
Zhao Yakuic936f442010-06-12 14:32:26 +08003782 ILK_CURSOR_FIFO,
3783 ILK_CURSOR_MAXWM,
3784 ILK_CURSOR_DFTWM,
3785 2,
3786 ILK_FIFO_LINE_SIZE
3787};
Chris Wilsond2102462011-01-24 17:43:27 +00003788static const struct intel_watermark_params ironlake_display_srwm_info = {
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003789 ILK_DISPLAY_SR_FIFO,
3790 ILK_DISPLAY_MAX_SRWM,
3791 ILK_DISPLAY_DFT_SRWM,
3792 2,
3793 ILK_FIFO_LINE_SIZE
3794};
Chris Wilsond2102462011-01-24 17:43:27 +00003795static const struct intel_watermark_params ironlake_cursor_srwm_info = {
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003796 ILK_CURSOR_SR_FIFO,
3797 ILK_CURSOR_MAX_SRWM,
3798 ILK_CURSOR_DFT_SRWM,
3799 2,
3800 ILK_FIFO_LINE_SIZE
3801};
3802
Chris Wilsond2102462011-01-24 17:43:27 +00003803static const struct intel_watermark_params sandybridge_display_wm_info = {
Yuanhan Liu13982612010-12-15 15:42:31 +08003804 SNB_DISPLAY_FIFO,
3805 SNB_DISPLAY_MAXWM,
3806 SNB_DISPLAY_DFTWM,
3807 2,
3808 SNB_FIFO_LINE_SIZE
3809};
Chris Wilsond2102462011-01-24 17:43:27 +00003810static const struct intel_watermark_params sandybridge_cursor_wm_info = {
Yuanhan Liu13982612010-12-15 15:42:31 +08003811 SNB_CURSOR_FIFO,
3812 SNB_CURSOR_MAXWM,
3813 SNB_CURSOR_DFTWM,
3814 2,
3815 SNB_FIFO_LINE_SIZE
3816};
Chris Wilsond2102462011-01-24 17:43:27 +00003817static const struct intel_watermark_params sandybridge_display_srwm_info = {
Yuanhan Liu13982612010-12-15 15:42:31 +08003818 SNB_DISPLAY_SR_FIFO,
3819 SNB_DISPLAY_MAX_SRWM,
3820 SNB_DISPLAY_DFT_SRWM,
3821 2,
3822 SNB_FIFO_LINE_SIZE
3823};
Chris Wilsond2102462011-01-24 17:43:27 +00003824static const struct intel_watermark_params sandybridge_cursor_srwm_info = {
Yuanhan Liu13982612010-12-15 15:42:31 +08003825 SNB_CURSOR_SR_FIFO,
3826 SNB_CURSOR_MAX_SRWM,
3827 SNB_CURSOR_DFT_SRWM,
3828 2,
3829 SNB_FIFO_LINE_SIZE
3830};
3831
3832
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003833/**
3834 * intel_calculate_wm - calculate watermark level
3835 * @clock_in_khz: pixel clock
3836 * @wm: chip FIFO params
3837 * @pixel_size: display pixel size
3838 * @latency_ns: memory latency for the platform
3839 *
3840 * Calculate the watermark level (the level at which the display plane will
3841 * start fetching from memory again). Each chip has a different display
3842 * FIFO size and allocation, so the caller needs to figure that out and pass
3843 * in the correct intel_watermark_params structure.
3844 *
3845 * As the pixel clock runs, the FIFO will be drained at a rate that depends
3846 * on the pixel size. When it reaches the watermark level, it'll start
3847 * fetching FIFO line sized based chunks from memory until the FIFO fills
3848 * past the watermark point. If the FIFO drains completely, a FIFO underrun
3849 * will occur, and a display engine hang could result.
3850 */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003851static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
Chris Wilsond2102462011-01-24 17:43:27 +00003852 const struct intel_watermark_params *wm,
3853 int fifo_size,
Shaohua Li7662c8b2009-06-26 11:23:55 +08003854 int pixel_size,
3855 unsigned long latency_ns)
3856{
Jesse Barnes390c4dd2009-07-16 13:01:01 -07003857 long entries_required, wm_size;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003858
Jesse Barnesd6604672009-09-11 12:25:56 -07003859 /*
3860 * Note: we need to make sure we don't overflow for various clock &
3861 * latency values.
3862 * clocks go from a few thousand to several hundred thousand.
3863 * latency is usually a few thousand
3864 */
3865 entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
3866 1000;
Chris Wilson8de9b312010-07-19 19:59:52 +01003867 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003868
Joe Perchesbbb0aef52011-04-17 20:35:52 -07003869 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003870
Chris Wilsond2102462011-01-24 17:43:27 +00003871 wm_size = fifo_size - (entries_required + wm->guard_size);
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003872
Joe Perchesbbb0aef52011-04-17 20:35:52 -07003873 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003874
Jesse Barnes390c4dd2009-07-16 13:01:01 -07003875 /* Don't promote wm_size to unsigned... */
3876 if (wm_size > (long)wm->max_wm)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003877 wm_size = wm->max_wm;
Chris Wilsonc3add4b2010-09-08 09:14:08 +01003878 if (wm_size <= 0)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003879 wm_size = wm->default_wm;
3880 return wm_size;
3881}
3882
3883struct cxsr_latency {
3884 int is_desktop;
Li Peng95534262010-05-18 18:58:44 +08003885 int is_ddr3;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003886 unsigned long fsb_freq;
3887 unsigned long mem_freq;
3888 unsigned long display_sr;
3889 unsigned long display_hpll_disable;
3890 unsigned long cursor_sr;
3891 unsigned long cursor_hpll_disable;
3892};
3893
Chris Wilson403c89f2010-08-04 15:25:31 +01003894static const struct cxsr_latency cxsr_latency_table[] = {
Li Peng95534262010-05-18 18:58:44 +08003895 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
3896 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
3897 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
3898 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
3899 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003900
Li Peng95534262010-05-18 18:58:44 +08003901 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
3902 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
3903 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
3904 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
3905 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003906
Li Peng95534262010-05-18 18:58:44 +08003907 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
3908 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
3909 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
3910 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
3911 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003912
Li Peng95534262010-05-18 18:58:44 +08003913 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
3914 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
3915 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
3916 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
3917 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003918
Li Peng95534262010-05-18 18:58:44 +08003919 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
3920 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
3921 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
3922 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
3923 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003924
Li Peng95534262010-05-18 18:58:44 +08003925 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
3926 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
3927 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
3928 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
3929 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003930};
3931
Chris Wilson403c89f2010-08-04 15:25:31 +01003932static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
3933 int is_ddr3,
3934 int fsb,
3935 int mem)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003936{
Chris Wilson403c89f2010-08-04 15:25:31 +01003937 const struct cxsr_latency *latency;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003938 int i;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003939
3940 if (fsb == 0 || mem == 0)
3941 return NULL;
3942
3943 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
3944 latency = &cxsr_latency_table[i];
3945 if (is_desktop == latency->is_desktop &&
Li Peng95534262010-05-18 18:58:44 +08003946 is_ddr3 == latency->is_ddr3 &&
Jaswinder Singh Rajputdecbbcd2009-09-12 23:15:07 +05303947 fsb == latency->fsb_freq && mem == latency->mem_freq)
3948 return latency;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003949 }
Jaswinder Singh Rajputdecbbcd2009-09-12 23:15:07 +05303950
Zhao Yakui28c97732009-10-09 11:39:41 +08003951 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Jaswinder Singh Rajputdecbbcd2009-09-12 23:15:07 +05303952
3953 return NULL;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003954}
3955
Adam Jacksonf2b115e2009-12-03 17:14:42 -05003956static void pineview_disable_cxsr(struct drm_device *dev)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003957{
3958 struct drm_i915_private *dev_priv = dev->dev_private;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003959
3960 /* deactivate cxsr */
Chris Wilson3e33d942010-08-04 11:17:25 +01003961 I915_WRITE(DSPFW3, I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003962}
3963
Jesse Barnesbcc24fb2009-08-31 10:24:31 -07003964/*
3965 * Latency for FIFO fetches is dependent on several factors:
3966 * - memory configuration (speed, channels)
3967 * - chipset
3968 * - current MCH state
3969 * It can be fairly high in some situations, so here we assume a fairly
3970 * pessimal value. It's a tradeoff between extra memory fetches (if we
3971 * set this value too high, the FIFO will fetch frequently to stay full)
3972 * and power consumption (set it too low to save power and we might see
3973 * FIFO underruns and display "flicker").
3974 *
3975 * A value of 5us seems to be a good balance; safe for very low end
3976 * platforms but not overly aggressive on lower latency configs.
3977 */
Tobias Klauser69e302a2009-12-23 14:14:34 +01003978static const int latency_ns = 5000;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003979
Jesse Barnese70236a2009-09-21 10:42:27 -07003980static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003981{
3982 struct drm_i915_private *dev_priv = dev->dev_private;
3983 uint32_t dsparb = I915_READ(DSPARB);
3984 int size;
3985
Chris Wilson8de9b312010-07-19 19:59:52 +01003986 size = dsparb & 0x7f;
3987 if (plane)
3988 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003989
Zhao Yakui28c97732009-10-09 11:39:41 +08003990 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
Chris Wilson5eddb702010-09-11 13:48:45 +01003991 plane ? "B" : "A", size);
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003992
3993 return size;
3994}
Shaohua Li7662c8b2009-06-26 11:23:55 +08003995
Jesse Barnese70236a2009-09-21 10:42:27 -07003996static int i85x_get_fifo_size(struct drm_device *dev, int plane)
3997{
3998 struct drm_i915_private *dev_priv = dev->dev_private;
3999 uint32_t dsparb = I915_READ(DSPARB);
4000 int size;
4001
Chris Wilson8de9b312010-07-19 19:59:52 +01004002 size = dsparb & 0x1ff;
4003 if (plane)
4004 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
Jesse Barnese70236a2009-09-21 10:42:27 -07004005 size >>= 1; /* Convert to cachelines */
4006
Zhao Yakui28c97732009-10-09 11:39:41 +08004007 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
Chris Wilson5eddb702010-09-11 13:48:45 +01004008 plane ? "B" : "A", size);
Jesse Barnese70236a2009-09-21 10:42:27 -07004009
4010 return size;
4011}
4012
4013static int i845_get_fifo_size(struct drm_device *dev, int plane)
4014{
4015 struct drm_i915_private *dev_priv = dev->dev_private;
4016 uint32_t dsparb = I915_READ(DSPARB);
4017 int size;
4018
4019 size = dsparb & 0x7f;
4020 size >>= 2; /* Convert to cachelines */
4021
Zhao Yakui28c97732009-10-09 11:39:41 +08004022 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
Chris Wilson5eddb702010-09-11 13:48:45 +01004023 plane ? "B" : "A",
4024 size);
Jesse Barnese70236a2009-09-21 10:42:27 -07004025
4026 return size;
4027}
4028
4029static int i830_get_fifo_size(struct drm_device *dev, int plane)
4030{
4031 struct drm_i915_private *dev_priv = dev->dev_private;
4032 uint32_t dsparb = I915_READ(DSPARB);
4033 int size;
4034
4035 size = dsparb & 0x7f;
4036 size >>= 1; /* Convert to cachelines */
4037
Zhao Yakui28c97732009-10-09 11:39:41 +08004038 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
Chris Wilson5eddb702010-09-11 13:48:45 +01004039 plane ? "B" : "A", size);
Jesse Barnese70236a2009-09-21 10:42:27 -07004040
4041 return size;
4042}
4043
Chris Wilsond2102462011-01-24 17:43:27 +00004044static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
4045{
4046 struct drm_crtc *crtc, *enabled = NULL;
4047
4048 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
4049 if (crtc->enabled && crtc->fb) {
4050 if (enabled)
4051 return NULL;
4052 enabled = crtc;
4053 }
4054 }
4055
4056 return enabled;
4057}
4058
4059static void pineview_update_wm(struct drm_device *dev)
Zhao Yakuid4294342010-03-22 22:45:36 +08004060{
4061 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond2102462011-01-24 17:43:27 +00004062 struct drm_crtc *crtc;
Chris Wilson403c89f2010-08-04 15:25:31 +01004063 const struct cxsr_latency *latency;
Zhao Yakuid4294342010-03-22 22:45:36 +08004064 u32 reg;
4065 unsigned long wm;
Zhao Yakuid4294342010-03-22 22:45:36 +08004066
Chris Wilson403c89f2010-08-04 15:25:31 +01004067 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
Li Peng95534262010-05-18 18:58:44 +08004068 dev_priv->fsb_freq, dev_priv->mem_freq);
Zhao Yakuid4294342010-03-22 22:45:36 +08004069 if (!latency) {
4070 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
4071 pineview_disable_cxsr(dev);
4072 return;
4073 }
4074
Chris Wilsond2102462011-01-24 17:43:27 +00004075 crtc = single_enabled_crtc(dev);
4076 if (crtc) {
4077 int clock = crtc->mode.clock;
4078 int pixel_size = crtc->fb->bits_per_pixel / 8;
Zhao Yakuid4294342010-03-22 22:45:36 +08004079
4080 /* Display SR */
Chris Wilsond2102462011-01-24 17:43:27 +00004081 wm = intel_calculate_wm(clock, &pineview_display_wm,
4082 pineview_display_wm.fifo_size,
Zhao Yakuid4294342010-03-22 22:45:36 +08004083 pixel_size, latency->display_sr);
4084 reg = I915_READ(DSPFW1);
4085 reg &= ~DSPFW_SR_MASK;
4086 reg |= wm << DSPFW_SR_SHIFT;
4087 I915_WRITE(DSPFW1, reg);
4088 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
4089
4090 /* cursor SR */
Chris Wilsond2102462011-01-24 17:43:27 +00004091 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
4092 pineview_display_wm.fifo_size,
Zhao Yakuid4294342010-03-22 22:45:36 +08004093 pixel_size, latency->cursor_sr);
4094 reg = I915_READ(DSPFW3);
4095 reg &= ~DSPFW_CURSOR_SR_MASK;
4096 reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
4097 I915_WRITE(DSPFW3, reg);
4098
4099 /* Display HPLL off SR */
Chris Wilsond2102462011-01-24 17:43:27 +00004100 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
4101 pineview_display_hplloff_wm.fifo_size,
Zhao Yakuid4294342010-03-22 22:45:36 +08004102 pixel_size, latency->display_hpll_disable);
4103 reg = I915_READ(DSPFW3);
4104 reg &= ~DSPFW_HPLL_SR_MASK;
4105 reg |= wm & DSPFW_HPLL_SR_MASK;
4106 I915_WRITE(DSPFW3, reg);
4107
4108 /* cursor HPLL off SR */
Chris Wilsond2102462011-01-24 17:43:27 +00004109 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
4110 pineview_display_hplloff_wm.fifo_size,
Zhao Yakuid4294342010-03-22 22:45:36 +08004111 pixel_size, latency->cursor_hpll_disable);
4112 reg = I915_READ(DSPFW3);
4113 reg &= ~DSPFW_HPLL_CURSOR_MASK;
4114 reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
4115 I915_WRITE(DSPFW3, reg);
4116 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
4117
4118 /* activate cxsr */
Chris Wilson3e33d942010-08-04 11:17:25 +01004119 I915_WRITE(DSPFW3,
4120 I915_READ(DSPFW3) | PINEVIEW_SELF_REFRESH_EN);
Zhao Yakuid4294342010-03-22 22:45:36 +08004121 DRM_DEBUG_KMS("Self-refresh is enabled\n");
4122 } else {
4123 pineview_disable_cxsr(dev);
4124 DRM_DEBUG_KMS("Self-refresh is disabled\n");
4125 }
4126}
4127
Chris Wilson417ae142011-01-19 15:04:42 +00004128static bool g4x_compute_wm0(struct drm_device *dev,
4129 int plane,
4130 const struct intel_watermark_params *display,
4131 int display_latency_ns,
4132 const struct intel_watermark_params *cursor,
4133 int cursor_latency_ns,
4134 int *plane_wm,
4135 int *cursor_wm)
Jesse Barnes652c3932009-08-17 13:31:43 -07004136{
Chris Wilson417ae142011-01-19 15:04:42 +00004137 struct drm_crtc *crtc;
4138 int htotal, hdisplay, clock, pixel_size;
4139 int line_time_us, line_count;
4140 int entries, tlb_miss;
Jesse Barnes652c3932009-08-17 13:31:43 -07004141
Chris Wilson417ae142011-01-19 15:04:42 +00004142 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilson5c72d062011-04-13 09:28:23 +01004143 if (crtc->fb == NULL || !crtc->enabled) {
4144 *cursor_wm = cursor->guard_size;
4145 *plane_wm = display->guard_size;
Chris Wilson417ae142011-01-19 15:04:42 +00004146 return false;
Chris Wilson5c72d062011-04-13 09:28:23 +01004147 }
Jesse Barnes0e442c62009-10-19 10:09:33 +09004148
Chris Wilson417ae142011-01-19 15:04:42 +00004149 htotal = crtc->mode.htotal;
4150 hdisplay = crtc->mode.hdisplay;
4151 clock = crtc->mode.clock;
4152 pixel_size = crtc->fb->bits_per_pixel / 8;
Jesse Barnes0e442c62009-10-19 10:09:33 +09004153
Chris Wilson417ae142011-01-19 15:04:42 +00004154 /* Use the small buffer method to calculate plane watermark */
4155 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
4156 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
4157 if (tlb_miss > 0)
4158 entries += tlb_miss;
4159 entries = DIV_ROUND_UP(entries, display->cacheline_size);
4160 *plane_wm = entries + display->guard_size;
4161 if (*plane_wm > (int)display->max_wm)
4162 *plane_wm = display->max_wm;
Jesse Barnes0e442c62009-10-19 10:09:33 +09004163
Chris Wilson417ae142011-01-19 15:04:42 +00004164 /* Use the large buffer method to calculate cursor watermark */
4165 line_time_us = ((htotal * 1000) / clock);
4166 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
4167 entries = line_count * 64 * pixel_size;
4168 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
4169 if (tlb_miss > 0)
4170 entries += tlb_miss;
4171 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
4172 *cursor_wm = entries + cursor->guard_size;
4173 if (*cursor_wm > (int)cursor->max_wm)
4174 *cursor_wm = (int)cursor->max_wm;
Jesse Barnes0e442c62009-10-19 10:09:33 +09004175
Chris Wilson417ae142011-01-19 15:04:42 +00004176 return true;
4177}
Jesse Barnes0e442c62009-10-19 10:09:33 +09004178
Chris Wilson417ae142011-01-19 15:04:42 +00004179/*
4180 * Check the wm result.
4181 *
4182 * If any calculated watermark values is larger than the maximum value that
4183 * can be programmed into the associated watermark register, that watermark
4184 * must be disabled.
4185 */
4186static bool g4x_check_srwm(struct drm_device *dev,
4187 int display_wm, int cursor_wm,
4188 const struct intel_watermark_params *display,
4189 const struct intel_watermark_params *cursor)
4190{
4191 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
4192 display_wm, cursor_wm);
Jesse Barnes0e442c62009-10-19 10:09:33 +09004193
Chris Wilson417ae142011-01-19 15:04:42 +00004194 if (display_wm > display->max_wm) {
Joe Perchesbbb0aef52011-04-17 20:35:52 -07004195 DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
Chris Wilson417ae142011-01-19 15:04:42 +00004196 display_wm, display->max_wm);
4197 return false;
Jesse Barnes0e442c62009-10-19 10:09:33 +09004198 }
4199
Chris Wilson417ae142011-01-19 15:04:42 +00004200 if (cursor_wm > cursor->max_wm) {
Joe Perchesbbb0aef52011-04-17 20:35:52 -07004201 DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
Chris Wilson417ae142011-01-19 15:04:42 +00004202 cursor_wm, cursor->max_wm);
4203 return false;
4204 }
Jesse Barnes0e442c62009-10-19 10:09:33 +09004205
Chris Wilson417ae142011-01-19 15:04:42 +00004206 if (!(display_wm || cursor_wm)) {
4207 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
4208 return false;
4209 }
Jesse Barnes0e442c62009-10-19 10:09:33 +09004210
Chris Wilson417ae142011-01-19 15:04:42 +00004211 return true;
4212}
4213
4214static bool g4x_compute_srwm(struct drm_device *dev,
Chris Wilsond2102462011-01-24 17:43:27 +00004215 int plane,
4216 int latency_ns,
Chris Wilson417ae142011-01-19 15:04:42 +00004217 const struct intel_watermark_params *display,
4218 const struct intel_watermark_params *cursor,
4219 int *display_wm, int *cursor_wm)
4220{
Chris Wilsond2102462011-01-24 17:43:27 +00004221 struct drm_crtc *crtc;
4222 int hdisplay, htotal, pixel_size, clock;
Chris Wilson417ae142011-01-19 15:04:42 +00004223 unsigned long line_time_us;
4224 int line_count, line_size;
4225 int small, large;
4226 int entries;
4227
4228 if (!latency_ns) {
4229 *display_wm = *cursor_wm = 0;
4230 return false;
4231 }
4232
Chris Wilsond2102462011-01-24 17:43:27 +00004233 crtc = intel_get_crtc_for_plane(dev, plane);
4234 hdisplay = crtc->mode.hdisplay;
4235 htotal = crtc->mode.htotal;
4236 clock = crtc->mode.clock;
4237 pixel_size = crtc->fb->bits_per_pixel / 8;
4238
Chris Wilson417ae142011-01-19 15:04:42 +00004239 line_time_us = (htotal * 1000) / clock;
4240 line_count = (latency_ns / line_time_us + 1000) / 1000;
4241 line_size = hdisplay * pixel_size;
4242
4243 /* Use the minimum of the small and large buffer method for primary */
4244 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
4245 large = line_count * line_size;
4246
4247 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
4248 *display_wm = entries + display->guard_size;
4249
4250 /* calculate the self-refresh watermark for display cursor */
4251 entries = line_count * pixel_size * 64;
4252 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
4253 *cursor_wm = entries + cursor->guard_size;
4254
4255 return g4x_check_srwm(dev,
4256 *display_wm, *cursor_wm,
4257 display, cursor);
4258}
4259
Gajanan Bhat12a3c052012-03-28 13:39:30 -07004260static bool vlv_compute_drain_latency(struct drm_device *dev,
4261 int plane,
4262 int *plane_prec_mult,
4263 int *plane_dl,
4264 int *cursor_prec_mult,
4265 int *cursor_dl)
4266{
4267 struct drm_crtc *crtc;
4268 int clock, pixel_size;
4269 int entries;
4270
4271 crtc = intel_get_crtc_for_plane(dev, plane);
4272 if (crtc->fb == NULL || !crtc->enabled)
4273 return false;
4274
4275 clock = crtc->mode.clock; /* VESA DOT Clock */
4276 pixel_size = crtc->fb->bits_per_pixel / 8; /* BPP */
4277
4278 entries = (clock / 1000) * pixel_size;
4279 *plane_prec_mult = (entries > 256) ?
4280 DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_16;
4281 *plane_dl = (64 * (*plane_prec_mult) * 4) / ((clock / 1000) *
4282 pixel_size);
4283
4284 entries = (clock / 1000) * 4; /* BPP is always 4 for cursor */
4285 *cursor_prec_mult = (entries > 256) ?
4286 DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_16;
4287 *cursor_dl = (64 * (*cursor_prec_mult) * 4) / ((clock / 1000) * 4);
4288
4289 return true;
4290}
4291
4292/*
4293 * Update drain latency registers of memory arbiter
4294 *
4295 * Valleyview SoC has a new memory arbiter and needs drain latency registers
4296 * to be programmed. Each plane has a drain latency multiplier and a drain
4297 * latency value.
4298 */
4299
4300static void vlv_update_drain_latency(struct drm_device *dev)
4301{
4302 struct drm_i915_private *dev_priv = dev->dev_private;
4303 int planea_prec, planea_dl, planeb_prec, planeb_dl;
4304 int cursora_prec, cursora_dl, cursorb_prec, cursorb_dl;
4305 int plane_prec_mult, cursor_prec_mult; /* Precision multiplier is
4306 either 16 or 32 */
4307
4308 /* For plane A, Cursor A */
4309 if (vlv_compute_drain_latency(dev, 0, &plane_prec_mult, &planea_dl,
4310 &cursor_prec_mult, &cursora_dl)) {
4311 cursora_prec = (cursor_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
4312 DDL_CURSORA_PRECISION_32 : DDL_CURSORA_PRECISION_16;
4313 planea_prec = (plane_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
4314 DDL_PLANEA_PRECISION_32 : DDL_PLANEA_PRECISION_16;
4315
4316 I915_WRITE(VLV_DDL1, cursora_prec |
4317 (cursora_dl << DDL_CURSORA_SHIFT) |
4318 planea_prec | planea_dl);
4319 }
4320
4321 /* For plane B, Cursor B */
4322 if (vlv_compute_drain_latency(dev, 1, &plane_prec_mult, &planeb_dl,
4323 &cursor_prec_mult, &cursorb_dl)) {
4324 cursorb_prec = (cursor_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
4325 DDL_CURSORB_PRECISION_32 : DDL_CURSORB_PRECISION_16;
4326 planeb_prec = (plane_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
4327 DDL_PLANEB_PRECISION_32 : DDL_PLANEB_PRECISION_16;
4328
4329 I915_WRITE(VLV_DDL2, cursorb_prec |
4330 (cursorb_dl << DDL_CURSORB_SHIFT) |
4331 planeb_prec | planeb_dl);
4332 }
4333}
4334
Yuanhan Liu7ccb4a52011-03-18 07:37:35 +00004335#define single_plane_enabled(mask) is_power_of_2(mask)
Chris Wilsond2102462011-01-24 17:43:27 +00004336
Jesse Barnesceb04242012-03-28 13:39:22 -07004337static void valleyview_update_wm(struct drm_device *dev)
4338{
4339 static const int sr_latency_ns = 12000;
4340 struct drm_i915_private *dev_priv = dev->dev_private;
4341 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
4342 int plane_sr, cursor_sr;
4343 unsigned int enabled = 0;
4344
Gajanan Bhat12a3c052012-03-28 13:39:30 -07004345 vlv_update_drain_latency(dev);
4346
Jesse Barnesceb04242012-03-28 13:39:22 -07004347 if (g4x_compute_wm0(dev, 0,
4348 &valleyview_wm_info, latency_ns,
4349 &valleyview_cursor_wm_info, latency_ns,
4350 &planea_wm, &cursora_wm))
4351 enabled |= 1;
4352
4353 if (g4x_compute_wm0(dev, 1,
4354 &valleyview_wm_info, latency_ns,
4355 &valleyview_cursor_wm_info, latency_ns,
4356 &planeb_wm, &cursorb_wm))
4357 enabled |= 2;
4358
4359 plane_sr = cursor_sr = 0;
4360 if (single_plane_enabled(enabled) &&
4361 g4x_compute_srwm(dev, ffs(enabled) - 1,
4362 sr_latency_ns,
4363 &valleyview_wm_info,
4364 &valleyview_cursor_wm_info,
4365 &plane_sr, &cursor_sr))
4366 I915_WRITE(FW_BLC_SELF_VLV, FW_CSPWRDWNEN);
4367 else
4368 I915_WRITE(FW_BLC_SELF_VLV,
4369 I915_READ(FW_BLC_SELF_VLV) & ~FW_CSPWRDWNEN);
4370
4371 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
4372 planea_wm, cursora_wm,
4373 planeb_wm, cursorb_wm,
4374 plane_sr, cursor_sr);
4375
4376 I915_WRITE(DSPFW1,
4377 (plane_sr << DSPFW_SR_SHIFT) |
4378 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
4379 (planeb_wm << DSPFW_PLANEB_SHIFT) |
4380 planea_wm);
4381 I915_WRITE(DSPFW2,
4382 (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
4383 (cursora_wm << DSPFW_CURSORA_SHIFT));
4384 I915_WRITE(DSPFW3,
4385 (I915_READ(DSPFW3) | (cursor_sr << DSPFW_CURSOR_SR_SHIFT)));
4386}
4387
Chris Wilsond2102462011-01-24 17:43:27 +00004388static void g4x_update_wm(struct drm_device *dev)
Chris Wilson417ae142011-01-19 15:04:42 +00004389{
4390 static const int sr_latency_ns = 12000;
4391 struct drm_i915_private *dev_priv = dev->dev_private;
4392 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
Chris Wilsond2102462011-01-24 17:43:27 +00004393 int plane_sr, cursor_sr;
4394 unsigned int enabled = 0;
Chris Wilson417ae142011-01-19 15:04:42 +00004395
4396 if (g4x_compute_wm0(dev, 0,
4397 &g4x_wm_info, latency_ns,
4398 &g4x_cursor_wm_info, latency_ns,
4399 &planea_wm, &cursora_wm))
Chris Wilsond2102462011-01-24 17:43:27 +00004400 enabled |= 1;
Chris Wilson417ae142011-01-19 15:04:42 +00004401
4402 if (g4x_compute_wm0(dev, 1,
4403 &g4x_wm_info, latency_ns,
4404 &g4x_cursor_wm_info, latency_ns,
4405 &planeb_wm, &cursorb_wm))
Chris Wilsond2102462011-01-24 17:43:27 +00004406 enabled |= 2;
Chris Wilson417ae142011-01-19 15:04:42 +00004407
4408 plane_sr = cursor_sr = 0;
Chris Wilsond2102462011-01-24 17:43:27 +00004409 if (single_plane_enabled(enabled) &&
4410 g4x_compute_srwm(dev, ffs(enabled) - 1,
4411 sr_latency_ns,
Chris Wilson417ae142011-01-19 15:04:42 +00004412 &g4x_wm_info,
4413 &g4x_cursor_wm_info,
4414 &plane_sr, &cursor_sr))
4415 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
4416 else
4417 I915_WRITE(FW_BLC_SELF,
4418 I915_READ(FW_BLC_SELF) & ~FW_BLC_SELF_EN);
4419
Chris Wilson308977a2011-02-02 10:41:20 +00004420 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
4421 planea_wm, cursora_wm,
4422 planeb_wm, cursorb_wm,
4423 plane_sr, cursor_sr);
Chris Wilson417ae142011-01-19 15:04:42 +00004424
4425 I915_WRITE(DSPFW1,
4426 (plane_sr << DSPFW_SR_SHIFT) |
Jesse Barnes0e442c62009-10-19 10:09:33 +09004427 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
Chris Wilson417ae142011-01-19 15:04:42 +00004428 (planeb_wm << DSPFW_PLANEB_SHIFT) |
4429 planea_wm);
4430 I915_WRITE(DSPFW2,
4431 (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
Jesse Barnes0e442c62009-10-19 10:09:33 +09004432 (cursora_wm << DSPFW_CURSORA_SHIFT));
4433 /* HPLL off in SR has some issues on G4x... disable it */
Chris Wilson417ae142011-01-19 15:04:42 +00004434 I915_WRITE(DSPFW3,
4435 (I915_READ(DSPFW3) & ~DSPFW_HPLL_SR_EN) |
Jesse Barnes0e442c62009-10-19 10:09:33 +09004436 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Jesse Barnes652c3932009-08-17 13:31:43 -07004437}
4438
Chris Wilsond2102462011-01-24 17:43:27 +00004439static void i965_update_wm(struct drm_device *dev)
Shaohua Li7662c8b2009-06-26 11:23:55 +08004440{
4441 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond2102462011-01-24 17:43:27 +00004442 struct drm_crtc *crtc;
4443 int srwm = 1;
Zhao Yakui4fe5e612010-06-12 14:32:25 +08004444 int cursor_sr = 16;
Shaohua Li7662c8b2009-06-26 11:23:55 +08004445
Jesse Barnes1dc75462009-10-19 10:08:17 +09004446 /* Calc sr entries for one plane configs */
Chris Wilsond2102462011-01-24 17:43:27 +00004447 crtc = single_enabled_crtc(dev);
4448 if (crtc) {
Jesse Barnes1dc75462009-10-19 10:08:17 +09004449 /* self-refresh has much higher latency */
Tobias Klauser69e302a2009-12-23 14:14:34 +01004450 static const int sr_latency_ns = 12000;
Chris Wilsond2102462011-01-24 17:43:27 +00004451 int clock = crtc->mode.clock;
4452 int htotal = crtc->mode.htotal;
4453 int hdisplay = crtc->mode.hdisplay;
4454 int pixel_size = crtc->fb->bits_per_pixel / 8;
4455 unsigned long line_time_us;
4456 int entries;
Jesse Barnes1dc75462009-10-19 10:08:17 +09004457
Chris Wilsond2102462011-01-24 17:43:27 +00004458 line_time_us = ((htotal * 1000) / clock);
Jesse Barnes1dc75462009-10-19 10:08:17 +09004459
4460 /* Use ns/us then divide to preserve precision */
Chris Wilsond2102462011-01-24 17:43:27 +00004461 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
4462 pixel_size * hdisplay;
4463 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
Chris Wilsond2102462011-01-24 17:43:27 +00004464 srwm = I965_FIFO_SIZE - entries;
Jesse Barnes1dc75462009-10-19 10:08:17 +09004465 if (srwm < 0)
4466 srwm = 1;
Zhao Yakui1b07e042010-06-12 14:32:24 +08004467 srwm &= 0x1ff;
Chris Wilson308977a2011-02-02 10:41:20 +00004468 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
4469 entries, srwm);
Zhao Yakui4fe5e612010-06-12 14:32:25 +08004470
Chris Wilsond2102462011-01-24 17:43:27 +00004471 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Chris Wilson5eddb702010-09-11 13:48:45 +01004472 pixel_size * 64;
Chris Wilsond2102462011-01-24 17:43:27 +00004473 entries = DIV_ROUND_UP(entries,
Chris Wilson8de9b312010-07-19 19:59:52 +01004474 i965_cursor_wm_info.cacheline_size);
Zhao Yakui4fe5e612010-06-12 14:32:25 +08004475 cursor_sr = i965_cursor_wm_info.fifo_size -
Chris Wilsond2102462011-01-24 17:43:27 +00004476 (entries + i965_cursor_wm_info.guard_size);
Zhao Yakui4fe5e612010-06-12 14:32:25 +08004477
4478 if (cursor_sr > i965_cursor_wm_info.max_wm)
4479 cursor_sr = i965_cursor_wm_info.max_wm;
4480
4481 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
4482 "cursor %d\n", srwm, cursor_sr);
4483
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004484 if (IS_CRESTLINE(dev))
Jesse Barnesadcdbc62010-06-30 13:49:37 -07004485 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
David John33c5fd12010-01-27 15:19:08 +05304486 } else {
4487 /* Turn off self refresh if both pipes are enabled */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004488 if (IS_CRESTLINE(dev))
Jesse Barnesadcdbc62010-06-30 13:49:37 -07004489 I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
4490 & ~FW_BLC_SELF_EN);
Jesse Barnes1dc75462009-10-19 10:08:17 +09004491 }
4492
4493 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
4494 srwm);
Shaohua Li7662c8b2009-06-26 11:23:55 +08004495
4496 /* 965 has limitations... */
Chris Wilson417ae142011-01-19 15:04:42 +00004497 I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
4498 (8 << 16) | (8 << 8) | (8 << 0));
Shaohua Li7662c8b2009-06-26 11:23:55 +08004499 I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
Zhao Yakui4fe5e612010-06-12 14:32:25 +08004500 /* update cursor SR watermark */
4501 I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Shaohua Li7662c8b2009-06-26 11:23:55 +08004502}
4503
Chris Wilsond2102462011-01-24 17:43:27 +00004504static void i9xx_update_wm(struct drm_device *dev)
Shaohua Li7662c8b2009-06-26 11:23:55 +08004505{
4506 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond2102462011-01-24 17:43:27 +00004507 const struct intel_watermark_params *wm_info;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07004508 uint32_t fwater_lo;
4509 uint32_t fwater_hi;
Chris Wilsond2102462011-01-24 17:43:27 +00004510 int cwm, srwm = 1;
4511 int fifo_size;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07004512 int planea_wm, planeb_wm;
Chris Wilsond2102462011-01-24 17:43:27 +00004513 struct drm_crtc *crtc, *enabled = NULL;
Shaohua Li7662c8b2009-06-26 11:23:55 +08004514
Chris Wilson72557b42011-01-31 10:29:55 +00004515 if (IS_I945GM(dev))
Chris Wilsond2102462011-01-24 17:43:27 +00004516 wm_info = &i945_wm_info;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004517 else if (!IS_GEN2(dev))
Chris Wilsond2102462011-01-24 17:43:27 +00004518 wm_info = &i915_wm_info;
Shaohua Li7662c8b2009-06-26 11:23:55 +08004519 else
Chris Wilsond2102462011-01-24 17:43:27 +00004520 wm_info = &i855_wm_info;
Shaohua Li7662c8b2009-06-26 11:23:55 +08004521
Chris Wilsond2102462011-01-24 17:43:27 +00004522 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
4523 crtc = intel_get_crtc_for_plane(dev, 0);
4524 if (crtc->enabled && crtc->fb) {
4525 planea_wm = intel_calculate_wm(crtc->mode.clock,
4526 wm_info, fifo_size,
4527 crtc->fb->bits_per_pixel / 8,
4528 latency_ns);
4529 enabled = crtc;
4530 } else
4531 planea_wm = fifo_size - wm_info->guard_size;
Shaohua Li7662c8b2009-06-26 11:23:55 +08004532
Chris Wilsond2102462011-01-24 17:43:27 +00004533 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
4534 crtc = intel_get_crtc_for_plane(dev, 1);
4535 if (crtc->enabled && crtc->fb) {
4536 planeb_wm = intel_calculate_wm(crtc->mode.clock,
4537 wm_info, fifo_size,
4538 crtc->fb->bits_per_pixel / 8,
4539 latency_ns);
4540 if (enabled == NULL)
4541 enabled = crtc;
4542 else
4543 enabled = NULL;
4544 } else
4545 planeb_wm = fifo_size - wm_info->guard_size;
Shaohua Li7662c8b2009-06-26 11:23:55 +08004546
Zhao Yakui28c97732009-10-09 11:39:41 +08004547 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
Shaohua Li7662c8b2009-06-26 11:23:55 +08004548
4549 /*
4550 * Overlay gets an aggressive default since video jitter is bad.
4551 */
4552 cwm = 2;
4553
Alexander Lam18b21902011-01-03 13:28:56 -05004554 /* Play safe and disable self-refresh before adjusting watermarks. */
4555 if (IS_I945G(dev) || IS_I945GM(dev))
4556 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | 0);
4557 else if (IS_I915GM(dev))
4558 I915_WRITE(INSTPM, I915_READ(INSTPM) & ~INSTPM_SELF_EN);
4559
Jesse Barnesdff33cf2009-07-14 10:15:56 -07004560 /* Calc sr entries for one plane configs */
Chris Wilsond2102462011-01-24 17:43:27 +00004561 if (HAS_FW_BLC(dev) && enabled) {
Jesse Barnesdff33cf2009-07-14 10:15:56 -07004562 /* self-refresh has much higher latency */
Tobias Klauser69e302a2009-12-23 14:14:34 +01004563 static const int sr_latency_ns = 6000;
Chris Wilsond2102462011-01-24 17:43:27 +00004564 int clock = enabled->mode.clock;
4565 int htotal = enabled->mode.htotal;
4566 int hdisplay = enabled->mode.hdisplay;
4567 int pixel_size = enabled->fb->bits_per_pixel / 8;
4568 unsigned long line_time_us;
4569 int entries;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07004570
Chris Wilsond2102462011-01-24 17:43:27 +00004571 line_time_us = (htotal * 1000) / clock;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07004572
4573 /* Use ns/us then divide to preserve precision */
Chris Wilsond2102462011-01-24 17:43:27 +00004574 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
4575 pixel_size * hdisplay;
4576 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
4577 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
4578 srwm = wm_info->fifo_size - entries;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07004579 if (srwm < 0)
4580 srwm = 1;
Li Pengee980b82010-01-27 19:01:11 +08004581
4582 if (IS_I945G(dev) || IS_I945GM(dev))
Alexander Lam18b21902011-01-03 13:28:56 -05004583 I915_WRITE(FW_BLC_SELF,
4584 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
4585 else if (IS_I915GM(dev))
Li Pengee980b82010-01-27 19:01:11 +08004586 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
Shaohua Li7662c8b2009-06-26 11:23:55 +08004587 }
4588
Zhao Yakui28c97732009-10-09 11:39:41 +08004589 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
Chris Wilson5eddb702010-09-11 13:48:45 +01004590 planea_wm, planeb_wm, cwm, srwm);
Shaohua Li7662c8b2009-06-26 11:23:55 +08004591
Jesse Barnesdff33cf2009-07-14 10:15:56 -07004592 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
4593 fwater_hi = (cwm & 0x1f);
4594
4595 /* Set request length to 8 cachelines per fetch */
4596 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
4597 fwater_hi = fwater_hi | (1 << 8);
Shaohua Li7662c8b2009-06-26 11:23:55 +08004598
4599 I915_WRITE(FW_BLC, fwater_lo);
4600 I915_WRITE(FW_BLC2, fwater_hi);
Alexander Lam18b21902011-01-03 13:28:56 -05004601
Chris Wilsond2102462011-01-24 17:43:27 +00004602 if (HAS_FW_BLC(dev)) {
4603 if (enabled) {
4604 if (IS_I945G(dev) || IS_I945GM(dev))
4605 I915_WRITE(FW_BLC_SELF,
4606 FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
4607 else if (IS_I915GM(dev))
4608 I915_WRITE(INSTPM, I915_READ(INSTPM) | INSTPM_SELF_EN);
4609 DRM_DEBUG_KMS("memory self refresh enabled\n");
4610 } else
4611 DRM_DEBUG_KMS("memory self refresh disabled\n");
4612 }
Shaohua Li7662c8b2009-06-26 11:23:55 +08004613}
4614
Chris Wilsond2102462011-01-24 17:43:27 +00004615static void i830_update_wm(struct drm_device *dev)
Shaohua Li7662c8b2009-06-26 11:23:55 +08004616{
4617 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond2102462011-01-24 17:43:27 +00004618 struct drm_crtc *crtc;
4619 uint32_t fwater_lo;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07004620 int planea_wm;
Shaohua Li7662c8b2009-06-26 11:23:55 +08004621
Chris Wilsond2102462011-01-24 17:43:27 +00004622 crtc = single_enabled_crtc(dev);
4623 if (crtc == NULL)
4624 return;
Shaohua Li7662c8b2009-06-26 11:23:55 +08004625
Chris Wilsond2102462011-01-24 17:43:27 +00004626 planea_wm = intel_calculate_wm(crtc->mode.clock, &i830_wm_info,
4627 dev_priv->display.get_fifo_size(dev, 0),
4628 crtc->fb->bits_per_pixel / 8,
4629 latency_ns);
4630 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
Jesse Barnesf3601322009-07-22 12:54:59 -07004631 fwater_lo |= (3<<8) | planea_wm;
4632
Zhao Yakui28c97732009-10-09 11:39:41 +08004633 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
Shaohua Li7662c8b2009-06-26 11:23:55 +08004634
4635 I915_WRITE(FW_BLC, fwater_lo);
4636}
4637
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08004638#define ILK_LP0_PLANE_LATENCY 700
Zhao Yakuic936f442010-06-12 14:32:26 +08004639#define ILK_LP0_CURSOR_LATENCY 1300
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08004640
Jesse Barnesb79d4992010-12-21 13:10:23 -08004641/*
4642 * Check the wm result.
4643 *
4644 * If any calculated watermark values is larger than the maximum value that
4645 * can be programmed into the associated watermark register, that watermark
4646 * must be disabled.
4647 */
4648static bool ironlake_check_srwm(struct drm_device *dev, int level,
4649 int fbc_wm, int display_wm, int cursor_wm,
4650 const struct intel_watermark_params *display,
4651 const struct intel_watermark_params *cursor)
4652{
4653 struct drm_i915_private *dev_priv = dev->dev_private;
4654
4655 DRM_DEBUG_KMS("watermark %d: display plane %d, fbc lines %d,"
4656 " cursor %d\n", level, display_wm, fbc_wm, cursor_wm);
4657
4658 if (fbc_wm > SNB_FBC_MAX_SRWM) {
4659 DRM_DEBUG_KMS("fbc watermark(%d) is too large(%d), disabling wm%d+\n",
4660 fbc_wm, SNB_FBC_MAX_SRWM, level);
4661
4662 /* fbc has it's own way to disable FBC WM */
4663 I915_WRITE(DISP_ARB_CTL,
4664 I915_READ(DISP_ARB_CTL) | DISP_FBC_WM_DIS);
4665 return false;
4666 }
4667
4668 if (display_wm > display->max_wm) {
4669 DRM_DEBUG_KMS("display watermark(%d) is too large(%d), disabling wm%d+\n",
4670 display_wm, SNB_DISPLAY_MAX_SRWM, level);
4671 return false;
4672 }
4673
4674 if (cursor_wm > cursor->max_wm) {
4675 DRM_DEBUG_KMS("cursor watermark(%d) is too large(%d), disabling wm%d+\n",
4676 cursor_wm, SNB_CURSOR_MAX_SRWM, level);
4677 return false;
4678 }
4679
4680 if (!(fbc_wm || display_wm || cursor_wm)) {
4681 DRM_DEBUG_KMS("latency %d is 0, disabling wm%d+\n", level, level);
4682 return false;
4683 }
4684
4685 return true;
4686}
4687
4688/*
4689 * Compute watermark values of WM[1-3],
4690 */
Chris Wilsond2102462011-01-24 17:43:27 +00004691static bool ironlake_compute_srwm(struct drm_device *dev, int level, int plane,
4692 int latency_ns,
Jesse Barnesb79d4992010-12-21 13:10:23 -08004693 const struct intel_watermark_params *display,
4694 const struct intel_watermark_params *cursor,
4695 int *fbc_wm, int *display_wm, int *cursor_wm)
4696{
Chris Wilsond2102462011-01-24 17:43:27 +00004697 struct drm_crtc *crtc;
Jesse Barnesb79d4992010-12-21 13:10:23 -08004698 unsigned long line_time_us;
Chris Wilsond2102462011-01-24 17:43:27 +00004699 int hdisplay, htotal, pixel_size, clock;
Jesse Barnesb79d4992010-12-21 13:10:23 -08004700 int line_count, line_size;
4701 int small, large;
4702 int entries;
4703
4704 if (!latency_ns) {
4705 *fbc_wm = *display_wm = *cursor_wm = 0;
4706 return false;
4707 }
4708
Chris Wilsond2102462011-01-24 17:43:27 +00004709 crtc = intel_get_crtc_for_plane(dev, plane);
4710 hdisplay = crtc->mode.hdisplay;
4711 htotal = crtc->mode.htotal;
4712 clock = crtc->mode.clock;
4713 pixel_size = crtc->fb->bits_per_pixel / 8;
4714
Jesse Barnesb79d4992010-12-21 13:10:23 -08004715 line_time_us = (htotal * 1000) / clock;
4716 line_count = (latency_ns / line_time_us + 1000) / 1000;
4717 line_size = hdisplay * pixel_size;
4718
4719 /* Use the minimum of the small and large buffer method for primary */
4720 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
4721 large = line_count * line_size;
4722
4723 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
4724 *display_wm = entries + display->guard_size;
4725
4726 /*
4727 * Spec says:
4728 * FBC WM = ((Final Primary WM * 64) / number of bytes per line) + 2
4729 */
4730 *fbc_wm = DIV_ROUND_UP(*display_wm * 64, line_size) + 2;
4731
4732 /* calculate the self-refresh watermark for display cursor */
4733 entries = line_count * pixel_size * 64;
4734 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
4735 *cursor_wm = entries + cursor->guard_size;
4736
4737 return ironlake_check_srwm(dev, level,
4738 *fbc_wm, *display_wm, *cursor_wm,
4739 display, cursor);
4740}
4741
Chris Wilsond2102462011-01-24 17:43:27 +00004742static void ironlake_update_wm(struct drm_device *dev)
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08004743{
4744 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond2102462011-01-24 17:43:27 +00004745 int fbc_wm, plane_wm, cursor_wm;
4746 unsigned int enabled;
Zhao Yakuic936f442010-06-12 14:32:26 +08004747
Chris Wilson4ed765f2010-09-11 10:46:47 +01004748 enabled = 0;
Chris Wilson9f405102011-05-12 22:17:14 +01004749 if (g4x_compute_wm0(dev, 0,
4750 &ironlake_display_wm_info,
4751 ILK_LP0_PLANE_LATENCY,
4752 &ironlake_cursor_wm_info,
4753 ILK_LP0_CURSOR_LATENCY,
4754 &plane_wm, &cursor_wm)) {
Chris Wilson4ed765f2010-09-11 10:46:47 +01004755 I915_WRITE(WM0_PIPEA_ILK,
4756 (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
4757 DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
4758 " plane %d, " "cursor: %d\n",
4759 plane_wm, cursor_wm);
Chris Wilsond2102462011-01-24 17:43:27 +00004760 enabled |= 1;
Zhao Yakuic936f442010-06-12 14:32:26 +08004761 }
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08004762
Chris Wilson9f405102011-05-12 22:17:14 +01004763 if (g4x_compute_wm0(dev, 1,
4764 &ironlake_display_wm_info,
4765 ILK_LP0_PLANE_LATENCY,
4766 &ironlake_cursor_wm_info,
4767 ILK_LP0_CURSOR_LATENCY,
4768 &plane_wm, &cursor_wm)) {
Chris Wilson4ed765f2010-09-11 10:46:47 +01004769 I915_WRITE(WM0_PIPEB_ILK,
4770 (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
4771 DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
4772 " plane %d, cursor: %d\n",
4773 plane_wm, cursor_wm);
Chris Wilsond2102462011-01-24 17:43:27 +00004774 enabled |= 2;
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08004775 }
4776
4777 /*
4778 * Calculate and update the self-refresh watermark only when one
4779 * display plane is used.
4780 */
Jesse Barnesb79d4992010-12-21 13:10:23 -08004781 I915_WRITE(WM3_LP_ILK, 0);
4782 I915_WRITE(WM2_LP_ILK, 0);
4783 I915_WRITE(WM1_LP_ILK, 0);
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08004784
Chris Wilsond2102462011-01-24 17:43:27 +00004785 if (!single_plane_enabled(enabled))
Jesse Barnesb79d4992010-12-21 13:10:23 -08004786 return;
Chris Wilsond2102462011-01-24 17:43:27 +00004787 enabled = ffs(enabled) - 1;
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08004788
Jesse Barnesb79d4992010-12-21 13:10:23 -08004789 /* WM1 */
Chris Wilsond2102462011-01-24 17:43:27 +00004790 if (!ironlake_compute_srwm(dev, 1, enabled,
4791 ILK_READ_WM1_LATENCY() * 500,
Jesse Barnesb79d4992010-12-21 13:10:23 -08004792 &ironlake_display_srwm_info,
4793 &ironlake_cursor_srwm_info,
4794 &fbc_wm, &plane_wm, &cursor_wm))
4795 return;
Chris Wilson4ed765f2010-09-11 10:46:47 +01004796
Jesse Barnesb79d4992010-12-21 13:10:23 -08004797 I915_WRITE(WM1_LP_ILK,
4798 WM1_LP_SR_EN |
4799 (ILK_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
4800 (fbc_wm << WM1_LP_FBC_SHIFT) |
4801 (plane_wm << WM1_LP_SR_SHIFT) |
4802 cursor_wm);
Chris Wilson4ed765f2010-09-11 10:46:47 +01004803
Jesse Barnesb79d4992010-12-21 13:10:23 -08004804 /* WM2 */
Chris Wilsond2102462011-01-24 17:43:27 +00004805 if (!ironlake_compute_srwm(dev, 2, enabled,
4806 ILK_READ_WM2_LATENCY() * 500,
Jesse Barnesb79d4992010-12-21 13:10:23 -08004807 &ironlake_display_srwm_info,
4808 &ironlake_cursor_srwm_info,
4809 &fbc_wm, &plane_wm, &cursor_wm))
4810 return;
Chris Wilson4ed765f2010-09-11 10:46:47 +01004811
Jesse Barnesb79d4992010-12-21 13:10:23 -08004812 I915_WRITE(WM2_LP_ILK,
4813 WM2_LP_EN |
4814 (ILK_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
4815 (fbc_wm << WM1_LP_FBC_SHIFT) |
4816 (plane_wm << WM1_LP_SR_SHIFT) |
4817 cursor_wm);
Yuanhan Liu13982612010-12-15 15:42:31 +08004818
4819 /*
Jesse Barnesb79d4992010-12-21 13:10:23 -08004820 * WM3 is unsupported on ILK, probably because we don't have latency
4821 * data for that power state
Yuanhan Liu13982612010-12-15 15:42:31 +08004822 */
Yuanhan Liu13982612010-12-15 15:42:31 +08004823}
4824
Jesse Barnesb840d907f2011-12-13 13:19:38 -08004825void sandybridge_update_wm(struct drm_device *dev)
Yuanhan Liu13982612010-12-15 15:42:31 +08004826{
4827 struct drm_i915_private *dev_priv = dev->dev_private;
Yuanhan Liua0fa62d2010-12-23 16:35:40 +08004828 int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
Jesse Barnes47842642012-01-16 11:57:54 -08004829 u32 val;
Chris Wilsond2102462011-01-24 17:43:27 +00004830 int fbc_wm, plane_wm, cursor_wm;
4831 unsigned int enabled;
Yuanhan Liu13982612010-12-15 15:42:31 +08004832
4833 enabled = 0;
Chris Wilson9f405102011-05-12 22:17:14 +01004834 if (g4x_compute_wm0(dev, 0,
4835 &sandybridge_display_wm_info, latency,
4836 &sandybridge_cursor_wm_info, latency,
4837 &plane_wm, &cursor_wm)) {
Jesse Barnes47842642012-01-16 11:57:54 -08004838 val = I915_READ(WM0_PIPEA_ILK);
4839 val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
4840 I915_WRITE(WM0_PIPEA_ILK, val |
4841 ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
Yuanhan Liu13982612010-12-15 15:42:31 +08004842 DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
4843 " plane %d, " "cursor: %d\n",
4844 plane_wm, cursor_wm);
Chris Wilsond2102462011-01-24 17:43:27 +00004845 enabled |= 1;
Yuanhan Liu13982612010-12-15 15:42:31 +08004846 }
4847
Chris Wilson9f405102011-05-12 22:17:14 +01004848 if (g4x_compute_wm0(dev, 1,
4849 &sandybridge_display_wm_info, latency,
4850 &sandybridge_cursor_wm_info, latency,
4851 &plane_wm, &cursor_wm)) {
Jesse Barnes47842642012-01-16 11:57:54 -08004852 val = I915_READ(WM0_PIPEB_ILK);
4853 val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
4854 I915_WRITE(WM0_PIPEB_ILK, val |
4855 ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
Yuanhan Liu13982612010-12-15 15:42:31 +08004856 DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
4857 " plane %d, cursor: %d\n",
4858 plane_wm, cursor_wm);
Chris Wilsond2102462011-01-24 17:43:27 +00004859 enabled |= 2;
Yuanhan Liu13982612010-12-15 15:42:31 +08004860 }
4861
Jesse Barnesd6c892d2011-10-12 15:36:42 -07004862 /* IVB has 3 pipes */
4863 if (IS_IVYBRIDGE(dev) &&
4864 g4x_compute_wm0(dev, 2,
4865 &sandybridge_display_wm_info, latency,
4866 &sandybridge_cursor_wm_info, latency,
4867 &plane_wm, &cursor_wm)) {
Jesse Barnes47842642012-01-16 11:57:54 -08004868 val = I915_READ(WM0_PIPEC_IVB);
4869 val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
4870 I915_WRITE(WM0_PIPEC_IVB, val |
4871 ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
Jesse Barnesd6c892d2011-10-12 15:36:42 -07004872 DRM_DEBUG_KMS("FIFO watermarks For pipe C -"
4873 " plane %d, cursor: %d\n",
4874 plane_wm, cursor_wm);
4875 enabled |= 3;
4876 }
4877
Yuanhan Liu13982612010-12-15 15:42:31 +08004878 /*
4879 * Calculate and update the self-refresh watermark only when one
4880 * display plane is used.
4881 *
4882 * SNB support 3 levels of watermark.
4883 *
4884 * WM1/WM2/WM2 watermarks have to be enabled in the ascending order,
4885 * and disabled in the descending order
4886 *
4887 */
4888 I915_WRITE(WM3_LP_ILK, 0);
4889 I915_WRITE(WM2_LP_ILK, 0);
4890 I915_WRITE(WM1_LP_ILK, 0);
4891
Jesse Barnesb840d907f2011-12-13 13:19:38 -08004892 if (!single_plane_enabled(enabled) ||
4893 dev_priv->sprite_scaling_enabled)
Yuanhan Liu13982612010-12-15 15:42:31 +08004894 return;
Chris Wilsond2102462011-01-24 17:43:27 +00004895 enabled = ffs(enabled) - 1;
Yuanhan Liu13982612010-12-15 15:42:31 +08004896
4897 /* WM1 */
Chris Wilsond2102462011-01-24 17:43:27 +00004898 if (!ironlake_compute_srwm(dev, 1, enabled,
4899 SNB_READ_WM1_LATENCY() * 500,
Jesse Barnesb79d4992010-12-21 13:10:23 -08004900 &sandybridge_display_srwm_info,
4901 &sandybridge_cursor_srwm_info,
4902 &fbc_wm, &plane_wm, &cursor_wm))
Yuanhan Liu13982612010-12-15 15:42:31 +08004903 return;
4904
4905 I915_WRITE(WM1_LP_ILK,
4906 WM1_LP_SR_EN |
4907 (SNB_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
4908 (fbc_wm << WM1_LP_FBC_SHIFT) |
4909 (plane_wm << WM1_LP_SR_SHIFT) |
4910 cursor_wm);
4911
4912 /* WM2 */
Chris Wilsond2102462011-01-24 17:43:27 +00004913 if (!ironlake_compute_srwm(dev, 2, enabled,
4914 SNB_READ_WM2_LATENCY() * 500,
Jesse Barnesb79d4992010-12-21 13:10:23 -08004915 &sandybridge_display_srwm_info,
4916 &sandybridge_cursor_srwm_info,
4917 &fbc_wm, &plane_wm, &cursor_wm))
Yuanhan Liu13982612010-12-15 15:42:31 +08004918 return;
4919
4920 I915_WRITE(WM2_LP_ILK,
4921 WM2_LP_EN |
4922 (SNB_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
4923 (fbc_wm << WM1_LP_FBC_SHIFT) |
4924 (plane_wm << WM1_LP_SR_SHIFT) |
4925 cursor_wm);
4926
4927 /* WM3 */
Chris Wilsond2102462011-01-24 17:43:27 +00004928 if (!ironlake_compute_srwm(dev, 3, enabled,
4929 SNB_READ_WM3_LATENCY() * 500,
Jesse Barnesb79d4992010-12-21 13:10:23 -08004930 &sandybridge_display_srwm_info,
4931 &sandybridge_cursor_srwm_info,
4932 &fbc_wm, &plane_wm, &cursor_wm))
Yuanhan Liu13982612010-12-15 15:42:31 +08004933 return;
4934
4935 I915_WRITE(WM3_LP_ILK,
4936 WM3_LP_EN |
4937 (SNB_READ_WM3_LATENCY() << WM1_LP_LATENCY_SHIFT) |
4938 (fbc_wm << WM1_LP_FBC_SHIFT) |
4939 (plane_wm << WM1_LP_SR_SHIFT) |
4940 cursor_wm);
4941}
4942
Jesse Barnesb840d907f2011-12-13 13:19:38 -08004943static bool
4944sandybridge_compute_sprite_wm(struct drm_device *dev, int plane,
4945 uint32_t sprite_width, int pixel_size,
4946 const struct intel_watermark_params *display,
4947 int display_latency_ns, int *sprite_wm)
4948{
4949 struct drm_crtc *crtc;
4950 int clock;
4951 int entries, tlb_miss;
4952
4953 crtc = intel_get_crtc_for_plane(dev, plane);
4954 if (crtc->fb == NULL || !crtc->enabled) {
4955 *sprite_wm = display->guard_size;
4956 return false;
4957 }
4958
4959 clock = crtc->mode.clock;
4960
4961 /* Use the small buffer method to calculate the sprite watermark */
4962 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
4963 tlb_miss = display->fifo_size*display->cacheline_size -
4964 sprite_width * 8;
4965 if (tlb_miss > 0)
4966 entries += tlb_miss;
4967 entries = DIV_ROUND_UP(entries, display->cacheline_size);
4968 *sprite_wm = entries + display->guard_size;
4969 if (*sprite_wm > (int)display->max_wm)
4970 *sprite_wm = display->max_wm;
4971
4972 return true;
4973}
4974
4975static bool
4976sandybridge_compute_sprite_srwm(struct drm_device *dev, int plane,
4977 uint32_t sprite_width, int pixel_size,
4978 const struct intel_watermark_params *display,
4979 int latency_ns, int *sprite_wm)
4980{
4981 struct drm_crtc *crtc;
4982 unsigned long line_time_us;
4983 int clock;
4984 int line_count, line_size;
4985 int small, large;
4986 int entries;
4987
4988 if (!latency_ns) {
4989 *sprite_wm = 0;
4990 return false;
4991 }
4992
4993 crtc = intel_get_crtc_for_plane(dev, plane);
4994 clock = crtc->mode.clock;
4995
4996 line_time_us = (sprite_width * 1000) / clock;
4997 line_count = (latency_ns / line_time_us + 1000) / 1000;
4998 line_size = sprite_width * pixel_size;
4999
5000 /* Use the minimum of the small and large buffer method for primary */
5001 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
5002 large = line_count * line_size;
5003
5004 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
5005 *sprite_wm = entries + display->guard_size;
5006
5007 return *sprite_wm > 0x3ff ? false : true;
5008}
5009
5010static void sandybridge_update_sprite_wm(struct drm_device *dev, int pipe,
5011 uint32_t sprite_width, int pixel_size)
5012{
5013 struct drm_i915_private *dev_priv = dev->dev_private;
5014 int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
Jesse Barnes47842642012-01-16 11:57:54 -08005015 u32 val;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08005016 int sprite_wm, reg;
5017 int ret;
5018
5019 switch (pipe) {
5020 case 0:
5021 reg = WM0_PIPEA_ILK;
5022 break;
5023 case 1:
5024 reg = WM0_PIPEB_ILK;
5025 break;
5026 case 2:
5027 reg = WM0_PIPEC_IVB;
5028 break;
5029 default:
5030 return; /* bad pipe */
5031 }
5032
5033 ret = sandybridge_compute_sprite_wm(dev, pipe, sprite_width, pixel_size,
5034 &sandybridge_display_wm_info,
5035 latency, &sprite_wm);
5036 if (!ret) {
5037 DRM_DEBUG_KMS("failed to compute sprite wm for pipe %d\n",
5038 pipe);
5039 return;
5040 }
5041
Jesse Barnes47842642012-01-16 11:57:54 -08005042 val = I915_READ(reg);
5043 val &= ~WM0_PIPE_SPRITE_MASK;
5044 I915_WRITE(reg, val | (sprite_wm << WM0_PIPE_SPRITE_SHIFT));
Jesse Barnesb840d907f2011-12-13 13:19:38 -08005045 DRM_DEBUG_KMS("sprite watermarks For pipe %d - %d\n", pipe, sprite_wm);
5046
5047
5048 ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
5049 pixel_size,
5050 &sandybridge_display_srwm_info,
5051 SNB_READ_WM1_LATENCY() * 500,
5052 &sprite_wm);
5053 if (!ret) {
5054 DRM_DEBUG_KMS("failed to compute sprite lp1 wm on pipe %d\n",
5055 pipe);
5056 return;
5057 }
5058 I915_WRITE(WM1S_LP_ILK, sprite_wm);
5059
5060 /* Only IVB has two more LP watermarks for sprite */
5061 if (!IS_IVYBRIDGE(dev))
5062 return;
5063
5064 ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
5065 pixel_size,
5066 &sandybridge_display_srwm_info,
5067 SNB_READ_WM2_LATENCY() * 500,
5068 &sprite_wm);
5069 if (!ret) {
5070 DRM_DEBUG_KMS("failed to compute sprite lp2 wm on pipe %d\n",
5071 pipe);
5072 return;
5073 }
5074 I915_WRITE(WM2S_LP_IVB, sprite_wm);
5075
5076 ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
5077 pixel_size,
5078 &sandybridge_display_srwm_info,
5079 SNB_READ_WM3_LATENCY() * 500,
5080 &sprite_wm);
5081 if (!ret) {
5082 DRM_DEBUG_KMS("failed to compute sprite lp3 wm on pipe %d\n",
5083 pipe);
5084 return;
5085 }
5086 I915_WRITE(WM3S_LP_IVB, sprite_wm);
5087}
5088
Shaohua Li7662c8b2009-06-26 11:23:55 +08005089/**
5090 * intel_update_watermarks - update FIFO watermark values based on current modes
5091 *
5092 * Calculate watermark values for the various WM regs based on current mode
5093 * and plane configuration.
5094 *
5095 * There are several cases to deal with here:
5096 * - normal (i.e. non-self-refresh)
5097 * - self-refresh (SR) mode
5098 * - lines are large relative to FIFO size (buffer can hold up to 2)
5099 * - lines are small relative to FIFO size (buffer can hold more than 2
5100 * lines), so need to account for TLB latency
5101 *
5102 * The normal calculation is:
5103 * watermark = dotclock * bytes per pixel * latency
5104 * where latency is platform & configuration dependent (we assume pessimal
5105 * values here).
5106 *
5107 * The SR calculation is:
5108 * watermark = (trunc(latency/line time)+1) * surface width *
5109 * bytes per pixel
5110 * where
5111 * line time = htotal / dotclock
Zhao Yakuifa143212010-06-12 14:32:23 +08005112 * surface width = hdisplay for normal plane and 64 for cursor
Shaohua Li7662c8b2009-06-26 11:23:55 +08005113 * and latency is assumed to be high, as above.
5114 *
5115 * The final value programmed to the register should always be rounded up,
5116 * and include an extra 2 entries to account for clock crossings.
5117 *
5118 * We don't use the sprite, so we can ignore that. And on Crestline we have
5119 * to set the non-SR watermarks to 8.
Chris Wilson5eddb702010-09-11 13:48:45 +01005120 */
Shaohua Li7662c8b2009-06-26 11:23:55 +08005121static void intel_update_watermarks(struct drm_device *dev)
5122{
Jesse Barnese70236a2009-09-21 10:42:27 -07005123 struct drm_i915_private *dev_priv = dev->dev_private;
Shaohua Li7662c8b2009-06-26 11:23:55 +08005124
Chris Wilsond2102462011-01-24 17:43:27 +00005125 if (dev_priv->display.update_wm)
5126 dev_priv->display.update_wm(dev);
Shaohua Li7662c8b2009-06-26 11:23:55 +08005127}
5128
Jesse Barnesb840d907f2011-12-13 13:19:38 -08005129void intel_update_sprite_watermarks(struct drm_device *dev, int pipe,
5130 uint32_t sprite_width, int pixel_size)
5131{
5132 struct drm_i915_private *dev_priv = dev->dev_private;
5133
5134 if (dev_priv->display.update_sprite_wm)
5135 dev_priv->display.update_sprite_wm(dev, pipe, sprite_width,
5136 pixel_size);
5137}
5138
Chris Wilsona7615032011-01-12 17:04:08 +00005139static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
5140{
Keith Packard72bbe582011-09-26 16:09:45 -07005141 if (i915_panel_use_ssc >= 0)
5142 return i915_panel_use_ssc != 0;
5143 return dev_priv->lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07005144 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00005145}
5146
Jesse Barnes5a354202011-06-24 12:19:22 -07005147/**
5148 * intel_choose_pipe_bpp_dither - figure out what color depth the pipe should send
5149 * @crtc: CRTC structure
Adam Jackson3b5c78a2011-12-13 15:41:00 -08005150 * @mode: requested mode
Jesse Barnes5a354202011-06-24 12:19:22 -07005151 *
5152 * A pipe may be connected to one or more outputs. Based on the depth of the
5153 * attached framebuffer, choose a good color depth to use on the pipe.
5154 *
5155 * If possible, match the pipe depth to the fb depth. In some cases, this
5156 * isn't ideal, because the connected output supports a lesser or restricted
5157 * set of depths. Resolve that here:
5158 * LVDS typically supports only 6bpc, so clamp down in that case
5159 * HDMI supports only 8bpc or 12bpc, so clamp to 8bpc with dither for 10bpc
5160 * Displays may support a restricted set as well, check EDID and clamp as
5161 * appropriate.
Adam Jackson3b5c78a2011-12-13 15:41:00 -08005162 * DP may want to dither down to 6bpc to fit larger modes
Jesse Barnes5a354202011-06-24 12:19:22 -07005163 *
5164 * RETURNS:
5165 * Dithering requirement (i.e. false if display bpc and pipe bpc match,
5166 * true if they don't match).
5167 */
5168static bool intel_choose_pipe_bpp_dither(struct drm_crtc *crtc,
Adam Jackson3b5c78a2011-12-13 15:41:00 -08005169 unsigned int *pipe_bpp,
5170 struct drm_display_mode *mode)
Jesse Barnes5a354202011-06-24 12:19:22 -07005171{
5172 struct drm_device *dev = crtc->dev;
5173 struct drm_i915_private *dev_priv = dev->dev_private;
5174 struct drm_encoder *encoder;
5175 struct drm_connector *connector;
5176 unsigned int display_bpc = UINT_MAX, bpc;
5177
5178 /* Walk the encoders & connectors on this crtc, get min bpc */
5179 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
5180 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5181
5182 if (encoder->crtc != crtc)
5183 continue;
5184
5185 if (intel_encoder->type == INTEL_OUTPUT_LVDS) {
5186 unsigned int lvds_bpc;
5187
5188 if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) ==
5189 LVDS_A3_POWER_UP)
5190 lvds_bpc = 8;
5191 else
5192 lvds_bpc = 6;
5193
5194 if (lvds_bpc < display_bpc) {
Adam Jackson82820492011-10-10 16:33:34 -04005195 DRM_DEBUG_KMS("clamping display bpc (was %d) to LVDS (%d)\n", display_bpc, lvds_bpc);
Jesse Barnes5a354202011-06-24 12:19:22 -07005196 display_bpc = lvds_bpc;
5197 }
5198 continue;
5199 }
5200
5201 if (intel_encoder->type == INTEL_OUTPUT_EDP) {
5202 /* Use VBT settings if we have an eDP panel */
5203 unsigned int edp_bpc = dev_priv->edp.bpp / 3;
5204
5205 if (edp_bpc < display_bpc) {
Adam Jackson82820492011-10-10 16:33:34 -04005206 DRM_DEBUG_KMS("clamping display bpc (was %d) to eDP (%d)\n", display_bpc, edp_bpc);
Jesse Barnes5a354202011-06-24 12:19:22 -07005207 display_bpc = edp_bpc;
5208 }
5209 continue;
5210 }
5211
5212 /* Not one of the known troublemakers, check the EDID */
5213 list_for_each_entry(connector, &dev->mode_config.connector_list,
5214 head) {
5215 if (connector->encoder != encoder)
5216 continue;
5217
Jesse Barnes62ac41a2011-07-28 12:55:14 -07005218 /* Don't use an invalid EDID bpc value */
5219 if (connector->display_info.bpc &&
5220 connector->display_info.bpc < display_bpc) {
Adam Jackson82820492011-10-10 16:33:34 -04005221 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 -07005222 display_bpc = connector->display_info.bpc;
5223 }
5224 }
5225
5226 /*
5227 * HDMI is either 12 or 8, so if the display lets 10bpc sneak
5228 * through, clamp it down. (Note: >12bpc will be caught below.)
5229 */
5230 if (intel_encoder->type == INTEL_OUTPUT_HDMI) {
5231 if (display_bpc > 8 && display_bpc < 12) {
Adam Jackson82820492011-10-10 16:33:34 -04005232 DRM_DEBUG_KMS("forcing bpc to 12 for HDMI\n");
Jesse Barnes5a354202011-06-24 12:19:22 -07005233 display_bpc = 12;
5234 } else {
Adam Jackson82820492011-10-10 16:33:34 -04005235 DRM_DEBUG_KMS("forcing bpc to 8 for HDMI\n");
Jesse Barnes5a354202011-06-24 12:19:22 -07005236 display_bpc = 8;
5237 }
5238 }
5239 }
5240
Adam Jackson3b5c78a2011-12-13 15:41:00 -08005241 if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
5242 DRM_DEBUG_KMS("Dithering DP to 6bpc\n");
5243 display_bpc = 6;
5244 }
5245
Jesse Barnes5a354202011-06-24 12:19:22 -07005246 /*
5247 * We could just drive the pipe at the highest bpc all the time and
5248 * enable dithering as needed, but that costs bandwidth. So choose
5249 * the minimum value that expresses the full color range of the fb but
5250 * also stays within the max display bpc discovered above.
5251 */
5252
5253 switch (crtc->fb->depth) {
5254 case 8:
5255 bpc = 8; /* since we go through a colormap */
5256 break;
5257 case 15:
5258 case 16:
5259 bpc = 6; /* min is 18bpp */
5260 break;
5261 case 24:
Keith Packard578393c2011-09-05 11:53:21 -07005262 bpc = 8;
Jesse Barnes5a354202011-06-24 12:19:22 -07005263 break;
5264 case 30:
Keith Packard578393c2011-09-05 11:53:21 -07005265 bpc = 10;
Jesse Barnes5a354202011-06-24 12:19:22 -07005266 break;
5267 case 48:
Keith Packard578393c2011-09-05 11:53:21 -07005268 bpc = 12;
Jesse Barnes5a354202011-06-24 12:19:22 -07005269 break;
5270 default:
5271 DRM_DEBUG("unsupported depth, assuming 24 bits\n");
5272 bpc = min((unsigned int)8, display_bpc);
5273 break;
5274 }
5275
Keith Packard578393c2011-09-05 11:53:21 -07005276 display_bpc = min(display_bpc, bpc);
5277
Adam Jackson82820492011-10-10 16:33:34 -04005278 DRM_DEBUG_KMS("setting pipe bpc to %d (max display bpc %d)\n",
5279 bpc, display_bpc);
Jesse Barnes5a354202011-06-24 12:19:22 -07005280
Keith Packard578393c2011-09-05 11:53:21 -07005281 *pipe_bpp = display_bpc * 3;
Jesse Barnes5a354202011-06-24 12:19:22 -07005282
5283 return display_bpc != bpc;
5284}
5285
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005286static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
5287{
5288 struct drm_device *dev = crtc->dev;
5289 struct drm_i915_private *dev_priv = dev->dev_private;
5290 int refclk;
5291
5292 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
5293 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
5294 refclk = dev_priv->lvds_ssc_freq * 1000;
5295 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
5296 refclk / 1000);
5297 } else if (!IS_GEN2(dev)) {
5298 refclk = 96000;
5299 } else {
5300 refclk = 48000;
5301 }
5302
5303 return refclk;
5304}
5305
5306static void i9xx_adjust_sdvo_tv_clock(struct drm_display_mode *adjusted_mode,
5307 intel_clock_t *clock)
5308{
5309 /* SDVO TV has fixed PLL values depend on its clock range,
5310 this mirrors vbios setting. */
5311 if (adjusted_mode->clock >= 100000
5312 && adjusted_mode->clock < 140500) {
5313 clock->p1 = 2;
5314 clock->p2 = 10;
5315 clock->n = 3;
5316 clock->m1 = 16;
5317 clock->m2 = 8;
5318 } else if (adjusted_mode->clock >= 140500
5319 && adjusted_mode->clock <= 200000) {
5320 clock->p1 = 1;
5321 clock->p2 = 10;
5322 clock->n = 6;
5323 clock->m1 = 12;
5324 clock->m2 = 8;
5325 }
5326}
5327
Jesse Barnesa7516a02011-12-15 12:30:37 -08005328static void i9xx_update_pll_dividers(struct drm_crtc *crtc,
5329 intel_clock_t *clock,
5330 intel_clock_t *reduced_clock)
5331{
5332 struct drm_device *dev = crtc->dev;
5333 struct drm_i915_private *dev_priv = dev->dev_private;
5334 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5335 int pipe = intel_crtc->pipe;
5336 u32 fp, fp2 = 0;
5337
5338 if (IS_PINEVIEW(dev)) {
5339 fp = (1 << clock->n) << 16 | clock->m1 << 8 | clock->m2;
5340 if (reduced_clock)
5341 fp2 = (1 << reduced_clock->n) << 16 |
5342 reduced_clock->m1 << 8 | reduced_clock->m2;
5343 } else {
5344 fp = clock->n << 16 | clock->m1 << 8 | clock->m2;
5345 if (reduced_clock)
5346 fp2 = reduced_clock->n << 16 | reduced_clock->m1 << 8 |
5347 reduced_clock->m2;
5348 }
5349
5350 I915_WRITE(FP0(pipe), fp);
5351
5352 intel_crtc->lowfreq_avail = false;
5353 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
5354 reduced_clock && i915_powersave) {
5355 I915_WRITE(FP1(pipe), fp2);
5356 intel_crtc->lowfreq_avail = true;
5357 } else {
5358 I915_WRITE(FP1(pipe), fp);
5359 }
5360}
5361
Daniel Vetter93e537a2012-03-28 23:11:26 +02005362static void intel_update_lvds(struct drm_crtc *crtc, intel_clock_t *clock,
5363 struct drm_display_mode *adjusted_mode)
5364{
5365 struct drm_device *dev = crtc->dev;
5366 struct drm_i915_private *dev_priv = dev->dev_private;
5367 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5368 int pipe = intel_crtc->pipe;
5369 u32 temp, lvds_sync = 0;
5370
5371 temp = I915_READ(LVDS);
5372 temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
5373 if (pipe == 1) {
5374 temp |= LVDS_PIPEB_SELECT;
5375 } else {
5376 temp &= ~LVDS_PIPEB_SELECT;
5377 }
5378 /* set the corresponsding LVDS_BORDER bit */
5379 temp |= dev_priv->lvds_border_bits;
5380 /* Set the B0-B3 data pairs corresponding to whether we're going to
5381 * set the DPLLs for dual-channel mode or not.
5382 */
5383 if (clock->p2 == 7)
5384 temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
5385 else
5386 temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
5387
5388 /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
5389 * appropriately here, but we need to look more thoroughly into how
5390 * panels behave in the two modes.
5391 */
5392 /* set the dithering flag on LVDS as needed */
5393 if (INTEL_INFO(dev)->gen >= 4) {
5394 if (dev_priv->lvds_dither)
5395 temp |= LVDS_ENABLE_DITHER;
5396 else
5397 temp &= ~LVDS_ENABLE_DITHER;
5398 }
5399 if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
5400 lvds_sync |= LVDS_HSYNC_POLARITY;
5401 if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
5402 lvds_sync |= LVDS_VSYNC_POLARITY;
5403 if ((temp & (LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY))
5404 != lvds_sync) {
5405 char flags[2] = "-+";
5406 DRM_INFO("Changing LVDS panel from "
5407 "(%chsync, %cvsync) to (%chsync, %cvsync)\n",
5408 flags[!(temp & LVDS_HSYNC_POLARITY)],
5409 flags[!(temp & LVDS_VSYNC_POLARITY)],
5410 flags[!(lvds_sync & LVDS_HSYNC_POLARITY)],
5411 flags[!(lvds_sync & LVDS_VSYNC_POLARITY)]);
5412 temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
5413 temp |= lvds_sync;
5414 }
5415 I915_WRITE(LVDS, temp);
5416}
5417
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005418static void i9xx_update_pll(struct drm_crtc *crtc,
5419 struct drm_display_mode *mode,
5420 struct drm_display_mode *adjusted_mode,
5421 intel_clock_t *clock, intel_clock_t *reduced_clock,
5422 int num_connectors)
5423{
5424 struct drm_device *dev = crtc->dev;
5425 struct drm_i915_private *dev_priv = dev->dev_private;
5426 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5427 int pipe = intel_crtc->pipe;
5428 u32 dpll;
5429 bool is_sdvo;
5430
5431 is_sdvo = intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO) ||
5432 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI);
5433
5434 dpll = DPLL_VGA_MODE_DIS;
5435
5436 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
5437 dpll |= DPLLB_MODE_LVDS;
5438 else
5439 dpll |= DPLLB_MODE_DAC_SERIAL;
5440 if (is_sdvo) {
5441 int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
5442 if (pixel_multiplier > 1) {
5443 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
5444 dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
5445 }
5446 dpll |= DPLL_DVO_HIGH_SPEED;
5447 }
5448 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
5449 dpll |= DPLL_DVO_HIGH_SPEED;
5450
5451 /* compute bitmask from p1 value */
5452 if (IS_PINEVIEW(dev))
5453 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
5454 else {
5455 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5456 if (IS_G4X(dev) && reduced_clock)
5457 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5458 }
5459 switch (clock->p2) {
5460 case 5:
5461 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5462 break;
5463 case 7:
5464 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5465 break;
5466 case 10:
5467 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5468 break;
5469 case 14:
5470 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5471 break;
5472 }
5473 if (INTEL_INFO(dev)->gen >= 4)
5474 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
5475
5476 if (is_sdvo && intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
5477 dpll |= PLL_REF_INPUT_TVCLKINBC;
5478 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
5479 /* XXX: just matching BIOS for now */
5480 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
5481 dpll |= 3;
5482 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
5483 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5484 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5485 else
5486 dpll |= PLL_REF_INPUT_DREFCLK;
5487
5488 dpll |= DPLL_VCO_ENABLE;
5489 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
5490 POSTING_READ(DPLL(pipe));
5491 udelay(150);
5492
5493 /* The LVDS pin pair needs to be on before the DPLLs are enabled.
5494 * This is an exception to the general rule that mode_set doesn't turn
5495 * things on.
5496 */
5497 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
5498 intel_update_lvds(crtc, clock, adjusted_mode);
5499
5500 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
5501 intel_dp_set_m_n(crtc, mode, adjusted_mode);
5502
5503 I915_WRITE(DPLL(pipe), dpll);
5504
5505 /* Wait for the clocks to stabilize. */
5506 POSTING_READ(DPLL(pipe));
5507 udelay(150);
5508
5509 if (INTEL_INFO(dev)->gen >= 4) {
5510 u32 temp = 0;
5511 if (is_sdvo) {
5512 temp = intel_mode_get_pixel_multiplier(adjusted_mode);
5513 if (temp > 1)
5514 temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
5515 else
5516 temp = 0;
5517 }
5518 I915_WRITE(DPLL_MD(pipe), temp);
5519 } else {
5520 /* The pixel multiplier can only be updated once the
5521 * DPLL is enabled and the clocks are stable.
5522 *
5523 * So write it again.
5524 */
5525 I915_WRITE(DPLL(pipe), dpll);
5526 }
5527}
5528
5529static void i8xx_update_pll(struct drm_crtc *crtc,
5530 struct drm_display_mode *adjusted_mode,
5531 intel_clock_t *clock,
5532 int num_connectors)
5533{
5534 struct drm_device *dev = crtc->dev;
5535 struct drm_i915_private *dev_priv = dev->dev_private;
5536 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5537 int pipe = intel_crtc->pipe;
5538 u32 dpll;
5539
5540 dpll = DPLL_VGA_MODE_DIS;
5541
5542 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
5543 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5544 } else {
5545 if (clock->p1 == 2)
5546 dpll |= PLL_P1_DIVIDE_BY_TWO;
5547 else
5548 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5549 if (clock->p2 == 4)
5550 dpll |= PLL_P2_DIVIDE_BY_4;
5551 }
5552
5553 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
5554 /* XXX: just matching BIOS for now */
5555 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
5556 dpll |= 3;
5557 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
5558 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5559 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5560 else
5561 dpll |= PLL_REF_INPUT_DREFCLK;
5562
5563 dpll |= DPLL_VCO_ENABLE;
5564 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
5565 POSTING_READ(DPLL(pipe));
5566 udelay(150);
5567
5568 I915_WRITE(DPLL(pipe), dpll);
5569
5570 /* Wait for the clocks to stabilize. */
5571 POSTING_READ(DPLL(pipe));
5572 udelay(150);
5573
5574 /* The LVDS pin pair needs to be on before the DPLLs are enabled.
5575 * This is an exception to the general rule that mode_set doesn't turn
5576 * things on.
5577 */
5578 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
5579 intel_update_lvds(crtc, clock, adjusted_mode);
5580
5581 /* The pixel multiplier can only be updated once the
5582 * DPLL is enabled and the clocks are stable.
5583 *
5584 * So write it again.
5585 */
5586 I915_WRITE(DPLL(pipe), dpll);
5587}
5588
Eric Anholtf564048e2011-03-30 13:01:02 -07005589static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
5590 struct drm_display_mode *mode,
5591 struct drm_display_mode *adjusted_mode,
5592 int x, int y,
5593 struct drm_framebuffer *old_fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005594{
5595 struct drm_device *dev = crtc->dev;
5596 struct drm_i915_private *dev_priv = dev->dev_private;
5597 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5598 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07005599 int plane = intel_crtc->plane;
Eric Anholtc751ce42010-03-25 11:48:48 -07005600 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07005601 intel_clock_t clock, reduced_clock;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005602 u32 dspcntr, pipeconf, vsyncshift;
5603 bool ok, has_reduced_clock = false, is_sdvo = false;
5604 bool is_lvds = false, is_tv = false, is_dp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08005605 struct drm_mode_config *mode_config = &dev->mode_config;
Chris Wilson5eddb702010-09-11 13:48:45 +01005606 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08005607 const intel_limit_t *limit;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00005608 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08005609
Chris Wilson5eddb702010-09-11 13:48:45 +01005610 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5611 if (encoder->base.crtc != crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08005612 continue;
5613
Chris Wilson5eddb702010-09-11 13:48:45 +01005614 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08005615 case INTEL_OUTPUT_LVDS:
5616 is_lvds = true;
5617 break;
5618 case INTEL_OUTPUT_SDVO:
Eric Anholt7d573822009-01-02 13:33:00 -08005619 case INTEL_OUTPUT_HDMI:
Jesse Barnes79e53942008-11-07 14:24:08 -08005620 is_sdvo = true;
Chris Wilson5eddb702010-09-11 13:48:45 +01005621 if (encoder->needs_tv_clock)
Jesse Barnese2f0ba92009-02-02 15:11:52 -08005622 is_tv = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08005623 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005624 case INTEL_OUTPUT_TVOUT:
5625 is_tv = true;
5626 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005627 case INTEL_OUTPUT_DISPLAYPORT:
5628 is_dp = true;
5629 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005630 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005631
Eric Anholtc751ce42010-03-25 11:48:48 -07005632 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08005633 }
5634
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005635 refclk = i9xx_get_refclk(crtc, num_connectors);
Jesse Barnes79e53942008-11-07 14:24:08 -08005636
Ma Lingd4906092009-03-18 20:13:27 +08005637 /*
5638 * Returns a set of divisors for the desired target clock with the given
5639 * refclk, or FALSE. The returned values represent the clock equation:
5640 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
5641 */
Chris Wilson1b894b52010-12-14 20:04:54 +00005642 limit = intel_limit(crtc, refclk);
Sean Paulcec2f352012-01-10 15:09:36 -08005643 ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
5644 &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08005645 if (!ok) {
5646 DRM_ERROR("Couldn't find PLL settings for mode!\n");
Eric Anholtf564048e2011-03-30 13:01:02 -07005647 return -EINVAL;
5648 }
5649
5650 /* Ensure that the cursor is valid for the new mode before changing... */
5651 intel_crtc_update_cursor(crtc, true);
5652
5653 if (is_lvds && dev_priv->lvds_downclock_avail) {
Sean Paulcec2f352012-01-10 15:09:36 -08005654 /*
5655 * Ensure we match the reduced clock's P to the target clock.
5656 * If the clocks don't match, we can't switch the display clock
5657 * by using the FP0/FP1. In such case we will disable the LVDS
5658 * downclock feature.
5659 */
Eric Anholtf564048e2011-03-30 13:01:02 -07005660 has_reduced_clock = limit->find_pll(limit, crtc,
5661 dev_priv->lvds_downclock,
5662 refclk,
Sean Paulcec2f352012-01-10 15:09:36 -08005663 &clock,
Eric Anholtf564048e2011-03-30 13:01:02 -07005664 &reduced_clock);
Eric Anholtf564048e2011-03-30 13:01:02 -07005665 }
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005666
5667 if (is_sdvo && is_tv)
5668 i9xx_adjust_sdvo_tv_clock(adjusted_mode, &clock);
Eric Anholtf564048e2011-03-30 13:01:02 -07005669
Jesse Barnesa7516a02011-12-15 12:30:37 -08005670 i9xx_update_pll_dividers(crtc, &clock, has_reduced_clock ?
5671 &reduced_clock : NULL);
Eric Anholtf564048e2011-03-30 13:01:02 -07005672
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005673 if (IS_GEN2(dev))
5674 i8xx_update_pll(crtc, adjusted_mode, &clock, num_connectors);
Eric Anholtf564048e2011-03-30 13:01:02 -07005675 else
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005676 i9xx_update_pll(crtc, mode, adjusted_mode, &clock,
5677 has_reduced_clock ? &reduced_clock : NULL,
5678 num_connectors);
Eric Anholtf564048e2011-03-30 13:01:02 -07005679
5680 /* setup pipeconf */
5681 pipeconf = I915_READ(PIPECONF(pipe));
5682
5683 /* Set up the display plane register */
5684 dspcntr = DISPPLANE_GAMMA_ENABLE;
5685
Eric Anholt929c77f2011-03-30 13:01:04 -07005686 if (pipe == 0)
5687 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
5688 else
5689 dspcntr |= DISPPLANE_SEL_PIPE_B;
Eric Anholtf564048e2011-03-30 13:01:02 -07005690
5691 if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
5692 /* Enable pixel doubling when the dot clock is > 90% of the (display)
5693 * core speed.
5694 *
5695 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
5696 * pipe == 0 check?
5697 */
5698 if (mode->clock >
5699 dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
5700 pipeconf |= PIPECONF_DOUBLE_WIDE;
5701 else
5702 pipeconf &= ~PIPECONF_DOUBLE_WIDE;
5703 }
5704
Adam Jackson3b5c78a2011-12-13 15:41:00 -08005705 /* default to 8bpc */
5706 pipeconf &= ~(PIPECONF_BPP_MASK | PIPECONF_DITHER_EN);
5707 if (is_dp) {
5708 if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
5709 pipeconf |= PIPECONF_BPP_6 |
5710 PIPECONF_DITHER_EN |
5711 PIPECONF_DITHER_TYPE_SP;
5712 }
5713 }
5714
Eric Anholtf564048e2011-03-30 13:01:02 -07005715 DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
5716 drm_mode_debug_printmodeline(mode);
5717
Jesse Barnesa7516a02011-12-15 12:30:37 -08005718 if (HAS_PIPE_CXSR(dev)) {
5719 if (intel_crtc->lowfreq_avail) {
Eric Anholtf564048e2011-03-30 13:01:02 -07005720 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
5721 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005722 } else {
Eric Anholtf564048e2011-03-30 13:01:02 -07005723 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
5724 pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
5725 }
5726 }
5727
Keith Packard617cf882012-02-08 13:53:38 -08005728 pipeconf &= ~PIPECONF_INTERLACE_MASK;
Daniel Vetterdbb02572012-01-28 14:49:23 +01005729 if (!IS_GEN2(dev) &&
5730 adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Eric Anholtf564048e2011-03-30 13:01:02 -07005731 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
5732 /* the chip adds 2 halflines automatically */
Eric Anholtf564048e2011-03-30 13:01:02 -07005733 adjusted_mode->crtc_vtotal -= 1;
Eric Anholtf564048e2011-03-30 13:01:02 -07005734 adjusted_mode->crtc_vblank_end -= 1;
Daniel Vetter0529a0d2012-01-28 14:49:24 +01005735 vsyncshift = adjusted_mode->crtc_hsync_start
5736 - adjusted_mode->crtc_htotal/2;
5737 } else {
Keith Packard617cf882012-02-08 13:53:38 -08005738 pipeconf |= PIPECONF_PROGRESSIVE;
Daniel Vetter0529a0d2012-01-28 14:49:24 +01005739 vsyncshift = 0;
5740 }
5741
5742 if (!IS_GEN3(dev))
5743 I915_WRITE(VSYNCSHIFT(pipe), vsyncshift);
Eric Anholtf564048e2011-03-30 13:01:02 -07005744
5745 I915_WRITE(HTOTAL(pipe),
5746 (adjusted_mode->crtc_hdisplay - 1) |
5747 ((adjusted_mode->crtc_htotal - 1) << 16));
5748 I915_WRITE(HBLANK(pipe),
5749 (adjusted_mode->crtc_hblank_start - 1) |
5750 ((adjusted_mode->crtc_hblank_end - 1) << 16));
5751 I915_WRITE(HSYNC(pipe),
5752 (adjusted_mode->crtc_hsync_start - 1) |
5753 ((adjusted_mode->crtc_hsync_end - 1) << 16));
5754
5755 I915_WRITE(VTOTAL(pipe),
5756 (adjusted_mode->crtc_vdisplay - 1) |
5757 ((adjusted_mode->crtc_vtotal - 1) << 16));
5758 I915_WRITE(VBLANK(pipe),
5759 (adjusted_mode->crtc_vblank_start - 1) |
5760 ((adjusted_mode->crtc_vblank_end - 1) << 16));
5761 I915_WRITE(VSYNC(pipe),
5762 (adjusted_mode->crtc_vsync_start - 1) |
5763 ((adjusted_mode->crtc_vsync_end - 1) << 16));
5764
5765 /* pipesrc and dspsize control the size that is scaled from,
5766 * which should always be the user's requested size.
5767 */
Eric Anholt929c77f2011-03-30 13:01:04 -07005768 I915_WRITE(DSPSIZE(plane),
5769 ((mode->vdisplay - 1) << 16) |
5770 (mode->hdisplay - 1));
5771 I915_WRITE(DSPPOS(plane), 0);
Eric Anholtf564048e2011-03-30 13:01:02 -07005772 I915_WRITE(PIPESRC(pipe),
5773 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
5774
Eric Anholtf564048e2011-03-30 13:01:02 -07005775 I915_WRITE(PIPECONF(pipe), pipeconf);
5776 POSTING_READ(PIPECONF(pipe));
Eric Anholt929c77f2011-03-30 13:01:04 -07005777 intel_enable_pipe(dev_priv, pipe, false);
Eric Anholtf564048e2011-03-30 13:01:02 -07005778
5779 intel_wait_for_vblank(dev, pipe);
5780
Eric Anholtf564048e2011-03-30 13:01:02 -07005781 I915_WRITE(DSPCNTR(plane), dspcntr);
5782 POSTING_READ(DSPCNTR(plane));
Keith Packard284d9522011-06-06 17:12:49 -07005783 intel_enable_plane(dev_priv, plane, pipe);
Eric Anholtf564048e2011-03-30 13:01:02 -07005784
5785 ret = intel_pipe_set_base(crtc, x, y, old_fb);
5786
5787 intel_update_watermarks(dev);
5788
Eric Anholtf564048e2011-03-30 13:01:02 -07005789 return ret;
5790}
5791
Keith Packard9fb526d2011-09-26 22:24:57 -07005792/*
5793 * Initialize reference clocks when the driver loads
5794 */
5795void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07005796{
5797 struct drm_i915_private *dev_priv = dev->dev_private;
5798 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005799 struct intel_encoder *encoder;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005800 u32 temp;
5801 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005802 bool has_cpu_edp = false;
5803 bool has_pch_edp = false;
5804 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07005805 bool has_ck505 = false;
5806 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005807
5808 /* We need to take the global config into account */
Keith Packard199e5d72011-09-22 12:01:57 -07005809 list_for_each_entry(encoder, &mode_config->encoder_list,
5810 base.head) {
5811 switch (encoder->type) {
5812 case INTEL_OUTPUT_LVDS:
5813 has_panel = true;
5814 has_lvds = true;
5815 break;
5816 case INTEL_OUTPUT_EDP:
5817 has_panel = true;
5818 if (intel_encoder_is_pch_edp(&encoder->base))
5819 has_pch_edp = true;
5820 else
5821 has_cpu_edp = true;
5822 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005823 }
5824 }
5825
Keith Packard99eb6a02011-09-26 14:29:12 -07005826 if (HAS_PCH_IBX(dev)) {
5827 has_ck505 = dev_priv->display_clock_mode;
5828 can_ssc = has_ck505;
5829 } else {
5830 has_ck505 = false;
5831 can_ssc = true;
5832 }
5833
5834 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_pch_edp %d has_cpu_edp %d has_ck505 %d\n",
5835 has_panel, has_lvds, has_pch_edp, has_cpu_edp,
5836 has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005837
5838 /* Ironlake: try to setup display ref clock before DPLL
5839 * enabling. This is only under driver's control after
5840 * PCH B stepping, previous chipset stepping should be
5841 * ignoring this setting.
5842 */
5843 temp = I915_READ(PCH_DREF_CONTROL);
5844 /* Always enable nonspread source */
5845 temp &= ~DREF_NONSPREAD_SOURCE_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005846
Keith Packard99eb6a02011-09-26 14:29:12 -07005847 if (has_ck505)
5848 temp |= DREF_NONSPREAD_CK505_ENABLE;
5849 else
5850 temp |= DREF_NONSPREAD_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005851
Keith Packard199e5d72011-09-22 12:01:57 -07005852 if (has_panel) {
5853 temp &= ~DREF_SSC_SOURCE_MASK;
5854 temp |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005855
Keith Packard199e5d72011-09-22 12:01:57 -07005856 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07005857 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005858 DRM_DEBUG_KMS("Using SSC on panel\n");
Jesse Barnes13d83a62011-08-03 12:59:20 -07005859 temp |= DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005860 }
Keith Packard199e5d72011-09-22 12:01:57 -07005861
5862 /* Get SSC going before enabling the outputs */
5863 I915_WRITE(PCH_DREF_CONTROL, temp);
5864 POSTING_READ(PCH_DREF_CONTROL);
5865 udelay(200);
5866
Jesse Barnes13d83a62011-08-03 12:59:20 -07005867 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5868
5869 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07005870 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07005871 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005872 DRM_DEBUG_KMS("Using SSC on eDP\n");
Jesse Barnes13d83a62011-08-03 12:59:20 -07005873 temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005874 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07005875 else
5876 temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005877 } else
5878 temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5879
5880 I915_WRITE(PCH_DREF_CONTROL, temp);
5881 POSTING_READ(PCH_DREF_CONTROL);
5882 udelay(200);
5883 } else {
5884 DRM_DEBUG_KMS("Disabling SSC entirely\n");
5885
5886 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5887
5888 /* Turn off CPU output */
5889 temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5890
5891 I915_WRITE(PCH_DREF_CONTROL, temp);
5892 POSTING_READ(PCH_DREF_CONTROL);
5893 udelay(200);
5894
5895 /* Turn off the SSC source */
5896 temp &= ~DREF_SSC_SOURCE_MASK;
5897 temp |= DREF_SSC_SOURCE_DISABLE;
5898
5899 /* Turn off SSC1 */
5900 temp &= ~ DREF_SSC1_ENABLE;
5901
Jesse Barnes13d83a62011-08-03 12:59:20 -07005902 I915_WRITE(PCH_DREF_CONTROL, temp);
5903 POSTING_READ(PCH_DREF_CONTROL);
5904 udelay(200);
5905 }
5906}
5907
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005908static int ironlake_get_refclk(struct drm_crtc *crtc)
5909{
5910 struct drm_device *dev = crtc->dev;
5911 struct drm_i915_private *dev_priv = dev->dev_private;
5912 struct intel_encoder *encoder;
5913 struct drm_mode_config *mode_config = &dev->mode_config;
5914 struct intel_encoder *edp_encoder = NULL;
5915 int num_connectors = 0;
5916 bool is_lvds = false;
5917
5918 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5919 if (encoder->base.crtc != crtc)
5920 continue;
5921
5922 switch (encoder->type) {
5923 case INTEL_OUTPUT_LVDS:
5924 is_lvds = true;
5925 break;
5926 case INTEL_OUTPUT_EDP:
5927 edp_encoder = encoder;
5928 break;
5929 }
5930 num_connectors++;
5931 }
5932
5933 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
5934 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
5935 dev_priv->lvds_ssc_freq);
5936 return dev_priv->lvds_ssc_freq * 1000;
5937 }
5938
5939 return 120000;
5940}
5941
Eric Anholtf564048e2011-03-30 13:01:02 -07005942static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
5943 struct drm_display_mode *mode,
5944 struct drm_display_mode *adjusted_mode,
5945 int x, int y,
5946 struct drm_framebuffer *old_fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005947{
5948 struct drm_device *dev = crtc->dev;
5949 struct drm_i915_private *dev_priv = dev->dev_private;
5950 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5951 int pipe = intel_crtc->pipe;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00005952 int plane = intel_crtc->plane;
Jesse Barnes79e53942008-11-07 14:24:08 -08005953 int refclk, num_connectors = 0;
5954 intel_clock_t clock, reduced_clock;
5955 u32 dpll, fp = 0, fp2 = 0, dspcntr, pipeconf;
Eric Anholta07d6782011-03-30 13:01:08 -07005956 bool ok, has_reduced_clock = false, is_sdvo = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08005957 bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
5958 struct intel_encoder *has_edp_encoder = NULL;
5959 struct drm_mode_config *mode_config = &dev->mode_config;
5960 struct intel_encoder *encoder;
5961 const intel_limit_t *limit;
5962 int ret;
5963 struct fdi_m_n m_n = {0};
Eric Anholtfae14982011-03-30 13:01:09 -07005964 u32 temp;
Jesse Barnes79e53942008-11-07 14:24:08 -08005965 u32 lvds_sync = 0;
Jesse Barnes5a354202011-06-24 12:19:22 -07005966 int target_clock, pixel_multiplier, lane, link_bw, factor;
5967 unsigned int pipe_bpp;
5968 bool dither;
Jesse Barnes79e53942008-11-07 14:24:08 -08005969
Jesse Barnes79e53942008-11-07 14:24:08 -08005970 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5971 if (encoder->base.crtc != crtc)
5972 continue;
5973
5974 switch (encoder->type) {
5975 case INTEL_OUTPUT_LVDS:
5976 is_lvds = true;
5977 break;
5978 case INTEL_OUTPUT_SDVO:
5979 case INTEL_OUTPUT_HDMI:
5980 is_sdvo = true;
5981 if (encoder->needs_tv_clock)
5982 is_tv = true;
5983 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005984 case INTEL_OUTPUT_TVOUT:
5985 is_tv = true;
5986 break;
5987 case INTEL_OUTPUT_ANALOG:
5988 is_crt = true;
5989 break;
5990 case INTEL_OUTPUT_DISPLAYPORT:
5991 is_dp = true;
5992 break;
5993 case INTEL_OUTPUT_EDP:
5994 has_edp_encoder = encoder;
5995 break;
5996 }
5997
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005998 num_connectors++;
5999 }
6000
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006001 refclk = ironlake_get_refclk(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006002
6003 /*
6004 * Returns a set of divisors for the desired target clock with the given
6005 * refclk, or FALSE. The returned values represent the clock equation:
6006 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
6007 */
6008 limit = intel_limit(crtc, refclk);
Sean Paulcec2f352012-01-10 15:09:36 -08006009 ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
6010 &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006011 if (!ok) {
6012 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6013 return -EINVAL;
6014 }
6015
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006016 /* Ensure that the cursor is valid for the new mode before changing... */
Chris Wilson6b383a72010-09-13 13:54:26 +01006017 intel_crtc_update_cursor(crtc, true);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006018
Zhao Yakuiddc90032010-01-06 22:05:56 +08006019 if (is_lvds && dev_priv->lvds_downclock_avail) {
Sean Paulcec2f352012-01-10 15:09:36 -08006020 /*
6021 * Ensure we match the reduced clock's P to the target clock.
6022 * If the clocks don't match, we can't switch the display clock
6023 * by using the FP0/FP1. In such case we will disable the LVDS
6024 * downclock feature.
6025 */
Zhao Yakuiddc90032010-01-06 22:05:56 +08006026 has_reduced_clock = limit->find_pll(limit, crtc,
Chris Wilson5eddb702010-09-11 13:48:45 +01006027 dev_priv->lvds_downclock,
6028 refclk,
Sean Paulcec2f352012-01-10 15:09:36 -08006029 &clock,
Chris Wilson5eddb702010-09-11 13:48:45 +01006030 &reduced_clock);
Jesse Barnes652c3932009-08-17 13:31:43 -07006031 }
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08006032 /* SDVO TV has fixed PLL values depend on its clock range,
6033 this mirrors vbios setting. */
6034 if (is_sdvo && is_tv) {
6035 if (adjusted_mode->clock >= 100000
Chris Wilson5eddb702010-09-11 13:48:45 +01006036 && adjusted_mode->clock < 140500) {
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08006037 clock.p1 = 2;
6038 clock.p2 = 10;
6039 clock.n = 3;
6040 clock.m1 = 16;
6041 clock.m2 = 8;
6042 } else if (adjusted_mode->clock >= 140500
Chris Wilson5eddb702010-09-11 13:48:45 +01006043 && adjusted_mode->clock <= 200000) {
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08006044 clock.p1 = 1;
6045 clock.p2 = 10;
6046 clock.n = 6;
6047 clock.m1 = 12;
6048 clock.m2 = 8;
6049 }
6050 }
6051
Zhenyu Wang2c072452009-06-05 15:38:42 +08006052 /* FDI link */
Eric Anholt8febb292011-03-30 13:01:07 -07006053 pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
6054 lane = 0;
6055 /* CPU eDP doesn't require FDI link, so just set DP M/N
6056 according to current link config */
6057 if (has_edp_encoder &&
6058 !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
6059 target_clock = mode->clock;
6060 intel_edp_link_config(has_edp_encoder,
6061 &lane, &link_bw);
6062 } else {
6063 /* [e]DP over FDI requires target mode clock
6064 instead of link clock */
6065 if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08006066 target_clock = mode->clock;
Eric Anholt8febb292011-03-30 13:01:07 -07006067 else
6068 target_clock = adjusted_mode->clock;
Chris Wilson021357a2010-09-07 20:54:59 +01006069
Eric Anholt8febb292011-03-30 13:01:07 -07006070 /* FDI is a binary signal running at ~2.7GHz, encoding
6071 * each output octet as 10 bits. The actual frequency
6072 * is stored as a divider into a 100MHz clock, and the
6073 * mode pixel clock is stored in units of 1KHz.
6074 * Hence the bw of each lane in terms of the mode signal
6075 * is:
6076 */
6077 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08006078 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08006079
Eric Anholt8febb292011-03-30 13:01:07 -07006080 /* determine panel color depth */
6081 temp = I915_READ(PIPECONF(pipe));
6082 temp &= ~PIPE_BPC_MASK;
Adam Jackson3b5c78a2011-12-13 15:41:00 -08006083 dither = intel_choose_pipe_bpp_dither(crtc, &pipe_bpp, mode);
Jesse Barnes5a354202011-06-24 12:19:22 -07006084 switch (pipe_bpp) {
6085 case 18:
6086 temp |= PIPE_6BPC;
6087 break;
6088 case 24:
Eric Anholt8febb292011-03-30 13:01:07 -07006089 temp |= PIPE_8BPC;
Eric Anholt8febb292011-03-30 13:01:07 -07006090 break;
Jesse Barnes5a354202011-06-24 12:19:22 -07006091 case 30:
6092 temp |= PIPE_10BPC;
Eric Anholt8febb292011-03-30 13:01:07 -07006093 break;
Jesse Barnes5a354202011-06-24 12:19:22 -07006094 case 36:
6095 temp |= PIPE_12BPC;
Eric Anholt8febb292011-03-30 13:01:07 -07006096 break;
6097 default:
Jesse Barnes62ac41a2011-07-28 12:55:14 -07006098 WARN(1, "intel_choose_pipe_bpp returned invalid value %d\n",
6099 pipe_bpp);
Jesse Barnes5a354202011-06-24 12:19:22 -07006100 temp |= PIPE_8BPC;
6101 pipe_bpp = 24;
6102 break;
Eric Anholt8febb292011-03-30 13:01:07 -07006103 }
6104
Jesse Barnes5a354202011-06-24 12:19:22 -07006105 intel_crtc->bpp = pipe_bpp;
6106 I915_WRITE(PIPECONF(pipe), temp);
6107
Eric Anholt8febb292011-03-30 13:01:07 -07006108 if (!lane) {
6109 /*
6110 * Account for spread spectrum to avoid
6111 * oversubscribing the link. Max center spread
6112 * is 2.5%; use 5% for safety's sake.
6113 */
Jesse Barnes5a354202011-06-24 12:19:22 -07006114 u32 bps = target_clock * intel_crtc->bpp * 21 / 20;
Eric Anholt8febb292011-03-30 13:01:07 -07006115 lane = bps / (link_bw * 8) + 1;
6116 }
6117
6118 intel_crtc->fdi_lanes = lane;
6119
6120 if (pixel_multiplier > 1)
6121 link_bw *= pixel_multiplier;
Jesse Barnes5a354202011-06-24 12:19:22 -07006122 ironlake_compute_m_n(intel_crtc->bpp, lane, target_clock, link_bw,
6123 &m_n);
Eric Anholt8febb292011-03-30 13:01:07 -07006124
Eric Anholta07d6782011-03-30 13:01:08 -07006125 fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
6126 if (has_reduced_clock)
6127 fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
6128 reduced_clock.m2;
Jesse Barnes79e53942008-11-07 14:24:08 -08006129
Chris Wilsonc1858122010-12-03 21:35:48 +00006130 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07006131 factor = 21;
6132 if (is_lvds) {
6133 if ((intel_panel_use_ssc(dev_priv) &&
6134 dev_priv->lvds_ssc_freq == 100) ||
6135 (I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP)
6136 factor = 25;
6137 } else if (is_sdvo && is_tv)
6138 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00006139
Jesse Barnescb0e0932011-07-28 14:50:30 -07006140 if (clock.m < factor * clock.n)
Eric Anholt8febb292011-03-30 13:01:07 -07006141 fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00006142
Chris Wilson5eddb702010-09-11 13:48:45 +01006143 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006144
Eric Anholta07d6782011-03-30 13:01:08 -07006145 if (is_lvds)
6146 dpll |= DPLLB_MODE_LVDS;
6147 else
6148 dpll |= DPLLB_MODE_DAC_SERIAL;
6149 if (is_sdvo) {
6150 int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
6151 if (pixel_multiplier > 1) {
6152 dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Jesse Barnes79e53942008-11-07 14:24:08 -08006153 }
Eric Anholta07d6782011-03-30 13:01:08 -07006154 dpll |= DPLL_DVO_HIGH_SPEED;
6155 }
6156 if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
6157 dpll |= DPLL_DVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08006158
Eric Anholta07d6782011-03-30 13:01:08 -07006159 /* compute bitmask from p1 value */
6160 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6161 /* also FPA1 */
6162 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
6163
6164 switch (clock.p2) {
6165 case 5:
6166 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6167 break;
6168 case 7:
6169 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6170 break;
6171 case 10:
6172 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6173 break;
6174 case 14:
6175 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6176 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08006177 }
6178
6179 if (is_sdvo && is_tv)
6180 dpll |= PLL_REF_INPUT_TVCLKINBC;
6181 else if (is_tv)
6182 /* XXX: just matching BIOS for now */
6183 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
6184 dpll |= 3;
Chris Wilsona7615032011-01-12 17:04:08 +00006185 else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Jesse Barnes79e53942008-11-07 14:24:08 -08006186 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6187 else
6188 dpll |= PLL_REF_INPUT_DREFCLK;
6189
6190 /* setup pipeconf */
Chris Wilson5eddb702010-09-11 13:48:45 +01006191 pipeconf = I915_READ(PIPECONF(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006192
6193 /* Set up the display plane register */
6194 dspcntr = DISPPLANE_GAMMA_ENABLE;
6195
Jesse Barnesf7cb34d2011-10-12 10:49:14 -07006196 DRM_DEBUG_KMS("Mode for pipe %d:\n", pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08006197 drm_mode_debug_printmodeline(mode);
6198
Jesse Barnes5c5313c2010-10-07 16:01:11 -07006199 /* PCH eDP needs FDI, but CPU eDP does not */
Jesse Barnes4b645f12011-10-12 09:51:31 -07006200 if (!intel_crtc->no_pll) {
6201 if (!has_edp_encoder ||
6202 intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
6203 I915_WRITE(PCH_FP0(pipe), fp);
6204 I915_WRITE(PCH_DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01006205
Jesse Barnes4b645f12011-10-12 09:51:31 -07006206 POSTING_READ(PCH_DPLL(pipe));
6207 udelay(150);
6208 }
6209 } else {
6210 if (dpll == (I915_READ(PCH_DPLL(0)) & 0x7fffffff) &&
6211 fp == I915_READ(PCH_FP0(0))) {
6212 intel_crtc->use_pll_a = true;
6213 DRM_DEBUG_KMS("using pipe a dpll\n");
6214 } else if (dpll == (I915_READ(PCH_DPLL(1)) & 0x7fffffff) &&
6215 fp == I915_READ(PCH_FP0(1))) {
6216 intel_crtc->use_pll_a = false;
6217 DRM_DEBUG_KMS("using pipe b dpll\n");
6218 } else {
6219 DRM_DEBUG_KMS("no matching PLL configuration for pipe 2\n");
6220 return -EINVAL;
6221 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006222 }
6223
6224 /* The LVDS pin pair needs to be on before the DPLLs are enabled.
6225 * This is an exception to the general rule that mode_set doesn't turn
6226 * things on.
6227 */
6228 if (is_lvds) {
Eric Anholtfae14982011-03-30 13:01:09 -07006229 temp = I915_READ(PCH_LVDS);
Chris Wilson5eddb702010-09-11 13:48:45 +01006230 temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
Jesse Barnes7885d202012-01-12 14:51:17 -08006231 if (HAS_PCH_CPT(dev)) {
6232 temp &= ~PORT_TRANS_SEL_MASK;
Jesse Barnes4b645f12011-10-12 09:51:31 -07006233 temp |= PORT_TRANS_SEL_CPT(pipe);
Jesse Barnes7885d202012-01-12 14:51:17 -08006234 } else {
6235 if (pipe == 1)
6236 temp |= LVDS_PIPEB_SELECT;
6237 else
6238 temp &= ~LVDS_PIPEB_SELECT;
6239 }
Jesse Barnes4b645f12011-10-12 09:51:31 -07006240
Zhao Yakuia3e17eb2009-10-10 10:42:37 +08006241 /* set the corresponsding LVDS_BORDER bit */
Chris Wilson5eddb702010-09-11 13:48:45 +01006242 temp |= dev_priv->lvds_border_bits;
Jesse Barnes79e53942008-11-07 14:24:08 -08006243 /* Set the B0-B3 data pairs corresponding to whether we're going to
6244 * set the DPLLs for dual-channel mode or not.
6245 */
6246 if (clock.p2 == 7)
Chris Wilson5eddb702010-09-11 13:48:45 +01006247 temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
Jesse Barnes79e53942008-11-07 14:24:08 -08006248 else
Chris Wilson5eddb702010-09-11 13:48:45 +01006249 temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
Jesse Barnes79e53942008-11-07 14:24:08 -08006250
6251 /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
6252 * appropriately here, but we need to look more thoroughly into how
6253 * panels behave in the two modes.
6254 */
Bryan Freedaa9b5002011-01-12 13:43:19 -08006255 if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
6256 lvds_sync |= LVDS_HSYNC_POLARITY;
6257 if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
6258 lvds_sync |= LVDS_VSYNC_POLARITY;
6259 if ((temp & (LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY))
6260 != lvds_sync) {
6261 char flags[2] = "-+";
6262 DRM_INFO("Changing LVDS panel from "
6263 "(%chsync, %cvsync) to (%chsync, %cvsync)\n",
6264 flags[!(temp & LVDS_HSYNC_POLARITY)],
6265 flags[!(temp & LVDS_VSYNC_POLARITY)],
6266 flags[!(lvds_sync & LVDS_HSYNC_POLARITY)],
6267 flags[!(lvds_sync & LVDS_VSYNC_POLARITY)]);
6268 temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
6269 temp |= lvds_sync;
6270 }
Eric Anholtfae14982011-03-30 13:01:09 -07006271 I915_WRITE(PCH_LVDS, temp);
Jesse Barnes79e53942008-11-07 14:24:08 -08006272 }
Jesse Barnes434ed092010-09-07 14:48:06 -07006273
Eric Anholt8febb292011-03-30 13:01:07 -07006274 pipeconf &= ~PIPECONF_DITHER_EN;
6275 pipeconf &= ~PIPECONF_DITHER_TYPE_MASK;
Jesse Barnes5a354202011-06-24 12:19:22 -07006276 if ((is_lvds && dev_priv->lvds_dither) || dither) {
Eric Anholt8febb292011-03-30 13:01:07 -07006277 pipeconf |= PIPECONF_DITHER_EN;
Daniel Vetterf74974c2011-10-11 17:27:51 +02006278 pipeconf |= PIPECONF_DITHER_TYPE_SP;
Jesse Barnes434ed092010-09-07 14:48:06 -07006279 }
Jesse Barnes5c5313c2010-10-07 16:01:11 -07006280 if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07006281 intel_dp_set_m_n(crtc, mode, adjusted_mode);
Eric Anholt8febb292011-03-30 13:01:07 -07006282 } else {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08006283 /* For non-DP output, clear any trans DP clock recovery setting.*/
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006284 I915_WRITE(TRANSDATA_M1(pipe), 0);
6285 I915_WRITE(TRANSDATA_N1(pipe), 0);
6286 I915_WRITE(TRANSDPLINK_M1(pipe), 0);
6287 I915_WRITE(TRANSDPLINK_N1(pipe), 0);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08006288 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006289
Jesse Barnes4b645f12011-10-12 09:51:31 -07006290 if (!intel_crtc->no_pll &&
6291 (!has_edp_encoder ||
6292 intel_encoder_is_pch_edp(&has_edp_encoder->base))) {
Eric Anholtfae14982011-03-30 13:01:09 -07006293 I915_WRITE(PCH_DPLL(pipe), dpll);
Chris Wilson5eddb702010-09-11 13:48:45 +01006294
Zhenyu Wang32f9d652009-07-24 01:00:32 +08006295 /* Wait for the clocks to stabilize. */
Eric Anholtfae14982011-03-30 13:01:09 -07006296 POSTING_READ(PCH_DPLL(pipe));
Zhenyu Wang32f9d652009-07-24 01:00:32 +08006297 udelay(150);
6298
Eric Anholt8febb292011-03-30 13:01:07 -07006299 /* The pixel multiplier can only be updated once the
6300 * DPLL is enabled and the clocks are stable.
6301 *
6302 * So write it again.
6303 */
Eric Anholtfae14982011-03-30 13:01:09 -07006304 I915_WRITE(PCH_DPLL(pipe), dpll);
Jesse Barnes79e53942008-11-07 14:24:08 -08006305 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006306
Chris Wilson5eddb702010-09-11 13:48:45 +01006307 intel_crtc->lowfreq_avail = false;
Jesse Barnes4b645f12011-10-12 09:51:31 -07006308 if (!intel_crtc->no_pll) {
6309 if (is_lvds && has_reduced_clock && i915_powersave) {
6310 I915_WRITE(PCH_FP1(pipe), fp2);
6311 intel_crtc->lowfreq_avail = true;
6312 if (HAS_PIPE_CXSR(dev)) {
6313 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
6314 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
6315 }
6316 } else {
6317 I915_WRITE(PCH_FP1(pipe), fp);
6318 if (HAS_PIPE_CXSR(dev)) {
6319 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
6320 pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
6321 }
Jesse Barnes652c3932009-08-17 13:31:43 -07006322 }
6323 }
6324
Keith Packard617cf882012-02-08 13:53:38 -08006325 pipeconf &= ~PIPECONF_INTERLACE_MASK;
Krzysztof Halasa734b4152010-05-25 18:41:46 +02006326 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Daniel Vetter5def4742012-01-28 14:49:22 +01006327 pipeconf |= PIPECONF_INTERLACED_ILK;
Krzysztof Halasa734b4152010-05-25 18:41:46 +02006328 /* the chip adds 2 halflines automatically */
Krzysztof Halasa734b4152010-05-25 18:41:46 +02006329 adjusted_mode->crtc_vtotal -= 1;
Krzysztof Halasa734b4152010-05-25 18:41:46 +02006330 adjusted_mode->crtc_vblank_end -= 1;
Daniel Vetter0529a0d2012-01-28 14:49:24 +01006331 I915_WRITE(VSYNCSHIFT(pipe),
6332 adjusted_mode->crtc_hsync_start
6333 - adjusted_mode->crtc_htotal/2);
6334 } else {
Keith Packard617cf882012-02-08 13:53:38 -08006335 pipeconf |= PIPECONF_PROGRESSIVE;
Daniel Vetter0529a0d2012-01-28 14:49:24 +01006336 I915_WRITE(VSYNCSHIFT(pipe), 0);
6337 }
Krzysztof Halasa734b4152010-05-25 18:41:46 +02006338
Chris Wilson5eddb702010-09-11 13:48:45 +01006339 I915_WRITE(HTOTAL(pipe),
6340 (adjusted_mode->crtc_hdisplay - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08006341 ((adjusted_mode->crtc_htotal - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01006342 I915_WRITE(HBLANK(pipe),
6343 (adjusted_mode->crtc_hblank_start - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08006344 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01006345 I915_WRITE(HSYNC(pipe),
6346 (adjusted_mode->crtc_hsync_start - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08006347 ((adjusted_mode->crtc_hsync_end - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01006348
6349 I915_WRITE(VTOTAL(pipe),
6350 (adjusted_mode->crtc_vdisplay - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08006351 ((adjusted_mode->crtc_vtotal - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01006352 I915_WRITE(VBLANK(pipe),
6353 (adjusted_mode->crtc_vblank_start - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08006354 ((adjusted_mode->crtc_vblank_end - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01006355 I915_WRITE(VSYNC(pipe),
6356 (adjusted_mode->crtc_vsync_start - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08006357 ((adjusted_mode->crtc_vsync_end - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01006358
Eric Anholt8febb292011-03-30 13:01:07 -07006359 /* pipesrc controls the size that is scaled from, which should
6360 * always be the user's requested size.
Jesse Barnes79e53942008-11-07 14:24:08 -08006361 */
Chris Wilson5eddb702010-09-11 13:48:45 +01006362 I915_WRITE(PIPESRC(pipe),
6363 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
Zhenyu Wang2c072452009-06-05 15:38:42 +08006364
Eric Anholt8febb292011-03-30 13:01:07 -07006365 I915_WRITE(PIPE_DATA_M1(pipe), TU_SIZE(m_n.tu) | m_n.gmch_m);
6366 I915_WRITE(PIPE_DATA_N1(pipe), m_n.gmch_n);
6367 I915_WRITE(PIPE_LINK_M1(pipe), m_n.link_m);
6368 I915_WRITE(PIPE_LINK_N1(pipe), m_n.link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006369
Eric Anholt8febb292011-03-30 13:01:07 -07006370 if (has_edp_encoder &&
6371 !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
6372 ironlake_set_pll_edp(crtc, adjusted_mode->clock);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006373 }
6374
Chris Wilson5eddb702010-09-11 13:48:45 +01006375 I915_WRITE(PIPECONF(pipe), pipeconf);
6376 POSTING_READ(PIPECONF(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006377
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006378 intel_wait_for_vblank(dev, pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08006379
Chris Wilson5eddb702010-09-11 13:48:45 +01006380 I915_WRITE(DSPCNTR(plane), dspcntr);
Jesse Barnesb24e7172011-01-04 15:09:30 -08006381 POSTING_READ(DSPCNTR(plane));
Jesse Barnes79e53942008-11-07 14:24:08 -08006382
Chris Wilson5c3b82e2009-02-11 13:25:09 +00006383 ret = intel_pipe_set_base(crtc, x, y, old_fb);
Shaohua Li7662c8b2009-06-26 11:23:55 +08006384
6385 intel_update_watermarks(dev);
6386
Chris Wilson1f803ee2009-06-06 09:45:59 +01006387 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006388}
6389
Eric Anholtf564048e2011-03-30 13:01:02 -07006390static int intel_crtc_mode_set(struct drm_crtc *crtc,
6391 struct drm_display_mode *mode,
6392 struct drm_display_mode *adjusted_mode,
6393 int x, int y,
6394 struct drm_framebuffer *old_fb)
6395{
6396 struct drm_device *dev = crtc->dev;
6397 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt0b701d22011-03-30 13:01:03 -07006398 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6399 int pipe = intel_crtc->pipe;
Eric Anholtf564048e2011-03-30 13:01:02 -07006400 int ret;
6401
Eric Anholt0b701d22011-03-30 13:01:03 -07006402 drm_vblank_pre_modeset(dev, pipe);
6403
Eric Anholtf564048e2011-03-30 13:01:02 -07006404 ret = dev_priv->display.crtc_mode_set(crtc, mode, adjusted_mode,
6405 x, y, old_fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08006406 drm_vblank_post_modeset(dev, pipe);
6407
Jesse Barnesd8e70a22011-11-15 10:28:54 -08006408 if (ret)
6409 intel_crtc->dpms_mode = DRM_MODE_DPMS_OFF;
6410 else
6411 intel_crtc->dpms_mode = DRM_MODE_DPMS_ON;
Keith Packard120eced2011-07-27 01:21:40 -07006412
Jesse Barnes79e53942008-11-07 14:24:08 -08006413 return ret;
6414}
6415
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006416static bool intel_eld_uptodate(struct drm_connector *connector,
6417 int reg_eldv, uint32_t bits_eldv,
6418 int reg_elda, uint32_t bits_elda,
6419 int reg_edid)
6420{
6421 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6422 uint8_t *eld = connector->eld;
6423 uint32_t i;
6424
6425 i = I915_READ(reg_eldv);
6426 i &= bits_eldv;
6427
6428 if (!eld[0])
6429 return !i;
6430
6431 if (!i)
6432 return false;
6433
6434 i = I915_READ(reg_elda);
6435 i &= ~bits_elda;
6436 I915_WRITE(reg_elda, i);
6437
6438 for (i = 0; i < eld[2]; i++)
6439 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
6440 return false;
6441
6442 return true;
6443}
6444
Wu Fengguange0dac652011-09-05 14:25:34 +08006445static void g4x_write_eld(struct drm_connector *connector,
6446 struct drm_crtc *crtc)
6447{
6448 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6449 uint8_t *eld = connector->eld;
6450 uint32_t eldv;
6451 uint32_t len;
6452 uint32_t i;
6453
6454 i = I915_READ(G4X_AUD_VID_DID);
6455
6456 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
6457 eldv = G4X_ELDV_DEVCL_DEVBLC;
6458 else
6459 eldv = G4X_ELDV_DEVCTG;
6460
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006461 if (intel_eld_uptodate(connector,
6462 G4X_AUD_CNTL_ST, eldv,
6463 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
6464 G4X_HDMIW_HDMIEDID))
6465 return;
6466
Wu Fengguange0dac652011-09-05 14:25:34 +08006467 i = I915_READ(G4X_AUD_CNTL_ST);
6468 i &= ~(eldv | G4X_ELD_ADDR);
6469 len = (i >> 9) & 0x1f; /* ELD buffer size */
6470 I915_WRITE(G4X_AUD_CNTL_ST, i);
6471
6472 if (!eld[0])
6473 return;
6474
6475 len = min_t(uint8_t, eld[2], len);
6476 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6477 for (i = 0; i < len; i++)
6478 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
6479
6480 i = I915_READ(G4X_AUD_CNTL_ST);
6481 i |= eldv;
6482 I915_WRITE(G4X_AUD_CNTL_ST, i);
6483}
6484
6485static void ironlake_write_eld(struct drm_connector *connector,
6486 struct drm_crtc *crtc)
6487{
6488 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6489 uint8_t *eld = connector->eld;
6490 uint32_t eldv;
6491 uint32_t i;
6492 int len;
6493 int hdmiw_hdmiedid;
Wu Fengguangb6daa022012-01-06 14:41:31 -06006494 int aud_config;
Wu Fengguange0dac652011-09-05 14:25:34 +08006495 int aud_cntl_st;
6496 int aud_cntrl_st2;
6497
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08006498 if (HAS_PCH_IBX(connector->dev)) {
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006499 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID_A;
Wu Fengguangb6daa022012-01-06 14:41:31 -06006500 aud_config = IBX_AUD_CONFIG_A;
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006501 aud_cntl_st = IBX_AUD_CNTL_ST_A;
6502 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08006503 } else {
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006504 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID_A;
Wu Fengguangb6daa022012-01-06 14:41:31 -06006505 aud_config = CPT_AUD_CONFIG_A;
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006506 aud_cntl_st = CPT_AUD_CNTL_ST_A;
6507 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08006508 }
6509
6510 i = to_intel_crtc(crtc)->pipe;
6511 hdmiw_hdmiedid += i * 0x100;
6512 aud_cntl_st += i * 0x100;
Wu Fengguangb6daa022012-01-06 14:41:31 -06006513 aud_config += i * 0x100;
Wu Fengguange0dac652011-09-05 14:25:34 +08006514
6515 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(i));
6516
6517 i = I915_READ(aud_cntl_st);
6518 i = (i >> 29) & 0x3; /* DIP_Port_Select, 0x1 = PortB */
6519 if (!i) {
6520 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
6521 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006522 eldv = IBX_ELD_VALIDB;
6523 eldv |= IBX_ELD_VALIDB << 4;
6524 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08006525 } else {
6526 DRM_DEBUG_DRIVER("ELD on port %c\n", 'A' + i);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006527 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08006528 }
6529
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006530 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6531 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6532 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
Wu Fengguangb6daa022012-01-06 14:41:31 -06006533 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6534 } else
6535 I915_WRITE(aud_config, 0);
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006536
6537 if (intel_eld_uptodate(connector,
6538 aud_cntrl_st2, eldv,
6539 aud_cntl_st, IBX_ELD_ADDRESS,
6540 hdmiw_hdmiedid))
6541 return;
6542
Wu Fengguange0dac652011-09-05 14:25:34 +08006543 i = I915_READ(aud_cntrl_st2);
6544 i &= ~eldv;
6545 I915_WRITE(aud_cntrl_st2, i);
6546
6547 if (!eld[0])
6548 return;
6549
Wu Fengguange0dac652011-09-05 14:25:34 +08006550 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006551 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08006552 I915_WRITE(aud_cntl_st, i);
6553
6554 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6555 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6556 for (i = 0; i < len; i++)
6557 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6558
6559 i = I915_READ(aud_cntrl_st2);
6560 i |= eldv;
6561 I915_WRITE(aud_cntrl_st2, i);
6562}
6563
6564void intel_write_eld(struct drm_encoder *encoder,
6565 struct drm_display_mode *mode)
6566{
6567 struct drm_crtc *crtc = encoder->crtc;
6568 struct drm_connector *connector;
6569 struct drm_device *dev = encoder->dev;
6570 struct drm_i915_private *dev_priv = dev->dev_private;
6571
6572 connector = drm_select_eld(encoder, mode);
6573 if (!connector)
6574 return;
6575
6576 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6577 connector->base.id,
6578 drm_get_connector_name(connector),
6579 connector->encoder->base.id,
6580 drm_get_encoder_name(connector->encoder));
6581
6582 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
6583
6584 if (dev_priv->display.write_eld)
6585 dev_priv->display.write_eld(connector, crtc);
6586}
6587
Jesse Barnes79e53942008-11-07 14:24:08 -08006588/** Loads the palette/gamma unit for the CRTC with the prepared values */
6589void intel_crtc_load_lut(struct drm_crtc *crtc)
6590{
6591 struct drm_device *dev = crtc->dev;
6592 struct drm_i915_private *dev_priv = dev->dev_private;
6593 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006594 int palreg = PALETTE(intel_crtc->pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08006595 int i;
6596
6597 /* The clocks have to be on to load the palette. */
6598 if (!crtc->enabled)
6599 return;
6600
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006601 /* use legacy palette for Ironlake */
Eric Anholtbad720f2009-10-22 16:11:14 -07006602 if (HAS_PCH_SPLIT(dev))
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006603 palreg = LGC_PALETTE(intel_crtc->pipe);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006604
Jesse Barnes79e53942008-11-07 14:24:08 -08006605 for (i = 0; i < 256; i++) {
6606 I915_WRITE(palreg + 4 * i,
6607 (intel_crtc->lut_r[i] << 16) |
6608 (intel_crtc->lut_g[i] << 8) |
6609 intel_crtc->lut_b[i]);
6610 }
6611}
6612
Chris Wilson560b85b2010-08-07 11:01:38 +01006613static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
6614{
6615 struct drm_device *dev = crtc->dev;
6616 struct drm_i915_private *dev_priv = dev->dev_private;
6617 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6618 bool visible = base != 0;
6619 u32 cntl;
6620
6621 if (intel_crtc->cursor_visible == visible)
6622 return;
6623
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006624 cntl = I915_READ(_CURACNTR);
Chris Wilson560b85b2010-08-07 11:01:38 +01006625 if (visible) {
6626 /* On these chipsets we can only modify the base whilst
6627 * the cursor is disabled.
6628 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006629 I915_WRITE(_CURABASE, base);
Chris Wilson560b85b2010-08-07 11:01:38 +01006630
6631 cntl &= ~(CURSOR_FORMAT_MASK);
6632 /* XXX width must be 64, stride 256 => 0x00 << 28 */
6633 cntl |= CURSOR_ENABLE |
6634 CURSOR_GAMMA_ENABLE |
6635 CURSOR_FORMAT_ARGB;
6636 } else
6637 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006638 I915_WRITE(_CURACNTR, cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01006639
6640 intel_crtc->cursor_visible = visible;
6641}
6642
6643static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
6644{
6645 struct drm_device *dev = crtc->dev;
6646 struct drm_i915_private *dev_priv = dev->dev_private;
6647 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6648 int pipe = intel_crtc->pipe;
6649 bool visible = base != 0;
6650
6651 if (intel_crtc->cursor_visible != visible) {
Jesse Barnes548f2452011-02-17 10:40:53 -08006652 uint32_t cntl = I915_READ(CURCNTR(pipe));
Chris Wilson560b85b2010-08-07 11:01:38 +01006653 if (base) {
6654 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
6655 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6656 cntl |= pipe << 28; /* Connect to correct pipe */
6657 } else {
6658 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6659 cntl |= CURSOR_MODE_DISABLE;
6660 }
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006661 I915_WRITE(CURCNTR(pipe), cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01006662
6663 intel_crtc->cursor_visible = visible;
6664 }
6665 /* and commit changes on next vblank */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006666 I915_WRITE(CURBASE(pipe), base);
Chris Wilson560b85b2010-08-07 11:01:38 +01006667}
6668
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006669static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
6670{
6671 struct drm_device *dev = crtc->dev;
6672 struct drm_i915_private *dev_priv = dev->dev_private;
6673 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6674 int pipe = intel_crtc->pipe;
6675 bool visible = base != 0;
6676
6677 if (intel_crtc->cursor_visible != visible) {
6678 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
6679 if (base) {
6680 cntl &= ~CURSOR_MODE;
6681 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6682 } else {
6683 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6684 cntl |= CURSOR_MODE_DISABLE;
6685 }
6686 I915_WRITE(CURCNTR_IVB(pipe), cntl);
6687
6688 intel_crtc->cursor_visible = visible;
6689 }
6690 /* and commit changes on next vblank */
6691 I915_WRITE(CURBASE_IVB(pipe), base);
6692}
6693
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006694/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01006695static void intel_crtc_update_cursor(struct drm_crtc *crtc,
6696 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006697{
6698 struct drm_device *dev = crtc->dev;
6699 struct drm_i915_private *dev_priv = dev->dev_private;
6700 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6701 int pipe = intel_crtc->pipe;
6702 int x = intel_crtc->cursor_x;
6703 int y = intel_crtc->cursor_y;
Chris Wilson560b85b2010-08-07 11:01:38 +01006704 u32 base, pos;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006705 bool visible;
6706
6707 pos = 0;
6708
Chris Wilson6b383a72010-09-13 13:54:26 +01006709 if (on && crtc->enabled && crtc->fb) {
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006710 base = intel_crtc->cursor_addr;
6711 if (x > (int) crtc->fb->width)
6712 base = 0;
6713
6714 if (y > (int) crtc->fb->height)
6715 base = 0;
6716 } else
6717 base = 0;
6718
6719 if (x < 0) {
6720 if (x + intel_crtc->cursor_width < 0)
6721 base = 0;
6722
6723 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
6724 x = -x;
6725 }
6726 pos |= x << CURSOR_X_SHIFT;
6727
6728 if (y < 0) {
6729 if (y + intel_crtc->cursor_height < 0)
6730 base = 0;
6731
6732 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
6733 y = -y;
6734 }
6735 pos |= y << CURSOR_Y_SHIFT;
6736
6737 visible = base != 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01006738 if (!visible && !intel_crtc->cursor_visible)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006739 return;
6740
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006741 if (IS_IVYBRIDGE(dev)) {
6742 I915_WRITE(CURPOS_IVB(pipe), pos);
6743 ivb_update_cursor(crtc, base);
6744 } else {
6745 I915_WRITE(CURPOS(pipe), pos);
6746 if (IS_845G(dev) || IS_I865G(dev))
6747 i845_update_cursor(crtc, base);
6748 else
6749 i9xx_update_cursor(crtc, base);
6750 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006751
6752 if (visible)
6753 intel_mark_busy(dev, to_intel_framebuffer(crtc->fb)->obj);
6754}
6755
Jesse Barnes79e53942008-11-07 14:24:08 -08006756static int intel_crtc_cursor_set(struct drm_crtc *crtc,
Chris Wilson05394f32010-11-08 19:18:58 +00006757 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08006758 uint32_t handle,
6759 uint32_t width, uint32_t height)
6760{
6761 struct drm_device *dev = crtc->dev;
6762 struct drm_i915_private *dev_priv = dev->dev_private;
6763 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00006764 struct drm_i915_gem_object *obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006765 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006766 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006767
Zhao Yakui28c97732009-10-09 11:39:41 +08006768 DRM_DEBUG_KMS("\n");
Jesse Barnes79e53942008-11-07 14:24:08 -08006769
6770 /* if we want to turn off the cursor ignore width and height */
6771 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08006772 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006773 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00006774 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10006775 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006776 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08006777 }
6778
6779 /* Currently we only support 64x64 cursors */
6780 if (width != 64 || height != 64) {
6781 DRM_ERROR("we currently only support 64x64 cursors\n");
6782 return -EINVAL;
6783 }
6784
Chris Wilson05394f32010-11-08 19:18:58 +00006785 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00006786 if (&obj->base == NULL)
Jesse Barnes79e53942008-11-07 14:24:08 -08006787 return -ENOENT;
6788
Chris Wilson05394f32010-11-08 19:18:58 +00006789 if (obj->base.size < width * height * 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006790 DRM_ERROR("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10006791 ret = -ENOMEM;
6792 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08006793 }
6794
Dave Airlie71acb5e2008-12-30 20:31:46 +10006795 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006796 mutex_lock(&dev->struct_mutex);
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05006797 if (!dev_priv->info->cursor_needs_physical) {
Chris Wilsond9e86c02010-11-10 16:40:20 +00006798 if (obj->tiling_mode) {
6799 DRM_ERROR("cursor cannot be tiled\n");
6800 ret = -EINVAL;
6801 goto fail_locked;
6802 }
6803
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006804 ret = i915_gem_object_pin_to_display_plane(obj, 0, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01006805 if (ret) {
6806 DRM_ERROR("failed to move cursor bo into the GTT\n");
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006807 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006808 }
6809
Chris Wilsond9e86c02010-11-10 16:40:20 +00006810 ret = i915_gem_object_put_fence(obj);
6811 if (ret) {
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006812 DRM_ERROR("failed to release fence for cursor");
Chris Wilsond9e86c02010-11-10 16:40:20 +00006813 goto fail_unpin;
6814 }
6815
Chris Wilson05394f32010-11-08 19:18:58 +00006816 addr = obj->gtt_offset;
Dave Airlie71acb5e2008-12-30 20:31:46 +10006817 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01006818 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson05394f32010-11-08 19:18:58 +00006819 ret = i915_gem_attach_phys_object(dev, obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01006820 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
6821 align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10006822 if (ret) {
6823 DRM_ERROR("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006824 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10006825 }
Chris Wilson05394f32010-11-08 19:18:58 +00006826 addr = obj->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006827 }
6828
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006829 if (IS_GEN2(dev))
Jesse Barnes14b60392009-05-20 16:47:08 -04006830 I915_WRITE(CURSIZE, (height << 12) | width);
6831
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006832 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006833 if (intel_crtc->cursor_bo) {
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05006834 if (dev_priv->info->cursor_needs_physical) {
Chris Wilson05394f32010-11-08 19:18:58 +00006835 if (intel_crtc->cursor_bo != obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10006836 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
6837 } else
6838 i915_gem_object_unpin(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00006839 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006840 }
Jesse Barnes80824002009-09-10 15:28:06 -07006841
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006842 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006843
6844 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00006845 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006846 intel_crtc->cursor_width = width;
6847 intel_crtc->cursor_height = height;
6848
Chris Wilson6b383a72010-09-13 13:54:26 +01006849 intel_crtc_update_cursor(crtc, true);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006850
Jesse Barnes79e53942008-11-07 14:24:08 -08006851 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006852fail_unpin:
Chris Wilson05394f32010-11-08 19:18:58 +00006853 i915_gem_object_unpin(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006854fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10006855 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00006856fail:
Chris Wilson05394f32010-11-08 19:18:58 +00006857 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10006858 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006859}
6860
6861static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
6862{
Jesse Barnes79e53942008-11-07 14:24:08 -08006863 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006864
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006865 intel_crtc->cursor_x = x;
6866 intel_crtc->cursor_y = y;
Jesse Barnes652c3932009-08-17 13:31:43 -07006867
Chris Wilson6b383a72010-09-13 13:54:26 +01006868 intel_crtc_update_cursor(crtc, true);
Jesse Barnes79e53942008-11-07 14:24:08 -08006869
6870 return 0;
6871}
6872
6873/** Sets the color ramps on behalf of RandR */
6874void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
6875 u16 blue, int regno)
6876{
6877 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6878
6879 intel_crtc->lut_r[regno] = red >> 8;
6880 intel_crtc->lut_g[regno] = green >> 8;
6881 intel_crtc->lut_b[regno] = blue >> 8;
6882}
6883
Dave Airlieb8c00ac2009-10-06 13:54:01 +10006884void intel_crtc_fb_gamma_get(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 *red = intel_crtc->lut_r[regno] << 8;
6890 *green = intel_crtc->lut_g[regno] << 8;
6891 *blue = intel_crtc->lut_b[regno] << 8;
6892}
6893
Jesse Barnes79e53942008-11-07 14:24:08 -08006894static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01006895 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08006896{
James Simmons72034252010-08-03 01:33:19 +01006897 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08006898 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006899
James Simmons72034252010-08-03 01:33:19 +01006900 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006901 intel_crtc->lut_r[i] = red[i] >> 8;
6902 intel_crtc->lut_g[i] = green[i] >> 8;
6903 intel_crtc->lut_b[i] = blue[i] >> 8;
6904 }
6905
6906 intel_crtc_load_lut(crtc);
6907}
6908
6909/**
6910 * Get a pipe with a simple mode set on it for doing load-based monitor
6911 * detection.
6912 *
6913 * It will be up to the load-detect code to adjust the pipe as appropriate for
Eric Anholtc751ce42010-03-25 11:48:48 -07006914 * its requirements. The pipe will be connected to no other encoders.
Jesse Barnes79e53942008-11-07 14:24:08 -08006915 *
Eric Anholtc751ce42010-03-25 11:48:48 -07006916 * Currently this code will only succeed if there is a pipe with no encoders
Jesse Barnes79e53942008-11-07 14:24:08 -08006917 * configured for it. In the future, it could choose to temporarily disable
6918 * some outputs to free up a pipe for its use.
6919 *
6920 * \return crtc, or NULL if no pipes are available.
6921 */
6922
6923/* VESA 640x480x72Hz mode to set on the pipe */
6924static struct drm_display_mode load_detect_mode = {
6925 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
6926 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
6927};
6928
Chris Wilsond2dff872011-04-19 08:36:26 +01006929static struct drm_framebuffer *
6930intel_framebuffer_create(struct drm_device *dev,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006931 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilsond2dff872011-04-19 08:36:26 +01006932 struct drm_i915_gem_object *obj)
6933{
6934 struct intel_framebuffer *intel_fb;
6935 int ret;
6936
6937 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
6938 if (!intel_fb) {
6939 drm_gem_object_unreference_unlocked(&obj->base);
6940 return ERR_PTR(-ENOMEM);
6941 }
6942
6943 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
6944 if (ret) {
6945 drm_gem_object_unreference_unlocked(&obj->base);
6946 kfree(intel_fb);
6947 return ERR_PTR(ret);
6948 }
6949
6950 return &intel_fb->base;
6951}
6952
6953static u32
6954intel_framebuffer_pitch_for_width(int width, int bpp)
6955{
6956 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
6957 return ALIGN(pitch, 64);
6958}
6959
6960static u32
6961intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
6962{
6963 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
6964 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
6965}
6966
6967static struct drm_framebuffer *
6968intel_framebuffer_create_for_mode(struct drm_device *dev,
6969 struct drm_display_mode *mode,
6970 int depth, int bpp)
6971{
6972 struct drm_i915_gem_object *obj;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006973 struct drm_mode_fb_cmd2 mode_cmd;
Chris Wilsond2dff872011-04-19 08:36:26 +01006974
6975 obj = i915_gem_alloc_object(dev,
6976 intel_framebuffer_size_for_mode(mode, bpp));
6977 if (obj == NULL)
6978 return ERR_PTR(-ENOMEM);
6979
6980 mode_cmd.width = mode->hdisplay;
6981 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006982 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
6983 bpp);
6984 mode_cmd.pixel_format = 0;
Chris Wilsond2dff872011-04-19 08:36:26 +01006985
6986 return intel_framebuffer_create(dev, &mode_cmd, obj);
6987}
6988
6989static struct drm_framebuffer *
6990mode_fits_in_fbdev(struct drm_device *dev,
6991 struct drm_display_mode *mode)
6992{
6993 struct drm_i915_private *dev_priv = dev->dev_private;
6994 struct drm_i915_gem_object *obj;
6995 struct drm_framebuffer *fb;
6996
6997 if (dev_priv->fbdev == NULL)
6998 return NULL;
6999
7000 obj = dev_priv->fbdev->ifb.obj;
7001 if (obj == NULL)
7002 return NULL;
7003
7004 fb = &dev_priv->fbdev->ifb.base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007005 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
7006 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01007007 return NULL;
7008
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007009 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01007010 return NULL;
7011
7012 return fb;
7013}
7014
Chris Wilson71731882011-04-19 23:10:58 +01007015bool intel_get_load_detect_pipe(struct intel_encoder *intel_encoder,
7016 struct drm_connector *connector,
7017 struct drm_display_mode *mode,
Chris Wilson8261b192011-04-19 23:18:09 +01007018 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08007019{
7020 struct intel_crtc *intel_crtc;
7021 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01007022 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08007023 struct drm_crtc *crtc = NULL;
7024 struct drm_device *dev = encoder->dev;
Chris Wilsond2dff872011-04-19 08:36:26 +01007025 struct drm_framebuffer *old_fb;
Jesse Barnes79e53942008-11-07 14:24:08 -08007026 int i = -1;
7027
Chris Wilsond2dff872011-04-19 08:36:26 +01007028 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7029 connector->base.id, drm_get_connector_name(connector),
7030 encoder->base.id, drm_get_encoder_name(encoder));
7031
Jesse Barnes79e53942008-11-07 14:24:08 -08007032 /*
7033 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01007034 *
Jesse Barnes79e53942008-11-07 14:24:08 -08007035 * - if the connector already has an assigned crtc, use it (but make
7036 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01007037 *
Jesse Barnes79e53942008-11-07 14:24:08 -08007038 * - try to find the first unused crtc that can drive this connector,
7039 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08007040 */
7041
7042 /* See if we already have a CRTC for this connector */
7043 if (encoder->crtc) {
7044 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01007045
Jesse Barnes79e53942008-11-07 14:24:08 -08007046 intel_crtc = to_intel_crtc(crtc);
Chris Wilson8261b192011-04-19 23:18:09 +01007047 old->dpms_mode = intel_crtc->dpms_mode;
7048 old->load_detect_temp = false;
7049
7050 /* Make sure the crtc and connector are running */
Jesse Barnes79e53942008-11-07 14:24:08 -08007051 if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
Chris Wilson64927112011-04-20 07:25:26 +01007052 struct drm_encoder_helper_funcs *encoder_funcs;
7053 struct drm_crtc_helper_funcs *crtc_funcs;
7054
Jesse Barnes79e53942008-11-07 14:24:08 -08007055 crtc_funcs = crtc->helper_private;
7056 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
Chris Wilson64927112011-04-20 07:25:26 +01007057
7058 encoder_funcs = encoder->helper_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08007059 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
7060 }
Chris Wilson8261b192011-04-19 23:18:09 +01007061
Chris Wilson71731882011-04-19 23:10:58 +01007062 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08007063 }
7064
7065 /* Find an unused one (if possible) */
7066 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
7067 i++;
7068 if (!(encoder->possible_crtcs & (1 << i)))
7069 continue;
7070 if (!possible_crtc->enabled) {
7071 crtc = possible_crtc;
7072 break;
7073 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007074 }
7075
7076 /*
7077 * If we didn't find an unused CRTC, don't use any.
7078 */
7079 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01007080 DRM_DEBUG_KMS("no pipe available for load-detect\n");
7081 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007082 }
7083
7084 encoder->crtc = crtc;
Zhenyu Wangc1c43972010-03-30 14:39:30 +08007085 connector->encoder = encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08007086
7087 intel_crtc = to_intel_crtc(crtc);
Chris Wilson8261b192011-04-19 23:18:09 +01007088 old->dpms_mode = intel_crtc->dpms_mode;
7089 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01007090 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08007091
Chris Wilson64927112011-04-20 07:25:26 +01007092 if (!mode)
7093 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08007094
Chris Wilsond2dff872011-04-19 08:36:26 +01007095 old_fb = crtc->fb;
7096
7097 /* We need a framebuffer large enough to accommodate all accesses
7098 * that the plane may generate whilst we perform load detection.
7099 * We can not rely on the fbcon either being present (we get called
7100 * during its initialisation to detect all boot displays, or it may
7101 * not even exist) or that it is large enough to satisfy the
7102 * requested mode.
7103 */
7104 crtc->fb = mode_fits_in_fbdev(dev, mode);
7105 if (crtc->fb == NULL) {
7106 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
7107 crtc->fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
7108 old->release_fb = crtc->fb;
7109 } else
7110 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
7111 if (IS_ERR(crtc->fb)) {
7112 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
7113 crtc->fb = old_fb;
7114 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007115 }
Chris Wilsond2dff872011-04-19 08:36:26 +01007116
7117 if (!drm_crtc_helper_set_mode(crtc, mode, 0, 0, old_fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01007118 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01007119 if (old->release_fb)
7120 old->release_fb->funcs->destroy(old->release_fb);
7121 crtc->fb = old_fb;
Chris Wilson64927112011-04-20 07:25:26 +01007122 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007123 }
Chris Wilson71731882011-04-19 23:10:58 +01007124
Jesse Barnes79e53942008-11-07 14:24:08 -08007125 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007126 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08007127
Chris Wilson71731882011-04-19 23:10:58 +01007128 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08007129}
7130
Zhenyu Wangc1c43972010-03-30 14:39:30 +08007131void intel_release_load_detect_pipe(struct intel_encoder *intel_encoder,
Chris Wilson8261b192011-04-19 23:18:09 +01007132 struct drm_connector *connector,
7133 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08007134{
Chris Wilson4ef69c72010-09-09 15:14:28 +01007135 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08007136 struct drm_device *dev = encoder->dev;
7137 struct drm_crtc *crtc = encoder->crtc;
7138 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
7139 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
7140
Chris Wilsond2dff872011-04-19 08:36:26 +01007141 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7142 connector->base.id, drm_get_connector_name(connector),
7143 encoder->base.id, drm_get_encoder_name(encoder));
7144
Chris Wilson8261b192011-04-19 23:18:09 +01007145 if (old->load_detect_temp) {
Zhenyu Wangc1c43972010-03-30 14:39:30 +08007146 connector->encoder = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08007147 drm_helper_disable_unused_functions(dev);
Chris Wilsond2dff872011-04-19 08:36:26 +01007148
7149 if (old->release_fb)
7150 old->release_fb->funcs->destroy(old->release_fb);
7151
Chris Wilson0622a532011-04-21 09:32:11 +01007152 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08007153 }
7154
Eric Anholtc751ce42010-03-25 11:48:48 -07007155 /* Switch crtc and encoder back off if necessary */
Chris Wilson0622a532011-04-21 09:32:11 +01007156 if (old->dpms_mode != DRM_MODE_DPMS_ON) {
7157 encoder_funcs->dpms(encoder, old->dpms_mode);
Chris Wilson8261b192011-04-19 23:18:09 +01007158 crtc_funcs->dpms(crtc, old->dpms_mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08007159 }
7160}
7161
7162/* Returns the clock of the currently programmed mode of the given pipe. */
7163static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
7164{
7165 struct drm_i915_private *dev_priv = dev->dev_private;
7166 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7167 int pipe = intel_crtc->pipe;
Jesse Barnes548f2452011-02-17 10:40:53 -08007168 u32 dpll = I915_READ(DPLL(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08007169 u32 fp;
7170 intel_clock_t clock;
7171
7172 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Chris Wilson39adb7a2011-04-22 22:17:21 +01007173 fp = I915_READ(FP0(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08007174 else
Chris Wilson39adb7a2011-04-22 22:17:21 +01007175 fp = I915_READ(FP1(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08007176
7177 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05007178 if (IS_PINEVIEW(dev)) {
7179 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
7180 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08007181 } else {
7182 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
7183 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
7184 }
7185
Chris Wilsona6c45cf2010-09-17 00:32:17 +01007186 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05007187 if (IS_PINEVIEW(dev))
7188 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
7189 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08007190 else
7191 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08007192 DPLL_FPA01_P1_POST_DIV_SHIFT);
7193
7194 switch (dpll & DPLL_MODE_MASK) {
7195 case DPLLB_MODE_DAC_SERIAL:
7196 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
7197 5 : 10;
7198 break;
7199 case DPLLB_MODE_LVDS:
7200 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
7201 7 : 14;
7202 break;
7203 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08007204 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08007205 "mode\n", (int)(dpll & DPLL_MODE_MASK));
7206 return 0;
7207 }
7208
7209 /* XXX: Handle the 100Mhz refclk */
Shaohua Li21778322009-02-23 15:19:16 +08007210 intel_clock(dev, 96000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08007211 } else {
7212 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
7213
7214 if (is_lvds) {
7215 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
7216 DPLL_FPA01_P1_POST_DIV_SHIFT);
7217 clock.p2 = 14;
7218
7219 if ((dpll & PLL_REF_INPUT_MASK) ==
7220 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
7221 /* XXX: might not be 66MHz */
Shaohua Li21778322009-02-23 15:19:16 +08007222 intel_clock(dev, 66000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08007223 } else
Shaohua Li21778322009-02-23 15:19:16 +08007224 intel_clock(dev, 48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08007225 } else {
7226 if (dpll & PLL_P1_DIVIDE_BY_TWO)
7227 clock.p1 = 2;
7228 else {
7229 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
7230 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
7231 }
7232 if (dpll & PLL_P2_DIVIDE_BY_4)
7233 clock.p2 = 4;
7234 else
7235 clock.p2 = 2;
7236
Shaohua Li21778322009-02-23 15:19:16 +08007237 intel_clock(dev, 48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08007238 }
7239 }
7240
7241 /* XXX: It would be nice to validate the clocks, but we can't reuse
7242 * i830PllIsValid() because it relies on the xf86_config connector
7243 * configuration being accurate, which it isn't necessarily.
7244 */
7245
7246 return clock.dot;
7247}
7248
7249/** Returns the currently programmed mode of the given pipe. */
7250struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
7251 struct drm_crtc *crtc)
7252{
Jesse Barnes548f2452011-02-17 10:40:53 -08007253 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08007254 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7255 int pipe = intel_crtc->pipe;
7256 struct drm_display_mode *mode;
Jesse Barnes548f2452011-02-17 10:40:53 -08007257 int htot = I915_READ(HTOTAL(pipe));
7258 int hsync = I915_READ(HSYNC(pipe));
7259 int vtot = I915_READ(VTOTAL(pipe));
7260 int vsync = I915_READ(VSYNC(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08007261
7262 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
7263 if (!mode)
7264 return NULL;
7265
7266 mode->clock = intel_crtc_clock_get(dev, crtc);
7267 mode->hdisplay = (htot & 0xffff) + 1;
7268 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
7269 mode->hsync_start = (hsync & 0xffff) + 1;
7270 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
7271 mode->vdisplay = (vtot & 0xffff) + 1;
7272 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
7273 mode->vsync_start = (vsync & 0xffff) + 1;
7274 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
7275
7276 drm_mode_set_name(mode);
7277 drm_mode_set_crtcinfo(mode, 0);
7278
7279 return mode;
7280}
7281
Jesse Barnes652c3932009-08-17 13:31:43 -07007282#define GPU_IDLE_TIMEOUT 500 /* ms */
7283
7284/* When this timer fires, we've been idle for awhile */
7285static void intel_gpu_idle_timer(unsigned long arg)
7286{
7287 struct drm_device *dev = (struct drm_device *)arg;
7288 drm_i915_private_t *dev_priv = dev->dev_private;
7289
Chris Wilsonff7ea4c2010-12-08 09:43:41 +00007290 if (!list_empty(&dev_priv->mm.active_list)) {
7291 /* Still processing requests, so just re-arm the timer. */
7292 mod_timer(&dev_priv->idle_timer, jiffies +
7293 msecs_to_jiffies(GPU_IDLE_TIMEOUT));
7294 return;
7295 }
Jesse Barnes652c3932009-08-17 13:31:43 -07007296
Chris Wilsonff7ea4c2010-12-08 09:43:41 +00007297 dev_priv->busy = false;
Eric Anholt01dfba92009-09-06 15:18:53 -07007298 queue_work(dev_priv->wq, &dev_priv->idle_work);
Jesse Barnes652c3932009-08-17 13:31:43 -07007299}
7300
Jesse Barnes652c3932009-08-17 13:31:43 -07007301#define CRTC_IDLE_TIMEOUT 1000 /* ms */
7302
7303static void intel_crtc_idle_timer(unsigned long arg)
7304{
7305 struct intel_crtc *intel_crtc = (struct intel_crtc *)arg;
7306 struct drm_crtc *crtc = &intel_crtc->base;
7307 drm_i915_private_t *dev_priv = crtc->dev->dev_private;
Chris Wilsonff7ea4c2010-12-08 09:43:41 +00007308 struct intel_framebuffer *intel_fb;
7309
7310 intel_fb = to_intel_framebuffer(crtc->fb);
7311 if (intel_fb && intel_fb->obj->active) {
7312 /* The framebuffer is still being accessed by the GPU. */
7313 mod_timer(&intel_crtc->idle_timer, jiffies +
7314 msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
7315 return;
7316 }
Jesse Barnes652c3932009-08-17 13:31:43 -07007317
Jesse Barnes652c3932009-08-17 13:31:43 -07007318 intel_crtc->busy = false;
Eric Anholt01dfba92009-09-06 15:18:53 -07007319 queue_work(dev_priv->wq, &dev_priv->idle_work);
Jesse Barnes652c3932009-08-17 13:31:43 -07007320}
7321
Daniel Vetter3dec0092010-08-20 21:40:52 +02007322static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07007323{
7324 struct drm_device *dev = crtc->dev;
7325 drm_i915_private_t *dev_priv = dev->dev_private;
7326 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7327 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007328 int dpll_reg = DPLL(pipe);
7329 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07007330
Eric Anholtbad720f2009-10-22 16:11:14 -07007331 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07007332 return;
7333
7334 if (!dev_priv->lvds_downclock_avail)
7335 return;
7336
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007337 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07007338 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08007339 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007340
Sean Paul8ac5a6d2012-02-13 13:14:51 -05007341 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007342
7343 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
7344 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007345 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007346
Jesse Barnes652c3932009-08-17 13:31:43 -07007347 dpll = I915_READ(dpll_reg);
7348 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08007349 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007350 }
7351
7352 /* Schedule downclock */
Daniel Vetter3dec0092010-08-20 21:40:52 +02007353 mod_timer(&intel_crtc->idle_timer, jiffies +
7354 msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
Jesse Barnes652c3932009-08-17 13:31:43 -07007355}
7356
7357static void intel_decrease_pllclock(struct drm_crtc *crtc)
7358{
7359 struct drm_device *dev = crtc->dev;
7360 drm_i915_private_t *dev_priv = dev->dev_private;
7361 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7362 int pipe = intel_crtc->pipe;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007363 int dpll_reg = DPLL(pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007364 int dpll = I915_READ(dpll_reg);
7365
Eric Anholtbad720f2009-10-22 16:11:14 -07007366 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07007367 return;
7368
7369 if (!dev_priv->lvds_downclock_avail)
7370 return;
7371
7372 /*
7373 * Since this is called by a timer, we should never get here in
7374 * the manual case.
7375 */
7376 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08007377 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007378
Sean Paul8ac5a6d2012-02-13 13:14:51 -05007379 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007380
7381 dpll |= DISPLAY_RATE_SELECT_FPA1;
7382 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007383 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007384 dpll = I915_READ(dpll_reg);
7385 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08007386 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007387 }
7388
7389}
7390
7391/**
7392 * intel_idle_update - adjust clocks for idleness
7393 * @work: work struct
7394 *
7395 * Either the GPU or display (or both) went idle. Check the busy status
7396 * here and adjust the CRTC and GPU clocks as necessary.
7397 */
7398static void intel_idle_update(struct work_struct *work)
7399{
7400 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
7401 idle_work);
7402 struct drm_device *dev = dev_priv->dev;
7403 struct drm_crtc *crtc;
7404 struct intel_crtc *intel_crtc;
7405
7406 if (!i915_powersave)
7407 return;
7408
7409 mutex_lock(&dev->struct_mutex);
7410
Jesse Barnes7648fa92010-05-20 14:28:11 -07007411 i915_update_gfx_val(dev_priv);
7412
Jesse Barnes652c3932009-08-17 13:31:43 -07007413 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
7414 /* Skip inactive CRTCs */
7415 if (!crtc->fb)
7416 continue;
7417
7418 intel_crtc = to_intel_crtc(crtc);
7419 if (!intel_crtc->busy)
7420 intel_decrease_pllclock(crtc);
7421 }
7422
Li Peng45ac22c2010-06-12 23:38:35 +08007423
Jesse Barnes652c3932009-08-17 13:31:43 -07007424 mutex_unlock(&dev->struct_mutex);
7425}
7426
7427/**
7428 * intel_mark_busy - mark the GPU and possibly the display busy
7429 * @dev: drm device
7430 * @obj: object we're operating on
7431 *
7432 * Callers can use this function to indicate that the GPU is busy processing
7433 * commands. If @obj matches one of the CRTC objects (i.e. it's a scanout
7434 * buffer), we'll also mark the display as busy, so we know to increase its
7435 * clock frequency.
7436 */
Chris Wilson05394f32010-11-08 19:18:58 +00007437void intel_mark_busy(struct drm_device *dev, struct drm_i915_gem_object *obj)
Jesse Barnes652c3932009-08-17 13:31:43 -07007438{
7439 drm_i915_private_t *dev_priv = dev->dev_private;
7440 struct drm_crtc *crtc = NULL;
7441 struct intel_framebuffer *intel_fb;
7442 struct intel_crtc *intel_crtc;
7443
Zhenyu Wang5e17ee72009-09-03 09:30:06 +08007444 if (!drm_core_check_feature(dev, DRIVER_MODESET))
7445 return;
7446
Alexander Lam18b21902011-01-03 13:28:56 -05007447 if (!dev_priv->busy)
Chris Wilson28cf7982009-11-30 01:08:56 +00007448 dev_priv->busy = true;
Alexander Lam18b21902011-01-03 13:28:56 -05007449 else
Chris Wilson28cf7982009-11-30 01:08:56 +00007450 mod_timer(&dev_priv->idle_timer, jiffies +
7451 msecs_to_jiffies(GPU_IDLE_TIMEOUT));
Jesse Barnes652c3932009-08-17 13:31:43 -07007452
7453 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
7454 if (!crtc->fb)
7455 continue;
7456
7457 intel_crtc = to_intel_crtc(crtc);
7458 intel_fb = to_intel_framebuffer(crtc->fb);
7459 if (intel_fb->obj == obj) {
7460 if (!intel_crtc->busy) {
7461 /* Non-busy -> busy, upclock */
Daniel Vetter3dec0092010-08-20 21:40:52 +02007462 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07007463 intel_crtc->busy = true;
7464 } else {
7465 /* Busy -> busy, put off timer */
7466 mod_timer(&intel_crtc->idle_timer, jiffies +
7467 msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
7468 }
7469 }
7470 }
7471}
7472
Jesse Barnes79e53942008-11-07 14:24:08 -08007473static void intel_crtc_destroy(struct drm_crtc *crtc)
7474{
7475 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02007476 struct drm_device *dev = crtc->dev;
7477 struct intel_unpin_work *work;
7478 unsigned long flags;
7479
7480 spin_lock_irqsave(&dev->event_lock, flags);
7481 work = intel_crtc->unpin_work;
7482 intel_crtc->unpin_work = NULL;
7483 spin_unlock_irqrestore(&dev->event_lock, flags);
7484
7485 if (work) {
7486 cancel_work_sync(&work->work);
7487 kfree(work);
7488 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007489
7490 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02007491
Jesse Barnes79e53942008-11-07 14:24:08 -08007492 kfree(intel_crtc);
7493}
7494
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007495static void intel_unpin_work_fn(struct work_struct *__work)
7496{
7497 struct intel_unpin_work *work =
7498 container_of(__work, struct intel_unpin_work, work);
7499
7500 mutex_lock(&work->dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01007501 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00007502 drm_gem_object_unreference(&work->pending_flip_obj->base);
7503 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00007504
Chris Wilson7782de32011-07-08 12:22:41 +01007505 intel_update_fbc(work->dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007506 mutex_unlock(&work->dev->struct_mutex);
7507 kfree(work);
7508}
7509
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007510static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01007511 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007512{
7513 drm_i915_private_t *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007514 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7515 struct intel_unpin_work *work;
Chris Wilson05394f32010-11-08 19:18:58 +00007516 struct drm_i915_gem_object *obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007517 struct drm_pending_vblank_event *e;
Mario Kleiner49b14a52010-12-09 07:00:07 +01007518 struct timeval tnow, tvbl;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007519 unsigned long flags;
7520
7521 /* Ignore early vblank irqs */
7522 if (intel_crtc == NULL)
7523 return;
7524
Mario Kleiner49b14a52010-12-09 07:00:07 +01007525 do_gettimeofday(&tnow);
7526
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007527 spin_lock_irqsave(&dev->event_lock, flags);
7528 work = intel_crtc->unpin_work;
7529 if (work == NULL || !work->pending) {
7530 spin_unlock_irqrestore(&dev->event_lock, flags);
7531 return;
7532 }
7533
7534 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007535
7536 if (work->event) {
7537 e = work->event;
Mario Kleiner49b14a52010-12-09 07:00:07 +01007538 e->event.sequence = drm_vblank_count_and_time(dev, intel_crtc->pipe, &tvbl);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01007539
7540 /* Called before vblank count and timestamps have
7541 * been updated for the vblank interval of flip
7542 * completion? Need to increment vblank count and
7543 * add one videorefresh duration to returned timestamp
Mario Kleiner49b14a52010-12-09 07:00:07 +01007544 * to account for this. We assume this happened if we
7545 * get called over 0.9 frame durations after the last
7546 * timestamped vblank.
7547 *
7548 * This calculation can not be used with vrefresh rates
7549 * below 5Hz (10Hz to be on the safe side) without
7550 * promoting to 64 integers.
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01007551 */
Mario Kleiner49b14a52010-12-09 07:00:07 +01007552 if (10 * (timeval_to_ns(&tnow) - timeval_to_ns(&tvbl)) >
7553 9 * crtc->framedur_ns) {
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01007554 e->event.sequence++;
Mario Kleiner49b14a52010-12-09 07:00:07 +01007555 tvbl = ns_to_timeval(timeval_to_ns(&tvbl) +
7556 crtc->framedur_ns);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01007557 }
7558
Mario Kleiner49b14a52010-12-09 07:00:07 +01007559 e->event.tv_sec = tvbl.tv_sec;
7560 e->event.tv_usec = tvbl.tv_usec;
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01007561
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007562 list_add_tail(&e->base.link,
7563 &e->base.file_priv->event_list);
7564 wake_up_interruptible(&e->base.file_priv->event_wait);
7565 }
7566
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01007567 drm_vblank_put(dev, intel_crtc->pipe);
7568
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007569 spin_unlock_irqrestore(&dev->event_lock, flags);
7570
Chris Wilson05394f32010-11-08 19:18:58 +00007571 obj = work->old_fb_obj;
Chris Wilsond9e86c02010-11-10 16:40:20 +00007572
Chris Wilsone59f2ba2010-10-07 17:28:15 +01007573 atomic_clear_mask(1 << intel_crtc->plane,
Chris Wilson05394f32010-11-08 19:18:58 +00007574 &obj->pending_flip.counter);
7575 if (atomic_read(&obj->pending_flip) == 0)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01007576 wake_up(&dev_priv->pending_flip_queue);
Chris Wilsond9e86c02010-11-10 16:40:20 +00007577
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007578 schedule_work(&work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07007579
7580 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007581}
7582
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007583void intel_finish_page_flip(struct drm_device *dev, int pipe)
7584{
7585 drm_i915_private_t *dev_priv = dev->dev_private;
7586 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
7587
Mario Kleiner49b14a52010-12-09 07:00:07 +01007588 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007589}
7590
7591void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
7592{
7593 drm_i915_private_t *dev_priv = dev->dev_private;
7594 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
7595
Mario Kleiner49b14a52010-12-09 07:00:07 +01007596 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007597}
7598
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007599void intel_prepare_page_flip(struct drm_device *dev, int plane)
7600{
7601 drm_i915_private_t *dev_priv = dev->dev_private;
7602 struct intel_crtc *intel_crtc =
7603 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
7604 unsigned long flags;
7605
7606 spin_lock_irqsave(&dev->event_lock, flags);
Jesse Barnesde3f4402010-01-14 13:18:02 -08007607 if (intel_crtc->unpin_work) {
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01007608 if ((++intel_crtc->unpin_work->pending) > 1)
7609 DRM_ERROR("Prepared flip multiple times\n");
Jesse Barnesde3f4402010-01-14 13:18:02 -08007610 } else {
7611 DRM_DEBUG_DRIVER("preparing flip with no unpin work?\n");
7612 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007613 spin_unlock_irqrestore(&dev->event_lock, flags);
7614}
7615
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007616static int intel_gen2_queue_flip(struct drm_device *dev,
7617 struct drm_crtc *crtc,
7618 struct drm_framebuffer *fb,
7619 struct drm_i915_gem_object *obj)
7620{
7621 struct drm_i915_private *dev_priv = dev->dev_private;
7622 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7623 unsigned long offset;
7624 u32 flip_mask;
7625 int ret;
7626
7627 ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
7628 if (ret)
7629 goto out;
7630
7631 /* Offset into the new buffer for cases of shared fbs between CRTCs */
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007632 offset = crtc->y * fb->pitches[0] + crtc->x * fb->bits_per_pixel/8;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007633
7634 ret = BEGIN_LP_RING(6);
7635 if (ret)
7636 goto out;
7637
7638 /* Can't queue multiple flips, so wait for the previous
7639 * one to finish before executing the next.
7640 */
7641 if (intel_crtc->plane)
7642 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7643 else
7644 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
7645 OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);
7646 OUT_RING(MI_NOOP);
7647 OUT_RING(MI_DISPLAY_FLIP |
7648 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007649 OUT_RING(fb->pitches[0]);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007650 OUT_RING(obj->gtt_offset + offset);
Daniel Vetterc6a32fc2012-01-20 10:43:44 +01007651 OUT_RING(0); /* aux display base address, unused */
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007652 ADVANCE_LP_RING();
7653out:
7654 return ret;
7655}
7656
7657static int intel_gen3_queue_flip(struct drm_device *dev,
7658 struct drm_crtc *crtc,
7659 struct drm_framebuffer *fb,
7660 struct drm_i915_gem_object *obj)
7661{
7662 struct drm_i915_private *dev_priv = dev->dev_private;
7663 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7664 unsigned long offset;
7665 u32 flip_mask;
7666 int ret;
7667
7668 ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
7669 if (ret)
7670 goto out;
7671
7672 /* Offset into the new buffer for cases of shared fbs between CRTCs */
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007673 offset = crtc->y * fb->pitches[0] + crtc->x * fb->bits_per_pixel/8;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007674
7675 ret = BEGIN_LP_RING(6);
7676 if (ret)
7677 goto out;
7678
7679 if (intel_crtc->plane)
7680 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7681 else
7682 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
7683 OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);
7684 OUT_RING(MI_NOOP);
7685 OUT_RING(MI_DISPLAY_FLIP_I915 |
7686 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007687 OUT_RING(fb->pitches[0]);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007688 OUT_RING(obj->gtt_offset + offset);
7689 OUT_RING(MI_NOOP);
7690
7691 ADVANCE_LP_RING();
7692out:
7693 return ret;
7694}
7695
7696static int intel_gen4_queue_flip(struct drm_device *dev,
7697 struct drm_crtc *crtc,
7698 struct drm_framebuffer *fb,
7699 struct drm_i915_gem_object *obj)
7700{
7701 struct drm_i915_private *dev_priv = dev->dev_private;
7702 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7703 uint32_t pf, pipesrc;
7704 int ret;
7705
7706 ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
7707 if (ret)
7708 goto out;
7709
7710 ret = BEGIN_LP_RING(4);
7711 if (ret)
7712 goto out;
7713
7714 /* i965+ uses the linear or tiled offsets from the
7715 * Display Registers (which do not change across a page-flip)
7716 * so we need only reprogram the base address.
7717 */
7718 OUT_RING(MI_DISPLAY_FLIP |
7719 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007720 OUT_RING(fb->pitches[0]);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007721 OUT_RING(obj->gtt_offset | obj->tiling_mode);
7722
7723 /* XXX Enabling the panel-fitter across page-flip is so far
7724 * untested on non-native modes, so ignore it for now.
7725 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
7726 */
7727 pf = 0;
7728 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
7729 OUT_RING(pf | pipesrc);
7730 ADVANCE_LP_RING();
7731out:
7732 return ret;
7733}
7734
7735static int intel_gen6_queue_flip(struct drm_device *dev,
7736 struct drm_crtc *crtc,
7737 struct drm_framebuffer *fb,
7738 struct drm_i915_gem_object *obj)
7739{
7740 struct drm_i915_private *dev_priv = dev->dev_private;
7741 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7742 uint32_t pf, pipesrc;
7743 int ret;
7744
7745 ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
7746 if (ret)
7747 goto out;
7748
7749 ret = BEGIN_LP_RING(4);
7750 if (ret)
7751 goto out;
7752
7753 OUT_RING(MI_DISPLAY_FLIP |
7754 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007755 OUT_RING(fb->pitches[0] | obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007756 OUT_RING(obj->gtt_offset);
7757
7758 pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
7759 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
7760 OUT_RING(pf | pipesrc);
7761 ADVANCE_LP_RING();
7762out:
7763 return ret;
7764}
7765
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007766/*
7767 * On gen7 we currently use the blit ring because (in early silicon at least)
7768 * the render ring doesn't give us interrpts for page flip completion, which
7769 * means clients will hang after the first flip is queued. Fortunately the
7770 * blit ring generates interrupts properly, so use it instead.
7771 */
7772static int intel_gen7_queue_flip(struct drm_device *dev,
7773 struct drm_crtc *crtc,
7774 struct drm_framebuffer *fb,
7775 struct drm_i915_gem_object *obj)
7776{
7777 struct drm_i915_private *dev_priv = dev->dev_private;
7778 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7779 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
7780 int ret;
7781
7782 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7783 if (ret)
7784 goto out;
7785
7786 ret = intel_ring_begin(ring, 4);
7787 if (ret)
7788 goto out;
7789
7790 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | (intel_crtc->plane << 19));
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007791 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007792 intel_ring_emit(ring, (obj->gtt_offset));
7793 intel_ring_emit(ring, (MI_NOOP));
7794 intel_ring_advance(ring);
7795out:
7796 return ret;
7797}
7798
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007799static int intel_default_queue_flip(struct drm_device *dev,
7800 struct drm_crtc *crtc,
7801 struct drm_framebuffer *fb,
7802 struct drm_i915_gem_object *obj)
7803{
7804 return -ENODEV;
7805}
7806
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007807static int intel_crtc_page_flip(struct drm_crtc *crtc,
7808 struct drm_framebuffer *fb,
7809 struct drm_pending_vblank_event *event)
7810{
7811 struct drm_device *dev = crtc->dev;
7812 struct drm_i915_private *dev_priv = dev->dev_private;
7813 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00007814 struct drm_i915_gem_object *obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007815 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7816 struct intel_unpin_work *work;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007817 unsigned long flags;
Chris Wilson52e68632010-08-08 10:15:59 +01007818 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007819
7820 work = kzalloc(sizeof *work, GFP_KERNEL);
7821 if (work == NULL)
7822 return -ENOMEM;
7823
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007824 work->event = event;
7825 work->dev = crtc->dev;
7826 intel_fb = to_intel_framebuffer(crtc->fb);
Jesse Barnesb1b87f62010-01-26 14:40:05 -08007827 work->old_fb_obj = intel_fb->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007828 INIT_WORK(&work->work, intel_unpin_work_fn);
7829
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007830 ret = drm_vblank_get(dev, intel_crtc->pipe);
7831 if (ret)
7832 goto free_work;
7833
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007834 /* We borrow the event spin lock for protecting unpin_work */
7835 spin_lock_irqsave(&dev->event_lock, flags);
7836 if (intel_crtc->unpin_work) {
7837 spin_unlock_irqrestore(&dev->event_lock, flags);
7838 kfree(work);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007839 drm_vblank_put(dev, intel_crtc->pipe);
Chris Wilson468f0b42010-05-27 13:18:13 +01007840
7841 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007842 return -EBUSY;
7843 }
7844 intel_crtc->unpin_work = work;
7845 spin_unlock_irqrestore(&dev->event_lock, flags);
7846
7847 intel_fb = to_intel_framebuffer(fb);
7848 obj = intel_fb->obj;
7849
Chris Wilson468f0b42010-05-27 13:18:13 +01007850 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007851
Jesse Barnes75dfca82010-02-10 15:09:44 -08007852 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00007853 drm_gem_object_reference(&work->old_fb_obj->base);
7854 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007855
7856 crtc->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01007857
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007858 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007859
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01007860 work->enable_stall_check = true;
7861
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007862 /* Block clients from rendering to the new back buffer until
7863 * the flip occurs and the object is no longer visible.
7864 */
Chris Wilson05394f32010-11-08 19:18:58 +00007865 atomic_add(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007866
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007867 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
7868 if (ret)
7869 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007870
Chris Wilson7782de32011-07-08 12:22:41 +01007871 intel_disable_fbc(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007872 mutex_unlock(&dev->struct_mutex);
7873
Jesse Barnese5510fa2010-07-01 16:48:37 -07007874 trace_i915_flip_request(intel_crtc->plane, obj);
7875
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007876 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01007877
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007878cleanup_pending:
7879 atomic_sub(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
Chris Wilson05394f32010-11-08 19:18:58 +00007880 drm_gem_object_unreference(&work->old_fb_obj->base);
7881 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01007882 mutex_unlock(&dev->struct_mutex);
7883
7884 spin_lock_irqsave(&dev->event_lock, flags);
7885 intel_crtc->unpin_work = NULL;
7886 spin_unlock_irqrestore(&dev->event_lock, flags);
7887
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007888 drm_vblank_put(dev, intel_crtc->pipe);
7889free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01007890 kfree(work);
7891
7892 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007893}
7894
Chris Wilson47f1c6c2010-12-03 15:37:31 +00007895static void intel_sanitize_modesetting(struct drm_device *dev,
7896 int pipe, int plane)
7897{
7898 struct drm_i915_private *dev_priv = dev->dev_private;
7899 u32 reg, val;
7900
7901 if (HAS_PCH_SPLIT(dev))
7902 return;
7903
7904 /* Who knows what state these registers were left in by the BIOS or
7905 * grub?
7906 *
7907 * If we leave the registers in a conflicting state (e.g. with the
7908 * display plane reading from the other pipe than the one we intend
7909 * to use) then when we attempt to teardown the active mode, we will
7910 * not disable the pipes and planes in the correct order -- leaving
7911 * a plane reading from a disabled pipe and possibly leading to
7912 * undefined behaviour.
7913 */
7914
7915 reg = DSPCNTR(plane);
7916 val = I915_READ(reg);
7917
7918 if ((val & DISPLAY_PLANE_ENABLE) == 0)
7919 return;
7920 if (!!(val & DISPPLANE_SEL_PIPE_MASK) == pipe)
7921 return;
7922
7923 /* This display plane is active and attached to the other CPU pipe. */
7924 pipe = !pipe;
7925
7926 /* Disable the plane and wait for it to stop reading from the pipe. */
Jesse Barnesb24e7172011-01-04 15:09:30 -08007927 intel_disable_plane(dev_priv, plane, pipe);
7928 intel_disable_pipe(dev_priv, pipe);
Chris Wilson47f1c6c2010-12-03 15:37:31 +00007929}
Jesse Barnes79e53942008-11-07 14:24:08 -08007930
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007931static void intel_crtc_reset(struct drm_crtc *crtc)
7932{
7933 struct drm_device *dev = crtc->dev;
7934 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7935
7936 /* Reset flags back to the 'unknown' status so that they
7937 * will be correctly set on the initial modeset.
7938 */
7939 intel_crtc->dpms_mode = -1;
7940
7941 /* We need to fix up any BIOS configuration that conflicts with
7942 * our expectations.
7943 */
7944 intel_sanitize_modesetting(dev, intel_crtc->pipe, intel_crtc->plane);
7945}
7946
7947static struct drm_crtc_helper_funcs intel_helper_funcs = {
7948 .dpms = intel_crtc_dpms,
7949 .mode_fixup = intel_crtc_mode_fixup,
7950 .mode_set = intel_crtc_mode_set,
7951 .mode_set_base = intel_pipe_set_base,
7952 .mode_set_base_atomic = intel_pipe_set_base_atomic,
7953 .load_lut = intel_crtc_load_lut,
7954 .disable = intel_crtc_disable,
7955};
7956
7957static const struct drm_crtc_funcs intel_crtc_funcs = {
7958 .reset = intel_crtc_reset,
7959 .cursor_set = intel_crtc_cursor_set,
7960 .cursor_move = intel_crtc_cursor_move,
7961 .gamma_set = intel_crtc_gamma_set,
7962 .set_config = drm_crtc_helper_set_config,
7963 .destroy = intel_crtc_destroy,
7964 .page_flip = intel_crtc_page_flip,
7965};
7966
Hannes Ederb358d0a2008-12-18 21:18:47 +01007967static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -08007968{
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08007969 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08007970 struct intel_crtc *intel_crtc;
7971 int i;
7972
7973 intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
7974 if (intel_crtc == NULL)
7975 return;
7976
7977 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
7978
7979 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -08007980 for (i = 0; i < 256; i++) {
7981 intel_crtc->lut_r[i] = i;
7982 intel_crtc->lut_g[i] = i;
7983 intel_crtc->lut_b[i] = i;
7984 }
7985
Jesse Barnes80824002009-09-10 15:28:06 -07007986 /* Swap pipes & planes for FBC on pre-965 */
7987 intel_crtc->pipe = pipe;
7988 intel_crtc->plane = pipe;
Chris Wilsone2e767a2010-09-13 16:53:12 +01007989 if (IS_MOBILE(dev) && IS_GEN3(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08007990 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +01007991 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07007992 }
7993
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08007994 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
7995 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
7996 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
7997 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
7998
Chris Wilson5d1d0cc2011-01-24 15:02:15 +00007999 intel_crtc_reset(&intel_crtc->base);
Chris Wilson04dbff52011-02-10 17:38:35 +00008000 intel_crtc->active = true; /* force the pipe off on setup_init_config */
Jesse Barnes5a354202011-06-24 12:19:22 -07008001 intel_crtc->bpp = 24; /* default for pre-Ironlake */
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07008002
8003 if (HAS_PCH_SPLIT(dev)) {
Jesse Barnes4b645f12011-10-12 09:51:31 -07008004 if (pipe == 2 && IS_IVYBRIDGE(dev))
8005 intel_crtc->no_pll = true;
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07008006 intel_helper_funcs.prepare = ironlake_crtc_prepare;
8007 intel_helper_funcs.commit = ironlake_crtc_commit;
8008 } else {
8009 intel_helper_funcs.prepare = i9xx_crtc_prepare;
8010 intel_helper_funcs.commit = i9xx_crtc_commit;
8011 }
8012
Jesse Barnes79e53942008-11-07 14:24:08 -08008013 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
8014
Jesse Barnes652c3932009-08-17 13:31:43 -07008015 intel_crtc->busy = false;
8016
8017 setup_timer(&intel_crtc->idle_timer, intel_crtc_idle_timer,
8018 (unsigned long)intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08008019}
8020
Carl Worth08d7b3d2009-04-29 14:43:54 -07008021int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00008022 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -07008023{
8024 drm_i915_private_t *dev_priv = dev->dev_private;
8025 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +02008026 struct drm_mode_object *drmmode_obj;
8027 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -07008028
8029 if (!dev_priv) {
8030 DRM_ERROR("called with no initialization\n");
8031 return -EINVAL;
8032 }
8033
Daniel Vetterc05422d2009-08-11 16:05:30 +02008034 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
8035 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -07008036
Daniel Vetterc05422d2009-08-11 16:05:30 +02008037 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -07008038 DRM_ERROR("no such CRTC id\n");
8039 return -EINVAL;
8040 }
8041
Daniel Vetterc05422d2009-08-11 16:05:30 +02008042 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
8043 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -07008044
Daniel Vetterc05422d2009-08-11 16:05:30 +02008045 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -07008046}
8047
Zhenyu Wangc5e4df32010-03-30 14:39:27 +08008048static int intel_encoder_clones(struct drm_device *dev, int type_mask)
Jesse Barnes79e53942008-11-07 14:24:08 -08008049{
Chris Wilson4ef69c72010-09-09 15:14:28 +01008050 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08008051 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008052 int entry = 0;
8053
Chris Wilson4ef69c72010-09-09 15:14:28 +01008054 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
8055 if (type_mask & encoder->clone_mask)
Jesse Barnes79e53942008-11-07 14:24:08 -08008056 index_mask |= (1 << entry);
8057 entry++;
8058 }
Chris Wilson4ef69c72010-09-09 15:14:28 +01008059
Jesse Barnes79e53942008-11-07 14:24:08 -08008060 return index_mask;
8061}
8062
Chris Wilson4d302442010-12-14 19:21:29 +00008063static bool has_edp_a(struct drm_device *dev)
8064{
8065 struct drm_i915_private *dev_priv = dev->dev_private;
8066
8067 if (!IS_MOBILE(dev))
8068 return false;
8069
8070 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
8071 return false;
8072
8073 if (IS_GEN5(dev) &&
8074 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
8075 return false;
8076
8077 return true;
8078}
8079
Jesse Barnes79e53942008-11-07 14:24:08 -08008080static void intel_setup_outputs(struct drm_device *dev)
8081{
Eric Anholt725e30a2009-01-22 13:01:02 -08008082 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +01008083 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -04008084 bool dpd_is_edp = false;
Chris Wilsonf3cfcba2012-02-09 09:35:53 +00008085 bool has_lvds;
Jesse Barnes79e53942008-11-07 14:24:08 -08008086
Chris Wilsonf3cfcba2012-02-09 09:35:53 +00008087 has_lvds = intel_lvds_init(dev);
Chris Wilsonc5d1b512010-11-29 18:00:23 +00008088 if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
8089 /* disable the panel fitter on everything but LVDS */
8090 I915_WRITE(PFIT_CONTROL, 0);
8091 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008092
Eric Anholtbad720f2009-10-22 16:11:14 -07008093 if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -04008094 dpd_is_edp = intel_dpd_is_edp(dev);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008095
Chris Wilson4d302442010-12-14 19:21:29 +00008096 if (has_edp_a(dev))
Zhenyu Wang32f9d652009-07-24 01:00:32 +08008097 intel_dp_init(dev, DP_A);
8098
Adam Jacksoncb0953d2010-07-16 14:46:29 -04008099 if (dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
8100 intel_dp_init(dev, PCH_DP_D);
8101 }
8102
8103 intel_crt_init(dev);
8104
8105 if (HAS_PCH_SPLIT(dev)) {
8106 int found;
8107
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008108 if (I915_READ(HDMIB) & PORT_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +08008109 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +01008110 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008111 if (!found)
8112 intel_hdmi_init(dev, HDMIB);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08008113 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
8114 intel_dp_init(dev, PCH_DP_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008115 }
8116
8117 if (I915_READ(HDMIC) & PORT_DETECTED)
8118 intel_hdmi_init(dev, HDMIC);
8119
8120 if (I915_READ(HDMID) & PORT_DETECTED)
8121 intel_hdmi_init(dev, HDMID);
8122
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08008123 if (I915_READ(PCH_DP_C) & DP_DETECTED)
8124 intel_dp_init(dev, PCH_DP_C);
8125
Adam Jacksoncb0953d2010-07-16 14:46:29 -04008126 if (!dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08008127 intel_dp_init(dev, PCH_DP_D);
8128
Zhenyu Wang103a1962009-11-27 11:44:36 +08008129 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +08008130 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -08008131
Eric Anholt725e30a2009-01-22 13:01:02 -08008132 if (I915_READ(SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008133 DRM_DEBUG_KMS("probing SDVOB\n");
Daniel Vettereef4eac2012-03-23 23:43:35 +01008134 found = intel_sdvo_init(dev, SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008135 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
8136 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Eric Anholt725e30a2009-01-22 13:01:02 -08008137 intel_hdmi_init(dev, SDVOB);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008138 }
Ma Ling27185ae2009-08-24 13:50:23 +08008139
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008140 if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
8141 DRM_DEBUG_KMS("probing DP_B\n");
Keith Packarda4fc5ed2009-04-07 16:16:42 -07008142 intel_dp_init(dev, DP_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008143 }
Eric Anholt725e30a2009-01-22 13:01:02 -08008144 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -04008145
8146 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -04008147
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008148 if (I915_READ(SDVOB) & SDVO_DETECTED) {
8149 DRM_DEBUG_KMS("probing SDVOC\n");
Daniel Vettereef4eac2012-03-23 23:43:35 +01008150 found = intel_sdvo_init(dev, SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008151 }
Ma Ling27185ae2009-08-24 13:50:23 +08008152
8153 if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
8154
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008155 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
8156 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Eric Anholt725e30a2009-01-22 13:01:02 -08008157 intel_hdmi_init(dev, SDVOC);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008158 }
8159 if (SUPPORTS_INTEGRATED_DP(dev)) {
8160 DRM_DEBUG_KMS("probing DP_C\n");
Keith Packarda4fc5ed2009-04-07 16:16:42 -07008161 intel_dp_init(dev, DP_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008162 }
Eric Anholt725e30a2009-01-22 13:01:02 -08008163 }
Ma Ling27185ae2009-08-24 13:50:23 +08008164
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008165 if (SUPPORTS_INTEGRATED_DP(dev) &&
8166 (I915_READ(DP_D) & DP_DETECTED)) {
8167 DRM_DEBUG_KMS("probing DP_D\n");
Keith Packarda4fc5ed2009-04-07 16:16:42 -07008168 intel_dp_init(dev, DP_D);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008169 }
Eric Anholtbad720f2009-10-22 16:11:14 -07008170 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08008171 intel_dvo_init(dev);
8172
Zhenyu Wang103a1962009-11-27 11:44:36 +08008173 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08008174 intel_tv_init(dev);
8175
Chris Wilson4ef69c72010-09-09 15:14:28 +01008176 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
8177 encoder->base.possible_crtcs = encoder->crtc_mask;
8178 encoder->base.possible_clones =
8179 intel_encoder_clones(dev, encoder->clone_mask);
Jesse Barnes79e53942008-11-07 14:24:08 -08008180 }
Chris Wilson47356eb2011-01-11 17:06:04 +00008181
Chris Wilson2c7111d2011-03-29 10:40:27 +01008182 /* disable all the possible outputs/crtcs before entering KMS mode */
8183 drm_helper_disable_unused_functions(dev);
Keith Packard9fb526d2011-09-26 22:24:57 -07008184
8185 if (HAS_PCH_SPLIT(dev))
8186 ironlake_init_pch_refclk(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08008187}
8188
8189static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
8190{
8191 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08008192
8193 drm_framebuffer_cleanup(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00008194 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08008195
8196 kfree(intel_fb);
8197}
8198
8199static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +00008200 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08008201 unsigned int *handle)
8202{
8203 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00008204 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08008205
Chris Wilson05394f32010-11-08 19:18:58 +00008206 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -08008207}
8208
8209static const struct drm_framebuffer_funcs intel_fb_funcs = {
8210 .destroy = intel_user_framebuffer_destroy,
8211 .create_handle = intel_user_framebuffer_create_handle,
8212};
8213
Dave Airlie38651672010-03-30 05:34:13 +00008214int intel_framebuffer_init(struct drm_device *dev,
8215 struct intel_framebuffer *intel_fb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008216 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilson05394f32010-11-08 19:18:58 +00008217 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -08008218{
Jesse Barnes79e53942008-11-07 14:24:08 -08008219 int ret;
8220
Chris Wilson05394f32010-11-08 19:18:58 +00008221 if (obj->tiling_mode == I915_TILING_Y)
Chris Wilson57cd6502010-08-08 12:34:44 +01008222 return -EINVAL;
8223
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008224 if (mode_cmd->pitches[0] & 63)
Chris Wilson57cd6502010-08-08 12:34:44 +01008225 return -EINVAL;
8226
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008227 switch (mode_cmd->pixel_format) {
Ville Syrjälä04b39242011-11-17 18:05:13 +02008228 case DRM_FORMAT_RGB332:
8229 case DRM_FORMAT_RGB565:
8230 case DRM_FORMAT_XRGB8888:
8231 case DRM_FORMAT_ARGB8888:
8232 case DRM_FORMAT_XRGB2101010:
8233 case DRM_FORMAT_ARGB2101010:
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008234 /* RGB formats are common across chipsets */
Jesse Barnesb5626742011-06-24 12:19:27 -07008235 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02008236 case DRM_FORMAT_YUYV:
8237 case DRM_FORMAT_UYVY:
8238 case DRM_FORMAT_YVYU:
8239 case DRM_FORMAT_VYUY:
Chris Wilson57cd6502010-08-08 12:34:44 +01008240 break;
8241 default:
Eugeni Dodonovaca25842012-01-17 15:25:45 -02008242 DRM_DEBUG_KMS("unsupported pixel format %u\n",
8243 mode_cmd->pixel_format);
Chris Wilson57cd6502010-08-08 12:34:44 +01008244 return -EINVAL;
8245 }
8246
Jesse Barnes79e53942008-11-07 14:24:08 -08008247 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
8248 if (ret) {
8249 DRM_ERROR("framebuffer init failed %d\n", ret);
8250 return ret;
8251 }
8252
8253 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
Jesse Barnes79e53942008-11-07 14:24:08 -08008254 intel_fb->obj = obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08008255 return 0;
8256}
8257
Jesse Barnes79e53942008-11-07 14:24:08 -08008258static struct drm_framebuffer *
8259intel_user_framebuffer_create(struct drm_device *dev,
8260 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008261 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -08008262{
Chris Wilson05394f32010-11-08 19:18:58 +00008263 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08008264
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008265 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
8266 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +00008267 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +01008268 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -08008269
Chris Wilsond2dff872011-04-19 08:36:26 +01008270 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08008271}
8272
Jesse Barnes79e53942008-11-07 14:24:08 -08008273static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -08008274 .fb_create = intel_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +00008275 .output_poll_changed = intel_fb_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -08008276};
8277
Chris Wilson05394f32010-11-08 19:18:58 +00008278static struct drm_i915_gem_object *
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08008279intel_alloc_context_page(struct drm_device *dev)
Chris Wilson9ea8d052010-01-04 18:57:56 +00008280{
Chris Wilson05394f32010-11-08 19:18:58 +00008281 struct drm_i915_gem_object *ctx;
Chris Wilson9ea8d052010-01-04 18:57:56 +00008282 int ret;
8283
Ben Widawsky2c34b852011-03-19 18:14:26 -07008284 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
8285
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08008286 ctx = i915_gem_alloc_object(dev, 4096);
8287 if (!ctx) {
Chris Wilson9ea8d052010-01-04 18:57:56 +00008288 DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
8289 return NULL;
8290 }
8291
Daniel Vetter75e9e912010-11-04 17:11:09 +01008292 ret = i915_gem_object_pin(ctx, 4096, true);
Chris Wilson9ea8d052010-01-04 18:57:56 +00008293 if (ret) {
8294 DRM_ERROR("failed to pin power context: %d\n", ret);
8295 goto err_unref;
8296 }
8297
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08008298 ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
Chris Wilson9ea8d052010-01-04 18:57:56 +00008299 if (ret) {
8300 DRM_ERROR("failed to set-domain on power context: %d\n", ret);
8301 goto err_unpin;
8302 }
Chris Wilson9ea8d052010-01-04 18:57:56 +00008303
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08008304 return ctx;
Chris Wilson9ea8d052010-01-04 18:57:56 +00008305
8306err_unpin:
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08008307 i915_gem_object_unpin(ctx);
Chris Wilson9ea8d052010-01-04 18:57:56 +00008308err_unref:
Chris Wilson05394f32010-11-08 19:18:58 +00008309 drm_gem_object_unreference(&ctx->base);
Chris Wilson9ea8d052010-01-04 18:57:56 +00008310 mutex_unlock(&dev->struct_mutex);
8311 return NULL;
8312}
8313
Jesse Barnes7648fa92010-05-20 14:28:11 -07008314bool ironlake_set_drps(struct drm_device *dev, u8 val)
8315{
8316 struct drm_i915_private *dev_priv = dev->dev_private;
8317 u16 rgvswctl;
8318
8319 rgvswctl = I915_READ16(MEMSWCTL);
8320 if (rgvswctl & MEMCTL_CMD_STS) {
8321 DRM_DEBUG("gpu busy, RCS change rejected\n");
8322 return false; /* still busy with another command */
8323 }
8324
8325 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
8326 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
8327 I915_WRITE16(MEMSWCTL, rgvswctl);
8328 POSTING_READ16(MEMSWCTL);
8329
8330 rgvswctl |= MEMCTL_CMD_STS;
8331 I915_WRITE16(MEMSWCTL, rgvswctl);
8332
8333 return true;
8334}
8335
Jesse Barnesf97108d2010-01-29 11:27:07 -08008336void ironlake_enable_drps(struct drm_device *dev)
8337{
8338 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7648fa92010-05-20 14:28:11 -07008339 u32 rgvmodectl = I915_READ(MEMMODECTL);
Jesse Barnesf97108d2010-01-29 11:27:07 -08008340 u8 fmax, fmin, fstart, vstart;
Jesse Barnesf97108d2010-01-29 11:27:07 -08008341
Jesse Barnesea056c12010-09-10 10:02:13 -07008342 /* Enable temp reporting */
8343 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
8344 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
8345
Jesse Barnesf97108d2010-01-29 11:27:07 -08008346 /* 100ms RC evaluation intervals */
8347 I915_WRITE(RCUPEI, 100000);
8348 I915_WRITE(RCDNEI, 100000);
8349
8350 /* Set max/min thresholds to 90ms and 80ms respectively */
8351 I915_WRITE(RCBMAXAVG, 90000);
8352 I915_WRITE(RCBMINAVG, 80000);
8353
8354 I915_WRITE(MEMIHYST, 1);
8355
8356 /* Set up min, max, and cur for interrupt handling */
8357 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
8358 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
8359 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
8360 MEMMODE_FSTART_SHIFT;
Jesse Barnes7648fa92010-05-20 14:28:11 -07008361
Jesse Barnesf97108d2010-01-29 11:27:07 -08008362 vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
8363 PXVFREQ_PX_SHIFT;
8364
Jesse Barnes80dbf4b2010-11-01 14:12:01 -07008365 dev_priv->fmax = fmax; /* IPS callback will increase this */
Jesse Barnes7648fa92010-05-20 14:28:11 -07008366 dev_priv->fstart = fstart;
8367
Jesse Barnes80dbf4b2010-11-01 14:12:01 -07008368 dev_priv->max_delay = fstart;
Jesse Barnesf97108d2010-01-29 11:27:07 -08008369 dev_priv->min_delay = fmin;
8370 dev_priv->cur_delay = fstart;
8371
Jesse Barnes80dbf4b2010-11-01 14:12:01 -07008372 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
8373 fmax, fmin, fstart);
Jesse Barnes7648fa92010-05-20 14:28:11 -07008374
Jesse Barnesf97108d2010-01-29 11:27:07 -08008375 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
8376
8377 /*
8378 * Interrupts will be enabled in ironlake_irq_postinstall
8379 */
8380
8381 I915_WRITE(VIDSTART, vstart);
8382 POSTING_READ(VIDSTART);
8383
8384 rgvmodectl |= MEMMODE_SWMODE_EN;
8385 I915_WRITE(MEMMODECTL, rgvmodectl);
8386
Chris Wilson481b6af2010-08-23 17:43:35 +01008387 if (wait_for((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Chris Wilson913d8d12010-08-07 11:01:35 +01008388 DRM_ERROR("stuck trying to change perf mode\n");
Jesse Barnesf97108d2010-01-29 11:27:07 -08008389 msleep(1);
8390
Jesse Barnes7648fa92010-05-20 14:28:11 -07008391 ironlake_set_drps(dev, fstart);
Jesse Barnesf97108d2010-01-29 11:27:07 -08008392
Jesse Barnes7648fa92010-05-20 14:28:11 -07008393 dev_priv->last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
8394 I915_READ(0x112e0);
8395 dev_priv->last_time1 = jiffies_to_msecs(jiffies);
8396 dev_priv->last_count2 = I915_READ(0x112f4);
8397 getrawmonotonic(&dev_priv->last_time2);
Jesse Barnesf97108d2010-01-29 11:27:07 -08008398}
8399
8400void ironlake_disable_drps(struct drm_device *dev)
8401{
8402 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7648fa92010-05-20 14:28:11 -07008403 u16 rgvswctl = I915_READ16(MEMSWCTL);
Jesse Barnesf97108d2010-01-29 11:27:07 -08008404
8405 /* Ack interrupts, disable EFC interrupt */
8406 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
8407 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
8408 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
8409 I915_WRITE(DEIIR, DE_PCU_EVENT);
8410 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
8411
8412 /* Go back to the starting frequency */
Jesse Barnes7648fa92010-05-20 14:28:11 -07008413 ironlake_set_drps(dev, dev_priv->fstart);
Jesse Barnesf97108d2010-01-29 11:27:07 -08008414 msleep(1);
8415 rgvswctl |= MEMCTL_CMD_STS;
8416 I915_WRITE(MEMSWCTL, rgvswctl);
8417 msleep(1);
8418
8419}
8420
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08008421void gen6_set_rps(struct drm_device *dev, u8 val)
8422{
8423 struct drm_i915_private *dev_priv = dev->dev_private;
8424 u32 swreq;
8425
8426 swreq = (val & 0x3ff) << 25;
8427 I915_WRITE(GEN6_RPNSWREQ, swreq);
8428}
8429
8430void gen6_disable_rps(struct drm_device *dev)
8431{
8432 struct drm_i915_private *dev_priv = dev->dev_private;
8433
8434 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
8435 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
8436 I915_WRITE(GEN6_PMIER, 0);
Daniel Vetter6fdd4d92011-09-08 14:00:22 +02008437 /* Complete PM interrupt masking here doesn't race with the rps work
8438 * item again unmasking PM interrupts because that is using a different
8439 * register (PMIMR) to mask PM interrupts. The only risk is in leaving
8440 * stale bits in PMIIR and PMIMR which gen6_enable_rps will clean up. */
Ben Widawsky4912d042011-04-25 11:25:20 -07008441
8442 spin_lock_irq(&dev_priv->rps_lock);
8443 dev_priv->pm_iir = 0;
8444 spin_unlock_irq(&dev_priv->rps_lock);
8445
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08008446 I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
8447}
8448
Jesse Barnes7648fa92010-05-20 14:28:11 -07008449static unsigned long intel_pxfreq(u32 vidfreq)
8450{
8451 unsigned long freq;
8452 int div = (vidfreq & 0x3f0000) >> 16;
8453 int post = (vidfreq & 0x3000) >> 12;
8454 int pre = (vidfreq & 0x7);
8455
8456 if (!pre)
8457 return 0;
8458
8459 freq = ((div * 133333) / ((1<<post) * pre));
8460
8461 return freq;
8462}
8463
8464void intel_init_emon(struct drm_device *dev)
8465{
8466 struct drm_i915_private *dev_priv = dev->dev_private;
8467 u32 lcfuse;
8468 u8 pxw[16];
8469 int i;
8470
8471 /* Disable to program */
8472 I915_WRITE(ECR, 0);
8473 POSTING_READ(ECR);
8474
8475 /* Program energy weights for various events */
8476 I915_WRITE(SDEW, 0x15040d00);
8477 I915_WRITE(CSIEW0, 0x007f0000);
8478 I915_WRITE(CSIEW1, 0x1e220004);
8479 I915_WRITE(CSIEW2, 0x04000004);
8480
8481 for (i = 0; i < 5; i++)
8482 I915_WRITE(PEW + (i * 4), 0);
8483 for (i = 0; i < 3; i++)
8484 I915_WRITE(DEW + (i * 4), 0);
8485
8486 /* Program P-state weights to account for frequency power adjustment */
8487 for (i = 0; i < 16; i++) {
8488 u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
8489 unsigned long freq = intel_pxfreq(pxvidfreq);
8490 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
8491 PXVFREQ_PX_SHIFT;
8492 unsigned long val;
8493
8494 val = vid * vid;
8495 val *= (freq / 1000);
8496 val *= 255;
8497 val /= (127*127*900);
8498 if (val > 0xff)
8499 DRM_ERROR("bad pxval: %ld\n", val);
8500 pxw[i] = val;
8501 }
8502 /* Render standby states get 0 weight */
8503 pxw[14] = 0;
8504 pxw[15] = 0;
8505
8506 for (i = 0; i < 4; i++) {
8507 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
8508 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
8509 I915_WRITE(PXW + (i * 4), val);
8510 }
8511
8512 /* Adjust magic regs to magic values (more experimental results) */
8513 I915_WRITE(OGW0, 0);
8514 I915_WRITE(OGW1, 0);
8515 I915_WRITE(EG0, 0x00007f00);
8516 I915_WRITE(EG1, 0x0000000e);
8517 I915_WRITE(EG2, 0x000e0000);
8518 I915_WRITE(EG3, 0x68000300);
8519 I915_WRITE(EG4, 0x42000000);
8520 I915_WRITE(EG5, 0x00140031);
8521 I915_WRITE(EG6, 0);
8522 I915_WRITE(EG7, 0);
8523
8524 for (i = 0; i < 8; i++)
8525 I915_WRITE(PXWL + (i * 4), 0);
8526
8527 /* Enable PMON + select events */
8528 I915_WRITE(ECR, 0x80000019);
8529
8530 lcfuse = I915_READ(LCFUSE02);
8531
8532 dev_priv->corr = (lcfuse & LCFUSE_HIV_MASK);
8533}
8534
Keith Packardc0f372b32011-11-16 22:24:52 -08008535static bool intel_enable_rc6(struct drm_device *dev)
8536{
8537 /*
8538 * Respect the kernel parameter if it is set
8539 */
8540 if (i915_enable_rc6 >= 0)
8541 return i915_enable_rc6;
8542
8543 /*
8544 * Disable RC6 on Ironlake
8545 */
8546 if (INTEL_INFO(dev)->gen == 5)
8547 return 0;
8548
8549 /*
Keith Packard371de6e2011-12-26 17:02:11 -08008550 * Disable rc6 on Sandybridge
Keith Packardc0f372b32011-11-16 22:24:52 -08008551 */
8552 if (INTEL_INFO(dev)->gen == 6) {
Keith Packard371de6e2011-12-26 17:02:11 -08008553 DRM_DEBUG_DRIVER("Sandybridge: RC6 disabled\n");
8554 return 0;
Keith Packardc0f372b32011-11-16 22:24:52 -08008555 }
8556 DRM_DEBUG_DRIVER("RC6 enabled\n");
8557 return 1;
8558}
8559
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08008560void gen6_enable_rps(struct drm_i915_private *dev_priv)
Chris Wilson8fd26852010-12-08 18:40:43 +00008561{
Jesse Barnesa6044e22010-12-20 11:34:20 -08008562 u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
8563 u32 gt_perf_status = I915_READ(GEN6_GT_PERF_STATUS);
Jesse Barnes7df87212011-03-30 14:08:56 -07008564 u32 pcu_mbox, rc6_mask = 0;
Ben Widawskydd202c62012-02-09 10:15:18 +01008565 u32 gtfifodbg;
Jesse Barnesa6044e22010-12-20 11:34:20 -08008566 int cur_freq, min_freq, max_freq;
Chris Wilson8fd26852010-12-08 18:40:43 +00008567 int i;
8568
8569 /* Here begins a magic sequence of register writes to enable
8570 * auto-downclocking.
8571 *
8572 * Perhaps there might be some value in exposing these to
8573 * userspace...
8574 */
8575 I915_WRITE(GEN6_RC_STATE, 0);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01008576 mutex_lock(&dev_priv->dev->struct_mutex);
Ben Widawskydd202c62012-02-09 10:15:18 +01008577
8578 /* Clear the DBG now so we don't confuse earlier errors */
8579 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
8580 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
8581 I915_WRITE(GTFIFODBG, gtfifodbg);
8582 }
8583
Ben Widawskyfcca7922011-04-25 11:23:07 -07008584 gen6_gt_force_wake_get(dev_priv);
Chris Wilson8fd26852010-12-08 18:40:43 +00008585
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08008586 /* disable the counters and set deterministic thresholds */
Chris Wilson8fd26852010-12-08 18:40:43 +00008587 I915_WRITE(GEN6_RC_CONTROL, 0);
8588
8589 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
8590 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
8591 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
8592 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
8593 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
8594
8595 for (i = 0; i < I915_NUM_RINGS; i++)
8596 I915_WRITE(RING_MAX_IDLE(dev_priv->ring[i].mmio_base), 10);
8597
8598 I915_WRITE(GEN6_RC_SLEEP, 0);
8599 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
8600 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
8601 I915_WRITE(GEN6_RC6p_THRESHOLD, 100000);
8602 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
8603
Keith Packardc0f372b32011-11-16 22:24:52 -08008604 if (intel_enable_rc6(dev_priv->dev))
Jesse Barnes7df87212011-03-30 14:08:56 -07008605 rc6_mask = GEN6_RC_CTL_RC6p_ENABLE |
8606 GEN6_RC_CTL_RC6_ENABLE;
8607
Chris Wilson8fd26852010-12-08 18:40:43 +00008608 I915_WRITE(GEN6_RC_CONTROL,
Jesse Barnes7df87212011-03-30 14:08:56 -07008609 rc6_mask |
Chris Wilson9c3d2f72010-12-17 10:54:26 +00008610 GEN6_RC_CTL_EI_MODE(1) |
Chris Wilson8fd26852010-12-08 18:40:43 +00008611 GEN6_RC_CTL_HW_ENABLE);
8612
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08008613 I915_WRITE(GEN6_RPNSWREQ,
Chris Wilson8fd26852010-12-08 18:40:43 +00008614 GEN6_FREQUENCY(10) |
8615 GEN6_OFFSET(0) |
8616 GEN6_AGGRESSIVE_TURBO);
8617 I915_WRITE(GEN6_RC_VIDEO_FREQ,
8618 GEN6_FREQUENCY(12));
8619
8620 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
8621 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
8622 18 << 24 |
8623 6 << 16);
Jesse Barnesccab5c82011-01-18 15:49:25 -08008624 I915_WRITE(GEN6_RP_UP_THRESHOLD, 10000);
8625 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 1000000);
Chris Wilson8fd26852010-12-08 18:40:43 +00008626 I915_WRITE(GEN6_RP_UP_EI, 100000);
Jesse Barnesccab5c82011-01-18 15:49:25 -08008627 I915_WRITE(GEN6_RP_DOWN_EI, 5000000);
Chris Wilson8fd26852010-12-08 18:40:43 +00008628 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
8629 I915_WRITE(GEN6_RP_CONTROL,
8630 GEN6_RP_MEDIA_TURBO |
Ben Widawsky6ed55ee2011-12-12 19:21:59 -08008631 GEN6_RP_MEDIA_HW_MODE |
Chris Wilson8fd26852010-12-08 18:40:43 +00008632 GEN6_RP_MEDIA_IS_GFX |
8633 GEN6_RP_ENABLE |
Jesse Barnesccab5c82011-01-18 15:49:25 -08008634 GEN6_RP_UP_BUSY_AVG |
8635 GEN6_RP_DOWN_IDLE_CONT);
Chris Wilson8fd26852010-12-08 18:40:43 +00008636
8637 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
8638 500))
8639 DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
8640
8641 I915_WRITE(GEN6_PCODE_DATA, 0);
8642 I915_WRITE(GEN6_PCODE_MAILBOX,
8643 GEN6_PCODE_READY |
8644 GEN6_PCODE_WRITE_MIN_FREQ_TABLE);
8645 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
8646 500))
8647 DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
8648
Jesse Barnesa6044e22010-12-20 11:34:20 -08008649 min_freq = (rp_state_cap & 0xff0000) >> 16;
8650 max_freq = rp_state_cap & 0xff;
8651 cur_freq = (gt_perf_status & 0xff00) >> 8;
8652
8653 /* Check for overclock support */
8654 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
8655 500))
8656 DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
8657 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_READ_OC_PARAMS);
8658 pcu_mbox = I915_READ(GEN6_PCODE_DATA);
8659 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
8660 500))
8661 DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
8662 if (pcu_mbox & (1<<31)) { /* OC supported */
8663 max_freq = pcu_mbox & 0xff;
Jesse Barnese281fca2011-03-18 10:32:07 -07008664 DRM_DEBUG_DRIVER("overclocking supported, adjusting frequency max to %dMHz\n", pcu_mbox * 50);
Jesse Barnesa6044e22010-12-20 11:34:20 -08008665 }
8666
8667 /* In units of 100MHz */
8668 dev_priv->max_delay = max_freq;
8669 dev_priv->min_delay = min_freq;
8670 dev_priv->cur_delay = cur_freq;
8671
Chris Wilson8fd26852010-12-08 18:40:43 +00008672 /* requires MSI enabled */
8673 I915_WRITE(GEN6_PMIER,
8674 GEN6_PM_MBOX_EVENT |
8675 GEN6_PM_THERMAL_EVENT |
8676 GEN6_PM_RP_DOWN_TIMEOUT |
8677 GEN6_PM_RP_UP_THRESHOLD |
8678 GEN6_PM_RP_DOWN_THRESHOLD |
8679 GEN6_PM_RP_UP_EI_EXPIRED |
8680 GEN6_PM_RP_DOWN_EI_EXPIRED);
Ben Widawsky4912d042011-04-25 11:25:20 -07008681 spin_lock_irq(&dev_priv->rps_lock);
8682 WARN_ON(dev_priv->pm_iir != 0);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08008683 I915_WRITE(GEN6_PMIMR, 0);
Ben Widawsky4912d042011-04-25 11:25:20 -07008684 spin_unlock_irq(&dev_priv->rps_lock);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08008685 /* enable all PM interrupts */
8686 I915_WRITE(GEN6_PMINTRMSK, 0);
Chris Wilson8fd26852010-12-08 18:40:43 +00008687
Ben Widawskyfcca7922011-04-25 11:23:07 -07008688 gen6_gt_force_wake_put(dev_priv);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01008689 mutex_unlock(&dev_priv->dev->struct_mutex);
Chris Wilson8fd26852010-12-08 18:40:43 +00008690}
8691
Jesse Barnes23b2f8b2011-06-28 13:04:16 -07008692void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
8693{
8694 int min_freq = 15;
8695 int gpu_freq, ia_freq, max_ia_freq;
8696 int scaling_factor = 180;
8697
8698 max_ia_freq = cpufreq_quick_get_max(0);
8699 /*
8700 * Default to measured freq if none found, PCU will ensure we don't go
8701 * over
8702 */
8703 if (!max_ia_freq)
8704 max_ia_freq = tsc_khz;
8705
8706 /* Convert from kHz to MHz */
8707 max_ia_freq /= 1000;
8708
8709 mutex_lock(&dev_priv->dev->struct_mutex);
8710
8711 /*
8712 * For each potential GPU frequency, load a ring frequency we'd like
8713 * to use for memory access. We do this by specifying the IA frequency
8714 * the PCU should use as a reference to determine the ring frequency.
8715 */
8716 for (gpu_freq = dev_priv->max_delay; gpu_freq >= dev_priv->min_delay;
8717 gpu_freq--) {
8718 int diff = dev_priv->max_delay - gpu_freq;
8719
8720 /*
8721 * For GPU frequencies less than 750MHz, just use the lowest
8722 * ring freq.
8723 */
8724 if (gpu_freq < min_freq)
8725 ia_freq = 800;
8726 else
8727 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
8728 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
8729
8730 I915_WRITE(GEN6_PCODE_DATA,
8731 (ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT) |
8732 gpu_freq);
8733 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY |
8734 GEN6_PCODE_WRITE_MIN_FREQ_TABLE);
8735 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) &
8736 GEN6_PCODE_READY) == 0, 10)) {
8737 DRM_ERROR("pcode write of freq table timed out\n");
8738 continue;
8739 }
8740 }
8741
8742 mutex_unlock(&dev_priv->dev->struct_mutex);
8743}
8744
Jesse Barnes6067aae2011-04-28 15:04:31 -07008745static void ironlake_init_clock_gating(struct drm_device *dev)
8746{
8747 struct drm_i915_private *dev_priv = dev->dev_private;
8748 uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
8749
8750 /* Required for FBC */
8751 dspclk_gate |= DPFCUNIT_CLOCK_GATE_DISABLE |
8752 DPFCRUNIT_CLOCK_GATE_DISABLE |
8753 DPFDUNIT_CLOCK_GATE_DISABLE;
8754 /* Required for CxSR */
8755 dspclk_gate |= DPARBUNIT_CLOCK_GATE_DISABLE;
8756
8757 I915_WRITE(PCH_3DCGDIS0,
8758 MARIUNIT_CLOCK_GATE_DISABLE |
8759 SVSMUNIT_CLOCK_GATE_DISABLE);
8760 I915_WRITE(PCH_3DCGDIS1,
8761 VFMUNIT_CLOCK_GATE_DISABLE);
8762
8763 I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
8764
8765 /*
Jesse Barnes6067aae2011-04-28 15:04:31 -07008766 * According to the spec the following bits should be set in
8767 * order to enable memory self-refresh
8768 * The bit 22/21 of 0x42004
8769 * The bit 5 of 0x42020
8770 * The bit 15 of 0x45000
8771 */
8772 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8773 (I915_READ(ILK_DISPLAY_CHICKEN2) |
8774 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
8775 I915_WRITE(ILK_DSPCLK_GATE,
8776 (I915_READ(ILK_DSPCLK_GATE) |
8777 ILK_DPARB_CLK_GATE));
8778 I915_WRITE(DISP_ARB_CTL,
8779 (I915_READ(DISP_ARB_CTL) |
8780 DISP_FBC_WM_DIS));
8781 I915_WRITE(WM3_LP_ILK, 0);
8782 I915_WRITE(WM2_LP_ILK, 0);
8783 I915_WRITE(WM1_LP_ILK, 0);
8784
8785 /*
8786 * Based on the document from hardware guys the following bits
8787 * should be set unconditionally in order to enable FBC.
8788 * The bit 22 of 0x42000
8789 * The bit 22 of 0x42004
8790 * The bit 7,8,9 of 0x42020.
8791 */
8792 if (IS_IRONLAKE_M(dev)) {
8793 I915_WRITE(ILK_DISPLAY_CHICKEN1,
8794 I915_READ(ILK_DISPLAY_CHICKEN1) |
8795 ILK_FBCQ_DIS);
8796 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8797 I915_READ(ILK_DISPLAY_CHICKEN2) |
8798 ILK_DPARB_GATE);
8799 I915_WRITE(ILK_DSPCLK_GATE,
8800 I915_READ(ILK_DSPCLK_GATE) |
8801 ILK_DPFC_DIS1 |
8802 ILK_DPFC_DIS2 |
8803 ILK_CLK_FBC);
8804 }
8805
8806 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8807 I915_READ(ILK_DISPLAY_CHICKEN2) |
8808 ILK_ELPIN_409_SELECT);
8809 I915_WRITE(_3D_CHICKEN2,
8810 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
8811 _3D_CHICKEN2_WM_READ_PIPELINED);
8812}
8813
8814static void gen6_init_clock_gating(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07008815{
8816 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08008817 int pipe;
Jesse Barnes6067aae2011-04-28 15:04:31 -07008818 uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
8819
8820 I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
Jesse Barnes652c3932009-08-17 13:31:43 -07008821
Jesse Barnes6067aae2011-04-28 15:04:31 -07008822 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8823 I915_READ(ILK_DISPLAY_CHICKEN2) |
8824 ILK_ELPIN_409_SELECT);
Eric Anholt8956c8b2010-03-18 13:21:14 -07008825
Jesse Barnes6067aae2011-04-28 15:04:31 -07008826 I915_WRITE(WM3_LP_ILK, 0);
8827 I915_WRITE(WM2_LP_ILK, 0);
8828 I915_WRITE(WM1_LP_ILK, 0);
Eric Anholt8956c8b2010-03-18 13:21:14 -07008829
Eric Anholt406478d2011-11-07 16:07:04 -08008830 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
8831 * gating disable must be set. Failure to set it results in
8832 * flickering pixels due to Z write ordering failures after
8833 * some amount of runtime in the Mesa "fire" demo, and Unigine
8834 * Sanctuary and Tropics, and apparently anything else with
8835 * alpha test or pixel discard.
Eric Anholt9ca1d102011-11-07 16:07:05 -08008836 *
8837 * According to the spec, bit 11 (RCCUNIT) must also be set,
8838 * but we didn't debug actual testcases to find it out.
Eric Anholt406478d2011-11-07 16:07:04 -08008839 */
Eric Anholt9ca1d102011-11-07 16:07:05 -08008840 I915_WRITE(GEN6_UCGCTL2,
8841 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
8842 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
Eric Anholt406478d2011-11-07 16:07:04 -08008843
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08008844 /*
Jesse Barnes6067aae2011-04-28 15:04:31 -07008845 * According to the spec the following bits should be
8846 * set in order to enable memory self-refresh and fbc:
8847 * The bit21 and bit22 of 0x42000
8848 * The bit21 and bit22 of 0x42004
8849 * The bit5 and bit7 of 0x42020
8850 * The bit14 of 0x70180
8851 * The bit14 of 0x71180
Jesse Barnes382b0932010-10-07 16:01:25 -07008852 */
Jesse Barnes6067aae2011-04-28 15:04:31 -07008853 I915_WRITE(ILK_DISPLAY_CHICKEN1,
8854 I915_READ(ILK_DISPLAY_CHICKEN1) |
8855 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
8856 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8857 I915_READ(ILK_DISPLAY_CHICKEN2) |
8858 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
8859 I915_WRITE(ILK_DSPCLK_GATE,
8860 I915_READ(ILK_DSPCLK_GATE) |
8861 ILK_DPARB_CLK_GATE |
8862 ILK_DPFD_CLK_GATE);
Jesse Barnes382b0932010-10-07 16:01:25 -07008863
Keith Packardd74362c2011-07-28 14:47:14 -07008864 for_each_pipe(pipe) {
Jesse Barnes6067aae2011-04-28 15:04:31 -07008865 I915_WRITE(DSPCNTR(pipe),
8866 I915_READ(DSPCNTR(pipe)) |
8867 DISPPLANE_TRICKLE_FEED_DISABLE);
Keith Packardd74362c2011-07-28 14:47:14 -07008868 intel_flush_display_plane(dev_priv, pipe);
8869 }
Jesse Barnes6067aae2011-04-28 15:04:31 -07008870}
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08008871
Jesse Barnes28963a32011-05-11 09:42:30 -07008872static void ivybridge_init_clock_gating(struct drm_device *dev)
8873{
8874 struct drm_i915_private *dev_priv = dev->dev_private;
8875 int pipe;
8876 uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
Jesse Barnes652c3932009-08-17 13:31:43 -07008877
Jesse Barnes28963a32011-05-11 09:42:30 -07008878 I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08008879
Jesse Barnes28963a32011-05-11 09:42:30 -07008880 I915_WRITE(WM3_LP_ILK, 0);
8881 I915_WRITE(WM2_LP_ILK, 0);
8882 I915_WRITE(WM1_LP_ILK, 0);
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08008883
Jesse Barnes28963a32011-05-11 09:42:30 -07008884 I915_WRITE(ILK_DSPCLK_GATE, IVB_VRHUNIT_CLK_GATE);
Eric Anholtde6e2ea2010-11-06 14:53:32 -07008885
Eric Anholt116ac8d2011-12-21 10:31:09 -08008886 I915_WRITE(IVB_CHICKEN3,
8887 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
8888 CHICKEN3_DGMG_DONE_FIX_DISABLE);
8889
Keith Packardd74362c2011-07-28 14:47:14 -07008890 for_each_pipe(pipe) {
Jesse Barnes28963a32011-05-11 09:42:30 -07008891 I915_WRITE(DSPCNTR(pipe),
8892 I915_READ(DSPCNTR(pipe)) |
8893 DISPPLANE_TRICKLE_FEED_DISABLE);
Keith Packardd74362c2011-07-28 14:47:14 -07008894 intel_flush_display_plane(dev_priv, pipe);
8895 }
Jesse Barnes28963a32011-05-11 09:42:30 -07008896}
Eric Anholt67e92af2010-11-06 14:53:33 -07008897
Jesse Barnesfb046852012-03-28 13:39:26 -07008898static void valleyview_init_clock_gating(struct drm_device *dev)
8899{
8900 struct drm_i915_private *dev_priv = dev->dev_private;
8901 int pipe;
8902 uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
8903
8904 I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
8905
8906 I915_WRITE(WM3_LP_ILK, 0);
8907 I915_WRITE(WM2_LP_ILK, 0);
8908 I915_WRITE(WM1_LP_ILK, 0);
8909
8910 /* According to the spec, bit 13 (RCZUNIT) must be set on IVB.
8911 * This implements the WaDisableRCZUnitClockGating workaround.
8912 */
8913 I915_WRITE(GEN6_UCGCTL2, GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
8914
8915 I915_WRITE(ILK_DSPCLK_GATE, IVB_VRHUNIT_CLK_GATE);
8916
8917 I915_WRITE(IVB_CHICKEN3,
8918 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
8919 CHICKEN3_DGMG_DONE_FIX_DISABLE);
8920
8921 /* Apply the WaDisableRHWOOptimizationForRenderHang workaround. */
8922 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
8923 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
8924
8925 /* WaApplyL3ControlAndL3ChickenMode requires those two on Ivy Bridge */
8926 I915_WRITE(GEN7_L3CNTLREG1, GEN7_WA_FOR_GEN7_L3_CONTROL);
8927 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER, GEN7_WA_L3_CHICKEN_MODE);
8928
8929 /* This is required by WaCatErrorRejectionIssue */
8930 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8931 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8932 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8933
8934 for_each_pipe(pipe) {
8935 I915_WRITE(DSPCNTR(pipe),
8936 I915_READ(DSPCNTR(pipe)) |
8937 DISPPLANE_TRICKLE_FEED_DISABLE);
8938 intel_flush_display_plane(dev_priv, pipe);
8939 }
8940
8941 I915_WRITE(CACHE_MODE_1, I915_READ(CACHE_MODE_1) |
8942 (PIXEL_SUBSPAN_COLLECT_OPT_DISABLE << 16) |
8943 PIXEL_SUBSPAN_COLLECT_OPT_DISABLE);
8944}
8945
Jesse Barnes6067aae2011-04-28 15:04:31 -07008946static void g4x_init_clock_gating(struct drm_device *dev)
8947{
8948 struct drm_i915_private *dev_priv = dev->dev_private;
8949 uint32_t dspclk_gate;
Chris Wilson8fd26852010-12-08 18:40:43 +00008950
Jesse Barnes6067aae2011-04-28 15:04:31 -07008951 I915_WRITE(RENCLK_GATE_D1, 0);
8952 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
8953 GS_UNIT_CLOCK_GATE_DISABLE |
8954 CL_UNIT_CLOCK_GATE_DISABLE);
8955 I915_WRITE(RAMCLK_GATE_D, 0);
8956 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
8957 OVRUNIT_CLOCK_GATE_DISABLE |
8958 OVCUNIT_CLOCK_GATE_DISABLE;
8959 if (IS_GM45(dev))
8960 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
8961 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
8962}
Yuanhan Liu13982612010-12-15 15:42:31 +08008963
Jesse Barnes6067aae2011-04-28 15:04:31 -07008964static void crestline_init_clock_gating(struct drm_device *dev)
8965{
8966 struct drm_i915_private *dev_priv = dev->dev_private;
Yuanhan Liu13982612010-12-15 15:42:31 +08008967
Jesse Barnes6067aae2011-04-28 15:04:31 -07008968 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
8969 I915_WRITE(RENCLK_GATE_D2, 0);
8970 I915_WRITE(DSPCLK_GATE_D, 0);
8971 I915_WRITE(RAMCLK_GATE_D, 0);
8972 I915_WRITE16(DEUC, 0);
8973}
Jesse Barnes652c3932009-08-17 13:31:43 -07008974
Jesse Barnes6067aae2011-04-28 15:04:31 -07008975static void broadwater_init_clock_gating(struct drm_device *dev)
8976{
8977 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -07008978
Jesse Barnes6067aae2011-04-28 15:04:31 -07008979 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
8980 I965_RCC_CLOCK_GATE_DISABLE |
8981 I965_RCPB_CLOCK_GATE_DISABLE |
8982 I965_ISC_CLOCK_GATE_DISABLE |
8983 I965_FBC_CLOCK_GATE_DISABLE);
8984 I915_WRITE(RENCLK_GATE_D2, 0);
8985}
Jesse Barnes652c3932009-08-17 13:31:43 -07008986
Jesse Barnes6067aae2011-04-28 15:04:31 -07008987static void gen3_init_clock_gating(struct drm_device *dev)
8988{
8989 struct drm_i915_private *dev_priv = dev->dev_private;
8990 u32 dstate = I915_READ(D_STATE);
8991
8992 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
8993 DSTATE_DOT_CLOCK_GATING;
8994 I915_WRITE(D_STATE, dstate);
8995}
8996
8997static void i85x_init_clock_gating(struct drm_device *dev)
8998{
8999 struct drm_i915_private *dev_priv = dev->dev_private;
9000
9001 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
9002}
9003
9004static void i830_init_clock_gating(struct drm_device *dev)
9005{
9006 struct drm_i915_private *dev_priv = dev->dev_private;
9007
9008 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes652c3932009-08-17 13:31:43 -07009009}
9010
Jesse Barnes645c62a2011-05-11 09:49:31 -07009011static void ibx_init_clock_gating(struct drm_device *dev)
9012{
9013 struct drm_i915_private *dev_priv = dev->dev_private;
9014
9015 /*
9016 * On Ibex Peak and Cougar Point, we need to disable clock
9017 * gating for the panel power sequencer or it will fail to
9018 * start up when no ports are active.
9019 */
9020 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
9021}
9022
9023static void cpt_init_clock_gating(struct drm_device *dev)
9024{
9025 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes3bcf6032011-07-27 11:51:40 -07009026 int pipe;
Jesse Barnes645c62a2011-05-11 09:49:31 -07009027
9028 /*
9029 * On Ibex Peak and Cougar Point, we need to disable clock
9030 * gating for the panel power sequencer or it will fail to
9031 * start up when no ports are active.
9032 */
9033 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
9034 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
9035 DPLS_EDP_PPS_FIX_DIS);
Jesse Barnes3bcf6032011-07-27 11:51:40 -07009036 /* Without this, mode sets may fail silently on FDI */
9037 for_each_pipe(pipe)
9038 I915_WRITE(TRANS_CHICKEN2(pipe), TRANS_AUTOTRAIN_GEN_STALL_DIS);
Jesse Barnes79e53942008-11-07 14:24:08 -08009039}
9040
Chris Wilsonac668082011-02-09 16:15:32 +00009041static void ironlake_teardown_rc6(struct drm_device *dev)
Chris Wilson0cdab212010-12-05 17:27:06 +00009042{
9043 struct drm_i915_private *dev_priv = dev->dev_private;
9044
9045 if (dev_priv->renderctx) {
Chris Wilsonac668082011-02-09 16:15:32 +00009046 i915_gem_object_unpin(dev_priv->renderctx);
9047 drm_gem_object_unreference(&dev_priv->renderctx->base);
Chris Wilson0cdab212010-12-05 17:27:06 +00009048 dev_priv->renderctx = NULL;
9049 }
9050
9051 if (dev_priv->pwrctx) {
Chris Wilsonac668082011-02-09 16:15:32 +00009052 i915_gem_object_unpin(dev_priv->pwrctx);
9053 drm_gem_object_unreference(&dev_priv->pwrctx->base);
Chris Wilson0cdab212010-12-05 17:27:06 +00009054 dev_priv->pwrctx = NULL;
9055 }
9056}
9057
Jesse Barnesd5bb0812011-01-05 12:01:26 -08009058static void ironlake_disable_rc6(struct drm_device *dev)
9059{
9060 struct drm_i915_private *dev_priv = dev->dev_private;
9061
Chris Wilsonac668082011-02-09 16:15:32 +00009062 if (I915_READ(PWRCTXA)) {
9063 /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
9064 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
9065 wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
9066 50);
9067
9068 I915_WRITE(PWRCTXA, 0);
9069 POSTING_READ(PWRCTXA);
9070
9071 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
9072 POSTING_READ(RSTDBYCTL);
9073 }
9074
Chris Wilson99507302011-02-24 09:42:52 +00009075 ironlake_teardown_rc6(dev);
Chris Wilsonac668082011-02-09 16:15:32 +00009076}
9077
9078static int ironlake_setup_rc6(struct drm_device *dev)
9079{
9080 struct drm_i915_private *dev_priv = dev->dev_private;
9081
9082 if (dev_priv->renderctx == NULL)
9083 dev_priv->renderctx = intel_alloc_context_page(dev);
9084 if (!dev_priv->renderctx)
9085 return -ENOMEM;
9086
9087 if (dev_priv->pwrctx == NULL)
9088 dev_priv->pwrctx = intel_alloc_context_page(dev);
9089 if (!dev_priv->pwrctx) {
9090 ironlake_teardown_rc6(dev);
9091 return -ENOMEM;
9092 }
9093
9094 return 0;
Jesse Barnesd5bb0812011-01-05 12:01:26 -08009095}
9096
9097void ironlake_enable_rc6(struct drm_device *dev)
9098{
9099 struct drm_i915_private *dev_priv = dev->dev_private;
9100 int ret;
9101
Chris Wilsonac668082011-02-09 16:15:32 +00009102 /* rc6 disabled by default due to repeated reports of hanging during
9103 * boot and resume.
9104 */
Keith Packardc0f372b32011-11-16 22:24:52 -08009105 if (!intel_enable_rc6(dev))
Chris Wilsonac668082011-02-09 16:15:32 +00009106 return;
9107
Ben Widawsky2c34b852011-03-19 18:14:26 -07009108 mutex_lock(&dev->struct_mutex);
Chris Wilsonac668082011-02-09 16:15:32 +00009109 ret = ironlake_setup_rc6(dev);
Ben Widawsky2c34b852011-03-19 18:14:26 -07009110 if (ret) {
9111 mutex_unlock(&dev->struct_mutex);
Chris Wilsonac668082011-02-09 16:15:32 +00009112 return;
Ben Widawsky2c34b852011-03-19 18:14:26 -07009113 }
Chris Wilsonac668082011-02-09 16:15:32 +00009114
Jesse Barnesd5bb0812011-01-05 12:01:26 -08009115 /*
9116 * GPU can automatically power down the render unit if given a page
9117 * to save state.
9118 */
9119 ret = BEGIN_LP_RING(6);
9120 if (ret) {
Chris Wilsonac668082011-02-09 16:15:32 +00009121 ironlake_teardown_rc6(dev);
Ben Widawsky2c34b852011-03-19 18:14:26 -07009122 mutex_unlock(&dev->struct_mutex);
Jesse Barnesd5bb0812011-01-05 12:01:26 -08009123 return;
9124 }
Chris Wilsonac668082011-02-09 16:15:32 +00009125
Jesse Barnesd5bb0812011-01-05 12:01:26 -08009126 OUT_RING(MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
9127 OUT_RING(MI_SET_CONTEXT);
9128 OUT_RING(dev_priv->renderctx->gtt_offset |
9129 MI_MM_SPACE_GTT |
9130 MI_SAVE_EXT_STATE_EN |
9131 MI_RESTORE_EXT_STATE_EN |
9132 MI_RESTORE_INHIBIT);
9133 OUT_RING(MI_SUSPEND_FLUSH);
9134 OUT_RING(MI_NOOP);
9135 OUT_RING(MI_FLUSH);
9136 ADVANCE_LP_RING();
9137
Ben Widawsky4a246cf2011-03-19 18:14:28 -07009138 /*
9139 * Wait for the command parser to advance past MI_SET_CONTEXT. The HW
9140 * does an implicit flush, combined with MI_FLUSH above, it should be
9141 * safe to assume that renderctx is valid
9142 */
9143 ret = intel_wait_ring_idle(LP_RING(dev_priv));
9144 if (ret) {
9145 DRM_ERROR("failed to enable ironlake power power savings\n");
9146 ironlake_teardown_rc6(dev);
9147 mutex_unlock(&dev->struct_mutex);
9148 return;
9149 }
9150
Jesse Barnesd5bb0812011-01-05 12:01:26 -08009151 I915_WRITE(PWRCTXA, dev_priv->pwrctx->gtt_offset | PWRCTX_EN);
9152 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
Ben Widawsky2c34b852011-03-19 18:14:26 -07009153 mutex_unlock(&dev->struct_mutex);
Jesse Barnesd5bb0812011-01-05 12:01:26 -08009154}
9155
Jesse Barnes645c62a2011-05-11 09:49:31 -07009156void intel_init_clock_gating(struct drm_device *dev)
9157{
9158 struct drm_i915_private *dev_priv = dev->dev_private;
9159
9160 dev_priv->display.init_clock_gating(dev);
9161
9162 if (dev_priv->display.init_pch_clock_gating)
9163 dev_priv->display.init_pch_clock_gating(dev);
9164}
Chris Wilsonac668082011-02-09 16:15:32 +00009165
Jesse Barnese70236a2009-09-21 10:42:27 -07009166/* Set up chip specific display functions */
9167static void intel_init_display(struct drm_device *dev)
9168{
9169 struct drm_i915_private *dev_priv = dev->dev_private;
9170
9171 /* We always want a DPMS function */
Eric Anholtf564048e2011-03-30 13:01:02 -07009172 if (HAS_PCH_SPLIT(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05009173 dev_priv->display.dpms = ironlake_crtc_dpms;
Eric Anholtf564048e2011-03-30 13:01:02 -07009174 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Jesse Barnes17638cd2011-06-24 12:19:23 -07009175 dev_priv->display.update_plane = ironlake_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07009176 } else {
Jesse Barnese70236a2009-09-21 10:42:27 -07009177 dev_priv->display.dpms = i9xx_crtc_dpms;
Eric Anholtf564048e2011-03-30 13:01:02 -07009178 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Jesse Barnes17638cd2011-06-24 12:19:23 -07009179 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07009180 }
Jesse Barnese70236a2009-09-21 10:42:27 -07009181
Adam Jacksonee5382a2010-04-23 11:17:39 -04009182 if (I915_HAS_FBC(dev)) {
Yuanhan Liu9c04f012010-12-15 15:42:32 +08009183 if (HAS_PCH_SPLIT(dev)) {
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08009184 dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
9185 dev_priv->display.enable_fbc = ironlake_enable_fbc;
9186 dev_priv->display.disable_fbc = ironlake_disable_fbc;
9187 } else if (IS_GM45(dev)) {
Jesse Barnes74dff282009-09-14 15:39:40 -07009188 dev_priv->display.fbc_enabled = g4x_fbc_enabled;
9189 dev_priv->display.enable_fbc = g4x_enable_fbc;
9190 dev_priv->display.disable_fbc = g4x_disable_fbc;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009191 } else if (IS_CRESTLINE(dev)) {
Jesse Barnese70236a2009-09-21 10:42:27 -07009192 dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
9193 dev_priv->display.enable_fbc = i8xx_enable_fbc;
9194 dev_priv->display.disable_fbc = i8xx_disable_fbc;
9195 }
Jesse Barnes74dff282009-09-14 15:39:40 -07009196 /* 855GM needs testing */
Jesse Barnese70236a2009-09-21 10:42:27 -07009197 }
9198
9199 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07009200 if (IS_VALLEYVIEW(dev))
9201 dev_priv->display.get_display_clock_speed =
9202 valleyview_get_display_clock_speed;
9203 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -07009204 dev_priv->display.get_display_clock_speed =
9205 i945_get_display_clock_speed;
9206 else if (IS_I915G(dev))
9207 dev_priv->display.get_display_clock_speed =
9208 i915_get_display_clock_speed;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05009209 else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07009210 dev_priv->display.get_display_clock_speed =
9211 i9xx_misc_get_display_clock_speed;
9212 else if (IS_I915GM(dev))
9213 dev_priv->display.get_display_clock_speed =
9214 i915gm_get_display_clock_speed;
9215 else if (IS_I865G(dev))
9216 dev_priv->display.get_display_clock_speed =
9217 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +02009218 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07009219 dev_priv->display.get_display_clock_speed =
9220 i855_get_display_clock_speed;
9221 else /* 852, 830 */
9222 dev_priv->display.get_display_clock_speed =
9223 i830_get_display_clock_speed;
9224
9225 /* For FIFO watermark updates */
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08009226 if (HAS_PCH_SPLIT(dev)) {
Keith Packard8d715f02011-11-18 20:39:01 -08009227 dev_priv->display.force_wake_get = __gen6_gt_force_wake_get;
9228 dev_priv->display.force_wake_put = __gen6_gt_force_wake_put;
9229
9230 /* IVB configs may use multi-threaded forcewake */
9231 if (IS_IVYBRIDGE(dev)) {
9232 u32 ecobus;
9233
Keith Packardc7dffff2011-12-09 11:33:00 -08009234 /* A small trick here - if the bios hasn't configured MT forcewake,
9235 * and if the device is in RC6, then force_wake_mt_get will not wake
9236 * the device and the ECOBUS read will return zero. Which will be
9237 * (correctly) interpreted by the test below as MT forcewake being
9238 * disabled.
9239 */
Keith Packard8d715f02011-11-18 20:39:01 -08009240 mutex_lock(&dev->struct_mutex);
9241 __gen6_gt_force_wake_mt_get(dev_priv);
Keith Packardc7dffff2011-12-09 11:33:00 -08009242 ecobus = I915_READ_NOTRACE(ECOBUS);
Keith Packard8d715f02011-11-18 20:39:01 -08009243 __gen6_gt_force_wake_mt_put(dev_priv);
9244 mutex_unlock(&dev->struct_mutex);
9245
9246 if (ecobus & FORCEWAKE_MT_ENABLE) {
9247 DRM_DEBUG_KMS("Using MT version of forcewake\n");
9248 dev_priv->display.force_wake_get =
9249 __gen6_gt_force_wake_mt_get;
9250 dev_priv->display.force_wake_put =
9251 __gen6_gt_force_wake_mt_put;
9252 }
9253 }
9254
Jesse Barnes645c62a2011-05-11 09:49:31 -07009255 if (HAS_PCH_IBX(dev))
9256 dev_priv->display.init_pch_clock_gating = ibx_init_clock_gating;
9257 else if (HAS_PCH_CPT(dev))
9258 dev_priv->display.init_pch_clock_gating = cpt_init_clock_gating;
9259
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01009260 if (IS_GEN5(dev)) {
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08009261 if (I915_READ(MLTR_ILK) & ILK_SRLT_MASK)
9262 dev_priv->display.update_wm = ironlake_update_wm;
9263 else {
9264 DRM_DEBUG_KMS("Failed to get proper latency. "
9265 "Disable CxSR\n");
9266 dev_priv->display.update_wm = NULL;
9267 }
Jesse Barnes674cf962011-04-28 14:27:04 -07009268 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Jesse Barnes6067aae2011-04-28 15:04:31 -07009269 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
Wu Fengguange0dac652011-09-05 14:25:34 +08009270 dev_priv->display.write_eld = ironlake_write_eld;
Yuanhan Liu13982612010-12-15 15:42:31 +08009271 } else if (IS_GEN6(dev)) {
9272 if (SNB_READ_WM0_LATENCY()) {
9273 dev_priv->display.update_wm = sandybridge_update_wm;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08009274 dev_priv->display.update_sprite_wm = sandybridge_update_sprite_wm;
Yuanhan Liu13982612010-12-15 15:42:31 +08009275 } else {
9276 DRM_DEBUG_KMS("Failed to read display plane latency. "
9277 "Disable CxSR\n");
9278 dev_priv->display.update_wm = NULL;
9279 }
Jesse Barnes674cf962011-04-28 14:27:04 -07009280 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Jesse Barnes6067aae2011-04-28 15:04:31 -07009281 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
Wu Fengguange0dac652011-09-05 14:25:34 +08009282 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnes357555c2011-04-28 15:09:55 -07009283 } else if (IS_IVYBRIDGE(dev)) {
9284 /* FIXME: detect B0+ stepping and use auto training */
9285 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Jesse Barnesfe100d42011-04-28 14:29:45 -07009286 if (SNB_READ_WM0_LATENCY()) {
9287 dev_priv->display.update_wm = sandybridge_update_wm;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08009288 dev_priv->display.update_sprite_wm = sandybridge_update_sprite_wm;
Jesse Barnesfe100d42011-04-28 14:29:45 -07009289 } else {
9290 DRM_DEBUG_KMS("Failed to read display plane latency. "
9291 "Disable CxSR\n");
9292 dev_priv->display.update_wm = NULL;
9293 }
Jesse Barnes28963a32011-05-11 09:42:30 -07009294 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
Wu Fengguange0dac652011-09-05 14:25:34 +08009295 dev_priv->display.write_eld = ironlake_write_eld;
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08009296 } else
9297 dev_priv->display.update_wm = NULL;
Jesse Barnesceb04242012-03-28 13:39:22 -07009298 } else if (IS_VALLEYVIEW(dev)) {
9299 dev_priv->display.update_wm = valleyview_update_wm;
Jesse Barnesfb046852012-03-28 13:39:26 -07009300 dev_priv->display.init_clock_gating =
9301 valleyview_init_clock_gating;
Jesse Barnes575155a2012-03-28 13:39:37 -07009302 dev_priv->display.force_wake_get = vlv_force_wake_get;
9303 dev_priv->display.force_wake_put = vlv_force_wake_put;
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08009304 } else if (IS_PINEVIEW(dev)) {
Zhao Yakuid4294342010-03-22 22:45:36 +08009305 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
Li Peng95534262010-05-18 18:58:44 +08009306 dev_priv->is_ddr3,
Zhao Yakuid4294342010-03-22 22:45:36 +08009307 dev_priv->fsb_freq,
9308 dev_priv->mem_freq)) {
9309 DRM_INFO("failed to find known CxSR latency "
Li Peng95534262010-05-18 18:58:44 +08009310 "(found ddr%s fsb freq %d, mem freq %d), "
Zhao Yakuid4294342010-03-22 22:45:36 +08009311 "disabling CxSR\n",
Akshay Joshi0206e352011-08-16 15:34:10 -04009312 (dev_priv->is_ddr3 == 1) ? "3" : "2",
Zhao Yakuid4294342010-03-22 22:45:36 +08009313 dev_priv->fsb_freq, dev_priv->mem_freq);
9314 /* Disable CxSR and never update its watermark again */
9315 pineview_disable_cxsr(dev);
9316 dev_priv->display.update_wm = NULL;
9317 } else
9318 dev_priv->display.update_wm = pineview_update_wm;
Jason Stubbs95e0ee92011-05-28 14:26:48 +10009319 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
Jesse Barnes6067aae2011-04-28 15:04:31 -07009320 } else if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +08009321 dev_priv->display.write_eld = g4x_write_eld;
Jesse Barnese70236a2009-09-21 10:42:27 -07009322 dev_priv->display.update_wm = g4x_update_wm;
Jesse Barnes6067aae2011-04-28 15:04:31 -07009323 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
9324 } else if (IS_GEN4(dev)) {
Jesse Barnese70236a2009-09-21 10:42:27 -07009325 dev_priv->display.update_wm = i965_update_wm;
Jesse Barnes6067aae2011-04-28 15:04:31 -07009326 if (IS_CRESTLINE(dev))
9327 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
9328 else if (IS_BROADWATER(dev))
9329 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
9330 } else if (IS_GEN3(dev)) {
Jesse Barnese70236a2009-09-21 10:42:27 -07009331 dev_priv->display.update_wm = i9xx_update_wm;
9332 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Jesse Barnes6067aae2011-04-28 15:04:31 -07009333 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
9334 } else if (IS_I865G(dev)) {
9335 dev_priv->display.update_wm = i830_update_wm;
9336 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
9337 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Adam Jackson8f4695e2010-04-16 18:20:57 -04009338 } else if (IS_I85X(dev)) {
9339 dev_priv->display.update_wm = i9xx_update_wm;
9340 dev_priv->display.get_fifo_size = i85x_get_fifo_size;
Jesse Barnes6067aae2011-04-28 15:04:31 -07009341 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
Jesse Barnese70236a2009-09-21 10:42:27 -07009342 } else {
Adam Jackson8f4695e2010-04-16 18:20:57 -04009343 dev_priv->display.update_wm = i830_update_wm;
Jesse Barnes6067aae2011-04-28 15:04:31 -07009344 dev_priv->display.init_clock_gating = i830_init_clock_gating;
Adam Jackson8f4695e2010-04-16 18:20:57 -04009345 if (IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07009346 dev_priv->display.get_fifo_size = i845_get_fifo_size;
9347 else
9348 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Jesse Barnese70236a2009-09-21 10:42:27 -07009349 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009350
9351 /* Default just returns -ENODEV to indicate unsupported */
9352 dev_priv->display.queue_flip = intel_default_queue_flip;
9353
9354 switch (INTEL_INFO(dev)->gen) {
9355 case 2:
9356 dev_priv->display.queue_flip = intel_gen2_queue_flip;
9357 break;
9358
9359 case 3:
9360 dev_priv->display.queue_flip = intel_gen3_queue_flip;
9361 break;
9362
9363 case 4:
9364 case 5:
9365 dev_priv->display.queue_flip = intel_gen4_queue_flip;
9366 break;
9367
9368 case 6:
9369 dev_priv->display.queue_flip = intel_gen6_queue_flip;
9370 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009371 case 7:
9372 dev_priv->display.queue_flip = intel_gen7_queue_flip;
9373 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009374 }
Jesse Barnese70236a2009-09-21 10:42:27 -07009375}
9376
Jesse Barnesb690e962010-07-19 13:53:12 -07009377/*
9378 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
9379 * resume, or other times. This quirk makes sure that's the case for
9380 * affected systems.
9381 */
Akshay Joshi0206e352011-08-16 15:34:10 -04009382static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -07009383{
9384 struct drm_i915_private *dev_priv = dev->dev_private;
9385
9386 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02009387 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -07009388}
9389
Keith Packard435793d2011-07-12 14:56:22 -07009390/*
9391 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
9392 */
9393static void quirk_ssc_force_disable(struct drm_device *dev)
9394{
9395 struct drm_i915_private *dev_priv = dev->dev_private;
9396 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02009397 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -07009398}
9399
Carsten Emde4dca20e2012-03-15 15:56:26 +01009400/*
Carsten Emde5a15ab52012-03-15 15:56:27 +01009401 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
9402 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +01009403 */
9404static void quirk_invert_brightness(struct drm_device *dev)
9405{
9406 struct drm_i915_private *dev_priv = dev->dev_private;
9407 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02009408 DRM_INFO("applying inverted panel brightness quirk\n");
Carsten Emde4dca20e2012-03-15 15:56:26 +01009409}
9410
Jesse Barnesb690e962010-07-19 13:53:12 -07009411struct intel_quirk {
9412 int device;
9413 int subsystem_vendor;
9414 int subsystem_device;
9415 void (*hook)(struct drm_device *dev);
9416};
9417
9418struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -07009419 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -04009420 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -07009421
9422 /* Thinkpad R31 needs pipe A force quirk */
9423 { 0x3577, 0x1014, 0x0505, quirk_pipea_force },
9424 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
9425 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
9426
9427 /* ThinkPad X30 needs pipe A force quirk (LP: #304614) */
9428 { 0x3577, 0x1014, 0x0513, quirk_pipea_force },
9429 /* ThinkPad X40 needs pipe A force quirk */
9430
9431 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
9432 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
9433
9434 /* 855 & before need to leave pipe A & dpll A up */
9435 { 0x3582, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
9436 { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Keith Packard435793d2011-07-12 14:56:22 -07009437
9438 /* Lenovo U160 cannot use SSC on LVDS */
9439 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +02009440
9441 /* Sony Vaio Y cannot use SSC on LVDS */
9442 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +01009443
9444 /* Acer Aspire 5734Z must invert backlight brightness */
9445 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
Jesse Barnesb690e962010-07-19 13:53:12 -07009446};
9447
9448static void intel_init_quirks(struct drm_device *dev)
9449{
9450 struct pci_dev *d = dev->pdev;
9451 int i;
9452
9453 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
9454 struct intel_quirk *q = &intel_quirks[i];
9455
9456 if (d->device == q->device &&
9457 (d->subsystem_vendor == q->subsystem_vendor ||
9458 q->subsystem_vendor == PCI_ANY_ID) &&
9459 (d->subsystem_device == q->subsystem_device ||
9460 q->subsystem_device == PCI_ANY_ID))
9461 q->hook(dev);
9462 }
9463}
9464
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009465/* Disable the VGA plane that we never use */
9466static void i915_disable_vga(struct drm_device *dev)
9467{
9468 struct drm_i915_private *dev_priv = dev->dev_private;
9469 u8 sr1;
9470 u32 vga_reg;
9471
9472 if (HAS_PCH_SPLIT(dev))
9473 vga_reg = CPU_VGACNTRL;
9474 else
9475 vga_reg = VGACNTRL;
9476
9477 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
9478 outb(1, VGA_SR_INDEX);
9479 sr1 = inb(VGA_SR_DATA);
9480 outb(sr1 | 1<<5, VGA_SR_DATA);
9481 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
9482 udelay(300);
9483
9484 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9485 POSTING_READ(vga_reg);
9486}
9487
Jesse Barnes79e53942008-11-07 14:24:08 -08009488void intel_modeset_init(struct drm_device *dev)
9489{
Jesse Barnes652c3932009-08-17 13:31:43 -07009490 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08009491 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08009492
9493 drm_mode_config_init(dev);
9494
9495 dev->mode_config.min_width = 0;
9496 dev->mode_config.min_height = 0;
9497
Dave Airlie019d96c2011-09-29 16:20:42 +01009498 dev->mode_config.preferred_depth = 24;
9499 dev->mode_config.prefer_shadow = 1;
9500
Jesse Barnes79e53942008-11-07 14:24:08 -08009501 dev->mode_config.funcs = (void *)&intel_mode_funcs;
9502
Jesse Barnesb690e962010-07-19 13:53:12 -07009503 intel_init_quirks(dev);
9504
Jesse Barnese70236a2009-09-21 10:42:27 -07009505 intel_init_display(dev);
9506
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009507 if (IS_GEN2(dev)) {
9508 dev->mode_config.max_width = 2048;
9509 dev->mode_config.max_height = 2048;
9510 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -07009511 dev->mode_config.max_width = 4096;
9512 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -08009513 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009514 dev->mode_config.max_width = 8192;
9515 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -08009516 }
Chris Wilson35c30472010-12-22 14:07:12 +00009517 dev->mode_config.fb_base = dev->agp->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08009518
Zhao Yakui28c97732009-10-09 11:39:41 +08009519 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Dave Airliea3524f12010-06-06 18:59:41 +10009520 dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -08009521
Dave Airliea3524f12010-06-06 18:59:41 +10009522 for (i = 0; i < dev_priv->num_pipe; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009523 intel_crtc_init(dev, i);
Jesse Barnes00c2064b2012-01-13 15:48:39 -08009524 ret = intel_plane_init(dev, i);
9525 if (ret)
9526 DRM_DEBUG_KMS("plane %d init failed: %d\n", i, ret);
Jesse Barnes79e53942008-11-07 14:24:08 -08009527 }
9528
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009529 /* Just disable it once at startup */
9530 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009531 intel_setup_outputs(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -07009532
Jesse Barnes645c62a2011-05-11 09:49:31 -07009533 intel_init_clock_gating(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009534
Jesse Barnes7648fa92010-05-20 14:28:11 -07009535 if (IS_IRONLAKE_M(dev)) {
Jesse Barnesf97108d2010-01-29 11:27:07 -08009536 ironlake_enable_drps(dev);
Jesse Barnes7648fa92010-05-20 14:28:11 -07009537 intel_init_emon(dev);
9538 }
Jesse Barnesf97108d2010-01-29 11:27:07 -08009539
Jesse Barnes1c70c0c2011-06-29 13:34:36 -07009540 if (IS_GEN6(dev) || IS_GEN7(dev)) {
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08009541 gen6_enable_rps(dev_priv);
Jesse Barnes23b2f8b2011-06-28 13:04:16 -07009542 gen6_update_ring_freq(dev_priv);
9543 }
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08009544
Jesse Barnes652c3932009-08-17 13:31:43 -07009545 INIT_WORK(&dev_priv->idle_work, intel_idle_update);
9546 setup_timer(&dev_priv->idle_timer, intel_gpu_idle_timer,
9547 (unsigned long)dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01009548}
9549
9550void intel_modeset_gem_init(struct drm_device *dev)
9551{
9552 if (IS_IRONLAKE_M(dev))
9553 ironlake_enable_rc6(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02009554
9555 intel_setup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009556}
9557
9558void intel_modeset_cleanup(struct drm_device *dev)
9559{
Jesse Barnes652c3932009-08-17 13:31:43 -07009560 struct drm_i915_private *dev_priv = dev->dev_private;
9561 struct drm_crtc *crtc;
9562 struct intel_crtc *intel_crtc;
9563
Keith Packardf87ea762010-10-03 19:36:26 -07009564 drm_kms_helper_poll_fini(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -07009565 mutex_lock(&dev->struct_mutex);
9566
Jesse Barnes723bfd72010-10-07 16:01:13 -07009567 intel_unregister_dsm_handler();
9568
9569
Jesse Barnes652c3932009-08-17 13:31:43 -07009570 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
9571 /* Skip inactive CRTCs */
9572 if (!crtc->fb)
9573 continue;
9574
9575 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3dec0092010-08-20 21:40:52 +02009576 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07009577 }
9578
Chris Wilson973d04f2011-07-08 12:22:37 +01009579 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -07009580
Jesse Barnesf97108d2010-01-29 11:27:07 -08009581 if (IS_IRONLAKE_M(dev))
9582 ironlake_disable_drps(dev);
Jesse Barnes1c70c0c2011-06-29 13:34:36 -07009583 if (IS_GEN6(dev) || IS_GEN7(dev))
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08009584 gen6_disable_rps(dev);
Jesse Barnesf97108d2010-01-29 11:27:07 -08009585
Jesse Barnesd5bb0812011-01-05 12:01:26 -08009586 if (IS_IRONLAKE_M(dev))
9587 ironlake_disable_rc6(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +00009588
Jesse Barnes57f350b2012-03-28 13:39:25 -07009589 if (IS_VALLEYVIEW(dev))
9590 vlv_init_dpio(dev);
9591
Kristian Høgsberg69341a52009-11-11 12:19:17 -05009592 mutex_unlock(&dev->struct_mutex);
9593
Daniel Vetter6c0d93502010-08-20 18:26:46 +02009594 /* Disable the irq before mode object teardown, for the irq might
9595 * enqueue unpin/hotplug work. */
9596 drm_irq_uninstall(dev);
9597 cancel_work_sync(&dev_priv->hotplug_work);
Daniel Vetter6fdd4d92011-09-08 14:00:22 +02009598 cancel_work_sync(&dev_priv->rps_work);
Daniel Vetter6c0d93502010-08-20 18:26:46 +02009599
Chris Wilson1630fe72011-07-08 12:22:42 +01009600 /* flush any delayed tasks or pending work */
9601 flush_scheduled_work();
9602
Daniel Vetter3dec0092010-08-20 21:40:52 +02009603 /* Shut off idle work before the crtcs get freed. */
9604 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
9605 intel_crtc = to_intel_crtc(crtc);
9606 del_timer_sync(&intel_crtc->idle_timer);
9607 }
9608 del_timer_sync(&dev_priv->idle_timer);
9609 cancel_work_sync(&dev_priv->idle_work);
9610
Jesse Barnes79e53942008-11-07 14:24:08 -08009611 drm_mode_config_cleanup(dev);
9612}
9613
Dave Airlie28d52042009-09-21 14:33:58 +10009614/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +08009615 * Return which encoder is currently attached for connector.
9616 */
Chris Wilsondf0e9242010-09-09 16:20:55 +01009617struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -08009618{
Chris Wilsondf0e9242010-09-09 16:20:55 +01009619 return &intel_attached_encoder(connector)->base;
9620}
Jesse Barnes79e53942008-11-07 14:24:08 -08009621
Chris Wilsondf0e9242010-09-09 16:20:55 +01009622void intel_connector_attach_encoder(struct intel_connector *connector,
9623 struct intel_encoder *encoder)
9624{
9625 connector->encoder = encoder;
9626 drm_mode_connector_attach_encoder(&connector->base,
9627 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08009628}
Dave Airlie28d52042009-09-21 14:33:58 +10009629
9630/*
9631 * set vga decode state - true == enable VGA decode
9632 */
9633int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
9634{
9635 struct drm_i915_private *dev_priv = dev->dev_private;
9636 u16 gmch_ctrl;
9637
9638 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
9639 if (state)
9640 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
9641 else
9642 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
9643 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
9644 return 0;
9645}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009646
9647#ifdef CONFIG_DEBUG_FS
9648#include <linux/seq_file.h>
9649
9650struct intel_display_error_state {
9651 struct intel_cursor_error_state {
9652 u32 control;
9653 u32 position;
9654 u32 base;
9655 u32 size;
9656 } cursor[2];
9657
9658 struct intel_pipe_error_state {
9659 u32 conf;
9660 u32 source;
9661
9662 u32 htotal;
9663 u32 hblank;
9664 u32 hsync;
9665 u32 vtotal;
9666 u32 vblank;
9667 u32 vsync;
9668 } pipe[2];
9669
9670 struct intel_plane_error_state {
9671 u32 control;
9672 u32 stride;
9673 u32 size;
9674 u32 pos;
9675 u32 addr;
9676 u32 surface;
9677 u32 tile_offset;
9678 } plane[2];
9679};
9680
9681struct intel_display_error_state *
9682intel_display_capture_error_state(struct drm_device *dev)
9683{
Akshay Joshi0206e352011-08-16 15:34:10 -04009684 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009685 struct intel_display_error_state *error;
9686 int i;
9687
9688 error = kmalloc(sizeof(*error), GFP_ATOMIC);
9689 if (error == NULL)
9690 return NULL;
9691
9692 for (i = 0; i < 2; i++) {
9693 error->cursor[i].control = I915_READ(CURCNTR(i));
9694 error->cursor[i].position = I915_READ(CURPOS(i));
9695 error->cursor[i].base = I915_READ(CURBASE(i));
9696
9697 error->plane[i].control = I915_READ(DSPCNTR(i));
9698 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
9699 error->plane[i].size = I915_READ(DSPSIZE(i));
Akshay Joshi0206e352011-08-16 15:34:10 -04009700 error->plane[i].pos = I915_READ(DSPPOS(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009701 error->plane[i].addr = I915_READ(DSPADDR(i));
9702 if (INTEL_INFO(dev)->gen >= 4) {
9703 error->plane[i].surface = I915_READ(DSPSURF(i));
9704 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
9705 }
9706
9707 error->pipe[i].conf = I915_READ(PIPECONF(i));
9708 error->pipe[i].source = I915_READ(PIPESRC(i));
9709 error->pipe[i].htotal = I915_READ(HTOTAL(i));
9710 error->pipe[i].hblank = I915_READ(HBLANK(i));
9711 error->pipe[i].hsync = I915_READ(HSYNC(i));
9712 error->pipe[i].vtotal = I915_READ(VTOTAL(i));
9713 error->pipe[i].vblank = I915_READ(VBLANK(i));
9714 error->pipe[i].vsync = I915_READ(VSYNC(i));
9715 }
9716
9717 return error;
9718}
9719
9720void
9721intel_display_print_error_state(struct seq_file *m,
9722 struct drm_device *dev,
9723 struct intel_display_error_state *error)
9724{
9725 int i;
9726
9727 for (i = 0; i < 2; i++) {
9728 seq_printf(m, "Pipe [%d]:\n", i);
9729 seq_printf(m, " CONF: %08x\n", error->pipe[i].conf);
9730 seq_printf(m, " SRC: %08x\n", error->pipe[i].source);
9731 seq_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
9732 seq_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
9733 seq_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
9734 seq_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
9735 seq_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
9736 seq_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
9737
9738 seq_printf(m, "Plane [%d]:\n", i);
9739 seq_printf(m, " CNTR: %08x\n", error->plane[i].control);
9740 seq_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
9741 seq_printf(m, " SIZE: %08x\n", error->plane[i].size);
9742 seq_printf(m, " POS: %08x\n", error->plane[i].pos);
9743 seq_printf(m, " ADDR: %08x\n", error->plane[i].addr);
9744 if (INTEL_INFO(dev)->gen >= 4) {
9745 seq_printf(m, " SURF: %08x\n", error->plane[i].surface);
9746 seq_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
9747 }
9748
9749 seq_printf(m, "Cursor [%d]:\n", i);
9750 seq_printf(m, " CNTR: %08x\n", error->cursor[i].control);
9751 seq_printf(m, " POS: %08x\n", error->cursor[i].position);
9752 seq_printf(m, " BASE: %08x\n", error->cursor[i].base);
9753 }
9754}
9755#endif