blob: 2bf72a4b069f9010d35c14a90f017b823c476738 [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 Barnesb24e7172011-01-04 15:09:30 -08001061static const char *state_string(bool enabled)
1062{
1063 return enabled ? "on" : "off";
1064}
1065
1066/* Only for pre-ILK configs */
1067static void assert_pll(struct drm_i915_private *dev_priv,
1068 enum pipe pipe, bool state)
1069{
1070 int reg;
1071 u32 val;
1072 bool cur_state;
1073
1074 reg = DPLL(pipe);
1075 val = I915_READ(reg);
1076 cur_state = !!(val & DPLL_VCO_ENABLE);
1077 WARN(cur_state != state,
1078 "PLL state assertion failure (expected %s, current %s)\n",
1079 state_string(state), state_string(cur_state));
1080}
1081#define assert_pll_enabled(d, p) assert_pll(d, p, true)
1082#define assert_pll_disabled(d, p) assert_pll(d, p, false)
1083
1084static void assert_pipe_enabled(struct drm_i915_private *dev_priv,
1085 enum pipe pipe)
1086{
1087 int reg;
1088 u32 val;
1089
1090 reg = PIPECONF(pipe);
1091 val = I915_READ(reg);
1092 WARN(!(val & PIPECONF_ENABLE),
1093 "pipe %c assertion failure, should be active but is disabled\n",
1094 pipe ? 'B' : 'A');
1095}
1096
1097static void assert_plane_enabled(struct drm_i915_private *dev_priv,
1098 enum plane plane)
1099{
1100 int reg;
1101 u32 val;
1102
1103 reg = DSPCNTR(plane);
1104 val = I915_READ(reg);
1105 WARN(!(val & DISPLAY_PLANE_ENABLE),
1106 "plane %c assertion failure, should be active but is disabled\n",
1107 plane ? 'B' : 'A');
1108}
1109
1110static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1111 enum pipe pipe)
1112{
1113 int reg, i;
1114 u32 val;
1115 int cur_pipe;
1116
1117 /* Need to check both planes against the pipe */
1118 for (i = 0; i < 2; i++) {
1119 reg = DSPCNTR(i);
1120 val = I915_READ(reg);
1121 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1122 DISPPLANE_SEL_PIPE_SHIFT;
1123 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
1124 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1125 i, pipe ? 'B' : 'A');
1126 }
1127}
1128
1129/**
1130 * intel_enable_pipe - enable a pipe, assertiing requirements
1131 * @dev_priv: i915 private structure
1132 * @pipe: pipe to enable
1133 *
1134 * Enable @pipe, making sure that various hardware specific requirements
1135 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1136 *
1137 * @pipe should be %PIPE_A or %PIPE_B.
1138 *
1139 * Will wait until the pipe is actually running (i.e. first vblank) before
1140 * returning.
1141 */
1142static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
1143{
1144 int reg;
1145 u32 val;
1146
1147 /*
1148 * A pipe without a PLL won't actually be able to drive bits from
1149 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1150 * need the check.
1151 */
1152 if (!HAS_PCH_SPLIT(dev_priv->dev))
1153 assert_pll_enabled(dev_priv, pipe);
1154
1155 reg = PIPECONF(pipe);
1156 val = I915_READ(reg);
1157 val |= PIPECONF_ENABLE;
1158 I915_WRITE(reg, val);
1159 POSTING_READ(reg);
1160 intel_wait_for_vblank(dev_priv->dev, pipe);
1161}
1162
1163/**
1164 * intel_disable_pipe - disable a pipe, assertiing requirements
1165 * @dev_priv: i915 private structure
1166 * @pipe: pipe to disable
1167 *
1168 * Disable @pipe, making sure that various hardware specific requirements
1169 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1170 *
1171 * @pipe should be %PIPE_A or %PIPE_B.
1172 *
1173 * Will wait until the pipe has shut down before returning.
1174 */
1175static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1176 enum pipe pipe)
1177{
1178 int reg;
1179 u32 val;
1180
1181 /*
1182 * Make sure planes won't keep trying to pump pixels to us,
1183 * or we might hang the display.
1184 */
1185 assert_planes_disabled(dev_priv, pipe);
1186
1187 /* Don't disable pipe A or pipe A PLLs if needed */
1188 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1189 return;
1190
1191 reg = PIPECONF(pipe);
1192 val = I915_READ(reg);
1193 val &= ~PIPECONF_ENABLE;
1194 I915_WRITE(reg, val);
1195 POSTING_READ(reg);
1196 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1197}
1198
1199/**
1200 * intel_enable_plane - enable a display plane on a given pipe
1201 * @dev_priv: i915 private structure
1202 * @plane: plane to enable
1203 * @pipe: pipe being fed
1204 *
1205 * Enable @plane on @pipe, making sure that @pipe is running first.
1206 */
1207static void intel_enable_plane(struct drm_i915_private *dev_priv,
1208 enum plane plane, enum pipe pipe)
1209{
1210 int reg;
1211 u32 val;
1212
1213 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1214 assert_pipe_enabled(dev_priv, pipe);
1215
1216 reg = DSPCNTR(plane);
1217 val = I915_READ(reg);
1218 val |= DISPLAY_PLANE_ENABLE;
1219 I915_WRITE(reg, val);
1220 POSTING_READ(reg);
1221 intel_wait_for_vblank(dev_priv->dev, pipe);
1222}
1223
1224/*
1225 * Plane regs are double buffered, going from enabled->disabled needs a
1226 * trigger in order to latch. The display address reg provides this.
1227 */
1228static void intel_flush_display_plane(struct drm_i915_private *dev_priv,
1229 enum plane plane)
1230{
1231 u32 reg = DSPADDR(plane);
1232 I915_WRITE(reg, I915_READ(reg));
1233}
1234
1235/**
1236 * intel_disable_plane - disable a display plane
1237 * @dev_priv: i915 private structure
1238 * @plane: plane to disable
1239 * @pipe: pipe consuming the data
1240 *
1241 * Disable @plane; should be an independent operation.
1242 */
1243static void intel_disable_plane(struct drm_i915_private *dev_priv,
1244 enum plane plane, enum pipe pipe)
1245{
1246 int reg;
1247 u32 val;
1248
1249 reg = DSPCNTR(plane);
1250 val = I915_READ(reg);
1251 val &= ~DISPLAY_PLANE_ENABLE;
1252 I915_WRITE(reg, val);
1253 POSTING_READ(reg);
1254 intel_flush_display_plane(dev_priv, plane);
1255 intel_wait_for_vblank(dev_priv->dev, pipe);
1256}
1257
Jesse Barnes80824002009-09-10 15:28:06 -07001258static void i8xx_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
1259{
1260 struct drm_device *dev = crtc->dev;
1261 struct drm_i915_private *dev_priv = dev->dev_private;
1262 struct drm_framebuffer *fb = crtc->fb;
1263 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00001264 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes80824002009-09-10 15:28:06 -07001265 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1266 int plane, i;
1267 u32 fbc_ctl, fbc_ctl2;
1268
Chris Wilsonbed4a672010-09-11 10:47:47 +01001269 if (fb->pitch == dev_priv->cfb_pitch &&
Chris Wilson05394f32010-11-08 19:18:58 +00001270 obj->fence_reg == dev_priv->cfb_fence &&
Chris Wilsonbed4a672010-09-11 10:47:47 +01001271 intel_crtc->plane == dev_priv->cfb_plane &&
1272 I915_READ(FBC_CONTROL) & FBC_CTL_EN)
1273 return;
1274
1275 i8xx_disable_fbc(dev);
1276
Jesse Barnes80824002009-09-10 15:28:06 -07001277 dev_priv->cfb_pitch = dev_priv->cfb_size / FBC_LL_SIZE;
1278
1279 if (fb->pitch < dev_priv->cfb_pitch)
1280 dev_priv->cfb_pitch = fb->pitch;
1281
1282 /* FBC_CTL wants 64B units */
1283 dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
Chris Wilson05394f32010-11-08 19:18:58 +00001284 dev_priv->cfb_fence = obj->fence_reg;
Jesse Barnes80824002009-09-10 15:28:06 -07001285 dev_priv->cfb_plane = intel_crtc->plane;
1286 plane = dev_priv->cfb_plane == 0 ? FBC_CTL_PLANEA : FBC_CTL_PLANEB;
1287
1288 /* Clear old tags */
1289 for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
1290 I915_WRITE(FBC_TAG + (i * 4), 0);
1291
1292 /* Set it up... */
1293 fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | plane;
Chris Wilson05394f32010-11-08 19:18:58 +00001294 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes80824002009-09-10 15:28:06 -07001295 fbc_ctl2 |= FBC_CTL_CPU_FENCE;
1296 I915_WRITE(FBC_CONTROL2, fbc_ctl2);
1297 I915_WRITE(FBC_FENCE_OFF, crtc->y);
1298
1299 /* enable it... */
1300 fbc_ctl = FBC_CTL_EN | FBC_CTL_PERIODIC;
Jesse Barnesee25df22010-02-06 10:41:53 -08001301 if (IS_I945GM(dev))
Priit Laes49677902010-03-02 11:37:00 +02001302 fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
Jesse Barnes80824002009-09-10 15:28:06 -07001303 fbc_ctl |= (dev_priv->cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
1304 fbc_ctl |= (interval & 0x2fff) << FBC_CTL_INTERVAL_SHIFT;
Chris Wilson05394f32010-11-08 19:18:58 +00001305 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes80824002009-09-10 15:28:06 -07001306 fbc_ctl |= dev_priv->cfb_fence;
1307 I915_WRITE(FBC_CONTROL, fbc_ctl);
1308
Zhao Yakui28c97732009-10-09 11:39:41 +08001309 DRM_DEBUG_KMS("enabled FBC, pitch %ld, yoff %d, plane %d, ",
Chris Wilson5eddb702010-09-11 13:48:45 +01001310 dev_priv->cfb_pitch, crtc->y, dev_priv->cfb_plane);
Jesse Barnes80824002009-09-10 15:28:06 -07001311}
1312
1313void i8xx_disable_fbc(struct drm_device *dev)
1314{
1315 struct drm_i915_private *dev_priv = dev->dev_private;
1316 u32 fbc_ctl;
1317
1318 /* Disable compression */
1319 fbc_ctl = I915_READ(FBC_CONTROL);
Chris Wilsona5cad622010-09-22 13:15:10 +01001320 if ((fbc_ctl & FBC_CTL_EN) == 0)
1321 return;
1322
Jesse Barnes80824002009-09-10 15:28:06 -07001323 fbc_ctl &= ~FBC_CTL_EN;
1324 I915_WRITE(FBC_CONTROL, fbc_ctl);
1325
1326 /* Wait for compressing bit to clear */
Chris Wilson481b6af2010-08-23 17:43:35 +01001327 if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
Chris Wilson913d8d12010-08-07 11:01:35 +01001328 DRM_DEBUG_KMS("FBC idle timed out\n");
1329 return;
Jesse Barnes9517a922010-05-21 09:40:45 -07001330 }
Jesse Barnes80824002009-09-10 15:28:06 -07001331
Zhao Yakui28c97732009-10-09 11:39:41 +08001332 DRM_DEBUG_KMS("disabled FBC\n");
Jesse Barnes80824002009-09-10 15:28:06 -07001333}
1334
Adam Jacksonee5382a2010-04-23 11:17:39 -04001335static bool i8xx_fbc_enabled(struct drm_device *dev)
Jesse Barnes80824002009-09-10 15:28:06 -07001336{
Jesse Barnes80824002009-09-10 15:28:06 -07001337 struct drm_i915_private *dev_priv = dev->dev_private;
1338
1339 return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
1340}
1341
Jesse Barnes74dff282009-09-14 15:39:40 -07001342static void g4x_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
1343{
1344 struct drm_device *dev = crtc->dev;
1345 struct drm_i915_private *dev_priv = dev->dev_private;
1346 struct drm_framebuffer *fb = crtc->fb;
1347 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00001348 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes74dff282009-09-14 15:39:40 -07001349 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5eddb702010-09-11 13:48:45 +01001350 int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
Jesse Barnes74dff282009-09-14 15:39:40 -07001351 unsigned long stall_watermark = 200;
1352 u32 dpfc_ctl;
1353
Chris Wilsonbed4a672010-09-11 10:47:47 +01001354 dpfc_ctl = I915_READ(DPFC_CONTROL);
1355 if (dpfc_ctl & DPFC_CTL_EN) {
1356 if (dev_priv->cfb_pitch == dev_priv->cfb_pitch / 64 - 1 &&
Chris Wilson05394f32010-11-08 19:18:58 +00001357 dev_priv->cfb_fence == obj->fence_reg &&
Chris Wilsonbed4a672010-09-11 10:47:47 +01001358 dev_priv->cfb_plane == intel_crtc->plane &&
1359 dev_priv->cfb_y == crtc->y)
1360 return;
1361
1362 I915_WRITE(DPFC_CONTROL, dpfc_ctl & ~DPFC_CTL_EN);
1363 POSTING_READ(DPFC_CONTROL);
1364 intel_wait_for_vblank(dev, intel_crtc->pipe);
1365 }
1366
Jesse Barnes74dff282009-09-14 15:39:40 -07001367 dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
Chris Wilson05394f32010-11-08 19:18:58 +00001368 dev_priv->cfb_fence = obj->fence_reg;
Jesse Barnes74dff282009-09-14 15:39:40 -07001369 dev_priv->cfb_plane = intel_crtc->plane;
Chris Wilsonbed4a672010-09-11 10:47:47 +01001370 dev_priv->cfb_y = crtc->y;
Jesse Barnes74dff282009-09-14 15:39:40 -07001371
1372 dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;
Chris Wilson05394f32010-11-08 19:18:58 +00001373 if (obj->tiling_mode != I915_TILING_NONE) {
Jesse Barnes74dff282009-09-14 15:39:40 -07001374 dpfc_ctl |= DPFC_CTL_FENCE_EN | dev_priv->cfb_fence;
1375 I915_WRITE(DPFC_CHICKEN, DPFC_HT_MODIFY);
1376 } else {
1377 I915_WRITE(DPFC_CHICKEN, ~DPFC_HT_MODIFY);
1378 }
1379
Jesse Barnes74dff282009-09-14 15:39:40 -07001380 I915_WRITE(DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
1381 (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
1382 (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
1383 I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
1384
1385 /* enable it... */
1386 I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);
1387
Zhao Yakui28c97732009-10-09 11:39:41 +08001388 DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
Jesse Barnes74dff282009-09-14 15:39:40 -07001389}
1390
1391void g4x_disable_fbc(struct drm_device *dev)
1392{
1393 struct drm_i915_private *dev_priv = dev->dev_private;
1394 u32 dpfc_ctl;
1395
1396 /* Disable compression */
1397 dpfc_ctl = I915_READ(DPFC_CONTROL);
Chris Wilsonbed4a672010-09-11 10:47:47 +01001398 if (dpfc_ctl & DPFC_CTL_EN) {
1399 dpfc_ctl &= ~DPFC_CTL_EN;
1400 I915_WRITE(DPFC_CONTROL, dpfc_ctl);
Jesse Barnes74dff282009-09-14 15:39:40 -07001401
Chris Wilsonbed4a672010-09-11 10:47:47 +01001402 DRM_DEBUG_KMS("disabled FBC\n");
1403 }
Jesse Barnes74dff282009-09-14 15:39:40 -07001404}
1405
Adam Jacksonee5382a2010-04-23 11:17:39 -04001406static bool g4x_fbc_enabled(struct drm_device *dev)
Jesse Barnes74dff282009-09-14 15:39:40 -07001407{
Jesse Barnes74dff282009-09-14 15:39:40 -07001408 struct drm_i915_private *dev_priv = dev->dev_private;
1409
1410 return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
1411}
1412
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001413static void ironlake_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
1414{
1415 struct drm_device *dev = crtc->dev;
1416 struct drm_i915_private *dev_priv = dev->dev_private;
1417 struct drm_framebuffer *fb = crtc->fb;
1418 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00001419 struct drm_i915_gem_object *obj = intel_fb->obj;
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001420 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5eddb702010-09-11 13:48:45 +01001421 int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001422 unsigned long stall_watermark = 200;
1423 u32 dpfc_ctl;
1424
Chris Wilsonbed4a672010-09-11 10:47:47 +01001425 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
1426 if (dpfc_ctl & DPFC_CTL_EN) {
1427 if (dev_priv->cfb_pitch == dev_priv->cfb_pitch / 64 - 1 &&
Chris Wilson05394f32010-11-08 19:18:58 +00001428 dev_priv->cfb_fence == obj->fence_reg &&
Chris Wilsonbed4a672010-09-11 10:47:47 +01001429 dev_priv->cfb_plane == intel_crtc->plane &&
Chris Wilson05394f32010-11-08 19:18:58 +00001430 dev_priv->cfb_offset == obj->gtt_offset &&
Chris Wilsonbed4a672010-09-11 10:47:47 +01001431 dev_priv->cfb_y == crtc->y)
1432 return;
1433
1434 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl & ~DPFC_CTL_EN);
1435 POSTING_READ(ILK_DPFC_CONTROL);
1436 intel_wait_for_vblank(dev, intel_crtc->pipe);
1437 }
1438
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001439 dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
Chris Wilson05394f32010-11-08 19:18:58 +00001440 dev_priv->cfb_fence = obj->fence_reg;
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001441 dev_priv->cfb_plane = intel_crtc->plane;
Chris Wilson05394f32010-11-08 19:18:58 +00001442 dev_priv->cfb_offset = obj->gtt_offset;
Chris Wilsonbed4a672010-09-11 10:47:47 +01001443 dev_priv->cfb_y = crtc->y;
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001444
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001445 dpfc_ctl &= DPFC_RESERVED;
1446 dpfc_ctl |= (plane | DPFC_CTL_LIMIT_1X);
Chris Wilson05394f32010-11-08 19:18:58 +00001447 if (obj->tiling_mode != I915_TILING_NONE) {
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001448 dpfc_ctl |= (DPFC_CTL_FENCE_EN | dev_priv->cfb_fence);
1449 I915_WRITE(ILK_DPFC_CHICKEN, DPFC_HT_MODIFY);
1450 } else {
1451 I915_WRITE(ILK_DPFC_CHICKEN, ~DPFC_HT_MODIFY);
1452 }
1453
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001454 I915_WRITE(ILK_DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
1455 (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
1456 (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
1457 I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
Chris Wilson05394f32010-11-08 19:18:58 +00001458 I915_WRITE(ILK_FBC_RT_BASE, obj->gtt_offset | ILK_FBC_RT_VALID);
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001459 /* enable it... */
Chris Wilsonbed4a672010-09-11 10:47:47 +01001460 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001461
Yuanhan Liu9c04f012010-12-15 15:42:32 +08001462 if (IS_GEN6(dev)) {
1463 I915_WRITE(SNB_DPFC_CTL_SA,
1464 SNB_CPU_FENCE_ENABLE | dev_priv->cfb_fence);
1465 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
1466 }
1467
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001468 DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
1469}
1470
1471void ironlake_disable_fbc(struct drm_device *dev)
1472{
1473 struct drm_i915_private *dev_priv = dev->dev_private;
1474 u32 dpfc_ctl;
1475
1476 /* Disable compression */
1477 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
Chris Wilsonbed4a672010-09-11 10:47:47 +01001478 if (dpfc_ctl & DPFC_CTL_EN) {
1479 dpfc_ctl &= ~DPFC_CTL_EN;
1480 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001481
Chris Wilsonbed4a672010-09-11 10:47:47 +01001482 DRM_DEBUG_KMS("disabled FBC\n");
1483 }
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001484}
1485
1486static bool ironlake_fbc_enabled(struct drm_device *dev)
1487{
1488 struct drm_i915_private *dev_priv = dev->dev_private;
1489
1490 return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
1491}
1492
Adam Jacksonee5382a2010-04-23 11:17:39 -04001493bool intel_fbc_enabled(struct drm_device *dev)
1494{
1495 struct drm_i915_private *dev_priv = dev->dev_private;
1496
1497 if (!dev_priv->display.fbc_enabled)
1498 return false;
1499
1500 return dev_priv->display.fbc_enabled(dev);
1501}
1502
1503void intel_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
1504{
1505 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
1506
1507 if (!dev_priv->display.enable_fbc)
1508 return;
1509
1510 dev_priv->display.enable_fbc(crtc, interval);
1511}
1512
1513void intel_disable_fbc(struct drm_device *dev)
1514{
1515 struct drm_i915_private *dev_priv = dev->dev_private;
1516
1517 if (!dev_priv->display.disable_fbc)
1518 return;
1519
1520 dev_priv->display.disable_fbc(dev);
1521}
1522
Jesse Barnes80824002009-09-10 15:28:06 -07001523/**
1524 * intel_update_fbc - enable/disable FBC as needed
Chris Wilsonbed4a672010-09-11 10:47:47 +01001525 * @dev: the drm_device
Jesse Barnes80824002009-09-10 15:28:06 -07001526 *
1527 * Set up the framebuffer compression hardware at mode set time. We
1528 * enable it if possible:
1529 * - plane A only (on pre-965)
1530 * - no pixel mulitply/line duplication
1531 * - no alpha buffer discard
1532 * - no dual wide
1533 * - framebuffer <= 2048 in width, 1536 in height
1534 *
1535 * We can't assume that any compression will take place (worst case),
1536 * so the compressed buffer has to be the same size as the uncompressed
1537 * one. It also must reside (along with the line length buffer) in
1538 * stolen memory.
1539 *
1540 * We need to enable/disable FBC on a global basis.
1541 */
Chris Wilsonbed4a672010-09-11 10:47:47 +01001542static void intel_update_fbc(struct drm_device *dev)
Jesse Barnes80824002009-09-10 15:28:06 -07001543{
Jesse Barnes80824002009-09-10 15:28:06 -07001544 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonbed4a672010-09-11 10:47:47 +01001545 struct drm_crtc *crtc = NULL, *tmp_crtc;
1546 struct intel_crtc *intel_crtc;
1547 struct drm_framebuffer *fb;
Jesse Barnes80824002009-09-10 15:28:06 -07001548 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00001549 struct drm_i915_gem_object *obj;
Jesse Barnes9c928d12010-07-23 15:20:00 -07001550
1551 DRM_DEBUG_KMS("\n");
Jesse Barnes80824002009-09-10 15:28:06 -07001552
1553 if (!i915_powersave)
1554 return;
1555
Adam Jacksonee5382a2010-04-23 11:17:39 -04001556 if (!I915_HAS_FBC(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07001557 return;
1558
Jesse Barnes80824002009-09-10 15:28:06 -07001559 /*
1560 * If FBC is already on, we just have to verify that we can
1561 * keep it that way...
1562 * Need to disable if:
Jesse Barnes9c928d12010-07-23 15:20:00 -07001563 * - more than one pipe is active
Jesse Barnes80824002009-09-10 15:28:06 -07001564 * - changing FBC params (stride, fence, mode)
1565 * - new fb is too large to fit in compressed buffer
1566 * - going to an unsupported config (interlace, pixel multiply, etc.)
1567 */
Jesse Barnes9c928d12010-07-23 15:20:00 -07001568 list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head) {
Chris Wilsonbed4a672010-09-11 10:47:47 +01001569 if (tmp_crtc->enabled) {
1570 if (crtc) {
1571 DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
1572 dev_priv->no_fbc_reason = FBC_MULTIPLE_PIPES;
1573 goto out_disable;
1574 }
1575 crtc = tmp_crtc;
1576 }
Jesse Barnes9c928d12010-07-23 15:20:00 -07001577 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01001578
1579 if (!crtc || crtc->fb == NULL) {
1580 DRM_DEBUG_KMS("no output, disabling\n");
1581 dev_priv->no_fbc_reason = FBC_NO_OUTPUT;
Jesse Barnes9c928d12010-07-23 15:20:00 -07001582 goto out_disable;
1583 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01001584
1585 intel_crtc = to_intel_crtc(crtc);
1586 fb = crtc->fb;
1587 intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00001588 obj = intel_fb->obj;
Chris Wilsonbed4a672010-09-11 10:47:47 +01001589
Chris Wilson05394f32010-11-08 19:18:58 +00001590 if (intel_fb->obj->base.size > dev_priv->cfb_size) {
Zhao Yakui28c97732009-10-09 11:39:41 +08001591 DRM_DEBUG_KMS("framebuffer too large, disabling "
Chris Wilson5eddb702010-09-11 13:48:45 +01001592 "compression\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08001593 dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
Jesse Barnes80824002009-09-10 15:28:06 -07001594 goto out_disable;
1595 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01001596 if ((crtc->mode.flags & DRM_MODE_FLAG_INTERLACE) ||
1597 (crtc->mode.flags & DRM_MODE_FLAG_DBLSCAN)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08001598 DRM_DEBUG_KMS("mode incompatible with compression, "
Chris Wilson5eddb702010-09-11 13:48:45 +01001599 "disabling\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08001600 dev_priv->no_fbc_reason = FBC_UNSUPPORTED_MODE;
Jesse Barnes80824002009-09-10 15:28:06 -07001601 goto out_disable;
1602 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01001603 if ((crtc->mode.hdisplay > 2048) ||
1604 (crtc->mode.vdisplay > 1536)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08001605 DRM_DEBUG_KMS("mode too large for compression, disabling\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08001606 dev_priv->no_fbc_reason = FBC_MODE_TOO_LARGE;
Jesse Barnes80824002009-09-10 15:28:06 -07001607 goto out_disable;
1608 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01001609 if ((IS_I915GM(dev) || IS_I945GM(dev)) && intel_crtc->plane != 0) {
Zhao Yakui28c97732009-10-09 11:39:41 +08001610 DRM_DEBUG_KMS("plane not 0, disabling compression\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08001611 dev_priv->no_fbc_reason = FBC_BAD_PLANE;
Jesse Barnes80824002009-09-10 15:28:06 -07001612 goto out_disable;
1613 }
Chris Wilson05394f32010-11-08 19:18:58 +00001614 if (obj->tiling_mode != I915_TILING_X) {
Zhao Yakui28c97732009-10-09 11:39:41 +08001615 DRM_DEBUG_KMS("framebuffer not tiled, disabling compression\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08001616 dev_priv->no_fbc_reason = FBC_NOT_TILED;
Jesse Barnes80824002009-09-10 15:28:06 -07001617 goto out_disable;
1618 }
1619
Jason Wesselc924b932010-08-05 09:22:32 -05001620 /* If the kernel debugger is active, always disable compression */
1621 if (in_dbg_master())
1622 goto out_disable;
1623
Chris Wilsonbed4a672010-09-11 10:47:47 +01001624 intel_enable_fbc(crtc, 500);
Jesse Barnes80824002009-09-10 15:28:06 -07001625 return;
1626
1627out_disable:
Jesse Barnes80824002009-09-10 15:28:06 -07001628 /* Multiple disables should be harmless */
Chris Wilsona9394062010-05-27 13:18:16 +01001629 if (intel_fbc_enabled(dev)) {
1630 DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
Adam Jacksonee5382a2010-04-23 11:17:39 -04001631 intel_disable_fbc(dev);
Chris Wilsona9394062010-05-27 13:18:16 +01001632 }
Jesse Barnes80824002009-09-10 15:28:06 -07001633}
1634
Chris Wilson127bd2a2010-07-23 23:32:05 +01001635int
Chris Wilson48b956c2010-09-14 12:50:34 +01001636intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00001637 struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00001638 struct intel_ring_buffer *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001639{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001640 u32 alignment;
1641 int ret;
1642
Chris Wilson05394f32010-11-08 19:18:58 +00001643 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001644 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01001645 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1646 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001647 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01001648 alignment = 4 * 1024;
1649 else
1650 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001651 break;
1652 case I915_TILING_X:
1653 /* pin() will align the object as required by fence */
1654 alignment = 0;
1655 break;
1656 case I915_TILING_Y:
1657 /* FIXME: Is this true? */
1658 DRM_ERROR("Y tiled not allowed for scan out buffers\n");
1659 return -EINVAL;
1660 default:
1661 BUG();
1662 }
1663
Daniel Vetter75e9e912010-11-04 17:11:09 +01001664 ret = i915_gem_object_pin(obj, alignment, true);
Chris Wilson48b956c2010-09-14 12:50:34 +01001665 if (ret)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001666 return ret;
1667
Chris Wilson48b956c2010-09-14 12:50:34 +01001668 ret = i915_gem_object_set_to_display_plane(obj, pipelined);
1669 if (ret)
1670 goto err_unpin;
Chris Wilson72133422010-09-13 23:56:38 +01001671
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001672 /* Install a fence for tiled scan-out. Pre-i965 always needs a
1673 * fence, whereas 965+ only requires a fence if using
1674 * framebuffer compression. For simplicity, we always install
1675 * a fence as the cost is not that onerous.
1676 */
Chris Wilson05394f32010-11-08 19:18:58 +00001677 if (obj->tiling_mode != I915_TILING_NONE) {
Chris Wilsond9e86c02010-11-10 16:40:20 +00001678 ret = i915_gem_object_get_fence(obj, pipelined, false);
Chris Wilson48b956c2010-09-14 12:50:34 +01001679 if (ret)
1680 goto err_unpin;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001681 }
1682
1683 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01001684
1685err_unpin:
1686 i915_gem_object_unpin(obj);
1687 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001688}
1689
Jesse Barnes81255562010-08-02 12:07:50 -07001690/* Assume fb object is pinned & idle & fenced and just update base pointers */
1691static int
1692intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
Jason Wessel21c74a82010-10-13 14:09:44 -05001693 int x, int y, enum mode_set_atomic state)
Jesse Barnes81255562010-08-02 12:07:50 -07001694{
1695 struct drm_device *dev = crtc->dev;
1696 struct drm_i915_private *dev_priv = dev->dev_private;
1697 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1698 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00001699 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07001700 int plane = intel_crtc->plane;
1701 unsigned long Start, Offset;
Jesse Barnes81255562010-08-02 12:07:50 -07001702 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01001703 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07001704
1705 switch (plane) {
1706 case 0:
1707 case 1:
1708 break;
1709 default:
1710 DRM_ERROR("Can't update plane %d in SAREA\n", plane);
1711 return -EINVAL;
1712 }
1713
1714 intel_fb = to_intel_framebuffer(fb);
1715 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07001716
Chris Wilson5eddb702010-09-11 13:48:45 +01001717 reg = DSPCNTR(plane);
1718 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07001719 /* Mask out pixel format bits in case we change it */
1720 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
1721 switch (fb->bits_per_pixel) {
1722 case 8:
1723 dspcntr |= DISPPLANE_8BPP;
1724 break;
1725 case 16:
1726 if (fb->depth == 15)
1727 dspcntr |= DISPPLANE_15_16BPP;
1728 else
1729 dspcntr |= DISPPLANE_16BPP;
1730 break;
1731 case 24:
1732 case 32:
1733 dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
1734 break;
1735 default:
1736 DRM_ERROR("Unknown color depth\n");
1737 return -EINVAL;
1738 }
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001739 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00001740 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07001741 dspcntr |= DISPPLANE_TILED;
1742 else
1743 dspcntr &= ~DISPPLANE_TILED;
1744 }
1745
Chris Wilson4e6cfef2010-08-08 13:20:19 +01001746 if (HAS_PCH_SPLIT(dev))
Jesse Barnes81255562010-08-02 12:07:50 -07001747 /* must disable */
1748 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
1749
Chris Wilson5eddb702010-09-11 13:48:45 +01001750 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07001751
Chris Wilson05394f32010-11-08 19:18:58 +00001752 Start = obj->gtt_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07001753 Offset = y * fb->pitch + x * (fb->bits_per_pixel / 8);
1754
Chris Wilson4e6cfef2010-08-08 13:20:19 +01001755 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
1756 Start, Offset, x, y, fb->pitch);
Chris Wilson5eddb702010-09-11 13:48:45 +01001757 I915_WRITE(DSPSTRIDE(plane), fb->pitch);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001758 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson5eddb702010-09-11 13:48:45 +01001759 I915_WRITE(DSPSURF(plane), Start);
1760 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
1761 I915_WRITE(DSPADDR(plane), Offset);
1762 } else
1763 I915_WRITE(DSPADDR(plane), Start + Offset);
1764 POSTING_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07001765
Chris Wilsonbed4a672010-09-11 10:47:47 +01001766 intel_update_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02001767 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07001768
1769 return 0;
1770}
1771
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001772static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05001773intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
1774 struct drm_framebuffer *old_fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08001775{
1776 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08001777 struct drm_i915_master_private *master_priv;
1778 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001779 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08001780
1781 /* no fb bound */
1782 if (!crtc->fb) {
Zhao Yakui28c97732009-10-09 11:39:41 +08001783 DRM_DEBUG_KMS("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001784 return 0;
1785 }
1786
Chris Wilson265db952010-09-20 15:41:01 +01001787 switch (intel_crtc->plane) {
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001788 case 0:
1789 case 1:
1790 break;
1791 default:
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001792 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08001793 }
1794
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001795 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01001796 ret = intel_pin_and_fence_fb_obj(dev,
1797 to_intel_framebuffer(crtc->fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00001798 NULL);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001799 if (ret != 0) {
1800 mutex_unlock(&dev->struct_mutex);
1801 return ret;
1802 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05001803
Chris Wilson265db952010-09-20 15:41:01 +01001804 if (old_fb) {
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01001805 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001806 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
Chris Wilson265db952010-09-20 15:41:01 +01001807
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01001808 wait_event(dev_priv->pending_flip_queue,
Chris Wilson05394f32010-11-08 19:18:58 +00001809 atomic_read(&obj->pending_flip) == 0);
Chris Wilson85345512010-11-13 09:49:11 +00001810
1811 /* Big Hammer, we also need to ensure that any pending
1812 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
1813 * current scanout is retired before unpinning the old
1814 * framebuffer.
1815 */
Chris Wilson05394f32010-11-08 19:18:58 +00001816 ret = i915_gem_object_flush_gpu(obj, false);
Chris Wilson85345512010-11-13 09:49:11 +00001817 if (ret) {
1818 i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
1819 mutex_unlock(&dev->struct_mutex);
1820 return ret;
1821 }
Chris Wilson265db952010-09-20 15:41:01 +01001822 }
1823
Jason Wessel21c74a82010-10-13 14:09:44 -05001824 ret = intel_pipe_set_base_atomic(crtc, crtc->fb, x, y,
1825 LEAVE_ATOMIC_MODE_SET);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01001826 if (ret) {
Chris Wilson265db952010-09-20 15:41:01 +01001827 i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001828 mutex_unlock(&dev->struct_mutex);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01001829 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08001830 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05001831
Chris Wilsonb7f1de22010-12-14 16:09:31 +00001832 if (old_fb) {
1833 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson265db952010-09-20 15:41:01 +01001834 i915_gem_object_unpin(to_intel_framebuffer(old_fb)->obj);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00001835 }
Jesse Barnes652c3932009-08-17 13:31:43 -07001836
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001837 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08001838
1839 if (!dev->primary->master)
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001840 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08001841
1842 master_priv = dev->primary->master->driver_priv;
1843 if (!master_priv->sarea_priv)
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001844 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08001845
Chris Wilson265db952010-09-20 15:41:01 +01001846 if (intel_crtc->pipe) {
Jesse Barnes79e53942008-11-07 14:24:08 -08001847 master_priv->sarea_priv->pipeB_x = x;
1848 master_priv->sarea_priv->pipeB_y = y;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001849 } else {
1850 master_priv->sarea_priv->pipeA_x = x;
1851 master_priv->sarea_priv->pipeA_y = y;
Jesse Barnes79e53942008-11-07 14:24:08 -08001852 }
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001853
1854 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08001855}
1856
Chris Wilson5eddb702010-09-11 13:48:45 +01001857static void ironlake_set_pll_edp(struct drm_crtc *crtc, int clock)
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001858{
1859 struct drm_device *dev = crtc->dev;
1860 struct drm_i915_private *dev_priv = dev->dev_private;
1861 u32 dpa_ctl;
1862
Zhao Yakui28c97732009-10-09 11:39:41 +08001863 DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", clock);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001864 dpa_ctl = I915_READ(DP_A);
1865 dpa_ctl &= ~DP_PLL_FREQ_MASK;
1866
1867 if (clock < 200000) {
1868 u32 temp;
1869 dpa_ctl |= DP_PLL_FREQ_160MHZ;
1870 /* workaround for 160Mhz:
1871 1) program 0x4600c bits 15:0 = 0x8124
1872 2) program 0x46010 bit 0 = 1
1873 3) program 0x46034 bit 24 = 1
1874 4) program 0x64000 bit 14 = 1
1875 */
1876 temp = I915_READ(0x4600c);
1877 temp &= 0xffff0000;
1878 I915_WRITE(0x4600c, temp | 0x8124);
1879
1880 temp = I915_READ(0x46010);
1881 I915_WRITE(0x46010, temp | 1);
1882
1883 temp = I915_READ(0x46034);
1884 I915_WRITE(0x46034, temp | (1 << 24));
1885 } else {
1886 dpa_ctl |= DP_PLL_FREQ_270MHZ;
1887 }
1888 I915_WRITE(DP_A, dpa_ctl);
1889
Chris Wilson5eddb702010-09-11 13:48:45 +01001890 POSTING_READ(DP_A);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001891 udelay(500);
1892}
1893
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08001894static void intel_fdi_normal_train(struct drm_crtc *crtc)
1895{
1896 struct drm_device *dev = crtc->dev;
1897 struct drm_i915_private *dev_priv = dev->dev_private;
1898 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1899 int pipe = intel_crtc->pipe;
1900 u32 reg, temp;
1901
1902 /* enable normal train */
1903 reg = FDI_TX_CTL(pipe);
1904 temp = I915_READ(reg);
1905 temp &= ~FDI_LINK_TRAIN_NONE;
1906 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
1907 I915_WRITE(reg, temp);
1908
1909 reg = FDI_RX_CTL(pipe);
1910 temp = I915_READ(reg);
1911 if (HAS_PCH_CPT(dev)) {
1912 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
1913 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
1914 } else {
1915 temp &= ~FDI_LINK_TRAIN_NONE;
1916 temp |= FDI_LINK_TRAIN_NONE;
1917 }
1918 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
1919
1920 /* wait one idle pattern time */
1921 POSTING_READ(reg);
1922 udelay(1000);
1923}
1924
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001925/* The FDI link training functions for ILK/Ibexpeak. */
1926static void ironlake_fdi_link_train(struct drm_crtc *crtc)
1927{
1928 struct drm_device *dev = crtc->dev;
1929 struct drm_i915_private *dev_priv = dev->dev_private;
1930 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1931 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01001932 u32 reg, temp, tries;
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001933
Adam Jacksone1a44742010-06-25 15:32:14 -04001934 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
1935 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01001936 reg = FDI_RX_IMR(pipe);
1937 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04001938 temp &= ~FDI_RX_SYMBOL_LOCK;
1939 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01001940 I915_WRITE(reg, temp);
1941 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04001942 udelay(150);
1943
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001944 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01001945 reg = FDI_TX_CTL(pipe);
1946 temp = I915_READ(reg);
Adam Jackson77ffb592010-04-12 11:38:44 -04001947 temp &= ~(7 << 19);
1948 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001949 temp &= ~FDI_LINK_TRAIN_NONE;
1950 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01001951 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001952
Chris Wilson5eddb702010-09-11 13:48:45 +01001953 reg = FDI_RX_CTL(pipe);
1954 temp = I915_READ(reg);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001955 temp &= ~FDI_LINK_TRAIN_NONE;
1956 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01001957 I915_WRITE(reg, temp | FDI_RX_ENABLE);
1958
1959 POSTING_READ(reg);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001960 udelay(150);
1961
Jesse Barnes5b2adf82010-10-07 16:01:15 -07001962 /* Ironlake workaround, enable clock pointer after FDI enable*/
1963 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_ENABLE);
1964
Chris Wilson5eddb702010-09-11 13:48:45 +01001965 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04001966 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01001967 temp = I915_READ(reg);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001968 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
1969
1970 if ((temp & FDI_RX_BIT_LOCK)) {
1971 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01001972 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001973 break;
1974 }
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001975 }
Adam Jacksone1a44742010-06-25 15:32:14 -04001976 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01001977 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001978
1979 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01001980 reg = FDI_TX_CTL(pipe);
1981 temp = I915_READ(reg);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001982 temp &= ~FDI_LINK_TRAIN_NONE;
1983 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01001984 I915_WRITE(reg, temp);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001985
Chris Wilson5eddb702010-09-11 13:48:45 +01001986 reg = FDI_RX_CTL(pipe);
1987 temp = I915_READ(reg);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001988 temp &= ~FDI_LINK_TRAIN_NONE;
1989 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01001990 I915_WRITE(reg, temp);
1991
1992 POSTING_READ(reg);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001993 udelay(150);
1994
Chris Wilson5eddb702010-09-11 13:48:45 +01001995 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04001996 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01001997 temp = I915_READ(reg);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08001998 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
1999
2000 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002001 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08002002 DRM_DEBUG_KMS("FDI train 2 done.\n");
2003 break;
2004 }
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08002005 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002006 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002007 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08002008
2009 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002010
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08002011}
2012
Chris Wilson5eddb702010-09-11 13:48:45 +01002013static const int const snb_b_fdi_train_param [] = {
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08002014 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2015 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2016 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2017 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2018};
2019
2020/* The FDI link training functions for SNB/Cougarpoint. */
2021static void gen6_fdi_link_train(struct drm_crtc *crtc)
2022{
2023 struct drm_device *dev = crtc->dev;
2024 struct drm_i915_private *dev_priv = dev->dev_private;
2025 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2026 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002027 u32 reg, temp, i;
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08002028
Adam Jacksone1a44742010-06-25 15:32:14 -04002029 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2030 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002031 reg = FDI_RX_IMR(pipe);
2032 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002033 temp &= ~FDI_RX_SYMBOL_LOCK;
2034 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002035 I915_WRITE(reg, temp);
2036
2037 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002038 udelay(150);
2039
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08002040 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002041 reg = FDI_TX_CTL(pipe);
2042 temp = I915_READ(reg);
Adam Jackson77ffb592010-04-12 11:38:44 -04002043 temp &= ~(7 << 19);
2044 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08002045 temp &= ~FDI_LINK_TRAIN_NONE;
2046 temp |= FDI_LINK_TRAIN_PATTERN_1;
2047 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2048 /* SNB-B */
2049 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002050 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08002051
Chris Wilson5eddb702010-09-11 13:48:45 +01002052 reg = FDI_RX_CTL(pipe);
2053 temp = I915_READ(reg);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08002054 if (HAS_PCH_CPT(dev)) {
2055 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2056 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2057 } else {
2058 temp &= ~FDI_LINK_TRAIN_NONE;
2059 temp |= FDI_LINK_TRAIN_PATTERN_1;
2060 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002061 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2062
2063 POSTING_READ(reg);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08002064 udelay(150);
2065
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08002066 for (i = 0; i < 4; i++ ) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002067 reg = FDI_TX_CTL(pipe);
2068 temp = I915_READ(reg);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08002069 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2070 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002071 I915_WRITE(reg, temp);
2072
2073 POSTING_READ(reg);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08002074 udelay(500);
2075
Chris Wilson5eddb702010-09-11 13:48:45 +01002076 reg = FDI_RX_IIR(pipe);
2077 temp = I915_READ(reg);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08002078 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2079
2080 if (temp & FDI_RX_BIT_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002081 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08002082 DRM_DEBUG_KMS("FDI train 1 done.\n");
2083 break;
2084 }
2085 }
2086 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002087 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08002088
2089 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002090 reg = FDI_TX_CTL(pipe);
2091 temp = I915_READ(reg);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08002092 temp &= ~FDI_LINK_TRAIN_NONE;
2093 temp |= FDI_LINK_TRAIN_PATTERN_2;
2094 if (IS_GEN6(dev)) {
2095 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2096 /* SNB-B */
2097 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2098 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002099 I915_WRITE(reg, temp);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08002100
Chris Wilson5eddb702010-09-11 13:48:45 +01002101 reg = FDI_RX_CTL(pipe);
2102 temp = I915_READ(reg);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08002103 if (HAS_PCH_CPT(dev)) {
2104 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2105 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2106 } else {
2107 temp &= ~FDI_LINK_TRAIN_NONE;
2108 temp |= FDI_LINK_TRAIN_PATTERN_2;
2109 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002110 I915_WRITE(reg, temp);
2111
2112 POSTING_READ(reg);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08002113 udelay(150);
2114
2115 for (i = 0; i < 4; i++ ) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002116 reg = FDI_TX_CTL(pipe);
2117 temp = I915_READ(reg);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08002118 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2119 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002120 I915_WRITE(reg, temp);
2121
2122 POSTING_READ(reg);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08002123 udelay(500);
2124
Chris Wilson5eddb702010-09-11 13:48:45 +01002125 reg = FDI_RX_IIR(pipe);
2126 temp = I915_READ(reg);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08002127 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2128
2129 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002130 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08002131 DRM_DEBUG_KMS("FDI train 2 done.\n");
2132 break;
2133 }
2134 }
2135 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002136 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08002137
2138 DRM_DEBUG_KMS("FDI train done.\n");
2139}
2140
Jesse Barnes0e23b992010-09-10 11:10:00 -07002141static void ironlake_fdi_enable(struct drm_crtc *crtc)
2142{
2143 struct drm_device *dev = crtc->dev;
2144 struct drm_i915_private *dev_priv = dev->dev_private;
2145 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2146 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002147 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002148
Jesse Barnesc64e3112010-09-10 11:27:03 -07002149 /* Write the TU size bits so error detection works */
Chris Wilson5eddb702010-09-11 13:48:45 +01002150 I915_WRITE(FDI_RX_TUSIZE1(pipe),
2151 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
Jesse Barnesc64e3112010-09-10 11:27:03 -07002152
Jesse Barnes0e23b992010-09-10 11:10:00 -07002153 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01002154 reg = FDI_RX_CTL(pipe);
2155 temp = I915_READ(reg);
2156 temp &= ~((0x7 << 19) | (0x7 << 16));
Jesse Barnes0e23b992010-09-10 11:10:00 -07002157 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Chris Wilson5eddb702010-09-11 13:48:45 +01002158 temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2159 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2160
2161 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002162 udelay(200);
2163
2164 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002165 temp = I915_READ(reg);
2166 I915_WRITE(reg, temp | FDI_PCDCLK);
2167
2168 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002169 udelay(200);
2170
2171 /* Enable CPU FDI TX PLL, always on for Ironlake */
Chris Wilson5eddb702010-09-11 13:48:45 +01002172 reg = FDI_TX_CTL(pipe);
2173 temp = I915_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002174 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002175 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
2176
2177 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002178 udelay(100);
2179 }
2180}
2181
Chris Wilson6b383a72010-09-13 13:54:26 +01002182/*
2183 * When we disable a pipe, we need to clear any pending scanline wait events
2184 * to avoid hanging the ring, which we assume we are waiting on.
2185 */
2186static void intel_clear_scanline_wait(struct drm_device *dev)
2187{
2188 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson8168bd42010-11-11 17:54:52 +00002189 struct intel_ring_buffer *ring;
Chris Wilson6b383a72010-09-13 13:54:26 +01002190 u32 tmp;
2191
2192 if (IS_GEN2(dev))
2193 /* Can't break the hang on i8xx */
2194 return;
2195
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002196 ring = LP_RING(dev_priv);
Chris Wilson8168bd42010-11-11 17:54:52 +00002197 tmp = I915_READ_CTL(ring);
2198 if (tmp & RING_WAIT)
2199 I915_WRITE_CTL(ring, tmp);
Chris Wilson6b383a72010-09-13 13:54:26 +01002200}
2201
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002202static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2203{
Chris Wilson05394f32010-11-08 19:18:58 +00002204 struct drm_i915_gem_object *obj;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002205 struct drm_i915_private *dev_priv;
2206
2207 if (crtc->fb == NULL)
2208 return;
2209
Chris Wilson05394f32010-11-08 19:18:58 +00002210 obj = to_intel_framebuffer(crtc->fb)->obj;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002211 dev_priv = crtc->dev->dev_private;
2212 wait_event(dev_priv->pending_flip_queue,
Chris Wilson05394f32010-11-08 19:18:58 +00002213 atomic_read(&obj->pending_flip) == 0);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002214}
2215
Jesse Barnes6be4a602010-09-10 10:26:01 -07002216static void ironlake_crtc_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08002217{
2218 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002219 struct drm_i915_private *dev_priv = dev->dev_private;
2220 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2221 int pipe = intel_crtc->pipe;
Shaohua Li7662c8b2009-06-26 11:23:55 +08002222 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002223 u32 reg, temp;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002224
Chris Wilsonf7abfe82010-09-13 14:19:16 +01002225 if (intel_crtc->active)
2226 return;
2227
2228 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01002229 intel_update_watermarks(dev);
2230
Jesse Barnes6be4a602010-09-10 10:26:01 -07002231 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
2232 temp = I915_READ(PCH_LVDS);
Chris Wilson5eddb702010-09-11 13:48:45 +01002233 if ((temp & LVDS_PORT_EN) == 0)
Jesse Barnes6be4a602010-09-10 10:26:01 -07002234 I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002235 }
2236
Jesse Barnes0e23b992010-09-10 11:10:00 -07002237 ironlake_fdi_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002238
2239 /* Enable panel fitting for LVDS */
2240 if (dev_priv->pch_pf_size &&
Jesse Barnes1d850362010-10-07 16:01:10 -07002241 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) || HAS_eDP)) {
Jesse Barnes6be4a602010-09-10 10:26:01 -07002242 /* Force use of hard-coded filter coefficients
2243 * as some pre-programmed values are broken,
2244 * e.g. x201.
2245 */
2246 I915_WRITE(pipe ? PFB_CTL_1 : PFA_CTL_1,
2247 PF_ENABLE | PF_FILTER_MED_3x3);
2248 I915_WRITE(pipe ? PFB_WIN_POS : PFA_WIN_POS,
2249 dev_priv->pch_pf_pos);
2250 I915_WRITE(pipe ? PFB_WIN_SZ : PFA_WIN_SZ,
2251 dev_priv->pch_pf_size);
2252 }
2253
Jesse Barnesb24e7172011-01-04 15:09:30 -08002254 intel_enable_pipe(dev_priv, pipe);
2255 intel_enable_plane(dev_priv, plane, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002256
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002257 /* For PCH output, training FDI link */
2258 if (IS_GEN6(dev))
2259 gen6_fdi_link_train(crtc);
2260 else
2261 ironlake_fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002262
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002263 /* enable PCH DPLL */
Chris Wilson5eddb702010-09-11 13:48:45 +01002264 reg = PCH_DPLL(pipe);
2265 temp = I915_READ(reg);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002266 if ((temp & DPLL_VCO_ENABLE) == 0) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002267 I915_WRITE(reg, temp | DPLL_VCO_ENABLE);
2268 POSTING_READ(reg);
Chris Wilson8c4223b2010-09-10 22:33:42 +01002269 udelay(200);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002270 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002271
2272 if (HAS_PCH_CPT(dev)) {
2273 /* Be sure PCH DPLL SEL is set */
2274 temp = I915_READ(PCH_DPLL_SEL);
Chris Wilson5eddb702010-09-11 13:48:45 +01002275 if (pipe == 0 && (temp & TRANSA_DPLL_ENABLE) == 0)
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002276 temp |= (TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL);
Chris Wilson5eddb702010-09-11 13:48:45 +01002277 else if (pipe == 1 && (temp & TRANSB_DPLL_ENABLE) == 0)
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002278 temp |= (TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
2279 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002280 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002281
Chris Wilson5eddb702010-09-11 13:48:45 +01002282 /* set transcoder timing */
2283 I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
2284 I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
2285 I915_WRITE(TRANS_HSYNC(pipe), I915_READ(HSYNC(pipe)));
2286
2287 I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
2288 I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
2289 I915_WRITE(TRANS_VSYNC(pipe), I915_READ(VSYNC(pipe)));
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002290
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002291 intel_fdi_normal_train(crtc);
2292
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002293 /* For PCH DP, enable TRANS_DP_CTL */
2294 if (HAS_PCH_CPT(dev) &&
2295 intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002296 reg = TRANS_DP_CTL(pipe);
2297 temp = I915_READ(reg);
2298 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08002299 TRANS_DP_SYNC_MASK |
2300 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01002301 temp |= (TRANS_DP_OUTPUT_ENABLE |
2302 TRANS_DP_ENH_FRAMING);
Eric Anholt220cad32010-11-18 09:32:58 +08002303 temp |= TRANS_DP_8BPC;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002304
2305 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01002306 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002307 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01002308 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002309
2310 switch (intel_trans_dp_port_sel(crtc)) {
2311 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01002312 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002313 break;
2314 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01002315 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002316 break;
2317 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01002318 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002319 break;
2320 default:
2321 DRM_DEBUG_KMS("Wrong PCH DP port return. Guess port B\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002322 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002323 break;
2324 }
2325
Chris Wilson5eddb702010-09-11 13:48:45 +01002326 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002327 }
2328
2329 /* enable PCH transcoder */
Chris Wilson5eddb702010-09-11 13:48:45 +01002330 reg = TRANSCONF(pipe);
2331 temp = I915_READ(reg);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002332 /*
2333 * make the BPC in transcoder be consistent with
2334 * that in pipeconf reg.
2335 */
2336 temp &= ~PIPE_BPC_MASK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002337 temp |= I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK;
2338 I915_WRITE(reg, temp | TRANS_ENABLE);
2339 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Jesse Barnes17f67662010-10-07 16:01:19 -07002340 DRM_ERROR("failed to enable transcoder %d\n", pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002341
2342 intel_crtc_load_lut(crtc);
Chris Wilsonbed4a672010-09-11 10:47:47 +01002343 intel_update_fbc(dev);
Chris Wilson6b383a72010-09-13 13:54:26 +01002344 intel_crtc_update_cursor(crtc, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002345}
2346
2347static void ironlake_crtc_disable(struct drm_crtc *crtc)
2348{
2349 struct drm_device *dev = crtc->dev;
2350 struct drm_i915_private *dev_priv = dev->dev_private;
2351 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2352 int pipe = intel_crtc->pipe;
2353 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002354 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07002355
Chris Wilsonf7abfe82010-09-13 14:19:16 +01002356 if (!intel_crtc->active)
2357 return;
2358
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002359 intel_crtc_wait_for_pending_flips(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002360 drm_vblank_off(dev, pipe);
Chris Wilson6b383a72010-09-13 13:54:26 +01002361 intel_crtc_update_cursor(crtc, false);
Chris Wilson5eddb702010-09-11 13:48:45 +01002362
Jesse Barnesb24e7172011-01-04 15:09:30 -08002363 intel_disable_plane(dev_priv, plane, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002364
2365 if (dev_priv->cfb_plane == plane &&
2366 dev_priv->display.disable_fbc)
2367 dev_priv->display.disable_fbc(dev);
2368
Jesse Barnesb24e7172011-01-04 15:09:30 -08002369 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002370
Jesse Barnes6be4a602010-09-10 10:26:01 -07002371 /* Disable PF */
2372 I915_WRITE(pipe ? PFB_CTL_1 : PFA_CTL_1, 0);
2373 I915_WRITE(pipe ? PFB_WIN_SZ : PFA_WIN_SZ, 0);
2374
2375 /* disable CPU FDI tx and PCH FDI rx */
Chris Wilson5eddb702010-09-11 13:48:45 +01002376 reg = FDI_TX_CTL(pipe);
2377 temp = I915_READ(reg);
2378 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2379 POSTING_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002380
Chris Wilson5eddb702010-09-11 13:48:45 +01002381 reg = FDI_RX_CTL(pipe);
2382 temp = I915_READ(reg);
2383 temp &= ~(0x7 << 16);
2384 temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2385 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002386
Chris Wilson5eddb702010-09-11 13:48:45 +01002387 POSTING_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002388 udelay(100);
2389
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002390 /* Ironlake workaround, disable clock pointer after downing FDI */
Zhenyu Wange07ac3a2010-11-04 09:02:54 +00002391 if (HAS_PCH_IBX(dev))
2392 I915_WRITE(FDI_RX_CHICKEN(pipe),
2393 I915_READ(FDI_RX_CHICKEN(pipe) &
2394 ~FDI_RX_PHASE_SYNC_POINTER_ENABLE));
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002395
Jesse Barnes6be4a602010-09-10 10:26:01 -07002396 /* still set train pattern 1 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002397 reg = FDI_TX_CTL(pipe);
2398 temp = I915_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002399 temp &= ~FDI_LINK_TRAIN_NONE;
2400 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002401 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002402
Chris Wilson5eddb702010-09-11 13:48:45 +01002403 reg = FDI_RX_CTL(pipe);
2404 temp = I915_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002405 if (HAS_PCH_CPT(dev)) {
2406 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2407 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2408 } else {
2409 temp &= ~FDI_LINK_TRAIN_NONE;
2410 temp |= FDI_LINK_TRAIN_PATTERN_1;
2411 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002412 /* BPC in FDI rx is consistent with that in PIPECONF */
2413 temp &= ~(0x07 << 16);
2414 temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2415 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002416
Chris Wilson5eddb702010-09-11 13:48:45 +01002417 POSTING_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002418 udelay(100);
2419
2420 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
2421 temp = I915_READ(PCH_LVDS);
Chris Wilson5eddb702010-09-11 13:48:45 +01002422 if (temp & LVDS_PORT_EN) {
2423 I915_WRITE(PCH_LVDS, temp & ~LVDS_PORT_EN);
2424 POSTING_READ(PCH_LVDS);
2425 udelay(100);
2426 }
Jesse Barnes6be4a602010-09-10 10:26:01 -07002427 }
2428
2429 /* disable PCH transcoder */
Chris Wilson5eddb702010-09-11 13:48:45 +01002430 reg = TRANSCONF(plane);
2431 temp = I915_READ(reg);
2432 if (temp & TRANS_ENABLE) {
2433 I915_WRITE(reg, temp & ~TRANS_ENABLE);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002434 /* wait for PCH transcoder off, transcoder state */
Chris Wilson5eddb702010-09-11 13:48:45 +01002435 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Jesse Barnes6be4a602010-09-10 10:26:01 -07002436 DRM_ERROR("failed to disable transcoder\n");
2437 }
2438
Jesse Barnes6be4a602010-09-10 10:26:01 -07002439 if (HAS_PCH_CPT(dev)) {
2440 /* disable TRANS_DP_CTL */
Chris Wilson5eddb702010-09-11 13:48:45 +01002441 reg = TRANS_DP_CTL(pipe);
2442 temp = I915_READ(reg);
2443 temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
2444 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002445
2446 /* disable DPLL_SEL */
2447 temp = I915_READ(PCH_DPLL_SEL);
Chris Wilson5eddb702010-09-11 13:48:45 +01002448 if (pipe == 0)
Jesse Barnes6be4a602010-09-10 10:26:01 -07002449 temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
2450 else
2451 temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
2452 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002453 }
2454
2455 /* disable PCH DPLL */
Chris Wilson5eddb702010-09-11 13:48:45 +01002456 reg = PCH_DPLL(pipe);
2457 temp = I915_READ(reg);
2458 I915_WRITE(reg, temp & ~DPLL_VCO_ENABLE);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002459
2460 /* Switch from PCDclk to Rawclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002461 reg = FDI_RX_CTL(pipe);
2462 temp = I915_READ(reg);
2463 I915_WRITE(reg, temp & ~FDI_PCDCLK);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002464
2465 /* Disable CPU FDI TX PLL */
Chris Wilson5eddb702010-09-11 13:48:45 +01002466 reg = FDI_TX_CTL(pipe);
2467 temp = I915_READ(reg);
2468 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2469
2470 POSTING_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002471 udelay(100);
2472
Chris Wilson5eddb702010-09-11 13:48:45 +01002473 reg = FDI_RX_CTL(pipe);
2474 temp = I915_READ(reg);
2475 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002476
2477 /* Wait for the clocks to turn off. */
Chris Wilson5eddb702010-09-11 13:48:45 +01002478 POSTING_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002479 udelay(100);
Chris Wilson6b383a72010-09-13 13:54:26 +01002480
Chris Wilsonf7abfe82010-09-13 14:19:16 +01002481 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01002482 intel_update_watermarks(dev);
2483 intel_update_fbc(dev);
2484 intel_clear_scanline_wait(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002485}
2486
2487static void ironlake_crtc_dpms(struct drm_crtc *crtc, int mode)
2488{
2489 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2490 int pipe = intel_crtc->pipe;
2491 int plane = intel_crtc->plane;
2492
Zhenyu Wang2c072452009-06-05 15:38:42 +08002493 /* XXX: When our outputs are all unaware of DPMS modes other than off
2494 * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
2495 */
2496 switch (mode) {
2497 case DRM_MODE_DPMS_ON:
2498 case DRM_MODE_DPMS_STANDBY:
2499 case DRM_MODE_DPMS_SUSPEND:
Chris Wilson868dc582010-08-07 11:01:31 +01002500 DRM_DEBUG_KMS("crtc %d/%d dpms on\n", pipe, plane);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002501 ironlake_crtc_enable(crtc);
Chris Wilson868dc582010-08-07 11:01:31 +01002502 break;
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08002503
Zhenyu Wang2c072452009-06-05 15:38:42 +08002504 case DRM_MODE_DPMS_OFF:
Chris Wilson868dc582010-08-07 11:01:31 +01002505 DRM_DEBUG_KMS("crtc %d/%d dpms off\n", pipe, plane);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002506 ironlake_crtc_disable(crtc);
Zhenyu Wang2c072452009-06-05 15:38:42 +08002507 break;
2508 }
2509}
2510
Daniel Vetter02e792f2009-09-15 22:57:34 +02002511static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
2512{
Daniel Vetter02e792f2009-09-15 22:57:34 +02002513 if (!enable && intel_crtc->overlay) {
Chris Wilson23f09ce2010-08-12 13:53:37 +01002514 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02002515
Chris Wilson23f09ce2010-08-12 13:53:37 +01002516 mutex_lock(&dev->struct_mutex);
2517 (void) intel_overlay_switch_off(intel_crtc->overlay, false);
2518 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02002519 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02002520
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01002521 /* Let userspace switch the overlay on again. In most cases userspace
2522 * has to recompute where to put it anyway.
2523 */
Daniel Vetter02e792f2009-09-15 22:57:34 +02002524}
2525
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002526static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08002527{
2528 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002529 struct drm_i915_private *dev_priv = dev->dev_private;
2530 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2531 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07002532 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002533 u32 reg, temp;
Jesse Barnes79e53942008-11-07 14:24:08 -08002534
Chris Wilsonf7abfe82010-09-13 14:19:16 +01002535 if (intel_crtc->active)
2536 return;
2537
2538 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01002539 intel_update_watermarks(dev);
2540
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002541 /* Enable the DPLL */
Chris Wilson5eddb702010-09-11 13:48:45 +01002542 reg = DPLL(pipe);
2543 temp = I915_READ(reg);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002544 if ((temp & DPLL_VCO_ENABLE) == 0) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002545 I915_WRITE(reg, temp);
2546
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002547 /* Wait for the clocks to stabilize. */
Chris Wilson5eddb702010-09-11 13:48:45 +01002548 POSTING_READ(reg);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002549 udelay(150);
Chris Wilson5eddb702010-09-11 13:48:45 +01002550
2551 I915_WRITE(reg, temp | DPLL_VCO_ENABLE);
2552
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002553 /* Wait for the clocks to stabilize. */
Chris Wilson5eddb702010-09-11 13:48:45 +01002554 POSTING_READ(reg);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002555 udelay(150);
Chris Wilson5eddb702010-09-11 13:48:45 +01002556
2557 I915_WRITE(reg, temp | DPLL_VCO_ENABLE);
2558
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002559 /* Wait for the clocks to stabilize. */
Chris Wilson5eddb702010-09-11 13:48:45 +01002560 POSTING_READ(reg);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002561 udelay(150);
2562 }
2563
Jesse Barnesb24e7172011-01-04 15:09:30 -08002564 intel_enable_pipe(dev_priv, pipe);
2565 intel_enable_plane(dev_priv, plane, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002566
2567 intel_crtc_load_lut(crtc);
Chris Wilsonbed4a672010-09-11 10:47:47 +01002568 intel_update_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002569
2570 /* Give the overlay scaler a chance to enable if it's on this pipe */
2571 intel_crtc_dpms_overlay(intel_crtc, true);
Chris Wilson6b383a72010-09-13 13:54:26 +01002572 intel_crtc_update_cursor(crtc, true);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002573}
2574
2575static void i9xx_crtc_disable(struct drm_crtc *crtc)
2576{
2577 struct drm_device *dev = crtc->dev;
2578 struct drm_i915_private *dev_priv = dev->dev_private;
2579 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2580 int pipe = intel_crtc->pipe;
2581 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002582 u32 reg, temp;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002583
Chris Wilsonf7abfe82010-09-13 14:19:16 +01002584 if (!intel_crtc->active)
2585 return;
2586
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002587 /* Give the overlay scaler a chance to disable if it's on this pipe */
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002588 intel_crtc_wait_for_pending_flips(crtc);
2589 drm_vblank_off(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002590 intel_crtc_dpms_overlay(intel_crtc, false);
Chris Wilson6b383a72010-09-13 13:54:26 +01002591 intel_crtc_update_cursor(crtc, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002592
2593 if (dev_priv->cfb_plane == plane &&
2594 dev_priv->display.disable_fbc)
2595 dev_priv->display.disable_fbc(dev);
2596
Jesse Barnesb24e7172011-01-04 15:09:30 -08002597 intel_disable_plane(dev_priv, plane, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002598
2599 /* Don't disable pipe A or pipe A PLLs if needed */
Chris Wilson5eddb702010-09-11 13:48:45 +01002600 if (pipe == 0 && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
Chris Wilson6b383a72010-09-13 13:54:26 +01002601 goto done;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002602
Jesse Barnesb24e7172011-01-04 15:09:30 -08002603 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002604
Chris Wilson5eddb702010-09-11 13:48:45 +01002605 reg = DPLL(pipe);
2606 temp = I915_READ(reg);
2607 if (temp & DPLL_VCO_ENABLE) {
2608 I915_WRITE(reg, temp & ~DPLL_VCO_ENABLE);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002609
Chris Wilson5eddb702010-09-11 13:48:45 +01002610 /* Wait for the clocks to turn off. */
2611 POSTING_READ(reg);
2612 udelay(150);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002613 }
Chris Wilson6b383a72010-09-13 13:54:26 +01002614
2615done:
Chris Wilsonf7abfe82010-09-13 14:19:16 +01002616 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01002617 intel_update_fbc(dev);
2618 intel_update_watermarks(dev);
2619 intel_clear_scanline_wait(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002620}
2621
2622static void i9xx_crtc_dpms(struct drm_crtc *crtc, int mode)
2623{
Jesse Barnes79e53942008-11-07 14:24:08 -08002624 /* XXX: When our outputs are all unaware of DPMS modes other than off
2625 * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
2626 */
2627 switch (mode) {
2628 case DRM_MODE_DPMS_ON:
2629 case DRM_MODE_DPMS_STANDBY:
2630 case DRM_MODE_DPMS_SUSPEND:
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002631 i9xx_crtc_enable(crtc);
2632 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08002633 case DRM_MODE_DPMS_OFF:
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002634 i9xx_crtc_disable(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08002635 break;
2636 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08002637}
2638
2639/**
2640 * Sets the power management mode of the pipe and plane.
Zhenyu Wang2c072452009-06-05 15:38:42 +08002641 */
2642static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
2643{
2644 struct drm_device *dev = crtc->dev;
Jesse Barnese70236a2009-09-21 10:42:27 -07002645 struct drm_i915_private *dev_priv = dev->dev_private;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002646 struct drm_i915_master_private *master_priv;
2647 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2648 int pipe = intel_crtc->pipe;
2649 bool enabled;
2650
Chris Wilson032d2a02010-09-06 16:17:22 +01002651 if (intel_crtc->dpms_mode == mode)
2652 return;
2653
Chris Wilsondebcadd2010-08-07 11:01:33 +01002654 intel_crtc->dpms_mode = mode;
Chris Wilsondebcadd2010-08-07 11:01:33 +01002655
Jesse Barnese70236a2009-09-21 10:42:27 -07002656 dev_priv->display.dpms(crtc, mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08002657
2658 if (!dev->primary->master)
2659 return;
2660
2661 master_priv = dev->primary->master->driver_priv;
2662 if (!master_priv->sarea_priv)
2663 return;
2664
2665 enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
2666
2667 switch (pipe) {
2668 case 0:
2669 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
2670 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
2671 break;
2672 case 1:
2673 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
2674 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
2675 break;
2676 default:
2677 DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
2678 break;
2679 }
Jesse Barnes79e53942008-11-07 14:24:08 -08002680}
2681
Chris Wilsoncdd59982010-09-08 16:30:16 +01002682static void intel_crtc_disable(struct drm_crtc *crtc)
2683{
2684 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
2685 struct drm_device *dev = crtc->dev;
2686
2687 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
2688
2689 if (crtc->fb) {
2690 mutex_lock(&dev->struct_mutex);
2691 i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
2692 mutex_unlock(&dev->struct_mutex);
2693 }
2694}
2695
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07002696/* Prepare for a mode set.
2697 *
2698 * Note we could be a lot smarter here. We need to figure out which outputs
2699 * will be enabled, which disabled (in short, how the config will changes)
2700 * and perform the minimum necessary steps to accomplish that, e.g. updating
2701 * watermarks, FBC configuration, making sure PLLs are programmed correctly,
2702 * panel fitting is in the proper state, etc.
2703 */
2704static void i9xx_crtc_prepare(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08002705{
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07002706 i9xx_crtc_disable(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08002707}
2708
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07002709static void i9xx_crtc_commit(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08002710{
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07002711 i9xx_crtc_enable(crtc);
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07002712}
2713
2714static void ironlake_crtc_prepare(struct drm_crtc *crtc)
2715{
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07002716 ironlake_crtc_disable(crtc);
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07002717}
2718
2719static void ironlake_crtc_commit(struct drm_crtc *crtc)
2720{
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07002721 ironlake_crtc_enable(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08002722}
2723
2724void intel_encoder_prepare (struct drm_encoder *encoder)
2725{
2726 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
2727 /* lvds has its own version of prepare see intel_lvds_prepare */
2728 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
2729}
2730
2731void intel_encoder_commit (struct drm_encoder *encoder)
2732{
2733 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
2734 /* lvds has its own version of commit see intel_lvds_commit */
2735 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
2736}
2737
Chris Wilsonea5b2132010-08-04 13:50:23 +01002738void intel_encoder_destroy(struct drm_encoder *encoder)
2739{
Chris Wilson4ef69c72010-09-09 15:14:28 +01002740 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01002741
Chris Wilsonea5b2132010-08-04 13:50:23 +01002742 drm_encoder_cleanup(encoder);
2743 kfree(intel_encoder);
2744}
2745
Jesse Barnes79e53942008-11-07 14:24:08 -08002746static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
2747 struct drm_display_mode *mode,
2748 struct drm_display_mode *adjusted_mode)
2749{
Zhenyu Wang2c072452009-06-05 15:38:42 +08002750 struct drm_device *dev = crtc->dev;
Chris Wilson89749352010-09-12 18:25:19 +01002751
Eric Anholtbad720f2009-10-22 16:11:14 -07002752 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08002753 /* FDI link clock is fixed at 2.7G */
Jesse Barnes2377b742010-07-07 14:06:43 -07002754 if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
2755 return false;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002756 }
Chris Wilson89749352010-09-12 18:25:19 +01002757
2758 /* XXX some encoders set the crtcinfo, others don't.
2759 * Obviously we need some form of conflict resolution here...
2760 */
2761 if (adjusted_mode->crtc_htotal == 0)
2762 drm_mode_set_crtcinfo(adjusted_mode, 0);
2763
Jesse Barnes79e53942008-11-07 14:24:08 -08002764 return true;
2765}
2766
Jesse Barnese70236a2009-09-21 10:42:27 -07002767static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08002768{
Jesse Barnese70236a2009-09-21 10:42:27 -07002769 return 400000;
2770}
Jesse Barnes79e53942008-11-07 14:24:08 -08002771
Jesse Barnese70236a2009-09-21 10:42:27 -07002772static int i915_get_display_clock_speed(struct drm_device *dev)
2773{
2774 return 333000;
2775}
Jesse Barnes79e53942008-11-07 14:24:08 -08002776
Jesse Barnese70236a2009-09-21 10:42:27 -07002777static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
2778{
2779 return 200000;
2780}
Jesse Barnes79e53942008-11-07 14:24:08 -08002781
Jesse Barnese70236a2009-09-21 10:42:27 -07002782static int i915gm_get_display_clock_speed(struct drm_device *dev)
2783{
2784 u16 gcfgc = 0;
2785
2786 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
2787
2788 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08002789 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07002790 else {
2791 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
2792 case GC_DISPLAY_CLOCK_333_MHZ:
2793 return 333000;
2794 default:
2795 case GC_DISPLAY_CLOCK_190_200_MHZ:
2796 return 190000;
2797 }
2798 }
2799}
Jesse Barnes79e53942008-11-07 14:24:08 -08002800
Jesse Barnese70236a2009-09-21 10:42:27 -07002801static int i865_get_display_clock_speed(struct drm_device *dev)
2802{
2803 return 266000;
2804}
2805
2806static int i855_get_display_clock_speed(struct drm_device *dev)
2807{
2808 u16 hpllcc = 0;
2809 /* Assume that the hardware is in the high speed state. This
2810 * should be the default.
2811 */
2812 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
2813 case GC_CLOCK_133_200:
2814 case GC_CLOCK_100_200:
2815 return 200000;
2816 case GC_CLOCK_166_250:
2817 return 250000;
2818 case GC_CLOCK_100_133:
2819 return 133000;
2820 }
2821
2822 /* Shouldn't happen */
2823 return 0;
2824}
2825
2826static int i830_get_display_clock_speed(struct drm_device *dev)
2827{
2828 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08002829}
2830
Zhenyu Wang2c072452009-06-05 15:38:42 +08002831struct fdi_m_n {
2832 u32 tu;
2833 u32 gmch_m;
2834 u32 gmch_n;
2835 u32 link_m;
2836 u32 link_n;
2837};
2838
2839static void
2840fdi_reduce_ratio(u32 *num, u32 *den)
2841{
2842 while (*num > 0xffffff || *den > 0xffffff) {
2843 *num >>= 1;
2844 *den >>= 1;
2845 }
2846}
2847
Zhenyu Wang2c072452009-06-05 15:38:42 +08002848static void
Adam Jacksonf2b115e2009-12-03 17:14:42 -05002849ironlake_compute_m_n(int bits_per_pixel, int nlanes, int pixel_clock,
2850 int link_clock, struct fdi_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08002851{
Zhenyu Wang2c072452009-06-05 15:38:42 +08002852 m_n->tu = 64; /* default size */
2853
Chris Wilson22ed1112010-12-04 01:01:29 +00002854 /* BUG_ON(pixel_clock > INT_MAX / 36); */
2855 m_n->gmch_m = bits_per_pixel * pixel_clock;
2856 m_n->gmch_n = link_clock * nlanes * 8;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002857 fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
2858
Chris Wilson22ed1112010-12-04 01:01:29 +00002859 m_n->link_m = pixel_clock;
2860 m_n->link_n = link_clock;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002861 fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
2862}
2863
2864
Shaohua Li7662c8b2009-06-26 11:23:55 +08002865struct intel_watermark_params {
2866 unsigned long fifo_size;
2867 unsigned long max_wm;
2868 unsigned long default_wm;
2869 unsigned long guard_size;
2870 unsigned long cacheline_size;
2871};
2872
Adam Jacksonf2b115e2009-12-03 17:14:42 -05002873/* Pineview has different values for various configs */
2874static struct intel_watermark_params pineview_display_wm = {
2875 PINEVIEW_DISPLAY_FIFO,
2876 PINEVIEW_MAX_WM,
2877 PINEVIEW_DFT_WM,
2878 PINEVIEW_GUARD_WM,
2879 PINEVIEW_FIFO_LINE_SIZE
Shaohua Li7662c8b2009-06-26 11:23:55 +08002880};
Adam Jacksonf2b115e2009-12-03 17:14:42 -05002881static struct intel_watermark_params pineview_display_hplloff_wm = {
2882 PINEVIEW_DISPLAY_FIFO,
2883 PINEVIEW_MAX_WM,
2884 PINEVIEW_DFT_HPLLOFF_WM,
2885 PINEVIEW_GUARD_WM,
2886 PINEVIEW_FIFO_LINE_SIZE
Shaohua Li7662c8b2009-06-26 11:23:55 +08002887};
Adam Jacksonf2b115e2009-12-03 17:14:42 -05002888static struct intel_watermark_params pineview_cursor_wm = {
2889 PINEVIEW_CURSOR_FIFO,
2890 PINEVIEW_CURSOR_MAX_WM,
2891 PINEVIEW_CURSOR_DFT_WM,
2892 PINEVIEW_CURSOR_GUARD_WM,
2893 PINEVIEW_FIFO_LINE_SIZE,
Shaohua Li7662c8b2009-06-26 11:23:55 +08002894};
Adam Jacksonf2b115e2009-12-03 17:14:42 -05002895static struct intel_watermark_params pineview_cursor_hplloff_wm = {
2896 PINEVIEW_CURSOR_FIFO,
2897 PINEVIEW_CURSOR_MAX_WM,
2898 PINEVIEW_CURSOR_DFT_WM,
2899 PINEVIEW_CURSOR_GUARD_WM,
2900 PINEVIEW_FIFO_LINE_SIZE
Shaohua Li7662c8b2009-06-26 11:23:55 +08002901};
Jesse Barnes0e442c62009-10-19 10:09:33 +09002902static struct intel_watermark_params g4x_wm_info = {
2903 G4X_FIFO_SIZE,
2904 G4X_MAX_WM,
2905 G4X_MAX_WM,
2906 2,
2907 G4X_FIFO_LINE_SIZE,
2908};
Zhao Yakui4fe5e612010-06-12 14:32:25 +08002909static struct intel_watermark_params g4x_cursor_wm_info = {
2910 I965_CURSOR_FIFO,
2911 I965_CURSOR_MAX_WM,
2912 I965_CURSOR_DFT_WM,
2913 2,
2914 G4X_FIFO_LINE_SIZE,
2915};
2916static struct intel_watermark_params i965_cursor_wm_info = {
2917 I965_CURSOR_FIFO,
2918 I965_CURSOR_MAX_WM,
2919 I965_CURSOR_DFT_WM,
2920 2,
2921 I915_FIFO_LINE_SIZE,
2922};
Shaohua Li7662c8b2009-06-26 11:23:55 +08002923static struct intel_watermark_params i945_wm_info = {
Shaohua Li7662c8b2009-06-26 11:23:55 +08002924 I945_FIFO_SIZE,
2925 I915_MAX_WM,
2926 1,
Jesse Barnesdff33cf2009-07-14 10:15:56 -07002927 2,
2928 I915_FIFO_LINE_SIZE
2929};
2930static struct intel_watermark_params i915_wm_info = {
2931 I915_FIFO_SIZE,
2932 I915_MAX_WM,
2933 1,
2934 2,
Shaohua Li7662c8b2009-06-26 11:23:55 +08002935 I915_FIFO_LINE_SIZE
2936};
2937static struct intel_watermark_params i855_wm_info = {
2938 I855GM_FIFO_SIZE,
2939 I915_MAX_WM,
2940 1,
Jesse Barnesdff33cf2009-07-14 10:15:56 -07002941 2,
Shaohua Li7662c8b2009-06-26 11:23:55 +08002942 I830_FIFO_LINE_SIZE
2943};
2944static struct intel_watermark_params i830_wm_info = {
2945 I830_FIFO_SIZE,
2946 I915_MAX_WM,
2947 1,
Jesse Barnesdff33cf2009-07-14 10:15:56 -07002948 2,
Shaohua Li7662c8b2009-06-26 11:23:55 +08002949 I830_FIFO_LINE_SIZE
2950};
2951
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08002952static struct intel_watermark_params ironlake_display_wm_info = {
2953 ILK_DISPLAY_FIFO,
2954 ILK_DISPLAY_MAXWM,
2955 ILK_DISPLAY_DFTWM,
2956 2,
2957 ILK_FIFO_LINE_SIZE
2958};
2959
Zhao Yakuic936f442010-06-12 14:32:26 +08002960static struct intel_watermark_params ironlake_cursor_wm_info = {
2961 ILK_CURSOR_FIFO,
2962 ILK_CURSOR_MAXWM,
2963 ILK_CURSOR_DFTWM,
2964 2,
2965 ILK_FIFO_LINE_SIZE
2966};
2967
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08002968static struct intel_watermark_params ironlake_display_srwm_info = {
2969 ILK_DISPLAY_SR_FIFO,
2970 ILK_DISPLAY_MAX_SRWM,
2971 ILK_DISPLAY_DFT_SRWM,
2972 2,
2973 ILK_FIFO_LINE_SIZE
2974};
2975
2976static struct intel_watermark_params ironlake_cursor_srwm_info = {
2977 ILK_CURSOR_SR_FIFO,
2978 ILK_CURSOR_MAX_SRWM,
2979 ILK_CURSOR_DFT_SRWM,
2980 2,
2981 ILK_FIFO_LINE_SIZE
2982};
2983
Yuanhan Liu13982612010-12-15 15:42:31 +08002984static struct intel_watermark_params sandybridge_display_wm_info = {
2985 SNB_DISPLAY_FIFO,
2986 SNB_DISPLAY_MAXWM,
2987 SNB_DISPLAY_DFTWM,
2988 2,
2989 SNB_FIFO_LINE_SIZE
2990};
2991
2992static struct intel_watermark_params sandybridge_cursor_wm_info = {
2993 SNB_CURSOR_FIFO,
2994 SNB_CURSOR_MAXWM,
2995 SNB_CURSOR_DFTWM,
2996 2,
2997 SNB_FIFO_LINE_SIZE
2998};
2999
3000static struct intel_watermark_params sandybridge_display_srwm_info = {
3001 SNB_DISPLAY_SR_FIFO,
3002 SNB_DISPLAY_MAX_SRWM,
3003 SNB_DISPLAY_DFT_SRWM,
3004 2,
3005 SNB_FIFO_LINE_SIZE
3006};
3007
3008static struct intel_watermark_params sandybridge_cursor_srwm_info = {
3009 SNB_CURSOR_SR_FIFO,
3010 SNB_CURSOR_MAX_SRWM,
3011 SNB_CURSOR_DFT_SRWM,
3012 2,
3013 SNB_FIFO_LINE_SIZE
3014};
3015
3016
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003017/**
3018 * intel_calculate_wm - calculate watermark level
3019 * @clock_in_khz: pixel clock
3020 * @wm: chip FIFO params
3021 * @pixel_size: display pixel size
3022 * @latency_ns: memory latency for the platform
3023 *
3024 * Calculate the watermark level (the level at which the display plane will
3025 * start fetching from memory again). Each chip has a different display
3026 * FIFO size and allocation, so the caller needs to figure that out and pass
3027 * in the correct intel_watermark_params structure.
3028 *
3029 * As the pixel clock runs, the FIFO will be drained at a rate that depends
3030 * on the pixel size. When it reaches the watermark level, it'll start
3031 * fetching FIFO line sized based chunks from memory until the FIFO fills
3032 * past the watermark point. If the FIFO drains completely, a FIFO underrun
3033 * will occur, and a display engine hang could result.
3034 */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003035static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
3036 struct intel_watermark_params *wm,
3037 int pixel_size,
3038 unsigned long latency_ns)
3039{
Jesse Barnes390c4dd2009-07-16 13:01:01 -07003040 long entries_required, wm_size;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003041
Jesse Barnesd6604672009-09-11 12:25:56 -07003042 /*
3043 * Note: we need to make sure we don't overflow for various clock &
3044 * latency values.
3045 * clocks go from a few thousand to several hundred thousand.
3046 * latency is usually a few thousand
3047 */
3048 entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
3049 1000;
Chris Wilson8de9b312010-07-19 19:59:52 +01003050 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003051
Zhao Yakui28c97732009-10-09 11:39:41 +08003052 DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries_required);
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003053
3054 wm_size = wm->fifo_size - (entries_required + wm->guard_size);
3055
Zhao Yakui28c97732009-10-09 11:39:41 +08003056 DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003057
Jesse Barnes390c4dd2009-07-16 13:01:01 -07003058 /* Don't promote wm_size to unsigned... */
3059 if (wm_size > (long)wm->max_wm)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003060 wm_size = wm->max_wm;
Chris Wilsonc3add4b2010-09-08 09:14:08 +01003061 if (wm_size <= 0)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003062 wm_size = wm->default_wm;
3063 return wm_size;
3064}
3065
3066struct cxsr_latency {
3067 int is_desktop;
Li Peng95534262010-05-18 18:58:44 +08003068 int is_ddr3;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003069 unsigned long fsb_freq;
3070 unsigned long mem_freq;
3071 unsigned long display_sr;
3072 unsigned long display_hpll_disable;
3073 unsigned long cursor_sr;
3074 unsigned long cursor_hpll_disable;
3075};
3076
Chris Wilson403c89f2010-08-04 15:25:31 +01003077static const struct cxsr_latency cxsr_latency_table[] = {
Li Peng95534262010-05-18 18:58:44 +08003078 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
3079 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
3080 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
3081 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
3082 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003083
Li Peng95534262010-05-18 18:58:44 +08003084 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
3085 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
3086 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
3087 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
3088 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003089
Li Peng95534262010-05-18 18:58:44 +08003090 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
3091 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
3092 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
3093 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
3094 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003095
Li Peng95534262010-05-18 18:58:44 +08003096 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
3097 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
3098 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
3099 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
3100 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003101
Li Peng95534262010-05-18 18:58:44 +08003102 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
3103 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
3104 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
3105 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
3106 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003107
Li Peng95534262010-05-18 18:58:44 +08003108 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
3109 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
3110 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
3111 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
3112 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003113};
3114
Chris Wilson403c89f2010-08-04 15:25:31 +01003115static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
3116 int is_ddr3,
3117 int fsb,
3118 int mem)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003119{
Chris Wilson403c89f2010-08-04 15:25:31 +01003120 const struct cxsr_latency *latency;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003121 int i;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003122
3123 if (fsb == 0 || mem == 0)
3124 return NULL;
3125
3126 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
3127 latency = &cxsr_latency_table[i];
3128 if (is_desktop == latency->is_desktop &&
Li Peng95534262010-05-18 18:58:44 +08003129 is_ddr3 == latency->is_ddr3 &&
Jaswinder Singh Rajputdecbbcd2009-09-12 23:15:07 +05303130 fsb == latency->fsb_freq && mem == latency->mem_freq)
3131 return latency;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003132 }
Jaswinder Singh Rajputdecbbcd2009-09-12 23:15:07 +05303133
Zhao Yakui28c97732009-10-09 11:39:41 +08003134 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Jaswinder Singh Rajputdecbbcd2009-09-12 23:15:07 +05303135
3136 return NULL;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003137}
3138
Adam Jacksonf2b115e2009-12-03 17:14:42 -05003139static void pineview_disable_cxsr(struct drm_device *dev)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003140{
3141 struct drm_i915_private *dev_priv = dev->dev_private;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003142
3143 /* deactivate cxsr */
Chris Wilson3e33d942010-08-04 11:17:25 +01003144 I915_WRITE(DSPFW3, I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003145}
3146
Jesse Barnesbcc24fb2009-08-31 10:24:31 -07003147/*
3148 * Latency for FIFO fetches is dependent on several factors:
3149 * - memory configuration (speed, channels)
3150 * - chipset
3151 * - current MCH state
3152 * It can be fairly high in some situations, so here we assume a fairly
3153 * pessimal value. It's a tradeoff between extra memory fetches (if we
3154 * set this value too high, the FIFO will fetch frequently to stay full)
3155 * and power consumption (set it too low to save power and we might see
3156 * FIFO underruns and display "flicker").
3157 *
3158 * A value of 5us seems to be a good balance; safe for very low end
3159 * platforms but not overly aggressive on lower latency configs.
3160 */
Tobias Klauser69e302a2009-12-23 14:14:34 +01003161static const int latency_ns = 5000;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003162
Jesse Barnese70236a2009-09-21 10:42:27 -07003163static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003164{
3165 struct drm_i915_private *dev_priv = dev->dev_private;
3166 uint32_t dsparb = I915_READ(DSPARB);
3167 int size;
3168
Chris Wilson8de9b312010-07-19 19:59:52 +01003169 size = dsparb & 0x7f;
3170 if (plane)
3171 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003172
Zhao Yakui28c97732009-10-09 11:39:41 +08003173 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
Chris Wilson5eddb702010-09-11 13:48:45 +01003174 plane ? "B" : "A", size);
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003175
3176 return size;
3177}
Shaohua Li7662c8b2009-06-26 11:23:55 +08003178
Jesse Barnese70236a2009-09-21 10:42:27 -07003179static int i85x_get_fifo_size(struct drm_device *dev, int plane)
3180{
3181 struct drm_i915_private *dev_priv = dev->dev_private;
3182 uint32_t dsparb = I915_READ(DSPARB);
3183 int size;
3184
Chris Wilson8de9b312010-07-19 19:59:52 +01003185 size = dsparb & 0x1ff;
3186 if (plane)
3187 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
Jesse Barnese70236a2009-09-21 10:42:27 -07003188 size >>= 1; /* Convert to cachelines */
3189
Zhao Yakui28c97732009-10-09 11:39:41 +08003190 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
Chris Wilson5eddb702010-09-11 13:48:45 +01003191 plane ? "B" : "A", size);
Jesse Barnese70236a2009-09-21 10:42:27 -07003192
3193 return size;
3194}
3195
3196static int i845_get_fifo_size(struct drm_device *dev, int plane)
3197{
3198 struct drm_i915_private *dev_priv = dev->dev_private;
3199 uint32_t dsparb = I915_READ(DSPARB);
3200 int size;
3201
3202 size = dsparb & 0x7f;
3203 size >>= 2; /* Convert to cachelines */
3204
Zhao Yakui28c97732009-10-09 11:39:41 +08003205 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
Chris Wilson5eddb702010-09-11 13:48:45 +01003206 plane ? "B" : "A",
3207 size);
Jesse Barnese70236a2009-09-21 10:42:27 -07003208
3209 return size;
3210}
3211
3212static int i830_get_fifo_size(struct drm_device *dev, int plane)
3213{
3214 struct drm_i915_private *dev_priv = dev->dev_private;
3215 uint32_t dsparb = I915_READ(DSPARB);
3216 int size;
3217
3218 size = dsparb & 0x7f;
3219 size >>= 1; /* Convert to cachelines */
3220
Zhao Yakui28c97732009-10-09 11:39:41 +08003221 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
Chris Wilson5eddb702010-09-11 13:48:45 +01003222 plane ? "B" : "A", size);
Jesse Barnese70236a2009-09-21 10:42:27 -07003223
3224 return size;
3225}
3226
Zhao Yakuid4294342010-03-22 22:45:36 +08003227static void pineview_update_wm(struct drm_device *dev, int planea_clock,
Chris Wilson5eddb702010-09-11 13:48:45 +01003228 int planeb_clock, int sr_hdisplay, int unused,
3229 int pixel_size)
Zhao Yakuid4294342010-03-22 22:45:36 +08003230{
3231 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson403c89f2010-08-04 15:25:31 +01003232 const struct cxsr_latency *latency;
Zhao Yakuid4294342010-03-22 22:45:36 +08003233 u32 reg;
3234 unsigned long wm;
Zhao Yakuid4294342010-03-22 22:45:36 +08003235 int sr_clock;
3236
Chris Wilson403c89f2010-08-04 15:25:31 +01003237 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
Li Peng95534262010-05-18 18:58:44 +08003238 dev_priv->fsb_freq, dev_priv->mem_freq);
Zhao Yakuid4294342010-03-22 22:45:36 +08003239 if (!latency) {
3240 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
3241 pineview_disable_cxsr(dev);
3242 return;
3243 }
3244
3245 if (!planea_clock || !planeb_clock) {
3246 sr_clock = planea_clock ? planea_clock : planeb_clock;
3247
3248 /* Display SR */
3249 wm = intel_calculate_wm(sr_clock, &pineview_display_wm,
3250 pixel_size, latency->display_sr);
3251 reg = I915_READ(DSPFW1);
3252 reg &= ~DSPFW_SR_MASK;
3253 reg |= wm << DSPFW_SR_SHIFT;
3254 I915_WRITE(DSPFW1, reg);
3255 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
3256
3257 /* cursor SR */
3258 wm = intel_calculate_wm(sr_clock, &pineview_cursor_wm,
3259 pixel_size, latency->cursor_sr);
3260 reg = I915_READ(DSPFW3);
3261 reg &= ~DSPFW_CURSOR_SR_MASK;
3262 reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
3263 I915_WRITE(DSPFW3, reg);
3264
3265 /* Display HPLL off SR */
3266 wm = intel_calculate_wm(sr_clock, &pineview_display_hplloff_wm,
3267 pixel_size, latency->display_hpll_disable);
3268 reg = I915_READ(DSPFW3);
3269 reg &= ~DSPFW_HPLL_SR_MASK;
3270 reg |= wm & DSPFW_HPLL_SR_MASK;
3271 I915_WRITE(DSPFW3, reg);
3272
3273 /* cursor HPLL off SR */
3274 wm = intel_calculate_wm(sr_clock, &pineview_cursor_hplloff_wm,
3275 pixel_size, latency->cursor_hpll_disable);
3276 reg = I915_READ(DSPFW3);
3277 reg &= ~DSPFW_HPLL_CURSOR_MASK;
3278 reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
3279 I915_WRITE(DSPFW3, reg);
3280 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
3281
3282 /* activate cxsr */
Chris Wilson3e33d942010-08-04 11:17:25 +01003283 I915_WRITE(DSPFW3,
3284 I915_READ(DSPFW3) | PINEVIEW_SELF_REFRESH_EN);
Zhao Yakuid4294342010-03-22 22:45:36 +08003285 DRM_DEBUG_KMS("Self-refresh is enabled\n");
3286 } else {
3287 pineview_disable_cxsr(dev);
3288 DRM_DEBUG_KMS("Self-refresh is disabled\n");
3289 }
3290}
3291
Jesse Barnes0e442c62009-10-19 10:09:33 +09003292static void g4x_update_wm(struct drm_device *dev, int planea_clock,
Zhao Yakuifa143212010-06-12 14:32:23 +08003293 int planeb_clock, int sr_hdisplay, int sr_htotal,
3294 int pixel_size)
Jesse Barnes652c3932009-08-17 13:31:43 -07003295{
3296 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e442c62009-10-19 10:09:33 +09003297 int total_size, cacheline_size;
3298 int planea_wm, planeb_wm, cursora_wm, cursorb_wm, cursor_sr;
3299 struct intel_watermark_params planea_params, planeb_params;
3300 unsigned long line_time_us;
3301 int sr_clock, sr_entries = 0, entries_required;
Jesse Barnes652c3932009-08-17 13:31:43 -07003302
Jesse Barnes0e442c62009-10-19 10:09:33 +09003303 /* Create copies of the base settings for each pipe */
3304 planea_params = planeb_params = g4x_wm_info;
3305
3306 /* Grab a couple of global values before we overwrite them */
3307 total_size = planea_params.fifo_size;
3308 cacheline_size = planea_params.cacheline_size;
3309
3310 /*
3311 * Note: we need to make sure we don't overflow for various clock &
3312 * latency values.
3313 * clocks go from a few thousand to several hundred thousand.
3314 * latency is usually a few thousand
3315 */
3316 entries_required = ((planea_clock / 1000) * pixel_size * latency_ns) /
3317 1000;
Chris Wilson8de9b312010-07-19 19:59:52 +01003318 entries_required = DIV_ROUND_UP(entries_required, G4X_FIFO_LINE_SIZE);
Jesse Barnes0e442c62009-10-19 10:09:33 +09003319 planea_wm = entries_required + planea_params.guard_size;
3320
3321 entries_required = ((planeb_clock / 1000) * pixel_size * latency_ns) /
3322 1000;
Chris Wilson8de9b312010-07-19 19:59:52 +01003323 entries_required = DIV_ROUND_UP(entries_required, G4X_FIFO_LINE_SIZE);
Jesse Barnes0e442c62009-10-19 10:09:33 +09003324 planeb_wm = entries_required + planeb_params.guard_size;
3325
3326 cursora_wm = cursorb_wm = 16;
3327 cursor_sr = 32;
3328
3329 DRM_DEBUG("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
3330
3331 /* Calc sr entries for one plane configs */
3332 if (sr_hdisplay && (!planea_clock || !planeb_clock)) {
3333 /* self-refresh has much higher latency */
Tobias Klauser69e302a2009-12-23 14:14:34 +01003334 static const int sr_latency_ns = 12000;
Jesse Barnes0e442c62009-10-19 10:09:33 +09003335
3336 sr_clock = planea_clock ? planea_clock : planeb_clock;
Zhao Yakuifa143212010-06-12 14:32:23 +08003337 line_time_us = ((sr_htotal * 1000) / sr_clock);
Jesse Barnes0e442c62009-10-19 10:09:33 +09003338
3339 /* Use ns/us then divide to preserve precision */
Zhao Yakuifa143212010-06-12 14:32:23 +08003340 sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Chris Wilson5eddb702010-09-11 13:48:45 +01003341 pixel_size * sr_hdisplay;
Chris Wilson8de9b312010-07-19 19:59:52 +01003342 sr_entries = DIV_ROUND_UP(sr_entries, cacheline_size);
Zhao Yakui4fe5e612010-06-12 14:32:25 +08003343
3344 entries_required = (((sr_latency_ns / line_time_us) +
3345 1000) / 1000) * pixel_size * 64;
Chris Wilson8de9b312010-07-19 19:59:52 +01003346 entries_required = DIV_ROUND_UP(entries_required,
Chris Wilson5eddb702010-09-11 13:48:45 +01003347 g4x_cursor_wm_info.cacheline_size);
Zhao Yakui4fe5e612010-06-12 14:32:25 +08003348 cursor_sr = entries_required + g4x_cursor_wm_info.guard_size;
3349
3350 if (cursor_sr > g4x_cursor_wm_info.max_wm)
3351 cursor_sr = g4x_cursor_wm_info.max_wm;
3352 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
3353 "cursor %d\n", sr_entries, cursor_sr);
3354
Jesse Barnes0e442c62009-10-19 10:09:33 +09003355 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
David John33c5fd12010-01-27 15:19:08 +05303356 } else {
3357 /* Turn off self refresh if both pipes are enabled */
3358 I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
Chris Wilson5eddb702010-09-11 13:48:45 +01003359 & ~FW_BLC_SELF_EN);
Jesse Barnes0e442c62009-10-19 10:09:33 +09003360 }
3361
3362 DRM_DEBUG("Setting FIFO watermarks - A: %d, B: %d, SR %d\n",
3363 planea_wm, planeb_wm, sr_entries);
3364
3365 planea_wm &= 0x3f;
3366 planeb_wm &= 0x3f;
3367
3368 I915_WRITE(DSPFW1, (sr_entries << DSPFW_SR_SHIFT) |
3369 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
3370 (planeb_wm << DSPFW_PLANEB_SHIFT) | planea_wm);
3371 I915_WRITE(DSPFW2, (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
3372 (cursora_wm << DSPFW_CURSORA_SHIFT));
3373 /* HPLL off in SR has some issues on G4x... disable it */
3374 I915_WRITE(DSPFW3, (I915_READ(DSPFW3) & ~DSPFW_HPLL_SR_EN) |
3375 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Jesse Barnes652c3932009-08-17 13:31:43 -07003376}
3377
Jesse Barnes1dc75462009-10-19 10:08:17 +09003378static void i965_update_wm(struct drm_device *dev, int planea_clock,
Zhao Yakuifa143212010-06-12 14:32:23 +08003379 int planeb_clock, int sr_hdisplay, int sr_htotal,
3380 int pixel_size)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003381{
3382 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1dc75462009-10-19 10:08:17 +09003383 unsigned long line_time_us;
3384 int sr_clock, sr_entries, srwm = 1;
Zhao Yakui4fe5e612010-06-12 14:32:25 +08003385 int cursor_sr = 16;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003386
Jesse Barnes1dc75462009-10-19 10:08:17 +09003387 /* Calc sr entries for one plane configs */
3388 if (sr_hdisplay && (!planea_clock || !planeb_clock)) {
3389 /* self-refresh has much higher latency */
Tobias Klauser69e302a2009-12-23 14:14:34 +01003390 static const int sr_latency_ns = 12000;
Jesse Barnes1dc75462009-10-19 10:08:17 +09003391
3392 sr_clock = planea_clock ? planea_clock : planeb_clock;
Zhao Yakuifa143212010-06-12 14:32:23 +08003393 line_time_us = ((sr_htotal * 1000) / sr_clock);
Jesse Barnes1dc75462009-10-19 10:08:17 +09003394
3395 /* Use ns/us then divide to preserve precision */
Zhao Yakuifa143212010-06-12 14:32:23 +08003396 sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Chris Wilson5eddb702010-09-11 13:48:45 +01003397 pixel_size * sr_hdisplay;
Chris Wilson8de9b312010-07-19 19:59:52 +01003398 sr_entries = DIV_ROUND_UP(sr_entries, I915_FIFO_LINE_SIZE);
Jesse Barnes1dc75462009-10-19 10:08:17 +09003399 DRM_DEBUG("self-refresh entries: %d\n", sr_entries);
Zhao Yakui1b07e042010-06-12 14:32:24 +08003400 srwm = I965_FIFO_SIZE - sr_entries;
Jesse Barnes1dc75462009-10-19 10:08:17 +09003401 if (srwm < 0)
3402 srwm = 1;
Zhao Yakui1b07e042010-06-12 14:32:24 +08003403 srwm &= 0x1ff;
Zhao Yakui4fe5e612010-06-12 14:32:25 +08003404
3405 sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Chris Wilson5eddb702010-09-11 13:48:45 +01003406 pixel_size * 64;
Chris Wilson8de9b312010-07-19 19:59:52 +01003407 sr_entries = DIV_ROUND_UP(sr_entries,
3408 i965_cursor_wm_info.cacheline_size);
Zhao Yakui4fe5e612010-06-12 14:32:25 +08003409 cursor_sr = i965_cursor_wm_info.fifo_size -
Chris Wilson5eddb702010-09-11 13:48:45 +01003410 (sr_entries + i965_cursor_wm_info.guard_size);
Zhao Yakui4fe5e612010-06-12 14:32:25 +08003411
3412 if (cursor_sr > i965_cursor_wm_info.max_wm)
3413 cursor_sr = i965_cursor_wm_info.max_wm;
3414
3415 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
3416 "cursor %d\n", srwm, cursor_sr);
3417
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003418 if (IS_CRESTLINE(dev))
Jesse Barnesadcdbc62010-06-30 13:49:37 -07003419 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
David John33c5fd12010-01-27 15:19:08 +05303420 } else {
3421 /* Turn off self refresh if both pipes are enabled */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003422 if (IS_CRESTLINE(dev))
Jesse Barnesadcdbc62010-06-30 13:49:37 -07003423 I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
3424 & ~FW_BLC_SELF_EN);
Jesse Barnes1dc75462009-10-19 10:08:17 +09003425 }
3426
3427 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
3428 srwm);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003429
3430 /* 965 has limitations... */
Jesse Barnes1dc75462009-10-19 10:08:17 +09003431 I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) | (8 << 16) | (8 << 8) |
3432 (8 << 0));
Shaohua Li7662c8b2009-06-26 11:23:55 +08003433 I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
Zhao Yakui4fe5e612010-06-12 14:32:25 +08003434 /* update cursor SR watermark */
3435 I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Shaohua Li7662c8b2009-06-26 11:23:55 +08003436}
3437
3438static void i9xx_update_wm(struct drm_device *dev, int planea_clock,
Zhao Yakuifa143212010-06-12 14:32:23 +08003439 int planeb_clock, int sr_hdisplay, int sr_htotal,
3440 int pixel_size)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003441{
3442 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003443 uint32_t fwater_lo;
3444 uint32_t fwater_hi;
3445 int total_size, cacheline_size, cwm, srwm = 1;
3446 int planea_wm, planeb_wm;
3447 struct intel_watermark_params planea_params, planeb_params;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003448 unsigned long line_time_us;
3449 int sr_clock, sr_entries = 0;
3450
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003451 /* Create copies of the base settings for each pipe */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003452 if (IS_CRESTLINE(dev) || IS_I945GM(dev))
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003453 planea_params = planeb_params = i945_wm_info;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003454 else if (!IS_GEN2(dev))
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003455 planea_params = planeb_params = i915_wm_info;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003456 else
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003457 planea_params = planeb_params = i855_wm_info;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003458
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003459 /* Grab a couple of global values before we overwrite them */
3460 total_size = planea_params.fifo_size;
3461 cacheline_size = planea_params.cacheline_size;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003462
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003463 /* Update per-plane FIFO sizes */
Jesse Barnese70236a2009-09-21 10:42:27 -07003464 planea_params.fifo_size = dev_priv->display.get_fifo_size(dev, 0);
3465 planeb_params.fifo_size = dev_priv->display.get_fifo_size(dev, 1);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003466
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003467 planea_wm = intel_calculate_wm(planea_clock, &planea_params,
3468 pixel_size, latency_ns);
3469 planeb_wm = intel_calculate_wm(planeb_clock, &planeb_params,
3470 pixel_size, latency_ns);
Zhao Yakui28c97732009-10-09 11:39:41 +08003471 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003472
3473 /*
3474 * Overlay gets an aggressive default since video jitter is bad.
3475 */
3476 cwm = 2;
3477
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003478 /* Calc sr entries for one plane configs */
Jesse Barnes652c3932009-08-17 13:31:43 -07003479 if (HAS_FW_BLC(dev) && sr_hdisplay &&
3480 (!planea_clock || !planeb_clock)) {
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003481 /* self-refresh has much higher latency */
Tobias Klauser69e302a2009-12-23 14:14:34 +01003482 static const int sr_latency_ns = 6000;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003483
Shaohua Li7662c8b2009-06-26 11:23:55 +08003484 sr_clock = planea_clock ? planea_clock : planeb_clock;
Zhao Yakuifa143212010-06-12 14:32:23 +08003485 line_time_us = ((sr_htotal * 1000) / sr_clock);
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003486
3487 /* Use ns/us then divide to preserve precision */
Zhao Yakuifa143212010-06-12 14:32:23 +08003488 sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Chris Wilson5eddb702010-09-11 13:48:45 +01003489 pixel_size * sr_hdisplay;
Chris Wilson8de9b312010-07-19 19:59:52 +01003490 sr_entries = DIV_ROUND_UP(sr_entries, cacheline_size);
Zhao Yakui28c97732009-10-09 11:39:41 +08003491 DRM_DEBUG_KMS("self-refresh entries: %d\n", sr_entries);
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003492 srwm = total_size - sr_entries;
3493 if (srwm < 0)
3494 srwm = 1;
Li Pengee980b82010-01-27 19:01:11 +08003495
3496 if (IS_I945G(dev) || IS_I945GM(dev))
3497 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
3498 else if (IS_I915GM(dev)) {
3499 /* 915M has a smaller SRWM field */
3500 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
3501 I915_WRITE(INSTPM, I915_READ(INSTPM) | INSTPM_SELF_EN);
3502 }
David John33c5fd12010-01-27 15:19:08 +05303503 } else {
3504 /* Turn off self refresh if both pipes are enabled */
Li Pengee980b82010-01-27 19:01:11 +08003505 if (IS_I945G(dev) || IS_I945GM(dev)) {
3506 I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
3507 & ~FW_BLC_SELF_EN);
3508 } else if (IS_I915GM(dev)) {
3509 I915_WRITE(INSTPM, I915_READ(INSTPM) & ~INSTPM_SELF_EN);
3510 }
Shaohua Li7662c8b2009-06-26 11:23:55 +08003511 }
3512
Zhao Yakui28c97732009-10-09 11:39:41 +08003513 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
Chris Wilson5eddb702010-09-11 13:48:45 +01003514 planea_wm, planeb_wm, cwm, srwm);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003515
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003516 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
3517 fwater_hi = (cwm & 0x1f);
3518
3519 /* Set request length to 8 cachelines per fetch */
3520 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
3521 fwater_hi = fwater_hi | (1 << 8);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003522
3523 I915_WRITE(FW_BLC, fwater_lo);
3524 I915_WRITE(FW_BLC2, fwater_hi);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003525}
3526
Jesse Barnese70236a2009-09-21 10:42:27 -07003527static void i830_update_wm(struct drm_device *dev, int planea_clock, int unused,
Zhao Yakuifa143212010-06-12 14:32:23 +08003528 int unused2, int unused3, int pixel_size)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003529{
3530 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf3601322009-07-22 12:54:59 -07003531 uint32_t fwater_lo = I915_READ(FW_BLC) & ~0xfff;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003532 int planea_wm;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003533
Jesse Barnese70236a2009-09-21 10:42:27 -07003534 i830_wm_info.fifo_size = dev_priv->display.get_fifo_size(dev, 0);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003535
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003536 planea_wm = intel_calculate_wm(planea_clock, &i830_wm_info,
3537 pixel_size, latency_ns);
Jesse Barnesf3601322009-07-22 12:54:59 -07003538 fwater_lo |= (3<<8) | planea_wm;
3539
Zhao Yakui28c97732009-10-09 11:39:41 +08003540 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003541
3542 I915_WRITE(FW_BLC, fwater_lo);
3543}
3544
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003545#define ILK_LP0_PLANE_LATENCY 700
Zhao Yakuic936f442010-06-12 14:32:26 +08003546#define ILK_LP0_CURSOR_LATENCY 1300
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003547
Chris Wilson4ed765f2010-09-11 10:46:47 +01003548static bool ironlake_compute_wm0(struct drm_device *dev,
3549 int pipe,
Yuanhan Liu13982612010-12-15 15:42:31 +08003550 const struct intel_watermark_params *display,
Yuanhan Liua0fa62d2010-12-23 16:35:40 +08003551 int display_latency_ns,
Yuanhan Liu13982612010-12-15 15:42:31 +08003552 const struct intel_watermark_params *cursor,
Yuanhan Liua0fa62d2010-12-23 16:35:40 +08003553 int cursor_latency_ns,
Chris Wilson4ed765f2010-09-11 10:46:47 +01003554 int *plane_wm,
3555 int *cursor_wm)
3556{
3557 struct drm_crtc *crtc;
Chris Wilsondb66e372011-01-08 09:02:21 +00003558 int htotal, hdisplay, clock, pixel_size;
3559 int line_time_us, line_count;
3560 int entries, tlb_miss;
Chris Wilson4ed765f2010-09-11 10:46:47 +01003561
3562 crtc = intel_get_crtc_for_pipe(dev, pipe);
3563 if (crtc->fb == NULL || !crtc->enabled)
3564 return false;
3565
3566 htotal = crtc->mode.htotal;
3567 hdisplay = crtc->mode.hdisplay;
3568 clock = crtc->mode.clock;
3569 pixel_size = crtc->fb->bits_per_pixel / 8;
3570
3571 /* Use the small buffer method to calculate plane watermark */
Yuanhan Liua0fa62d2010-12-23 16:35:40 +08003572 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
Chris Wilsondb66e372011-01-08 09:02:21 +00003573 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
3574 if (tlb_miss > 0)
3575 entries += tlb_miss;
Yuanhan Liu13982612010-12-15 15:42:31 +08003576 entries = DIV_ROUND_UP(entries, display->cacheline_size);
3577 *plane_wm = entries + display->guard_size;
3578 if (*plane_wm > (int)display->max_wm)
3579 *plane_wm = display->max_wm;
Chris Wilson4ed765f2010-09-11 10:46:47 +01003580
3581 /* Use the large buffer method to calculate cursor watermark */
3582 line_time_us = ((htotal * 1000) / clock);
Yuanhan Liua0fa62d2010-12-23 16:35:40 +08003583 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
Chris Wilson4ed765f2010-09-11 10:46:47 +01003584 entries = line_count * 64 * pixel_size;
Chris Wilsondb66e372011-01-08 09:02:21 +00003585 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
3586 if (tlb_miss > 0)
3587 entries += tlb_miss;
Yuanhan Liu13982612010-12-15 15:42:31 +08003588 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
3589 *cursor_wm = entries + cursor->guard_size;
3590 if (*cursor_wm > (int)cursor->max_wm)
3591 *cursor_wm = (int)cursor->max_wm;
Chris Wilson4ed765f2010-09-11 10:46:47 +01003592
3593 return true;
3594}
3595
Jesse Barnesb79d4992010-12-21 13:10:23 -08003596/*
3597 * Check the wm result.
3598 *
3599 * If any calculated watermark values is larger than the maximum value that
3600 * can be programmed into the associated watermark register, that watermark
3601 * must be disabled.
3602 */
3603static bool ironlake_check_srwm(struct drm_device *dev, int level,
3604 int fbc_wm, int display_wm, int cursor_wm,
3605 const struct intel_watermark_params *display,
3606 const struct intel_watermark_params *cursor)
3607{
3608 struct drm_i915_private *dev_priv = dev->dev_private;
3609
3610 DRM_DEBUG_KMS("watermark %d: display plane %d, fbc lines %d,"
3611 " cursor %d\n", level, display_wm, fbc_wm, cursor_wm);
3612
3613 if (fbc_wm > SNB_FBC_MAX_SRWM) {
3614 DRM_DEBUG_KMS("fbc watermark(%d) is too large(%d), disabling wm%d+\n",
3615 fbc_wm, SNB_FBC_MAX_SRWM, level);
3616
3617 /* fbc has it's own way to disable FBC WM */
3618 I915_WRITE(DISP_ARB_CTL,
3619 I915_READ(DISP_ARB_CTL) | DISP_FBC_WM_DIS);
3620 return false;
3621 }
3622
3623 if (display_wm > display->max_wm) {
3624 DRM_DEBUG_KMS("display watermark(%d) is too large(%d), disabling wm%d+\n",
3625 display_wm, SNB_DISPLAY_MAX_SRWM, level);
3626 return false;
3627 }
3628
3629 if (cursor_wm > cursor->max_wm) {
3630 DRM_DEBUG_KMS("cursor watermark(%d) is too large(%d), disabling wm%d+\n",
3631 cursor_wm, SNB_CURSOR_MAX_SRWM, level);
3632 return false;
3633 }
3634
3635 if (!(fbc_wm || display_wm || cursor_wm)) {
3636 DRM_DEBUG_KMS("latency %d is 0, disabling wm%d+\n", level, level);
3637 return false;
3638 }
3639
3640 return true;
3641}
3642
3643/*
3644 * Compute watermark values of WM[1-3],
3645 */
3646static bool ironlake_compute_srwm(struct drm_device *dev, int level,
3647 int hdisplay, int htotal,
3648 int pixel_size, int clock, int latency_ns,
3649 const struct intel_watermark_params *display,
3650 const struct intel_watermark_params *cursor,
3651 int *fbc_wm, int *display_wm, int *cursor_wm)
3652{
3653
3654 unsigned long line_time_us;
3655 int line_count, line_size;
3656 int small, large;
3657 int entries;
3658
3659 if (!latency_ns) {
3660 *fbc_wm = *display_wm = *cursor_wm = 0;
3661 return false;
3662 }
3663
3664 line_time_us = (htotal * 1000) / clock;
3665 line_count = (latency_ns / line_time_us + 1000) / 1000;
3666 line_size = hdisplay * pixel_size;
3667
3668 /* Use the minimum of the small and large buffer method for primary */
3669 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
3670 large = line_count * line_size;
3671
3672 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
3673 *display_wm = entries + display->guard_size;
3674
3675 /*
3676 * Spec says:
3677 * FBC WM = ((Final Primary WM * 64) / number of bytes per line) + 2
3678 */
3679 *fbc_wm = DIV_ROUND_UP(*display_wm * 64, line_size) + 2;
3680
3681 /* calculate the self-refresh watermark for display cursor */
3682 entries = line_count * pixel_size * 64;
3683 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
3684 *cursor_wm = entries + cursor->guard_size;
3685
3686 return ironlake_check_srwm(dev, level,
3687 *fbc_wm, *display_wm, *cursor_wm,
3688 display, cursor);
3689}
3690
Chris Wilson4ed765f2010-09-11 10:46:47 +01003691static void ironlake_update_wm(struct drm_device *dev,
3692 int planea_clock, int planeb_clock,
Jesse Barnesb79d4992010-12-21 13:10:23 -08003693 int hdisplay, int htotal,
Chris Wilson4ed765f2010-09-11 10:46:47 +01003694 int pixel_size)
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003695{
3696 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesb79d4992010-12-21 13:10:23 -08003697 int fbc_wm, plane_wm, cursor_wm, enabled;
3698 int clock;
Zhao Yakuic936f442010-06-12 14:32:26 +08003699
Chris Wilson4ed765f2010-09-11 10:46:47 +01003700 enabled = 0;
Yuanhan Liu13982612010-12-15 15:42:31 +08003701 if (ironlake_compute_wm0(dev, 0,
3702 &ironlake_display_wm_info,
3703 ILK_LP0_PLANE_LATENCY,
3704 &ironlake_cursor_wm_info,
3705 ILK_LP0_CURSOR_LATENCY,
3706 &plane_wm, &cursor_wm)) {
Chris Wilson4ed765f2010-09-11 10:46:47 +01003707 I915_WRITE(WM0_PIPEA_ILK,
3708 (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
3709 DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
3710 " plane %d, " "cursor: %d\n",
3711 plane_wm, cursor_wm);
3712 enabled++;
Zhao Yakuic936f442010-06-12 14:32:26 +08003713 }
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003714
Yuanhan Liu13982612010-12-15 15:42:31 +08003715 if (ironlake_compute_wm0(dev, 1,
3716 &ironlake_display_wm_info,
3717 ILK_LP0_PLANE_LATENCY,
3718 &ironlake_cursor_wm_info,
3719 ILK_LP0_CURSOR_LATENCY,
3720 &plane_wm, &cursor_wm)) {
Chris Wilson4ed765f2010-09-11 10:46:47 +01003721 I915_WRITE(WM0_PIPEB_ILK,
3722 (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
3723 DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
3724 " plane %d, cursor: %d\n",
3725 plane_wm, cursor_wm);
3726 enabled++;
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003727 }
3728
3729 /*
3730 * Calculate and update the self-refresh watermark only when one
3731 * display plane is used.
3732 */
Jesse Barnesb79d4992010-12-21 13:10:23 -08003733 I915_WRITE(WM3_LP_ILK, 0);
3734 I915_WRITE(WM2_LP_ILK, 0);
3735 I915_WRITE(WM1_LP_ILK, 0);
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003736
Jesse Barnesb79d4992010-12-21 13:10:23 -08003737 if (enabled != 1)
3738 return;
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003739
Jesse Barnesb79d4992010-12-21 13:10:23 -08003740 clock = planea_clock ? planea_clock : planeb_clock;
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003741
Jesse Barnesb79d4992010-12-21 13:10:23 -08003742 /* WM1 */
3743 if (!ironlake_compute_srwm(dev, 1, hdisplay, htotal, pixel_size,
3744 clock, ILK_READ_WM1_LATENCY() * 500,
3745 &ironlake_display_srwm_info,
3746 &ironlake_cursor_srwm_info,
3747 &fbc_wm, &plane_wm, &cursor_wm))
3748 return;
Chris Wilson4ed765f2010-09-11 10:46:47 +01003749
Jesse Barnesb79d4992010-12-21 13:10:23 -08003750 I915_WRITE(WM1_LP_ILK,
3751 WM1_LP_SR_EN |
3752 (ILK_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
3753 (fbc_wm << WM1_LP_FBC_SHIFT) |
3754 (plane_wm << WM1_LP_SR_SHIFT) |
3755 cursor_wm);
Chris Wilson4ed765f2010-09-11 10:46:47 +01003756
Jesse Barnesb79d4992010-12-21 13:10:23 -08003757 /* WM2 */
3758 if (!ironlake_compute_srwm(dev, 2, hdisplay, htotal, pixel_size,
3759 clock, ILK_READ_WM2_LATENCY() * 500,
3760 &ironlake_display_srwm_info,
3761 &ironlake_cursor_srwm_info,
3762 &fbc_wm, &plane_wm, &cursor_wm))
3763 return;
Chris Wilson4ed765f2010-09-11 10:46:47 +01003764
Jesse Barnesb79d4992010-12-21 13:10:23 -08003765 I915_WRITE(WM2_LP_ILK,
3766 WM2_LP_EN |
3767 (ILK_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
3768 (fbc_wm << WM1_LP_FBC_SHIFT) |
3769 (plane_wm << WM1_LP_SR_SHIFT) |
3770 cursor_wm);
Yuanhan Liu13982612010-12-15 15:42:31 +08003771
3772 /*
Jesse Barnesb79d4992010-12-21 13:10:23 -08003773 * WM3 is unsupported on ILK, probably because we don't have latency
3774 * data for that power state
Yuanhan Liu13982612010-12-15 15:42:31 +08003775 */
Yuanhan Liu13982612010-12-15 15:42:31 +08003776}
3777
3778static void sandybridge_update_wm(struct drm_device *dev,
3779 int planea_clock, int planeb_clock,
3780 int hdisplay, int htotal,
3781 int pixel_size)
3782{
3783 struct drm_i915_private *dev_priv = dev->dev_private;
Yuanhan Liua0fa62d2010-12-23 16:35:40 +08003784 int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
Yuanhan Liu13982612010-12-15 15:42:31 +08003785 int fbc_wm, plane_wm, cursor_wm, enabled;
3786 int clock;
3787
3788 enabled = 0;
3789 if (ironlake_compute_wm0(dev, 0,
3790 &sandybridge_display_wm_info, latency,
3791 &sandybridge_cursor_wm_info, latency,
3792 &plane_wm, &cursor_wm)) {
3793 I915_WRITE(WM0_PIPEA_ILK,
3794 (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
3795 DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
3796 " plane %d, " "cursor: %d\n",
3797 plane_wm, cursor_wm);
3798 enabled++;
3799 }
3800
3801 if (ironlake_compute_wm0(dev, 1,
3802 &sandybridge_display_wm_info, latency,
3803 &sandybridge_cursor_wm_info, latency,
3804 &plane_wm, &cursor_wm)) {
3805 I915_WRITE(WM0_PIPEB_ILK,
3806 (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
3807 DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
3808 " plane %d, cursor: %d\n",
3809 plane_wm, cursor_wm);
3810 enabled++;
3811 }
3812
3813 /*
3814 * Calculate and update the self-refresh watermark only when one
3815 * display plane is used.
3816 *
3817 * SNB support 3 levels of watermark.
3818 *
3819 * WM1/WM2/WM2 watermarks have to be enabled in the ascending order,
3820 * and disabled in the descending order
3821 *
3822 */
3823 I915_WRITE(WM3_LP_ILK, 0);
3824 I915_WRITE(WM2_LP_ILK, 0);
3825 I915_WRITE(WM1_LP_ILK, 0);
3826
3827 if (enabled != 1)
3828 return;
3829
3830 clock = planea_clock ? planea_clock : planeb_clock;
3831
3832 /* WM1 */
Jesse Barnesb79d4992010-12-21 13:10:23 -08003833 if (!ironlake_compute_srwm(dev, 1, hdisplay, htotal, pixel_size,
3834 clock, SNB_READ_WM1_LATENCY() * 500,
3835 &sandybridge_display_srwm_info,
3836 &sandybridge_cursor_srwm_info,
3837 &fbc_wm, &plane_wm, &cursor_wm))
Yuanhan Liu13982612010-12-15 15:42:31 +08003838 return;
3839
3840 I915_WRITE(WM1_LP_ILK,
3841 WM1_LP_SR_EN |
3842 (SNB_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
3843 (fbc_wm << WM1_LP_FBC_SHIFT) |
3844 (plane_wm << WM1_LP_SR_SHIFT) |
3845 cursor_wm);
3846
3847 /* WM2 */
Jesse Barnesb79d4992010-12-21 13:10:23 -08003848 if (!ironlake_compute_srwm(dev, 2,
3849 hdisplay, htotal, pixel_size,
3850 clock, SNB_READ_WM2_LATENCY() * 500,
3851 &sandybridge_display_srwm_info,
3852 &sandybridge_cursor_srwm_info,
3853 &fbc_wm, &plane_wm, &cursor_wm))
Yuanhan Liu13982612010-12-15 15:42:31 +08003854 return;
3855
3856 I915_WRITE(WM2_LP_ILK,
3857 WM2_LP_EN |
3858 (SNB_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
3859 (fbc_wm << WM1_LP_FBC_SHIFT) |
3860 (plane_wm << WM1_LP_SR_SHIFT) |
3861 cursor_wm);
3862
3863 /* WM3 */
Jesse Barnesb79d4992010-12-21 13:10:23 -08003864 if (!ironlake_compute_srwm(dev, 3,
3865 hdisplay, htotal, pixel_size,
3866 clock, SNB_READ_WM3_LATENCY() * 500,
3867 &sandybridge_display_srwm_info,
3868 &sandybridge_cursor_srwm_info,
3869 &fbc_wm, &plane_wm, &cursor_wm))
Yuanhan Liu13982612010-12-15 15:42:31 +08003870 return;
3871
3872 I915_WRITE(WM3_LP_ILK,
3873 WM3_LP_EN |
3874 (SNB_READ_WM3_LATENCY() << WM1_LP_LATENCY_SHIFT) |
3875 (fbc_wm << WM1_LP_FBC_SHIFT) |
3876 (plane_wm << WM1_LP_SR_SHIFT) |
3877 cursor_wm);
3878}
3879
Shaohua Li7662c8b2009-06-26 11:23:55 +08003880/**
3881 * intel_update_watermarks - update FIFO watermark values based on current modes
3882 *
3883 * Calculate watermark values for the various WM regs based on current mode
3884 * and plane configuration.
3885 *
3886 * There are several cases to deal with here:
3887 * - normal (i.e. non-self-refresh)
3888 * - self-refresh (SR) mode
3889 * - lines are large relative to FIFO size (buffer can hold up to 2)
3890 * - lines are small relative to FIFO size (buffer can hold more than 2
3891 * lines), so need to account for TLB latency
3892 *
3893 * The normal calculation is:
3894 * watermark = dotclock * bytes per pixel * latency
3895 * where latency is platform & configuration dependent (we assume pessimal
3896 * values here).
3897 *
3898 * The SR calculation is:
3899 * watermark = (trunc(latency/line time)+1) * surface width *
3900 * bytes per pixel
3901 * where
3902 * line time = htotal / dotclock
Zhao Yakuifa143212010-06-12 14:32:23 +08003903 * surface width = hdisplay for normal plane and 64 for cursor
Shaohua Li7662c8b2009-06-26 11:23:55 +08003904 * and latency is assumed to be high, as above.
3905 *
3906 * The final value programmed to the register should always be rounded up,
3907 * and include an extra 2 entries to account for clock crossings.
3908 *
3909 * We don't use the sprite, so we can ignore that. And on Crestline we have
3910 * to set the non-SR watermarks to 8.
Chris Wilson5eddb702010-09-11 13:48:45 +01003911 */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003912static void intel_update_watermarks(struct drm_device *dev)
3913{
Jesse Barnese70236a2009-09-21 10:42:27 -07003914 struct drm_i915_private *dev_priv = dev->dev_private;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003915 struct drm_crtc *crtc;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003916 int sr_hdisplay = 0;
3917 unsigned long planea_clock = 0, planeb_clock = 0, sr_clock = 0;
3918 int enabled = 0, pixel_size = 0;
Zhao Yakuifa143212010-06-12 14:32:23 +08003919 int sr_htotal = 0;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003920
Zhenyu Wangc03342f2009-09-29 11:01:23 +08003921 if (!dev_priv->display.update_wm)
3922 return;
3923
Shaohua Li7662c8b2009-06-26 11:23:55 +08003924 /* Get the clock config from both planes */
3925 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Chris Wilsondebcadd2010-08-07 11:01:33 +01003926 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003927 if (intel_crtc->active) {
Shaohua Li7662c8b2009-06-26 11:23:55 +08003928 enabled++;
3929 if (intel_crtc->plane == 0) {
Zhao Yakui28c97732009-10-09 11:39:41 +08003930 DRM_DEBUG_KMS("plane A (pipe %d) clock: %d\n",
Chris Wilson5eddb702010-09-11 13:48:45 +01003931 intel_crtc->pipe, crtc->mode.clock);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003932 planea_clock = crtc->mode.clock;
3933 } else {
Zhao Yakui28c97732009-10-09 11:39:41 +08003934 DRM_DEBUG_KMS("plane B (pipe %d) clock: %d\n",
Chris Wilson5eddb702010-09-11 13:48:45 +01003935 intel_crtc->pipe, crtc->mode.clock);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003936 planeb_clock = crtc->mode.clock;
3937 }
3938 sr_hdisplay = crtc->mode.hdisplay;
3939 sr_clock = crtc->mode.clock;
Zhao Yakuifa143212010-06-12 14:32:23 +08003940 sr_htotal = crtc->mode.htotal;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003941 if (crtc->fb)
3942 pixel_size = crtc->fb->bits_per_pixel / 8;
3943 else
3944 pixel_size = 4; /* by default */
3945 }
3946 }
3947
3948 if (enabled <= 0)
3949 return;
3950
Jesse Barnese70236a2009-09-21 10:42:27 -07003951 dev_priv->display.update_wm(dev, planea_clock, planeb_clock,
Zhao Yakuifa143212010-06-12 14:32:23 +08003952 sr_hdisplay, sr_htotal, pixel_size);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003953}
3954
Chris Wilsona7615032011-01-12 17:04:08 +00003955static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
3956{
3957 return dev_priv->lvds_use_ssc && i915_panel_use_ssc;
3958}
3959
Chris Wilson5c3b82e2009-02-11 13:25:09 +00003960static int intel_crtc_mode_set(struct drm_crtc *crtc,
3961 struct drm_display_mode *mode,
3962 struct drm_display_mode *adjusted_mode,
3963 int x, int y,
3964 struct drm_framebuffer *old_fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08003965{
3966 struct drm_device *dev = crtc->dev;
3967 struct drm_i915_private *dev_priv = dev->dev_private;
3968 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3969 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07003970 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01003971 u32 fp_reg, dpll_reg;
Eric Anholtc751ce42010-03-25 11:48:48 -07003972 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07003973 intel_clock_t clock, reduced_clock;
Chris Wilson5eddb702010-09-11 13:48:45 +01003974 u32 dpll, fp = 0, fp2 = 0, dspcntr, pipeconf;
Jesse Barnes652c3932009-08-17 13:31:43 -07003975 bool ok, has_reduced_clock = false, is_sdvo = false, is_dvo = false;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003976 bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
Chris Wilson8e647a22010-08-22 10:54:23 +01003977 struct intel_encoder *has_edp_encoder = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08003978 struct drm_mode_config *mode_config = &dev->mode_config;
Chris Wilson5eddb702010-09-11 13:48:45 +01003979 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08003980 const intel_limit_t *limit;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00003981 int ret;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003982 struct fdi_m_n m_n = {0};
Chris Wilson5eddb702010-09-11 13:48:45 +01003983 u32 reg, temp;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08003984 int target_clock;
Jesse Barnes79e53942008-11-07 14:24:08 -08003985
3986 drm_vblank_pre_modeset(dev, pipe);
3987
Chris Wilson5eddb702010-09-11 13:48:45 +01003988 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
3989 if (encoder->base.crtc != crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003990 continue;
3991
Chris Wilson5eddb702010-09-11 13:48:45 +01003992 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003993 case INTEL_OUTPUT_LVDS:
3994 is_lvds = true;
3995 break;
3996 case INTEL_OUTPUT_SDVO:
Eric Anholt7d573822009-01-02 13:33:00 -08003997 case INTEL_OUTPUT_HDMI:
Jesse Barnes79e53942008-11-07 14:24:08 -08003998 is_sdvo = true;
Chris Wilson5eddb702010-09-11 13:48:45 +01003999 if (encoder->needs_tv_clock)
Jesse Barnese2f0ba92009-02-02 15:11:52 -08004000 is_tv = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08004001 break;
4002 case INTEL_OUTPUT_DVO:
4003 is_dvo = true;
4004 break;
4005 case INTEL_OUTPUT_TVOUT:
4006 is_tv = true;
4007 break;
4008 case INTEL_OUTPUT_ANALOG:
4009 is_crt = true;
4010 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004011 case INTEL_OUTPUT_DISPLAYPORT:
4012 is_dp = true;
4013 break;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004014 case INTEL_OUTPUT_EDP:
Chris Wilson5eddb702010-09-11 13:48:45 +01004015 has_edp_encoder = encoder;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004016 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08004017 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05004018
Eric Anholtc751ce42010-03-25 11:48:48 -07004019 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08004020 }
4021
Chris Wilsona7615032011-01-12 17:04:08 +00004022 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Kristian Høgsberg43565a02009-02-13 20:56:52 -05004023 refclk = dev_priv->lvds_ssc_freq * 1000;
Zhao Yakui28c97732009-10-09 11:39:41 +08004024 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
Chris Wilson5eddb702010-09-11 13:48:45 +01004025 refclk / 1000);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004026 } else if (!IS_GEN2(dev)) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004027 refclk = 96000;
Jesse Barnes1cb1b752010-10-07 16:01:17 -07004028 if (HAS_PCH_SPLIT(dev) &&
4029 (!has_edp_encoder || intel_encoder_is_pch_edp(&has_edp_encoder->base)))
Zhenyu Wang2c072452009-06-05 15:38:42 +08004030 refclk = 120000; /* 120Mhz refclk */
Jesse Barnes79e53942008-11-07 14:24:08 -08004031 } else {
4032 refclk = 48000;
4033 }
4034
Ma Lingd4906092009-03-18 20:13:27 +08004035 /*
4036 * Returns a set of divisors for the desired target clock with the given
4037 * refclk, or FALSE. The returned values represent the clock equation:
4038 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
4039 */
Chris Wilson1b894b52010-12-14 20:04:54 +00004040 limit = intel_limit(crtc, refclk);
Ma Lingd4906092009-03-18 20:13:27 +08004041 ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08004042 if (!ok) {
4043 DRM_ERROR("Couldn't find PLL settings for mode!\n");
Chris Wilson1f803ee2009-06-06 09:45:59 +01004044 drm_vblank_post_modeset(dev, pipe);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00004045 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08004046 }
4047
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004048 /* Ensure that the cursor is valid for the new mode before changing... */
Chris Wilson6b383a72010-09-13 13:54:26 +01004049 intel_crtc_update_cursor(crtc, true);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004050
Zhao Yakuiddc90032010-01-06 22:05:56 +08004051 if (is_lvds && dev_priv->lvds_downclock_avail) {
4052 has_reduced_clock = limit->find_pll(limit, crtc,
Chris Wilson5eddb702010-09-11 13:48:45 +01004053 dev_priv->lvds_downclock,
4054 refclk,
4055 &reduced_clock);
Zhao Yakui18f9ed12009-11-20 03:24:16 +00004056 if (has_reduced_clock && (clock.p != reduced_clock.p)) {
4057 /*
4058 * If the different P is found, it means that we can't
4059 * switch the display clock by using the FP0/FP1.
4060 * In such case we will disable the LVDS downclock
4061 * feature.
4062 */
4063 DRM_DEBUG_KMS("Different P is found for "
Chris Wilson5eddb702010-09-11 13:48:45 +01004064 "LVDS clock/downclock\n");
Zhao Yakui18f9ed12009-11-20 03:24:16 +00004065 has_reduced_clock = 0;
4066 }
Jesse Barnes652c3932009-08-17 13:31:43 -07004067 }
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08004068 /* SDVO TV has fixed PLL values depend on its clock range,
4069 this mirrors vbios setting. */
4070 if (is_sdvo && is_tv) {
4071 if (adjusted_mode->clock >= 100000
Chris Wilson5eddb702010-09-11 13:48:45 +01004072 && adjusted_mode->clock < 140500) {
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08004073 clock.p1 = 2;
4074 clock.p2 = 10;
4075 clock.n = 3;
4076 clock.m1 = 16;
4077 clock.m2 = 8;
4078 } else if (adjusted_mode->clock >= 140500
Chris Wilson5eddb702010-09-11 13:48:45 +01004079 && adjusted_mode->clock <= 200000) {
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08004080 clock.p1 = 1;
4081 clock.p2 = 10;
4082 clock.n = 6;
4083 clock.m1 = 12;
4084 clock.m2 = 8;
4085 }
4086 }
4087
Zhenyu Wang2c072452009-06-05 15:38:42 +08004088 /* FDI link */
Eric Anholtbad720f2009-10-22 16:11:14 -07004089 if (HAS_PCH_SPLIT(dev)) {
Chris Wilson49078f72010-12-04 07:45:57 +00004090 int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
Adam Jackson77ffb592010-04-12 11:38:44 -04004091 int lane = 0, link_bw, bpp;
Jesse Barnes5c5313c2010-10-07 16:01:11 -07004092 /* CPU eDP doesn't require FDI link, so just set DP M/N
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004093 according to current link config */
Jesse Barnes858bc212011-01-04 10:46:49 -08004094 if (has_edp_encoder && !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08004095 target_clock = mode->clock;
Chris Wilson8e647a22010-08-22 10:54:23 +01004096 intel_edp_link_config(has_edp_encoder,
4097 &lane, &link_bw);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004098 } else {
Jesse Barnes5c5313c2010-10-07 16:01:11 -07004099 /* [e]DP over FDI requires target mode clock
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004100 instead of link clock */
Jesse Barnes5c5313c2010-10-07 16:01:11 -07004101 if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004102 target_clock = mode->clock;
4103 else
4104 target_clock = adjusted_mode->clock;
Chris Wilson021357a2010-09-07 20:54:59 +01004105
4106 /* FDI is a binary signal running at ~2.7GHz, encoding
4107 * each output octet as 10 bits. The actual frequency
4108 * is stored as a divider into a 100MHz clock, and the
4109 * mode pixel clock is stored in units of 1KHz.
4110 * Hence the bw of each lane in terms of the mode signal
4111 * is:
4112 */
4113 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004114 }
Zhenyu Wang58a27472009-09-25 08:01:28 +00004115
4116 /* determine panel color depth */
Chris Wilson5eddb702010-09-11 13:48:45 +01004117 temp = I915_READ(PIPECONF(pipe));
Zhao Yakuie5a95eb2010-01-04 16:29:32 +08004118 temp &= ~PIPE_BPC_MASK;
4119 if (is_lvds) {
Zhao Yakuie5a95eb2010-01-04 16:29:32 +08004120 /* the BPC will be 6 if it is 18-bit LVDS panel */
Chris Wilson5eddb702010-09-11 13:48:45 +01004121 if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) == LVDS_A3_POWER_UP)
Zhao Yakuie5a95eb2010-01-04 16:29:32 +08004122 temp |= PIPE_8BPC;
4123 else
4124 temp |= PIPE_6BPC;
Jesse Barnes1d850362010-10-07 16:01:10 -07004125 } else if (has_edp_encoder) {
Chris Wilson5ceb0f92010-09-24 10:24:28 +01004126 switch (dev_priv->edp.bpp/3) {
Zhenyu Wang885a5fb2010-01-12 05:38:31 +08004127 case 8:
4128 temp |= PIPE_8BPC;
4129 break;
4130 case 10:
4131 temp |= PIPE_10BPC;
4132 break;
4133 case 6:
4134 temp |= PIPE_6BPC;
4135 break;
4136 case 12:
4137 temp |= PIPE_12BPC;
4138 break;
4139 }
Zhao Yakuie5a95eb2010-01-04 16:29:32 +08004140 } else
4141 temp |= PIPE_8BPC;
Chris Wilson5eddb702010-09-11 13:48:45 +01004142 I915_WRITE(PIPECONF(pipe), temp);
Zhenyu Wang58a27472009-09-25 08:01:28 +00004143
4144 switch (temp & PIPE_BPC_MASK) {
4145 case PIPE_8BPC:
4146 bpp = 24;
4147 break;
4148 case PIPE_10BPC:
4149 bpp = 30;
4150 break;
4151 case PIPE_6BPC:
4152 bpp = 18;
4153 break;
4154 case PIPE_12BPC:
4155 bpp = 36;
4156 break;
4157 default:
4158 DRM_ERROR("unknown pipe bpc value\n");
4159 bpp = 24;
4160 }
4161
Adam Jackson77ffb592010-04-12 11:38:44 -04004162 if (!lane) {
4163 /*
4164 * Account for spread spectrum to avoid
4165 * oversubscribing the link. Max center spread
4166 * is 2.5%; use 5% for safety's sake.
4167 */
4168 u32 bps = target_clock * bpp * 21 / 20;
4169 lane = bps / (link_bw * 8) + 1;
4170 }
4171
4172 intel_crtc->fdi_lanes = lane;
4173
Chris Wilson49078f72010-12-04 07:45:57 +00004174 if (pixel_multiplier > 1)
4175 link_bw *= pixel_multiplier;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05004176 ironlake_compute_m_n(bpp, lane, target_clock, link_bw, &m_n);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08004177 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08004178
Zhenyu Wangc038e512009-10-19 15:43:48 +08004179 /* Ironlake: try to setup display ref clock before DPLL
4180 * enabling. This is only under driver's control after
4181 * PCH B stepping, previous chipset stepping should be
4182 * ignoring this setting.
4183 */
Eric Anholtbad720f2009-10-22 16:11:14 -07004184 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wangc038e512009-10-19 15:43:48 +08004185 temp = I915_READ(PCH_DREF_CONTROL);
4186 /* Always enable nonspread source */
4187 temp &= ~DREF_NONSPREAD_SOURCE_MASK;
4188 temp |= DREF_NONSPREAD_SOURCE_ENABLE;
Zhenyu Wangc038e512009-10-19 15:43:48 +08004189 temp &= ~DREF_SSC_SOURCE_MASK;
4190 temp |= DREF_SSC_SOURCE_ENABLE;
4191 I915_WRITE(PCH_DREF_CONTROL, temp);
Zhenyu Wangc038e512009-10-19 15:43:48 +08004192
Chris Wilson5eddb702010-09-11 13:48:45 +01004193 POSTING_READ(PCH_DREF_CONTROL);
Zhenyu Wangc038e512009-10-19 15:43:48 +08004194 udelay(200);
4195
Chris Wilson8e647a22010-08-22 10:54:23 +01004196 if (has_edp_encoder) {
Chris Wilsona7615032011-01-12 17:04:08 +00004197 if (intel_panel_use_ssc(dev_priv)) {
Zhenyu Wangc038e512009-10-19 15:43:48 +08004198 temp |= DREF_SSC1_ENABLE;
4199 I915_WRITE(PCH_DREF_CONTROL, temp);
Zhenyu Wangc038e512009-10-19 15:43:48 +08004200
Chris Wilson5eddb702010-09-11 13:48:45 +01004201 POSTING_READ(PCH_DREF_CONTROL);
Zhenyu Wangc038e512009-10-19 15:43:48 +08004202 udelay(200);
Jesse Barnes7f823282010-10-07 16:01:16 -07004203 }
4204 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Zhenyu Wangc038e512009-10-19 15:43:48 +08004205
Jesse Barnes7f823282010-10-07 16:01:16 -07004206 /* Enable CPU source on CPU attached eDP */
4207 if (!intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
Chris Wilsona7615032011-01-12 17:04:08 +00004208 if (intel_panel_use_ssc(dev_priv))
Jesse Barnes7f823282010-10-07 16:01:16 -07004209 temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
4210 else
4211 temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Zhenyu Wangc038e512009-10-19 15:43:48 +08004212 } else {
Jesse Barnes7f823282010-10-07 16:01:16 -07004213 /* Enable SSC on PCH eDP if needed */
Chris Wilsona7615032011-01-12 17:04:08 +00004214 if (intel_panel_use_ssc(dev_priv)) {
Jesse Barnes7f823282010-10-07 16:01:16 -07004215 DRM_ERROR("enabling SSC on PCH\n");
4216 temp |= DREF_SUPERSPREAD_SOURCE_ENABLE;
4217 }
Zhenyu Wangc038e512009-10-19 15:43:48 +08004218 }
Chris Wilson5eddb702010-09-11 13:48:45 +01004219 I915_WRITE(PCH_DREF_CONTROL, temp);
Jesse Barnes7f823282010-10-07 16:01:16 -07004220 POSTING_READ(PCH_DREF_CONTROL);
4221 udelay(200);
Zhenyu Wangc038e512009-10-19 15:43:48 +08004222 }
4223 }
4224
Adam Jacksonf2b115e2009-12-03 17:14:42 -05004225 if (IS_PINEVIEW(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +08004226 fp = (1 << clock.n) << 16 | clock.m1 << 8 | clock.m2;
Jesse Barnes652c3932009-08-17 13:31:43 -07004227 if (has_reduced_clock)
4228 fp2 = (1 << reduced_clock.n) << 16 |
4229 reduced_clock.m1 << 8 | reduced_clock.m2;
4230 } else {
Shaohua Li21778322009-02-23 15:19:16 +08004231 fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
Jesse Barnes652c3932009-08-17 13:31:43 -07004232 if (has_reduced_clock)
4233 fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
4234 reduced_clock.m2;
4235 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004236
Chris Wilsonc1858122010-12-03 21:35:48 +00004237 /* Enable autotuning of the PLL clock (if permissible) */
4238 if (HAS_PCH_SPLIT(dev)) {
4239 int factor = 21;
4240
4241 if (is_lvds) {
Chris Wilsona7615032011-01-12 17:04:08 +00004242 if ((intel_panel_use_ssc(dev_priv) &&
Chris Wilsonc1858122010-12-03 21:35:48 +00004243 dev_priv->lvds_ssc_freq == 100) ||
4244 (I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP)
4245 factor = 25;
4246 } else if (is_sdvo && is_tv)
4247 factor = 20;
4248
4249 if (clock.m1 < factor * clock.n)
4250 fp |= FP_CB_TUNE;
4251 }
4252
Chris Wilson5eddb702010-09-11 13:48:45 +01004253 dpll = 0;
Eric Anholtbad720f2009-10-22 16:11:14 -07004254 if (!HAS_PCH_SPLIT(dev))
Zhenyu Wang2c072452009-06-05 15:38:42 +08004255 dpll = DPLL_VGA_MODE_DIS;
4256
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004257 if (!IS_GEN2(dev)) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004258 if (is_lvds)
4259 dpll |= DPLLB_MODE_LVDS;
4260 else
4261 dpll |= DPLLB_MODE_DAC_SERIAL;
4262 if (is_sdvo) {
Chris Wilson6c9547f2010-08-25 10:05:17 +01004263 int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
4264 if (pixel_multiplier > 1) {
4265 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4266 dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
4267 else if (HAS_PCH_SPLIT(dev))
4268 dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
4269 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004270 dpll |= DPLL_DVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08004271 }
Jesse Barnes83240122010-10-07 16:01:18 -07004272 if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004273 dpll |= DPLL_DVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08004274
4275 /* compute bitmask from p1 value */
Adam Jacksonf2b115e2009-12-03 17:14:42 -05004276 if (IS_PINEVIEW(dev))
4277 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004278 else {
Shaohua Li21778322009-02-23 15:19:16 +08004279 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004280 /* also FPA1 */
Eric Anholtbad720f2009-10-22 16:11:14 -07004281 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang2c072452009-06-05 15:38:42 +08004282 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Jesse Barnes652c3932009-08-17 13:31:43 -07004283 if (IS_G4X(dev) && has_reduced_clock)
4284 dpll |= (1 << (reduced_clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004285 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004286 switch (clock.p2) {
4287 case 5:
4288 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4289 break;
4290 case 7:
4291 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4292 break;
4293 case 10:
4294 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4295 break;
4296 case 14:
4297 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4298 break;
4299 }
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004300 if (INTEL_INFO(dev)->gen >= 4 && !HAS_PCH_SPLIT(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08004301 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
4302 } else {
4303 if (is_lvds) {
4304 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4305 } else {
4306 if (clock.p1 == 2)
4307 dpll |= PLL_P1_DIVIDE_BY_TWO;
4308 else
4309 dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4310 if (clock.p2 == 4)
4311 dpll |= PLL_P2_DIVIDE_BY_4;
4312 }
4313 }
4314
Kristian Høgsberg43565a02009-02-13 20:56:52 -05004315 if (is_sdvo && is_tv)
4316 dpll |= PLL_REF_INPUT_TVCLKINBC;
4317 else if (is_tv)
Jesse Barnes79e53942008-11-07 14:24:08 -08004318 /* XXX: just matching BIOS for now */
Kristian Høgsberg43565a02009-02-13 20:56:52 -05004319 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
Jesse Barnes79e53942008-11-07 14:24:08 -08004320 dpll |= 3;
Chris Wilsona7615032011-01-12 17:04:08 +00004321 else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05004322 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08004323 else
4324 dpll |= PLL_REF_INPUT_DREFCLK;
4325
4326 /* setup pipeconf */
Chris Wilson5eddb702010-09-11 13:48:45 +01004327 pipeconf = I915_READ(PIPECONF(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08004328
4329 /* Set up the display plane register */
4330 dspcntr = DISPPLANE_GAMMA_ENABLE;
4331
Adam Jacksonf2b115e2009-12-03 17:14:42 -05004332 /* Ironlake's plane is forced to pipe, bit 24 is to
Zhenyu Wang2c072452009-06-05 15:38:42 +08004333 enable color space conversion */
Eric Anholtbad720f2009-10-22 16:11:14 -07004334 if (!HAS_PCH_SPLIT(dev)) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08004335 if (pipe == 0)
Jesse Barnes80824002009-09-10 15:28:06 -07004336 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004337 else
4338 dspcntr |= DISPPLANE_SEL_PIPE_B;
4339 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004340
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004341 if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004342 /* Enable pixel doubling when the dot clock is > 90% of the (display)
4343 * core speed.
4344 *
4345 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
4346 * pipe == 0 check?
4347 */
Jesse Barnese70236a2009-09-21 10:42:27 -07004348 if (mode->clock >
4349 dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
Chris Wilson5eddb702010-09-11 13:48:45 +01004350 pipeconf |= PIPECONF_DOUBLE_WIDE;
Jesse Barnes79e53942008-11-07 14:24:08 -08004351 else
Chris Wilson5eddb702010-09-11 13:48:45 +01004352 pipeconf &= ~PIPECONF_DOUBLE_WIDE;
Jesse Barnes79e53942008-11-07 14:24:08 -08004353 }
4354
Jesse Barnesb24e7172011-01-04 15:09:30 -08004355 if (!HAS_PCH_SPLIT(dev))
Jesse Barnes65993d62011-01-04 15:09:29 -08004356 dpll |= DPLL_VCO_ENABLE;
Linus Torvalds8d86dc62010-06-08 20:16:28 -07004357
Zhao Yakui28c97732009-10-09 11:39:41 +08004358 DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
Jesse Barnes79e53942008-11-07 14:24:08 -08004359 drm_mode_debug_printmodeline(mode);
4360
Adam Jacksonf2b115e2009-12-03 17:14:42 -05004361 /* assign to Ironlake registers */
Eric Anholtbad720f2009-10-22 16:11:14 -07004362 if (HAS_PCH_SPLIT(dev)) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004363 fp_reg = PCH_FP0(pipe);
4364 dpll_reg = PCH_DPLL(pipe);
4365 } else {
4366 fp_reg = FP0(pipe);
4367 dpll_reg = DPLL(pipe);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004368 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004369
Jesse Barnes5c5313c2010-10-07 16:01:11 -07004370 /* PCH eDP needs FDI, but CPU eDP does not */
4371 if (!has_edp_encoder || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004372 I915_WRITE(fp_reg, fp);
4373 I915_WRITE(dpll_reg, dpll & ~DPLL_VCO_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01004374
4375 POSTING_READ(dpll_reg);
Jesse Barnes79e53942008-11-07 14:24:08 -08004376 udelay(150);
4377 }
4378
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08004379 /* enable transcoder DPLL */
4380 if (HAS_PCH_CPT(dev)) {
4381 temp = I915_READ(PCH_DPLL_SEL);
Chris Wilson5eddb702010-09-11 13:48:45 +01004382 if (pipe == 0)
4383 temp |= TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL;
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08004384 else
Chris Wilson5eddb702010-09-11 13:48:45 +01004385 temp |= TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL;
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08004386 I915_WRITE(PCH_DPLL_SEL, temp);
Chris Wilson5eddb702010-09-11 13:48:45 +01004387
4388 POSTING_READ(PCH_DPLL_SEL);
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08004389 udelay(150);
4390 }
4391
Jesse Barnes79e53942008-11-07 14:24:08 -08004392 /* The LVDS pin pair needs to be on before the DPLLs are enabled.
4393 * This is an exception to the general rule that mode_set doesn't turn
4394 * things on.
4395 */
4396 if (is_lvds) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004397 reg = LVDS;
Eric Anholtbad720f2009-10-22 16:11:14 -07004398 if (HAS_PCH_SPLIT(dev))
Chris Wilson5eddb702010-09-11 13:48:45 +01004399 reg = PCH_LVDS;
Zhenyu Wang541998a2009-06-05 15:38:44 +08004400
Chris Wilson5eddb702010-09-11 13:48:45 +01004401 temp = I915_READ(reg);
4402 temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
Zhenyu Wangb3b095b2010-04-07 16:15:56 +08004403 if (pipe == 1) {
4404 if (HAS_PCH_CPT(dev))
Chris Wilson5eddb702010-09-11 13:48:45 +01004405 temp |= PORT_TRANS_B_SEL_CPT;
Zhenyu Wangb3b095b2010-04-07 16:15:56 +08004406 else
Chris Wilson5eddb702010-09-11 13:48:45 +01004407 temp |= LVDS_PIPEB_SELECT;
Zhenyu Wangb3b095b2010-04-07 16:15:56 +08004408 } else {
4409 if (HAS_PCH_CPT(dev))
Chris Wilson5eddb702010-09-11 13:48:45 +01004410 temp &= ~PORT_TRANS_SEL_MASK;
Zhenyu Wangb3b095b2010-04-07 16:15:56 +08004411 else
Chris Wilson5eddb702010-09-11 13:48:45 +01004412 temp &= ~LVDS_PIPEB_SELECT;
Zhenyu Wangb3b095b2010-04-07 16:15:56 +08004413 }
Zhao Yakuia3e17eb2009-10-10 10:42:37 +08004414 /* set the corresponsding LVDS_BORDER bit */
Chris Wilson5eddb702010-09-11 13:48:45 +01004415 temp |= dev_priv->lvds_border_bits;
Jesse Barnes79e53942008-11-07 14:24:08 -08004416 /* Set the B0-B3 data pairs corresponding to whether we're going to
4417 * set the DPLLs for dual-channel mode or not.
4418 */
4419 if (clock.p2 == 7)
Chris Wilson5eddb702010-09-11 13:48:45 +01004420 temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
Jesse Barnes79e53942008-11-07 14:24:08 -08004421 else
Chris Wilson5eddb702010-09-11 13:48:45 +01004422 temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
Jesse Barnes79e53942008-11-07 14:24:08 -08004423
4424 /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
4425 * appropriately here, but we need to look more thoroughly into how
4426 * panels behave in the two modes.
4427 */
Jesse Barnes434ed092010-09-07 14:48:06 -07004428 /* set the dithering flag on non-PCH LVDS as needed */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004429 if (INTEL_INFO(dev)->gen >= 4 && !HAS_PCH_SPLIT(dev)) {
Jesse Barnes434ed092010-09-07 14:48:06 -07004430 if (dev_priv->lvds_dither)
Chris Wilson5eddb702010-09-11 13:48:45 +01004431 temp |= LVDS_ENABLE_DITHER;
Jesse Barnes434ed092010-09-07 14:48:06 -07004432 else
Chris Wilson5eddb702010-09-11 13:48:45 +01004433 temp &= ~LVDS_ENABLE_DITHER;
Zhao Yakui898822c2010-01-04 16:29:30 +08004434 }
Chris Wilson5eddb702010-09-11 13:48:45 +01004435 I915_WRITE(reg, temp);
Jesse Barnes79e53942008-11-07 14:24:08 -08004436 }
Jesse Barnes434ed092010-09-07 14:48:06 -07004437
4438 /* set the dithering flag and clear for anything other than a panel. */
4439 if (HAS_PCH_SPLIT(dev)) {
4440 pipeconf &= ~PIPECONF_DITHER_EN;
4441 pipeconf &= ~PIPECONF_DITHER_TYPE_MASK;
4442 if (dev_priv->lvds_dither && (is_lvds || has_edp_encoder)) {
4443 pipeconf |= PIPECONF_DITHER_EN;
4444 pipeconf |= PIPECONF_DITHER_TYPE_ST1;
4445 }
4446 }
4447
Jesse Barnes5c5313c2010-10-07 16:01:11 -07004448 if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004449 intel_dp_set_m_n(crtc, mode, adjusted_mode);
Jesse Barnes5c5313c2010-10-07 16:01:11 -07004450 } else if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wang8db9d77b2010-04-07 16:15:54 +08004451 /* For non-DP output, clear any trans DP clock recovery setting.*/
4452 if (pipe == 0) {
4453 I915_WRITE(TRANSA_DATA_M1, 0);
4454 I915_WRITE(TRANSA_DATA_N1, 0);
4455 I915_WRITE(TRANSA_DP_LINK_M1, 0);
4456 I915_WRITE(TRANSA_DP_LINK_N1, 0);
4457 } else {
4458 I915_WRITE(TRANSB_DATA_M1, 0);
4459 I915_WRITE(TRANSB_DATA_N1, 0);
4460 I915_WRITE(TRANSB_DP_LINK_M1, 0);
4461 I915_WRITE(TRANSB_DP_LINK_N1, 0);
4462 }
4463 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004464
Jesse Barnes5c5313c2010-10-07 16:01:11 -07004465 if (!has_edp_encoder || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004466 I915_WRITE(dpll_reg, dpll);
Chris Wilson5eddb702010-09-11 13:48:45 +01004467
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004468 /* Wait for the clocks to stabilize. */
Chris Wilson5eddb702010-09-11 13:48:45 +01004469 POSTING_READ(dpll_reg);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004470 udelay(150);
4471
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004472 if (INTEL_INFO(dev)->gen >= 4 && !HAS_PCH_SPLIT(dev)) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004473 temp = 0;
Zhao Yakuibb66c512009-09-10 15:45:49 +08004474 if (is_sdvo) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004475 temp = intel_mode_get_pixel_multiplier(adjusted_mode);
4476 if (temp > 1)
4477 temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Chris Wilson6c9547f2010-08-25 10:05:17 +01004478 else
Chris Wilson5eddb702010-09-11 13:48:45 +01004479 temp = 0;
4480 }
4481 I915_WRITE(DPLL_MD(pipe), temp);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004482 } else {
Chris Wilsona589b9f2010-12-03 21:13:16 +00004483 /* The pixel multiplier can only be updated once the
4484 * DPLL is enabled and the clocks are stable.
4485 *
4486 * So write it again.
4487 */
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004488 I915_WRITE(dpll_reg, dpll);
4489 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004490 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004491
Chris Wilson5eddb702010-09-11 13:48:45 +01004492 intel_crtc->lowfreq_avail = false;
Jesse Barnes652c3932009-08-17 13:31:43 -07004493 if (is_lvds && has_reduced_clock && i915_powersave) {
4494 I915_WRITE(fp_reg + 4, fp2);
4495 intel_crtc->lowfreq_avail = true;
4496 if (HAS_PIPE_CXSR(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08004497 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07004498 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
4499 }
4500 } else {
4501 I915_WRITE(fp_reg + 4, fp);
Jesse Barnes652c3932009-08-17 13:31:43 -07004502 if (HAS_PIPE_CXSR(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08004503 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07004504 pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
4505 }
4506 }
4507
Krzysztof Halasa734b4152010-05-25 18:41:46 +02004508 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4509 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
4510 /* the chip adds 2 halflines automatically */
4511 adjusted_mode->crtc_vdisplay -= 1;
4512 adjusted_mode->crtc_vtotal -= 1;
4513 adjusted_mode->crtc_vblank_start -= 1;
4514 adjusted_mode->crtc_vblank_end -= 1;
4515 adjusted_mode->crtc_vsync_end -= 1;
4516 adjusted_mode->crtc_vsync_start -= 1;
4517 } else
4518 pipeconf &= ~PIPECONF_INTERLACE_W_FIELD_INDICATION; /* progressive */
4519
Chris Wilson5eddb702010-09-11 13:48:45 +01004520 I915_WRITE(HTOTAL(pipe),
4521 (adjusted_mode->crtc_hdisplay - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08004522 ((adjusted_mode->crtc_htotal - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01004523 I915_WRITE(HBLANK(pipe),
4524 (adjusted_mode->crtc_hblank_start - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08004525 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01004526 I915_WRITE(HSYNC(pipe),
4527 (adjusted_mode->crtc_hsync_start - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08004528 ((adjusted_mode->crtc_hsync_end - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01004529
4530 I915_WRITE(VTOTAL(pipe),
4531 (adjusted_mode->crtc_vdisplay - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08004532 ((adjusted_mode->crtc_vtotal - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01004533 I915_WRITE(VBLANK(pipe),
4534 (adjusted_mode->crtc_vblank_start - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08004535 ((adjusted_mode->crtc_vblank_end - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01004536 I915_WRITE(VSYNC(pipe),
4537 (adjusted_mode->crtc_vsync_start - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08004538 ((adjusted_mode->crtc_vsync_end - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01004539
4540 /* pipesrc and dspsize control the size that is scaled from,
4541 * which should always be the user's requested size.
Jesse Barnes79e53942008-11-07 14:24:08 -08004542 */
Eric Anholtbad720f2009-10-22 16:11:14 -07004543 if (!HAS_PCH_SPLIT(dev)) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004544 I915_WRITE(DSPSIZE(plane),
4545 ((mode->vdisplay - 1) << 16) |
4546 (mode->hdisplay - 1));
4547 I915_WRITE(DSPPOS(plane), 0);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004548 }
Chris Wilson5eddb702010-09-11 13:48:45 +01004549 I915_WRITE(PIPESRC(pipe),
4550 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
Zhenyu Wang2c072452009-06-05 15:38:42 +08004551
Eric Anholtbad720f2009-10-22 16:11:14 -07004552 if (HAS_PCH_SPLIT(dev)) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004553 I915_WRITE(PIPE_DATA_M1(pipe), TU_SIZE(m_n.tu) | m_n.gmch_m);
4554 I915_WRITE(PIPE_DATA_N1(pipe), m_n.gmch_n);
4555 I915_WRITE(PIPE_LINK_M1(pipe), m_n.link_m);
4556 I915_WRITE(PIPE_LINK_N1(pipe), m_n.link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004557
Jesse Barnes5c5313c2010-10-07 16:01:11 -07004558 if (has_edp_encoder && !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05004559 ironlake_set_pll_edp(crtc, adjusted_mode->clock);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004560 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08004561 }
4562
Chris Wilson5eddb702010-09-11 13:48:45 +01004563 I915_WRITE(PIPECONF(pipe), pipeconf);
4564 POSTING_READ(PIPECONF(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08004565 if (!HAS_PCH_SPLIT(dev))
4566 intel_enable_pipe(dev_priv, pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08004567
Jesse Barnes9d0498a2010-08-18 13:20:54 -07004568 intel_wait_for_vblank(dev, pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08004569
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01004570 if (IS_GEN5(dev)) {
Zhenyu Wang553bd142009-09-02 10:57:52 +08004571 /* enable address swizzle for tiling buffer */
4572 temp = I915_READ(DISP_ARB_CTL);
4573 I915_WRITE(DISP_ARB_CTL, temp | DISP_TILE_SURFACE_SWIZZLING);
4574 }
4575
Chris Wilson5eddb702010-09-11 13:48:45 +01004576 I915_WRITE(DSPCNTR(plane), dspcntr);
Jesse Barnesb24e7172011-01-04 15:09:30 -08004577 POSTING_READ(DSPCNTR(plane));
4578 if (!HAS_PCH_SPLIT(dev))
4579 intel_enable_plane(dev_priv, plane, pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08004580
Chris Wilson5c3b82e2009-02-11 13:25:09 +00004581 ret = intel_pipe_set_base(crtc, x, y, old_fb);
Shaohua Li7662c8b2009-06-26 11:23:55 +08004582
4583 intel_update_watermarks(dev);
4584
Jesse Barnes79e53942008-11-07 14:24:08 -08004585 drm_vblank_post_modeset(dev, pipe);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00004586
Chris Wilson1f803ee2009-06-06 09:45:59 +01004587 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08004588}
4589
4590/** Loads the palette/gamma unit for the CRTC with the prepared values */
4591void intel_crtc_load_lut(struct drm_crtc *crtc)
4592{
4593 struct drm_device *dev = crtc->dev;
4594 struct drm_i915_private *dev_priv = dev->dev_private;
4595 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4596 int palreg = (intel_crtc->pipe == 0) ? PALETTE_A : PALETTE_B;
4597 int i;
4598
4599 /* The clocks have to be on to load the palette. */
4600 if (!crtc->enabled)
4601 return;
4602
Adam Jacksonf2b115e2009-12-03 17:14:42 -05004603 /* use legacy palette for Ironlake */
Eric Anholtbad720f2009-10-22 16:11:14 -07004604 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang2c072452009-06-05 15:38:42 +08004605 palreg = (intel_crtc->pipe == 0) ? LGC_PALETTE_A :
4606 LGC_PALETTE_B;
4607
Jesse Barnes79e53942008-11-07 14:24:08 -08004608 for (i = 0; i < 256; i++) {
4609 I915_WRITE(palreg + 4 * i,
4610 (intel_crtc->lut_r[i] << 16) |
4611 (intel_crtc->lut_g[i] << 8) |
4612 intel_crtc->lut_b[i]);
4613 }
4614}
4615
Chris Wilson560b85b2010-08-07 11:01:38 +01004616static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
4617{
4618 struct drm_device *dev = crtc->dev;
4619 struct drm_i915_private *dev_priv = dev->dev_private;
4620 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4621 bool visible = base != 0;
4622 u32 cntl;
4623
4624 if (intel_crtc->cursor_visible == visible)
4625 return;
4626
4627 cntl = I915_READ(CURACNTR);
4628 if (visible) {
4629 /* On these chipsets we can only modify the base whilst
4630 * the cursor is disabled.
4631 */
4632 I915_WRITE(CURABASE, base);
4633
4634 cntl &= ~(CURSOR_FORMAT_MASK);
4635 /* XXX width must be 64, stride 256 => 0x00 << 28 */
4636 cntl |= CURSOR_ENABLE |
4637 CURSOR_GAMMA_ENABLE |
4638 CURSOR_FORMAT_ARGB;
4639 } else
4640 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
4641 I915_WRITE(CURACNTR, cntl);
4642
4643 intel_crtc->cursor_visible = visible;
4644}
4645
4646static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
4647{
4648 struct drm_device *dev = crtc->dev;
4649 struct drm_i915_private *dev_priv = dev->dev_private;
4650 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4651 int pipe = intel_crtc->pipe;
4652 bool visible = base != 0;
4653
4654 if (intel_crtc->cursor_visible != visible) {
4655 uint32_t cntl = I915_READ(pipe == 0 ? CURACNTR : CURBCNTR);
4656 if (base) {
4657 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
4658 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
4659 cntl |= pipe << 28; /* Connect to correct pipe */
4660 } else {
4661 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
4662 cntl |= CURSOR_MODE_DISABLE;
4663 }
4664 I915_WRITE(pipe == 0 ? CURACNTR : CURBCNTR, cntl);
4665
4666 intel_crtc->cursor_visible = visible;
4667 }
4668 /* and commit changes on next vblank */
4669 I915_WRITE(pipe == 0 ? CURABASE : CURBBASE, base);
4670}
4671
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004672/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01004673static void intel_crtc_update_cursor(struct drm_crtc *crtc,
4674 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004675{
4676 struct drm_device *dev = crtc->dev;
4677 struct drm_i915_private *dev_priv = dev->dev_private;
4678 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4679 int pipe = intel_crtc->pipe;
4680 int x = intel_crtc->cursor_x;
4681 int y = intel_crtc->cursor_y;
Chris Wilson560b85b2010-08-07 11:01:38 +01004682 u32 base, pos;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004683 bool visible;
4684
4685 pos = 0;
4686
Chris Wilson6b383a72010-09-13 13:54:26 +01004687 if (on && crtc->enabled && crtc->fb) {
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004688 base = intel_crtc->cursor_addr;
4689 if (x > (int) crtc->fb->width)
4690 base = 0;
4691
4692 if (y > (int) crtc->fb->height)
4693 base = 0;
4694 } else
4695 base = 0;
4696
4697 if (x < 0) {
4698 if (x + intel_crtc->cursor_width < 0)
4699 base = 0;
4700
4701 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
4702 x = -x;
4703 }
4704 pos |= x << CURSOR_X_SHIFT;
4705
4706 if (y < 0) {
4707 if (y + intel_crtc->cursor_height < 0)
4708 base = 0;
4709
4710 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
4711 y = -y;
4712 }
4713 pos |= y << CURSOR_Y_SHIFT;
4714
4715 visible = base != 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01004716 if (!visible && !intel_crtc->cursor_visible)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004717 return;
4718
4719 I915_WRITE(pipe == 0 ? CURAPOS : CURBPOS, pos);
Chris Wilson560b85b2010-08-07 11:01:38 +01004720 if (IS_845G(dev) || IS_I865G(dev))
4721 i845_update_cursor(crtc, base);
4722 else
4723 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004724
4725 if (visible)
4726 intel_mark_busy(dev, to_intel_framebuffer(crtc->fb)->obj);
4727}
4728
Jesse Barnes79e53942008-11-07 14:24:08 -08004729static int intel_crtc_cursor_set(struct drm_crtc *crtc,
Chris Wilson05394f32010-11-08 19:18:58 +00004730 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08004731 uint32_t handle,
4732 uint32_t width, uint32_t height)
4733{
4734 struct drm_device *dev = crtc->dev;
4735 struct drm_i915_private *dev_priv = dev->dev_private;
4736 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00004737 struct drm_i915_gem_object *obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004738 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004739 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08004740
Zhao Yakui28c97732009-10-09 11:39:41 +08004741 DRM_DEBUG_KMS("\n");
Jesse Barnes79e53942008-11-07 14:24:08 -08004742
4743 /* if we want to turn off the cursor ignore width and height */
4744 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08004745 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004746 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00004747 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10004748 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004749 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08004750 }
4751
4752 /* Currently we only support 64x64 cursors */
4753 if (width != 64 || height != 64) {
4754 DRM_ERROR("we currently only support 64x64 cursors\n");
4755 return -EINVAL;
4756 }
4757
Chris Wilson05394f32010-11-08 19:18:58 +00004758 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
4759 if (!obj)
Jesse Barnes79e53942008-11-07 14:24:08 -08004760 return -ENOENT;
4761
Chris Wilson05394f32010-11-08 19:18:58 +00004762 if (obj->base.size < width * height * 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004763 DRM_ERROR("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10004764 ret = -ENOMEM;
4765 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08004766 }
4767
Dave Airlie71acb5e2008-12-30 20:31:46 +10004768 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05004769 mutex_lock(&dev->struct_mutex);
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05004770 if (!dev_priv->info->cursor_needs_physical) {
Chris Wilsond9e86c02010-11-10 16:40:20 +00004771 if (obj->tiling_mode) {
4772 DRM_ERROR("cursor cannot be tiled\n");
4773 ret = -EINVAL;
4774 goto fail_locked;
4775 }
4776
Chris Wilson05394f32010-11-08 19:18:58 +00004777 ret = i915_gem_object_pin(obj, PAGE_SIZE, true);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004778 if (ret) {
4779 DRM_ERROR("failed to pin cursor bo\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05004780 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004781 }
Chris Wilsone7b526b2010-06-02 08:30:48 +01004782
Chris Wilson05394f32010-11-08 19:18:58 +00004783 ret = i915_gem_object_set_to_gtt_domain(obj, 0);
Chris Wilsone7b526b2010-06-02 08:30:48 +01004784 if (ret) {
4785 DRM_ERROR("failed to move cursor bo into the GTT\n");
4786 goto fail_unpin;
4787 }
4788
Chris Wilsond9e86c02010-11-10 16:40:20 +00004789 ret = i915_gem_object_put_fence(obj);
4790 if (ret) {
4791 DRM_ERROR("failed to move cursor bo into the GTT\n");
4792 goto fail_unpin;
4793 }
4794
Chris Wilson05394f32010-11-08 19:18:58 +00004795 addr = obj->gtt_offset;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004796 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004797 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson05394f32010-11-08 19:18:58 +00004798 ret = i915_gem_attach_phys_object(dev, obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004799 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
4800 align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004801 if (ret) {
4802 DRM_ERROR("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05004803 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004804 }
Chris Wilson05394f32010-11-08 19:18:58 +00004805 addr = obj->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004806 }
4807
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004808 if (IS_GEN2(dev))
Jesse Barnes14b60392009-05-20 16:47:08 -04004809 I915_WRITE(CURSIZE, (height << 12) | width);
4810
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004811 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004812 if (intel_crtc->cursor_bo) {
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05004813 if (dev_priv->info->cursor_needs_physical) {
Chris Wilson05394f32010-11-08 19:18:58 +00004814 if (intel_crtc->cursor_bo != obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004815 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
4816 } else
4817 i915_gem_object_unpin(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00004818 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004819 }
Jesse Barnes80824002009-09-10 15:28:06 -07004820
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05004821 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004822
4823 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00004824 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004825 intel_crtc->cursor_width = width;
4826 intel_crtc->cursor_height = height;
4827
Chris Wilson6b383a72010-09-13 13:54:26 +01004828 intel_crtc_update_cursor(crtc, true);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004829
Jesse Barnes79e53942008-11-07 14:24:08 -08004830 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01004831fail_unpin:
Chris Wilson05394f32010-11-08 19:18:58 +00004832 i915_gem_object_unpin(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05004833fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10004834 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00004835fail:
Chris Wilson05394f32010-11-08 19:18:58 +00004836 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10004837 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08004838}
4839
4840static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
4841{
Jesse Barnes79e53942008-11-07 14:24:08 -08004842 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08004843
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004844 intel_crtc->cursor_x = x;
4845 intel_crtc->cursor_y = y;
Jesse Barnes652c3932009-08-17 13:31:43 -07004846
Chris Wilson6b383a72010-09-13 13:54:26 +01004847 intel_crtc_update_cursor(crtc, true);
Jesse Barnes79e53942008-11-07 14:24:08 -08004848
4849 return 0;
4850}
4851
4852/** Sets the color ramps on behalf of RandR */
4853void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
4854 u16 blue, int regno)
4855{
4856 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4857
4858 intel_crtc->lut_r[regno] = red >> 8;
4859 intel_crtc->lut_g[regno] = green >> 8;
4860 intel_crtc->lut_b[regno] = blue >> 8;
4861}
4862
Dave Airlieb8c00ac2009-10-06 13:54:01 +10004863void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
4864 u16 *blue, int regno)
4865{
4866 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4867
4868 *red = intel_crtc->lut_r[regno] << 8;
4869 *green = intel_crtc->lut_g[regno] << 8;
4870 *blue = intel_crtc->lut_b[regno] << 8;
4871}
4872
Jesse Barnes79e53942008-11-07 14:24:08 -08004873static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01004874 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08004875{
James Simmons72034252010-08-03 01:33:19 +01004876 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08004877 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08004878
James Simmons72034252010-08-03 01:33:19 +01004879 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004880 intel_crtc->lut_r[i] = red[i] >> 8;
4881 intel_crtc->lut_g[i] = green[i] >> 8;
4882 intel_crtc->lut_b[i] = blue[i] >> 8;
4883 }
4884
4885 intel_crtc_load_lut(crtc);
4886}
4887
4888/**
4889 * Get a pipe with a simple mode set on it for doing load-based monitor
4890 * detection.
4891 *
4892 * It will be up to the load-detect code to adjust the pipe as appropriate for
Eric Anholtc751ce42010-03-25 11:48:48 -07004893 * its requirements. The pipe will be connected to no other encoders.
Jesse Barnes79e53942008-11-07 14:24:08 -08004894 *
Eric Anholtc751ce42010-03-25 11:48:48 -07004895 * Currently this code will only succeed if there is a pipe with no encoders
Jesse Barnes79e53942008-11-07 14:24:08 -08004896 * configured for it. In the future, it could choose to temporarily disable
4897 * some outputs to free up a pipe for its use.
4898 *
4899 * \return crtc, or NULL if no pipes are available.
4900 */
4901
4902/* VESA 640x480x72Hz mode to set on the pipe */
4903static struct drm_display_mode load_detect_mode = {
4904 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
4905 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
4906};
4907
Eric Anholt21d40d32010-03-25 11:11:14 -07004908struct drm_crtc *intel_get_load_detect_pipe(struct intel_encoder *intel_encoder,
Zhenyu Wangc1c43972010-03-30 14:39:30 +08004909 struct drm_connector *connector,
Jesse Barnes79e53942008-11-07 14:24:08 -08004910 struct drm_display_mode *mode,
4911 int *dpms_mode)
4912{
4913 struct intel_crtc *intel_crtc;
4914 struct drm_crtc *possible_crtc;
4915 struct drm_crtc *supported_crtc =NULL;
Chris Wilson4ef69c72010-09-09 15:14:28 +01004916 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08004917 struct drm_crtc *crtc = NULL;
4918 struct drm_device *dev = encoder->dev;
4919 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
4920 struct drm_crtc_helper_funcs *crtc_funcs;
4921 int i = -1;
4922
4923 /*
4924 * Algorithm gets a little messy:
4925 * - if the connector already has an assigned crtc, use it (but make
4926 * sure it's on first)
4927 * - try to find the first unused crtc that can drive this connector,
4928 * and use that if we find one
4929 * - if there are no unused crtcs available, try to use the first
4930 * one we found that supports the connector
4931 */
4932
4933 /* See if we already have a CRTC for this connector */
4934 if (encoder->crtc) {
4935 crtc = encoder->crtc;
4936 /* Make sure the crtc and connector are running */
4937 intel_crtc = to_intel_crtc(crtc);
4938 *dpms_mode = intel_crtc->dpms_mode;
4939 if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
4940 crtc_funcs = crtc->helper_private;
4941 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
4942 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
4943 }
4944 return crtc;
4945 }
4946
4947 /* Find an unused one (if possible) */
4948 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
4949 i++;
4950 if (!(encoder->possible_crtcs & (1 << i)))
4951 continue;
4952 if (!possible_crtc->enabled) {
4953 crtc = possible_crtc;
4954 break;
4955 }
4956 if (!supported_crtc)
4957 supported_crtc = possible_crtc;
4958 }
4959
4960 /*
4961 * If we didn't find an unused CRTC, don't use any.
4962 */
4963 if (!crtc) {
4964 return NULL;
4965 }
4966
4967 encoder->crtc = crtc;
Zhenyu Wangc1c43972010-03-30 14:39:30 +08004968 connector->encoder = encoder;
Eric Anholt21d40d32010-03-25 11:11:14 -07004969 intel_encoder->load_detect_temp = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08004970
4971 intel_crtc = to_intel_crtc(crtc);
4972 *dpms_mode = intel_crtc->dpms_mode;
4973
4974 if (!crtc->enabled) {
4975 if (!mode)
4976 mode = &load_detect_mode;
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05004977 drm_crtc_helper_set_mode(crtc, mode, 0, 0, crtc->fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08004978 } else {
4979 if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
4980 crtc_funcs = crtc->helper_private;
4981 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
4982 }
4983
4984 /* Add this connector to the crtc */
4985 encoder_funcs->mode_set(encoder, &crtc->mode, &crtc->mode);
4986 encoder_funcs->commit(encoder);
4987 }
4988 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07004989 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08004990
4991 return crtc;
4992}
4993
Zhenyu Wangc1c43972010-03-30 14:39:30 +08004994void intel_release_load_detect_pipe(struct intel_encoder *intel_encoder,
4995 struct drm_connector *connector, int dpms_mode)
Jesse Barnes79e53942008-11-07 14:24:08 -08004996{
Chris Wilson4ef69c72010-09-09 15:14:28 +01004997 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08004998 struct drm_device *dev = encoder->dev;
4999 struct drm_crtc *crtc = encoder->crtc;
5000 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
5001 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
5002
Eric Anholt21d40d32010-03-25 11:11:14 -07005003 if (intel_encoder->load_detect_temp) {
Jesse Barnes79e53942008-11-07 14:24:08 -08005004 encoder->crtc = NULL;
Zhenyu Wangc1c43972010-03-30 14:39:30 +08005005 connector->encoder = NULL;
Eric Anholt21d40d32010-03-25 11:11:14 -07005006 intel_encoder->load_detect_temp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08005007 crtc->enabled = drm_helper_crtc_in_use(crtc);
5008 drm_helper_disable_unused_functions(dev);
5009 }
5010
Eric Anholtc751ce42010-03-25 11:48:48 -07005011 /* Switch crtc and encoder back off if necessary */
Jesse Barnes79e53942008-11-07 14:24:08 -08005012 if (crtc->enabled && dpms_mode != DRM_MODE_DPMS_ON) {
5013 if (encoder->crtc == crtc)
5014 encoder_funcs->dpms(encoder, dpms_mode);
5015 crtc_funcs->dpms(crtc, dpms_mode);
5016 }
5017}
5018
5019/* Returns the clock of the currently programmed mode of the given pipe. */
5020static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
5021{
5022 struct drm_i915_private *dev_priv = dev->dev_private;
5023 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5024 int pipe = intel_crtc->pipe;
5025 u32 dpll = I915_READ((pipe == 0) ? DPLL_A : DPLL_B);
5026 u32 fp;
5027 intel_clock_t clock;
5028
5029 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
5030 fp = I915_READ((pipe == 0) ? FPA0 : FPB0);
5031 else
5032 fp = I915_READ((pipe == 0) ? FPA1 : FPB1);
5033
5034 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05005035 if (IS_PINEVIEW(dev)) {
5036 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
5037 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08005038 } else {
5039 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
5040 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
5041 }
5042
Chris Wilsona6c45cf2010-09-17 00:32:17 +01005043 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05005044 if (IS_PINEVIEW(dev))
5045 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
5046 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08005047 else
5048 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08005049 DPLL_FPA01_P1_POST_DIV_SHIFT);
5050
5051 switch (dpll & DPLL_MODE_MASK) {
5052 case DPLLB_MODE_DAC_SERIAL:
5053 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
5054 5 : 10;
5055 break;
5056 case DPLLB_MODE_LVDS:
5057 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
5058 7 : 14;
5059 break;
5060 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08005061 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08005062 "mode\n", (int)(dpll & DPLL_MODE_MASK));
5063 return 0;
5064 }
5065
5066 /* XXX: Handle the 100Mhz refclk */
Shaohua Li21778322009-02-23 15:19:16 +08005067 intel_clock(dev, 96000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08005068 } else {
5069 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
5070
5071 if (is_lvds) {
5072 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
5073 DPLL_FPA01_P1_POST_DIV_SHIFT);
5074 clock.p2 = 14;
5075
5076 if ((dpll & PLL_REF_INPUT_MASK) ==
5077 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
5078 /* XXX: might not be 66MHz */
Shaohua Li21778322009-02-23 15:19:16 +08005079 intel_clock(dev, 66000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08005080 } else
Shaohua Li21778322009-02-23 15:19:16 +08005081 intel_clock(dev, 48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08005082 } else {
5083 if (dpll & PLL_P1_DIVIDE_BY_TWO)
5084 clock.p1 = 2;
5085 else {
5086 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
5087 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
5088 }
5089 if (dpll & PLL_P2_DIVIDE_BY_4)
5090 clock.p2 = 4;
5091 else
5092 clock.p2 = 2;
5093
Shaohua Li21778322009-02-23 15:19:16 +08005094 intel_clock(dev, 48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08005095 }
5096 }
5097
5098 /* XXX: It would be nice to validate the clocks, but we can't reuse
5099 * i830PllIsValid() because it relies on the xf86_config connector
5100 * configuration being accurate, which it isn't necessarily.
5101 */
5102
5103 return clock.dot;
5104}
5105
5106/** Returns the currently programmed mode of the given pipe. */
5107struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
5108 struct drm_crtc *crtc)
5109{
5110 struct drm_i915_private *dev_priv = dev->dev_private;
5111 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5112 int pipe = intel_crtc->pipe;
5113 struct drm_display_mode *mode;
5114 int htot = I915_READ((pipe == 0) ? HTOTAL_A : HTOTAL_B);
5115 int hsync = I915_READ((pipe == 0) ? HSYNC_A : HSYNC_B);
5116 int vtot = I915_READ((pipe == 0) ? VTOTAL_A : VTOTAL_B);
5117 int vsync = I915_READ((pipe == 0) ? VSYNC_A : VSYNC_B);
5118
5119 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
5120 if (!mode)
5121 return NULL;
5122
5123 mode->clock = intel_crtc_clock_get(dev, crtc);
5124 mode->hdisplay = (htot & 0xffff) + 1;
5125 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
5126 mode->hsync_start = (hsync & 0xffff) + 1;
5127 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
5128 mode->vdisplay = (vtot & 0xffff) + 1;
5129 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
5130 mode->vsync_start = (vsync & 0xffff) + 1;
5131 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
5132
5133 drm_mode_set_name(mode);
5134 drm_mode_set_crtcinfo(mode, 0);
5135
5136 return mode;
5137}
5138
Jesse Barnes652c3932009-08-17 13:31:43 -07005139#define GPU_IDLE_TIMEOUT 500 /* ms */
5140
5141/* When this timer fires, we've been idle for awhile */
5142static void intel_gpu_idle_timer(unsigned long arg)
5143{
5144 struct drm_device *dev = (struct drm_device *)arg;
5145 drm_i915_private_t *dev_priv = dev->dev_private;
5146
Chris Wilsonff7ea4c2010-12-08 09:43:41 +00005147 if (!list_empty(&dev_priv->mm.active_list)) {
5148 /* Still processing requests, so just re-arm the timer. */
5149 mod_timer(&dev_priv->idle_timer, jiffies +
5150 msecs_to_jiffies(GPU_IDLE_TIMEOUT));
5151 return;
5152 }
Jesse Barnes652c3932009-08-17 13:31:43 -07005153
Chris Wilsonff7ea4c2010-12-08 09:43:41 +00005154 dev_priv->busy = false;
Eric Anholt01dfba92009-09-06 15:18:53 -07005155 queue_work(dev_priv->wq, &dev_priv->idle_work);
Jesse Barnes652c3932009-08-17 13:31:43 -07005156}
5157
Jesse Barnes652c3932009-08-17 13:31:43 -07005158#define CRTC_IDLE_TIMEOUT 1000 /* ms */
5159
5160static void intel_crtc_idle_timer(unsigned long arg)
5161{
5162 struct intel_crtc *intel_crtc = (struct intel_crtc *)arg;
5163 struct drm_crtc *crtc = &intel_crtc->base;
5164 drm_i915_private_t *dev_priv = crtc->dev->dev_private;
Chris Wilsonff7ea4c2010-12-08 09:43:41 +00005165 struct intel_framebuffer *intel_fb;
5166
5167 intel_fb = to_intel_framebuffer(crtc->fb);
5168 if (intel_fb && intel_fb->obj->active) {
5169 /* The framebuffer is still being accessed by the GPU. */
5170 mod_timer(&intel_crtc->idle_timer, jiffies +
5171 msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
5172 return;
5173 }
Jesse Barnes652c3932009-08-17 13:31:43 -07005174
Jesse Barnes652c3932009-08-17 13:31:43 -07005175 intel_crtc->busy = false;
Eric Anholt01dfba92009-09-06 15:18:53 -07005176 queue_work(dev_priv->wq, &dev_priv->idle_work);
Jesse Barnes652c3932009-08-17 13:31:43 -07005177}
5178
Daniel Vetter3dec0092010-08-20 21:40:52 +02005179static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07005180{
5181 struct drm_device *dev = crtc->dev;
5182 drm_i915_private_t *dev_priv = dev->dev_private;
5183 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5184 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08005185 int dpll_reg = DPLL(pipe);
5186 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07005187
Eric Anholtbad720f2009-10-22 16:11:14 -07005188 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07005189 return;
5190
5191 if (!dev_priv->lvds_downclock_avail)
5192 return;
5193
Jesse Barnesdbdc6472010-12-30 09:36:39 -08005194 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07005195 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08005196 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07005197
5198 /* Unlock panel regs */
Jesse Barnesdbdc6472010-12-30 09:36:39 -08005199 I915_WRITE(PP_CONTROL,
5200 I915_READ(PP_CONTROL) | PANEL_UNLOCK_REGS);
Jesse Barnes652c3932009-08-17 13:31:43 -07005201
5202 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
5203 I915_WRITE(dpll_reg, dpll);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08005204 POSTING_READ(dpll_reg);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07005205 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08005206
Jesse Barnes652c3932009-08-17 13:31:43 -07005207 dpll = I915_READ(dpll_reg);
5208 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08005209 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07005210
5211 /* ...and lock them again */
5212 I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
5213 }
5214
5215 /* Schedule downclock */
Daniel Vetter3dec0092010-08-20 21:40:52 +02005216 mod_timer(&intel_crtc->idle_timer, jiffies +
5217 msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
Jesse Barnes652c3932009-08-17 13:31:43 -07005218}
5219
5220static void intel_decrease_pllclock(struct drm_crtc *crtc)
5221{
5222 struct drm_device *dev = crtc->dev;
5223 drm_i915_private_t *dev_priv = dev->dev_private;
5224 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5225 int pipe = intel_crtc->pipe;
5226 int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
5227 int dpll = I915_READ(dpll_reg);
5228
Eric Anholtbad720f2009-10-22 16:11:14 -07005229 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07005230 return;
5231
5232 if (!dev_priv->lvds_downclock_avail)
5233 return;
5234
5235 /*
5236 * Since this is called by a timer, we should never get here in
5237 * the manual case.
5238 */
5239 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08005240 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07005241
5242 /* Unlock panel regs */
Jesse Barnes4a655f02010-07-22 13:18:18 -07005243 I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) |
5244 PANEL_UNLOCK_REGS);
Jesse Barnes652c3932009-08-17 13:31:43 -07005245
5246 dpll |= DISPLAY_RATE_SELECT_FPA1;
5247 I915_WRITE(dpll_reg, dpll);
5248 dpll = I915_READ(dpll_reg);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07005249 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07005250 dpll = I915_READ(dpll_reg);
5251 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08005252 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07005253
5254 /* ...and lock them again */
5255 I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
5256 }
5257
5258}
5259
5260/**
5261 * intel_idle_update - adjust clocks for idleness
5262 * @work: work struct
5263 *
5264 * Either the GPU or display (or both) went idle. Check the busy status
5265 * here and adjust the CRTC and GPU clocks as necessary.
5266 */
5267static void intel_idle_update(struct work_struct *work)
5268{
5269 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
5270 idle_work);
5271 struct drm_device *dev = dev_priv->dev;
5272 struct drm_crtc *crtc;
5273 struct intel_crtc *intel_crtc;
Li Peng45ac22c2010-06-12 23:38:35 +08005274 int enabled = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07005275
5276 if (!i915_powersave)
5277 return;
5278
5279 mutex_lock(&dev->struct_mutex);
5280
Jesse Barnes7648fa92010-05-20 14:28:11 -07005281 i915_update_gfx_val(dev_priv);
5282
Jesse Barnes652c3932009-08-17 13:31:43 -07005283 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
5284 /* Skip inactive CRTCs */
5285 if (!crtc->fb)
5286 continue;
5287
Li Peng45ac22c2010-06-12 23:38:35 +08005288 enabled++;
Jesse Barnes652c3932009-08-17 13:31:43 -07005289 intel_crtc = to_intel_crtc(crtc);
5290 if (!intel_crtc->busy)
5291 intel_decrease_pllclock(crtc);
5292 }
5293
Li Peng45ac22c2010-06-12 23:38:35 +08005294 if ((enabled == 1) && (IS_I945G(dev) || IS_I945GM(dev))) {
5295 DRM_DEBUG_DRIVER("enable memory self refresh on 945\n");
5296 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
5297 }
5298
Jesse Barnes652c3932009-08-17 13:31:43 -07005299 mutex_unlock(&dev->struct_mutex);
5300}
5301
5302/**
5303 * intel_mark_busy - mark the GPU and possibly the display busy
5304 * @dev: drm device
5305 * @obj: object we're operating on
5306 *
5307 * Callers can use this function to indicate that the GPU is busy processing
5308 * commands. If @obj matches one of the CRTC objects (i.e. it's a scanout
5309 * buffer), we'll also mark the display as busy, so we know to increase its
5310 * clock frequency.
5311 */
Chris Wilson05394f32010-11-08 19:18:58 +00005312void intel_mark_busy(struct drm_device *dev, struct drm_i915_gem_object *obj)
Jesse Barnes652c3932009-08-17 13:31:43 -07005313{
5314 drm_i915_private_t *dev_priv = dev->dev_private;
5315 struct drm_crtc *crtc = NULL;
5316 struct intel_framebuffer *intel_fb;
5317 struct intel_crtc *intel_crtc;
5318
Zhenyu Wang5e17ee72009-09-03 09:30:06 +08005319 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5320 return;
5321
Li Peng060e6452010-02-10 01:54:24 +08005322 if (!dev_priv->busy) {
5323 if (IS_I945G(dev) || IS_I945GM(dev)) {
5324 u32 fw_blc_self;
Li Pengee980b82010-01-27 19:01:11 +08005325
Li Peng060e6452010-02-10 01:54:24 +08005326 DRM_DEBUG_DRIVER("disable memory self refresh on 945\n");
5327 fw_blc_self = I915_READ(FW_BLC_SELF);
5328 fw_blc_self &= ~FW_BLC_SELF_EN;
5329 I915_WRITE(FW_BLC_SELF, fw_blc_self | FW_BLC_SELF_EN_MASK);
5330 }
Chris Wilson28cf7982009-11-30 01:08:56 +00005331 dev_priv->busy = true;
Li Peng060e6452010-02-10 01:54:24 +08005332 } else
Chris Wilson28cf7982009-11-30 01:08:56 +00005333 mod_timer(&dev_priv->idle_timer, jiffies +
5334 msecs_to_jiffies(GPU_IDLE_TIMEOUT));
Jesse Barnes652c3932009-08-17 13:31:43 -07005335
5336 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
5337 if (!crtc->fb)
5338 continue;
5339
5340 intel_crtc = to_intel_crtc(crtc);
5341 intel_fb = to_intel_framebuffer(crtc->fb);
5342 if (intel_fb->obj == obj) {
5343 if (!intel_crtc->busy) {
Li Peng060e6452010-02-10 01:54:24 +08005344 if (IS_I945G(dev) || IS_I945GM(dev)) {
5345 u32 fw_blc_self;
5346
5347 DRM_DEBUG_DRIVER("disable memory self refresh on 945\n");
5348 fw_blc_self = I915_READ(FW_BLC_SELF);
5349 fw_blc_self &= ~FW_BLC_SELF_EN;
5350 I915_WRITE(FW_BLC_SELF, fw_blc_self | FW_BLC_SELF_EN_MASK);
5351 }
Jesse Barnes652c3932009-08-17 13:31:43 -07005352 /* Non-busy -> busy, upclock */
Daniel Vetter3dec0092010-08-20 21:40:52 +02005353 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07005354 intel_crtc->busy = true;
5355 } else {
5356 /* Busy -> busy, put off timer */
5357 mod_timer(&intel_crtc->idle_timer, jiffies +
5358 msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
5359 }
5360 }
5361 }
5362}
5363
Jesse Barnes79e53942008-11-07 14:24:08 -08005364static void intel_crtc_destroy(struct drm_crtc *crtc)
5365{
5366 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02005367 struct drm_device *dev = crtc->dev;
5368 struct intel_unpin_work *work;
5369 unsigned long flags;
5370
5371 spin_lock_irqsave(&dev->event_lock, flags);
5372 work = intel_crtc->unpin_work;
5373 intel_crtc->unpin_work = NULL;
5374 spin_unlock_irqrestore(&dev->event_lock, flags);
5375
5376 if (work) {
5377 cancel_work_sync(&work->work);
5378 kfree(work);
5379 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005380
5381 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02005382
Jesse Barnes79e53942008-11-07 14:24:08 -08005383 kfree(intel_crtc);
5384}
5385
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005386static void intel_unpin_work_fn(struct work_struct *__work)
5387{
5388 struct intel_unpin_work *work =
5389 container_of(__work, struct intel_unpin_work, work);
5390
5391 mutex_lock(&work->dev->struct_mutex);
Jesse Barnesb1b87f62010-01-26 14:40:05 -08005392 i915_gem_object_unpin(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00005393 drm_gem_object_unreference(&work->pending_flip_obj->base);
5394 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00005395
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005396 mutex_unlock(&work->dev->struct_mutex);
5397 kfree(work);
5398}
5399
Jesse Barnes1afe3e92010-03-26 10:35:20 -07005400static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01005401 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005402{
5403 drm_i915_private_t *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005404 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5405 struct intel_unpin_work *work;
Chris Wilson05394f32010-11-08 19:18:58 +00005406 struct drm_i915_gem_object *obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005407 struct drm_pending_vblank_event *e;
Mario Kleiner49b14a52010-12-09 07:00:07 +01005408 struct timeval tnow, tvbl;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005409 unsigned long flags;
5410
5411 /* Ignore early vblank irqs */
5412 if (intel_crtc == NULL)
5413 return;
5414
Mario Kleiner49b14a52010-12-09 07:00:07 +01005415 do_gettimeofday(&tnow);
5416
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005417 spin_lock_irqsave(&dev->event_lock, flags);
5418 work = intel_crtc->unpin_work;
5419 if (work == NULL || !work->pending) {
5420 spin_unlock_irqrestore(&dev->event_lock, flags);
5421 return;
5422 }
5423
5424 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005425
5426 if (work->event) {
5427 e = work->event;
Mario Kleiner49b14a52010-12-09 07:00:07 +01005428 e->event.sequence = drm_vblank_count_and_time(dev, intel_crtc->pipe, &tvbl);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01005429
5430 /* Called before vblank count and timestamps have
5431 * been updated for the vblank interval of flip
5432 * completion? Need to increment vblank count and
5433 * add one videorefresh duration to returned timestamp
Mario Kleiner49b14a52010-12-09 07:00:07 +01005434 * to account for this. We assume this happened if we
5435 * get called over 0.9 frame durations after the last
5436 * timestamped vblank.
5437 *
5438 * This calculation can not be used with vrefresh rates
5439 * below 5Hz (10Hz to be on the safe side) without
5440 * promoting to 64 integers.
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01005441 */
Mario Kleiner49b14a52010-12-09 07:00:07 +01005442 if (10 * (timeval_to_ns(&tnow) - timeval_to_ns(&tvbl)) >
5443 9 * crtc->framedur_ns) {
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01005444 e->event.sequence++;
Mario Kleiner49b14a52010-12-09 07:00:07 +01005445 tvbl = ns_to_timeval(timeval_to_ns(&tvbl) +
5446 crtc->framedur_ns);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01005447 }
5448
Mario Kleiner49b14a52010-12-09 07:00:07 +01005449 e->event.tv_sec = tvbl.tv_sec;
5450 e->event.tv_usec = tvbl.tv_usec;
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01005451
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005452 list_add_tail(&e->base.link,
5453 &e->base.file_priv->event_list);
5454 wake_up_interruptible(&e->base.file_priv->event_wait);
5455 }
5456
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01005457 drm_vblank_put(dev, intel_crtc->pipe);
5458
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005459 spin_unlock_irqrestore(&dev->event_lock, flags);
5460
Chris Wilson05394f32010-11-08 19:18:58 +00005461 obj = work->old_fb_obj;
Chris Wilsond9e86c02010-11-10 16:40:20 +00005462
Chris Wilsone59f2ba2010-10-07 17:28:15 +01005463 atomic_clear_mask(1 << intel_crtc->plane,
Chris Wilson05394f32010-11-08 19:18:58 +00005464 &obj->pending_flip.counter);
5465 if (atomic_read(&obj->pending_flip) == 0)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005466 wake_up(&dev_priv->pending_flip_queue);
Chris Wilsond9e86c02010-11-10 16:40:20 +00005467
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005468 schedule_work(&work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07005469
5470 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005471}
5472
Jesse Barnes1afe3e92010-03-26 10:35:20 -07005473void intel_finish_page_flip(struct drm_device *dev, int pipe)
5474{
5475 drm_i915_private_t *dev_priv = dev->dev_private;
5476 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
5477
Mario Kleiner49b14a52010-12-09 07:00:07 +01005478 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07005479}
5480
5481void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
5482{
5483 drm_i915_private_t *dev_priv = dev->dev_private;
5484 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
5485
Mario Kleiner49b14a52010-12-09 07:00:07 +01005486 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07005487}
5488
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005489void intel_prepare_page_flip(struct drm_device *dev, int plane)
5490{
5491 drm_i915_private_t *dev_priv = dev->dev_private;
5492 struct intel_crtc *intel_crtc =
5493 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
5494 unsigned long flags;
5495
5496 spin_lock_irqsave(&dev->event_lock, flags);
Jesse Barnesde3f4402010-01-14 13:18:02 -08005497 if (intel_crtc->unpin_work) {
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01005498 if ((++intel_crtc->unpin_work->pending) > 1)
5499 DRM_ERROR("Prepared flip multiple times\n");
Jesse Barnesde3f4402010-01-14 13:18:02 -08005500 } else {
5501 DRM_DEBUG_DRIVER("preparing flip with no unpin work?\n");
5502 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005503 spin_unlock_irqrestore(&dev->event_lock, flags);
5504}
5505
5506static int intel_crtc_page_flip(struct drm_crtc *crtc,
5507 struct drm_framebuffer *fb,
5508 struct drm_pending_vblank_event *event)
5509{
5510 struct drm_device *dev = crtc->dev;
5511 struct drm_i915_private *dev_priv = dev->dev_private;
5512 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00005513 struct drm_i915_gem_object *obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005514 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5515 struct intel_unpin_work *work;
Jesse Barnesbe9a3db2010-07-23 12:03:37 -07005516 unsigned long flags, offset;
Chris Wilson52e68632010-08-08 10:15:59 +01005517 int pipe = intel_crtc->pipe;
Chris Wilson20f0cd52010-09-23 11:00:38 +01005518 u32 pf, pipesrc;
Chris Wilson52e68632010-08-08 10:15:59 +01005519 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005520
5521 work = kzalloc(sizeof *work, GFP_KERNEL);
5522 if (work == NULL)
5523 return -ENOMEM;
5524
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005525 work->event = event;
5526 work->dev = crtc->dev;
5527 intel_fb = to_intel_framebuffer(crtc->fb);
Jesse Barnesb1b87f62010-01-26 14:40:05 -08005528 work->old_fb_obj = intel_fb->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005529 INIT_WORK(&work->work, intel_unpin_work_fn);
5530
5531 /* We borrow the event spin lock for protecting unpin_work */
5532 spin_lock_irqsave(&dev->event_lock, flags);
5533 if (intel_crtc->unpin_work) {
5534 spin_unlock_irqrestore(&dev->event_lock, flags);
5535 kfree(work);
Chris Wilson468f0b42010-05-27 13:18:13 +01005536
5537 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005538 return -EBUSY;
5539 }
5540 intel_crtc->unpin_work = work;
5541 spin_unlock_irqrestore(&dev->event_lock, flags);
5542
5543 intel_fb = to_intel_framebuffer(fb);
5544 obj = intel_fb->obj;
5545
Chris Wilson468f0b42010-05-27 13:18:13 +01005546 mutex_lock(&dev->struct_mutex);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00005547 ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
Chris Wilson96b099f2010-06-07 14:03:04 +01005548 if (ret)
5549 goto cleanup_work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005550
Jesse Barnes75dfca80a2010-02-10 15:09:44 -08005551 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00005552 drm_gem_object_reference(&work->old_fb_obj->base);
5553 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005554
5555 crtc->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01005556
5557 ret = drm_vblank_get(dev, intel_crtc->pipe);
5558 if (ret)
5559 goto cleanup_objs;
5560
Chris Wilsonc7f9f9a2010-09-19 15:05:13 +01005561 if (IS_GEN3(dev) || IS_GEN2(dev)) {
5562 u32 flip_mask;
5563
5564 /* Can't queue multiple flips, so wait for the previous
5565 * one to finish before executing the next.
5566 */
Chris Wilsone1f99ce2010-10-27 12:45:26 +01005567 ret = BEGIN_LP_RING(2);
5568 if (ret)
5569 goto cleanup_objs;
5570
Chris Wilsonc7f9f9a2010-09-19 15:05:13 +01005571 if (intel_crtc->plane)
5572 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
5573 else
5574 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
5575 OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);
5576 OUT_RING(MI_NOOP);
Daniel Vetter6146b3d2010-08-04 21:22:10 +02005577 ADVANCE_LP_RING();
5578 }
Jesse Barnes83f7fd02010-04-05 14:03:51 -07005579
Chris Wilsone1f99ce2010-10-27 12:45:26 +01005580 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01005581
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01005582 work->enable_stall_check = true;
5583
Jesse Barnesbe9a3db2010-07-23 12:03:37 -07005584 /* Offset into the new buffer for cases of shared fbs between CRTCs */
Chris Wilson52e68632010-08-08 10:15:59 +01005585 offset = crtc->y * fb->pitch + crtc->x * fb->bits_per_pixel/8;
Jesse Barnesbe9a3db2010-07-23 12:03:37 -07005586
Chris Wilsone1f99ce2010-10-27 12:45:26 +01005587 ret = BEGIN_LP_RING(4);
5588 if (ret)
5589 goto cleanup_objs;
5590
5591 /* Block clients from rendering to the new back buffer until
5592 * the flip occurs and the object is no longer visible.
5593 */
Chris Wilson05394f32010-11-08 19:18:58 +00005594 atomic_add(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01005595
5596 switch (INTEL_INFO(dev)->gen) {
Chris Wilson52e68632010-08-08 10:15:59 +01005597 case 2:
Jesse Barnes1afe3e92010-03-26 10:35:20 -07005598 OUT_RING(MI_DISPLAY_FLIP |
5599 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
5600 OUT_RING(fb->pitch);
Chris Wilson05394f32010-11-08 19:18:58 +00005601 OUT_RING(obj->gtt_offset + offset);
Chris Wilson52e68632010-08-08 10:15:59 +01005602 OUT_RING(MI_NOOP);
5603 break;
5604
5605 case 3:
Jesse Barnes1afe3e92010-03-26 10:35:20 -07005606 OUT_RING(MI_DISPLAY_FLIP_I915 |
5607 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
5608 OUT_RING(fb->pitch);
Chris Wilson05394f32010-11-08 19:18:58 +00005609 OUT_RING(obj->gtt_offset + offset);
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08005610 OUT_RING(MI_NOOP);
Chris Wilson52e68632010-08-08 10:15:59 +01005611 break;
5612
5613 case 4:
5614 case 5:
5615 /* i965+ uses the linear or tiled offsets from the
5616 * Display Registers (which do not change across a page-flip)
5617 * so we need only reprogram the base address.
5618 */
Daniel Vetter69d0b962010-08-04 21:22:09 +02005619 OUT_RING(MI_DISPLAY_FLIP |
5620 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
5621 OUT_RING(fb->pitch);
Chris Wilson05394f32010-11-08 19:18:58 +00005622 OUT_RING(obj->gtt_offset | obj->tiling_mode);
Chris Wilson52e68632010-08-08 10:15:59 +01005623
5624 /* XXX Enabling the panel-fitter across page-flip is so far
5625 * untested on non-native modes, so ignore it for now.
5626 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
5627 */
5628 pf = 0;
5629 pipesrc = I915_READ(pipe == 0 ? PIPEASRC : PIPEBSRC) & 0x0fff0fff;
5630 OUT_RING(pf | pipesrc);
5631 break;
5632
5633 case 6:
5634 OUT_RING(MI_DISPLAY_FLIP |
5635 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Chris Wilson05394f32010-11-08 19:18:58 +00005636 OUT_RING(fb->pitch | obj->tiling_mode);
5637 OUT_RING(obj->gtt_offset);
Chris Wilson52e68632010-08-08 10:15:59 +01005638
5639 pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
5640 pipesrc = I915_READ(pipe == 0 ? PIPEASRC : PIPEBSRC) & 0x0fff0fff;
5641 OUT_RING(pf | pipesrc);
5642 break;
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08005643 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005644 ADVANCE_LP_RING();
5645
5646 mutex_unlock(&dev->struct_mutex);
5647
Jesse Barnese5510fa2010-07-01 16:48:37 -07005648 trace_i915_flip_request(intel_crtc->plane, obj);
5649
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005650 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01005651
5652cleanup_objs:
Chris Wilson05394f32010-11-08 19:18:58 +00005653 drm_gem_object_unreference(&work->old_fb_obj->base);
5654 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01005655cleanup_work:
5656 mutex_unlock(&dev->struct_mutex);
5657
5658 spin_lock_irqsave(&dev->event_lock, flags);
5659 intel_crtc->unpin_work = NULL;
5660 spin_unlock_irqrestore(&dev->event_lock, flags);
5661
5662 kfree(work);
5663
5664 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005665}
5666
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07005667static struct drm_crtc_helper_funcs intel_helper_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -08005668 .dpms = intel_crtc_dpms,
5669 .mode_fixup = intel_crtc_mode_fixup,
5670 .mode_set = intel_crtc_mode_set,
5671 .mode_set_base = intel_pipe_set_base,
Jesse Barnes81255562010-08-02 12:07:50 -07005672 .mode_set_base_atomic = intel_pipe_set_base_atomic,
Dave Airlie068143d2009-10-05 09:58:02 +10005673 .load_lut = intel_crtc_load_lut,
Chris Wilsoncdd59982010-09-08 16:30:16 +01005674 .disable = intel_crtc_disable,
Jesse Barnes79e53942008-11-07 14:24:08 -08005675};
5676
5677static const struct drm_crtc_funcs intel_crtc_funcs = {
5678 .cursor_set = intel_crtc_cursor_set,
5679 .cursor_move = intel_crtc_cursor_move,
5680 .gamma_set = intel_crtc_gamma_set,
5681 .set_config = drm_crtc_helper_set_config,
5682 .destroy = intel_crtc_destroy,
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005683 .page_flip = intel_crtc_page_flip,
Jesse Barnes79e53942008-11-07 14:24:08 -08005684};
5685
Chris Wilson47f1c6c2010-12-03 15:37:31 +00005686static void intel_sanitize_modesetting(struct drm_device *dev,
5687 int pipe, int plane)
5688{
5689 struct drm_i915_private *dev_priv = dev->dev_private;
5690 u32 reg, val;
5691
5692 if (HAS_PCH_SPLIT(dev))
5693 return;
5694
5695 /* Who knows what state these registers were left in by the BIOS or
5696 * grub?
5697 *
5698 * If we leave the registers in a conflicting state (e.g. with the
5699 * display plane reading from the other pipe than the one we intend
5700 * to use) then when we attempt to teardown the active mode, we will
5701 * not disable the pipes and planes in the correct order -- leaving
5702 * a plane reading from a disabled pipe and possibly leading to
5703 * undefined behaviour.
5704 */
5705
5706 reg = DSPCNTR(plane);
5707 val = I915_READ(reg);
5708
5709 if ((val & DISPLAY_PLANE_ENABLE) == 0)
5710 return;
5711 if (!!(val & DISPPLANE_SEL_PIPE_MASK) == pipe)
5712 return;
5713
5714 /* This display plane is active and attached to the other CPU pipe. */
5715 pipe = !pipe;
5716
5717 /* Disable the plane and wait for it to stop reading from the pipe. */
Jesse Barnesb24e7172011-01-04 15:09:30 -08005718 intel_disable_plane(dev_priv, plane, pipe);
5719 intel_disable_pipe(dev_priv, pipe);
Chris Wilson47f1c6c2010-12-03 15:37:31 +00005720}
Jesse Barnes79e53942008-11-07 14:24:08 -08005721
Hannes Ederb358d0a2008-12-18 21:18:47 +01005722static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -08005723{
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08005724 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08005725 struct intel_crtc *intel_crtc;
5726 int i;
5727
5728 intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
5729 if (intel_crtc == NULL)
5730 return;
5731
5732 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
5733
5734 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -08005735 for (i = 0; i < 256; i++) {
5736 intel_crtc->lut_r[i] = i;
5737 intel_crtc->lut_g[i] = i;
5738 intel_crtc->lut_b[i] = i;
5739 }
5740
Jesse Barnes80824002009-09-10 15:28:06 -07005741 /* Swap pipes & planes for FBC on pre-965 */
5742 intel_crtc->pipe = pipe;
5743 intel_crtc->plane = pipe;
Chris Wilsone2e767a2010-09-13 16:53:12 +01005744 if (IS_MOBILE(dev) && IS_GEN3(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08005745 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +01005746 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07005747 }
5748
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08005749 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
5750 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
5751 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
5752 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
5753
Jesse Barnes79e53942008-11-07 14:24:08 -08005754 intel_crtc->cursor_addr = 0;
Chris Wilson032d2a02010-09-06 16:17:22 +01005755 intel_crtc->dpms_mode = -1;
Chris Wilsone65d9302010-09-13 16:58:39 +01005756 intel_crtc->active = true; /* force the pipe off on setup_init_config */
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07005757
5758 if (HAS_PCH_SPLIT(dev)) {
5759 intel_helper_funcs.prepare = ironlake_crtc_prepare;
5760 intel_helper_funcs.commit = ironlake_crtc_commit;
5761 } else {
5762 intel_helper_funcs.prepare = i9xx_crtc_prepare;
5763 intel_helper_funcs.commit = i9xx_crtc_commit;
5764 }
5765
Jesse Barnes79e53942008-11-07 14:24:08 -08005766 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
5767
Jesse Barnes652c3932009-08-17 13:31:43 -07005768 intel_crtc->busy = false;
5769
5770 setup_timer(&intel_crtc->idle_timer, intel_crtc_idle_timer,
5771 (unsigned long)intel_crtc);
Chris Wilson47f1c6c2010-12-03 15:37:31 +00005772
5773 intel_sanitize_modesetting(dev, intel_crtc->pipe, intel_crtc->plane);
Jesse Barnes79e53942008-11-07 14:24:08 -08005774}
5775
Carl Worth08d7b3d2009-04-29 14:43:54 -07005776int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00005777 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -07005778{
5779 drm_i915_private_t *dev_priv = dev->dev_private;
5780 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +02005781 struct drm_mode_object *drmmode_obj;
5782 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -07005783
5784 if (!dev_priv) {
5785 DRM_ERROR("called with no initialization\n");
5786 return -EINVAL;
5787 }
5788
Daniel Vetterc05422d2009-08-11 16:05:30 +02005789 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
5790 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -07005791
Daniel Vetterc05422d2009-08-11 16:05:30 +02005792 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -07005793 DRM_ERROR("no such CRTC id\n");
5794 return -EINVAL;
5795 }
5796
Daniel Vetterc05422d2009-08-11 16:05:30 +02005797 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
5798 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -07005799
Daniel Vetterc05422d2009-08-11 16:05:30 +02005800 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -07005801}
5802
Zhenyu Wangc5e4df32010-03-30 14:39:27 +08005803static int intel_encoder_clones(struct drm_device *dev, int type_mask)
Jesse Barnes79e53942008-11-07 14:24:08 -08005804{
Chris Wilson4ef69c72010-09-09 15:14:28 +01005805 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08005806 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005807 int entry = 0;
5808
Chris Wilson4ef69c72010-09-09 15:14:28 +01005809 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
5810 if (type_mask & encoder->clone_mask)
Jesse Barnes79e53942008-11-07 14:24:08 -08005811 index_mask |= (1 << entry);
5812 entry++;
5813 }
Chris Wilson4ef69c72010-09-09 15:14:28 +01005814
Jesse Barnes79e53942008-11-07 14:24:08 -08005815 return index_mask;
5816}
5817
Chris Wilson4d302442010-12-14 19:21:29 +00005818static bool has_edp_a(struct drm_device *dev)
5819{
5820 struct drm_i915_private *dev_priv = dev->dev_private;
5821
5822 if (!IS_MOBILE(dev))
5823 return false;
5824
5825 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
5826 return false;
5827
5828 if (IS_GEN5(dev) &&
5829 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
5830 return false;
5831
5832 return true;
5833}
5834
Jesse Barnes79e53942008-11-07 14:24:08 -08005835static void intel_setup_outputs(struct drm_device *dev)
5836{
Eric Anholt725e30a2009-01-22 13:01:02 -08005837 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +01005838 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -04005839 bool dpd_is_edp = false;
Chris Wilsonc5d1b512010-11-29 18:00:23 +00005840 bool has_lvds = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08005841
Zhenyu Wang541998a2009-06-05 15:38:44 +08005842 if (IS_MOBILE(dev) && !IS_I830(dev))
Chris Wilsonc5d1b512010-11-29 18:00:23 +00005843 has_lvds = intel_lvds_init(dev);
5844 if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
5845 /* disable the panel fitter on everything but LVDS */
5846 I915_WRITE(PFIT_CONTROL, 0);
5847 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005848
Eric Anholtbad720f2009-10-22 16:11:14 -07005849 if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -04005850 dpd_is_edp = intel_dpd_is_edp(dev);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08005851
Chris Wilson4d302442010-12-14 19:21:29 +00005852 if (has_edp_a(dev))
Zhenyu Wang32f9d652009-07-24 01:00:32 +08005853 intel_dp_init(dev, DP_A);
5854
Adam Jacksoncb0953d2010-07-16 14:46:29 -04005855 if (dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
5856 intel_dp_init(dev, PCH_DP_D);
5857 }
5858
5859 intel_crt_init(dev);
5860
5861 if (HAS_PCH_SPLIT(dev)) {
5862 int found;
5863
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08005864 if (I915_READ(HDMIB) & PORT_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +08005865 /* PCH SDVOB multiplex with HDMIB */
5866 found = intel_sdvo_init(dev, PCH_SDVOB);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08005867 if (!found)
5868 intel_hdmi_init(dev, HDMIB);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08005869 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
5870 intel_dp_init(dev, PCH_DP_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08005871 }
5872
5873 if (I915_READ(HDMIC) & PORT_DETECTED)
5874 intel_hdmi_init(dev, HDMIC);
5875
5876 if (I915_READ(HDMID) & PORT_DETECTED)
5877 intel_hdmi_init(dev, HDMID);
5878
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08005879 if (I915_READ(PCH_DP_C) & DP_DETECTED)
5880 intel_dp_init(dev, PCH_DP_C);
5881
Adam Jacksoncb0953d2010-07-16 14:46:29 -04005882 if (!dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08005883 intel_dp_init(dev, PCH_DP_D);
5884
Zhenyu Wang103a1962009-11-27 11:44:36 +08005885 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +08005886 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -08005887
Eric Anholt725e30a2009-01-22 13:01:02 -08005888 if (I915_READ(SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005889 DRM_DEBUG_KMS("probing SDVOB\n");
Eric Anholt725e30a2009-01-22 13:01:02 -08005890 found = intel_sdvo_init(dev, SDVOB);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005891 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
5892 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Eric Anholt725e30a2009-01-22 13:01:02 -08005893 intel_hdmi_init(dev, SDVOB);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005894 }
Ma Ling27185ae2009-08-24 13:50:23 +08005895
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005896 if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
5897 DRM_DEBUG_KMS("probing DP_B\n");
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005898 intel_dp_init(dev, DP_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005899 }
Eric Anholt725e30a2009-01-22 13:01:02 -08005900 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -04005901
5902 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -04005903
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005904 if (I915_READ(SDVOB) & SDVO_DETECTED) {
5905 DRM_DEBUG_KMS("probing SDVOC\n");
Eric Anholt725e30a2009-01-22 13:01:02 -08005906 found = intel_sdvo_init(dev, SDVOC);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005907 }
Ma Ling27185ae2009-08-24 13:50:23 +08005908
5909 if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
5910
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005911 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
5912 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Eric Anholt725e30a2009-01-22 13:01:02 -08005913 intel_hdmi_init(dev, SDVOC);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005914 }
5915 if (SUPPORTS_INTEGRATED_DP(dev)) {
5916 DRM_DEBUG_KMS("probing DP_C\n");
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005917 intel_dp_init(dev, DP_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005918 }
Eric Anholt725e30a2009-01-22 13:01:02 -08005919 }
Ma Ling27185ae2009-08-24 13:50:23 +08005920
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005921 if (SUPPORTS_INTEGRATED_DP(dev) &&
5922 (I915_READ(DP_D) & DP_DETECTED)) {
5923 DRM_DEBUG_KMS("probing DP_D\n");
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005924 intel_dp_init(dev, DP_D);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005925 }
Eric Anholtbad720f2009-10-22 16:11:14 -07005926 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08005927 intel_dvo_init(dev);
5928
Zhenyu Wang103a1962009-11-27 11:44:36 +08005929 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08005930 intel_tv_init(dev);
5931
Chris Wilson4ef69c72010-09-09 15:14:28 +01005932 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
5933 encoder->base.possible_crtcs = encoder->crtc_mask;
5934 encoder->base.possible_clones =
5935 intel_encoder_clones(dev, encoder->clone_mask);
Jesse Barnes79e53942008-11-07 14:24:08 -08005936 }
Chris Wilson47356eb2011-01-11 17:06:04 +00005937
5938 intel_panel_setup_backlight(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08005939}
5940
5941static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
5942{
5943 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08005944
5945 drm_framebuffer_cleanup(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00005946 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08005947
5948 kfree(intel_fb);
5949}
5950
5951static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +00005952 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08005953 unsigned int *handle)
5954{
5955 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00005956 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08005957
Chris Wilson05394f32010-11-08 19:18:58 +00005958 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -08005959}
5960
5961static const struct drm_framebuffer_funcs intel_fb_funcs = {
5962 .destroy = intel_user_framebuffer_destroy,
5963 .create_handle = intel_user_framebuffer_create_handle,
5964};
5965
Dave Airlie38651672010-03-30 05:34:13 +00005966int intel_framebuffer_init(struct drm_device *dev,
5967 struct intel_framebuffer *intel_fb,
5968 struct drm_mode_fb_cmd *mode_cmd,
Chris Wilson05394f32010-11-08 19:18:58 +00005969 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -08005970{
Jesse Barnes79e53942008-11-07 14:24:08 -08005971 int ret;
5972
Chris Wilson05394f32010-11-08 19:18:58 +00005973 if (obj->tiling_mode == I915_TILING_Y)
Chris Wilson57cd6502010-08-08 12:34:44 +01005974 return -EINVAL;
5975
5976 if (mode_cmd->pitch & 63)
5977 return -EINVAL;
5978
5979 switch (mode_cmd->bpp) {
5980 case 8:
5981 case 16:
5982 case 24:
5983 case 32:
5984 break;
5985 default:
5986 return -EINVAL;
5987 }
5988
Jesse Barnes79e53942008-11-07 14:24:08 -08005989 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
5990 if (ret) {
5991 DRM_ERROR("framebuffer init failed %d\n", ret);
5992 return ret;
5993 }
5994
5995 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
Jesse Barnes79e53942008-11-07 14:24:08 -08005996 intel_fb->obj = obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08005997 return 0;
5998}
5999
Jesse Barnes79e53942008-11-07 14:24:08 -08006000static struct drm_framebuffer *
6001intel_user_framebuffer_create(struct drm_device *dev,
6002 struct drm_file *filp,
6003 struct drm_mode_fb_cmd *mode_cmd)
6004{
Chris Wilson05394f32010-11-08 19:18:58 +00006005 struct drm_i915_gem_object *obj;
Dave Airlie38651672010-03-30 05:34:13 +00006006 struct intel_framebuffer *intel_fb;
Jesse Barnes79e53942008-11-07 14:24:08 -08006007 int ret;
6008
Chris Wilson05394f32010-11-08 19:18:58 +00006009 obj = to_intel_bo(drm_gem_object_lookup(dev, filp, mode_cmd->handle));
Jesse Barnes79e53942008-11-07 14:24:08 -08006010 if (!obj)
Chris Wilsoncce13ff2010-08-08 13:36:38 +01006011 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -08006012
Dave Airlie38651672010-03-30 05:34:13 +00006013 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
6014 if (!intel_fb)
Chris Wilsoncce13ff2010-08-08 13:36:38 +01006015 return ERR_PTR(-ENOMEM);
Dave Airlie38651672010-03-30 05:34:13 +00006016
Chris Wilson05394f32010-11-08 19:18:58 +00006017 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08006018 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00006019 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie38651672010-03-30 05:34:13 +00006020 kfree(intel_fb);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01006021 return ERR_PTR(ret);
Jesse Barnes79e53942008-11-07 14:24:08 -08006022 }
6023
Dave Airlie38651672010-03-30 05:34:13 +00006024 return &intel_fb->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08006025}
6026
Jesse Barnes79e53942008-11-07 14:24:08 -08006027static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -08006028 .fb_create = intel_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +00006029 .output_poll_changed = intel_fb_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -08006030};
6031
Chris Wilson05394f32010-11-08 19:18:58 +00006032static struct drm_i915_gem_object *
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08006033intel_alloc_context_page(struct drm_device *dev)
Chris Wilson9ea8d052010-01-04 18:57:56 +00006034{
Chris Wilson05394f32010-11-08 19:18:58 +00006035 struct drm_i915_gem_object *ctx;
Chris Wilson9ea8d052010-01-04 18:57:56 +00006036 int ret;
6037
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08006038 ctx = i915_gem_alloc_object(dev, 4096);
6039 if (!ctx) {
Chris Wilson9ea8d052010-01-04 18:57:56 +00006040 DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
6041 return NULL;
6042 }
6043
6044 mutex_lock(&dev->struct_mutex);
Daniel Vetter75e9e912010-11-04 17:11:09 +01006045 ret = i915_gem_object_pin(ctx, 4096, true);
Chris Wilson9ea8d052010-01-04 18:57:56 +00006046 if (ret) {
6047 DRM_ERROR("failed to pin power context: %d\n", ret);
6048 goto err_unref;
6049 }
6050
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08006051 ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
Chris Wilson9ea8d052010-01-04 18:57:56 +00006052 if (ret) {
6053 DRM_ERROR("failed to set-domain on power context: %d\n", ret);
6054 goto err_unpin;
6055 }
6056 mutex_unlock(&dev->struct_mutex);
6057
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08006058 return ctx;
Chris Wilson9ea8d052010-01-04 18:57:56 +00006059
6060err_unpin:
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08006061 i915_gem_object_unpin(ctx);
Chris Wilson9ea8d052010-01-04 18:57:56 +00006062err_unref:
Chris Wilson05394f32010-11-08 19:18:58 +00006063 drm_gem_object_unreference(&ctx->base);
Chris Wilson9ea8d052010-01-04 18:57:56 +00006064 mutex_unlock(&dev->struct_mutex);
6065 return NULL;
6066}
6067
Jesse Barnes7648fa92010-05-20 14:28:11 -07006068bool ironlake_set_drps(struct drm_device *dev, u8 val)
6069{
6070 struct drm_i915_private *dev_priv = dev->dev_private;
6071 u16 rgvswctl;
6072
6073 rgvswctl = I915_READ16(MEMSWCTL);
6074 if (rgvswctl & MEMCTL_CMD_STS) {
6075 DRM_DEBUG("gpu busy, RCS change rejected\n");
6076 return false; /* still busy with another command */
6077 }
6078
6079 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
6080 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
6081 I915_WRITE16(MEMSWCTL, rgvswctl);
6082 POSTING_READ16(MEMSWCTL);
6083
6084 rgvswctl |= MEMCTL_CMD_STS;
6085 I915_WRITE16(MEMSWCTL, rgvswctl);
6086
6087 return true;
6088}
6089
Jesse Barnesf97108d2010-01-29 11:27:07 -08006090void ironlake_enable_drps(struct drm_device *dev)
6091{
6092 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7648fa92010-05-20 14:28:11 -07006093 u32 rgvmodectl = I915_READ(MEMMODECTL);
Jesse Barnesf97108d2010-01-29 11:27:07 -08006094 u8 fmax, fmin, fstart, vstart;
Jesse Barnesf97108d2010-01-29 11:27:07 -08006095
Jesse Barnesea056c12010-09-10 10:02:13 -07006096 /* Enable temp reporting */
6097 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
6098 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
6099
Jesse Barnesf97108d2010-01-29 11:27:07 -08006100 /* 100ms RC evaluation intervals */
6101 I915_WRITE(RCUPEI, 100000);
6102 I915_WRITE(RCDNEI, 100000);
6103
6104 /* Set max/min thresholds to 90ms and 80ms respectively */
6105 I915_WRITE(RCBMAXAVG, 90000);
6106 I915_WRITE(RCBMINAVG, 80000);
6107
6108 I915_WRITE(MEMIHYST, 1);
6109
6110 /* Set up min, max, and cur for interrupt handling */
6111 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
6112 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
6113 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
6114 MEMMODE_FSTART_SHIFT;
Jesse Barnes7648fa92010-05-20 14:28:11 -07006115
Jesse Barnesf97108d2010-01-29 11:27:07 -08006116 vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
6117 PXVFREQ_PX_SHIFT;
6118
Jesse Barnes80dbf4b2010-11-01 14:12:01 -07006119 dev_priv->fmax = fmax; /* IPS callback will increase this */
Jesse Barnes7648fa92010-05-20 14:28:11 -07006120 dev_priv->fstart = fstart;
6121
Jesse Barnes80dbf4b2010-11-01 14:12:01 -07006122 dev_priv->max_delay = fstart;
Jesse Barnesf97108d2010-01-29 11:27:07 -08006123 dev_priv->min_delay = fmin;
6124 dev_priv->cur_delay = fstart;
6125
Jesse Barnes80dbf4b2010-11-01 14:12:01 -07006126 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
6127 fmax, fmin, fstart);
Jesse Barnes7648fa92010-05-20 14:28:11 -07006128
Jesse Barnesf97108d2010-01-29 11:27:07 -08006129 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
6130
6131 /*
6132 * Interrupts will be enabled in ironlake_irq_postinstall
6133 */
6134
6135 I915_WRITE(VIDSTART, vstart);
6136 POSTING_READ(VIDSTART);
6137
6138 rgvmodectl |= MEMMODE_SWMODE_EN;
6139 I915_WRITE(MEMMODECTL, rgvmodectl);
6140
Chris Wilson481b6af2010-08-23 17:43:35 +01006141 if (wait_for((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Chris Wilson913d8d12010-08-07 11:01:35 +01006142 DRM_ERROR("stuck trying to change perf mode\n");
Jesse Barnesf97108d2010-01-29 11:27:07 -08006143 msleep(1);
6144
Jesse Barnes7648fa92010-05-20 14:28:11 -07006145 ironlake_set_drps(dev, fstart);
Jesse Barnesf97108d2010-01-29 11:27:07 -08006146
Jesse Barnes7648fa92010-05-20 14:28:11 -07006147 dev_priv->last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
6148 I915_READ(0x112e0);
6149 dev_priv->last_time1 = jiffies_to_msecs(jiffies);
6150 dev_priv->last_count2 = I915_READ(0x112f4);
6151 getrawmonotonic(&dev_priv->last_time2);
Jesse Barnesf97108d2010-01-29 11:27:07 -08006152}
6153
6154void ironlake_disable_drps(struct drm_device *dev)
6155{
6156 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7648fa92010-05-20 14:28:11 -07006157 u16 rgvswctl = I915_READ16(MEMSWCTL);
Jesse Barnesf97108d2010-01-29 11:27:07 -08006158
6159 /* Ack interrupts, disable EFC interrupt */
6160 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
6161 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
6162 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
6163 I915_WRITE(DEIIR, DE_PCU_EVENT);
6164 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
6165
6166 /* Go back to the starting frequency */
Jesse Barnes7648fa92010-05-20 14:28:11 -07006167 ironlake_set_drps(dev, dev_priv->fstart);
Jesse Barnesf97108d2010-01-29 11:27:07 -08006168 msleep(1);
6169 rgvswctl |= MEMCTL_CMD_STS;
6170 I915_WRITE(MEMSWCTL, rgvswctl);
6171 msleep(1);
6172
6173}
6174
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08006175void gen6_set_rps(struct drm_device *dev, u8 val)
6176{
6177 struct drm_i915_private *dev_priv = dev->dev_private;
6178 u32 swreq;
6179
6180 swreq = (val & 0x3ff) << 25;
6181 I915_WRITE(GEN6_RPNSWREQ, swreq);
6182}
6183
6184void gen6_disable_rps(struct drm_device *dev)
6185{
6186 struct drm_i915_private *dev_priv = dev->dev_private;
6187
6188 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
6189 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
6190 I915_WRITE(GEN6_PMIER, 0);
6191 I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
6192}
6193
Jesse Barnes7648fa92010-05-20 14:28:11 -07006194static unsigned long intel_pxfreq(u32 vidfreq)
6195{
6196 unsigned long freq;
6197 int div = (vidfreq & 0x3f0000) >> 16;
6198 int post = (vidfreq & 0x3000) >> 12;
6199 int pre = (vidfreq & 0x7);
6200
6201 if (!pre)
6202 return 0;
6203
6204 freq = ((div * 133333) / ((1<<post) * pre));
6205
6206 return freq;
6207}
6208
6209void intel_init_emon(struct drm_device *dev)
6210{
6211 struct drm_i915_private *dev_priv = dev->dev_private;
6212 u32 lcfuse;
6213 u8 pxw[16];
6214 int i;
6215
6216 /* Disable to program */
6217 I915_WRITE(ECR, 0);
6218 POSTING_READ(ECR);
6219
6220 /* Program energy weights for various events */
6221 I915_WRITE(SDEW, 0x15040d00);
6222 I915_WRITE(CSIEW0, 0x007f0000);
6223 I915_WRITE(CSIEW1, 0x1e220004);
6224 I915_WRITE(CSIEW2, 0x04000004);
6225
6226 for (i = 0; i < 5; i++)
6227 I915_WRITE(PEW + (i * 4), 0);
6228 for (i = 0; i < 3; i++)
6229 I915_WRITE(DEW + (i * 4), 0);
6230
6231 /* Program P-state weights to account for frequency power adjustment */
6232 for (i = 0; i < 16; i++) {
6233 u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
6234 unsigned long freq = intel_pxfreq(pxvidfreq);
6235 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
6236 PXVFREQ_PX_SHIFT;
6237 unsigned long val;
6238
6239 val = vid * vid;
6240 val *= (freq / 1000);
6241 val *= 255;
6242 val /= (127*127*900);
6243 if (val > 0xff)
6244 DRM_ERROR("bad pxval: %ld\n", val);
6245 pxw[i] = val;
6246 }
6247 /* Render standby states get 0 weight */
6248 pxw[14] = 0;
6249 pxw[15] = 0;
6250
6251 for (i = 0; i < 4; i++) {
6252 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
6253 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
6254 I915_WRITE(PXW + (i * 4), val);
6255 }
6256
6257 /* Adjust magic regs to magic values (more experimental results) */
6258 I915_WRITE(OGW0, 0);
6259 I915_WRITE(OGW1, 0);
6260 I915_WRITE(EG0, 0x00007f00);
6261 I915_WRITE(EG1, 0x0000000e);
6262 I915_WRITE(EG2, 0x000e0000);
6263 I915_WRITE(EG3, 0x68000300);
6264 I915_WRITE(EG4, 0x42000000);
6265 I915_WRITE(EG5, 0x00140031);
6266 I915_WRITE(EG6, 0);
6267 I915_WRITE(EG7, 0);
6268
6269 for (i = 0; i < 8; i++)
6270 I915_WRITE(PXWL + (i * 4), 0);
6271
6272 /* Enable PMON + select events */
6273 I915_WRITE(ECR, 0x80000019);
6274
6275 lcfuse = I915_READ(LCFUSE02);
6276
6277 dev_priv->corr = (lcfuse & LCFUSE_HIV_MASK);
6278}
6279
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08006280void gen6_enable_rps(struct drm_i915_private *dev_priv)
Chris Wilson8fd26852010-12-08 18:40:43 +00006281{
Jesse Barnesa6044e22010-12-20 11:34:20 -08006282 u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
6283 u32 gt_perf_status = I915_READ(GEN6_GT_PERF_STATUS);
6284 u32 pcu_mbox;
6285 int cur_freq, min_freq, max_freq;
Chris Wilson8fd26852010-12-08 18:40:43 +00006286 int i;
6287
6288 /* Here begins a magic sequence of register writes to enable
6289 * auto-downclocking.
6290 *
6291 * Perhaps there might be some value in exposing these to
6292 * userspace...
6293 */
6294 I915_WRITE(GEN6_RC_STATE, 0);
6295 __gen6_force_wake_get(dev_priv);
6296
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08006297 /* disable the counters and set deterministic thresholds */
Chris Wilson8fd26852010-12-08 18:40:43 +00006298 I915_WRITE(GEN6_RC_CONTROL, 0);
6299
6300 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
6301 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
6302 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
6303 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
6304 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
6305
6306 for (i = 0; i < I915_NUM_RINGS; i++)
6307 I915_WRITE(RING_MAX_IDLE(dev_priv->ring[i].mmio_base), 10);
6308
6309 I915_WRITE(GEN6_RC_SLEEP, 0);
6310 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
6311 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
6312 I915_WRITE(GEN6_RC6p_THRESHOLD, 100000);
6313 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
6314
6315 I915_WRITE(GEN6_RC_CONTROL,
6316 GEN6_RC_CTL_RC6p_ENABLE |
6317 GEN6_RC_CTL_RC6_ENABLE |
Chris Wilson9c3d2f72010-12-17 10:54:26 +00006318 GEN6_RC_CTL_EI_MODE(1) |
Chris Wilson8fd26852010-12-08 18:40:43 +00006319 GEN6_RC_CTL_HW_ENABLE);
6320
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08006321 I915_WRITE(GEN6_RPNSWREQ,
Chris Wilson8fd26852010-12-08 18:40:43 +00006322 GEN6_FREQUENCY(10) |
6323 GEN6_OFFSET(0) |
6324 GEN6_AGGRESSIVE_TURBO);
6325 I915_WRITE(GEN6_RC_VIDEO_FREQ,
6326 GEN6_FREQUENCY(12));
6327
6328 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
6329 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
6330 18 << 24 |
6331 6 << 16);
6332 I915_WRITE(GEN6_RP_UP_THRESHOLD, 90000);
6333 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 100000);
6334 I915_WRITE(GEN6_RP_UP_EI, 100000);
6335 I915_WRITE(GEN6_RP_DOWN_EI, 300000);
6336 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
6337 I915_WRITE(GEN6_RP_CONTROL,
6338 GEN6_RP_MEDIA_TURBO |
6339 GEN6_RP_USE_NORMAL_FREQ |
6340 GEN6_RP_MEDIA_IS_GFX |
6341 GEN6_RP_ENABLE |
6342 GEN6_RP_UP_BUSY_MAX |
6343 GEN6_RP_DOWN_BUSY_MIN);
6344
6345 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6346 500))
6347 DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
6348
6349 I915_WRITE(GEN6_PCODE_DATA, 0);
6350 I915_WRITE(GEN6_PCODE_MAILBOX,
6351 GEN6_PCODE_READY |
6352 GEN6_PCODE_WRITE_MIN_FREQ_TABLE);
6353 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6354 500))
6355 DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
6356
Jesse Barnesa6044e22010-12-20 11:34:20 -08006357 min_freq = (rp_state_cap & 0xff0000) >> 16;
6358 max_freq = rp_state_cap & 0xff;
6359 cur_freq = (gt_perf_status & 0xff00) >> 8;
6360
6361 /* Check for overclock support */
6362 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6363 500))
6364 DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
6365 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_READ_OC_PARAMS);
6366 pcu_mbox = I915_READ(GEN6_PCODE_DATA);
6367 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6368 500))
6369 DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
6370 if (pcu_mbox & (1<<31)) { /* OC supported */
6371 max_freq = pcu_mbox & 0xff;
6372 DRM_DEBUG_DRIVER("overclocking supported, adjusting frequency max to %dMHz\n", pcu_mbox * 100);
6373 }
6374
6375 /* In units of 100MHz */
6376 dev_priv->max_delay = max_freq;
6377 dev_priv->min_delay = min_freq;
6378 dev_priv->cur_delay = cur_freq;
6379
Chris Wilson8fd26852010-12-08 18:40:43 +00006380 /* requires MSI enabled */
6381 I915_WRITE(GEN6_PMIER,
6382 GEN6_PM_MBOX_EVENT |
6383 GEN6_PM_THERMAL_EVENT |
6384 GEN6_PM_RP_DOWN_TIMEOUT |
6385 GEN6_PM_RP_UP_THRESHOLD |
6386 GEN6_PM_RP_DOWN_THRESHOLD |
6387 GEN6_PM_RP_UP_EI_EXPIRED |
6388 GEN6_PM_RP_DOWN_EI_EXPIRED);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08006389 I915_WRITE(GEN6_PMIMR, 0);
6390 /* enable all PM interrupts */
6391 I915_WRITE(GEN6_PMINTRMSK, 0);
Chris Wilson8fd26852010-12-08 18:40:43 +00006392
6393 __gen6_force_wake_put(dev_priv);
6394}
6395
Chris Wilson0cdab212010-12-05 17:27:06 +00006396void intel_enable_clock_gating(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07006397{
6398 struct drm_i915_private *dev_priv = dev->dev_private;
6399
6400 /*
6401 * Disable clock gating reported to work incorrectly according to the
6402 * specs, but enable as much else as we can.
6403 */
Eric Anholtbad720f2009-10-22 16:11:14 -07006404 if (HAS_PCH_SPLIT(dev)) {
Eric Anholt8956c8b2010-03-18 13:21:14 -07006405 uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
6406
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01006407 if (IS_GEN5(dev)) {
Eric Anholt8956c8b2010-03-18 13:21:14 -07006408 /* Required for FBC */
6409 dspclk_gate |= DPFDUNIT_CLOCK_GATE_DISABLE;
6410 /* Required for CxSR */
6411 dspclk_gate |= DPARBUNIT_CLOCK_GATE_DISABLE;
6412
6413 I915_WRITE(PCH_3DCGDIS0,
6414 MARIUNIT_CLOCK_GATE_DISABLE |
6415 SVSMUNIT_CLOCK_GATE_DISABLE);
Eric Anholt06f37752010-12-14 10:06:46 -08006416 I915_WRITE(PCH_3DCGDIS1,
6417 VFMUNIT_CLOCK_GATE_DISABLE);
Eric Anholt8956c8b2010-03-18 13:21:14 -07006418 }
6419
6420 I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08006421
6422 /*
Jesse Barnes382b0932010-10-07 16:01:25 -07006423 * On Ibex Peak and Cougar Point, we need to disable clock
6424 * gating for the panel power sequencer or it will fail to
6425 * start up when no ports are active.
6426 */
6427 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
6428
6429 /*
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08006430 * According to the spec the following bits should be set in
6431 * order to enable memory self-refresh
6432 * The bit 22/21 of 0x42004
6433 * The bit 5 of 0x42020
6434 * The bit 15 of 0x45000
6435 */
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01006436 if (IS_GEN5(dev)) {
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08006437 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6438 (I915_READ(ILK_DISPLAY_CHICKEN2) |
6439 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
6440 I915_WRITE(ILK_DSPCLK_GATE,
6441 (I915_READ(ILK_DSPCLK_GATE) |
6442 ILK_DPARB_CLK_GATE));
6443 I915_WRITE(DISP_ARB_CTL,
6444 (I915_READ(DISP_ARB_CTL) |
6445 DISP_FBC_WM_DIS));
Yuanhan Liu13982612010-12-15 15:42:31 +08006446 I915_WRITE(WM3_LP_ILK, 0);
6447 I915_WRITE(WM2_LP_ILK, 0);
6448 I915_WRITE(WM1_LP_ILK, 0);
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08006449 }
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08006450 /*
6451 * Based on the document from hardware guys the following bits
6452 * should be set unconditionally in order to enable FBC.
6453 * The bit 22 of 0x42000
6454 * The bit 22 of 0x42004
6455 * The bit 7,8,9 of 0x42020.
6456 */
6457 if (IS_IRONLAKE_M(dev)) {
6458 I915_WRITE(ILK_DISPLAY_CHICKEN1,
6459 I915_READ(ILK_DISPLAY_CHICKEN1) |
6460 ILK_FBCQ_DIS);
6461 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6462 I915_READ(ILK_DISPLAY_CHICKEN2) |
6463 ILK_DPARB_GATE);
6464 I915_WRITE(ILK_DSPCLK_GATE,
6465 I915_READ(ILK_DSPCLK_GATE) |
6466 ILK_DPFC_DIS1 |
6467 ILK_DPFC_DIS2 |
6468 ILK_CLK_FBC);
6469 }
Eric Anholtde6e2ea2010-11-06 14:53:32 -07006470
Eric Anholt67e92af2010-11-06 14:53:33 -07006471 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6472 I915_READ(ILK_DISPLAY_CHICKEN2) |
6473 ILK_ELPIN_409_SELECT);
6474
Eric Anholtde6e2ea2010-11-06 14:53:32 -07006475 if (IS_GEN5(dev)) {
6476 I915_WRITE(_3D_CHICKEN2,
6477 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
6478 _3D_CHICKEN2_WM_READ_PIPELINED);
6479 }
Chris Wilson8fd26852010-12-08 18:40:43 +00006480
Yuanhan Liu13982612010-12-15 15:42:31 +08006481 if (IS_GEN6(dev)) {
6482 I915_WRITE(WM3_LP_ILK, 0);
6483 I915_WRITE(WM2_LP_ILK, 0);
6484 I915_WRITE(WM1_LP_ILK, 0);
6485
6486 /*
6487 * According to the spec the following bits should be
6488 * set in order to enable memory self-refresh and fbc:
6489 * The bit21 and bit22 of 0x42000
6490 * The bit21 and bit22 of 0x42004
6491 * The bit5 and bit7 of 0x42020
6492 * The bit14 of 0x70180
6493 * The bit14 of 0x71180
6494 */
6495 I915_WRITE(ILK_DISPLAY_CHICKEN1,
6496 I915_READ(ILK_DISPLAY_CHICKEN1) |
6497 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
6498 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6499 I915_READ(ILK_DISPLAY_CHICKEN2) |
6500 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
6501 I915_WRITE(ILK_DSPCLK_GATE,
6502 I915_READ(ILK_DSPCLK_GATE) |
6503 ILK_DPARB_CLK_GATE |
6504 ILK_DPFD_CLK_GATE);
6505
6506 I915_WRITE(DSPACNTR,
6507 I915_READ(DSPACNTR) |
6508 DISPPLANE_TRICKLE_FEED_DISABLE);
6509 I915_WRITE(DSPBCNTR,
6510 I915_READ(DSPBCNTR) |
6511 DISPPLANE_TRICKLE_FEED_DISABLE);
6512 }
Zhenyu Wangc03342f2009-09-29 11:01:23 +08006513 } else if (IS_G4X(dev)) {
Jesse Barnes652c3932009-08-17 13:31:43 -07006514 uint32_t dspclk_gate;
6515 I915_WRITE(RENCLK_GATE_D1, 0);
6516 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
6517 GS_UNIT_CLOCK_GATE_DISABLE |
6518 CL_UNIT_CLOCK_GATE_DISABLE);
6519 I915_WRITE(RAMCLK_GATE_D, 0);
6520 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
6521 OVRUNIT_CLOCK_GATE_DISABLE |
6522 OVCUNIT_CLOCK_GATE_DISABLE;
6523 if (IS_GM45(dev))
6524 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
6525 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006526 } else if (IS_CRESTLINE(dev)) {
Jesse Barnes652c3932009-08-17 13:31:43 -07006527 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
6528 I915_WRITE(RENCLK_GATE_D2, 0);
6529 I915_WRITE(DSPCLK_GATE_D, 0);
6530 I915_WRITE(RAMCLK_GATE_D, 0);
6531 I915_WRITE16(DEUC, 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006532 } else if (IS_BROADWATER(dev)) {
Jesse Barnes652c3932009-08-17 13:31:43 -07006533 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
6534 I965_RCC_CLOCK_GATE_DISABLE |
6535 I965_RCPB_CLOCK_GATE_DISABLE |
6536 I965_ISC_CLOCK_GATE_DISABLE |
6537 I965_FBC_CLOCK_GATE_DISABLE);
6538 I915_WRITE(RENCLK_GATE_D2, 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006539 } else if (IS_GEN3(dev)) {
Jesse Barnes652c3932009-08-17 13:31:43 -07006540 u32 dstate = I915_READ(D_STATE);
6541
6542 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
6543 DSTATE_DOT_CLOCK_GATING;
6544 I915_WRITE(D_STATE, dstate);
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +02006545 } else if (IS_I85X(dev) || IS_I865G(dev)) {
Jesse Barnes652c3932009-08-17 13:31:43 -07006546 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
6547 } else if (IS_I830(dev)) {
6548 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
6549 }
6550}
6551
Chris Wilson0cdab212010-12-05 17:27:06 +00006552void intel_disable_clock_gating(struct drm_device *dev)
6553{
6554 struct drm_i915_private *dev_priv = dev->dev_private;
6555
6556 if (dev_priv->renderctx) {
6557 struct drm_i915_gem_object *obj = dev_priv->renderctx;
6558
6559 I915_WRITE(CCID, 0);
6560 POSTING_READ(CCID);
6561
6562 i915_gem_object_unpin(obj);
6563 drm_gem_object_unreference(&obj->base);
6564 dev_priv->renderctx = NULL;
6565 }
6566
6567 if (dev_priv->pwrctx) {
6568 struct drm_i915_gem_object *obj = dev_priv->pwrctx;
6569
6570 I915_WRITE(PWRCTXA, 0);
6571 POSTING_READ(PWRCTXA);
6572
6573 i915_gem_object_unpin(obj);
6574 drm_gem_object_unreference(&obj->base);
6575 dev_priv->pwrctx = NULL;
6576 }
6577}
6578
Jesse Barnesd5bb0812011-01-05 12:01:26 -08006579static void ironlake_disable_rc6(struct drm_device *dev)
6580{
6581 struct drm_i915_private *dev_priv = dev->dev_private;
6582
6583 /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
6584 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
6585 wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
6586 10);
6587 POSTING_READ(CCID);
6588 I915_WRITE(PWRCTXA, 0);
6589 POSTING_READ(PWRCTXA);
6590 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
6591 POSTING_READ(RSTDBYCTL);
6592 i915_gem_object_unpin(dev_priv->renderctx);
6593 drm_gem_object_unreference(&dev_priv->renderctx->base);
6594 dev_priv->renderctx = NULL;
6595 i915_gem_object_unpin(dev_priv->pwrctx);
6596 drm_gem_object_unreference(&dev_priv->pwrctx->base);
6597 dev_priv->pwrctx = NULL;
6598}
6599
6600void ironlake_enable_rc6(struct drm_device *dev)
6601{
6602 struct drm_i915_private *dev_priv = dev->dev_private;
6603 int ret;
6604
6605 /*
6606 * GPU can automatically power down the render unit if given a page
6607 * to save state.
6608 */
6609 ret = BEGIN_LP_RING(6);
6610 if (ret) {
6611 ironlake_disable_rc6(dev);
6612 return;
6613 }
6614 OUT_RING(MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
6615 OUT_RING(MI_SET_CONTEXT);
6616 OUT_RING(dev_priv->renderctx->gtt_offset |
6617 MI_MM_SPACE_GTT |
6618 MI_SAVE_EXT_STATE_EN |
6619 MI_RESTORE_EXT_STATE_EN |
6620 MI_RESTORE_INHIBIT);
6621 OUT_RING(MI_SUSPEND_FLUSH);
6622 OUT_RING(MI_NOOP);
6623 OUT_RING(MI_FLUSH);
6624 ADVANCE_LP_RING();
6625
6626 I915_WRITE(PWRCTXA, dev_priv->pwrctx->gtt_offset | PWRCTX_EN);
6627 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
6628}
6629
Jesse Barnese70236a2009-09-21 10:42:27 -07006630/* Set up chip specific display functions */
6631static void intel_init_display(struct drm_device *dev)
6632{
6633 struct drm_i915_private *dev_priv = dev->dev_private;
6634
6635 /* We always want a DPMS function */
Eric Anholtbad720f2009-10-22 16:11:14 -07006636 if (HAS_PCH_SPLIT(dev))
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006637 dev_priv->display.dpms = ironlake_crtc_dpms;
Jesse Barnese70236a2009-09-21 10:42:27 -07006638 else
6639 dev_priv->display.dpms = i9xx_crtc_dpms;
6640
Adam Jacksonee5382a2010-04-23 11:17:39 -04006641 if (I915_HAS_FBC(dev)) {
Yuanhan Liu9c04f012010-12-15 15:42:32 +08006642 if (HAS_PCH_SPLIT(dev)) {
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08006643 dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
6644 dev_priv->display.enable_fbc = ironlake_enable_fbc;
6645 dev_priv->display.disable_fbc = ironlake_disable_fbc;
6646 } else if (IS_GM45(dev)) {
Jesse Barnes74dff282009-09-14 15:39:40 -07006647 dev_priv->display.fbc_enabled = g4x_fbc_enabled;
6648 dev_priv->display.enable_fbc = g4x_enable_fbc;
6649 dev_priv->display.disable_fbc = g4x_disable_fbc;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006650 } else if (IS_CRESTLINE(dev)) {
Jesse Barnese70236a2009-09-21 10:42:27 -07006651 dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
6652 dev_priv->display.enable_fbc = i8xx_enable_fbc;
6653 dev_priv->display.disable_fbc = i8xx_disable_fbc;
6654 }
Jesse Barnes74dff282009-09-14 15:39:40 -07006655 /* 855GM needs testing */
Jesse Barnese70236a2009-09-21 10:42:27 -07006656 }
6657
6658 /* Returns the core display clock speed */
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006659 if (IS_I945G(dev) || (IS_G33(dev) && ! IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -07006660 dev_priv->display.get_display_clock_speed =
6661 i945_get_display_clock_speed;
6662 else if (IS_I915G(dev))
6663 dev_priv->display.get_display_clock_speed =
6664 i915_get_display_clock_speed;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006665 else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07006666 dev_priv->display.get_display_clock_speed =
6667 i9xx_misc_get_display_clock_speed;
6668 else if (IS_I915GM(dev))
6669 dev_priv->display.get_display_clock_speed =
6670 i915gm_get_display_clock_speed;
6671 else if (IS_I865G(dev))
6672 dev_priv->display.get_display_clock_speed =
6673 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +02006674 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07006675 dev_priv->display.get_display_clock_speed =
6676 i855_get_display_clock_speed;
6677 else /* 852, 830 */
6678 dev_priv->display.get_display_clock_speed =
6679 i830_get_display_clock_speed;
6680
6681 /* For FIFO watermark updates */
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08006682 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01006683 if (IS_GEN5(dev)) {
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08006684 if (I915_READ(MLTR_ILK) & ILK_SRLT_MASK)
6685 dev_priv->display.update_wm = ironlake_update_wm;
6686 else {
6687 DRM_DEBUG_KMS("Failed to get proper latency. "
6688 "Disable CxSR\n");
6689 dev_priv->display.update_wm = NULL;
6690 }
Yuanhan Liu13982612010-12-15 15:42:31 +08006691 } else if (IS_GEN6(dev)) {
6692 if (SNB_READ_WM0_LATENCY()) {
6693 dev_priv->display.update_wm = sandybridge_update_wm;
6694 } else {
6695 DRM_DEBUG_KMS("Failed to read display plane latency. "
6696 "Disable CxSR\n");
6697 dev_priv->display.update_wm = NULL;
6698 }
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08006699 } else
6700 dev_priv->display.update_wm = NULL;
6701 } else if (IS_PINEVIEW(dev)) {
Zhao Yakuid4294342010-03-22 22:45:36 +08006702 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
Li Peng95534262010-05-18 18:58:44 +08006703 dev_priv->is_ddr3,
Zhao Yakuid4294342010-03-22 22:45:36 +08006704 dev_priv->fsb_freq,
6705 dev_priv->mem_freq)) {
6706 DRM_INFO("failed to find known CxSR latency "
Li Peng95534262010-05-18 18:58:44 +08006707 "(found ddr%s fsb freq %d, mem freq %d), "
Zhao Yakuid4294342010-03-22 22:45:36 +08006708 "disabling CxSR\n",
Li Peng95534262010-05-18 18:58:44 +08006709 (dev_priv->is_ddr3 == 1) ? "3": "2",
Zhao Yakuid4294342010-03-22 22:45:36 +08006710 dev_priv->fsb_freq, dev_priv->mem_freq);
6711 /* Disable CxSR and never update its watermark again */
6712 pineview_disable_cxsr(dev);
6713 dev_priv->display.update_wm = NULL;
6714 } else
6715 dev_priv->display.update_wm = pineview_update_wm;
6716 } else if (IS_G4X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07006717 dev_priv->display.update_wm = g4x_update_wm;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006718 else if (IS_GEN4(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07006719 dev_priv->display.update_wm = i965_update_wm;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006720 else if (IS_GEN3(dev)) {
Jesse Barnese70236a2009-09-21 10:42:27 -07006721 dev_priv->display.update_wm = i9xx_update_wm;
6722 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Adam Jackson8f4695e2010-04-16 18:20:57 -04006723 } else if (IS_I85X(dev)) {
6724 dev_priv->display.update_wm = i9xx_update_wm;
6725 dev_priv->display.get_fifo_size = i85x_get_fifo_size;
Jesse Barnese70236a2009-09-21 10:42:27 -07006726 } else {
Adam Jackson8f4695e2010-04-16 18:20:57 -04006727 dev_priv->display.update_wm = i830_update_wm;
6728 if (IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07006729 dev_priv->display.get_fifo_size = i845_get_fifo_size;
6730 else
6731 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Jesse Barnese70236a2009-09-21 10:42:27 -07006732 }
6733}
6734
Jesse Barnesb690e962010-07-19 13:53:12 -07006735/*
6736 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
6737 * resume, or other times. This quirk makes sure that's the case for
6738 * affected systems.
6739 */
6740static void quirk_pipea_force (struct drm_device *dev)
6741{
6742 struct drm_i915_private *dev_priv = dev->dev_private;
6743
6744 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
6745 DRM_DEBUG_DRIVER("applying pipe a force quirk\n");
6746}
6747
6748struct intel_quirk {
6749 int device;
6750 int subsystem_vendor;
6751 int subsystem_device;
6752 void (*hook)(struct drm_device *dev);
6753};
6754
6755struct intel_quirk intel_quirks[] = {
6756 /* HP Compaq 2730p needs pipe A force quirk (LP: #291555) */
6757 { 0x2a42, 0x103c, 0x30eb, quirk_pipea_force },
6758 /* HP Mini needs pipe A force quirk (LP: #322104) */
6759 { 0x27ae,0x103c, 0x361a, quirk_pipea_force },
6760
6761 /* Thinkpad R31 needs pipe A force quirk */
6762 { 0x3577, 0x1014, 0x0505, quirk_pipea_force },
6763 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
6764 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
6765
6766 /* ThinkPad X30 needs pipe A force quirk (LP: #304614) */
6767 { 0x3577, 0x1014, 0x0513, quirk_pipea_force },
6768 /* ThinkPad X40 needs pipe A force quirk */
6769
6770 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
6771 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
6772
6773 /* 855 & before need to leave pipe A & dpll A up */
6774 { 0x3582, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
6775 { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
6776};
6777
6778static void intel_init_quirks(struct drm_device *dev)
6779{
6780 struct pci_dev *d = dev->pdev;
6781 int i;
6782
6783 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
6784 struct intel_quirk *q = &intel_quirks[i];
6785
6786 if (d->device == q->device &&
6787 (d->subsystem_vendor == q->subsystem_vendor ||
6788 q->subsystem_vendor == PCI_ANY_ID) &&
6789 (d->subsystem_device == q->subsystem_device ||
6790 q->subsystem_device == PCI_ANY_ID))
6791 q->hook(dev);
6792 }
6793}
6794
Jesse Barnes9cce37f2010-08-13 15:11:26 -07006795/* Disable the VGA plane that we never use */
6796static void i915_disable_vga(struct drm_device *dev)
6797{
6798 struct drm_i915_private *dev_priv = dev->dev_private;
6799 u8 sr1;
6800 u32 vga_reg;
6801
6802 if (HAS_PCH_SPLIT(dev))
6803 vga_reg = CPU_VGACNTRL;
6804 else
6805 vga_reg = VGACNTRL;
6806
6807 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
6808 outb(1, VGA_SR_INDEX);
6809 sr1 = inb(VGA_SR_DATA);
6810 outb(sr1 | 1<<5, VGA_SR_DATA);
6811 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
6812 udelay(300);
6813
6814 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
6815 POSTING_READ(vga_reg);
6816}
6817
Jesse Barnes79e53942008-11-07 14:24:08 -08006818void intel_modeset_init(struct drm_device *dev)
6819{
Jesse Barnes652c3932009-08-17 13:31:43 -07006820 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08006821 int i;
6822
6823 drm_mode_config_init(dev);
6824
6825 dev->mode_config.min_width = 0;
6826 dev->mode_config.min_height = 0;
6827
6828 dev->mode_config.funcs = (void *)&intel_mode_funcs;
6829
Jesse Barnesb690e962010-07-19 13:53:12 -07006830 intel_init_quirks(dev);
6831
Jesse Barnese70236a2009-09-21 10:42:27 -07006832 intel_init_display(dev);
6833
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006834 if (IS_GEN2(dev)) {
6835 dev->mode_config.max_width = 2048;
6836 dev->mode_config.max_height = 2048;
6837 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -07006838 dev->mode_config.max_width = 4096;
6839 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -08006840 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006841 dev->mode_config.max_width = 8192;
6842 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -08006843 }
Chris Wilson35c30472010-12-22 14:07:12 +00006844 dev->mode_config.fb_base = dev->agp->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08006845
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006846 if (IS_MOBILE(dev) || !IS_GEN2(dev))
Dave Airliea3524f12010-06-06 18:59:41 +10006847 dev_priv->num_pipe = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -08006848 else
Dave Airliea3524f12010-06-06 18:59:41 +10006849 dev_priv->num_pipe = 1;
Zhao Yakui28c97732009-10-09 11:39:41 +08006850 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Dave Airliea3524f12010-06-06 18:59:41 +10006851 dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -08006852
Dave Airliea3524f12010-06-06 18:59:41 +10006853 for (i = 0; i < dev_priv->num_pipe; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006854 intel_crtc_init(dev, i);
6855 }
6856
6857 intel_setup_outputs(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -07006858
Chris Wilson0cdab212010-12-05 17:27:06 +00006859 intel_enable_clock_gating(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -07006860
Jesse Barnes9cce37f2010-08-13 15:11:26 -07006861 /* Just disable it once at startup */
6862 i915_disable_vga(dev);
6863
Jesse Barnes7648fa92010-05-20 14:28:11 -07006864 if (IS_IRONLAKE_M(dev)) {
Jesse Barnesf97108d2010-01-29 11:27:07 -08006865 ironlake_enable_drps(dev);
Jesse Barnes7648fa92010-05-20 14:28:11 -07006866 intel_init_emon(dev);
6867 }
Jesse Barnesf97108d2010-01-29 11:27:07 -08006868
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08006869 if (IS_GEN6(dev))
6870 gen6_enable_rps(dev_priv);
6871
Jesse Barnesd5bb0812011-01-05 12:01:26 -08006872 if (IS_IRONLAKE_M(dev)) {
6873 dev_priv->renderctx = intel_alloc_context_page(dev);
6874 if (!dev_priv->renderctx)
6875 goto skip_rc6;
6876 dev_priv->pwrctx = intel_alloc_context_page(dev);
6877 if (!dev_priv->pwrctx) {
6878 i915_gem_object_unpin(dev_priv->renderctx);
6879 drm_gem_object_unreference(&dev_priv->renderctx->base);
6880 dev_priv->renderctx = NULL;
6881 goto skip_rc6;
6882 }
6883 ironlake_enable_rc6(dev);
6884 }
6885
6886skip_rc6:
Jesse Barnes652c3932009-08-17 13:31:43 -07006887 INIT_WORK(&dev_priv->idle_work, intel_idle_update);
6888 setup_timer(&dev_priv->idle_timer, intel_gpu_idle_timer,
6889 (unsigned long)dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02006890
6891 intel_setup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006892}
6893
6894void intel_modeset_cleanup(struct drm_device *dev)
6895{
Jesse Barnes652c3932009-08-17 13:31:43 -07006896 struct drm_i915_private *dev_priv = dev->dev_private;
6897 struct drm_crtc *crtc;
6898 struct intel_crtc *intel_crtc;
6899
Keith Packardf87ea762010-10-03 19:36:26 -07006900 drm_kms_helper_poll_fini(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -07006901 mutex_lock(&dev->struct_mutex);
6902
Jesse Barnes723bfd72010-10-07 16:01:13 -07006903 intel_unregister_dsm_handler();
6904
6905
Jesse Barnes652c3932009-08-17 13:31:43 -07006906 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
6907 /* Skip inactive CRTCs */
6908 if (!crtc->fb)
6909 continue;
6910
6911 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3dec0092010-08-20 21:40:52 +02006912 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07006913 }
6914
Jesse Barnese70236a2009-09-21 10:42:27 -07006915 if (dev_priv->display.disable_fbc)
6916 dev_priv->display.disable_fbc(dev);
6917
Jesse Barnesf97108d2010-01-29 11:27:07 -08006918 if (IS_IRONLAKE_M(dev))
6919 ironlake_disable_drps(dev);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08006920 if (IS_GEN6(dev))
6921 gen6_disable_rps(dev);
Jesse Barnesf97108d2010-01-29 11:27:07 -08006922
Jesse Barnesd5bb0812011-01-05 12:01:26 -08006923 if (IS_IRONLAKE_M(dev))
6924 ironlake_disable_rc6(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +00006925
Kristian Høgsberg69341a52009-11-11 12:19:17 -05006926 mutex_unlock(&dev->struct_mutex);
6927
Daniel Vetter6c0d93502010-08-20 18:26:46 +02006928 /* Disable the irq before mode object teardown, for the irq might
6929 * enqueue unpin/hotplug work. */
6930 drm_irq_uninstall(dev);
6931 cancel_work_sync(&dev_priv->hotplug_work);
6932
Daniel Vetter3dec0092010-08-20 21:40:52 +02006933 /* Shut off idle work before the crtcs get freed. */
6934 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
6935 intel_crtc = to_intel_crtc(crtc);
6936 del_timer_sync(&intel_crtc->idle_timer);
6937 }
6938 del_timer_sync(&dev_priv->idle_timer);
6939 cancel_work_sync(&dev_priv->idle_work);
6940
Jesse Barnes79e53942008-11-07 14:24:08 -08006941 drm_mode_config_cleanup(dev);
6942}
6943
Dave Airlie28d52042009-09-21 14:33:58 +10006944/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +08006945 * Return which encoder is currently attached for connector.
6946 */
Chris Wilsondf0e9242010-09-09 16:20:55 +01006947struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -08006948{
Chris Wilsondf0e9242010-09-09 16:20:55 +01006949 return &intel_attached_encoder(connector)->base;
6950}
Jesse Barnes79e53942008-11-07 14:24:08 -08006951
Chris Wilsondf0e9242010-09-09 16:20:55 +01006952void intel_connector_attach_encoder(struct intel_connector *connector,
6953 struct intel_encoder *encoder)
6954{
6955 connector->encoder = encoder;
6956 drm_mode_connector_attach_encoder(&connector->base,
6957 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08006958}
Dave Airlie28d52042009-09-21 14:33:58 +10006959
6960/*
6961 * set vga decode state - true == enable VGA decode
6962 */
6963int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
6964{
6965 struct drm_i915_private *dev_priv = dev->dev_private;
6966 u16 gmch_ctrl;
6967
6968 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
6969 if (state)
6970 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
6971 else
6972 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
6973 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
6974 return 0;
6975}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00006976
6977#ifdef CONFIG_DEBUG_FS
6978#include <linux/seq_file.h>
6979
6980struct intel_display_error_state {
6981 struct intel_cursor_error_state {
6982 u32 control;
6983 u32 position;
6984 u32 base;
6985 u32 size;
6986 } cursor[2];
6987
6988 struct intel_pipe_error_state {
6989 u32 conf;
6990 u32 source;
6991
6992 u32 htotal;
6993 u32 hblank;
6994 u32 hsync;
6995 u32 vtotal;
6996 u32 vblank;
6997 u32 vsync;
6998 } pipe[2];
6999
7000 struct intel_plane_error_state {
7001 u32 control;
7002 u32 stride;
7003 u32 size;
7004 u32 pos;
7005 u32 addr;
7006 u32 surface;
7007 u32 tile_offset;
7008 } plane[2];
7009};
7010
7011struct intel_display_error_state *
7012intel_display_capture_error_state(struct drm_device *dev)
7013{
7014 drm_i915_private_t *dev_priv = dev->dev_private;
7015 struct intel_display_error_state *error;
7016 int i;
7017
7018 error = kmalloc(sizeof(*error), GFP_ATOMIC);
7019 if (error == NULL)
7020 return NULL;
7021
7022 for (i = 0; i < 2; i++) {
7023 error->cursor[i].control = I915_READ(CURCNTR(i));
7024 error->cursor[i].position = I915_READ(CURPOS(i));
7025 error->cursor[i].base = I915_READ(CURBASE(i));
7026
7027 error->plane[i].control = I915_READ(DSPCNTR(i));
7028 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
7029 error->plane[i].size = I915_READ(DSPSIZE(i));
7030 error->plane[i].pos= I915_READ(DSPPOS(i));
7031 error->plane[i].addr = I915_READ(DSPADDR(i));
7032 if (INTEL_INFO(dev)->gen >= 4) {
7033 error->plane[i].surface = I915_READ(DSPSURF(i));
7034 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
7035 }
7036
7037 error->pipe[i].conf = I915_READ(PIPECONF(i));
7038 error->pipe[i].source = I915_READ(PIPESRC(i));
7039 error->pipe[i].htotal = I915_READ(HTOTAL(i));
7040 error->pipe[i].hblank = I915_READ(HBLANK(i));
7041 error->pipe[i].hsync = I915_READ(HSYNC(i));
7042 error->pipe[i].vtotal = I915_READ(VTOTAL(i));
7043 error->pipe[i].vblank = I915_READ(VBLANK(i));
7044 error->pipe[i].vsync = I915_READ(VSYNC(i));
7045 }
7046
7047 return error;
7048}
7049
7050void
7051intel_display_print_error_state(struct seq_file *m,
7052 struct drm_device *dev,
7053 struct intel_display_error_state *error)
7054{
7055 int i;
7056
7057 for (i = 0; i < 2; i++) {
7058 seq_printf(m, "Pipe [%d]:\n", i);
7059 seq_printf(m, " CONF: %08x\n", error->pipe[i].conf);
7060 seq_printf(m, " SRC: %08x\n", error->pipe[i].source);
7061 seq_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
7062 seq_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
7063 seq_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
7064 seq_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
7065 seq_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
7066 seq_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
7067
7068 seq_printf(m, "Plane [%d]:\n", i);
7069 seq_printf(m, " CNTR: %08x\n", error->plane[i].control);
7070 seq_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
7071 seq_printf(m, " SIZE: %08x\n", error->plane[i].size);
7072 seq_printf(m, " POS: %08x\n", error->plane[i].pos);
7073 seq_printf(m, " ADDR: %08x\n", error->plane[i].addr);
7074 if (INTEL_INFO(dev)->gen >= 4) {
7075 seq_printf(m, " SURF: %08x\n", error->plane[i].surface);
7076 seq_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
7077 }
7078
7079 seq_printf(m, "Cursor [%d]:\n", i);
7080 seq_printf(m, " CNTR: %08x\n", error->cursor[i].control);
7081 seq_printf(m, " POS: %08x\n", error->cursor[i].position);
7082 seq_printf(m, " BASE: %08x\n", error->cursor[i].base);
7083 }
7084}
7085#endif