blob: 79753b8ac797d6307cb65436630b68a7791b77f1 [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
Jesse Barnesc1c7af62009-09-10 15:28:03 -070027#include <linux/module.h>
28#include <linux/input.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080029#include <linux/i2c.h>
Shaohua Li7662c8b2009-06-26 11:23:55 +080030#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Jesse Barnes9cce37f2010-08-13 15:11:26 -070032#include <linux/vgaarb.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080033#include "drmP.h"
34#include "intel_drv.h"
35#include "i915_drm.h"
36#include "i915_drv.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070037#include "i915_trace.h"
Dave Airlieab2c0672009-12-04 10:55:24 +100038#include "drm_dp_helper.h"
Jesse Barnes79e53942008-11-07 14:24:08 -080039
40#include "drm_crtc_helper.h"
41
Zhenyu Wang32f9d652009-07-24 01:00:32 +080042#define HAS_eDP (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
43
Jesse Barnes79e53942008-11-07 14:24:08 -080044bool intel_pipe_has_type (struct drm_crtc *crtc, int type);
Shaohua Li7662c8b2009-06-26 11:23:55 +080045static void intel_update_watermarks(struct drm_device *dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +020046static void intel_increase_pllclock(struct drm_crtc *crtc);
Chris Wilson6b383a72010-09-13 13:54:26 +010047static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080048
49typedef struct {
50 /* given values */
51 int n;
52 int m1, m2;
53 int p1, p2;
54 /* derived values */
55 int dot;
56 int vco;
57 int m;
58 int p;
59} intel_clock_t;
60
61typedef struct {
62 int min, max;
63} intel_range_t;
64
65typedef struct {
66 int dot_limit;
67 int p2_slow, p2_fast;
68} intel_p2_t;
69
70#define INTEL_P2_NUM 2
Ma Lingd4906092009-03-18 20:13:27 +080071typedef struct intel_limit intel_limit_t;
72struct intel_limit {
Jesse Barnes79e53942008-11-07 14:24:08 -080073 intel_range_t dot, vco, n, m, m1, m2, p, p1;
74 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +080075 bool (* find_pll)(const intel_limit_t *, struct drm_crtc *,
76 int, int, intel_clock_t *);
77};
Jesse Barnes79e53942008-11-07 14:24:08 -080078
79#define I8XX_DOT_MIN 25000
80#define I8XX_DOT_MAX 350000
81#define I8XX_VCO_MIN 930000
82#define I8XX_VCO_MAX 1400000
83#define I8XX_N_MIN 3
84#define I8XX_N_MAX 16
85#define I8XX_M_MIN 96
86#define I8XX_M_MAX 140
87#define I8XX_M1_MIN 18
88#define I8XX_M1_MAX 26
89#define I8XX_M2_MIN 6
90#define I8XX_M2_MAX 16
91#define I8XX_P_MIN 4
92#define I8XX_P_MAX 128
93#define I8XX_P1_MIN 2
94#define I8XX_P1_MAX 33
95#define I8XX_P1_LVDS_MIN 1
96#define I8XX_P1_LVDS_MAX 6
97#define I8XX_P2_SLOW 4
98#define I8XX_P2_FAST 2
99#define I8XX_P2_LVDS_SLOW 14
ling.ma@intel.com0c2e39522009-07-17 11:44:30 +0800100#define I8XX_P2_LVDS_FAST 7
Jesse Barnes79e53942008-11-07 14:24:08 -0800101#define I8XX_P2_SLOW_LIMIT 165000
102
103#define I9XX_DOT_MIN 20000
104#define I9XX_DOT_MAX 400000
105#define I9XX_VCO_MIN 1400000
106#define I9XX_VCO_MAX 2800000
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500107#define PINEVIEW_VCO_MIN 1700000
108#define PINEVIEW_VCO_MAX 3500000
Kristian Høgsbergf3cade52009-02-13 20:56:50 -0500109#define I9XX_N_MIN 1
110#define I9XX_N_MAX 6
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500111/* Pineview's Ncounter is a ring counter */
112#define PINEVIEW_N_MIN 3
113#define PINEVIEW_N_MAX 6
Jesse Barnes79e53942008-11-07 14:24:08 -0800114#define I9XX_M_MIN 70
115#define I9XX_M_MAX 120
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500116#define PINEVIEW_M_MIN 2
117#define PINEVIEW_M_MAX 256
Jesse Barnes79e53942008-11-07 14:24:08 -0800118#define I9XX_M1_MIN 10
Kristian Høgsbergf3cade52009-02-13 20:56:50 -0500119#define I9XX_M1_MAX 22
Jesse Barnes79e53942008-11-07 14:24:08 -0800120#define I9XX_M2_MIN 5
121#define I9XX_M2_MAX 9
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500122/* Pineview M1 is reserved, and must be 0 */
123#define PINEVIEW_M1_MIN 0
124#define PINEVIEW_M1_MAX 0
125#define PINEVIEW_M2_MIN 0
126#define PINEVIEW_M2_MAX 254
Jesse Barnes79e53942008-11-07 14:24:08 -0800127#define I9XX_P_SDVO_DAC_MIN 5
128#define I9XX_P_SDVO_DAC_MAX 80
129#define I9XX_P_LVDS_MIN 7
130#define I9XX_P_LVDS_MAX 98
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500131#define PINEVIEW_P_LVDS_MIN 7
132#define PINEVIEW_P_LVDS_MAX 112
Jesse Barnes79e53942008-11-07 14:24:08 -0800133#define I9XX_P1_MIN 1
134#define I9XX_P1_MAX 8
135#define I9XX_P2_SDVO_DAC_SLOW 10
136#define I9XX_P2_SDVO_DAC_FAST 5
137#define I9XX_P2_SDVO_DAC_SLOW_LIMIT 200000
138#define I9XX_P2_LVDS_SLOW 14
139#define I9XX_P2_LVDS_FAST 7
140#define I9XX_P2_LVDS_SLOW_LIMIT 112000
141
Ma Ling044c7c42009-03-18 20:13:23 +0800142/*The parameter is for SDVO on G4x platform*/
143#define G4X_DOT_SDVO_MIN 25000
144#define G4X_DOT_SDVO_MAX 270000
145#define G4X_VCO_MIN 1750000
146#define G4X_VCO_MAX 3500000
147#define G4X_N_SDVO_MIN 1
148#define G4X_N_SDVO_MAX 4
149#define G4X_M_SDVO_MIN 104
150#define G4X_M_SDVO_MAX 138
151#define G4X_M1_SDVO_MIN 17
152#define G4X_M1_SDVO_MAX 23
153#define G4X_M2_SDVO_MIN 5
154#define G4X_M2_SDVO_MAX 11
155#define G4X_P_SDVO_MIN 10
156#define G4X_P_SDVO_MAX 30
157#define G4X_P1_SDVO_MIN 1
158#define G4X_P1_SDVO_MAX 3
159#define G4X_P2_SDVO_SLOW 10
160#define G4X_P2_SDVO_FAST 10
161#define G4X_P2_SDVO_LIMIT 270000
162
163/*The parameter is for HDMI_DAC on G4x platform*/
164#define G4X_DOT_HDMI_DAC_MIN 22000
165#define G4X_DOT_HDMI_DAC_MAX 400000
166#define G4X_N_HDMI_DAC_MIN 1
167#define G4X_N_HDMI_DAC_MAX 4
168#define G4X_M_HDMI_DAC_MIN 104
169#define G4X_M_HDMI_DAC_MAX 138
170#define G4X_M1_HDMI_DAC_MIN 16
171#define G4X_M1_HDMI_DAC_MAX 23
172#define G4X_M2_HDMI_DAC_MIN 5
173#define G4X_M2_HDMI_DAC_MAX 11
174#define G4X_P_HDMI_DAC_MIN 5
175#define G4X_P_HDMI_DAC_MAX 80
176#define G4X_P1_HDMI_DAC_MIN 1
177#define G4X_P1_HDMI_DAC_MAX 8
178#define G4X_P2_HDMI_DAC_SLOW 10
179#define G4X_P2_HDMI_DAC_FAST 5
180#define G4X_P2_HDMI_DAC_LIMIT 165000
181
182/*The parameter is for SINGLE_CHANNEL_LVDS on G4x platform*/
183#define G4X_DOT_SINGLE_CHANNEL_LVDS_MIN 20000
184#define G4X_DOT_SINGLE_CHANNEL_LVDS_MAX 115000
185#define G4X_N_SINGLE_CHANNEL_LVDS_MIN 1
186#define G4X_N_SINGLE_CHANNEL_LVDS_MAX 3
187#define G4X_M_SINGLE_CHANNEL_LVDS_MIN 104
188#define G4X_M_SINGLE_CHANNEL_LVDS_MAX 138
189#define G4X_M1_SINGLE_CHANNEL_LVDS_MIN 17
190#define G4X_M1_SINGLE_CHANNEL_LVDS_MAX 23
191#define G4X_M2_SINGLE_CHANNEL_LVDS_MIN 5
192#define G4X_M2_SINGLE_CHANNEL_LVDS_MAX 11
193#define G4X_P_SINGLE_CHANNEL_LVDS_MIN 28
194#define G4X_P_SINGLE_CHANNEL_LVDS_MAX 112
195#define G4X_P1_SINGLE_CHANNEL_LVDS_MIN 2
196#define G4X_P1_SINGLE_CHANNEL_LVDS_MAX 8
197#define G4X_P2_SINGLE_CHANNEL_LVDS_SLOW 14
198#define G4X_P2_SINGLE_CHANNEL_LVDS_FAST 14
199#define G4X_P2_SINGLE_CHANNEL_LVDS_LIMIT 0
200
201/*The parameter is for DUAL_CHANNEL_LVDS on G4x platform*/
202#define G4X_DOT_DUAL_CHANNEL_LVDS_MIN 80000
203#define G4X_DOT_DUAL_CHANNEL_LVDS_MAX 224000
204#define G4X_N_DUAL_CHANNEL_LVDS_MIN 1
205#define G4X_N_DUAL_CHANNEL_LVDS_MAX 3
206#define G4X_M_DUAL_CHANNEL_LVDS_MIN 104
207#define G4X_M_DUAL_CHANNEL_LVDS_MAX 138
208#define G4X_M1_DUAL_CHANNEL_LVDS_MIN 17
209#define G4X_M1_DUAL_CHANNEL_LVDS_MAX 23
210#define G4X_M2_DUAL_CHANNEL_LVDS_MIN 5
211#define G4X_M2_DUAL_CHANNEL_LVDS_MAX 11
212#define G4X_P_DUAL_CHANNEL_LVDS_MIN 14
213#define G4X_P_DUAL_CHANNEL_LVDS_MAX 42
214#define G4X_P1_DUAL_CHANNEL_LVDS_MIN 2
215#define G4X_P1_DUAL_CHANNEL_LVDS_MAX 6
216#define G4X_P2_DUAL_CHANNEL_LVDS_SLOW 7
217#define G4X_P2_DUAL_CHANNEL_LVDS_FAST 7
218#define G4X_P2_DUAL_CHANNEL_LVDS_LIMIT 0
219
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700220/*The parameter is for DISPLAY PORT on G4x platform*/
221#define G4X_DOT_DISPLAY_PORT_MIN 161670
222#define G4X_DOT_DISPLAY_PORT_MAX 227000
223#define G4X_N_DISPLAY_PORT_MIN 1
224#define G4X_N_DISPLAY_PORT_MAX 2
225#define G4X_M_DISPLAY_PORT_MIN 97
226#define G4X_M_DISPLAY_PORT_MAX 108
227#define G4X_M1_DISPLAY_PORT_MIN 0x10
228#define G4X_M1_DISPLAY_PORT_MAX 0x12
229#define G4X_M2_DISPLAY_PORT_MIN 0x05
230#define G4X_M2_DISPLAY_PORT_MAX 0x06
231#define G4X_P_DISPLAY_PORT_MIN 10
232#define G4X_P_DISPLAY_PORT_MAX 20
233#define G4X_P1_DISPLAY_PORT_MIN 1
234#define G4X_P1_DISPLAY_PORT_MAX 2
235#define G4X_P2_DISPLAY_PORT_SLOW 10
236#define G4X_P2_DISPLAY_PORT_FAST 10
237#define G4X_P2_DISPLAY_PORT_LIMIT 0
238
Eric Anholtbad720f2009-10-22 16:11:14 -0700239/* Ironlake / Sandybridge */
Zhenyu Wang2c072452009-06-05 15:38:42 +0800240/* as we calculate clock using (register_value + 2) for
241 N/M1/M2, so here the range value for them is (actual_value-2).
242 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500243#define IRONLAKE_DOT_MIN 25000
244#define IRONLAKE_DOT_MAX 350000
245#define IRONLAKE_VCO_MIN 1760000
246#define IRONLAKE_VCO_MAX 3510000
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500247#define IRONLAKE_M1_MIN 12
Zhao Yakuia59e3852010-01-06 22:05:57 +0800248#define IRONLAKE_M1_MAX 22
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500249#define IRONLAKE_M2_MIN 5
250#define IRONLAKE_M2_MAX 9
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500251#define IRONLAKE_P2_DOT_LIMIT 225000 /* 225Mhz */
Zhenyu Wang2c072452009-06-05 15:38:42 +0800252
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800253/* We have parameter ranges for different type of outputs. */
254
255/* DAC & HDMI Refclk 120Mhz */
256#define IRONLAKE_DAC_N_MIN 1
257#define IRONLAKE_DAC_N_MAX 5
258#define IRONLAKE_DAC_M_MIN 79
259#define IRONLAKE_DAC_M_MAX 127
260#define IRONLAKE_DAC_P_MIN 5
261#define IRONLAKE_DAC_P_MAX 80
262#define IRONLAKE_DAC_P1_MIN 1
263#define IRONLAKE_DAC_P1_MAX 8
264#define IRONLAKE_DAC_P2_SLOW 10
265#define IRONLAKE_DAC_P2_FAST 5
266
267/* LVDS single-channel 120Mhz refclk */
268#define IRONLAKE_LVDS_S_N_MIN 1
269#define IRONLAKE_LVDS_S_N_MAX 3
270#define IRONLAKE_LVDS_S_M_MIN 79
271#define IRONLAKE_LVDS_S_M_MAX 118
272#define IRONLAKE_LVDS_S_P_MIN 28
273#define IRONLAKE_LVDS_S_P_MAX 112
274#define IRONLAKE_LVDS_S_P1_MIN 2
275#define IRONLAKE_LVDS_S_P1_MAX 8
276#define IRONLAKE_LVDS_S_P2_SLOW 14
277#define IRONLAKE_LVDS_S_P2_FAST 14
278
279/* LVDS dual-channel 120Mhz refclk */
280#define IRONLAKE_LVDS_D_N_MIN 1
281#define IRONLAKE_LVDS_D_N_MAX 3
282#define IRONLAKE_LVDS_D_M_MIN 79
283#define IRONLAKE_LVDS_D_M_MAX 127
284#define IRONLAKE_LVDS_D_P_MIN 14
285#define IRONLAKE_LVDS_D_P_MAX 56
286#define IRONLAKE_LVDS_D_P1_MIN 2
287#define IRONLAKE_LVDS_D_P1_MAX 8
288#define IRONLAKE_LVDS_D_P2_SLOW 7
289#define IRONLAKE_LVDS_D_P2_FAST 7
290
291/* LVDS single-channel 100Mhz refclk */
292#define IRONLAKE_LVDS_S_SSC_N_MIN 1
293#define IRONLAKE_LVDS_S_SSC_N_MAX 2
294#define IRONLAKE_LVDS_S_SSC_M_MIN 79
295#define IRONLAKE_LVDS_S_SSC_M_MAX 126
296#define IRONLAKE_LVDS_S_SSC_P_MIN 28
297#define IRONLAKE_LVDS_S_SSC_P_MAX 112
298#define IRONLAKE_LVDS_S_SSC_P1_MIN 2
299#define IRONLAKE_LVDS_S_SSC_P1_MAX 8
300#define IRONLAKE_LVDS_S_SSC_P2_SLOW 14
301#define IRONLAKE_LVDS_S_SSC_P2_FAST 14
302
303/* LVDS dual-channel 100Mhz refclk */
304#define IRONLAKE_LVDS_D_SSC_N_MIN 1
305#define IRONLAKE_LVDS_D_SSC_N_MAX 3
306#define IRONLAKE_LVDS_D_SSC_M_MIN 79
307#define IRONLAKE_LVDS_D_SSC_M_MAX 126
308#define IRONLAKE_LVDS_D_SSC_P_MIN 14
309#define IRONLAKE_LVDS_D_SSC_P_MAX 42
310#define IRONLAKE_LVDS_D_SSC_P1_MIN 2
311#define IRONLAKE_LVDS_D_SSC_P1_MAX 6
312#define IRONLAKE_LVDS_D_SSC_P2_SLOW 7
313#define IRONLAKE_LVDS_D_SSC_P2_FAST 7
314
315/* DisplayPort */
316#define IRONLAKE_DP_N_MIN 1
317#define IRONLAKE_DP_N_MAX 2
318#define IRONLAKE_DP_M_MIN 81
319#define IRONLAKE_DP_M_MAX 90
320#define IRONLAKE_DP_P_MIN 10
321#define IRONLAKE_DP_P_MAX 20
322#define IRONLAKE_DP_P2_FAST 10
323#define IRONLAKE_DP_P2_SLOW 10
324#define IRONLAKE_DP_P2_LIMIT 0
325#define IRONLAKE_DP_P1_MIN 1
326#define IRONLAKE_DP_P1_MAX 2
Zhao Yakui45476682009-12-31 16:06:04 +0800327
Jesse Barnes2377b742010-07-07 14:06:43 -0700328/* FDI */
329#define IRONLAKE_FDI_FREQ 2700000 /* in kHz for mode->clock */
330
Ma Lingd4906092009-03-18 20:13:27 +0800331static bool
332intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
333 int target, int refclk, intel_clock_t *best_clock);
334static bool
335intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
336 int target, int refclk, intel_clock_t *best_clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800337
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700338static bool
339intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
340 int target, int refclk, intel_clock_t *best_clock);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800341static bool
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500342intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,
343 int target, int refclk, intel_clock_t *best_clock);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700344
Chris Wilson021357a2010-09-07 20:54:59 +0100345static inline u32 /* units of 100MHz */
346intel_fdi_link_freq(struct drm_device *dev)
347{
Chris Wilson8b99e682010-10-13 09:59:17 +0100348 if (IS_GEN5(dev)) {
349 struct drm_i915_private *dev_priv = dev->dev_private;
350 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
351 } else
352 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100353}
354
Keith Packarde4b36692009-06-05 19:22:17 -0700355static const intel_limit_t intel_limits_i8xx_dvo = {
Jesse Barnes79e53942008-11-07 14:24:08 -0800356 .dot = { .min = I8XX_DOT_MIN, .max = I8XX_DOT_MAX },
357 .vco = { .min = I8XX_VCO_MIN, .max = I8XX_VCO_MAX },
358 .n = { .min = I8XX_N_MIN, .max = I8XX_N_MAX },
359 .m = { .min = I8XX_M_MIN, .max = I8XX_M_MAX },
360 .m1 = { .min = I8XX_M1_MIN, .max = I8XX_M1_MAX },
361 .m2 = { .min = I8XX_M2_MIN, .max = I8XX_M2_MAX },
362 .p = { .min = I8XX_P_MIN, .max = I8XX_P_MAX },
363 .p1 = { .min = I8XX_P1_MIN, .max = I8XX_P1_MAX },
364 .p2 = { .dot_limit = I8XX_P2_SLOW_LIMIT,
365 .p2_slow = I8XX_P2_SLOW, .p2_fast = I8XX_P2_FAST },
Ma Lingd4906092009-03-18 20:13:27 +0800366 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700367};
368
369static const intel_limit_t intel_limits_i8xx_lvds = {
Jesse Barnes79e53942008-11-07 14:24:08 -0800370 .dot = { .min = I8XX_DOT_MIN, .max = I8XX_DOT_MAX },
371 .vco = { .min = I8XX_VCO_MIN, .max = I8XX_VCO_MAX },
372 .n = { .min = I8XX_N_MIN, .max = I8XX_N_MAX },
373 .m = { .min = I8XX_M_MIN, .max = I8XX_M_MAX },
374 .m1 = { .min = I8XX_M1_MIN, .max = I8XX_M1_MAX },
375 .m2 = { .min = I8XX_M2_MIN, .max = I8XX_M2_MAX },
376 .p = { .min = I8XX_P_MIN, .max = I8XX_P_MAX },
377 .p1 = { .min = I8XX_P1_LVDS_MIN, .max = I8XX_P1_LVDS_MAX },
378 .p2 = { .dot_limit = I8XX_P2_SLOW_LIMIT,
379 .p2_slow = I8XX_P2_LVDS_SLOW, .p2_fast = I8XX_P2_LVDS_FAST },
Ma Lingd4906092009-03-18 20:13:27 +0800380 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700381};
382
383static const intel_limit_t intel_limits_i9xx_sdvo = {
Jesse Barnes79e53942008-11-07 14:24:08 -0800384 .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
385 .vco = { .min = I9XX_VCO_MIN, .max = I9XX_VCO_MAX },
386 .n = { .min = I9XX_N_MIN, .max = I9XX_N_MAX },
387 .m = { .min = I9XX_M_MIN, .max = I9XX_M_MAX },
388 .m1 = { .min = I9XX_M1_MIN, .max = I9XX_M1_MAX },
389 .m2 = { .min = I9XX_M2_MIN, .max = I9XX_M2_MAX },
390 .p = { .min = I9XX_P_SDVO_DAC_MIN, .max = I9XX_P_SDVO_DAC_MAX },
391 .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
392 .p2 = { .dot_limit = I9XX_P2_SDVO_DAC_SLOW_LIMIT,
393 .p2_slow = I9XX_P2_SDVO_DAC_SLOW, .p2_fast = I9XX_P2_SDVO_DAC_FAST },
Ma Lingd4906092009-03-18 20:13:27 +0800394 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700395};
396
397static const intel_limit_t intel_limits_i9xx_lvds = {
Jesse Barnes79e53942008-11-07 14:24:08 -0800398 .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
399 .vco = { .min = I9XX_VCO_MIN, .max = I9XX_VCO_MAX },
400 .n = { .min = I9XX_N_MIN, .max = I9XX_N_MAX },
401 .m = { .min = I9XX_M_MIN, .max = I9XX_M_MAX },
402 .m1 = { .min = I9XX_M1_MIN, .max = I9XX_M1_MAX },
403 .m2 = { .min = I9XX_M2_MIN, .max = I9XX_M2_MAX },
404 .p = { .min = I9XX_P_LVDS_MIN, .max = I9XX_P_LVDS_MAX },
405 .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
406 /* The single-channel range is 25-112Mhz, and dual-channel
407 * is 80-224Mhz. Prefer single channel as much as possible.
408 */
409 .p2 = { .dot_limit = I9XX_P2_LVDS_SLOW_LIMIT,
410 .p2_slow = I9XX_P2_LVDS_SLOW, .p2_fast = I9XX_P2_LVDS_FAST },
Ma Lingd4906092009-03-18 20:13:27 +0800411 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700412};
413
Ma Ling044c7c42009-03-18 20:13:23 +0800414 /* below parameter and function is for G4X Chipset Family*/
Keith Packarde4b36692009-06-05 19:22:17 -0700415static const intel_limit_t intel_limits_g4x_sdvo = {
Ma Ling044c7c42009-03-18 20:13:23 +0800416 .dot = { .min = G4X_DOT_SDVO_MIN, .max = G4X_DOT_SDVO_MAX },
417 .vco = { .min = G4X_VCO_MIN, .max = G4X_VCO_MAX},
418 .n = { .min = G4X_N_SDVO_MIN, .max = G4X_N_SDVO_MAX },
419 .m = { .min = G4X_M_SDVO_MIN, .max = G4X_M_SDVO_MAX },
420 .m1 = { .min = G4X_M1_SDVO_MIN, .max = G4X_M1_SDVO_MAX },
421 .m2 = { .min = G4X_M2_SDVO_MIN, .max = G4X_M2_SDVO_MAX },
422 .p = { .min = G4X_P_SDVO_MIN, .max = G4X_P_SDVO_MAX },
423 .p1 = { .min = G4X_P1_SDVO_MIN, .max = G4X_P1_SDVO_MAX},
424 .p2 = { .dot_limit = G4X_P2_SDVO_LIMIT,
425 .p2_slow = G4X_P2_SDVO_SLOW,
426 .p2_fast = G4X_P2_SDVO_FAST
427 },
Ma Lingd4906092009-03-18 20:13:27 +0800428 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700429};
430
431static const intel_limit_t intel_limits_g4x_hdmi = {
Ma Ling044c7c42009-03-18 20:13:23 +0800432 .dot = { .min = G4X_DOT_HDMI_DAC_MIN, .max = G4X_DOT_HDMI_DAC_MAX },
433 .vco = { .min = G4X_VCO_MIN, .max = G4X_VCO_MAX},
434 .n = { .min = G4X_N_HDMI_DAC_MIN, .max = G4X_N_HDMI_DAC_MAX },
435 .m = { .min = G4X_M_HDMI_DAC_MIN, .max = G4X_M_HDMI_DAC_MAX },
436 .m1 = { .min = G4X_M1_HDMI_DAC_MIN, .max = G4X_M1_HDMI_DAC_MAX },
437 .m2 = { .min = G4X_M2_HDMI_DAC_MIN, .max = G4X_M2_HDMI_DAC_MAX },
438 .p = { .min = G4X_P_HDMI_DAC_MIN, .max = G4X_P_HDMI_DAC_MAX },
439 .p1 = { .min = G4X_P1_HDMI_DAC_MIN, .max = G4X_P1_HDMI_DAC_MAX},
440 .p2 = { .dot_limit = G4X_P2_HDMI_DAC_LIMIT,
441 .p2_slow = G4X_P2_HDMI_DAC_SLOW,
442 .p2_fast = G4X_P2_HDMI_DAC_FAST
443 },
Ma Lingd4906092009-03-18 20:13:27 +0800444 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700445};
446
447static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Ma Ling044c7c42009-03-18 20:13:23 +0800448 .dot = { .min = G4X_DOT_SINGLE_CHANNEL_LVDS_MIN,
449 .max = G4X_DOT_SINGLE_CHANNEL_LVDS_MAX },
450 .vco = { .min = G4X_VCO_MIN,
451 .max = G4X_VCO_MAX },
452 .n = { .min = G4X_N_SINGLE_CHANNEL_LVDS_MIN,
453 .max = G4X_N_SINGLE_CHANNEL_LVDS_MAX },
454 .m = { .min = G4X_M_SINGLE_CHANNEL_LVDS_MIN,
455 .max = G4X_M_SINGLE_CHANNEL_LVDS_MAX },
456 .m1 = { .min = G4X_M1_SINGLE_CHANNEL_LVDS_MIN,
457 .max = G4X_M1_SINGLE_CHANNEL_LVDS_MAX },
458 .m2 = { .min = G4X_M2_SINGLE_CHANNEL_LVDS_MIN,
459 .max = G4X_M2_SINGLE_CHANNEL_LVDS_MAX },
460 .p = { .min = G4X_P_SINGLE_CHANNEL_LVDS_MIN,
461 .max = G4X_P_SINGLE_CHANNEL_LVDS_MAX },
462 .p1 = { .min = G4X_P1_SINGLE_CHANNEL_LVDS_MIN,
463 .max = G4X_P1_SINGLE_CHANNEL_LVDS_MAX },
464 .p2 = { .dot_limit = G4X_P2_SINGLE_CHANNEL_LVDS_LIMIT,
465 .p2_slow = G4X_P2_SINGLE_CHANNEL_LVDS_SLOW,
466 .p2_fast = G4X_P2_SINGLE_CHANNEL_LVDS_FAST
467 },
Ma Lingd4906092009-03-18 20:13:27 +0800468 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700469};
470
471static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Ma Ling044c7c42009-03-18 20:13:23 +0800472 .dot = { .min = G4X_DOT_DUAL_CHANNEL_LVDS_MIN,
473 .max = G4X_DOT_DUAL_CHANNEL_LVDS_MAX },
474 .vco = { .min = G4X_VCO_MIN,
475 .max = G4X_VCO_MAX },
476 .n = { .min = G4X_N_DUAL_CHANNEL_LVDS_MIN,
477 .max = G4X_N_DUAL_CHANNEL_LVDS_MAX },
478 .m = { .min = G4X_M_DUAL_CHANNEL_LVDS_MIN,
479 .max = G4X_M_DUAL_CHANNEL_LVDS_MAX },
480 .m1 = { .min = G4X_M1_DUAL_CHANNEL_LVDS_MIN,
481 .max = G4X_M1_DUAL_CHANNEL_LVDS_MAX },
482 .m2 = { .min = G4X_M2_DUAL_CHANNEL_LVDS_MIN,
483 .max = G4X_M2_DUAL_CHANNEL_LVDS_MAX },
484 .p = { .min = G4X_P_DUAL_CHANNEL_LVDS_MIN,
485 .max = G4X_P_DUAL_CHANNEL_LVDS_MAX },
486 .p1 = { .min = G4X_P1_DUAL_CHANNEL_LVDS_MIN,
487 .max = G4X_P1_DUAL_CHANNEL_LVDS_MAX },
488 .p2 = { .dot_limit = G4X_P2_DUAL_CHANNEL_LVDS_LIMIT,
489 .p2_slow = G4X_P2_DUAL_CHANNEL_LVDS_SLOW,
490 .p2_fast = G4X_P2_DUAL_CHANNEL_LVDS_FAST
491 },
Ma Lingd4906092009-03-18 20:13:27 +0800492 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700493};
494
495static const intel_limit_t intel_limits_g4x_display_port = {
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700496 .dot = { .min = G4X_DOT_DISPLAY_PORT_MIN,
497 .max = G4X_DOT_DISPLAY_PORT_MAX },
498 .vco = { .min = G4X_VCO_MIN,
499 .max = G4X_VCO_MAX},
500 .n = { .min = G4X_N_DISPLAY_PORT_MIN,
501 .max = G4X_N_DISPLAY_PORT_MAX },
502 .m = { .min = G4X_M_DISPLAY_PORT_MIN,
503 .max = G4X_M_DISPLAY_PORT_MAX },
504 .m1 = { .min = G4X_M1_DISPLAY_PORT_MIN,
505 .max = G4X_M1_DISPLAY_PORT_MAX },
506 .m2 = { .min = G4X_M2_DISPLAY_PORT_MIN,
507 .max = G4X_M2_DISPLAY_PORT_MAX },
508 .p = { .min = G4X_P_DISPLAY_PORT_MIN,
509 .max = G4X_P_DISPLAY_PORT_MAX },
510 .p1 = { .min = G4X_P1_DISPLAY_PORT_MIN,
511 .max = G4X_P1_DISPLAY_PORT_MAX},
512 .p2 = { .dot_limit = G4X_P2_DISPLAY_PORT_LIMIT,
513 .p2_slow = G4X_P2_DISPLAY_PORT_SLOW,
514 .p2_fast = G4X_P2_DISPLAY_PORT_FAST },
515 .find_pll = intel_find_pll_g4x_dp,
Keith Packarde4b36692009-06-05 19:22:17 -0700516};
517
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500518static const intel_limit_t intel_limits_pineview_sdvo = {
Shaohua Li21778322009-02-23 15:19:16 +0800519 .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX},
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500520 .vco = { .min = PINEVIEW_VCO_MIN, .max = PINEVIEW_VCO_MAX },
521 .n = { .min = PINEVIEW_N_MIN, .max = PINEVIEW_N_MAX },
522 .m = { .min = PINEVIEW_M_MIN, .max = PINEVIEW_M_MAX },
523 .m1 = { .min = PINEVIEW_M1_MIN, .max = PINEVIEW_M1_MAX },
524 .m2 = { .min = PINEVIEW_M2_MIN, .max = PINEVIEW_M2_MAX },
Shaohua Li21778322009-02-23 15:19:16 +0800525 .p = { .min = I9XX_P_SDVO_DAC_MIN, .max = I9XX_P_SDVO_DAC_MAX },
526 .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
527 .p2 = { .dot_limit = I9XX_P2_SDVO_DAC_SLOW_LIMIT,
528 .p2_slow = I9XX_P2_SDVO_DAC_SLOW, .p2_fast = I9XX_P2_SDVO_DAC_FAST },
Shaohua Li61157072009-04-03 15:24:43 +0800529 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700530};
531
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500532static const intel_limit_t intel_limits_pineview_lvds = {
Shaohua Li21778322009-02-23 15:19:16 +0800533 .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500534 .vco = { .min = PINEVIEW_VCO_MIN, .max = PINEVIEW_VCO_MAX },
535 .n = { .min = PINEVIEW_N_MIN, .max = PINEVIEW_N_MAX },
536 .m = { .min = PINEVIEW_M_MIN, .max = PINEVIEW_M_MAX },
537 .m1 = { .min = PINEVIEW_M1_MIN, .max = PINEVIEW_M1_MAX },
538 .m2 = { .min = PINEVIEW_M2_MIN, .max = PINEVIEW_M2_MAX },
539 .p = { .min = PINEVIEW_P_LVDS_MIN, .max = PINEVIEW_P_LVDS_MAX },
Shaohua Li21778322009-02-23 15:19:16 +0800540 .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500541 /* Pineview only supports single-channel mode. */
Shaohua Li21778322009-02-23 15:19:16 +0800542 .p2 = { .dot_limit = I9XX_P2_LVDS_SLOW_LIMIT,
543 .p2_slow = I9XX_P2_LVDS_SLOW, .p2_fast = I9XX_P2_LVDS_SLOW },
Shaohua Li61157072009-04-03 15:24:43 +0800544 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700545};
546
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800547static const intel_limit_t intel_limits_ironlake_dac = {
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500548 .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
549 .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800550 .n = { .min = IRONLAKE_DAC_N_MIN, .max = IRONLAKE_DAC_N_MAX },
551 .m = { .min = IRONLAKE_DAC_M_MIN, .max = IRONLAKE_DAC_M_MAX },
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500552 .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
553 .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800554 .p = { .min = IRONLAKE_DAC_P_MIN, .max = IRONLAKE_DAC_P_MAX },
555 .p1 = { .min = IRONLAKE_DAC_P1_MIN, .max = IRONLAKE_DAC_P1_MAX },
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500556 .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800557 .p2_slow = IRONLAKE_DAC_P2_SLOW,
558 .p2_fast = IRONLAKE_DAC_P2_FAST },
Zhao Yakui45476682009-12-31 16:06:04 +0800559 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700560};
561
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800562static const intel_limit_t intel_limits_ironlake_single_lvds = {
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500563 .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
564 .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800565 .n = { .min = IRONLAKE_LVDS_S_N_MIN, .max = IRONLAKE_LVDS_S_N_MAX },
566 .m = { .min = IRONLAKE_LVDS_S_M_MIN, .max = IRONLAKE_LVDS_S_M_MAX },
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500567 .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
568 .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800569 .p = { .min = IRONLAKE_LVDS_S_P_MIN, .max = IRONLAKE_LVDS_S_P_MAX },
570 .p1 = { .min = IRONLAKE_LVDS_S_P1_MIN, .max = IRONLAKE_LVDS_S_P1_MAX },
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500571 .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800572 .p2_slow = IRONLAKE_LVDS_S_P2_SLOW,
573 .p2_fast = IRONLAKE_LVDS_S_P2_FAST },
574 .find_pll = intel_g4x_find_best_PLL,
575};
576
577static const intel_limit_t intel_limits_ironlake_dual_lvds = {
578 .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
579 .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
580 .n = { .min = IRONLAKE_LVDS_D_N_MIN, .max = IRONLAKE_LVDS_D_N_MAX },
581 .m = { .min = IRONLAKE_LVDS_D_M_MIN, .max = IRONLAKE_LVDS_D_M_MAX },
582 .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
583 .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
584 .p = { .min = IRONLAKE_LVDS_D_P_MIN, .max = IRONLAKE_LVDS_D_P_MAX },
585 .p1 = { .min = IRONLAKE_LVDS_D_P1_MIN, .max = IRONLAKE_LVDS_D_P1_MAX },
586 .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
587 .p2_slow = IRONLAKE_LVDS_D_P2_SLOW,
588 .p2_fast = IRONLAKE_LVDS_D_P2_FAST },
589 .find_pll = intel_g4x_find_best_PLL,
590};
591
592static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
593 .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
594 .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
595 .n = { .min = IRONLAKE_LVDS_S_SSC_N_MIN, .max = IRONLAKE_LVDS_S_SSC_N_MAX },
596 .m = { .min = IRONLAKE_LVDS_S_SSC_M_MIN, .max = IRONLAKE_LVDS_S_SSC_M_MAX },
597 .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
598 .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
599 .p = { .min = IRONLAKE_LVDS_S_SSC_P_MIN, .max = IRONLAKE_LVDS_S_SSC_P_MAX },
600 .p1 = { .min = IRONLAKE_LVDS_S_SSC_P1_MIN,.max = IRONLAKE_LVDS_S_SSC_P1_MAX },
601 .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
602 .p2_slow = IRONLAKE_LVDS_S_SSC_P2_SLOW,
603 .p2_fast = IRONLAKE_LVDS_S_SSC_P2_FAST },
604 .find_pll = intel_g4x_find_best_PLL,
605};
606
607static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
608 .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
609 .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
610 .n = { .min = IRONLAKE_LVDS_D_SSC_N_MIN, .max = IRONLAKE_LVDS_D_SSC_N_MAX },
611 .m = { .min = IRONLAKE_LVDS_D_SSC_M_MIN, .max = IRONLAKE_LVDS_D_SSC_M_MAX },
612 .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
613 .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
614 .p = { .min = IRONLAKE_LVDS_D_SSC_P_MIN, .max = IRONLAKE_LVDS_D_SSC_P_MAX },
615 .p1 = { .min = IRONLAKE_LVDS_D_SSC_P1_MIN,.max = IRONLAKE_LVDS_D_SSC_P1_MAX },
616 .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
617 .p2_slow = IRONLAKE_LVDS_D_SSC_P2_SLOW,
618 .p2_fast = IRONLAKE_LVDS_D_SSC_P2_FAST },
Zhao Yakui45476682009-12-31 16:06:04 +0800619 .find_pll = intel_g4x_find_best_PLL,
620};
621
622static const intel_limit_t intel_limits_ironlake_display_port = {
623 .dot = { .min = IRONLAKE_DOT_MIN,
624 .max = IRONLAKE_DOT_MAX },
625 .vco = { .min = IRONLAKE_VCO_MIN,
626 .max = IRONLAKE_VCO_MAX},
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800627 .n = { .min = IRONLAKE_DP_N_MIN,
628 .max = IRONLAKE_DP_N_MAX },
629 .m = { .min = IRONLAKE_DP_M_MIN,
630 .max = IRONLAKE_DP_M_MAX },
Zhao Yakui45476682009-12-31 16:06:04 +0800631 .m1 = { .min = IRONLAKE_M1_MIN,
632 .max = IRONLAKE_M1_MAX },
633 .m2 = { .min = IRONLAKE_M2_MIN,
634 .max = IRONLAKE_M2_MAX },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800635 .p = { .min = IRONLAKE_DP_P_MIN,
636 .max = IRONLAKE_DP_P_MAX },
637 .p1 = { .min = IRONLAKE_DP_P1_MIN,
638 .max = IRONLAKE_DP_P1_MAX},
639 .p2 = { .dot_limit = IRONLAKE_DP_P2_LIMIT,
640 .p2_slow = IRONLAKE_DP_P2_SLOW,
641 .p2_fast = IRONLAKE_DP_P2_FAST },
Zhao Yakui45476682009-12-31 16:06:04 +0800642 .find_pll = intel_find_pll_ironlake_dp,
Jesse Barnes79e53942008-11-07 14:24:08 -0800643};
644
Chris Wilson1b894b52010-12-14 20:04:54 +0000645static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
646 int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800647{
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800648 struct drm_device *dev = crtc->dev;
649 struct drm_i915_private *dev_priv = dev->dev_private;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800650 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800651
652 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800653 if ((I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) ==
654 LVDS_CLKB_POWER_UP) {
655 /* LVDS dual channel */
Chris Wilson1b894b52010-12-14 20:04:54 +0000656 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800657 limit = &intel_limits_ironlake_dual_lvds_100m;
658 else
659 limit = &intel_limits_ironlake_dual_lvds;
660 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000661 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800662 limit = &intel_limits_ironlake_single_lvds_100m;
663 else
664 limit = &intel_limits_ironlake_single_lvds;
665 }
666 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
Zhao Yakui45476682009-12-31 16:06:04 +0800667 HAS_eDP)
668 limit = &intel_limits_ironlake_display_port;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800669 else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800670 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800671
672 return limit;
673}
674
Ma Ling044c7c42009-03-18 20:13:23 +0800675static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
676{
677 struct drm_device *dev = crtc->dev;
678 struct drm_i915_private *dev_priv = dev->dev_private;
679 const intel_limit_t *limit;
680
681 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
682 if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
683 LVDS_CLKB_POWER_UP)
684 /* LVDS with dual channel */
Keith Packarde4b36692009-06-05 19:22:17 -0700685 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800686 else
687 /* LVDS with dual channel */
Keith Packarde4b36692009-06-05 19:22:17 -0700688 limit = &intel_limits_g4x_single_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800689 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
690 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700691 limit = &intel_limits_g4x_hdmi;
Ma Ling044c7c42009-03-18 20:13:23 +0800692 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700693 limit = &intel_limits_g4x_sdvo;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700694 } else if (intel_pipe_has_type (crtc, INTEL_OUTPUT_DISPLAYPORT)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700695 limit = &intel_limits_g4x_display_port;
Ma Ling044c7c42009-03-18 20:13:23 +0800696 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700697 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800698
699 return limit;
700}
701
Chris Wilson1b894b52010-12-14 20:04:54 +0000702static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800703{
704 struct drm_device *dev = crtc->dev;
705 const intel_limit_t *limit;
706
Eric Anholtbad720f2009-10-22 16:11:14 -0700707 if (HAS_PCH_SPLIT(dev))
Chris Wilson1b894b52010-12-14 20:04:54 +0000708 limit = intel_ironlake_limit(crtc, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800709 else if (IS_G4X(dev)) {
Ma Ling044c7c42009-03-18 20:13:23 +0800710 limit = intel_g4x_limit(crtc);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500711 } else if (IS_PINEVIEW(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +0800712 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500713 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800714 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500715 limit = &intel_limits_pineview_sdvo;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100716 } else if (!IS_GEN2(dev)) {
717 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
718 limit = &intel_limits_i9xx_lvds;
719 else
720 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800721 } else {
722 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700723 limit = &intel_limits_i8xx_lvds;
Jesse Barnes79e53942008-11-07 14:24:08 -0800724 else
Keith Packarde4b36692009-06-05 19:22:17 -0700725 limit = &intel_limits_i8xx_dvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800726 }
727 return limit;
728}
729
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500730/* m1 is reserved as 0 in Pineview, n is a ring counter */
731static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800732{
Shaohua Li21778322009-02-23 15:19:16 +0800733 clock->m = clock->m2 + 2;
734 clock->p = clock->p1 * clock->p2;
735 clock->vco = refclk * clock->m / clock->n;
736 clock->dot = clock->vco / clock->p;
737}
738
739static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
740{
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500741 if (IS_PINEVIEW(dev)) {
742 pineview_clock(refclk, clock);
Shaohua Li21778322009-02-23 15:19:16 +0800743 return;
744 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800745 clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
746 clock->p = clock->p1 * clock->p2;
747 clock->vco = refclk * clock->m / (clock->n + 2);
748 clock->dot = clock->vco / clock->p;
749}
750
Jesse Barnes79e53942008-11-07 14:24:08 -0800751/**
752 * Returns whether any output on the specified pipe is of the specified type
753 */
Chris Wilson4ef69c72010-09-09 15:14:28 +0100754bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
Jesse Barnes79e53942008-11-07 14:24:08 -0800755{
Chris Wilson4ef69c72010-09-09 15:14:28 +0100756 struct drm_device *dev = crtc->dev;
757 struct drm_mode_config *mode_config = &dev->mode_config;
758 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -0800759
Chris Wilson4ef69c72010-09-09 15:14:28 +0100760 list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
761 if (encoder->base.crtc == crtc && encoder->type == type)
762 return true;
763
764 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -0800765}
766
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800767#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800768/**
769 * Returns whether the given set of divisors are valid for a given refclk with
770 * the given connectors.
771 */
772
Chris Wilson1b894b52010-12-14 20:04:54 +0000773static bool intel_PLL_is_valid(struct drm_device *dev,
774 const intel_limit_t *limit,
775 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800776{
Jesse Barnes79e53942008-11-07 14:24:08 -0800777 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
778 INTELPllInvalid ("p1 out of range\n");
779 if (clock->p < limit->p.min || limit->p.max < clock->p)
780 INTELPllInvalid ("p out of range\n");
781 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
782 INTELPllInvalid ("m2 out of range\n");
783 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
784 INTELPllInvalid ("m1 out of range\n");
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500785 if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800786 INTELPllInvalid ("m1 <= m2\n");
787 if (clock->m < limit->m.min || limit->m.max < clock->m)
788 INTELPllInvalid ("m out of range\n");
789 if (clock->n < limit->n.min || limit->n.max < clock->n)
790 INTELPllInvalid ("n out of range\n");
791 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
792 INTELPllInvalid ("vco out of range\n");
793 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
794 * connector, etc., rather than just a single range.
795 */
796 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
797 INTELPllInvalid ("dot out of range\n");
798
799 return true;
800}
801
Ma Lingd4906092009-03-18 20:13:27 +0800802static bool
803intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
804 int target, int refclk, intel_clock_t *best_clock)
805
Jesse Barnes79e53942008-11-07 14:24:08 -0800806{
807 struct drm_device *dev = crtc->dev;
808 struct drm_i915_private *dev_priv = dev->dev_private;
809 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800810 int err = target;
811
Bruno Prémontbc5e5712009-08-08 13:01:17 +0200812 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Florian Mickler832cc282009-07-13 18:40:32 +0800813 (I915_READ(LVDS)) != 0) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800814 /*
815 * For LVDS, if the panel is on, just rely on its current
816 * settings for dual-channel. We haven't figured out how to
817 * reliably set up different single/dual channel state, if we
818 * even can.
819 */
820 if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
821 LVDS_CLKB_POWER_UP)
822 clock.p2 = limit->p2.p2_fast;
823 else
824 clock.p2 = limit->p2.p2_slow;
825 } else {
826 if (target < limit->p2.dot_limit)
827 clock.p2 = limit->p2.p2_slow;
828 else
829 clock.p2 = limit->p2.p2_fast;
830 }
831
832 memset (best_clock, 0, sizeof (*best_clock));
833
Zhao Yakui42158662009-11-20 11:24:18 +0800834 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
835 clock.m1++) {
836 for (clock.m2 = limit->m2.min;
837 clock.m2 <= limit->m2.max; clock.m2++) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500838 /* m1 is always 0 in Pineview */
839 if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
Zhao Yakui42158662009-11-20 11:24:18 +0800840 break;
841 for (clock.n = limit->n.min;
842 clock.n <= limit->n.max; clock.n++) {
843 for (clock.p1 = limit->p1.min;
844 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800845 int this_err;
846
Shaohua Li21778322009-02-23 15:19:16 +0800847 intel_clock(dev, refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000848 if (!intel_PLL_is_valid(dev, limit,
849 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800850 continue;
851
852 this_err = abs(clock.dot - target);
853 if (this_err < err) {
854 *best_clock = clock;
855 err = this_err;
856 }
857 }
858 }
859 }
860 }
861
862 return (err != target);
863}
864
Ma Lingd4906092009-03-18 20:13:27 +0800865static bool
866intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
867 int target, int refclk, intel_clock_t *best_clock)
868{
869 struct drm_device *dev = crtc->dev;
870 struct drm_i915_private *dev_priv = dev->dev_private;
871 intel_clock_t clock;
872 int max_n;
873 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400874 /* approximately equals target * 0.00585 */
875 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800876 found = false;
877
878 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Zhao Yakui45476682009-12-31 16:06:04 +0800879 int lvds_reg;
880
Eric Anholtc619eed2010-01-28 16:45:52 -0800881 if (HAS_PCH_SPLIT(dev))
Zhao Yakui45476682009-12-31 16:06:04 +0800882 lvds_reg = PCH_LVDS;
883 else
884 lvds_reg = LVDS;
885 if ((I915_READ(lvds_reg) & LVDS_CLKB_POWER_MASK) ==
Ma Lingd4906092009-03-18 20:13:27 +0800886 LVDS_CLKB_POWER_UP)
887 clock.p2 = limit->p2.p2_fast;
888 else
889 clock.p2 = limit->p2.p2_slow;
890 } else {
891 if (target < limit->p2.dot_limit)
892 clock.p2 = limit->p2.p2_slow;
893 else
894 clock.p2 = limit->p2.p2_fast;
895 }
896
897 memset(best_clock, 0, sizeof(*best_clock));
898 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200899 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800900 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200901 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800902 for (clock.m1 = limit->m1.max;
903 clock.m1 >= limit->m1.min; clock.m1--) {
904 for (clock.m2 = limit->m2.max;
905 clock.m2 >= limit->m2.min; clock.m2--) {
906 for (clock.p1 = limit->p1.max;
907 clock.p1 >= limit->p1.min; clock.p1--) {
908 int this_err;
909
Shaohua Li21778322009-02-23 15:19:16 +0800910 intel_clock(dev, refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000911 if (!intel_PLL_is_valid(dev, limit,
912 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800913 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000914
915 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800916 if (this_err < err_most) {
917 *best_clock = clock;
918 err_most = this_err;
919 max_n = clock.n;
920 found = true;
921 }
922 }
923 }
924 }
925 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800926 return found;
927}
Ma Lingd4906092009-03-18 20:13:27 +0800928
Zhenyu Wang2c072452009-06-05 15:38:42 +0800929static bool
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500930intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
931 int target, int refclk, intel_clock_t *best_clock)
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800932{
933 struct drm_device *dev = crtc->dev;
934 intel_clock_t clock;
Zhao Yakui45476682009-12-31 16:06:04 +0800935
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800936 if (target < 200000) {
937 clock.n = 1;
938 clock.p1 = 2;
939 clock.p2 = 10;
940 clock.m1 = 12;
941 clock.m2 = 9;
942 } else {
943 clock.n = 2;
944 clock.p1 = 1;
945 clock.p2 = 10;
946 clock.m1 = 14;
947 clock.m2 = 8;
948 }
949 intel_clock(dev, refclk, &clock);
950 memcpy(best_clock, &clock, sizeof(intel_clock_t));
951 return true;
952}
953
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700954/* DisplayPort has only two frequencies, 162MHz and 270MHz */
955static bool
956intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
957 int target, int refclk, intel_clock_t *best_clock)
958{
Chris Wilson5eddb702010-09-11 13:48:45 +0100959 intel_clock_t clock;
960 if (target < 200000) {
961 clock.p1 = 2;
962 clock.p2 = 10;
963 clock.n = 2;
964 clock.m1 = 23;
965 clock.m2 = 8;
966 } else {
967 clock.p1 = 1;
968 clock.p2 = 10;
969 clock.n = 1;
970 clock.m1 = 14;
971 clock.m2 = 2;
972 }
973 clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
974 clock.p = (clock.p1 * clock.p2);
975 clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
976 clock.vco = 0;
977 memcpy(best_clock, &clock, sizeof(intel_clock_t));
978 return true;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700979}
980
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700981/**
982 * intel_wait_for_vblank - wait for vblank on a given pipe
983 * @dev: drm device
984 * @pipe: pipe to wait for
985 *
986 * Wait for vblank to occur on a given pipe. Needed for various bits of
987 * mode setting code.
988 */
989void intel_wait_for_vblank(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -0800990{
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700991 struct drm_i915_private *dev_priv = dev->dev_private;
992 int pipestat_reg = (pipe == 0 ? PIPEASTAT : PIPEBSTAT);
993
Chris Wilson300387c2010-09-05 20:25:43 +0100994 /* Clear existing vblank status. Note this will clear any other
995 * sticky status fields as well.
996 *
997 * This races with i915_driver_irq_handler() with the result
998 * that either function could miss a vblank event. Here it is not
999 * fatal, as we will either wait upon the next vblank interrupt or
1000 * timeout. Generally speaking intel_wait_for_vblank() is only
1001 * called during modeset at which time the GPU should be idle and
1002 * should *not* be performing page flips and thus not waiting on
1003 * vblanks...
1004 * Currently, the result of us stealing a vblank from the irq
1005 * handler is that a single frame will be skipped during swapbuffers.
1006 */
1007 I915_WRITE(pipestat_reg,
1008 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
1009
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001010 /* Wait for vblank interrupt bit to set */
Chris Wilson481b6af2010-08-23 17:43:35 +01001011 if (wait_for(I915_READ(pipestat_reg) &
1012 PIPE_VBLANK_INTERRUPT_STATUS,
1013 50))
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001014 DRM_DEBUG_KMS("vblank wait timed out\n");
1015}
1016
Keith Packardab7ad7f2010-10-03 00:33:06 -07001017/*
1018 * intel_wait_for_pipe_off - wait for pipe to turn off
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001019 * @dev: drm device
1020 * @pipe: pipe to wait for
1021 *
1022 * After disabling a pipe, we can't wait for vblank in the usual way,
1023 * spinning on the vblank interrupt status bit, since we won't actually
1024 * see an interrupt when the pipe is disabled.
1025 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001026 * On Gen4 and above:
1027 * wait for the pipe register state bit to turn off
1028 *
1029 * Otherwise:
1030 * wait for the display line value to settle (it usually
1031 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001032 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001033 */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001034void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001035{
1036 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001037
Keith Packardab7ad7f2010-10-03 00:33:06 -07001038 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson58e10eb2010-10-03 10:56:11 +01001039 int reg = PIPECONF(pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001040
Keith Packardab7ad7f2010-10-03 00:33:06 -07001041 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001042 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1043 100))
Keith Packardab7ad7f2010-10-03 00:33:06 -07001044 DRM_DEBUG_KMS("pipe_off wait timed out\n");
1045 } else {
1046 u32 last_line;
Chris Wilson58e10eb2010-10-03 10:56:11 +01001047 int reg = PIPEDSL(pipe);
Keith Packardab7ad7f2010-10-03 00:33:06 -07001048 unsigned long timeout = jiffies + msecs_to_jiffies(100);
1049
1050 /* Wait for the display line to settle */
1051 do {
Chris Wilson58e10eb2010-10-03 10:56:11 +01001052 last_line = I915_READ(reg) & DSL_LINEMASK;
Keith Packardab7ad7f2010-10-03 00:33:06 -07001053 mdelay(5);
Chris Wilson58e10eb2010-10-03 10:56:11 +01001054 } while (((I915_READ(reg) & DSL_LINEMASK) != last_line) &&
Keith Packardab7ad7f2010-10-03 00:33:06 -07001055 time_after(timeout, jiffies));
1056 if (time_after(jiffies, timeout))
1057 DRM_DEBUG_KMS("pipe_off wait timed out\n");
1058 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001059}
1060
Jesse Barnes80824002009-09-10 15:28:06 -07001061static void i8xx_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
1062{
1063 struct drm_device *dev = crtc->dev;
1064 struct drm_i915_private *dev_priv = dev->dev_private;
1065 struct drm_framebuffer *fb = crtc->fb;
1066 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00001067 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes80824002009-09-10 15:28:06 -07001068 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1069 int plane, i;
1070 u32 fbc_ctl, fbc_ctl2;
1071
Chris Wilsonbed4a672010-09-11 10:47:47 +01001072 if (fb->pitch == dev_priv->cfb_pitch &&
Chris Wilson05394f32010-11-08 19:18:58 +00001073 obj->fence_reg == dev_priv->cfb_fence &&
Chris Wilsonbed4a672010-09-11 10:47:47 +01001074 intel_crtc->plane == dev_priv->cfb_plane &&
1075 I915_READ(FBC_CONTROL) & FBC_CTL_EN)
1076 return;
1077
1078 i8xx_disable_fbc(dev);
1079
Jesse Barnes80824002009-09-10 15:28:06 -07001080 dev_priv->cfb_pitch = dev_priv->cfb_size / FBC_LL_SIZE;
1081
1082 if (fb->pitch < dev_priv->cfb_pitch)
1083 dev_priv->cfb_pitch = fb->pitch;
1084
1085 /* FBC_CTL wants 64B units */
1086 dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
Chris Wilson05394f32010-11-08 19:18:58 +00001087 dev_priv->cfb_fence = obj->fence_reg;
Jesse Barnes80824002009-09-10 15:28:06 -07001088 dev_priv->cfb_plane = intel_crtc->plane;
1089 plane = dev_priv->cfb_plane == 0 ? FBC_CTL_PLANEA : FBC_CTL_PLANEB;
1090
1091 /* Clear old tags */
1092 for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
1093 I915_WRITE(FBC_TAG + (i * 4), 0);
1094
1095 /* Set it up... */
1096 fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | plane;
Chris Wilson05394f32010-11-08 19:18:58 +00001097 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes80824002009-09-10 15:28:06 -07001098 fbc_ctl2 |= FBC_CTL_CPU_FENCE;
1099 I915_WRITE(FBC_CONTROL2, fbc_ctl2);
1100 I915_WRITE(FBC_FENCE_OFF, crtc->y);
1101
1102 /* enable it... */
1103 fbc_ctl = FBC_CTL_EN | FBC_CTL_PERIODIC;
Jesse Barnesee25df22010-02-06 10:41:53 -08001104 if (IS_I945GM(dev))
Priit Laes49677902010-03-02 11:37:00 +02001105 fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
Jesse Barnes80824002009-09-10 15:28:06 -07001106 fbc_ctl |= (dev_priv->cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
1107 fbc_ctl |= (interval & 0x2fff) << FBC_CTL_INTERVAL_SHIFT;
Chris Wilson05394f32010-11-08 19:18:58 +00001108 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes80824002009-09-10 15:28:06 -07001109 fbc_ctl |= dev_priv->cfb_fence;
1110 I915_WRITE(FBC_CONTROL, fbc_ctl);
1111
Zhao Yakui28c97732009-10-09 11:39:41 +08001112 DRM_DEBUG_KMS("enabled FBC, pitch %ld, yoff %d, plane %d, ",
Chris Wilson5eddb702010-09-11 13:48:45 +01001113 dev_priv->cfb_pitch, crtc->y, dev_priv->cfb_plane);
Jesse Barnes80824002009-09-10 15:28:06 -07001114}
1115
1116void i8xx_disable_fbc(struct drm_device *dev)
1117{
1118 struct drm_i915_private *dev_priv = dev->dev_private;
1119 u32 fbc_ctl;
1120
1121 /* Disable compression */
1122 fbc_ctl = I915_READ(FBC_CONTROL);
Chris Wilsona5cad622010-09-22 13:15:10 +01001123 if ((fbc_ctl & FBC_CTL_EN) == 0)
1124 return;
1125
Jesse Barnes80824002009-09-10 15:28:06 -07001126 fbc_ctl &= ~FBC_CTL_EN;
1127 I915_WRITE(FBC_CONTROL, fbc_ctl);
1128
1129 /* Wait for compressing bit to clear */
Chris Wilson481b6af2010-08-23 17:43:35 +01001130 if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
Chris Wilson913d8d12010-08-07 11:01:35 +01001131 DRM_DEBUG_KMS("FBC idle timed out\n");
1132 return;
Jesse Barnes9517a922010-05-21 09:40:45 -07001133 }
Jesse Barnes80824002009-09-10 15:28:06 -07001134
Zhao Yakui28c97732009-10-09 11:39:41 +08001135 DRM_DEBUG_KMS("disabled FBC\n");
Jesse Barnes80824002009-09-10 15:28:06 -07001136}
1137
Adam Jacksonee5382a2010-04-23 11:17:39 -04001138static bool i8xx_fbc_enabled(struct drm_device *dev)
Jesse Barnes80824002009-09-10 15:28:06 -07001139{
Jesse Barnes80824002009-09-10 15:28:06 -07001140 struct drm_i915_private *dev_priv = dev->dev_private;
1141
1142 return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
1143}
1144
Jesse Barnes74dff282009-09-14 15:39:40 -07001145static void g4x_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
1146{
1147 struct drm_device *dev = crtc->dev;
1148 struct drm_i915_private *dev_priv = dev->dev_private;
1149 struct drm_framebuffer *fb = crtc->fb;
1150 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00001151 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes74dff282009-09-14 15:39:40 -07001152 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5eddb702010-09-11 13:48:45 +01001153 int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
Jesse Barnes74dff282009-09-14 15:39:40 -07001154 unsigned long stall_watermark = 200;
1155 u32 dpfc_ctl;
1156
Chris Wilsonbed4a672010-09-11 10:47:47 +01001157 dpfc_ctl = I915_READ(DPFC_CONTROL);
1158 if (dpfc_ctl & DPFC_CTL_EN) {
1159 if (dev_priv->cfb_pitch == dev_priv->cfb_pitch / 64 - 1 &&
Chris Wilson05394f32010-11-08 19:18:58 +00001160 dev_priv->cfb_fence == obj->fence_reg &&
Chris Wilsonbed4a672010-09-11 10:47:47 +01001161 dev_priv->cfb_plane == intel_crtc->plane &&
1162 dev_priv->cfb_y == crtc->y)
1163 return;
1164
1165 I915_WRITE(DPFC_CONTROL, dpfc_ctl & ~DPFC_CTL_EN);
1166 POSTING_READ(DPFC_CONTROL);
1167 intel_wait_for_vblank(dev, intel_crtc->pipe);
1168 }
1169
Jesse Barnes74dff282009-09-14 15:39:40 -07001170 dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
Chris Wilson05394f32010-11-08 19:18:58 +00001171 dev_priv->cfb_fence = obj->fence_reg;
Jesse Barnes74dff282009-09-14 15:39:40 -07001172 dev_priv->cfb_plane = intel_crtc->plane;
Chris Wilsonbed4a672010-09-11 10:47:47 +01001173 dev_priv->cfb_y = crtc->y;
Jesse Barnes74dff282009-09-14 15:39:40 -07001174
1175 dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;
Chris Wilson05394f32010-11-08 19:18:58 +00001176 if (obj->tiling_mode != I915_TILING_NONE) {
Jesse Barnes74dff282009-09-14 15:39:40 -07001177 dpfc_ctl |= DPFC_CTL_FENCE_EN | dev_priv->cfb_fence;
1178 I915_WRITE(DPFC_CHICKEN, DPFC_HT_MODIFY);
1179 } else {
1180 I915_WRITE(DPFC_CHICKEN, ~DPFC_HT_MODIFY);
1181 }
1182
Jesse Barnes74dff282009-09-14 15:39:40 -07001183 I915_WRITE(DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
1184 (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
1185 (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
1186 I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
1187
1188 /* enable it... */
1189 I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);
1190
Zhao Yakui28c97732009-10-09 11:39:41 +08001191 DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
Jesse Barnes74dff282009-09-14 15:39:40 -07001192}
1193
1194void g4x_disable_fbc(struct drm_device *dev)
1195{
1196 struct drm_i915_private *dev_priv = dev->dev_private;
1197 u32 dpfc_ctl;
1198
1199 /* Disable compression */
1200 dpfc_ctl = I915_READ(DPFC_CONTROL);
Chris Wilsonbed4a672010-09-11 10:47:47 +01001201 if (dpfc_ctl & DPFC_CTL_EN) {
1202 dpfc_ctl &= ~DPFC_CTL_EN;
1203 I915_WRITE(DPFC_CONTROL, dpfc_ctl);
Jesse Barnes74dff282009-09-14 15:39:40 -07001204
Chris Wilsonbed4a672010-09-11 10:47:47 +01001205 DRM_DEBUG_KMS("disabled FBC\n");
1206 }
Jesse Barnes74dff282009-09-14 15:39:40 -07001207}
1208
Adam Jacksonee5382a2010-04-23 11:17:39 -04001209static bool g4x_fbc_enabled(struct drm_device *dev)
Jesse Barnes74dff282009-09-14 15:39:40 -07001210{
Jesse Barnes74dff282009-09-14 15:39:40 -07001211 struct drm_i915_private *dev_priv = dev->dev_private;
1212
1213 return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
1214}
1215
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001216static void ironlake_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
1217{
1218 struct drm_device *dev = crtc->dev;
1219 struct drm_i915_private *dev_priv = dev->dev_private;
1220 struct drm_framebuffer *fb = crtc->fb;
1221 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00001222 struct drm_i915_gem_object *obj = intel_fb->obj;
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001223 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5eddb702010-09-11 13:48:45 +01001224 int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001225 unsigned long stall_watermark = 200;
1226 u32 dpfc_ctl;
1227
Chris Wilsonbed4a672010-09-11 10:47:47 +01001228 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
1229 if (dpfc_ctl & DPFC_CTL_EN) {
1230 if (dev_priv->cfb_pitch == dev_priv->cfb_pitch / 64 - 1 &&
Chris Wilson05394f32010-11-08 19:18:58 +00001231 dev_priv->cfb_fence == obj->fence_reg &&
Chris Wilsonbed4a672010-09-11 10:47:47 +01001232 dev_priv->cfb_plane == intel_crtc->plane &&
Chris Wilson05394f32010-11-08 19:18:58 +00001233 dev_priv->cfb_offset == obj->gtt_offset &&
Chris Wilsonbed4a672010-09-11 10:47:47 +01001234 dev_priv->cfb_y == crtc->y)
1235 return;
1236
1237 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl & ~DPFC_CTL_EN);
1238 POSTING_READ(ILK_DPFC_CONTROL);
1239 intel_wait_for_vblank(dev, intel_crtc->pipe);
1240 }
1241
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001242 dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
Chris Wilson05394f32010-11-08 19:18:58 +00001243 dev_priv->cfb_fence = obj->fence_reg;
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001244 dev_priv->cfb_plane = intel_crtc->plane;
Chris Wilson05394f32010-11-08 19:18:58 +00001245 dev_priv->cfb_offset = obj->gtt_offset;
Chris Wilsonbed4a672010-09-11 10:47:47 +01001246 dev_priv->cfb_y = crtc->y;
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001247
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001248 dpfc_ctl &= DPFC_RESERVED;
1249 dpfc_ctl |= (plane | DPFC_CTL_LIMIT_1X);
Chris Wilson05394f32010-11-08 19:18:58 +00001250 if (obj->tiling_mode != I915_TILING_NONE) {
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001251 dpfc_ctl |= (DPFC_CTL_FENCE_EN | dev_priv->cfb_fence);
1252 I915_WRITE(ILK_DPFC_CHICKEN, DPFC_HT_MODIFY);
1253 } else {
1254 I915_WRITE(ILK_DPFC_CHICKEN, ~DPFC_HT_MODIFY);
1255 }
1256
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001257 I915_WRITE(ILK_DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
1258 (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
1259 (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
1260 I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
Chris Wilson05394f32010-11-08 19:18:58 +00001261 I915_WRITE(ILK_FBC_RT_BASE, obj->gtt_offset | ILK_FBC_RT_VALID);
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001262 /* enable it... */
Chris Wilsonbed4a672010-09-11 10:47:47 +01001263 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001264
Yuanhan Liu9c04f012010-12-15 15:42:32 +08001265 if (IS_GEN6(dev)) {
1266 I915_WRITE(SNB_DPFC_CTL_SA,
1267 SNB_CPU_FENCE_ENABLE | dev_priv->cfb_fence);
1268 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
1269 }
1270
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001271 DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
1272}
1273
1274void ironlake_disable_fbc(struct drm_device *dev)
1275{
1276 struct drm_i915_private *dev_priv = dev->dev_private;
1277 u32 dpfc_ctl;
1278
1279 /* Disable compression */
1280 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
Chris Wilsonbed4a672010-09-11 10:47:47 +01001281 if (dpfc_ctl & DPFC_CTL_EN) {
1282 dpfc_ctl &= ~DPFC_CTL_EN;
1283 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001284
Chris Wilsonbed4a672010-09-11 10:47:47 +01001285 DRM_DEBUG_KMS("disabled FBC\n");
1286 }
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001287}
1288
1289static bool ironlake_fbc_enabled(struct drm_device *dev)
1290{
1291 struct drm_i915_private *dev_priv = dev->dev_private;
1292
1293 return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
1294}
1295
Adam Jacksonee5382a2010-04-23 11:17:39 -04001296bool intel_fbc_enabled(struct drm_device *dev)
1297{
1298 struct drm_i915_private *dev_priv = dev->dev_private;
1299
1300 if (!dev_priv->display.fbc_enabled)
1301 return false;
1302
1303 return dev_priv->display.fbc_enabled(dev);
1304}
1305
1306void intel_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
1307{
1308 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
1309
1310 if (!dev_priv->display.enable_fbc)
1311 return;
1312
1313 dev_priv->display.enable_fbc(crtc, interval);
1314}
1315
1316void intel_disable_fbc(struct drm_device *dev)
1317{
1318 struct drm_i915_private *dev_priv = dev->dev_private;
1319
1320 if (!dev_priv->display.disable_fbc)
1321 return;
1322
1323 dev_priv->display.disable_fbc(dev);
1324}
1325
Jesse Barnes80824002009-09-10 15:28:06 -07001326/**
1327 * intel_update_fbc - enable/disable FBC as needed
Chris Wilsonbed4a672010-09-11 10:47:47 +01001328 * @dev: the drm_device
Jesse Barnes80824002009-09-10 15:28:06 -07001329 *
1330 * Set up the framebuffer compression hardware at mode set time. We
1331 * enable it if possible:
1332 * - plane A only (on pre-965)
1333 * - no pixel mulitply/line duplication
1334 * - no alpha buffer discard
1335 * - no dual wide
1336 * - framebuffer <= 2048 in width, 1536 in height
1337 *
1338 * We can't assume that any compression will take place (worst case),
1339 * so the compressed buffer has to be the same size as the uncompressed
1340 * one. It also must reside (along with the line length buffer) in
1341 * stolen memory.
1342 *
1343 * We need to enable/disable FBC on a global basis.
1344 */
Chris Wilsonbed4a672010-09-11 10:47:47 +01001345static void intel_update_fbc(struct drm_device *dev)
Jesse Barnes80824002009-09-10 15:28:06 -07001346{
Jesse Barnes80824002009-09-10 15:28:06 -07001347 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonbed4a672010-09-11 10:47:47 +01001348 struct drm_crtc *crtc = NULL, *tmp_crtc;
1349 struct intel_crtc *intel_crtc;
1350 struct drm_framebuffer *fb;
Jesse Barnes80824002009-09-10 15:28:06 -07001351 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00001352 struct drm_i915_gem_object *obj;
Jesse Barnes9c928d12010-07-23 15:20:00 -07001353
1354 DRM_DEBUG_KMS("\n");
Jesse Barnes80824002009-09-10 15:28:06 -07001355
1356 if (!i915_powersave)
1357 return;
1358
Adam Jacksonee5382a2010-04-23 11:17:39 -04001359 if (!I915_HAS_FBC(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07001360 return;
1361
Jesse Barnes80824002009-09-10 15:28:06 -07001362 /*
1363 * If FBC is already on, we just have to verify that we can
1364 * keep it that way...
1365 * Need to disable if:
Jesse Barnes9c928d12010-07-23 15:20:00 -07001366 * - more than one pipe is active
Jesse Barnes80824002009-09-10 15:28:06 -07001367 * - changing FBC params (stride, fence, mode)
1368 * - new fb is too large to fit in compressed buffer
1369 * - going to an unsupported config (interlace, pixel multiply, etc.)
1370 */
Jesse Barnes9c928d12010-07-23 15:20:00 -07001371 list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head) {
Chris Wilsonbed4a672010-09-11 10:47:47 +01001372 if (tmp_crtc->enabled) {
1373 if (crtc) {
1374 DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
1375 dev_priv->no_fbc_reason = FBC_MULTIPLE_PIPES;
1376 goto out_disable;
1377 }
1378 crtc = tmp_crtc;
1379 }
Jesse Barnes9c928d12010-07-23 15:20:00 -07001380 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01001381
1382 if (!crtc || crtc->fb == NULL) {
1383 DRM_DEBUG_KMS("no output, disabling\n");
1384 dev_priv->no_fbc_reason = FBC_NO_OUTPUT;
Jesse Barnes9c928d12010-07-23 15:20:00 -07001385 goto out_disable;
1386 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01001387
1388 intel_crtc = to_intel_crtc(crtc);
1389 fb = crtc->fb;
1390 intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00001391 obj = intel_fb->obj;
Chris Wilsonbed4a672010-09-11 10:47:47 +01001392
Chris Wilson05394f32010-11-08 19:18:58 +00001393 if (intel_fb->obj->base.size > dev_priv->cfb_size) {
Zhao Yakui28c97732009-10-09 11:39:41 +08001394 DRM_DEBUG_KMS("framebuffer too large, disabling "
Chris Wilson5eddb702010-09-11 13:48:45 +01001395 "compression\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08001396 dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
Jesse Barnes80824002009-09-10 15:28:06 -07001397 goto out_disable;
1398 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01001399 if ((crtc->mode.flags & DRM_MODE_FLAG_INTERLACE) ||
1400 (crtc->mode.flags & DRM_MODE_FLAG_DBLSCAN)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08001401 DRM_DEBUG_KMS("mode incompatible with compression, "
Chris Wilson5eddb702010-09-11 13:48:45 +01001402 "disabling\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08001403 dev_priv->no_fbc_reason = FBC_UNSUPPORTED_MODE;
Jesse Barnes80824002009-09-10 15:28:06 -07001404 goto out_disable;
1405 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01001406 if ((crtc->mode.hdisplay > 2048) ||
1407 (crtc->mode.vdisplay > 1536)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08001408 DRM_DEBUG_KMS("mode too large for compression, disabling\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08001409 dev_priv->no_fbc_reason = FBC_MODE_TOO_LARGE;
Jesse Barnes80824002009-09-10 15:28:06 -07001410 goto out_disable;
1411 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01001412 if ((IS_I915GM(dev) || IS_I945GM(dev)) && intel_crtc->plane != 0) {
Zhao Yakui28c97732009-10-09 11:39:41 +08001413 DRM_DEBUG_KMS("plane not 0, disabling compression\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08001414 dev_priv->no_fbc_reason = FBC_BAD_PLANE;
Jesse Barnes80824002009-09-10 15:28:06 -07001415 goto out_disable;
1416 }
Chris Wilson05394f32010-11-08 19:18:58 +00001417 if (obj->tiling_mode != I915_TILING_X) {
Zhao Yakui28c97732009-10-09 11:39:41 +08001418 DRM_DEBUG_KMS("framebuffer not tiled, disabling compression\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08001419 dev_priv->no_fbc_reason = FBC_NOT_TILED;
Jesse Barnes80824002009-09-10 15:28:06 -07001420 goto out_disable;
1421 }
1422
Jason Wesselc924b932010-08-05 09:22:32 -05001423 /* If the kernel debugger is active, always disable compression */
1424 if (in_dbg_master())
1425 goto out_disable;
1426
Chris Wilsonbed4a672010-09-11 10:47:47 +01001427 intel_enable_fbc(crtc, 500);
Jesse Barnes80824002009-09-10 15:28:06 -07001428 return;
1429
1430out_disable:
Jesse Barnes80824002009-09-10 15:28:06 -07001431 /* Multiple disables should be harmless */
Chris Wilsona9394062010-05-27 13:18:16 +01001432 if (intel_fbc_enabled(dev)) {
1433 DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
Adam Jacksonee5382a2010-04-23 11:17:39 -04001434 intel_disable_fbc(dev);
Chris Wilsona9394062010-05-27 13:18:16 +01001435 }
Jesse Barnes80824002009-09-10 15:28:06 -07001436}
1437
Chris Wilson127bd2a2010-07-23 23:32:05 +01001438int
Chris Wilson48b956c2010-09-14 12:50:34 +01001439intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00001440 struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00001441 struct intel_ring_buffer *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001442{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001443 u32 alignment;
1444 int ret;
1445
Chris Wilson05394f32010-11-08 19:18:58 +00001446 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001447 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01001448 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1449 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001450 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01001451 alignment = 4 * 1024;
1452 else
1453 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001454 break;
1455 case I915_TILING_X:
1456 /* pin() will align the object as required by fence */
1457 alignment = 0;
1458 break;
1459 case I915_TILING_Y:
1460 /* FIXME: Is this true? */
1461 DRM_ERROR("Y tiled not allowed for scan out buffers\n");
1462 return -EINVAL;
1463 default:
1464 BUG();
1465 }
1466
Daniel Vetter75e9e912010-11-04 17:11:09 +01001467 ret = i915_gem_object_pin(obj, alignment, true);
Chris Wilson48b956c2010-09-14 12:50:34 +01001468 if (ret)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001469 return ret;
1470
Chris Wilson48b956c2010-09-14 12:50:34 +01001471 ret = i915_gem_object_set_to_display_plane(obj, pipelined);
1472 if (ret)
1473 goto err_unpin;
Chris Wilson72133422010-09-13 23:56:38 +01001474
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001475 /* Install a fence for tiled scan-out. Pre-i965 always needs a
1476 * fence, whereas 965+ only requires a fence if using
1477 * framebuffer compression. For simplicity, we always install
1478 * a fence as the cost is not that onerous.
1479 */
Chris Wilson05394f32010-11-08 19:18:58 +00001480 if (obj->tiling_mode != I915_TILING_NONE) {
Chris Wilsond9e86c02010-11-10 16:40:20 +00001481 ret = i915_gem_object_get_fence(obj, pipelined, false);
Chris Wilson48b956c2010-09-14 12:50:34 +01001482 if (ret)
1483 goto err_unpin;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001484 }
1485
1486 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01001487
1488err_unpin:
1489 i915_gem_object_unpin(obj);
1490 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001491}
1492
Jesse Barnes81255562010-08-02 12:07:50 -07001493/* Assume fb object is pinned & idle & fenced and just update base pointers */
1494static int
1495intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
Jason Wessel21c74a82010-10-13 14:09:44 -05001496 int x, int y, enum mode_set_atomic state)
Jesse Barnes81255562010-08-02 12:07:50 -07001497{
1498 struct drm_device *dev = crtc->dev;
1499 struct drm_i915_private *dev_priv = dev->dev_private;
1500 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1501 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00001502 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07001503 int plane = intel_crtc->plane;
1504 unsigned long Start, Offset;
Jesse Barnes81255562010-08-02 12:07:50 -07001505 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01001506 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07001507
1508 switch (plane) {
1509 case 0:
1510 case 1:
1511 break;
1512 default:
1513 DRM_ERROR("Can't update plane %d in SAREA\n", plane);
1514 return -EINVAL;
1515 }
1516
1517 intel_fb = to_intel_framebuffer(fb);
1518 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07001519
Chris Wilson5eddb702010-09-11 13:48:45 +01001520 reg = DSPCNTR(plane);
1521 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07001522 /* Mask out pixel format bits in case we change it */
1523 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
1524 switch (fb->bits_per_pixel) {
1525 case 8:
1526 dspcntr |= DISPPLANE_8BPP;
1527 break;
1528 case 16:
1529 if (fb->depth == 15)
1530 dspcntr |= DISPPLANE_15_16BPP;
1531 else
1532 dspcntr |= DISPPLANE_16BPP;
1533 break;
1534 case 24:
1535 case 32:
1536 dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
1537 break;
1538 default:
1539 DRM_ERROR("Unknown color depth\n");
1540 return -EINVAL;
1541 }
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001542 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00001543 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07001544 dspcntr |= DISPPLANE_TILED;
1545 else
1546 dspcntr &= ~DISPPLANE_TILED;
1547 }
1548
Chris Wilson4e6cfef2010-08-08 13:20:19 +01001549 if (HAS_PCH_SPLIT(dev))
Jesse Barnes81255562010-08-02 12:07:50 -07001550 /* must disable */
1551 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
1552
Chris Wilson5eddb702010-09-11 13:48:45 +01001553 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07001554
Chris Wilson05394f32010-11-08 19:18:58 +00001555 Start = obj->gtt_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07001556 Offset = y * fb->pitch + x * (fb->bits_per_pixel / 8);
1557
Chris Wilson4e6cfef2010-08-08 13:20:19 +01001558 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
1559 Start, Offset, x, y, fb->pitch);
Chris Wilson5eddb702010-09-11 13:48:45 +01001560 I915_WRITE(DSPSTRIDE(plane), fb->pitch);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001561 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson5eddb702010-09-11 13:48:45 +01001562 I915_WRITE(DSPSURF(plane), Start);
1563 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
1564 I915_WRITE(DSPADDR(plane), Offset);
1565 } else
1566 I915_WRITE(DSPADDR(plane), Start + Offset);
1567 POSTING_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07001568
Chris Wilsonbed4a672010-09-11 10:47:47 +01001569 intel_update_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02001570 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07001571
1572 return 0;
1573}
1574
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001575static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05001576intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
1577 struct drm_framebuffer *old_fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08001578{
1579 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08001580 struct drm_i915_master_private *master_priv;
1581 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001582 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08001583
1584 /* no fb bound */
1585 if (!crtc->fb) {
Zhao Yakui28c97732009-10-09 11:39:41 +08001586 DRM_DEBUG_KMS("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001587 return 0;
1588 }
1589
Chris Wilson265db952010-09-20 15:41:01 +01001590 switch (intel_crtc->plane) {
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001591 case 0:
1592 case 1:
1593 break;
1594 default:
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001595 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08001596 }
1597
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001598 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01001599 ret = intel_pin_and_fence_fb_obj(dev,
1600 to_intel_framebuffer(crtc->fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00001601 NULL);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001602 if (ret != 0) {
1603 mutex_unlock(&dev->struct_mutex);
1604 return ret;
1605 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05001606
Chris Wilson265db952010-09-20 15:41:01 +01001607 if (old_fb) {
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01001608 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001609 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
Chris Wilson265db952010-09-20 15:41:01 +01001610
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01001611 wait_event(dev_priv->pending_flip_queue,
Chris Wilson05394f32010-11-08 19:18:58 +00001612 atomic_read(&obj->pending_flip) == 0);
Chris Wilson85345512010-11-13 09:49:11 +00001613
1614 /* Big Hammer, we also need to ensure that any pending
1615 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
1616 * current scanout is retired before unpinning the old
1617 * framebuffer.
1618 */
Chris Wilson05394f32010-11-08 19:18:58 +00001619 ret = i915_gem_object_flush_gpu(obj, false);
Chris Wilson85345512010-11-13 09:49:11 +00001620 if (ret) {
1621 i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
1622 mutex_unlock(&dev->struct_mutex);
1623 return ret;
1624 }
Chris Wilson265db952010-09-20 15:41:01 +01001625 }
1626
Jason Wessel21c74a82010-10-13 14:09:44 -05001627 ret = intel_pipe_set_base_atomic(crtc, crtc->fb, x, y,
1628 LEAVE_ATOMIC_MODE_SET);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01001629 if (ret) {
Chris Wilson265db952010-09-20 15:41:01 +01001630 i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001631 mutex_unlock(&dev->struct_mutex);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01001632 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08001633 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05001634
Chris Wilsonb7f1de22010-12-14 16:09:31 +00001635 if (old_fb) {
1636 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson265db952010-09-20 15:41:01 +01001637 i915_gem_object_unpin(to_intel_framebuffer(old_fb)->obj);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00001638 }
Jesse Barnes652c3932009-08-17 13:31:43 -07001639
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001640 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08001641
1642 if (!dev->primary->master)
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001643 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08001644
1645 master_priv = dev->primary->master->driver_priv;
1646 if (!master_priv->sarea_priv)
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001647 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08001648
Chris Wilson265db952010-09-20 15:41:01 +01001649 if (intel_crtc->pipe) {
Jesse Barnes79e53942008-11-07 14:24:08 -08001650 master_priv->sarea_priv->pipeB_x = x;
1651 master_priv->sarea_priv->pipeB_y = y;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001652 } else {
1653 master_priv->sarea_priv->pipeA_x = x;
1654 master_priv->sarea_priv->pipeA_y = y;
Jesse Barnes79e53942008-11-07 14:24:08 -08001655 }
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001656
1657 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08001658}
1659
Chris Wilson5eddb702010-09-11 13:48:45 +01001660static void ironlake_set_pll_edp(struct drm_crtc *crtc, int clock)
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001661{
1662 struct drm_device *dev = crtc->dev;
1663 struct drm_i915_private *dev_priv = dev->dev_private;
1664 u32 dpa_ctl;
1665
Zhao Yakui28c97732009-10-09 11:39:41 +08001666 DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", clock);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001667 dpa_ctl = I915_READ(DP_A);
1668 dpa_ctl &= ~DP_PLL_FREQ_MASK;
1669
1670 if (clock < 200000) {
1671 u32 temp;
1672 dpa_ctl |= DP_PLL_FREQ_160MHZ;
1673 /* workaround for 160Mhz:
1674 1) program 0x4600c bits 15:0 = 0x8124
1675 2) program 0x46010 bit 0 = 1
1676 3) program 0x46034 bit 24 = 1
1677 4) program 0x64000 bit 14 = 1
1678 */
1679 temp = I915_READ(0x4600c);
1680 temp &= 0xffff0000;
1681 I915_WRITE(0x4600c, temp | 0x8124);
1682
1683 temp = I915_READ(0x46010);
1684 I915_WRITE(0x46010, temp | 1);
1685
1686 temp = I915_READ(0x46034);
1687 I915_WRITE(0x46034, temp | (1 << 24));
1688 } else {
1689 dpa_ctl |= DP_PLL_FREQ_270MHZ;
1690 }
1691 I915_WRITE(DP_A, dpa_ctl);
1692
Chris Wilson5eddb702010-09-11 13:48:45 +01001693 POSTING_READ(DP_A);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001694 udelay(500);
1695}
1696
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08001697static void intel_fdi_normal_train(struct drm_crtc *crtc)
1698{
1699 struct drm_device *dev = crtc->dev;
1700 struct drm_i915_private *dev_priv = dev->dev_private;
1701 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1702 int pipe = intel_crtc->pipe;
1703 u32 reg, temp;
1704
1705 /* enable normal train */
1706 reg = FDI_TX_CTL(pipe);
1707 temp = I915_READ(reg);
1708 temp &= ~FDI_LINK_TRAIN_NONE;
1709 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
1710 I915_WRITE(reg, temp);
1711
1712 reg = FDI_RX_CTL(pipe);
1713 temp = I915_READ(reg);
1714 if (HAS_PCH_CPT(dev)) {
1715 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
1716 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
1717 } else {
1718 temp &= ~FDI_LINK_TRAIN_NONE;
1719 temp |= FDI_LINK_TRAIN_NONE;
1720 }
1721 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
1722
1723 /* wait one idle pattern time */
1724 POSTING_READ(reg);
1725 udelay(1000);
1726}
1727
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001728/* The FDI link training functions for ILK/Ibexpeak. */
1729static void ironlake_fdi_link_train(struct drm_crtc *crtc)
1730{
1731 struct drm_device *dev = crtc->dev;
1732 struct drm_i915_private *dev_priv = dev->dev_private;
1733 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1734 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01001735 u32 reg, temp, tries;
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001736
Adam Jacksone1a44742010-06-25 15:32:14 -04001737 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
1738 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01001739 reg = FDI_RX_IMR(pipe);
1740 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04001741 temp &= ~FDI_RX_SYMBOL_LOCK;
1742 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01001743 I915_WRITE(reg, temp);
1744 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04001745 udelay(150);
1746
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001747 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01001748 reg = FDI_TX_CTL(pipe);
1749 temp = I915_READ(reg);
Adam Jackson77ffb592010-04-12 11:38:44 -04001750 temp &= ~(7 << 19);
1751 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001752 temp &= ~FDI_LINK_TRAIN_NONE;
1753 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01001754 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001755
Chris Wilson5eddb702010-09-11 13:48:45 +01001756 reg = FDI_RX_CTL(pipe);
1757 temp = I915_READ(reg);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001758 temp &= ~FDI_LINK_TRAIN_NONE;
1759 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01001760 I915_WRITE(reg, temp | FDI_RX_ENABLE);
1761
1762 POSTING_READ(reg);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001763 udelay(150);
1764
Jesse Barnes5b2adf82010-10-07 16:01:15 -07001765 /* Ironlake workaround, enable clock pointer after FDI enable*/
1766 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_ENABLE);
1767
Chris Wilson5eddb702010-09-11 13:48:45 +01001768 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04001769 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01001770 temp = I915_READ(reg);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001771 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
1772
1773 if ((temp & FDI_RX_BIT_LOCK)) {
1774 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01001775 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001776 break;
1777 }
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001778 }
Adam Jacksone1a44742010-06-25 15:32:14 -04001779 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01001780 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001781
1782 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01001783 reg = FDI_TX_CTL(pipe);
1784 temp = I915_READ(reg);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001785 temp &= ~FDI_LINK_TRAIN_NONE;
1786 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01001787 I915_WRITE(reg, temp);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001788
Chris Wilson5eddb702010-09-11 13:48:45 +01001789 reg = FDI_RX_CTL(pipe);
1790 temp = I915_READ(reg);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001791 temp &= ~FDI_LINK_TRAIN_NONE;
1792 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01001793 I915_WRITE(reg, temp);
1794
1795 POSTING_READ(reg);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001796 udelay(150);
1797
Chris Wilson5eddb702010-09-11 13:48:45 +01001798 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04001799 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01001800 temp = I915_READ(reg);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001801 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
1802
1803 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01001804 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001805 DRM_DEBUG_KMS("FDI train 2 done.\n");
1806 break;
1807 }
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001808 }
Adam Jacksone1a44742010-06-25 15:32:14 -04001809 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01001810 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001811
1812 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07001813
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001814}
1815
Chris Wilson5eddb702010-09-11 13:48:45 +01001816static const int const snb_b_fdi_train_param [] = {
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001817 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
1818 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
1819 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
1820 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
1821};
1822
1823/* The FDI link training functions for SNB/Cougarpoint. */
1824static void gen6_fdi_link_train(struct drm_crtc *crtc)
1825{
1826 struct drm_device *dev = crtc->dev;
1827 struct drm_i915_private *dev_priv = dev->dev_private;
1828 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1829 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01001830 u32 reg, temp, i;
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001831
Adam Jacksone1a44742010-06-25 15:32:14 -04001832 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
1833 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01001834 reg = FDI_RX_IMR(pipe);
1835 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04001836 temp &= ~FDI_RX_SYMBOL_LOCK;
1837 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01001838 I915_WRITE(reg, temp);
1839
1840 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04001841 udelay(150);
1842
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001843 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01001844 reg = FDI_TX_CTL(pipe);
1845 temp = I915_READ(reg);
Adam Jackson77ffb592010-04-12 11:38:44 -04001846 temp &= ~(7 << 19);
1847 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001848 temp &= ~FDI_LINK_TRAIN_NONE;
1849 temp |= FDI_LINK_TRAIN_PATTERN_1;
1850 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
1851 /* SNB-B */
1852 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01001853 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001854
Chris Wilson5eddb702010-09-11 13:48:45 +01001855 reg = FDI_RX_CTL(pipe);
1856 temp = I915_READ(reg);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001857 if (HAS_PCH_CPT(dev)) {
1858 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
1859 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
1860 } else {
1861 temp &= ~FDI_LINK_TRAIN_NONE;
1862 temp |= FDI_LINK_TRAIN_PATTERN_1;
1863 }
Chris Wilson5eddb702010-09-11 13:48:45 +01001864 I915_WRITE(reg, temp | FDI_RX_ENABLE);
1865
1866 POSTING_READ(reg);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001867 udelay(150);
1868
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001869 for (i = 0; i < 4; i++ ) {
Chris Wilson5eddb702010-09-11 13:48:45 +01001870 reg = FDI_TX_CTL(pipe);
1871 temp = I915_READ(reg);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001872 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
1873 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01001874 I915_WRITE(reg, temp);
1875
1876 POSTING_READ(reg);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001877 udelay(500);
1878
Chris Wilson5eddb702010-09-11 13:48:45 +01001879 reg = FDI_RX_IIR(pipe);
1880 temp = I915_READ(reg);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001881 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
1882
1883 if (temp & FDI_RX_BIT_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01001884 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001885 DRM_DEBUG_KMS("FDI train 1 done.\n");
1886 break;
1887 }
1888 }
1889 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01001890 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001891
1892 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01001893 reg = FDI_TX_CTL(pipe);
1894 temp = I915_READ(reg);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001895 temp &= ~FDI_LINK_TRAIN_NONE;
1896 temp |= FDI_LINK_TRAIN_PATTERN_2;
1897 if (IS_GEN6(dev)) {
1898 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
1899 /* SNB-B */
1900 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
1901 }
Chris Wilson5eddb702010-09-11 13:48:45 +01001902 I915_WRITE(reg, temp);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001903
Chris Wilson5eddb702010-09-11 13:48:45 +01001904 reg = FDI_RX_CTL(pipe);
1905 temp = I915_READ(reg);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001906 if (HAS_PCH_CPT(dev)) {
1907 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
1908 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
1909 } else {
1910 temp &= ~FDI_LINK_TRAIN_NONE;
1911 temp |= FDI_LINK_TRAIN_PATTERN_2;
1912 }
Chris Wilson5eddb702010-09-11 13:48:45 +01001913 I915_WRITE(reg, temp);
1914
1915 POSTING_READ(reg);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001916 udelay(150);
1917
1918 for (i = 0; i < 4; i++ ) {
Chris Wilson5eddb702010-09-11 13:48:45 +01001919 reg = FDI_TX_CTL(pipe);
1920 temp = I915_READ(reg);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001921 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
1922 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01001923 I915_WRITE(reg, temp);
1924
1925 POSTING_READ(reg);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001926 udelay(500);
1927
Chris Wilson5eddb702010-09-11 13:48:45 +01001928 reg = FDI_RX_IIR(pipe);
1929 temp = I915_READ(reg);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001930 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
1931
1932 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01001933 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001934 DRM_DEBUG_KMS("FDI train 2 done.\n");
1935 break;
1936 }
1937 }
1938 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01001939 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001940
1941 DRM_DEBUG_KMS("FDI train done.\n");
1942}
1943
Jesse Barnes0e23b992010-09-10 11:10:00 -07001944static void ironlake_fdi_enable(struct drm_crtc *crtc)
1945{
1946 struct drm_device *dev = crtc->dev;
1947 struct drm_i915_private *dev_priv = dev->dev_private;
1948 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1949 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01001950 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07001951
Jesse Barnesc64e3112010-09-10 11:27:03 -07001952 /* Write the TU size bits so error detection works */
Chris Wilson5eddb702010-09-11 13:48:45 +01001953 I915_WRITE(FDI_RX_TUSIZE1(pipe),
1954 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
Jesse Barnesc64e3112010-09-10 11:27:03 -07001955
Jesse Barnes0e23b992010-09-10 11:10:00 -07001956 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01001957 reg = FDI_RX_CTL(pipe);
1958 temp = I915_READ(reg);
1959 temp &= ~((0x7 << 19) | (0x7 << 16));
Jesse Barnes0e23b992010-09-10 11:10:00 -07001960 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Chris Wilson5eddb702010-09-11 13:48:45 +01001961 temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
1962 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
1963
1964 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07001965 udelay(200);
1966
1967 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01001968 temp = I915_READ(reg);
1969 I915_WRITE(reg, temp | FDI_PCDCLK);
1970
1971 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07001972 udelay(200);
1973
1974 /* Enable CPU FDI TX PLL, always on for Ironlake */
Chris Wilson5eddb702010-09-11 13:48:45 +01001975 reg = FDI_TX_CTL(pipe);
1976 temp = I915_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07001977 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
Chris Wilson5eddb702010-09-11 13:48:45 +01001978 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
1979
1980 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07001981 udelay(100);
1982 }
1983}
1984
Chris Wilson5eddb702010-09-11 13:48:45 +01001985static void intel_flush_display_plane(struct drm_device *dev,
1986 int plane)
1987{
1988 struct drm_i915_private *dev_priv = dev->dev_private;
1989 u32 reg = DSPADDR(plane);
1990 I915_WRITE(reg, I915_READ(reg));
1991}
1992
Chris Wilson6b383a72010-09-13 13:54:26 +01001993/*
1994 * When we disable a pipe, we need to clear any pending scanline wait events
1995 * to avoid hanging the ring, which we assume we are waiting on.
1996 */
1997static void intel_clear_scanline_wait(struct drm_device *dev)
1998{
1999 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson8168bd42010-11-11 17:54:52 +00002000 struct intel_ring_buffer *ring;
Chris Wilson6b383a72010-09-13 13:54:26 +01002001 u32 tmp;
2002
2003 if (IS_GEN2(dev))
2004 /* Can't break the hang on i8xx */
2005 return;
2006
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002007 ring = LP_RING(dev_priv);
Chris Wilson8168bd42010-11-11 17:54:52 +00002008 tmp = I915_READ_CTL(ring);
2009 if (tmp & RING_WAIT)
2010 I915_WRITE_CTL(ring, tmp);
Chris Wilson6b383a72010-09-13 13:54:26 +01002011}
2012
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002013static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2014{
Chris Wilson05394f32010-11-08 19:18:58 +00002015 struct drm_i915_gem_object *obj;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002016 struct drm_i915_private *dev_priv;
2017
2018 if (crtc->fb == NULL)
2019 return;
2020
Chris Wilson05394f32010-11-08 19:18:58 +00002021 obj = to_intel_framebuffer(crtc->fb)->obj;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002022 dev_priv = crtc->dev->dev_private;
2023 wait_event(dev_priv->pending_flip_queue,
Chris Wilson05394f32010-11-08 19:18:58 +00002024 atomic_read(&obj->pending_flip) == 0);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002025}
2026
Jesse Barnes6be4a602010-09-10 10:26:01 -07002027static void ironlake_crtc_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08002028{
2029 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002030 struct drm_i915_private *dev_priv = dev->dev_private;
2031 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2032 int pipe = intel_crtc->pipe;
Shaohua Li7662c8b2009-06-26 11:23:55 +08002033 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002034 u32 reg, temp;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002035
Chris Wilsonf7abfe82010-09-13 14:19:16 +01002036 if (intel_crtc->active)
2037 return;
2038
2039 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01002040 intel_update_watermarks(dev);
2041
Jesse Barnes6be4a602010-09-10 10:26:01 -07002042 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
2043 temp = I915_READ(PCH_LVDS);
Chris Wilson5eddb702010-09-11 13:48:45 +01002044 if ((temp & LVDS_PORT_EN) == 0)
Jesse Barnes6be4a602010-09-10 10:26:01 -07002045 I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002046 }
2047
Jesse Barnes0e23b992010-09-10 11:10:00 -07002048 ironlake_fdi_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002049
2050 /* Enable panel fitting for LVDS */
2051 if (dev_priv->pch_pf_size &&
Jesse Barnes1d850362010-10-07 16:01:10 -07002052 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) || HAS_eDP)) {
Jesse Barnes6be4a602010-09-10 10:26:01 -07002053 /* Force use of hard-coded filter coefficients
2054 * as some pre-programmed values are broken,
2055 * e.g. x201.
2056 */
2057 I915_WRITE(pipe ? PFB_CTL_1 : PFA_CTL_1,
2058 PF_ENABLE | PF_FILTER_MED_3x3);
2059 I915_WRITE(pipe ? PFB_WIN_POS : PFA_WIN_POS,
2060 dev_priv->pch_pf_pos);
2061 I915_WRITE(pipe ? PFB_WIN_SZ : PFA_WIN_SZ,
2062 dev_priv->pch_pf_size);
2063 }
2064
2065 /* Enable CPU pipe */
Chris Wilson5eddb702010-09-11 13:48:45 +01002066 reg = PIPECONF(pipe);
2067 temp = I915_READ(reg);
2068 if ((temp & PIPECONF_ENABLE) == 0) {
2069 I915_WRITE(reg, temp | PIPECONF_ENABLE);
2070 POSTING_READ(reg);
Jesse Barnes17f67662010-10-07 16:01:19 -07002071 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002072 }
2073
2074 /* configure and enable CPU plane */
Chris Wilson5eddb702010-09-11 13:48:45 +01002075 reg = DSPCNTR(plane);
2076 temp = I915_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002077 if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002078 I915_WRITE(reg, temp | DISPLAY_PLANE_ENABLE);
2079 intel_flush_display_plane(dev, plane);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002080 }
2081
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002082 /* For PCH output, training FDI link */
2083 if (IS_GEN6(dev))
2084 gen6_fdi_link_train(crtc);
2085 else
2086 ironlake_fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002087
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002088 /* enable PCH DPLL */
Chris Wilson5eddb702010-09-11 13:48:45 +01002089 reg = PCH_DPLL(pipe);
2090 temp = I915_READ(reg);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002091 if ((temp & DPLL_VCO_ENABLE) == 0) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002092 I915_WRITE(reg, temp | DPLL_VCO_ENABLE);
2093 POSTING_READ(reg);
Chris Wilson8c4223b2010-09-10 22:33:42 +01002094 udelay(200);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002095 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002096
2097 if (HAS_PCH_CPT(dev)) {
2098 /* Be sure PCH DPLL SEL is set */
2099 temp = I915_READ(PCH_DPLL_SEL);
Chris Wilson5eddb702010-09-11 13:48:45 +01002100 if (pipe == 0 && (temp & TRANSA_DPLL_ENABLE) == 0)
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002101 temp |= (TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL);
Chris Wilson5eddb702010-09-11 13:48:45 +01002102 else if (pipe == 1 && (temp & TRANSB_DPLL_ENABLE) == 0)
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002103 temp |= (TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
2104 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002105 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002106
Chris Wilson5eddb702010-09-11 13:48:45 +01002107 /* set transcoder timing */
2108 I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
2109 I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
2110 I915_WRITE(TRANS_HSYNC(pipe), I915_READ(HSYNC(pipe)));
2111
2112 I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
2113 I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
2114 I915_WRITE(TRANS_VSYNC(pipe), I915_READ(VSYNC(pipe)));
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002115
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002116 intel_fdi_normal_train(crtc);
2117
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002118 /* For PCH DP, enable TRANS_DP_CTL */
2119 if (HAS_PCH_CPT(dev) &&
2120 intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002121 reg = TRANS_DP_CTL(pipe);
2122 temp = I915_READ(reg);
2123 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08002124 TRANS_DP_SYNC_MASK |
2125 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01002126 temp |= (TRANS_DP_OUTPUT_ENABLE |
2127 TRANS_DP_ENH_FRAMING);
Eric Anholt220cad32010-11-18 09:32:58 +08002128 temp |= TRANS_DP_8BPC;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002129
2130 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01002131 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002132 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01002133 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002134
2135 switch (intel_trans_dp_port_sel(crtc)) {
2136 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01002137 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002138 break;
2139 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01002140 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002141 break;
2142 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01002143 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002144 break;
2145 default:
2146 DRM_DEBUG_KMS("Wrong PCH DP port return. Guess port B\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002147 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002148 break;
2149 }
2150
Chris Wilson5eddb702010-09-11 13:48:45 +01002151 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002152 }
2153
2154 /* enable PCH transcoder */
Chris Wilson5eddb702010-09-11 13:48:45 +01002155 reg = TRANSCONF(pipe);
2156 temp = I915_READ(reg);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002157 /*
2158 * make the BPC in transcoder be consistent with
2159 * that in pipeconf reg.
2160 */
2161 temp &= ~PIPE_BPC_MASK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002162 temp |= I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK;
2163 I915_WRITE(reg, temp | TRANS_ENABLE);
2164 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Jesse Barnes17f67662010-10-07 16:01:19 -07002165 DRM_ERROR("failed to enable transcoder %d\n", pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002166
2167 intel_crtc_load_lut(crtc);
Chris Wilsonbed4a672010-09-11 10:47:47 +01002168 intel_update_fbc(dev);
Chris Wilson6b383a72010-09-13 13:54:26 +01002169 intel_crtc_update_cursor(crtc, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002170}
2171
2172static void ironlake_crtc_disable(struct drm_crtc *crtc)
2173{
2174 struct drm_device *dev = crtc->dev;
2175 struct drm_i915_private *dev_priv = dev->dev_private;
2176 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2177 int pipe = intel_crtc->pipe;
2178 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002179 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07002180
Chris Wilsonf7abfe82010-09-13 14:19:16 +01002181 if (!intel_crtc->active)
2182 return;
2183
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002184 intel_crtc_wait_for_pending_flips(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002185 drm_vblank_off(dev, pipe);
Chris Wilson6b383a72010-09-13 13:54:26 +01002186 intel_crtc_update_cursor(crtc, false);
Chris Wilson5eddb702010-09-11 13:48:45 +01002187
Jesse Barnes6be4a602010-09-10 10:26:01 -07002188 /* Disable display plane */
Chris Wilson5eddb702010-09-11 13:48:45 +01002189 reg = DSPCNTR(plane);
2190 temp = I915_READ(reg);
2191 if (temp & DISPLAY_PLANE_ENABLE) {
2192 I915_WRITE(reg, temp & ~DISPLAY_PLANE_ENABLE);
2193 intel_flush_display_plane(dev, plane);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002194 }
2195
2196 if (dev_priv->cfb_plane == plane &&
2197 dev_priv->display.disable_fbc)
2198 dev_priv->display.disable_fbc(dev);
2199
2200 /* disable cpu pipe, disable after all planes disabled */
Chris Wilson5eddb702010-09-11 13:48:45 +01002201 reg = PIPECONF(pipe);
2202 temp = I915_READ(reg);
2203 if (temp & PIPECONF_ENABLE) {
2204 I915_WRITE(reg, temp & ~PIPECONF_ENABLE);
Jesse Barnes17f67662010-10-07 16:01:19 -07002205 POSTING_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002206 /* wait for cpu pipe off, pipe state */
Jesse Barnes17f67662010-10-07 16:01:19 -07002207 intel_wait_for_pipe_off(dev, intel_crtc->pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01002208 }
Jesse Barnes6be4a602010-09-10 10:26:01 -07002209
Jesse Barnes6be4a602010-09-10 10:26:01 -07002210 /* Disable PF */
2211 I915_WRITE(pipe ? PFB_CTL_1 : PFA_CTL_1, 0);
2212 I915_WRITE(pipe ? PFB_WIN_SZ : PFA_WIN_SZ, 0);
2213
2214 /* disable CPU FDI tx and PCH FDI rx */
Chris Wilson5eddb702010-09-11 13:48:45 +01002215 reg = FDI_TX_CTL(pipe);
2216 temp = I915_READ(reg);
2217 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2218 POSTING_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002219
Chris Wilson5eddb702010-09-11 13:48:45 +01002220 reg = FDI_RX_CTL(pipe);
2221 temp = I915_READ(reg);
2222 temp &= ~(0x7 << 16);
2223 temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2224 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002225
Chris Wilson5eddb702010-09-11 13:48:45 +01002226 POSTING_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002227 udelay(100);
2228
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002229 /* Ironlake workaround, disable clock pointer after downing FDI */
Zhenyu Wange07ac3a2010-11-04 09:02:54 +00002230 if (HAS_PCH_IBX(dev))
2231 I915_WRITE(FDI_RX_CHICKEN(pipe),
2232 I915_READ(FDI_RX_CHICKEN(pipe) &
2233 ~FDI_RX_PHASE_SYNC_POINTER_ENABLE));
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002234
Jesse Barnes6be4a602010-09-10 10:26:01 -07002235 /* still set train pattern 1 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002236 reg = FDI_TX_CTL(pipe);
2237 temp = I915_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002238 temp &= ~FDI_LINK_TRAIN_NONE;
2239 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002240 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002241
Chris Wilson5eddb702010-09-11 13:48:45 +01002242 reg = FDI_RX_CTL(pipe);
2243 temp = I915_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002244 if (HAS_PCH_CPT(dev)) {
2245 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2246 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2247 } else {
2248 temp &= ~FDI_LINK_TRAIN_NONE;
2249 temp |= FDI_LINK_TRAIN_PATTERN_1;
2250 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002251 /* BPC in FDI rx is consistent with that in PIPECONF */
2252 temp &= ~(0x07 << 16);
2253 temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2254 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002255
Chris Wilson5eddb702010-09-11 13:48:45 +01002256 POSTING_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002257 udelay(100);
2258
2259 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
2260 temp = I915_READ(PCH_LVDS);
Chris Wilson5eddb702010-09-11 13:48:45 +01002261 if (temp & LVDS_PORT_EN) {
2262 I915_WRITE(PCH_LVDS, temp & ~LVDS_PORT_EN);
2263 POSTING_READ(PCH_LVDS);
2264 udelay(100);
2265 }
Jesse Barnes6be4a602010-09-10 10:26:01 -07002266 }
2267
2268 /* disable PCH transcoder */
Chris Wilson5eddb702010-09-11 13:48:45 +01002269 reg = TRANSCONF(plane);
2270 temp = I915_READ(reg);
2271 if (temp & TRANS_ENABLE) {
2272 I915_WRITE(reg, temp & ~TRANS_ENABLE);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002273 /* wait for PCH transcoder off, transcoder state */
Chris Wilson5eddb702010-09-11 13:48:45 +01002274 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Jesse Barnes6be4a602010-09-10 10:26:01 -07002275 DRM_ERROR("failed to disable transcoder\n");
2276 }
2277
Jesse Barnes6be4a602010-09-10 10:26:01 -07002278 if (HAS_PCH_CPT(dev)) {
2279 /* disable TRANS_DP_CTL */
Chris Wilson5eddb702010-09-11 13:48:45 +01002280 reg = TRANS_DP_CTL(pipe);
2281 temp = I915_READ(reg);
2282 temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
2283 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002284
2285 /* disable DPLL_SEL */
2286 temp = I915_READ(PCH_DPLL_SEL);
Chris Wilson5eddb702010-09-11 13:48:45 +01002287 if (pipe == 0)
Jesse Barnes6be4a602010-09-10 10:26:01 -07002288 temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
2289 else
2290 temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
2291 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002292 }
2293
2294 /* disable PCH DPLL */
Chris Wilson5eddb702010-09-11 13:48:45 +01002295 reg = PCH_DPLL(pipe);
2296 temp = I915_READ(reg);
2297 I915_WRITE(reg, temp & ~DPLL_VCO_ENABLE);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002298
2299 /* Switch from PCDclk to Rawclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002300 reg = FDI_RX_CTL(pipe);
2301 temp = I915_READ(reg);
2302 I915_WRITE(reg, temp & ~FDI_PCDCLK);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002303
2304 /* Disable CPU FDI TX PLL */
Chris Wilson5eddb702010-09-11 13:48:45 +01002305 reg = FDI_TX_CTL(pipe);
2306 temp = I915_READ(reg);
2307 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2308
2309 POSTING_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002310 udelay(100);
2311
Chris Wilson5eddb702010-09-11 13:48:45 +01002312 reg = FDI_RX_CTL(pipe);
2313 temp = I915_READ(reg);
2314 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002315
2316 /* Wait for the clocks to turn off. */
Chris Wilson5eddb702010-09-11 13:48:45 +01002317 POSTING_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002318 udelay(100);
Chris Wilson6b383a72010-09-13 13:54:26 +01002319
Chris Wilsonf7abfe82010-09-13 14:19:16 +01002320 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01002321 intel_update_watermarks(dev);
2322 intel_update_fbc(dev);
2323 intel_clear_scanline_wait(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002324}
2325
2326static void ironlake_crtc_dpms(struct drm_crtc *crtc, int mode)
2327{
2328 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2329 int pipe = intel_crtc->pipe;
2330 int plane = intel_crtc->plane;
2331
Zhenyu Wang2c072452009-06-05 15:38:42 +08002332 /* XXX: When our outputs are all unaware of DPMS modes other than off
2333 * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
2334 */
2335 switch (mode) {
2336 case DRM_MODE_DPMS_ON:
2337 case DRM_MODE_DPMS_STANDBY:
2338 case DRM_MODE_DPMS_SUSPEND:
Chris Wilson868dc582010-08-07 11:01:31 +01002339 DRM_DEBUG_KMS("crtc %d/%d dpms on\n", pipe, plane);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002340 ironlake_crtc_enable(crtc);
Chris Wilson868dc582010-08-07 11:01:31 +01002341 break;
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08002342
Zhenyu Wang2c072452009-06-05 15:38:42 +08002343 case DRM_MODE_DPMS_OFF:
Chris Wilson868dc582010-08-07 11:01:31 +01002344 DRM_DEBUG_KMS("crtc %d/%d dpms off\n", pipe, plane);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002345 ironlake_crtc_disable(crtc);
Zhenyu Wang2c072452009-06-05 15:38:42 +08002346 break;
2347 }
2348}
2349
Daniel Vetter02e792f2009-09-15 22:57:34 +02002350static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
2351{
Daniel Vetter02e792f2009-09-15 22:57:34 +02002352 if (!enable && intel_crtc->overlay) {
Chris Wilson23f09ce2010-08-12 13:53:37 +01002353 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02002354
Chris Wilson23f09ce2010-08-12 13:53:37 +01002355 mutex_lock(&dev->struct_mutex);
2356 (void) intel_overlay_switch_off(intel_crtc->overlay, false);
2357 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02002358 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02002359
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01002360 /* Let userspace switch the overlay on again. In most cases userspace
2361 * has to recompute where to put it anyway.
2362 */
Daniel Vetter02e792f2009-09-15 22:57:34 +02002363}
2364
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002365static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08002366{
2367 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002368 struct drm_i915_private *dev_priv = dev->dev_private;
2369 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2370 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07002371 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002372 u32 reg, temp;
Jesse Barnes79e53942008-11-07 14:24:08 -08002373
Chris Wilsonf7abfe82010-09-13 14:19:16 +01002374 if (intel_crtc->active)
2375 return;
2376
2377 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01002378 intel_update_watermarks(dev);
2379
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002380 /* Enable the DPLL */
Chris Wilson5eddb702010-09-11 13:48:45 +01002381 reg = DPLL(pipe);
2382 temp = I915_READ(reg);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002383 if ((temp & DPLL_VCO_ENABLE) == 0) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002384 I915_WRITE(reg, temp);
2385
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002386 /* Wait for the clocks to stabilize. */
Chris Wilson5eddb702010-09-11 13:48:45 +01002387 POSTING_READ(reg);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002388 udelay(150);
Chris Wilson5eddb702010-09-11 13:48:45 +01002389
2390 I915_WRITE(reg, temp | DPLL_VCO_ENABLE);
2391
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002392 /* Wait for the clocks to stabilize. */
Chris Wilson5eddb702010-09-11 13:48:45 +01002393 POSTING_READ(reg);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002394 udelay(150);
Chris Wilson5eddb702010-09-11 13:48:45 +01002395
2396 I915_WRITE(reg, temp | DPLL_VCO_ENABLE);
2397
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002398 /* Wait for the clocks to stabilize. */
Chris Wilson5eddb702010-09-11 13:48:45 +01002399 POSTING_READ(reg);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002400 udelay(150);
2401 }
2402
2403 /* Enable the pipe */
Chris Wilson5eddb702010-09-11 13:48:45 +01002404 reg = PIPECONF(pipe);
2405 temp = I915_READ(reg);
2406 if ((temp & PIPECONF_ENABLE) == 0)
2407 I915_WRITE(reg, temp | PIPECONF_ENABLE);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002408
2409 /* Enable the plane */
Chris Wilson5eddb702010-09-11 13:48:45 +01002410 reg = DSPCNTR(plane);
2411 temp = I915_READ(reg);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002412 if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002413 I915_WRITE(reg, temp | DISPLAY_PLANE_ENABLE);
2414 intel_flush_display_plane(dev, plane);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002415 }
2416
2417 intel_crtc_load_lut(crtc);
Chris Wilsonbed4a672010-09-11 10:47:47 +01002418 intel_update_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002419
2420 /* Give the overlay scaler a chance to enable if it's on this pipe */
2421 intel_crtc_dpms_overlay(intel_crtc, true);
Chris Wilson6b383a72010-09-13 13:54:26 +01002422 intel_crtc_update_cursor(crtc, true);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002423}
2424
2425static void i9xx_crtc_disable(struct drm_crtc *crtc)
2426{
2427 struct drm_device *dev = crtc->dev;
2428 struct drm_i915_private *dev_priv = dev->dev_private;
2429 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2430 int pipe = intel_crtc->pipe;
2431 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002432 u32 reg, temp;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002433
Chris Wilsonf7abfe82010-09-13 14:19:16 +01002434 if (!intel_crtc->active)
2435 return;
2436
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002437 /* Give the overlay scaler a chance to disable if it's on this pipe */
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002438 intel_crtc_wait_for_pending_flips(crtc);
2439 drm_vblank_off(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002440 intel_crtc_dpms_overlay(intel_crtc, false);
Chris Wilson6b383a72010-09-13 13:54:26 +01002441 intel_crtc_update_cursor(crtc, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002442
2443 if (dev_priv->cfb_plane == plane &&
2444 dev_priv->display.disable_fbc)
2445 dev_priv->display.disable_fbc(dev);
2446
2447 /* Disable display plane */
Chris Wilson5eddb702010-09-11 13:48:45 +01002448 reg = DSPCNTR(plane);
2449 temp = I915_READ(reg);
2450 if (temp & DISPLAY_PLANE_ENABLE) {
2451 I915_WRITE(reg, temp & ~DISPLAY_PLANE_ENABLE);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002452 /* Flush the plane changes */
Chris Wilson5eddb702010-09-11 13:48:45 +01002453 intel_flush_display_plane(dev, plane);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002454
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002455 /* Wait for vblank for the disable to take effect */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002456 if (IS_GEN2(dev))
Chris Wilson58e10eb2010-10-03 10:56:11 +01002457 intel_wait_for_vblank(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002458 }
2459
2460 /* Don't disable pipe A or pipe A PLLs if needed */
Chris Wilson5eddb702010-09-11 13:48:45 +01002461 if (pipe == 0 && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
Chris Wilson6b383a72010-09-13 13:54:26 +01002462 goto done;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002463
2464 /* Next, disable display pipes */
Chris Wilson5eddb702010-09-11 13:48:45 +01002465 reg = PIPECONF(pipe);
2466 temp = I915_READ(reg);
2467 if (temp & PIPECONF_ENABLE) {
2468 I915_WRITE(reg, temp & ~PIPECONF_ENABLE);
2469
Chris Wilson58e10eb2010-10-03 10:56:11 +01002470 /* Wait for the pipe to turn off */
Chris Wilson5eddb702010-09-11 13:48:45 +01002471 POSTING_READ(reg);
Chris Wilson58e10eb2010-10-03 10:56:11 +01002472 intel_wait_for_pipe_off(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002473 }
2474
Chris Wilson5eddb702010-09-11 13:48:45 +01002475 reg = DPLL(pipe);
2476 temp = I915_READ(reg);
2477 if (temp & DPLL_VCO_ENABLE) {
2478 I915_WRITE(reg, temp & ~DPLL_VCO_ENABLE);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002479
Chris Wilson5eddb702010-09-11 13:48:45 +01002480 /* Wait for the clocks to turn off. */
2481 POSTING_READ(reg);
2482 udelay(150);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002483 }
Chris Wilson6b383a72010-09-13 13:54:26 +01002484
2485done:
Chris Wilsonf7abfe82010-09-13 14:19:16 +01002486 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01002487 intel_update_fbc(dev);
2488 intel_update_watermarks(dev);
2489 intel_clear_scanline_wait(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002490}
2491
2492static void i9xx_crtc_dpms(struct drm_crtc *crtc, int mode)
2493{
Jesse Barnes79e53942008-11-07 14:24:08 -08002494 /* XXX: When our outputs are all unaware of DPMS modes other than off
2495 * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
2496 */
2497 switch (mode) {
2498 case DRM_MODE_DPMS_ON:
2499 case DRM_MODE_DPMS_STANDBY:
2500 case DRM_MODE_DPMS_SUSPEND:
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002501 i9xx_crtc_enable(crtc);
2502 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08002503 case DRM_MODE_DPMS_OFF:
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002504 i9xx_crtc_disable(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08002505 break;
2506 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08002507}
2508
2509/**
2510 * Sets the power management mode of the pipe and plane.
Zhenyu Wang2c072452009-06-05 15:38:42 +08002511 */
2512static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
2513{
2514 struct drm_device *dev = crtc->dev;
Jesse Barnese70236a2009-09-21 10:42:27 -07002515 struct drm_i915_private *dev_priv = dev->dev_private;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002516 struct drm_i915_master_private *master_priv;
2517 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2518 int pipe = intel_crtc->pipe;
2519 bool enabled;
2520
Chris Wilson032d2a02010-09-06 16:17:22 +01002521 if (intel_crtc->dpms_mode == mode)
2522 return;
2523
Chris Wilsondebcadd2010-08-07 11:01:33 +01002524 intel_crtc->dpms_mode = mode;
Chris Wilsondebcadd2010-08-07 11:01:33 +01002525
Jesse Barnese70236a2009-09-21 10:42:27 -07002526 dev_priv->display.dpms(crtc, mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08002527
2528 if (!dev->primary->master)
2529 return;
2530
2531 master_priv = dev->primary->master->driver_priv;
2532 if (!master_priv->sarea_priv)
2533 return;
2534
2535 enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
2536
2537 switch (pipe) {
2538 case 0:
2539 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
2540 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
2541 break;
2542 case 1:
2543 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
2544 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
2545 break;
2546 default:
2547 DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
2548 break;
2549 }
Jesse Barnes79e53942008-11-07 14:24:08 -08002550}
2551
Chris Wilsoncdd59982010-09-08 16:30:16 +01002552static void intel_crtc_disable(struct drm_crtc *crtc)
2553{
2554 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
2555 struct drm_device *dev = crtc->dev;
2556
2557 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
2558
2559 if (crtc->fb) {
2560 mutex_lock(&dev->struct_mutex);
2561 i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
2562 mutex_unlock(&dev->struct_mutex);
2563 }
2564}
2565
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07002566/* Prepare for a mode set.
2567 *
2568 * Note we could be a lot smarter here. We need to figure out which outputs
2569 * will be enabled, which disabled (in short, how the config will changes)
2570 * and perform the minimum necessary steps to accomplish that, e.g. updating
2571 * watermarks, FBC configuration, making sure PLLs are programmed correctly,
2572 * panel fitting is in the proper state, etc.
2573 */
2574static void i9xx_crtc_prepare(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08002575{
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07002576 i9xx_crtc_disable(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08002577}
2578
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07002579static void i9xx_crtc_commit(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08002580{
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07002581 i9xx_crtc_enable(crtc);
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07002582}
2583
2584static void ironlake_crtc_prepare(struct drm_crtc *crtc)
2585{
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07002586 ironlake_crtc_disable(crtc);
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07002587}
2588
2589static void ironlake_crtc_commit(struct drm_crtc *crtc)
2590{
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07002591 ironlake_crtc_enable(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08002592}
2593
2594void intel_encoder_prepare (struct drm_encoder *encoder)
2595{
2596 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
2597 /* lvds has its own version of prepare see intel_lvds_prepare */
2598 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
2599}
2600
2601void intel_encoder_commit (struct drm_encoder *encoder)
2602{
2603 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
2604 /* lvds has its own version of commit see intel_lvds_commit */
2605 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
2606}
2607
Chris Wilsonea5b2132010-08-04 13:50:23 +01002608void intel_encoder_destroy(struct drm_encoder *encoder)
2609{
Chris Wilson4ef69c72010-09-09 15:14:28 +01002610 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01002611
Chris Wilsonea5b2132010-08-04 13:50:23 +01002612 drm_encoder_cleanup(encoder);
2613 kfree(intel_encoder);
2614}
2615
Jesse Barnes79e53942008-11-07 14:24:08 -08002616static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
2617 struct drm_display_mode *mode,
2618 struct drm_display_mode *adjusted_mode)
2619{
Zhenyu Wang2c072452009-06-05 15:38:42 +08002620 struct drm_device *dev = crtc->dev;
Chris Wilson89749352010-09-12 18:25:19 +01002621
Eric Anholtbad720f2009-10-22 16:11:14 -07002622 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08002623 /* FDI link clock is fixed at 2.7G */
Jesse Barnes2377b742010-07-07 14:06:43 -07002624 if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
2625 return false;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002626 }
Chris Wilson89749352010-09-12 18:25:19 +01002627
2628 /* XXX some encoders set the crtcinfo, others don't.
2629 * Obviously we need some form of conflict resolution here...
2630 */
2631 if (adjusted_mode->crtc_htotal == 0)
2632 drm_mode_set_crtcinfo(adjusted_mode, 0);
2633
Jesse Barnes79e53942008-11-07 14:24:08 -08002634 return true;
2635}
2636
Jesse Barnese70236a2009-09-21 10:42:27 -07002637static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08002638{
Jesse Barnese70236a2009-09-21 10:42:27 -07002639 return 400000;
2640}
Jesse Barnes79e53942008-11-07 14:24:08 -08002641
Jesse Barnese70236a2009-09-21 10:42:27 -07002642static int i915_get_display_clock_speed(struct drm_device *dev)
2643{
2644 return 333000;
2645}
Jesse Barnes79e53942008-11-07 14:24:08 -08002646
Jesse Barnese70236a2009-09-21 10:42:27 -07002647static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
2648{
2649 return 200000;
2650}
Jesse Barnes79e53942008-11-07 14:24:08 -08002651
Jesse Barnese70236a2009-09-21 10:42:27 -07002652static int i915gm_get_display_clock_speed(struct drm_device *dev)
2653{
2654 u16 gcfgc = 0;
2655
2656 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
2657
2658 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08002659 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07002660 else {
2661 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
2662 case GC_DISPLAY_CLOCK_333_MHZ:
2663 return 333000;
2664 default:
2665 case GC_DISPLAY_CLOCK_190_200_MHZ:
2666 return 190000;
2667 }
2668 }
2669}
Jesse Barnes79e53942008-11-07 14:24:08 -08002670
Jesse Barnese70236a2009-09-21 10:42:27 -07002671static int i865_get_display_clock_speed(struct drm_device *dev)
2672{
2673 return 266000;
2674}
2675
2676static int i855_get_display_clock_speed(struct drm_device *dev)
2677{
2678 u16 hpllcc = 0;
2679 /* Assume that the hardware is in the high speed state. This
2680 * should be the default.
2681 */
2682 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
2683 case GC_CLOCK_133_200:
2684 case GC_CLOCK_100_200:
2685 return 200000;
2686 case GC_CLOCK_166_250:
2687 return 250000;
2688 case GC_CLOCK_100_133:
2689 return 133000;
2690 }
2691
2692 /* Shouldn't happen */
2693 return 0;
2694}
2695
2696static int i830_get_display_clock_speed(struct drm_device *dev)
2697{
2698 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08002699}
2700
Zhenyu Wang2c072452009-06-05 15:38:42 +08002701struct fdi_m_n {
2702 u32 tu;
2703 u32 gmch_m;
2704 u32 gmch_n;
2705 u32 link_m;
2706 u32 link_n;
2707};
2708
2709static void
2710fdi_reduce_ratio(u32 *num, u32 *den)
2711{
2712 while (*num > 0xffffff || *den > 0xffffff) {
2713 *num >>= 1;
2714 *den >>= 1;
2715 }
2716}
2717
Zhenyu Wang2c072452009-06-05 15:38:42 +08002718static void
Adam Jacksonf2b115e2009-12-03 17:14:42 -05002719ironlake_compute_m_n(int bits_per_pixel, int nlanes, int pixel_clock,
2720 int link_clock, struct fdi_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08002721{
Zhenyu Wang2c072452009-06-05 15:38:42 +08002722 m_n->tu = 64; /* default size */
2723
Chris Wilson22ed1112010-12-04 01:01:29 +00002724 /* BUG_ON(pixel_clock > INT_MAX / 36); */
2725 m_n->gmch_m = bits_per_pixel * pixel_clock;
2726 m_n->gmch_n = link_clock * nlanes * 8;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002727 fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
2728
Chris Wilson22ed1112010-12-04 01:01:29 +00002729 m_n->link_m = pixel_clock;
2730 m_n->link_n = link_clock;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002731 fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
2732}
2733
2734
Shaohua Li7662c8b2009-06-26 11:23:55 +08002735struct intel_watermark_params {
2736 unsigned long fifo_size;
2737 unsigned long max_wm;
2738 unsigned long default_wm;
2739 unsigned long guard_size;
2740 unsigned long cacheline_size;
2741};
2742
Adam Jacksonf2b115e2009-12-03 17:14:42 -05002743/* Pineview has different values for various configs */
2744static struct intel_watermark_params pineview_display_wm = {
2745 PINEVIEW_DISPLAY_FIFO,
2746 PINEVIEW_MAX_WM,
2747 PINEVIEW_DFT_WM,
2748 PINEVIEW_GUARD_WM,
2749 PINEVIEW_FIFO_LINE_SIZE
Shaohua Li7662c8b2009-06-26 11:23:55 +08002750};
Adam Jacksonf2b115e2009-12-03 17:14:42 -05002751static struct intel_watermark_params pineview_display_hplloff_wm = {
2752 PINEVIEW_DISPLAY_FIFO,
2753 PINEVIEW_MAX_WM,
2754 PINEVIEW_DFT_HPLLOFF_WM,
2755 PINEVIEW_GUARD_WM,
2756 PINEVIEW_FIFO_LINE_SIZE
Shaohua Li7662c8b2009-06-26 11:23:55 +08002757};
Adam Jacksonf2b115e2009-12-03 17:14:42 -05002758static struct intel_watermark_params pineview_cursor_wm = {
2759 PINEVIEW_CURSOR_FIFO,
2760 PINEVIEW_CURSOR_MAX_WM,
2761 PINEVIEW_CURSOR_DFT_WM,
2762 PINEVIEW_CURSOR_GUARD_WM,
2763 PINEVIEW_FIFO_LINE_SIZE,
Shaohua Li7662c8b2009-06-26 11:23:55 +08002764};
Adam Jacksonf2b115e2009-12-03 17:14:42 -05002765static struct intel_watermark_params pineview_cursor_hplloff_wm = {
2766 PINEVIEW_CURSOR_FIFO,
2767 PINEVIEW_CURSOR_MAX_WM,
2768 PINEVIEW_CURSOR_DFT_WM,
2769 PINEVIEW_CURSOR_GUARD_WM,
2770 PINEVIEW_FIFO_LINE_SIZE
Shaohua Li7662c8b2009-06-26 11:23:55 +08002771};
Jesse Barnes0e442c62009-10-19 10:09:33 +09002772static struct intel_watermark_params g4x_wm_info = {
2773 G4X_FIFO_SIZE,
2774 G4X_MAX_WM,
2775 G4X_MAX_WM,
2776 2,
2777 G4X_FIFO_LINE_SIZE,
2778};
Zhao Yakui4fe5e612010-06-12 14:32:25 +08002779static struct intel_watermark_params g4x_cursor_wm_info = {
2780 I965_CURSOR_FIFO,
2781 I965_CURSOR_MAX_WM,
2782 I965_CURSOR_DFT_WM,
2783 2,
2784 G4X_FIFO_LINE_SIZE,
2785};
2786static struct intel_watermark_params i965_cursor_wm_info = {
2787 I965_CURSOR_FIFO,
2788 I965_CURSOR_MAX_WM,
2789 I965_CURSOR_DFT_WM,
2790 2,
2791 I915_FIFO_LINE_SIZE,
2792};
Shaohua Li7662c8b2009-06-26 11:23:55 +08002793static struct intel_watermark_params i945_wm_info = {
Shaohua Li7662c8b2009-06-26 11:23:55 +08002794 I945_FIFO_SIZE,
2795 I915_MAX_WM,
2796 1,
Jesse Barnesdff33cf2009-07-14 10:15:56 -07002797 2,
2798 I915_FIFO_LINE_SIZE
2799};
2800static struct intel_watermark_params i915_wm_info = {
2801 I915_FIFO_SIZE,
2802 I915_MAX_WM,
2803 1,
2804 2,
Shaohua Li7662c8b2009-06-26 11:23:55 +08002805 I915_FIFO_LINE_SIZE
2806};
2807static struct intel_watermark_params i855_wm_info = {
2808 I855GM_FIFO_SIZE,
2809 I915_MAX_WM,
2810 1,
Jesse Barnesdff33cf2009-07-14 10:15:56 -07002811 2,
Shaohua Li7662c8b2009-06-26 11:23:55 +08002812 I830_FIFO_LINE_SIZE
2813};
2814static struct intel_watermark_params i830_wm_info = {
2815 I830_FIFO_SIZE,
2816 I915_MAX_WM,
2817 1,
Jesse Barnesdff33cf2009-07-14 10:15:56 -07002818 2,
Shaohua Li7662c8b2009-06-26 11:23:55 +08002819 I830_FIFO_LINE_SIZE
2820};
2821
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08002822static struct intel_watermark_params ironlake_display_wm_info = {
2823 ILK_DISPLAY_FIFO,
2824 ILK_DISPLAY_MAXWM,
2825 ILK_DISPLAY_DFTWM,
2826 2,
2827 ILK_FIFO_LINE_SIZE
2828};
2829
Zhao Yakuic936f442010-06-12 14:32:26 +08002830static struct intel_watermark_params ironlake_cursor_wm_info = {
2831 ILK_CURSOR_FIFO,
2832 ILK_CURSOR_MAXWM,
2833 ILK_CURSOR_DFTWM,
2834 2,
2835 ILK_FIFO_LINE_SIZE
2836};
2837
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08002838static struct intel_watermark_params ironlake_display_srwm_info = {
2839 ILK_DISPLAY_SR_FIFO,
2840 ILK_DISPLAY_MAX_SRWM,
2841 ILK_DISPLAY_DFT_SRWM,
2842 2,
2843 ILK_FIFO_LINE_SIZE
2844};
2845
2846static struct intel_watermark_params ironlake_cursor_srwm_info = {
2847 ILK_CURSOR_SR_FIFO,
2848 ILK_CURSOR_MAX_SRWM,
2849 ILK_CURSOR_DFT_SRWM,
2850 2,
2851 ILK_FIFO_LINE_SIZE
2852};
2853
Yuanhan Liu13982612010-12-15 15:42:31 +08002854static struct intel_watermark_params sandybridge_display_wm_info = {
2855 SNB_DISPLAY_FIFO,
2856 SNB_DISPLAY_MAXWM,
2857 SNB_DISPLAY_DFTWM,
2858 2,
2859 SNB_FIFO_LINE_SIZE
2860};
2861
2862static struct intel_watermark_params sandybridge_cursor_wm_info = {
2863 SNB_CURSOR_FIFO,
2864 SNB_CURSOR_MAXWM,
2865 SNB_CURSOR_DFTWM,
2866 2,
2867 SNB_FIFO_LINE_SIZE
2868};
2869
2870static struct intel_watermark_params sandybridge_display_srwm_info = {
2871 SNB_DISPLAY_SR_FIFO,
2872 SNB_DISPLAY_MAX_SRWM,
2873 SNB_DISPLAY_DFT_SRWM,
2874 2,
2875 SNB_FIFO_LINE_SIZE
2876};
2877
2878static struct intel_watermark_params sandybridge_cursor_srwm_info = {
2879 SNB_CURSOR_SR_FIFO,
2880 SNB_CURSOR_MAX_SRWM,
2881 SNB_CURSOR_DFT_SRWM,
2882 2,
2883 SNB_FIFO_LINE_SIZE
2884};
2885
2886
Jesse Barnesdff33cf2009-07-14 10:15:56 -07002887/**
2888 * intel_calculate_wm - calculate watermark level
2889 * @clock_in_khz: pixel clock
2890 * @wm: chip FIFO params
2891 * @pixel_size: display pixel size
2892 * @latency_ns: memory latency for the platform
2893 *
2894 * Calculate the watermark level (the level at which the display plane will
2895 * start fetching from memory again). Each chip has a different display
2896 * FIFO size and allocation, so the caller needs to figure that out and pass
2897 * in the correct intel_watermark_params structure.
2898 *
2899 * As the pixel clock runs, the FIFO will be drained at a rate that depends
2900 * on the pixel size. When it reaches the watermark level, it'll start
2901 * fetching FIFO line sized based chunks from memory until the FIFO fills
2902 * past the watermark point. If the FIFO drains completely, a FIFO underrun
2903 * will occur, and a display engine hang could result.
2904 */
Shaohua Li7662c8b2009-06-26 11:23:55 +08002905static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
2906 struct intel_watermark_params *wm,
2907 int pixel_size,
2908 unsigned long latency_ns)
2909{
Jesse Barnes390c4dd2009-07-16 13:01:01 -07002910 long entries_required, wm_size;
Shaohua Li7662c8b2009-06-26 11:23:55 +08002911
Jesse Barnesd6604672009-09-11 12:25:56 -07002912 /*
2913 * Note: we need to make sure we don't overflow for various clock &
2914 * latency values.
2915 * clocks go from a few thousand to several hundred thousand.
2916 * latency is usually a few thousand
2917 */
2918 entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
2919 1000;
Chris Wilson8de9b312010-07-19 19:59:52 +01002920 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
Jesse Barnesdff33cf2009-07-14 10:15:56 -07002921
Zhao Yakui28c97732009-10-09 11:39:41 +08002922 DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries_required);
Jesse Barnesdff33cf2009-07-14 10:15:56 -07002923
2924 wm_size = wm->fifo_size - (entries_required + wm->guard_size);
2925
Zhao Yakui28c97732009-10-09 11:39:41 +08002926 DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
Shaohua Li7662c8b2009-06-26 11:23:55 +08002927
Jesse Barnes390c4dd2009-07-16 13:01:01 -07002928 /* Don't promote wm_size to unsigned... */
2929 if (wm_size > (long)wm->max_wm)
Shaohua Li7662c8b2009-06-26 11:23:55 +08002930 wm_size = wm->max_wm;
Chris Wilsonc3add4b2010-09-08 09:14:08 +01002931 if (wm_size <= 0)
Shaohua Li7662c8b2009-06-26 11:23:55 +08002932 wm_size = wm->default_wm;
2933 return wm_size;
2934}
2935
2936struct cxsr_latency {
2937 int is_desktop;
Li Peng95534262010-05-18 18:58:44 +08002938 int is_ddr3;
Shaohua Li7662c8b2009-06-26 11:23:55 +08002939 unsigned long fsb_freq;
2940 unsigned long mem_freq;
2941 unsigned long display_sr;
2942 unsigned long display_hpll_disable;
2943 unsigned long cursor_sr;
2944 unsigned long cursor_hpll_disable;
2945};
2946
Chris Wilson403c89f2010-08-04 15:25:31 +01002947static const struct cxsr_latency cxsr_latency_table[] = {
Li Peng95534262010-05-18 18:58:44 +08002948 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
2949 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
2950 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
2951 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
2952 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08002953
Li Peng95534262010-05-18 18:58:44 +08002954 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
2955 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
2956 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
2957 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
2958 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08002959
Li Peng95534262010-05-18 18:58:44 +08002960 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
2961 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
2962 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
2963 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
2964 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08002965
Li Peng95534262010-05-18 18:58:44 +08002966 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
2967 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
2968 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
2969 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
2970 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08002971
Li Peng95534262010-05-18 18:58:44 +08002972 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
2973 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
2974 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
2975 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
2976 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08002977
Li Peng95534262010-05-18 18:58:44 +08002978 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
2979 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
2980 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
2981 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
2982 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08002983};
2984
Chris Wilson403c89f2010-08-04 15:25:31 +01002985static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
2986 int is_ddr3,
2987 int fsb,
2988 int mem)
Shaohua Li7662c8b2009-06-26 11:23:55 +08002989{
Chris Wilson403c89f2010-08-04 15:25:31 +01002990 const struct cxsr_latency *latency;
Shaohua Li7662c8b2009-06-26 11:23:55 +08002991 int i;
Shaohua Li7662c8b2009-06-26 11:23:55 +08002992
2993 if (fsb == 0 || mem == 0)
2994 return NULL;
2995
2996 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
2997 latency = &cxsr_latency_table[i];
2998 if (is_desktop == latency->is_desktop &&
Li Peng95534262010-05-18 18:58:44 +08002999 is_ddr3 == latency->is_ddr3 &&
Jaswinder Singh Rajputdecbbcd2009-09-12 23:15:07 +05303000 fsb == latency->fsb_freq && mem == latency->mem_freq)
3001 return latency;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003002 }
Jaswinder Singh Rajputdecbbcd2009-09-12 23:15:07 +05303003
Zhao Yakui28c97732009-10-09 11:39:41 +08003004 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Jaswinder Singh Rajputdecbbcd2009-09-12 23:15:07 +05303005
3006 return NULL;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003007}
3008
Adam Jacksonf2b115e2009-12-03 17:14:42 -05003009static void pineview_disable_cxsr(struct drm_device *dev)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003010{
3011 struct drm_i915_private *dev_priv = dev->dev_private;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003012
3013 /* deactivate cxsr */
Chris Wilson3e33d942010-08-04 11:17:25 +01003014 I915_WRITE(DSPFW3, I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003015}
3016
Jesse Barnesbcc24fb2009-08-31 10:24:31 -07003017/*
3018 * Latency for FIFO fetches is dependent on several factors:
3019 * - memory configuration (speed, channels)
3020 * - chipset
3021 * - current MCH state
3022 * It can be fairly high in some situations, so here we assume a fairly
3023 * pessimal value. It's a tradeoff between extra memory fetches (if we
3024 * set this value too high, the FIFO will fetch frequently to stay full)
3025 * and power consumption (set it too low to save power and we might see
3026 * FIFO underruns and display "flicker").
3027 *
3028 * A value of 5us seems to be a good balance; safe for very low end
3029 * platforms but not overly aggressive on lower latency configs.
3030 */
Tobias Klauser69e302a2009-12-23 14:14:34 +01003031static const int latency_ns = 5000;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003032
Jesse Barnese70236a2009-09-21 10:42:27 -07003033static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003034{
3035 struct drm_i915_private *dev_priv = dev->dev_private;
3036 uint32_t dsparb = I915_READ(DSPARB);
3037 int size;
3038
Chris Wilson8de9b312010-07-19 19:59:52 +01003039 size = dsparb & 0x7f;
3040 if (plane)
3041 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003042
Zhao Yakui28c97732009-10-09 11:39:41 +08003043 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
Chris Wilson5eddb702010-09-11 13:48:45 +01003044 plane ? "B" : "A", size);
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003045
3046 return size;
3047}
Shaohua Li7662c8b2009-06-26 11:23:55 +08003048
Jesse Barnese70236a2009-09-21 10:42:27 -07003049static int i85x_get_fifo_size(struct drm_device *dev, int plane)
3050{
3051 struct drm_i915_private *dev_priv = dev->dev_private;
3052 uint32_t dsparb = I915_READ(DSPARB);
3053 int size;
3054
Chris Wilson8de9b312010-07-19 19:59:52 +01003055 size = dsparb & 0x1ff;
3056 if (plane)
3057 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
Jesse Barnese70236a2009-09-21 10:42:27 -07003058 size >>= 1; /* Convert to cachelines */
3059
Zhao Yakui28c97732009-10-09 11:39:41 +08003060 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
Chris Wilson5eddb702010-09-11 13:48:45 +01003061 plane ? "B" : "A", size);
Jesse Barnese70236a2009-09-21 10:42:27 -07003062
3063 return size;
3064}
3065
3066static int i845_get_fifo_size(struct drm_device *dev, int plane)
3067{
3068 struct drm_i915_private *dev_priv = dev->dev_private;
3069 uint32_t dsparb = I915_READ(DSPARB);
3070 int size;
3071
3072 size = dsparb & 0x7f;
3073 size >>= 2; /* Convert to cachelines */
3074
Zhao Yakui28c97732009-10-09 11:39:41 +08003075 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
Chris Wilson5eddb702010-09-11 13:48:45 +01003076 plane ? "B" : "A",
3077 size);
Jesse Barnese70236a2009-09-21 10:42:27 -07003078
3079 return size;
3080}
3081
3082static int i830_get_fifo_size(struct drm_device *dev, int plane)
3083{
3084 struct drm_i915_private *dev_priv = dev->dev_private;
3085 uint32_t dsparb = I915_READ(DSPARB);
3086 int size;
3087
3088 size = dsparb & 0x7f;
3089 size >>= 1; /* Convert to cachelines */
3090
Zhao Yakui28c97732009-10-09 11:39:41 +08003091 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
Chris Wilson5eddb702010-09-11 13:48:45 +01003092 plane ? "B" : "A", size);
Jesse Barnese70236a2009-09-21 10:42:27 -07003093
3094 return size;
3095}
3096
Zhao Yakuid4294342010-03-22 22:45:36 +08003097static void pineview_update_wm(struct drm_device *dev, int planea_clock,
Chris Wilson5eddb702010-09-11 13:48:45 +01003098 int planeb_clock, int sr_hdisplay, int unused,
3099 int pixel_size)
Zhao Yakuid4294342010-03-22 22:45:36 +08003100{
3101 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson403c89f2010-08-04 15:25:31 +01003102 const struct cxsr_latency *latency;
Zhao Yakuid4294342010-03-22 22:45:36 +08003103 u32 reg;
3104 unsigned long wm;
Zhao Yakuid4294342010-03-22 22:45:36 +08003105 int sr_clock;
3106
Chris Wilson403c89f2010-08-04 15:25:31 +01003107 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
Li Peng95534262010-05-18 18:58:44 +08003108 dev_priv->fsb_freq, dev_priv->mem_freq);
Zhao Yakuid4294342010-03-22 22:45:36 +08003109 if (!latency) {
3110 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
3111 pineview_disable_cxsr(dev);
3112 return;
3113 }
3114
3115 if (!planea_clock || !planeb_clock) {
3116 sr_clock = planea_clock ? planea_clock : planeb_clock;
3117
3118 /* Display SR */
3119 wm = intel_calculate_wm(sr_clock, &pineview_display_wm,
3120 pixel_size, latency->display_sr);
3121 reg = I915_READ(DSPFW1);
3122 reg &= ~DSPFW_SR_MASK;
3123 reg |= wm << DSPFW_SR_SHIFT;
3124 I915_WRITE(DSPFW1, reg);
3125 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
3126
3127 /* cursor SR */
3128 wm = intel_calculate_wm(sr_clock, &pineview_cursor_wm,
3129 pixel_size, latency->cursor_sr);
3130 reg = I915_READ(DSPFW3);
3131 reg &= ~DSPFW_CURSOR_SR_MASK;
3132 reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
3133 I915_WRITE(DSPFW3, reg);
3134
3135 /* Display HPLL off SR */
3136 wm = intel_calculate_wm(sr_clock, &pineview_display_hplloff_wm,
3137 pixel_size, latency->display_hpll_disable);
3138 reg = I915_READ(DSPFW3);
3139 reg &= ~DSPFW_HPLL_SR_MASK;
3140 reg |= wm & DSPFW_HPLL_SR_MASK;
3141 I915_WRITE(DSPFW3, reg);
3142
3143 /* cursor HPLL off SR */
3144 wm = intel_calculate_wm(sr_clock, &pineview_cursor_hplloff_wm,
3145 pixel_size, latency->cursor_hpll_disable);
3146 reg = I915_READ(DSPFW3);
3147 reg &= ~DSPFW_HPLL_CURSOR_MASK;
3148 reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
3149 I915_WRITE(DSPFW3, reg);
3150 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
3151
3152 /* activate cxsr */
Chris Wilson3e33d942010-08-04 11:17:25 +01003153 I915_WRITE(DSPFW3,
3154 I915_READ(DSPFW3) | PINEVIEW_SELF_REFRESH_EN);
Zhao Yakuid4294342010-03-22 22:45:36 +08003155 DRM_DEBUG_KMS("Self-refresh is enabled\n");
3156 } else {
3157 pineview_disable_cxsr(dev);
3158 DRM_DEBUG_KMS("Self-refresh is disabled\n");
3159 }
3160}
3161
Jesse Barnes0e442c62009-10-19 10:09:33 +09003162static void g4x_update_wm(struct drm_device *dev, int planea_clock,
Zhao Yakuifa143212010-06-12 14:32:23 +08003163 int planeb_clock, int sr_hdisplay, int sr_htotal,
3164 int pixel_size)
Jesse Barnes652c3932009-08-17 13:31:43 -07003165{
3166 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e442c62009-10-19 10:09:33 +09003167 int total_size, cacheline_size;
3168 int planea_wm, planeb_wm, cursora_wm, cursorb_wm, cursor_sr;
3169 struct intel_watermark_params planea_params, planeb_params;
3170 unsigned long line_time_us;
3171 int sr_clock, sr_entries = 0, entries_required;
Jesse Barnes652c3932009-08-17 13:31:43 -07003172
Jesse Barnes0e442c62009-10-19 10:09:33 +09003173 /* Create copies of the base settings for each pipe */
3174 planea_params = planeb_params = g4x_wm_info;
3175
3176 /* Grab a couple of global values before we overwrite them */
3177 total_size = planea_params.fifo_size;
3178 cacheline_size = planea_params.cacheline_size;
3179
3180 /*
3181 * Note: we need to make sure we don't overflow for various clock &
3182 * latency values.
3183 * clocks go from a few thousand to several hundred thousand.
3184 * latency is usually a few thousand
3185 */
3186 entries_required = ((planea_clock / 1000) * pixel_size * latency_ns) /
3187 1000;
Chris Wilson8de9b312010-07-19 19:59:52 +01003188 entries_required = DIV_ROUND_UP(entries_required, G4X_FIFO_LINE_SIZE);
Jesse Barnes0e442c62009-10-19 10:09:33 +09003189 planea_wm = entries_required + planea_params.guard_size;
3190
3191 entries_required = ((planeb_clock / 1000) * pixel_size * latency_ns) /
3192 1000;
Chris Wilson8de9b312010-07-19 19:59:52 +01003193 entries_required = DIV_ROUND_UP(entries_required, G4X_FIFO_LINE_SIZE);
Jesse Barnes0e442c62009-10-19 10:09:33 +09003194 planeb_wm = entries_required + planeb_params.guard_size;
3195
3196 cursora_wm = cursorb_wm = 16;
3197 cursor_sr = 32;
3198
3199 DRM_DEBUG("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
3200
3201 /* Calc sr entries for one plane configs */
3202 if (sr_hdisplay && (!planea_clock || !planeb_clock)) {
3203 /* self-refresh has much higher latency */
Tobias Klauser69e302a2009-12-23 14:14:34 +01003204 static const int sr_latency_ns = 12000;
Jesse Barnes0e442c62009-10-19 10:09:33 +09003205
3206 sr_clock = planea_clock ? planea_clock : planeb_clock;
Zhao Yakuifa143212010-06-12 14:32:23 +08003207 line_time_us = ((sr_htotal * 1000) / sr_clock);
Jesse Barnes0e442c62009-10-19 10:09:33 +09003208
3209 /* Use ns/us then divide to preserve precision */
Zhao Yakuifa143212010-06-12 14:32:23 +08003210 sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Chris Wilson5eddb702010-09-11 13:48:45 +01003211 pixel_size * sr_hdisplay;
Chris Wilson8de9b312010-07-19 19:59:52 +01003212 sr_entries = DIV_ROUND_UP(sr_entries, cacheline_size);
Zhao Yakui4fe5e612010-06-12 14:32:25 +08003213
3214 entries_required = (((sr_latency_ns / line_time_us) +
3215 1000) / 1000) * pixel_size * 64;
Chris Wilson8de9b312010-07-19 19:59:52 +01003216 entries_required = DIV_ROUND_UP(entries_required,
Chris Wilson5eddb702010-09-11 13:48:45 +01003217 g4x_cursor_wm_info.cacheline_size);
Zhao Yakui4fe5e612010-06-12 14:32:25 +08003218 cursor_sr = entries_required + g4x_cursor_wm_info.guard_size;
3219
3220 if (cursor_sr > g4x_cursor_wm_info.max_wm)
3221 cursor_sr = g4x_cursor_wm_info.max_wm;
3222 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
3223 "cursor %d\n", sr_entries, cursor_sr);
3224
Jesse Barnes0e442c62009-10-19 10:09:33 +09003225 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
David John33c5fd12010-01-27 15:19:08 +05303226 } else {
3227 /* Turn off self refresh if both pipes are enabled */
3228 I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
Chris Wilson5eddb702010-09-11 13:48:45 +01003229 & ~FW_BLC_SELF_EN);
Jesse Barnes0e442c62009-10-19 10:09:33 +09003230 }
3231
3232 DRM_DEBUG("Setting FIFO watermarks - A: %d, B: %d, SR %d\n",
3233 planea_wm, planeb_wm, sr_entries);
3234
3235 planea_wm &= 0x3f;
3236 planeb_wm &= 0x3f;
3237
3238 I915_WRITE(DSPFW1, (sr_entries << DSPFW_SR_SHIFT) |
3239 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
3240 (planeb_wm << DSPFW_PLANEB_SHIFT) | planea_wm);
3241 I915_WRITE(DSPFW2, (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
3242 (cursora_wm << DSPFW_CURSORA_SHIFT));
3243 /* HPLL off in SR has some issues on G4x... disable it */
3244 I915_WRITE(DSPFW3, (I915_READ(DSPFW3) & ~DSPFW_HPLL_SR_EN) |
3245 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Jesse Barnes652c3932009-08-17 13:31:43 -07003246}
3247
Jesse Barnes1dc75462009-10-19 10:08:17 +09003248static void i965_update_wm(struct drm_device *dev, int planea_clock,
Zhao Yakuifa143212010-06-12 14:32:23 +08003249 int planeb_clock, int sr_hdisplay, int sr_htotal,
3250 int pixel_size)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003251{
3252 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1dc75462009-10-19 10:08:17 +09003253 unsigned long line_time_us;
3254 int sr_clock, sr_entries, srwm = 1;
Zhao Yakui4fe5e612010-06-12 14:32:25 +08003255 int cursor_sr = 16;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003256
Jesse Barnes1dc75462009-10-19 10:08:17 +09003257 /* Calc sr entries for one plane configs */
3258 if (sr_hdisplay && (!planea_clock || !planeb_clock)) {
3259 /* self-refresh has much higher latency */
Tobias Klauser69e302a2009-12-23 14:14:34 +01003260 static const int sr_latency_ns = 12000;
Jesse Barnes1dc75462009-10-19 10:08:17 +09003261
3262 sr_clock = planea_clock ? planea_clock : planeb_clock;
Zhao Yakuifa143212010-06-12 14:32:23 +08003263 line_time_us = ((sr_htotal * 1000) / sr_clock);
Jesse Barnes1dc75462009-10-19 10:08:17 +09003264
3265 /* Use ns/us then divide to preserve precision */
Zhao Yakuifa143212010-06-12 14:32:23 +08003266 sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Chris Wilson5eddb702010-09-11 13:48:45 +01003267 pixel_size * sr_hdisplay;
Chris Wilson8de9b312010-07-19 19:59:52 +01003268 sr_entries = DIV_ROUND_UP(sr_entries, I915_FIFO_LINE_SIZE);
Jesse Barnes1dc75462009-10-19 10:08:17 +09003269 DRM_DEBUG("self-refresh entries: %d\n", sr_entries);
Zhao Yakui1b07e042010-06-12 14:32:24 +08003270 srwm = I965_FIFO_SIZE - sr_entries;
Jesse Barnes1dc75462009-10-19 10:08:17 +09003271 if (srwm < 0)
3272 srwm = 1;
Zhao Yakui1b07e042010-06-12 14:32:24 +08003273 srwm &= 0x1ff;
Zhao Yakui4fe5e612010-06-12 14:32:25 +08003274
3275 sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Chris Wilson5eddb702010-09-11 13:48:45 +01003276 pixel_size * 64;
Chris Wilson8de9b312010-07-19 19:59:52 +01003277 sr_entries = DIV_ROUND_UP(sr_entries,
3278 i965_cursor_wm_info.cacheline_size);
Zhao Yakui4fe5e612010-06-12 14:32:25 +08003279 cursor_sr = i965_cursor_wm_info.fifo_size -
Chris Wilson5eddb702010-09-11 13:48:45 +01003280 (sr_entries + i965_cursor_wm_info.guard_size);
Zhao Yakui4fe5e612010-06-12 14:32:25 +08003281
3282 if (cursor_sr > i965_cursor_wm_info.max_wm)
3283 cursor_sr = i965_cursor_wm_info.max_wm;
3284
3285 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
3286 "cursor %d\n", srwm, cursor_sr);
3287
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003288 if (IS_CRESTLINE(dev))
Jesse Barnesadcdbc62010-06-30 13:49:37 -07003289 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
David John33c5fd12010-01-27 15:19:08 +05303290 } else {
3291 /* Turn off self refresh if both pipes are enabled */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003292 if (IS_CRESTLINE(dev))
Jesse Barnesadcdbc62010-06-30 13:49:37 -07003293 I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
3294 & ~FW_BLC_SELF_EN);
Jesse Barnes1dc75462009-10-19 10:08:17 +09003295 }
3296
3297 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
3298 srwm);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003299
3300 /* 965 has limitations... */
Jesse Barnes1dc75462009-10-19 10:08:17 +09003301 I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) | (8 << 16) | (8 << 8) |
3302 (8 << 0));
Shaohua Li7662c8b2009-06-26 11:23:55 +08003303 I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
Zhao Yakui4fe5e612010-06-12 14:32:25 +08003304 /* update cursor SR watermark */
3305 I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Shaohua Li7662c8b2009-06-26 11:23:55 +08003306}
3307
3308static void i9xx_update_wm(struct drm_device *dev, int planea_clock,
Zhao Yakuifa143212010-06-12 14:32:23 +08003309 int planeb_clock, int sr_hdisplay, int sr_htotal,
3310 int pixel_size)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003311{
3312 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003313 uint32_t fwater_lo;
3314 uint32_t fwater_hi;
3315 int total_size, cacheline_size, cwm, srwm = 1;
3316 int planea_wm, planeb_wm;
3317 struct intel_watermark_params planea_params, planeb_params;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003318 unsigned long line_time_us;
3319 int sr_clock, sr_entries = 0;
3320
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003321 /* Create copies of the base settings for each pipe */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003322 if (IS_CRESTLINE(dev) || IS_I945GM(dev))
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003323 planea_params = planeb_params = i945_wm_info;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003324 else if (!IS_GEN2(dev))
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003325 planea_params = planeb_params = i915_wm_info;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003326 else
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003327 planea_params = planeb_params = i855_wm_info;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003328
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003329 /* Grab a couple of global values before we overwrite them */
3330 total_size = planea_params.fifo_size;
3331 cacheline_size = planea_params.cacheline_size;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003332
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003333 /* Update per-plane FIFO sizes */
Jesse Barnese70236a2009-09-21 10:42:27 -07003334 planea_params.fifo_size = dev_priv->display.get_fifo_size(dev, 0);
3335 planeb_params.fifo_size = dev_priv->display.get_fifo_size(dev, 1);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003336
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003337 planea_wm = intel_calculate_wm(planea_clock, &planea_params,
3338 pixel_size, latency_ns);
3339 planeb_wm = intel_calculate_wm(planeb_clock, &planeb_params,
3340 pixel_size, latency_ns);
Zhao Yakui28c97732009-10-09 11:39:41 +08003341 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003342
3343 /*
3344 * Overlay gets an aggressive default since video jitter is bad.
3345 */
3346 cwm = 2;
3347
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003348 /* Calc sr entries for one plane configs */
Jesse Barnes652c3932009-08-17 13:31:43 -07003349 if (HAS_FW_BLC(dev) && sr_hdisplay &&
3350 (!planea_clock || !planeb_clock)) {
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003351 /* self-refresh has much higher latency */
Tobias Klauser69e302a2009-12-23 14:14:34 +01003352 static const int sr_latency_ns = 6000;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003353
Shaohua Li7662c8b2009-06-26 11:23:55 +08003354 sr_clock = planea_clock ? planea_clock : planeb_clock;
Zhao Yakuifa143212010-06-12 14:32:23 +08003355 line_time_us = ((sr_htotal * 1000) / sr_clock);
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003356
3357 /* Use ns/us then divide to preserve precision */
Zhao Yakuifa143212010-06-12 14:32:23 +08003358 sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Chris Wilson5eddb702010-09-11 13:48:45 +01003359 pixel_size * sr_hdisplay;
Chris Wilson8de9b312010-07-19 19:59:52 +01003360 sr_entries = DIV_ROUND_UP(sr_entries, cacheline_size);
Zhao Yakui28c97732009-10-09 11:39:41 +08003361 DRM_DEBUG_KMS("self-refresh entries: %d\n", sr_entries);
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003362 srwm = total_size - sr_entries;
3363 if (srwm < 0)
3364 srwm = 1;
Li Pengee980b82010-01-27 19:01:11 +08003365
3366 if (IS_I945G(dev) || IS_I945GM(dev))
3367 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
3368 else if (IS_I915GM(dev)) {
3369 /* 915M has a smaller SRWM field */
3370 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
3371 I915_WRITE(INSTPM, I915_READ(INSTPM) | INSTPM_SELF_EN);
3372 }
David John33c5fd12010-01-27 15:19:08 +05303373 } else {
3374 /* Turn off self refresh if both pipes are enabled */
Li Pengee980b82010-01-27 19:01:11 +08003375 if (IS_I945G(dev) || IS_I945GM(dev)) {
3376 I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
3377 & ~FW_BLC_SELF_EN);
3378 } else if (IS_I915GM(dev)) {
3379 I915_WRITE(INSTPM, I915_READ(INSTPM) & ~INSTPM_SELF_EN);
3380 }
Shaohua Li7662c8b2009-06-26 11:23:55 +08003381 }
3382
Zhao Yakui28c97732009-10-09 11:39:41 +08003383 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
Chris Wilson5eddb702010-09-11 13:48:45 +01003384 planea_wm, planeb_wm, cwm, srwm);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003385
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003386 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
3387 fwater_hi = (cwm & 0x1f);
3388
3389 /* Set request length to 8 cachelines per fetch */
3390 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
3391 fwater_hi = fwater_hi | (1 << 8);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003392
3393 I915_WRITE(FW_BLC, fwater_lo);
3394 I915_WRITE(FW_BLC2, fwater_hi);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003395}
3396
Jesse Barnese70236a2009-09-21 10:42:27 -07003397static void i830_update_wm(struct drm_device *dev, int planea_clock, int unused,
Zhao Yakuifa143212010-06-12 14:32:23 +08003398 int unused2, int unused3, int pixel_size)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003399{
3400 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf3601322009-07-22 12:54:59 -07003401 uint32_t fwater_lo = I915_READ(FW_BLC) & ~0xfff;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003402 int planea_wm;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003403
Jesse Barnese70236a2009-09-21 10:42:27 -07003404 i830_wm_info.fifo_size = dev_priv->display.get_fifo_size(dev, 0);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003405
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003406 planea_wm = intel_calculate_wm(planea_clock, &i830_wm_info,
3407 pixel_size, latency_ns);
Jesse Barnesf3601322009-07-22 12:54:59 -07003408 fwater_lo |= (3<<8) | planea_wm;
3409
Zhao Yakui28c97732009-10-09 11:39:41 +08003410 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003411
3412 I915_WRITE(FW_BLC, fwater_lo);
3413}
3414
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003415#define ILK_LP0_PLANE_LATENCY 700
Zhao Yakuic936f442010-06-12 14:32:26 +08003416#define ILK_LP0_CURSOR_LATENCY 1300
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003417
Chris Wilson4ed765f2010-09-11 10:46:47 +01003418static bool ironlake_compute_wm0(struct drm_device *dev,
3419 int pipe,
Yuanhan Liu13982612010-12-15 15:42:31 +08003420 const struct intel_watermark_params *display,
3421 int display_latency,
3422 const struct intel_watermark_params *cursor,
3423 int cursor_latency,
Chris Wilson4ed765f2010-09-11 10:46:47 +01003424 int *plane_wm,
3425 int *cursor_wm)
3426{
3427 struct drm_crtc *crtc;
3428 int htotal, hdisplay, clock, pixel_size = 0;
3429 int line_time_us, line_count, entries;
3430
3431 crtc = intel_get_crtc_for_pipe(dev, pipe);
3432 if (crtc->fb == NULL || !crtc->enabled)
3433 return false;
3434
3435 htotal = crtc->mode.htotal;
3436 hdisplay = crtc->mode.hdisplay;
3437 clock = crtc->mode.clock;
3438 pixel_size = crtc->fb->bits_per_pixel / 8;
3439
3440 /* Use the small buffer method to calculate plane watermark */
Yuanhan Liu13982612010-12-15 15:42:31 +08003441 entries = ((clock * pixel_size / 1000) * display_latency * 100) / 1000;
3442 entries = DIV_ROUND_UP(entries, display->cacheline_size);
3443 *plane_wm = entries + display->guard_size;
3444 if (*plane_wm > (int)display->max_wm)
3445 *plane_wm = display->max_wm;
Chris Wilson4ed765f2010-09-11 10:46:47 +01003446
3447 /* Use the large buffer method to calculate cursor watermark */
3448 line_time_us = ((htotal * 1000) / clock);
Yuanhan Liu13982612010-12-15 15:42:31 +08003449 line_count = (cursor_latency * 100 / line_time_us + 1000) / 1000;
Chris Wilson4ed765f2010-09-11 10:46:47 +01003450 entries = line_count * 64 * pixel_size;
Yuanhan Liu13982612010-12-15 15:42:31 +08003451 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
3452 *cursor_wm = entries + cursor->guard_size;
3453 if (*cursor_wm > (int)cursor->max_wm)
3454 *cursor_wm = (int)cursor->max_wm;
Chris Wilson4ed765f2010-09-11 10:46:47 +01003455
3456 return true;
3457}
3458
Jesse Barnesb79d4992010-12-21 13:10:23 -08003459/*
3460 * Check the wm result.
3461 *
3462 * If any calculated watermark values is larger than the maximum value that
3463 * can be programmed into the associated watermark register, that watermark
3464 * must be disabled.
3465 */
3466static bool ironlake_check_srwm(struct drm_device *dev, int level,
3467 int fbc_wm, int display_wm, int cursor_wm,
3468 const struct intel_watermark_params *display,
3469 const struct intel_watermark_params *cursor)
3470{
3471 struct drm_i915_private *dev_priv = dev->dev_private;
3472
3473 DRM_DEBUG_KMS("watermark %d: display plane %d, fbc lines %d,"
3474 " cursor %d\n", level, display_wm, fbc_wm, cursor_wm);
3475
3476 if (fbc_wm > SNB_FBC_MAX_SRWM) {
3477 DRM_DEBUG_KMS("fbc watermark(%d) is too large(%d), disabling wm%d+\n",
3478 fbc_wm, SNB_FBC_MAX_SRWM, level);
3479
3480 /* fbc has it's own way to disable FBC WM */
3481 I915_WRITE(DISP_ARB_CTL,
3482 I915_READ(DISP_ARB_CTL) | DISP_FBC_WM_DIS);
3483 return false;
3484 }
3485
3486 if (display_wm > display->max_wm) {
3487 DRM_DEBUG_KMS("display watermark(%d) is too large(%d), disabling wm%d+\n",
3488 display_wm, SNB_DISPLAY_MAX_SRWM, level);
3489 return false;
3490 }
3491
3492 if (cursor_wm > cursor->max_wm) {
3493 DRM_DEBUG_KMS("cursor watermark(%d) is too large(%d), disabling wm%d+\n",
3494 cursor_wm, SNB_CURSOR_MAX_SRWM, level);
3495 return false;
3496 }
3497
3498 if (!(fbc_wm || display_wm || cursor_wm)) {
3499 DRM_DEBUG_KMS("latency %d is 0, disabling wm%d+\n", level, level);
3500 return false;
3501 }
3502
3503 return true;
3504}
3505
3506/*
3507 * Compute watermark values of WM[1-3],
3508 */
3509static bool ironlake_compute_srwm(struct drm_device *dev, int level,
3510 int hdisplay, int htotal,
3511 int pixel_size, int clock, int latency_ns,
3512 const struct intel_watermark_params *display,
3513 const struct intel_watermark_params *cursor,
3514 int *fbc_wm, int *display_wm, int *cursor_wm)
3515{
3516
3517 unsigned long line_time_us;
3518 int line_count, line_size;
3519 int small, large;
3520 int entries;
3521
3522 if (!latency_ns) {
3523 *fbc_wm = *display_wm = *cursor_wm = 0;
3524 return false;
3525 }
3526
3527 line_time_us = (htotal * 1000) / clock;
3528 line_count = (latency_ns / line_time_us + 1000) / 1000;
3529 line_size = hdisplay * pixel_size;
3530
3531 /* Use the minimum of the small and large buffer method for primary */
3532 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
3533 large = line_count * line_size;
3534
3535 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
3536 *display_wm = entries + display->guard_size;
3537
3538 /*
3539 * Spec says:
3540 * FBC WM = ((Final Primary WM * 64) / number of bytes per line) + 2
3541 */
3542 *fbc_wm = DIV_ROUND_UP(*display_wm * 64, line_size) + 2;
3543
3544 /* calculate the self-refresh watermark for display cursor */
3545 entries = line_count * pixel_size * 64;
3546 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
3547 *cursor_wm = entries + cursor->guard_size;
3548
3549 return ironlake_check_srwm(dev, level,
3550 *fbc_wm, *display_wm, *cursor_wm,
3551 display, cursor);
3552}
3553
Chris Wilson4ed765f2010-09-11 10:46:47 +01003554static void ironlake_update_wm(struct drm_device *dev,
3555 int planea_clock, int planeb_clock,
Jesse Barnesb79d4992010-12-21 13:10:23 -08003556 int hdisplay, int htotal,
Chris Wilson4ed765f2010-09-11 10:46:47 +01003557 int pixel_size)
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003558{
3559 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesb79d4992010-12-21 13:10:23 -08003560 int fbc_wm, plane_wm, cursor_wm, enabled;
3561 int clock;
Zhao Yakuic936f442010-06-12 14:32:26 +08003562
Chris Wilson4ed765f2010-09-11 10:46:47 +01003563 enabled = 0;
Yuanhan Liu13982612010-12-15 15:42:31 +08003564 if (ironlake_compute_wm0(dev, 0,
3565 &ironlake_display_wm_info,
3566 ILK_LP0_PLANE_LATENCY,
3567 &ironlake_cursor_wm_info,
3568 ILK_LP0_CURSOR_LATENCY,
3569 &plane_wm, &cursor_wm)) {
Chris Wilson4ed765f2010-09-11 10:46:47 +01003570 I915_WRITE(WM0_PIPEA_ILK,
3571 (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
3572 DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
3573 " plane %d, " "cursor: %d\n",
3574 plane_wm, cursor_wm);
3575 enabled++;
Zhao Yakuic936f442010-06-12 14:32:26 +08003576 }
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003577
Yuanhan Liu13982612010-12-15 15:42:31 +08003578 if (ironlake_compute_wm0(dev, 1,
3579 &ironlake_display_wm_info,
3580 ILK_LP0_PLANE_LATENCY,
3581 &ironlake_cursor_wm_info,
3582 ILK_LP0_CURSOR_LATENCY,
3583 &plane_wm, &cursor_wm)) {
Chris Wilson4ed765f2010-09-11 10:46:47 +01003584 I915_WRITE(WM0_PIPEB_ILK,
3585 (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
3586 DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
3587 " plane %d, cursor: %d\n",
3588 plane_wm, cursor_wm);
3589 enabled++;
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003590 }
3591
3592 /*
3593 * Calculate and update the self-refresh watermark only when one
3594 * display plane is used.
3595 */
Jesse Barnesb79d4992010-12-21 13:10:23 -08003596 I915_WRITE(WM3_LP_ILK, 0);
3597 I915_WRITE(WM2_LP_ILK, 0);
3598 I915_WRITE(WM1_LP_ILK, 0);
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003599
Jesse Barnesb79d4992010-12-21 13:10:23 -08003600 if (enabled != 1)
3601 return;
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003602
Jesse Barnesb79d4992010-12-21 13:10:23 -08003603 clock = planea_clock ? planea_clock : planeb_clock;
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003604
Jesse Barnesb79d4992010-12-21 13:10:23 -08003605 /* WM1 */
3606 if (!ironlake_compute_srwm(dev, 1, hdisplay, htotal, pixel_size,
3607 clock, ILK_READ_WM1_LATENCY() * 500,
3608 &ironlake_display_srwm_info,
3609 &ironlake_cursor_srwm_info,
3610 &fbc_wm, &plane_wm, &cursor_wm))
3611 return;
Chris Wilson4ed765f2010-09-11 10:46:47 +01003612
Jesse Barnesb79d4992010-12-21 13:10:23 -08003613 I915_WRITE(WM1_LP_ILK,
3614 WM1_LP_SR_EN |
3615 (ILK_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
3616 (fbc_wm << WM1_LP_FBC_SHIFT) |
3617 (plane_wm << WM1_LP_SR_SHIFT) |
3618 cursor_wm);
Chris Wilson4ed765f2010-09-11 10:46:47 +01003619
Jesse Barnesb79d4992010-12-21 13:10:23 -08003620 /* WM2 */
3621 if (!ironlake_compute_srwm(dev, 2, hdisplay, htotal, pixel_size,
3622 clock, ILK_READ_WM2_LATENCY() * 500,
3623 &ironlake_display_srwm_info,
3624 &ironlake_cursor_srwm_info,
3625 &fbc_wm, &plane_wm, &cursor_wm))
3626 return;
Chris Wilson4ed765f2010-09-11 10:46:47 +01003627
Jesse Barnesb79d4992010-12-21 13:10:23 -08003628 I915_WRITE(WM2_LP_ILK,
3629 WM2_LP_EN |
3630 (ILK_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
3631 (fbc_wm << WM1_LP_FBC_SHIFT) |
3632 (plane_wm << WM1_LP_SR_SHIFT) |
3633 cursor_wm);
Yuanhan Liu13982612010-12-15 15:42:31 +08003634
3635 /*
Jesse Barnesb79d4992010-12-21 13:10:23 -08003636 * WM3 is unsupported on ILK, probably because we don't have latency
3637 * data for that power state
Yuanhan Liu13982612010-12-15 15:42:31 +08003638 */
Yuanhan Liu13982612010-12-15 15:42:31 +08003639}
3640
3641static void sandybridge_update_wm(struct drm_device *dev,
3642 int planea_clock, int planeb_clock,
3643 int hdisplay, int htotal,
3644 int pixel_size)
3645{
3646 struct drm_i915_private *dev_priv = dev->dev_private;
3647 int latency = SNB_READ_WM0_LATENCY();
3648 int fbc_wm, plane_wm, cursor_wm, enabled;
3649 int clock;
3650
3651 enabled = 0;
3652 if (ironlake_compute_wm0(dev, 0,
3653 &sandybridge_display_wm_info, latency,
3654 &sandybridge_cursor_wm_info, latency,
3655 &plane_wm, &cursor_wm)) {
3656 I915_WRITE(WM0_PIPEA_ILK,
3657 (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
3658 DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
3659 " plane %d, " "cursor: %d\n",
3660 plane_wm, cursor_wm);
3661 enabled++;
3662 }
3663
3664 if (ironlake_compute_wm0(dev, 1,
3665 &sandybridge_display_wm_info, latency,
3666 &sandybridge_cursor_wm_info, latency,
3667 &plane_wm, &cursor_wm)) {
3668 I915_WRITE(WM0_PIPEB_ILK,
3669 (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
3670 DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
3671 " plane %d, cursor: %d\n",
3672 plane_wm, cursor_wm);
3673 enabled++;
3674 }
3675
3676 /*
3677 * Calculate and update the self-refresh watermark only when one
3678 * display plane is used.
3679 *
3680 * SNB support 3 levels of watermark.
3681 *
3682 * WM1/WM2/WM2 watermarks have to be enabled in the ascending order,
3683 * and disabled in the descending order
3684 *
3685 */
3686 I915_WRITE(WM3_LP_ILK, 0);
3687 I915_WRITE(WM2_LP_ILK, 0);
3688 I915_WRITE(WM1_LP_ILK, 0);
3689
3690 if (enabled != 1)
3691 return;
3692
3693 clock = planea_clock ? planea_clock : planeb_clock;
3694
3695 /* WM1 */
Jesse Barnesb79d4992010-12-21 13:10:23 -08003696 if (!ironlake_compute_srwm(dev, 1, hdisplay, htotal, pixel_size,
3697 clock, SNB_READ_WM1_LATENCY() * 500,
3698 &sandybridge_display_srwm_info,
3699 &sandybridge_cursor_srwm_info,
3700 &fbc_wm, &plane_wm, &cursor_wm))
Yuanhan Liu13982612010-12-15 15:42:31 +08003701 return;
3702
3703 I915_WRITE(WM1_LP_ILK,
3704 WM1_LP_SR_EN |
3705 (SNB_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
3706 (fbc_wm << WM1_LP_FBC_SHIFT) |
3707 (plane_wm << WM1_LP_SR_SHIFT) |
3708 cursor_wm);
3709
3710 /* WM2 */
Jesse Barnesb79d4992010-12-21 13:10:23 -08003711 if (!ironlake_compute_srwm(dev, 2,
3712 hdisplay, htotal, pixel_size,
3713 clock, SNB_READ_WM2_LATENCY() * 500,
3714 &sandybridge_display_srwm_info,
3715 &sandybridge_cursor_srwm_info,
3716 &fbc_wm, &plane_wm, &cursor_wm))
Yuanhan Liu13982612010-12-15 15:42:31 +08003717 return;
3718
3719 I915_WRITE(WM2_LP_ILK,
3720 WM2_LP_EN |
3721 (SNB_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
3722 (fbc_wm << WM1_LP_FBC_SHIFT) |
3723 (plane_wm << WM1_LP_SR_SHIFT) |
3724 cursor_wm);
3725
3726 /* WM3 */
Jesse Barnesb79d4992010-12-21 13:10:23 -08003727 if (!ironlake_compute_srwm(dev, 3,
3728 hdisplay, htotal, pixel_size,
3729 clock, SNB_READ_WM3_LATENCY() * 500,
3730 &sandybridge_display_srwm_info,
3731 &sandybridge_cursor_srwm_info,
3732 &fbc_wm, &plane_wm, &cursor_wm))
Yuanhan Liu13982612010-12-15 15:42:31 +08003733 return;
3734
3735 I915_WRITE(WM3_LP_ILK,
3736 WM3_LP_EN |
3737 (SNB_READ_WM3_LATENCY() << WM1_LP_LATENCY_SHIFT) |
3738 (fbc_wm << WM1_LP_FBC_SHIFT) |
3739 (plane_wm << WM1_LP_SR_SHIFT) |
3740 cursor_wm);
3741}
3742
Shaohua Li7662c8b2009-06-26 11:23:55 +08003743/**
3744 * intel_update_watermarks - update FIFO watermark values based on current modes
3745 *
3746 * Calculate watermark values for the various WM regs based on current mode
3747 * and plane configuration.
3748 *
3749 * There are several cases to deal with here:
3750 * - normal (i.e. non-self-refresh)
3751 * - self-refresh (SR) mode
3752 * - lines are large relative to FIFO size (buffer can hold up to 2)
3753 * - lines are small relative to FIFO size (buffer can hold more than 2
3754 * lines), so need to account for TLB latency
3755 *
3756 * The normal calculation is:
3757 * watermark = dotclock * bytes per pixel * latency
3758 * where latency is platform & configuration dependent (we assume pessimal
3759 * values here).
3760 *
3761 * The SR calculation is:
3762 * watermark = (trunc(latency/line time)+1) * surface width *
3763 * bytes per pixel
3764 * where
3765 * line time = htotal / dotclock
Zhao Yakuifa143212010-06-12 14:32:23 +08003766 * surface width = hdisplay for normal plane and 64 for cursor
Shaohua Li7662c8b2009-06-26 11:23:55 +08003767 * and latency is assumed to be high, as above.
3768 *
3769 * The final value programmed to the register should always be rounded up,
3770 * and include an extra 2 entries to account for clock crossings.
3771 *
3772 * We don't use the sprite, so we can ignore that. And on Crestline we have
3773 * to set the non-SR watermarks to 8.
Chris Wilson5eddb702010-09-11 13:48:45 +01003774 */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003775static void intel_update_watermarks(struct drm_device *dev)
3776{
Jesse Barnese70236a2009-09-21 10:42:27 -07003777 struct drm_i915_private *dev_priv = dev->dev_private;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003778 struct drm_crtc *crtc;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003779 int sr_hdisplay = 0;
3780 unsigned long planea_clock = 0, planeb_clock = 0, sr_clock = 0;
3781 int enabled = 0, pixel_size = 0;
Zhao Yakuifa143212010-06-12 14:32:23 +08003782 int sr_htotal = 0;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003783
Zhenyu Wangc03342f2009-09-29 11:01:23 +08003784 if (!dev_priv->display.update_wm)
3785 return;
3786
Shaohua Li7662c8b2009-06-26 11:23:55 +08003787 /* Get the clock config from both planes */
3788 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Chris Wilsondebcadd2010-08-07 11:01:33 +01003789 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003790 if (intel_crtc->active) {
Shaohua Li7662c8b2009-06-26 11:23:55 +08003791 enabled++;
3792 if (intel_crtc->plane == 0) {
Zhao Yakui28c97732009-10-09 11:39:41 +08003793 DRM_DEBUG_KMS("plane A (pipe %d) clock: %d\n",
Chris Wilson5eddb702010-09-11 13:48:45 +01003794 intel_crtc->pipe, crtc->mode.clock);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003795 planea_clock = crtc->mode.clock;
3796 } else {
Zhao Yakui28c97732009-10-09 11:39:41 +08003797 DRM_DEBUG_KMS("plane B (pipe %d) clock: %d\n",
Chris Wilson5eddb702010-09-11 13:48:45 +01003798 intel_crtc->pipe, crtc->mode.clock);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003799 planeb_clock = crtc->mode.clock;
3800 }
3801 sr_hdisplay = crtc->mode.hdisplay;
3802 sr_clock = crtc->mode.clock;
Zhao Yakuifa143212010-06-12 14:32:23 +08003803 sr_htotal = crtc->mode.htotal;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003804 if (crtc->fb)
3805 pixel_size = crtc->fb->bits_per_pixel / 8;
3806 else
3807 pixel_size = 4; /* by default */
3808 }
3809 }
3810
3811 if (enabled <= 0)
3812 return;
3813
Jesse Barnese70236a2009-09-21 10:42:27 -07003814 dev_priv->display.update_wm(dev, planea_clock, planeb_clock,
Zhao Yakuifa143212010-06-12 14:32:23 +08003815 sr_hdisplay, sr_htotal, pixel_size);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003816}
3817
Chris Wilson5c3b82e2009-02-11 13:25:09 +00003818static int intel_crtc_mode_set(struct drm_crtc *crtc,
3819 struct drm_display_mode *mode,
3820 struct drm_display_mode *adjusted_mode,
3821 int x, int y,
3822 struct drm_framebuffer *old_fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08003823{
3824 struct drm_device *dev = crtc->dev;
3825 struct drm_i915_private *dev_priv = dev->dev_private;
3826 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3827 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07003828 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01003829 u32 fp_reg, dpll_reg;
Eric Anholtc751ce42010-03-25 11:48:48 -07003830 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07003831 intel_clock_t clock, reduced_clock;
Chris Wilson5eddb702010-09-11 13:48:45 +01003832 u32 dpll, fp = 0, fp2 = 0, dspcntr, pipeconf;
Jesse Barnes652c3932009-08-17 13:31:43 -07003833 bool ok, has_reduced_clock = false, is_sdvo = false, is_dvo = false;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003834 bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
Chris Wilson8e647a22010-08-22 10:54:23 +01003835 struct intel_encoder *has_edp_encoder = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08003836 struct drm_mode_config *mode_config = &dev->mode_config;
Chris Wilson5eddb702010-09-11 13:48:45 +01003837 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08003838 const intel_limit_t *limit;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00003839 int ret;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003840 struct fdi_m_n m_n = {0};
Chris Wilson5eddb702010-09-11 13:48:45 +01003841 u32 reg, temp;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08003842 int target_clock;
Jesse Barnes79e53942008-11-07 14:24:08 -08003843
3844 drm_vblank_pre_modeset(dev, pipe);
3845
Chris Wilson5eddb702010-09-11 13:48:45 +01003846 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
3847 if (encoder->base.crtc != crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003848 continue;
3849
Chris Wilson5eddb702010-09-11 13:48:45 +01003850 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003851 case INTEL_OUTPUT_LVDS:
3852 is_lvds = true;
3853 break;
3854 case INTEL_OUTPUT_SDVO:
Eric Anholt7d573822009-01-02 13:33:00 -08003855 case INTEL_OUTPUT_HDMI:
Jesse Barnes79e53942008-11-07 14:24:08 -08003856 is_sdvo = true;
Chris Wilson5eddb702010-09-11 13:48:45 +01003857 if (encoder->needs_tv_clock)
Jesse Barnese2f0ba92009-02-02 15:11:52 -08003858 is_tv = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08003859 break;
3860 case INTEL_OUTPUT_DVO:
3861 is_dvo = true;
3862 break;
3863 case INTEL_OUTPUT_TVOUT:
3864 is_tv = true;
3865 break;
3866 case INTEL_OUTPUT_ANALOG:
3867 is_crt = true;
3868 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003869 case INTEL_OUTPUT_DISPLAYPORT:
3870 is_dp = true;
3871 break;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08003872 case INTEL_OUTPUT_EDP:
Chris Wilson5eddb702010-09-11 13:48:45 +01003873 has_edp_encoder = encoder;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08003874 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08003875 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05003876
Eric Anholtc751ce42010-03-25 11:48:48 -07003877 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08003878 }
3879
Eric Anholtc751ce42010-03-25 11:48:48 -07003880 if (is_lvds && dev_priv->lvds_use_ssc && num_connectors < 2) {
Kristian Høgsberg43565a02009-02-13 20:56:52 -05003881 refclk = dev_priv->lvds_ssc_freq * 1000;
Zhao Yakui28c97732009-10-09 11:39:41 +08003882 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
Chris Wilson5eddb702010-09-11 13:48:45 +01003883 refclk / 1000);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003884 } else if (!IS_GEN2(dev)) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003885 refclk = 96000;
Jesse Barnes1cb1b752010-10-07 16:01:17 -07003886 if (HAS_PCH_SPLIT(dev) &&
3887 (!has_edp_encoder || intel_encoder_is_pch_edp(&has_edp_encoder->base)))
Zhenyu Wang2c072452009-06-05 15:38:42 +08003888 refclk = 120000; /* 120Mhz refclk */
Jesse Barnes79e53942008-11-07 14:24:08 -08003889 } else {
3890 refclk = 48000;
3891 }
3892
Ma Lingd4906092009-03-18 20:13:27 +08003893 /*
3894 * Returns a set of divisors for the desired target clock with the given
3895 * refclk, or FALSE. The returned values represent the clock equation:
3896 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
3897 */
Chris Wilson1b894b52010-12-14 20:04:54 +00003898 limit = intel_limit(crtc, refclk);
Ma Lingd4906092009-03-18 20:13:27 +08003899 ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08003900 if (!ok) {
3901 DRM_ERROR("Couldn't find PLL settings for mode!\n");
Chris Wilson1f803ee2009-06-06 09:45:59 +01003902 drm_vblank_post_modeset(dev, pipe);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00003903 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08003904 }
3905
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01003906 /* Ensure that the cursor is valid for the new mode before changing... */
Chris Wilson6b383a72010-09-13 13:54:26 +01003907 intel_crtc_update_cursor(crtc, true);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01003908
Zhao Yakuiddc90032010-01-06 22:05:56 +08003909 if (is_lvds && dev_priv->lvds_downclock_avail) {
3910 has_reduced_clock = limit->find_pll(limit, crtc,
Chris Wilson5eddb702010-09-11 13:48:45 +01003911 dev_priv->lvds_downclock,
3912 refclk,
3913 &reduced_clock);
Zhao Yakui18f9ed12009-11-20 03:24:16 +00003914 if (has_reduced_clock && (clock.p != reduced_clock.p)) {
3915 /*
3916 * If the different P is found, it means that we can't
3917 * switch the display clock by using the FP0/FP1.
3918 * In such case we will disable the LVDS downclock
3919 * feature.
3920 */
3921 DRM_DEBUG_KMS("Different P is found for "
Chris Wilson5eddb702010-09-11 13:48:45 +01003922 "LVDS clock/downclock\n");
Zhao Yakui18f9ed12009-11-20 03:24:16 +00003923 has_reduced_clock = 0;
3924 }
Jesse Barnes652c3932009-08-17 13:31:43 -07003925 }
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08003926 /* SDVO TV has fixed PLL values depend on its clock range,
3927 this mirrors vbios setting. */
3928 if (is_sdvo && is_tv) {
3929 if (adjusted_mode->clock >= 100000
Chris Wilson5eddb702010-09-11 13:48:45 +01003930 && adjusted_mode->clock < 140500) {
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08003931 clock.p1 = 2;
3932 clock.p2 = 10;
3933 clock.n = 3;
3934 clock.m1 = 16;
3935 clock.m2 = 8;
3936 } else if (adjusted_mode->clock >= 140500
Chris Wilson5eddb702010-09-11 13:48:45 +01003937 && adjusted_mode->clock <= 200000) {
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08003938 clock.p1 = 1;
3939 clock.p2 = 10;
3940 clock.n = 6;
3941 clock.m1 = 12;
3942 clock.m2 = 8;
3943 }
3944 }
3945
Zhenyu Wang2c072452009-06-05 15:38:42 +08003946 /* FDI link */
Eric Anholtbad720f2009-10-22 16:11:14 -07003947 if (HAS_PCH_SPLIT(dev)) {
Chris Wilson49078f72010-12-04 07:45:57 +00003948 int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
Adam Jackson77ffb592010-04-12 11:38:44 -04003949 int lane = 0, link_bw, bpp;
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003950 /* CPU eDP doesn't require FDI link, so just set DP M/N
Zhenyu Wang32f9d652009-07-24 01:00:32 +08003951 according to current link config */
Jesse Barnes858bc212011-01-04 10:46:49 -08003952 if (has_edp_encoder && !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08003953 target_clock = mode->clock;
Chris Wilson8e647a22010-08-22 10:54:23 +01003954 intel_edp_link_config(has_edp_encoder,
3955 &lane, &link_bw);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08003956 } else {
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003957 /* [e]DP over FDI requires target mode clock
Zhenyu Wang32f9d652009-07-24 01:00:32 +08003958 instead of link clock */
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003959 if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
Zhenyu Wang32f9d652009-07-24 01:00:32 +08003960 target_clock = mode->clock;
3961 else
3962 target_clock = adjusted_mode->clock;
Chris Wilson021357a2010-09-07 20:54:59 +01003963
3964 /* FDI is a binary signal running at ~2.7GHz, encoding
3965 * each output octet as 10 bits. The actual frequency
3966 * is stored as a divider into a 100MHz clock, and the
3967 * mode pixel clock is stored in units of 1KHz.
3968 * Hence the bw of each lane in terms of the mode signal
3969 * is:
3970 */
3971 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08003972 }
Zhenyu Wang58a27472009-09-25 08:01:28 +00003973
3974 /* determine panel color depth */
Chris Wilson5eddb702010-09-11 13:48:45 +01003975 temp = I915_READ(PIPECONF(pipe));
Zhao Yakuie5a95eb2010-01-04 16:29:32 +08003976 temp &= ~PIPE_BPC_MASK;
3977 if (is_lvds) {
Zhao Yakuie5a95eb2010-01-04 16:29:32 +08003978 /* the BPC will be 6 if it is 18-bit LVDS panel */
Chris Wilson5eddb702010-09-11 13:48:45 +01003979 if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) == LVDS_A3_POWER_UP)
Zhao Yakuie5a95eb2010-01-04 16:29:32 +08003980 temp |= PIPE_8BPC;
3981 else
3982 temp |= PIPE_6BPC;
Jesse Barnes1d850362010-10-07 16:01:10 -07003983 } else if (has_edp_encoder) {
Chris Wilson5ceb0f92010-09-24 10:24:28 +01003984 switch (dev_priv->edp.bpp/3) {
Zhenyu Wang885a5fb2010-01-12 05:38:31 +08003985 case 8:
3986 temp |= PIPE_8BPC;
3987 break;
3988 case 10:
3989 temp |= PIPE_10BPC;
3990 break;
3991 case 6:
3992 temp |= PIPE_6BPC;
3993 break;
3994 case 12:
3995 temp |= PIPE_12BPC;
3996 break;
3997 }
Zhao Yakuie5a95eb2010-01-04 16:29:32 +08003998 } else
3999 temp |= PIPE_8BPC;
Chris Wilson5eddb702010-09-11 13:48:45 +01004000 I915_WRITE(PIPECONF(pipe), temp);
Zhenyu Wang58a27472009-09-25 08:01:28 +00004001
4002 switch (temp & PIPE_BPC_MASK) {
4003 case PIPE_8BPC:
4004 bpp = 24;
4005 break;
4006 case PIPE_10BPC:
4007 bpp = 30;
4008 break;
4009 case PIPE_6BPC:
4010 bpp = 18;
4011 break;
4012 case PIPE_12BPC:
4013 bpp = 36;
4014 break;
4015 default:
4016 DRM_ERROR("unknown pipe bpc value\n");
4017 bpp = 24;
4018 }
4019
Adam Jackson77ffb592010-04-12 11:38:44 -04004020 if (!lane) {
4021 /*
4022 * Account for spread spectrum to avoid
4023 * oversubscribing the link. Max center spread
4024 * is 2.5%; use 5% for safety's sake.
4025 */
4026 u32 bps = target_clock * bpp * 21 / 20;
4027 lane = bps / (link_bw * 8) + 1;
4028 }
4029
4030 intel_crtc->fdi_lanes = lane;
4031
Chris Wilson49078f72010-12-04 07:45:57 +00004032 if (pixel_multiplier > 1)
4033 link_bw *= pixel_multiplier;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05004034 ironlake_compute_m_n(bpp, lane, target_clock, link_bw, &m_n);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08004035 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08004036
Zhenyu Wangc038e512009-10-19 15:43:48 +08004037 /* Ironlake: try to setup display ref clock before DPLL
4038 * enabling. This is only under driver's control after
4039 * PCH B stepping, previous chipset stepping should be
4040 * ignoring this setting.
4041 */
Eric Anholtbad720f2009-10-22 16:11:14 -07004042 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wangc038e512009-10-19 15:43:48 +08004043 temp = I915_READ(PCH_DREF_CONTROL);
4044 /* Always enable nonspread source */
4045 temp &= ~DREF_NONSPREAD_SOURCE_MASK;
4046 temp |= DREF_NONSPREAD_SOURCE_ENABLE;
Zhenyu Wangc038e512009-10-19 15:43:48 +08004047 temp &= ~DREF_SSC_SOURCE_MASK;
4048 temp |= DREF_SSC_SOURCE_ENABLE;
4049 I915_WRITE(PCH_DREF_CONTROL, temp);
Zhenyu Wangc038e512009-10-19 15:43:48 +08004050
Chris Wilson5eddb702010-09-11 13:48:45 +01004051 POSTING_READ(PCH_DREF_CONTROL);
Zhenyu Wangc038e512009-10-19 15:43:48 +08004052 udelay(200);
4053
Chris Wilson8e647a22010-08-22 10:54:23 +01004054 if (has_edp_encoder) {
Zhenyu Wangc038e512009-10-19 15:43:48 +08004055 if (dev_priv->lvds_use_ssc) {
4056 temp |= DREF_SSC1_ENABLE;
4057 I915_WRITE(PCH_DREF_CONTROL, temp);
Zhenyu Wangc038e512009-10-19 15:43:48 +08004058
Chris Wilson5eddb702010-09-11 13:48:45 +01004059 POSTING_READ(PCH_DREF_CONTROL);
Zhenyu Wangc038e512009-10-19 15:43:48 +08004060 udelay(200);
Jesse Barnes7f823282010-10-07 16:01:16 -07004061 }
4062 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Zhenyu Wangc038e512009-10-19 15:43:48 +08004063
Jesse Barnes7f823282010-10-07 16:01:16 -07004064 /* Enable CPU source on CPU attached eDP */
4065 if (!intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
4066 if (dev_priv->lvds_use_ssc)
4067 temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
4068 else
4069 temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Zhenyu Wangc038e512009-10-19 15:43:48 +08004070 } else {
Jesse Barnes7f823282010-10-07 16:01:16 -07004071 /* Enable SSC on PCH eDP if needed */
4072 if (dev_priv->lvds_use_ssc) {
4073 DRM_ERROR("enabling SSC on PCH\n");
4074 temp |= DREF_SUPERSPREAD_SOURCE_ENABLE;
4075 }
Zhenyu Wangc038e512009-10-19 15:43:48 +08004076 }
Chris Wilson5eddb702010-09-11 13:48:45 +01004077 I915_WRITE(PCH_DREF_CONTROL, temp);
Jesse Barnes7f823282010-10-07 16:01:16 -07004078 POSTING_READ(PCH_DREF_CONTROL);
4079 udelay(200);
Zhenyu Wangc038e512009-10-19 15:43:48 +08004080 }
4081 }
4082
Adam Jacksonf2b115e2009-12-03 17:14:42 -05004083 if (IS_PINEVIEW(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +08004084 fp = (1 << clock.n) << 16 | clock.m1 << 8 | clock.m2;
Jesse Barnes652c3932009-08-17 13:31:43 -07004085 if (has_reduced_clock)
4086 fp2 = (1 << reduced_clock.n) << 16 |
4087 reduced_clock.m1 << 8 | reduced_clock.m2;
4088 } else {
Shaohua Li21778322009-02-23 15:19:16 +08004089 fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
Jesse Barnes652c3932009-08-17 13:31:43 -07004090 if (has_reduced_clock)
4091 fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
4092 reduced_clock.m2;
4093 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004094
Chris Wilsonc1858122010-12-03 21:35:48 +00004095 /* Enable autotuning of the PLL clock (if permissible) */
4096 if (HAS_PCH_SPLIT(dev)) {
4097 int factor = 21;
4098
4099 if (is_lvds) {
4100 if ((dev_priv->lvds_use_ssc &&
4101 dev_priv->lvds_ssc_freq == 100) ||
4102 (I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP)
4103 factor = 25;
4104 } else if (is_sdvo && is_tv)
4105 factor = 20;
4106
4107 if (clock.m1 < factor * clock.n)
4108 fp |= FP_CB_TUNE;
4109 }
4110
Chris Wilson5eddb702010-09-11 13:48:45 +01004111 dpll = 0;
Eric Anholtbad720f2009-10-22 16:11:14 -07004112 if (!HAS_PCH_SPLIT(dev))
Zhenyu Wang2c072452009-06-05 15:38:42 +08004113 dpll = DPLL_VGA_MODE_DIS;
4114
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004115 if (!IS_GEN2(dev)) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004116 if (is_lvds)
4117 dpll |= DPLLB_MODE_LVDS;
4118 else
4119 dpll |= DPLLB_MODE_DAC_SERIAL;
4120 if (is_sdvo) {
Chris Wilson6c9547f2010-08-25 10:05:17 +01004121 int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
4122 if (pixel_multiplier > 1) {
4123 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4124 dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
4125 else if (HAS_PCH_SPLIT(dev))
4126 dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
4127 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004128 dpll |= DPLL_DVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08004129 }
Jesse Barnes83240122010-10-07 16:01:18 -07004130 if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004131 dpll |= DPLL_DVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08004132
4133 /* compute bitmask from p1 value */
Adam Jacksonf2b115e2009-12-03 17:14:42 -05004134 if (IS_PINEVIEW(dev))
4135 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004136 else {
Shaohua Li21778322009-02-23 15:19:16 +08004137 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004138 /* also FPA1 */
Eric Anholtbad720f2009-10-22 16:11:14 -07004139 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang2c072452009-06-05 15:38:42 +08004140 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Jesse Barnes652c3932009-08-17 13:31:43 -07004141 if (IS_G4X(dev) && has_reduced_clock)
4142 dpll |= (1 << (reduced_clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004143 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004144 switch (clock.p2) {
4145 case 5:
4146 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4147 break;
4148 case 7:
4149 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4150 break;
4151 case 10:
4152 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4153 break;
4154 case 14:
4155 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4156 break;
4157 }
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004158 if (INTEL_INFO(dev)->gen >= 4 && !HAS_PCH_SPLIT(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08004159 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
4160 } else {
4161 if (is_lvds) {
4162 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4163 } else {
4164 if (clock.p1 == 2)
4165 dpll |= PLL_P1_DIVIDE_BY_TWO;
4166 else
4167 dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4168 if (clock.p2 == 4)
4169 dpll |= PLL_P2_DIVIDE_BY_4;
4170 }
4171 }
4172
Kristian Høgsberg43565a02009-02-13 20:56:52 -05004173 if (is_sdvo && is_tv)
4174 dpll |= PLL_REF_INPUT_TVCLKINBC;
4175 else if (is_tv)
Jesse Barnes79e53942008-11-07 14:24:08 -08004176 /* XXX: just matching BIOS for now */
Kristian Høgsberg43565a02009-02-13 20:56:52 -05004177 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
Jesse Barnes79e53942008-11-07 14:24:08 -08004178 dpll |= 3;
Eric Anholtc751ce42010-03-25 11:48:48 -07004179 else if (is_lvds && dev_priv->lvds_use_ssc && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05004180 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08004181 else
4182 dpll |= PLL_REF_INPUT_DREFCLK;
4183
4184 /* setup pipeconf */
Chris Wilson5eddb702010-09-11 13:48:45 +01004185 pipeconf = I915_READ(PIPECONF(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08004186
4187 /* Set up the display plane register */
4188 dspcntr = DISPPLANE_GAMMA_ENABLE;
4189
Adam Jacksonf2b115e2009-12-03 17:14:42 -05004190 /* Ironlake's plane is forced to pipe, bit 24 is to
Zhenyu Wang2c072452009-06-05 15:38:42 +08004191 enable color space conversion */
Eric Anholtbad720f2009-10-22 16:11:14 -07004192 if (!HAS_PCH_SPLIT(dev)) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08004193 if (pipe == 0)
Jesse Barnes80824002009-09-10 15:28:06 -07004194 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004195 else
4196 dspcntr |= DISPPLANE_SEL_PIPE_B;
4197 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004198
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004199 if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004200 /* Enable pixel doubling when the dot clock is > 90% of the (display)
4201 * core speed.
4202 *
4203 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
4204 * pipe == 0 check?
4205 */
Jesse Barnese70236a2009-09-21 10:42:27 -07004206 if (mode->clock >
4207 dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
Chris Wilson5eddb702010-09-11 13:48:45 +01004208 pipeconf |= PIPECONF_DOUBLE_WIDE;
Jesse Barnes79e53942008-11-07 14:24:08 -08004209 else
Chris Wilson5eddb702010-09-11 13:48:45 +01004210 pipeconf &= ~PIPECONF_DOUBLE_WIDE;
Jesse Barnes79e53942008-11-07 14:24:08 -08004211 }
4212
Linus Torvalds8d86dc62010-06-08 20:16:28 -07004213 dspcntr |= DISPLAY_PLANE_ENABLE;
Chris Wilson5eddb702010-09-11 13:48:45 +01004214 pipeconf |= PIPECONF_ENABLE;
Linus Torvalds8d86dc62010-06-08 20:16:28 -07004215 dpll |= DPLL_VCO_ENABLE;
4216
Zhao Yakui28c97732009-10-09 11:39:41 +08004217 DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
Jesse Barnes79e53942008-11-07 14:24:08 -08004218 drm_mode_debug_printmodeline(mode);
4219
Adam Jacksonf2b115e2009-12-03 17:14:42 -05004220 /* assign to Ironlake registers */
Eric Anholtbad720f2009-10-22 16:11:14 -07004221 if (HAS_PCH_SPLIT(dev)) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004222 fp_reg = PCH_FP0(pipe);
4223 dpll_reg = PCH_DPLL(pipe);
4224 } else {
4225 fp_reg = FP0(pipe);
4226 dpll_reg = DPLL(pipe);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004227 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004228
Jesse Barnes5c5313c2010-10-07 16:01:11 -07004229 /* PCH eDP needs FDI, but CPU eDP does not */
4230 if (!has_edp_encoder || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004231 I915_WRITE(fp_reg, fp);
4232 I915_WRITE(dpll_reg, dpll & ~DPLL_VCO_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01004233
4234 POSTING_READ(dpll_reg);
Jesse Barnes79e53942008-11-07 14:24:08 -08004235 udelay(150);
4236 }
4237
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08004238 /* enable transcoder DPLL */
4239 if (HAS_PCH_CPT(dev)) {
4240 temp = I915_READ(PCH_DPLL_SEL);
Chris Wilson5eddb702010-09-11 13:48:45 +01004241 if (pipe == 0)
4242 temp |= TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL;
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08004243 else
Chris Wilson5eddb702010-09-11 13:48:45 +01004244 temp |= TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL;
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08004245 I915_WRITE(PCH_DPLL_SEL, temp);
Chris Wilson5eddb702010-09-11 13:48:45 +01004246
4247 POSTING_READ(PCH_DPLL_SEL);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08004248 udelay(150);
4249 }
4250
Jesse Barnes79e53942008-11-07 14:24:08 -08004251 /* The LVDS pin pair needs to be on before the DPLLs are enabled.
4252 * This is an exception to the general rule that mode_set doesn't turn
4253 * things on.
4254 */
4255 if (is_lvds) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004256 reg = LVDS;
Eric Anholtbad720f2009-10-22 16:11:14 -07004257 if (HAS_PCH_SPLIT(dev))
Chris Wilson5eddb702010-09-11 13:48:45 +01004258 reg = PCH_LVDS;
Zhenyu Wang541998a2009-06-05 15:38:44 +08004259
Chris Wilson5eddb702010-09-11 13:48:45 +01004260 temp = I915_READ(reg);
4261 temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
Zhenyu Wangb3b095b2010-04-07 16:15:56 +08004262 if (pipe == 1) {
4263 if (HAS_PCH_CPT(dev))
Chris Wilson5eddb702010-09-11 13:48:45 +01004264 temp |= PORT_TRANS_B_SEL_CPT;
Zhenyu Wangb3b095b2010-04-07 16:15:56 +08004265 else
Chris Wilson5eddb702010-09-11 13:48:45 +01004266 temp |= LVDS_PIPEB_SELECT;
Zhenyu Wangb3b095b2010-04-07 16:15:56 +08004267 } else {
4268 if (HAS_PCH_CPT(dev))
Chris Wilson5eddb702010-09-11 13:48:45 +01004269 temp &= ~PORT_TRANS_SEL_MASK;
Zhenyu Wangb3b095b2010-04-07 16:15:56 +08004270 else
Chris Wilson5eddb702010-09-11 13:48:45 +01004271 temp &= ~LVDS_PIPEB_SELECT;
Zhenyu Wangb3b095b2010-04-07 16:15:56 +08004272 }
Zhao Yakuia3e17eb2009-10-10 10:42:37 +08004273 /* set the corresponsding LVDS_BORDER bit */
Chris Wilson5eddb702010-09-11 13:48:45 +01004274 temp |= dev_priv->lvds_border_bits;
Jesse Barnes79e53942008-11-07 14:24:08 -08004275 /* Set the B0-B3 data pairs corresponding to whether we're going to
4276 * set the DPLLs for dual-channel mode or not.
4277 */
4278 if (clock.p2 == 7)
Chris Wilson5eddb702010-09-11 13:48:45 +01004279 temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
Jesse Barnes79e53942008-11-07 14:24:08 -08004280 else
Chris Wilson5eddb702010-09-11 13:48:45 +01004281 temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
Jesse Barnes79e53942008-11-07 14:24:08 -08004282
4283 /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
4284 * appropriately here, but we need to look more thoroughly into how
4285 * panels behave in the two modes.
4286 */
Jesse Barnes434ed092010-09-07 14:48:06 -07004287 /* set the dithering flag on non-PCH LVDS as needed */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004288 if (INTEL_INFO(dev)->gen >= 4 && !HAS_PCH_SPLIT(dev)) {
Jesse Barnes434ed092010-09-07 14:48:06 -07004289 if (dev_priv->lvds_dither)
Chris Wilson5eddb702010-09-11 13:48:45 +01004290 temp |= LVDS_ENABLE_DITHER;
Jesse Barnes434ed092010-09-07 14:48:06 -07004291 else
Chris Wilson5eddb702010-09-11 13:48:45 +01004292 temp &= ~LVDS_ENABLE_DITHER;
Zhao Yakui898822c2010-01-04 16:29:30 +08004293 }
Chris Wilson5eddb702010-09-11 13:48:45 +01004294 I915_WRITE(reg, temp);
Jesse Barnes79e53942008-11-07 14:24:08 -08004295 }
Jesse Barnes434ed092010-09-07 14:48:06 -07004296
4297 /* set the dithering flag and clear for anything other than a panel. */
4298 if (HAS_PCH_SPLIT(dev)) {
4299 pipeconf &= ~PIPECONF_DITHER_EN;
4300 pipeconf &= ~PIPECONF_DITHER_TYPE_MASK;
4301 if (dev_priv->lvds_dither && (is_lvds || has_edp_encoder)) {
4302 pipeconf |= PIPECONF_DITHER_EN;
4303 pipeconf |= PIPECONF_DITHER_TYPE_ST1;
4304 }
4305 }
4306
Jesse Barnes5c5313c2010-10-07 16:01:11 -07004307 if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004308 intel_dp_set_m_n(crtc, mode, adjusted_mode);
Jesse Barnes5c5313c2010-10-07 16:01:11 -07004309 } else if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08004310 /* For non-DP output, clear any trans DP clock recovery setting.*/
4311 if (pipe == 0) {
4312 I915_WRITE(TRANSA_DATA_M1, 0);
4313 I915_WRITE(TRANSA_DATA_N1, 0);
4314 I915_WRITE(TRANSA_DP_LINK_M1, 0);
4315 I915_WRITE(TRANSA_DP_LINK_N1, 0);
4316 } else {
4317 I915_WRITE(TRANSB_DATA_M1, 0);
4318 I915_WRITE(TRANSB_DATA_N1, 0);
4319 I915_WRITE(TRANSB_DP_LINK_M1, 0);
4320 I915_WRITE(TRANSB_DP_LINK_N1, 0);
4321 }
4322 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004323
Jesse Barnes5c5313c2010-10-07 16:01:11 -07004324 if (!has_edp_encoder || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004325 I915_WRITE(dpll_reg, dpll);
Chris Wilson5eddb702010-09-11 13:48:45 +01004326
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004327 /* Wait for the clocks to stabilize. */
Chris Wilson5eddb702010-09-11 13:48:45 +01004328 POSTING_READ(dpll_reg);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004329 udelay(150);
4330
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004331 if (INTEL_INFO(dev)->gen >= 4 && !HAS_PCH_SPLIT(dev)) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004332 temp = 0;
Zhao Yakuibb66c512009-09-10 15:45:49 +08004333 if (is_sdvo) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004334 temp = intel_mode_get_pixel_multiplier(adjusted_mode);
4335 if (temp > 1)
4336 temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Chris Wilson6c9547f2010-08-25 10:05:17 +01004337 else
Chris Wilson5eddb702010-09-11 13:48:45 +01004338 temp = 0;
4339 }
4340 I915_WRITE(DPLL_MD(pipe), temp);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004341 } else {
Chris Wilsona589b9f2010-12-03 21:13:16 +00004342 /* The pixel multiplier can only be updated once the
4343 * DPLL is enabled and the clocks are stable.
4344 *
4345 * So write it again.
4346 */
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004347 I915_WRITE(dpll_reg, dpll);
4348 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004349 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004350
Chris Wilson5eddb702010-09-11 13:48:45 +01004351 intel_crtc->lowfreq_avail = false;
Jesse Barnes652c3932009-08-17 13:31:43 -07004352 if (is_lvds && has_reduced_clock && i915_powersave) {
4353 I915_WRITE(fp_reg + 4, fp2);
4354 intel_crtc->lowfreq_avail = true;
4355 if (HAS_PIPE_CXSR(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08004356 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07004357 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
4358 }
4359 } else {
4360 I915_WRITE(fp_reg + 4, fp);
Jesse Barnes652c3932009-08-17 13:31:43 -07004361 if (HAS_PIPE_CXSR(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08004362 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07004363 pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
4364 }
4365 }
4366
Krzysztof Halasa734b4152010-05-25 18:41:46 +02004367 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4368 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
4369 /* the chip adds 2 halflines automatically */
4370 adjusted_mode->crtc_vdisplay -= 1;
4371 adjusted_mode->crtc_vtotal -= 1;
4372 adjusted_mode->crtc_vblank_start -= 1;
4373 adjusted_mode->crtc_vblank_end -= 1;
4374 adjusted_mode->crtc_vsync_end -= 1;
4375 adjusted_mode->crtc_vsync_start -= 1;
4376 } else
4377 pipeconf &= ~PIPECONF_INTERLACE_W_FIELD_INDICATION; /* progressive */
4378
Chris Wilson5eddb702010-09-11 13:48:45 +01004379 I915_WRITE(HTOTAL(pipe),
4380 (adjusted_mode->crtc_hdisplay - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08004381 ((adjusted_mode->crtc_htotal - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01004382 I915_WRITE(HBLANK(pipe),
4383 (adjusted_mode->crtc_hblank_start - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08004384 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01004385 I915_WRITE(HSYNC(pipe),
4386 (adjusted_mode->crtc_hsync_start - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08004387 ((adjusted_mode->crtc_hsync_end - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01004388
4389 I915_WRITE(VTOTAL(pipe),
4390 (adjusted_mode->crtc_vdisplay - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08004391 ((adjusted_mode->crtc_vtotal - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01004392 I915_WRITE(VBLANK(pipe),
4393 (adjusted_mode->crtc_vblank_start - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08004394 ((adjusted_mode->crtc_vblank_end - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01004395 I915_WRITE(VSYNC(pipe),
4396 (adjusted_mode->crtc_vsync_start - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08004397 ((adjusted_mode->crtc_vsync_end - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01004398
4399 /* pipesrc and dspsize control the size that is scaled from,
4400 * which should always be the user's requested size.
Jesse Barnes79e53942008-11-07 14:24:08 -08004401 */
Eric Anholtbad720f2009-10-22 16:11:14 -07004402 if (!HAS_PCH_SPLIT(dev)) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004403 I915_WRITE(DSPSIZE(plane),
4404 ((mode->vdisplay - 1) << 16) |
4405 (mode->hdisplay - 1));
4406 I915_WRITE(DSPPOS(plane), 0);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004407 }
Chris Wilson5eddb702010-09-11 13:48:45 +01004408 I915_WRITE(PIPESRC(pipe),
4409 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
Zhenyu Wang2c072452009-06-05 15:38:42 +08004410
Eric Anholtbad720f2009-10-22 16:11:14 -07004411 if (HAS_PCH_SPLIT(dev)) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004412 I915_WRITE(PIPE_DATA_M1(pipe), TU_SIZE(m_n.tu) | m_n.gmch_m);
4413 I915_WRITE(PIPE_DATA_N1(pipe), m_n.gmch_n);
4414 I915_WRITE(PIPE_LINK_M1(pipe), m_n.link_m);
4415 I915_WRITE(PIPE_LINK_N1(pipe), m_n.link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004416
Jesse Barnes5c5313c2010-10-07 16:01:11 -07004417 if (has_edp_encoder && !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05004418 ironlake_set_pll_edp(crtc, adjusted_mode->clock);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004419 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08004420 }
4421
Chris Wilson5eddb702010-09-11 13:48:45 +01004422 I915_WRITE(PIPECONF(pipe), pipeconf);
4423 POSTING_READ(PIPECONF(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08004424
Jesse Barnes9d0498a2010-08-18 13:20:54 -07004425 intel_wait_for_vblank(dev, pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08004426
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01004427 if (IS_GEN5(dev)) {
Zhenyu Wang553bd142009-09-02 10:57:52 +08004428 /* enable address swizzle for tiling buffer */
4429 temp = I915_READ(DISP_ARB_CTL);
4430 I915_WRITE(DISP_ARB_CTL, temp | DISP_TILE_SURFACE_SWIZZLING);
4431 }
4432
Chris Wilson5eddb702010-09-11 13:48:45 +01004433 I915_WRITE(DSPCNTR(plane), dspcntr);
Jesse Barnes79e53942008-11-07 14:24:08 -08004434
Chris Wilson5c3b82e2009-02-11 13:25:09 +00004435 ret = intel_pipe_set_base(crtc, x, y, old_fb);
Shaohua Li7662c8b2009-06-26 11:23:55 +08004436
4437 intel_update_watermarks(dev);
4438
Jesse Barnes79e53942008-11-07 14:24:08 -08004439 drm_vblank_post_modeset(dev, pipe);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00004440
Chris Wilson1f803ee2009-06-06 09:45:59 +01004441 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08004442}
4443
4444/** Loads the palette/gamma unit for the CRTC with the prepared values */
4445void intel_crtc_load_lut(struct drm_crtc *crtc)
4446{
4447 struct drm_device *dev = crtc->dev;
4448 struct drm_i915_private *dev_priv = dev->dev_private;
4449 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4450 int palreg = (intel_crtc->pipe == 0) ? PALETTE_A : PALETTE_B;
4451 int i;
4452
4453 /* The clocks have to be on to load the palette. */
4454 if (!crtc->enabled)
4455 return;
4456
Adam Jacksonf2b115e2009-12-03 17:14:42 -05004457 /* use legacy palette for Ironlake */
Eric Anholtbad720f2009-10-22 16:11:14 -07004458 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang2c072452009-06-05 15:38:42 +08004459 palreg = (intel_crtc->pipe == 0) ? LGC_PALETTE_A :
4460 LGC_PALETTE_B;
4461
Jesse Barnes79e53942008-11-07 14:24:08 -08004462 for (i = 0; i < 256; i++) {
4463 I915_WRITE(palreg + 4 * i,
4464 (intel_crtc->lut_r[i] << 16) |
4465 (intel_crtc->lut_g[i] << 8) |
4466 intel_crtc->lut_b[i]);
4467 }
4468}
4469
Chris Wilson560b85b2010-08-07 11:01:38 +01004470static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
4471{
4472 struct drm_device *dev = crtc->dev;
4473 struct drm_i915_private *dev_priv = dev->dev_private;
4474 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4475 bool visible = base != 0;
4476 u32 cntl;
4477
4478 if (intel_crtc->cursor_visible == visible)
4479 return;
4480
4481 cntl = I915_READ(CURACNTR);
4482 if (visible) {
4483 /* On these chipsets we can only modify the base whilst
4484 * the cursor is disabled.
4485 */
4486 I915_WRITE(CURABASE, base);
4487
4488 cntl &= ~(CURSOR_FORMAT_MASK);
4489 /* XXX width must be 64, stride 256 => 0x00 << 28 */
4490 cntl |= CURSOR_ENABLE |
4491 CURSOR_GAMMA_ENABLE |
4492 CURSOR_FORMAT_ARGB;
4493 } else
4494 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
4495 I915_WRITE(CURACNTR, cntl);
4496
4497 intel_crtc->cursor_visible = visible;
4498}
4499
4500static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
4501{
4502 struct drm_device *dev = crtc->dev;
4503 struct drm_i915_private *dev_priv = dev->dev_private;
4504 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4505 int pipe = intel_crtc->pipe;
4506 bool visible = base != 0;
4507
4508 if (intel_crtc->cursor_visible != visible) {
4509 uint32_t cntl = I915_READ(pipe == 0 ? CURACNTR : CURBCNTR);
4510 if (base) {
4511 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
4512 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
4513 cntl |= pipe << 28; /* Connect to correct pipe */
4514 } else {
4515 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
4516 cntl |= CURSOR_MODE_DISABLE;
4517 }
4518 I915_WRITE(pipe == 0 ? CURACNTR : CURBCNTR, cntl);
4519
4520 intel_crtc->cursor_visible = visible;
4521 }
4522 /* and commit changes on next vblank */
4523 I915_WRITE(pipe == 0 ? CURABASE : CURBBASE, base);
4524}
4525
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004526/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01004527static void intel_crtc_update_cursor(struct drm_crtc *crtc,
4528 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004529{
4530 struct drm_device *dev = crtc->dev;
4531 struct drm_i915_private *dev_priv = dev->dev_private;
4532 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4533 int pipe = intel_crtc->pipe;
4534 int x = intel_crtc->cursor_x;
4535 int y = intel_crtc->cursor_y;
Chris Wilson560b85b2010-08-07 11:01:38 +01004536 u32 base, pos;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004537 bool visible;
4538
4539 pos = 0;
4540
Chris Wilson6b383a72010-09-13 13:54:26 +01004541 if (on && crtc->enabled && crtc->fb) {
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004542 base = intel_crtc->cursor_addr;
4543 if (x > (int) crtc->fb->width)
4544 base = 0;
4545
4546 if (y > (int) crtc->fb->height)
4547 base = 0;
4548 } else
4549 base = 0;
4550
4551 if (x < 0) {
4552 if (x + intel_crtc->cursor_width < 0)
4553 base = 0;
4554
4555 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
4556 x = -x;
4557 }
4558 pos |= x << CURSOR_X_SHIFT;
4559
4560 if (y < 0) {
4561 if (y + intel_crtc->cursor_height < 0)
4562 base = 0;
4563
4564 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
4565 y = -y;
4566 }
4567 pos |= y << CURSOR_Y_SHIFT;
4568
4569 visible = base != 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01004570 if (!visible && !intel_crtc->cursor_visible)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004571 return;
4572
4573 I915_WRITE(pipe == 0 ? CURAPOS : CURBPOS, pos);
Chris Wilson560b85b2010-08-07 11:01:38 +01004574 if (IS_845G(dev) || IS_I865G(dev))
4575 i845_update_cursor(crtc, base);
4576 else
4577 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004578
4579 if (visible)
4580 intel_mark_busy(dev, to_intel_framebuffer(crtc->fb)->obj);
4581}
4582
Jesse Barnes79e53942008-11-07 14:24:08 -08004583static int intel_crtc_cursor_set(struct drm_crtc *crtc,
Chris Wilson05394f32010-11-08 19:18:58 +00004584 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08004585 uint32_t handle,
4586 uint32_t width, uint32_t height)
4587{
4588 struct drm_device *dev = crtc->dev;
4589 struct drm_i915_private *dev_priv = dev->dev_private;
4590 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00004591 struct drm_i915_gem_object *obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004592 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004593 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08004594
Zhao Yakui28c97732009-10-09 11:39:41 +08004595 DRM_DEBUG_KMS("\n");
Jesse Barnes79e53942008-11-07 14:24:08 -08004596
4597 /* if we want to turn off the cursor ignore width and height */
4598 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08004599 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004600 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00004601 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10004602 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004603 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08004604 }
4605
4606 /* Currently we only support 64x64 cursors */
4607 if (width != 64 || height != 64) {
4608 DRM_ERROR("we currently only support 64x64 cursors\n");
4609 return -EINVAL;
4610 }
4611
Chris Wilson05394f32010-11-08 19:18:58 +00004612 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
4613 if (!obj)
Jesse Barnes79e53942008-11-07 14:24:08 -08004614 return -ENOENT;
4615
Chris Wilson05394f32010-11-08 19:18:58 +00004616 if (obj->base.size < width * height * 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004617 DRM_ERROR("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10004618 ret = -ENOMEM;
4619 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08004620 }
4621
Dave Airlie71acb5e2008-12-30 20:31:46 +10004622 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05004623 mutex_lock(&dev->struct_mutex);
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05004624 if (!dev_priv->info->cursor_needs_physical) {
Chris Wilsond9e86c02010-11-10 16:40:20 +00004625 if (obj->tiling_mode) {
4626 DRM_ERROR("cursor cannot be tiled\n");
4627 ret = -EINVAL;
4628 goto fail_locked;
4629 }
4630
Chris Wilson05394f32010-11-08 19:18:58 +00004631 ret = i915_gem_object_pin(obj, PAGE_SIZE, true);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004632 if (ret) {
4633 DRM_ERROR("failed to pin cursor bo\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05004634 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004635 }
Chris Wilsone7b526b2010-06-02 08:30:48 +01004636
Chris Wilson05394f32010-11-08 19:18:58 +00004637 ret = i915_gem_object_set_to_gtt_domain(obj, 0);
Chris Wilsone7b526b2010-06-02 08:30:48 +01004638 if (ret) {
4639 DRM_ERROR("failed to move cursor bo into the GTT\n");
4640 goto fail_unpin;
4641 }
4642
Chris Wilsond9e86c02010-11-10 16:40:20 +00004643 ret = i915_gem_object_put_fence(obj);
4644 if (ret) {
4645 DRM_ERROR("failed to move cursor bo into the GTT\n");
4646 goto fail_unpin;
4647 }
4648
Chris Wilson05394f32010-11-08 19:18:58 +00004649 addr = obj->gtt_offset;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004650 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004651 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson05394f32010-11-08 19:18:58 +00004652 ret = i915_gem_attach_phys_object(dev, obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004653 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
4654 align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004655 if (ret) {
4656 DRM_ERROR("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05004657 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004658 }
Chris Wilson05394f32010-11-08 19:18:58 +00004659 addr = obj->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004660 }
4661
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004662 if (IS_GEN2(dev))
Jesse Barnes14b60392009-05-20 16:47:08 -04004663 I915_WRITE(CURSIZE, (height << 12) | width);
4664
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004665 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004666 if (intel_crtc->cursor_bo) {
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05004667 if (dev_priv->info->cursor_needs_physical) {
Chris Wilson05394f32010-11-08 19:18:58 +00004668 if (intel_crtc->cursor_bo != obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004669 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
4670 } else
4671 i915_gem_object_unpin(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00004672 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004673 }
Jesse Barnes80824002009-09-10 15:28:06 -07004674
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05004675 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004676
4677 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00004678 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004679 intel_crtc->cursor_width = width;
4680 intel_crtc->cursor_height = height;
4681
Chris Wilson6b383a72010-09-13 13:54:26 +01004682 intel_crtc_update_cursor(crtc, true);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004683
Jesse Barnes79e53942008-11-07 14:24:08 -08004684 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01004685fail_unpin:
Chris Wilson05394f32010-11-08 19:18:58 +00004686 i915_gem_object_unpin(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05004687fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10004688 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00004689fail:
Chris Wilson05394f32010-11-08 19:18:58 +00004690 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10004691 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08004692}
4693
4694static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
4695{
Jesse Barnes79e53942008-11-07 14:24:08 -08004696 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08004697
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004698 intel_crtc->cursor_x = x;
4699 intel_crtc->cursor_y = y;
Jesse Barnes652c3932009-08-17 13:31:43 -07004700
Chris Wilson6b383a72010-09-13 13:54:26 +01004701 intel_crtc_update_cursor(crtc, true);
Jesse Barnes79e53942008-11-07 14:24:08 -08004702
4703 return 0;
4704}
4705
4706/** Sets the color ramps on behalf of RandR */
4707void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
4708 u16 blue, int regno)
4709{
4710 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4711
4712 intel_crtc->lut_r[regno] = red >> 8;
4713 intel_crtc->lut_g[regno] = green >> 8;
4714 intel_crtc->lut_b[regno] = blue >> 8;
4715}
4716
Dave Airlieb8c00ac2009-10-06 13:54:01 +10004717void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
4718 u16 *blue, int regno)
4719{
4720 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4721
4722 *red = intel_crtc->lut_r[regno] << 8;
4723 *green = intel_crtc->lut_g[regno] << 8;
4724 *blue = intel_crtc->lut_b[regno] << 8;
4725}
4726
Jesse Barnes79e53942008-11-07 14:24:08 -08004727static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01004728 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08004729{
James Simmons72034252010-08-03 01:33:19 +01004730 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08004731 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08004732
James Simmons72034252010-08-03 01:33:19 +01004733 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004734 intel_crtc->lut_r[i] = red[i] >> 8;
4735 intel_crtc->lut_g[i] = green[i] >> 8;
4736 intel_crtc->lut_b[i] = blue[i] >> 8;
4737 }
4738
4739 intel_crtc_load_lut(crtc);
4740}
4741
4742/**
4743 * Get a pipe with a simple mode set on it for doing load-based monitor
4744 * detection.
4745 *
4746 * It will be up to the load-detect code to adjust the pipe as appropriate for
Eric Anholtc751ce42010-03-25 11:48:48 -07004747 * its requirements. The pipe will be connected to no other encoders.
Jesse Barnes79e53942008-11-07 14:24:08 -08004748 *
Eric Anholtc751ce42010-03-25 11:48:48 -07004749 * Currently this code will only succeed if there is a pipe with no encoders
Jesse Barnes79e53942008-11-07 14:24:08 -08004750 * configured for it. In the future, it could choose to temporarily disable
4751 * some outputs to free up a pipe for its use.
4752 *
4753 * \return crtc, or NULL if no pipes are available.
4754 */
4755
4756/* VESA 640x480x72Hz mode to set on the pipe */
4757static struct drm_display_mode load_detect_mode = {
4758 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
4759 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
4760};
4761
Eric Anholt21d40d32010-03-25 11:11:14 -07004762struct drm_crtc *intel_get_load_detect_pipe(struct intel_encoder *intel_encoder,
Zhenyu Wangc1c43972010-03-30 14:39:30 +08004763 struct drm_connector *connector,
Jesse Barnes79e53942008-11-07 14:24:08 -08004764 struct drm_display_mode *mode,
4765 int *dpms_mode)
4766{
4767 struct intel_crtc *intel_crtc;
4768 struct drm_crtc *possible_crtc;
4769 struct drm_crtc *supported_crtc =NULL;
Chris Wilson4ef69c72010-09-09 15:14:28 +01004770 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08004771 struct drm_crtc *crtc = NULL;
4772 struct drm_device *dev = encoder->dev;
4773 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
4774 struct drm_crtc_helper_funcs *crtc_funcs;
4775 int i = -1;
4776
4777 /*
4778 * Algorithm gets a little messy:
4779 * - if the connector already has an assigned crtc, use it (but make
4780 * sure it's on first)
4781 * - try to find the first unused crtc that can drive this connector,
4782 * and use that if we find one
4783 * - if there are no unused crtcs available, try to use the first
4784 * one we found that supports the connector
4785 */
4786
4787 /* See if we already have a CRTC for this connector */
4788 if (encoder->crtc) {
4789 crtc = encoder->crtc;
4790 /* Make sure the crtc and connector are running */
4791 intel_crtc = to_intel_crtc(crtc);
4792 *dpms_mode = intel_crtc->dpms_mode;
4793 if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
4794 crtc_funcs = crtc->helper_private;
4795 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
4796 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
4797 }
4798 return crtc;
4799 }
4800
4801 /* Find an unused one (if possible) */
4802 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
4803 i++;
4804 if (!(encoder->possible_crtcs & (1 << i)))
4805 continue;
4806 if (!possible_crtc->enabled) {
4807 crtc = possible_crtc;
4808 break;
4809 }
4810 if (!supported_crtc)
4811 supported_crtc = possible_crtc;
4812 }
4813
4814 /*
4815 * If we didn't find an unused CRTC, don't use any.
4816 */
4817 if (!crtc) {
4818 return NULL;
4819 }
4820
4821 encoder->crtc = crtc;
Zhenyu Wangc1c43972010-03-30 14:39:30 +08004822 connector->encoder = encoder;
Eric Anholt21d40d32010-03-25 11:11:14 -07004823 intel_encoder->load_detect_temp = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08004824
4825 intel_crtc = to_intel_crtc(crtc);
4826 *dpms_mode = intel_crtc->dpms_mode;
4827
4828 if (!crtc->enabled) {
4829 if (!mode)
4830 mode = &load_detect_mode;
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05004831 drm_crtc_helper_set_mode(crtc, mode, 0, 0, crtc->fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08004832 } else {
4833 if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
4834 crtc_funcs = crtc->helper_private;
4835 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
4836 }
4837
4838 /* Add this connector to the crtc */
4839 encoder_funcs->mode_set(encoder, &crtc->mode, &crtc->mode);
4840 encoder_funcs->commit(encoder);
4841 }
4842 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07004843 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08004844
4845 return crtc;
4846}
4847
Zhenyu Wangc1c43972010-03-30 14:39:30 +08004848void intel_release_load_detect_pipe(struct intel_encoder *intel_encoder,
4849 struct drm_connector *connector, int dpms_mode)
Jesse Barnes79e53942008-11-07 14:24:08 -08004850{
Chris Wilson4ef69c72010-09-09 15:14:28 +01004851 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08004852 struct drm_device *dev = encoder->dev;
4853 struct drm_crtc *crtc = encoder->crtc;
4854 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
4855 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
4856
Eric Anholt21d40d32010-03-25 11:11:14 -07004857 if (intel_encoder->load_detect_temp) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004858 encoder->crtc = NULL;
Zhenyu Wangc1c43972010-03-30 14:39:30 +08004859 connector->encoder = NULL;
Eric Anholt21d40d32010-03-25 11:11:14 -07004860 intel_encoder->load_detect_temp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08004861 crtc->enabled = drm_helper_crtc_in_use(crtc);
4862 drm_helper_disable_unused_functions(dev);
4863 }
4864
Eric Anholtc751ce42010-03-25 11:48:48 -07004865 /* Switch crtc and encoder back off if necessary */
Jesse Barnes79e53942008-11-07 14:24:08 -08004866 if (crtc->enabled && dpms_mode != DRM_MODE_DPMS_ON) {
4867 if (encoder->crtc == crtc)
4868 encoder_funcs->dpms(encoder, dpms_mode);
4869 crtc_funcs->dpms(crtc, dpms_mode);
4870 }
4871}
4872
4873/* Returns the clock of the currently programmed mode of the given pipe. */
4874static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
4875{
4876 struct drm_i915_private *dev_priv = dev->dev_private;
4877 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4878 int pipe = intel_crtc->pipe;
4879 u32 dpll = I915_READ((pipe == 0) ? DPLL_A : DPLL_B);
4880 u32 fp;
4881 intel_clock_t clock;
4882
4883 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
4884 fp = I915_READ((pipe == 0) ? FPA0 : FPB0);
4885 else
4886 fp = I915_READ((pipe == 0) ? FPA1 : FPB1);
4887
4888 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05004889 if (IS_PINEVIEW(dev)) {
4890 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
4891 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08004892 } else {
4893 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
4894 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
4895 }
4896
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004897 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05004898 if (IS_PINEVIEW(dev))
4899 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
4900 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08004901 else
4902 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08004903 DPLL_FPA01_P1_POST_DIV_SHIFT);
4904
4905 switch (dpll & DPLL_MODE_MASK) {
4906 case DPLLB_MODE_DAC_SERIAL:
4907 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
4908 5 : 10;
4909 break;
4910 case DPLLB_MODE_LVDS:
4911 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
4912 7 : 14;
4913 break;
4914 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08004915 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08004916 "mode\n", (int)(dpll & DPLL_MODE_MASK));
4917 return 0;
4918 }
4919
4920 /* XXX: Handle the 100Mhz refclk */
Shaohua Li21778322009-02-23 15:19:16 +08004921 intel_clock(dev, 96000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08004922 } else {
4923 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
4924
4925 if (is_lvds) {
4926 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
4927 DPLL_FPA01_P1_POST_DIV_SHIFT);
4928 clock.p2 = 14;
4929
4930 if ((dpll & PLL_REF_INPUT_MASK) ==
4931 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
4932 /* XXX: might not be 66MHz */
Shaohua Li21778322009-02-23 15:19:16 +08004933 intel_clock(dev, 66000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08004934 } else
Shaohua Li21778322009-02-23 15:19:16 +08004935 intel_clock(dev, 48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08004936 } else {
4937 if (dpll & PLL_P1_DIVIDE_BY_TWO)
4938 clock.p1 = 2;
4939 else {
4940 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
4941 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
4942 }
4943 if (dpll & PLL_P2_DIVIDE_BY_4)
4944 clock.p2 = 4;
4945 else
4946 clock.p2 = 2;
4947
Shaohua Li21778322009-02-23 15:19:16 +08004948 intel_clock(dev, 48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08004949 }
4950 }
4951
4952 /* XXX: It would be nice to validate the clocks, but we can't reuse
4953 * i830PllIsValid() because it relies on the xf86_config connector
4954 * configuration being accurate, which it isn't necessarily.
4955 */
4956
4957 return clock.dot;
4958}
4959
4960/** Returns the currently programmed mode of the given pipe. */
4961struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
4962 struct drm_crtc *crtc)
4963{
4964 struct drm_i915_private *dev_priv = dev->dev_private;
4965 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4966 int pipe = intel_crtc->pipe;
4967 struct drm_display_mode *mode;
4968 int htot = I915_READ((pipe == 0) ? HTOTAL_A : HTOTAL_B);
4969 int hsync = I915_READ((pipe == 0) ? HSYNC_A : HSYNC_B);
4970 int vtot = I915_READ((pipe == 0) ? VTOTAL_A : VTOTAL_B);
4971 int vsync = I915_READ((pipe == 0) ? VSYNC_A : VSYNC_B);
4972
4973 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
4974 if (!mode)
4975 return NULL;
4976
4977 mode->clock = intel_crtc_clock_get(dev, crtc);
4978 mode->hdisplay = (htot & 0xffff) + 1;
4979 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
4980 mode->hsync_start = (hsync & 0xffff) + 1;
4981 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
4982 mode->vdisplay = (vtot & 0xffff) + 1;
4983 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
4984 mode->vsync_start = (vsync & 0xffff) + 1;
4985 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
4986
4987 drm_mode_set_name(mode);
4988 drm_mode_set_crtcinfo(mode, 0);
4989
4990 return mode;
4991}
4992
Jesse Barnes652c3932009-08-17 13:31:43 -07004993#define GPU_IDLE_TIMEOUT 500 /* ms */
4994
4995/* When this timer fires, we've been idle for awhile */
4996static void intel_gpu_idle_timer(unsigned long arg)
4997{
4998 struct drm_device *dev = (struct drm_device *)arg;
4999 drm_i915_private_t *dev_priv = dev->dev_private;
5000
Chris Wilsonff7ea4c2010-12-08 09:43:41 +00005001 if (!list_empty(&dev_priv->mm.active_list)) {
5002 /* Still processing requests, so just re-arm the timer. */
5003 mod_timer(&dev_priv->idle_timer, jiffies +
5004 msecs_to_jiffies(GPU_IDLE_TIMEOUT));
5005 return;
5006 }
Jesse Barnes652c3932009-08-17 13:31:43 -07005007
Chris Wilsonff7ea4c2010-12-08 09:43:41 +00005008 dev_priv->busy = false;
Eric Anholt01dfba92009-09-06 15:18:53 -07005009 queue_work(dev_priv->wq, &dev_priv->idle_work);
Jesse Barnes652c3932009-08-17 13:31:43 -07005010}
5011
Jesse Barnes652c3932009-08-17 13:31:43 -07005012#define CRTC_IDLE_TIMEOUT 1000 /* ms */
5013
5014static void intel_crtc_idle_timer(unsigned long arg)
5015{
5016 struct intel_crtc *intel_crtc = (struct intel_crtc *)arg;
5017 struct drm_crtc *crtc = &intel_crtc->base;
5018 drm_i915_private_t *dev_priv = crtc->dev->dev_private;
Chris Wilsonff7ea4c2010-12-08 09:43:41 +00005019 struct intel_framebuffer *intel_fb;
5020
5021 intel_fb = to_intel_framebuffer(crtc->fb);
5022 if (intel_fb && intel_fb->obj->active) {
5023 /* The framebuffer is still being accessed by the GPU. */
5024 mod_timer(&intel_crtc->idle_timer, jiffies +
5025 msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
5026 return;
5027 }
Jesse Barnes652c3932009-08-17 13:31:43 -07005028
Jesse Barnes652c3932009-08-17 13:31:43 -07005029 intel_crtc->busy = false;
Eric Anholt01dfba92009-09-06 15:18:53 -07005030 queue_work(dev_priv->wq, &dev_priv->idle_work);
Jesse Barnes652c3932009-08-17 13:31:43 -07005031}
5032
Daniel Vetter3dec0092010-08-20 21:40:52 +02005033static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07005034{
5035 struct drm_device *dev = crtc->dev;
5036 drm_i915_private_t *dev_priv = dev->dev_private;
5037 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5038 int pipe = intel_crtc->pipe;
5039 int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
5040 int dpll = I915_READ(dpll_reg);
5041
Eric Anholtbad720f2009-10-22 16:11:14 -07005042 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07005043 return;
5044
5045 if (!dev_priv->lvds_downclock_avail)
5046 return;
5047
5048 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08005049 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07005050
5051 /* Unlock panel regs */
Jesse Barnes4a655f02010-07-22 13:18:18 -07005052 I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) |
5053 PANEL_UNLOCK_REGS);
Jesse Barnes652c3932009-08-17 13:31:43 -07005054
5055 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
5056 I915_WRITE(dpll_reg, dpll);
5057 dpll = I915_READ(dpll_reg);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07005058 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07005059 dpll = I915_READ(dpll_reg);
5060 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08005061 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07005062
5063 /* ...and lock them again */
5064 I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
5065 }
5066
5067 /* Schedule downclock */
Daniel Vetter3dec0092010-08-20 21:40:52 +02005068 mod_timer(&intel_crtc->idle_timer, jiffies +
5069 msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
Jesse Barnes652c3932009-08-17 13:31:43 -07005070}
5071
5072static void intel_decrease_pllclock(struct drm_crtc *crtc)
5073{
5074 struct drm_device *dev = crtc->dev;
5075 drm_i915_private_t *dev_priv = dev->dev_private;
5076 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5077 int pipe = intel_crtc->pipe;
5078 int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
5079 int dpll = I915_READ(dpll_reg);
5080
Eric Anholtbad720f2009-10-22 16:11:14 -07005081 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07005082 return;
5083
5084 if (!dev_priv->lvds_downclock_avail)
5085 return;
5086
5087 /*
5088 * Since this is called by a timer, we should never get here in
5089 * the manual case.
5090 */
5091 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08005092 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07005093
5094 /* Unlock panel regs */
Jesse Barnes4a655f02010-07-22 13:18:18 -07005095 I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) |
5096 PANEL_UNLOCK_REGS);
Jesse Barnes652c3932009-08-17 13:31:43 -07005097
5098 dpll |= DISPLAY_RATE_SELECT_FPA1;
5099 I915_WRITE(dpll_reg, dpll);
5100 dpll = I915_READ(dpll_reg);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07005101 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07005102 dpll = I915_READ(dpll_reg);
5103 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08005104 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07005105
5106 /* ...and lock them again */
5107 I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
5108 }
5109
5110}
5111
5112/**
5113 * intel_idle_update - adjust clocks for idleness
5114 * @work: work struct
5115 *
5116 * Either the GPU or display (or both) went idle. Check the busy status
5117 * here and adjust the CRTC and GPU clocks as necessary.
5118 */
5119static void intel_idle_update(struct work_struct *work)
5120{
5121 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
5122 idle_work);
5123 struct drm_device *dev = dev_priv->dev;
5124 struct drm_crtc *crtc;
5125 struct intel_crtc *intel_crtc;
Li Peng45ac22c2010-06-12 23:38:35 +08005126 int enabled = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07005127
5128 if (!i915_powersave)
5129 return;
5130
5131 mutex_lock(&dev->struct_mutex);
5132
Jesse Barnes7648fa92010-05-20 14:28:11 -07005133 i915_update_gfx_val(dev_priv);
5134
Jesse Barnes652c3932009-08-17 13:31:43 -07005135 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
5136 /* Skip inactive CRTCs */
5137 if (!crtc->fb)
5138 continue;
5139
Li Peng45ac22c2010-06-12 23:38:35 +08005140 enabled++;
Jesse Barnes652c3932009-08-17 13:31:43 -07005141 intel_crtc = to_intel_crtc(crtc);
5142 if (!intel_crtc->busy)
5143 intel_decrease_pllclock(crtc);
5144 }
5145
Li Peng45ac22c2010-06-12 23:38:35 +08005146 if ((enabled == 1) && (IS_I945G(dev) || IS_I945GM(dev))) {
5147 DRM_DEBUG_DRIVER("enable memory self refresh on 945\n");
5148 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
5149 }
5150
Jesse Barnes652c3932009-08-17 13:31:43 -07005151 mutex_unlock(&dev->struct_mutex);
5152}
5153
5154/**
5155 * intel_mark_busy - mark the GPU and possibly the display busy
5156 * @dev: drm device
5157 * @obj: object we're operating on
5158 *
5159 * Callers can use this function to indicate that the GPU is busy processing
5160 * commands. If @obj matches one of the CRTC objects (i.e. it's a scanout
5161 * buffer), we'll also mark the display as busy, so we know to increase its
5162 * clock frequency.
5163 */
Chris Wilson05394f32010-11-08 19:18:58 +00005164void intel_mark_busy(struct drm_device *dev, struct drm_i915_gem_object *obj)
Jesse Barnes652c3932009-08-17 13:31:43 -07005165{
5166 drm_i915_private_t *dev_priv = dev->dev_private;
5167 struct drm_crtc *crtc = NULL;
5168 struct intel_framebuffer *intel_fb;
5169 struct intel_crtc *intel_crtc;
5170
Zhenyu Wang5e17ee72009-09-03 09:30:06 +08005171 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5172 return;
5173
Li Peng060e6452010-02-10 01:54:24 +08005174 if (!dev_priv->busy) {
5175 if (IS_I945G(dev) || IS_I945GM(dev)) {
5176 u32 fw_blc_self;
Li Pengee980b82010-01-27 19:01:11 +08005177
Li Peng060e6452010-02-10 01:54:24 +08005178 DRM_DEBUG_DRIVER("disable memory self refresh on 945\n");
5179 fw_blc_self = I915_READ(FW_BLC_SELF);
5180 fw_blc_self &= ~FW_BLC_SELF_EN;
5181 I915_WRITE(FW_BLC_SELF, fw_blc_self | FW_BLC_SELF_EN_MASK);
5182 }
Chris Wilson28cf7982009-11-30 01:08:56 +00005183 dev_priv->busy = true;
Li Peng060e6452010-02-10 01:54:24 +08005184 } else
Chris Wilson28cf7982009-11-30 01:08:56 +00005185 mod_timer(&dev_priv->idle_timer, jiffies +
5186 msecs_to_jiffies(GPU_IDLE_TIMEOUT));
Jesse Barnes652c3932009-08-17 13:31:43 -07005187
5188 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
5189 if (!crtc->fb)
5190 continue;
5191
5192 intel_crtc = to_intel_crtc(crtc);
5193 intel_fb = to_intel_framebuffer(crtc->fb);
5194 if (intel_fb->obj == obj) {
5195 if (!intel_crtc->busy) {
Li Peng060e6452010-02-10 01:54:24 +08005196 if (IS_I945G(dev) || IS_I945GM(dev)) {
5197 u32 fw_blc_self;
5198
5199 DRM_DEBUG_DRIVER("disable memory self refresh on 945\n");
5200 fw_blc_self = I915_READ(FW_BLC_SELF);
5201 fw_blc_self &= ~FW_BLC_SELF_EN;
5202 I915_WRITE(FW_BLC_SELF, fw_blc_self | FW_BLC_SELF_EN_MASK);
5203 }
Jesse Barnes652c3932009-08-17 13:31:43 -07005204 /* Non-busy -> busy, upclock */
Daniel Vetter3dec0092010-08-20 21:40:52 +02005205 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07005206 intel_crtc->busy = true;
5207 } else {
5208 /* Busy -> busy, put off timer */
5209 mod_timer(&intel_crtc->idle_timer, jiffies +
5210 msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
5211 }
5212 }
5213 }
5214}
5215
Jesse Barnes79e53942008-11-07 14:24:08 -08005216static void intel_crtc_destroy(struct drm_crtc *crtc)
5217{
5218 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02005219 struct drm_device *dev = crtc->dev;
5220 struct intel_unpin_work *work;
5221 unsigned long flags;
5222
5223 spin_lock_irqsave(&dev->event_lock, flags);
5224 work = intel_crtc->unpin_work;
5225 intel_crtc->unpin_work = NULL;
5226 spin_unlock_irqrestore(&dev->event_lock, flags);
5227
5228 if (work) {
5229 cancel_work_sync(&work->work);
5230 kfree(work);
5231 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005232
5233 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02005234
Jesse Barnes79e53942008-11-07 14:24:08 -08005235 kfree(intel_crtc);
5236}
5237
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005238static void intel_unpin_work_fn(struct work_struct *__work)
5239{
5240 struct intel_unpin_work *work =
5241 container_of(__work, struct intel_unpin_work, work);
5242
5243 mutex_lock(&work->dev->struct_mutex);
Jesse Barnesb1b87f62010-01-26 14:40:05 -08005244 i915_gem_object_unpin(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00005245 drm_gem_object_unreference(&work->pending_flip_obj->base);
5246 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00005247
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005248 mutex_unlock(&work->dev->struct_mutex);
5249 kfree(work);
5250}
5251
Jesse Barnes1afe3e92010-03-26 10:35:20 -07005252static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01005253 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005254{
5255 drm_i915_private_t *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005256 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5257 struct intel_unpin_work *work;
Chris Wilson05394f32010-11-08 19:18:58 +00005258 struct drm_i915_gem_object *obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005259 struct drm_pending_vblank_event *e;
Mario Kleiner49b14a52010-12-09 07:00:07 +01005260 struct timeval tnow, tvbl;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005261 unsigned long flags;
5262
5263 /* Ignore early vblank irqs */
5264 if (intel_crtc == NULL)
5265 return;
5266
Mario Kleiner49b14a52010-12-09 07:00:07 +01005267 do_gettimeofday(&tnow);
5268
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005269 spin_lock_irqsave(&dev->event_lock, flags);
5270 work = intel_crtc->unpin_work;
5271 if (work == NULL || !work->pending) {
5272 spin_unlock_irqrestore(&dev->event_lock, flags);
5273 return;
5274 }
5275
5276 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005277
5278 if (work->event) {
5279 e = work->event;
Mario Kleiner49b14a52010-12-09 07:00:07 +01005280 e->event.sequence = drm_vblank_count_and_time(dev, intel_crtc->pipe, &tvbl);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01005281
5282 /* Called before vblank count and timestamps have
5283 * been updated for the vblank interval of flip
5284 * completion? Need to increment vblank count and
5285 * add one videorefresh duration to returned timestamp
Mario Kleiner49b14a52010-12-09 07:00:07 +01005286 * to account for this. We assume this happened if we
5287 * get called over 0.9 frame durations after the last
5288 * timestamped vblank.
5289 *
5290 * This calculation can not be used with vrefresh rates
5291 * below 5Hz (10Hz to be on the safe side) without
5292 * promoting to 64 integers.
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01005293 */
Mario Kleiner49b14a52010-12-09 07:00:07 +01005294 if (10 * (timeval_to_ns(&tnow) - timeval_to_ns(&tvbl)) >
5295 9 * crtc->framedur_ns) {
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01005296 e->event.sequence++;
Mario Kleiner49b14a52010-12-09 07:00:07 +01005297 tvbl = ns_to_timeval(timeval_to_ns(&tvbl) +
5298 crtc->framedur_ns);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01005299 }
5300
Mario Kleiner49b14a52010-12-09 07:00:07 +01005301 e->event.tv_sec = tvbl.tv_sec;
5302 e->event.tv_usec = tvbl.tv_usec;
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01005303
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005304 list_add_tail(&e->base.link,
5305 &e->base.file_priv->event_list);
5306 wake_up_interruptible(&e->base.file_priv->event_wait);
5307 }
5308
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01005309 drm_vblank_put(dev, intel_crtc->pipe);
5310
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005311 spin_unlock_irqrestore(&dev->event_lock, flags);
5312
Chris Wilson05394f32010-11-08 19:18:58 +00005313 obj = work->old_fb_obj;
Chris Wilsond9e86c02010-11-10 16:40:20 +00005314
Chris Wilsone59f2ba2010-10-07 17:28:15 +01005315 atomic_clear_mask(1 << intel_crtc->plane,
Chris Wilson05394f32010-11-08 19:18:58 +00005316 &obj->pending_flip.counter);
5317 if (atomic_read(&obj->pending_flip) == 0)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005318 wake_up(&dev_priv->pending_flip_queue);
Chris Wilsond9e86c02010-11-10 16:40:20 +00005319
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005320 schedule_work(&work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07005321
5322 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005323}
5324
Jesse Barnes1afe3e92010-03-26 10:35:20 -07005325void intel_finish_page_flip(struct drm_device *dev, int pipe)
5326{
5327 drm_i915_private_t *dev_priv = dev->dev_private;
5328 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
5329
Mario Kleiner49b14a52010-12-09 07:00:07 +01005330 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07005331}
5332
5333void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
5334{
5335 drm_i915_private_t *dev_priv = dev->dev_private;
5336 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
5337
Mario Kleiner49b14a52010-12-09 07:00:07 +01005338 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07005339}
5340
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005341void intel_prepare_page_flip(struct drm_device *dev, int plane)
5342{
5343 drm_i915_private_t *dev_priv = dev->dev_private;
5344 struct intel_crtc *intel_crtc =
5345 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
5346 unsigned long flags;
5347
5348 spin_lock_irqsave(&dev->event_lock, flags);
Jesse Barnesde3f4402010-01-14 13:18:02 -08005349 if (intel_crtc->unpin_work) {
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01005350 if ((++intel_crtc->unpin_work->pending) > 1)
5351 DRM_ERROR("Prepared flip multiple times\n");
Jesse Barnesde3f4402010-01-14 13:18:02 -08005352 } else {
5353 DRM_DEBUG_DRIVER("preparing flip with no unpin work?\n");
5354 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005355 spin_unlock_irqrestore(&dev->event_lock, flags);
5356}
5357
5358static int intel_crtc_page_flip(struct drm_crtc *crtc,
5359 struct drm_framebuffer *fb,
5360 struct drm_pending_vblank_event *event)
5361{
5362 struct drm_device *dev = crtc->dev;
5363 struct drm_i915_private *dev_priv = dev->dev_private;
5364 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00005365 struct drm_i915_gem_object *obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005366 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5367 struct intel_unpin_work *work;
Jesse Barnesbe9a3db2010-07-23 12:03:37 -07005368 unsigned long flags, offset;
Chris Wilson52e68632010-08-08 10:15:59 +01005369 int pipe = intel_crtc->pipe;
Chris Wilson20f0cd52010-09-23 11:00:38 +01005370 u32 pf, pipesrc;
Chris Wilson52e68632010-08-08 10:15:59 +01005371 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005372
5373 work = kzalloc(sizeof *work, GFP_KERNEL);
5374 if (work == NULL)
5375 return -ENOMEM;
5376
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005377 work->event = event;
5378 work->dev = crtc->dev;
5379 intel_fb = to_intel_framebuffer(crtc->fb);
Jesse Barnesb1b87f62010-01-26 14:40:05 -08005380 work->old_fb_obj = intel_fb->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005381 INIT_WORK(&work->work, intel_unpin_work_fn);
5382
5383 /* We borrow the event spin lock for protecting unpin_work */
5384 spin_lock_irqsave(&dev->event_lock, flags);
5385 if (intel_crtc->unpin_work) {
5386 spin_unlock_irqrestore(&dev->event_lock, flags);
5387 kfree(work);
Chris Wilson468f0b42010-05-27 13:18:13 +01005388
5389 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005390 return -EBUSY;
5391 }
5392 intel_crtc->unpin_work = work;
5393 spin_unlock_irqrestore(&dev->event_lock, flags);
5394
5395 intel_fb = to_intel_framebuffer(fb);
5396 obj = intel_fb->obj;
5397
Chris Wilson468f0b42010-05-27 13:18:13 +01005398 mutex_lock(&dev->struct_mutex);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00005399 ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
Chris Wilson96b099f2010-06-07 14:03:04 +01005400 if (ret)
5401 goto cleanup_work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005402
Jesse Barnes75dfca80a2010-02-10 15:09:44 -08005403 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00005404 drm_gem_object_reference(&work->old_fb_obj->base);
5405 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005406
5407 crtc->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01005408
5409 ret = drm_vblank_get(dev, intel_crtc->pipe);
5410 if (ret)
5411 goto cleanup_objs;
5412
Chris Wilsonc7f9f9a2010-09-19 15:05:13 +01005413 if (IS_GEN3(dev) || IS_GEN2(dev)) {
5414 u32 flip_mask;
5415
5416 /* Can't queue multiple flips, so wait for the previous
5417 * one to finish before executing the next.
5418 */
Chris Wilsone1f99ce2010-10-27 12:45:26 +01005419 ret = BEGIN_LP_RING(2);
5420 if (ret)
5421 goto cleanup_objs;
5422
Chris Wilsonc7f9f9a2010-09-19 15:05:13 +01005423 if (intel_crtc->plane)
5424 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
5425 else
5426 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
5427 OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);
5428 OUT_RING(MI_NOOP);
Daniel Vetter6146b3d2010-08-04 21:22:10 +02005429 ADVANCE_LP_RING();
5430 }
Jesse Barnes83f7fd02010-04-05 14:03:51 -07005431
Chris Wilsone1f99ce2010-10-27 12:45:26 +01005432 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01005433
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01005434 work->enable_stall_check = true;
5435
Jesse Barnesbe9a3db2010-07-23 12:03:37 -07005436 /* Offset into the new buffer for cases of shared fbs between CRTCs */
Chris Wilson52e68632010-08-08 10:15:59 +01005437 offset = crtc->y * fb->pitch + crtc->x * fb->bits_per_pixel/8;
Jesse Barnesbe9a3db2010-07-23 12:03:37 -07005438
Chris Wilsone1f99ce2010-10-27 12:45:26 +01005439 ret = BEGIN_LP_RING(4);
5440 if (ret)
5441 goto cleanup_objs;
5442
5443 /* Block clients from rendering to the new back buffer until
5444 * the flip occurs and the object is no longer visible.
5445 */
Chris Wilson05394f32010-11-08 19:18:58 +00005446 atomic_add(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01005447
5448 switch (INTEL_INFO(dev)->gen) {
Chris Wilson52e68632010-08-08 10:15:59 +01005449 case 2:
Jesse Barnes1afe3e92010-03-26 10:35:20 -07005450 OUT_RING(MI_DISPLAY_FLIP |
5451 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
5452 OUT_RING(fb->pitch);
Chris Wilson05394f32010-11-08 19:18:58 +00005453 OUT_RING(obj->gtt_offset + offset);
Chris Wilson52e68632010-08-08 10:15:59 +01005454 OUT_RING(MI_NOOP);
5455 break;
5456
5457 case 3:
Jesse Barnes1afe3e92010-03-26 10:35:20 -07005458 OUT_RING(MI_DISPLAY_FLIP_I915 |
5459 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
5460 OUT_RING(fb->pitch);
Chris Wilson05394f32010-11-08 19:18:58 +00005461 OUT_RING(obj->gtt_offset + offset);
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08005462 OUT_RING(MI_NOOP);
Chris Wilson52e68632010-08-08 10:15:59 +01005463 break;
5464
5465 case 4:
5466 case 5:
5467 /* i965+ uses the linear or tiled offsets from the
5468 * Display Registers (which do not change across a page-flip)
5469 * so we need only reprogram the base address.
5470 */
Daniel Vetter69d0b962010-08-04 21:22:09 +02005471 OUT_RING(MI_DISPLAY_FLIP |
5472 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
5473 OUT_RING(fb->pitch);
Chris Wilson05394f32010-11-08 19:18:58 +00005474 OUT_RING(obj->gtt_offset | obj->tiling_mode);
Chris Wilson52e68632010-08-08 10:15:59 +01005475
5476 /* XXX Enabling the panel-fitter across page-flip is so far
5477 * untested on non-native modes, so ignore it for now.
5478 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
5479 */
5480 pf = 0;
5481 pipesrc = I915_READ(pipe == 0 ? PIPEASRC : PIPEBSRC) & 0x0fff0fff;
5482 OUT_RING(pf | pipesrc);
5483 break;
5484
5485 case 6:
5486 OUT_RING(MI_DISPLAY_FLIP |
5487 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Chris Wilson05394f32010-11-08 19:18:58 +00005488 OUT_RING(fb->pitch | obj->tiling_mode);
5489 OUT_RING(obj->gtt_offset);
Chris Wilson52e68632010-08-08 10:15:59 +01005490
5491 pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
5492 pipesrc = I915_READ(pipe == 0 ? PIPEASRC : PIPEBSRC) & 0x0fff0fff;
5493 OUT_RING(pf | pipesrc);
5494 break;
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08005495 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005496 ADVANCE_LP_RING();
5497
5498 mutex_unlock(&dev->struct_mutex);
5499
Jesse Barnese5510fa2010-07-01 16:48:37 -07005500 trace_i915_flip_request(intel_crtc->plane, obj);
5501
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005502 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01005503
5504cleanup_objs:
Chris Wilson05394f32010-11-08 19:18:58 +00005505 drm_gem_object_unreference(&work->old_fb_obj->base);
5506 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01005507cleanup_work:
5508 mutex_unlock(&dev->struct_mutex);
5509
5510 spin_lock_irqsave(&dev->event_lock, flags);
5511 intel_crtc->unpin_work = NULL;
5512 spin_unlock_irqrestore(&dev->event_lock, flags);
5513
5514 kfree(work);
5515
5516 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005517}
5518
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07005519static struct drm_crtc_helper_funcs intel_helper_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -08005520 .dpms = intel_crtc_dpms,
5521 .mode_fixup = intel_crtc_mode_fixup,
5522 .mode_set = intel_crtc_mode_set,
5523 .mode_set_base = intel_pipe_set_base,
Jesse Barnes81255562010-08-02 12:07:50 -07005524 .mode_set_base_atomic = intel_pipe_set_base_atomic,
Dave Airlie068143d2009-10-05 09:58:02 +10005525 .load_lut = intel_crtc_load_lut,
Chris Wilsoncdd59982010-09-08 16:30:16 +01005526 .disable = intel_crtc_disable,
Jesse Barnes79e53942008-11-07 14:24:08 -08005527};
5528
5529static const struct drm_crtc_funcs intel_crtc_funcs = {
5530 .cursor_set = intel_crtc_cursor_set,
5531 .cursor_move = intel_crtc_cursor_move,
5532 .gamma_set = intel_crtc_gamma_set,
5533 .set_config = drm_crtc_helper_set_config,
5534 .destroy = intel_crtc_destroy,
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005535 .page_flip = intel_crtc_page_flip,
Jesse Barnes79e53942008-11-07 14:24:08 -08005536};
5537
Chris Wilson47f1c6c2010-12-03 15:37:31 +00005538static void intel_sanitize_modesetting(struct drm_device *dev,
5539 int pipe, int plane)
5540{
5541 struct drm_i915_private *dev_priv = dev->dev_private;
5542 u32 reg, val;
5543
5544 if (HAS_PCH_SPLIT(dev))
5545 return;
5546
5547 /* Who knows what state these registers were left in by the BIOS or
5548 * grub?
5549 *
5550 * If we leave the registers in a conflicting state (e.g. with the
5551 * display plane reading from the other pipe than the one we intend
5552 * to use) then when we attempt to teardown the active mode, we will
5553 * not disable the pipes and planes in the correct order -- leaving
5554 * a plane reading from a disabled pipe and possibly leading to
5555 * undefined behaviour.
5556 */
5557
5558 reg = DSPCNTR(plane);
5559 val = I915_READ(reg);
5560
5561 if ((val & DISPLAY_PLANE_ENABLE) == 0)
5562 return;
5563 if (!!(val & DISPPLANE_SEL_PIPE_MASK) == pipe)
5564 return;
5565
5566 /* This display plane is active and attached to the other CPU pipe. */
5567 pipe = !pipe;
5568
5569 /* Disable the plane and wait for it to stop reading from the pipe. */
5570 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
5571 intel_flush_display_plane(dev, plane);
5572
5573 if (IS_GEN2(dev))
5574 intel_wait_for_vblank(dev, pipe);
5575
5576 if (pipe == 0 && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
5577 return;
5578
5579 /* Switch off the pipe. */
5580 reg = PIPECONF(pipe);
5581 val = I915_READ(reg);
5582 if (val & PIPECONF_ENABLE) {
5583 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
5584 intel_wait_for_pipe_off(dev, pipe);
5585 }
5586}
Jesse Barnes79e53942008-11-07 14:24:08 -08005587
Hannes Ederb358d0a2008-12-18 21:18:47 +01005588static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -08005589{
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08005590 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08005591 struct intel_crtc *intel_crtc;
5592 int i;
5593
5594 intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
5595 if (intel_crtc == NULL)
5596 return;
5597
5598 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
5599
5600 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -08005601 for (i = 0; i < 256; i++) {
5602 intel_crtc->lut_r[i] = i;
5603 intel_crtc->lut_g[i] = i;
5604 intel_crtc->lut_b[i] = i;
5605 }
5606
Jesse Barnes80824002009-09-10 15:28:06 -07005607 /* Swap pipes & planes for FBC on pre-965 */
5608 intel_crtc->pipe = pipe;
5609 intel_crtc->plane = pipe;
Chris Wilsone2e767a2010-09-13 16:53:12 +01005610 if (IS_MOBILE(dev) && IS_GEN3(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08005611 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +01005612 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07005613 }
5614
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08005615 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
5616 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
5617 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
5618 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
5619
Jesse Barnes79e53942008-11-07 14:24:08 -08005620 intel_crtc->cursor_addr = 0;
Chris Wilson032d2a02010-09-06 16:17:22 +01005621 intel_crtc->dpms_mode = -1;
Chris Wilsone65d9302010-09-13 16:58:39 +01005622 intel_crtc->active = true; /* force the pipe off on setup_init_config */
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07005623
5624 if (HAS_PCH_SPLIT(dev)) {
5625 intel_helper_funcs.prepare = ironlake_crtc_prepare;
5626 intel_helper_funcs.commit = ironlake_crtc_commit;
5627 } else {
5628 intel_helper_funcs.prepare = i9xx_crtc_prepare;
5629 intel_helper_funcs.commit = i9xx_crtc_commit;
5630 }
5631
Jesse Barnes79e53942008-11-07 14:24:08 -08005632 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
5633
Jesse Barnes652c3932009-08-17 13:31:43 -07005634 intel_crtc->busy = false;
5635
5636 setup_timer(&intel_crtc->idle_timer, intel_crtc_idle_timer,
5637 (unsigned long)intel_crtc);
Chris Wilson47f1c6c2010-12-03 15:37:31 +00005638
5639 intel_sanitize_modesetting(dev, intel_crtc->pipe, intel_crtc->plane);
Jesse Barnes79e53942008-11-07 14:24:08 -08005640}
5641
Carl Worth08d7b3d2009-04-29 14:43:54 -07005642int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00005643 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -07005644{
5645 drm_i915_private_t *dev_priv = dev->dev_private;
5646 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +02005647 struct drm_mode_object *drmmode_obj;
5648 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -07005649
5650 if (!dev_priv) {
5651 DRM_ERROR("called with no initialization\n");
5652 return -EINVAL;
5653 }
5654
Daniel Vetterc05422d2009-08-11 16:05:30 +02005655 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
5656 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -07005657
Daniel Vetterc05422d2009-08-11 16:05:30 +02005658 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -07005659 DRM_ERROR("no such CRTC id\n");
5660 return -EINVAL;
5661 }
5662
Daniel Vetterc05422d2009-08-11 16:05:30 +02005663 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
5664 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -07005665
Daniel Vetterc05422d2009-08-11 16:05:30 +02005666 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -07005667}
5668
Zhenyu Wangc5e4df32010-03-30 14:39:27 +08005669static int intel_encoder_clones(struct drm_device *dev, int type_mask)
Jesse Barnes79e53942008-11-07 14:24:08 -08005670{
Chris Wilson4ef69c72010-09-09 15:14:28 +01005671 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08005672 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005673 int entry = 0;
5674
Chris Wilson4ef69c72010-09-09 15:14:28 +01005675 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
5676 if (type_mask & encoder->clone_mask)
Jesse Barnes79e53942008-11-07 14:24:08 -08005677 index_mask |= (1 << entry);
5678 entry++;
5679 }
Chris Wilson4ef69c72010-09-09 15:14:28 +01005680
Jesse Barnes79e53942008-11-07 14:24:08 -08005681 return index_mask;
5682}
5683
Chris Wilson4d302442010-12-14 19:21:29 +00005684static bool has_edp_a(struct drm_device *dev)
5685{
5686 struct drm_i915_private *dev_priv = dev->dev_private;
5687
5688 if (!IS_MOBILE(dev))
5689 return false;
5690
5691 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
5692 return false;
5693
5694 if (IS_GEN5(dev) &&
5695 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
5696 return false;
5697
5698 return true;
5699}
5700
Jesse Barnes79e53942008-11-07 14:24:08 -08005701static void intel_setup_outputs(struct drm_device *dev)
5702{
Eric Anholt725e30a2009-01-22 13:01:02 -08005703 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +01005704 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -04005705 bool dpd_is_edp = false;
Chris Wilsonc5d1b512010-11-29 18:00:23 +00005706 bool has_lvds = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08005707
Zhenyu Wang541998a2009-06-05 15:38:44 +08005708 if (IS_MOBILE(dev) && !IS_I830(dev))
Chris Wilsonc5d1b512010-11-29 18:00:23 +00005709 has_lvds = intel_lvds_init(dev);
5710 if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
5711 /* disable the panel fitter on everything but LVDS */
5712 I915_WRITE(PFIT_CONTROL, 0);
5713 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005714
Eric Anholtbad720f2009-10-22 16:11:14 -07005715 if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -04005716 dpd_is_edp = intel_dpd_is_edp(dev);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08005717
Chris Wilson4d302442010-12-14 19:21:29 +00005718 if (has_edp_a(dev))
Zhenyu Wang32f9d652009-07-24 01:00:32 +08005719 intel_dp_init(dev, DP_A);
5720
Adam Jacksoncb0953d2010-07-16 14:46:29 -04005721 if (dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
5722 intel_dp_init(dev, PCH_DP_D);
5723 }
5724
5725 intel_crt_init(dev);
5726
5727 if (HAS_PCH_SPLIT(dev)) {
5728 int found;
5729
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08005730 if (I915_READ(HDMIB) & PORT_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +08005731 /* PCH SDVOB multiplex with HDMIB */
5732 found = intel_sdvo_init(dev, PCH_SDVOB);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08005733 if (!found)
5734 intel_hdmi_init(dev, HDMIB);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08005735 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
5736 intel_dp_init(dev, PCH_DP_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08005737 }
5738
5739 if (I915_READ(HDMIC) & PORT_DETECTED)
5740 intel_hdmi_init(dev, HDMIC);
5741
5742 if (I915_READ(HDMID) & PORT_DETECTED)
5743 intel_hdmi_init(dev, HDMID);
5744
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08005745 if (I915_READ(PCH_DP_C) & DP_DETECTED)
5746 intel_dp_init(dev, PCH_DP_C);
5747
Adam Jacksoncb0953d2010-07-16 14:46:29 -04005748 if (!dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08005749 intel_dp_init(dev, PCH_DP_D);
5750
Zhenyu Wang103a1962009-11-27 11:44:36 +08005751 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +08005752 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -08005753
Eric Anholt725e30a2009-01-22 13:01:02 -08005754 if (I915_READ(SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005755 DRM_DEBUG_KMS("probing SDVOB\n");
Eric Anholt725e30a2009-01-22 13:01:02 -08005756 found = intel_sdvo_init(dev, SDVOB);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005757 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
5758 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Eric Anholt725e30a2009-01-22 13:01:02 -08005759 intel_hdmi_init(dev, SDVOB);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005760 }
Ma Ling27185ae2009-08-24 13:50:23 +08005761
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005762 if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
5763 DRM_DEBUG_KMS("probing DP_B\n");
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005764 intel_dp_init(dev, DP_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005765 }
Eric Anholt725e30a2009-01-22 13:01:02 -08005766 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -04005767
5768 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -04005769
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005770 if (I915_READ(SDVOB) & SDVO_DETECTED) {
5771 DRM_DEBUG_KMS("probing SDVOC\n");
Eric Anholt725e30a2009-01-22 13:01:02 -08005772 found = intel_sdvo_init(dev, SDVOC);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005773 }
Ma Ling27185ae2009-08-24 13:50:23 +08005774
5775 if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
5776
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005777 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
5778 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Eric Anholt725e30a2009-01-22 13:01:02 -08005779 intel_hdmi_init(dev, SDVOC);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005780 }
5781 if (SUPPORTS_INTEGRATED_DP(dev)) {
5782 DRM_DEBUG_KMS("probing DP_C\n");
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005783 intel_dp_init(dev, DP_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005784 }
Eric Anholt725e30a2009-01-22 13:01:02 -08005785 }
Ma Ling27185ae2009-08-24 13:50:23 +08005786
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005787 if (SUPPORTS_INTEGRATED_DP(dev) &&
5788 (I915_READ(DP_D) & DP_DETECTED)) {
5789 DRM_DEBUG_KMS("probing DP_D\n");
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005790 intel_dp_init(dev, DP_D);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005791 }
Eric Anholtbad720f2009-10-22 16:11:14 -07005792 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08005793 intel_dvo_init(dev);
5794
Zhenyu Wang103a1962009-11-27 11:44:36 +08005795 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08005796 intel_tv_init(dev);
5797
Chris Wilson4ef69c72010-09-09 15:14:28 +01005798 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
5799 encoder->base.possible_crtcs = encoder->crtc_mask;
5800 encoder->base.possible_clones =
5801 intel_encoder_clones(dev, encoder->clone_mask);
Jesse Barnes79e53942008-11-07 14:24:08 -08005802 }
Chris Wilson47356eb2011-01-11 17:06:04 +00005803
5804 intel_panel_setup_backlight(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08005805}
5806
5807static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
5808{
5809 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08005810
5811 drm_framebuffer_cleanup(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00005812 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08005813
5814 kfree(intel_fb);
5815}
5816
5817static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +00005818 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08005819 unsigned int *handle)
5820{
5821 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00005822 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08005823
Chris Wilson05394f32010-11-08 19:18:58 +00005824 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -08005825}
5826
5827static const struct drm_framebuffer_funcs intel_fb_funcs = {
5828 .destroy = intel_user_framebuffer_destroy,
5829 .create_handle = intel_user_framebuffer_create_handle,
5830};
5831
Dave Airlie38651672010-03-30 05:34:13 +00005832int intel_framebuffer_init(struct drm_device *dev,
5833 struct intel_framebuffer *intel_fb,
5834 struct drm_mode_fb_cmd *mode_cmd,
Chris Wilson05394f32010-11-08 19:18:58 +00005835 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -08005836{
Jesse Barnes79e53942008-11-07 14:24:08 -08005837 int ret;
5838
Chris Wilson05394f32010-11-08 19:18:58 +00005839 if (obj->tiling_mode == I915_TILING_Y)
Chris Wilson57cd6502010-08-08 12:34:44 +01005840 return -EINVAL;
5841
5842 if (mode_cmd->pitch & 63)
5843 return -EINVAL;
5844
5845 switch (mode_cmd->bpp) {
5846 case 8:
5847 case 16:
5848 case 24:
5849 case 32:
5850 break;
5851 default:
5852 return -EINVAL;
5853 }
5854
Jesse Barnes79e53942008-11-07 14:24:08 -08005855 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
5856 if (ret) {
5857 DRM_ERROR("framebuffer init failed %d\n", ret);
5858 return ret;
5859 }
5860
5861 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
Jesse Barnes79e53942008-11-07 14:24:08 -08005862 intel_fb->obj = obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08005863 return 0;
5864}
5865
Jesse Barnes79e53942008-11-07 14:24:08 -08005866static struct drm_framebuffer *
5867intel_user_framebuffer_create(struct drm_device *dev,
5868 struct drm_file *filp,
5869 struct drm_mode_fb_cmd *mode_cmd)
5870{
Chris Wilson05394f32010-11-08 19:18:58 +00005871 struct drm_i915_gem_object *obj;
Dave Airlie38651672010-03-30 05:34:13 +00005872 struct intel_framebuffer *intel_fb;
Jesse Barnes79e53942008-11-07 14:24:08 -08005873 int ret;
5874
Chris Wilson05394f32010-11-08 19:18:58 +00005875 obj = to_intel_bo(drm_gem_object_lookup(dev, filp, mode_cmd->handle));
Jesse Barnes79e53942008-11-07 14:24:08 -08005876 if (!obj)
Chris Wilsoncce13ff2010-08-08 13:36:38 +01005877 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -08005878
Dave Airlie38651672010-03-30 05:34:13 +00005879 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
5880 if (!intel_fb)
Chris Wilsoncce13ff2010-08-08 13:36:38 +01005881 return ERR_PTR(-ENOMEM);
Dave Airlie38651672010-03-30 05:34:13 +00005882
Chris Wilson05394f32010-11-08 19:18:58 +00005883 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08005884 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00005885 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie38651672010-03-30 05:34:13 +00005886 kfree(intel_fb);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01005887 return ERR_PTR(ret);
Jesse Barnes79e53942008-11-07 14:24:08 -08005888 }
5889
Dave Airlie38651672010-03-30 05:34:13 +00005890 return &intel_fb->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08005891}
5892
Jesse Barnes79e53942008-11-07 14:24:08 -08005893static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -08005894 .fb_create = intel_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +00005895 .output_poll_changed = intel_fb_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -08005896};
5897
Chris Wilson05394f32010-11-08 19:18:58 +00005898static struct drm_i915_gem_object *
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08005899intel_alloc_context_page(struct drm_device *dev)
Chris Wilson9ea8d052010-01-04 18:57:56 +00005900{
Chris Wilson05394f32010-11-08 19:18:58 +00005901 struct drm_i915_gem_object *ctx;
Chris Wilson9ea8d052010-01-04 18:57:56 +00005902 int ret;
5903
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08005904 ctx = i915_gem_alloc_object(dev, 4096);
5905 if (!ctx) {
Chris Wilson9ea8d052010-01-04 18:57:56 +00005906 DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
5907 return NULL;
5908 }
5909
5910 mutex_lock(&dev->struct_mutex);
Daniel Vetter75e9e912010-11-04 17:11:09 +01005911 ret = i915_gem_object_pin(ctx, 4096, true);
Chris Wilson9ea8d052010-01-04 18:57:56 +00005912 if (ret) {
5913 DRM_ERROR("failed to pin power context: %d\n", ret);
5914 goto err_unref;
5915 }
5916
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08005917 ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
Chris Wilson9ea8d052010-01-04 18:57:56 +00005918 if (ret) {
5919 DRM_ERROR("failed to set-domain on power context: %d\n", ret);
5920 goto err_unpin;
5921 }
5922 mutex_unlock(&dev->struct_mutex);
5923
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08005924 return ctx;
Chris Wilson9ea8d052010-01-04 18:57:56 +00005925
5926err_unpin:
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08005927 i915_gem_object_unpin(ctx);
Chris Wilson9ea8d052010-01-04 18:57:56 +00005928err_unref:
Chris Wilson05394f32010-11-08 19:18:58 +00005929 drm_gem_object_unreference(&ctx->base);
Chris Wilson9ea8d052010-01-04 18:57:56 +00005930 mutex_unlock(&dev->struct_mutex);
5931 return NULL;
5932}
5933
Jesse Barnes7648fa92010-05-20 14:28:11 -07005934bool ironlake_set_drps(struct drm_device *dev, u8 val)
5935{
5936 struct drm_i915_private *dev_priv = dev->dev_private;
5937 u16 rgvswctl;
5938
5939 rgvswctl = I915_READ16(MEMSWCTL);
5940 if (rgvswctl & MEMCTL_CMD_STS) {
5941 DRM_DEBUG("gpu busy, RCS change rejected\n");
5942 return false; /* still busy with another command */
5943 }
5944
5945 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
5946 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
5947 I915_WRITE16(MEMSWCTL, rgvswctl);
5948 POSTING_READ16(MEMSWCTL);
5949
5950 rgvswctl |= MEMCTL_CMD_STS;
5951 I915_WRITE16(MEMSWCTL, rgvswctl);
5952
5953 return true;
5954}
5955
Jesse Barnesf97108d2010-01-29 11:27:07 -08005956void ironlake_enable_drps(struct drm_device *dev)
5957{
5958 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7648fa92010-05-20 14:28:11 -07005959 u32 rgvmodectl = I915_READ(MEMMODECTL);
Jesse Barnesf97108d2010-01-29 11:27:07 -08005960 u8 fmax, fmin, fstart, vstart;
Jesse Barnesf97108d2010-01-29 11:27:07 -08005961
Jesse Barnesea056c12010-09-10 10:02:13 -07005962 /* Enable temp reporting */
5963 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
5964 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
5965
Jesse Barnesf97108d2010-01-29 11:27:07 -08005966 /* 100ms RC evaluation intervals */
5967 I915_WRITE(RCUPEI, 100000);
5968 I915_WRITE(RCDNEI, 100000);
5969
5970 /* Set max/min thresholds to 90ms and 80ms respectively */
5971 I915_WRITE(RCBMAXAVG, 90000);
5972 I915_WRITE(RCBMINAVG, 80000);
5973
5974 I915_WRITE(MEMIHYST, 1);
5975
5976 /* Set up min, max, and cur for interrupt handling */
5977 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
5978 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
5979 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
5980 MEMMODE_FSTART_SHIFT;
Jesse Barnes7648fa92010-05-20 14:28:11 -07005981
Jesse Barnesf97108d2010-01-29 11:27:07 -08005982 vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
5983 PXVFREQ_PX_SHIFT;
5984
Jesse Barnes80dbf4b2010-11-01 14:12:01 -07005985 dev_priv->fmax = fmax; /* IPS callback will increase this */
Jesse Barnes7648fa92010-05-20 14:28:11 -07005986 dev_priv->fstart = fstart;
5987
Jesse Barnes80dbf4b2010-11-01 14:12:01 -07005988 dev_priv->max_delay = fstart;
Jesse Barnesf97108d2010-01-29 11:27:07 -08005989 dev_priv->min_delay = fmin;
5990 dev_priv->cur_delay = fstart;
5991
Jesse Barnes80dbf4b2010-11-01 14:12:01 -07005992 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
5993 fmax, fmin, fstart);
Jesse Barnes7648fa92010-05-20 14:28:11 -07005994
Jesse Barnesf97108d2010-01-29 11:27:07 -08005995 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
5996
5997 /*
5998 * Interrupts will be enabled in ironlake_irq_postinstall
5999 */
6000
6001 I915_WRITE(VIDSTART, vstart);
6002 POSTING_READ(VIDSTART);
6003
6004 rgvmodectl |= MEMMODE_SWMODE_EN;
6005 I915_WRITE(MEMMODECTL, rgvmodectl);
6006
Chris Wilson481b6af2010-08-23 17:43:35 +01006007 if (wait_for((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Chris Wilson913d8d12010-08-07 11:01:35 +01006008 DRM_ERROR("stuck trying to change perf mode\n");
Jesse Barnesf97108d2010-01-29 11:27:07 -08006009 msleep(1);
6010
Jesse Barnes7648fa92010-05-20 14:28:11 -07006011 ironlake_set_drps(dev, fstart);
Jesse Barnesf97108d2010-01-29 11:27:07 -08006012
Jesse Barnes7648fa92010-05-20 14:28:11 -07006013 dev_priv->last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
6014 I915_READ(0x112e0);
6015 dev_priv->last_time1 = jiffies_to_msecs(jiffies);
6016 dev_priv->last_count2 = I915_READ(0x112f4);
6017 getrawmonotonic(&dev_priv->last_time2);
Jesse Barnesf97108d2010-01-29 11:27:07 -08006018}
6019
6020void ironlake_disable_drps(struct drm_device *dev)
6021{
6022 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7648fa92010-05-20 14:28:11 -07006023 u16 rgvswctl = I915_READ16(MEMSWCTL);
Jesse Barnesf97108d2010-01-29 11:27:07 -08006024
6025 /* Ack interrupts, disable EFC interrupt */
6026 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
6027 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
6028 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
6029 I915_WRITE(DEIIR, DE_PCU_EVENT);
6030 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
6031
6032 /* Go back to the starting frequency */
Jesse Barnes7648fa92010-05-20 14:28:11 -07006033 ironlake_set_drps(dev, dev_priv->fstart);
Jesse Barnesf97108d2010-01-29 11:27:07 -08006034 msleep(1);
6035 rgvswctl |= MEMCTL_CMD_STS;
6036 I915_WRITE(MEMSWCTL, rgvswctl);
6037 msleep(1);
6038
6039}
6040
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08006041void gen6_set_rps(struct drm_device *dev, u8 val)
6042{
6043 struct drm_i915_private *dev_priv = dev->dev_private;
6044 u32 swreq;
6045
6046 swreq = (val & 0x3ff) << 25;
6047 I915_WRITE(GEN6_RPNSWREQ, swreq);
6048}
6049
6050void gen6_disable_rps(struct drm_device *dev)
6051{
6052 struct drm_i915_private *dev_priv = dev->dev_private;
6053
6054 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
6055 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
6056 I915_WRITE(GEN6_PMIER, 0);
6057 I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
6058}
6059
Jesse Barnes7648fa92010-05-20 14:28:11 -07006060static unsigned long intel_pxfreq(u32 vidfreq)
6061{
6062 unsigned long freq;
6063 int div = (vidfreq & 0x3f0000) >> 16;
6064 int post = (vidfreq & 0x3000) >> 12;
6065 int pre = (vidfreq & 0x7);
6066
6067 if (!pre)
6068 return 0;
6069
6070 freq = ((div * 133333) / ((1<<post) * pre));
6071
6072 return freq;
6073}
6074
6075void intel_init_emon(struct drm_device *dev)
6076{
6077 struct drm_i915_private *dev_priv = dev->dev_private;
6078 u32 lcfuse;
6079 u8 pxw[16];
6080 int i;
6081
6082 /* Disable to program */
6083 I915_WRITE(ECR, 0);
6084 POSTING_READ(ECR);
6085
6086 /* Program energy weights for various events */
6087 I915_WRITE(SDEW, 0x15040d00);
6088 I915_WRITE(CSIEW0, 0x007f0000);
6089 I915_WRITE(CSIEW1, 0x1e220004);
6090 I915_WRITE(CSIEW2, 0x04000004);
6091
6092 for (i = 0; i < 5; i++)
6093 I915_WRITE(PEW + (i * 4), 0);
6094 for (i = 0; i < 3; i++)
6095 I915_WRITE(DEW + (i * 4), 0);
6096
6097 /* Program P-state weights to account for frequency power adjustment */
6098 for (i = 0; i < 16; i++) {
6099 u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
6100 unsigned long freq = intel_pxfreq(pxvidfreq);
6101 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
6102 PXVFREQ_PX_SHIFT;
6103 unsigned long val;
6104
6105 val = vid * vid;
6106 val *= (freq / 1000);
6107 val *= 255;
6108 val /= (127*127*900);
6109 if (val > 0xff)
6110 DRM_ERROR("bad pxval: %ld\n", val);
6111 pxw[i] = val;
6112 }
6113 /* Render standby states get 0 weight */
6114 pxw[14] = 0;
6115 pxw[15] = 0;
6116
6117 for (i = 0; i < 4; i++) {
6118 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
6119 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
6120 I915_WRITE(PXW + (i * 4), val);
6121 }
6122
6123 /* Adjust magic regs to magic values (more experimental results) */
6124 I915_WRITE(OGW0, 0);
6125 I915_WRITE(OGW1, 0);
6126 I915_WRITE(EG0, 0x00007f00);
6127 I915_WRITE(EG1, 0x0000000e);
6128 I915_WRITE(EG2, 0x000e0000);
6129 I915_WRITE(EG3, 0x68000300);
6130 I915_WRITE(EG4, 0x42000000);
6131 I915_WRITE(EG5, 0x00140031);
6132 I915_WRITE(EG6, 0);
6133 I915_WRITE(EG7, 0);
6134
6135 for (i = 0; i < 8; i++)
6136 I915_WRITE(PXWL + (i * 4), 0);
6137
6138 /* Enable PMON + select events */
6139 I915_WRITE(ECR, 0x80000019);
6140
6141 lcfuse = I915_READ(LCFUSE02);
6142
6143 dev_priv->corr = (lcfuse & LCFUSE_HIV_MASK);
6144}
6145
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08006146void gen6_enable_rps(struct drm_i915_private *dev_priv)
Chris Wilson8fd26852010-12-08 18:40:43 +00006147{
Jesse Barnesa6044e22010-12-20 11:34:20 -08006148 u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
6149 u32 gt_perf_status = I915_READ(GEN6_GT_PERF_STATUS);
6150 u32 pcu_mbox;
6151 int cur_freq, min_freq, max_freq;
Chris Wilson8fd26852010-12-08 18:40:43 +00006152 int i;
6153
6154 /* Here begins a magic sequence of register writes to enable
6155 * auto-downclocking.
6156 *
6157 * Perhaps there might be some value in exposing these to
6158 * userspace...
6159 */
6160 I915_WRITE(GEN6_RC_STATE, 0);
6161 __gen6_force_wake_get(dev_priv);
6162
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08006163 /* disable the counters and set deterministic thresholds */
Chris Wilson8fd26852010-12-08 18:40:43 +00006164 I915_WRITE(GEN6_RC_CONTROL, 0);
6165
6166 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
6167 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
6168 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
6169 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
6170 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
6171
6172 for (i = 0; i < I915_NUM_RINGS; i++)
6173 I915_WRITE(RING_MAX_IDLE(dev_priv->ring[i].mmio_base), 10);
6174
6175 I915_WRITE(GEN6_RC_SLEEP, 0);
6176 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
6177 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
6178 I915_WRITE(GEN6_RC6p_THRESHOLD, 100000);
6179 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
6180
6181 I915_WRITE(GEN6_RC_CONTROL,
6182 GEN6_RC_CTL_RC6p_ENABLE |
6183 GEN6_RC_CTL_RC6_ENABLE |
Chris Wilson9c3d2f72010-12-17 10:54:26 +00006184 GEN6_RC_CTL_EI_MODE(1) |
Chris Wilson8fd26852010-12-08 18:40:43 +00006185 GEN6_RC_CTL_HW_ENABLE);
6186
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08006187 I915_WRITE(GEN6_RPNSWREQ,
Chris Wilson8fd26852010-12-08 18:40:43 +00006188 GEN6_FREQUENCY(10) |
6189 GEN6_OFFSET(0) |
6190 GEN6_AGGRESSIVE_TURBO);
6191 I915_WRITE(GEN6_RC_VIDEO_FREQ,
6192 GEN6_FREQUENCY(12));
6193
6194 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
6195 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
6196 18 << 24 |
6197 6 << 16);
6198 I915_WRITE(GEN6_RP_UP_THRESHOLD, 90000);
6199 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 100000);
6200 I915_WRITE(GEN6_RP_UP_EI, 100000);
6201 I915_WRITE(GEN6_RP_DOWN_EI, 300000);
6202 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
6203 I915_WRITE(GEN6_RP_CONTROL,
6204 GEN6_RP_MEDIA_TURBO |
6205 GEN6_RP_USE_NORMAL_FREQ |
6206 GEN6_RP_MEDIA_IS_GFX |
6207 GEN6_RP_ENABLE |
6208 GEN6_RP_UP_BUSY_MAX |
6209 GEN6_RP_DOWN_BUSY_MIN);
6210
6211 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6212 500))
6213 DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
6214
6215 I915_WRITE(GEN6_PCODE_DATA, 0);
6216 I915_WRITE(GEN6_PCODE_MAILBOX,
6217 GEN6_PCODE_READY |
6218 GEN6_PCODE_WRITE_MIN_FREQ_TABLE);
6219 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6220 500))
6221 DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
6222
Jesse Barnesa6044e22010-12-20 11:34:20 -08006223 min_freq = (rp_state_cap & 0xff0000) >> 16;
6224 max_freq = rp_state_cap & 0xff;
6225 cur_freq = (gt_perf_status & 0xff00) >> 8;
6226
6227 /* Check for overclock support */
6228 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6229 500))
6230 DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
6231 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_READ_OC_PARAMS);
6232 pcu_mbox = I915_READ(GEN6_PCODE_DATA);
6233 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6234 500))
6235 DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
6236 if (pcu_mbox & (1<<31)) { /* OC supported */
6237 max_freq = pcu_mbox & 0xff;
6238 DRM_DEBUG_DRIVER("overclocking supported, adjusting frequency max to %dMHz\n", pcu_mbox * 100);
6239 }
6240
6241 /* In units of 100MHz */
6242 dev_priv->max_delay = max_freq;
6243 dev_priv->min_delay = min_freq;
6244 dev_priv->cur_delay = cur_freq;
6245
Chris Wilson8fd26852010-12-08 18:40:43 +00006246 /* requires MSI enabled */
6247 I915_WRITE(GEN6_PMIER,
6248 GEN6_PM_MBOX_EVENT |
6249 GEN6_PM_THERMAL_EVENT |
6250 GEN6_PM_RP_DOWN_TIMEOUT |
6251 GEN6_PM_RP_UP_THRESHOLD |
6252 GEN6_PM_RP_DOWN_THRESHOLD |
6253 GEN6_PM_RP_UP_EI_EXPIRED |
6254 GEN6_PM_RP_DOWN_EI_EXPIRED);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08006255 I915_WRITE(GEN6_PMIMR, 0);
6256 /* enable all PM interrupts */
6257 I915_WRITE(GEN6_PMINTRMSK, 0);
Chris Wilson8fd26852010-12-08 18:40:43 +00006258
6259 __gen6_force_wake_put(dev_priv);
6260}
6261
Chris Wilson0cdab212010-12-05 17:27:06 +00006262void intel_enable_clock_gating(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07006263{
6264 struct drm_i915_private *dev_priv = dev->dev_private;
6265
6266 /*
6267 * Disable clock gating reported to work incorrectly according to the
6268 * specs, but enable as much else as we can.
6269 */
Eric Anholtbad720f2009-10-22 16:11:14 -07006270 if (HAS_PCH_SPLIT(dev)) {
Eric Anholt8956c8b2010-03-18 13:21:14 -07006271 uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
6272
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01006273 if (IS_GEN5(dev)) {
Eric Anholt8956c8b2010-03-18 13:21:14 -07006274 /* Required for FBC */
6275 dspclk_gate |= DPFDUNIT_CLOCK_GATE_DISABLE;
6276 /* Required for CxSR */
6277 dspclk_gate |= DPARBUNIT_CLOCK_GATE_DISABLE;
6278
6279 I915_WRITE(PCH_3DCGDIS0,
6280 MARIUNIT_CLOCK_GATE_DISABLE |
6281 SVSMUNIT_CLOCK_GATE_DISABLE);
Eric Anholt06f37752010-12-14 10:06:46 -08006282 I915_WRITE(PCH_3DCGDIS1,
6283 VFMUNIT_CLOCK_GATE_DISABLE);
Eric Anholt8956c8b2010-03-18 13:21:14 -07006284 }
6285
6286 I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08006287
6288 /*
Jesse Barnes382b0932010-10-07 16:01:25 -07006289 * On Ibex Peak and Cougar Point, we need to disable clock
6290 * gating for the panel power sequencer or it will fail to
6291 * start up when no ports are active.
6292 */
6293 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
6294
6295 /*
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08006296 * According to the spec the following bits should be set in
6297 * order to enable memory self-refresh
6298 * The bit 22/21 of 0x42004
6299 * The bit 5 of 0x42020
6300 * The bit 15 of 0x45000
6301 */
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01006302 if (IS_GEN5(dev)) {
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08006303 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6304 (I915_READ(ILK_DISPLAY_CHICKEN2) |
6305 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
6306 I915_WRITE(ILK_DSPCLK_GATE,
6307 (I915_READ(ILK_DSPCLK_GATE) |
6308 ILK_DPARB_CLK_GATE));
6309 I915_WRITE(DISP_ARB_CTL,
6310 (I915_READ(DISP_ARB_CTL) |
6311 DISP_FBC_WM_DIS));
Yuanhan Liu13982612010-12-15 15:42:31 +08006312 I915_WRITE(WM3_LP_ILK, 0);
6313 I915_WRITE(WM2_LP_ILK, 0);
6314 I915_WRITE(WM1_LP_ILK, 0);
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08006315 }
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08006316 /*
6317 * Based on the document from hardware guys the following bits
6318 * should be set unconditionally in order to enable FBC.
6319 * The bit 22 of 0x42000
6320 * The bit 22 of 0x42004
6321 * The bit 7,8,9 of 0x42020.
6322 */
6323 if (IS_IRONLAKE_M(dev)) {
6324 I915_WRITE(ILK_DISPLAY_CHICKEN1,
6325 I915_READ(ILK_DISPLAY_CHICKEN1) |
6326 ILK_FBCQ_DIS);
6327 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6328 I915_READ(ILK_DISPLAY_CHICKEN2) |
6329 ILK_DPARB_GATE);
6330 I915_WRITE(ILK_DSPCLK_GATE,
6331 I915_READ(ILK_DSPCLK_GATE) |
6332 ILK_DPFC_DIS1 |
6333 ILK_DPFC_DIS2 |
6334 ILK_CLK_FBC);
6335 }
Eric Anholtde6e2ea2010-11-06 14:53:32 -07006336
Eric Anholt67e92af2010-11-06 14:53:33 -07006337 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6338 I915_READ(ILK_DISPLAY_CHICKEN2) |
6339 ILK_ELPIN_409_SELECT);
6340
Eric Anholtde6e2ea2010-11-06 14:53:32 -07006341 if (IS_GEN5(dev)) {
6342 I915_WRITE(_3D_CHICKEN2,
6343 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
6344 _3D_CHICKEN2_WM_READ_PIPELINED);
6345 }
Chris Wilson8fd26852010-12-08 18:40:43 +00006346
Yuanhan Liu13982612010-12-15 15:42:31 +08006347 if (IS_GEN6(dev)) {
6348 I915_WRITE(WM3_LP_ILK, 0);
6349 I915_WRITE(WM2_LP_ILK, 0);
6350 I915_WRITE(WM1_LP_ILK, 0);
6351
6352 /*
6353 * According to the spec the following bits should be
6354 * set in order to enable memory self-refresh and fbc:
6355 * The bit21 and bit22 of 0x42000
6356 * The bit21 and bit22 of 0x42004
6357 * The bit5 and bit7 of 0x42020
6358 * The bit14 of 0x70180
6359 * The bit14 of 0x71180
6360 */
6361 I915_WRITE(ILK_DISPLAY_CHICKEN1,
6362 I915_READ(ILK_DISPLAY_CHICKEN1) |
6363 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
6364 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6365 I915_READ(ILK_DISPLAY_CHICKEN2) |
6366 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
6367 I915_WRITE(ILK_DSPCLK_GATE,
6368 I915_READ(ILK_DSPCLK_GATE) |
6369 ILK_DPARB_CLK_GATE |
6370 ILK_DPFD_CLK_GATE);
6371
6372 I915_WRITE(DSPACNTR,
6373 I915_READ(DSPACNTR) |
6374 DISPPLANE_TRICKLE_FEED_DISABLE);
6375 I915_WRITE(DSPBCNTR,
6376 I915_READ(DSPBCNTR) |
6377 DISPPLANE_TRICKLE_FEED_DISABLE);
6378 }
Zhenyu Wangc03342f2009-09-29 11:01:23 +08006379 } else if (IS_G4X(dev)) {
Jesse Barnes652c3932009-08-17 13:31:43 -07006380 uint32_t dspclk_gate;
6381 I915_WRITE(RENCLK_GATE_D1, 0);
6382 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
6383 GS_UNIT_CLOCK_GATE_DISABLE |
6384 CL_UNIT_CLOCK_GATE_DISABLE);
6385 I915_WRITE(RAMCLK_GATE_D, 0);
6386 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
6387 OVRUNIT_CLOCK_GATE_DISABLE |
6388 OVCUNIT_CLOCK_GATE_DISABLE;
6389 if (IS_GM45(dev))
6390 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
6391 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006392 } else if (IS_CRESTLINE(dev)) {
Jesse Barnes652c3932009-08-17 13:31:43 -07006393 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
6394 I915_WRITE(RENCLK_GATE_D2, 0);
6395 I915_WRITE(DSPCLK_GATE_D, 0);
6396 I915_WRITE(RAMCLK_GATE_D, 0);
6397 I915_WRITE16(DEUC, 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006398 } else if (IS_BROADWATER(dev)) {
Jesse Barnes652c3932009-08-17 13:31:43 -07006399 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
6400 I965_RCC_CLOCK_GATE_DISABLE |
6401 I965_RCPB_CLOCK_GATE_DISABLE |
6402 I965_ISC_CLOCK_GATE_DISABLE |
6403 I965_FBC_CLOCK_GATE_DISABLE);
6404 I915_WRITE(RENCLK_GATE_D2, 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006405 } else if (IS_GEN3(dev)) {
Jesse Barnes652c3932009-08-17 13:31:43 -07006406 u32 dstate = I915_READ(D_STATE);
6407
6408 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
6409 DSTATE_DOT_CLOCK_GATING;
6410 I915_WRITE(D_STATE, dstate);
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +02006411 } else if (IS_I85X(dev) || IS_I865G(dev)) {
Jesse Barnes652c3932009-08-17 13:31:43 -07006412 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
6413 } else if (IS_I830(dev)) {
6414 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
6415 }
Jesse Barnes97f5ab62009-10-08 10:16:48 -07006416
6417 /*
6418 * GPU can automatically power down the render unit if given a page
6419 * to save state.
6420 */
Chris Wilsonc5780272010-12-07 23:04:14 +00006421 if (IS_IRONLAKE_M(dev) && 0) { /* XXX causes a failure during suspend */
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08006422 if (dev_priv->renderctx == NULL)
6423 dev_priv->renderctx = intel_alloc_context_page(dev);
6424 if (dev_priv->renderctx) {
Chris Wilson05394f32010-11-08 19:18:58 +00006425 struct drm_i915_gem_object *obj = dev_priv->renderctx;
6426 if (BEGIN_LP_RING(4) == 0) {
6427 OUT_RING(MI_SET_CONTEXT);
6428 OUT_RING(obj->gtt_offset |
6429 MI_MM_SPACE_GTT |
6430 MI_SAVE_EXT_STATE_EN |
6431 MI_RESTORE_EXT_STATE_EN |
6432 MI_RESTORE_INHIBIT);
6433 OUT_RING(MI_NOOP);
6434 OUT_RING(MI_FLUSH);
6435 ADVANCE_LP_RING();
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08006436 }
Chris Wilsonbc416062010-09-07 21:51:02 +01006437 } else
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08006438 DRM_DEBUG_KMS("Failed to allocate render context."
Chris Wilsonbc416062010-09-07 21:51:02 +01006439 "Disable RC6\n");
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08006440 }
6441
Chris Wilson3c8cdf92010-12-05 16:45:02 +00006442 if (IS_GEN4(dev) && IS_MOBILE(dev)) {
Chris Wilson05394f32010-11-08 19:18:58 +00006443 if (dev_priv->pwrctx == NULL)
6444 dev_priv->pwrctx = intel_alloc_context_page(dev);
Andrew Lutomirski7e8b60f2009-11-08 13:49:51 -05006445 if (dev_priv->pwrctx) {
Chris Wilson05394f32010-11-08 19:18:58 +00006446 struct drm_i915_gem_object *obj = dev_priv->pwrctx;
6447 I915_WRITE(PWRCTXA, obj->gtt_offset | PWRCTX_EN);
Chris Wilson9ea8d052010-01-04 18:57:56 +00006448 I915_WRITE(MCHBAR_RENDER_STANDBY,
6449 I915_READ(MCHBAR_RENDER_STANDBY) & ~RCX_SW_EXIT);
6450 }
Jesse Barnes97f5ab62009-10-08 10:16:48 -07006451 }
Jesse Barnes652c3932009-08-17 13:31:43 -07006452}
6453
Chris Wilson0cdab212010-12-05 17:27:06 +00006454void intel_disable_clock_gating(struct drm_device *dev)
6455{
6456 struct drm_i915_private *dev_priv = dev->dev_private;
6457
6458 if (dev_priv->renderctx) {
6459 struct drm_i915_gem_object *obj = dev_priv->renderctx;
6460
6461 I915_WRITE(CCID, 0);
6462 POSTING_READ(CCID);
6463
6464 i915_gem_object_unpin(obj);
6465 drm_gem_object_unreference(&obj->base);
6466 dev_priv->renderctx = NULL;
6467 }
6468
6469 if (dev_priv->pwrctx) {
6470 struct drm_i915_gem_object *obj = dev_priv->pwrctx;
6471
6472 I915_WRITE(PWRCTXA, 0);
6473 POSTING_READ(PWRCTXA);
6474
6475 i915_gem_object_unpin(obj);
6476 drm_gem_object_unreference(&obj->base);
6477 dev_priv->pwrctx = NULL;
6478 }
6479}
6480
Jesse Barnese70236a2009-09-21 10:42:27 -07006481/* Set up chip specific display functions */
6482static void intel_init_display(struct drm_device *dev)
6483{
6484 struct drm_i915_private *dev_priv = dev->dev_private;
6485
6486 /* We always want a DPMS function */
Eric Anholtbad720f2009-10-22 16:11:14 -07006487 if (HAS_PCH_SPLIT(dev))
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006488 dev_priv->display.dpms = ironlake_crtc_dpms;
Jesse Barnese70236a2009-09-21 10:42:27 -07006489 else
6490 dev_priv->display.dpms = i9xx_crtc_dpms;
6491
Adam Jacksonee5382a2010-04-23 11:17:39 -04006492 if (I915_HAS_FBC(dev)) {
Yuanhan Liu9c04f012010-12-15 15:42:32 +08006493 if (HAS_PCH_SPLIT(dev)) {
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08006494 dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
6495 dev_priv->display.enable_fbc = ironlake_enable_fbc;
6496 dev_priv->display.disable_fbc = ironlake_disable_fbc;
6497 } else if (IS_GM45(dev)) {
Jesse Barnes74dff282009-09-14 15:39:40 -07006498 dev_priv->display.fbc_enabled = g4x_fbc_enabled;
6499 dev_priv->display.enable_fbc = g4x_enable_fbc;
6500 dev_priv->display.disable_fbc = g4x_disable_fbc;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006501 } else if (IS_CRESTLINE(dev)) {
Jesse Barnese70236a2009-09-21 10:42:27 -07006502 dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
6503 dev_priv->display.enable_fbc = i8xx_enable_fbc;
6504 dev_priv->display.disable_fbc = i8xx_disable_fbc;
6505 }
Jesse Barnes74dff282009-09-14 15:39:40 -07006506 /* 855GM needs testing */
Jesse Barnese70236a2009-09-21 10:42:27 -07006507 }
6508
6509 /* Returns the core display clock speed */
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006510 if (IS_I945G(dev) || (IS_G33(dev) && ! IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -07006511 dev_priv->display.get_display_clock_speed =
6512 i945_get_display_clock_speed;
6513 else if (IS_I915G(dev))
6514 dev_priv->display.get_display_clock_speed =
6515 i915_get_display_clock_speed;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006516 else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07006517 dev_priv->display.get_display_clock_speed =
6518 i9xx_misc_get_display_clock_speed;
6519 else if (IS_I915GM(dev))
6520 dev_priv->display.get_display_clock_speed =
6521 i915gm_get_display_clock_speed;
6522 else if (IS_I865G(dev))
6523 dev_priv->display.get_display_clock_speed =
6524 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +02006525 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07006526 dev_priv->display.get_display_clock_speed =
6527 i855_get_display_clock_speed;
6528 else /* 852, 830 */
6529 dev_priv->display.get_display_clock_speed =
6530 i830_get_display_clock_speed;
6531
6532 /* For FIFO watermark updates */
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08006533 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01006534 if (IS_GEN5(dev)) {
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08006535 if (I915_READ(MLTR_ILK) & ILK_SRLT_MASK)
6536 dev_priv->display.update_wm = ironlake_update_wm;
6537 else {
6538 DRM_DEBUG_KMS("Failed to get proper latency. "
6539 "Disable CxSR\n");
6540 dev_priv->display.update_wm = NULL;
6541 }
Yuanhan Liu13982612010-12-15 15:42:31 +08006542 } else if (IS_GEN6(dev)) {
6543 if (SNB_READ_WM0_LATENCY()) {
6544 dev_priv->display.update_wm = sandybridge_update_wm;
6545 } else {
6546 DRM_DEBUG_KMS("Failed to read display plane latency. "
6547 "Disable CxSR\n");
6548 dev_priv->display.update_wm = NULL;
6549 }
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08006550 } else
6551 dev_priv->display.update_wm = NULL;
6552 } else if (IS_PINEVIEW(dev)) {
Zhao Yakuid4294342010-03-22 22:45:36 +08006553 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
Li Peng95534262010-05-18 18:58:44 +08006554 dev_priv->is_ddr3,
Zhao Yakuid4294342010-03-22 22:45:36 +08006555 dev_priv->fsb_freq,
6556 dev_priv->mem_freq)) {
6557 DRM_INFO("failed to find known CxSR latency "
Li Peng95534262010-05-18 18:58:44 +08006558 "(found ddr%s fsb freq %d, mem freq %d), "
Zhao Yakuid4294342010-03-22 22:45:36 +08006559 "disabling CxSR\n",
Li Peng95534262010-05-18 18:58:44 +08006560 (dev_priv->is_ddr3 == 1) ? "3": "2",
Zhao Yakuid4294342010-03-22 22:45:36 +08006561 dev_priv->fsb_freq, dev_priv->mem_freq);
6562 /* Disable CxSR and never update its watermark again */
6563 pineview_disable_cxsr(dev);
6564 dev_priv->display.update_wm = NULL;
6565 } else
6566 dev_priv->display.update_wm = pineview_update_wm;
6567 } else if (IS_G4X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07006568 dev_priv->display.update_wm = g4x_update_wm;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006569 else if (IS_GEN4(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07006570 dev_priv->display.update_wm = i965_update_wm;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006571 else if (IS_GEN3(dev)) {
Jesse Barnese70236a2009-09-21 10:42:27 -07006572 dev_priv->display.update_wm = i9xx_update_wm;
6573 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Adam Jackson8f4695e2010-04-16 18:20:57 -04006574 } else if (IS_I85X(dev)) {
6575 dev_priv->display.update_wm = i9xx_update_wm;
6576 dev_priv->display.get_fifo_size = i85x_get_fifo_size;
Jesse Barnese70236a2009-09-21 10:42:27 -07006577 } else {
Adam Jackson8f4695e2010-04-16 18:20:57 -04006578 dev_priv->display.update_wm = i830_update_wm;
6579 if (IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07006580 dev_priv->display.get_fifo_size = i845_get_fifo_size;
6581 else
6582 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Jesse Barnese70236a2009-09-21 10:42:27 -07006583 }
6584}
6585
Jesse Barnesb690e962010-07-19 13:53:12 -07006586/*
6587 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
6588 * resume, or other times. This quirk makes sure that's the case for
6589 * affected systems.
6590 */
6591static void quirk_pipea_force (struct drm_device *dev)
6592{
6593 struct drm_i915_private *dev_priv = dev->dev_private;
6594
6595 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
6596 DRM_DEBUG_DRIVER("applying pipe a force quirk\n");
6597}
6598
6599struct intel_quirk {
6600 int device;
6601 int subsystem_vendor;
6602 int subsystem_device;
6603 void (*hook)(struct drm_device *dev);
6604};
6605
6606struct intel_quirk intel_quirks[] = {
6607 /* HP Compaq 2730p needs pipe A force quirk (LP: #291555) */
6608 { 0x2a42, 0x103c, 0x30eb, quirk_pipea_force },
6609 /* HP Mini needs pipe A force quirk (LP: #322104) */
6610 { 0x27ae,0x103c, 0x361a, quirk_pipea_force },
6611
6612 /* Thinkpad R31 needs pipe A force quirk */
6613 { 0x3577, 0x1014, 0x0505, quirk_pipea_force },
6614 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
6615 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
6616
6617 /* ThinkPad X30 needs pipe A force quirk (LP: #304614) */
6618 { 0x3577, 0x1014, 0x0513, quirk_pipea_force },
6619 /* ThinkPad X40 needs pipe A force quirk */
6620
6621 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
6622 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
6623
6624 /* 855 & before need to leave pipe A & dpll A up */
6625 { 0x3582, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
6626 { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
6627};
6628
6629static void intel_init_quirks(struct drm_device *dev)
6630{
6631 struct pci_dev *d = dev->pdev;
6632 int i;
6633
6634 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
6635 struct intel_quirk *q = &intel_quirks[i];
6636
6637 if (d->device == q->device &&
6638 (d->subsystem_vendor == q->subsystem_vendor ||
6639 q->subsystem_vendor == PCI_ANY_ID) &&
6640 (d->subsystem_device == q->subsystem_device ||
6641 q->subsystem_device == PCI_ANY_ID))
6642 q->hook(dev);
6643 }
6644}
6645
Jesse Barnes9cce37f2010-08-13 15:11:26 -07006646/* Disable the VGA plane that we never use */
6647static void i915_disable_vga(struct drm_device *dev)
6648{
6649 struct drm_i915_private *dev_priv = dev->dev_private;
6650 u8 sr1;
6651 u32 vga_reg;
6652
6653 if (HAS_PCH_SPLIT(dev))
6654 vga_reg = CPU_VGACNTRL;
6655 else
6656 vga_reg = VGACNTRL;
6657
6658 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
6659 outb(1, VGA_SR_INDEX);
6660 sr1 = inb(VGA_SR_DATA);
6661 outb(sr1 | 1<<5, VGA_SR_DATA);
6662 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
6663 udelay(300);
6664
6665 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
6666 POSTING_READ(vga_reg);
6667}
6668
Jesse Barnes79e53942008-11-07 14:24:08 -08006669void intel_modeset_init(struct drm_device *dev)
6670{
Jesse Barnes652c3932009-08-17 13:31:43 -07006671 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08006672 int i;
6673
6674 drm_mode_config_init(dev);
6675
6676 dev->mode_config.min_width = 0;
6677 dev->mode_config.min_height = 0;
6678
6679 dev->mode_config.funcs = (void *)&intel_mode_funcs;
6680
Jesse Barnesb690e962010-07-19 13:53:12 -07006681 intel_init_quirks(dev);
6682
Jesse Barnese70236a2009-09-21 10:42:27 -07006683 intel_init_display(dev);
6684
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006685 if (IS_GEN2(dev)) {
6686 dev->mode_config.max_width = 2048;
6687 dev->mode_config.max_height = 2048;
6688 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -07006689 dev->mode_config.max_width = 4096;
6690 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -08006691 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006692 dev->mode_config.max_width = 8192;
6693 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -08006694 }
Chris Wilson35c30472010-12-22 14:07:12 +00006695 dev->mode_config.fb_base = dev->agp->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08006696
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006697 if (IS_MOBILE(dev) || !IS_GEN2(dev))
Dave Airliea3524f12010-06-06 18:59:41 +10006698 dev_priv->num_pipe = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -08006699 else
Dave Airliea3524f12010-06-06 18:59:41 +10006700 dev_priv->num_pipe = 1;
Zhao Yakui28c97732009-10-09 11:39:41 +08006701 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Dave Airliea3524f12010-06-06 18:59:41 +10006702 dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -08006703
Dave Airliea3524f12010-06-06 18:59:41 +10006704 for (i = 0; i < dev_priv->num_pipe; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006705 intel_crtc_init(dev, i);
6706 }
6707
6708 intel_setup_outputs(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -07006709
Chris Wilson0cdab212010-12-05 17:27:06 +00006710 intel_enable_clock_gating(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -07006711
Jesse Barnes9cce37f2010-08-13 15:11:26 -07006712 /* Just disable it once at startup */
6713 i915_disable_vga(dev);
6714
Jesse Barnes7648fa92010-05-20 14:28:11 -07006715 if (IS_IRONLAKE_M(dev)) {
Jesse Barnesf97108d2010-01-29 11:27:07 -08006716 ironlake_enable_drps(dev);
Jesse Barnes7648fa92010-05-20 14:28:11 -07006717 intel_init_emon(dev);
6718 }
Jesse Barnesf97108d2010-01-29 11:27:07 -08006719
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08006720 if (IS_GEN6(dev))
6721 gen6_enable_rps(dev_priv);
6722
Jesse Barnes652c3932009-08-17 13:31:43 -07006723 INIT_WORK(&dev_priv->idle_work, intel_idle_update);
6724 setup_timer(&dev_priv->idle_timer, intel_gpu_idle_timer,
6725 (unsigned long)dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02006726
6727 intel_setup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006728}
6729
6730void intel_modeset_cleanup(struct drm_device *dev)
6731{
Jesse Barnes652c3932009-08-17 13:31:43 -07006732 struct drm_i915_private *dev_priv = dev->dev_private;
6733 struct drm_crtc *crtc;
6734 struct intel_crtc *intel_crtc;
6735
Keith Packardf87ea762010-10-03 19:36:26 -07006736 drm_kms_helper_poll_fini(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -07006737 mutex_lock(&dev->struct_mutex);
6738
Jesse Barnes723bfd72010-10-07 16:01:13 -07006739 intel_unregister_dsm_handler();
6740
6741
Jesse Barnes652c3932009-08-17 13:31:43 -07006742 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
6743 /* Skip inactive CRTCs */
6744 if (!crtc->fb)
6745 continue;
6746
6747 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3dec0092010-08-20 21:40:52 +02006748 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07006749 }
6750
Jesse Barnese70236a2009-09-21 10:42:27 -07006751 if (dev_priv->display.disable_fbc)
6752 dev_priv->display.disable_fbc(dev);
6753
Jesse Barnesf97108d2010-01-29 11:27:07 -08006754 if (IS_IRONLAKE_M(dev))
6755 ironlake_disable_drps(dev);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08006756 if (IS_GEN6(dev))
6757 gen6_disable_rps(dev);
Jesse Barnesf97108d2010-01-29 11:27:07 -08006758
Chris Wilson0cdab212010-12-05 17:27:06 +00006759 intel_disable_clock_gating(dev);
6760
Kristian Høgsberg69341a52009-11-11 12:19:17 -05006761 mutex_unlock(&dev->struct_mutex);
6762
Daniel Vetter6c0d93502010-08-20 18:26:46 +02006763 /* Disable the irq before mode object teardown, for the irq might
6764 * enqueue unpin/hotplug work. */
6765 drm_irq_uninstall(dev);
6766 cancel_work_sync(&dev_priv->hotplug_work);
6767
Daniel Vetter3dec0092010-08-20 21:40:52 +02006768 /* Shut off idle work before the crtcs get freed. */
6769 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
6770 intel_crtc = to_intel_crtc(crtc);
6771 del_timer_sync(&intel_crtc->idle_timer);
6772 }
6773 del_timer_sync(&dev_priv->idle_timer);
6774 cancel_work_sync(&dev_priv->idle_work);
6775
Jesse Barnes79e53942008-11-07 14:24:08 -08006776 drm_mode_config_cleanup(dev);
6777}
6778
Dave Airlie28d52042009-09-21 14:33:58 +10006779/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +08006780 * Return which encoder is currently attached for connector.
6781 */
Chris Wilsondf0e9242010-09-09 16:20:55 +01006782struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -08006783{
Chris Wilsondf0e9242010-09-09 16:20:55 +01006784 return &intel_attached_encoder(connector)->base;
6785}
Jesse Barnes79e53942008-11-07 14:24:08 -08006786
Chris Wilsondf0e9242010-09-09 16:20:55 +01006787void intel_connector_attach_encoder(struct intel_connector *connector,
6788 struct intel_encoder *encoder)
6789{
6790 connector->encoder = encoder;
6791 drm_mode_connector_attach_encoder(&connector->base,
6792 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08006793}
Dave Airlie28d52042009-09-21 14:33:58 +10006794
6795/*
6796 * set vga decode state - true == enable VGA decode
6797 */
6798int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
6799{
6800 struct drm_i915_private *dev_priv = dev->dev_private;
6801 u16 gmch_ctrl;
6802
6803 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
6804 if (state)
6805 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
6806 else
6807 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
6808 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
6809 return 0;
6810}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00006811
6812#ifdef CONFIG_DEBUG_FS
6813#include <linux/seq_file.h>
6814
6815struct intel_display_error_state {
6816 struct intel_cursor_error_state {
6817 u32 control;
6818 u32 position;
6819 u32 base;
6820 u32 size;
6821 } cursor[2];
6822
6823 struct intel_pipe_error_state {
6824 u32 conf;
6825 u32 source;
6826
6827 u32 htotal;
6828 u32 hblank;
6829 u32 hsync;
6830 u32 vtotal;
6831 u32 vblank;
6832 u32 vsync;
6833 } pipe[2];
6834
6835 struct intel_plane_error_state {
6836 u32 control;
6837 u32 stride;
6838 u32 size;
6839 u32 pos;
6840 u32 addr;
6841 u32 surface;
6842 u32 tile_offset;
6843 } plane[2];
6844};
6845
6846struct intel_display_error_state *
6847intel_display_capture_error_state(struct drm_device *dev)
6848{
6849 drm_i915_private_t *dev_priv = dev->dev_private;
6850 struct intel_display_error_state *error;
6851 int i;
6852
6853 error = kmalloc(sizeof(*error), GFP_ATOMIC);
6854 if (error == NULL)
6855 return NULL;
6856
6857 for (i = 0; i < 2; i++) {
6858 error->cursor[i].control = I915_READ(CURCNTR(i));
6859 error->cursor[i].position = I915_READ(CURPOS(i));
6860 error->cursor[i].base = I915_READ(CURBASE(i));
6861
6862 error->plane[i].control = I915_READ(DSPCNTR(i));
6863 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
6864 error->plane[i].size = I915_READ(DSPSIZE(i));
6865 error->plane[i].pos= I915_READ(DSPPOS(i));
6866 error->plane[i].addr = I915_READ(DSPADDR(i));
6867 if (INTEL_INFO(dev)->gen >= 4) {
6868 error->plane[i].surface = I915_READ(DSPSURF(i));
6869 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
6870 }
6871
6872 error->pipe[i].conf = I915_READ(PIPECONF(i));
6873 error->pipe[i].source = I915_READ(PIPESRC(i));
6874 error->pipe[i].htotal = I915_READ(HTOTAL(i));
6875 error->pipe[i].hblank = I915_READ(HBLANK(i));
6876 error->pipe[i].hsync = I915_READ(HSYNC(i));
6877 error->pipe[i].vtotal = I915_READ(VTOTAL(i));
6878 error->pipe[i].vblank = I915_READ(VBLANK(i));
6879 error->pipe[i].vsync = I915_READ(VSYNC(i));
6880 }
6881
6882 return error;
6883}
6884
6885void
6886intel_display_print_error_state(struct seq_file *m,
6887 struct drm_device *dev,
6888 struct intel_display_error_state *error)
6889{
6890 int i;
6891
6892 for (i = 0; i < 2; i++) {
6893 seq_printf(m, "Pipe [%d]:\n", i);
6894 seq_printf(m, " CONF: %08x\n", error->pipe[i].conf);
6895 seq_printf(m, " SRC: %08x\n", error->pipe[i].source);
6896 seq_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
6897 seq_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
6898 seq_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
6899 seq_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
6900 seq_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
6901 seq_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
6902
6903 seq_printf(m, "Plane [%d]:\n", i);
6904 seq_printf(m, " CNTR: %08x\n", error->plane[i].control);
6905 seq_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
6906 seq_printf(m, " SIZE: %08x\n", error->plane[i].size);
6907 seq_printf(m, " POS: %08x\n", error->plane[i].pos);
6908 seq_printf(m, " ADDR: %08x\n", error->plane[i].addr);
6909 if (INTEL_INFO(dev)->gen >= 4) {
6910 seq_printf(m, " SURF: %08x\n", error->plane[i].surface);
6911 seq_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
6912 }
6913
6914 seq_printf(m, "Cursor [%d]:\n", i);
6915 seq_printf(m, " CNTR: %08x\n", error->cursor[i].control);
6916 seq_printf(m, " POS: %08x\n", error->cursor[i].position);
6917 seq_printf(m, " BASE: %08x\n", error->cursor[i].base);
6918 }
6919}
6920#endif