blob: 3b006536b3d23cda6c771de53f423345f35b9c89 [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.com0c2e3952009-07-17 11:44:30 +0800100#define I8XX_P2_LVDS_FAST 7
Jesse Barnes79e53942008-11-07 14:24:08 -0800101#define I8XX_P2_SLOW_LIMIT 165000
102
103#define I9XX_DOT_MIN 20000
104#define I9XX_DOT_MAX 400000
105#define I9XX_VCO_MIN 1400000
106#define I9XX_VCO_MAX 2800000
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500107#define PINEVIEW_VCO_MIN 1700000
108#define PINEVIEW_VCO_MAX 3500000
Kristian Høgsbergf3cade52009-02-13 20:56:50 -0500109#define I9XX_N_MIN 1
110#define I9XX_N_MAX 6
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500111/* Pineview's Ncounter is a ring counter */
112#define PINEVIEW_N_MIN 3
113#define PINEVIEW_N_MAX 6
Jesse Barnes79e53942008-11-07 14:24:08 -0800114#define I9XX_M_MIN 70
115#define I9XX_M_MAX 120
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500116#define PINEVIEW_M_MIN 2
117#define PINEVIEW_M_MAX 256
Jesse Barnes79e53942008-11-07 14:24:08 -0800118#define I9XX_M1_MIN 10
Kristian Høgsbergf3cade52009-02-13 20:56:50 -0500119#define I9XX_M1_MAX 22
Jesse Barnes79e53942008-11-07 14:24:08 -0800120#define I9XX_M2_MIN 5
121#define I9XX_M2_MAX 9
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500122/* Pineview M1 is reserved, and must be 0 */
123#define PINEVIEW_M1_MIN 0
124#define PINEVIEW_M1_MAX 0
125#define PINEVIEW_M2_MIN 0
126#define PINEVIEW_M2_MAX 254
Jesse Barnes79e53942008-11-07 14:24:08 -0800127#define I9XX_P_SDVO_DAC_MIN 5
128#define I9XX_P_SDVO_DAC_MAX 80
129#define I9XX_P_LVDS_MIN 7
130#define I9XX_P_LVDS_MAX 98
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500131#define PINEVIEW_P_LVDS_MIN 7
132#define PINEVIEW_P_LVDS_MAX 112
Jesse Barnes79e53942008-11-07 14:24:08 -0800133#define I9XX_P1_MIN 1
134#define I9XX_P1_MAX 8
135#define I9XX_P2_SDVO_DAC_SLOW 10
136#define I9XX_P2_SDVO_DAC_FAST 5
137#define I9XX_P2_SDVO_DAC_SLOW_LIMIT 200000
138#define I9XX_P2_LVDS_SLOW 14
139#define I9XX_P2_LVDS_FAST 7
140#define I9XX_P2_LVDS_SLOW_LIMIT 112000
141
Ma Ling044c7c42009-03-18 20:13:23 +0800142/*The parameter is for SDVO on G4x platform*/
143#define G4X_DOT_SDVO_MIN 25000
144#define G4X_DOT_SDVO_MAX 270000
145#define G4X_VCO_MIN 1750000
146#define G4X_VCO_MAX 3500000
147#define G4X_N_SDVO_MIN 1
148#define G4X_N_SDVO_MAX 4
149#define G4X_M_SDVO_MIN 104
150#define G4X_M_SDVO_MAX 138
151#define G4X_M1_SDVO_MIN 17
152#define G4X_M1_SDVO_MAX 23
153#define G4X_M2_SDVO_MIN 5
154#define G4X_M2_SDVO_MAX 11
155#define G4X_P_SDVO_MIN 10
156#define G4X_P_SDVO_MAX 30
157#define G4X_P1_SDVO_MIN 1
158#define G4X_P1_SDVO_MAX 3
159#define G4X_P2_SDVO_SLOW 10
160#define G4X_P2_SDVO_FAST 10
161#define G4X_P2_SDVO_LIMIT 270000
162
163/*The parameter is for HDMI_DAC on G4x platform*/
164#define G4X_DOT_HDMI_DAC_MIN 22000
165#define G4X_DOT_HDMI_DAC_MAX 400000
166#define G4X_N_HDMI_DAC_MIN 1
167#define G4X_N_HDMI_DAC_MAX 4
168#define G4X_M_HDMI_DAC_MIN 104
169#define G4X_M_HDMI_DAC_MAX 138
170#define G4X_M1_HDMI_DAC_MIN 16
171#define G4X_M1_HDMI_DAC_MAX 23
172#define G4X_M2_HDMI_DAC_MIN 5
173#define G4X_M2_HDMI_DAC_MAX 11
174#define G4X_P_HDMI_DAC_MIN 5
175#define G4X_P_HDMI_DAC_MAX 80
176#define G4X_P1_HDMI_DAC_MIN 1
177#define G4X_P1_HDMI_DAC_MAX 8
178#define G4X_P2_HDMI_DAC_SLOW 10
179#define G4X_P2_HDMI_DAC_FAST 5
180#define G4X_P2_HDMI_DAC_LIMIT 165000
181
182/*The parameter is for SINGLE_CHANNEL_LVDS on G4x platform*/
183#define G4X_DOT_SINGLE_CHANNEL_LVDS_MIN 20000
184#define G4X_DOT_SINGLE_CHANNEL_LVDS_MAX 115000
185#define G4X_N_SINGLE_CHANNEL_LVDS_MIN 1
186#define G4X_N_SINGLE_CHANNEL_LVDS_MAX 3
187#define G4X_M_SINGLE_CHANNEL_LVDS_MIN 104
188#define G4X_M_SINGLE_CHANNEL_LVDS_MAX 138
189#define G4X_M1_SINGLE_CHANNEL_LVDS_MIN 17
190#define G4X_M1_SINGLE_CHANNEL_LVDS_MAX 23
191#define G4X_M2_SINGLE_CHANNEL_LVDS_MIN 5
192#define G4X_M2_SINGLE_CHANNEL_LVDS_MAX 11
193#define G4X_P_SINGLE_CHANNEL_LVDS_MIN 28
194#define G4X_P_SINGLE_CHANNEL_LVDS_MAX 112
195#define G4X_P1_SINGLE_CHANNEL_LVDS_MIN 2
196#define G4X_P1_SINGLE_CHANNEL_LVDS_MAX 8
197#define G4X_P2_SINGLE_CHANNEL_LVDS_SLOW 14
198#define G4X_P2_SINGLE_CHANNEL_LVDS_FAST 14
199#define G4X_P2_SINGLE_CHANNEL_LVDS_LIMIT 0
200
201/*The parameter is for DUAL_CHANNEL_LVDS on G4x platform*/
202#define G4X_DOT_DUAL_CHANNEL_LVDS_MIN 80000
203#define G4X_DOT_DUAL_CHANNEL_LVDS_MAX 224000
204#define G4X_N_DUAL_CHANNEL_LVDS_MIN 1
205#define G4X_N_DUAL_CHANNEL_LVDS_MAX 3
206#define G4X_M_DUAL_CHANNEL_LVDS_MIN 104
207#define G4X_M_DUAL_CHANNEL_LVDS_MAX 138
208#define G4X_M1_DUAL_CHANNEL_LVDS_MIN 17
209#define G4X_M1_DUAL_CHANNEL_LVDS_MAX 23
210#define G4X_M2_DUAL_CHANNEL_LVDS_MIN 5
211#define G4X_M2_DUAL_CHANNEL_LVDS_MAX 11
212#define G4X_P_DUAL_CHANNEL_LVDS_MIN 14
213#define G4X_P_DUAL_CHANNEL_LVDS_MAX 42
214#define G4X_P1_DUAL_CHANNEL_LVDS_MIN 2
215#define G4X_P1_DUAL_CHANNEL_LVDS_MAX 6
216#define G4X_P2_DUAL_CHANNEL_LVDS_SLOW 7
217#define G4X_P2_DUAL_CHANNEL_LVDS_FAST 7
218#define G4X_P2_DUAL_CHANNEL_LVDS_LIMIT 0
219
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700220/*The parameter is for DISPLAY PORT on G4x platform*/
221#define G4X_DOT_DISPLAY_PORT_MIN 161670
222#define G4X_DOT_DISPLAY_PORT_MAX 227000
223#define G4X_N_DISPLAY_PORT_MIN 1
224#define G4X_N_DISPLAY_PORT_MAX 2
225#define G4X_M_DISPLAY_PORT_MIN 97
226#define G4X_M_DISPLAY_PORT_MAX 108
227#define G4X_M1_DISPLAY_PORT_MIN 0x10
228#define G4X_M1_DISPLAY_PORT_MAX 0x12
229#define G4X_M2_DISPLAY_PORT_MIN 0x05
230#define G4X_M2_DISPLAY_PORT_MAX 0x06
231#define G4X_P_DISPLAY_PORT_MIN 10
232#define G4X_P_DISPLAY_PORT_MAX 20
233#define G4X_P1_DISPLAY_PORT_MIN 1
234#define G4X_P1_DISPLAY_PORT_MAX 2
235#define G4X_P2_DISPLAY_PORT_SLOW 10
236#define G4X_P2_DISPLAY_PORT_FAST 10
237#define G4X_P2_DISPLAY_PORT_LIMIT 0
238
Eric Anholtbad720f2009-10-22 16:11:14 -0700239/* Ironlake / Sandybridge */
Zhenyu Wang2c072452009-06-05 15:38:42 +0800240/* as we calculate clock using (register_value + 2) for
241 N/M1/M2, so here the range value for them is (actual_value-2).
242 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500243#define IRONLAKE_DOT_MIN 25000
244#define IRONLAKE_DOT_MAX 350000
245#define IRONLAKE_VCO_MIN 1760000
246#define IRONLAKE_VCO_MAX 3510000
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500247#define IRONLAKE_M1_MIN 12
Zhao Yakuia59e3852010-01-06 22:05:57 +0800248#define IRONLAKE_M1_MAX 22
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500249#define IRONLAKE_M2_MIN 5
250#define IRONLAKE_M2_MAX 9
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500251#define IRONLAKE_P2_DOT_LIMIT 225000 /* 225Mhz */
Zhenyu Wang2c072452009-06-05 15:38:42 +0800252
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800253/* We have parameter ranges for different type of outputs. */
254
255/* DAC & HDMI Refclk 120Mhz */
256#define IRONLAKE_DAC_N_MIN 1
257#define IRONLAKE_DAC_N_MAX 5
258#define IRONLAKE_DAC_M_MIN 79
259#define IRONLAKE_DAC_M_MAX 127
260#define IRONLAKE_DAC_P_MIN 5
261#define IRONLAKE_DAC_P_MAX 80
262#define IRONLAKE_DAC_P1_MIN 1
263#define IRONLAKE_DAC_P1_MAX 8
264#define IRONLAKE_DAC_P2_SLOW 10
265#define IRONLAKE_DAC_P2_FAST 5
266
267/* LVDS single-channel 120Mhz refclk */
268#define IRONLAKE_LVDS_S_N_MIN 1
269#define IRONLAKE_LVDS_S_N_MAX 3
270#define IRONLAKE_LVDS_S_M_MIN 79
271#define IRONLAKE_LVDS_S_M_MAX 118
272#define IRONLAKE_LVDS_S_P_MIN 28
273#define IRONLAKE_LVDS_S_P_MAX 112
274#define IRONLAKE_LVDS_S_P1_MIN 2
275#define IRONLAKE_LVDS_S_P1_MAX 8
276#define IRONLAKE_LVDS_S_P2_SLOW 14
277#define IRONLAKE_LVDS_S_P2_FAST 14
278
279/* LVDS dual-channel 120Mhz refclk */
280#define IRONLAKE_LVDS_D_N_MIN 1
281#define IRONLAKE_LVDS_D_N_MAX 3
282#define IRONLAKE_LVDS_D_M_MIN 79
283#define IRONLAKE_LVDS_D_M_MAX 127
284#define IRONLAKE_LVDS_D_P_MIN 14
285#define IRONLAKE_LVDS_D_P_MAX 56
286#define IRONLAKE_LVDS_D_P1_MIN 2
287#define IRONLAKE_LVDS_D_P1_MAX 8
288#define IRONLAKE_LVDS_D_P2_SLOW 7
289#define IRONLAKE_LVDS_D_P2_FAST 7
290
291/* LVDS single-channel 100Mhz refclk */
292#define IRONLAKE_LVDS_S_SSC_N_MIN 1
293#define IRONLAKE_LVDS_S_SSC_N_MAX 2
294#define IRONLAKE_LVDS_S_SSC_M_MIN 79
295#define IRONLAKE_LVDS_S_SSC_M_MAX 126
296#define IRONLAKE_LVDS_S_SSC_P_MIN 28
297#define IRONLAKE_LVDS_S_SSC_P_MAX 112
298#define IRONLAKE_LVDS_S_SSC_P1_MIN 2
299#define IRONLAKE_LVDS_S_SSC_P1_MAX 8
300#define IRONLAKE_LVDS_S_SSC_P2_SLOW 14
301#define IRONLAKE_LVDS_S_SSC_P2_FAST 14
302
303/* LVDS dual-channel 100Mhz refclk */
304#define IRONLAKE_LVDS_D_SSC_N_MIN 1
305#define IRONLAKE_LVDS_D_SSC_N_MAX 3
306#define IRONLAKE_LVDS_D_SSC_M_MIN 79
307#define IRONLAKE_LVDS_D_SSC_M_MAX 126
308#define IRONLAKE_LVDS_D_SSC_P_MIN 14
309#define IRONLAKE_LVDS_D_SSC_P_MAX 42
310#define IRONLAKE_LVDS_D_SSC_P1_MIN 2
311#define IRONLAKE_LVDS_D_SSC_P1_MAX 6
312#define IRONLAKE_LVDS_D_SSC_P2_SLOW 7
313#define IRONLAKE_LVDS_D_SSC_P2_FAST 7
314
315/* DisplayPort */
316#define IRONLAKE_DP_N_MIN 1
317#define IRONLAKE_DP_N_MAX 2
318#define IRONLAKE_DP_M_MIN 81
319#define IRONLAKE_DP_M_MAX 90
320#define IRONLAKE_DP_P_MIN 10
321#define IRONLAKE_DP_P_MAX 20
322#define IRONLAKE_DP_P2_FAST 10
323#define IRONLAKE_DP_P2_SLOW 10
324#define IRONLAKE_DP_P2_LIMIT 0
325#define IRONLAKE_DP_P1_MIN 1
326#define IRONLAKE_DP_P1_MAX 2
Zhao Yakui45476682009-12-31 16:06:04 +0800327
Jesse Barnes2377b742010-07-07 14:06:43 -0700328/* FDI */
329#define IRONLAKE_FDI_FREQ 2700000 /* in kHz for mode->clock */
330
Ma Lingd4906092009-03-18 20:13:27 +0800331static bool
332intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
333 int target, int refclk, intel_clock_t *best_clock);
334static bool
335intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
336 int target, int refclk, intel_clock_t *best_clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800337
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700338static bool
339intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
340 int target, int refclk, intel_clock_t *best_clock);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800341static bool
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500342intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,
343 int target, int refclk, intel_clock_t *best_clock);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700344
Chris Wilson021357a2010-09-07 20:54:59 +0100345static inline u32 /* units of 100MHz */
346intel_fdi_link_freq(struct drm_device *dev)
347{
Chris Wilson8b99e682010-10-13 09:59:17 +0100348 if (IS_GEN5(dev)) {
349 struct drm_i915_private *dev_priv = dev->dev_private;
350 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
351 } else
352 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100353}
354
Keith Packarde4b36692009-06-05 19:22:17 -0700355static const intel_limit_t intel_limits_i8xx_dvo = {
Jesse Barnes79e53942008-11-07 14:24:08 -0800356 .dot = { .min = I8XX_DOT_MIN, .max = I8XX_DOT_MAX },
357 .vco = { .min = I8XX_VCO_MIN, .max = I8XX_VCO_MAX },
358 .n = { .min = I8XX_N_MIN, .max = I8XX_N_MAX },
359 .m = { .min = I8XX_M_MIN, .max = I8XX_M_MAX },
360 .m1 = { .min = I8XX_M1_MIN, .max = I8XX_M1_MAX },
361 .m2 = { .min = I8XX_M2_MIN, .max = I8XX_M2_MAX },
362 .p = { .min = I8XX_P_MIN, .max = I8XX_P_MAX },
363 .p1 = { .min = I8XX_P1_MIN, .max = I8XX_P1_MAX },
364 .p2 = { .dot_limit = I8XX_P2_SLOW_LIMIT,
365 .p2_slow = I8XX_P2_SLOW, .p2_fast = I8XX_P2_FAST },
Ma Lingd4906092009-03-18 20:13:27 +0800366 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700367};
368
369static const intel_limit_t intel_limits_i8xx_lvds = {
Jesse Barnes79e53942008-11-07 14:24:08 -0800370 .dot = { .min = I8XX_DOT_MIN, .max = I8XX_DOT_MAX },
371 .vco = { .min = I8XX_VCO_MIN, .max = I8XX_VCO_MAX },
372 .n = { .min = I8XX_N_MIN, .max = I8XX_N_MAX },
373 .m = { .min = I8XX_M_MIN, .max = I8XX_M_MAX },
374 .m1 = { .min = I8XX_M1_MIN, .max = I8XX_M1_MAX },
375 .m2 = { .min = I8XX_M2_MIN, .max = I8XX_M2_MAX },
376 .p = { .min = I8XX_P_MIN, .max = I8XX_P_MAX },
377 .p1 = { .min = I8XX_P1_LVDS_MIN, .max = I8XX_P1_LVDS_MAX },
378 .p2 = { .dot_limit = I8XX_P2_SLOW_LIMIT,
379 .p2_slow = I8XX_P2_LVDS_SLOW, .p2_fast = I8XX_P2_LVDS_FAST },
Ma Lingd4906092009-03-18 20:13:27 +0800380 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700381};
382
383static const intel_limit_t intel_limits_i9xx_sdvo = {
Jesse Barnes79e53942008-11-07 14:24:08 -0800384 .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
385 .vco = { .min = I9XX_VCO_MIN, .max = I9XX_VCO_MAX },
386 .n = { .min = I9XX_N_MIN, .max = I9XX_N_MAX },
387 .m = { .min = I9XX_M_MIN, .max = I9XX_M_MAX },
388 .m1 = { .min = I9XX_M1_MIN, .max = I9XX_M1_MAX },
389 .m2 = { .min = I9XX_M2_MIN, .max = I9XX_M2_MAX },
390 .p = { .min = I9XX_P_SDVO_DAC_MIN, .max = I9XX_P_SDVO_DAC_MAX },
391 .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
392 .p2 = { .dot_limit = I9XX_P2_SDVO_DAC_SLOW_LIMIT,
393 .p2_slow = I9XX_P2_SDVO_DAC_SLOW, .p2_fast = I9XX_P2_SDVO_DAC_FAST },
Ma Lingd4906092009-03-18 20:13:27 +0800394 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700395};
396
397static const intel_limit_t intel_limits_i9xx_lvds = {
Jesse Barnes79e53942008-11-07 14:24:08 -0800398 .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
399 .vco = { .min = I9XX_VCO_MIN, .max = I9XX_VCO_MAX },
400 .n = { .min = I9XX_N_MIN, .max = I9XX_N_MAX },
401 .m = { .min = I9XX_M_MIN, .max = I9XX_M_MAX },
402 .m1 = { .min = I9XX_M1_MIN, .max = I9XX_M1_MAX },
403 .m2 = { .min = I9XX_M2_MIN, .max = I9XX_M2_MAX },
404 .p = { .min = I9XX_P_LVDS_MIN, .max = I9XX_P_LVDS_MAX },
405 .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
406 /* The single-channel range is 25-112Mhz, and dual-channel
407 * is 80-224Mhz. Prefer single channel as much as possible.
408 */
409 .p2 = { .dot_limit = I9XX_P2_LVDS_SLOW_LIMIT,
410 .p2_slow = I9XX_P2_LVDS_SLOW, .p2_fast = I9XX_P2_LVDS_FAST },
Ma Lingd4906092009-03-18 20:13:27 +0800411 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700412};
413
Ma Ling044c7c42009-03-18 20:13:23 +0800414 /* below parameter and function is for G4X Chipset Family*/
Keith Packarde4b36692009-06-05 19:22:17 -0700415static const intel_limit_t intel_limits_g4x_sdvo = {
Ma Ling044c7c42009-03-18 20:13:23 +0800416 .dot = { .min = G4X_DOT_SDVO_MIN, .max = G4X_DOT_SDVO_MAX },
417 .vco = { .min = G4X_VCO_MIN, .max = G4X_VCO_MAX},
418 .n = { .min = G4X_N_SDVO_MIN, .max = G4X_N_SDVO_MAX },
419 .m = { .min = G4X_M_SDVO_MIN, .max = G4X_M_SDVO_MAX },
420 .m1 = { .min = G4X_M1_SDVO_MIN, .max = G4X_M1_SDVO_MAX },
421 .m2 = { .min = G4X_M2_SDVO_MIN, .max = G4X_M2_SDVO_MAX },
422 .p = { .min = G4X_P_SDVO_MIN, .max = G4X_P_SDVO_MAX },
423 .p1 = { .min = G4X_P1_SDVO_MIN, .max = G4X_P1_SDVO_MAX},
424 .p2 = { .dot_limit = G4X_P2_SDVO_LIMIT,
425 .p2_slow = G4X_P2_SDVO_SLOW,
426 .p2_fast = G4X_P2_SDVO_FAST
427 },
Ma Lingd4906092009-03-18 20:13:27 +0800428 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700429};
430
431static const intel_limit_t intel_limits_g4x_hdmi = {
Ma Ling044c7c42009-03-18 20:13:23 +0800432 .dot = { .min = G4X_DOT_HDMI_DAC_MIN, .max = G4X_DOT_HDMI_DAC_MAX },
433 .vco = { .min = G4X_VCO_MIN, .max = G4X_VCO_MAX},
434 .n = { .min = G4X_N_HDMI_DAC_MIN, .max = G4X_N_HDMI_DAC_MAX },
435 .m = { .min = G4X_M_HDMI_DAC_MIN, .max = G4X_M_HDMI_DAC_MAX },
436 .m1 = { .min = G4X_M1_HDMI_DAC_MIN, .max = G4X_M1_HDMI_DAC_MAX },
437 .m2 = { .min = G4X_M2_HDMI_DAC_MIN, .max = G4X_M2_HDMI_DAC_MAX },
438 .p = { .min = G4X_P_HDMI_DAC_MIN, .max = G4X_P_HDMI_DAC_MAX },
439 .p1 = { .min = G4X_P1_HDMI_DAC_MIN, .max = G4X_P1_HDMI_DAC_MAX},
440 .p2 = { .dot_limit = G4X_P2_HDMI_DAC_LIMIT,
441 .p2_slow = G4X_P2_HDMI_DAC_SLOW,
442 .p2_fast = G4X_P2_HDMI_DAC_FAST
443 },
Ma Lingd4906092009-03-18 20:13:27 +0800444 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700445};
446
447static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Ma Ling044c7c42009-03-18 20:13:23 +0800448 .dot = { .min = G4X_DOT_SINGLE_CHANNEL_LVDS_MIN,
449 .max = G4X_DOT_SINGLE_CHANNEL_LVDS_MAX },
450 .vco = { .min = G4X_VCO_MIN,
451 .max = G4X_VCO_MAX },
452 .n = { .min = G4X_N_SINGLE_CHANNEL_LVDS_MIN,
453 .max = G4X_N_SINGLE_CHANNEL_LVDS_MAX },
454 .m = { .min = G4X_M_SINGLE_CHANNEL_LVDS_MIN,
455 .max = G4X_M_SINGLE_CHANNEL_LVDS_MAX },
456 .m1 = { .min = G4X_M1_SINGLE_CHANNEL_LVDS_MIN,
457 .max = G4X_M1_SINGLE_CHANNEL_LVDS_MAX },
458 .m2 = { .min = G4X_M2_SINGLE_CHANNEL_LVDS_MIN,
459 .max = G4X_M2_SINGLE_CHANNEL_LVDS_MAX },
460 .p = { .min = G4X_P_SINGLE_CHANNEL_LVDS_MIN,
461 .max = G4X_P_SINGLE_CHANNEL_LVDS_MAX },
462 .p1 = { .min = G4X_P1_SINGLE_CHANNEL_LVDS_MIN,
463 .max = G4X_P1_SINGLE_CHANNEL_LVDS_MAX },
464 .p2 = { .dot_limit = G4X_P2_SINGLE_CHANNEL_LVDS_LIMIT,
465 .p2_slow = G4X_P2_SINGLE_CHANNEL_LVDS_SLOW,
466 .p2_fast = G4X_P2_SINGLE_CHANNEL_LVDS_FAST
467 },
Ma Lingd4906092009-03-18 20:13:27 +0800468 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700469};
470
471static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Ma Ling044c7c42009-03-18 20:13:23 +0800472 .dot = { .min = G4X_DOT_DUAL_CHANNEL_LVDS_MIN,
473 .max = G4X_DOT_DUAL_CHANNEL_LVDS_MAX },
474 .vco = { .min = G4X_VCO_MIN,
475 .max = G4X_VCO_MAX },
476 .n = { .min = G4X_N_DUAL_CHANNEL_LVDS_MIN,
477 .max = G4X_N_DUAL_CHANNEL_LVDS_MAX },
478 .m = { .min = G4X_M_DUAL_CHANNEL_LVDS_MIN,
479 .max = G4X_M_DUAL_CHANNEL_LVDS_MAX },
480 .m1 = { .min = G4X_M1_DUAL_CHANNEL_LVDS_MIN,
481 .max = G4X_M1_DUAL_CHANNEL_LVDS_MAX },
482 .m2 = { .min = G4X_M2_DUAL_CHANNEL_LVDS_MIN,
483 .max = G4X_M2_DUAL_CHANNEL_LVDS_MAX },
484 .p = { .min = G4X_P_DUAL_CHANNEL_LVDS_MIN,
485 .max = G4X_P_DUAL_CHANNEL_LVDS_MAX },
486 .p1 = { .min = G4X_P1_DUAL_CHANNEL_LVDS_MIN,
487 .max = G4X_P1_DUAL_CHANNEL_LVDS_MAX },
488 .p2 = { .dot_limit = G4X_P2_DUAL_CHANNEL_LVDS_LIMIT,
489 .p2_slow = G4X_P2_DUAL_CHANNEL_LVDS_SLOW,
490 .p2_fast = G4X_P2_DUAL_CHANNEL_LVDS_FAST
491 },
Ma Lingd4906092009-03-18 20:13:27 +0800492 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700493};
494
495static const intel_limit_t intel_limits_g4x_display_port = {
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700496 .dot = { .min = G4X_DOT_DISPLAY_PORT_MIN,
497 .max = G4X_DOT_DISPLAY_PORT_MAX },
498 .vco = { .min = G4X_VCO_MIN,
499 .max = G4X_VCO_MAX},
500 .n = { .min = G4X_N_DISPLAY_PORT_MIN,
501 .max = G4X_N_DISPLAY_PORT_MAX },
502 .m = { .min = G4X_M_DISPLAY_PORT_MIN,
503 .max = G4X_M_DISPLAY_PORT_MAX },
504 .m1 = { .min = G4X_M1_DISPLAY_PORT_MIN,
505 .max = G4X_M1_DISPLAY_PORT_MAX },
506 .m2 = { .min = G4X_M2_DISPLAY_PORT_MIN,
507 .max = G4X_M2_DISPLAY_PORT_MAX },
508 .p = { .min = G4X_P_DISPLAY_PORT_MIN,
509 .max = G4X_P_DISPLAY_PORT_MAX },
510 .p1 = { .min = G4X_P1_DISPLAY_PORT_MIN,
511 .max = G4X_P1_DISPLAY_PORT_MAX},
512 .p2 = { .dot_limit = G4X_P2_DISPLAY_PORT_LIMIT,
513 .p2_slow = G4X_P2_DISPLAY_PORT_SLOW,
514 .p2_fast = G4X_P2_DISPLAY_PORT_FAST },
515 .find_pll = intel_find_pll_g4x_dp,
Keith Packarde4b36692009-06-05 19:22:17 -0700516};
517
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500518static const intel_limit_t intel_limits_pineview_sdvo = {
Shaohua Li21778322009-02-23 15:19:16 +0800519 .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX},
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500520 .vco = { .min = PINEVIEW_VCO_MIN, .max = PINEVIEW_VCO_MAX },
521 .n = { .min = PINEVIEW_N_MIN, .max = PINEVIEW_N_MAX },
522 .m = { .min = PINEVIEW_M_MIN, .max = PINEVIEW_M_MAX },
523 .m1 = { .min = PINEVIEW_M1_MIN, .max = PINEVIEW_M1_MAX },
524 .m2 = { .min = PINEVIEW_M2_MIN, .max = PINEVIEW_M2_MAX },
Shaohua Li21778322009-02-23 15:19:16 +0800525 .p = { .min = I9XX_P_SDVO_DAC_MIN, .max = I9XX_P_SDVO_DAC_MAX },
526 .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
527 .p2 = { .dot_limit = I9XX_P2_SDVO_DAC_SLOW_LIMIT,
528 .p2_slow = I9XX_P2_SDVO_DAC_SLOW, .p2_fast = I9XX_P2_SDVO_DAC_FAST },
Shaohua Li61157072009-04-03 15:24:43 +0800529 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700530};
531
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500532static const intel_limit_t intel_limits_pineview_lvds = {
Shaohua Li21778322009-02-23 15:19:16 +0800533 .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500534 .vco = { .min = PINEVIEW_VCO_MIN, .max = PINEVIEW_VCO_MAX },
535 .n = { .min = PINEVIEW_N_MIN, .max = PINEVIEW_N_MAX },
536 .m = { .min = PINEVIEW_M_MIN, .max = PINEVIEW_M_MAX },
537 .m1 = { .min = PINEVIEW_M1_MIN, .max = PINEVIEW_M1_MAX },
538 .m2 = { .min = PINEVIEW_M2_MIN, .max = PINEVIEW_M2_MAX },
539 .p = { .min = PINEVIEW_P_LVDS_MIN, .max = PINEVIEW_P_LVDS_MAX },
Shaohua Li21778322009-02-23 15:19:16 +0800540 .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500541 /* Pineview only supports single-channel mode. */
Shaohua Li21778322009-02-23 15:19:16 +0800542 .p2 = { .dot_limit = I9XX_P2_LVDS_SLOW_LIMIT,
543 .p2_slow = I9XX_P2_LVDS_SLOW, .p2_fast = I9XX_P2_LVDS_SLOW },
Shaohua Li61157072009-04-03 15:24:43 +0800544 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700545};
546
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800547static const intel_limit_t intel_limits_ironlake_dac = {
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500548 .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
549 .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800550 .n = { .min = IRONLAKE_DAC_N_MIN, .max = IRONLAKE_DAC_N_MAX },
551 .m = { .min = IRONLAKE_DAC_M_MIN, .max = IRONLAKE_DAC_M_MAX },
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500552 .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
553 .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800554 .p = { .min = IRONLAKE_DAC_P_MIN, .max = IRONLAKE_DAC_P_MAX },
555 .p1 = { .min = IRONLAKE_DAC_P1_MIN, .max = IRONLAKE_DAC_P1_MAX },
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500556 .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800557 .p2_slow = IRONLAKE_DAC_P2_SLOW,
558 .p2_fast = IRONLAKE_DAC_P2_FAST },
Zhao Yakui45476682009-12-31 16:06:04 +0800559 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700560};
561
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800562static const intel_limit_t intel_limits_ironlake_single_lvds = {
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500563 .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
564 .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800565 .n = { .min = IRONLAKE_LVDS_S_N_MIN, .max = IRONLAKE_LVDS_S_N_MAX },
566 .m = { .min = IRONLAKE_LVDS_S_M_MIN, .max = IRONLAKE_LVDS_S_M_MAX },
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500567 .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
568 .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800569 .p = { .min = IRONLAKE_LVDS_S_P_MIN, .max = IRONLAKE_LVDS_S_P_MAX },
570 .p1 = { .min = IRONLAKE_LVDS_S_P1_MIN, .max = IRONLAKE_LVDS_S_P1_MAX },
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500571 .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800572 .p2_slow = IRONLAKE_LVDS_S_P2_SLOW,
573 .p2_fast = IRONLAKE_LVDS_S_P2_FAST },
574 .find_pll = intel_g4x_find_best_PLL,
575};
576
577static const intel_limit_t intel_limits_ironlake_dual_lvds = {
578 .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
579 .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
580 .n = { .min = IRONLAKE_LVDS_D_N_MIN, .max = IRONLAKE_LVDS_D_N_MAX },
581 .m = { .min = IRONLAKE_LVDS_D_M_MIN, .max = IRONLAKE_LVDS_D_M_MAX },
582 .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
583 .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
584 .p = { .min = IRONLAKE_LVDS_D_P_MIN, .max = IRONLAKE_LVDS_D_P_MAX },
585 .p1 = { .min = IRONLAKE_LVDS_D_P1_MIN, .max = IRONLAKE_LVDS_D_P1_MAX },
586 .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
587 .p2_slow = IRONLAKE_LVDS_D_P2_SLOW,
588 .p2_fast = IRONLAKE_LVDS_D_P2_FAST },
589 .find_pll = intel_g4x_find_best_PLL,
590};
591
592static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
593 .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
594 .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
595 .n = { .min = IRONLAKE_LVDS_S_SSC_N_MIN, .max = IRONLAKE_LVDS_S_SSC_N_MAX },
596 .m = { .min = IRONLAKE_LVDS_S_SSC_M_MIN, .max = IRONLAKE_LVDS_S_SSC_M_MAX },
597 .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
598 .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
599 .p = { .min = IRONLAKE_LVDS_S_SSC_P_MIN, .max = IRONLAKE_LVDS_S_SSC_P_MAX },
600 .p1 = { .min = IRONLAKE_LVDS_S_SSC_P1_MIN,.max = IRONLAKE_LVDS_S_SSC_P1_MAX },
601 .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
602 .p2_slow = IRONLAKE_LVDS_S_SSC_P2_SLOW,
603 .p2_fast = IRONLAKE_LVDS_S_SSC_P2_FAST },
604 .find_pll = intel_g4x_find_best_PLL,
605};
606
607static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
608 .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
609 .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
610 .n = { .min = IRONLAKE_LVDS_D_SSC_N_MIN, .max = IRONLAKE_LVDS_D_SSC_N_MAX },
611 .m = { .min = IRONLAKE_LVDS_D_SSC_M_MIN, .max = IRONLAKE_LVDS_D_SSC_M_MAX },
612 .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
613 .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
614 .p = { .min = IRONLAKE_LVDS_D_SSC_P_MIN, .max = IRONLAKE_LVDS_D_SSC_P_MAX },
615 .p1 = { .min = IRONLAKE_LVDS_D_SSC_P1_MIN,.max = IRONLAKE_LVDS_D_SSC_P1_MAX },
616 .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
617 .p2_slow = IRONLAKE_LVDS_D_SSC_P2_SLOW,
618 .p2_fast = IRONLAKE_LVDS_D_SSC_P2_FAST },
Zhao Yakui45476682009-12-31 16:06:04 +0800619 .find_pll = intel_g4x_find_best_PLL,
620};
621
622static const intel_limit_t intel_limits_ironlake_display_port = {
623 .dot = { .min = IRONLAKE_DOT_MIN,
624 .max = IRONLAKE_DOT_MAX },
625 .vco = { .min = IRONLAKE_VCO_MIN,
626 .max = IRONLAKE_VCO_MAX},
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800627 .n = { .min = IRONLAKE_DP_N_MIN,
628 .max = IRONLAKE_DP_N_MAX },
629 .m = { .min = IRONLAKE_DP_M_MIN,
630 .max = IRONLAKE_DP_M_MAX },
Zhao Yakui45476682009-12-31 16:06:04 +0800631 .m1 = { .min = IRONLAKE_M1_MIN,
632 .max = IRONLAKE_M1_MAX },
633 .m2 = { .min = IRONLAKE_M2_MIN,
634 .max = IRONLAKE_M2_MAX },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800635 .p = { .min = IRONLAKE_DP_P_MIN,
636 .max = IRONLAKE_DP_P_MAX },
637 .p1 = { .min = IRONLAKE_DP_P1_MIN,
638 .max = IRONLAKE_DP_P1_MAX},
639 .p2 = { .dot_limit = IRONLAKE_DP_P2_LIMIT,
640 .p2_slow = IRONLAKE_DP_P2_SLOW,
641 .p2_fast = IRONLAKE_DP_P2_FAST },
Zhao Yakui45476682009-12-31 16:06:04 +0800642 .find_pll = intel_find_pll_ironlake_dp,
Jesse Barnes79e53942008-11-07 14:24:08 -0800643};
644
Chris Wilson1b894b52010-12-14 20:04:54 +0000645static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
646 int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800647{
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800648 struct drm_device *dev = crtc->dev;
649 struct drm_i915_private *dev_priv = dev->dev_private;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800650 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800651
652 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800653 if ((I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) ==
654 LVDS_CLKB_POWER_UP) {
655 /* LVDS dual channel */
Chris Wilson1b894b52010-12-14 20:04:54 +0000656 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800657 limit = &intel_limits_ironlake_dual_lvds_100m;
658 else
659 limit = &intel_limits_ironlake_dual_lvds;
660 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000661 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800662 limit = &intel_limits_ironlake_single_lvds_100m;
663 else
664 limit = &intel_limits_ironlake_single_lvds;
665 }
666 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
Zhao Yakui45476682009-12-31 16:06:04 +0800667 HAS_eDP)
668 limit = &intel_limits_ironlake_display_port;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800669 else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800670 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800671
672 return limit;
673}
674
Ma Ling044c7c42009-03-18 20:13:23 +0800675static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
676{
677 struct drm_device *dev = crtc->dev;
678 struct drm_i915_private *dev_priv = dev->dev_private;
679 const intel_limit_t *limit;
680
681 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
682 if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
683 LVDS_CLKB_POWER_UP)
684 /* LVDS with dual channel */
Keith Packarde4b36692009-06-05 19:22:17 -0700685 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800686 else
687 /* LVDS with dual channel */
Keith Packarde4b36692009-06-05 19:22:17 -0700688 limit = &intel_limits_g4x_single_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800689 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
690 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700691 limit = &intel_limits_g4x_hdmi;
Ma Ling044c7c42009-03-18 20:13:23 +0800692 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700693 limit = &intel_limits_g4x_sdvo;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700694 } else if (intel_pipe_has_type (crtc, INTEL_OUTPUT_DISPLAYPORT)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700695 limit = &intel_limits_g4x_display_port;
Ma Ling044c7c42009-03-18 20:13:23 +0800696 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700697 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800698
699 return limit;
700}
701
Chris Wilson1b894b52010-12-14 20:04:54 +0000702static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800703{
704 struct drm_device *dev = crtc->dev;
705 const intel_limit_t *limit;
706
Eric Anholtbad720f2009-10-22 16:11:14 -0700707 if (HAS_PCH_SPLIT(dev))
Chris Wilson1b894b52010-12-14 20:04:54 +0000708 limit = intel_ironlake_limit(crtc, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800709 else if (IS_G4X(dev)) {
Ma Ling044c7c42009-03-18 20:13:23 +0800710 limit = intel_g4x_limit(crtc);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500711 } else if (IS_PINEVIEW(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +0800712 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500713 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800714 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500715 limit = &intel_limits_pineview_sdvo;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100716 } else if (!IS_GEN2(dev)) {
717 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
718 limit = &intel_limits_i9xx_lvds;
719 else
720 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800721 } else {
722 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700723 limit = &intel_limits_i8xx_lvds;
Jesse Barnes79e53942008-11-07 14:24:08 -0800724 else
Keith Packarde4b36692009-06-05 19:22:17 -0700725 limit = &intel_limits_i8xx_dvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800726 }
727 return limit;
728}
729
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500730/* m1 is reserved as 0 in Pineview, n is a ring counter */
731static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800732{
Shaohua Li21778322009-02-23 15:19:16 +0800733 clock->m = clock->m2 + 2;
734 clock->p = clock->p1 * clock->p2;
735 clock->vco = refclk * clock->m / clock->n;
736 clock->dot = clock->vco / clock->p;
737}
738
739static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
740{
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500741 if (IS_PINEVIEW(dev)) {
742 pineview_clock(refclk, clock);
Shaohua Li21778322009-02-23 15:19:16 +0800743 return;
744 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800745 clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
746 clock->p = clock->p1 * clock->p2;
747 clock->vco = refclk * clock->m / (clock->n + 2);
748 clock->dot = clock->vco / clock->p;
749}
750
Jesse Barnes79e53942008-11-07 14:24:08 -0800751/**
752 * Returns whether any output on the specified pipe is of the specified type
753 */
Chris Wilson4ef69c72010-09-09 15:14:28 +0100754bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
Jesse Barnes79e53942008-11-07 14:24:08 -0800755{
Chris Wilson4ef69c72010-09-09 15:14:28 +0100756 struct drm_device *dev = crtc->dev;
757 struct drm_mode_config *mode_config = &dev->mode_config;
758 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -0800759
Chris Wilson4ef69c72010-09-09 15:14:28 +0100760 list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
761 if (encoder->base.crtc == crtc && encoder->type == type)
762 return true;
763
764 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -0800765}
766
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800767#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800768/**
769 * Returns whether the given set of divisors are valid for a given refclk with
770 * the given connectors.
771 */
772
Chris Wilson1b894b52010-12-14 20:04:54 +0000773static bool intel_PLL_is_valid(struct drm_device *dev,
774 const intel_limit_t *limit,
775 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800776{
Jesse Barnes79e53942008-11-07 14:24:08 -0800777 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
778 INTELPllInvalid ("p1 out of range\n");
779 if (clock->p < limit->p.min || limit->p.max < clock->p)
780 INTELPllInvalid ("p out of range\n");
781 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
782 INTELPllInvalid ("m2 out of range\n");
783 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
784 INTELPllInvalid ("m1 out of range\n");
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500785 if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800786 INTELPllInvalid ("m1 <= m2\n");
787 if (clock->m < limit->m.min || limit->m.max < clock->m)
788 INTELPllInvalid ("m out of range\n");
789 if (clock->n < limit->n.min || limit->n.max < clock->n)
790 INTELPllInvalid ("n out of range\n");
791 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
792 INTELPllInvalid ("vco out of range\n");
793 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
794 * connector, etc., rather than just a single range.
795 */
796 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
797 INTELPllInvalid ("dot out of range\n");
798
799 return true;
800}
801
Ma Lingd4906092009-03-18 20:13:27 +0800802static bool
803intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
804 int target, int refclk, intel_clock_t *best_clock)
805
Jesse Barnes79e53942008-11-07 14:24:08 -0800806{
807 struct drm_device *dev = crtc->dev;
808 struct drm_i915_private *dev_priv = dev->dev_private;
809 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800810 int err = target;
811
Bruno Prémontbc5e5712009-08-08 13:01:17 +0200812 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Florian Mickler832cc282009-07-13 18:40:32 +0800813 (I915_READ(LVDS)) != 0) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800814 /*
815 * For LVDS, if the panel is on, just rely on its current
816 * settings for dual-channel. We haven't figured out how to
817 * reliably set up different single/dual channel state, if we
818 * even can.
819 */
820 if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
821 LVDS_CLKB_POWER_UP)
822 clock.p2 = limit->p2.p2_fast;
823 else
824 clock.p2 = limit->p2.p2_slow;
825 } else {
826 if (target < limit->p2.dot_limit)
827 clock.p2 = limit->p2.p2_slow;
828 else
829 clock.p2 = limit->p2.p2_fast;
830 }
831
832 memset (best_clock, 0, sizeof (*best_clock));
833
Zhao Yakui42158662009-11-20 11:24:18 +0800834 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
835 clock.m1++) {
836 for (clock.m2 = limit->m2.min;
837 clock.m2 <= limit->m2.max; clock.m2++) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500838 /* m1 is always 0 in Pineview */
839 if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
Zhao Yakui42158662009-11-20 11:24:18 +0800840 break;
841 for (clock.n = limit->n.min;
842 clock.n <= limit->n.max; clock.n++) {
843 for (clock.p1 = limit->p1.min;
844 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800845 int this_err;
846
Shaohua Li21778322009-02-23 15:19:16 +0800847 intel_clock(dev, refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000848 if (!intel_PLL_is_valid(dev, limit,
849 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800850 continue;
851
852 this_err = abs(clock.dot - target);
853 if (this_err < err) {
854 *best_clock = clock;
855 err = this_err;
856 }
857 }
858 }
859 }
860 }
861
862 return (err != target);
863}
864
Ma Lingd4906092009-03-18 20:13:27 +0800865static bool
866intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
867 int target, int refclk, intel_clock_t *best_clock)
868{
869 struct drm_device *dev = crtc->dev;
870 struct drm_i915_private *dev_priv = dev->dev_private;
871 intel_clock_t clock;
872 int max_n;
873 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400874 /* approximately equals target * 0.00585 */
875 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800876 found = false;
877
878 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Zhao Yakui45476682009-12-31 16:06:04 +0800879 int lvds_reg;
880
Eric Anholtc619eed2010-01-28 16:45:52 -0800881 if (HAS_PCH_SPLIT(dev))
Zhao Yakui45476682009-12-31 16:06:04 +0800882 lvds_reg = PCH_LVDS;
883 else
884 lvds_reg = LVDS;
885 if ((I915_READ(lvds_reg) & LVDS_CLKB_POWER_MASK) ==
Ma Lingd4906092009-03-18 20:13:27 +0800886 LVDS_CLKB_POWER_UP)
887 clock.p2 = limit->p2.p2_fast;
888 else
889 clock.p2 = limit->p2.p2_slow;
890 } else {
891 if (target < limit->p2.dot_limit)
892 clock.p2 = limit->p2.p2_slow;
893 else
894 clock.p2 = limit->p2.p2_fast;
895 }
896
897 memset(best_clock, 0, sizeof(*best_clock));
898 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200899 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800900 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200901 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800902 for (clock.m1 = limit->m1.max;
903 clock.m1 >= limit->m1.min; clock.m1--) {
904 for (clock.m2 = limit->m2.max;
905 clock.m2 >= limit->m2.min; clock.m2--) {
906 for (clock.p1 = limit->p1.max;
907 clock.p1 >= limit->p1.min; clock.p1--) {
908 int this_err;
909
Shaohua Li21778322009-02-23 15:19:16 +0800910 intel_clock(dev, refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000911 if (!intel_PLL_is_valid(dev, limit,
912 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800913 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000914
915 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800916 if (this_err < err_most) {
917 *best_clock = clock;
918 err_most = this_err;
919 max_n = clock.n;
920 found = true;
921 }
922 }
923 }
924 }
925 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800926 return found;
927}
Ma Lingd4906092009-03-18 20:13:27 +0800928
Zhenyu Wang2c072452009-06-05 15:38:42 +0800929static bool
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500930intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
931 int target, int refclk, intel_clock_t *best_clock)
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800932{
933 struct drm_device *dev = crtc->dev;
934 intel_clock_t clock;
Zhao Yakui45476682009-12-31 16:06:04 +0800935
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800936 if (target < 200000) {
937 clock.n = 1;
938 clock.p1 = 2;
939 clock.p2 = 10;
940 clock.m1 = 12;
941 clock.m2 = 9;
942 } else {
943 clock.n = 2;
944 clock.p1 = 1;
945 clock.p2 = 10;
946 clock.m1 = 14;
947 clock.m2 = 8;
948 }
949 intel_clock(dev, refclk, &clock);
950 memcpy(best_clock, &clock, sizeof(intel_clock_t));
951 return true;
952}
953
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700954/* DisplayPort has only two frequencies, 162MHz and 270MHz */
955static bool
956intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
957 int target, int refclk, intel_clock_t *best_clock)
958{
Chris Wilson5eddb702010-09-11 13:48:45 +0100959 intel_clock_t clock;
960 if (target < 200000) {
961 clock.p1 = 2;
962 clock.p2 = 10;
963 clock.n = 2;
964 clock.m1 = 23;
965 clock.m2 = 8;
966 } else {
967 clock.p1 = 1;
968 clock.p2 = 10;
969 clock.n = 1;
970 clock.m1 = 14;
971 clock.m2 = 2;
972 }
973 clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
974 clock.p = (clock.p1 * clock.p2);
975 clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
976 clock.vco = 0;
977 memcpy(best_clock, &clock, sizeof(intel_clock_t));
978 return true;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700979}
980
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700981/**
982 * intel_wait_for_vblank - wait for vblank on a given pipe
983 * @dev: drm device
984 * @pipe: pipe to wait for
985 *
986 * Wait for vblank to occur on a given pipe. Needed for various bits of
987 * mode setting code.
988 */
989void intel_wait_for_vblank(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -0800990{
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700991 struct drm_i915_private *dev_priv = dev->dev_private;
992 int pipestat_reg = (pipe == 0 ? PIPEASTAT : PIPEBSTAT);
993
Chris Wilson300387c2010-09-05 20:25:43 +0100994 /* Clear existing vblank status. Note this will clear any other
995 * sticky status fields as well.
996 *
997 * This races with i915_driver_irq_handler() with the result
998 * that either function could miss a vblank event. Here it is not
999 * fatal, as we will either wait upon the next vblank interrupt or
1000 * timeout. Generally speaking intel_wait_for_vblank() is only
1001 * called during modeset at which time the GPU should be idle and
1002 * should *not* be performing page flips and thus not waiting on
1003 * vblanks...
1004 * Currently, the result of us stealing a vblank from the irq
1005 * handler is that a single frame will be skipped during swapbuffers.
1006 */
1007 I915_WRITE(pipestat_reg,
1008 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
1009
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001010 /* Wait for vblank interrupt bit to set */
Chris Wilson481b6af2010-08-23 17:43:35 +01001011 if (wait_for(I915_READ(pipestat_reg) &
1012 PIPE_VBLANK_INTERRUPT_STATUS,
1013 50))
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001014 DRM_DEBUG_KMS("vblank wait timed out\n");
1015}
1016
Keith Packardab7ad7f2010-10-03 00:33:06 -07001017/*
1018 * intel_wait_for_pipe_off - wait for pipe to turn off
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001019 * @dev: drm device
1020 * @pipe: pipe to wait for
1021 *
1022 * After disabling a pipe, we can't wait for vblank in the usual way,
1023 * spinning on the vblank interrupt status bit, since we won't actually
1024 * see an interrupt when the pipe is disabled.
1025 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001026 * On Gen4 and above:
1027 * wait for the pipe register state bit to turn off
1028 *
1029 * Otherwise:
1030 * wait for the display line value to settle (it usually
1031 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001032 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001033 */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001034void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001035{
1036 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001037
Keith Packardab7ad7f2010-10-03 00:33:06 -07001038 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson58e10eb2010-10-03 10:56:11 +01001039 int reg = PIPECONF(pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001040
Keith Packardab7ad7f2010-10-03 00:33:06 -07001041 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001042 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1043 100))
Keith Packardab7ad7f2010-10-03 00:33:06 -07001044 DRM_DEBUG_KMS("pipe_off wait timed out\n");
1045 } else {
1046 u32 last_line;
Chris Wilson58e10eb2010-10-03 10:56:11 +01001047 int reg = PIPEDSL(pipe);
Keith Packardab7ad7f2010-10-03 00:33:06 -07001048 unsigned long timeout = jiffies + msecs_to_jiffies(100);
1049
1050 /* Wait for the display line to settle */
1051 do {
Chris Wilson58e10eb2010-10-03 10:56:11 +01001052 last_line = I915_READ(reg) & DSL_LINEMASK;
Keith Packardab7ad7f2010-10-03 00:33:06 -07001053 mdelay(5);
Chris Wilson58e10eb2010-10-03 10:56:11 +01001054 } while (((I915_READ(reg) & DSL_LINEMASK) != last_line) &&
Keith Packardab7ad7f2010-10-03 00:33:06 -07001055 time_after(timeout, jiffies));
1056 if (time_after(jiffies, timeout))
1057 DRM_DEBUG_KMS("pipe_off wait timed out\n");
1058 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001059}
1060
Jesse Barnes80824002009-09-10 15:28:06 -07001061static void i8xx_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
1062{
1063 struct drm_device *dev = crtc->dev;
1064 struct drm_i915_private *dev_priv = dev->dev_private;
1065 struct drm_framebuffer *fb = crtc->fb;
1066 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00001067 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes80824002009-09-10 15:28:06 -07001068 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1069 int plane, i;
1070 u32 fbc_ctl, fbc_ctl2;
1071
Chris Wilsonbed4a672010-09-11 10:47:47 +01001072 if (fb->pitch == dev_priv->cfb_pitch &&
Chris Wilson05394f32010-11-08 19:18:58 +00001073 obj->fence_reg == dev_priv->cfb_fence &&
Chris Wilsonbed4a672010-09-11 10:47:47 +01001074 intel_crtc->plane == dev_priv->cfb_plane &&
1075 I915_READ(FBC_CONTROL) & FBC_CTL_EN)
1076 return;
1077
1078 i8xx_disable_fbc(dev);
1079
Jesse Barnes80824002009-09-10 15:28:06 -07001080 dev_priv->cfb_pitch = dev_priv->cfb_size / FBC_LL_SIZE;
1081
1082 if (fb->pitch < dev_priv->cfb_pitch)
1083 dev_priv->cfb_pitch = fb->pitch;
1084
1085 /* FBC_CTL wants 64B units */
1086 dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
Chris Wilson05394f32010-11-08 19:18:58 +00001087 dev_priv->cfb_fence = obj->fence_reg;
Jesse Barnes80824002009-09-10 15:28:06 -07001088 dev_priv->cfb_plane = intel_crtc->plane;
1089 plane = dev_priv->cfb_plane == 0 ? FBC_CTL_PLANEA : FBC_CTL_PLANEB;
1090
1091 /* Clear old tags */
1092 for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
1093 I915_WRITE(FBC_TAG + (i * 4), 0);
1094
1095 /* Set it up... */
1096 fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | plane;
Chris Wilson05394f32010-11-08 19:18:58 +00001097 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes80824002009-09-10 15:28:06 -07001098 fbc_ctl2 |= FBC_CTL_CPU_FENCE;
1099 I915_WRITE(FBC_CONTROL2, fbc_ctl2);
1100 I915_WRITE(FBC_FENCE_OFF, crtc->y);
1101
1102 /* enable it... */
1103 fbc_ctl = FBC_CTL_EN | FBC_CTL_PERIODIC;
Jesse Barnesee25df22010-02-06 10:41:53 -08001104 if (IS_I945GM(dev))
Priit Laes49677902010-03-02 11:37:00 +02001105 fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
Jesse Barnes80824002009-09-10 15:28:06 -07001106 fbc_ctl |= (dev_priv->cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
1107 fbc_ctl |= (interval & 0x2fff) << FBC_CTL_INTERVAL_SHIFT;
Chris Wilson05394f32010-11-08 19:18:58 +00001108 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes80824002009-09-10 15:28:06 -07001109 fbc_ctl |= dev_priv->cfb_fence;
1110 I915_WRITE(FBC_CONTROL, fbc_ctl);
1111
Zhao Yakui28c97732009-10-09 11:39:41 +08001112 DRM_DEBUG_KMS("enabled FBC, pitch %ld, yoff %d, plane %d, ",
Chris Wilson5eddb702010-09-11 13:48:45 +01001113 dev_priv->cfb_pitch, crtc->y, dev_priv->cfb_plane);
Jesse Barnes80824002009-09-10 15:28:06 -07001114}
1115
1116void i8xx_disable_fbc(struct drm_device *dev)
1117{
1118 struct drm_i915_private *dev_priv = dev->dev_private;
1119 u32 fbc_ctl;
1120
1121 /* Disable compression */
1122 fbc_ctl = I915_READ(FBC_CONTROL);
Chris Wilsona5cad622010-09-22 13:15:10 +01001123 if ((fbc_ctl & FBC_CTL_EN) == 0)
1124 return;
1125
Jesse Barnes80824002009-09-10 15:28:06 -07001126 fbc_ctl &= ~FBC_CTL_EN;
1127 I915_WRITE(FBC_CONTROL, fbc_ctl);
1128
1129 /* Wait for compressing bit to clear */
Chris Wilson481b6af2010-08-23 17:43:35 +01001130 if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
Chris Wilson913d8d12010-08-07 11:01:35 +01001131 DRM_DEBUG_KMS("FBC idle timed out\n");
1132 return;
Jesse Barnes9517a922010-05-21 09:40:45 -07001133 }
Jesse Barnes80824002009-09-10 15:28:06 -07001134
Zhao Yakui28c97732009-10-09 11:39:41 +08001135 DRM_DEBUG_KMS("disabled FBC\n");
Jesse Barnes80824002009-09-10 15:28:06 -07001136}
1137
Adam Jacksonee5382a2010-04-23 11:17:39 -04001138static bool i8xx_fbc_enabled(struct drm_device *dev)
Jesse Barnes80824002009-09-10 15:28:06 -07001139{
Jesse Barnes80824002009-09-10 15:28:06 -07001140 struct drm_i915_private *dev_priv = dev->dev_private;
1141
1142 return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
1143}
1144
Jesse Barnes74dff282009-09-14 15:39:40 -07001145static void g4x_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
1146{
1147 struct drm_device *dev = crtc->dev;
1148 struct drm_i915_private *dev_priv = dev->dev_private;
1149 struct drm_framebuffer *fb = crtc->fb;
1150 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00001151 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes74dff282009-09-14 15:39:40 -07001152 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5eddb702010-09-11 13:48:45 +01001153 int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
Jesse Barnes74dff282009-09-14 15:39:40 -07001154 unsigned long stall_watermark = 200;
1155 u32 dpfc_ctl;
1156
Chris Wilsonbed4a672010-09-11 10:47:47 +01001157 dpfc_ctl = I915_READ(DPFC_CONTROL);
1158 if (dpfc_ctl & DPFC_CTL_EN) {
1159 if (dev_priv->cfb_pitch == dev_priv->cfb_pitch / 64 - 1 &&
Chris Wilson05394f32010-11-08 19:18:58 +00001160 dev_priv->cfb_fence == obj->fence_reg &&
Chris Wilsonbed4a672010-09-11 10:47:47 +01001161 dev_priv->cfb_plane == intel_crtc->plane &&
1162 dev_priv->cfb_y == crtc->y)
1163 return;
1164
1165 I915_WRITE(DPFC_CONTROL, dpfc_ctl & ~DPFC_CTL_EN);
1166 POSTING_READ(DPFC_CONTROL);
1167 intel_wait_for_vblank(dev, intel_crtc->pipe);
1168 }
1169
Jesse Barnes74dff282009-09-14 15:39:40 -07001170 dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
Chris Wilson05394f32010-11-08 19:18:58 +00001171 dev_priv->cfb_fence = obj->fence_reg;
Jesse Barnes74dff282009-09-14 15:39:40 -07001172 dev_priv->cfb_plane = intel_crtc->plane;
Chris Wilsonbed4a672010-09-11 10:47:47 +01001173 dev_priv->cfb_y = crtc->y;
Jesse Barnes74dff282009-09-14 15:39:40 -07001174
1175 dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;
Chris Wilson05394f32010-11-08 19:18:58 +00001176 if (obj->tiling_mode != I915_TILING_NONE) {
Jesse Barnes74dff282009-09-14 15:39:40 -07001177 dpfc_ctl |= DPFC_CTL_FENCE_EN | dev_priv->cfb_fence;
1178 I915_WRITE(DPFC_CHICKEN, DPFC_HT_MODIFY);
1179 } else {
1180 I915_WRITE(DPFC_CHICKEN, ~DPFC_HT_MODIFY);
1181 }
1182
Jesse Barnes74dff282009-09-14 15:39:40 -07001183 I915_WRITE(DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
1184 (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
1185 (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
1186 I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
1187
1188 /* enable it... */
1189 I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);
1190
Zhao Yakui28c97732009-10-09 11:39:41 +08001191 DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
Jesse Barnes74dff282009-09-14 15:39:40 -07001192}
1193
1194void g4x_disable_fbc(struct drm_device *dev)
1195{
1196 struct drm_i915_private *dev_priv = dev->dev_private;
1197 u32 dpfc_ctl;
1198
1199 /* Disable compression */
1200 dpfc_ctl = I915_READ(DPFC_CONTROL);
Chris Wilsonbed4a672010-09-11 10:47:47 +01001201 if (dpfc_ctl & DPFC_CTL_EN) {
1202 dpfc_ctl &= ~DPFC_CTL_EN;
1203 I915_WRITE(DPFC_CONTROL, dpfc_ctl);
Jesse Barnes74dff282009-09-14 15:39:40 -07001204
Chris Wilsonbed4a672010-09-11 10:47:47 +01001205 DRM_DEBUG_KMS("disabled FBC\n");
1206 }
Jesse Barnes74dff282009-09-14 15:39:40 -07001207}
1208
Adam Jacksonee5382a2010-04-23 11:17:39 -04001209static bool g4x_fbc_enabled(struct drm_device *dev)
Jesse Barnes74dff282009-09-14 15:39:40 -07001210{
Jesse Barnes74dff282009-09-14 15:39:40 -07001211 struct drm_i915_private *dev_priv = dev->dev_private;
1212
1213 return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
1214}
1215
Jesse Barnes4efe0702011-01-18 11:25:41 -08001216static void sandybridge_blit_fbc_update(struct drm_device *dev)
1217{
1218 struct drm_i915_private *dev_priv = dev->dev_private;
1219 u32 blt_ecoskpd;
1220
1221 /* Make sure blitter notifies FBC of writes */
1222 __gen6_force_wake_get(dev_priv);
1223 blt_ecoskpd = I915_READ(GEN6_BLITTER_ECOSKPD);
1224 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY <<
1225 GEN6_BLITTER_LOCK_SHIFT;
1226 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
1227 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY;
1228 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
1229 blt_ecoskpd &= ~(GEN6_BLITTER_FBC_NOTIFY <<
1230 GEN6_BLITTER_LOCK_SHIFT);
1231 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
1232 POSTING_READ(GEN6_BLITTER_ECOSKPD);
1233 __gen6_force_wake_put(dev_priv);
1234}
1235
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001236static void ironlake_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
1237{
1238 struct drm_device *dev = crtc->dev;
1239 struct drm_i915_private *dev_priv = dev->dev_private;
1240 struct drm_framebuffer *fb = crtc->fb;
1241 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00001242 struct drm_i915_gem_object *obj = intel_fb->obj;
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001243 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5eddb702010-09-11 13:48:45 +01001244 int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001245 unsigned long stall_watermark = 200;
1246 u32 dpfc_ctl;
1247
Chris Wilsonbed4a672010-09-11 10:47:47 +01001248 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
1249 if (dpfc_ctl & DPFC_CTL_EN) {
1250 if (dev_priv->cfb_pitch == dev_priv->cfb_pitch / 64 - 1 &&
Chris Wilson05394f32010-11-08 19:18:58 +00001251 dev_priv->cfb_fence == obj->fence_reg &&
Chris Wilsonbed4a672010-09-11 10:47:47 +01001252 dev_priv->cfb_plane == intel_crtc->plane &&
Chris Wilson05394f32010-11-08 19:18:58 +00001253 dev_priv->cfb_offset == obj->gtt_offset &&
Chris Wilsonbed4a672010-09-11 10:47:47 +01001254 dev_priv->cfb_y == crtc->y)
1255 return;
1256
1257 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl & ~DPFC_CTL_EN);
1258 POSTING_READ(ILK_DPFC_CONTROL);
1259 intel_wait_for_vblank(dev, intel_crtc->pipe);
1260 }
1261
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001262 dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
Chris Wilson05394f32010-11-08 19:18:58 +00001263 dev_priv->cfb_fence = obj->fence_reg;
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001264 dev_priv->cfb_plane = intel_crtc->plane;
Chris Wilson05394f32010-11-08 19:18:58 +00001265 dev_priv->cfb_offset = obj->gtt_offset;
Chris Wilsonbed4a672010-09-11 10:47:47 +01001266 dev_priv->cfb_y = crtc->y;
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001267
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001268 dpfc_ctl &= DPFC_RESERVED;
1269 dpfc_ctl |= (plane | DPFC_CTL_LIMIT_1X);
Chris Wilson05394f32010-11-08 19:18:58 +00001270 if (obj->tiling_mode != I915_TILING_NONE) {
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001271 dpfc_ctl |= (DPFC_CTL_FENCE_EN | dev_priv->cfb_fence);
1272 I915_WRITE(ILK_DPFC_CHICKEN, DPFC_HT_MODIFY);
1273 } else {
1274 I915_WRITE(ILK_DPFC_CHICKEN, ~DPFC_HT_MODIFY);
1275 }
1276
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001277 I915_WRITE(ILK_DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
1278 (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
1279 (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
1280 I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
Chris Wilson05394f32010-11-08 19:18:58 +00001281 I915_WRITE(ILK_FBC_RT_BASE, obj->gtt_offset | ILK_FBC_RT_VALID);
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001282 /* enable it... */
Chris Wilsonbed4a672010-09-11 10:47:47 +01001283 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001284
Yuanhan Liu9c04f012010-12-15 15:42:32 +08001285 if (IS_GEN6(dev)) {
1286 I915_WRITE(SNB_DPFC_CTL_SA,
1287 SNB_CPU_FENCE_ENABLE | dev_priv->cfb_fence);
1288 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
Jesse Barnes4efe0702011-01-18 11:25:41 -08001289 sandybridge_blit_fbc_update(dev);
Yuanhan Liu9c04f012010-12-15 15:42:32 +08001290 }
1291
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001292 DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
1293}
1294
1295void ironlake_disable_fbc(struct drm_device *dev)
1296{
1297 struct drm_i915_private *dev_priv = dev->dev_private;
1298 u32 dpfc_ctl;
1299
1300 /* Disable compression */
1301 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
Chris Wilsonbed4a672010-09-11 10:47:47 +01001302 if (dpfc_ctl & DPFC_CTL_EN) {
1303 dpfc_ctl &= ~DPFC_CTL_EN;
1304 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001305
Chris Wilsonbed4a672010-09-11 10:47:47 +01001306 DRM_DEBUG_KMS("disabled FBC\n");
1307 }
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001308}
1309
1310static bool ironlake_fbc_enabled(struct drm_device *dev)
1311{
1312 struct drm_i915_private *dev_priv = dev->dev_private;
1313
1314 return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
1315}
1316
Adam Jacksonee5382a2010-04-23 11:17:39 -04001317bool intel_fbc_enabled(struct drm_device *dev)
1318{
1319 struct drm_i915_private *dev_priv = dev->dev_private;
1320
1321 if (!dev_priv->display.fbc_enabled)
1322 return false;
1323
1324 return dev_priv->display.fbc_enabled(dev);
1325}
1326
1327void intel_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
1328{
1329 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
1330
1331 if (!dev_priv->display.enable_fbc)
1332 return;
1333
1334 dev_priv->display.enable_fbc(crtc, interval);
1335}
1336
1337void intel_disable_fbc(struct drm_device *dev)
1338{
1339 struct drm_i915_private *dev_priv = dev->dev_private;
1340
1341 if (!dev_priv->display.disable_fbc)
1342 return;
1343
1344 dev_priv->display.disable_fbc(dev);
1345}
1346
Jesse Barnes80824002009-09-10 15:28:06 -07001347/**
1348 * intel_update_fbc - enable/disable FBC as needed
Chris Wilsonbed4a672010-09-11 10:47:47 +01001349 * @dev: the drm_device
Jesse Barnes80824002009-09-10 15:28:06 -07001350 *
1351 * Set up the framebuffer compression hardware at mode set time. We
1352 * enable it if possible:
1353 * - plane A only (on pre-965)
1354 * - no pixel mulitply/line duplication
1355 * - no alpha buffer discard
1356 * - no dual wide
1357 * - framebuffer <= 2048 in width, 1536 in height
1358 *
1359 * We can't assume that any compression will take place (worst case),
1360 * so the compressed buffer has to be the same size as the uncompressed
1361 * one. It also must reside (along with the line length buffer) in
1362 * stolen memory.
1363 *
1364 * We need to enable/disable FBC on a global basis.
1365 */
Chris Wilsonbed4a672010-09-11 10:47:47 +01001366static void intel_update_fbc(struct drm_device *dev)
Jesse Barnes80824002009-09-10 15:28:06 -07001367{
Jesse Barnes80824002009-09-10 15:28:06 -07001368 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonbed4a672010-09-11 10:47:47 +01001369 struct drm_crtc *crtc = NULL, *tmp_crtc;
1370 struct intel_crtc *intel_crtc;
1371 struct drm_framebuffer *fb;
Jesse Barnes80824002009-09-10 15:28:06 -07001372 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00001373 struct drm_i915_gem_object *obj;
Jesse Barnes9c928d12010-07-23 15:20:00 -07001374
1375 DRM_DEBUG_KMS("\n");
Jesse Barnes80824002009-09-10 15:28:06 -07001376
1377 if (!i915_powersave)
1378 return;
1379
Adam Jacksonee5382a2010-04-23 11:17:39 -04001380 if (!I915_HAS_FBC(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07001381 return;
1382
Jesse Barnes80824002009-09-10 15:28:06 -07001383 /*
1384 * If FBC is already on, we just have to verify that we can
1385 * keep it that way...
1386 * Need to disable if:
Jesse Barnes9c928d12010-07-23 15:20:00 -07001387 * - more than one pipe is active
Jesse Barnes80824002009-09-10 15:28:06 -07001388 * - changing FBC params (stride, fence, mode)
1389 * - new fb is too large to fit in compressed buffer
1390 * - going to an unsupported config (interlace, pixel multiply, etc.)
1391 */
Jesse Barnes9c928d12010-07-23 15:20:00 -07001392 list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head) {
Chris Wilsonbed4a672010-09-11 10:47:47 +01001393 if (tmp_crtc->enabled) {
1394 if (crtc) {
1395 DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
1396 dev_priv->no_fbc_reason = FBC_MULTIPLE_PIPES;
1397 goto out_disable;
1398 }
1399 crtc = tmp_crtc;
1400 }
Jesse Barnes9c928d12010-07-23 15:20:00 -07001401 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01001402
1403 if (!crtc || crtc->fb == NULL) {
1404 DRM_DEBUG_KMS("no output, disabling\n");
1405 dev_priv->no_fbc_reason = FBC_NO_OUTPUT;
Jesse Barnes9c928d12010-07-23 15:20:00 -07001406 goto out_disable;
1407 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01001408
1409 intel_crtc = to_intel_crtc(crtc);
1410 fb = crtc->fb;
1411 intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00001412 obj = intel_fb->obj;
Chris Wilsonbed4a672010-09-11 10:47:47 +01001413
Chris Wilson05394f32010-11-08 19:18:58 +00001414 if (intel_fb->obj->base.size > dev_priv->cfb_size) {
Zhao Yakui28c97732009-10-09 11:39:41 +08001415 DRM_DEBUG_KMS("framebuffer too large, disabling "
Chris Wilson5eddb702010-09-11 13:48:45 +01001416 "compression\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08001417 dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
Jesse Barnes80824002009-09-10 15:28:06 -07001418 goto out_disable;
1419 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01001420 if ((crtc->mode.flags & DRM_MODE_FLAG_INTERLACE) ||
1421 (crtc->mode.flags & DRM_MODE_FLAG_DBLSCAN)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08001422 DRM_DEBUG_KMS("mode incompatible with compression, "
Chris Wilson5eddb702010-09-11 13:48:45 +01001423 "disabling\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08001424 dev_priv->no_fbc_reason = FBC_UNSUPPORTED_MODE;
Jesse Barnes80824002009-09-10 15:28:06 -07001425 goto out_disable;
1426 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01001427 if ((crtc->mode.hdisplay > 2048) ||
1428 (crtc->mode.vdisplay > 1536)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08001429 DRM_DEBUG_KMS("mode too large for compression, disabling\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08001430 dev_priv->no_fbc_reason = FBC_MODE_TOO_LARGE;
Jesse Barnes80824002009-09-10 15:28:06 -07001431 goto out_disable;
1432 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01001433 if ((IS_I915GM(dev) || IS_I945GM(dev)) && intel_crtc->plane != 0) {
Zhao Yakui28c97732009-10-09 11:39:41 +08001434 DRM_DEBUG_KMS("plane not 0, disabling compression\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08001435 dev_priv->no_fbc_reason = FBC_BAD_PLANE;
Jesse Barnes80824002009-09-10 15:28:06 -07001436 goto out_disable;
1437 }
Chris Wilson05394f32010-11-08 19:18:58 +00001438 if (obj->tiling_mode != I915_TILING_X) {
Zhao Yakui28c97732009-10-09 11:39:41 +08001439 DRM_DEBUG_KMS("framebuffer not tiled, disabling compression\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08001440 dev_priv->no_fbc_reason = FBC_NOT_TILED;
Jesse Barnes80824002009-09-10 15:28:06 -07001441 goto out_disable;
1442 }
1443
Jason Wesselc924b932010-08-05 09:22:32 -05001444 /* If the kernel debugger is active, always disable compression */
1445 if (in_dbg_master())
1446 goto out_disable;
1447
Chris Wilsonbed4a672010-09-11 10:47:47 +01001448 intel_enable_fbc(crtc, 500);
Jesse Barnes80824002009-09-10 15:28:06 -07001449 return;
1450
1451out_disable:
Jesse Barnes80824002009-09-10 15:28:06 -07001452 /* Multiple disables should be harmless */
Chris Wilsona9394062010-05-27 13:18:16 +01001453 if (intel_fbc_enabled(dev)) {
1454 DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
Adam Jacksonee5382a2010-04-23 11:17:39 -04001455 intel_disable_fbc(dev);
Chris Wilsona9394062010-05-27 13:18:16 +01001456 }
Jesse Barnes80824002009-09-10 15:28:06 -07001457}
1458
Chris Wilson127bd2a2010-07-23 23:32:05 +01001459int
Chris Wilson48b956c2010-09-14 12:50:34 +01001460intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00001461 struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00001462 struct intel_ring_buffer *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001463{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001464 u32 alignment;
1465 int ret;
1466
Chris Wilson05394f32010-11-08 19:18:58 +00001467 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001468 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01001469 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1470 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001471 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01001472 alignment = 4 * 1024;
1473 else
1474 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001475 break;
1476 case I915_TILING_X:
1477 /* pin() will align the object as required by fence */
1478 alignment = 0;
1479 break;
1480 case I915_TILING_Y:
1481 /* FIXME: Is this true? */
1482 DRM_ERROR("Y tiled not allowed for scan out buffers\n");
1483 return -EINVAL;
1484 default:
1485 BUG();
1486 }
1487
Daniel Vetter75e9e912010-11-04 17:11:09 +01001488 ret = i915_gem_object_pin(obj, alignment, true);
Chris Wilson48b956c2010-09-14 12:50:34 +01001489 if (ret)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001490 return ret;
1491
Chris Wilson48b956c2010-09-14 12:50:34 +01001492 ret = i915_gem_object_set_to_display_plane(obj, pipelined);
1493 if (ret)
1494 goto err_unpin;
Chris Wilson72133422010-09-13 23:56:38 +01001495
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001496 /* Install a fence for tiled scan-out. Pre-i965 always needs a
1497 * fence, whereas 965+ only requires a fence if using
1498 * framebuffer compression. For simplicity, we always install
1499 * a fence as the cost is not that onerous.
1500 */
Chris Wilson05394f32010-11-08 19:18:58 +00001501 if (obj->tiling_mode != I915_TILING_NONE) {
Chris Wilsond9e86c02010-11-10 16:40:20 +00001502 ret = i915_gem_object_get_fence(obj, pipelined, false);
Chris Wilson48b956c2010-09-14 12:50:34 +01001503 if (ret)
1504 goto err_unpin;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001505 }
1506
1507 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01001508
1509err_unpin:
1510 i915_gem_object_unpin(obj);
1511 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001512}
1513
Jesse Barnes81255562010-08-02 12:07:50 -07001514/* Assume fb object is pinned & idle & fenced and just update base pointers */
1515static int
1516intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
Jason Wessel21c74a82010-10-13 14:09:44 -05001517 int x, int y, enum mode_set_atomic state)
Jesse Barnes81255562010-08-02 12:07:50 -07001518{
1519 struct drm_device *dev = crtc->dev;
1520 struct drm_i915_private *dev_priv = dev->dev_private;
1521 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1522 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00001523 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07001524 int plane = intel_crtc->plane;
1525 unsigned long Start, Offset;
Jesse Barnes81255562010-08-02 12:07:50 -07001526 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01001527 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07001528
1529 switch (plane) {
1530 case 0:
1531 case 1:
1532 break;
1533 default:
1534 DRM_ERROR("Can't update plane %d in SAREA\n", plane);
1535 return -EINVAL;
1536 }
1537
1538 intel_fb = to_intel_framebuffer(fb);
1539 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07001540
Chris Wilson5eddb702010-09-11 13:48:45 +01001541 reg = DSPCNTR(plane);
1542 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07001543 /* Mask out pixel format bits in case we change it */
1544 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
1545 switch (fb->bits_per_pixel) {
1546 case 8:
1547 dspcntr |= DISPPLANE_8BPP;
1548 break;
1549 case 16:
1550 if (fb->depth == 15)
1551 dspcntr |= DISPPLANE_15_16BPP;
1552 else
1553 dspcntr |= DISPPLANE_16BPP;
1554 break;
1555 case 24:
1556 case 32:
1557 dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
1558 break;
1559 default:
1560 DRM_ERROR("Unknown color depth\n");
1561 return -EINVAL;
1562 }
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001563 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00001564 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07001565 dspcntr |= DISPPLANE_TILED;
1566 else
1567 dspcntr &= ~DISPPLANE_TILED;
1568 }
1569
Chris Wilson4e6cfef2010-08-08 13:20:19 +01001570 if (HAS_PCH_SPLIT(dev))
Jesse Barnes81255562010-08-02 12:07:50 -07001571 /* must disable */
1572 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
1573
Chris Wilson5eddb702010-09-11 13:48:45 +01001574 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07001575
Chris Wilson05394f32010-11-08 19:18:58 +00001576 Start = obj->gtt_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07001577 Offset = y * fb->pitch + x * (fb->bits_per_pixel / 8);
1578
Chris Wilson4e6cfef2010-08-08 13:20:19 +01001579 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
1580 Start, Offset, x, y, fb->pitch);
Chris Wilson5eddb702010-09-11 13:48:45 +01001581 I915_WRITE(DSPSTRIDE(plane), fb->pitch);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001582 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson5eddb702010-09-11 13:48:45 +01001583 I915_WRITE(DSPSURF(plane), Start);
1584 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
1585 I915_WRITE(DSPADDR(plane), Offset);
1586 } else
1587 I915_WRITE(DSPADDR(plane), Start + Offset);
1588 POSTING_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07001589
Chris Wilsonbed4a672010-09-11 10:47:47 +01001590 intel_update_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02001591 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07001592
1593 return 0;
1594}
1595
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001596static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05001597intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
1598 struct drm_framebuffer *old_fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08001599{
1600 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08001601 struct drm_i915_master_private *master_priv;
1602 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001603 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08001604
1605 /* no fb bound */
1606 if (!crtc->fb) {
Zhao Yakui28c97732009-10-09 11:39:41 +08001607 DRM_DEBUG_KMS("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001608 return 0;
1609 }
1610
Chris Wilson265db952010-09-20 15:41:01 +01001611 switch (intel_crtc->plane) {
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001612 case 0:
1613 case 1:
1614 break;
1615 default:
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001616 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08001617 }
1618
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001619 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01001620 ret = intel_pin_and_fence_fb_obj(dev,
1621 to_intel_framebuffer(crtc->fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00001622 NULL);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001623 if (ret != 0) {
1624 mutex_unlock(&dev->struct_mutex);
1625 return ret;
1626 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05001627
Chris Wilson265db952010-09-20 15:41:01 +01001628 if (old_fb) {
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01001629 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001630 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
Chris Wilson265db952010-09-20 15:41:01 +01001631
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01001632 wait_event(dev_priv->pending_flip_queue,
Chris Wilson05394f32010-11-08 19:18:58 +00001633 atomic_read(&obj->pending_flip) == 0);
Chris Wilson85345512010-11-13 09:49:11 +00001634
1635 /* Big Hammer, we also need to ensure that any pending
1636 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
1637 * current scanout is retired before unpinning the old
1638 * framebuffer.
1639 */
Chris Wilson05394f32010-11-08 19:18:58 +00001640 ret = i915_gem_object_flush_gpu(obj, false);
Chris Wilson85345512010-11-13 09:49:11 +00001641 if (ret) {
1642 i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
1643 mutex_unlock(&dev->struct_mutex);
1644 return ret;
1645 }
Chris Wilson265db952010-09-20 15:41:01 +01001646 }
1647
Jason Wessel21c74a82010-10-13 14:09:44 -05001648 ret = intel_pipe_set_base_atomic(crtc, crtc->fb, x, y,
1649 LEAVE_ATOMIC_MODE_SET);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01001650 if (ret) {
Chris Wilson265db952010-09-20 15:41:01 +01001651 i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001652 mutex_unlock(&dev->struct_mutex);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01001653 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08001654 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05001655
Chris Wilsonb7f1de22010-12-14 16:09:31 +00001656 if (old_fb) {
1657 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson265db952010-09-20 15:41:01 +01001658 i915_gem_object_unpin(to_intel_framebuffer(old_fb)->obj);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00001659 }
Jesse Barnes652c3932009-08-17 13:31:43 -07001660
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001661 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08001662
1663 if (!dev->primary->master)
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001664 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08001665
1666 master_priv = dev->primary->master->driver_priv;
1667 if (!master_priv->sarea_priv)
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001668 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08001669
Chris Wilson265db952010-09-20 15:41:01 +01001670 if (intel_crtc->pipe) {
Jesse Barnes79e53942008-11-07 14:24:08 -08001671 master_priv->sarea_priv->pipeB_x = x;
1672 master_priv->sarea_priv->pipeB_y = y;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001673 } else {
1674 master_priv->sarea_priv->pipeA_x = x;
1675 master_priv->sarea_priv->pipeA_y = y;
Jesse Barnes79e53942008-11-07 14:24:08 -08001676 }
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001677
1678 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08001679}
1680
Chris Wilson5eddb702010-09-11 13:48:45 +01001681static void ironlake_set_pll_edp(struct drm_crtc *crtc, int clock)
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001682{
1683 struct drm_device *dev = crtc->dev;
1684 struct drm_i915_private *dev_priv = dev->dev_private;
1685 u32 dpa_ctl;
1686
Zhao Yakui28c97732009-10-09 11:39:41 +08001687 DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", clock);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001688 dpa_ctl = I915_READ(DP_A);
1689 dpa_ctl &= ~DP_PLL_FREQ_MASK;
1690
1691 if (clock < 200000) {
1692 u32 temp;
1693 dpa_ctl |= DP_PLL_FREQ_160MHZ;
1694 /* workaround for 160Mhz:
1695 1) program 0x4600c bits 15:0 = 0x8124
1696 2) program 0x46010 bit 0 = 1
1697 3) program 0x46034 bit 24 = 1
1698 4) program 0x64000 bit 14 = 1
1699 */
1700 temp = I915_READ(0x4600c);
1701 temp &= 0xffff0000;
1702 I915_WRITE(0x4600c, temp | 0x8124);
1703
1704 temp = I915_READ(0x46010);
1705 I915_WRITE(0x46010, temp | 1);
1706
1707 temp = I915_READ(0x46034);
1708 I915_WRITE(0x46034, temp | (1 << 24));
1709 } else {
1710 dpa_ctl |= DP_PLL_FREQ_270MHZ;
1711 }
1712 I915_WRITE(DP_A, dpa_ctl);
1713
Chris Wilson5eddb702010-09-11 13:48:45 +01001714 POSTING_READ(DP_A);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001715 udelay(500);
1716}
1717
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08001718static void intel_fdi_normal_train(struct drm_crtc *crtc)
1719{
1720 struct drm_device *dev = crtc->dev;
1721 struct drm_i915_private *dev_priv = dev->dev_private;
1722 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1723 int pipe = intel_crtc->pipe;
1724 u32 reg, temp;
1725
1726 /* enable normal train */
1727 reg = FDI_TX_CTL(pipe);
1728 temp = I915_READ(reg);
1729 temp &= ~FDI_LINK_TRAIN_NONE;
1730 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
1731 I915_WRITE(reg, temp);
1732
1733 reg = FDI_RX_CTL(pipe);
1734 temp = I915_READ(reg);
1735 if (HAS_PCH_CPT(dev)) {
1736 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
1737 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
1738 } else {
1739 temp &= ~FDI_LINK_TRAIN_NONE;
1740 temp |= FDI_LINK_TRAIN_NONE;
1741 }
1742 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
1743
1744 /* wait one idle pattern time */
1745 POSTING_READ(reg);
1746 udelay(1000);
1747}
1748
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001749/* The FDI link training functions for ILK/Ibexpeak. */
1750static void ironlake_fdi_link_train(struct drm_crtc *crtc)
1751{
1752 struct drm_device *dev = crtc->dev;
1753 struct drm_i915_private *dev_priv = dev->dev_private;
1754 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1755 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01001756 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001757
Adam Jacksone1a44742010-06-25 15:32:14 -04001758 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
1759 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01001760 reg = FDI_RX_IMR(pipe);
1761 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04001762 temp &= ~FDI_RX_SYMBOL_LOCK;
1763 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01001764 I915_WRITE(reg, temp);
1765 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04001766 udelay(150);
1767
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001768 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01001769 reg = FDI_TX_CTL(pipe);
1770 temp = I915_READ(reg);
Adam Jackson77ffb592010-04-12 11:38:44 -04001771 temp &= ~(7 << 19);
1772 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001773 temp &= ~FDI_LINK_TRAIN_NONE;
1774 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01001775 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001776
Chris Wilson5eddb702010-09-11 13:48:45 +01001777 reg = FDI_RX_CTL(pipe);
1778 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001779 temp &= ~FDI_LINK_TRAIN_NONE;
1780 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01001781 I915_WRITE(reg, temp | FDI_RX_ENABLE);
1782
1783 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001784 udelay(150);
1785
Jesse Barnes5b2adf82010-10-07 16:01:15 -07001786 /* Ironlake workaround, enable clock pointer after FDI enable*/
1787 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_ENABLE);
1788
Chris Wilson5eddb702010-09-11 13:48:45 +01001789 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04001790 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01001791 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001792 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
1793
1794 if ((temp & FDI_RX_BIT_LOCK)) {
1795 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01001796 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001797 break;
1798 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001799 }
Adam Jacksone1a44742010-06-25 15:32:14 -04001800 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01001801 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001802
1803 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01001804 reg = FDI_TX_CTL(pipe);
1805 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001806 temp &= ~FDI_LINK_TRAIN_NONE;
1807 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01001808 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001809
Chris Wilson5eddb702010-09-11 13:48:45 +01001810 reg = FDI_RX_CTL(pipe);
1811 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001812 temp &= ~FDI_LINK_TRAIN_NONE;
1813 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01001814 I915_WRITE(reg, temp);
1815
1816 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001817 udelay(150);
1818
Chris Wilson5eddb702010-09-11 13:48:45 +01001819 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04001820 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01001821 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001822 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
1823
1824 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01001825 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001826 DRM_DEBUG_KMS("FDI train 2 done.\n");
1827 break;
1828 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001829 }
Adam Jacksone1a44742010-06-25 15:32:14 -04001830 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01001831 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001832
1833 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07001834
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001835}
1836
Chris Wilson5eddb702010-09-11 13:48:45 +01001837static const int const snb_b_fdi_train_param [] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001838 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
1839 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
1840 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
1841 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
1842};
1843
1844/* The FDI link training functions for SNB/Cougarpoint. */
1845static void gen6_fdi_link_train(struct drm_crtc *crtc)
1846{
1847 struct drm_device *dev = crtc->dev;
1848 struct drm_i915_private *dev_priv = dev->dev_private;
1849 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1850 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01001851 u32 reg, temp, i;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001852
Adam Jacksone1a44742010-06-25 15:32:14 -04001853 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
1854 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01001855 reg = FDI_RX_IMR(pipe);
1856 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04001857 temp &= ~FDI_RX_SYMBOL_LOCK;
1858 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01001859 I915_WRITE(reg, temp);
1860
1861 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04001862 udelay(150);
1863
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001864 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01001865 reg = FDI_TX_CTL(pipe);
1866 temp = I915_READ(reg);
Adam Jackson77ffb592010-04-12 11:38:44 -04001867 temp &= ~(7 << 19);
1868 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001869 temp &= ~FDI_LINK_TRAIN_NONE;
1870 temp |= FDI_LINK_TRAIN_PATTERN_1;
1871 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
1872 /* SNB-B */
1873 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01001874 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001875
Chris Wilson5eddb702010-09-11 13:48:45 +01001876 reg = FDI_RX_CTL(pipe);
1877 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001878 if (HAS_PCH_CPT(dev)) {
1879 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
1880 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
1881 } else {
1882 temp &= ~FDI_LINK_TRAIN_NONE;
1883 temp |= FDI_LINK_TRAIN_PATTERN_1;
1884 }
Chris Wilson5eddb702010-09-11 13:48:45 +01001885 I915_WRITE(reg, temp | FDI_RX_ENABLE);
1886
1887 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001888 udelay(150);
1889
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001890 for (i = 0; i < 4; i++ ) {
Chris Wilson5eddb702010-09-11 13:48:45 +01001891 reg = FDI_TX_CTL(pipe);
1892 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001893 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
1894 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01001895 I915_WRITE(reg, temp);
1896
1897 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001898 udelay(500);
1899
Chris Wilson5eddb702010-09-11 13:48:45 +01001900 reg = FDI_RX_IIR(pipe);
1901 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001902 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
1903
1904 if (temp & FDI_RX_BIT_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01001905 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001906 DRM_DEBUG_KMS("FDI train 1 done.\n");
1907 break;
1908 }
1909 }
1910 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01001911 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001912
1913 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01001914 reg = FDI_TX_CTL(pipe);
1915 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001916 temp &= ~FDI_LINK_TRAIN_NONE;
1917 temp |= FDI_LINK_TRAIN_PATTERN_2;
1918 if (IS_GEN6(dev)) {
1919 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
1920 /* SNB-B */
1921 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
1922 }
Chris Wilson5eddb702010-09-11 13:48:45 +01001923 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001924
Chris Wilson5eddb702010-09-11 13:48:45 +01001925 reg = FDI_RX_CTL(pipe);
1926 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001927 if (HAS_PCH_CPT(dev)) {
1928 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
1929 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
1930 } else {
1931 temp &= ~FDI_LINK_TRAIN_NONE;
1932 temp |= FDI_LINK_TRAIN_PATTERN_2;
1933 }
Chris Wilson5eddb702010-09-11 13:48:45 +01001934 I915_WRITE(reg, temp);
1935
1936 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001937 udelay(150);
1938
1939 for (i = 0; i < 4; i++ ) {
Chris Wilson5eddb702010-09-11 13:48:45 +01001940 reg = FDI_TX_CTL(pipe);
1941 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001942 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
1943 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01001944 I915_WRITE(reg, temp);
1945
1946 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001947 udelay(500);
1948
Chris Wilson5eddb702010-09-11 13:48:45 +01001949 reg = FDI_RX_IIR(pipe);
1950 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001951 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
1952
1953 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01001954 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001955 DRM_DEBUG_KMS("FDI train 2 done.\n");
1956 break;
1957 }
1958 }
1959 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01001960 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08001961
1962 DRM_DEBUG_KMS("FDI train done.\n");
1963}
1964
Jesse Barnes0e23b992010-09-10 11:10:00 -07001965static void ironlake_fdi_enable(struct drm_crtc *crtc)
1966{
1967 struct drm_device *dev = crtc->dev;
1968 struct drm_i915_private *dev_priv = dev->dev_private;
1969 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1970 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01001971 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07001972
Jesse Barnesc64e3112010-09-10 11:27:03 -07001973 /* Write the TU size bits so error detection works */
Chris Wilson5eddb702010-09-11 13:48:45 +01001974 I915_WRITE(FDI_RX_TUSIZE1(pipe),
1975 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
Jesse Barnesc64e3112010-09-10 11:27:03 -07001976
Jesse Barnes0e23b992010-09-10 11:10:00 -07001977 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01001978 reg = FDI_RX_CTL(pipe);
1979 temp = I915_READ(reg);
1980 temp &= ~((0x7 << 19) | (0x7 << 16));
Jesse Barnes0e23b992010-09-10 11:10:00 -07001981 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Chris Wilson5eddb702010-09-11 13:48:45 +01001982 temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
1983 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
1984
1985 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07001986 udelay(200);
1987
1988 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01001989 temp = I915_READ(reg);
1990 I915_WRITE(reg, temp | FDI_PCDCLK);
1991
1992 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07001993 udelay(200);
1994
1995 /* Enable CPU FDI TX PLL, always on for Ironlake */
Chris Wilson5eddb702010-09-11 13:48:45 +01001996 reg = FDI_TX_CTL(pipe);
1997 temp = I915_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07001998 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
Chris Wilson5eddb702010-09-11 13:48:45 +01001999 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
2000
2001 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002002 udelay(100);
2003 }
2004}
2005
Chris Wilson5eddb702010-09-11 13:48:45 +01002006static void intel_flush_display_plane(struct drm_device *dev,
2007 int plane)
2008{
2009 struct drm_i915_private *dev_priv = dev->dev_private;
2010 u32 reg = DSPADDR(plane);
2011 I915_WRITE(reg, I915_READ(reg));
2012}
2013
Chris Wilson6b383a72010-09-13 13:54:26 +01002014/*
2015 * When we disable a pipe, we need to clear any pending scanline wait events
2016 * to avoid hanging the ring, which we assume we are waiting on.
2017 */
2018static void intel_clear_scanline_wait(struct drm_device *dev)
2019{
2020 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson8168bd42010-11-11 17:54:52 +00002021 struct intel_ring_buffer *ring;
Chris Wilson6b383a72010-09-13 13:54:26 +01002022 u32 tmp;
2023
2024 if (IS_GEN2(dev))
2025 /* Can't break the hang on i8xx */
2026 return;
2027
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002028 ring = LP_RING(dev_priv);
Chris Wilson8168bd42010-11-11 17:54:52 +00002029 tmp = I915_READ_CTL(ring);
2030 if (tmp & RING_WAIT)
2031 I915_WRITE_CTL(ring, tmp);
Chris Wilson6b383a72010-09-13 13:54:26 +01002032}
2033
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002034static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2035{
Chris Wilson05394f32010-11-08 19:18:58 +00002036 struct drm_i915_gem_object *obj;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002037 struct drm_i915_private *dev_priv;
2038
2039 if (crtc->fb == NULL)
2040 return;
2041
Chris Wilson05394f32010-11-08 19:18:58 +00002042 obj = to_intel_framebuffer(crtc->fb)->obj;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002043 dev_priv = crtc->dev->dev_private;
2044 wait_event(dev_priv->pending_flip_queue,
Chris Wilson05394f32010-11-08 19:18:58 +00002045 atomic_read(&obj->pending_flip) == 0);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002046}
2047
Jesse Barnes6be4a602010-09-10 10:26:01 -07002048static void ironlake_crtc_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08002049{
2050 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002051 struct drm_i915_private *dev_priv = dev->dev_private;
2052 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2053 int pipe = intel_crtc->pipe;
Shaohua Li7662c8b2009-06-26 11:23:55 +08002054 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002055 u32 reg, temp;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002056
Chris Wilsonf7abfe82010-09-13 14:19:16 +01002057 if (intel_crtc->active)
2058 return;
2059
2060 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01002061 intel_update_watermarks(dev);
2062
Jesse Barnes6be4a602010-09-10 10:26:01 -07002063 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
2064 temp = I915_READ(PCH_LVDS);
Chris Wilson5eddb702010-09-11 13:48:45 +01002065 if ((temp & LVDS_PORT_EN) == 0)
Jesse Barnes6be4a602010-09-10 10:26:01 -07002066 I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002067 }
2068
Jesse Barnes0e23b992010-09-10 11:10:00 -07002069 ironlake_fdi_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002070
2071 /* Enable panel fitting for LVDS */
2072 if (dev_priv->pch_pf_size &&
Jesse Barnes1d850362010-10-07 16:01:10 -07002073 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) || HAS_eDP)) {
Jesse Barnes6be4a602010-09-10 10:26:01 -07002074 /* Force use of hard-coded filter coefficients
2075 * as some pre-programmed values are broken,
2076 * e.g. x201.
2077 */
2078 I915_WRITE(pipe ? PFB_CTL_1 : PFA_CTL_1,
2079 PF_ENABLE | PF_FILTER_MED_3x3);
2080 I915_WRITE(pipe ? PFB_WIN_POS : PFA_WIN_POS,
2081 dev_priv->pch_pf_pos);
2082 I915_WRITE(pipe ? PFB_WIN_SZ : PFA_WIN_SZ,
2083 dev_priv->pch_pf_size);
2084 }
2085
2086 /* Enable CPU pipe */
Chris Wilson5eddb702010-09-11 13:48:45 +01002087 reg = PIPECONF(pipe);
2088 temp = I915_READ(reg);
2089 if ((temp & PIPECONF_ENABLE) == 0) {
2090 I915_WRITE(reg, temp | PIPECONF_ENABLE);
2091 POSTING_READ(reg);
Jesse Barnes17f67662010-10-07 16:01:19 -07002092 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002093 }
2094
2095 /* configure and enable CPU plane */
Chris Wilson5eddb702010-09-11 13:48:45 +01002096 reg = DSPCNTR(plane);
2097 temp = I915_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002098 if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002099 I915_WRITE(reg, temp | DISPLAY_PLANE_ENABLE);
2100 intel_flush_display_plane(dev, plane);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002101 }
2102
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002103 /* For PCH output, training FDI link */
2104 if (IS_GEN6(dev))
2105 gen6_fdi_link_train(crtc);
2106 else
2107 ironlake_fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002108
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002109 /* enable PCH DPLL */
Chris Wilson5eddb702010-09-11 13:48:45 +01002110 reg = PCH_DPLL(pipe);
2111 temp = I915_READ(reg);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002112 if ((temp & DPLL_VCO_ENABLE) == 0) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002113 I915_WRITE(reg, temp | DPLL_VCO_ENABLE);
2114 POSTING_READ(reg);
Chris Wilson8c4223b2010-09-10 22:33:42 +01002115 udelay(200);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002116 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002117
2118 if (HAS_PCH_CPT(dev)) {
2119 /* Be sure PCH DPLL SEL is set */
2120 temp = I915_READ(PCH_DPLL_SEL);
Chris Wilson5eddb702010-09-11 13:48:45 +01002121 if (pipe == 0 && (temp & TRANSA_DPLL_ENABLE) == 0)
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002122 temp |= (TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL);
Chris Wilson5eddb702010-09-11 13:48:45 +01002123 else if (pipe == 1 && (temp & TRANSB_DPLL_ENABLE) == 0)
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002124 temp |= (TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
2125 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002126 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002127
Chris Wilson5eddb702010-09-11 13:48:45 +01002128 /* set transcoder timing */
2129 I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
2130 I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
2131 I915_WRITE(TRANS_HSYNC(pipe), I915_READ(HSYNC(pipe)));
2132
2133 I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
2134 I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
2135 I915_WRITE(TRANS_VSYNC(pipe), I915_READ(VSYNC(pipe)));
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002136
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002137 intel_fdi_normal_train(crtc);
2138
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002139 /* For PCH DP, enable TRANS_DP_CTL */
2140 if (HAS_PCH_CPT(dev) &&
2141 intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002142 reg = TRANS_DP_CTL(pipe);
2143 temp = I915_READ(reg);
2144 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08002145 TRANS_DP_SYNC_MASK |
2146 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01002147 temp |= (TRANS_DP_OUTPUT_ENABLE |
2148 TRANS_DP_ENH_FRAMING);
Eric Anholt220cad32010-11-18 09:32:58 +08002149 temp |= TRANS_DP_8BPC;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002150
2151 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01002152 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002153 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01002154 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002155
2156 switch (intel_trans_dp_port_sel(crtc)) {
2157 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01002158 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002159 break;
2160 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01002161 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002162 break;
2163 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01002164 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002165 break;
2166 default:
2167 DRM_DEBUG_KMS("Wrong PCH DP port return. Guess port B\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002168 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002169 break;
2170 }
2171
Chris Wilson5eddb702010-09-11 13:48:45 +01002172 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002173 }
2174
2175 /* enable PCH transcoder */
Chris Wilson5eddb702010-09-11 13:48:45 +01002176 reg = TRANSCONF(pipe);
2177 temp = I915_READ(reg);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002178 /*
2179 * make the BPC in transcoder be consistent with
2180 * that in pipeconf reg.
2181 */
2182 temp &= ~PIPE_BPC_MASK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002183 temp |= I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK;
2184 I915_WRITE(reg, temp | TRANS_ENABLE);
2185 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Jesse Barnes17f67662010-10-07 16:01:19 -07002186 DRM_ERROR("failed to enable transcoder %d\n", pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002187
2188 intel_crtc_load_lut(crtc);
Chris Wilsonbed4a672010-09-11 10:47:47 +01002189 intel_update_fbc(dev);
Chris Wilson6b383a72010-09-13 13:54:26 +01002190 intel_crtc_update_cursor(crtc, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002191}
2192
2193static void ironlake_crtc_disable(struct drm_crtc *crtc)
2194{
2195 struct drm_device *dev = crtc->dev;
2196 struct drm_i915_private *dev_priv = dev->dev_private;
2197 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2198 int pipe = intel_crtc->pipe;
2199 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002200 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07002201
Chris Wilsonf7abfe82010-09-13 14:19:16 +01002202 if (!intel_crtc->active)
2203 return;
2204
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002205 intel_crtc_wait_for_pending_flips(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002206 drm_vblank_off(dev, pipe);
Chris Wilson6b383a72010-09-13 13:54:26 +01002207 intel_crtc_update_cursor(crtc, false);
Chris Wilson5eddb702010-09-11 13:48:45 +01002208
Jesse Barnes6be4a602010-09-10 10:26:01 -07002209 /* Disable display plane */
Chris Wilson5eddb702010-09-11 13:48:45 +01002210 reg = DSPCNTR(plane);
2211 temp = I915_READ(reg);
2212 if (temp & DISPLAY_PLANE_ENABLE) {
2213 I915_WRITE(reg, temp & ~DISPLAY_PLANE_ENABLE);
2214 intel_flush_display_plane(dev, plane);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002215 }
2216
2217 if (dev_priv->cfb_plane == plane &&
2218 dev_priv->display.disable_fbc)
2219 dev_priv->display.disable_fbc(dev);
2220
2221 /* disable cpu pipe, disable after all planes disabled */
Chris Wilson5eddb702010-09-11 13:48:45 +01002222 reg = PIPECONF(pipe);
2223 temp = I915_READ(reg);
2224 if (temp & PIPECONF_ENABLE) {
2225 I915_WRITE(reg, temp & ~PIPECONF_ENABLE);
Jesse Barnes17f67662010-10-07 16:01:19 -07002226 POSTING_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002227 /* wait for cpu pipe off, pipe state */
Jesse Barnes17f67662010-10-07 16:01:19 -07002228 intel_wait_for_pipe_off(dev, intel_crtc->pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01002229 }
Jesse Barnes6be4a602010-09-10 10:26:01 -07002230
Jesse Barnes6be4a602010-09-10 10:26:01 -07002231 /* Disable PF */
2232 I915_WRITE(pipe ? PFB_CTL_1 : PFA_CTL_1, 0);
2233 I915_WRITE(pipe ? PFB_WIN_SZ : PFA_WIN_SZ, 0);
2234
2235 /* disable CPU FDI tx and PCH FDI rx */
Chris Wilson5eddb702010-09-11 13:48:45 +01002236 reg = FDI_TX_CTL(pipe);
2237 temp = I915_READ(reg);
2238 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2239 POSTING_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002240
Chris Wilson5eddb702010-09-11 13:48:45 +01002241 reg = FDI_RX_CTL(pipe);
2242 temp = I915_READ(reg);
2243 temp &= ~(0x7 << 16);
2244 temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2245 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002246
Chris Wilson5eddb702010-09-11 13:48:45 +01002247 POSTING_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002248 udelay(100);
2249
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002250 /* Ironlake workaround, disable clock pointer after downing FDI */
Zhenyu Wange07ac3a2010-11-04 09:02:54 +00002251 if (HAS_PCH_IBX(dev))
2252 I915_WRITE(FDI_RX_CHICKEN(pipe),
2253 I915_READ(FDI_RX_CHICKEN(pipe) &
2254 ~FDI_RX_PHASE_SYNC_POINTER_ENABLE));
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002255
Jesse Barnes6be4a602010-09-10 10:26:01 -07002256 /* still set train pattern 1 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002257 reg = FDI_TX_CTL(pipe);
2258 temp = I915_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002259 temp &= ~FDI_LINK_TRAIN_NONE;
2260 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002261 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002262
Chris Wilson5eddb702010-09-11 13:48:45 +01002263 reg = FDI_RX_CTL(pipe);
2264 temp = I915_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002265 if (HAS_PCH_CPT(dev)) {
2266 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2267 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2268 } else {
2269 temp &= ~FDI_LINK_TRAIN_NONE;
2270 temp |= FDI_LINK_TRAIN_PATTERN_1;
2271 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002272 /* BPC in FDI rx is consistent with that in PIPECONF */
2273 temp &= ~(0x07 << 16);
2274 temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2275 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002276
Chris Wilson5eddb702010-09-11 13:48:45 +01002277 POSTING_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002278 udelay(100);
2279
2280 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
2281 temp = I915_READ(PCH_LVDS);
Chris Wilson5eddb702010-09-11 13:48:45 +01002282 if (temp & LVDS_PORT_EN) {
2283 I915_WRITE(PCH_LVDS, temp & ~LVDS_PORT_EN);
2284 POSTING_READ(PCH_LVDS);
2285 udelay(100);
2286 }
Jesse Barnes6be4a602010-09-10 10:26:01 -07002287 }
2288
2289 /* disable PCH transcoder */
Chris Wilson5eddb702010-09-11 13:48:45 +01002290 reg = TRANSCONF(plane);
2291 temp = I915_READ(reg);
2292 if (temp & TRANS_ENABLE) {
2293 I915_WRITE(reg, temp & ~TRANS_ENABLE);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002294 /* wait for PCH transcoder off, transcoder state */
Chris Wilson5eddb702010-09-11 13:48:45 +01002295 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Jesse Barnes6be4a602010-09-10 10:26:01 -07002296 DRM_ERROR("failed to disable transcoder\n");
2297 }
2298
Jesse Barnes6be4a602010-09-10 10:26:01 -07002299 if (HAS_PCH_CPT(dev)) {
2300 /* disable TRANS_DP_CTL */
Chris Wilson5eddb702010-09-11 13:48:45 +01002301 reg = TRANS_DP_CTL(pipe);
2302 temp = I915_READ(reg);
2303 temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
2304 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002305
2306 /* disable DPLL_SEL */
2307 temp = I915_READ(PCH_DPLL_SEL);
Chris Wilson5eddb702010-09-11 13:48:45 +01002308 if (pipe == 0)
Jesse Barnes6be4a602010-09-10 10:26:01 -07002309 temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
2310 else
2311 temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
2312 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002313 }
2314
2315 /* disable PCH DPLL */
Chris Wilson5eddb702010-09-11 13:48:45 +01002316 reg = PCH_DPLL(pipe);
2317 temp = I915_READ(reg);
2318 I915_WRITE(reg, temp & ~DPLL_VCO_ENABLE);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002319
2320 /* Switch from PCDclk to Rawclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002321 reg = FDI_RX_CTL(pipe);
2322 temp = I915_READ(reg);
2323 I915_WRITE(reg, temp & ~FDI_PCDCLK);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002324
2325 /* Disable CPU FDI TX PLL */
Chris Wilson5eddb702010-09-11 13:48:45 +01002326 reg = FDI_TX_CTL(pipe);
2327 temp = I915_READ(reg);
2328 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2329
2330 POSTING_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002331 udelay(100);
2332
Chris Wilson5eddb702010-09-11 13:48:45 +01002333 reg = FDI_RX_CTL(pipe);
2334 temp = I915_READ(reg);
2335 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002336
2337 /* Wait for the clocks to turn off. */
Chris Wilson5eddb702010-09-11 13:48:45 +01002338 POSTING_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002339 udelay(100);
Chris Wilson6b383a72010-09-13 13:54:26 +01002340
Chris Wilsonf7abfe82010-09-13 14:19:16 +01002341 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01002342 intel_update_watermarks(dev);
2343 intel_update_fbc(dev);
2344 intel_clear_scanline_wait(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002345}
2346
2347static void ironlake_crtc_dpms(struct drm_crtc *crtc, int mode)
2348{
2349 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2350 int pipe = intel_crtc->pipe;
2351 int plane = intel_crtc->plane;
2352
Zhenyu Wang2c072452009-06-05 15:38:42 +08002353 /* XXX: When our outputs are all unaware of DPMS modes other than off
2354 * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
2355 */
2356 switch (mode) {
2357 case DRM_MODE_DPMS_ON:
2358 case DRM_MODE_DPMS_STANDBY:
2359 case DRM_MODE_DPMS_SUSPEND:
Chris Wilson868dc582010-08-07 11:01:31 +01002360 DRM_DEBUG_KMS("crtc %d/%d dpms on\n", pipe, plane);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002361 ironlake_crtc_enable(crtc);
Chris Wilson868dc582010-08-07 11:01:31 +01002362 break;
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08002363
Zhenyu Wang2c072452009-06-05 15:38:42 +08002364 case DRM_MODE_DPMS_OFF:
Chris Wilson868dc582010-08-07 11:01:31 +01002365 DRM_DEBUG_KMS("crtc %d/%d dpms off\n", pipe, plane);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002366 ironlake_crtc_disable(crtc);
Zhenyu Wang2c072452009-06-05 15:38:42 +08002367 break;
2368 }
2369}
2370
Daniel Vetter02e792f2009-09-15 22:57:34 +02002371static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
2372{
Daniel Vetter02e792f2009-09-15 22:57:34 +02002373 if (!enable && intel_crtc->overlay) {
Chris Wilson23f09ce2010-08-12 13:53:37 +01002374 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02002375
Chris Wilson23f09ce2010-08-12 13:53:37 +01002376 mutex_lock(&dev->struct_mutex);
2377 (void) intel_overlay_switch_off(intel_crtc->overlay, false);
2378 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02002379 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02002380
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01002381 /* Let userspace switch the overlay on again. In most cases userspace
2382 * has to recompute where to put it anyway.
2383 */
Daniel Vetter02e792f2009-09-15 22:57:34 +02002384}
2385
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002386static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08002387{
2388 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002389 struct drm_i915_private *dev_priv = dev->dev_private;
2390 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2391 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07002392 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002393 u32 reg, temp;
Jesse Barnes79e53942008-11-07 14:24:08 -08002394
Chris Wilsonf7abfe82010-09-13 14:19:16 +01002395 if (intel_crtc->active)
2396 return;
2397
2398 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01002399 intel_update_watermarks(dev);
2400
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002401 /* Enable the DPLL */
Chris Wilson5eddb702010-09-11 13:48:45 +01002402 reg = DPLL(pipe);
2403 temp = I915_READ(reg);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002404 if ((temp & DPLL_VCO_ENABLE) == 0) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002405 I915_WRITE(reg, temp);
2406
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002407 /* Wait for the clocks to stabilize. */
Chris Wilson5eddb702010-09-11 13:48:45 +01002408 POSTING_READ(reg);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002409 udelay(150);
Chris Wilson5eddb702010-09-11 13:48:45 +01002410
2411 I915_WRITE(reg, temp | DPLL_VCO_ENABLE);
2412
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002413 /* Wait for the clocks to stabilize. */
Chris Wilson5eddb702010-09-11 13:48:45 +01002414 POSTING_READ(reg);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002415 udelay(150);
Chris Wilson5eddb702010-09-11 13:48:45 +01002416
2417 I915_WRITE(reg, temp | DPLL_VCO_ENABLE);
2418
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002419 /* Wait for the clocks to stabilize. */
Chris Wilson5eddb702010-09-11 13:48:45 +01002420 POSTING_READ(reg);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002421 udelay(150);
2422 }
2423
2424 /* Enable the pipe */
Chris Wilson5eddb702010-09-11 13:48:45 +01002425 reg = PIPECONF(pipe);
2426 temp = I915_READ(reg);
2427 if ((temp & PIPECONF_ENABLE) == 0)
2428 I915_WRITE(reg, temp | PIPECONF_ENABLE);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002429
2430 /* Enable the plane */
Chris Wilson5eddb702010-09-11 13:48:45 +01002431 reg = DSPCNTR(plane);
2432 temp = I915_READ(reg);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002433 if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002434 I915_WRITE(reg, temp | DISPLAY_PLANE_ENABLE);
2435 intel_flush_display_plane(dev, plane);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002436 }
2437
2438 intel_crtc_load_lut(crtc);
Chris Wilsonbed4a672010-09-11 10:47:47 +01002439 intel_update_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002440
2441 /* Give the overlay scaler a chance to enable if it's on this pipe */
2442 intel_crtc_dpms_overlay(intel_crtc, true);
Chris Wilson6b383a72010-09-13 13:54:26 +01002443 intel_crtc_update_cursor(crtc, true);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002444}
2445
2446static void i9xx_crtc_disable(struct drm_crtc *crtc)
2447{
2448 struct drm_device *dev = crtc->dev;
2449 struct drm_i915_private *dev_priv = dev->dev_private;
2450 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2451 int pipe = intel_crtc->pipe;
2452 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002453 u32 reg, temp;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002454
Chris Wilsonf7abfe82010-09-13 14:19:16 +01002455 if (!intel_crtc->active)
2456 return;
2457
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002458 /* Give the overlay scaler a chance to disable if it's on this pipe */
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002459 intel_crtc_wait_for_pending_flips(crtc);
2460 drm_vblank_off(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002461 intel_crtc_dpms_overlay(intel_crtc, false);
Chris Wilson6b383a72010-09-13 13:54:26 +01002462 intel_crtc_update_cursor(crtc, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002463
2464 if (dev_priv->cfb_plane == plane &&
2465 dev_priv->display.disable_fbc)
2466 dev_priv->display.disable_fbc(dev);
2467
2468 /* Disable display plane */
Chris Wilson5eddb702010-09-11 13:48:45 +01002469 reg = DSPCNTR(plane);
2470 temp = I915_READ(reg);
2471 if (temp & DISPLAY_PLANE_ENABLE) {
2472 I915_WRITE(reg, temp & ~DISPLAY_PLANE_ENABLE);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002473 /* Flush the plane changes */
Chris Wilson5eddb702010-09-11 13:48:45 +01002474 intel_flush_display_plane(dev, plane);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002475
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002476 /* Wait for vblank for the disable to take effect */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002477 if (IS_GEN2(dev))
Chris Wilson58e10eb2010-10-03 10:56:11 +01002478 intel_wait_for_vblank(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002479 }
2480
2481 /* Don't disable pipe A or pipe A PLLs if needed */
Chris Wilson5eddb702010-09-11 13:48:45 +01002482 if (pipe == 0 && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
Chris Wilson6b383a72010-09-13 13:54:26 +01002483 goto done;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002484
2485 /* Next, disable display pipes */
Chris Wilson5eddb702010-09-11 13:48:45 +01002486 reg = PIPECONF(pipe);
2487 temp = I915_READ(reg);
2488 if (temp & PIPECONF_ENABLE) {
2489 I915_WRITE(reg, temp & ~PIPECONF_ENABLE);
2490
Chris Wilson58e10eb2010-10-03 10:56:11 +01002491 /* Wait for the pipe to turn off */
Chris Wilson5eddb702010-09-11 13:48:45 +01002492 POSTING_READ(reg);
Chris Wilson58e10eb2010-10-03 10:56:11 +01002493 intel_wait_for_pipe_off(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002494 }
2495
Chris Wilson5eddb702010-09-11 13:48:45 +01002496 reg = DPLL(pipe);
2497 temp = I915_READ(reg);
2498 if (temp & DPLL_VCO_ENABLE) {
2499 I915_WRITE(reg, temp & ~DPLL_VCO_ENABLE);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002500
Chris Wilson5eddb702010-09-11 13:48:45 +01002501 /* Wait for the clocks to turn off. */
2502 POSTING_READ(reg);
2503 udelay(150);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002504 }
Chris Wilson6b383a72010-09-13 13:54:26 +01002505
2506done:
Chris Wilsonf7abfe82010-09-13 14:19:16 +01002507 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01002508 intel_update_fbc(dev);
2509 intel_update_watermarks(dev);
2510 intel_clear_scanline_wait(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002511}
2512
2513static void i9xx_crtc_dpms(struct drm_crtc *crtc, int mode)
2514{
Jesse Barnes79e53942008-11-07 14:24:08 -08002515 /* XXX: When our outputs are all unaware of DPMS modes other than off
2516 * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
2517 */
2518 switch (mode) {
2519 case DRM_MODE_DPMS_ON:
2520 case DRM_MODE_DPMS_STANDBY:
2521 case DRM_MODE_DPMS_SUSPEND:
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002522 i9xx_crtc_enable(crtc);
2523 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08002524 case DRM_MODE_DPMS_OFF:
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002525 i9xx_crtc_disable(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08002526 break;
2527 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08002528}
2529
2530/**
2531 * Sets the power management mode of the pipe and plane.
Zhenyu Wang2c072452009-06-05 15:38:42 +08002532 */
2533static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
2534{
2535 struct drm_device *dev = crtc->dev;
Jesse Barnese70236a2009-09-21 10:42:27 -07002536 struct drm_i915_private *dev_priv = dev->dev_private;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002537 struct drm_i915_master_private *master_priv;
2538 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2539 int pipe = intel_crtc->pipe;
2540 bool enabled;
2541
Chris Wilson032d2a02010-09-06 16:17:22 +01002542 if (intel_crtc->dpms_mode == mode)
2543 return;
2544
Chris Wilsondebcadd2010-08-07 11:01:33 +01002545 intel_crtc->dpms_mode = mode;
Chris Wilsondebcadd2010-08-07 11:01:33 +01002546
Jesse Barnese70236a2009-09-21 10:42:27 -07002547 dev_priv->display.dpms(crtc, mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08002548
2549 if (!dev->primary->master)
2550 return;
2551
2552 master_priv = dev->primary->master->driver_priv;
2553 if (!master_priv->sarea_priv)
2554 return;
2555
2556 enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
2557
2558 switch (pipe) {
2559 case 0:
2560 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
2561 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
2562 break;
2563 case 1:
2564 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
2565 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
2566 break;
2567 default:
2568 DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
2569 break;
2570 }
Jesse Barnes79e53942008-11-07 14:24:08 -08002571}
2572
Chris Wilsoncdd59982010-09-08 16:30:16 +01002573static void intel_crtc_disable(struct drm_crtc *crtc)
2574{
2575 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
2576 struct drm_device *dev = crtc->dev;
2577
2578 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
2579
2580 if (crtc->fb) {
2581 mutex_lock(&dev->struct_mutex);
2582 i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
2583 mutex_unlock(&dev->struct_mutex);
2584 }
2585}
2586
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07002587/* Prepare for a mode set.
2588 *
2589 * Note we could be a lot smarter here. We need to figure out which outputs
2590 * will be enabled, which disabled (in short, how the config will changes)
2591 * and perform the minimum necessary steps to accomplish that, e.g. updating
2592 * watermarks, FBC configuration, making sure PLLs are programmed correctly,
2593 * panel fitting is in the proper state, etc.
2594 */
2595static void i9xx_crtc_prepare(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08002596{
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07002597 i9xx_crtc_disable(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08002598}
2599
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07002600static void i9xx_crtc_commit(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08002601{
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07002602 i9xx_crtc_enable(crtc);
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07002603}
2604
2605static void ironlake_crtc_prepare(struct drm_crtc *crtc)
2606{
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07002607 ironlake_crtc_disable(crtc);
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07002608}
2609
2610static void ironlake_crtc_commit(struct drm_crtc *crtc)
2611{
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07002612 ironlake_crtc_enable(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08002613}
2614
2615void intel_encoder_prepare (struct drm_encoder *encoder)
2616{
2617 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
2618 /* lvds has its own version of prepare see intel_lvds_prepare */
2619 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
2620}
2621
2622void intel_encoder_commit (struct drm_encoder *encoder)
2623{
2624 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
2625 /* lvds has its own version of commit see intel_lvds_commit */
2626 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
2627}
2628
Chris Wilsonea5b2132010-08-04 13:50:23 +01002629void intel_encoder_destroy(struct drm_encoder *encoder)
2630{
Chris Wilson4ef69c72010-09-09 15:14:28 +01002631 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01002632
Chris Wilsonea5b2132010-08-04 13:50:23 +01002633 drm_encoder_cleanup(encoder);
2634 kfree(intel_encoder);
2635}
2636
Jesse Barnes79e53942008-11-07 14:24:08 -08002637static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
2638 struct drm_display_mode *mode,
2639 struct drm_display_mode *adjusted_mode)
2640{
Zhenyu Wang2c072452009-06-05 15:38:42 +08002641 struct drm_device *dev = crtc->dev;
Chris Wilson89749352010-09-12 18:25:19 +01002642
Eric Anholtbad720f2009-10-22 16:11:14 -07002643 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08002644 /* FDI link clock is fixed at 2.7G */
Jesse Barnes2377b742010-07-07 14:06:43 -07002645 if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
2646 return false;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002647 }
Chris Wilson89749352010-09-12 18:25:19 +01002648
2649 /* XXX some encoders set the crtcinfo, others don't.
2650 * Obviously we need some form of conflict resolution here...
2651 */
2652 if (adjusted_mode->crtc_htotal == 0)
2653 drm_mode_set_crtcinfo(adjusted_mode, 0);
2654
Jesse Barnes79e53942008-11-07 14:24:08 -08002655 return true;
2656}
2657
Jesse Barnese70236a2009-09-21 10:42:27 -07002658static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08002659{
Jesse Barnese70236a2009-09-21 10:42:27 -07002660 return 400000;
2661}
Jesse Barnes79e53942008-11-07 14:24:08 -08002662
Jesse Barnese70236a2009-09-21 10:42:27 -07002663static int i915_get_display_clock_speed(struct drm_device *dev)
2664{
2665 return 333000;
2666}
Jesse Barnes79e53942008-11-07 14:24:08 -08002667
Jesse Barnese70236a2009-09-21 10:42:27 -07002668static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
2669{
2670 return 200000;
2671}
Jesse Barnes79e53942008-11-07 14:24:08 -08002672
Jesse Barnese70236a2009-09-21 10:42:27 -07002673static int i915gm_get_display_clock_speed(struct drm_device *dev)
2674{
2675 u16 gcfgc = 0;
2676
2677 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
2678
2679 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08002680 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07002681 else {
2682 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
2683 case GC_DISPLAY_CLOCK_333_MHZ:
2684 return 333000;
2685 default:
2686 case GC_DISPLAY_CLOCK_190_200_MHZ:
2687 return 190000;
2688 }
2689 }
2690}
Jesse Barnes79e53942008-11-07 14:24:08 -08002691
Jesse Barnese70236a2009-09-21 10:42:27 -07002692static int i865_get_display_clock_speed(struct drm_device *dev)
2693{
2694 return 266000;
2695}
2696
2697static int i855_get_display_clock_speed(struct drm_device *dev)
2698{
2699 u16 hpllcc = 0;
2700 /* Assume that the hardware is in the high speed state. This
2701 * should be the default.
2702 */
2703 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
2704 case GC_CLOCK_133_200:
2705 case GC_CLOCK_100_200:
2706 return 200000;
2707 case GC_CLOCK_166_250:
2708 return 250000;
2709 case GC_CLOCK_100_133:
2710 return 133000;
2711 }
2712
2713 /* Shouldn't happen */
2714 return 0;
2715}
2716
2717static int i830_get_display_clock_speed(struct drm_device *dev)
2718{
2719 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08002720}
2721
Zhenyu Wang2c072452009-06-05 15:38:42 +08002722struct fdi_m_n {
2723 u32 tu;
2724 u32 gmch_m;
2725 u32 gmch_n;
2726 u32 link_m;
2727 u32 link_n;
2728};
2729
2730static void
2731fdi_reduce_ratio(u32 *num, u32 *den)
2732{
2733 while (*num > 0xffffff || *den > 0xffffff) {
2734 *num >>= 1;
2735 *den >>= 1;
2736 }
2737}
2738
Zhenyu Wang2c072452009-06-05 15:38:42 +08002739static void
Adam Jacksonf2b115e2009-12-03 17:14:42 -05002740ironlake_compute_m_n(int bits_per_pixel, int nlanes, int pixel_clock,
2741 int link_clock, struct fdi_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08002742{
Zhenyu Wang2c072452009-06-05 15:38:42 +08002743 m_n->tu = 64; /* default size */
2744
Chris Wilson22ed1112010-12-04 01:01:29 +00002745 /* BUG_ON(pixel_clock > INT_MAX / 36); */
2746 m_n->gmch_m = bits_per_pixel * pixel_clock;
2747 m_n->gmch_n = link_clock * nlanes * 8;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002748 fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
2749
Chris Wilson22ed1112010-12-04 01:01:29 +00002750 m_n->link_m = pixel_clock;
2751 m_n->link_n = link_clock;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002752 fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
2753}
2754
2755
Shaohua Li7662c8b2009-06-26 11:23:55 +08002756struct intel_watermark_params {
2757 unsigned long fifo_size;
2758 unsigned long max_wm;
2759 unsigned long default_wm;
2760 unsigned long guard_size;
2761 unsigned long cacheline_size;
2762};
2763
Adam Jacksonf2b115e2009-12-03 17:14:42 -05002764/* Pineview has different values for various configs */
2765static struct intel_watermark_params pineview_display_wm = {
2766 PINEVIEW_DISPLAY_FIFO,
2767 PINEVIEW_MAX_WM,
2768 PINEVIEW_DFT_WM,
2769 PINEVIEW_GUARD_WM,
2770 PINEVIEW_FIFO_LINE_SIZE
Shaohua Li7662c8b2009-06-26 11:23:55 +08002771};
Adam Jacksonf2b115e2009-12-03 17:14:42 -05002772static struct intel_watermark_params pineview_display_hplloff_wm = {
2773 PINEVIEW_DISPLAY_FIFO,
2774 PINEVIEW_MAX_WM,
2775 PINEVIEW_DFT_HPLLOFF_WM,
2776 PINEVIEW_GUARD_WM,
2777 PINEVIEW_FIFO_LINE_SIZE
Shaohua Li7662c8b2009-06-26 11:23:55 +08002778};
Adam Jacksonf2b115e2009-12-03 17:14:42 -05002779static struct intel_watermark_params pineview_cursor_wm = {
2780 PINEVIEW_CURSOR_FIFO,
2781 PINEVIEW_CURSOR_MAX_WM,
2782 PINEVIEW_CURSOR_DFT_WM,
2783 PINEVIEW_CURSOR_GUARD_WM,
2784 PINEVIEW_FIFO_LINE_SIZE,
Shaohua Li7662c8b2009-06-26 11:23:55 +08002785};
Adam Jacksonf2b115e2009-12-03 17:14:42 -05002786static struct intel_watermark_params pineview_cursor_hplloff_wm = {
2787 PINEVIEW_CURSOR_FIFO,
2788 PINEVIEW_CURSOR_MAX_WM,
2789 PINEVIEW_CURSOR_DFT_WM,
2790 PINEVIEW_CURSOR_GUARD_WM,
2791 PINEVIEW_FIFO_LINE_SIZE
Shaohua Li7662c8b2009-06-26 11:23:55 +08002792};
Jesse Barnes0e442c62009-10-19 10:09:33 +09002793static struct intel_watermark_params g4x_wm_info = {
2794 G4X_FIFO_SIZE,
2795 G4X_MAX_WM,
2796 G4X_MAX_WM,
2797 2,
2798 G4X_FIFO_LINE_SIZE,
2799};
Zhao Yakui4fe5e612010-06-12 14:32:25 +08002800static struct intel_watermark_params g4x_cursor_wm_info = {
2801 I965_CURSOR_FIFO,
2802 I965_CURSOR_MAX_WM,
2803 I965_CURSOR_DFT_WM,
2804 2,
2805 G4X_FIFO_LINE_SIZE,
2806};
2807static struct intel_watermark_params i965_cursor_wm_info = {
2808 I965_CURSOR_FIFO,
2809 I965_CURSOR_MAX_WM,
2810 I965_CURSOR_DFT_WM,
2811 2,
2812 I915_FIFO_LINE_SIZE,
2813};
Shaohua Li7662c8b2009-06-26 11:23:55 +08002814static struct intel_watermark_params i945_wm_info = {
Shaohua Li7662c8b2009-06-26 11:23:55 +08002815 I945_FIFO_SIZE,
2816 I915_MAX_WM,
2817 1,
Jesse Barnesdff33cf2009-07-14 10:15:56 -07002818 2,
2819 I915_FIFO_LINE_SIZE
2820};
2821static struct intel_watermark_params i915_wm_info = {
2822 I915_FIFO_SIZE,
2823 I915_MAX_WM,
2824 1,
2825 2,
Shaohua Li7662c8b2009-06-26 11:23:55 +08002826 I915_FIFO_LINE_SIZE
2827};
2828static struct intel_watermark_params i855_wm_info = {
2829 I855GM_FIFO_SIZE,
2830 I915_MAX_WM,
2831 1,
Jesse Barnesdff33cf2009-07-14 10:15:56 -07002832 2,
Shaohua Li7662c8b2009-06-26 11:23:55 +08002833 I830_FIFO_LINE_SIZE
2834};
2835static struct intel_watermark_params i830_wm_info = {
2836 I830_FIFO_SIZE,
2837 I915_MAX_WM,
2838 1,
Jesse Barnesdff33cf2009-07-14 10:15:56 -07002839 2,
Shaohua Li7662c8b2009-06-26 11:23:55 +08002840 I830_FIFO_LINE_SIZE
2841};
2842
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08002843static struct intel_watermark_params ironlake_display_wm_info = {
2844 ILK_DISPLAY_FIFO,
2845 ILK_DISPLAY_MAXWM,
2846 ILK_DISPLAY_DFTWM,
2847 2,
2848 ILK_FIFO_LINE_SIZE
2849};
2850
Zhao Yakuic936f442010-06-12 14:32:26 +08002851static struct intel_watermark_params ironlake_cursor_wm_info = {
2852 ILK_CURSOR_FIFO,
2853 ILK_CURSOR_MAXWM,
2854 ILK_CURSOR_DFTWM,
2855 2,
2856 ILK_FIFO_LINE_SIZE
2857};
2858
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08002859static struct intel_watermark_params ironlake_display_srwm_info = {
2860 ILK_DISPLAY_SR_FIFO,
2861 ILK_DISPLAY_MAX_SRWM,
2862 ILK_DISPLAY_DFT_SRWM,
2863 2,
2864 ILK_FIFO_LINE_SIZE
2865};
2866
2867static struct intel_watermark_params ironlake_cursor_srwm_info = {
2868 ILK_CURSOR_SR_FIFO,
2869 ILK_CURSOR_MAX_SRWM,
2870 ILK_CURSOR_DFT_SRWM,
2871 2,
2872 ILK_FIFO_LINE_SIZE
2873};
2874
Yuanhan Liu13982612010-12-15 15:42:31 +08002875static struct intel_watermark_params sandybridge_display_wm_info = {
2876 SNB_DISPLAY_FIFO,
2877 SNB_DISPLAY_MAXWM,
2878 SNB_DISPLAY_DFTWM,
2879 2,
2880 SNB_FIFO_LINE_SIZE
2881};
2882
2883static struct intel_watermark_params sandybridge_cursor_wm_info = {
2884 SNB_CURSOR_FIFO,
2885 SNB_CURSOR_MAXWM,
2886 SNB_CURSOR_DFTWM,
2887 2,
2888 SNB_FIFO_LINE_SIZE
2889};
2890
2891static struct intel_watermark_params sandybridge_display_srwm_info = {
2892 SNB_DISPLAY_SR_FIFO,
2893 SNB_DISPLAY_MAX_SRWM,
2894 SNB_DISPLAY_DFT_SRWM,
2895 2,
2896 SNB_FIFO_LINE_SIZE
2897};
2898
2899static struct intel_watermark_params sandybridge_cursor_srwm_info = {
2900 SNB_CURSOR_SR_FIFO,
2901 SNB_CURSOR_MAX_SRWM,
2902 SNB_CURSOR_DFT_SRWM,
2903 2,
2904 SNB_FIFO_LINE_SIZE
2905};
2906
2907
Jesse Barnesdff33cf2009-07-14 10:15:56 -07002908/**
2909 * intel_calculate_wm - calculate watermark level
2910 * @clock_in_khz: pixel clock
2911 * @wm: chip FIFO params
2912 * @pixel_size: display pixel size
2913 * @latency_ns: memory latency for the platform
2914 *
2915 * Calculate the watermark level (the level at which the display plane will
2916 * start fetching from memory again). Each chip has a different display
2917 * FIFO size and allocation, so the caller needs to figure that out and pass
2918 * in the correct intel_watermark_params structure.
2919 *
2920 * As the pixel clock runs, the FIFO will be drained at a rate that depends
2921 * on the pixel size. When it reaches the watermark level, it'll start
2922 * fetching FIFO line sized based chunks from memory until the FIFO fills
2923 * past the watermark point. If the FIFO drains completely, a FIFO underrun
2924 * will occur, and a display engine hang could result.
2925 */
Shaohua Li7662c8b2009-06-26 11:23:55 +08002926static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
2927 struct intel_watermark_params *wm,
2928 int pixel_size,
2929 unsigned long latency_ns)
2930{
Jesse Barnes390c4dd2009-07-16 13:01:01 -07002931 long entries_required, wm_size;
Shaohua Li7662c8b2009-06-26 11:23:55 +08002932
Jesse Barnesd6604672009-09-11 12:25:56 -07002933 /*
2934 * Note: we need to make sure we don't overflow for various clock &
2935 * latency values.
2936 * clocks go from a few thousand to several hundred thousand.
2937 * latency is usually a few thousand
2938 */
2939 entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
2940 1000;
Chris Wilson8de9b312010-07-19 19:59:52 +01002941 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
Jesse Barnesdff33cf2009-07-14 10:15:56 -07002942
Zhao Yakui28c97732009-10-09 11:39:41 +08002943 DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries_required);
Jesse Barnesdff33cf2009-07-14 10:15:56 -07002944
2945 wm_size = wm->fifo_size - (entries_required + wm->guard_size);
2946
Zhao Yakui28c97732009-10-09 11:39:41 +08002947 DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
Shaohua Li7662c8b2009-06-26 11:23:55 +08002948
Jesse Barnes390c4dd2009-07-16 13:01:01 -07002949 /* Don't promote wm_size to unsigned... */
2950 if (wm_size > (long)wm->max_wm)
Shaohua Li7662c8b2009-06-26 11:23:55 +08002951 wm_size = wm->max_wm;
Chris Wilsonc3add4b2010-09-08 09:14:08 +01002952 if (wm_size <= 0)
Shaohua Li7662c8b2009-06-26 11:23:55 +08002953 wm_size = wm->default_wm;
2954 return wm_size;
2955}
2956
2957struct cxsr_latency {
2958 int is_desktop;
Li Peng95534262010-05-18 18:58:44 +08002959 int is_ddr3;
Shaohua Li7662c8b2009-06-26 11:23:55 +08002960 unsigned long fsb_freq;
2961 unsigned long mem_freq;
2962 unsigned long display_sr;
2963 unsigned long display_hpll_disable;
2964 unsigned long cursor_sr;
2965 unsigned long cursor_hpll_disable;
2966};
2967
Chris Wilson403c89f2010-08-04 15:25:31 +01002968static const struct cxsr_latency cxsr_latency_table[] = {
Li Peng95534262010-05-18 18:58:44 +08002969 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
2970 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
2971 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
2972 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
2973 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08002974
Li Peng95534262010-05-18 18:58:44 +08002975 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
2976 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
2977 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
2978 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
2979 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08002980
Li Peng95534262010-05-18 18:58:44 +08002981 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
2982 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
2983 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
2984 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
2985 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08002986
Li Peng95534262010-05-18 18:58:44 +08002987 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
2988 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
2989 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
2990 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
2991 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08002992
Li Peng95534262010-05-18 18:58:44 +08002993 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
2994 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
2995 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
2996 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
2997 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08002998
Li Peng95534262010-05-18 18:58:44 +08002999 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
3000 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
3001 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
3002 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
3003 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003004};
3005
Chris Wilson403c89f2010-08-04 15:25:31 +01003006static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
3007 int is_ddr3,
3008 int fsb,
3009 int mem)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003010{
Chris Wilson403c89f2010-08-04 15:25:31 +01003011 const struct cxsr_latency *latency;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003012 int i;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003013
3014 if (fsb == 0 || mem == 0)
3015 return NULL;
3016
3017 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
3018 latency = &cxsr_latency_table[i];
3019 if (is_desktop == latency->is_desktop &&
Li Peng95534262010-05-18 18:58:44 +08003020 is_ddr3 == latency->is_ddr3 &&
Jaswinder Singh Rajputdecbbcd2009-09-12 23:15:07 +05303021 fsb == latency->fsb_freq && mem == latency->mem_freq)
3022 return latency;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003023 }
Jaswinder Singh Rajputdecbbcd2009-09-12 23:15:07 +05303024
Zhao Yakui28c97732009-10-09 11:39:41 +08003025 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Jaswinder Singh Rajputdecbbcd2009-09-12 23:15:07 +05303026
3027 return NULL;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003028}
3029
Adam Jacksonf2b115e2009-12-03 17:14:42 -05003030static void pineview_disable_cxsr(struct drm_device *dev)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003031{
3032 struct drm_i915_private *dev_priv = dev->dev_private;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003033
3034 /* deactivate cxsr */
Chris Wilson3e33d942010-08-04 11:17:25 +01003035 I915_WRITE(DSPFW3, I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003036}
3037
Jesse Barnesbcc24fb2009-08-31 10:24:31 -07003038/*
3039 * Latency for FIFO fetches is dependent on several factors:
3040 * - memory configuration (speed, channels)
3041 * - chipset
3042 * - current MCH state
3043 * It can be fairly high in some situations, so here we assume a fairly
3044 * pessimal value. It's a tradeoff between extra memory fetches (if we
3045 * set this value too high, the FIFO will fetch frequently to stay full)
3046 * and power consumption (set it too low to save power and we might see
3047 * FIFO underruns and display "flicker").
3048 *
3049 * A value of 5us seems to be a good balance; safe for very low end
3050 * platforms but not overly aggressive on lower latency configs.
3051 */
Tobias Klauser69e302a2009-12-23 14:14:34 +01003052static const int latency_ns = 5000;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003053
Jesse Barnese70236a2009-09-21 10:42:27 -07003054static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003055{
3056 struct drm_i915_private *dev_priv = dev->dev_private;
3057 uint32_t dsparb = I915_READ(DSPARB);
3058 int size;
3059
Chris Wilson8de9b312010-07-19 19:59:52 +01003060 size = dsparb & 0x7f;
3061 if (plane)
3062 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003063
Zhao Yakui28c97732009-10-09 11:39:41 +08003064 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
Chris Wilson5eddb702010-09-11 13:48:45 +01003065 plane ? "B" : "A", size);
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003066
3067 return size;
3068}
Shaohua Li7662c8b2009-06-26 11:23:55 +08003069
Jesse Barnese70236a2009-09-21 10:42:27 -07003070static int i85x_get_fifo_size(struct drm_device *dev, int plane)
3071{
3072 struct drm_i915_private *dev_priv = dev->dev_private;
3073 uint32_t dsparb = I915_READ(DSPARB);
3074 int size;
3075
Chris Wilson8de9b312010-07-19 19:59:52 +01003076 size = dsparb & 0x1ff;
3077 if (plane)
3078 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
Jesse Barnese70236a2009-09-21 10:42:27 -07003079 size >>= 1; /* Convert to cachelines */
3080
Zhao Yakui28c97732009-10-09 11:39:41 +08003081 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
Chris Wilson5eddb702010-09-11 13:48:45 +01003082 plane ? "B" : "A", size);
Jesse Barnese70236a2009-09-21 10:42:27 -07003083
3084 return size;
3085}
3086
3087static int i845_get_fifo_size(struct drm_device *dev, int plane)
3088{
3089 struct drm_i915_private *dev_priv = dev->dev_private;
3090 uint32_t dsparb = I915_READ(DSPARB);
3091 int size;
3092
3093 size = dsparb & 0x7f;
3094 size >>= 2; /* Convert to cachelines */
3095
Zhao Yakui28c97732009-10-09 11:39:41 +08003096 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
Chris Wilson5eddb702010-09-11 13:48:45 +01003097 plane ? "B" : "A",
3098 size);
Jesse Barnese70236a2009-09-21 10:42:27 -07003099
3100 return size;
3101}
3102
3103static int i830_get_fifo_size(struct drm_device *dev, int plane)
3104{
3105 struct drm_i915_private *dev_priv = dev->dev_private;
3106 uint32_t dsparb = I915_READ(DSPARB);
3107 int size;
3108
3109 size = dsparb & 0x7f;
3110 size >>= 1; /* Convert to cachelines */
3111
Zhao Yakui28c97732009-10-09 11:39:41 +08003112 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
Chris Wilson5eddb702010-09-11 13:48:45 +01003113 plane ? "B" : "A", size);
Jesse Barnese70236a2009-09-21 10:42:27 -07003114
3115 return size;
3116}
3117
Zhao Yakuid4294342010-03-22 22:45:36 +08003118static void pineview_update_wm(struct drm_device *dev, int planea_clock,
Chris Wilson5eddb702010-09-11 13:48:45 +01003119 int planeb_clock, int sr_hdisplay, int unused,
3120 int pixel_size)
Zhao Yakuid4294342010-03-22 22:45:36 +08003121{
3122 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson403c89f2010-08-04 15:25:31 +01003123 const struct cxsr_latency *latency;
Zhao Yakuid4294342010-03-22 22:45:36 +08003124 u32 reg;
3125 unsigned long wm;
Zhao Yakuid4294342010-03-22 22:45:36 +08003126 int sr_clock;
3127
Chris Wilson403c89f2010-08-04 15:25:31 +01003128 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
Li Peng95534262010-05-18 18:58:44 +08003129 dev_priv->fsb_freq, dev_priv->mem_freq);
Zhao Yakuid4294342010-03-22 22:45:36 +08003130 if (!latency) {
3131 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
3132 pineview_disable_cxsr(dev);
3133 return;
3134 }
3135
3136 if (!planea_clock || !planeb_clock) {
3137 sr_clock = planea_clock ? planea_clock : planeb_clock;
3138
3139 /* Display SR */
3140 wm = intel_calculate_wm(sr_clock, &pineview_display_wm,
3141 pixel_size, latency->display_sr);
3142 reg = I915_READ(DSPFW1);
3143 reg &= ~DSPFW_SR_MASK;
3144 reg |= wm << DSPFW_SR_SHIFT;
3145 I915_WRITE(DSPFW1, reg);
3146 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
3147
3148 /* cursor SR */
3149 wm = intel_calculate_wm(sr_clock, &pineview_cursor_wm,
3150 pixel_size, latency->cursor_sr);
3151 reg = I915_READ(DSPFW3);
3152 reg &= ~DSPFW_CURSOR_SR_MASK;
3153 reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
3154 I915_WRITE(DSPFW3, reg);
3155
3156 /* Display HPLL off SR */
3157 wm = intel_calculate_wm(sr_clock, &pineview_display_hplloff_wm,
3158 pixel_size, latency->display_hpll_disable);
3159 reg = I915_READ(DSPFW3);
3160 reg &= ~DSPFW_HPLL_SR_MASK;
3161 reg |= wm & DSPFW_HPLL_SR_MASK;
3162 I915_WRITE(DSPFW3, reg);
3163
3164 /* cursor HPLL off SR */
3165 wm = intel_calculate_wm(sr_clock, &pineview_cursor_hplloff_wm,
3166 pixel_size, latency->cursor_hpll_disable);
3167 reg = I915_READ(DSPFW3);
3168 reg &= ~DSPFW_HPLL_CURSOR_MASK;
3169 reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
3170 I915_WRITE(DSPFW3, reg);
3171 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
3172
3173 /* activate cxsr */
Chris Wilson3e33d942010-08-04 11:17:25 +01003174 I915_WRITE(DSPFW3,
3175 I915_READ(DSPFW3) | PINEVIEW_SELF_REFRESH_EN);
Zhao Yakuid4294342010-03-22 22:45:36 +08003176 DRM_DEBUG_KMS("Self-refresh is enabled\n");
3177 } else {
3178 pineview_disable_cxsr(dev);
3179 DRM_DEBUG_KMS("Self-refresh is disabled\n");
3180 }
3181}
3182
Jesse Barnes0e442c62009-10-19 10:09:33 +09003183static void g4x_update_wm(struct drm_device *dev, int planea_clock,
Zhao Yakuifa143212010-06-12 14:32:23 +08003184 int planeb_clock, int sr_hdisplay, int sr_htotal,
3185 int pixel_size)
Jesse Barnes652c3932009-08-17 13:31:43 -07003186{
3187 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e442c62009-10-19 10:09:33 +09003188 int total_size, cacheline_size;
3189 int planea_wm, planeb_wm, cursora_wm, cursorb_wm, cursor_sr;
3190 struct intel_watermark_params planea_params, planeb_params;
3191 unsigned long line_time_us;
3192 int sr_clock, sr_entries = 0, entries_required;
Jesse Barnes652c3932009-08-17 13:31:43 -07003193
Jesse Barnes0e442c62009-10-19 10:09:33 +09003194 /* Create copies of the base settings for each pipe */
3195 planea_params = planeb_params = g4x_wm_info;
3196
3197 /* Grab a couple of global values before we overwrite them */
3198 total_size = planea_params.fifo_size;
3199 cacheline_size = planea_params.cacheline_size;
3200
3201 /*
3202 * Note: we need to make sure we don't overflow for various clock &
3203 * latency values.
3204 * clocks go from a few thousand to several hundred thousand.
3205 * latency is usually a few thousand
3206 */
3207 entries_required = ((planea_clock / 1000) * pixel_size * latency_ns) /
3208 1000;
Chris Wilson8de9b312010-07-19 19:59:52 +01003209 entries_required = DIV_ROUND_UP(entries_required, G4X_FIFO_LINE_SIZE);
Jesse Barnes0e442c62009-10-19 10:09:33 +09003210 planea_wm = entries_required + planea_params.guard_size;
3211
3212 entries_required = ((planeb_clock / 1000) * pixel_size * latency_ns) /
3213 1000;
Chris Wilson8de9b312010-07-19 19:59:52 +01003214 entries_required = DIV_ROUND_UP(entries_required, G4X_FIFO_LINE_SIZE);
Jesse Barnes0e442c62009-10-19 10:09:33 +09003215 planeb_wm = entries_required + planeb_params.guard_size;
3216
3217 cursora_wm = cursorb_wm = 16;
3218 cursor_sr = 32;
3219
3220 DRM_DEBUG("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
3221
3222 /* Calc sr entries for one plane configs */
3223 if (sr_hdisplay && (!planea_clock || !planeb_clock)) {
3224 /* self-refresh has much higher latency */
Tobias Klauser69e302a2009-12-23 14:14:34 +01003225 static const int sr_latency_ns = 12000;
Jesse Barnes0e442c62009-10-19 10:09:33 +09003226
3227 sr_clock = planea_clock ? planea_clock : planeb_clock;
Zhao Yakuifa143212010-06-12 14:32:23 +08003228 line_time_us = ((sr_htotal * 1000) / sr_clock);
Jesse Barnes0e442c62009-10-19 10:09:33 +09003229
3230 /* Use ns/us then divide to preserve precision */
Zhao Yakuifa143212010-06-12 14:32:23 +08003231 sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Chris Wilson5eddb702010-09-11 13:48:45 +01003232 pixel_size * sr_hdisplay;
Chris Wilson8de9b312010-07-19 19:59:52 +01003233 sr_entries = DIV_ROUND_UP(sr_entries, cacheline_size);
Zhao Yakui4fe5e612010-06-12 14:32:25 +08003234
3235 entries_required = (((sr_latency_ns / line_time_us) +
3236 1000) / 1000) * pixel_size * 64;
Chris Wilson8de9b312010-07-19 19:59:52 +01003237 entries_required = DIV_ROUND_UP(entries_required,
Chris Wilson5eddb702010-09-11 13:48:45 +01003238 g4x_cursor_wm_info.cacheline_size);
Zhao Yakui4fe5e612010-06-12 14:32:25 +08003239 cursor_sr = entries_required + g4x_cursor_wm_info.guard_size;
3240
3241 if (cursor_sr > g4x_cursor_wm_info.max_wm)
3242 cursor_sr = g4x_cursor_wm_info.max_wm;
3243 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
3244 "cursor %d\n", sr_entries, cursor_sr);
3245
Jesse Barnes0e442c62009-10-19 10:09:33 +09003246 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
David John33c5fd12010-01-27 15:19:08 +05303247 } else {
3248 /* Turn off self refresh if both pipes are enabled */
3249 I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
Chris Wilson5eddb702010-09-11 13:48:45 +01003250 & ~FW_BLC_SELF_EN);
Jesse Barnes0e442c62009-10-19 10:09:33 +09003251 }
3252
3253 DRM_DEBUG("Setting FIFO watermarks - A: %d, B: %d, SR %d\n",
3254 planea_wm, planeb_wm, sr_entries);
3255
3256 planea_wm &= 0x3f;
3257 planeb_wm &= 0x3f;
3258
3259 I915_WRITE(DSPFW1, (sr_entries << DSPFW_SR_SHIFT) |
3260 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
3261 (planeb_wm << DSPFW_PLANEB_SHIFT) | planea_wm);
3262 I915_WRITE(DSPFW2, (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
3263 (cursora_wm << DSPFW_CURSORA_SHIFT));
3264 /* HPLL off in SR has some issues on G4x... disable it */
3265 I915_WRITE(DSPFW3, (I915_READ(DSPFW3) & ~DSPFW_HPLL_SR_EN) |
3266 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Jesse Barnes652c3932009-08-17 13:31:43 -07003267}
3268
Jesse Barnes1dc75462009-10-19 10:08:17 +09003269static void i965_update_wm(struct drm_device *dev, int planea_clock,
Zhao Yakuifa143212010-06-12 14:32:23 +08003270 int planeb_clock, int sr_hdisplay, int sr_htotal,
3271 int pixel_size)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003272{
3273 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1dc75462009-10-19 10:08:17 +09003274 unsigned long line_time_us;
3275 int sr_clock, sr_entries, srwm = 1;
Zhao Yakui4fe5e612010-06-12 14:32:25 +08003276 int cursor_sr = 16;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003277
Jesse Barnes1dc75462009-10-19 10:08:17 +09003278 /* Calc sr entries for one plane configs */
3279 if (sr_hdisplay && (!planea_clock || !planeb_clock)) {
3280 /* self-refresh has much higher latency */
Tobias Klauser69e302a2009-12-23 14:14:34 +01003281 static const int sr_latency_ns = 12000;
Jesse Barnes1dc75462009-10-19 10:08:17 +09003282
3283 sr_clock = planea_clock ? planea_clock : planeb_clock;
Zhao Yakuifa143212010-06-12 14:32:23 +08003284 line_time_us = ((sr_htotal * 1000) / sr_clock);
Jesse Barnes1dc75462009-10-19 10:08:17 +09003285
3286 /* Use ns/us then divide to preserve precision */
Zhao Yakuifa143212010-06-12 14:32:23 +08003287 sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Chris Wilson5eddb702010-09-11 13:48:45 +01003288 pixel_size * sr_hdisplay;
Chris Wilson8de9b312010-07-19 19:59:52 +01003289 sr_entries = DIV_ROUND_UP(sr_entries, I915_FIFO_LINE_SIZE);
Jesse Barnes1dc75462009-10-19 10:08:17 +09003290 DRM_DEBUG("self-refresh entries: %d\n", sr_entries);
Zhao Yakui1b07e042010-06-12 14:32:24 +08003291 srwm = I965_FIFO_SIZE - sr_entries;
Jesse Barnes1dc75462009-10-19 10:08:17 +09003292 if (srwm < 0)
3293 srwm = 1;
Zhao Yakui1b07e042010-06-12 14:32:24 +08003294 srwm &= 0x1ff;
Zhao Yakui4fe5e612010-06-12 14:32:25 +08003295
3296 sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Chris Wilson5eddb702010-09-11 13:48:45 +01003297 pixel_size * 64;
Chris Wilson8de9b312010-07-19 19:59:52 +01003298 sr_entries = DIV_ROUND_UP(sr_entries,
3299 i965_cursor_wm_info.cacheline_size);
Zhao Yakui4fe5e612010-06-12 14:32:25 +08003300 cursor_sr = i965_cursor_wm_info.fifo_size -
Chris Wilson5eddb702010-09-11 13:48:45 +01003301 (sr_entries + i965_cursor_wm_info.guard_size);
Zhao Yakui4fe5e612010-06-12 14:32:25 +08003302
3303 if (cursor_sr > i965_cursor_wm_info.max_wm)
3304 cursor_sr = i965_cursor_wm_info.max_wm;
3305
3306 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
3307 "cursor %d\n", srwm, cursor_sr);
3308
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003309 if (IS_CRESTLINE(dev))
Jesse Barnesadcdbc62010-06-30 13:49:37 -07003310 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
David John33c5fd12010-01-27 15:19:08 +05303311 } else {
3312 /* Turn off self refresh if both pipes are enabled */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003313 if (IS_CRESTLINE(dev))
Jesse Barnesadcdbc62010-06-30 13:49:37 -07003314 I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
3315 & ~FW_BLC_SELF_EN);
Jesse Barnes1dc75462009-10-19 10:08:17 +09003316 }
3317
3318 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
3319 srwm);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003320
3321 /* 965 has limitations... */
Jesse Barnes1dc75462009-10-19 10:08:17 +09003322 I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) | (8 << 16) | (8 << 8) |
3323 (8 << 0));
Shaohua Li7662c8b2009-06-26 11:23:55 +08003324 I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
Zhao Yakui4fe5e612010-06-12 14:32:25 +08003325 /* update cursor SR watermark */
3326 I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Shaohua Li7662c8b2009-06-26 11:23:55 +08003327}
3328
3329static void i9xx_update_wm(struct drm_device *dev, int planea_clock,
Zhao Yakuifa143212010-06-12 14:32:23 +08003330 int planeb_clock, int sr_hdisplay, int sr_htotal,
3331 int pixel_size)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003332{
3333 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003334 uint32_t fwater_lo;
3335 uint32_t fwater_hi;
3336 int total_size, cacheline_size, cwm, srwm = 1;
3337 int planea_wm, planeb_wm;
3338 struct intel_watermark_params planea_params, planeb_params;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003339 unsigned long line_time_us;
3340 int sr_clock, sr_entries = 0;
3341
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003342 /* Create copies of the base settings for each pipe */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003343 if (IS_CRESTLINE(dev) || IS_I945GM(dev))
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003344 planea_params = planeb_params = i945_wm_info;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003345 else if (!IS_GEN2(dev))
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003346 planea_params = planeb_params = i915_wm_info;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003347 else
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003348 planea_params = planeb_params = i855_wm_info;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003349
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003350 /* Grab a couple of global values before we overwrite them */
3351 total_size = planea_params.fifo_size;
3352 cacheline_size = planea_params.cacheline_size;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003353
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003354 /* Update per-plane FIFO sizes */
Jesse Barnese70236a2009-09-21 10:42:27 -07003355 planea_params.fifo_size = dev_priv->display.get_fifo_size(dev, 0);
3356 planeb_params.fifo_size = dev_priv->display.get_fifo_size(dev, 1);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003357
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003358 planea_wm = intel_calculate_wm(planea_clock, &planea_params,
3359 pixel_size, latency_ns);
3360 planeb_wm = intel_calculate_wm(planeb_clock, &planeb_params,
3361 pixel_size, latency_ns);
Zhao Yakui28c97732009-10-09 11:39:41 +08003362 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003363
3364 /*
3365 * Overlay gets an aggressive default since video jitter is bad.
3366 */
3367 cwm = 2;
3368
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003369 /* Calc sr entries for one plane configs */
Jesse Barnes652c3932009-08-17 13:31:43 -07003370 if (HAS_FW_BLC(dev) && sr_hdisplay &&
3371 (!planea_clock || !planeb_clock)) {
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003372 /* self-refresh has much higher latency */
Tobias Klauser69e302a2009-12-23 14:14:34 +01003373 static const int sr_latency_ns = 6000;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003374
Shaohua Li7662c8b2009-06-26 11:23:55 +08003375 sr_clock = planea_clock ? planea_clock : planeb_clock;
Zhao Yakuifa143212010-06-12 14:32:23 +08003376 line_time_us = ((sr_htotal * 1000) / sr_clock);
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003377
3378 /* Use ns/us then divide to preserve precision */
Zhao Yakuifa143212010-06-12 14:32:23 +08003379 sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Chris Wilson5eddb702010-09-11 13:48:45 +01003380 pixel_size * sr_hdisplay;
Chris Wilson8de9b312010-07-19 19:59:52 +01003381 sr_entries = DIV_ROUND_UP(sr_entries, cacheline_size);
Zhao Yakui28c97732009-10-09 11:39:41 +08003382 DRM_DEBUG_KMS("self-refresh entries: %d\n", sr_entries);
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003383 srwm = total_size - sr_entries;
3384 if (srwm < 0)
3385 srwm = 1;
Li Pengee980b82010-01-27 19:01:11 +08003386
3387 if (IS_I945G(dev) || IS_I945GM(dev))
3388 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
3389 else if (IS_I915GM(dev)) {
3390 /* 915M has a smaller SRWM field */
3391 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
3392 I915_WRITE(INSTPM, I915_READ(INSTPM) | INSTPM_SELF_EN);
3393 }
David John33c5fd12010-01-27 15:19:08 +05303394 } else {
3395 /* Turn off self refresh if both pipes are enabled */
Li Pengee980b82010-01-27 19:01:11 +08003396 if (IS_I945G(dev) || IS_I945GM(dev)) {
3397 I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
3398 & ~FW_BLC_SELF_EN);
3399 } else if (IS_I915GM(dev)) {
3400 I915_WRITE(INSTPM, I915_READ(INSTPM) & ~INSTPM_SELF_EN);
3401 }
Shaohua Li7662c8b2009-06-26 11:23:55 +08003402 }
3403
Zhao Yakui28c97732009-10-09 11:39:41 +08003404 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
Chris Wilson5eddb702010-09-11 13:48:45 +01003405 planea_wm, planeb_wm, cwm, srwm);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003406
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003407 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
3408 fwater_hi = (cwm & 0x1f);
3409
3410 /* Set request length to 8 cachelines per fetch */
3411 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
3412 fwater_hi = fwater_hi | (1 << 8);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003413
3414 I915_WRITE(FW_BLC, fwater_lo);
3415 I915_WRITE(FW_BLC2, fwater_hi);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003416}
3417
Jesse Barnese70236a2009-09-21 10:42:27 -07003418static void i830_update_wm(struct drm_device *dev, int planea_clock, int unused,
Zhao Yakuifa143212010-06-12 14:32:23 +08003419 int unused2, int unused3, int pixel_size)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003420{
3421 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf3601322009-07-22 12:54:59 -07003422 uint32_t fwater_lo = I915_READ(FW_BLC) & ~0xfff;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003423 int planea_wm;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003424
Jesse Barnese70236a2009-09-21 10:42:27 -07003425 i830_wm_info.fifo_size = dev_priv->display.get_fifo_size(dev, 0);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003426
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003427 planea_wm = intel_calculate_wm(planea_clock, &i830_wm_info,
3428 pixel_size, latency_ns);
Jesse Barnesf3601322009-07-22 12:54:59 -07003429 fwater_lo |= (3<<8) | planea_wm;
3430
Zhao Yakui28c97732009-10-09 11:39:41 +08003431 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003432
3433 I915_WRITE(FW_BLC, fwater_lo);
3434}
3435
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003436#define ILK_LP0_PLANE_LATENCY 700
Zhao Yakuic936f442010-06-12 14:32:26 +08003437#define ILK_LP0_CURSOR_LATENCY 1300
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003438
Chris Wilson4ed765f2010-09-11 10:46:47 +01003439static bool ironlake_compute_wm0(struct drm_device *dev,
3440 int pipe,
Yuanhan Liu13982612010-12-15 15:42:31 +08003441 const struct intel_watermark_params *display,
Yuanhan Liua0fa62d2010-12-23 16:35:40 +08003442 int display_latency_ns,
Yuanhan Liu13982612010-12-15 15:42:31 +08003443 const struct intel_watermark_params *cursor,
Yuanhan Liua0fa62d2010-12-23 16:35:40 +08003444 int cursor_latency_ns,
Chris Wilson4ed765f2010-09-11 10:46:47 +01003445 int *plane_wm,
3446 int *cursor_wm)
3447{
3448 struct drm_crtc *crtc;
Chris Wilsondb66e372011-01-08 09:02:21 +00003449 int htotal, hdisplay, clock, pixel_size;
3450 int line_time_us, line_count;
3451 int entries, tlb_miss;
Chris Wilson4ed765f2010-09-11 10:46:47 +01003452
3453 crtc = intel_get_crtc_for_pipe(dev, pipe);
3454 if (crtc->fb == NULL || !crtc->enabled)
3455 return false;
3456
3457 htotal = crtc->mode.htotal;
3458 hdisplay = crtc->mode.hdisplay;
3459 clock = crtc->mode.clock;
3460 pixel_size = crtc->fb->bits_per_pixel / 8;
3461
3462 /* Use the small buffer method to calculate plane watermark */
Yuanhan Liua0fa62d2010-12-23 16:35:40 +08003463 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
Chris Wilsondb66e372011-01-08 09:02:21 +00003464 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
3465 if (tlb_miss > 0)
3466 entries += tlb_miss;
Yuanhan Liu13982612010-12-15 15:42:31 +08003467 entries = DIV_ROUND_UP(entries, display->cacheline_size);
3468 *plane_wm = entries + display->guard_size;
3469 if (*plane_wm > (int)display->max_wm)
3470 *plane_wm = display->max_wm;
Chris Wilson4ed765f2010-09-11 10:46:47 +01003471
3472 /* Use the large buffer method to calculate cursor watermark */
3473 line_time_us = ((htotal * 1000) / clock);
Yuanhan Liua0fa62d2010-12-23 16:35:40 +08003474 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
Chris Wilson4ed765f2010-09-11 10:46:47 +01003475 entries = line_count * 64 * pixel_size;
Chris Wilsondb66e372011-01-08 09:02:21 +00003476 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
3477 if (tlb_miss > 0)
3478 entries += tlb_miss;
Yuanhan Liu13982612010-12-15 15:42:31 +08003479 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
3480 *cursor_wm = entries + cursor->guard_size;
3481 if (*cursor_wm > (int)cursor->max_wm)
3482 *cursor_wm = (int)cursor->max_wm;
Chris Wilson4ed765f2010-09-11 10:46:47 +01003483
3484 return true;
3485}
3486
Jesse Barnesb79d4992010-12-21 13:10:23 -08003487/*
3488 * Check the wm result.
3489 *
3490 * If any calculated watermark values is larger than the maximum value that
3491 * can be programmed into the associated watermark register, that watermark
3492 * must be disabled.
3493 */
3494static bool ironlake_check_srwm(struct drm_device *dev, int level,
3495 int fbc_wm, int display_wm, int cursor_wm,
3496 const struct intel_watermark_params *display,
3497 const struct intel_watermark_params *cursor)
3498{
3499 struct drm_i915_private *dev_priv = dev->dev_private;
3500
3501 DRM_DEBUG_KMS("watermark %d: display plane %d, fbc lines %d,"
3502 " cursor %d\n", level, display_wm, fbc_wm, cursor_wm);
3503
3504 if (fbc_wm > SNB_FBC_MAX_SRWM) {
3505 DRM_DEBUG_KMS("fbc watermark(%d) is too large(%d), disabling wm%d+\n",
3506 fbc_wm, SNB_FBC_MAX_SRWM, level);
3507
3508 /* fbc has it's own way to disable FBC WM */
3509 I915_WRITE(DISP_ARB_CTL,
3510 I915_READ(DISP_ARB_CTL) | DISP_FBC_WM_DIS);
3511 return false;
3512 }
3513
3514 if (display_wm > display->max_wm) {
3515 DRM_DEBUG_KMS("display watermark(%d) is too large(%d), disabling wm%d+\n",
3516 display_wm, SNB_DISPLAY_MAX_SRWM, level);
3517 return false;
3518 }
3519
3520 if (cursor_wm > cursor->max_wm) {
3521 DRM_DEBUG_KMS("cursor watermark(%d) is too large(%d), disabling wm%d+\n",
3522 cursor_wm, SNB_CURSOR_MAX_SRWM, level);
3523 return false;
3524 }
3525
3526 if (!(fbc_wm || display_wm || cursor_wm)) {
3527 DRM_DEBUG_KMS("latency %d is 0, disabling wm%d+\n", level, level);
3528 return false;
3529 }
3530
3531 return true;
3532}
3533
3534/*
3535 * Compute watermark values of WM[1-3],
3536 */
3537static bool ironlake_compute_srwm(struct drm_device *dev, int level,
3538 int hdisplay, int htotal,
3539 int pixel_size, int clock, int latency_ns,
3540 const struct intel_watermark_params *display,
3541 const struct intel_watermark_params *cursor,
3542 int *fbc_wm, int *display_wm, int *cursor_wm)
3543{
3544
3545 unsigned long line_time_us;
3546 int line_count, line_size;
3547 int small, large;
3548 int entries;
3549
3550 if (!latency_ns) {
3551 *fbc_wm = *display_wm = *cursor_wm = 0;
3552 return false;
3553 }
3554
3555 line_time_us = (htotal * 1000) / clock;
3556 line_count = (latency_ns / line_time_us + 1000) / 1000;
3557 line_size = hdisplay * pixel_size;
3558
3559 /* Use the minimum of the small and large buffer method for primary */
3560 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
3561 large = line_count * line_size;
3562
3563 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
3564 *display_wm = entries + display->guard_size;
3565
3566 /*
3567 * Spec says:
3568 * FBC WM = ((Final Primary WM * 64) / number of bytes per line) + 2
3569 */
3570 *fbc_wm = DIV_ROUND_UP(*display_wm * 64, line_size) + 2;
3571
3572 /* calculate the self-refresh watermark for display cursor */
3573 entries = line_count * pixel_size * 64;
3574 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
3575 *cursor_wm = entries + cursor->guard_size;
3576
3577 return ironlake_check_srwm(dev, level,
3578 *fbc_wm, *display_wm, *cursor_wm,
3579 display, cursor);
3580}
3581
Chris Wilson4ed765f2010-09-11 10:46:47 +01003582static void ironlake_update_wm(struct drm_device *dev,
3583 int planea_clock, int planeb_clock,
Jesse Barnesb79d4992010-12-21 13:10:23 -08003584 int hdisplay, int htotal,
Chris Wilson4ed765f2010-09-11 10:46:47 +01003585 int pixel_size)
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003586{
3587 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesb79d4992010-12-21 13:10:23 -08003588 int fbc_wm, plane_wm, cursor_wm, enabled;
3589 int clock;
Zhao Yakuic936f442010-06-12 14:32:26 +08003590
Chris Wilson4ed765f2010-09-11 10:46:47 +01003591 enabled = 0;
Yuanhan Liu13982612010-12-15 15:42:31 +08003592 if (ironlake_compute_wm0(dev, 0,
3593 &ironlake_display_wm_info,
3594 ILK_LP0_PLANE_LATENCY,
3595 &ironlake_cursor_wm_info,
3596 ILK_LP0_CURSOR_LATENCY,
3597 &plane_wm, &cursor_wm)) {
Chris Wilson4ed765f2010-09-11 10:46:47 +01003598 I915_WRITE(WM0_PIPEA_ILK,
3599 (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
3600 DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
3601 " plane %d, " "cursor: %d\n",
3602 plane_wm, cursor_wm);
3603 enabled++;
Zhao Yakuic936f442010-06-12 14:32:26 +08003604 }
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003605
Yuanhan Liu13982612010-12-15 15:42:31 +08003606 if (ironlake_compute_wm0(dev, 1,
3607 &ironlake_display_wm_info,
3608 ILK_LP0_PLANE_LATENCY,
3609 &ironlake_cursor_wm_info,
3610 ILK_LP0_CURSOR_LATENCY,
3611 &plane_wm, &cursor_wm)) {
Chris Wilson4ed765f2010-09-11 10:46:47 +01003612 I915_WRITE(WM0_PIPEB_ILK,
3613 (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
3614 DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
3615 " plane %d, cursor: %d\n",
3616 plane_wm, cursor_wm);
3617 enabled++;
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003618 }
3619
3620 /*
3621 * Calculate and update the self-refresh watermark only when one
3622 * display plane is used.
3623 */
Jesse Barnesb79d4992010-12-21 13:10:23 -08003624 I915_WRITE(WM3_LP_ILK, 0);
3625 I915_WRITE(WM2_LP_ILK, 0);
3626 I915_WRITE(WM1_LP_ILK, 0);
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003627
Jesse Barnesb79d4992010-12-21 13:10:23 -08003628 if (enabled != 1)
3629 return;
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003630
Jesse Barnesb79d4992010-12-21 13:10:23 -08003631 clock = planea_clock ? planea_clock : planeb_clock;
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003632
Jesse Barnesb79d4992010-12-21 13:10:23 -08003633 /* WM1 */
3634 if (!ironlake_compute_srwm(dev, 1, hdisplay, htotal, pixel_size,
3635 clock, ILK_READ_WM1_LATENCY() * 500,
3636 &ironlake_display_srwm_info,
3637 &ironlake_cursor_srwm_info,
3638 &fbc_wm, &plane_wm, &cursor_wm))
3639 return;
Chris Wilson4ed765f2010-09-11 10:46:47 +01003640
Jesse Barnesb79d4992010-12-21 13:10:23 -08003641 I915_WRITE(WM1_LP_ILK,
3642 WM1_LP_SR_EN |
3643 (ILK_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
3644 (fbc_wm << WM1_LP_FBC_SHIFT) |
3645 (plane_wm << WM1_LP_SR_SHIFT) |
3646 cursor_wm);
Chris Wilson4ed765f2010-09-11 10:46:47 +01003647
Jesse Barnesb79d4992010-12-21 13:10:23 -08003648 /* WM2 */
3649 if (!ironlake_compute_srwm(dev, 2, hdisplay, htotal, pixel_size,
3650 clock, ILK_READ_WM2_LATENCY() * 500,
3651 &ironlake_display_srwm_info,
3652 &ironlake_cursor_srwm_info,
3653 &fbc_wm, &plane_wm, &cursor_wm))
3654 return;
Chris Wilson4ed765f2010-09-11 10:46:47 +01003655
Jesse Barnesb79d4992010-12-21 13:10:23 -08003656 I915_WRITE(WM2_LP_ILK,
3657 WM2_LP_EN |
3658 (ILK_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
3659 (fbc_wm << WM1_LP_FBC_SHIFT) |
3660 (plane_wm << WM1_LP_SR_SHIFT) |
3661 cursor_wm);
Yuanhan Liu13982612010-12-15 15:42:31 +08003662
3663 /*
Jesse Barnesb79d4992010-12-21 13:10:23 -08003664 * WM3 is unsupported on ILK, probably because we don't have latency
3665 * data for that power state
Yuanhan Liu13982612010-12-15 15:42:31 +08003666 */
Yuanhan Liu13982612010-12-15 15:42:31 +08003667}
3668
3669static void sandybridge_update_wm(struct drm_device *dev,
3670 int planea_clock, int planeb_clock,
3671 int hdisplay, int htotal,
3672 int pixel_size)
3673{
3674 struct drm_i915_private *dev_priv = dev->dev_private;
Yuanhan Liua0fa62d2010-12-23 16:35:40 +08003675 int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
Yuanhan Liu13982612010-12-15 15:42:31 +08003676 int fbc_wm, plane_wm, cursor_wm, enabled;
3677 int clock;
3678
3679 enabled = 0;
3680 if (ironlake_compute_wm0(dev, 0,
3681 &sandybridge_display_wm_info, latency,
3682 &sandybridge_cursor_wm_info, latency,
3683 &plane_wm, &cursor_wm)) {
3684 I915_WRITE(WM0_PIPEA_ILK,
3685 (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
3686 DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
3687 " plane %d, " "cursor: %d\n",
3688 plane_wm, cursor_wm);
3689 enabled++;
3690 }
3691
3692 if (ironlake_compute_wm0(dev, 1,
3693 &sandybridge_display_wm_info, latency,
3694 &sandybridge_cursor_wm_info, latency,
3695 &plane_wm, &cursor_wm)) {
3696 I915_WRITE(WM0_PIPEB_ILK,
3697 (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
3698 DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
3699 " plane %d, cursor: %d\n",
3700 plane_wm, cursor_wm);
3701 enabled++;
3702 }
3703
3704 /*
3705 * Calculate and update the self-refresh watermark only when one
3706 * display plane is used.
3707 *
3708 * SNB support 3 levels of watermark.
3709 *
3710 * WM1/WM2/WM2 watermarks have to be enabled in the ascending order,
3711 * and disabled in the descending order
3712 *
3713 */
3714 I915_WRITE(WM3_LP_ILK, 0);
3715 I915_WRITE(WM2_LP_ILK, 0);
3716 I915_WRITE(WM1_LP_ILK, 0);
3717
3718 if (enabled != 1)
3719 return;
3720
3721 clock = planea_clock ? planea_clock : planeb_clock;
3722
3723 /* WM1 */
Jesse Barnesb79d4992010-12-21 13:10:23 -08003724 if (!ironlake_compute_srwm(dev, 1, hdisplay, htotal, pixel_size,
3725 clock, SNB_READ_WM1_LATENCY() * 500,
3726 &sandybridge_display_srwm_info,
3727 &sandybridge_cursor_srwm_info,
3728 &fbc_wm, &plane_wm, &cursor_wm))
Yuanhan Liu13982612010-12-15 15:42:31 +08003729 return;
3730
3731 I915_WRITE(WM1_LP_ILK,
3732 WM1_LP_SR_EN |
3733 (SNB_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
3734 (fbc_wm << WM1_LP_FBC_SHIFT) |
3735 (plane_wm << WM1_LP_SR_SHIFT) |
3736 cursor_wm);
3737
3738 /* WM2 */
Jesse Barnesb79d4992010-12-21 13:10:23 -08003739 if (!ironlake_compute_srwm(dev, 2,
3740 hdisplay, htotal, pixel_size,
3741 clock, SNB_READ_WM2_LATENCY() * 500,
3742 &sandybridge_display_srwm_info,
3743 &sandybridge_cursor_srwm_info,
3744 &fbc_wm, &plane_wm, &cursor_wm))
Yuanhan Liu13982612010-12-15 15:42:31 +08003745 return;
3746
3747 I915_WRITE(WM2_LP_ILK,
3748 WM2_LP_EN |
3749 (SNB_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
3750 (fbc_wm << WM1_LP_FBC_SHIFT) |
3751 (plane_wm << WM1_LP_SR_SHIFT) |
3752 cursor_wm);
3753
3754 /* WM3 */
Jesse Barnesb79d4992010-12-21 13:10:23 -08003755 if (!ironlake_compute_srwm(dev, 3,
3756 hdisplay, htotal, pixel_size,
3757 clock, SNB_READ_WM3_LATENCY() * 500,
3758 &sandybridge_display_srwm_info,
3759 &sandybridge_cursor_srwm_info,
3760 &fbc_wm, &plane_wm, &cursor_wm))
Yuanhan Liu13982612010-12-15 15:42:31 +08003761 return;
3762
3763 I915_WRITE(WM3_LP_ILK,
3764 WM3_LP_EN |
3765 (SNB_READ_WM3_LATENCY() << WM1_LP_LATENCY_SHIFT) |
3766 (fbc_wm << WM1_LP_FBC_SHIFT) |
3767 (plane_wm << WM1_LP_SR_SHIFT) |
3768 cursor_wm);
3769}
3770
Shaohua Li7662c8b2009-06-26 11:23:55 +08003771/**
3772 * intel_update_watermarks - update FIFO watermark values based on current modes
3773 *
3774 * Calculate watermark values for the various WM regs based on current mode
3775 * and plane configuration.
3776 *
3777 * There are several cases to deal with here:
3778 * - normal (i.e. non-self-refresh)
3779 * - self-refresh (SR) mode
3780 * - lines are large relative to FIFO size (buffer can hold up to 2)
3781 * - lines are small relative to FIFO size (buffer can hold more than 2
3782 * lines), so need to account for TLB latency
3783 *
3784 * The normal calculation is:
3785 * watermark = dotclock * bytes per pixel * latency
3786 * where latency is platform & configuration dependent (we assume pessimal
3787 * values here).
3788 *
3789 * The SR calculation is:
3790 * watermark = (trunc(latency/line time)+1) * surface width *
3791 * bytes per pixel
3792 * where
3793 * line time = htotal / dotclock
Zhao Yakuifa143212010-06-12 14:32:23 +08003794 * surface width = hdisplay for normal plane and 64 for cursor
Shaohua Li7662c8b2009-06-26 11:23:55 +08003795 * and latency is assumed to be high, as above.
3796 *
3797 * The final value programmed to the register should always be rounded up,
3798 * and include an extra 2 entries to account for clock crossings.
3799 *
3800 * We don't use the sprite, so we can ignore that. And on Crestline we have
3801 * to set the non-SR watermarks to 8.
Chris Wilson5eddb702010-09-11 13:48:45 +01003802 */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003803static void intel_update_watermarks(struct drm_device *dev)
3804{
Jesse Barnese70236a2009-09-21 10:42:27 -07003805 struct drm_i915_private *dev_priv = dev->dev_private;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003806 struct drm_crtc *crtc;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003807 int sr_hdisplay = 0;
3808 unsigned long planea_clock = 0, planeb_clock = 0, sr_clock = 0;
3809 int enabled = 0, pixel_size = 0;
Zhao Yakuifa143212010-06-12 14:32:23 +08003810 int sr_htotal = 0;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003811
Zhenyu Wangc03342f2009-09-29 11:01:23 +08003812 if (!dev_priv->display.update_wm)
3813 return;
3814
Shaohua Li7662c8b2009-06-26 11:23:55 +08003815 /* Get the clock config from both planes */
3816 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Chris Wilsondebcadd2010-08-07 11:01:33 +01003817 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003818 if (intel_crtc->active) {
Shaohua Li7662c8b2009-06-26 11:23:55 +08003819 enabled++;
3820 if (intel_crtc->plane == 0) {
Zhao Yakui28c97732009-10-09 11:39:41 +08003821 DRM_DEBUG_KMS("plane A (pipe %d) clock: %d\n",
Chris Wilson5eddb702010-09-11 13:48:45 +01003822 intel_crtc->pipe, crtc->mode.clock);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003823 planea_clock = crtc->mode.clock;
3824 } else {
Zhao Yakui28c97732009-10-09 11:39:41 +08003825 DRM_DEBUG_KMS("plane B (pipe %d) clock: %d\n",
Chris Wilson5eddb702010-09-11 13:48:45 +01003826 intel_crtc->pipe, crtc->mode.clock);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003827 planeb_clock = crtc->mode.clock;
3828 }
3829 sr_hdisplay = crtc->mode.hdisplay;
3830 sr_clock = crtc->mode.clock;
Zhao Yakuifa143212010-06-12 14:32:23 +08003831 sr_htotal = crtc->mode.htotal;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003832 if (crtc->fb)
3833 pixel_size = crtc->fb->bits_per_pixel / 8;
3834 else
3835 pixel_size = 4; /* by default */
3836 }
3837 }
3838
3839 if (enabled <= 0)
3840 return;
3841
Jesse Barnese70236a2009-09-21 10:42:27 -07003842 dev_priv->display.update_wm(dev, planea_clock, planeb_clock,
Zhao Yakuifa143212010-06-12 14:32:23 +08003843 sr_hdisplay, sr_htotal, pixel_size);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003844}
3845
Chris Wilsona7615032011-01-12 17:04:08 +00003846static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
3847{
3848 return dev_priv->lvds_use_ssc && i915_panel_use_ssc;
3849}
3850
Chris Wilson5c3b82e2009-02-11 13:25:09 +00003851static int intel_crtc_mode_set(struct drm_crtc *crtc,
3852 struct drm_display_mode *mode,
3853 struct drm_display_mode *adjusted_mode,
3854 int x, int y,
3855 struct drm_framebuffer *old_fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08003856{
3857 struct drm_device *dev = crtc->dev;
3858 struct drm_i915_private *dev_priv = dev->dev_private;
3859 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3860 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07003861 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01003862 u32 fp_reg, dpll_reg;
Eric Anholtc751ce42010-03-25 11:48:48 -07003863 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07003864 intel_clock_t clock, reduced_clock;
Chris Wilson5eddb702010-09-11 13:48:45 +01003865 u32 dpll, fp = 0, fp2 = 0, dspcntr, pipeconf;
Jesse Barnes652c3932009-08-17 13:31:43 -07003866 bool ok, has_reduced_clock = false, is_sdvo = false, is_dvo = false;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003867 bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
Chris Wilson8e647a22010-08-22 10:54:23 +01003868 struct intel_encoder *has_edp_encoder = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08003869 struct drm_mode_config *mode_config = &dev->mode_config;
Chris Wilson5eddb702010-09-11 13:48:45 +01003870 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08003871 const intel_limit_t *limit;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00003872 int ret;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003873 struct fdi_m_n m_n = {0};
Chris Wilson5eddb702010-09-11 13:48:45 +01003874 u32 reg, temp;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08003875 int target_clock;
Jesse Barnes79e53942008-11-07 14:24:08 -08003876
3877 drm_vblank_pre_modeset(dev, pipe);
3878
Chris Wilson5eddb702010-09-11 13:48:45 +01003879 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
3880 if (encoder->base.crtc != crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003881 continue;
3882
Chris Wilson5eddb702010-09-11 13:48:45 +01003883 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003884 case INTEL_OUTPUT_LVDS:
3885 is_lvds = true;
3886 break;
3887 case INTEL_OUTPUT_SDVO:
Eric Anholt7d573822009-01-02 13:33:00 -08003888 case INTEL_OUTPUT_HDMI:
Jesse Barnes79e53942008-11-07 14:24:08 -08003889 is_sdvo = true;
Chris Wilson5eddb702010-09-11 13:48:45 +01003890 if (encoder->needs_tv_clock)
Jesse Barnese2f0ba92009-02-02 15:11:52 -08003891 is_tv = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08003892 break;
3893 case INTEL_OUTPUT_DVO:
3894 is_dvo = true;
3895 break;
3896 case INTEL_OUTPUT_TVOUT:
3897 is_tv = true;
3898 break;
3899 case INTEL_OUTPUT_ANALOG:
3900 is_crt = true;
3901 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003902 case INTEL_OUTPUT_DISPLAYPORT:
3903 is_dp = true;
3904 break;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08003905 case INTEL_OUTPUT_EDP:
Chris Wilson5eddb702010-09-11 13:48:45 +01003906 has_edp_encoder = encoder;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08003907 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08003908 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05003909
Eric Anholtc751ce42010-03-25 11:48:48 -07003910 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08003911 }
3912
Chris Wilsona7615032011-01-12 17:04:08 +00003913 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Kristian Høgsberg43565a02009-02-13 20:56:52 -05003914 refclk = dev_priv->lvds_ssc_freq * 1000;
Zhao Yakui28c97732009-10-09 11:39:41 +08003915 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
Chris Wilson5eddb702010-09-11 13:48:45 +01003916 refclk / 1000);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003917 } else if (!IS_GEN2(dev)) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003918 refclk = 96000;
Jesse Barnes1cb1b752010-10-07 16:01:17 -07003919 if (HAS_PCH_SPLIT(dev) &&
3920 (!has_edp_encoder || intel_encoder_is_pch_edp(&has_edp_encoder->base)))
Zhenyu Wang2c072452009-06-05 15:38:42 +08003921 refclk = 120000; /* 120Mhz refclk */
Jesse Barnes79e53942008-11-07 14:24:08 -08003922 } else {
3923 refclk = 48000;
3924 }
3925
Ma Lingd4906092009-03-18 20:13:27 +08003926 /*
3927 * Returns a set of divisors for the desired target clock with the given
3928 * refclk, or FALSE. The returned values represent the clock equation:
3929 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
3930 */
Chris Wilson1b894b52010-12-14 20:04:54 +00003931 limit = intel_limit(crtc, refclk);
Ma Lingd4906092009-03-18 20:13:27 +08003932 ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08003933 if (!ok) {
3934 DRM_ERROR("Couldn't find PLL settings for mode!\n");
Chris Wilson1f803ee2009-06-06 09:45:59 +01003935 drm_vblank_post_modeset(dev, pipe);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00003936 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08003937 }
3938
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01003939 /* Ensure that the cursor is valid for the new mode before changing... */
Chris Wilson6b383a72010-09-13 13:54:26 +01003940 intel_crtc_update_cursor(crtc, true);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01003941
Zhao Yakuiddc90032010-01-06 22:05:56 +08003942 if (is_lvds && dev_priv->lvds_downclock_avail) {
3943 has_reduced_clock = limit->find_pll(limit, crtc,
Chris Wilson5eddb702010-09-11 13:48:45 +01003944 dev_priv->lvds_downclock,
3945 refclk,
3946 &reduced_clock);
Zhao Yakui18f9ed12009-11-20 03:24:16 +00003947 if (has_reduced_clock && (clock.p != reduced_clock.p)) {
3948 /*
3949 * If the different P is found, it means that we can't
3950 * switch the display clock by using the FP0/FP1.
3951 * In such case we will disable the LVDS downclock
3952 * feature.
3953 */
3954 DRM_DEBUG_KMS("Different P is found for "
Chris Wilson5eddb702010-09-11 13:48:45 +01003955 "LVDS clock/downclock\n");
Zhao Yakui18f9ed12009-11-20 03:24:16 +00003956 has_reduced_clock = 0;
3957 }
Jesse Barnes652c3932009-08-17 13:31:43 -07003958 }
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08003959 /* SDVO TV has fixed PLL values depend on its clock range,
3960 this mirrors vbios setting. */
3961 if (is_sdvo && is_tv) {
3962 if (adjusted_mode->clock >= 100000
Chris Wilson5eddb702010-09-11 13:48:45 +01003963 && adjusted_mode->clock < 140500) {
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08003964 clock.p1 = 2;
3965 clock.p2 = 10;
3966 clock.n = 3;
3967 clock.m1 = 16;
3968 clock.m2 = 8;
3969 } else if (adjusted_mode->clock >= 140500
Chris Wilson5eddb702010-09-11 13:48:45 +01003970 && adjusted_mode->clock <= 200000) {
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08003971 clock.p1 = 1;
3972 clock.p2 = 10;
3973 clock.n = 6;
3974 clock.m1 = 12;
3975 clock.m2 = 8;
3976 }
3977 }
3978
Zhenyu Wang2c072452009-06-05 15:38:42 +08003979 /* FDI link */
Eric Anholtbad720f2009-10-22 16:11:14 -07003980 if (HAS_PCH_SPLIT(dev)) {
Chris Wilson49078f72010-12-04 07:45:57 +00003981 int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
Adam Jackson77ffb592010-04-12 11:38:44 -04003982 int lane = 0, link_bw, bpp;
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003983 /* CPU eDP doesn't require FDI link, so just set DP M/N
Zhenyu Wang32f9d652009-07-24 01:00:32 +08003984 according to current link config */
Jesse Barnes858bc212011-01-04 10:46:49 -08003985 if (has_edp_encoder && !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08003986 target_clock = mode->clock;
Chris Wilson8e647a22010-08-22 10:54:23 +01003987 intel_edp_link_config(has_edp_encoder,
3988 &lane, &link_bw);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08003989 } else {
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003990 /* [e]DP over FDI requires target mode clock
Zhenyu Wang32f9d652009-07-24 01:00:32 +08003991 instead of link clock */
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003992 if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
Zhenyu Wang32f9d652009-07-24 01:00:32 +08003993 target_clock = mode->clock;
3994 else
3995 target_clock = adjusted_mode->clock;
Chris Wilson021357a2010-09-07 20:54:59 +01003996
3997 /* FDI is a binary signal running at ~2.7GHz, encoding
3998 * each output octet as 10 bits. The actual frequency
3999 * is stored as a divider into a 100MHz clock, and the
4000 * mode pixel clock is stored in units of 1KHz.
4001 * Hence the bw of each lane in terms of the mode signal
4002 * is:
4003 */
4004 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004005 }
Zhenyu Wang58a27472009-09-25 08:01:28 +00004006
4007 /* determine panel color depth */
Chris Wilson5eddb702010-09-11 13:48:45 +01004008 temp = I915_READ(PIPECONF(pipe));
Zhao Yakuie5a95eb2010-01-04 16:29:32 +08004009 temp &= ~PIPE_BPC_MASK;
4010 if (is_lvds) {
Zhao Yakuie5a95eb2010-01-04 16:29:32 +08004011 /* the BPC will be 6 if it is 18-bit LVDS panel */
Chris Wilson5eddb702010-09-11 13:48:45 +01004012 if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) == LVDS_A3_POWER_UP)
Zhao Yakuie5a95eb2010-01-04 16:29:32 +08004013 temp |= PIPE_8BPC;
4014 else
4015 temp |= PIPE_6BPC;
Jesse Barnes1d850362010-10-07 16:01:10 -07004016 } else if (has_edp_encoder) {
Chris Wilson5ceb0f92010-09-24 10:24:28 +01004017 switch (dev_priv->edp.bpp/3) {
Zhenyu Wang885a5fb2010-01-12 05:38:31 +08004018 case 8:
4019 temp |= PIPE_8BPC;
4020 break;
4021 case 10:
4022 temp |= PIPE_10BPC;
4023 break;
4024 case 6:
4025 temp |= PIPE_6BPC;
4026 break;
4027 case 12:
4028 temp |= PIPE_12BPC;
4029 break;
4030 }
Zhao Yakuie5a95eb2010-01-04 16:29:32 +08004031 } else
4032 temp |= PIPE_8BPC;
Chris Wilson5eddb702010-09-11 13:48:45 +01004033 I915_WRITE(PIPECONF(pipe), temp);
Zhenyu Wang58a27472009-09-25 08:01:28 +00004034
4035 switch (temp & PIPE_BPC_MASK) {
4036 case PIPE_8BPC:
4037 bpp = 24;
4038 break;
4039 case PIPE_10BPC:
4040 bpp = 30;
4041 break;
4042 case PIPE_6BPC:
4043 bpp = 18;
4044 break;
4045 case PIPE_12BPC:
4046 bpp = 36;
4047 break;
4048 default:
4049 DRM_ERROR("unknown pipe bpc value\n");
4050 bpp = 24;
4051 }
4052
Adam Jackson77ffb592010-04-12 11:38:44 -04004053 if (!lane) {
4054 /*
4055 * Account for spread spectrum to avoid
4056 * oversubscribing the link. Max center spread
4057 * is 2.5%; use 5% for safety's sake.
4058 */
4059 u32 bps = target_clock * bpp * 21 / 20;
4060 lane = bps / (link_bw * 8) + 1;
4061 }
4062
4063 intel_crtc->fdi_lanes = lane;
4064
Chris Wilson49078f72010-12-04 07:45:57 +00004065 if (pixel_multiplier > 1)
4066 link_bw *= pixel_multiplier;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05004067 ironlake_compute_m_n(bpp, lane, target_clock, link_bw, &m_n);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08004068 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08004069
Zhenyu Wangc038e512009-10-19 15:43:48 +08004070 /* Ironlake: try to setup display ref clock before DPLL
4071 * enabling. This is only under driver's control after
4072 * PCH B stepping, previous chipset stepping should be
4073 * ignoring this setting.
4074 */
Eric Anholtbad720f2009-10-22 16:11:14 -07004075 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wangc038e512009-10-19 15:43:48 +08004076 temp = I915_READ(PCH_DREF_CONTROL);
4077 /* Always enable nonspread source */
4078 temp &= ~DREF_NONSPREAD_SOURCE_MASK;
4079 temp |= DREF_NONSPREAD_SOURCE_ENABLE;
Zhenyu Wangc038e512009-10-19 15:43:48 +08004080 temp &= ~DREF_SSC_SOURCE_MASK;
4081 temp |= DREF_SSC_SOURCE_ENABLE;
4082 I915_WRITE(PCH_DREF_CONTROL, temp);
Zhenyu Wangc038e512009-10-19 15:43:48 +08004083
Chris Wilson5eddb702010-09-11 13:48:45 +01004084 POSTING_READ(PCH_DREF_CONTROL);
Zhenyu Wangc038e512009-10-19 15:43:48 +08004085 udelay(200);
4086
Chris Wilson8e647a22010-08-22 10:54:23 +01004087 if (has_edp_encoder) {
Chris Wilsona7615032011-01-12 17:04:08 +00004088 if (intel_panel_use_ssc(dev_priv)) {
Zhenyu Wangc038e512009-10-19 15:43:48 +08004089 temp |= DREF_SSC1_ENABLE;
4090 I915_WRITE(PCH_DREF_CONTROL, temp);
Zhenyu Wangc038e512009-10-19 15:43:48 +08004091
Chris Wilson5eddb702010-09-11 13:48:45 +01004092 POSTING_READ(PCH_DREF_CONTROL);
Zhenyu Wangc038e512009-10-19 15:43:48 +08004093 udelay(200);
Jesse Barnes7f823282010-10-07 16:01:16 -07004094 }
4095 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Zhenyu Wangc038e512009-10-19 15:43:48 +08004096
Jesse Barnes7f823282010-10-07 16:01:16 -07004097 /* Enable CPU source on CPU attached eDP */
4098 if (!intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
Chris Wilsona7615032011-01-12 17:04:08 +00004099 if (intel_panel_use_ssc(dev_priv))
Jesse Barnes7f823282010-10-07 16:01:16 -07004100 temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
4101 else
4102 temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Zhenyu Wangc038e512009-10-19 15:43:48 +08004103 } else {
Jesse Barnes7f823282010-10-07 16:01:16 -07004104 /* Enable SSC on PCH eDP if needed */
Chris Wilsona7615032011-01-12 17:04:08 +00004105 if (intel_panel_use_ssc(dev_priv)) {
Jesse Barnes7f823282010-10-07 16:01:16 -07004106 DRM_ERROR("enabling SSC on PCH\n");
4107 temp |= DREF_SUPERSPREAD_SOURCE_ENABLE;
4108 }
Zhenyu Wangc038e512009-10-19 15:43:48 +08004109 }
Chris Wilson5eddb702010-09-11 13:48:45 +01004110 I915_WRITE(PCH_DREF_CONTROL, temp);
Jesse Barnes7f823282010-10-07 16:01:16 -07004111 POSTING_READ(PCH_DREF_CONTROL);
4112 udelay(200);
Zhenyu Wangc038e512009-10-19 15:43:48 +08004113 }
4114 }
4115
Adam Jacksonf2b115e2009-12-03 17:14:42 -05004116 if (IS_PINEVIEW(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +08004117 fp = (1 << clock.n) << 16 | clock.m1 << 8 | clock.m2;
Jesse Barnes652c3932009-08-17 13:31:43 -07004118 if (has_reduced_clock)
4119 fp2 = (1 << reduced_clock.n) << 16 |
4120 reduced_clock.m1 << 8 | reduced_clock.m2;
4121 } else {
Shaohua Li21778322009-02-23 15:19:16 +08004122 fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
Jesse Barnes652c3932009-08-17 13:31:43 -07004123 if (has_reduced_clock)
4124 fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
4125 reduced_clock.m2;
4126 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004127
Chris Wilsonc1858122010-12-03 21:35:48 +00004128 /* Enable autotuning of the PLL clock (if permissible) */
4129 if (HAS_PCH_SPLIT(dev)) {
4130 int factor = 21;
4131
4132 if (is_lvds) {
Chris Wilsona7615032011-01-12 17:04:08 +00004133 if ((intel_panel_use_ssc(dev_priv) &&
Chris Wilsonc1858122010-12-03 21:35:48 +00004134 dev_priv->lvds_ssc_freq == 100) ||
4135 (I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP)
4136 factor = 25;
4137 } else if (is_sdvo && is_tv)
4138 factor = 20;
4139
4140 if (clock.m1 < factor * clock.n)
4141 fp |= FP_CB_TUNE;
4142 }
4143
Chris Wilson5eddb702010-09-11 13:48:45 +01004144 dpll = 0;
Eric Anholtbad720f2009-10-22 16:11:14 -07004145 if (!HAS_PCH_SPLIT(dev))
Zhenyu Wang2c072452009-06-05 15:38:42 +08004146 dpll = DPLL_VGA_MODE_DIS;
4147
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004148 if (!IS_GEN2(dev)) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004149 if (is_lvds)
4150 dpll |= DPLLB_MODE_LVDS;
4151 else
4152 dpll |= DPLLB_MODE_DAC_SERIAL;
4153 if (is_sdvo) {
Chris Wilson6c9547f2010-08-25 10:05:17 +01004154 int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
4155 if (pixel_multiplier > 1) {
4156 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4157 dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
4158 else if (HAS_PCH_SPLIT(dev))
4159 dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
4160 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004161 dpll |= DPLL_DVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08004162 }
Jesse Barnes83240122010-10-07 16:01:18 -07004163 if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004164 dpll |= DPLL_DVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08004165
4166 /* compute bitmask from p1 value */
Adam Jacksonf2b115e2009-12-03 17:14:42 -05004167 if (IS_PINEVIEW(dev))
4168 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004169 else {
Shaohua Li21778322009-02-23 15:19:16 +08004170 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004171 /* also FPA1 */
Eric Anholtbad720f2009-10-22 16:11:14 -07004172 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang2c072452009-06-05 15:38:42 +08004173 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Jesse Barnes652c3932009-08-17 13:31:43 -07004174 if (IS_G4X(dev) && has_reduced_clock)
4175 dpll |= (1 << (reduced_clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004176 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004177 switch (clock.p2) {
4178 case 5:
4179 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4180 break;
4181 case 7:
4182 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4183 break;
4184 case 10:
4185 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4186 break;
4187 case 14:
4188 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4189 break;
4190 }
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004191 if (INTEL_INFO(dev)->gen >= 4 && !HAS_PCH_SPLIT(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08004192 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
4193 } else {
4194 if (is_lvds) {
4195 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4196 } else {
4197 if (clock.p1 == 2)
4198 dpll |= PLL_P1_DIVIDE_BY_TWO;
4199 else
4200 dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4201 if (clock.p2 == 4)
4202 dpll |= PLL_P2_DIVIDE_BY_4;
4203 }
4204 }
4205
Kristian Høgsberg43565a02009-02-13 20:56:52 -05004206 if (is_sdvo && is_tv)
4207 dpll |= PLL_REF_INPUT_TVCLKINBC;
4208 else if (is_tv)
Jesse Barnes79e53942008-11-07 14:24:08 -08004209 /* XXX: just matching BIOS for now */
Kristian Høgsberg43565a02009-02-13 20:56:52 -05004210 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
Jesse Barnes79e53942008-11-07 14:24:08 -08004211 dpll |= 3;
Chris Wilsona7615032011-01-12 17:04:08 +00004212 else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05004213 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08004214 else
4215 dpll |= PLL_REF_INPUT_DREFCLK;
4216
4217 /* setup pipeconf */
Chris Wilson5eddb702010-09-11 13:48:45 +01004218 pipeconf = I915_READ(PIPECONF(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08004219
4220 /* Set up the display plane register */
4221 dspcntr = DISPPLANE_GAMMA_ENABLE;
4222
Adam Jacksonf2b115e2009-12-03 17:14:42 -05004223 /* Ironlake's plane is forced to pipe, bit 24 is to
Zhenyu Wang2c072452009-06-05 15:38:42 +08004224 enable color space conversion */
Eric Anholtbad720f2009-10-22 16:11:14 -07004225 if (!HAS_PCH_SPLIT(dev)) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08004226 if (pipe == 0)
Jesse Barnes80824002009-09-10 15:28:06 -07004227 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004228 else
4229 dspcntr |= DISPPLANE_SEL_PIPE_B;
4230 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004231
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004232 if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004233 /* Enable pixel doubling when the dot clock is > 90% of the (display)
4234 * core speed.
4235 *
4236 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
4237 * pipe == 0 check?
4238 */
Jesse Barnese70236a2009-09-21 10:42:27 -07004239 if (mode->clock >
4240 dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
Chris Wilson5eddb702010-09-11 13:48:45 +01004241 pipeconf |= PIPECONF_DOUBLE_WIDE;
Jesse Barnes79e53942008-11-07 14:24:08 -08004242 else
Chris Wilson5eddb702010-09-11 13:48:45 +01004243 pipeconf &= ~PIPECONF_DOUBLE_WIDE;
Jesse Barnes79e53942008-11-07 14:24:08 -08004244 }
4245
Linus Torvalds8d86dc62010-06-08 20:16:28 -07004246 dspcntr |= DISPLAY_PLANE_ENABLE;
Chris Wilson5eddb702010-09-11 13:48:45 +01004247 pipeconf |= PIPECONF_ENABLE;
Linus Torvalds8d86dc62010-06-08 20:16:28 -07004248 dpll |= DPLL_VCO_ENABLE;
4249
Zhao Yakui28c97732009-10-09 11:39:41 +08004250 DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
Jesse Barnes79e53942008-11-07 14:24:08 -08004251 drm_mode_debug_printmodeline(mode);
4252
Adam Jacksonf2b115e2009-12-03 17:14:42 -05004253 /* assign to Ironlake registers */
Eric Anholtbad720f2009-10-22 16:11:14 -07004254 if (HAS_PCH_SPLIT(dev)) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004255 fp_reg = PCH_FP0(pipe);
4256 dpll_reg = PCH_DPLL(pipe);
4257 } else {
4258 fp_reg = FP0(pipe);
4259 dpll_reg = DPLL(pipe);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004260 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004261
Jesse Barnes5c5313c2010-10-07 16:01:11 -07004262 /* PCH eDP needs FDI, but CPU eDP does not */
4263 if (!has_edp_encoder || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004264 I915_WRITE(fp_reg, fp);
4265 I915_WRITE(dpll_reg, dpll & ~DPLL_VCO_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01004266
4267 POSTING_READ(dpll_reg);
Jesse Barnes79e53942008-11-07 14:24:08 -08004268 udelay(150);
4269 }
4270
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004271 /* enable transcoder DPLL */
4272 if (HAS_PCH_CPT(dev)) {
4273 temp = I915_READ(PCH_DPLL_SEL);
Chris Wilson5eddb702010-09-11 13:48:45 +01004274 if (pipe == 0)
4275 temp |= TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004276 else
Chris Wilson5eddb702010-09-11 13:48:45 +01004277 temp |= TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004278 I915_WRITE(PCH_DPLL_SEL, temp);
Chris Wilson5eddb702010-09-11 13:48:45 +01004279
4280 POSTING_READ(PCH_DPLL_SEL);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004281 udelay(150);
4282 }
4283
Jesse Barnes79e53942008-11-07 14:24:08 -08004284 /* The LVDS pin pair needs to be on before the DPLLs are enabled.
4285 * This is an exception to the general rule that mode_set doesn't turn
4286 * things on.
4287 */
4288 if (is_lvds) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004289 reg = LVDS;
Eric Anholtbad720f2009-10-22 16:11:14 -07004290 if (HAS_PCH_SPLIT(dev))
Chris Wilson5eddb702010-09-11 13:48:45 +01004291 reg = PCH_LVDS;
Zhenyu Wang541998a2009-06-05 15:38:44 +08004292
Chris Wilson5eddb702010-09-11 13:48:45 +01004293 temp = I915_READ(reg);
4294 temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
Zhenyu Wangb3b095b2010-04-07 16:15:56 +08004295 if (pipe == 1) {
4296 if (HAS_PCH_CPT(dev))
Chris Wilson5eddb702010-09-11 13:48:45 +01004297 temp |= PORT_TRANS_B_SEL_CPT;
Zhenyu Wangb3b095b2010-04-07 16:15:56 +08004298 else
Chris Wilson5eddb702010-09-11 13:48:45 +01004299 temp |= LVDS_PIPEB_SELECT;
Zhenyu Wangb3b095b2010-04-07 16:15:56 +08004300 } else {
4301 if (HAS_PCH_CPT(dev))
Chris Wilson5eddb702010-09-11 13:48:45 +01004302 temp &= ~PORT_TRANS_SEL_MASK;
Zhenyu Wangb3b095b2010-04-07 16:15:56 +08004303 else
Chris Wilson5eddb702010-09-11 13:48:45 +01004304 temp &= ~LVDS_PIPEB_SELECT;
Zhenyu Wangb3b095b2010-04-07 16:15:56 +08004305 }
Zhao Yakuia3e17eb2009-10-10 10:42:37 +08004306 /* set the corresponsding LVDS_BORDER bit */
Chris Wilson5eddb702010-09-11 13:48:45 +01004307 temp |= dev_priv->lvds_border_bits;
Jesse Barnes79e53942008-11-07 14:24:08 -08004308 /* Set the B0-B3 data pairs corresponding to whether we're going to
4309 * set the DPLLs for dual-channel mode or not.
4310 */
4311 if (clock.p2 == 7)
Chris Wilson5eddb702010-09-11 13:48:45 +01004312 temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
Jesse Barnes79e53942008-11-07 14:24:08 -08004313 else
Chris Wilson5eddb702010-09-11 13:48:45 +01004314 temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
Jesse Barnes79e53942008-11-07 14:24:08 -08004315
4316 /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
4317 * appropriately here, but we need to look more thoroughly into how
4318 * panels behave in the two modes.
4319 */
Jesse Barnes434ed092010-09-07 14:48:06 -07004320 /* set the dithering flag on non-PCH LVDS as needed */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004321 if (INTEL_INFO(dev)->gen >= 4 && !HAS_PCH_SPLIT(dev)) {
Jesse Barnes434ed092010-09-07 14:48:06 -07004322 if (dev_priv->lvds_dither)
Chris Wilson5eddb702010-09-11 13:48:45 +01004323 temp |= LVDS_ENABLE_DITHER;
Jesse Barnes434ed092010-09-07 14:48:06 -07004324 else
Chris Wilson5eddb702010-09-11 13:48:45 +01004325 temp &= ~LVDS_ENABLE_DITHER;
Zhao Yakui898822c2010-01-04 16:29:30 +08004326 }
Chris Wilson5eddb702010-09-11 13:48:45 +01004327 I915_WRITE(reg, temp);
Jesse Barnes79e53942008-11-07 14:24:08 -08004328 }
Jesse Barnes434ed092010-09-07 14:48:06 -07004329
4330 /* set the dithering flag and clear for anything other than a panel. */
4331 if (HAS_PCH_SPLIT(dev)) {
4332 pipeconf &= ~PIPECONF_DITHER_EN;
4333 pipeconf &= ~PIPECONF_DITHER_TYPE_MASK;
4334 if (dev_priv->lvds_dither && (is_lvds || has_edp_encoder)) {
4335 pipeconf |= PIPECONF_DITHER_EN;
4336 pipeconf |= PIPECONF_DITHER_TYPE_ST1;
4337 }
4338 }
4339
Jesse Barnes5c5313c2010-10-07 16:01:11 -07004340 if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004341 intel_dp_set_m_n(crtc, mode, adjusted_mode);
Jesse Barnes5c5313c2010-10-07 16:01:11 -07004342 } else if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004343 /* For non-DP output, clear any trans DP clock recovery setting.*/
4344 if (pipe == 0) {
4345 I915_WRITE(TRANSA_DATA_M1, 0);
4346 I915_WRITE(TRANSA_DATA_N1, 0);
4347 I915_WRITE(TRANSA_DP_LINK_M1, 0);
4348 I915_WRITE(TRANSA_DP_LINK_N1, 0);
4349 } else {
4350 I915_WRITE(TRANSB_DATA_M1, 0);
4351 I915_WRITE(TRANSB_DATA_N1, 0);
4352 I915_WRITE(TRANSB_DP_LINK_M1, 0);
4353 I915_WRITE(TRANSB_DP_LINK_N1, 0);
4354 }
4355 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004356
Jesse Barnes5c5313c2010-10-07 16:01:11 -07004357 if (!has_edp_encoder || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004358 I915_WRITE(dpll_reg, dpll);
Chris Wilson5eddb702010-09-11 13:48:45 +01004359
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004360 /* Wait for the clocks to stabilize. */
Chris Wilson5eddb702010-09-11 13:48:45 +01004361 POSTING_READ(dpll_reg);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004362 udelay(150);
4363
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004364 if (INTEL_INFO(dev)->gen >= 4 && !HAS_PCH_SPLIT(dev)) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004365 temp = 0;
Zhao Yakuibb66c512009-09-10 15:45:49 +08004366 if (is_sdvo) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004367 temp = intel_mode_get_pixel_multiplier(adjusted_mode);
4368 if (temp > 1)
4369 temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Chris Wilson6c9547f2010-08-25 10:05:17 +01004370 else
Chris Wilson5eddb702010-09-11 13:48:45 +01004371 temp = 0;
4372 }
4373 I915_WRITE(DPLL_MD(pipe), temp);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004374 } else {
Chris Wilsona589b9f2010-12-03 21:13:16 +00004375 /* The pixel multiplier can only be updated once the
4376 * DPLL is enabled and the clocks are stable.
4377 *
4378 * So write it again.
4379 */
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004380 I915_WRITE(dpll_reg, dpll);
4381 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004382 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004383
Chris Wilson5eddb702010-09-11 13:48:45 +01004384 intel_crtc->lowfreq_avail = false;
Jesse Barnes652c3932009-08-17 13:31:43 -07004385 if (is_lvds && has_reduced_clock && i915_powersave) {
4386 I915_WRITE(fp_reg + 4, fp2);
4387 intel_crtc->lowfreq_avail = true;
4388 if (HAS_PIPE_CXSR(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08004389 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07004390 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
4391 }
4392 } else {
4393 I915_WRITE(fp_reg + 4, fp);
Jesse Barnes652c3932009-08-17 13:31:43 -07004394 if (HAS_PIPE_CXSR(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08004395 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07004396 pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
4397 }
4398 }
4399
Krzysztof Halasa734b4152010-05-25 18:41:46 +02004400 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4401 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
4402 /* the chip adds 2 halflines automatically */
4403 adjusted_mode->crtc_vdisplay -= 1;
4404 adjusted_mode->crtc_vtotal -= 1;
4405 adjusted_mode->crtc_vblank_start -= 1;
4406 adjusted_mode->crtc_vblank_end -= 1;
4407 adjusted_mode->crtc_vsync_end -= 1;
4408 adjusted_mode->crtc_vsync_start -= 1;
4409 } else
4410 pipeconf &= ~PIPECONF_INTERLACE_W_FIELD_INDICATION; /* progressive */
4411
Chris Wilson5eddb702010-09-11 13:48:45 +01004412 I915_WRITE(HTOTAL(pipe),
4413 (adjusted_mode->crtc_hdisplay - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08004414 ((adjusted_mode->crtc_htotal - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01004415 I915_WRITE(HBLANK(pipe),
4416 (adjusted_mode->crtc_hblank_start - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08004417 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01004418 I915_WRITE(HSYNC(pipe),
4419 (adjusted_mode->crtc_hsync_start - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08004420 ((adjusted_mode->crtc_hsync_end - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01004421
4422 I915_WRITE(VTOTAL(pipe),
4423 (adjusted_mode->crtc_vdisplay - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08004424 ((adjusted_mode->crtc_vtotal - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01004425 I915_WRITE(VBLANK(pipe),
4426 (adjusted_mode->crtc_vblank_start - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08004427 ((adjusted_mode->crtc_vblank_end - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01004428 I915_WRITE(VSYNC(pipe),
4429 (adjusted_mode->crtc_vsync_start - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08004430 ((adjusted_mode->crtc_vsync_end - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01004431
4432 /* pipesrc and dspsize control the size that is scaled from,
4433 * which should always be the user's requested size.
Jesse Barnes79e53942008-11-07 14:24:08 -08004434 */
Eric Anholtbad720f2009-10-22 16:11:14 -07004435 if (!HAS_PCH_SPLIT(dev)) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004436 I915_WRITE(DSPSIZE(plane),
4437 ((mode->vdisplay - 1) << 16) |
4438 (mode->hdisplay - 1));
4439 I915_WRITE(DSPPOS(plane), 0);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004440 }
Chris Wilson5eddb702010-09-11 13:48:45 +01004441 I915_WRITE(PIPESRC(pipe),
4442 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
Zhenyu Wang2c072452009-06-05 15:38:42 +08004443
Eric Anholtbad720f2009-10-22 16:11:14 -07004444 if (HAS_PCH_SPLIT(dev)) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004445 I915_WRITE(PIPE_DATA_M1(pipe), TU_SIZE(m_n.tu) | m_n.gmch_m);
4446 I915_WRITE(PIPE_DATA_N1(pipe), m_n.gmch_n);
4447 I915_WRITE(PIPE_LINK_M1(pipe), m_n.link_m);
4448 I915_WRITE(PIPE_LINK_N1(pipe), m_n.link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004449
Jesse Barnes5c5313c2010-10-07 16:01:11 -07004450 if (has_edp_encoder && !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05004451 ironlake_set_pll_edp(crtc, adjusted_mode->clock);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004452 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08004453 }
4454
Chris Wilson5eddb702010-09-11 13:48:45 +01004455 I915_WRITE(PIPECONF(pipe), pipeconf);
4456 POSTING_READ(PIPECONF(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08004457
Jesse Barnes9d0498a2010-08-18 13:20:54 -07004458 intel_wait_for_vblank(dev, pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08004459
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01004460 if (IS_GEN5(dev)) {
Zhenyu Wang553bd142009-09-02 10:57:52 +08004461 /* enable address swizzle for tiling buffer */
4462 temp = I915_READ(DISP_ARB_CTL);
4463 I915_WRITE(DISP_ARB_CTL, temp | DISP_TILE_SURFACE_SWIZZLING);
4464 }
4465
Chris Wilson5eddb702010-09-11 13:48:45 +01004466 I915_WRITE(DSPCNTR(plane), dspcntr);
Jesse Barnes79e53942008-11-07 14:24:08 -08004467
Chris Wilson5c3b82e2009-02-11 13:25:09 +00004468 ret = intel_pipe_set_base(crtc, x, y, old_fb);
Shaohua Li7662c8b2009-06-26 11:23:55 +08004469
4470 intel_update_watermarks(dev);
4471
Jesse Barnes79e53942008-11-07 14:24:08 -08004472 drm_vblank_post_modeset(dev, pipe);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00004473
Chris Wilson1f803ee2009-06-06 09:45:59 +01004474 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08004475}
4476
4477/** Loads the palette/gamma unit for the CRTC with the prepared values */
4478void intel_crtc_load_lut(struct drm_crtc *crtc)
4479{
4480 struct drm_device *dev = crtc->dev;
4481 struct drm_i915_private *dev_priv = dev->dev_private;
4482 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4483 int palreg = (intel_crtc->pipe == 0) ? PALETTE_A : PALETTE_B;
4484 int i;
4485
4486 /* The clocks have to be on to load the palette. */
4487 if (!crtc->enabled)
4488 return;
4489
Adam Jacksonf2b115e2009-12-03 17:14:42 -05004490 /* use legacy palette for Ironlake */
Eric Anholtbad720f2009-10-22 16:11:14 -07004491 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang2c072452009-06-05 15:38:42 +08004492 palreg = (intel_crtc->pipe == 0) ? LGC_PALETTE_A :
4493 LGC_PALETTE_B;
4494
Jesse Barnes79e53942008-11-07 14:24:08 -08004495 for (i = 0; i < 256; i++) {
4496 I915_WRITE(palreg + 4 * i,
4497 (intel_crtc->lut_r[i] << 16) |
4498 (intel_crtc->lut_g[i] << 8) |
4499 intel_crtc->lut_b[i]);
4500 }
4501}
4502
Chris Wilson560b85b2010-08-07 11:01:38 +01004503static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
4504{
4505 struct drm_device *dev = crtc->dev;
4506 struct drm_i915_private *dev_priv = dev->dev_private;
4507 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4508 bool visible = base != 0;
4509 u32 cntl;
4510
4511 if (intel_crtc->cursor_visible == visible)
4512 return;
4513
4514 cntl = I915_READ(CURACNTR);
4515 if (visible) {
4516 /* On these chipsets we can only modify the base whilst
4517 * the cursor is disabled.
4518 */
4519 I915_WRITE(CURABASE, base);
4520
4521 cntl &= ~(CURSOR_FORMAT_MASK);
4522 /* XXX width must be 64, stride 256 => 0x00 << 28 */
4523 cntl |= CURSOR_ENABLE |
4524 CURSOR_GAMMA_ENABLE |
4525 CURSOR_FORMAT_ARGB;
4526 } else
4527 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
4528 I915_WRITE(CURACNTR, cntl);
4529
4530 intel_crtc->cursor_visible = visible;
4531}
4532
4533static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
4534{
4535 struct drm_device *dev = crtc->dev;
4536 struct drm_i915_private *dev_priv = dev->dev_private;
4537 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4538 int pipe = intel_crtc->pipe;
4539 bool visible = base != 0;
4540
4541 if (intel_crtc->cursor_visible != visible) {
4542 uint32_t cntl = I915_READ(pipe == 0 ? CURACNTR : CURBCNTR);
4543 if (base) {
4544 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
4545 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
4546 cntl |= pipe << 28; /* Connect to correct pipe */
4547 } else {
4548 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
4549 cntl |= CURSOR_MODE_DISABLE;
4550 }
4551 I915_WRITE(pipe == 0 ? CURACNTR : CURBCNTR, cntl);
4552
4553 intel_crtc->cursor_visible = visible;
4554 }
4555 /* and commit changes on next vblank */
4556 I915_WRITE(pipe == 0 ? CURABASE : CURBBASE, base);
4557}
4558
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004559/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01004560static void intel_crtc_update_cursor(struct drm_crtc *crtc,
4561 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004562{
4563 struct drm_device *dev = crtc->dev;
4564 struct drm_i915_private *dev_priv = dev->dev_private;
4565 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4566 int pipe = intel_crtc->pipe;
4567 int x = intel_crtc->cursor_x;
4568 int y = intel_crtc->cursor_y;
Chris Wilson560b85b2010-08-07 11:01:38 +01004569 u32 base, pos;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004570 bool visible;
4571
4572 pos = 0;
4573
Chris Wilson6b383a72010-09-13 13:54:26 +01004574 if (on && crtc->enabled && crtc->fb) {
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004575 base = intel_crtc->cursor_addr;
4576 if (x > (int) crtc->fb->width)
4577 base = 0;
4578
4579 if (y > (int) crtc->fb->height)
4580 base = 0;
4581 } else
4582 base = 0;
4583
4584 if (x < 0) {
4585 if (x + intel_crtc->cursor_width < 0)
4586 base = 0;
4587
4588 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
4589 x = -x;
4590 }
4591 pos |= x << CURSOR_X_SHIFT;
4592
4593 if (y < 0) {
4594 if (y + intel_crtc->cursor_height < 0)
4595 base = 0;
4596
4597 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
4598 y = -y;
4599 }
4600 pos |= y << CURSOR_Y_SHIFT;
4601
4602 visible = base != 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01004603 if (!visible && !intel_crtc->cursor_visible)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004604 return;
4605
4606 I915_WRITE(pipe == 0 ? CURAPOS : CURBPOS, pos);
Chris Wilson560b85b2010-08-07 11:01:38 +01004607 if (IS_845G(dev) || IS_I865G(dev))
4608 i845_update_cursor(crtc, base);
4609 else
4610 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004611
4612 if (visible)
4613 intel_mark_busy(dev, to_intel_framebuffer(crtc->fb)->obj);
4614}
4615
Jesse Barnes79e53942008-11-07 14:24:08 -08004616static int intel_crtc_cursor_set(struct drm_crtc *crtc,
Chris Wilson05394f32010-11-08 19:18:58 +00004617 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08004618 uint32_t handle,
4619 uint32_t width, uint32_t height)
4620{
4621 struct drm_device *dev = crtc->dev;
4622 struct drm_i915_private *dev_priv = dev->dev_private;
4623 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00004624 struct drm_i915_gem_object *obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004625 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004626 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08004627
Zhao Yakui28c97732009-10-09 11:39:41 +08004628 DRM_DEBUG_KMS("\n");
Jesse Barnes79e53942008-11-07 14:24:08 -08004629
4630 /* if we want to turn off the cursor ignore width and height */
4631 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08004632 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004633 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00004634 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10004635 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004636 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08004637 }
4638
4639 /* Currently we only support 64x64 cursors */
4640 if (width != 64 || height != 64) {
4641 DRM_ERROR("we currently only support 64x64 cursors\n");
4642 return -EINVAL;
4643 }
4644
Chris Wilson05394f32010-11-08 19:18:58 +00004645 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
4646 if (!obj)
Jesse Barnes79e53942008-11-07 14:24:08 -08004647 return -ENOENT;
4648
Chris Wilson05394f32010-11-08 19:18:58 +00004649 if (obj->base.size < width * height * 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004650 DRM_ERROR("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10004651 ret = -ENOMEM;
4652 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08004653 }
4654
Dave Airlie71acb5e2008-12-30 20:31:46 +10004655 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05004656 mutex_lock(&dev->struct_mutex);
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05004657 if (!dev_priv->info->cursor_needs_physical) {
Chris Wilsond9e86c02010-11-10 16:40:20 +00004658 if (obj->tiling_mode) {
4659 DRM_ERROR("cursor cannot be tiled\n");
4660 ret = -EINVAL;
4661 goto fail_locked;
4662 }
4663
Chris Wilson05394f32010-11-08 19:18:58 +00004664 ret = i915_gem_object_pin(obj, PAGE_SIZE, true);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004665 if (ret) {
4666 DRM_ERROR("failed to pin cursor bo\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05004667 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004668 }
Chris Wilsone7b526b2010-06-02 08:30:48 +01004669
Chris Wilson05394f32010-11-08 19:18:58 +00004670 ret = i915_gem_object_set_to_gtt_domain(obj, 0);
Chris Wilsone7b526b2010-06-02 08:30:48 +01004671 if (ret) {
4672 DRM_ERROR("failed to move cursor bo into the GTT\n");
4673 goto fail_unpin;
4674 }
4675
Chris Wilsond9e86c02010-11-10 16:40:20 +00004676 ret = i915_gem_object_put_fence(obj);
4677 if (ret) {
4678 DRM_ERROR("failed to move cursor bo into the GTT\n");
4679 goto fail_unpin;
4680 }
4681
Chris Wilson05394f32010-11-08 19:18:58 +00004682 addr = obj->gtt_offset;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004683 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004684 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson05394f32010-11-08 19:18:58 +00004685 ret = i915_gem_attach_phys_object(dev, obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004686 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
4687 align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004688 if (ret) {
4689 DRM_ERROR("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05004690 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004691 }
Chris Wilson05394f32010-11-08 19:18:58 +00004692 addr = obj->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004693 }
4694
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004695 if (IS_GEN2(dev))
Jesse Barnes14b603912009-05-20 16:47:08 -04004696 I915_WRITE(CURSIZE, (height << 12) | width);
4697
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004698 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004699 if (intel_crtc->cursor_bo) {
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05004700 if (dev_priv->info->cursor_needs_physical) {
Chris Wilson05394f32010-11-08 19:18:58 +00004701 if (intel_crtc->cursor_bo != obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004702 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
4703 } else
4704 i915_gem_object_unpin(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00004705 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004706 }
Jesse Barnes80824002009-09-10 15:28:06 -07004707
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05004708 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004709
4710 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00004711 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004712 intel_crtc->cursor_width = width;
4713 intel_crtc->cursor_height = height;
4714
Chris Wilson6b383a72010-09-13 13:54:26 +01004715 intel_crtc_update_cursor(crtc, true);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004716
Jesse Barnes79e53942008-11-07 14:24:08 -08004717 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01004718fail_unpin:
Chris Wilson05394f32010-11-08 19:18:58 +00004719 i915_gem_object_unpin(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05004720fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10004721 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00004722fail:
Chris Wilson05394f32010-11-08 19:18:58 +00004723 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10004724 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08004725}
4726
4727static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
4728{
Jesse Barnes79e53942008-11-07 14:24:08 -08004729 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08004730
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004731 intel_crtc->cursor_x = x;
4732 intel_crtc->cursor_y = y;
Jesse Barnes652c3932009-08-17 13:31:43 -07004733
Chris Wilson6b383a72010-09-13 13:54:26 +01004734 intel_crtc_update_cursor(crtc, true);
Jesse Barnes79e53942008-11-07 14:24:08 -08004735
4736 return 0;
4737}
4738
4739/** Sets the color ramps on behalf of RandR */
4740void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
4741 u16 blue, int regno)
4742{
4743 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4744
4745 intel_crtc->lut_r[regno] = red >> 8;
4746 intel_crtc->lut_g[regno] = green >> 8;
4747 intel_crtc->lut_b[regno] = blue >> 8;
4748}
4749
Dave Airlieb8c00ac2009-10-06 13:54:01 +10004750void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
4751 u16 *blue, int regno)
4752{
4753 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4754
4755 *red = intel_crtc->lut_r[regno] << 8;
4756 *green = intel_crtc->lut_g[regno] << 8;
4757 *blue = intel_crtc->lut_b[regno] << 8;
4758}
4759
Jesse Barnes79e53942008-11-07 14:24:08 -08004760static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01004761 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08004762{
James Simmons72034252010-08-03 01:33:19 +01004763 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08004764 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08004765
James Simmons72034252010-08-03 01:33:19 +01004766 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004767 intel_crtc->lut_r[i] = red[i] >> 8;
4768 intel_crtc->lut_g[i] = green[i] >> 8;
4769 intel_crtc->lut_b[i] = blue[i] >> 8;
4770 }
4771
4772 intel_crtc_load_lut(crtc);
4773}
4774
4775/**
4776 * Get a pipe with a simple mode set on it for doing load-based monitor
4777 * detection.
4778 *
4779 * It will be up to the load-detect code to adjust the pipe as appropriate for
Eric Anholtc751ce42010-03-25 11:48:48 -07004780 * its requirements. The pipe will be connected to no other encoders.
Jesse Barnes79e53942008-11-07 14:24:08 -08004781 *
Eric Anholtc751ce42010-03-25 11:48:48 -07004782 * Currently this code will only succeed if there is a pipe with no encoders
Jesse Barnes79e53942008-11-07 14:24:08 -08004783 * configured for it. In the future, it could choose to temporarily disable
4784 * some outputs to free up a pipe for its use.
4785 *
4786 * \return crtc, or NULL if no pipes are available.
4787 */
4788
4789/* VESA 640x480x72Hz mode to set on the pipe */
4790static struct drm_display_mode load_detect_mode = {
4791 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
4792 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
4793};
4794
Eric Anholt21d40d32010-03-25 11:11:14 -07004795struct drm_crtc *intel_get_load_detect_pipe(struct intel_encoder *intel_encoder,
Zhenyu Wangc1c43972010-03-30 14:39:30 +08004796 struct drm_connector *connector,
Jesse Barnes79e53942008-11-07 14:24:08 -08004797 struct drm_display_mode *mode,
4798 int *dpms_mode)
4799{
4800 struct intel_crtc *intel_crtc;
4801 struct drm_crtc *possible_crtc;
4802 struct drm_crtc *supported_crtc =NULL;
Chris Wilson4ef69c72010-09-09 15:14:28 +01004803 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08004804 struct drm_crtc *crtc = NULL;
4805 struct drm_device *dev = encoder->dev;
4806 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
4807 struct drm_crtc_helper_funcs *crtc_funcs;
4808 int i = -1;
4809
4810 /*
4811 * Algorithm gets a little messy:
4812 * - if the connector already has an assigned crtc, use it (but make
4813 * sure it's on first)
4814 * - try to find the first unused crtc that can drive this connector,
4815 * and use that if we find one
4816 * - if there are no unused crtcs available, try to use the first
4817 * one we found that supports the connector
4818 */
4819
4820 /* See if we already have a CRTC for this connector */
4821 if (encoder->crtc) {
4822 crtc = encoder->crtc;
4823 /* Make sure the crtc and connector are running */
4824 intel_crtc = to_intel_crtc(crtc);
4825 *dpms_mode = intel_crtc->dpms_mode;
4826 if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
4827 crtc_funcs = crtc->helper_private;
4828 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
4829 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
4830 }
4831 return crtc;
4832 }
4833
4834 /* Find an unused one (if possible) */
4835 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
4836 i++;
4837 if (!(encoder->possible_crtcs & (1 << i)))
4838 continue;
4839 if (!possible_crtc->enabled) {
4840 crtc = possible_crtc;
4841 break;
4842 }
4843 if (!supported_crtc)
4844 supported_crtc = possible_crtc;
4845 }
4846
4847 /*
4848 * If we didn't find an unused CRTC, don't use any.
4849 */
4850 if (!crtc) {
4851 return NULL;
4852 }
4853
4854 encoder->crtc = crtc;
Zhenyu Wangc1c43972010-03-30 14:39:30 +08004855 connector->encoder = encoder;
Eric Anholt21d40d32010-03-25 11:11:14 -07004856 intel_encoder->load_detect_temp = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08004857
4858 intel_crtc = to_intel_crtc(crtc);
4859 *dpms_mode = intel_crtc->dpms_mode;
4860
4861 if (!crtc->enabled) {
4862 if (!mode)
4863 mode = &load_detect_mode;
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05004864 drm_crtc_helper_set_mode(crtc, mode, 0, 0, crtc->fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08004865 } else {
4866 if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
4867 crtc_funcs = crtc->helper_private;
4868 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
4869 }
4870
4871 /* Add this connector to the crtc */
4872 encoder_funcs->mode_set(encoder, &crtc->mode, &crtc->mode);
4873 encoder_funcs->commit(encoder);
4874 }
4875 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07004876 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08004877
4878 return crtc;
4879}
4880
Zhenyu Wangc1c43972010-03-30 14:39:30 +08004881void intel_release_load_detect_pipe(struct intel_encoder *intel_encoder,
4882 struct drm_connector *connector, int dpms_mode)
Jesse Barnes79e53942008-11-07 14:24:08 -08004883{
Chris Wilson4ef69c72010-09-09 15:14:28 +01004884 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08004885 struct drm_device *dev = encoder->dev;
4886 struct drm_crtc *crtc = encoder->crtc;
4887 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
4888 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
4889
Eric Anholt21d40d32010-03-25 11:11:14 -07004890 if (intel_encoder->load_detect_temp) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004891 encoder->crtc = NULL;
Zhenyu Wangc1c43972010-03-30 14:39:30 +08004892 connector->encoder = NULL;
Eric Anholt21d40d32010-03-25 11:11:14 -07004893 intel_encoder->load_detect_temp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08004894 crtc->enabled = drm_helper_crtc_in_use(crtc);
4895 drm_helper_disable_unused_functions(dev);
4896 }
4897
Eric Anholtc751ce42010-03-25 11:48:48 -07004898 /* Switch crtc and encoder back off if necessary */
Jesse Barnes79e53942008-11-07 14:24:08 -08004899 if (crtc->enabled && dpms_mode != DRM_MODE_DPMS_ON) {
4900 if (encoder->crtc == crtc)
4901 encoder_funcs->dpms(encoder, dpms_mode);
4902 crtc_funcs->dpms(crtc, dpms_mode);
4903 }
4904}
4905
4906/* Returns the clock of the currently programmed mode of the given pipe. */
4907static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
4908{
4909 struct drm_i915_private *dev_priv = dev->dev_private;
4910 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4911 int pipe = intel_crtc->pipe;
4912 u32 dpll = I915_READ((pipe == 0) ? DPLL_A : DPLL_B);
4913 u32 fp;
4914 intel_clock_t clock;
4915
4916 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
4917 fp = I915_READ((pipe == 0) ? FPA0 : FPB0);
4918 else
4919 fp = I915_READ((pipe == 0) ? FPA1 : FPB1);
4920
4921 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05004922 if (IS_PINEVIEW(dev)) {
4923 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
4924 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08004925 } else {
4926 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
4927 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
4928 }
4929
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004930 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05004931 if (IS_PINEVIEW(dev))
4932 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
4933 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08004934 else
4935 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08004936 DPLL_FPA01_P1_POST_DIV_SHIFT);
4937
4938 switch (dpll & DPLL_MODE_MASK) {
4939 case DPLLB_MODE_DAC_SERIAL:
4940 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
4941 5 : 10;
4942 break;
4943 case DPLLB_MODE_LVDS:
4944 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
4945 7 : 14;
4946 break;
4947 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08004948 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08004949 "mode\n", (int)(dpll & DPLL_MODE_MASK));
4950 return 0;
4951 }
4952
4953 /* XXX: Handle the 100Mhz refclk */
Shaohua Li21778322009-02-23 15:19:16 +08004954 intel_clock(dev, 96000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08004955 } else {
4956 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
4957
4958 if (is_lvds) {
4959 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
4960 DPLL_FPA01_P1_POST_DIV_SHIFT);
4961 clock.p2 = 14;
4962
4963 if ((dpll & PLL_REF_INPUT_MASK) ==
4964 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
4965 /* XXX: might not be 66MHz */
Shaohua Li21778322009-02-23 15:19:16 +08004966 intel_clock(dev, 66000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08004967 } else
Shaohua Li21778322009-02-23 15:19:16 +08004968 intel_clock(dev, 48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08004969 } else {
4970 if (dpll & PLL_P1_DIVIDE_BY_TWO)
4971 clock.p1 = 2;
4972 else {
4973 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
4974 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
4975 }
4976 if (dpll & PLL_P2_DIVIDE_BY_4)
4977 clock.p2 = 4;
4978 else
4979 clock.p2 = 2;
4980
Shaohua Li21778322009-02-23 15:19:16 +08004981 intel_clock(dev, 48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08004982 }
4983 }
4984
4985 /* XXX: It would be nice to validate the clocks, but we can't reuse
4986 * i830PllIsValid() because it relies on the xf86_config connector
4987 * configuration being accurate, which it isn't necessarily.
4988 */
4989
4990 return clock.dot;
4991}
4992
4993/** Returns the currently programmed mode of the given pipe. */
4994struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
4995 struct drm_crtc *crtc)
4996{
4997 struct drm_i915_private *dev_priv = dev->dev_private;
4998 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4999 int pipe = intel_crtc->pipe;
5000 struct drm_display_mode *mode;
5001 int htot = I915_READ((pipe == 0) ? HTOTAL_A : HTOTAL_B);
5002 int hsync = I915_READ((pipe == 0) ? HSYNC_A : HSYNC_B);
5003 int vtot = I915_READ((pipe == 0) ? VTOTAL_A : VTOTAL_B);
5004 int vsync = I915_READ((pipe == 0) ? VSYNC_A : VSYNC_B);
5005
5006 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
5007 if (!mode)
5008 return NULL;
5009
5010 mode->clock = intel_crtc_clock_get(dev, crtc);
5011 mode->hdisplay = (htot & 0xffff) + 1;
5012 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
5013 mode->hsync_start = (hsync & 0xffff) + 1;
5014 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
5015 mode->vdisplay = (vtot & 0xffff) + 1;
5016 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
5017 mode->vsync_start = (vsync & 0xffff) + 1;
5018 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
5019
5020 drm_mode_set_name(mode);
5021 drm_mode_set_crtcinfo(mode, 0);
5022
5023 return mode;
5024}
5025
Jesse Barnes652c3932009-08-17 13:31:43 -07005026#define GPU_IDLE_TIMEOUT 500 /* ms */
5027
5028/* When this timer fires, we've been idle for awhile */
5029static void intel_gpu_idle_timer(unsigned long arg)
5030{
5031 struct drm_device *dev = (struct drm_device *)arg;
5032 drm_i915_private_t *dev_priv = dev->dev_private;
5033
Chris Wilsonff7ea4c2010-12-08 09:43:41 +00005034 if (!list_empty(&dev_priv->mm.active_list)) {
5035 /* Still processing requests, so just re-arm the timer. */
5036 mod_timer(&dev_priv->idle_timer, jiffies +
5037 msecs_to_jiffies(GPU_IDLE_TIMEOUT));
5038 return;
5039 }
Jesse Barnes652c3932009-08-17 13:31:43 -07005040
Chris Wilsonff7ea4c2010-12-08 09:43:41 +00005041 dev_priv->busy = false;
Eric Anholt01dfba92009-09-06 15:18:53 -07005042 queue_work(dev_priv->wq, &dev_priv->idle_work);
Jesse Barnes652c3932009-08-17 13:31:43 -07005043}
5044
Jesse Barnes652c3932009-08-17 13:31:43 -07005045#define CRTC_IDLE_TIMEOUT 1000 /* ms */
5046
5047static void intel_crtc_idle_timer(unsigned long arg)
5048{
5049 struct intel_crtc *intel_crtc = (struct intel_crtc *)arg;
5050 struct drm_crtc *crtc = &intel_crtc->base;
5051 drm_i915_private_t *dev_priv = crtc->dev->dev_private;
Chris Wilsonff7ea4c2010-12-08 09:43:41 +00005052 struct intel_framebuffer *intel_fb;
5053
5054 intel_fb = to_intel_framebuffer(crtc->fb);
5055 if (intel_fb && intel_fb->obj->active) {
5056 /* The framebuffer is still being accessed by the GPU. */
5057 mod_timer(&intel_crtc->idle_timer, jiffies +
5058 msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
5059 return;
5060 }
Jesse Barnes652c3932009-08-17 13:31:43 -07005061
Jesse Barnes652c3932009-08-17 13:31:43 -07005062 intel_crtc->busy = false;
Eric Anholt01dfba92009-09-06 15:18:53 -07005063 queue_work(dev_priv->wq, &dev_priv->idle_work);
Jesse Barnes652c3932009-08-17 13:31:43 -07005064}
5065
Daniel Vetter3dec0092010-08-20 21:40:52 +02005066static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07005067{
5068 struct drm_device *dev = crtc->dev;
5069 drm_i915_private_t *dev_priv = dev->dev_private;
5070 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5071 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08005072 int dpll_reg = DPLL(pipe);
5073 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07005074
Eric Anholtbad720f2009-10-22 16:11:14 -07005075 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07005076 return;
5077
5078 if (!dev_priv->lvds_downclock_avail)
5079 return;
5080
Jesse Barnesdbdc6472010-12-30 09:36:39 -08005081 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07005082 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08005083 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07005084
5085 /* Unlock panel regs */
Jesse Barnesdbdc6472010-12-30 09:36:39 -08005086 I915_WRITE(PP_CONTROL,
5087 I915_READ(PP_CONTROL) | PANEL_UNLOCK_REGS);
Jesse Barnes652c3932009-08-17 13:31:43 -07005088
5089 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
5090 I915_WRITE(dpll_reg, dpll);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08005091 POSTING_READ(dpll_reg);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07005092 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08005093
Jesse Barnes652c3932009-08-17 13:31:43 -07005094 dpll = I915_READ(dpll_reg);
5095 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08005096 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07005097
5098 /* ...and lock them again */
5099 I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
5100 }
5101
5102 /* Schedule downclock */
Daniel Vetter3dec0092010-08-20 21:40:52 +02005103 mod_timer(&intel_crtc->idle_timer, jiffies +
5104 msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
Jesse Barnes652c3932009-08-17 13:31:43 -07005105}
5106
5107static void intel_decrease_pllclock(struct drm_crtc *crtc)
5108{
5109 struct drm_device *dev = crtc->dev;
5110 drm_i915_private_t *dev_priv = dev->dev_private;
5111 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5112 int pipe = intel_crtc->pipe;
5113 int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
5114 int dpll = I915_READ(dpll_reg);
5115
Eric Anholtbad720f2009-10-22 16:11:14 -07005116 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07005117 return;
5118
5119 if (!dev_priv->lvds_downclock_avail)
5120 return;
5121
5122 /*
5123 * Since this is called by a timer, we should never get here in
5124 * the manual case.
5125 */
5126 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08005127 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07005128
5129 /* Unlock panel regs */
Jesse Barnes4a655f02010-07-22 13:18:18 -07005130 I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) |
5131 PANEL_UNLOCK_REGS);
Jesse Barnes652c3932009-08-17 13:31:43 -07005132
5133 dpll |= DISPLAY_RATE_SELECT_FPA1;
5134 I915_WRITE(dpll_reg, dpll);
5135 dpll = I915_READ(dpll_reg);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07005136 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07005137 dpll = I915_READ(dpll_reg);
5138 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08005139 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07005140
5141 /* ...and lock them again */
5142 I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
5143 }
5144
5145}
5146
5147/**
5148 * intel_idle_update - adjust clocks for idleness
5149 * @work: work struct
5150 *
5151 * Either the GPU or display (or both) went idle. Check the busy status
5152 * here and adjust the CRTC and GPU clocks as necessary.
5153 */
5154static void intel_idle_update(struct work_struct *work)
5155{
5156 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
5157 idle_work);
5158 struct drm_device *dev = dev_priv->dev;
5159 struct drm_crtc *crtc;
5160 struct intel_crtc *intel_crtc;
Li Peng45ac22c2010-06-12 23:38:35 +08005161 int enabled = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07005162
5163 if (!i915_powersave)
5164 return;
5165
5166 mutex_lock(&dev->struct_mutex);
5167
Jesse Barnes7648fa92010-05-20 14:28:11 -07005168 i915_update_gfx_val(dev_priv);
5169
Jesse Barnes652c3932009-08-17 13:31:43 -07005170 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
5171 /* Skip inactive CRTCs */
5172 if (!crtc->fb)
5173 continue;
5174
Li Peng45ac22c2010-06-12 23:38:35 +08005175 enabled++;
Jesse Barnes652c3932009-08-17 13:31:43 -07005176 intel_crtc = to_intel_crtc(crtc);
5177 if (!intel_crtc->busy)
5178 intel_decrease_pllclock(crtc);
5179 }
5180
Li Peng45ac22c2010-06-12 23:38:35 +08005181 if ((enabled == 1) && (IS_I945G(dev) || IS_I945GM(dev))) {
5182 DRM_DEBUG_DRIVER("enable memory self refresh on 945\n");
5183 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
5184 }
5185
Jesse Barnes652c3932009-08-17 13:31:43 -07005186 mutex_unlock(&dev->struct_mutex);
5187}
5188
5189/**
5190 * intel_mark_busy - mark the GPU and possibly the display busy
5191 * @dev: drm device
5192 * @obj: object we're operating on
5193 *
5194 * Callers can use this function to indicate that the GPU is busy processing
5195 * commands. If @obj matches one of the CRTC objects (i.e. it's a scanout
5196 * buffer), we'll also mark the display as busy, so we know to increase its
5197 * clock frequency.
5198 */
Chris Wilson05394f32010-11-08 19:18:58 +00005199void intel_mark_busy(struct drm_device *dev, struct drm_i915_gem_object *obj)
Jesse Barnes652c3932009-08-17 13:31:43 -07005200{
5201 drm_i915_private_t *dev_priv = dev->dev_private;
5202 struct drm_crtc *crtc = NULL;
5203 struct intel_framebuffer *intel_fb;
5204 struct intel_crtc *intel_crtc;
5205
Zhenyu Wang5e17ee72009-09-03 09:30:06 +08005206 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5207 return;
5208
Li Peng060e6452010-02-10 01:54:24 +08005209 if (!dev_priv->busy) {
5210 if (IS_I945G(dev) || IS_I945GM(dev)) {
5211 u32 fw_blc_self;
Li Pengee980b82010-01-27 19:01:11 +08005212
Li Peng060e6452010-02-10 01:54:24 +08005213 DRM_DEBUG_DRIVER("disable memory self refresh on 945\n");
5214 fw_blc_self = I915_READ(FW_BLC_SELF);
5215 fw_blc_self &= ~FW_BLC_SELF_EN;
5216 I915_WRITE(FW_BLC_SELF, fw_blc_self | FW_BLC_SELF_EN_MASK);
5217 }
Chris Wilson28cf7982009-11-30 01:08:56 +00005218 dev_priv->busy = true;
Li Peng060e6452010-02-10 01:54:24 +08005219 } else
Chris Wilson28cf7982009-11-30 01:08:56 +00005220 mod_timer(&dev_priv->idle_timer, jiffies +
5221 msecs_to_jiffies(GPU_IDLE_TIMEOUT));
Jesse Barnes652c3932009-08-17 13:31:43 -07005222
5223 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
5224 if (!crtc->fb)
5225 continue;
5226
5227 intel_crtc = to_intel_crtc(crtc);
5228 intel_fb = to_intel_framebuffer(crtc->fb);
5229 if (intel_fb->obj == obj) {
5230 if (!intel_crtc->busy) {
Li Peng060e6452010-02-10 01:54:24 +08005231 if (IS_I945G(dev) || IS_I945GM(dev)) {
5232 u32 fw_blc_self;
5233
5234 DRM_DEBUG_DRIVER("disable memory self refresh on 945\n");
5235 fw_blc_self = I915_READ(FW_BLC_SELF);
5236 fw_blc_self &= ~FW_BLC_SELF_EN;
5237 I915_WRITE(FW_BLC_SELF, fw_blc_self | FW_BLC_SELF_EN_MASK);
5238 }
Jesse Barnes652c3932009-08-17 13:31:43 -07005239 /* Non-busy -> busy, upclock */
Daniel Vetter3dec0092010-08-20 21:40:52 +02005240 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07005241 intel_crtc->busy = true;
5242 } else {
5243 /* Busy -> busy, put off timer */
5244 mod_timer(&intel_crtc->idle_timer, jiffies +
5245 msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
5246 }
5247 }
5248 }
5249}
5250
Jesse Barnes79e53942008-11-07 14:24:08 -08005251static void intel_crtc_destroy(struct drm_crtc *crtc)
5252{
5253 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02005254 struct drm_device *dev = crtc->dev;
5255 struct intel_unpin_work *work;
5256 unsigned long flags;
5257
5258 spin_lock_irqsave(&dev->event_lock, flags);
5259 work = intel_crtc->unpin_work;
5260 intel_crtc->unpin_work = NULL;
5261 spin_unlock_irqrestore(&dev->event_lock, flags);
5262
5263 if (work) {
5264 cancel_work_sync(&work->work);
5265 kfree(work);
5266 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005267
5268 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02005269
Jesse Barnes79e53942008-11-07 14:24:08 -08005270 kfree(intel_crtc);
5271}
5272
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005273static void intel_unpin_work_fn(struct work_struct *__work)
5274{
5275 struct intel_unpin_work *work =
5276 container_of(__work, struct intel_unpin_work, work);
5277
5278 mutex_lock(&work->dev->struct_mutex);
Jesse Barnesb1b87f62010-01-26 14:40:05 -08005279 i915_gem_object_unpin(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00005280 drm_gem_object_unreference(&work->pending_flip_obj->base);
5281 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00005282
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005283 mutex_unlock(&work->dev->struct_mutex);
5284 kfree(work);
5285}
5286
Jesse Barnes1afe3e92010-03-26 10:35:20 -07005287static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01005288 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005289{
5290 drm_i915_private_t *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005291 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5292 struct intel_unpin_work *work;
Chris Wilson05394f32010-11-08 19:18:58 +00005293 struct drm_i915_gem_object *obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005294 struct drm_pending_vblank_event *e;
Mario Kleiner49b14a52010-12-09 07:00:07 +01005295 struct timeval tnow, tvbl;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005296 unsigned long flags;
5297
5298 /* Ignore early vblank irqs */
5299 if (intel_crtc == NULL)
5300 return;
5301
Mario Kleiner49b14a52010-12-09 07:00:07 +01005302 do_gettimeofday(&tnow);
5303
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005304 spin_lock_irqsave(&dev->event_lock, flags);
5305 work = intel_crtc->unpin_work;
5306 if (work == NULL || !work->pending) {
5307 spin_unlock_irqrestore(&dev->event_lock, flags);
5308 return;
5309 }
5310
5311 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005312
5313 if (work->event) {
5314 e = work->event;
Mario Kleiner49b14a52010-12-09 07:00:07 +01005315 e->event.sequence = drm_vblank_count_and_time(dev, intel_crtc->pipe, &tvbl);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01005316
5317 /* Called before vblank count and timestamps have
5318 * been updated for the vblank interval of flip
5319 * completion? Need to increment vblank count and
5320 * add one videorefresh duration to returned timestamp
Mario Kleiner49b14a52010-12-09 07:00:07 +01005321 * to account for this. We assume this happened if we
5322 * get called over 0.9 frame durations after the last
5323 * timestamped vblank.
5324 *
5325 * This calculation can not be used with vrefresh rates
5326 * below 5Hz (10Hz to be on the safe side) without
5327 * promoting to 64 integers.
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01005328 */
Mario Kleiner49b14a52010-12-09 07:00:07 +01005329 if (10 * (timeval_to_ns(&tnow) - timeval_to_ns(&tvbl)) >
5330 9 * crtc->framedur_ns) {
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01005331 e->event.sequence++;
Mario Kleiner49b14a52010-12-09 07:00:07 +01005332 tvbl = ns_to_timeval(timeval_to_ns(&tvbl) +
5333 crtc->framedur_ns);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01005334 }
5335
Mario Kleiner49b14a52010-12-09 07:00:07 +01005336 e->event.tv_sec = tvbl.tv_sec;
5337 e->event.tv_usec = tvbl.tv_usec;
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01005338
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005339 list_add_tail(&e->base.link,
5340 &e->base.file_priv->event_list);
5341 wake_up_interruptible(&e->base.file_priv->event_wait);
5342 }
5343
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01005344 drm_vblank_put(dev, intel_crtc->pipe);
5345
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005346 spin_unlock_irqrestore(&dev->event_lock, flags);
5347
Chris Wilson05394f32010-11-08 19:18:58 +00005348 obj = work->old_fb_obj;
Chris Wilsond9e86c02010-11-10 16:40:20 +00005349
Chris Wilsone59f2ba2010-10-07 17:28:15 +01005350 atomic_clear_mask(1 << intel_crtc->plane,
Chris Wilson05394f32010-11-08 19:18:58 +00005351 &obj->pending_flip.counter);
5352 if (atomic_read(&obj->pending_flip) == 0)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005353 wake_up(&dev_priv->pending_flip_queue);
Chris Wilsond9e86c02010-11-10 16:40:20 +00005354
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005355 schedule_work(&work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07005356
5357 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005358}
5359
Jesse Barnes1afe3e92010-03-26 10:35:20 -07005360void intel_finish_page_flip(struct drm_device *dev, int pipe)
5361{
5362 drm_i915_private_t *dev_priv = dev->dev_private;
5363 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
5364
Mario Kleiner49b14a52010-12-09 07:00:07 +01005365 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07005366}
5367
5368void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
5369{
5370 drm_i915_private_t *dev_priv = dev->dev_private;
5371 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
5372
Mario Kleiner49b14a52010-12-09 07:00:07 +01005373 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07005374}
5375
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005376void intel_prepare_page_flip(struct drm_device *dev, int plane)
5377{
5378 drm_i915_private_t *dev_priv = dev->dev_private;
5379 struct intel_crtc *intel_crtc =
5380 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
5381 unsigned long flags;
5382
5383 spin_lock_irqsave(&dev->event_lock, flags);
Jesse Barnesde3f4402010-01-14 13:18:02 -08005384 if (intel_crtc->unpin_work) {
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01005385 if ((++intel_crtc->unpin_work->pending) > 1)
5386 DRM_ERROR("Prepared flip multiple times\n");
Jesse Barnesde3f4402010-01-14 13:18:02 -08005387 } else {
5388 DRM_DEBUG_DRIVER("preparing flip with no unpin work?\n");
5389 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005390 spin_unlock_irqrestore(&dev->event_lock, flags);
5391}
5392
5393static int intel_crtc_page_flip(struct drm_crtc *crtc,
5394 struct drm_framebuffer *fb,
5395 struct drm_pending_vblank_event *event)
5396{
5397 struct drm_device *dev = crtc->dev;
5398 struct drm_i915_private *dev_priv = dev->dev_private;
5399 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00005400 struct drm_i915_gem_object *obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005401 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5402 struct intel_unpin_work *work;
Jesse Barnesbe9a3db2010-07-23 12:03:37 -07005403 unsigned long flags, offset;
Chris Wilson52e68632010-08-08 10:15:59 +01005404 int pipe = intel_crtc->pipe;
Chris Wilson20f0cd52010-09-23 11:00:38 +01005405 u32 pf, pipesrc;
Chris Wilson52e68632010-08-08 10:15:59 +01005406 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005407
5408 work = kzalloc(sizeof *work, GFP_KERNEL);
5409 if (work == NULL)
5410 return -ENOMEM;
5411
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005412 work->event = event;
5413 work->dev = crtc->dev;
5414 intel_fb = to_intel_framebuffer(crtc->fb);
Jesse Barnesb1b87f62010-01-26 14:40:05 -08005415 work->old_fb_obj = intel_fb->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005416 INIT_WORK(&work->work, intel_unpin_work_fn);
5417
5418 /* We borrow the event spin lock for protecting unpin_work */
5419 spin_lock_irqsave(&dev->event_lock, flags);
5420 if (intel_crtc->unpin_work) {
5421 spin_unlock_irqrestore(&dev->event_lock, flags);
5422 kfree(work);
Chris Wilson468f0b42010-05-27 13:18:13 +01005423
5424 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005425 return -EBUSY;
5426 }
5427 intel_crtc->unpin_work = work;
5428 spin_unlock_irqrestore(&dev->event_lock, flags);
5429
5430 intel_fb = to_intel_framebuffer(fb);
5431 obj = intel_fb->obj;
5432
Chris Wilson468f0b42010-05-27 13:18:13 +01005433 mutex_lock(&dev->struct_mutex);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00005434 ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
Chris Wilson96b099f2010-06-07 14:03:04 +01005435 if (ret)
5436 goto cleanup_work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005437
Jesse Barnes75dfca82010-02-10 15:09:44 -08005438 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00005439 drm_gem_object_reference(&work->old_fb_obj->base);
5440 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005441
5442 crtc->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01005443
5444 ret = drm_vblank_get(dev, intel_crtc->pipe);
5445 if (ret)
5446 goto cleanup_objs;
5447
Chris Wilsonc7f9f9a2010-09-19 15:05:13 +01005448 if (IS_GEN3(dev) || IS_GEN2(dev)) {
5449 u32 flip_mask;
5450
5451 /* Can't queue multiple flips, so wait for the previous
5452 * one to finish before executing the next.
5453 */
Chris Wilsone1f99ce2010-10-27 12:45:26 +01005454 ret = BEGIN_LP_RING(2);
5455 if (ret)
5456 goto cleanup_objs;
5457
Chris Wilsonc7f9f9a2010-09-19 15:05:13 +01005458 if (intel_crtc->plane)
5459 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
5460 else
5461 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
5462 OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);
5463 OUT_RING(MI_NOOP);
Daniel Vetter6146b3d2010-08-04 21:22:10 +02005464 ADVANCE_LP_RING();
5465 }
Jesse Barnes83f7fd02010-04-05 14:03:51 -07005466
Chris Wilsone1f99ce2010-10-27 12:45:26 +01005467 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01005468
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01005469 work->enable_stall_check = true;
5470
Jesse Barnesbe9a3db2010-07-23 12:03:37 -07005471 /* Offset into the new buffer for cases of shared fbs between CRTCs */
Chris Wilson52e68632010-08-08 10:15:59 +01005472 offset = crtc->y * fb->pitch + crtc->x * fb->bits_per_pixel/8;
Jesse Barnesbe9a3db2010-07-23 12:03:37 -07005473
Chris Wilsone1f99ce2010-10-27 12:45:26 +01005474 ret = BEGIN_LP_RING(4);
5475 if (ret)
5476 goto cleanup_objs;
5477
5478 /* Block clients from rendering to the new back buffer until
5479 * the flip occurs and the object is no longer visible.
5480 */
Chris Wilson05394f32010-11-08 19:18:58 +00005481 atomic_add(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01005482
5483 switch (INTEL_INFO(dev)->gen) {
Chris Wilson52e68632010-08-08 10:15:59 +01005484 case 2:
Jesse Barnes1afe3e92010-03-26 10:35:20 -07005485 OUT_RING(MI_DISPLAY_FLIP |
5486 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
5487 OUT_RING(fb->pitch);
Chris Wilson05394f32010-11-08 19:18:58 +00005488 OUT_RING(obj->gtt_offset + offset);
Chris Wilson52e68632010-08-08 10:15:59 +01005489 OUT_RING(MI_NOOP);
5490 break;
5491
5492 case 3:
Jesse Barnes1afe3e92010-03-26 10:35:20 -07005493 OUT_RING(MI_DISPLAY_FLIP_I915 |
5494 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
5495 OUT_RING(fb->pitch);
Chris Wilson05394f32010-11-08 19:18:58 +00005496 OUT_RING(obj->gtt_offset + offset);
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08005497 OUT_RING(MI_NOOP);
Chris Wilson52e68632010-08-08 10:15:59 +01005498 break;
5499
5500 case 4:
5501 case 5:
5502 /* i965+ uses the linear or tiled offsets from the
5503 * Display Registers (which do not change across a page-flip)
5504 * so we need only reprogram the base address.
5505 */
Daniel Vetter69d0b962010-08-04 21:22:09 +02005506 OUT_RING(MI_DISPLAY_FLIP |
5507 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
5508 OUT_RING(fb->pitch);
Chris Wilson05394f32010-11-08 19:18:58 +00005509 OUT_RING(obj->gtt_offset | obj->tiling_mode);
Chris Wilson52e68632010-08-08 10:15:59 +01005510
5511 /* XXX Enabling the panel-fitter across page-flip is so far
5512 * untested on non-native modes, so ignore it for now.
5513 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
5514 */
5515 pf = 0;
5516 pipesrc = I915_READ(pipe == 0 ? PIPEASRC : PIPEBSRC) & 0x0fff0fff;
5517 OUT_RING(pf | pipesrc);
5518 break;
5519
5520 case 6:
5521 OUT_RING(MI_DISPLAY_FLIP |
5522 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Chris Wilson05394f32010-11-08 19:18:58 +00005523 OUT_RING(fb->pitch | obj->tiling_mode);
5524 OUT_RING(obj->gtt_offset);
Chris Wilson52e68632010-08-08 10:15:59 +01005525
5526 pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
5527 pipesrc = I915_READ(pipe == 0 ? PIPEASRC : PIPEBSRC) & 0x0fff0fff;
5528 OUT_RING(pf | pipesrc);
5529 break;
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08005530 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005531 ADVANCE_LP_RING();
5532
5533 mutex_unlock(&dev->struct_mutex);
5534
Jesse Barnese5510fa2010-07-01 16:48:37 -07005535 trace_i915_flip_request(intel_crtc->plane, obj);
5536
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005537 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01005538
5539cleanup_objs:
Chris Wilson05394f32010-11-08 19:18:58 +00005540 drm_gem_object_unreference(&work->old_fb_obj->base);
5541 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01005542cleanup_work:
5543 mutex_unlock(&dev->struct_mutex);
5544
5545 spin_lock_irqsave(&dev->event_lock, flags);
5546 intel_crtc->unpin_work = NULL;
5547 spin_unlock_irqrestore(&dev->event_lock, flags);
5548
5549 kfree(work);
5550
5551 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005552}
5553
Chris Wilson5d1d0cc2011-01-24 15:02:15 +00005554static void intel_crtc_reset(struct drm_crtc *crtc)
5555{
5556 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5557
5558 /* Reset flags back to the 'unknown' status so that they
5559 * will be correctly set on the initial modeset.
5560 */
Chris Wilson5d1d0cc2011-01-24 15:02:15 +00005561 intel_crtc->dpms_mode = -1;
Chris Wilson5d1d0cc2011-01-24 15:02:15 +00005562}
5563
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07005564static struct drm_crtc_helper_funcs intel_helper_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -08005565 .dpms = intel_crtc_dpms,
5566 .mode_fixup = intel_crtc_mode_fixup,
5567 .mode_set = intel_crtc_mode_set,
5568 .mode_set_base = intel_pipe_set_base,
Jesse Barnes81255562010-08-02 12:07:50 -07005569 .mode_set_base_atomic = intel_pipe_set_base_atomic,
Dave Airlie068143d2009-10-05 09:58:02 +10005570 .load_lut = intel_crtc_load_lut,
Chris Wilsoncdd59982010-09-08 16:30:16 +01005571 .disable = intel_crtc_disable,
Jesse Barnes79e53942008-11-07 14:24:08 -08005572};
5573
5574static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilson5d1d0cc2011-01-24 15:02:15 +00005575 .reset = intel_crtc_reset,
Jesse Barnes79e53942008-11-07 14:24:08 -08005576 .cursor_set = intel_crtc_cursor_set,
5577 .cursor_move = intel_crtc_cursor_move,
5578 .gamma_set = intel_crtc_gamma_set,
5579 .set_config = drm_crtc_helper_set_config,
5580 .destroy = intel_crtc_destroy,
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005581 .page_flip = intel_crtc_page_flip,
Jesse Barnes79e53942008-11-07 14:24:08 -08005582};
5583
Chris Wilson47f1c6c2010-12-03 15:37:31 +00005584static void intel_sanitize_modesetting(struct drm_device *dev,
5585 int pipe, int plane)
5586{
5587 struct drm_i915_private *dev_priv = dev->dev_private;
5588 u32 reg, val;
5589
5590 if (HAS_PCH_SPLIT(dev))
5591 return;
5592
5593 /* Who knows what state these registers were left in by the BIOS or
5594 * grub?
5595 *
5596 * If we leave the registers in a conflicting state (e.g. with the
5597 * display plane reading from the other pipe than the one we intend
5598 * to use) then when we attempt to teardown the active mode, we will
5599 * not disable the pipes and planes in the correct order -- leaving
5600 * a plane reading from a disabled pipe and possibly leading to
5601 * undefined behaviour.
5602 */
5603
5604 reg = DSPCNTR(plane);
5605 val = I915_READ(reg);
5606
5607 if ((val & DISPLAY_PLANE_ENABLE) == 0)
5608 return;
5609 if (!!(val & DISPPLANE_SEL_PIPE_MASK) == pipe)
5610 return;
5611
5612 /* This display plane is active and attached to the other CPU pipe. */
5613 pipe = !pipe;
5614
5615 /* Disable the plane and wait for it to stop reading from the pipe. */
5616 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
5617 intel_flush_display_plane(dev, plane);
5618
5619 if (IS_GEN2(dev))
5620 intel_wait_for_vblank(dev, pipe);
5621
5622 if (pipe == 0 && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
5623 return;
5624
5625 /* Switch off the pipe. */
5626 reg = PIPECONF(pipe);
5627 val = I915_READ(reg);
5628 if (val & PIPECONF_ENABLE) {
5629 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
5630 intel_wait_for_pipe_off(dev, pipe);
5631 }
5632}
Jesse Barnes79e53942008-11-07 14:24:08 -08005633
Hannes Ederb358d0a2008-12-18 21:18:47 +01005634static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -08005635{
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08005636 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08005637 struct intel_crtc *intel_crtc;
5638 int i;
5639
5640 intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
5641 if (intel_crtc == NULL)
5642 return;
5643
5644 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
5645
5646 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -08005647 for (i = 0; i < 256; i++) {
5648 intel_crtc->lut_r[i] = i;
5649 intel_crtc->lut_g[i] = i;
5650 intel_crtc->lut_b[i] = i;
5651 }
5652
Jesse Barnes80824002009-09-10 15:28:06 -07005653 /* Swap pipes & planes for FBC on pre-965 */
5654 intel_crtc->pipe = pipe;
5655 intel_crtc->plane = pipe;
Chris Wilsone2e767a2010-09-13 16:53:12 +01005656 if (IS_MOBILE(dev) && IS_GEN3(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08005657 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +01005658 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07005659 }
5660
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08005661 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
5662 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
5663 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
5664 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
5665
Chris Wilson5d1d0cc2011-01-24 15:02:15 +00005666 intel_crtc_reset(&intel_crtc->base);
Chris Wilson04dbff52011-02-10 17:38:35 +00005667 intel_crtc->active = true; /* force the pipe off on setup_init_config */
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07005668
5669 if (HAS_PCH_SPLIT(dev)) {
5670 intel_helper_funcs.prepare = ironlake_crtc_prepare;
5671 intel_helper_funcs.commit = ironlake_crtc_commit;
5672 } else {
5673 intel_helper_funcs.prepare = i9xx_crtc_prepare;
5674 intel_helper_funcs.commit = i9xx_crtc_commit;
5675 }
5676
Jesse Barnes79e53942008-11-07 14:24:08 -08005677 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
5678
Jesse Barnes652c3932009-08-17 13:31:43 -07005679 intel_crtc->busy = false;
5680
5681 setup_timer(&intel_crtc->idle_timer, intel_crtc_idle_timer,
5682 (unsigned long)intel_crtc);
Chris Wilson47f1c6c2010-12-03 15:37:31 +00005683
5684 intel_sanitize_modesetting(dev, intel_crtc->pipe, intel_crtc->plane);
Jesse Barnes79e53942008-11-07 14:24:08 -08005685}
5686
Carl Worth08d7b3d2009-04-29 14:43:54 -07005687int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00005688 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -07005689{
5690 drm_i915_private_t *dev_priv = dev->dev_private;
5691 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +02005692 struct drm_mode_object *drmmode_obj;
5693 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -07005694
5695 if (!dev_priv) {
5696 DRM_ERROR("called with no initialization\n");
5697 return -EINVAL;
5698 }
5699
Daniel Vetterc05422d2009-08-11 16:05:30 +02005700 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
5701 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -07005702
Daniel Vetterc05422d2009-08-11 16:05:30 +02005703 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -07005704 DRM_ERROR("no such CRTC id\n");
5705 return -EINVAL;
5706 }
5707
Daniel Vetterc05422d2009-08-11 16:05:30 +02005708 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
5709 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -07005710
Daniel Vetterc05422d2009-08-11 16:05:30 +02005711 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -07005712}
5713
Zhenyu Wangc5e4df32010-03-30 14:39:27 +08005714static int intel_encoder_clones(struct drm_device *dev, int type_mask)
Jesse Barnes79e53942008-11-07 14:24:08 -08005715{
Chris Wilson4ef69c72010-09-09 15:14:28 +01005716 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08005717 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005718 int entry = 0;
5719
Chris Wilson4ef69c72010-09-09 15:14:28 +01005720 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
5721 if (type_mask & encoder->clone_mask)
Jesse Barnes79e53942008-11-07 14:24:08 -08005722 index_mask |= (1 << entry);
5723 entry++;
5724 }
Chris Wilson4ef69c72010-09-09 15:14:28 +01005725
Jesse Barnes79e53942008-11-07 14:24:08 -08005726 return index_mask;
5727}
5728
Chris Wilson4d302442010-12-14 19:21:29 +00005729static bool has_edp_a(struct drm_device *dev)
5730{
5731 struct drm_i915_private *dev_priv = dev->dev_private;
5732
5733 if (!IS_MOBILE(dev))
5734 return false;
5735
5736 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
5737 return false;
5738
5739 if (IS_GEN5(dev) &&
5740 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
5741 return false;
5742
5743 return true;
5744}
5745
Jesse Barnes79e53942008-11-07 14:24:08 -08005746static void intel_setup_outputs(struct drm_device *dev)
5747{
Eric Anholt725e30a2009-01-22 13:01:02 -08005748 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +01005749 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -04005750 bool dpd_is_edp = false;
Chris Wilsonc5d1b512010-11-29 18:00:23 +00005751 bool has_lvds = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08005752
Zhenyu Wang541998a2009-06-05 15:38:44 +08005753 if (IS_MOBILE(dev) && !IS_I830(dev))
Chris Wilsonc5d1b512010-11-29 18:00:23 +00005754 has_lvds = intel_lvds_init(dev);
5755 if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
5756 /* disable the panel fitter on everything but LVDS */
5757 I915_WRITE(PFIT_CONTROL, 0);
5758 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005759
Eric Anholtbad720f2009-10-22 16:11:14 -07005760 if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -04005761 dpd_is_edp = intel_dpd_is_edp(dev);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08005762
Chris Wilson4d302442010-12-14 19:21:29 +00005763 if (has_edp_a(dev))
Zhenyu Wang32f9d652009-07-24 01:00:32 +08005764 intel_dp_init(dev, DP_A);
5765
Adam Jacksoncb0953d2010-07-16 14:46:29 -04005766 if (dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
5767 intel_dp_init(dev, PCH_DP_D);
5768 }
5769
5770 intel_crt_init(dev);
5771
5772 if (HAS_PCH_SPLIT(dev)) {
5773 int found;
5774
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08005775 if (I915_READ(HDMIB) & PORT_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +08005776 /* PCH SDVOB multiplex with HDMIB */
5777 found = intel_sdvo_init(dev, PCH_SDVOB);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08005778 if (!found)
5779 intel_hdmi_init(dev, HDMIB);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08005780 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
5781 intel_dp_init(dev, PCH_DP_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08005782 }
5783
5784 if (I915_READ(HDMIC) & PORT_DETECTED)
5785 intel_hdmi_init(dev, HDMIC);
5786
5787 if (I915_READ(HDMID) & PORT_DETECTED)
5788 intel_hdmi_init(dev, HDMID);
5789
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08005790 if (I915_READ(PCH_DP_C) & DP_DETECTED)
5791 intel_dp_init(dev, PCH_DP_C);
5792
Adam Jacksoncb0953d2010-07-16 14:46:29 -04005793 if (!dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08005794 intel_dp_init(dev, PCH_DP_D);
5795
Zhenyu Wang103a1962009-11-27 11:44:36 +08005796 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +08005797 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -08005798
Eric Anholt725e30a2009-01-22 13:01:02 -08005799 if (I915_READ(SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005800 DRM_DEBUG_KMS("probing SDVOB\n");
Eric Anholt725e30a2009-01-22 13:01:02 -08005801 found = intel_sdvo_init(dev, SDVOB);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005802 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
5803 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Eric Anholt725e30a2009-01-22 13:01:02 -08005804 intel_hdmi_init(dev, SDVOB);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005805 }
Ma Ling27185ae2009-08-24 13:50:23 +08005806
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005807 if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
5808 DRM_DEBUG_KMS("probing DP_B\n");
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005809 intel_dp_init(dev, DP_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005810 }
Eric Anholt725e30a2009-01-22 13:01:02 -08005811 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -04005812
5813 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -04005814
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005815 if (I915_READ(SDVOB) & SDVO_DETECTED) {
5816 DRM_DEBUG_KMS("probing SDVOC\n");
Eric Anholt725e30a2009-01-22 13:01:02 -08005817 found = intel_sdvo_init(dev, SDVOC);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005818 }
Ma Ling27185ae2009-08-24 13:50:23 +08005819
5820 if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
5821
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005822 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
5823 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Eric Anholt725e30a2009-01-22 13:01:02 -08005824 intel_hdmi_init(dev, SDVOC);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005825 }
5826 if (SUPPORTS_INTEGRATED_DP(dev)) {
5827 DRM_DEBUG_KMS("probing DP_C\n");
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005828 intel_dp_init(dev, DP_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005829 }
Eric Anholt725e30a2009-01-22 13:01:02 -08005830 }
Ma Ling27185ae2009-08-24 13:50:23 +08005831
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005832 if (SUPPORTS_INTEGRATED_DP(dev) &&
5833 (I915_READ(DP_D) & DP_DETECTED)) {
5834 DRM_DEBUG_KMS("probing DP_D\n");
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005835 intel_dp_init(dev, DP_D);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005836 }
Eric Anholtbad720f2009-10-22 16:11:14 -07005837 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08005838 intel_dvo_init(dev);
5839
Zhenyu Wang103a1962009-11-27 11:44:36 +08005840 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08005841 intel_tv_init(dev);
5842
Chris Wilson4ef69c72010-09-09 15:14:28 +01005843 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
5844 encoder->base.possible_crtcs = encoder->crtc_mask;
5845 encoder->base.possible_clones =
5846 intel_encoder_clones(dev, encoder->clone_mask);
Jesse Barnes79e53942008-11-07 14:24:08 -08005847 }
Chris Wilson47356eb2011-01-11 17:06:04 +00005848
5849 intel_panel_setup_backlight(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08005850}
5851
5852static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
5853{
5854 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08005855
5856 drm_framebuffer_cleanup(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00005857 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08005858
5859 kfree(intel_fb);
5860}
5861
5862static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +00005863 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08005864 unsigned int *handle)
5865{
5866 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00005867 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08005868
Chris Wilson05394f32010-11-08 19:18:58 +00005869 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -08005870}
5871
5872static const struct drm_framebuffer_funcs intel_fb_funcs = {
5873 .destroy = intel_user_framebuffer_destroy,
5874 .create_handle = intel_user_framebuffer_create_handle,
5875};
5876
Dave Airlie38651672010-03-30 05:34:13 +00005877int intel_framebuffer_init(struct drm_device *dev,
5878 struct intel_framebuffer *intel_fb,
5879 struct drm_mode_fb_cmd *mode_cmd,
Chris Wilson05394f32010-11-08 19:18:58 +00005880 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -08005881{
Jesse Barnes79e53942008-11-07 14:24:08 -08005882 int ret;
5883
Chris Wilson05394f32010-11-08 19:18:58 +00005884 if (obj->tiling_mode == I915_TILING_Y)
Chris Wilson57cd6502010-08-08 12:34:44 +01005885 return -EINVAL;
5886
5887 if (mode_cmd->pitch & 63)
5888 return -EINVAL;
5889
5890 switch (mode_cmd->bpp) {
5891 case 8:
5892 case 16:
5893 case 24:
5894 case 32:
5895 break;
5896 default:
5897 return -EINVAL;
5898 }
5899
Jesse Barnes79e53942008-11-07 14:24:08 -08005900 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
5901 if (ret) {
5902 DRM_ERROR("framebuffer init failed %d\n", ret);
5903 return ret;
5904 }
5905
5906 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
Jesse Barnes79e53942008-11-07 14:24:08 -08005907 intel_fb->obj = obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08005908 return 0;
5909}
5910
Jesse Barnes79e53942008-11-07 14:24:08 -08005911static struct drm_framebuffer *
5912intel_user_framebuffer_create(struct drm_device *dev,
5913 struct drm_file *filp,
5914 struct drm_mode_fb_cmd *mode_cmd)
5915{
Chris Wilson05394f32010-11-08 19:18:58 +00005916 struct drm_i915_gem_object *obj;
Dave Airlie38651672010-03-30 05:34:13 +00005917 struct intel_framebuffer *intel_fb;
Jesse Barnes79e53942008-11-07 14:24:08 -08005918 int ret;
5919
Chris Wilson05394f32010-11-08 19:18:58 +00005920 obj = to_intel_bo(drm_gem_object_lookup(dev, filp, mode_cmd->handle));
Jesse Barnes79e53942008-11-07 14:24:08 -08005921 if (!obj)
Chris Wilsoncce13ff2010-08-08 13:36:38 +01005922 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -08005923
Dave Airlie38651672010-03-30 05:34:13 +00005924 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
5925 if (!intel_fb)
Chris Wilsoncce13ff2010-08-08 13:36:38 +01005926 return ERR_PTR(-ENOMEM);
Dave Airlie38651672010-03-30 05:34:13 +00005927
Chris Wilson05394f32010-11-08 19:18:58 +00005928 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08005929 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00005930 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie38651672010-03-30 05:34:13 +00005931 kfree(intel_fb);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01005932 return ERR_PTR(ret);
Jesse Barnes79e53942008-11-07 14:24:08 -08005933 }
5934
Dave Airlie38651672010-03-30 05:34:13 +00005935 return &intel_fb->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08005936}
5937
Jesse Barnes79e53942008-11-07 14:24:08 -08005938static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -08005939 .fb_create = intel_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +00005940 .output_poll_changed = intel_fb_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -08005941};
5942
Chris Wilson05394f32010-11-08 19:18:58 +00005943static struct drm_i915_gem_object *
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08005944intel_alloc_context_page(struct drm_device *dev)
Chris Wilson9ea8d052010-01-04 18:57:56 +00005945{
Chris Wilson05394f32010-11-08 19:18:58 +00005946 struct drm_i915_gem_object *ctx;
Chris Wilson9ea8d052010-01-04 18:57:56 +00005947 int ret;
5948
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08005949 ctx = i915_gem_alloc_object(dev, 4096);
5950 if (!ctx) {
Chris Wilson9ea8d052010-01-04 18:57:56 +00005951 DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
5952 return NULL;
5953 }
5954
5955 mutex_lock(&dev->struct_mutex);
Daniel Vetter75e9e912010-11-04 17:11:09 +01005956 ret = i915_gem_object_pin(ctx, 4096, true);
Chris Wilson9ea8d052010-01-04 18:57:56 +00005957 if (ret) {
5958 DRM_ERROR("failed to pin power context: %d\n", ret);
5959 goto err_unref;
5960 }
5961
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08005962 ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
Chris Wilson9ea8d052010-01-04 18:57:56 +00005963 if (ret) {
5964 DRM_ERROR("failed to set-domain on power context: %d\n", ret);
5965 goto err_unpin;
5966 }
5967 mutex_unlock(&dev->struct_mutex);
5968
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08005969 return ctx;
Chris Wilson9ea8d052010-01-04 18:57:56 +00005970
5971err_unpin:
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08005972 i915_gem_object_unpin(ctx);
Chris Wilson9ea8d052010-01-04 18:57:56 +00005973err_unref:
Chris Wilson05394f32010-11-08 19:18:58 +00005974 drm_gem_object_unreference(&ctx->base);
Chris Wilson9ea8d052010-01-04 18:57:56 +00005975 mutex_unlock(&dev->struct_mutex);
5976 return NULL;
5977}
5978
Jesse Barnes7648fa92010-05-20 14:28:11 -07005979bool ironlake_set_drps(struct drm_device *dev, u8 val)
5980{
5981 struct drm_i915_private *dev_priv = dev->dev_private;
5982 u16 rgvswctl;
5983
5984 rgvswctl = I915_READ16(MEMSWCTL);
5985 if (rgvswctl & MEMCTL_CMD_STS) {
5986 DRM_DEBUG("gpu busy, RCS change rejected\n");
5987 return false; /* still busy with another command */
5988 }
5989
5990 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
5991 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
5992 I915_WRITE16(MEMSWCTL, rgvswctl);
5993 POSTING_READ16(MEMSWCTL);
5994
5995 rgvswctl |= MEMCTL_CMD_STS;
5996 I915_WRITE16(MEMSWCTL, rgvswctl);
5997
5998 return true;
5999}
6000
Jesse Barnesf97108d2010-01-29 11:27:07 -08006001void ironlake_enable_drps(struct drm_device *dev)
6002{
6003 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7648fa92010-05-20 14:28:11 -07006004 u32 rgvmodectl = I915_READ(MEMMODECTL);
Jesse Barnesf97108d2010-01-29 11:27:07 -08006005 u8 fmax, fmin, fstart, vstart;
Jesse Barnesf97108d2010-01-29 11:27:07 -08006006
Jesse Barnesea056c12010-09-10 10:02:13 -07006007 /* Enable temp reporting */
6008 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
6009 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
6010
Jesse Barnesf97108d2010-01-29 11:27:07 -08006011 /* 100ms RC evaluation intervals */
6012 I915_WRITE(RCUPEI, 100000);
6013 I915_WRITE(RCDNEI, 100000);
6014
6015 /* Set max/min thresholds to 90ms and 80ms respectively */
6016 I915_WRITE(RCBMAXAVG, 90000);
6017 I915_WRITE(RCBMINAVG, 80000);
6018
6019 I915_WRITE(MEMIHYST, 1);
6020
6021 /* Set up min, max, and cur for interrupt handling */
6022 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
6023 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
6024 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
6025 MEMMODE_FSTART_SHIFT;
Jesse Barnes7648fa92010-05-20 14:28:11 -07006026
Jesse Barnesf97108d2010-01-29 11:27:07 -08006027 vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
6028 PXVFREQ_PX_SHIFT;
6029
Jesse Barnes80dbf4b2010-11-01 14:12:01 -07006030 dev_priv->fmax = fmax; /* IPS callback will increase this */
Jesse Barnes7648fa92010-05-20 14:28:11 -07006031 dev_priv->fstart = fstart;
6032
Jesse Barnes80dbf4b2010-11-01 14:12:01 -07006033 dev_priv->max_delay = fstart;
Jesse Barnesf97108d2010-01-29 11:27:07 -08006034 dev_priv->min_delay = fmin;
6035 dev_priv->cur_delay = fstart;
6036
Jesse Barnes80dbf4b2010-11-01 14:12:01 -07006037 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
6038 fmax, fmin, fstart);
Jesse Barnes7648fa92010-05-20 14:28:11 -07006039
Jesse Barnesf97108d2010-01-29 11:27:07 -08006040 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
6041
6042 /*
6043 * Interrupts will be enabled in ironlake_irq_postinstall
6044 */
6045
6046 I915_WRITE(VIDSTART, vstart);
6047 POSTING_READ(VIDSTART);
6048
6049 rgvmodectl |= MEMMODE_SWMODE_EN;
6050 I915_WRITE(MEMMODECTL, rgvmodectl);
6051
Chris Wilson481b6af2010-08-23 17:43:35 +01006052 if (wait_for((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Chris Wilson913d8d12010-08-07 11:01:35 +01006053 DRM_ERROR("stuck trying to change perf mode\n");
Jesse Barnesf97108d2010-01-29 11:27:07 -08006054 msleep(1);
6055
Jesse Barnes7648fa92010-05-20 14:28:11 -07006056 ironlake_set_drps(dev, fstart);
Jesse Barnesf97108d2010-01-29 11:27:07 -08006057
Jesse Barnes7648fa92010-05-20 14:28:11 -07006058 dev_priv->last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
6059 I915_READ(0x112e0);
6060 dev_priv->last_time1 = jiffies_to_msecs(jiffies);
6061 dev_priv->last_count2 = I915_READ(0x112f4);
6062 getrawmonotonic(&dev_priv->last_time2);
Jesse Barnesf97108d2010-01-29 11:27:07 -08006063}
6064
6065void ironlake_disable_drps(struct drm_device *dev)
6066{
6067 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7648fa92010-05-20 14:28:11 -07006068 u16 rgvswctl = I915_READ16(MEMSWCTL);
Jesse Barnesf97108d2010-01-29 11:27:07 -08006069
6070 /* Ack interrupts, disable EFC interrupt */
6071 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
6072 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
6073 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
6074 I915_WRITE(DEIIR, DE_PCU_EVENT);
6075 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
6076
6077 /* Go back to the starting frequency */
Jesse Barnes7648fa92010-05-20 14:28:11 -07006078 ironlake_set_drps(dev, dev_priv->fstart);
Jesse Barnesf97108d2010-01-29 11:27:07 -08006079 msleep(1);
6080 rgvswctl |= MEMCTL_CMD_STS;
6081 I915_WRITE(MEMSWCTL, rgvswctl);
6082 msleep(1);
6083
6084}
6085
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08006086void gen6_set_rps(struct drm_device *dev, u8 val)
6087{
6088 struct drm_i915_private *dev_priv = dev->dev_private;
6089 u32 swreq;
6090
6091 swreq = (val & 0x3ff) << 25;
6092 I915_WRITE(GEN6_RPNSWREQ, swreq);
6093}
6094
6095void gen6_disable_rps(struct drm_device *dev)
6096{
6097 struct drm_i915_private *dev_priv = dev->dev_private;
6098
6099 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
6100 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
6101 I915_WRITE(GEN6_PMIER, 0);
6102 I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
6103}
6104
Jesse Barnes7648fa92010-05-20 14:28:11 -07006105static unsigned long intel_pxfreq(u32 vidfreq)
6106{
6107 unsigned long freq;
6108 int div = (vidfreq & 0x3f0000) >> 16;
6109 int post = (vidfreq & 0x3000) >> 12;
6110 int pre = (vidfreq & 0x7);
6111
6112 if (!pre)
6113 return 0;
6114
6115 freq = ((div * 133333) / ((1<<post) * pre));
6116
6117 return freq;
6118}
6119
6120void intel_init_emon(struct drm_device *dev)
6121{
6122 struct drm_i915_private *dev_priv = dev->dev_private;
6123 u32 lcfuse;
6124 u8 pxw[16];
6125 int i;
6126
6127 /* Disable to program */
6128 I915_WRITE(ECR, 0);
6129 POSTING_READ(ECR);
6130
6131 /* Program energy weights for various events */
6132 I915_WRITE(SDEW, 0x15040d00);
6133 I915_WRITE(CSIEW0, 0x007f0000);
6134 I915_WRITE(CSIEW1, 0x1e220004);
6135 I915_WRITE(CSIEW2, 0x04000004);
6136
6137 for (i = 0; i < 5; i++)
6138 I915_WRITE(PEW + (i * 4), 0);
6139 for (i = 0; i < 3; i++)
6140 I915_WRITE(DEW + (i * 4), 0);
6141
6142 /* Program P-state weights to account for frequency power adjustment */
6143 for (i = 0; i < 16; i++) {
6144 u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
6145 unsigned long freq = intel_pxfreq(pxvidfreq);
6146 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
6147 PXVFREQ_PX_SHIFT;
6148 unsigned long val;
6149
6150 val = vid * vid;
6151 val *= (freq / 1000);
6152 val *= 255;
6153 val /= (127*127*900);
6154 if (val > 0xff)
6155 DRM_ERROR("bad pxval: %ld\n", val);
6156 pxw[i] = val;
6157 }
6158 /* Render standby states get 0 weight */
6159 pxw[14] = 0;
6160 pxw[15] = 0;
6161
6162 for (i = 0; i < 4; i++) {
6163 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
6164 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
6165 I915_WRITE(PXW + (i * 4), val);
6166 }
6167
6168 /* Adjust magic regs to magic values (more experimental results) */
6169 I915_WRITE(OGW0, 0);
6170 I915_WRITE(OGW1, 0);
6171 I915_WRITE(EG0, 0x00007f00);
6172 I915_WRITE(EG1, 0x0000000e);
6173 I915_WRITE(EG2, 0x000e0000);
6174 I915_WRITE(EG3, 0x68000300);
6175 I915_WRITE(EG4, 0x42000000);
6176 I915_WRITE(EG5, 0x00140031);
6177 I915_WRITE(EG6, 0);
6178 I915_WRITE(EG7, 0);
6179
6180 for (i = 0; i < 8; i++)
6181 I915_WRITE(PXWL + (i * 4), 0);
6182
6183 /* Enable PMON + select events */
6184 I915_WRITE(ECR, 0x80000019);
6185
6186 lcfuse = I915_READ(LCFUSE02);
6187
6188 dev_priv->corr = (lcfuse & LCFUSE_HIV_MASK);
6189}
6190
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08006191void gen6_enable_rps(struct drm_i915_private *dev_priv)
Chris Wilson8fd26852010-12-08 18:40:43 +00006192{
Jesse Barnesa6044e22010-12-20 11:34:20 -08006193 u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
6194 u32 gt_perf_status = I915_READ(GEN6_GT_PERF_STATUS);
6195 u32 pcu_mbox;
6196 int cur_freq, min_freq, max_freq;
Chris Wilson8fd26852010-12-08 18:40:43 +00006197 int i;
6198
6199 /* Here begins a magic sequence of register writes to enable
6200 * auto-downclocking.
6201 *
6202 * Perhaps there might be some value in exposing these to
6203 * userspace...
6204 */
6205 I915_WRITE(GEN6_RC_STATE, 0);
6206 __gen6_force_wake_get(dev_priv);
6207
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08006208 /* disable the counters and set deterministic thresholds */
Chris Wilson8fd26852010-12-08 18:40:43 +00006209 I915_WRITE(GEN6_RC_CONTROL, 0);
6210
6211 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
6212 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
6213 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
6214 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
6215 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
6216
6217 for (i = 0; i < I915_NUM_RINGS; i++)
6218 I915_WRITE(RING_MAX_IDLE(dev_priv->ring[i].mmio_base), 10);
6219
6220 I915_WRITE(GEN6_RC_SLEEP, 0);
6221 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
6222 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
6223 I915_WRITE(GEN6_RC6p_THRESHOLD, 100000);
6224 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
6225
6226 I915_WRITE(GEN6_RC_CONTROL,
6227 GEN6_RC_CTL_RC6p_ENABLE |
6228 GEN6_RC_CTL_RC6_ENABLE |
Chris Wilson9c3d2f72010-12-17 10:54:26 +00006229 GEN6_RC_CTL_EI_MODE(1) |
Chris Wilson8fd26852010-12-08 18:40:43 +00006230 GEN6_RC_CTL_HW_ENABLE);
6231
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08006232 I915_WRITE(GEN6_RPNSWREQ,
Chris Wilson8fd26852010-12-08 18:40:43 +00006233 GEN6_FREQUENCY(10) |
6234 GEN6_OFFSET(0) |
6235 GEN6_AGGRESSIVE_TURBO);
6236 I915_WRITE(GEN6_RC_VIDEO_FREQ,
6237 GEN6_FREQUENCY(12));
6238
6239 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
6240 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
6241 18 << 24 |
6242 6 << 16);
6243 I915_WRITE(GEN6_RP_UP_THRESHOLD, 90000);
6244 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 100000);
6245 I915_WRITE(GEN6_RP_UP_EI, 100000);
6246 I915_WRITE(GEN6_RP_DOWN_EI, 300000);
6247 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
6248 I915_WRITE(GEN6_RP_CONTROL,
6249 GEN6_RP_MEDIA_TURBO |
6250 GEN6_RP_USE_NORMAL_FREQ |
6251 GEN6_RP_MEDIA_IS_GFX |
6252 GEN6_RP_ENABLE |
6253 GEN6_RP_UP_BUSY_MAX |
6254 GEN6_RP_DOWN_BUSY_MIN);
6255
6256 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6257 500))
6258 DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
6259
6260 I915_WRITE(GEN6_PCODE_DATA, 0);
6261 I915_WRITE(GEN6_PCODE_MAILBOX,
6262 GEN6_PCODE_READY |
6263 GEN6_PCODE_WRITE_MIN_FREQ_TABLE);
6264 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6265 500))
6266 DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
6267
Jesse Barnesa6044e22010-12-20 11:34:20 -08006268 min_freq = (rp_state_cap & 0xff0000) >> 16;
6269 max_freq = rp_state_cap & 0xff;
6270 cur_freq = (gt_perf_status & 0xff00) >> 8;
6271
6272 /* Check for overclock support */
6273 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6274 500))
6275 DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
6276 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_READ_OC_PARAMS);
6277 pcu_mbox = I915_READ(GEN6_PCODE_DATA);
6278 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6279 500))
6280 DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
6281 if (pcu_mbox & (1<<31)) { /* OC supported */
6282 max_freq = pcu_mbox & 0xff;
6283 DRM_DEBUG_DRIVER("overclocking supported, adjusting frequency max to %dMHz\n", pcu_mbox * 100);
6284 }
6285
6286 /* In units of 100MHz */
6287 dev_priv->max_delay = max_freq;
6288 dev_priv->min_delay = min_freq;
6289 dev_priv->cur_delay = cur_freq;
6290
Chris Wilson8fd26852010-12-08 18:40:43 +00006291 /* requires MSI enabled */
6292 I915_WRITE(GEN6_PMIER,
6293 GEN6_PM_MBOX_EVENT |
6294 GEN6_PM_THERMAL_EVENT |
6295 GEN6_PM_RP_DOWN_TIMEOUT |
6296 GEN6_PM_RP_UP_THRESHOLD |
6297 GEN6_PM_RP_DOWN_THRESHOLD |
6298 GEN6_PM_RP_UP_EI_EXPIRED |
6299 GEN6_PM_RP_DOWN_EI_EXPIRED);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08006300 I915_WRITE(GEN6_PMIMR, 0);
6301 /* enable all PM interrupts */
6302 I915_WRITE(GEN6_PMINTRMSK, 0);
Chris Wilson8fd26852010-12-08 18:40:43 +00006303
6304 __gen6_force_wake_put(dev_priv);
6305}
6306
Chris Wilson0cdab212010-12-05 17:27:06 +00006307void intel_enable_clock_gating(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07006308{
6309 struct drm_i915_private *dev_priv = dev->dev_private;
6310
6311 /*
6312 * Disable clock gating reported to work incorrectly according to the
6313 * specs, but enable as much else as we can.
6314 */
Eric Anholtbad720f2009-10-22 16:11:14 -07006315 if (HAS_PCH_SPLIT(dev)) {
Eric Anholt8956c8b2010-03-18 13:21:14 -07006316 uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
6317
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01006318 if (IS_GEN5(dev)) {
Eric Anholt8956c8b2010-03-18 13:21:14 -07006319 /* Required for FBC */
Jesse Barnes1ffa3252011-01-17 13:35:57 -08006320 dspclk_gate |= DPFCUNIT_CLOCK_GATE_DISABLE |
6321 DPFCRUNIT_CLOCK_GATE_DISABLE |
6322 DPFDUNIT_CLOCK_GATE_DISABLE;
Eric Anholt8956c8b2010-03-18 13:21:14 -07006323 /* Required for CxSR */
6324 dspclk_gate |= DPARBUNIT_CLOCK_GATE_DISABLE;
6325
6326 I915_WRITE(PCH_3DCGDIS0,
6327 MARIUNIT_CLOCK_GATE_DISABLE |
6328 SVSMUNIT_CLOCK_GATE_DISABLE);
Eric Anholt06f37752010-12-14 10:06:46 -08006329 I915_WRITE(PCH_3DCGDIS1,
6330 VFMUNIT_CLOCK_GATE_DISABLE);
Eric Anholt8956c8b2010-03-18 13:21:14 -07006331 }
6332
6333 I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08006334
6335 /*
Jesse Barnes382b0932010-10-07 16:01:25 -07006336 * On Ibex Peak and Cougar Point, we need to disable clock
6337 * gating for the panel power sequencer or it will fail to
6338 * start up when no ports are active.
6339 */
6340 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
6341
6342 /*
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08006343 * According to the spec the following bits should be set in
6344 * order to enable memory self-refresh
6345 * The bit 22/21 of 0x42004
6346 * The bit 5 of 0x42020
6347 * The bit 15 of 0x45000
6348 */
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01006349 if (IS_GEN5(dev)) {
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08006350 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6351 (I915_READ(ILK_DISPLAY_CHICKEN2) |
6352 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
6353 I915_WRITE(ILK_DSPCLK_GATE,
6354 (I915_READ(ILK_DSPCLK_GATE) |
6355 ILK_DPARB_CLK_GATE));
6356 I915_WRITE(DISP_ARB_CTL,
6357 (I915_READ(DISP_ARB_CTL) |
6358 DISP_FBC_WM_DIS));
Yuanhan Liu13982612010-12-15 15:42:31 +08006359 I915_WRITE(WM3_LP_ILK, 0);
6360 I915_WRITE(WM2_LP_ILK, 0);
6361 I915_WRITE(WM1_LP_ILK, 0);
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08006362 }
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08006363 /*
6364 * Based on the document from hardware guys the following bits
6365 * should be set unconditionally in order to enable FBC.
6366 * The bit 22 of 0x42000
6367 * The bit 22 of 0x42004
6368 * The bit 7,8,9 of 0x42020.
6369 */
6370 if (IS_IRONLAKE_M(dev)) {
6371 I915_WRITE(ILK_DISPLAY_CHICKEN1,
6372 I915_READ(ILK_DISPLAY_CHICKEN1) |
6373 ILK_FBCQ_DIS);
6374 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6375 I915_READ(ILK_DISPLAY_CHICKEN2) |
6376 ILK_DPARB_GATE);
6377 I915_WRITE(ILK_DSPCLK_GATE,
6378 I915_READ(ILK_DSPCLK_GATE) |
6379 ILK_DPFC_DIS1 |
6380 ILK_DPFC_DIS2 |
6381 ILK_CLK_FBC);
6382 }
Eric Anholtde6e2ea2010-11-06 14:53:32 -07006383
Eric Anholt67e92af2010-11-06 14:53:33 -07006384 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6385 I915_READ(ILK_DISPLAY_CHICKEN2) |
6386 ILK_ELPIN_409_SELECT);
6387
Eric Anholtde6e2ea2010-11-06 14:53:32 -07006388 if (IS_GEN5(dev)) {
6389 I915_WRITE(_3D_CHICKEN2,
6390 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
6391 _3D_CHICKEN2_WM_READ_PIPELINED);
6392 }
Chris Wilson8fd26852010-12-08 18:40:43 +00006393
Yuanhan Liu13982612010-12-15 15:42:31 +08006394 if (IS_GEN6(dev)) {
6395 I915_WRITE(WM3_LP_ILK, 0);
6396 I915_WRITE(WM2_LP_ILK, 0);
6397 I915_WRITE(WM1_LP_ILK, 0);
6398
6399 /*
6400 * According to the spec the following bits should be
6401 * set in order to enable memory self-refresh and fbc:
6402 * The bit21 and bit22 of 0x42000
6403 * The bit21 and bit22 of 0x42004
6404 * The bit5 and bit7 of 0x42020
6405 * The bit14 of 0x70180
6406 * The bit14 of 0x71180
6407 */
6408 I915_WRITE(ILK_DISPLAY_CHICKEN1,
6409 I915_READ(ILK_DISPLAY_CHICKEN1) |
6410 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
6411 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6412 I915_READ(ILK_DISPLAY_CHICKEN2) |
6413 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
6414 I915_WRITE(ILK_DSPCLK_GATE,
6415 I915_READ(ILK_DSPCLK_GATE) |
6416 ILK_DPARB_CLK_GATE |
6417 ILK_DPFD_CLK_GATE);
6418
6419 I915_WRITE(DSPACNTR,
6420 I915_READ(DSPACNTR) |
6421 DISPPLANE_TRICKLE_FEED_DISABLE);
6422 I915_WRITE(DSPBCNTR,
6423 I915_READ(DSPBCNTR) |
6424 DISPPLANE_TRICKLE_FEED_DISABLE);
6425 }
Zhenyu Wangc03342f2009-09-29 11:01:23 +08006426 } else if (IS_G4X(dev)) {
Jesse Barnes652c3932009-08-17 13:31:43 -07006427 uint32_t dspclk_gate;
6428 I915_WRITE(RENCLK_GATE_D1, 0);
6429 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
6430 GS_UNIT_CLOCK_GATE_DISABLE |
6431 CL_UNIT_CLOCK_GATE_DISABLE);
6432 I915_WRITE(RAMCLK_GATE_D, 0);
6433 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
6434 OVRUNIT_CLOCK_GATE_DISABLE |
6435 OVCUNIT_CLOCK_GATE_DISABLE;
6436 if (IS_GM45(dev))
6437 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
6438 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006439 } else if (IS_CRESTLINE(dev)) {
Jesse Barnes652c3932009-08-17 13:31:43 -07006440 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
6441 I915_WRITE(RENCLK_GATE_D2, 0);
6442 I915_WRITE(DSPCLK_GATE_D, 0);
6443 I915_WRITE(RAMCLK_GATE_D, 0);
6444 I915_WRITE16(DEUC, 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006445 } else if (IS_BROADWATER(dev)) {
Jesse Barnes652c3932009-08-17 13:31:43 -07006446 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
6447 I965_RCC_CLOCK_GATE_DISABLE |
6448 I965_RCPB_CLOCK_GATE_DISABLE |
6449 I965_ISC_CLOCK_GATE_DISABLE |
6450 I965_FBC_CLOCK_GATE_DISABLE);
6451 I915_WRITE(RENCLK_GATE_D2, 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006452 } else if (IS_GEN3(dev)) {
Jesse Barnes652c3932009-08-17 13:31:43 -07006453 u32 dstate = I915_READ(D_STATE);
6454
6455 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
6456 DSTATE_DOT_CLOCK_GATING;
6457 I915_WRITE(D_STATE, dstate);
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +02006458 } else if (IS_I85X(dev) || IS_I865G(dev)) {
Jesse Barnes652c3932009-08-17 13:31:43 -07006459 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
6460 } else if (IS_I830(dev)) {
6461 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
6462 }
6463}
6464
Chris Wilsonac668082011-02-09 16:15:32 +00006465static void ironlake_teardown_rc6(struct drm_device *dev)
Chris Wilson0cdab212010-12-05 17:27:06 +00006466{
6467 struct drm_i915_private *dev_priv = dev->dev_private;
6468
6469 if (dev_priv->renderctx) {
Chris Wilsonac668082011-02-09 16:15:32 +00006470 i915_gem_object_unpin(dev_priv->renderctx);
6471 drm_gem_object_unreference(&dev_priv->renderctx->base);
Chris Wilson0cdab212010-12-05 17:27:06 +00006472 dev_priv->renderctx = NULL;
6473 }
6474
6475 if (dev_priv->pwrctx) {
Chris Wilsonac668082011-02-09 16:15:32 +00006476 i915_gem_object_unpin(dev_priv->pwrctx);
6477 drm_gem_object_unreference(&dev_priv->pwrctx->base);
Chris Wilson0cdab212010-12-05 17:27:06 +00006478 dev_priv->pwrctx = NULL;
6479 }
6480}
6481
Jesse Barnesd5bb0812011-01-05 12:01:26 -08006482static void ironlake_disable_rc6(struct drm_device *dev)
6483{
6484 struct drm_i915_private *dev_priv = dev->dev_private;
6485
Chris Wilsonac668082011-02-09 16:15:32 +00006486 if (I915_READ(PWRCTXA)) {
6487 /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
6488 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
6489 wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
6490 50);
6491
6492 I915_WRITE(PWRCTXA, 0);
6493 POSTING_READ(PWRCTXA);
6494
6495 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
6496 POSTING_READ(RSTDBYCTL);
6497 }
6498
6499 ironlake_disable_rc6(dev);
6500}
6501
6502static int ironlake_setup_rc6(struct drm_device *dev)
6503{
6504 struct drm_i915_private *dev_priv = dev->dev_private;
6505
6506 if (dev_priv->renderctx == NULL)
6507 dev_priv->renderctx = intel_alloc_context_page(dev);
6508 if (!dev_priv->renderctx)
6509 return -ENOMEM;
6510
6511 if (dev_priv->pwrctx == NULL)
6512 dev_priv->pwrctx = intel_alloc_context_page(dev);
6513 if (!dev_priv->pwrctx) {
6514 ironlake_teardown_rc6(dev);
6515 return -ENOMEM;
6516 }
6517
6518 return 0;
Jesse Barnesd5bb0812011-01-05 12:01:26 -08006519}
6520
6521void ironlake_enable_rc6(struct drm_device *dev)
6522{
6523 struct drm_i915_private *dev_priv = dev->dev_private;
6524 int ret;
6525
Chris Wilsonac668082011-02-09 16:15:32 +00006526 /* rc6 disabled by default due to repeated reports of hanging during
6527 * boot and resume.
6528 */
6529 if (!i915_enable_rc6)
6530 return;
6531
6532 ret = ironlake_setup_rc6(dev);
6533 if (ret)
6534 return;
6535
Jesse Barnesd5bb0812011-01-05 12:01:26 -08006536 /*
6537 * GPU can automatically power down the render unit if given a page
6538 * to save state.
6539 */
6540 ret = BEGIN_LP_RING(6);
6541 if (ret) {
Chris Wilsonac668082011-02-09 16:15:32 +00006542 ironlake_teardown_rc6(dev);
Jesse Barnesd5bb0812011-01-05 12:01:26 -08006543 return;
6544 }
Chris Wilsonac668082011-02-09 16:15:32 +00006545
Jesse Barnesd5bb0812011-01-05 12:01:26 -08006546 OUT_RING(MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
6547 OUT_RING(MI_SET_CONTEXT);
6548 OUT_RING(dev_priv->renderctx->gtt_offset |
6549 MI_MM_SPACE_GTT |
6550 MI_SAVE_EXT_STATE_EN |
6551 MI_RESTORE_EXT_STATE_EN |
6552 MI_RESTORE_INHIBIT);
6553 OUT_RING(MI_SUSPEND_FLUSH);
6554 OUT_RING(MI_NOOP);
6555 OUT_RING(MI_FLUSH);
6556 ADVANCE_LP_RING();
6557
6558 I915_WRITE(PWRCTXA, dev_priv->pwrctx->gtt_offset | PWRCTX_EN);
6559 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
6560}
6561
Chris Wilsonac668082011-02-09 16:15:32 +00006562
Jesse Barnese70236a2009-09-21 10:42:27 -07006563/* Set up chip specific display functions */
6564static void intel_init_display(struct drm_device *dev)
6565{
6566 struct drm_i915_private *dev_priv = dev->dev_private;
6567
6568 /* We always want a DPMS function */
Eric Anholtbad720f2009-10-22 16:11:14 -07006569 if (HAS_PCH_SPLIT(dev))
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006570 dev_priv->display.dpms = ironlake_crtc_dpms;
Jesse Barnese70236a2009-09-21 10:42:27 -07006571 else
6572 dev_priv->display.dpms = i9xx_crtc_dpms;
6573
Adam Jacksonee5382a2010-04-23 11:17:39 -04006574 if (I915_HAS_FBC(dev)) {
Yuanhan Liu9c04f012010-12-15 15:42:32 +08006575 if (HAS_PCH_SPLIT(dev)) {
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08006576 dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
6577 dev_priv->display.enable_fbc = ironlake_enable_fbc;
6578 dev_priv->display.disable_fbc = ironlake_disable_fbc;
6579 } else if (IS_GM45(dev)) {
Jesse Barnes74dff282009-09-14 15:39:40 -07006580 dev_priv->display.fbc_enabled = g4x_fbc_enabled;
6581 dev_priv->display.enable_fbc = g4x_enable_fbc;
6582 dev_priv->display.disable_fbc = g4x_disable_fbc;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006583 } else if (IS_CRESTLINE(dev)) {
Jesse Barnese70236a2009-09-21 10:42:27 -07006584 dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
6585 dev_priv->display.enable_fbc = i8xx_enable_fbc;
6586 dev_priv->display.disable_fbc = i8xx_disable_fbc;
6587 }
Jesse Barnes74dff282009-09-14 15:39:40 -07006588 /* 855GM needs testing */
Jesse Barnese70236a2009-09-21 10:42:27 -07006589 }
6590
6591 /* Returns the core display clock speed */
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006592 if (IS_I945G(dev) || (IS_G33(dev) && ! IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -07006593 dev_priv->display.get_display_clock_speed =
6594 i945_get_display_clock_speed;
6595 else if (IS_I915G(dev))
6596 dev_priv->display.get_display_clock_speed =
6597 i915_get_display_clock_speed;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006598 else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07006599 dev_priv->display.get_display_clock_speed =
6600 i9xx_misc_get_display_clock_speed;
6601 else if (IS_I915GM(dev))
6602 dev_priv->display.get_display_clock_speed =
6603 i915gm_get_display_clock_speed;
6604 else if (IS_I865G(dev))
6605 dev_priv->display.get_display_clock_speed =
6606 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +02006607 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07006608 dev_priv->display.get_display_clock_speed =
6609 i855_get_display_clock_speed;
6610 else /* 852, 830 */
6611 dev_priv->display.get_display_clock_speed =
6612 i830_get_display_clock_speed;
6613
6614 /* For FIFO watermark updates */
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08006615 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01006616 if (IS_GEN5(dev)) {
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08006617 if (I915_READ(MLTR_ILK) & ILK_SRLT_MASK)
6618 dev_priv->display.update_wm = ironlake_update_wm;
6619 else {
6620 DRM_DEBUG_KMS("Failed to get proper latency. "
6621 "Disable CxSR\n");
6622 dev_priv->display.update_wm = NULL;
6623 }
Yuanhan Liu13982612010-12-15 15:42:31 +08006624 } else if (IS_GEN6(dev)) {
6625 if (SNB_READ_WM0_LATENCY()) {
6626 dev_priv->display.update_wm = sandybridge_update_wm;
6627 } else {
6628 DRM_DEBUG_KMS("Failed to read display plane latency. "
6629 "Disable CxSR\n");
6630 dev_priv->display.update_wm = NULL;
6631 }
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08006632 } else
6633 dev_priv->display.update_wm = NULL;
6634 } else if (IS_PINEVIEW(dev)) {
Zhao Yakuid4294342010-03-22 22:45:36 +08006635 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
Li Peng95534262010-05-18 18:58:44 +08006636 dev_priv->is_ddr3,
Zhao Yakuid4294342010-03-22 22:45:36 +08006637 dev_priv->fsb_freq,
6638 dev_priv->mem_freq)) {
6639 DRM_INFO("failed to find known CxSR latency "
Li Peng95534262010-05-18 18:58:44 +08006640 "(found ddr%s fsb freq %d, mem freq %d), "
Zhao Yakuid4294342010-03-22 22:45:36 +08006641 "disabling CxSR\n",
Li Peng95534262010-05-18 18:58:44 +08006642 (dev_priv->is_ddr3 == 1) ? "3": "2",
Zhao Yakuid4294342010-03-22 22:45:36 +08006643 dev_priv->fsb_freq, dev_priv->mem_freq);
6644 /* Disable CxSR and never update its watermark again */
6645 pineview_disable_cxsr(dev);
6646 dev_priv->display.update_wm = NULL;
6647 } else
6648 dev_priv->display.update_wm = pineview_update_wm;
6649 } else if (IS_G4X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07006650 dev_priv->display.update_wm = g4x_update_wm;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006651 else if (IS_GEN4(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07006652 dev_priv->display.update_wm = i965_update_wm;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006653 else if (IS_GEN3(dev)) {
Jesse Barnese70236a2009-09-21 10:42:27 -07006654 dev_priv->display.update_wm = i9xx_update_wm;
6655 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Adam Jackson8f4695e2010-04-16 18:20:57 -04006656 } else if (IS_I85X(dev)) {
6657 dev_priv->display.update_wm = i9xx_update_wm;
6658 dev_priv->display.get_fifo_size = i85x_get_fifo_size;
Jesse Barnese70236a2009-09-21 10:42:27 -07006659 } else {
Adam Jackson8f4695e2010-04-16 18:20:57 -04006660 dev_priv->display.update_wm = i830_update_wm;
6661 if (IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07006662 dev_priv->display.get_fifo_size = i845_get_fifo_size;
6663 else
6664 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Jesse Barnese70236a2009-09-21 10:42:27 -07006665 }
6666}
6667
Jesse Barnesb690e962010-07-19 13:53:12 -07006668/*
6669 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
6670 * resume, or other times. This quirk makes sure that's the case for
6671 * affected systems.
6672 */
6673static void quirk_pipea_force (struct drm_device *dev)
6674{
6675 struct drm_i915_private *dev_priv = dev->dev_private;
6676
6677 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
6678 DRM_DEBUG_DRIVER("applying pipe a force quirk\n");
6679}
6680
6681struct intel_quirk {
6682 int device;
6683 int subsystem_vendor;
6684 int subsystem_device;
6685 void (*hook)(struct drm_device *dev);
6686};
6687
6688struct intel_quirk intel_quirks[] = {
6689 /* HP Compaq 2730p needs pipe A force quirk (LP: #291555) */
6690 { 0x2a42, 0x103c, 0x30eb, quirk_pipea_force },
6691 /* HP Mini needs pipe A force quirk (LP: #322104) */
6692 { 0x27ae,0x103c, 0x361a, quirk_pipea_force },
6693
6694 /* Thinkpad R31 needs pipe A force quirk */
6695 { 0x3577, 0x1014, 0x0505, quirk_pipea_force },
6696 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
6697 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
6698
6699 /* ThinkPad X30 needs pipe A force quirk (LP: #304614) */
6700 { 0x3577, 0x1014, 0x0513, quirk_pipea_force },
6701 /* ThinkPad X40 needs pipe A force quirk */
6702
6703 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
6704 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
6705
6706 /* 855 & before need to leave pipe A & dpll A up */
6707 { 0x3582, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
6708 { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
6709};
6710
6711static void intel_init_quirks(struct drm_device *dev)
6712{
6713 struct pci_dev *d = dev->pdev;
6714 int i;
6715
6716 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
6717 struct intel_quirk *q = &intel_quirks[i];
6718
6719 if (d->device == q->device &&
6720 (d->subsystem_vendor == q->subsystem_vendor ||
6721 q->subsystem_vendor == PCI_ANY_ID) &&
6722 (d->subsystem_device == q->subsystem_device ||
6723 q->subsystem_device == PCI_ANY_ID))
6724 q->hook(dev);
6725 }
6726}
6727
Jesse Barnes9cce37f2010-08-13 15:11:26 -07006728/* Disable the VGA plane that we never use */
6729static void i915_disable_vga(struct drm_device *dev)
6730{
6731 struct drm_i915_private *dev_priv = dev->dev_private;
6732 u8 sr1;
6733 u32 vga_reg;
6734
6735 if (HAS_PCH_SPLIT(dev))
6736 vga_reg = CPU_VGACNTRL;
6737 else
6738 vga_reg = VGACNTRL;
6739
6740 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
6741 outb(1, VGA_SR_INDEX);
6742 sr1 = inb(VGA_SR_DATA);
6743 outb(sr1 | 1<<5, VGA_SR_DATA);
6744 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
6745 udelay(300);
6746
6747 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
6748 POSTING_READ(vga_reg);
6749}
6750
Jesse Barnes79e53942008-11-07 14:24:08 -08006751void intel_modeset_init(struct drm_device *dev)
6752{
Jesse Barnes652c3932009-08-17 13:31:43 -07006753 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08006754 int i;
6755
6756 drm_mode_config_init(dev);
6757
6758 dev->mode_config.min_width = 0;
6759 dev->mode_config.min_height = 0;
6760
6761 dev->mode_config.funcs = (void *)&intel_mode_funcs;
6762
Jesse Barnesb690e962010-07-19 13:53:12 -07006763 intel_init_quirks(dev);
6764
Jesse Barnese70236a2009-09-21 10:42:27 -07006765 intel_init_display(dev);
6766
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006767 if (IS_GEN2(dev)) {
6768 dev->mode_config.max_width = 2048;
6769 dev->mode_config.max_height = 2048;
6770 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -07006771 dev->mode_config.max_width = 4096;
6772 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -08006773 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006774 dev->mode_config.max_width = 8192;
6775 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -08006776 }
Chris Wilson35c30472010-12-22 14:07:12 +00006777 dev->mode_config.fb_base = dev->agp->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08006778
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006779 if (IS_MOBILE(dev) || !IS_GEN2(dev))
Dave Airliea3524f12010-06-06 18:59:41 +10006780 dev_priv->num_pipe = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -08006781 else
Dave Airliea3524f12010-06-06 18:59:41 +10006782 dev_priv->num_pipe = 1;
Zhao Yakui28c97732009-10-09 11:39:41 +08006783 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Dave Airliea3524f12010-06-06 18:59:41 +10006784 dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -08006785
Dave Airliea3524f12010-06-06 18:59:41 +10006786 for (i = 0; i < dev_priv->num_pipe; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006787 intel_crtc_init(dev, i);
6788 }
6789
6790 intel_setup_outputs(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -07006791
Chris Wilson0cdab212010-12-05 17:27:06 +00006792 intel_enable_clock_gating(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -07006793
Jesse Barnes9cce37f2010-08-13 15:11:26 -07006794 /* Just disable it once at startup */
6795 i915_disable_vga(dev);
6796
Jesse Barnes7648fa92010-05-20 14:28:11 -07006797 if (IS_IRONLAKE_M(dev)) {
Jesse Barnesf97108d2010-01-29 11:27:07 -08006798 ironlake_enable_drps(dev);
Jesse Barnes7648fa92010-05-20 14:28:11 -07006799 intel_init_emon(dev);
6800 }
Jesse Barnesf97108d2010-01-29 11:27:07 -08006801
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08006802 if (IS_GEN6(dev))
6803 gen6_enable_rps(dev_priv);
6804
Chris Wilsonac668082011-02-09 16:15:32 +00006805 if (IS_IRONLAKE_M(dev))
Jesse Barnesd5bb0812011-01-05 12:01:26 -08006806 ironlake_enable_rc6(dev);
Jesse Barnesd5bb0812011-01-05 12:01:26 -08006807
Jesse Barnes652c3932009-08-17 13:31:43 -07006808 INIT_WORK(&dev_priv->idle_work, intel_idle_update);
6809 setup_timer(&dev_priv->idle_timer, intel_gpu_idle_timer,
6810 (unsigned long)dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02006811
6812 intel_setup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006813}
6814
6815void intel_modeset_cleanup(struct drm_device *dev)
6816{
Jesse Barnes652c3932009-08-17 13:31:43 -07006817 struct drm_i915_private *dev_priv = dev->dev_private;
6818 struct drm_crtc *crtc;
6819 struct intel_crtc *intel_crtc;
6820
Keith Packardf87ea762010-10-03 19:36:26 -07006821 drm_kms_helper_poll_fini(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -07006822 mutex_lock(&dev->struct_mutex);
6823
Jesse Barnes723bfd72010-10-07 16:01:13 -07006824 intel_unregister_dsm_handler();
6825
6826
Jesse Barnes652c3932009-08-17 13:31:43 -07006827 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
6828 /* Skip inactive CRTCs */
6829 if (!crtc->fb)
6830 continue;
6831
6832 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3dec0092010-08-20 21:40:52 +02006833 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07006834 }
6835
Jesse Barnese70236a2009-09-21 10:42:27 -07006836 if (dev_priv->display.disable_fbc)
6837 dev_priv->display.disable_fbc(dev);
6838
Jesse Barnesf97108d2010-01-29 11:27:07 -08006839 if (IS_IRONLAKE_M(dev))
6840 ironlake_disable_drps(dev);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08006841 if (IS_GEN6(dev))
6842 gen6_disable_rps(dev);
Jesse Barnesf97108d2010-01-29 11:27:07 -08006843
Jesse Barnesd5bb0812011-01-05 12:01:26 -08006844 if (IS_IRONLAKE_M(dev))
6845 ironlake_disable_rc6(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +00006846
Kristian Høgsberg69341a52009-11-11 12:19:17 -05006847 mutex_unlock(&dev->struct_mutex);
6848
Daniel Vetter6c0d93502010-08-20 18:26:46 +02006849 /* Disable the irq before mode object teardown, for the irq might
6850 * enqueue unpin/hotplug work. */
6851 drm_irq_uninstall(dev);
6852 cancel_work_sync(&dev_priv->hotplug_work);
6853
Daniel Vetter3dec0092010-08-20 21:40:52 +02006854 /* Shut off idle work before the crtcs get freed. */
6855 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
6856 intel_crtc = to_intel_crtc(crtc);
6857 del_timer_sync(&intel_crtc->idle_timer);
6858 }
6859 del_timer_sync(&dev_priv->idle_timer);
6860 cancel_work_sync(&dev_priv->idle_work);
6861
Jesse Barnes79e53942008-11-07 14:24:08 -08006862 drm_mode_config_cleanup(dev);
6863}
6864
Dave Airlie28d52042009-09-21 14:33:58 +10006865/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +08006866 * Return which encoder is currently attached for connector.
6867 */
Chris Wilsondf0e9242010-09-09 16:20:55 +01006868struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -08006869{
Chris Wilsondf0e9242010-09-09 16:20:55 +01006870 return &intel_attached_encoder(connector)->base;
6871}
Jesse Barnes79e53942008-11-07 14:24:08 -08006872
Chris Wilsondf0e9242010-09-09 16:20:55 +01006873void intel_connector_attach_encoder(struct intel_connector *connector,
6874 struct intel_encoder *encoder)
6875{
6876 connector->encoder = encoder;
6877 drm_mode_connector_attach_encoder(&connector->base,
6878 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08006879}
Dave Airlie28d52042009-09-21 14:33:58 +10006880
6881/*
6882 * set vga decode state - true == enable VGA decode
6883 */
6884int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
6885{
6886 struct drm_i915_private *dev_priv = dev->dev_private;
6887 u16 gmch_ctrl;
6888
6889 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
6890 if (state)
6891 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
6892 else
6893 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
6894 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
6895 return 0;
6896}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00006897
6898#ifdef CONFIG_DEBUG_FS
6899#include <linux/seq_file.h>
6900
6901struct intel_display_error_state {
6902 struct intel_cursor_error_state {
6903 u32 control;
6904 u32 position;
6905 u32 base;
6906 u32 size;
6907 } cursor[2];
6908
6909 struct intel_pipe_error_state {
6910 u32 conf;
6911 u32 source;
6912
6913 u32 htotal;
6914 u32 hblank;
6915 u32 hsync;
6916 u32 vtotal;
6917 u32 vblank;
6918 u32 vsync;
6919 } pipe[2];
6920
6921 struct intel_plane_error_state {
6922 u32 control;
6923 u32 stride;
6924 u32 size;
6925 u32 pos;
6926 u32 addr;
6927 u32 surface;
6928 u32 tile_offset;
6929 } plane[2];
6930};
6931
6932struct intel_display_error_state *
6933intel_display_capture_error_state(struct drm_device *dev)
6934{
6935 drm_i915_private_t *dev_priv = dev->dev_private;
6936 struct intel_display_error_state *error;
6937 int i;
6938
6939 error = kmalloc(sizeof(*error), GFP_ATOMIC);
6940 if (error == NULL)
6941 return NULL;
6942
6943 for (i = 0; i < 2; i++) {
6944 error->cursor[i].control = I915_READ(CURCNTR(i));
6945 error->cursor[i].position = I915_READ(CURPOS(i));
6946 error->cursor[i].base = I915_READ(CURBASE(i));
6947
6948 error->plane[i].control = I915_READ(DSPCNTR(i));
6949 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
6950 error->plane[i].size = I915_READ(DSPSIZE(i));
6951 error->plane[i].pos= I915_READ(DSPPOS(i));
6952 error->plane[i].addr = I915_READ(DSPADDR(i));
6953 if (INTEL_INFO(dev)->gen >= 4) {
6954 error->plane[i].surface = I915_READ(DSPSURF(i));
6955 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
6956 }
6957
6958 error->pipe[i].conf = I915_READ(PIPECONF(i));
6959 error->pipe[i].source = I915_READ(PIPESRC(i));
6960 error->pipe[i].htotal = I915_READ(HTOTAL(i));
6961 error->pipe[i].hblank = I915_READ(HBLANK(i));
6962 error->pipe[i].hsync = I915_READ(HSYNC(i));
6963 error->pipe[i].vtotal = I915_READ(VTOTAL(i));
6964 error->pipe[i].vblank = I915_READ(VBLANK(i));
6965 error->pipe[i].vsync = I915_READ(VSYNC(i));
6966 }
6967
6968 return error;
6969}
6970
6971void
6972intel_display_print_error_state(struct seq_file *m,
6973 struct drm_device *dev,
6974 struct intel_display_error_state *error)
6975{
6976 int i;
6977
6978 for (i = 0; i < 2; i++) {
6979 seq_printf(m, "Pipe [%d]:\n", i);
6980 seq_printf(m, " CONF: %08x\n", error->pipe[i].conf);
6981 seq_printf(m, " SRC: %08x\n", error->pipe[i].source);
6982 seq_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
6983 seq_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
6984 seq_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
6985 seq_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
6986 seq_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
6987 seq_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
6988
6989 seq_printf(m, "Plane [%d]:\n", i);
6990 seq_printf(m, " CNTR: %08x\n", error->plane[i].control);
6991 seq_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
6992 seq_printf(m, " SIZE: %08x\n", error->plane[i].size);
6993 seq_printf(m, " POS: %08x\n", error->plane[i].pos);
6994 seq_printf(m, " ADDR: %08x\n", error->plane[i].addr);
6995 if (INTEL_INFO(dev)->gen >= 4) {
6996 seq_printf(m, " SURF: %08x\n", error->plane[i].surface);
6997 seq_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
6998 }
6999
7000 seq_printf(m, "Cursor [%d]:\n", i);
7001 seq_printf(m, " CNTR: %08x\n", error->cursor[i].control);
7002 seq_printf(m, " POS: %08x\n", error->cursor[i].position);
7003 seq_printf(m, " BASE: %08x\n", error->cursor[i].base);
7004 }
7005}
7006#endif