blob: 607bd2fd21b9fd0798c9024b2963f0b1360becff [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 Barnesb24e7172011-01-04 15:09:30 -08001061static const char *state_string(bool enabled)
1062{
1063 return enabled ? "on" : "off";
1064}
1065
1066/* Only for pre-ILK configs */
1067static void assert_pll(struct drm_i915_private *dev_priv,
1068 enum pipe pipe, bool state)
1069{
1070 int reg;
1071 u32 val;
1072 bool cur_state;
1073
1074 reg = DPLL(pipe);
1075 val = I915_READ(reg);
1076 cur_state = !!(val & DPLL_VCO_ENABLE);
1077 WARN(cur_state != state,
1078 "PLL state assertion failure (expected %s, current %s)\n",
1079 state_string(state), state_string(cur_state));
1080}
1081#define assert_pll_enabled(d, p) assert_pll(d, p, true)
1082#define assert_pll_disabled(d, p) assert_pll(d, p, false)
1083
Jesse Barnesea0760c2011-01-04 15:09:32 -08001084static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1085 enum pipe pipe)
1086{
1087 int pp_reg, lvds_reg;
1088 u32 val;
1089 enum pipe panel_pipe = PIPE_A;
1090 bool locked = locked;
1091
1092 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1093 pp_reg = PCH_PP_CONTROL;
1094 lvds_reg = PCH_LVDS;
1095 } else {
1096 pp_reg = PP_CONTROL;
1097 lvds_reg = LVDS;
1098 }
1099
1100 val = I915_READ(pp_reg);
1101 if (!(val & PANEL_POWER_ON) ||
1102 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1103 locked = false;
1104
1105 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1106 panel_pipe = PIPE_B;
1107
1108 WARN(panel_pipe == pipe && locked,
1109 "panel assertion failure, pipe %c regs locked\n",
1110 pipe ? 'B' : 'A');
1111}
1112
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001113static void assert_pipe(struct drm_i915_private *dev_priv,
1114 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001115{
1116 int reg;
1117 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001118 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001119
1120 reg = PIPECONF(pipe);
1121 val = I915_READ(reg);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001122 cur_state = !!(val & PIPECONF_ENABLE);
1123 WARN(cur_state != state,
1124 "pipe %c assertion failure (expected %s, current %s)\n",
1125 pipe ? 'B' : 'A', state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001126}
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001127#define assert_pipe_enabled(d, p) assert_pipe(d, p, true)
1128#define assert_pipe_disabled(d, p) assert_pipe(d, p, false)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001129
1130static void assert_plane_enabled(struct drm_i915_private *dev_priv,
1131 enum plane plane)
1132{
1133 int reg;
1134 u32 val;
1135
1136 reg = DSPCNTR(plane);
1137 val = I915_READ(reg);
1138 WARN(!(val & DISPLAY_PLANE_ENABLE),
1139 "plane %c assertion failure, should be active but is disabled\n",
1140 plane ? 'B' : 'A');
1141}
1142
1143static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1144 enum pipe pipe)
1145{
1146 int reg, i;
1147 u32 val;
1148 int cur_pipe;
1149
1150 /* Need to check both planes against the pipe */
1151 for (i = 0; i < 2; i++) {
1152 reg = DSPCNTR(i);
1153 val = I915_READ(reg);
1154 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1155 DISPPLANE_SEL_PIPE_SHIFT;
1156 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
1157 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1158 i, pipe ? 'B' : 'A');
1159 }
1160}
1161
1162/**
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001163 * intel_enable_pll - enable a PLL
1164 * @dev_priv: i915 private structure
1165 * @pipe: pipe PLL to enable
1166 *
1167 * Enable @pipe's PLL so we can start pumping pixels from a plane. Check to
1168 * make sure the PLL reg is writable first though, since the panel write
1169 * protect mechanism may be enabled.
1170 *
1171 * Note! This is for pre-ILK only.
1172 */
1173static void intel_enable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1174{
1175 int reg;
1176 u32 val;
1177
1178 /* No really, not for ILK+ */
1179 BUG_ON(dev_priv->info->gen >= 5);
1180
1181 /* PLL is protected by panel, make sure we can write it */
1182 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
1183 assert_panel_unlocked(dev_priv, pipe);
1184
1185 reg = DPLL(pipe);
1186 val = I915_READ(reg);
1187 val |= DPLL_VCO_ENABLE;
1188
1189 /* We do this three times for luck */
1190 I915_WRITE(reg, val);
1191 POSTING_READ(reg);
1192 udelay(150); /* wait for warmup */
1193 I915_WRITE(reg, val);
1194 POSTING_READ(reg);
1195 udelay(150); /* wait for warmup */
1196 I915_WRITE(reg, val);
1197 POSTING_READ(reg);
1198 udelay(150); /* wait for warmup */
1199}
1200
1201/**
1202 * intel_disable_pll - disable a PLL
1203 * @dev_priv: i915 private structure
1204 * @pipe: pipe PLL to disable
1205 *
1206 * Disable the PLL for @pipe, making sure the pipe is off first.
1207 *
1208 * Note! This is for pre-ILK only.
1209 */
1210static void intel_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1211{
1212 int reg;
1213 u32 val;
1214
1215 /* Don't disable pipe A or pipe A PLLs if needed */
1216 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1217 return;
1218
1219 /* Make sure the pipe isn't still relying on us */
1220 assert_pipe_disabled(dev_priv, pipe);
1221
1222 reg = DPLL(pipe);
1223 val = I915_READ(reg);
1224 val &= ~DPLL_VCO_ENABLE;
1225 I915_WRITE(reg, val);
1226 POSTING_READ(reg);
1227}
1228
1229/**
Jesse Barnesb24e7172011-01-04 15:09:30 -08001230 * intel_enable_pipe - enable a pipe, assertiing requirements
1231 * @dev_priv: i915 private structure
1232 * @pipe: pipe to enable
1233 *
1234 * Enable @pipe, making sure that various hardware specific requirements
1235 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1236 *
1237 * @pipe should be %PIPE_A or %PIPE_B.
1238 *
1239 * Will wait until the pipe is actually running (i.e. first vblank) before
1240 * returning.
1241 */
1242static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
1243{
1244 int reg;
1245 u32 val;
1246
1247 /*
1248 * A pipe without a PLL won't actually be able to drive bits from
1249 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1250 * need the check.
1251 */
1252 if (!HAS_PCH_SPLIT(dev_priv->dev))
1253 assert_pll_enabled(dev_priv, pipe);
1254
1255 reg = PIPECONF(pipe);
1256 val = I915_READ(reg);
1257 val |= PIPECONF_ENABLE;
1258 I915_WRITE(reg, val);
1259 POSTING_READ(reg);
1260 intel_wait_for_vblank(dev_priv->dev, pipe);
1261}
1262
1263/**
1264 * intel_disable_pipe - disable a pipe, assertiing requirements
1265 * @dev_priv: i915 private structure
1266 * @pipe: pipe to disable
1267 *
1268 * Disable @pipe, making sure that various hardware specific requirements
1269 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1270 *
1271 * @pipe should be %PIPE_A or %PIPE_B.
1272 *
1273 * Will wait until the pipe has shut down before returning.
1274 */
1275static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1276 enum pipe pipe)
1277{
1278 int reg;
1279 u32 val;
1280
1281 /*
1282 * Make sure planes won't keep trying to pump pixels to us,
1283 * or we might hang the display.
1284 */
1285 assert_planes_disabled(dev_priv, pipe);
1286
1287 /* Don't disable pipe A or pipe A PLLs if needed */
1288 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1289 return;
1290
1291 reg = PIPECONF(pipe);
1292 val = I915_READ(reg);
1293 val &= ~PIPECONF_ENABLE;
1294 I915_WRITE(reg, val);
1295 POSTING_READ(reg);
1296 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1297}
1298
1299/**
1300 * intel_enable_plane - enable a display plane on a given pipe
1301 * @dev_priv: i915 private structure
1302 * @plane: plane to enable
1303 * @pipe: pipe being fed
1304 *
1305 * Enable @plane on @pipe, making sure that @pipe is running first.
1306 */
1307static void intel_enable_plane(struct drm_i915_private *dev_priv,
1308 enum plane plane, enum pipe pipe)
1309{
1310 int reg;
1311 u32 val;
1312
1313 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1314 assert_pipe_enabled(dev_priv, pipe);
1315
1316 reg = DSPCNTR(plane);
1317 val = I915_READ(reg);
1318 val |= DISPLAY_PLANE_ENABLE;
1319 I915_WRITE(reg, val);
1320 POSTING_READ(reg);
1321 intel_wait_for_vblank(dev_priv->dev, pipe);
1322}
1323
1324/*
1325 * Plane regs are double buffered, going from enabled->disabled needs a
1326 * trigger in order to latch. The display address reg provides this.
1327 */
1328static void intel_flush_display_plane(struct drm_i915_private *dev_priv,
1329 enum plane plane)
1330{
1331 u32 reg = DSPADDR(plane);
1332 I915_WRITE(reg, I915_READ(reg));
1333}
1334
1335/**
1336 * intel_disable_plane - disable a display plane
1337 * @dev_priv: i915 private structure
1338 * @plane: plane to disable
1339 * @pipe: pipe consuming the data
1340 *
1341 * Disable @plane; should be an independent operation.
1342 */
1343static void intel_disable_plane(struct drm_i915_private *dev_priv,
1344 enum plane plane, enum pipe pipe)
1345{
1346 int reg;
1347 u32 val;
1348
1349 reg = DSPCNTR(plane);
1350 val = I915_READ(reg);
1351 val &= ~DISPLAY_PLANE_ENABLE;
1352 I915_WRITE(reg, val);
1353 POSTING_READ(reg);
1354 intel_flush_display_plane(dev_priv, plane);
1355 intel_wait_for_vblank(dev_priv->dev, pipe);
1356}
1357
Jesse Barnes80824002009-09-10 15:28:06 -07001358static void i8xx_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
1359{
1360 struct drm_device *dev = crtc->dev;
1361 struct drm_i915_private *dev_priv = dev->dev_private;
1362 struct drm_framebuffer *fb = crtc->fb;
1363 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00001364 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes80824002009-09-10 15:28:06 -07001365 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1366 int plane, i;
1367 u32 fbc_ctl, fbc_ctl2;
1368
Chris Wilsonbed4a672010-09-11 10:47:47 +01001369 if (fb->pitch == dev_priv->cfb_pitch &&
Chris Wilson05394f32010-11-08 19:18:58 +00001370 obj->fence_reg == dev_priv->cfb_fence &&
Chris Wilsonbed4a672010-09-11 10:47:47 +01001371 intel_crtc->plane == dev_priv->cfb_plane &&
1372 I915_READ(FBC_CONTROL) & FBC_CTL_EN)
1373 return;
1374
1375 i8xx_disable_fbc(dev);
1376
Jesse Barnes80824002009-09-10 15:28:06 -07001377 dev_priv->cfb_pitch = dev_priv->cfb_size / FBC_LL_SIZE;
1378
1379 if (fb->pitch < dev_priv->cfb_pitch)
1380 dev_priv->cfb_pitch = fb->pitch;
1381
1382 /* FBC_CTL wants 64B units */
1383 dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
Chris Wilson05394f32010-11-08 19:18:58 +00001384 dev_priv->cfb_fence = obj->fence_reg;
Jesse Barnes80824002009-09-10 15:28:06 -07001385 dev_priv->cfb_plane = intel_crtc->plane;
1386 plane = dev_priv->cfb_plane == 0 ? FBC_CTL_PLANEA : FBC_CTL_PLANEB;
1387
1388 /* Clear old tags */
1389 for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
1390 I915_WRITE(FBC_TAG + (i * 4), 0);
1391
1392 /* Set it up... */
1393 fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | plane;
Chris Wilson05394f32010-11-08 19:18:58 +00001394 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes80824002009-09-10 15:28:06 -07001395 fbc_ctl2 |= FBC_CTL_CPU_FENCE;
1396 I915_WRITE(FBC_CONTROL2, fbc_ctl2);
1397 I915_WRITE(FBC_FENCE_OFF, crtc->y);
1398
1399 /* enable it... */
1400 fbc_ctl = FBC_CTL_EN | FBC_CTL_PERIODIC;
Jesse Barnesee25df22010-02-06 10:41:53 -08001401 if (IS_I945GM(dev))
Priit Laes49677902010-03-02 11:37:00 +02001402 fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
Jesse Barnes80824002009-09-10 15:28:06 -07001403 fbc_ctl |= (dev_priv->cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
1404 fbc_ctl |= (interval & 0x2fff) << FBC_CTL_INTERVAL_SHIFT;
Chris Wilson05394f32010-11-08 19:18:58 +00001405 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes80824002009-09-10 15:28:06 -07001406 fbc_ctl |= dev_priv->cfb_fence;
1407 I915_WRITE(FBC_CONTROL, fbc_ctl);
1408
Zhao Yakui28c97732009-10-09 11:39:41 +08001409 DRM_DEBUG_KMS("enabled FBC, pitch %ld, yoff %d, plane %d, ",
Chris Wilson5eddb702010-09-11 13:48:45 +01001410 dev_priv->cfb_pitch, crtc->y, dev_priv->cfb_plane);
Jesse Barnes80824002009-09-10 15:28:06 -07001411}
1412
1413void i8xx_disable_fbc(struct drm_device *dev)
1414{
1415 struct drm_i915_private *dev_priv = dev->dev_private;
1416 u32 fbc_ctl;
1417
1418 /* Disable compression */
1419 fbc_ctl = I915_READ(FBC_CONTROL);
Chris Wilsona5cad622010-09-22 13:15:10 +01001420 if ((fbc_ctl & FBC_CTL_EN) == 0)
1421 return;
1422
Jesse Barnes80824002009-09-10 15:28:06 -07001423 fbc_ctl &= ~FBC_CTL_EN;
1424 I915_WRITE(FBC_CONTROL, fbc_ctl);
1425
1426 /* Wait for compressing bit to clear */
Chris Wilson481b6af2010-08-23 17:43:35 +01001427 if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
Chris Wilson913d8d12010-08-07 11:01:35 +01001428 DRM_DEBUG_KMS("FBC idle timed out\n");
1429 return;
Jesse Barnes9517a922010-05-21 09:40:45 -07001430 }
Jesse Barnes80824002009-09-10 15:28:06 -07001431
Zhao Yakui28c97732009-10-09 11:39:41 +08001432 DRM_DEBUG_KMS("disabled FBC\n");
Jesse Barnes80824002009-09-10 15:28:06 -07001433}
1434
Adam Jacksonee5382a2010-04-23 11:17:39 -04001435static bool i8xx_fbc_enabled(struct drm_device *dev)
Jesse Barnes80824002009-09-10 15:28:06 -07001436{
Jesse Barnes80824002009-09-10 15:28:06 -07001437 struct drm_i915_private *dev_priv = dev->dev_private;
1438
1439 return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
1440}
1441
Jesse Barnes74dff282009-09-14 15:39:40 -07001442static void g4x_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
1443{
1444 struct drm_device *dev = crtc->dev;
1445 struct drm_i915_private *dev_priv = dev->dev_private;
1446 struct drm_framebuffer *fb = crtc->fb;
1447 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00001448 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes74dff282009-09-14 15:39:40 -07001449 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5eddb702010-09-11 13:48:45 +01001450 int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
Jesse Barnes74dff282009-09-14 15:39:40 -07001451 unsigned long stall_watermark = 200;
1452 u32 dpfc_ctl;
1453
Chris Wilsonbed4a672010-09-11 10:47:47 +01001454 dpfc_ctl = I915_READ(DPFC_CONTROL);
1455 if (dpfc_ctl & DPFC_CTL_EN) {
1456 if (dev_priv->cfb_pitch == dev_priv->cfb_pitch / 64 - 1 &&
Chris Wilson05394f32010-11-08 19:18:58 +00001457 dev_priv->cfb_fence == obj->fence_reg &&
Chris Wilsonbed4a672010-09-11 10:47:47 +01001458 dev_priv->cfb_plane == intel_crtc->plane &&
1459 dev_priv->cfb_y == crtc->y)
1460 return;
1461
1462 I915_WRITE(DPFC_CONTROL, dpfc_ctl & ~DPFC_CTL_EN);
1463 POSTING_READ(DPFC_CONTROL);
1464 intel_wait_for_vblank(dev, intel_crtc->pipe);
1465 }
1466
Jesse Barnes74dff282009-09-14 15:39:40 -07001467 dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
Chris Wilson05394f32010-11-08 19:18:58 +00001468 dev_priv->cfb_fence = obj->fence_reg;
Jesse Barnes74dff282009-09-14 15:39:40 -07001469 dev_priv->cfb_plane = intel_crtc->plane;
Chris Wilsonbed4a672010-09-11 10:47:47 +01001470 dev_priv->cfb_y = crtc->y;
Jesse Barnes74dff282009-09-14 15:39:40 -07001471
1472 dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;
Chris Wilson05394f32010-11-08 19:18:58 +00001473 if (obj->tiling_mode != I915_TILING_NONE) {
Jesse Barnes74dff282009-09-14 15:39:40 -07001474 dpfc_ctl |= DPFC_CTL_FENCE_EN | dev_priv->cfb_fence;
1475 I915_WRITE(DPFC_CHICKEN, DPFC_HT_MODIFY);
1476 } else {
1477 I915_WRITE(DPFC_CHICKEN, ~DPFC_HT_MODIFY);
1478 }
1479
Jesse Barnes74dff282009-09-14 15:39:40 -07001480 I915_WRITE(DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
1481 (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
1482 (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
1483 I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
1484
1485 /* enable it... */
1486 I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);
1487
Zhao Yakui28c97732009-10-09 11:39:41 +08001488 DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
Jesse Barnes74dff282009-09-14 15:39:40 -07001489}
1490
1491void g4x_disable_fbc(struct drm_device *dev)
1492{
1493 struct drm_i915_private *dev_priv = dev->dev_private;
1494 u32 dpfc_ctl;
1495
1496 /* Disable compression */
1497 dpfc_ctl = I915_READ(DPFC_CONTROL);
Chris Wilsonbed4a672010-09-11 10:47:47 +01001498 if (dpfc_ctl & DPFC_CTL_EN) {
1499 dpfc_ctl &= ~DPFC_CTL_EN;
1500 I915_WRITE(DPFC_CONTROL, dpfc_ctl);
Jesse Barnes74dff282009-09-14 15:39:40 -07001501
Chris Wilsonbed4a672010-09-11 10:47:47 +01001502 DRM_DEBUG_KMS("disabled FBC\n");
1503 }
Jesse Barnes74dff282009-09-14 15:39:40 -07001504}
1505
Adam Jacksonee5382a2010-04-23 11:17:39 -04001506static bool g4x_fbc_enabled(struct drm_device *dev)
Jesse Barnes74dff282009-09-14 15:39:40 -07001507{
Jesse Barnes74dff282009-09-14 15:39:40 -07001508 struct drm_i915_private *dev_priv = dev->dev_private;
1509
1510 return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
1511}
1512
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001513static void ironlake_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
1514{
1515 struct drm_device *dev = crtc->dev;
1516 struct drm_i915_private *dev_priv = dev->dev_private;
1517 struct drm_framebuffer *fb = crtc->fb;
1518 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00001519 struct drm_i915_gem_object *obj = intel_fb->obj;
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001520 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5eddb702010-09-11 13:48:45 +01001521 int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001522 unsigned long stall_watermark = 200;
1523 u32 dpfc_ctl;
1524
Chris Wilsonbed4a672010-09-11 10:47:47 +01001525 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
1526 if (dpfc_ctl & DPFC_CTL_EN) {
1527 if (dev_priv->cfb_pitch == dev_priv->cfb_pitch / 64 - 1 &&
Chris Wilson05394f32010-11-08 19:18:58 +00001528 dev_priv->cfb_fence == obj->fence_reg &&
Chris Wilsonbed4a672010-09-11 10:47:47 +01001529 dev_priv->cfb_plane == intel_crtc->plane &&
Chris Wilson05394f32010-11-08 19:18:58 +00001530 dev_priv->cfb_offset == obj->gtt_offset &&
Chris Wilsonbed4a672010-09-11 10:47:47 +01001531 dev_priv->cfb_y == crtc->y)
1532 return;
1533
1534 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl & ~DPFC_CTL_EN);
1535 POSTING_READ(ILK_DPFC_CONTROL);
1536 intel_wait_for_vblank(dev, intel_crtc->pipe);
1537 }
1538
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001539 dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
Chris Wilson05394f32010-11-08 19:18:58 +00001540 dev_priv->cfb_fence = obj->fence_reg;
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001541 dev_priv->cfb_plane = intel_crtc->plane;
Chris Wilson05394f32010-11-08 19:18:58 +00001542 dev_priv->cfb_offset = obj->gtt_offset;
Chris Wilsonbed4a672010-09-11 10:47:47 +01001543 dev_priv->cfb_y = crtc->y;
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001544
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001545 dpfc_ctl &= DPFC_RESERVED;
1546 dpfc_ctl |= (plane | DPFC_CTL_LIMIT_1X);
Chris Wilson05394f32010-11-08 19:18:58 +00001547 if (obj->tiling_mode != I915_TILING_NONE) {
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001548 dpfc_ctl |= (DPFC_CTL_FENCE_EN | dev_priv->cfb_fence);
1549 I915_WRITE(ILK_DPFC_CHICKEN, DPFC_HT_MODIFY);
1550 } else {
1551 I915_WRITE(ILK_DPFC_CHICKEN, ~DPFC_HT_MODIFY);
1552 }
1553
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001554 I915_WRITE(ILK_DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
1555 (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
1556 (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
1557 I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
Chris Wilson05394f32010-11-08 19:18:58 +00001558 I915_WRITE(ILK_FBC_RT_BASE, obj->gtt_offset | ILK_FBC_RT_VALID);
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001559 /* enable it... */
Chris Wilsonbed4a672010-09-11 10:47:47 +01001560 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001561
Yuanhan Liu9c04f012010-12-15 15:42:32 +08001562 if (IS_GEN6(dev)) {
1563 I915_WRITE(SNB_DPFC_CTL_SA,
1564 SNB_CPU_FENCE_ENABLE | dev_priv->cfb_fence);
1565 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
1566 }
1567
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001568 DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
1569}
1570
1571void ironlake_disable_fbc(struct drm_device *dev)
1572{
1573 struct drm_i915_private *dev_priv = dev->dev_private;
1574 u32 dpfc_ctl;
1575
1576 /* Disable compression */
1577 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
Chris Wilsonbed4a672010-09-11 10:47:47 +01001578 if (dpfc_ctl & DPFC_CTL_EN) {
1579 dpfc_ctl &= ~DPFC_CTL_EN;
1580 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001581
Chris Wilsonbed4a672010-09-11 10:47:47 +01001582 DRM_DEBUG_KMS("disabled FBC\n");
1583 }
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001584}
1585
1586static bool ironlake_fbc_enabled(struct drm_device *dev)
1587{
1588 struct drm_i915_private *dev_priv = dev->dev_private;
1589
1590 return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
1591}
1592
Adam Jacksonee5382a2010-04-23 11:17:39 -04001593bool intel_fbc_enabled(struct drm_device *dev)
1594{
1595 struct drm_i915_private *dev_priv = dev->dev_private;
1596
1597 if (!dev_priv->display.fbc_enabled)
1598 return false;
1599
1600 return dev_priv->display.fbc_enabled(dev);
1601}
1602
1603void intel_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
1604{
1605 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
1606
1607 if (!dev_priv->display.enable_fbc)
1608 return;
1609
1610 dev_priv->display.enable_fbc(crtc, interval);
1611}
1612
1613void intel_disable_fbc(struct drm_device *dev)
1614{
1615 struct drm_i915_private *dev_priv = dev->dev_private;
1616
1617 if (!dev_priv->display.disable_fbc)
1618 return;
1619
1620 dev_priv->display.disable_fbc(dev);
1621}
1622
Jesse Barnes80824002009-09-10 15:28:06 -07001623/**
1624 * intel_update_fbc - enable/disable FBC as needed
Chris Wilsonbed4a672010-09-11 10:47:47 +01001625 * @dev: the drm_device
Jesse Barnes80824002009-09-10 15:28:06 -07001626 *
1627 * Set up the framebuffer compression hardware at mode set time. We
1628 * enable it if possible:
1629 * - plane A only (on pre-965)
1630 * - no pixel mulitply/line duplication
1631 * - no alpha buffer discard
1632 * - no dual wide
1633 * - framebuffer <= 2048 in width, 1536 in height
1634 *
1635 * We can't assume that any compression will take place (worst case),
1636 * so the compressed buffer has to be the same size as the uncompressed
1637 * one. It also must reside (along with the line length buffer) in
1638 * stolen memory.
1639 *
1640 * We need to enable/disable FBC on a global basis.
1641 */
Chris Wilsonbed4a672010-09-11 10:47:47 +01001642static void intel_update_fbc(struct drm_device *dev)
Jesse Barnes80824002009-09-10 15:28:06 -07001643{
Jesse Barnes80824002009-09-10 15:28:06 -07001644 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonbed4a672010-09-11 10:47:47 +01001645 struct drm_crtc *crtc = NULL, *tmp_crtc;
1646 struct intel_crtc *intel_crtc;
1647 struct drm_framebuffer *fb;
Jesse Barnes80824002009-09-10 15:28:06 -07001648 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00001649 struct drm_i915_gem_object *obj;
Jesse Barnes9c928d12010-07-23 15:20:00 -07001650
1651 DRM_DEBUG_KMS("\n");
Jesse Barnes80824002009-09-10 15:28:06 -07001652
1653 if (!i915_powersave)
1654 return;
1655
Adam Jacksonee5382a2010-04-23 11:17:39 -04001656 if (!I915_HAS_FBC(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07001657 return;
1658
Jesse Barnes80824002009-09-10 15:28:06 -07001659 /*
1660 * If FBC is already on, we just have to verify that we can
1661 * keep it that way...
1662 * Need to disable if:
Jesse Barnes9c928d12010-07-23 15:20:00 -07001663 * - more than one pipe is active
Jesse Barnes80824002009-09-10 15:28:06 -07001664 * - changing FBC params (stride, fence, mode)
1665 * - new fb is too large to fit in compressed buffer
1666 * - going to an unsupported config (interlace, pixel multiply, etc.)
1667 */
Jesse Barnes9c928d12010-07-23 15:20:00 -07001668 list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head) {
Chris Wilsonbed4a672010-09-11 10:47:47 +01001669 if (tmp_crtc->enabled) {
1670 if (crtc) {
1671 DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
1672 dev_priv->no_fbc_reason = FBC_MULTIPLE_PIPES;
1673 goto out_disable;
1674 }
1675 crtc = tmp_crtc;
1676 }
Jesse Barnes9c928d12010-07-23 15:20:00 -07001677 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01001678
1679 if (!crtc || crtc->fb == NULL) {
1680 DRM_DEBUG_KMS("no output, disabling\n");
1681 dev_priv->no_fbc_reason = FBC_NO_OUTPUT;
Jesse Barnes9c928d12010-07-23 15:20:00 -07001682 goto out_disable;
1683 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01001684
1685 intel_crtc = to_intel_crtc(crtc);
1686 fb = crtc->fb;
1687 intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00001688 obj = intel_fb->obj;
Chris Wilsonbed4a672010-09-11 10:47:47 +01001689
Chris Wilson05394f32010-11-08 19:18:58 +00001690 if (intel_fb->obj->base.size > dev_priv->cfb_size) {
Zhao Yakui28c97732009-10-09 11:39:41 +08001691 DRM_DEBUG_KMS("framebuffer too large, disabling "
Chris Wilson5eddb702010-09-11 13:48:45 +01001692 "compression\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08001693 dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
Jesse Barnes80824002009-09-10 15:28:06 -07001694 goto out_disable;
1695 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01001696 if ((crtc->mode.flags & DRM_MODE_FLAG_INTERLACE) ||
1697 (crtc->mode.flags & DRM_MODE_FLAG_DBLSCAN)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08001698 DRM_DEBUG_KMS("mode incompatible with compression, "
Chris Wilson5eddb702010-09-11 13:48:45 +01001699 "disabling\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08001700 dev_priv->no_fbc_reason = FBC_UNSUPPORTED_MODE;
Jesse Barnes80824002009-09-10 15:28:06 -07001701 goto out_disable;
1702 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01001703 if ((crtc->mode.hdisplay > 2048) ||
1704 (crtc->mode.vdisplay > 1536)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08001705 DRM_DEBUG_KMS("mode too large for compression, disabling\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08001706 dev_priv->no_fbc_reason = FBC_MODE_TOO_LARGE;
Jesse Barnes80824002009-09-10 15:28:06 -07001707 goto out_disable;
1708 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01001709 if ((IS_I915GM(dev) || IS_I945GM(dev)) && intel_crtc->plane != 0) {
Zhao Yakui28c97732009-10-09 11:39:41 +08001710 DRM_DEBUG_KMS("plane not 0, disabling compression\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08001711 dev_priv->no_fbc_reason = FBC_BAD_PLANE;
Jesse Barnes80824002009-09-10 15:28:06 -07001712 goto out_disable;
1713 }
Chris Wilson05394f32010-11-08 19:18:58 +00001714 if (obj->tiling_mode != I915_TILING_X) {
Zhao Yakui28c97732009-10-09 11:39:41 +08001715 DRM_DEBUG_KMS("framebuffer not tiled, disabling compression\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08001716 dev_priv->no_fbc_reason = FBC_NOT_TILED;
Jesse Barnes80824002009-09-10 15:28:06 -07001717 goto out_disable;
1718 }
1719
Jason Wesselc924b932010-08-05 09:22:32 -05001720 /* If the kernel debugger is active, always disable compression */
1721 if (in_dbg_master())
1722 goto out_disable;
1723
Chris Wilsonbed4a672010-09-11 10:47:47 +01001724 intel_enable_fbc(crtc, 500);
Jesse Barnes80824002009-09-10 15:28:06 -07001725 return;
1726
1727out_disable:
Jesse Barnes80824002009-09-10 15:28:06 -07001728 /* Multiple disables should be harmless */
Chris Wilsona9394062010-05-27 13:18:16 +01001729 if (intel_fbc_enabled(dev)) {
1730 DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
Adam Jacksonee5382a2010-04-23 11:17:39 -04001731 intel_disable_fbc(dev);
Chris Wilsona9394062010-05-27 13:18:16 +01001732 }
Jesse Barnes80824002009-09-10 15:28:06 -07001733}
1734
Chris Wilson127bd2a2010-07-23 23:32:05 +01001735int
Chris Wilson48b956c2010-09-14 12:50:34 +01001736intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00001737 struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00001738 struct intel_ring_buffer *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001739{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001740 u32 alignment;
1741 int ret;
1742
Chris Wilson05394f32010-11-08 19:18:58 +00001743 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001744 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01001745 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1746 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001747 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01001748 alignment = 4 * 1024;
1749 else
1750 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001751 break;
1752 case I915_TILING_X:
1753 /* pin() will align the object as required by fence */
1754 alignment = 0;
1755 break;
1756 case I915_TILING_Y:
1757 /* FIXME: Is this true? */
1758 DRM_ERROR("Y tiled not allowed for scan out buffers\n");
1759 return -EINVAL;
1760 default:
1761 BUG();
1762 }
1763
Daniel Vetter75e9e912010-11-04 17:11:09 +01001764 ret = i915_gem_object_pin(obj, alignment, true);
Chris Wilson48b956c2010-09-14 12:50:34 +01001765 if (ret)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001766 return ret;
1767
Chris Wilson48b956c2010-09-14 12:50:34 +01001768 ret = i915_gem_object_set_to_display_plane(obj, pipelined);
1769 if (ret)
1770 goto err_unpin;
Chris Wilson72133422010-09-13 23:56:38 +01001771
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001772 /* Install a fence for tiled scan-out. Pre-i965 always needs a
1773 * fence, whereas 965+ only requires a fence if using
1774 * framebuffer compression. For simplicity, we always install
1775 * a fence as the cost is not that onerous.
1776 */
Chris Wilson05394f32010-11-08 19:18:58 +00001777 if (obj->tiling_mode != I915_TILING_NONE) {
Chris Wilsond9e86c02010-11-10 16:40:20 +00001778 ret = i915_gem_object_get_fence(obj, pipelined, false);
Chris Wilson48b956c2010-09-14 12:50:34 +01001779 if (ret)
1780 goto err_unpin;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001781 }
1782
1783 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01001784
1785err_unpin:
1786 i915_gem_object_unpin(obj);
1787 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001788}
1789
Jesse Barnes81255562010-08-02 12:07:50 -07001790/* Assume fb object is pinned & idle & fenced and just update base pointers */
1791static int
1792intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
Jason Wessel21c74a82010-10-13 14:09:44 -05001793 int x, int y, enum mode_set_atomic state)
Jesse Barnes81255562010-08-02 12:07:50 -07001794{
1795 struct drm_device *dev = crtc->dev;
1796 struct drm_i915_private *dev_priv = dev->dev_private;
1797 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1798 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00001799 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07001800 int plane = intel_crtc->plane;
1801 unsigned long Start, Offset;
Jesse Barnes81255562010-08-02 12:07:50 -07001802 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01001803 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07001804
1805 switch (plane) {
1806 case 0:
1807 case 1:
1808 break;
1809 default:
1810 DRM_ERROR("Can't update plane %d in SAREA\n", plane);
1811 return -EINVAL;
1812 }
1813
1814 intel_fb = to_intel_framebuffer(fb);
1815 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07001816
Chris Wilson5eddb702010-09-11 13:48:45 +01001817 reg = DSPCNTR(plane);
1818 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07001819 /* Mask out pixel format bits in case we change it */
1820 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
1821 switch (fb->bits_per_pixel) {
1822 case 8:
1823 dspcntr |= DISPPLANE_8BPP;
1824 break;
1825 case 16:
1826 if (fb->depth == 15)
1827 dspcntr |= DISPPLANE_15_16BPP;
1828 else
1829 dspcntr |= DISPPLANE_16BPP;
1830 break;
1831 case 24:
1832 case 32:
1833 dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
1834 break;
1835 default:
1836 DRM_ERROR("Unknown color depth\n");
1837 return -EINVAL;
1838 }
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001839 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00001840 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07001841 dspcntr |= DISPPLANE_TILED;
1842 else
1843 dspcntr &= ~DISPPLANE_TILED;
1844 }
1845
Chris Wilson4e6cfef2010-08-08 13:20:19 +01001846 if (HAS_PCH_SPLIT(dev))
Jesse Barnes81255562010-08-02 12:07:50 -07001847 /* must disable */
1848 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
1849
Chris Wilson5eddb702010-09-11 13:48:45 +01001850 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07001851
Chris Wilson05394f32010-11-08 19:18:58 +00001852 Start = obj->gtt_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07001853 Offset = y * fb->pitch + x * (fb->bits_per_pixel / 8);
1854
Chris Wilson4e6cfef2010-08-08 13:20:19 +01001855 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
1856 Start, Offset, x, y, fb->pitch);
Chris Wilson5eddb702010-09-11 13:48:45 +01001857 I915_WRITE(DSPSTRIDE(plane), fb->pitch);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001858 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson5eddb702010-09-11 13:48:45 +01001859 I915_WRITE(DSPSURF(plane), Start);
1860 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
1861 I915_WRITE(DSPADDR(plane), Offset);
1862 } else
1863 I915_WRITE(DSPADDR(plane), Start + Offset);
1864 POSTING_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07001865
Chris Wilsonbed4a672010-09-11 10:47:47 +01001866 intel_update_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02001867 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07001868
1869 return 0;
1870}
1871
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001872static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05001873intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
1874 struct drm_framebuffer *old_fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08001875{
1876 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08001877 struct drm_i915_master_private *master_priv;
1878 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001879 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08001880
1881 /* no fb bound */
1882 if (!crtc->fb) {
Zhao Yakui28c97732009-10-09 11:39:41 +08001883 DRM_DEBUG_KMS("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001884 return 0;
1885 }
1886
Chris Wilson265db952010-09-20 15:41:01 +01001887 switch (intel_crtc->plane) {
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001888 case 0:
1889 case 1:
1890 break;
1891 default:
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001892 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08001893 }
1894
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001895 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01001896 ret = intel_pin_and_fence_fb_obj(dev,
1897 to_intel_framebuffer(crtc->fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00001898 NULL);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001899 if (ret != 0) {
1900 mutex_unlock(&dev->struct_mutex);
1901 return ret;
1902 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05001903
Chris Wilson265db952010-09-20 15:41:01 +01001904 if (old_fb) {
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01001905 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001906 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
Chris Wilson265db952010-09-20 15:41:01 +01001907
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01001908 wait_event(dev_priv->pending_flip_queue,
Chris Wilson05394f32010-11-08 19:18:58 +00001909 atomic_read(&obj->pending_flip) == 0);
Chris Wilson85345512010-11-13 09:49:11 +00001910
1911 /* Big Hammer, we also need to ensure that any pending
1912 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
1913 * current scanout is retired before unpinning the old
1914 * framebuffer.
1915 */
Chris Wilson05394f32010-11-08 19:18:58 +00001916 ret = i915_gem_object_flush_gpu(obj, false);
Chris Wilson85345512010-11-13 09:49:11 +00001917 if (ret) {
1918 i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
1919 mutex_unlock(&dev->struct_mutex);
1920 return ret;
1921 }
Chris Wilson265db952010-09-20 15:41:01 +01001922 }
1923
Jason Wessel21c74a82010-10-13 14:09:44 -05001924 ret = intel_pipe_set_base_atomic(crtc, crtc->fb, x, y,
1925 LEAVE_ATOMIC_MODE_SET);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01001926 if (ret) {
Chris Wilson265db952010-09-20 15:41:01 +01001927 i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001928 mutex_unlock(&dev->struct_mutex);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01001929 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08001930 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05001931
Chris Wilsonb7f1de22010-12-14 16:09:31 +00001932 if (old_fb) {
1933 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson265db952010-09-20 15:41:01 +01001934 i915_gem_object_unpin(to_intel_framebuffer(old_fb)->obj);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00001935 }
Jesse Barnes652c3932009-08-17 13:31:43 -07001936
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001937 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08001938
1939 if (!dev->primary->master)
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001940 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08001941
1942 master_priv = dev->primary->master->driver_priv;
1943 if (!master_priv->sarea_priv)
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001944 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08001945
Chris Wilson265db952010-09-20 15:41:01 +01001946 if (intel_crtc->pipe) {
Jesse Barnes79e53942008-11-07 14:24:08 -08001947 master_priv->sarea_priv->pipeB_x = x;
1948 master_priv->sarea_priv->pipeB_y = y;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001949 } else {
1950 master_priv->sarea_priv->pipeA_x = x;
1951 master_priv->sarea_priv->pipeA_y = y;
Jesse Barnes79e53942008-11-07 14:24:08 -08001952 }
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001953
1954 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08001955}
1956
Chris Wilson5eddb702010-09-11 13:48:45 +01001957static void ironlake_set_pll_edp(struct drm_crtc *crtc, int clock)
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001958{
1959 struct drm_device *dev = crtc->dev;
1960 struct drm_i915_private *dev_priv = dev->dev_private;
1961 u32 dpa_ctl;
1962
Zhao Yakui28c97732009-10-09 11:39:41 +08001963 DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", clock);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001964 dpa_ctl = I915_READ(DP_A);
1965 dpa_ctl &= ~DP_PLL_FREQ_MASK;
1966
1967 if (clock < 200000) {
1968 u32 temp;
1969 dpa_ctl |= DP_PLL_FREQ_160MHZ;
1970 /* workaround for 160Mhz:
1971 1) program 0x4600c bits 15:0 = 0x8124
1972 2) program 0x46010 bit 0 = 1
1973 3) program 0x46034 bit 24 = 1
1974 4) program 0x64000 bit 14 = 1
1975 */
1976 temp = I915_READ(0x4600c);
1977 temp &= 0xffff0000;
1978 I915_WRITE(0x4600c, temp | 0x8124);
1979
1980 temp = I915_READ(0x46010);
1981 I915_WRITE(0x46010, temp | 1);
1982
1983 temp = I915_READ(0x46034);
1984 I915_WRITE(0x46034, temp | (1 << 24));
1985 } else {
1986 dpa_ctl |= DP_PLL_FREQ_270MHZ;
1987 }
1988 I915_WRITE(DP_A, dpa_ctl);
1989
Chris Wilson5eddb702010-09-11 13:48:45 +01001990 POSTING_READ(DP_A);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001991 udelay(500);
1992}
1993
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08001994static void intel_fdi_normal_train(struct drm_crtc *crtc)
1995{
1996 struct drm_device *dev = crtc->dev;
1997 struct drm_i915_private *dev_priv = dev->dev_private;
1998 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1999 int pipe = intel_crtc->pipe;
2000 u32 reg, temp;
2001
2002 /* enable normal train */
2003 reg = FDI_TX_CTL(pipe);
2004 temp = I915_READ(reg);
2005 temp &= ~FDI_LINK_TRAIN_NONE;
2006 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
2007 I915_WRITE(reg, temp);
2008
2009 reg = FDI_RX_CTL(pipe);
2010 temp = I915_READ(reg);
2011 if (HAS_PCH_CPT(dev)) {
2012 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2013 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2014 } else {
2015 temp &= ~FDI_LINK_TRAIN_NONE;
2016 temp |= FDI_LINK_TRAIN_NONE;
2017 }
2018 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2019
2020 /* wait one idle pattern time */
2021 POSTING_READ(reg);
2022 udelay(1000);
2023}
2024
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002025/* The FDI link training functions for ILK/Ibexpeak. */
2026static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2027{
2028 struct drm_device *dev = crtc->dev;
2029 struct drm_i915_private *dev_priv = dev->dev_private;
2030 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2031 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002032 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002033
Adam Jacksone1a44742010-06-25 15:32:14 -04002034 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2035 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002036 reg = FDI_RX_IMR(pipe);
2037 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002038 temp &= ~FDI_RX_SYMBOL_LOCK;
2039 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002040 I915_WRITE(reg, temp);
2041 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002042 udelay(150);
2043
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002044 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002045 reg = FDI_TX_CTL(pipe);
2046 temp = I915_READ(reg);
Adam Jackson77ffb592010-04-12 11:38:44 -04002047 temp &= ~(7 << 19);
2048 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002049 temp &= ~FDI_LINK_TRAIN_NONE;
2050 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002051 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002052
Chris Wilson5eddb702010-09-11 13:48:45 +01002053 reg = FDI_RX_CTL(pipe);
2054 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002055 temp &= ~FDI_LINK_TRAIN_NONE;
2056 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002057 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2058
2059 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002060 udelay(150);
2061
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002062 /* Ironlake workaround, enable clock pointer after FDI enable*/
2063 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_ENABLE);
2064
Chris Wilson5eddb702010-09-11 13:48:45 +01002065 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002066 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002067 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002068 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2069
2070 if ((temp & FDI_RX_BIT_LOCK)) {
2071 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002072 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002073 break;
2074 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002075 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002076 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002077 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002078
2079 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002080 reg = FDI_TX_CTL(pipe);
2081 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002082 temp &= ~FDI_LINK_TRAIN_NONE;
2083 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002084 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002085
Chris Wilson5eddb702010-09-11 13:48:45 +01002086 reg = FDI_RX_CTL(pipe);
2087 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002088 temp &= ~FDI_LINK_TRAIN_NONE;
2089 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002090 I915_WRITE(reg, temp);
2091
2092 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002093 udelay(150);
2094
Chris Wilson5eddb702010-09-11 13:48:45 +01002095 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002096 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002097 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002098 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2099
2100 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002101 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002102 DRM_DEBUG_KMS("FDI train 2 done.\n");
2103 break;
2104 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002105 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002106 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002107 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002108
2109 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002110
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002111}
2112
Chris Wilson5eddb702010-09-11 13:48:45 +01002113static const int const snb_b_fdi_train_param [] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002114 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2115 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2116 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2117 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2118};
2119
2120/* The FDI link training functions for SNB/Cougarpoint. */
2121static void gen6_fdi_link_train(struct drm_crtc *crtc)
2122{
2123 struct drm_device *dev = crtc->dev;
2124 struct drm_i915_private *dev_priv = dev->dev_private;
2125 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2126 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002127 u32 reg, temp, i;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002128
Adam Jacksone1a44742010-06-25 15:32:14 -04002129 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2130 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002131 reg = FDI_RX_IMR(pipe);
2132 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002133 temp &= ~FDI_RX_SYMBOL_LOCK;
2134 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002135 I915_WRITE(reg, temp);
2136
2137 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002138 udelay(150);
2139
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002140 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002141 reg = FDI_TX_CTL(pipe);
2142 temp = I915_READ(reg);
Adam Jackson77ffb592010-04-12 11:38:44 -04002143 temp &= ~(7 << 19);
2144 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002145 temp &= ~FDI_LINK_TRAIN_NONE;
2146 temp |= FDI_LINK_TRAIN_PATTERN_1;
2147 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2148 /* SNB-B */
2149 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002150 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002151
Chris Wilson5eddb702010-09-11 13:48:45 +01002152 reg = FDI_RX_CTL(pipe);
2153 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002154 if (HAS_PCH_CPT(dev)) {
2155 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2156 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2157 } else {
2158 temp &= ~FDI_LINK_TRAIN_NONE;
2159 temp |= FDI_LINK_TRAIN_PATTERN_1;
2160 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002161 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2162
2163 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002164 udelay(150);
2165
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002166 for (i = 0; i < 4; i++ ) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002167 reg = FDI_TX_CTL(pipe);
2168 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002169 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2170 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002171 I915_WRITE(reg, temp);
2172
2173 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002174 udelay(500);
2175
Chris Wilson5eddb702010-09-11 13:48:45 +01002176 reg = FDI_RX_IIR(pipe);
2177 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002178 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2179
2180 if (temp & FDI_RX_BIT_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002181 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002182 DRM_DEBUG_KMS("FDI train 1 done.\n");
2183 break;
2184 }
2185 }
2186 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002187 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002188
2189 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002190 reg = FDI_TX_CTL(pipe);
2191 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002192 temp &= ~FDI_LINK_TRAIN_NONE;
2193 temp |= FDI_LINK_TRAIN_PATTERN_2;
2194 if (IS_GEN6(dev)) {
2195 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2196 /* SNB-B */
2197 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2198 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002199 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002200
Chris Wilson5eddb702010-09-11 13:48:45 +01002201 reg = FDI_RX_CTL(pipe);
2202 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002203 if (HAS_PCH_CPT(dev)) {
2204 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2205 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2206 } else {
2207 temp &= ~FDI_LINK_TRAIN_NONE;
2208 temp |= FDI_LINK_TRAIN_PATTERN_2;
2209 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002210 I915_WRITE(reg, temp);
2211
2212 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002213 udelay(150);
2214
2215 for (i = 0; i < 4; i++ ) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002216 reg = FDI_TX_CTL(pipe);
2217 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002218 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2219 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002220 I915_WRITE(reg, temp);
2221
2222 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002223 udelay(500);
2224
Chris Wilson5eddb702010-09-11 13:48:45 +01002225 reg = FDI_RX_IIR(pipe);
2226 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002227 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2228
2229 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002230 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002231 DRM_DEBUG_KMS("FDI train 2 done.\n");
2232 break;
2233 }
2234 }
2235 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002236 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002237
2238 DRM_DEBUG_KMS("FDI train done.\n");
2239}
2240
Jesse Barnes0e23b992010-09-10 11:10:00 -07002241static void ironlake_fdi_enable(struct drm_crtc *crtc)
2242{
2243 struct drm_device *dev = crtc->dev;
2244 struct drm_i915_private *dev_priv = dev->dev_private;
2245 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2246 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002247 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002248
Jesse Barnesc64e3112010-09-10 11:27:03 -07002249 /* Write the TU size bits so error detection works */
Chris Wilson5eddb702010-09-11 13:48:45 +01002250 I915_WRITE(FDI_RX_TUSIZE1(pipe),
2251 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
Jesse Barnesc64e3112010-09-10 11:27:03 -07002252
Jesse Barnes0e23b992010-09-10 11:10:00 -07002253 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01002254 reg = FDI_RX_CTL(pipe);
2255 temp = I915_READ(reg);
2256 temp &= ~((0x7 << 19) | (0x7 << 16));
Jesse Barnes0e23b992010-09-10 11:10:00 -07002257 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Chris Wilson5eddb702010-09-11 13:48:45 +01002258 temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2259 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2260
2261 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002262 udelay(200);
2263
2264 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002265 temp = I915_READ(reg);
2266 I915_WRITE(reg, temp | FDI_PCDCLK);
2267
2268 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002269 udelay(200);
2270
2271 /* Enable CPU FDI TX PLL, always on for Ironlake */
Chris Wilson5eddb702010-09-11 13:48:45 +01002272 reg = FDI_TX_CTL(pipe);
2273 temp = I915_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002274 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002275 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
2276
2277 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002278 udelay(100);
2279 }
2280}
2281
Chris Wilson6b383a72010-09-13 13:54:26 +01002282/*
2283 * When we disable a pipe, we need to clear any pending scanline wait events
2284 * to avoid hanging the ring, which we assume we are waiting on.
2285 */
2286static void intel_clear_scanline_wait(struct drm_device *dev)
2287{
2288 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson8168bd42010-11-11 17:54:52 +00002289 struct intel_ring_buffer *ring;
Chris Wilson6b383a72010-09-13 13:54:26 +01002290 u32 tmp;
2291
2292 if (IS_GEN2(dev))
2293 /* Can't break the hang on i8xx */
2294 return;
2295
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002296 ring = LP_RING(dev_priv);
Chris Wilson8168bd42010-11-11 17:54:52 +00002297 tmp = I915_READ_CTL(ring);
2298 if (tmp & RING_WAIT)
2299 I915_WRITE_CTL(ring, tmp);
Chris Wilson6b383a72010-09-13 13:54:26 +01002300}
2301
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002302static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2303{
Chris Wilson05394f32010-11-08 19:18:58 +00002304 struct drm_i915_gem_object *obj;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002305 struct drm_i915_private *dev_priv;
2306
2307 if (crtc->fb == NULL)
2308 return;
2309
Chris Wilson05394f32010-11-08 19:18:58 +00002310 obj = to_intel_framebuffer(crtc->fb)->obj;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002311 dev_priv = crtc->dev->dev_private;
2312 wait_event(dev_priv->pending_flip_queue,
Chris Wilson05394f32010-11-08 19:18:58 +00002313 atomic_read(&obj->pending_flip) == 0);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002314}
2315
Jesse Barnes6be4a602010-09-10 10:26:01 -07002316static void ironlake_crtc_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08002317{
2318 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002319 struct drm_i915_private *dev_priv = dev->dev_private;
2320 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2321 int pipe = intel_crtc->pipe;
Shaohua Li7662c8b2009-06-26 11:23:55 +08002322 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002323 u32 reg, temp;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002324
Chris Wilsonf7abfe82010-09-13 14:19:16 +01002325 if (intel_crtc->active)
2326 return;
2327
2328 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01002329 intel_update_watermarks(dev);
2330
Jesse Barnes6be4a602010-09-10 10:26:01 -07002331 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
2332 temp = I915_READ(PCH_LVDS);
Chris Wilson5eddb702010-09-11 13:48:45 +01002333 if ((temp & LVDS_PORT_EN) == 0)
Jesse Barnes6be4a602010-09-10 10:26:01 -07002334 I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002335 }
2336
Jesse Barnes0e23b992010-09-10 11:10:00 -07002337 ironlake_fdi_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002338
2339 /* Enable panel fitting for LVDS */
2340 if (dev_priv->pch_pf_size &&
Jesse Barnes1d850362010-10-07 16:01:10 -07002341 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) || HAS_eDP)) {
Jesse Barnes6be4a602010-09-10 10:26:01 -07002342 /* Force use of hard-coded filter coefficients
2343 * as some pre-programmed values are broken,
2344 * e.g. x201.
2345 */
2346 I915_WRITE(pipe ? PFB_CTL_1 : PFA_CTL_1,
2347 PF_ENABLE | PF_FILTER_MED_3x3);
2348 I915_WRITE(pipe ? PFB_WIN_POS : PFA_WIN_POS,
2349 dev_priv->pch_pf_pos);
2350 I915_WRITE(pipe ? PFB_WIN_SZ : PFA_WIN_SZ,
2351 dev_priv->pch_pf_size);
2352 }
2353
Jesse Barnesb24e7172011-01-04 15:09:30 -08002354 intel_enable_pipe(dev_priv, pipe);
2355 intel_enable_plane(dev_priv, plane, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002356
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002357 /* For PCH output, training FDI link */
2358 if (IS_GEN6(dev))
2359 gen6_fdi_link_train(crtc);
2360 else
2361 ironlake_fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002362
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002363 /* enable PCH DPLL */
Chris Wilson5eddb702010-09-11 13:48:45 +01002364 reg = PCH_DPLL(pipe);
2365 temp = I915_READ(reg);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002366 if ((temp & DPLL_VCO_ENABLE) == 0) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002367 I915_WRITE(reg, temp | DPLL_VCO_ENABLE);
2368 POSTING_READ(reg);
Chris Wilson8c4223b2010-09-10 22:33:42 +01002369 udelay(200);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002370 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002371
2372 if (HAS_PCH_CPT(dev)) {
2373 /* Be sure PCH DPLL SEL is set */
2374 temp = I915_READ(PCH_DPLL_SEL);
Chris Wilson5eddb702010-09-11 13:48:45 +01002375 if (pipe == 0 && (temp & TRANSA_DPLL_ENABLE) == 0)
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002376 temp |= (TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL);
Chris Wilson5eddb702010-09-11 13:48:45 +01002377 else if (pipe == 1 && (temp & TRANSB_DPLL_ENABLE) == 0)
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002378 temp |= (TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
2379 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002380 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002381
Chris Wilson5eddb702010-09-11 13:48:45 +01002382 /* set transcoder timing */
2383 I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
2384 I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
2385 I915_WRITE(TRANS_HSYNC(pipe), I915_READ(HSYNC(pipe)));
2386
2387 I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
2388 I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
2389 I915_WRITE(TRANS_VSYNC(pipe), I915_READ(VSYNC(pipe)));
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002390
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002391 intel_fdi_normal_train(crtc);
2392
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002393 /* For PCH DP, enable TRANS_DP_CTL */
2394 if (HAS_PCH_CPT(dev) &&
2395 intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002396 reg = TRANS_DP_CTL(pipe);
2397 temp = I915_READ(reg);
2398 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08002399 TRANS_DP_SYNC_MASK |
2400 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01002401 temp |= (TRANS_DP_OUTPUT_ENABLE |
2402 TRANS_DP_ENH_FRAMING);
Eric Anholt220cad32010-11-18 09:32:58 +08002403 temp |= TRANS_DP_8BPC;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002404
2405 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01002406 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002407 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01002408 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002409
2410 switch (intel_trans_dp_port_sel(crtc)) {
2411 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01002412 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002413 break;
2414 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01002415 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002416 break;
2417 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01002418 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002419 break;
2420 default:
2421 DRM_DEBUG_KMS("Wrong PCH DP port return. Guess port B\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002422 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002423 break;
2424 }
2425
Chris Wilson5eddb702010-09-11 13:48:45 +01002426 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002427 }
2428
2429 /* enable PCH transcoder */
Chris Wilson5eddb702010-09-11 13:48:45 +01002430 reg = TRANSCONF(pipe);
2431 temp = I915_READ(reg);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002432 /*
2433 * make the BPC in transcoder be consistent with
2434 * that in pipeconf reg.
2435 */
2436 temp &= ~PIPE_BPC_MASK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002437 temp |= I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK;
2438 I915_WRITE(reg, temp | TRANS_ENABLE);
2439 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Jesse Barnes17f67662010-10-07 16:01:19 -07002440 DRM_ERROR("failed to enable transcoder %d\n", pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002441
2442 intel_crtc_load_lut(crtc);
Chris Wilsonbed4a672010-09-11 10:47:47 +01002443 intel_update_fbc(dev);
Chris Wilson6b383a72010-09-13 13:54:26 +01002444 intel_crtc_update_cursor(crtc, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002445}
2446
2447static void ironlake_crtc_disable(struct drm_crtc *crtc)
2448{
2449 struct drm_device *dev = crtc->dev;
2450 struct drm_i915_private *dev_priv = dev->dev_private;
2451 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2452 int pipe = intel_crtc->pipe;
2453 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002454 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07002455
Chris Wilsonf7abfe82010-09-13 14:19:16 +01002456 if (!intel_crtc->active)
2457 return;
2458
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002459 intel_crtc_wait_for_pending_flips(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002460 drm_vblank_off(dev, pipe);
Chris Wilson6b383a72010-09-13 13:54:26 +01002461 intel_crtc_update_cursor(crtc, false);
Chris Wilson5eddb702010-09-11 13:48:45 +01002462
Jesse Barnesb24e7172011-01-04 15:09:30 -08002463 intel_disable_plane(dev_priv, plane, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002464
2465 if (dev_priv->cfb_plane == plane &&
2466 dev_priv->display.disable_fbc)
2467 dev_priv->display.disable_fbc(dev);
2468
Jesse Barnesb24e7172011-01-04 15:09:30 -08002469 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002470
Jesse Barnes6be4a602010-09-10 10:26:01 -07002471 /* Disable PF */
2472 I915_WRITE(pipe ? PFB_CTL_1 : PFA_CTL_1, 0);
2473 I915_WRITE(pipe ? PFB_WIN_SZ : PFA_WIN_SZ, 0);
2474
2475 /* disable CPU FDI tx and PCH FDI rx */
Chris Wilson5eddb702010-09-11 13:48:45 +01002476 reg = FDI_TX_CTL(pipe);
2477 temp = I915_READ(reg);
2478 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2479 POSTING_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002480
Chris Wilson5eddb702010-09-11 13:48:45 +01002481 reg = FDI_RX_CTL(pipe);
2482 temp = I915_READ(reg);
2483 temp &= ~(0x7 << 16);
2484 temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2485 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002486
Chris Wilson5eddb702010-09-11 13:48:45 +01002487 POSTING_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002488 udelay(100);
2489
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002490 /* Ironlake workaround, disable clock pointer after downing FDI */
Zhenyu Wange07ac3a2010-11-04 09:02:54 +00002491 if (HAS_PCH_IBX(dev))
2492 I915_WRITE(FDI_RX_CHICKEN(pipe),
2493 I915_READ(FDI_RX_CHICKEN(pipe) &
2494 ~FDI_RX_PHASE_SYNC_POINTER_ENABLE));
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002495
Jesse Barnes6be4a602010-09-10 10:26:01 -07002496 /* still set train pattern 1 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002497 reg = FDI_TX_CTL(pipe);
2498 temp = I915_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002499 temp &= ~FDI_LINK_TRAIN_NONE;
2500 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002501 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002502
Chris Wilson5eddb702010-09-11 13:48:45 +01002503 reg = FDI_RX_CTL(pipe);
2504 temp = I915_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002505 if (HAS_PCH_CPT(dev)) {
2506 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2507 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2508 } else {
2509 temp &= ~FDI_LINK_TRAIN_NONE;
2510 temp |= FDI_LINK_TRAIN_PATTERN_1;
2511 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002512 /* BPC in FDI rx is consistent with that in PIPECONF */
2513 temp &= ~(0x07 << 16);
2514 temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2515 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002516
Chris Wilson5eddb702010-09-11 13:48:45 +01002517 POSTING_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002518 udelay(100);
2519
2520 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
2521 temp = I915_READ(PCH_LVDS);
Chris Wilson5eddb702010-09-11 13:48:45 +01002522 if (temp & LVDS_PORT_EN) {
2523 I915_WRITE(PCH_LVDS, temp & ~LVDS_PORT_EN);
2524 POSTING_READ(PCH_LVDS);
2525 udelay(100);
2526 }
Jesse Barnes6be4a602010-09-10 10:26:01 -07002527 }
2528
2529 /* disable PCH transcoder */
Chris Wilson5eddb702010-09-11 13:48:45 +01002530 reg = TRANSCONF(plane);
2531 temp = I915_READ(reg);
2532 if (temp & TRANS_ENABLE) {
2533 I915_WRITE(reg, temp & ~TRANS_ENABLE);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002534 /* wait for PCH transcoder off, transcoder state */
Chris Wilson5eddb702010-09-11 13:48:45 +01002535 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Jesse Barnes6be4a602010-09-10 10:26:01 -07002536 DRM_ERROR("failed to disable transcoder\n");
2537 }
2538
Jesse Barnes6be4a602010-09-10 10:26:01 -07002539 if (HAS_PCH_CPT(dev)) {
2540 /* disable TRANS_DP_CTL */
Chris Wilson5eddb702010-09-11 13:48:45 +01002541 reg = TRANS_DP_CTL(pipe);
2542 temp = I915_READ(reg);
2543 temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
2544 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002545
2546 /* disable DPLL_SEL */
2547 temp = I915_READ(PCH_DPLL_SEL);
Chris Wilson5eddb702010-09-11 13:48:45 +01002548 if (pipe == 0)
Jesse Barnes6be4a602010-09-10 10:26:01 -07002549 temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
2550 else
2551 temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
2552 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002553 }
2554
2555 /* disable PCH DPLL */
Chris Wilson5eddb702010-09-11 13:48:45 +01002556 reg = PCH_DPLL(pipe);
2557 temp = I915_READ(reg);
2558 I915_WRITE(reg, temp & ~DPLL_VCO_ENABLE);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002559
2560 /* Switch from PCDclk to Rawclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002561 reg = FDI_RX_CTL(pipe);
2562 temp = I915_READ(reg);
2563 I915_WRITE(reg, temp & ~FDI_PCDCLK);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002564
2565 /* Disable CPU FDI TX PLL */
Chris Wilson5eddb702010-09-11 13:48:45 +01002566 reg = FDI_TX_CTL(pipe);
2567 temp = I915_READ(reg);
2568 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2569
2570 POSTING_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002571 udelay(100);
2572
Chris Wilson5eddb702010-09-11 13:48:45 +01002573 reg = FDI_RX_CTL(pipe);
2574 temp = I915_READ(reg);
2575 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002576
2577 /* Wait for the clocks to turn off. */
Chris Wilson5eddb702010-09-11 13:48:45 +01002578 POSTING_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002579 udelay(100);
Chris Wilson6b383a72010-09-13 13:54:26 +01002580
Chris Wilsonf7abfe82010-09-13 14:19:16 +01002581 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01002582 intel_update_watermarks(dev);
2583 intel_update_fbc(dev);
2584 intel_clear_scanline_wait(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002585}
2586
2587static void ironlake_crtc_dpms(struct drm_crtc *crtc, int mode)
2588{
2589 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2590 int pipe = intel_crtc->pipe;
2591 int plane = intel_crtc->plane;
2592
Zhenyu Wang2c072452009-06-05 15:38:42 +08002593 /* XXX: When our outputs are all unaware of DPMS modes other than off
2594 * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
2595 */
2596 switch (mode) {
2597 case DRM_MODE_DPMS_ON:
2598 case DRM_MODE_DPMS_STANDBY:
2599 case DRM_MODE_DPMS_SUSPEND:
Chris Wilson868dc582010-08-07 11:01:31 +01002600 DRM_DEBUG_KMS("crtc %d/%d dpms on\n", pipe, plane);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002601 ironlake_crtc_enable(crtc);
Chris Wilson868dc582010-08-07 11:01:31 +01002602 break;
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08002603
Zhenyu Wang2c072452009-06-05 15:38:42 +08002604 case DRM_MODE_DPMS_OFF:
Chris Wilson868dc582010-08-07 11:01:31 +01002605 DRM_DEBUG_KMS("crtc %d/%d dpms off\n", pipe, plane);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002606 ironlake_crtc_disable(crtc);
Zhenyu Wang2c072452009-06-05 15:38:42 +08002607 break;
2608 }
2609}
2610
Daniel Vetter02e792f2009-09-15 22:57:34 +02002611static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
2612{
Daniel Vetter02e792f2009-09-15 22:57:34 +02002613 if (!enable && intel_crtc->overlay) {
Chris Wilson23f09ce2010-08-12 13:53:37 +01002614 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02002615
Chris Wilson23f09ce2010-08-12 13:53:37 +01002616 mutex_lock(&dev->struct_mutex);
2617 (void) intel_overlay_switch_off(intel_crtc->overlay, false);
2618 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02002619 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02002620
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01002621 /* Let userspace switch the overlay on again. In most cases userspace
2622 * has to recompute where to put it anyway.
2623 */
Daniel Vetter02e792f2009-09-15 22:57:34 +02002624}
2625
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002626static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08002627{
2628 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002629 struct drm_i915_private *dev_priv = dev->dev_private;
2630 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2631 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07002632 int plane = intel_crtc->plane;
Jesse Barnes79e53942008-11-07 14:24:08 -08002633
Chris Wilsonf7abfe82010-09-13 14:19:16 +01002634 if (intel_crtc->active)
2635 return;
2636
2637 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01002638 intel_update_watermarks(dev);
2639
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08002640 intel_enable_pll(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002641 intel_enable_pipe(dev_priv, pipe);
2642 intel_enable_plane(dev_priv, plane, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002643
2644 intel_crtc_load_lut(crtc);
Chris Wilsonbed4a672010-09-11 10:47:47 +01002645 intel_update_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002646
2647 /* Give the overlay scaler a chance to enable if it's on this pipe */
2648 intel_crtc_dpms_overlay(intel_crtc, true);
Chris Wilson6b383a72010-09-13 13:54:26 +01002649 intel_crtc_update_cursor(crtc, true);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002650}
2651
2652static void i9xx_crtc_disable(struct drm_crtc *crtc)
2653{
2654 struct drm_device *dev = crtc->dev;
2655 struct drm_i915_private *dev_priv = dev->dev_private;
2656 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2657 int pipe = intel_crtc->pipe;
2658 int plane = intel_crtc->plane;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002659
Chris Wilsonf7abfe82010-09-13 14:19:16 +01002660 if (!intel_crtc->active)
2661 return;
2662
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002663 /* Give the overlay scaler a chance to disable if it's on this pipe */
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002664 intel_crtc_wait_for_pending_flips(crtc);
2665 drm_vblank_off(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002666 intel_crtc_dpms_overlay(intel_crtc, false);
Chris Wilson6b383a72010-09-13 13:54:26 +01002667 intel_crtc_update_cursor(crtc, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002668
2669 if (dev_priv->cfb_plane == plane &&
2670 dev_priv->display.disable_fbc)
2671 dev_priv->display.disable_fbc(dev);
2672
Jesse Barnesb24e7172011-01-04 15:09:30 -08002673 intel_disable_plane(dev_priv, plane, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002674 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08002675 intel_disable_pll(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002676
Chris Wilsonf7abfe82010-09-13 14:19:16 +01002677 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01002678 intel_update_fbc(dev);
2679 intel_update_watermarks(dev);
2680 intel_clear_scanline_wait(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002681}
2682
2683static void i9xx_crtc_dpms(struct drm_crtc *crtc, int mode)
2684{
Jesse Barnes79e53942008-11-07 14:24:08 -08002685 /* XXX: When our outputs are all unaware of DPMS modes other than off
2686 * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
2687 */
2688 switch (mode) {
2689 case DRM_MODE_DPMS_ON:
2690 case DRM_MODE_DPMS_STANDBY:
2691 case DRM_MODE_DPMS_SUSPEND:
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002692 i9xx_crtc_enable(crtc);
2693 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08002694 case DRM_MODE_DPMS_OFF:
Jesse Barnes0b8765c62010-09-10 10:31:34 -07002695 i9xx_crtc_disable(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08002696 break;
2697 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08002698}
2699
2700/**
2701 * Sets the power management mode of the pipe and plane.
Zhenyu Wang2c072452009-06-05 15:38:42 +08002702 */
2703static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
2704{
2705 struct drm_device *dev = crtc->dev;
Jesse Barnese70236a2009-09-21 10:42:27 -07002706 struct drm_i915_private *dev_priv = dev->dev_private;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002707 struct drm_i915_master_private *master_priv;
2708 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2709 int pipe = intel_crtc->pipe;
2710 bool enabled;
2711
Chris Wilson032d2a02010-09-06 16:17:22 +01002712 if (intel_crtc->dpms_mode == mode)
2713 return;
2714
Chris Wilsondebcadd2010-08-07 11:01:33 +01002715 intel_crtc->dpms_mode = mode;
Chris Wilsondebcadd2010-08-07 11:01:33 +01002716
Jesse Barnese70236a2009-09-21 10:42:27 -07002717 dev_priv->display.dpms(crtc, mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08002718
2719 if (!dev->primary->master)
2720 return;
2721
2722 master_priv = dev->primary->master->driver_priv;
2723 if (!master_priv->sarea_priv)
2724 return;
2725
2726 enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
2727
2728 switch (pipe) {
2729 case 0:
2730 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
2731 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
2732 break;
2733 case 1:
2734 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
2735 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
2736 break;
2737 default:
2738 DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
2739 break;
2740 }
Jesse Barnes79e53942008-11-07 14:24:08 -08002741}
2742
Chris Wilsoncdd59982010-09-08 16:30:16 +01002743static void intel_crtc_disable(struct drm_crtc *crtc)
2744{
2745 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
2746 struct drm_device *dev = crtc->dev;
2747
2748 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
2749
2750 if (crtc->fb) {
2751 mutex_lock(&dev->struct_mutex);
2752 i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
2753 mutex_unlock(&dev->struct_mutex);
2754 }
2755}
2756
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07002757/* Prepare for a mode set.
2758 *
2759 * Note we could be a lot smarter here. We need to figure out which outputs
2760 * will be enabled, which disabled (in short, how the config will changes)
2761 * and perform the minimum necessary steps to accomplish that, e.g. updating
2762 * watermarks, FBC configuration, making sure PLLs are programmed correctly,
2763 * panel fitting is in the proper state, etc.
2764 */
2765static void i9xx_crtc_prepare(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08002766{
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07002767 i9xx_crtc_disable(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08002768}
2769
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07002770static void i9xx_crtc_commit(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08002771{
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07002772 i9xx_crtc_enable(crtc);
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07002773}
2774
2775static void ironlake_crtc_prepare(struct drm_crtc *crtc)
2776{
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07002777 ironlake_crtc_disable(crtc);
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07002778}
2779
2780static void ironlake_crtc_commit(struct drm_crtc *crtc)
2781{
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07002782 ironlake_crtc_enable(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08002783}
2784
2785void intel_encoder_prepare (struct drm_encoder *encoder)
2786{
2787 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
2788 /* lvds has its own version of prepare see intel_lvds_prepare */
2789 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
2790}
2791
2792void intel_encoder_commit (struct drm_encoder *encoder)
2793{
2794 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
2795 /* lvds has its own version of commit see intel_lvds_commit */
2796 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
2797}
2798
Chris Wilsonea5b2132010-08-04 13:50:23 +01002799void intel_encoder_destroy(struct drm_encoder *encoder)
2800{
Chris Wilson4ef69c72010-09-09 15:14:28 +01002801 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01002802
Chris Wilsonea5b2132010-08-04 13:50:23 +01002803 drm_encoder_cleanup(encoder);
2804 kfree(intel_encoder);
2805}
2806
Jesse Barnes79e53942008-11-07 14:24:08 -08002807static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
2808 struct drm_display_mode *mode,
2809 struct drm_display_mode *adjusted_mode)
2810{
Zhenyu Wang2c072452009-06-05 15:38:42 +08002811 struct drm_device *dev = crtc->dev;
Chris Wilson89749352010-09-12 18:25:19 +01002812
Eric Anholtbad720f2009-10-22 16:11:14 -07002813 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08002814 /* FDI link clock is fixed at 2.7G */
Jesse Barnes2377b742010-07-07 14:06:43 -07002815 if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
2816 return false;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002817 }
Chris Wilson89749352010-09-12 18:25:19 +01002818
2819 /* XXX some encoders set the crtcinfo, others don't.
2820 * Obviously we need some form of conflict resolution here...
2821 */
2822 if (adjusted_mode->crtc_htotal == 0)
2823 drm_mode_set_crtcinfo(adjusted_mode, 0);
2824
Jesse Barnes79e53942008-11-07 14:24:08 -08002825 return true;
2826}
2827
Jesse Barnese70236a2009-09-21 10:42:27 -07002828static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08002829{
Jesse Barnese70236a2009-09-21 10:42:27 -07002830 return 400000;
2831}
Jesse Barnes79e53942008-11-07 14:24:08 -08002832
Jesse Barnese70236a2009-09-21 10:42:27 -07002833static int i915_get_display_clock_speed(struct drm_device *dev)
2834{
2835 return 333000;
2836}
Jesse Barnes79e53942008-11-07 14:24:08 -08002837
Jesse Barnese70236a2009-09-21 10:42:27 -07002838static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
2839{
2840 return 200000;
2841}
Jesse Barnes79e53942008-11-07 14:24:08 -08002842
Jesse Barnese70236a2009-09-21 10:42:27 -07002843static int i915gm_get_display_clock_speed(struct drm_device *dev)
2844{
2845 u16 gcfgc = 0;
2846
2847 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
2848
2849 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08002850 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07002851 else {
2852 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
2853 case GC_DISPLAY_CLOCK_333_MHZ:
2854 return 333000;
2855 default:
2856 case GC_DISPLAY_CLOCK_190_200_MHZ:
2857 return 190000;
2858 }
2859 }
2860}
Jesse Barnes79e53942008-11-07 14:24:08 -08002861
Jesse Barnese70236a2009-09-21 10:42:27 -07002862static int i865_get_display_clock_speed(struct drm_device *dev)
2863{
2864 return 266000;
2865}
2866
2867static int i855_get_display_clock_speed(struct drm_device *dev)
2868{
2869 u16 hpllcc = 0;
2870 /* Assume that the hardware is in the high speed state. This
2871 * should be the default.
2872 */
2873 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
2874 case GC_CLOCK_133_200:
2875 case GC_CLOCK_100_200:
2876 return 200000;
2877 case GC_CLOCK_166_250:
2878 return 250000;
2879 case GC_CLOCK_100_133:
2880 return 133000;
2881 }
2882
2883 /* Shouldn't happen */
2884 return 0;
2885}
2886
2887static int i830_get_display_clock_speed(struct drm_device *dev)
2888{
2889 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08002890}
2891
Zhenyu Wang2c072452009-06-05 15:38:42 +08002892struct fdi_m_n {
2893 u32 tu;
2894 u32 gmch_m;
2895 u32 gmch_n;
2896 u32 link_m;
2897 u32 link_n;
2898};
2899
2900static void
2901fdi_reduce_ratio(u32 *num, u32 *den)
2902{
2903 while (*num > 0xffffff || *den > 0xffffff) {
2904 *num >>= 1;
2905 *den >>= 1;
2906 }
2907}
2908
Zhenyu Wang2c072452009-06-05 15:38:42 +08002909static void
Adam Jacksonf2b115e2009-12-03 17:14:42 -05002910ironlake_compute_m_n(int bits_per_pixel, int nlanes, int pixel_clock,
2911 int link_clock, struct fdi_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08002912{
Zhenyu Wang2c072452009-06-05 15:38:42 +08002913 m_n->tu = 64; /* default size */
2914
Chris Wilson22ed1112010-12-04 01:01:29 +00002915 /* BUG_ON(pixel_clock > INT_MAX / 36); */
2916 m_n->gmch_m = bits_per_pixel * pixel_clock;
2917 m_n->gmch_n = link_clock * nlanes * 8;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002918 fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
2919
Chris Wilson22ed1112010-12-04 01:01:29 +00002920 m_n->link_m = pixel_clock;
2921 m_n->link_n = link_clock;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002922 fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
2923}
2924
2925
Shaohua Li7662c8b2009-06-26 11:23:55 +08002926struct intel_watermark_params {
2927 unsigned long fifo_size;
2928 unsigned long max_wm;
2929 unsigned long default_wm;
2930 unsigned long guard_size;
2931 unsigned long cacheline_size;
2932};
2933
Adam Jacksonf2b115e2009-12-03 17:14:42 -05002934/* Pineview has different values for various configs */
2935static struct intel_watermark_params pineview_display_wm = {
2936 PINEVIEW_DISPLAY_FIFO,
2937 PINEVIEW_MAX_WM,
2938 PINEVIEW_DFT_WM,
2939 PINEVIEW_GUARD_WM,
2940 PINEVIEW_FIFO_LINE_SIZE
Shaohua Li7662c8b2009-06-26 11:23:55 +08002941};
Adam Jacksonf2b115e2009-12-03 17:14:42 -05002942static struct intel_watermark_params pineview_display_hplloff_wm = {
2943 PINEVIEW_DISPLAY_FIFO,
2944 PINEVIEW_MAX_WM,
2945 PINEVIEW_DFT_HPLLOFF_WM,
2946 PINEVIEW_GUARD_WM,
2947 PINEVIEW_FIFO_LINE_SIZE
Shaohua Li7662c8b2009-06-26 11:23:55 +08002948};
Adam Jacksonf2b115e2009-12-03 17:14:42 -05002949static struct intel_watermark_params pineview_cursor_wm = {
2950 PINEVIEW_CURSOR_FIFO,
2951 PINEVIEW_CURSOR_MAX_WM,
2952 PINEVIEW_CURSOR_DFT_WM,
2953 PINEVIEW_CURSOR_GUARD_WM,
2954 PINEVIEW_FIFO_LINE_SIZE,
Shaohua Li7662c8b2009-06-26 11:23:55 +08002955};
Adam Jacksonf2b115e2009-12-03 17:14:42 -05002956static struct intel_watermark_params pineview_cursor_hplloff_wm = {
2957 PINEVIEW_CURSOR_FIFO,
2958 PINEVIEW_CURSOR_MAX_WM,
2959 PINEVIEW_CURSOR_DFT_WM,
2960 PINEVIEW_CURSOR_GUARD_WM,
2961 PINEVIEW_FIFO_LINE_SIZE
Shaohua Li7662c8b2009-06-26 11:23:55 +08002962};
Jesse Barnes0e442c62009-10-19 10:09:33 +09002963static struct intel_watermark_params g4x_wm_info = {
2964 G4X_FIFO_SIZE,
2965 G4X_MAX_WM,
2966 G4X_MAX_WM,
2967 2,
2968 G4X_FIFO_LINE_SIZE,
2969};
Zhao Yakui4fe5e612010-06-12 14:32:25 +08002970static struct intel_watermark_params g4x_cursor_wm_info = {
2971 I965_CURSOR_FIFO,
2972 I965_CURSOR_MAX_WM,
2973 I965_CURSOR_DFT_WM,
2974 2,
2975 G4X_FIFO_LINE_SIZE,
2976};
2977static struct intel_watermark_params i965_cursor_wm_info = {
2978 I965_CURSOR_FIFO,
2979 I965_CURSOR_MAX_WM,
2980 I965_CURSOR_DFT_WM,
2981 2,
2982 I915_FIFO_LINE_SIZE,
2983};
Shaohua Li7662c8b2009-06-26 11:23:55 +08002984static struct intel_watermark_params i945_wm_info = {
Shaohua Li7662c8b2009-06-26 11:23:55 +08002985 I945_FIFO_SIZE,
2986 I915_MAX_WM,
2987 1,
Jesse Barnesdff33cf2009-07-14 10:15:56 -07002988 2,
2989 I915_FIFO_LINE_SIZE
2990};
2991static struct intel_watermark_params i915_wm_info = {
2992 I915_FIFO_SIZE,
2993 I915_MAX_WM,
2994 1,
2995 2,
Shaohua Li7662c8b2009-06-26 11:23:55 +08002996 I915_FIFO_LINE_SIZE
2997};
2998static struct intel_watermark_params i855_wm_info = {
2999 I855GM_FIFO_SIZE,
3000 I915_MAX_WM,
3001 1,
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003002 2,
Shaohua Li7662c8b2009-06-26 11:23:55 +08003003 I830_FIFO_LINE_SIZE
3004};
3005static struct intel_watermark_params i830_wm_info = {
3006 I830_FIFO_SIZE,
3007 I915_MAX_WM,
3008 1,
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003009 2,
Shaohua Li7662c8b2009-06-26 11:23:55 +08003010 I830_FIFO_LINE_SIZE
3011};
3012
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003013static struct intel_watermark_params ironlake_display_wm_info = {
3014 ILK_DISPLAY_FIFO,
3015 ILK_DISPLAY_MAXWM,
3016 ILK_DISPLAY_DFTWM,
3017 2,
3018 ILK_FIFO_LINE_SIZE
3019};
3020
Zhao Yakuic936f442010-06-12 14:32:26 +08003021static struct intel_watermark_params ironlake_cursor_wm_info = {
3022 ILK_CURSOR_FIFO,
3023 ILK_CURSOR_MAXWM,
3024 ILK_CURSOR_DFTWM,
3025 2,
3026 ILK_FIFO_LINE_SIZE
3027};
3028
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003029static struct intel_watermark_params ironlake_display_srwm_info = {
3030 ILK_DISPLAY_SR_FIFO,
3031 ILK_DISPLAY_MAX_SRWM,
3032 ILK_DISPLAY_DFT_SRWM,
3033 2,
3034 ILK_FIFO_LINE_SIZE
3035};
3036
3037static struct intel_watermark_params ironlake_cursor_srwm_info = {
3038 ILK_CURSOR_SR_FIFO,
3039 ILK_CURSOR_MAX_SRWM,
3040 ILK_CURSOR_DFT_SRWM,
3041 2,
3042 ILK_FIFO_LINE_SIZE
3043};
3044
Yuanhan Liu13982612010-12-15 15:42:31 +08003045static struct intel_watermark_params sandybridge_display_wm_info = {
3046 SNB_DISPLAY_FIFO,
3047 SNB_DISPLAY_MAXWM,
3048 SNB_DISPLAY_DFTWM,
3049 2,
3050 SNB_FIFO_LINE_SIZE
3051};
3052
3053static struct intel_watermark_params sandybridge_cursor_wm_info = {
3054 SNB_CURSOR_FIFO,
3055 SNB_CURSOR_MAXWM,
3056 SNB_CURSOR_DFTWM,
3057 2,
3058 SNB_FIFO_LINE_SIZE
3059};
3060
3061static struct intel_watermark_params sandybridge_display_srwm_info = {
3062 SNB_DISPLAY_SR_FIFO,
3063 SNB_DISPLAY_MAX_SRWM,
3064 SNB_DISPLAY_DFT_SRWM,
3065 2,
3066 SNB_FIFO_LINE_SIZE
3067};
3068
3069static struct intel_watermark_params sandybridge_cursor_srwm_info = {
3070 SNB_CURSOR_SR_FIFO,
3071 SNB_CURSOR_MAX_SRWM,
3072 SNB_CURSOR_DFT_SRWM,
3073 2,
3074 SNB_FIFO_LINE_SIZE
3075};
3076
3077
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003078/**
3079 * intel_calculate_wm - calculate watermark level
3080 * @clock_in_khz: pixel clock
3081 * @wm: chip FIFO params
3082 * @pixel_size: display pixel size
3083 * @latency_ns: memory latency for the platform
3084 *
3085 * Calculate the watermark level (the level at which the display plane will
3086 * start fetching from memory again). Each chip has a different display
3087 * FIFO size and allocation, so the caller needs to figure that out and pass
3088 * in the correct intel_watermark_params structure.
3089 *
3090 * As the pixel clock runs, the FIFO will be drained at a rate that depends
3091 * on the pixel size. When it reaches the watermark level, it'll start
3092 * fetching FIFO line sized based chunks from memory until the FIFO fills
3093 * past the watermark point. If the FIFO drains completely, a FIFO underrun
3094 * will occur, and a display engine hang could result.
3095 */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003096static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
3097 struct intel_watermark_params *wm,
3098 int pixel_size,
3099 unsigned long latency_ns)
3100{
Jesse Barnes390c4dd2009-07-16 13:01:01 -07003101 long entries_required, wm_size;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003102
Jesse Barnesd6604672009-09-11 12:25:56 -07003103 /*
3104 * Note: we need to make sure we don't overflow for various clock &
3105 * latency values.
3106 * clocks go from a few thousand to several hundred thousand.
3107 * latency is usually a few thousand
3108 */
3109 entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
3110 1000;
Chris Wilson8de9b312010-07-19 19:59:52 +01003111 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003112
Zhao Yakui28c97732009-10-09 11:39:41 +08003113 DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries_required);
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003114
3115 wm_size = wm->fifo_size - (entries_required + wm->guard_size);
3116
Zhao Yakui28c97732009-10-09 11:39:41 +08003117 DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003118
Jesse Barnes390c4dd2009-07-16 13:01:01 -07003119 /* Don't promote wm_size to unsigned... */
3120 if (wm_size > (long)wm->max_wm)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003121 wm_size = wm->max_wm;
Chris Wilsonc3add4b2010-09-08 09:14:08 +01003122 if (wm_size <= 0)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003123 wm_size = wm->default_wm;
3124 return wm_size;
3125}
3126
3127struct cxsr_latency {
3128 int is_desktop;
Li Peng95534262010-05-18 18:58:44 +08003129 int is_ddr3;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003130 unsigned long fsb_freq;
3131 unsigned long mem_freq;
3132 unsigned long display_sr;
3133 unsigned long display_hpll_disable;
3134 unsigned long cursor_sr;
3135 unsigned long cursor_hpll_disable;
3136};
3137
Chris Wilson403c89f2010-08-04 15:25:31 +01003138static const struct cxsr_latency cxsr_latency_table[] = {
Li Peng95534262010-05-18 18:58:44 +08003139 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
3140 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
3141 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
3142 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
3143 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003144
Li Peng95534262010-05-18 18:58:44 +08003145 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
3146 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
3147 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
3148 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
3149 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003150
Li Peng95534262010-05-18 18:58:44 +08003151 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
3152 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
3153 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
3154 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
3155 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003156
Li Peng95534262010-05-18 18:58:44 +08003157 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
3158 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
3159 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
3160 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
3161 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003162
Li Peng95534262010-05-18 18:58:44 +08003163 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
3164 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
3165 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
3166 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
3167 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003168
Li Peng95534262010-05-18 18:58:44 +08003169 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
3170 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
3171 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
3172 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
3173 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003174};
3175
Chris Wilson403c89f2010-08-04 15:25:31 +01003176static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
3177 int is_ddr3,
3178 int fsb,
3179 int mem)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003180{
Chris Wilson403c89f2010-08-04 15:25:31 +01003181 const struct cxsr_latency *latency;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003182 int i;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003183
3184 if (fsb == 0 || mem == 0)
3185 return NULL;
3186
3187 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
3188 latency = &cxsr_latency_table[i];
3189 if (is_desktop == latency->is_desktop &&
Li Peng95534262010-05-18 18:58:44 +08003190 is_ddr3 == latency->is_ddr3 &&
Jaswinder Singh Rajputdecbbcd2009-09-12 23:15:07 +05303191 fsb == latency->fsb_freq && mem == latency->mem_freq)
3192 return latency;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003193 }
Jaswinder Singh Rajputdecbbcd2009-09-12 23:15:07 +05303194
Zhao Yakui28c97732009-10-09 11:39:41 +08003195 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Jaswinder Singh Rajputdecbbcd2009-09-12 23:15:07 +05303196
3197 return NULL;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003198}
3199
Adam Jacksonf2b115e2009-12-03 17:14:42 -05003200static void pineview_disable_cxsr(struct drm_device *dev)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003201{
3202 struct drm_i915_private *dev_priv = dev->dev_private;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003203
3204 /* deactivate cxsr */
Chris Wilson3e33d942010-08-04 11:17:25 +01003205 I915_WRITE(DSPFW3, I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003206}
3207
Jesse Barnesbcc24fb2009-08-31 10:24:31 -07003208/*
3209 * Latency for FIFO fetches is dependent on several factors:
3210 * - memory configuration (speed, channels)
3211 * - chipset
3212 * - current MCH state
3213 * It can be fairly high in some situations, so here we assume a fairly
3214 * pessimal value. It's a tradeoff between extra memory fetches (if we
3215 * set this value too high, the FIFO will fetch frequently to stay full)
3216 * and power consumption (set it too low to save power and we might see
3217 * FIFO underruns and display "flicker").
3218 *
3219 * A value of 5us seems to be a good balance; safe for very low end
3220 * platforms but not overly aggressive on lower latency configs.
3221 */
Tobias Klauser69e302a2009-12-23 14:14:34 +01003222static const int latency_ns = 5000;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003223
Jesse Barnese70236a2009-09-21 10:42:27 -07003224static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003225{
3226 struct drm_i915_private *dev_priv = dev->dev_private;
3227 uint32_t dsparb = I915_READ(DSPARB);
3228 int size;
3229
Chris Wilson8de9b312010-07-19 19:59:52 +01003230 size = dsparb & 0x7f;
3231 if (plane)
3232 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003233
Zhao Yakui28c97732009-10-09 11:39:41 +08003234 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
Chris Wilson5eddb702010-09-11 13:48:45 +01003235 plane ? "B" : "A", size);
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003236
3237 return size;
3238}
Shaohua Li7662c8b2009-06-26 11:23:55 +08003239
Jesse Barnese70236a2009-09-21 10:42:27 -07003240static int i85x_get_fifo_size(struct drm_device *dev, int plane)
3241{
3242 struct drm_i915_private *dev_priv = dev->dev_private;
3243 uint32_t dsparb = I915_READ(DSPARB);
3244 int size;
3245
Chris Wilson8de9b312010-07-19 19:59:52 +01003246 size = dsparb & 0x1ff;
3247 if (plane)
3248 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
Jesse Barnese70236a2009-09-21 10:42:27 -07003249 size >>= 1; /* Convert to cachelines */
3250
Zhao Yakui28c97732009-10-09 11:39:41 +08003251 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
Chris Wilson5eddb702010-09-11 13:48:45 +01003252 plane ? "B" : "A", size);
Jesse Barnese70236a2009-09-21 10:42:27 -07003253
3254 return size;
3255}
3256
3257static int i845_get_fifo_size(struct drm_device *dev, int plane)
3258{
3259 struct drm_i915_private *dev_priv = dev->dev_private;
3260 uint32_t dsparb = I915_READ(DSPARB);
3261 int size;
3262
3263 size = dsparb & 0x7f;
3264 size >>= 2; /* Convert to cachelines */
3265
Zhao Yakui28c97732009-10-09 11:39:41 +08003266 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
Chris Wilson5eddb702010-09-11 13:48:45 +01003267 plane ? "B" : "A",
3268 size);
Jesse Barnese70236a2009-09-21 10:42:27 -07003269
3270 return size;
3271}
3272
3273static int i830_get_fifo_size(struct drm_device *dev, int plane)
3274{
3275 struct drm_i915_private *dev_priv = dev->dev_private;
3276 uint32_t dsparb = I915_READ(DSPARB);
3277 int size;
3278
3279 size = dsparb & 0x7f;
3280 size >>= 1; /* Convert to cachelines */
3281
Zhao Yakui28c97732009-10-09 11:39:41 +08003282 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
Chris Wilson5eddb702010-09-11 13:48:45 +01003283 plane ? "B" : "A", size);
Jesse Barnese70236a2009-09-21 10:42:27 -07003284
3285 return size;
3286}
3287
Zhao Yakuid4294342010-03-22 22:45:36 +08003288static void pineview_update_wm(struct drm_device *dev, int planea_clock,
Chris Wilson5eddb702010-09-11 13:48:45 +01003289 int planeb_clock, int sr_hdisplay, int unused,
3290 int pixel_size)
Zhao Yakuid4294342010-03-22 22:45:36 +08003291{
3292 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson403c89f2010-08-04 15:25:31 +01003293 const struct cxsr_latency *latency;
Zhao Yakuid4294342010-03-22 22:45:36 +08003294 u32 reg;
3295 unsigned long wm;
Zhao Yakuid4294342010-03-22 22:45:36 +08003296 int sr_clock;
3297
Chris Wilson403c89f2010-08-04 15:25:31 +01003298 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
Li Peng95534262010-05-18 18:58:44 +08003299 dev_priv->fsb_freq, dev_priv->mem_freq);
Zhao Yakuid4294342010-03-22 22:45:36 +08003300 if (!latency) {
3301 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
3302 pineview_disable_cxsr(dev);
3303 return;
3304 }
3305
3306 if (!planea_clock || !planeb_clock) {
3307 sr_clock = planea_clock ? planea_clock : planeb_clock;
3308
3309 /* Display SR */
3310 wm = intel_calculate_wm(sr_clock, &pineview_display_wm,
3311 pixel_size, latency->display_sr);
3312 reg = I915_READ(DSPFW1);
3313 reg &= ~DSPFW_SR_MASK;
3314 reg |= wm << DSPFW_SR_SHIFT;
3315 I915_WRITE(DSPFW1, reg);
3316 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
3317
3318 /* cursor SR */
3319 wm = intel_calculate_wm(sr_clock, &pineview_cursor_wm,
3320 pixel_size, latency->cursor_sr);
3321 reg = I915_READ(DSPFW3);
3322 reg &= ~DSPFW_CURSOR_SR_MASK;
3323 reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
3324 I915_WRITE(DSPFW3, reg);
3325
3326 /* Display HPLL off SR */
3327 wm = intel_calculate_wm(sr_clock, &pineview_display_hplloff_wm,
3328 pixel_size, latency->display_hpll_disable);
3329 reg = I915_READ(DSPFW3);
3330 reg &= ~DSPFW_HPLL_SR_MASK;
3331 reg |= wm & DSPFW_HPLL_SR_MASK;
3332 I915_WRITE(DSPFW3, reg);
3333
3334 /* cursor HPLL off SR */
3335 wm = intel_calculate_wm(sr_clock, &pineview_cursor_hplloff_wm,
3336 pixel_size, latency->cursor_hpll_disable);
3337 reg = I915_READ(DSPFW3);
3338 reg &= ~DSPFW_HPLL_CURSOR_MASK;
3339 reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
3340 I915_WRITE(DSPFW3, reg);
3341 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
3342
3343 /* activate cxsr */
Chris Wilson3e33d942010-08-04 11:17:25 +01003344 I915_WRITE(DSPFW3,
3345 I915_READ(DSPFW3) | PINEVIEW_SELF_REFRESH_EN);
Zhao Yakuid4294342010-03-22 22:45:36 +08003346 DRM_DEBUG_KMS("Self-refresh is enabled\n");
3347 } else {
3348 pineview_disable_cxsr(dev);
3349 DRM_DEBUG_KMS("Self-refresh is disabled\n");
3350 }
3351}
3352
Jesse Barnes0e442c62009-10-19 10:09:33 +09003353static void g4x_update_wm(struct drm_device *dev, int planea_clock,
Zhao Yakuifa143212010-06-12 14:32:23 +08003354 int planeb_clock, int sr_hdisplay, int sr_htotal,
3355 int pixel_size)
Jesse Barnes652c3932009-08-17 13:31:43 -07003356{
3357 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e442c62009-10-19 10:09:33 +09003358 int total_size, cacheline_size;
3359 int planea_wm, planeb_wm, cursora_wm, cursorb_wm, cursor_sr;
3360 struct intel_watermark_params planea_params, planeb_params;
3361 unsigned long line_time_us;
3362 int sr_clock, sr_entries = 0, entries_required;
Jesse Barnes652c3932009-08-17 13:31:43 -07003363
Jesse Barnes0e442c62009-10-19 10:09:33 +09003364 /* Create copies of the base settings for each pipe */
3365 planea_params = planeb_params = g4x_wm_info;
3366
3367 /* Grab a couple of global values before we overwrite them */
3368 total_size = planea_params.fifo_size;
3369 cacheline_size = planea_params.cacheline_size;
3370
3371 /*
3372 * Note: we need to make sure we don't overflow for various clock &
3373 * latency values.
3374 * clocks go from a few thousand to several hundred thousand.
3375 * latency is usually a few thousand
3376 */
3377 entries_required = ((planea_clock / 1000) * pixel_size * latency_ns) /
3378 1000;
Chris Wilson8de9b312010-07-19 19:59:52 +01003379 entries_required = DIV_ROUND_UP(entries_required, G4X_FIFO_LINE_SIZE);
Jesse Barnes0e442c62009-10-19 10:09:33 +09003380 planea_wm = entries_required + planea_params.guard_size;
3381
3382 entries_required = ((planeb_clock / 1000) * pixel_size * latency_ns) /
3383 1000;
Chris Wilson8de9b312010-07-19 19:59:52 +01003384 entries_required = DIV_ROUND_UP(entries_required, G4X_FIFO_LINE_SIZE);
Jesse Barnes0e442c62009-10-19 10:09:33 +09003385 planeb_wm = entries_required + planeb_params.guard_size;
3386
3387 cursora_wm = cursorb_wm = 16;
3388 cursor_sr = 32;
3389
3390 DRM_DEBUG("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
3391
3392 /* Calc sr entries for one plane configs */
3393 if (sr_hdisplay && (!planea_clock || !planeb_clock)) {
3394 /* self-refresh has much higher latency */
Tobias Klauser69e302a2009-12-23 14:14:34 +01003395 static const int sr_latency_ns = 12000;
Jesse Barnes0e442c62009-10-19 10:09:33 +09003396
3397 sr_clock = planea_clock ? planea_clock : planeb_clock;
Zhao Yakuifa143212010-06-12 14:32:23 +08003398 line_time_us = ((sr_htotal * 1000) / sr_clock);
Jesse Barnes0e442c62009-10-19 10:09:33 +09003399
3400 /* Use ns/us then divide to preserve precision */
Zhao Yakuifa143212010-06-12 14:32:23 +08003401 sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Chris Wilson5eddb702010-09-11 13:48:45 +01003402 pixel_size * sr_hdisplay;
Chris Wilson8de9b312010-07-19 19:59:52 +01003403 sr_entries = DIV_ROUND_UP(sr_entries, cacheline_size);
Zhao Yakui4fe5e612010-06-12 14:32:25 +08003404
3405 entries_required = (((sr_latency_ns / line_time_us) +
3406 1000) / 1000) * pixel_size * 64;
Chris Wilson8de9b312010-07-19 19:59:52 +01003407 entries_required = DIV_ROUND_UP(entries_required,
Chris Wilson5eddb702010-09-11 13:48:45 +01003408 g4x_cursor_wm_info.cacheline_size);
Zhao Yakui4fe5e612010-06-12 14:32:25 +08003409 cursor_sr = entries_required + g4x_cursor_wm_info.guard_size;
3410
3411 if (cursor_sr > g4x_cursor_wm_info.max_wm)
3412 cursor_sr = g4x_cursor_wm_info.max_wm;
3413 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
3414 "cursor %d\n", sr_entries, cursor_sr);
3415
Jesse Barnes0e442c62009-10-19 10:09:33 +09003416 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
David John33c5fd12010-01-27 15:19:08 +05303417 } else {
3418 /* Turn off self refresh if both pipes are enabled */
3419 I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
Chris Wilson5eddb702010-09-11 13:48:45 +01003420 & ~FW_BLC_SELF_EN);
Jesse Barnes0e442c62009-10-19 10:09:33 +09003421 }
3422
3423 DRM_DEBUG("Setting FIFO watermarks - A: %d, B: %d, SR %d\n",
3424 planea_wm, planeb_wm, sr_entries);
3425
3426 planea_wm &= 0x3f;
3427 planeb_wm &= 0x3f;
3428
3429 I915_WRITE(DSPFW1, (sr_entries << DSPFW_SR_SHIFT) |
3430 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
3431 (planeb_wm << DSPFW_PLANEB_SHIFT) | planea_wm);
3432 I915_WRITE(DSPFW2, (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
3433 (cursora_wm << DSPFW_CURSORA_SHIFT));
3434 /* HPLL off in SR has some issues on G4x... disable it */
3435 I915_WRITE(DSPFW3, (I915_READ(DSPFW3) & ~DSPFW_HPLL_SR_EN) |
3436 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Jesse Barnes652c3932009-08-17 13:31:43 -07003437}
3438
Jesse Barnes1dc75462009-10-19 10:08:17 +09003439static void i965_update_wm(struct drm_device *dev, int planea_clock,
Zhao Yakuifa143212010-06-12 14:32:23 +08003440 int planeb_clock, int sr_hdisplay, int sr_htotal,
3441 int pixel_size)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003442{
3443 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1dc75462009-10-19 10:08:17 +09003444 unsigned long line_time_us;
3445 int sr_clock, sr_entries, srwm = 1;
Zhao Yakui4fe5e612010-06-12 14:32:25 +08003446 int cursor_sr = 16;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003447
Jesse Barnes1dc75462009-10-19 10:08:17 +09003448 /* Calc sr entries for one plane configs */
3449 if (sr_hdisplay && (!planea_clock || !planeb_clock)) {
3450 /* self-refresh has much higher latency */
Tobias Klauser69e302a2009-12-23 14:14:34 +01003451 static const int sr_latency_ns = 12000;
Jesse Barnes1dc75462009-10-19 10:08:17 +09003452
3453 sr_clock = planea_clock ? planea_clock : planeb_clock;
Zhao Yakuifa143212010-06-12 14:32:23 +08003454 line_time_us = ((sr_htotal * 1000) / sr_clock);
Jesse Barnes1dc75462009-10-19 10:08:17 +09003455
3456 /* Use ns/us then divide to preserve precision */
Zhao Yakuifa143212010-06-12 14:32:23 +08003457 sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Chris Wilson5eddb702010-09-11 13:48:45 +01003458 pixel_size * sr_hdisplay;
Chris Wilson8de9b312010-07-19 19:59:52 +01003459 sr_entries = DIV_ROUND_UP(sr_entries, I915_FIFO_LINE_SIZE);
Jesse Barnes1dc75462009-10-19 10:08:17 +09003460 DRM_DEBUG("self-refresh entries: %d\n", sr_entries);
Zhao Yakui1b07e042010-06-12 14:32:24 +08003461 srwm = I965_FIFO_SIZE - sr_entries;
Jesse Barnes1dc75462009-10-19 10:08:17 +09003462 if (srwm < 0)
3463 srwm = 1;
Zhao Yakui1b07e042010-06-12 14:32:24 +08003464 srwm &= 0x1ff;
Zhao Yakui4fe5e612010-06-12 14:32:25 +08003465
3466 sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Chris Wilson5eddb702010-09-11 13:48:45 +01003467 pixel_size * 64;
Chris Wilson8de9b312010-07-19 19:59:52 +01003468 sr_entries = DIV_ROUND_UP(sr_entries,
3469 i965_cursor_wm_info.cacheline_size);
Zhao Yakui4fe5e612010-06-12 14:32:25 +08003470 cursor_sr = i965_cursor_wm_info.fifo_size -
Chris Wilson5eddb702010-09-11 13:48:45 +01003471 (sr_entries + i965_cursor_wm_info.guard_size);
Zhao Yakui4fe5e612010-06-12 14:32:25 +08003472
3473 if (cursor_sr > i965_cursor_wm_info.max_wm)
3474 cursor_sr = i965_cursor_wm_info.max_wm;
3475
3476 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
3477 "cursor %d\n", srwm, cursor_sr);
3478
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003479 if (IS_CRESTLINE(dev))
Jesse Barnesadcdbc62010-06-30 13:49:37 -07003480 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
David John33c5fd12010-01-27 15:19:08 +05303481 } else {
3482 /* Turn off self refresh if both pipes are enabled */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003483 if (IS_CRESTLINE(dev))
Jesse Barnesadcdbc62010-06-30 13:49:37 -07003484 I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
3485 & ~FW_BLC_SELF_EN);
Jesse Barnes1dc75462009-10-19 10:08:17 +09003486 }
3487
3488 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
3489 srwm);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003490
3491 /* 965 has limitations... */
Jesse Barnes1dc75462009-10-19 10:08:17 +09003492 I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) | (8 << 16) | (8 << 8) |
3493 (8 << 0));
Shaohua Li7662c8b2009-06-26 11:23:55 +08003494 I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
Zhao Yakui4fe5e612010-06-12 14:32:25 +08003495 /* update cursor SR watermark */
3496 I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Shaohua Li7662c8b2009-06-26 11:23:55 +08003497}
3498
3499static void i9xx_update_wm(struct drm_device *dev, int planea_clock,
Zhao Yakuifa143212010-06-12 14:32:23 +08003500 int planeb_clock, int sr_hdisplay, int sr_htotal,
3501 int pixel_size)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003502{
3503 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003504 uint32_t fwater_lo;
3505 uint32_t fwater_hi;
3506 int total_size, cacheline_size, cwm, srwm = 1;
3507 int planea_wm, planeb_wm;
3508 struct intel_watermark_params planea_params, planeb_params;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003509 unsigned long line_time_us;
3510 int sr_clock, sr_entries = 0;
3511
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003512 /* Create copies of the base settings for each pipe */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003513 if (IS_CRESTLINE(dev) || IS_I945GM(dev))
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003514 planea_params = planeb_params = i945_wm_info;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003515 else if (!IS_GEN2(dev))
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003516 planea_params = planeb_params = i915_wm_info;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003517 else
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003518 planea_params = planeb_params = i855_wm_info;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003519
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003520 /* Grab a couple of global values before we overwrite them */
3521 total_size = planea_params.fifo_size;
3522 cacheline_size = planea_params.cacheline_size;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003523
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003524 /* Update per-plane FIFO sizes */
Jesse Barnese70236a2009-09-21 10:42:27 -07003525 planea_params.fifo_size = dev_priv->display.get_fifo_size(dev, 0);
3526 planeb_params.fifo_size = dev_priv->display.get_fifo_size(dev, 1);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003527
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003528 planea_wm = intel_calculate_wm(planea_clock, &planea_params,
3529 pixel_size, latency_ns);
3530 planeb_wm = intel_calculate_wm(planeb_clock, &planeb_params,
3531 pixel_size, latency_ns);
Zhao Yakui28c97732009-10-09 11:39:41 +08003532 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003533
3534 /*
3535 * Overlay gets an aggressive default since video jitter is bad.
3536 */
3537 cwm = 2;
3538
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003539 /* Calc sr entries for one plane configs */
Jesse Barnes652c3932009-08-17 13:31:43 -07003540 if (HAS_FW_BLC(dev) && sr_hdisplay &&
3541 (!planea_clock || !planeb_clock)) {
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003542 /* self-refresh has much higher latency */
Tobias Klauser69e302a2009-12-23 14:14:34 +01003543 static const int sr_latency_ns = 6000;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003544
Shaohua Li7662c8b2009-06-26 11:23:55 +08003545 sr_clock = planea_clock ? planea_clock : planeb_clock;
Zhao Yakuifa143212010-06-12 14:32:23 +08003546 line_time_us = ((sr_htotal * 1000) / sr_clock);
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003547
3548 /* Use ns/us then divide to preserve precision */
Zhao Yakuifa143212010-06-12 14:32:23 +08003549 sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Chris Wilson5eddb702010-09-11 13:48:45 +01003550 pixel_size * sr_hdisplay;
Chris Wilson8de9b312010-07-19 19:59:52 +01003551 sr_entries = DIV_ROUND_UP(sr_entries, cacheline_size);
Zhao Yakui28c97732009-10-09 11:39:41 +08003552 DRM_DEBUG_KMS("self-refresh entries: %d\n", sr_entries);
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003553 srwm = total_size - sr_entries;
3554 if (srwm < 0)
3555 srwm = 1;
Li Pengee980b82010-01-27 19:01:11 +08003556
3557 if (IS_I945G(dev) || IS_I945GM(dev))
3558 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
3559 else if (IS_I915GM(dev)) {
3560 /* 915M has a smaller SRWM field */
3561 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
3562 I915_WRITE(INSTPM, I915_READ(INSTPM) | INSTPM_SELF_EN);
3563 }
David John33c5fd12010-01-27 15:19:08 +05303564 } else {
3565 /* Turn off self refresh if both pipes are enabled */
Li Pengee980b82010-01-27 19:01:11 +08003566 if (IS_I945G(dev) || IS_I945GM(dev)) {
3567 I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
3568 & ~FW_BLC_SELF_EN);
3569 } else if (IS_I915GM(dev)) {
3570 I915_WRITE(INSTPM, I915_READ(INSTPM) & ~INSTPM_SELF_EN);
3571 }
Shaohua Li7662c8b2009-06-26 11:23:55 +08003572 }
3573
Zhao Yakui28c97732009-10-09 11:39:41 +08003574 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
Chris Wilson5eddb702010-09-11 13:48:45 +01003575 planea_wm, planeb_wm, cwm, srwm);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003576
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003577 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
3578 fwater_hi = (cwm & 0x1f);
3579
3580 /* Set request length to 8 cachelines per fetch */
3581 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
3582 fwater_hi = fwater_hi | (1 << 8);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003583
3584 I915_WRITE(FW_BLC, fwater_lo);
3585 I915_WRITE(FW_BLC2, fwater_hi);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003586}
3587
Jesse Barnese70236a2009-09-21 10:42:27 -07003588static void i830_update_wm(struct drm_device *dev, int planea_clock, int unused,
Zhao Yakuifa143212010-06-12 14:32:23 +08003589 int unused2, int unused3, int pixel_size)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003590{
3591 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf3601322009-07-22 12:54:59 -07003592 uint32_t fwater_lo = I915_READ(FW_BLC) & ~0xfff;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003593 int planea_wm;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003594
Jesse Barnese70236a2009-09-21 10:42:27 -07003595 i830_wm_info.fifo_size = dev_priv->display.get_fifo_size(dev, 0);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003596
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003597 planea_wm = intel_calculate_wm(planea_clock, &i830_wm_info,
3598 pixel_size, latency_ns);
Jesse Barnesf3601322009-07-22 12:54:59 -07003599 fwater_lo |= (3<<8) | planea_wm;
3600
Zhao Yakui28c97732009-10-09 11:39:41 +08003601 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003602
3603 I915_WRITE(FW_BLC, fwater_lo);
3604}
3605
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003606#define ILK_LP0_PLANE_LATENCY 700
Zhao Yakuic936f442010-06-12 14:32:26 +08003607#define ILK_LP0_CURSOR_LATENCY 1300
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003608
Chris Wilson4ed765f2010-09-11 10:46:47 +01003609static bool ironlake_compute_wm0(struct drm_device *dev,
3610 int pipe,
Yuanhan Liu13982612010-12-15 15:42:31 +08003611 const struct intel_watermark_params *display,
Yuanhan Liua0fa62d2010-12-23 16:35:40 +08003612 int display_latency_ns,
Yuanhan Liu13982612010-12-15 15:42:31 +08003613 const struct intel_watermark_params *cursor,
Yuanhan Liua0fa62d2010-12-23 16:35:40 +08003614 int cursor_latency_ns,
Chris Wilson4ed765f2010-09-11 10:46:47 +01003615 int *plane_wm,
3616 int *cursor_wm)
3617{
3618 struct drm_crtc *crtc;
Chris Wilsondb66e372011-01-08 09:02:21 +00003619 int htotal, hdisplay, clock, pixel_size;
3620 int line_time_us, line_count;
3621 int entries, tlb_miss;
Chris Wilson4ed765f2010-09-11 10:46:47 +01003622
3623 crtc = intel_get_crtc_for_pipe(dev, pipe);
3624 if (crtc->fb == NULL || !crtc->enabled)
3625 return false;
3626
3627 htotal = crtc->mode.htotal;
3628 hdisplay = crtc->mode.hdisplay;
3629 clock = crtc->mode.clock;
3630 pixel_size = crtc->fb->bits_per_pixel / 8;
3631
3632 /* Use the small buffer method to calculate plane watermark */
Yuanhan Liua0fa62d2010-12-23 16:35:40 +08003633 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
Chris Wilsondb66e372011-01-08 09:02:21 +00003634 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
3635 if (tlb_miss > 0)
3636 entries += tlb_miss;
Yuanhan Liu13982612010-12-15 15:42:31 +08003637 entries = DIV_ROUND_UP(entries, display->cacheline_size);
3638 *plane_wm = entries + display->guard_size;
3639 if (*plane_wm > (int)display->max_wm)
3640 *plane_wm = display->max_wm;
Chris Wilson4ed765f2010-09-11 10:46:47 +01003641
3642 /* Use the large buffer method to calculate cursor watermark */
3643 line_time_us = ((htotal * 1000) / clock);
Yuanhan Liua0fa62d2010-12-23 16:35:40 +08003644 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
Chris Wilson4ed765f2010-09-11 10:46:47 +01003645 entries = line_count * 64 * pixel_size;
Chris Wilsondb66e372011-01-08 09:02:21 +00003646 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
3647 if (tlb_miss > 0)
3648 entries += tlb_miss;
Yuanhan Liu13982612010-12-15 15:42:31 +08003649 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
3650 *cursor_wm = entries + cursor->guard_size;
3651 if (*cursor_wm > (int)cursor->max_wm)
3652 *cursor_wm = (int)cursor->max_wm;
Chris Wilson4ed765f2010-09-11 10:46:47 +01003653
3654 return true;
3655}
3656
Jesse Barnesb79d4992010-12-21 13:10:23 -08003657/*
3658 * Check the wm result.
3659 *
3660 * If any calculated watermark values is larger than the maximum value that
3661 * can be programmed into the associated watermark register, that watermark
3662 * must be disabled.
3663 */
3664static bool ironlake_check_srwm(struct drm_device *dev, int level,
3665 int fbc_wm, int display_wm, int cursor_wm,
3666 const struct intel_watermark_params *display,
3667 const struct intel_watermark_params *cursor)
3668{
3669 struct drm_i915_private *dev_priv = dev->dev_private;
3670
3671 DRM_DEBUG_KMS("watermark %d: display plane %d, fbc lines %d,"
3672 " cursor %d\n", level, display_wm, fbc_wm, cursor_wm);
3673
3674 if (fbc_wm > SNB_FBC_MAX_SRWM) {
3675 DRM_DEBUG_KMS("fbc watermark(%d) is too large(%d), disabling wm%d+\n",
3676 fbc_wm, SNB_FBC_MAX_SRWM, level);
3677
3678 /* fbc has it's own way to disable FBC WM */
3679 I915_WRITE(DISP_ARB_CTL,
3680 I915_READ(DISP_ARB_CTL) | DISP_FBC_WM_DIS);
3681 return false;
3682 }
3683
3684 if (display_wm > display->max_wm) {
3685 DRM_DEBUG_KMS("display watermark(%d) is too large(%d), disabling wm%d+\n",
3686 display_wm, SNB_DISPLAY_MAX_SRWM, level);
3687 return false;
3688 }
3689
3690 if (cursor_wm > cursor->max_wm) {
3691 DRM_DEBUG_KMS("cursor watermark(%d) is too large(%d), disabling wm%d+\n",
3692 cursor_wm, SNB_CURSOR_MAX_SRWM, level);
3693 return false;
3694 }
3695
3696 if (!(fbc_wm || display_wm || cursor_wm)) {
3697 DRM_DEBUG_KMS("latency %d is 0, disabling wm%d+\n", level, level);
3698 return false;
3699 }
3700
3701 return true;
3702}
3703
3704/*
3705 * Compute watermark values of WM[1-3],
3706 */
3707static bool ironlake_compute_srwm(struct drm_device *dev, int level,
3708 int hdisplay, int htotal,
3709 int pixel_size, int clock, int latency_ns,
3710 const struct intel_watermark_params *display,
3711 const struct intel_watermark_params *cursor,
3712 int *fbc_wm, int *display_wm, int *cursor_wm)
3713{
3714
3715 unsigned long line_time_us;
3716 int line_count, line_size;
3717 int small, large;
3718 int entries;
3719
3720 if (!latency_ns) {
3721 *fbc_wm = *display_wm = *cursor_wm = 0;
3722 return false;
3723 }
3724
3725 line_time_us = (htotal * 1000) / clock;
3726 line_count = (latency_ns / line_time_us + 1000) / 1000;
3727 line_size = hdisplay * pixel_size;
3728
3729 /* Use the minimum of the small and large buffer method for primary */
3730 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
3731 large = line_count * line_size;
3732
3733 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
3734 *display_wm = entries + display->guard_size;
3735
3736 /*
3737 * Spec says:
3738 * FBC WM = ((Final Primary WM * 64) / number of bytes per line) + 2
3739 */
3740 *fbc_wm = DIV_ROUND_UP(*display_wm * 64, line_size) + 2;
3741
3742 /* calculate the self-refresh watermark for display cursor */
3743 entries = line_count * pixel_size * 64;
3744 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
3745 *cursor_wm = entries + cursor->guard_size;
3746
3747 return ironlake_check_srwm(dev, level,
3748 *fbc_wm, *display_wm, *cursor_wm,
3749 display, cursor);
3750}
3751
Chris Wilson4ed765f2010-09-11 10:46:47 +01003752static void ironlake_update_wm(struct drm_device *dev,
3753 int planea_clock, int planeb_clock,
Jesse Barnesb79d4992010-12-21 13:10:23 -08003754 int hdisplay, int htotal,
Chris Wilson4ed765f2010-09-11 10:46:47 +01003755 int pixel_size)
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003756{
3757 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesb79d4992010-12-21 13:10:23 -08003758 int fbc_wm, plane_wm, cursor_wm, enabled;
3759 int clock;
Zhao Yakuic936f442010-06-12 14:32:26 +08003760
Chris Wilson4ed765f2010-09-11 10:46:47 +01003761 enabled = 0;
Yuanhan Liu13982612010-12-15 15:42:31 +08003762 if (ironlake_compute_wm0(dev, 0,
3763 &ironlake_display_wm_info,
3764 ILK_LP0_PLANE_LATENCY,
3765 &ironlake_cursor_wm_info,
3766 ILK_LP0_CURSOR_LATENCY,
3767 &plane_wm, &cursor_wm)) {
Chris Wilson4ed765f2010-09-11 10:46:47 +01003768 I915_WRITE(WM0_PIPEA_ILK,
3769 (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
3770 DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
3771 " plane %d, " "cursor: %d\n",
3772 plane_wm, cursor_wm);
3773 enabled++;
Zhao Yakuic936f442010-06-12 14:32:26 +08003774 }
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003775
Yuanhan Liu13982612010-12-15 15:42:31 +08003776 if (ironlake_compute_wm0(dev, 1,
3777 &ironlake_display_wm_info,
3778 ILK_LP0_PLANE_LATENCY,
3779 &ironlake_cursor_wm_info,
3780 ILK_LP0_CURSOR_LATENCY,
3781 &plane_wm, &cursor_wm)) {
Chris Wilson4ed765f2010-09-11 10:46:47 +01003782 I915_WRITE(WM0_PIPEB_ILK,
3783 (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
3784 DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
3785 " plane %d, cursor: %d\n",
3786 plane_wm, cursor_wm);
3787 enabled++;
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003788 }
3789
3790 /*
3791 * Calculate and update the self-refresh watermark only when one
3792 * display plane is used.
3793 */
Jesse Barnesb79d4992010-12-21 13:10:23 -08003794 I915_WRITE(WM3_LP_ILK, 0);
3795 I915_WRITE(WM2_LP_ILK, 0);
3796 I915_WRITE(WM1_LP_ILK, 0);
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003797
Jesse Barnesb79d4992010-12-21 13:10:23 -08003798 if (enabled != 1)
3799 return;
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003800
Jesse Barnesb79d4992010-12-21 13:10:23 -08003801 clock = planea_clock ? planea_clock : planeb_clock;
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003802
Jesse Barnesb79d4992010-12-21 13:10:23 -08003803 /* WM1 */
3804 if (!ironlake_compute_srwm(dev, 1, hdisplay, htotal, pixel_size,
3805 clock, ILK_READ_WM1_LATENCY() * 500,
3806 &ironlake_display_srwm_info,
3807 &ironlake_cursor_srwm_info,
3808 &fbc_wm, &plane_wm, &cursor_wm))
3809 return;
Chris Wilson4ed765f2010-09-11 10:46:47 +01003810
Jesse Barnesb79d4992010-12-21 13:10:23 -08003811 I915_WRITE(WM1_LP_ILK,
3812 WM1_LP_SR_EN |
3813 (ILK_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
3814 (fbc_wm << WM1_LP_FBC_SHIFT) |
3815 (plane_wm << WM1_LP_SR_SHIFT) |
3816 cursor_wm);
Chris Wilson4ed765f2010-09-11 10:46:47 +01003817
Jesse Barnesb79d4992010-12-21 13:10:23 -08003818 /* WM2 */
3819 if (!ironlake_compute_srwm(dev, 2, hdisplay, htotal, pixel_size,
3820 clock, ILK_READ_WM2_LATENCY() * 500,
3821 &ironlake_display_srwm_info,
3822 &ironlake_cursor_srwm_info,
3823 &fbc_wm, &plane_wm, &cursor_wm))
3824 return;
Chris Wilson4ed765f2010-09-11 10:46:47 +01003825
Jesse Barnesb79d4992010-12-21 13:10:23 -08003826 I915_WRITE(WM2_LP_ILK,
3827 WM2_LP_EN |
3828 (ILK_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
3829 (fbc_wm << WM1_LP_FBC_SHIFT) |
3830 (plane_wm << WM1_LP_SR_SHIFT) |
3831 cursor_wm);
Yuanhan Liu13982612010-12-15 15:42:31 +08003832
3833 /*
Jesse Barnesb79d4992010-12-21 13:10:23 -08003834 * WM3 is unsupported on ILK, probably because we don't have latency
3835 * data for that power state
Yuanhan Liu13982612010-12-15 15:42:31 +08003836 */
Yuanhan Liu13982612010-12-15 15:42:31 +08003837}
3838
3839static void sandybridge_update_wm(struct drm_device *dev,
3840 int planea_clock, int planeb_clock,
3841 int hdisplay, int htotal,
3842 int pixel_size)
3843{
3844 struct drm_i915_private *dev_priv = dev->dev_private;
Yuanhan Liua0fa62d2010-12-23 16:35:40 +08003845 int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
Yuanhan Liu13982612010-12-15 15:42:31 +08003846 int fbc_wm, plane_wm, cursor_wm, enabled;
3847 int clock;
3848
3849 enabled = 0;
3850 if (ironlake_compute_wm0(dev, 0,
3851 &sandybridge_display_wm_info, latency,
3852 &sandybridge_cursor_wm_info, latency,
3853 &plane_wm, &cursor_wm)) {
3854 I915_WRITE(WM0_PIPEA_ILK,
3855 (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
3856 DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
3857 " plane %d, " "cursor: %d\n",
3858 plane_wm, cursor_wm);
3859 enabled++;
3860 }
3861
3862 if (ironlake_compute_wm0(dev, 1,
3863 &sandybridge_display_wm_info, latency,
3864 &sandybridge_cursor_wm_info, latency,
3865 &plane_wm, &cursor_wm)) {
3866 I915_WRITE(WM0_PIPEB_ILK,
3867 (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
3868 DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
3869 " plane %d, cursor: %d\n",
3870 plane_wm, cursor_wm);
3871 enabled++;
3872 }
3873
3874 /*
3875 * Calculate and update the self-refresh watermark only when one
3876 * display plane is used.
3877 *
3878 * SNB support 3 levels of watermark.
3879 *
3880 * WM1/WM2/WM2 watermarks have to be enabled in the ascending order,
3881 * and disabled in the descending order
3882 *
3883 */
3884 I915_WRITE(WM3_LP_ILK, 0);
3885 I915_WRITE(WM2_LP_ILK, 0);
3886 I915_WRITE(WM1_LP_ILK, 0);
3887
3888 if (enabled != 1)
3889 return;
3890
3891 clock = planea_clock ? planea_clock : planeb_clock;
3892
3893 /* WM1 */
Jesse Barnesb79d4992010-12-21 13:10:23 -08003894 if (!ironlake_compute_srwm(dev, 1, hdisplay, htotal, pixel_size,
3895 clock, SNB_READ_WM1_LATENCY() * 500,
3896 &sandybridge_display_srwm_info,
3897 &sandybridge_cursor_srwm_info,
3898 &fbc_wm, &plane_wm, &cursor_wm))
Yuanhan Liu13982612010-12-15 15:42:31 +08003899 return;
3900
3901 I915_WRITE(WM1_LP_ILK,
3902 WM1_LP_SR_EN |
3903 (SNB_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
3904 (fbc_wm << WM1_LP_FBC_SHIFT) |
3905 (plane_wm << WM1_LP_SR_SHIFT) |
3906 cursor_wm);
3907
3908 /* WM2 */
Jesse Barnesb79d4992010-12-21 13:10:23 -08003909 if (!ironlake_compute_srwm(dev, 2,
3910 hdisplay, htotal, pixel_size,
3911 clock, SNB_READ_WM2_LATENCY() * 500,
3912 &sandybridge_display_srwm_info,
3913 &sandybridge_cursor_srwm_info,
3914 &fbc_wm, &plane_wm, &cursor_wm))
Yuanhan Liu13982612010-12-15 15:42:31 +08003915 return;
3916
3917 I915_WRITE(WM2_LP_ILK,
3918 WM2_LP_EN |
3919 (SNB_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
3920 (fbc_wm << WM1_LP_FBC_SHIFT) |
3921 (plane_wm << WM1_LP_SR_SHIFT) |
3922 cursor_wm);
3923
3924 /* WM3 */
Jesse Barnesb79d4992010-12-21 13:10:23 -08003925 if (!ironlake_compute_srwm(dev, 3,
3926 hdisplay, htotal, pixel_size,
3927 clock, SNB_READ_WM3_LATENCY() * 500,
3928 &sandybridge_display_srwm_info,
3929 &sandybridge_cursor_srwm_info,
3930 &fbc_wm, &plane_wm, &cursor_wm))
Yuanhan Liu13982612010-12-15 15:42:31 +08003931 return;
3932
3933 I915_WRITE(WM3_LP_ILK,
3934 WM3_LP_EN |
3935 (SNB_READ_WM3_LATENCY() << WM1_LP_LATENCY_SHIFT) |
3936 (fbc_wm << WM1_LP_FBC_SHIFT) |
3937 (plane_wm << WM1_LP_SR_SHIFT) |
3938 cursor_wm);
3939}
3940
Shaohua Li7662c8b2009-06-26 11:23:55 +08003941/**
3942 * intel_update_watermarks - update FIFO watermark values based on current modes
3943 *
3944 * Calculate watermark values for the various WM regs based on current mode
3945 * and plane configuration.
3946 *
3947 * There are several cases to deal with here:
3948 * - normal (i.e. non-self-refresh)
3949 * - self-refresh (SR) mode
3950 * - lines are large relative to FIFO size (buffer can hold up to 2)
3951 * - lines are small relative to FIFO size (buffer can hold more than 2
3952 * lines), so need to account for TLB latency
3953 *
3954 * The normal calculation is:
3955 * watermark = dotclock * bytes per pixel * latency
3956 * where latency is platform & configuration dependent (we assume pessimal
3957 * values here).
3958 *
3959 * The SR calculation is:
3960 * watermark = (trunc(latency/line time)+1) * surface width *
3961 * bytes per pixel
3962 * where
3963 * line time = htotal / dotclock
Zhao Yakuifa143212010-06-12 14:32:23 +08003964 * surface width = hdisplay for normal plane and 64 for cursor
Shaohua Li7662c8b2009-06-26 11:23:55 +08003965 * and latency is assumed to be high, as above.
3966 *
3967 * The final value programmed to the register should always be rounded up,
3968 * and include an extra 2 entries to account for clock crossings.
3969 *
3970 * We don't use the sprite, so we can ignore that. And on Crestline we have
3971 * to set the non-SR watermarks to 8.
Chris Wilson5eddb702010-09-11 13:48:45 +01003972 */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003973static void intel_update_watermarks(struct drm_device *dev)
3974{
Jesse Barnese70236a2009-09-21 10:42:27 -07003975 struct drm_i915_private *dev_priv = dev->dev_private;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003976 struct drm_crtc *crtc;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003977 int sr_hdisplay = 0;
3978 unsigned long planea_clock = 0, planeb_clock = 0, sr_clock = 0;
3979 int enabled = 0, pixel_size = 0;
Zhao Yakuifa143212010-06-12 14:32:23 +08003980 int sr_htotal = 0;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003981
Zhenyu Wangc03342f2009-09-29 11:01:23 +08003982 if (!dev_priv->display.update_wm)
3983 return;
3984
Shaohua Li7662c8b2009-06-26 11:23:55 +08003985 /* Get the clock config from both planes */
3986 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Chris Wilsondebcadd2010-08-07 11:01:33 +01003987 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003988 if (intel_crtc->active) {
Shaohua Li7662c8b2009-06-26 11:23:55 +08003989 enabled++;
3990 if (intel_crtc->plane == 0) {
Zhao Yakui28c97732009-10-09 11:39:41 +08003991 DRM_DEBUG_KMS("plane A (pipe %d) clock: %d\n",
Chris Wilson5eddb702010-09-11 13:48:45 +01003992 intel_crtc->pipe, crtc->mode.clock);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003993 planea_clock = crtc->mode.clock;
3994 } else {
Zhao Yakui28c97732009-10-09 11:39:41 +08003995 DRM_DEBUG_KMS("plane B (pipe %d) clock: %d\n",
Chris Wilson5eddb702010-09-11 13:48:45 +01003996 intel_crtc->pipe, crtc->mode.clock);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003997 planeb_clock = crtc->mode.clock;
3998 }
3999 sr_hdisplay = crtc->mode.hdisplay;
4000 sr_clock = crtc->mode.clock;
Zhao Yakuifa143212010-06-12 14:32:23 +08004001 sr_htotal = crtc->mode.htotal;
Shaohua Li7662c8b2009-06-26 11:23:55 +08004002 if (crtc->fb)
4003 pixel_size = crtc->fb->bits_per_pixel / 8;
4004 else
4005 pixel_size = 4; /* by default */
4006 }
4007 }
4008
4009 if (enabled <= 0)
4010 return;
4011
Jesse Barnese70236a2009-09-21 10:42:27 -07004012 dev_priv->display.update_wm(dev, planea_clock, planeb_clock,
Zhao Yakuifa143212010-06-12 14:32:23 +08004013 sr_hdisplay, sr_htotal, pixel_size);
Shaohua Li7662c8b2009-06-26 11:23:55 +08004014}
4015
Chris Wilsona7615032011-01-12 17:04:08 +00004016static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
4017{
4018 return dev_priv->lvds_use_ssc && i915_panel_use_ssc;
4019}
4020
Chris Wilson5c3b82e2009-02-11 13:25:09 +00004021static int intel_crtc_mode_set(struct drm_crtc *crtc,
4022 struct drm_display_mode *mode,
4023 struct drm_display_mode *adjusted_mode,
4024 int x, int y,
4025 struct drm_framebuffer *old_fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08004026{
4027 struct drm_device *dev = crtc->dev;
4028 struct drm_i915_private *dev_priv = dev->dev_private;
4029 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4030 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07004031 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01004032 u32 fp_reg, dpll_reg;
Eric Anholtc751ce42010-03-25 11:48:48 -07004033 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07004034 intel_clock_t clock, reduced_clock;
Chris Wilson5eddb702010-09-11 13:48:45 +01004035 u32 dpll, fp = 0, fp2 = 0, dspcntr, pipeconf;
Jesse Barnes652c3932009-08-17 13:31:43 -07004036 bool ok, has_reduced_clock = false, is_sdvo = false, is_dvo = false;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004037 bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
Chris Wilson8e647a22010-08-22 10:54:23 +01004038 struct intel_encoder *has_edp_encoder = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08004039 struct drm_mode_config *mode_config = &dev->mode_config;
Chris Wilson5eddb702010-09-11 13:48:45 +01004040 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08004041 const intel_limit_t *limit;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00004042 int ret;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004043 struct fdi_m_n m_n = {0};
Chris Wilson5eddb702010-09-11 13:48:45 +01004044 u32 reg, temp;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08004045 int target_clock;
Jesse Barnes79e53942008-11-07 14:24:08 -08004046
4047 drm_vblank_pre_modeset(dev, pipe);
4048
Chris Wilson5eddb702010-09-11 13:48:45 +01004049 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
4050 if (encoder->base.crtc != crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004051 continue;
4052
Chris Wilson5eddb702010-09-11 13:48:45 +01004053 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004054 case INTEL_OUTPUT_LVDS:
4055 is_lvds = true;
4056 break;
4057 case INTEL_OUTPUT_SDVO:
Eric Anholt7d573822009-01-02 13:33:00 -08004058 case INTEL_OUTPUT_HDMI:
Jesse Barnes79e53942008-11-07 14:24:08 -08004059 is_sdvo = true;
Chris Wilson5eddb702010-09-11 13:48:45 +01004060 if (encoder->needs_tv_clock)
Jesse Barnese2f0ba92009-02-02 15:11:52 -08004061 is_tv = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08004062 break;
4063 case INTEL_OUTPUT_DVO:
4064 is_dvo = true;
4065 break;
4066 case INTEL_OUTPUT_TVOUT:
4067 is_tv = true;
4068 break;
4069 case INTEL_OUTPUT_ANALOG:
4070 is_crt = true;
4071 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004072 case INTEL_OUTPUT_DISPLAYPORT:
4073 is_dp = true;
4074 break;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004075 case INTEL_OUTPUT_EDP:
Chris Wilson5eddb702010-09-11 13:48:45 +01004076 has_edp_encoder = encoder;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004077 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08004078 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05004079
Eric Anholtc751ce42010-03-25 11:48:48 -07004080 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08004081 }
4082
Chris Wilsona7615032011-01-12 17:04:08 +00004083 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Kristian Høgsberg43565a02009-02-13 20:56:52 -05004084 refclk = dev_priv->lvds_ssc_freq * 1000;
Zhao Yakui28c97732009-10-09 11:39:41 +08004085 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
Chris Wilson5eddb702010-09-11 13:48:45 +01004086 refclk / 1000);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004087 } else if (!IS_GEN2(dev)) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004088 refclk = 96000;
Jesse Barnes1cb1b752010-10-07 16:01:17 -07004089 if (HAS_PCH_SPLIT(dev) &&
4090 (!has_edp_encoder || intel_encoder_is_pch_edp(&has_edp_encoder->base)))
Zhenyu Wang2c072452009-06-05 15:38:42 +08004091 refclk = 120000; /* 120Mhz refclk */
Jesse Barnes79e53942008-11-07 14:24:08 -08004092 } else {
4093 refclk = 48000;
4094 }
4095
Ma Lingd4906092009-03-18 20:13:27 +08004096 /*
4097 * Returns a set of divisors for the desired target clock with the given
4098 * refclk, or FALSE. The returned values represent the clock equation:
4099 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
4100 */
Chris Wilson1b894b52010-12-14 20:04:54 +00004101 limit = intel_limit(crtc, refclk);
Ma Lingd4906092009-03-18 20:13:27 +08004102 ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08004103 if (!ok) {
4104 DRM_ERROR("Couldn't find PLL settings for mode!\n");
Chris Wilson1f803ee2009-06-06 09:45:59 +01004105 drm_vblank_post_modeset(dev, pipe);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00004106 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08004107 }
4108
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004109 /* Ensure that the cursor is valid for the new mode before changing... */
Chris Wilson6b383a72010-09-13 13:54:26 +01004110 intel_crtc_update_cursor(crtc, true);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004111
Zhao Yakuiddc90032010-01-06 22:05:56 +08004112 if (is_lvds && dev_priv->lvds_downclock_avail) {
4113 has_reduced_clock = limit->find_pll(limit, crtc,
Chris Wilson5eddb702010-09-11 13:48:45 +01004114 dev_priv->lvds_downclock,
4115 refclk,
4116 &reduced_clock);
Zhao Yakui18f9ed12009-11-20 03:24:16 +00004117 if (has_reduced_clock && (clock.p != reduced_clock.p)) {
4118 /*
4119 * If the different P is found, it means that we can't
4120 * switch the display clock by using the FP0/FP1.
4121 * In such case we will disable the LVDS downclock
4122 * feature.
4123 */
4124 DRM_DEBUG_KMS("Different P is found for "
Chris Wilson5eddb702010-09-11 13:48:45 +01004125 "LVDS clock/downclock\n");
Zhao Yakui18f9ed12009-11-20 03:24:16 +00004126 has_reduced_clock = 0;
4127 }
Jesse Barnes652c3932009-08-17 13:31:43 -07004128 }
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08004129 /* SDVO TV has fixed PLL values depend on its clock range,
4130 this mirrors vbios setting. */
4131 if (is_sdvo && is_tv) {
4132 if (adjusted_mode->clock >= 100000
Chris Wilson5eddb702010-09-11 13:48:45 +01004133 && adjusted_mode->clock < 140500) {
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08004134 clock.p1 = 2;
4135 clock.p2 = 10;
4136 clock.n = 3;
4137 clock.m1 = 16;
4138 clock.m2 = 8;
4139 } else if (adjusted_mode->clock >= 140500
Chris Wilson5eddb702010-09-11 13:48:45 +01004140 && adjusted_mode->clock <= 200000) {
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08004141 clock.p1 = 1;
4142 clock.p2 = 10;
4143 clock.n = 6;
4144 clock.m1 = 12;
4145 clock.m2 = 8;
4146 }
4147 }
4148
Zhenyu Wang2c072452009-06-05 15:38:42 +08004149 /* FDI link */
Eric Anholtbad720f2009-10-22 16:11:14 -07004150 if (HAS_PCH_SPLIT(dev)) {
Chris Wilson49078f72010-12-04 07:45:57 +00004151 int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
Adam Jackson77ffb592010-04-12 11:38:44 -04004152 int lane = 0, link_bw, bpp;
Jesse Barnes5c5313c2010-10-07 16:01:11 -07004153 /* CPU eDP doesn't require FDI link, so just set DP M/N
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004154 according to current link config */
Jesse Barnes858bc212011-01-04 10:46:49 -08004155 if (has_edp_encoder && !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08004156 target_clock = mode->clock;
Chris Wilson8e647a22010-08-22 10:54:23 +01004157 intel_edp_link_config(has_edp_encoder,
4158 &lane, &link_bw);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004159 } else {
Jesse Barnes5c5313c2010-10-07 16:01:11 -07004160 /* [e]DP over FDI requires target mode clock
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004161 instead of link clock */
Jesse Barnes5c5313c2010-10-07 16:01:11 -07004162 if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004163 target_clock = mode->clock;
4164 else
4165 target_clock = adjusted_mode->clock;
Chris Wilson021357a2010-09-07 20:54:59 +01004166
4167 /* FDI is a binary signal running at ~2.7GHz, encoding
4168 * each output octet as 10 bits. The actual frequency
4169 * is stored as a divider into a 100MHz clock, and the
4170 * mode pixel clock is stored in units of 1KHz.
4171 * Hence the bw of each lane in terms of the mode signal
4172 * is:
4173 */
4174 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004175 }
Zhenyu Wang58a27472009-09-25 08:01:28 +00004176
4177 /* determine panel color depth */
Chris Wilson5eddb702010-09-11 13:48:45 +01004178 temp = I915_READ(PIPECONF(pipe));
Zhao Yakuie5a95eb2010-01-04 16:29:32 +08004179 temp &= ~PIPE_BPC_MASK;
4180 if (is_lvds) {
Zhao Yakuie5a95eb2010-01-04 16:29:32 +08004181 /* the BPC will be 6 if it is 18-bit LVDS panel */
Chris Wilson5eddb702010-09-11 13:48:45 +01004182 if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) == LVDS_A3_POWER_UP)
Zhao Yakuie5a95eb2010-01-04 16:29:32 +08004183 temp |= PIPE_8BPC;
4184 else
4185 temp |= PIPE_6BPC;
Jesse Barnes1d850362010-10-07 16:01:10 -07004186 } else if (has_edp_encoder) {
Chris Wilson5ceb0f92010-09-24 10:24:28 +01004187 switch (dev_priv->edp.bpp/3) {
Zhenyu Wang885a5fb2010-01-12 05:38:31 +08004188 case 8:
4189 temp |= PIPE_8BPC;
4190 break;
4191 case 10:
4192 temp |= PIPE_10BPC;
4193 break;
4194 case 6:
4195 temp |= PIPE_6BPC;
4196 break;
4197 case 12:
4198 temp |= PIPE_12BPC;
4199 break;
4200 }
Zhao Yakuie5a95eb2010-01-04 16:29:32 +08004201 } else
4202 temp |= PIPE_8BPC;
Chris Wilson5eddb702010-09-11 13:48:45 +01004203 I915_WRITE(PIPECONF(pipe), temp);
Zhenyu Wang58a27472009-09-25 08:01:28 +00004204
4205 switch (temp & PIPE_BPC_MASK) {
4206 case PIPE_8BPC:
4207 bpp = 24;
4208 break;
4209 case PIPE_10BPC:
4210 bpp = 30;
4211 break;
4212 case PIPE_6BPC:
4213 bpp = 18;
4214 break;
4215 case PIPE_12BPC:
4216 bpp = 36;
4217 break;
4218 default:
4219 DRM_ERROR("unknown pipe bpc value\n");
4220 bpp = 24;
4221 }
4222
Adam Jackson77ffb592010-04-12 11:38:44 -04004223 if (!lane) {
4224 /*
4225 * Account for spread spectrum to avoid
4226 * oversubscribing the link. Max center spread
4227 * is 2.5%; use 5% for safety's sake.
4228 */
4229 u32 bps = target_clock * bpp * 21 / 20;
4230 lane = bps / (link_bw * 8) + 1;
4231 }
4232
4233 intel_crtc->fdi_lanes = lane;
4234
Chris Wilson49078f72010-12-04 07:45:57 +00004235 if (pixel_multiplier > 1)
4236 link_bw *= pixel_multiplier;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05004237 ironlake_compute_m_n(bpp, lane, target_clock, link_bw, &m_n);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08004238 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08004239
Zhenyu Wangc038e512009-10-19 15:43:48 +08004240 /* Ironlake: try to setup display ref clock before DPLL
4241 * enabling. This is only under driver's control after
4242 * PCH B stepping, previous chipset stepping should be
4243 * ignoring this setting.
4244 */
Eric Anholtbad720f2009-10-22 16:11:14 -07004245 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wangc038e512009-10-19 15:43:48 +08004246 temp = I915_READ(PCH_DREF_CONTROL);
4247 /* Always enable nonspread source */
4248 temp &= ~DREF_NONSPREAD_SOURCE_MASK;
4249 temp |= DREF_NONSPREAD_SOURCE_ENABLE;
Zhenyu Wangc038e512009-10-19 15:43:48 +08004250 temp &= ~DREF_SSC_SOURCE_MASK;
4251 temp |= DREF_SSC_SOURCE_ENABLE;
4252 I915_WRITE(PCH_DREF_CONTROL, temp);
Zhenyu Wangc038e512009-10-19 15:43:48 +08004253
Chris Wilson5eddb702010-09-11 13:48:45 +01004254 POSTING_READ(PCH_DREF_CONTROL);
Zhenyu Wangc038e512009-10-19 15:43:48 +08004255 udelay(200);
4256
Chris Wilson8e647a22010-08-22 10:54:23 +01004257 if (has_edp_encoder) {
Chris Wilsona7615032011-01-12 17:04:08 +00004258 if (intel_panel_use_ssc(dev_priv)) {
Zhenyu Wangc038e512009-10-19 15:43:48 +08004259 temp |= DREF_SSC1_ENABLE;
4260 I915_WRITE(PCH_DREF_CONTROL, temp);
Zhenyu Wangc038e512009-10-19 15:43:48 +08004261
Chris Wilson5eddb702010-09-11 13:48:45 +01004262 POSTING_READ(PCH_DREF_CONTROL);
Zhenyu Wangc038e512009-10-19 15:43:48 +08004263 udelay(200);
Jesse Barnes7f823282010-10-07 16:01:16 -07004264 }
4265 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Zhenyu Wangc038e512009-10-19 15:43:48 +08004266
Jesse Barnes7f823282010-10-07 16:01:16 -07004267 /* Enable CPU source on CPU attached eDP */
4268 if (!intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
Chris Wilsona7615032011-01-12 17:04:08 +00004269 if (intel_panel_use_ssc(dev_priv))
Jesse Barnes7f823282010-10-07 16:01:16 -07004270 temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
4271 else
4272 temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Zhenyu Wangc038e512009-10-19 15:43:48 +08004273 } else {
Jesse Barnes7f823282010-10-07 16:01:16 -07004274 /* Enable SSC on PCH eDP if needed */
Chris Wilsona7615032011-01-12 17:04:08 +00004275 if (intel_panel_use_ssc(dev_priv)) {
Jesse Barnes7f823282010-10-07 16:01:16 -07004276 DRM_ERROR("enabling SSC on PCH\n");
4277 temp |= DREF_SUPERSPREAD_SOURCE_ENABLE;
4278 }
Zhenyu Wangc038e512009-10-19 15:43:48 +08004279 }
Chris Wilson5eddb702010-09-11 13:48:45 +01004280 I915_WRITE(PCH_DREF_CONTROL, temp);
Jesse Barnes7f823282010-10-07 16:01:16 -07004281 POSTING_READ(PCH_DREF_CONTROL);
4282 udelay(200);
Zhenyu Wangc038e512009-10-19 15:43:48 +08004283 }
4284 }
4285
Adam Jacksonf2b115e2009-12-03 17:14:42 -05004286 if (IS_PINEVIEW(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +08004287 fp = (1 << clock.n) << 16 | clock.m1 << 8 | clock.m2;
Jesse Barnes652c3932009-08-17 13:31:43 -07004288 if (has_reduced_clock)
4289 fp2 = (1 << reduced_clock.n) << 16 |
4290 reduced_clock.m1 << 8 | reduced_clock.m2;
4291 } else {
Shaohua Li21778322009-02-23 15:19:16 +08004292 fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
Jesse Barnes652c3932009-08-17 13:31:43 -07004293 if (has_reduced_clock)
4294 fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
4295 reduced_clock.m2;
4296 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004297
Chris Wilsonc1858122010-12-03 21:35:48 +00004298 /* Enable autotuning of the PLL clock (if permissible) */
4299 if (HAS_PCH_SPLIT(dev)) {
4300 int factor = 21;
4301
4302 if (is_lvds) {
Chris Wilsona7615032011-01-12 17:04:08 +00004303 if ((intel_panel_use_ssc(dev_priv) &&
Chris Wilsonc1858122010-12-03 21:35:48 +00004304 dev_priv->lvds_ssc_freq == 100) ||
4305 (I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP)
4306 factor = 25;
4307 } else if (is_sdvo && is_tv)
4308 factor = 20;
4309
4310 if (clock.m1 < factor * clock.n)
4311 fp |= FP_CB_TUNE;
4312 }
4313
Chris Wilson5eddb702010-09-11 13:48:45 +01004314 dpll = 0;
Eric Anholtbad720f2009-10-22 16:11:14 -07004315 if (!HAS_PCH_SPLIT(dev))
Zhenyu Wang2c072452009-06-05 15:38:42 +08004316 dpll = DPLL_VGA_MODE_DIS;
4317
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004318 if (!IS_GEN2(dev)) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004319 if (is_lvds)
4320 dpll |= DPLLB_MODE_LVDS;
4321 else
4322 dpll |= DPLLB_MODE_DAC_SERIAL;
4323 if (is_sdvo) {
Chris Wilson6c9547f2010-08-25 10:05:17 +01004324 int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
4325 if (pixel_multiplier > 1) {
4326 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4327 dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
4328 else if (HAS_PCH_SPLIT(dev))
4329 dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
4330 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004331 dpll |= DPLL_DVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08004332 }
Jesse Barnes83240122010-10-07 16:01:18 -07004333 if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004334 dpll |= DPLL_DVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08004335
4336 /* compute bitmask from p1 value */
Adam Jacksonf2b115e2009-12-03 17:14:42 -05004337 if (IS_PINEVIEW(dev))
4338 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004339 else {
Shaohua Li21778322009-02-23 15:19:16 +08004340 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004341 /* also FPA1 */
Eric Anholtbad720f2009-10-22 16:11:14 -07004342 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang2c072452009-06-05 15:38:42 +08004343 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Jesse Barnes652c3932009-08-17 13:31:43 -07004344 if (IS_G4X(dev) && has_reduced_clock)
4345 dpll |= (1 << (reduced_clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004346 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004347 switch (clock.p2) {
4348 case 5:
4349 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4350 break;
4351 case 7:
4352 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4353 break;
4354 case 10:
4355 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4356 break;
4357 case 14:
4358 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4359 break;
4360 }
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004361 if (INTEL_INFO(dev)->gen >= 4 && !HAS_PCH_SPLIT(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08004362 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
4363 } else {
4364 if (is_lvds) {
4365 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4366 } else {
4367 if (clock.p1 == 2)
4368 dpll |= PLL_P1_DIVIDE_BY_TWO;
4369 else
4370 dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4371 if (clock.p2 == 4)
4372 dpll |= PLL_P2_DIVIDE_BY_4;
4373 }
4374 }
4375
Kristian Høgsberg43565a02009-02-13 20:56:52 -05004376 if (is_sdvo && is_tv)
4377 dpll |= PLL_REF_INPUT_TVCLKINBC;
4378 else if (is_tv)
Jesse Barnes79e53942008-11-07 14:24:08 -08004379 /* XXX: just matching BIOS for now */
Kristian Høgsberg43565a02009-02-13 20:56:52 -05004380 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
Jesse Barnes79e53942008-11-07 14:24:08 -08004381 dpll |= 3;
Chris Wilsona7615032011-01-12 17:04:08 +00004382 else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05004383 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08004384 else
4385 dpll |= PLL_REF_INPUT_DREFCLK;
4386
4387 /* setup pipeconf */
Chris Wilson5eddb702010-09-11 13:48:45 +01004388 pipeconf = I915_READ(PIPECONF(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08004389
4390 /* Set up the display plane register */
4391 dspcntr = DISPPLANE_GAMMA_ENABLE;
4392
Adam Jacksonf2b115e2009-12-03 17:14:42 -05004393 /* Ironlake's plane is forced to pipe, bit 24 is to
Zhenyu Wang2c072452009-06-05 15:38:42 +08004394 enable color space conversion */
Eric Anholtbad720f2009-10-22 16:11:14 -07004395 if (!HAS_PCH_SPLIT(dev)) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08004396 if (pipe == 0)
Jesse Barnes80824002009-09-10 15:28:06 -07004397 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004398 else
4399 dspcntr |= DISPPLANE_SEL_PIPE_B;
4400 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004401
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004402 if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004403 /* Enable pixel doubling when the dot clock is > 90% of the (display)
4404 * core speed.
4405 *
4406 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
4407 * pipe == 0 check?
4408 */
Jesse Barnese70236a2009-09-21 10:42:27 -07004409 if (mode->clock >
4410 dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
Chris Wilson5eddb702010-09-11 13:48:45 +01004411 pipeconf |= PIPECONF_DOUBLE_WIDE;
Jesse Barnes79e53942008-11-07 14:24:08 -08004412 else
Chris Wilson5eddb702010-09-11 13:48:45 +01004413 pipeconf &= ~PIPECONF_DOUBLE_WIDE;
Jesse Barnes79e53942008-11-07 14:24:08 -08004414 }
4415
Jesse Barnesb24e7172011-01-04 15:09:30 -08004416 if (!HAS_PCH_SPLIT(dev))
Jesse Barnes65993d62011-01-04 15:09:29 -08004417 dpll |= DPLL_VCO_ENABLE;
Linus Torvalds8d86dc62010-06-08 20:16:28 -07004418
Zhao Yakui28c97732009-10-09 11:39:41 +08004419 DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
Jesse Barnes79e53942008-11-07 14:24:08 -08004420 drm_mode_debug_printmodeline(mode);
4421
Adam Jacksonf2b115e2009-12-03 17:14:42 -05004422 /* assign to Ironlake registers */
Eric Anholtbad720f2009-10-22 16:11:14 -07004423 if (HAS_PCH_SPLIT(dev)) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004424 fp_reg = PCH_FP0(pipe);
4425 dpll_reg = PCH_DPLL(pipe);
4426 } else {
4427 fp_reg = FP0(pipe);
4428 dpll_reg = DPLL(pipe);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004429 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004430
Jesse Barnes5c5313c2010-10-07 16:01:11 -07004431 /* PCH eDP needs FDI, but CPU eDP does not */
4432 if (!has_edp_encoder || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004433 I915_WRITE(fp_reg, fp);
4434 I915_WRITE(dpll_reg, dpll & ~DPLL_VCO_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01004435
4436 POSTING_READ(dpll_reg);
Jesse Barnes79e53942008-11-07 14:24:08 -08004437 udelay(150);
4438 }
4439
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004440 /* enable transcoder DPLL */
4441 if (HAS_PCH_CPT(dev)) {
4442 temp = I915_READ(PCH_DPLL_SEL);
Chris Wilson5eddb702010-09-11 13:48:45 +01004443 if (pipe == 0)
4444 temp |= TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004445 else
Chris Wilson5eddb702010-09-11 13:48:45 +01004446 temp |= TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004447 I915_WRITE(PCH_DPLL_SEL, temp);
Chris Wilson5eddb702010-09-11 13:48:45 +01004448
4449 POSTING_READ(PCH_DPLL_SEL);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004450 udelay(150);
4451 }
4452
Jesse Barnes79e53942008-11-07 14:24:08 -08004453 /* The LVDS pin pair needs to be on before the DPLLs are enabled.
4454 * This is an exception to the general rule that mode_set doesn't turn
4455 * things on.
4456 */
4457 if (is_lvds) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004458 reg = LVDS;
Eric Anholtbad720f2009-10-22 16:11:14 -07004459 if (HAS_PCH_SPLIT(dev))
Chris Wilson5eddb702010-09-11 13:48:45 +01004460 reg = PCH_LVDS;
Zhenyu Wang541998a2009-06-05 15:38:44 +08004461
Chris Wilson5eddb702010-09-11 13:48:45 +01004462 temp = I915_READ(reg);
4463 temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
Zhenyu Wangb3b095b2010-04-07 16:15:56 +08004464 if (pipe == 1) {
4465 if (HAS_PCH_CPT(dev))
Chris Wilson5eddb702010-09-11 13:48:45 +01004466 temp |= PORT_TRANS_B_SEL_CPT;
Zhenyu Wangb3b095b2010-04-07 16:15:56 +08004467 else
Chris Wilson5eddb702010-09-11 13:48:45 +01004468 temp |= LVDS_PIPEB_SELECT;
Zhenyu Wangb3b095b2010-04-07 16:15:56 +08004469 } else {
4470 if (HAS_PCH_CPT(dev))
Chris Wilson5eddb702010-09-11 13:48:45 +01004471 temp &= ~PORT_TRANS_SEL_MASK;
Zhenyu Wangb3b095b2010-04-07 16:15:56 +08004472 else
Chris Wilson5eddb702010-09-11 13:48:45 +01004473 temp &= ~LVDS_PIPEB_SELECT;
Zhenyu Wangb3b095b2010-04-07 16:15:56 +08004474 }
Zhao Yakuia3e17eb2009-10-10 10:42:37 +08004475 /* set the corresponsding LVDS_BORDER bit */
Chris Wilson5eddb702010-09-11 13:48:45 +01004476 temp |= dev_priv->lvds_border_bits;
Jesse Barnes79e53942008-11-07 14:24:08 -08004477 /* Set the B0-B3 data pairs corresponding to whether we're going to
4478 * set the DPLLs for dual-channel mode or not.
4479 */
4480 if (clock.p2 == 7)
Chris Wilson5eddb702010-09-11 13:48:45 +01004481 temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
Jesse Barnes79e53942008-11-07 14:24:08 -08004482 else
Chris Wilson5eddb702010-09-11 13:48:45 +01004483 temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
Jesse Barnes79e53942008-11-07 14:24:08 -08004484
4485 /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
4486 * appropriately here, but we need to look more thoroughly into how
4487 * panels behave in the two modes.
4488 */
Jesse Barnes434ed092010-09-07 14:48:06 -07004489 /* set the dithering flag on non-PCH LVDS as needed */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004490 if (INTEL_INFO(dev)->gen >= 4 && !HAS_PCH_SPLIT(dev)) {
Jesse Barnes434ed092010-09-07 14:48:06 -07004491 if (dev_priv->lvds_dither)
Chris Wilson5eddb702010-09-11 13:48:45 +01004492 temp |= LVDS_ENABLE_DITHER;
Jesse Barnes434ed092010-09-07 14:48:06 -07004493 else
Chris Wilson5eddb702010-09-11 13:48:45 +01004494 temp &= ~LVDS_ENABLE_DITHER;
Zhao Yakui898822c2010-01-04 16:29:30 +08004495 }
Chris Wilson5eddb702010-09-11 13:48:45 +01004496 I915_WRITE(reg, temp);
Jesse Barnes79e53942008-11-07 14:24:08 -08004497 }
Jesse Barnes434ed092010-09-07 14:48:06 -07004498
4499 /* set the dithering flag and clear for anything other than a panel. */
4500 if (HAS_PCH_SPLIT(dev)) {
4501 pipeconf &= ~PIPECONF_DITHER_EN;
4502 pipeconf &= ~PIPECONF_DITHER_TYPE_MASK;
4503 if (dev_priv->lvds_dither && (is_lvds || has_edp_encoder)) {
4504 pipeconf |= PIPECONF_DITHER_EN;
4505 pipeconf |= PIPECONF_DITHER_TYPE_ST1;
4506 }
4507 }
4508
Jesse Barnes5c5313c2010-10-07 16:01:11 -07004509 if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004510 intel_dp_set_m_n(crtc, mode, adjusted_mode);
Jesse Barnes5c5313c2010-10-07 16:01:11 -07004511 } else if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004512 /* For non-DP output, clear any trans DP clock recovery setting.*/
4513 if (pipe == 0) {
4514 I915_WRITE(TRANSA_DATA_M1, 0);
4515 I915_WRITE(TRANSA_DATA_N1, 0);
4516 I915_WRITE(TRANSA_DP_LINK_M1, 0);
4517 I915_WRITE(TRANSA_DP_LINK_N1, 0);
4518 } else {
4519 I915_WRITE(TRANSB_DATA_M1, 0);
4520 I915_WRITE(TRANSB_DATA_N1, 0);
4521 I915_WRITE(TRANSB_DP_LINK_M1, 0);
4522 I915_WRITE(TRANSB_DP_LINK_N1, 0);
4523 }
4524 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004525
Jesse Barnes5c5313c2010-10-07 16:01:11 -07004526 if (!has_edp_encoder || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004527 I915_WRITE(dpll_reg, dpll);
Chris Wilson5eddb702010-09-11 13:48:45 +01004528
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004529 /* Wait for the clocks to stabilize. */
Chris Wilson5eddb702010-09-11 13:48:45 +01004530 POSTING_READ(dpll_reg);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004531 udelay(150);
4532
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004533 if (INTEL_INFO(dev)->gen >= 4 && !HAS_PCH_SPLIT(dev)) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004534 temp = 0;
Zhao Yakuibb66c512009-09-10 15:45:49 +08004535 if (is_sdvo) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004536 temp = intel_mode_get_pixel_multiplier(adjusted_mode);
4537 if (temp > 1)
4538 temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Chris Wilson6c9547f2010-08-25 10:05:17 +01004539 else
Chris Wilson5eddb702010-09-11 13:48:45 +01004540 temp = 0;
4541 }
4542 I915_WRITE(DPLL_MD(pipe), temp);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004543 } else {
Chris Wilsona589b9f2010-12-03 21:13:16 +00004544 /* The pixel multiplier can only be updated once the
4545 * DPLL is enabled and the clocks are stable.
4546 *
4547 * So write it again.
4548 */
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004549 I915_WRITE(dpll_reg, dpll);
4550 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004551 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004552
Chris Wilson5eddb702010-09-11 13:48:45 +01004553 intel_crtc->lowfreq_avail = false;
Jesse Barnes652c3932009-08-17 13:31:43 -07004554 if (is_lvds && has_reduced_clock && i915_powersave) {
4555 I915_WRITE(fp_reg + 4, fp2);
4556 intel_crtc->lowfreq_avail = true;
4557 if (HAS_PIPE_CXSR(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08004558 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07004559 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
4560 }
4561 } else {
4562 I915_WRITE(fp_reg + 4, fp);
Jesse Barnes652c3932009-08-17 13:31:43 -07004563 if (HAS_PIPE_CXSR(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08004564 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07004565 pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
4566 }
4567 }
4568
Krzysztof Halasa734b4152010-05-25 18:41:46 +02004569 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4570 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
4571 /* the chip adds 2 halflines automatically */
4572 adjusted_mode->crtc_vdisplay -= 1;
4573 adjusted_mode->crtc_vtotal -= 1;
4574 adjusted_mode->crtc_vblank_start -= 1;
4575 adjusted_mode->crtc_vblank_end -= 1;
4576 adjusted_mode->crtc_vsync_end -= 1;
4577 adjusted_mode->crtc_vsync_start -= 1;
4578 } else
4579 pipeconf &= ~PIPECONF_INTERLACE_W_FIELD_INDICATION; /* progressive */
4580
Chris Wilson5eddb702010-09-11 13:48:45 +01004581 I915_WRITE(HTOTAL(pipe),
4582 (adjusted_mode->crtc_hdisplay - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08004583 ((adjusted_mode->crtc_htotal - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01004584 I915_WRITE(HBLANK(pipe),
4585 (adjusted_mode->crtc_hblank_start - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08004586 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01004587 I915_WRITE(HSYNC(pipe),
4588 (adjusted_mode->crtc_hsync_start - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08004589 ((adjusted_mode->crtc_hsync_end - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01004590
4591 I915_WRITE(VTOTAL(pipe),
4592 (adjusted_mode->crtc_vdisplay - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08004593 ((adjusted_mode->crtc_vtotal - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01004594 I915_WRITE(VBLANK(pipe),
4595 (adjusted_mode->crtc_vblank_start - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08004596 ((adjusted_mode->crtc_vblank_end - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01004597 I915_WRITE(VSYNC(pipe),
4598 (adjusted_mode->crtc_vsync_start - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08004599 ((adjusted_mode->crtc_vsync_end - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01004600
4601 /* pipesrc and dspsize control the size that is scaled from,
4602 * which should always be the user's requested size.
Jesse Barnes79e53942008-11-07 14:24:08 -08004603 */
Eric Anholtbad720f2009-10-22 16:11:14 -07004604 if (!HAS_PCH_SPLIT(dev)) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004605 I915_WRITE(DSPSIZE(plane),
4606 ((mode->vdisplay - 1) << 16) |
4607 (mode->hdisplay - 1));
4608 I915_WRITE(DSPPOS(plane), 0);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004609 }
Chris Wilson5eddb702010-09-11 13:48:45 +01004610 I915_WRITE(PIPESRC(pipe),
4611 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
Zhenyu Wang2c072452009-06-05 15:38:42 +08004612
Eric Anholtbad720f2009-10-22 16:11:14 -07004613 if (HAS_PCH_SPLIT(dev)) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004614 I915_WRITE(PIPE_DATA_M1(pipe), TU_SIZE(m_n.tu) | m_n.gmch_m);
4615 I915_WRITE(PIPE_DATA_N1(pipe), m_n.gmch_n);
4616 I915_WRITE(PIPE_LINK_M1(pipe), m_n.link_m);
4617 I915_WRITE(PIPE_LINK_N1(pipe), m_n.link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004618
Jesse Barnes5c5313c2010-10-07 16:01:11 -07004619 if (has_edp_encoder && !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05004620 ironlake_set_pll_edp(crtc, adjusted_mode->clock);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004621 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08004622 }
4623
Chris Wilson5eddb702010-09-11 13:48:45 +01004624 I915_WRITE(PIPECONF(pipe), pipeconf);
4625 POSTING_READ(PIPECONF(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08004626 if (!HAS_PCH_SPLIT(dev))
4627 intel_enable_pipe(dev_priv, pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08004628
Jesse Barnes9d0498a2010-08-18 13:20:54 -07004629 intel_wait_for_vblank(dev, pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08004630
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01004631 if (IS_GEN5(dev)) {
Zhenyu Wang553bd142009-09-02 10:57:52 +08004632 /* enable address swizzle for tiling buffer */
4633 temp = I915_READ(DISP_ARB_CTL);
4634 I915_WRITE(DISP_ARB_CTL, temp | DISP_TILE_SURFACE_SWIZZLING);
4635 }
4636
Chris Wilson5eddb702010-09-11 13:48:45 +01004637 I915_WRITE(DSPCNTR(plane), dspcntr);
Jesse Barnesb24e7172011-01-04 15:09:30 -08004638 POSTING_READ(DSPCNTR(plane));
4639 if (!HAS_PCH_SPLIT(dev))
4640 intel_enable_plane(dev_priv, plane, pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08004641
Chris Wilson5c3b82e2009-02-11 13:25:09 +00004642 ret = intel_pipe_set_base(crtc, x, y, old_fb);
Shaohua Li7662c8b2009-06-26 11:23:55 +08004643
4644 intel_update_watermarks(dev);
4645
Jesse Barnes79e53942008-11-07 14:24:08 -08004646 drm_vblank_post_modeset(dev, pipe);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00004647
Chris Wilson1f803ee2009-06-06 09:45:59 +01004648 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08004649}
4650
4651/** Loads the palette/gamma unit for the CRTC with the prepared values */
4652void intel_crtc_load_lut(struct drm_crtc *crtc)
4653{
4654 struct drm_device *dev = crtc->dev;
4655 struct drm_i915_private *dev_priv = dev->dev_private;
4656 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4657 int palreg = (intel_crtc->pipe == 0) ? PALETTE_A : PALETTE_B;
4658 int i;
4659
4660 /* The clocks have to be on to load the palette. */
4661 if (!crtc->enabled)
4662 return;
4663
Adam Jacksonf2b115e2009-12-03 17:14:42 -05004664 /* use legacy palette for Ironlake */
Eric Anholtbad720f2009-10-22 16:11:14 -07004665 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang2c072452009-06-05 15:38:42 +08004666 palreg = (intel_crtc->pipe == 0) ? LGC_PALETTE_A :
4667 LGC_PALETTE_B;
4668
Jesse Barnes79e53942008-11-07 14:24:08 -08004669 for (i = 0; i < 256; i++) {
4670 I915_WRITE(palreg + 4 * i,
4671 (intel_crtc->lut_r[i] << 16) |
4672 (intel_crtc->lut_g[i] << 8) |
4673 intel_crtc->lut_b[i]);
4674 }
4675}
4676
Chris Wilson560b85b2010-08-07 11:01:38 +01004677static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
4678{
4679 struct drm_device *dev = crtc->dev;
4680 struct drm_i915_private *dev_priv = dev->dev_private;
4681 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4682 bool visible = base != 0;
4683 u32 cntl;
4684
4685 if (intel_crtc->cursor_visible == visible)
4686 return;
4687
4688 cntl = I915_READ(CURACNTR);
4689 if (visible) {
4690 /* On these chipsets we can only modify the base whilst
4691 * the cursor is disabled.
4692 */
4693 I915_WRITE(CURABASE, base);
4694
4695 cntl &= ~(CURSOR_FORMAT_MASK);
4696 /* XXX width must be 64, stride 256 => 0x00 << 28 */
4697 cntl |= CURSOR_ENABLE |
4698 CURSOR_GAMMA_ENABLE |
4699 CURSOR_FORMAT_ARGB;
4700 } else
4701 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
4702 I915_WRITE(CURACNTR, cntl);
4703
4704 intel_crtc->cursor_visible = visible;
4705}
4706
4707static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
4708{
4709 struct drm_device *dev = crtc->dev;
4710 struct drm_i915_private *dev_priv = dev->dev_private;
4711 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4712 int pipe = intel_crtc->pipe;
4713 bool visible = base != 0;
4714
4715 if (intel_crtc->cursor_visible != visible) {
4716 uint32_t cntl = I915_READ(pipe == 0 ? CURACNTR : CURBCNTR);
4717 if (base) {
4718 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
4719 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
4720 cntl |= pipe << 28; /* Connect to correct pipe */
4721 } else {
4722 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
4723 cntl |= CURSOR_MODE_DISABLE;
4724 }
4725 I915_WRITE(pipe == 0 ? CURACNTR : CURBCNTR, cntl);
4726
4727 intel_crtc->cursor_visible = visible;
4728 }
4729 /* and commit changes on next vblank */
4730 I915_WRITE(pipe == 0 ? CURABASE : CURBBASE, base);
4731}
4732
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004733/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01004734static void intel_crtc_update_cursor(struct drm_crtc *crtc,
4735 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004736{
4737 struct drm_device *dev = crtc->dev;
4738 struct drm_i915_private *dev_priv = dev->dev_private;
4739 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4740 int pipe = intel_crtc->pipe;
4741 int x = intel_crtc->cursor_x;
4742 int y = intel_crtc->cursor_y;
Chris Wilson560b85b2010-08-07 11:01:38 +01004743 u32 base, pos;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004744 bool visible;
4745
4746 pos = 0;
4747
Chris Wilson6b383a72010-09-13 13:54:26 +01004748 if (on && crtc->enabled && crtc->fb) {
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004749 base = intel_crtc->cursor_addr;
4750 if (x > (int) crtc->fb->width)
4751 base = 0;
4752
4753 if (y > (int) crtc->fb->height)
4754 base = 0;
4755 } else
4756 base = 0;
4757
4758 if (x < 0) {
4759 if (x + intel_crtc->cursor_width < 0)
4760 base = 0;
4761
4762 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
4763 x = -x;
4764 }
4765 pos |= x << CURSOR_X_SHIFT;
4766
4767 if (y < 0) {
4768 if (y + intel_crtc->cursor_height < 0)
4769 base = 0;
4770
4771 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
4772 y = -y;
4773 }
4774 pos |= y << CURSOR_Y_SHIFT;
4775
4776 visible = base != 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01004777 if (!visible && !intel_crtc->cursor_visible)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004778 return;
4779
4780 I915_WRITE(pipe == 0 ? CURAPOS : CURBPOS, pos);
Chris Wilson560b85b2010-08-07 11:01:38 +01004781 if (IS_845G(dev) || IS_I865G(dev))
4782 i845_update_cursor(crtc, base);
4783 else
4784 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004785
4786 if (visible)
4787 intel_mark_busy(dev, to_intel_framebuffer(crtc->fb)->obj);
4788}
4789
Jesse Barnes79e53942008-11-07 14:24:08 -08004790static int intel_crtc_cursor_set(struct drm_crtc *crtc,
Chris Wilson05394f32010-11-08 19:18:58 +00004791 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08004792 uint32_t handle,
4793 uint32_t width, uint32_t height)
4794{
4795 struct drm_device *dev = crtc->dev;
4796 struct drm_i915_private *dev_priv = dev->dev_private;
4797 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00004798 struct drm_i915_gem_object *obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004799 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004800 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08004801
Zhao Yakui28c97732009-10-09 11:39:41 +08004802 DRM_DEBUG_KMS("\n");
Jesse Barnes79e53942008-11-07 14:24:08 -08004803
4804 /* if we want to turn off the cursor ignore width and height */
4805 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08004806 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004807 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00004808 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10004809 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004810 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08004811 }
4812
4813 /* Currently we only support 64x64 cursors */
4814 if (width != 64 || height != 64) {
4815 DRM_ERROR("we currently only support 64x64 cursors\n");
4816 return -EINVAL;
4817 }
4818
Chris Wilson05394f32010-11-08 19:18:58 +00004819 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
4820 if (!obj)
Jesse Barnes79e53942008-11-07 14:24:08 -08004821 return -ENOENT;
4822
Chris Wilson05394f32010-11-08 19:18:58 +00004823 if (obj->base.size < width * height * 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004824 DRM_ERROR("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10004825 ret = -ENOMEM;
4826 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08004827 }
4828
Dave Airlie71acb5e2008-12-30 20:31:46 +10004829 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05004830 mutex_lock(&dev->struct_mutex);
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05004831 if (!dev_priv->info->cursor_needs_physical) {
Chris Wilsond9e86c02010-11-10 16:40:20 +00004832 if (obj->tiling_mode) {
4833 DRM_ERROR("cursor cannot be tiled\n");
4834 ret = -EINVAL;
4835 goto fail_locked;
4836 }
4837
Chris Wilson05394f32010-11-08 19:18:58 +00004838 ret = i915_gem_object_pin(obj, PAGE_SIZE, true);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004839 if (ret) {
4840 DRM_ERROR("failed to pin cursor bo\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05004841 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004842 }
Chris Wilsone7b526b2010-06-02 08:30:48 +01004843
Chris Wilson05394f32010-11-08 19:18:58 +00004844 ret = i915_gem_object_set_to_gtt_domain(obj, 0);
Chris Wilsone7b526b2010-06-02 08:30:48 +01004845 if (ret) {
4846 DRM_ERROR("failed to move cursor bo into the GTT\n");
4847 goto fail_unpin;
4848 }
4849
Chris Wilsond9e86c02010-11-10 16:40:20 +00004850 ret = i915_gem_object_put_fence(obj);
4851 if (ret) {
4852 DRM_ERROR("failed to move cursor bo into the GTT\n");
4853 goto fail_unpin;
4854 }
4855
Chris Wilson05394f32010-11-08 19:18:58 +00004856 addr = obj->gtt_offset;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004857 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004858 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson05394f32010-11-08 19:18:58 +00004859 ret = i915_gem_attach_phys_object(dev, obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004860 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
4861 align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004862 if (ret) {
4863 DRM_ERROR("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05004864 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004865 }
Chris Wilson05394f32010-11-08 19:18:58 +00004866 addr = obj->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004867 }
4868
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004869 if (IS_GEN2(dev))
Jesse Barnes14b60392009-05-20 16:47:08 -04004870 I915_WRITE(CURSIZE, (height << 12) | width);
4871
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004872 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004873 if (intel_crtc->cursor_bo) {
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05004874 if (dev_priv->info->cursor_needs_physical) {
Chris Wilson05394f32010-11-08 19:18:58 +00004875 if (intel_crtc->cursor_bo != obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004876 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
4877 } else
4878 i915_gem_object_unpin(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00004879 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004880 }
Jesse Barnes80824002009-09-10 15:28:06 -07004881
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05004882 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004883
4884 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00004885 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004886 intel_crtc->cursor_width = width;
4887 intel_crtc->cursor_height = height;
4888
Chris Wilson6b383a72010-09-13 13:54:26 +01004889 intel_crtc_update_cursor(crtc, true);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004890
Jesse Barnes79e53942008-11-07 14:24:08 -08004891 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01004892fail_unpin:
Chris Wilson05394f32010-11-08 19:18:58 +00004893 i915_gem_object_unpin(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05004894fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10004895 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00004896fail:
Chris Wilson05394f32010-11-08 19:18:58 +00004897 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10004898 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08004899}
4900
4901static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
4902{
Jesse Barnes79e53942008-11-07 14:24:08 -08004903 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08004904
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004905 intel_crtc->cursor_x = x;
4906 intel_crtc->cursor_y = y;
Jesse Barnes652c3932009-08-17 13:31:43 -07004907
Chris Wilson6b383a72010-09-13 13:54:26 +01004908 intel_crtc_update_cursor(crtc, true);
Jesse Barnes79e53942008-11-07 14:24:08 -08004909
4910 return 0;
4911}
4912
4913/** Sets the color ramps on behalf of RandR */
4914void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
4915 u16 blue, int regno)
4916{
4917 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4918
4919 intel_crtc->lut_r[regno] = red >> 8;
4920 intel_crtc->lut_g[regno] = green >> 8;
4921 intel_crtc->lut_b[regno] = blue >> 8;
4922}
4923
Dave Airlieb8c00ac2009-10-06 13:54:01 +10004924void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
4925 u16 *blue, int regno)
4926{
4927 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4928
4929 *red = intel_crtc->lut_r[regno] << 8;
4930 *green = intel_crtc->lut_g[regno] << 8;
4931 *blue = intel_crtc->lut_b[regno] << 8;
4932}
4933
Jesse Barnes79e53942008-11-07 14:24:08 -08004934static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01004935 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08004936{
James Simmons72034252010-08-03 01:33:19 +01004937 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08004938 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08004939
James Simmons72034252010-08-03 01:33:19 +01004940 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004941 intel_crtc->lut_r[i] = red[i] >> 8;
4942 intel_crtc->lut_g[i] = green[i] >> 8;
4943 intel_crtc->lut_b[i] = blue[i] >> 8;
4944 }
4945
4946 intel_crtc_load_lut(crtc);
4947}
4948
4949/**
4950 * Get a pipe with a simple mode set on it for doing load-based monitor
4951 * detection.
4952 *
4953 * It will be up to the load-detect code to adjust the pipe as appropriate for
Eric Anholtc751ce42010-03-25 11:48:48 -07004954 * its requirements. The pipe will be connected to no other encoders.
Jesse Barnes79e53942008-11-07 14:24:08 -08004955 *
Eric Anholtc751ce42010-03-25 11:48:48 -07004956 * Currently this code will only succeed if there is a pipe with no encoders
Jesse Barnes79e53942008-11-07 14:24:08 -08004957 * configured for it. In the future, it could choose to temporarily disable
4958 * some outputs to free up a pipe for its use.
4959 *
4960 * \return crtc, or NULL if no pipes are available.
4961 */
4962
4963/* VESA 640x480x72Hz mode to set on the pipe */
4964static struct drm_display_mode load_detect_mode = {
4965 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
4966 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
4967};
4968
Eric Anholt21d40d32010-03-25 11:11:14 -07004969struct drm_crtc *intel_get_load_detect_pipe(struct intel_encoder *intel_encoder,
Zhenyu Wangc1c43972010-03-30 14:39:30 +08004970 struct drm_connector *connector,
Jesse Barnes79e53942008-11-07 14:24:08 -08004971 struct drm_display_mode *mode,
4972 int *dpms_mode)
4973{
4974 struct intel_crtc *intel_crtc;
4975 struct drm_crtc *possible_crtc;
4976 struct drm_crtc *supported_crtc =NULL;
Chris Wilson4ef69c72010-09-09 15:14:28 +01004977 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08004978 struct drm_crtc *crtc = NULL;
4979 struct drm_device *dev = encoder->dev;
4980 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
4981 struct drm_crtc_helper_funcs *crtc_funcs;
4982 int i = -1;
4983
4984 /*
4985 * Algorithm gets a little messy:
4986 * - if the connector already has an assigned crtc, use it (but make
4987 * sure it's on first)
4988 * - try to find the first unused crtc that can drive this connector,
4989 * and use that if we find one
4990 * - if there are no unused crtcs available, try to use the first
4991 * one we found that supports the connector
4992 */
4993
4994 /* See if we already have a CRTC for this connector */
4995 if (encoder->crtc) {
4996 crtc = encoder->crtc;
4997 /* Make sure the crtc and connector are running */
4998 intel_crtc = to_intel_crtc(crtc);
4999 *dpms_mode = intel_crtc->dpms_mode;
5000 if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
5001 crtc_funcs = crtc->helper_private;
5002 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
5003 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
5004 }
5005 return crtc;
5006 }
5007
5008 /* Find an unused one (if possible) */
5009 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
5010 i++;
5011 if (!(encoder->possible_crtcs & (1 << i)))
5012 continue;
5013 if (!possible_crtc->enabled) {
5014 crtc = possible_crtc;
5015 break;
5016 }
5017 if (!supported_crtc)
5018 supported_crtc = possible_crtc;
5019 }
5020
5021 /*
5022 * If we didn't find an unused CRTC, don't use any.
5023 */
5024 if (!crtc) {
5025 return NULL;
5026 }
5027
5028 encoder->crtc = crtc;
Zhenyu Wangc1c43972010-03-30 14:39:30 +08005029 connector->encoder = encoder;
Eric Anholt21d40d32010-03-25 11:11:14 -07005030 intel_encoder->load_detect_temp = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08005031
5032 intel_crtc = to_intel_crtc(crtc);
5033 *dpms_mode = intel_crtc->dpms_mode;
5034
5035 if (!crtc->enabled) {
5036 if (!mode)
5037 mode = &load_detect_mode;
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05005038 drm_crtc_helper_set_mode(crtc, mode, 0, 0, crtc->fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08005039 } else {
5040 if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
5041 crtc_funcs = crtc->helper_private;
5042 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
5043 }
5044
5045 /* Add this connector to the crtc */
5046 encoder_funcs->mode_set(encoder, &crtc->mode, &crtc->mode);
5047 encoder_funcs->commit(encoder);
5048 }
5049 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07005050 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08005051
5052 return crtc;
5053}
5054
Zhenyu Wangc1c43972010-03-30 14:39:30 +08005055void intel_release_load_detect_pipe(struct intel_encoder *intel_encoder,
5056 struct drm_connector *connector, int dpms_mode)
Jesse Barnes79e53942008-11-07 14:24:08 -08005057{
Chris Wilson4ef69c72010-09-09 15:14:28 +01005058 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08005059 struct drm_device *dev = encoder->dev;
5060 struct drm_crtc *crtc = encoder->crtc;
5061 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
5062 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
5063
Eric Anholt21d40d32010-03-25 11:11:14 -07005064 if (intel_encoder->load_detect_temp) {
Jesse Barnes79e53942008-11-07 14:24:08 -08005065 encoder->crtc = NULL;
Zhenyu Wangc1c43972010-03-30 14:39:30 +08005066 connector->encoder = NULL;
Eric Anholt21d40d32010-03-25 11:11:14 -07005067 intel_encoder->load_detect_temp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08005068 crtc->enabled = drm_helper_crtc_in_use(crtc);
5069 drm_helper_disable_unused_functions(dev);
5070 }
5071
Eric Anholtc751ce42010-03-25 11:48:48 -07005072 /* Switch crtc and encoder back off if necessary */
Jesse Barnes79e53942008-11-07 14:24:08 -08005073 if (crtc->enabled && dpms_mode != DRM_MODE_DPMS_ON) {
5074 if (encoder->crtc == crtc)
5075 encoder_funcs->dpms(encoder, dpms_mode);
5076 crtc_funcs->dpms(crtc, dpms_mode);
5077 }
5078}
5079
5080/* Returns the clock of the currently programmed mode of the given pipe. */
5081static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
5082{
5083 struct drm_i915_private *dev_priv = dev->dev_private;
5084 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5085 int pipe = intel_crtc->pipe;
5086 u32 dpll = I915_READ((pipe == 0) ? DPLL_A : DPLL_B);
5087 u32 fp;
5088 intel_clock_t clock;
5089
5090 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
5091 fp = I915_READ((pipe == 0) ? FPA0 : FPB0);
5092 else
5093 fp = I915_READ((pipe == 0) ? FPA1 : FPB1);
5094
5095 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05005096 if (IS_PINEVIEW(dev)) {
5097 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
5098 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08005099 } else {
5100 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
5101 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
5102 }
5103
Chris Wilsona6c45cf2010-09-17 00:32:17 +01005104 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05005105 if (IS_PINEVIEW(dev))
5106 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
5107 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08005108 else
5109 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08005110 DPLL_FPA01_P1_POST_DIV_SHIFT);
5111
5112 switch (dpll & DPLL_MODE_MASK) {
5113 case DPLLB_MODE_DAC_SERIAL:
5114 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
5115 5 : 10;
5116 break;
5117 case DPLLB_MODE_LVDS:
5118 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
5119 7 : 14;
5120 break;
5121 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08005122 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08005123 "mode\n", (int)(dpll & DPLL_MODE_MASK));
5124 return 0;
5125 }
5126
5127 /* XXX: Handle the 100Mhz refclk */
Shaohua Li21778322009-02-23 15:19:16 +08005128 intel_clock(dev, 96000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08005129 } else {
5130 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
5131
5132 if (is_lvds) {
5133 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
5134 DPLL_FPA01_P1_POST_DIV_SHIFT);
5135 clock.p2 = 14;
5136
5137 if ((dpll & PLL_REF_INPUT_MASK) ==
5138 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
5139 /* XXX: might not be 66MHz */
Shaohua Li21778322009-02-23 15:19:16 +08005140 intel_clock(dev, 66000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08005141 } else
Shaohua Li21778322009-02-23 15:19:16 +08005142 intel_clock(dev, 48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08005143 } else {
5144 if (dpll & PLL_P1_DIVIDE_BY_TWO)
5145 clock.p1 = 2;
5146 else {
5147 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
5148 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
5149 }
5150 if (dpll & PLL_P2_DIVIDE_BY_4)
5151 clock.p2 = 4;
5152 else
5153 clock.p2 = 2;
5154
Shaohua Li21778322009-02-23 15:19:16 +08005155 intel_clock(dev, 48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08005156 }
5157 }
5158
5159 /* XXX: It would be nice to validate the clocks, but we can't reuse
5160 * i830PllIsValid() because it relies on the xf86_config connector
5161 * configuration being accurate, which it isn't necessarily.
5162 */
5163
5164 return clock.dot;
5165}
5166
5167/** Returns the currently programmed mode of the given pipe. */
5168struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
5169 struct drm_crtc *crtc)
5170{
5171 struct drm_i915_private *dev_priv = dev->dev_private;
5172 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5173 int pipe = intel_crtc->pipe;
5174 struct drm_display_mode *mode;
5175 int htot = I915_READ((pipe == 0) ? HTOTAL_A : HTOTAL_B);
5176 int hsync = I915_READ((pipe == 0) ? HSYNC_A : HSYNC_B);
5177 int vtot = I915_READ((pipe == 0) ? VTOTAL_A : VTOTAL_B);
5178 int vsync = I915_READ((pipe == 0) ? VSYNC_A : VSYNC_B);
5179
5180 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
5181 if (!mode)
5182 return NULL;
5183
5184 mode->clock = intel_crtc_clock_get(dev, crtc);
5185 mode->hdisplay = (htot & 0xffff) + 1;
5186 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
5187 mode->hsync_start = (hsync & 0xffff) + 1;
5188 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
5189 mode->vdisplay = (vtot & 0xffff) + 1;
5190 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
5191 mode->vsync_start = (vsync & 0xffff) + 1;
5192 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
5193
5194 drm_mode_set_name(mode);
5195 drm_mode_set_crtcinfo(mode, 0);
5196
5197 return mode;
5198}
5199
Jesse Barnes652c3932009-08-17 13:31:43 -07005200#define GPU_IDLE_TIMEOUT 500 /* ms */
5201
5202/* When this timer fires, we've been idle for awhile */
5203static void intel_gpu_idle_timer(unsigned long arg)
5204{
5205 struct drm_device *dev = (struct drm_device *)arg;
5206 drm_i915_private_t *dev_priv = dev->dev_private;
5207
Chris Wilsonff7ea4c2010-12-08 09:43:41 +00005208 if (!list_empty(&dev_priv->mm.active_list)) {
5209 /* Still processing requests, so just re-arm the timer. */
5210 mod_timer(&dev_priv->idle_timer, jiffies +
5211 msecs_to_jiffies(GPU_IDLE_TIMEOUT));
5212 return;
5213 }
Jesse Barnes652c3932009-08-17 13:31:43 -07005214
Chris Wilsonff7ea4c2010-12-08 09:43:41 +00005215 dev_priv->busy = false;
Eric Anholt01dfba92009-09-06 15:18:53 -07005216 queue_work(dev_priv->wq, &dev_priv->idle_work);
Jesse Barnes652c3932009-08-17 13:31:43 -07005217}
5218
Jesse Barnes652c3932009-08-17 13:31:43 -07005219#define CRTC_IDLE_TIMEOUT 1000 /* ms */
5220
5221static void intel_crtc_idle_timer(unsigned long arg)
5222{
5223 struct intel_crtc *intel_crtc = (struct intel_crtc *)arg;
5224 struct drm_crtc *crtc = &intel_crtc->base;
5225 drm_i915_private_t *dev_priv = crtc->dev->dev_private;
Chris Wilsonff7ea4c2010-12-08 09:43:41 +00005226 struct intel_framebuffer *intel_fb;
5227
5228 intel_fb = to_intel_framebuffer(crtc->fb);
5229 if (intel_fb && intel_fb->obj->active) {
5230 /* The framebuffer is still being accessed by the GPU. */
5231 mod_timer(&intel_crtc->idle_timer, jiffies +
5232 msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
5233 return;
5234 }
Jesse Barnes652c3932009-08-17 13:31:43 -07005235
Jesse Barnes652c3932009-08-17 13:31:43 -07005236 intel_crtc->busy = false;
Eric Anholt01dfba92009-09-06 15:18:53 -07005237 queue_work(dev_priv->wq, &dev_priv->idle_work);
Jesse Barnes652c3932009-08-17 13:31:43 -07005238}
5239
Daniel Vetter3dec0092010-08-20 21:40:52 +02005240static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07005241{
5242 struct drm_device *dev = crtc->dev;
5243 drm_i915_private_t *dev_priv = dev->dev_private;
5244 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5245 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08005246 int dpll_reg = DPLL(pipe);
5247 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07005248
Eric Anholtbad720f2009-10-22 16:11:14 -07005249 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07005250 return;
5251
5252 if (!dev_priv->lvds_downclock_avail)
5253 return;
5254
Jesse Barnesdbdc6472010-12-30 09:36:39 -08005255 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07005256 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08005257 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07005258
5259 /* Unlock panel regs */
Jesse Barnesdbdc6472010-12-30 09:36:39 -08005260 I915_WRITE(PP_CONTROL,
5261 I915_READ(PP_CONTROL) | PANEL_UNLOCK_REGS);
Jesse Barnes652c3932009-08-17 13:31:43 -07005262
5263 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
5264 I915_WRITE(dpll_reg, dpll);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08005265 POSTING_READ(dpll_reg);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07005266 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08005267
Jesse Barnes652c3932009-08-17 13:31:43 -07005268 dpll = I915_READ(dpll_reg);
5269 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08005270 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07005271
5272 /* ...and lock them again */
5273 I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
5274 }
5275
5276 /* Schedule downclock */
Daniel Vetter3dec0092010-08-20 21:40:52 +02005277 mod_timer(&intel_crtc->idle_timer, jiffies +
5278 msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
Jesse Barnes652c3932009-08-17 13:31:43 -07005279}
5280
5281static void intel_decrease_pllclock(struct drm_crtc *crtc)
5282{
5283 struct drm_device *dev = crtc->dev;
5284 drm_i915_private_t *dev_priv = dev->dev_private;
5285 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5286 int pipe = intel_crtc->pipe;
5287 int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
5288 int dpll = I915_READ(dpll_reg);
5289
Eric Anholtbad720f2009-10-22 16:11:14 -07005290 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07005291 return;
5292
5293 if (!dev_priv->lvds_downclock_avail)
5294 return;
5295
5296 /*
5297 * Since this is called by a timer, we should never get here in
5298 * the manual case.
5299 */
5300 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08005301 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07005302
5303 /* Unlock panel regs */
Jesse Barnes4a655f02010-07-22 13:18:18 -07005304 I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) |
5305 PANEL_UNLOCK_REGS);
Jesse Barnes652c3932009-08-17 13:31:43 -07005306
5307 dpll |= DISPLAY_RATE_SELECT_FPA1;
5308 I915_WRITE(dpll_reg, dpll);
5309 dpll = I915_READ(dpll_reg);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07005310 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07005311 dpll = I915_READ(dpll_reg);
5312 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08005313 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07005314
5315 /* ...and lock them again */
5316 I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
5317 }
5318
5319}
5320
5321/**
5322 * intel_idle_update - adjust clocks for idleness
5323 * @work: work struct
5324 *
5325 * Either the GPU or display (or both) went idle. Check the busy status
5326 * here and adjust the CRTC and GPU clocks as necessary.
5327 */
5328static void intel_idle_update(struct work_struct *work)
5329{
5330 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
5331 idle_work);
5332 struct drm_device *dev = dev_priv->dev;
5333 struct drm_crtc *crtc;
5334 struct intel_crtc *intel_crtc;
Li Peng45ac22c2010-06-12 23:38:35 +08005335 int enabled = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07005336
5337 if (!i915_powersave)
5338 return;
5339
5340 mutex_lock(&dev->struct_mutex);
5341
Jesse Barnes7648fa92010-05-20 14:28:11 -07005342 i915_update_gfx_val(dev_priv);
5343
Jesse Barnes652c3932009-08-17 13:31:43 -07005344 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
5345 /* Skip inactive CRTCs */
5346 if (!crtc->fb)
5347 continue;
5348
Li Peng45ac22c2010-06-12 23:38:35 +08005349 enabled++;
Jesse Barnes652c3932009-08-17 13:31:43 -07005350 intel_crtc = to_intel_crtc(crtc);
5351 if (!intel_crtc->busy)
5352 intel_decrease_pllclock(crtc);
5353 }
5354
Li Peng45ac22c2010-06-12 23:38:35 +08005355 if ((enabled == 1) && (IS_I945G(dev) || IS_I945GM(dev))) {
5356 DRM_DEBUG_DRIVER("enable memory self refresh on 945\n");
5357 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
5358 }
5359
Jesse Barnes652c3932009-08-17 13:31:43 -07005360 mutex_unlock(&dev->struct_mutex);
5361}
5362
5363/**
5364 * intel_mark_busy - mark the GPU and possibly the display busy
5365 * @dev: drm device
5366 * @obj: object we're operating on
5367 *
5368 * Callers can use this function to indicate that the GPU is busy processing
5369 * commands. If @obj matches one of the CRTC objects (i.e. it's a scanout
5370 * buffer), we'll also mark the display as busy, so we know to increase its
5371 * clock frequency.
5372 */
Chris Wilson05394f32010-11-08 19:18:58 +00005373void intel_mark_busy(struct drm_device *dev, struct drm_i915_gem_object *obj)
Jesse Barnes652c3932009-08-17 13:31:43 -07005374{
5375 drm_i915_private_t *dev_priv = dev->dev_private;
5376 struct drm_crtc *crtc = NULL;
5377 struct intel_framebuffer *intel_fb;
5378 struct intel_crtc *intel_crtc;
5379
Zhenyu Wang5e17ee72009-09-03 09:30:06 +08005380 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5381 return;
5382
Li Peng060e6452010-02-10 01:54:24 +08005383 if (!dev_priv->busy) {
5384 if (IS_I945G(dev) || IS_I945GM(dev)) {
5385 u32 fw_blc_self;
Li Pengee980b82010-01-27 19:01:11 +08005386
Li Peng060e6452010-02-10 01:54:24 +08005387 DRM_DEBUG_DRIVER("disable memory self refresh on 945\n");
5388 fw_blc_self = I915_READ(FW_BLC_SELF);
5389 fw_blc_self &= ~FW_BLC_SELF_EN;
5390 I915_WRITE(FW_BLC_SELF, fw_blc_self | FW_BLC_SELF_EN_MASK);
5391 }
Chris Wilson28cf7982009-11-30 01:08:56 +00005392 dev_priv->busy = true;
Li Peng060e6452010-02-10 01:54:24 +08005393 } else
Chris Wilson28cf7982009-11-30 01:08:56 +00005394 mod_timer(&dev_priv->idle_timer, jiffies +
5395 msecs_to_jiffies(GPU_IDLE_TIMEOUT));
Jesse Barnes652c3932009-08-17 13:31:43 -07005396
5397 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
5398 if (!crtc->fb)
5399 continue;
5400
5401 intel_crtc = to_intel_crtc(crtc);
5402 intel_fb = to_intel_framebuffer(crtc->fb);
5403 if (intel_fb->obj == obj) {
5404 if (!intel_crtc->busy) {
Li Peng060e6452010-02-10 01:54:24 +08005405 if (IS_I945G(dev) || IS_I945GM(dev)) {
5406 u32 fw_blc_self;
5407
5408 DRM_DEBUG_DRIVER("disable memory self refresh on 945\n");
5409 fw_blc_self = I915_READ(FW_BLC_SELF);
5410 fw_blc_self &= ~FW_BLC_SELF_EN;
5411 I915_WRITE(FW_BLC_SELF, fw_blc_self | FW_BLC_SELF_EN_MASK);
5412 }
Jesse Barnes652c3932009-08-17 13:31:43 -07005413 /* Non-busy -> busy, upclock */
Daniel Vetter3dec0092010-08-20 21:40:52 +02005414 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07005415 intel_crtc->busy = true;
5416 } else {
5417 /* Busy -> busy, put off timer */
5418 mod_timer(&intel_crtc->idle_timer, jiffies +
5419 msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
5420 }
5421 }
5422 }
5423}
5424
Jesse Barnes79e53942008-11-07 14:24:08 -08005425static void intel_crtc_destroy(struct drm_crtc *crtc)
5426{
5427 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02005428 struct drm_device *dev = crtc->dev;
5429 struct intel_unpin_work *work;
5430 unsigned long flags;
5431
5432 spin_lock_irqsave(&dev->event_lock, flags);
5433 work = intel_crtc->unpin_work;
5434 intel_crtc->unpin_work = NULL;
5435 spin_unlock_irqrestore(&dev->event_lock, flags);
5436
5437 if (work) {
5438 cancel_work_sync(&work->work);
5439 kfree(work);
5440 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005441
5442 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02005443
Jesse Barnes79e53942008-11-07 14:24:08 -08005444 kfree(intel_crtc);
5445}
5446
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005447static void intel_unpin_work_fn(struct work_struct *__work)
5448{
5449 struct intel_unpin_work *work =
5450 container_of(__work, struct intel_unpin_work, work);
5451
5452 mutex_lock(&work->dev->struct_mutex);
Jesse Barnesb1b87f62010-01-26 14:40:05 -08005453 i915_gem_object_unpin(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00005454 drm_gem_object_unreference(&work->pending_flip_obj->base);
5455 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00005456
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005457 mutex_unlock(&work->dev->struct_mutex);
5458 kfree(work);
5459}
5460
Jesse Barnes1afe3e92010-03-26 10:35:20 -07005461static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01005462 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005463{
5464 drm_i915_private_t *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005465 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5466 struct intel_unpin_work *work;
Chris Wilson05394f32010-11-08 19:18:58 +00005467 struct drm_i915_gem_object *obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005468 struct drm_pending_vblank_event *e;
Mario Kleiner49b14a52010-12-09 07:00:07 +01005469 struct timeval tnow, tvbl;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005470 unsigned long flags;
5471
5472 /* Ignore early vblank irqs */
5473 if (intel_crtc == NULL)
5474 return;
5475
Mario Kleiner49b14a52010-12-09 07:00:07 +01005476 do_gettimeofday(&tnow);
5477
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005478 spin_lock_irqsave(&dev->event_lock, flags);
5479 work = intel_crtc->unpin_work;
5480 if (work == NULL || !work->pending) {
5481 spin_unlock_irqrestore(&dev->event_lock, flags);
5482 return;
5483 }
5484
5485 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005486
5487 if (work->event) {
5488 e = work->event;
Mario Kleiner49b14a52010-12-09 07:00:07 +01005489 e->event.sequence = drm_vblank_count_and_time(dev, intel_crtc->pipe, &tvbl);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01005490
5491 /* Called before vblank count and timestamps have
5492 * been updated for the vblank interval of flip
5493 * completion? Need to increment vblank count and
5494 * add one videorefresh duration to returned timestamp
Mario Kleiner49b14a52010-12-09 07:00:07 +01005495 * to account for this. We assume this happened if we
5496 * get called over 0.9 frame durations after the last
5497 * timestamped vblank.
5498 *
5499 * This calculation can not be used with vrefresh rates
5500 * below 5Hz (10Hz to be on the safe side) without
5501 * promoting to 64 integers.
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01005502 */
Mario Kleiner49b14a52010-12-09 07:00:07 +01005503 if (10 * (timeval_to_ns(&tnow) - timeval_to_ns(&tvbl)) >
5504 9 * crtc->framedur_ns) {
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01005505 e->event.sequence++;
Mario Kleiner49b14a52010-12-09 07:00:07 +01005506 tvbl = ns_to_timeval(timeval_to_ns(&tvbl) +
5507 crtc->framedur_ns);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01005508 }
5509
Mario Kleiner49b14a52010-12-09 07:00:07 +01005510 e->event.tv_sec = tvbl.tv_sec;
5511 e->event.tv_usec = tvbl.tv_usec;
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01005512
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005513 list_add_tail(&e->base.link,
5514 &e->base.file_priv->event_list);
5515 wake_up_interruptible(&e->base.file_priv->event_wait);
5516 }
5517
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01005518 drm_vblank_put(dev, intel_crtc->pipe);
5519
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005520 spin_unlock_irqrestore(&dev->event_lock, flags);
5521
Chris Wilson05394f32010-11-08 19:18:58 +00005522 obj = work->old_fb_obj;
Chris Wilsond9e86c02010-11-10 16:40:20 +00005523
Chris Wilsone59f2ba2010-10-07 17:28:15 +01005524 atomic_clear_mask(1 << intel_crtc->plane,
Chris Wilson05394f32010-11-08 19:18:58 +00005525 &obj->pending_flip.counter);
5526 if (atomic_read(&obj->pending_flip) == 0)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005527 wake_up(&dev_priv->pending_flip_queue);
Chris Wilsond9e86c02010-11-10 16:40:20 +00005528
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005529 schedule_work(&work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07005530
5531 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005532}
5533
Jesse Barnes1afe3e92010-03-26 10:35:20 -07005534void intel_finish_page_flip(struct drm_device *dev, int pipe)
5535{
5536 drm_i915_private_t *dev_priv = dev->dev_private;
5537 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
5538
Mario Kleiner49b14a52010-12-09 07:00:07 +01005539 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07005540}
5541
5542void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
5543{
5544 drm_i915_private_t *dev_priv = dev->dev_private;
5545 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
5546
Mario Kleiner49b14a52010-12-09 07:00:07 +01005547 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07005548}
5549
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005550void intel_prepare_page_flip(struct drm_device *dev, int plane)
5551{
5552 drm_i915_private_t *dev_priv = dev->dev_private;
5553 struct intel_crtc *intel_crtc =
5554 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
5555 unsigned long flags;
5556
5557 spin_lock_irqsave(&dev->event_lock, flags);
Jesse Barnesde3f4402010-01-14 13:18:02 -08005558 if (intel_crtc->unpin_work) {
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01005559 if ((++intel_crtc->unpin_work->pending) > 1)
5560 DRM_ERROR("Prepared flip multiple times\n");
Jesse Barnesde3f4402010-01-14 13:18:02 -08005561 } else {
5562 DRM_DEBUG_DRIVER("preparing flip with no unpin work?\n");
5563 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005564 spin_unlock_irqrestore(&dev->event_lock, flags);
5565}
5566
5567static int intel_crtc_page_flip(struct drm_crtc *crtc,
5568 struct drm_framebuffer *fb,
5569 struct drm_pending_vblank_event *event)
5570{
5571 struct drm_device *dev = crtc->dev;
5572 struct drm_i915_private *dev_priv = dev->dev_private;
5573 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00005574 struct drm_i915_gem_object *obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005575 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5576 struct intel_unpin_work *work;
Jesse Barnesbe9a3db2010-07-23 12:03:37 -07005577 unsigned long flags, offset;
Chris Wilson52e68632010-08-08 10:15:59 +01005578 int pipe = intel_crtc->pipe;
Chris Wilson20f0cd52010-09-23 11:00:38 +01005579 u32 pf, pipesrc;
Chris Wilson52e68632010-08-08 10:15:59 +01005580 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005581
5582 work = kzalloc(sizeof *work, GFP_KERNEL);
5583 if (work == NULL)
5584 return -ENOMEM;
5585
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005586 work->event = event;
5587 work->dev = crtc->dev;
5588 intel_fb = to_intel_framebuffer(crtc->fb);
Jesse Barnesb1b87f62010-01-26 14:40:05 -08005589 work->old_fb_obj = intel_fb->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005590 INIT_WORK(&work->work, intel_unpin_work_fn);
5591
5592 /* We borrow the event spin lock for protecting unpin_work */
5593 spin_lock_irqsave(&dev->event_lock, flags);
5594 if (intel_crtc->unpin_work) {
5595 spin_unlock_irqrestore(&dev->event_lock, flags);
5596 kfree(work);
Chris Wilson468f0b42010-05-27 13:18:13 +01005597
5598 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005599 return -EBUSY;
5600 }
5601 intel_crtc->unpin_work = work;
5602 spin_unlock_irqrestore(&dev->event_lock, flags);
5603
5604 intel_fb = to_intel_framebuffer(fb);
5605 obj = intel_fb->obj;
5606
Chris Wilson468f0b42010-05-27 13:18:13 +01005607 mutex_lock(&dev->struct_mutex);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00005608 ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
Chris Wilson96b099f2010-06-07 14:03:04 +01005609 if (ret)
5610 goto cleanup_work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005611
Jesse Barnes75dfca82010-02-10 15:09:44 -08005612 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00005613 drm_gem_object_reference(&work->old_fb_obj->base);
5614 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005615
5616 crtc->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01005617
5618 ret = drm_vblank_get(dev, intel_crtc->pipe);
5619 if (ret)
5620 goto cleanup_objs;
5621
Chris Wilsonc7f9f9a2010-09-19 15:05:13 +01005622 if (IS_GEN3(dev) || IS_GEN2(dev)) {
5623 u32 flip_mask;
5624
5625 /* Can't queue multiple flips, so wait for the previous
5626 * one to finish before executing the next.
5627 */
Chris Wilsone1f99ce2010-10-27 12:45:26 +01005628 ret = BEGIN_LP_RING(2);
5629 if (ret)
5630 goto cleanup_objs;
5631
Chris Wilsonc7f9f9a2010-09-19 15:05:13 +01005632 if (intel_crtc->plane)
5633 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
5634 else
5635 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
5636 OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);
5637 OUT_RING(MI_NOOP);
Daniel Vetter6146b3d2010-08-04 21:22:10 +02005638 ADVANCE_LP_RING();
5639 }
Jesse Barnes83f7fd02010-04-05 14:03:51 -07005640
Chris Wilsone1f99ce2010-10-27 12:45:26 +01005641 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01005642
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01005643 work->enable_stall_check = true;
5644
Jesse Barnesbe9a3db2010-07-23 12:03:37 -07005645 /* Offset into the new buffer for cases of shared fbs between CRTCs */
Chris Wilson52e68632010-08-08 10:15:59 +01005646 offset = crtc->y * fb->pitch + crtc->x * fb->bits_per_pixel/8;
Jesse Barnesbe9a3db2010-07-23 12:03:37 -07005647
Chris Wilsone1f99ce2010-10-27 12:45:26 +01005648 ret = BEGIN_LP_RING(4);
5649 if (ret)
5650 goto cleanup_objs;
5651
5652 /* Block clients from rendering to the new back buffer until
5653 * the flip occurs and the object is no longer visible.
5654 */
Chris Wilson05394f32010-11-08 19:18:58 +00005655 atomic_add(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01005656
5657 switch (INTEL_INFO(dev)->gen) {
Chris Wilson52e68632010-08-08 10:15:59 +01005658 case 2:
Jesse Barnes1afe3e92010-03-26 10:35:20 -07005659 OUT_RING(MI_DISPLAY_FLIP |
5660 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
5661 OUT_RING(fb->pitch);
Chris Wilson05394f32010-11-08 19:18:58 +00005662 OUT_RING(obj->gtt_offset + offset);
Chris Wilson52e68632010-08-08 10:15:59 +01005663 OUT_RING(MI_NOOP);
5664 break;
5665
5666 case 3:
Jesse Barnes1afe3e92010-03-26 10:35:20 -07005667 OUT_RING(MI_DISPLAY_FLIP_I915 |
5668 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
5669 OUT_RING(fb->pitch);
Chris Wilson05394f32010-11-08 19:18:58 +00005670 OUT_RING(obj->gtt_offset + offset);
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08005671 OUT_RING(MI_NOOP);
Chris Wilson52e68632010-08-08 10:15:59 +01005672 break;
5673
5674 case 4:
5675 case 5:
5676 /* i965+ uses the linear or tiled offsets from the
5677 * Display Registers (which do not change across a page-flip)
5678 * so we need only reprogram the base address.
5679 */
Daniel Vetter69d0b962010-08-04 21:22:09 +02005680 OUT_RING(MI_DISPLAY_FLIP |
5681 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
5682 OUT_RING(fb->pitch);
Chris Wilson05394f32010-11-08 19:18:58 +00005683 OUT_RING(obj->gtt_offset | obj->tiling_mode);
Chris Wilson52e68632010-08-08 10:15:59 +01005684
5685 /* XXX Enabling the panel-fitter across page-flip is so far
5686 * untested on non-native modes, so ignore it for now.
5687 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
5688 */
5689 pf = 0;
5690 pipesrc = I915_READ(pipe == 0 ? PIPEASRC : PIPEBSRC) & 0x0fff0fff;
5691 OUT_RING(pf | pipesrc);
5692 break;
5693
5694 case 6:
5695 OUT_RING(MI_DISPLAY_FLIP |
5696 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Chris Wilson05394f32010-11-08 19:18:58 +00005697 OUT_RING(fb->pitch | obj->tiling_mode);
5698 OUT_RING(obj->gtt_offset);
Chris Wilson52e68632010-08-08 10:15:59 +01005699
5700 pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
5701 pipesrc = I915_READ(pipe == 0 ? PIPEASRC : PIPEBSRC) & 0x0fff0fff;
5702 OUT_RING(pf | pipesrc);
5703 break;
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08005704 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005705 ADVANCE_LP_RING();
5706
5707 mutex_unlock(&dev->struct_mutex);
5708
Jesse Barnese5510fa2010-07-01 16:48:37 -07005709 trace_i915_flip_request(intel_crtc->plane, obj);
5710
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005711 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01005712
5713cleanup_objs:
Chris Wilson05394f32010-11-08 19:18:58 +00005714 drm_gem_object_unreference(&work->old_fb_obj->base);
5715 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01005716cleanup_work:
5717 mutex_unlock(&dev->struct_mutex);
5718
5719 spin_lock_irqsave(&dev->event_lock, flags);
5720 intel_crtc->unpin_work = NULL;
5721 spin_unlock_irqrestore(&dev->event_lock, flags);
5722
5723 kfree(work);
5724
5725 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005726}
5727
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07005728static struct drm_crtc_helper_funcs intel_helper_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -08005729 .dpms = intel_crtc_dpms,
5730 .mode_fixup = intel_crtc_mode_fixup,
5731 .mode_set = intel_crtc_mode_set,
5732 .mode_set_base = intel_pipe_set_base,
Jesse Barnes81255562010-08-02 12:07:50 -07005733 .mode_set_base_atomic = intel_pipe_set_base_atomic,
Dave Airlie068143d2009-10-05 09:58:02 +10005734 .load_lut = intel_crtc_load_lut,
Chris Wilsoncdd59982010-09-08 16:30:16 +01005735 .disable = intel_crtc_disable,
Jesse Barnes79e53942008-11-07 14:24:08 -08005736};
5737
5738static const struct drm_crtc_funcs intel_crtc_funcs = {
5739 .cursor_set = intel_crtc_cursor_set,
5740 .cursor_move = intel_crtc_cursor_move,
5741 .gamma_set = intel_crtc_gamma_set,
5742 .set_config = drm_crtc_helper_set_config,
5743 .destroy = intel_crtc_destroy,
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005744 .page_flip = intel_crtc_page_flip,
Jesse Barnes79e53942008-11-07 14:24:08 -08005745};
5746
Chris Wilson47f1c6c2010-12-03 15:37:31 +00005747static void intel_sanitize_modesetting(struct drm_device *dev,
5748 int pipe, int plane)
5749{
5750 struct drm_i915_private *dev_priv = dev->dev_private;
5751 u32 reg, val;
5752
5753 if (HAS_PCH_SPLIT(dev))
5754 return;
5755
5756 /* Who knows what state these registers were left in by the BIOS or
5757 * grub?
5758 *
5759 * If we leave the registers in a conflicting state (e.g. with the
5760 * display plane reading from the other pipe than the one we intend
5761 * to use) then when we attempt to teardown the active mode, we will
5762 * not disable the pipes and planes in the correct order -- leaving
5763 * a plane reading from a disabled pipe and possibly leading to
5764 * undefined behaviour.
5765 */
5766
5767 reg = DSPCNTR(plane);
5768 val = I915_READ(reg);
5769
5770 if ((val & DISPLAY_PLANE_ENABLE) == 0)
5771 return;
5772 if (!!(val & DISPPLANE_SEL_PIPE_MASK) == pipe)
5773 return;
5774
5775 /* This display plane is active and attached to the other CPU pipe. */
5776 pipe = !pipe;
5777
5778 /* Disable the plane and wait for it to stop reading from the pipe. */
Jesse Barnesb24e7172011-01-04 15:09:30 -08005779 intel_disable_plane(dev_priv, plane, pipe);
5780 intel_disable_pipe(dev_priv, pipe);
Chris Wilson47f1c6c2010-12-03 15:37:31 +00005781}
Jesse Barnes79e53942008-11-07 14:24:08 -08005782
Hannes Ederb358d0a2008-12-18 21:18:47 +01005783static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -08005784{
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08005785 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08005786 struct intel_crtc *intel_crtc;
5787 int i;
5788
5789 intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
5790 if (intel_crtc == NULL)
5791 return;
5792
5793 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
5794
5795 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -08005796 for (i = 0; i < 256; i++) {
5797 intel_crtc->lut_r[i] = i;
5798 intel_crtc->lut_g[i] = i;
5799 intel_crtc->lut_b[i] = i;
5800 }
5801
Jesse Barnes80824002009-09-10 15:28:06 -07005802 /* Swap pipes & planes for FBC on pre-965 */
5803 intel_crtc->pipe = pipe;
5804 intel_crtc->plane = pipe;
Chris Wilsone2e767a2010-09-13 16:53:12 +01005805 if (IS_MOBILE(dev) && IS_GEN3(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08005806 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +01005807 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07005808 }
5809
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08005810 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
5811 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
5812 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
5813 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
5814
Jesse Barnes79e53942008-11-07 14:24:08 -08005815 intel_crtc->cursor_addr = 0;
Chris Wilson032d2a02010-09-06 16:17:22 +01005816 intel_crtc->dpms_mode = -1;
Chris Wilsone65d9302010-09-13 16:58:39 +01005817 intel_crtc->active = true; /* force the pipe off on setup_init_config */
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07005818
5819 if (HAS_PCH_SPLIT(dev)) {
5820 intel_helper_funcs.prepare = ironlake_crtc_prepare;
5821 intel_helper_funcs.commit = ironlake_crtc_commit;
5822 } else {
5823 intel_helper_funcs.prepare = i9xx_crtc_prepare;
5824 intel_helper_funcs.commit = i9xx_crtc_commit;
5825 }
5826
Jesse Barnes79e53942008-11-07 14:24:08 -08005827 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
5828
Jesse Barnes652c3932009-08-17 13:31:43 -07005829 intel_crtc->busy = false;
5830
5831 setup_timer(&intel_crtc->idle_timer, intel_crtc_idle_timer,
5832 (unsigned long)intel_crtc);
Chris Wilson47f1c6c2010-12-03 15:37:31 +00005833
5834 intel_sanitize_modesetting(dev, intel_crtc->pipe, intel_crtc->plane);
Jesse Barnes79e53942008-11-07 14:24:08 -08005835}
5836
Carl Worth08d7b3d2009-04-29 14:43:54 -07005837int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00005838 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -07005839{
5840 drm_i915_private_t *dev_priv = dev->dev_private;
5841 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +02005842 struct drm_mode_object *drmmode_obj;
5843 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -07005844
5845 if (!dev_priv) {
5846 DRM_ERROR("called with no initialization\n");
5847 return -EINVAL;
5848 }
5849
Daniel Vetterc05422d2009-08-11 16:05:30 +02005850 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
5851 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -07005852
Daniel Vetterc05422d2009-08-11 16:05:30 +02005853 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -07005854 DRM_ERROR("no such CRTC id\n");
5855 return -EINVAL;
5856 }
5857
Daniel Vetterc05422d2009-08-11 16:05:30 +02005858 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
5859 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -07005860
Daniel Vetterc05422d2009-08-11 16:05:30 +02005861 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -07005862}
5863
Zhenyu Wangc5e4df32010-03-30 14:39:27 +08005864static int intel_encoder_clones(struct drm_device *dev, int type_mask)
Jesse Barnes79e53942008-11-07 14:24:08 -08005865{
Chris Wilson4ef69c72010-09-09 15:14:28 +01005866 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08005867 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005868 int entry = 0;
5869
Chris Wilson4ef69c72010-09-09 15:14:28 +01005870 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
5871 if (type_mask & encoder->clone_mask)
Jesse Barnes79e53942008-11-07 14:24:08 -08005872 index_mask |= (1 << entry);
5873 entry++;
5874 }
Chris Wilson4ef69c72010-09-09 15:14:28 +01005875
Jesse Barnes79e53942008-11-07 14:24:08 -08005876 return index_mask;
5877}
5878
Chris Wilson4d302442010-12-14 19:21:29 +00005879static bool has_edp_a(struct drm_device *dev)
5880{
5881 struct drm_i915_private *dev_priv = dev->dev_private;
5882
5883 if (!IS_MOBILE(dev))
5884 return false;
5885
5886 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
5887 return false;
5888
5889 if (IS_GEN5(dev) &&
5890 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
5891 return false;
5892
5893 return true;
5894}
5895
Jesse Barnes79e53942008-11-07 14:24:08 -08005896static void intel_setup_outputs(struct drm_device *dev)
5897{
Eric Anholt725e30a2009-01-22 13:01:02 -08005898 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +01005899 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -04005900 bool dpd_is_edp = false;
Chris Wilsonc5d1b512010-11-29 18:00:23 +00005901 bool has_lvds = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08005902
Zhenyu Wang541998a2009-06-05 15:38:44 +08005903 if (IS_MOBILE(dev) && !IS_I830(dev))
Chris Wilsonc5d1b512010-11-29 18:00:23 +00005904 has_lvds = intel_lvds_init(dev);
5905 if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
5906 /* disable the panel fitter on everything but LVDS */
5907 I915_WRITE(PFIT_CONTROL, 0);
5908 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005909
Eric Anholtbad720f2009-10-22 16:11:14 -07005910 if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -04005911 dpd_is_edp = intel_dpd_is_edp(dev);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08005912
Chris Wilson4d302442010-12-14 19:21:29 +00005913 if (has_edp_a(dev))
Zhenyu Wang32f9d652009-07-24 01:00:32 +08005914 intel_dp_init(dev, DP_A);
5915
Adam Jacksoncb0953d2010-07-16 14:46:29 -04005916 if (dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
5917 intel_dp_init(dev, PCH_DP_D);
5918 }
5919
5920 intel_crt_init(dev);
5921
5922 if (HAS_PCH_SPLIT(dev)) {
5923 int found;
5924
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08005925 if (I915_READ(HDMIB) & PORT_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +08005926 /* PCH SDVOB multiplex with HDMIB */
5927 found = intel_sdvo_init(dev, PCH_SDVOB);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08005928 if (!found)
5929 intel_hdmi_init(dev, HDMIB);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08005930 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
5931 intel_dp_init(dev, PCH_DP_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08005932 }
5933
5934 if (I915_READ(HDMIC) & PORT_DETECTED)
5935 intel_hdmi_init(dev, HDMIC);
5936
5937 if (I915_READ(HDMID) & PORT_DETECTED)
5938 intel_hdmi_init(dev, HDMID);
5939
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08005940 if (I915_READ(PCH_DP_C) & DP_DETECTED)
5941 intel_dp_init(dev, PCH_DP_C);
5942
Adam Jacksoncb0953d2010-07-16 14:46:29 -04005943 if (!dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08005944 intel_dp_init(dev, PCH_DP_D);
5945
Zhenyu Wang103a1962009-11-27 11:44:36 +08005946 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +08005947 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -08005948
Eric Anholt725e30a2009-01-22 13:01:02 -08005949 if (I915_READ(SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005950 DRM_DEBUG_KMS("probing SDVOB\n");
Eric Anholt725e30a2009-01-22 13:01:02 -08005951 found = intel_sdvo_init(dev, SDVOB);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005952 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
5953 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Eric Anholt725e30a2009-01-22 13:01:02 -08005954 intel_hdmi_init(dev, SDVOB);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005955 }
Ma Ling27185ae2009-08-24 13:50:23 +08005956
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005957 if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
5958 DRM_DEBUG_KMS("probing DP_B\n");
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005959 intel_dp_init(dev, DP_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005960 }
Eric Anholt725e30a2009-01-22 13:01:02 -08005961 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -04005962
5963 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -04005964
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005965 if (I915_READ(SDVOB) & SDVO_DETECTED) {
5966 DRM_DEBUG_KMS("probing SDVOC\n");
Eric Anholt725e30a2009-01-22 13:01:02 -08005967 found = intel_sdvo_init(dev, SDVOC);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005968 }
Ma Ling27185ae2009-08-24 13:50:23 +08005969
5970 if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
5971
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005972 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
5973 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Eric Anholt725e30a2009-01-22 13:01:02 -08005974 intel_hdmi_init(dev, SDVOC);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005975 }
5976 if (SUPPORTS_INTEGRATED_DP(dev)) {
5977 DRM_DEBUG_KMS("probing DP_C\n");
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005978 intel_dp_init(dev, DP_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005979 }
Eric Anholt725e30a2009-01-22 13:01:02 -08005980 }
Ma Ling27185ae2009-08-24 13:50:23 +08005981
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005982 if (SUPPORTS_INTEGRATED_DP(dev) &&
5983 (I915_READ(DP_D) & DP_DETECTED)) {
5984 DRM_DEBUG_KMS("probing DP_D\n");
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005985 intel_dp_init(dev, DP_D);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08005986 }
Eric Anholtbad720f2009-10-22 16:11:14 -07005987 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08005988 intel_dvo_init(dev);
5989
Zhenyu Wang103a1962009-11-27 11:44:36 +08005990 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08005991 intel_tv_init(dev);
5992
Chris Wilson4ef69c72010-09-09 15:14:28 +01005993 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
5994 encoder->base.possible_crtcs = encoder->crtc_mask;
5995 encoder->base.possible_clones =
5996 intel_encoder_clones(dev, encoder->clone_mask);
Jesse Barnes79e53942008-11-07 14:24:08 -08005997 }
Chris Wilson47356eb2011-01-11 17:06:04 +00005998
5999 intel_panel_setup_backlight(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006000}
6001
6002static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
6003{
6004 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08006005
6006 drm_framebuffer_cleanup(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00006007 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08006008
6009 kfree(intel_fb);
6010}
6011
6012static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +00006013 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08006014 unsigned int *handle)
6015{
6016 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00006017 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08006018
Chris Wilson05394f32010-11-08 19:18:58 +00006019 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -08006020}
6021
6022static const struct drm_framebuffer_funcs intel_fb_funcs = {
6023 .destroy = intel_user_framebuffer_destroy,
6024 .create_handle = intel_user_framebuffer_create_handle,
6025};
6026
Dave Airlie38651672010-03-30 05:34:13 +00006027int intel_framebuffer_init(struct drm_device *dev,
6028 struct intel_framebuffer *intel_fb,
6029 struct drm_mode_fb_cmd *mode_cmd,
Chris Wilson05394f32010-11-08 19:18:58 +00006030 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -08006031{
Jesse Barnes79e53942008-11-07 14:24:08 -08006032 int ret;
6033
Chris Wilson05394f32010-11-08 19:18:58 +00006034 if (obj->tiling_mode == I915_TILING_Y)
Chris Wilson57cd6502010-08-08 12:34:44 +01006035 return -EINVAL;
6036
6037 if (mode_cmd->pitch & 63)
6038 return -EINVAL;
6039
6040 switch (mode_cmd->bpp) {
6041 case 8:
6042 case 16:
6043 case 24:
6044 case 32:
6045 break;
6046 default:
6047 return -EINVAL;
6048 }
6049
Jesse Barnes79e53942008-11-07 14:24:08 -08006050 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
6051 if (ret) {
6052 DRM_ERROR("framebuffer init failed %d\n", ret);
6053 return ret;
6054 }
6055
6056 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
Jesse Barnes79e53942008-11-07 14:24:08 -08006057 intel_fb->obj = obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08006058 return 0;
6059}
6060
Jesse Barnes79e53942008-11-07 14:24:08 -08006061static struct drm_framebuffer *
6062intel_user_framebuffer_create(struct drm_device *dev,
6063 struct drm_file *filp,
6064 struct drm_mode_fb_cmd *mode_cmd)
6065{
Chris Wilson05394f32010-11-08 19:18:58 +00006066 struct drm_i915_gem_object *obj;
Dave Airlie38651672010-03-30 05:34:13 +00006067 struct intel_framebuffer *intel_fb;
Jesse Barnes79e53942008-11-07 14:24:08 -08006068 int ret;
6069
Chris Wilson05394f32010-11-08 19:18:58 +00006070 obj = to_intel_bo(drm_gem_object_lookup(dev, filp, mode_cmd->handle));
Jesse Barnes79e53942008-11-07 14:24:08 -08006071 if (!obj)
Chris Wilsoncce13ff2010-08-08 13:36:38 +01006072 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -08006073
Dave Airlie38651672010-03-30 05:34:13 +00006074 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
6075 if (!intel_fb)
Chris Wilsoncce13ff2010-08-08 13:36:38 +01006076 return ERR_PTR(-ENOMEM);
Dave Airlie38651672010-03-30 05:34:13 +00006077
Chris Wilson05394f32010-11-08 19:18:58 +00006078 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08006079 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00006080 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie38651672010-03-30 05:34:13 +00006081 kfree(intel_fb);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01006082 return ERR_PTR(ret);
Jesse Barnes79e53942008-11-07 14:24:08 -08006083 }
6084
Dave Airlie38651672010-03-30 05:34:13 +00006085 return &intel_fb->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08006086}
6087
Jesse Barnes79e53942008-11-07 14:24:08 -08006088static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -08006089 .fb_create = intel_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +00006090 .output_poll_changed = intel_fb_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -08006091};
6092
Chris Wilson05394f32010-11-08 19:18:58 +00006093static struct drm_i915_gem_object *
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08006094intel_alloc_context_page(struct drm_device *dev)
Chris Wilson9ea8d052010-01-04 18:57:56 +00006095{
Chris Wilson05394f32010-11-08 19:18:58 +00006096 struct drm_i915_gem_object *ctx;
Chris Wilson9ea8d052010-01-04 18:57:56 +00006097 int ret;
6098
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08006099 ctx = i915_gem_alloc_object(dev, 4096);
6100 if (!ctx) {
Chris Wilson9ea8d052010-01-04 18:57:56 +00006101 DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
6102 return NULL;
6103 }
6104
6105 mutex_lock(&dev->struct_mutex);
Daniel Vetter75e9e912010-11-04 17:11:09 +01006106 ret = i915_gem_object_pin(ctx, 4096, true);
Chris Wilson9ea8d052010-01-04 18:57:56 +00006107 if (ret) {
6108 DRM_ERROR("failed to pin power context: %d\n", ret);
6109 goto err_unref;
6110 }
6111
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08006112 ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
Chris Wilson9ea8d052010-01-04 18:57:56 +00006113 if (ret) {
6114 DRM_ERROR("failed to set-domain on power context: %d\n", ret);
6115 goto err_unpin;
6116 }
6117 mutex_unlock(&dev->struct_mutex);
6118
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08006119 return ctx;
Chris Wilson9ea8d052010-01-04 18:57:56 +00006120
6121err_unpin:
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08006122 i915_gem_object_unpin(ctx);
Chris Wilson9ea8d052010-01-04 18:57:56 +00006123err_unref:
Chris Wilson05394f32010-11-08 19:18:58 +00006124 drm_gem_object_unreference(&ctx->base);
Chris Wilson9ea8d052010-01-04 18:57:56 +00006125 mutex_unlock(&dev->struct_mutex);
6126 return NULL;
6127}
6128
Jesse Barnes7648fa92010-05-20 14:28:11 -07006129bool ironlake_set_drps(struct drm_device *dev, u8 val)
6130{
6131 struct drm_i915_private *dev_priv = dev->dev_private;
6132 u16 rgvswctl;
6133
6134 rgvswctl = I915_READ16(MEMSWCTL);
6135 if (rgvswctl & MEMCTL_CMD_STS) {
6136 DRM_DEBUG("gpu busy, RCS change rejected\n");
6137 return false; /* still busy with another command */
6138 }
6139
6140 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
6141 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
6142 I915_WRITE16(MEMSWCTL, rgvswctl);
6143 POSTING_READ16(MEMSWCTL);
6144
6145 rgvswctl |= MEMCTL_CMD_STS;
6146 I915_WRITE16(MEMSWCTL, rgvswctl);
6147
6148 return true;
6149}
6150
Jesse Barnesf97108d2010-01-29 11:27:07 -08006151void ironlake_enable_drps(struct drm_device *dev)
6152{
6153 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7648fa92010-05-20 14:28:11 -07006154 u32 rgvmodectl = I915_READ(MEMMODECTL);
Jesse Barnesf97108d2010-01-29 11:27:07 -08006155 u8 fmax, fmin, fstart, vstart;
Jesse Barnesf97108d2010-01-29 11:27:07 -08006156
Jesse Barnesea056c12010-09-10 10:02:13 -07006157 /* Enable temp reporting */
6158 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
6159 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
6160
Jesse Barnesf97108d2010-01-29 11:27:07 -08006161 /* 100ms RC evaluation intervals */
6162 I915_WRITE(RCUPEI, 100000);
6163 I915_WRITE(RCDNEI, 100000);
6164
6165 /* Set max/min thresholds to 90ms and 80ms respectively */
6166 I915_WRITE(RCBMAXAVG, 90000);
6167 I915_WRITE(RCBMINAVG, 80000);
6168
6169 I915_WRITE(MEMIHYST, 1);
6170
6171 /* Set up min, max, and cur for interrupt handling */
6172 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
6173 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
6174 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
6175 MEMMODE_FSTART_SHIFT;
Jesse Barnes7648fa92010-05-20 14:28:11 -07006176
Jesse Barnesf97108d2010-01-29 11:27:07 -08006177 vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
6178 PXVFREQ_PX_SHIFT;
6179
Jesse Barnes80dbf4b2010-11-01 14:12:01 -07006180 dev_priv->fmax = fmax; /* IPS callback will increase this */
Jesse Barnes7648fa92010-05-20 14:28:11 -07006181 dev_priv->fstart = fstart;
6182
Jesse Barnes80dbf4b2010-11-01 14:12:01 -07006183 dev_priv->max_delay = fstart;
Jesse Barnesf97108d2010-01-29 11:27:07 -08006184 dev_priv->min_delay = fmin;
6185 dev_priv->cur_delay = fstart;
6186
Jesse Barnes80dbf4b2010-11-01 14:12:01 -07006187 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
6188 fmax, fmin, fstart);
Jesse Barnes7648fa92010-05-20 14:28:11 -07006189
Jesse Barnesf97108d2010-01-29 11:27:07 -08006190 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
6191
6192 /*
6193 * Interrupts will be enabled in ironlake_irq_postinstall
6194 */
6195
6196 I915_WRITE(VIDSTART, vstart);
6197 POSTING_READ(VIDSTART);
6198
6199 rgvmodectl |= MEMMODE_SWMODE_EN;
6200 I915_WRITE(MEMMODECTL, rgvmodectl);
6201
Chris Wilson481b6af2010-08-23 17:43:35 +01006202 if (wait_for((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Chris Wilson913d8d12010-08-07 11:01:35 +01006203 DRM_ERROR("stuck trying to change perf mode\n");
Jesse Barnesf97108d2010-01-29 11:27:07 -08006204 msleep(1);
6205
Jesse Barnes7648fa92010-05-20 14:28:11 -07006206 ironlake_set_drps(dev, fstart);
Jesse Barnesf97108d2010-01-29 11:27:07 -08006207
Jesse Barnes7648fa92010-05-20 14:28:11 -07006208 dev_priv->last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
6209 I915_READ(0x112e0);
6210 dev_priv->last_time1 = jiffies_to_msecs(jiffies);
6211 dev_priv->last_count2 = I915_READ(0x112f4);
6212 getrawmonotonic(&dev_priv->last_time2);
Jesse Barnesf97108d2010-01-29 11:27:07 -08006213}
6214
6215void ironlake_disable_drps(struct drm_device *dev)
6216{
6217 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7648fa92010-05-20 14:28:11 -07006218 u16 rgvswctl = I915_READ16(MEMSWCTL);
Jesse Barnesf97108d2010-01-29 11:27:07 -08006219
6220 /* Ack interrupts, disable EFC interrupt */
6221 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
6222 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
6223 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
6224 I915_WRITE(DEIIR, DE_PCU_EVENT);
6225 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
6226
6227 /* Go back to the starting frequency */
Jesse Barnes7648fa92010-05-20 14:28:11 -07006228 ironlake_set_drps(dev, dev_priv->fstart);
Jesse Barnesf97108d2010-01-29 11:27:07 -08006229 msleep(1);
6230 rgvswctl |= MEMCTL_CMD_STS;
6231 I915_WRITE(MEMSWCTL, rgvswctl);
6232 msleep(1);
6233
6234}
6235
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08006236void gen6_set_rps(struct drm_device *dev, u8 val)
6237{
6238 struct drm_i915_private *dev_priv = dev->dev_private;
6239 u32 swreq;
6240
6241 swreq = (val & 0x3ff) << 25;
6242 I915_WRITE(GEN6_RPNSWREQ, swreq);
6243}
6244
6245void gen6_disable_rps(struct drm_device *dev)
6246{
6247 struct drm_i915_private *dev_priv = dev->dev_private;
6248
6249 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
6250 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
6251 I915_WRITE(GEN6_PMIER, 0);
6252 I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
6253}
6254
Jesse Barnes7648fa92010-05-20 14:28:11 -07006255static unsigned long intel_pxfreq(u32 vidfreq)
6256{
6257 unsigned long freq;
6258 int div = (vidfreq & 0x3f0000) >> 16;
6259 int post = (vidfreq & 0x3000) >> 12;
6260 int pre = (vidfreq & 0x7);
6261
6262 if (!pre)
6263 return 0;
6264
6265 freq = ((div * 133333) / ((1<<post) * pre));
6266
6267 return freq;
6268}
6269
6270void intel_init_emon(struct drm_device *dev)
6271{
6272 struct drm_i915_private *dev_priv = dev->dev_private;
6273 u32 lcfuse;
6274 u8 pxw[16];
6275 int i;
6276
6277 /* Disable to program */
6278 I915_WRITE(ECR, 0);
6279 POSTING_READ(ECR);
6280
6281 /* Program energy weights for various events */
6282 I915_WRITE(SDEW, 0x15040d00);
6283 I915_WRITE(CSIEW0, 0x007f0000);
6284 I915_WRITE(CSIEW1, 0x1e220004);
6285 I915_WRITE(CSIEW2, 0x04000004);
6286
6287 for (i = 0; i < 5; i++)
6288 I915_WRITE(PEW + (i * 4), 0);
6289 for (i = 0; i < 3; i++)
6290 I915_WRITE(DEW + (i * 4), 0);
6291
6292 /* Program P-state weights to account for frequency power adjustment */
6293 for (i = 0; i < 16; i++) {
6294 u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
6295 unsigned long freq = intel_pxfreq(pxvidfreq);
6296 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
6297 PXVFREQ_PX_SHIFT;
6298 unsigned long val;
6299
6300 val = vid * vid;
6301 val *= (freq / 1000);
6302 val *= 255;
6303 val /= (127*127*900);
6304 if (val > 0xff)
6305 DRM_ERROR("bad pxval: %ld\n", val);
6306 pxw[i] = val;
6307 }
6308 /* Render standby states get 0 weight */
6309 pxw[14] = 0;
6310 pxw[15] = 0;
6311
6312 for (i = 0; i < 4; i++) {
6313 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
6314 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
6315 I915_WRITE(PXW + (i * 4), val);
6316 }
6317
6318 /* Adjust magic regs to magic values (more experimental results) */
6319 I915_WRITE(OGW0, 0);
6320 I915_WRITE(OGW1, 0);
6321 I915_WRITE(EG0, 0x00007f00);
6322 I915_WRITE(EG1, 0x0000000e);
6323 I915_WRITE(EG2, 0x000e0000);
6324 I915_WRITE(EG3, 0x68000300);
6325 I915_WRITE(EG4, 0x42000000);
6326 I915_WRITE(EG5, 0x00140031);
6327 I915_WRITE(EG6, 0);
6328 I915_WRITE(EG7, 0);
6329
6330 for (i = 0; i < 8; i++)
6331 I915_WRITE(PXWL + (i * 4), 0);
6332
6333 /* Enable PMON + select events */
6334 I915_WRITE(ECR, 0x80000019);
6335
6336 lcfuse = I915_READ(LCFUSE02);
6337
6338 dev_priv->corr = (lcfuse & LCFUSE_HIV_MASK);
6339}
6340
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08006341void gen6_enable_rps(struct drm_i915_private *dev_priv)
Chris Wilson8fd26852010-12-08 18:40:43 +00006342{
Jesse Barnesa6044e22010-12-20 11:34:20 -08006343 u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
6344 u32 gt_perf_status = I915_READ(GEN6_GT_PERF_STATUS);
6345 u32 pcu_mbox;
6346 int cur_freq, min_freq, max_freq;
Chris Wilson8fd26852010-12-08 18:40:43 +00006347 int i;
6348
6349 /* Here begins a magic sequence of register writes to enable
6350 * auto-downclocking.
6351 *
6352 * Perhaps there might be some value in exposing these to
6353 * userspace...
6354 */
6355 I915_WRITE(GEN6_RC_STATE, 0);
6356 __gen6_force_wake_get(dev_priv);
6357
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08006358 /* disable the counters and set deterministic thresholds */
Chris Wilson8fd26852010-12-08 18:40:43 +00006359 I915_WRITE(GEN6_RC_CONTROL, 0);
6360
6361 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
6362 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
6363 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
6364 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
6365 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
6366
6367 for (i = 0; i < I915_NUM_RINGS; i++)
6368 I915_WRITE(RING_MAX_IDLE(dev_priv->ring[i].mmio_base), 10);
6369
6370 I915_WRITE(GEN6_RC_SLEEP, 0);
6371 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
6372 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
6373 I915_WRITE(GEN6_RC6p_THRESHOLD, 100000);
6374 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
6375
6376 I915_WRITE(GEN6_RC_CONTROL,
6377 GEN6_RC_CTL_RC6p_ENABLE |
6378 GEN6_RC_CTL_RC6_ENABLE |
Chris Wilson9c3d2f72010-12-17 10:54:26 +00006379 GEN6_RC_CTL_EI_MODE(1) |
Chris Wilson8fd26852010-12-08 18:40:43 +00006380 GEN6_RC_CTL_HW_ENABLE);
6381
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08006382 I915_WRITE(GEN6_RPNSWREQ,
Chris Wilson8fd26852010-12-08 18:40:43 +00006383 GEN6_FREQUENCY(10) |
6384 GEN6_OFFSET(0) |
6385 GEN6_AGGRESSIVE_TURBO);
6386 I915_WRITE(GEN6_RC_VIDEO_FREQ,
6387 GEN6_FREQUENCY(12));
6388
6389 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
6390 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
6391 18 << 24 |
6392 6 << 16);
6393 I915_WRITE(GEN6_RP_UP_THRESHOLD, 90000);
6394 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 100000);
6395 I915_WRITE(GEN6_RP_UP_EI, 100000);
6396 I915_WRITE(GEN6_RP_DOWN_EI, 300000);
6397 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
6398 I915_WRITE(GEN6_RP_CONTROL,
6399 GEN6_RP_MEDIA_TURBO |
6400 GEN6_RP_USE_NORMAL_FREQ |
6401 GEN6_RP_MEDIA_IS_GFX |
6402 GEN6_RP_ENABLE |
6403 GEN6_RP_UP_BUSY_MAX |
6404 GEN6_RP_DOWN_BUSY_MIN);
6405
6406 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6407 500))
6408 DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
6409
6410 I915_WRITE(GEN6_PCODE_DATA, 0);
6411 I915_WRITE(GEN6_PCODE_MAILBOX,
6412 GEN6_PCODE_READY |
6413 GEN6_PCODE_WRITE_MIN_FREQ_TABLE);
6414 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6415 500))
6416 DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
6417
Jesse Barnesa6044e22010-12-20 11:34:20 -08006418 min_freq = (rp_state_cap & 0xff0000) >> 16;
6419 max_freq = rp_state_cap & 0xff;
6420 cur_freq = (gt_perf_status & 0xff00) >> 8;
6421
6422 /* Check for overclock support */
6423 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6424 500))
6425 DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
6426 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_READ_OC_PARAMS);
6427 pcu_mbox = I915_READ(GEN6_PCODE_DATA);
6428 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6429 500))
6430 DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
6431 if (pcu_mbox & (1<<31)) { /* OC supported */
6432 max_freq = pcu_mbox & 0xff;
6433 DRM_DEBUG_DRIVER("overclocking supported, adjusting frequency max to %dMHz\n", pcu_mbox * 100);
6434 }
6435
6436 /* In units of 100MHz */
6437 dev_priv->max_delay = max_freq;
6438 dev_priv->min_delay = min_freq;
6439 dev_priv->cur_delay = cur_freq;
6440
Chris Wilson8fd26852010-12-08 18:40:43 +00006441 /* requires MSI enabled */
6442 I915_WRITE(GEN6_PMIER,
6443 GEN6_PM_MBOX_EVENT |
6444 GEN6_PM_THERMAL_EVENT |
6445 GEN6_PM_RP_DOWN_TIMEOUT |
6446 GEN6_PM_RP_UP_THRESHOLD |
6447 GEN6_PM_RP_DOWN_THRESHOLD |
6448 GEN6_PM_RP_UP_EI_EXPIRED |
6449 GEN6_PM_RP_DOWN_EI_EXPIRED);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08006450 I915_WRITE(GEN6_PMIMR, 0);
6451 /* enable all PM interrupts */
6452 I915_WRITE(GEN6_PMINTRMSK, 0);
Chris Wilson8fd26852010-12-08 18:40:43 +00006453
6454 __gen6_force_wake_put(dev_priv);
6455}
6456
Chris Wilson0cdab212010-12-05 17:27:06 +00006457void intel_enable_clock_gating(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07006458{
6459 struct drm_i915_private *dev_priv = dev->dev_private;
6460
6461 /*
6462 * Disable clock gating reported to work incorrectly according to the
6463 * specs, but enable as much else as we can.
6464 */
Eric Anholtbad720f2009-10-22 16:11:14 -07006465 if (HAS_PCH_SPLIT(dev)) {
Eric Anholt8956c8b2010-03-18 13:21:14 -07006466 uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
6467
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01006468 if (IS_GEN5(dev)) {
Eric Anholt8956c8b2010-03-18 13:21:14 -07006469 /* Required for FBC */
6470 dspclk_gate |= DPFDUNIT_CLOCK_GATE_DISABLE;
6471 /* Required for CxSR */
6472 dspclk_gate |= DPARBUNIT_CLOCK_GATE_DISABLE;
6473
6474 I915_WRITE(PCH_3DCGDIS0,
6475 MARIUNIT_CLOCK_GATE_DISABLE |
6476 SVSMUNIT_CLOCK_GATE_DISABLE);
Eric Anholt06f37752010-12-14 10:06:46 -08006477 I915_WRITE(PCH_3DCGDIS1,
6478 VFMUNIT_CLOCK_GATE_DISABLE);
Eric Anholt8956c8b2010-03-18 13:21:14 -07006479 }
6480
6481 I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08006482
6483 /*
Jesse Barnes382b0932010-10-07 16:01:25 -07006484 * On Ibex Peak and Cougar Point, we need to disable clock
6485 * gating for the panel power sequencer or it will fail to
6486 * start up when no ports are active.
6487 */
6488 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
6489
6490 /*
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08006491 * According to the spec the following bits should be set in
6492 * order to enable memory self-refresh
6493 * The bit 22/21 of 0x42004
6494 * The bit 5 of 0x42020
6495 * The bit 15 of 0x45000
6496 */
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01006497 if (IS_GEN5(dev)) {
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08006498 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6499 (I915_READ(ILK_DISPLAY_CHICKEN2) |
6500 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
6501 I915_WRITE(ILK_DSPCLK_GATE,
6502 (I915_READ(ILK_DSPCLK_GATE) |
6503 ILK_DPARB_CLK_GATE));
6504 I915_WRITE(DISP_ARB_CTL,
6505 (I915_READ(DISP_ARB_CTL) |
6506 DISP_FBC_WM_DIS));
Yuanhan Liu13982612010-12-15 15:42:31 +08006507 I915_WRITE(WM3_LP_ILK, 0);
6508 I915_WRITE(WM2_LP_ILK, 0);
6509 I915_WRITE(WM1_LP_ILK, 0);
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08006510 }
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08006511 /*
6512 * Based on the document from hardware guys the following bits
6513 * should be set unconditionally in order to enable FBC.
6514 * The bit 22 of 0x42000
6515 * The bit 22 of 0x42004
6516 * The bit 7,8,9 of 0x42020.
6517 */
6518 if (IS_IRONLAKE_M(dev)) {
6519 I915_WRITE(ILK_DISPLAY_CHICKEN1,
6520 I915_READ(ILK_DISPLAY_CHICKEN1) |
6521 ILK_FBCQ_DIS);
6522 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6523 I915_READ(ILK_DISPLAY_CHICKEN2) |
6524 ILK_DPARB_GATE);
6525 I915_WRITE(ILK_DSPCLK_GATE,
6526 I915_READ(ILK_DSPCLK_GATE) |
6527 ILK_DPFC_DIS1 |
6528 ILK_DPFC_DIS2 |
6529 ILK_CLK_FBC);
6530 }
Eric Anholtde6e2ea2010-11-06 14:53:32 -07006531
Eric Anholt67e92af2010-11-06 14:53:33 -07006532 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6533 I915_READ(ILK_DISPLAY_CHICKEN2) |
6534 ILK_ELPIN_409_SELECT);
6535
Eric Anholtde6e2ea2010-11-06 14:53:32 -07006536 if (IS_GEN5(dev)) {
6537 I915_WRITE(_3D_CHICKEN2,
6538 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
6539 _3D_CHICKEN2_WM_READ_PIPELINED);
6540 }
Chris Wilson8fd26852010-12-08 18:40:43 +00006541
Yuanhan Liu13982612010-12-15 15:42:31 +08006542 if (IS_GEN6(dev)) {
6543 I915_WRITE(WM3_LP_ILK, 0);
6544 I915_WRITE(WM2_LP_ILK, 0);
6545 I915_WRITE(WM1_LP_ILK, 0);
6546
6547 /*
6548 * According to the spec the following bits should be
6549 * set in order to enable memory self-refresh and fbc:
6550 * The bit21 and bit22 of 0x42000
6551 * The bit21 and bit22 of 0x42004
6552 * The bit5 and bit7 of 0x42020
6553 * The bit14 of 0x70180
6554 * The bit14 of 0x71180
6555 */
6556 I915_WRITE(ILK_DISPLAY_CHICKEN1,
6557 I915_READ(ILK_DISPLAY_CHICKEN1) |
6558 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
6559 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6560 I915_READ(ILK_DISPLAY_CHICKEN2) |
6561 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
6562 I915_WRITE(ILK_DSPCLK_GATE,
6563 I915_READ(ILK_DSPCLK_GATE) |
6564 ILK_DPARB_CLK_GATE |
6565 ILK_DPFD_CLK_GATE);
6566
6567 I915_WRITE(DSPACNTR,
6568 I915_READ(DSPACNTR) |
6569 DISPPLANE_TRICKLE_FEED_DISABLE);
6570 I915_WRITE(DSPBCNTR,
6571 I915_READ(DSPBCNTR) |
6572 DISPPLANE_TRICKLE_FEED_DISABLE);
6573 }
Zhenyu Wangc03342f2009-09-29 11:01:23 +08006574 } else if (IS_G4X(dev)) {
Jesse Barnes652c3932009-08-17 13:31:43 -07006575 uint32_t dspclk_gate;
6576 I915_WRITE(RENCLK_GATE_D1, 0);
6577 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
6578 GS_UNIT_CLOCK_GATE_DISABLE |
6579 CL_UNIT_CLOCK_GATE_DISABLE);
6580 I915_WRITE(RAMCLK_GATE_D, 0);
6581 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
6582 OVRUNIT_CLOCK_GATE_DISABLE |
6583 OVCUNIT_CLOCK_GATE_DISABLE;
6584 if (IS_GM45(dev))
6585 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
6586 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006587 } else if (IS_CRESTLINE(dev)) {
Jesse Barnes652c3932009-08-17 13:31:43 -07006588 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
6589 I915_WRITE(RENCLK_GATE_D2, 0);
6590 I915_WRITE(DSPCLK_GATE_D, 0);
6591 I915_WRITE(RAMCLK_GATE_D, 0);
6592 I915_WRITE16(DEUC, 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006593 } else if (IS_BROADWATER(dev)) {
Jesse Barnes652c3932009-08-17 13:31:43 -07006594 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
6595 I965_RCC_CLOCK_GATE_DISABLE |
6596 I965_RCPB_CLOCK_GATE_DISABLE |
6597 I965_ISC_CLOCK_GATE_DISABLE |
6598 I965_FBC_CLOCK_GATE_DISABLE);
6599 I915_WRITE(RENCLK_GATE_D2, 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006600 } else if (IS_GEN3(dev)) {
Jesse Barnes652c3932009-08-17 13:31:43 -07006601 u32 dstate = I915_READ(D_STATE);
6602
6603 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
6604 DSTATE_DOT_CLOCK_GATING;
6605 I915_WRITE(D_STATE, dstate);
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +02006606 } else if (IS_I85X(dev) || IS_I865G(dev)) {
Jesse Barnes652c3932009-08-17 13:31:43 -07006607 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
6608 } else if (IS_I830(dev)) {
6609 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
6610 }
6611}
6612
Chris Wilson0cdab212010-12-05 17:27:06 +00006613void intel_disable_clock_gating(struct drm_device *dev)
6614{
6615 struct drm_i915_private *dev_priv = dev->dev_private;
6616
6617 if (dev_priv->renderctx) {
6618 struct drm_i915_gem_object *obj = dev_priv->renderctx;
6619
6620 I915_WRITE(CCID, 0);
6621 POSTING_READ(CCID);
6622
6623 i915_gem_object_unpin(obj);
6624 drm_gem_object_unreference(&obj->base);
6625 dev_priv->renderctx = NULL;
6626 }
6627
6628 if (dev_priv->pwrctx) {
6629 struct drm_i915_gem_object *obj = dev_priv->pwrctx;
6630
6631 I915_WRITE(PWRCTXA, 0);
6632 POSTING_READ(PWRCTXA);
6633
6634 i915_gem_object_unpin(obj);
6635 drm_gem_object_unreference(&obj->base);
6636 dev_priv->pwrctx = NULL;
6637 }
6638}
6639
Jesse Barnesd5bb0812011-01-05 12:01:26 -08006640static void ironlake_disable_rc6(struct drm_device *dev)
6641{
6642 struct drm_i915_private *dev_priv = dev->dev_private;
6643
6644 /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
6645 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
6646 wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
6647 10);
6648 POSTING_READ(CCID);
6649 I915_WRITE(PWRCTXA, 0);
6650 POSTING_READ(PWRCTXA);
6651 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
6652 POSTING_READ(RSTDBYCTL);
6653 i915_gem_object_unpin(dev_priv->renderctx);
6654 drm_gem_object_unreference(&dev_priv->renderctx->base);
6655 dev_priv->renderctx = NULL;
6656 i915_gem_object_unpin(dev_priv->pwrctx);
6657 drm_gem_object_unreference(&dev_priv->pwrctx->base);
6658 dev_priv->pwrctx = NULL;
6659}
6660
6661void ironlake_enable_rc6(struct drm_device *dev)
6662{
6663 struct drm_i915_private *dev_priv = dev->dev_private;
6664 int ret;
6665
6666 /*
6667 * GPU can automatically power down the render unit if given a page
6668 * to save state.
6669 */
6670 ret = BEGIN_LP_RING(6);
6671 if (ret) {
6672 ironlake_disable_rc6(dev);
6673 return;
6674 }
6675 OUT_RING(MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
6676 OUT_RING(MI_SET_CONTEXT);
6677 OUT_RING(dev_priv->renderctx->gtt_offset |
6678 MI_MM_SPACE_GTT |
6679 MI_SAVE_EXT_STATE_EN |
6680 MI_RESTORE_EXT_STATE_EN |
6681 MI_RESTORE_INHIBIT);
6682 OUT_RING(MI_SUSPEND_FLUSH);
6683 OUT_RING(MI_NOOP);
6684 OUT_RING(MI_FLUSH);
6685 ADVANCE_LP_RING();
6686
6687 I915_WRITE(PWRCTXA, dev_priv->pwrctx->gtt_offset | PWRCTX_EN);
6688 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
6689}
6690
Jesse Barnese70236a2009-09-21 10:42:27 -07006691/* Set up chip specific display functions */
6692static void intel_init_display(struct drm_device *dev)
6693{
6694 struct drm_i915_private *dev_priv = dev->dev_private;
6695
6696 /* We always want a DPMS function */
Eric Anholtbad720f2009-10-22 16:11:14 -07006697 if (HAS_PCH_SPLIT(dev))
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006698 dev_priv->display.dpms = ironlake_crtc_dpms;
Jesse Barnese70236a2009-09-21 10:42:27 -07006699 else
6700 dev_priv->display.dpms = i9xx_crtc_dpms;
6701
Adam Jacksonee5382a2010-04-23 11:17:39 -04006702 if (I915_HAS_FBC(dev)) {
Yuanhan Liu9c04f012010-12-15 15:42:32 +08006703 if (HAS_PCH_SPLIT(dev)) {
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08006704 dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
6705 dev_priv->display.enable_fbc = ironlake_enable_fbc;
6706 dev_priv->display.disable_fbc = ironlake_disable_fbc;
6707 } else if (IS_GM45(dev)) {
Jesse Barnes74dff282009-09-14 15:39:40 -07006708 dev_priv->display.fbc_enabled = g4x_fbc_enabled;
6709 dev_priv->display.enable_fbc = g4x_enable_fbc;
6710 dev_priv->display.disable_fbc = g4x_disable_fbc;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006711 } else if (IS_CRESTLINE(dev)) {
Jesse Barnese70236a2009-09-21 10:42:27 -07006712 dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
6713 dev_priv->display.enable_fbc = i8xx_enable_fbc;
6714 dev_priv->display.disable_fbc = i8xx_disable_fbc;
6715 }
Jesse Barnes74dff282009-09-14 15:39:40 -07006716 /* 855GM needs testing */
Jesse Barnese70236a2009-09-21 10:42:27 -07006717 }
6718
6719 /* Returns the core display clock speed */
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006720 if (IS_I945G(dev) || (IS_G33(dev) && ! IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -07006721 dev_priv->display.get_display_clock_speed =
6722 i945_get_display_clock_speed;
6723 else if (IS_I915G(dev))
6724 dev_priv->display.get_display_clock_speed =
6725 i915_get_display_clock_speed;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006726 else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07006727 dev_priv->display.get_display_clock_speed =
6728 i9xx_misc_get_display_clock_speed;
6729 else if (IS_I915GM(dev))
6730 dev_priv->display.get_display_clock_speed =
6731 i915gm_get_display_clock_speed;
6732 else if (IS_I865G(dev))
6733 dev_priv->display.get_display_clock_speed =
6734 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +02006735 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07006736 dev_priv->display.get_display_clock_speed =
6737 i855_get_display_clock_speed;
6738 else /* 852, 830 */
6739 dev_priv->display.get_display_clock_speed =
6740 i830_get_display_clock_speed;
6741
6742 /* For FIFO watermark updates */
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08006743 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01006744 if (IS_GEN5(dev)) {
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08006745 if (I915_READ(MLTR_ILK) & ILK_SRLT_MASK)
6746 dev_priv->display.update_wm = ironlake_update_wm;
6747 else {
6748 DRM_DEBUG_KMS("Failed to get proper latency. "
6749 "Disable CxSR\n");
6750 dev_priv->display.update_wm = NULL;
6751 }
Yuanhan Liu13982612010-12-15 15:42:31 +08006752 } else if (IS_GEN6(dev)) {
6753 if (SNB_READ_WM0_LATENCY()) {
6754 dev_priv->display.update_wm = sandybridge_update_wm;
6755 } else {
6756 DRM_DEBUG_KMS("Failed to read display plane latency. "
6757 "Disable CxSR\n");
6758 dev_priv->display.update_wm = NULL;
6759 }
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08006760 } else
6761 dev_priv->display.update_wm = NULL;
6762 } else if (IS_PINEVIEW(dev)) {
Zhao Yakuid4294342010-03-22 22:45:36 +08006763 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
Li Peng95534262010-05-18 18:58:44 +08006764 dev_priv->is_ddr3,
Zhao Yakuid4294342010-03-22 22:45:36 +08006765 dev_priv->fsb_freq,
6766 dev_priv->mem_freq)) {
6767 DRM_INFO("failed to find known CxSR latency "
Li Peng95534262010-05-18 18:58:44 +08006768 "(found ddr%s fsb freq %d, mem freq %d), "
Zhao Yakuid4294342010-03-22 22:45:36 +08006769 "disabling CxSR\n",
Li Peng95534262010-05-18 18:58:44 +08006770 (dev_priv->is_ddr3 == 1) ? "3": "2",
Zhao Yakuid4294342010-03-22 22:45:36 +08006771 dev_priv->fsb_freq, dev_priv->mem_freq);
6772 /* Disable CxSR and never update its watermark again */
6773 pineview_disable_cxsr(dev);
6774 dev_priv->display.update_wm = NULL;
6775 } else
6776 dev_priv->display.update_wm = pineview_update_wm;
6777 } else if (IS_G4X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07006778 dev_priv->display.update_wm = g4x_update_wm;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006779 else if (IS_GEN4(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07006780 dev_priv->display.update_wm = i965_update_wm;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006781 else if (IS_GEN3(dev)) {
Jesse Barnese70236a2009-09-21 10:42:27 -07006782 dev_priv->display.update_wm = i9xx_update_wm;
6783 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Adam Jackson8f4695e2010-04-16 18:20:57 -04006784 } else if (IS_I85X(dev)) {
6785 dev_priv->display.update_wm = i9xx_update_wm;
6786 dev_priv->display.get_fifo_size = i85x_get_fifo_size;
Jesse Barnese70236a2009-09-21 10:42:27 -07006787 } else {
Adam Jackson8f4695e2010-04-16 18:20:57 -04006788 dev_priv->display.update_wm = i830_update_wm;
6789 if (IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07006790 dev_priv->display.get_fifo_size = i845_get_fifo_size;
6791 else
6792 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Jesse Barnese70236a2009-09-21 10:42:27 -07006793 }
6794}
6795
Jesse Barnesb690e962010-07-19 13:53:12 -07006796/*
6797 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
6798 * resume, or other times. This quirk makes sure that's the case for
6799 * affected systems.
6800 */
6801static void quirk_pipea_force (struct drm_device *dev)
6802{
6803 struct drm_i915_private *dev_priv = dev->dev_private;
6804
6805 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
6806 DRM_DEBUG_DRIVER("applying pipe a force quirk\n");
6807}
6808
6809struct intel_quirk {
6810 int device;
6811 int subsystem_vendor;
6812 int subsystem_device;
6813 void (*hook)(struct drm_device *dev);
6814};
6815
6816struct intel_quirk intel_quirks[] = {
6817 /* HP Compaq 2730p needs pipe A force quirk (LP: #291555) */
6818 { 0x2a42, 0x103c, 0x30eb, quirk_pipea_force },
6819 /* HP Mini needs pipe A force quirk (LP: #322104) */
6820 { 0x27ae,0x103c, 0x361a, quirk_pipea_force },
6821
6822 /* Thinkpad R31 needs pipe A force quirk */
6823 { 0x3577, 0x1014, 0x0505, quirk_pipea_force },
6824 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
6825 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
6826
6827 /* ThinkPad X30 needs pipe A force quirk (LP: #304614) */
6828 { 0x3577, 0x1014, 0x0513, quirk_pipea_force },
6829 /* ThinkPad X40 needs pipe A force quirk */
6830
6831 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
6832 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
6833
6834 /* 855 & before need to leave pipe A & dpll A up */
6835 { 0x3582, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
6836 { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
6837};
6838
6839static void intel_init_quirks(struct drm_device *dev)
6840{
6841 struct pci_dev *d = dev->pdev;
6842 int i;
6843
6844 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
6845 struct intel_quirk *q = &intel_quirks[i];
6846
6847 if (d->device == q->device &&
6848 (d->subsystem_vendor == q->subsystem_vendor ||
6849 q->subsystem_vendor == PCI_ANY_ID) &&
6850 (d->subsystem_device == q->subsystem_device ||
6851 q->subsystem_device == PCI_ANY_ID))
6852 q->hook(dev);
6853 }
6854}
6855
Jesse Barnes9cce37f2010-08-13 15:11:26 -07006856/* Disable the VGA plane that we never use */
6857static void i915_disable_vga(struct drm_device *dev)
6858{
6859 struct drm_i915_private *dev_priv = dev->dev_private;
6860 u8 sr1;
6861 u32 vga_reg;
6862
6863 if (HAS_PCH_SPLIT(dev))
6864 vga_reg = CPU_VGACNTRL;
6865 else
6866 vga_reg = VGACNTRL;
6867
6868 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
6869 outb(1, VGA_SR_INDEX);
6870 sr1 = inb(VGA_SR_DATA);
6871 outb(sr1 | 1<<5, VGA_SR_DATA);
6872 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
6873 udelay(300);
6874
6875 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
6876 POSTING_READ(vga_reg);
6877}
6878
Jesse Barnes79e53942008-11-07 14:24:08 -08006879void intel_modeset_init(struct drm_device *dev)
6880{
Jesse Barnes652c3932009-08-17 13:31:43 -07006881 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08006882 int i;
6883
6884 drm_mode_config_init(dev);
6885
6886 dev->mode_config.min_width = 0;
6887 dev->mode_config.min_height = 0;
6888
6889 dev->mode_config.funcs = (void *)&intel_mode_funcs;
6890
Jesse Barnesb690e962010-07-19 13:53:12 -07006891 intel_init_quirks(dev);
6892
Jesse Barnese70236a2009-09-21 10:42:27 -07006893 intel_init_display(dev);
6894
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006895 if (IS_GEN2(dev)) {
6896 dev->mode_config.max_width = 2048;
6897 dev->mode_config.max_height = 2048;
6898 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -07006899 dev->mode_config.max_width = 4096;
6900 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -08006901 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006902 dev->mode_config.max_width = 8192;
6903 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -08006904 }
Chris Wilson35c30472010-12-22 14:07:12 +00006905 dev->mode_config.fb_base = dev->agp->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08006906
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006907 if (IS_MOBILE(dev) || !IS_GEN2(dev))
Dave Airliea3524f12010-06-06 18:59:41 +10006908 dev_priv->num_pipe = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -08006909 else
Dave Airliea3524f12010-06-06 18:59:41 +10006910 dev_priv->num_pipe = 1;
Zhao Yakui28c97732009-10-09 11:39:41 +08006911 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Dave Airliea3524f12010-06-06 18:59:41 +10006912 dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -08006913
Dave Airliea3524f12010-06-06 18:59:41 +10006914 for (i = 0; i < dev_priv->num_pipe; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006915 intel_crtc_init(dev, i);
6916 }
6917
6918 intel_setup_outputs(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -07006919
Chris Wilson0cdab212010-12-05 17:27:06 +00006920 intel_enable_clock_gating(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -07006921
Jesse Barnes9cce37f2010-08-13 15:11:26 -07006922 /* Just disable it once at startup */
6923 i915_disable_vga(dev);
6924
Jesse Barnes7648fa92010-05-20 14:28:11 -07006925 if (IS_IRONLAKE_M(dev)) {
Jesse Barnesf97108d2010-01-29 11:27:07 -08006926 ironlake_enable_drps(dev);
Jesse Barnes7648fa92010-05-20 14:28:11 -07006927 intel_init_emon(dev);
6928 }
Jesse Barnesf97108d2010-01-29 11:27:07 -08006929
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08006930 if (IS_GEN6(dev))
6931 gen6_enable_rps(dev_priv);
6932
Jesse Barnesd5bb0812011-01-05 12:01:26 -08006933 if (IS_IRONLAKE_M(dev)) {
6934 dev_priv->renderctx = intel_alloc_context_page(dev);
6935 if (!dev_priv->renderctx)
6936 goto skip_rc6;
6937 dev_priv->pwrctx = intel_alloc_context_page(dev);
6938 if (!dev_priv->pwrctx) {
6939 i915_gem_object_unpin(dev_priv->renderctx);
6940 drm_gem_object_unreference(&dev_priv->renderctx->base);
6941 dev_priv->renderctx = NULL;
6942 goto skip_rc6;
6943 }
6944 ironlake_enable_rc6(dev);
6945 }
6946
6947skip_rc6:
Jesse Barnes652c3932009-08-17 13:31:43 -07006948 INIT_WORK(&dev_priv->idle_work, intel_idle_update);
6949 setup_timer(&dev_priv->idle_timer, intel_gpu_idle_timer,
6950 (unsigned long)dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02006951
6952 intel_setup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006953}
6954
6955void intel_modeset_cleanup(struct drm_device *dev)
6956{
Jesse Barnes652c3932009-08-17 13:31:43 -07006957 struct drm_i915_private *dev_priv = dev->dev_private;
6958 struct drm_crtc *crtc;
6959 struct intel_crtc *intel_crtc;
6960
Keith Packardf87ea762010-10-03 19:36:26 -07006961 drm_kms_helper_poll_fini(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -07006962 mutex_lock(&dev->struct_mutex);
6963
Jesse Barnes723bfd72010-10-07 16:01:13 -07006964 intel_unregister_dsm_handler();
6965
6966
Jesse Barnes652c3932009-08-17 13:31:43 -07006967 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
6968 /* Skip inactive CRTCs */
6969 if (!crtc->fb)
6970 continue;
6971
6972 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3dec0092010-08-20 21:40:52 +02006973 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07006974 }
6975
Jesse Barnese70236a2009-09-21 10:42:27 -07006976 if (dev_priv->display.disable_fbc)
6977 dev_priv->display.disable_fbc(dev);
6978
Jesse Barnesf97108d2010-01-29 11:27:07 -08006979 if (IS_IRONLAKE_M(dev))
6980 ironlake_disable_drps(dev);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08006981 if (IS_GEN6(dev))
6982 gen6_disable_rps(dev);
Jesse Barnesf97108d2010-01-29 11:27:07 -08006983
Jesse Barnesd5bb0812011-01-05 12:01:26 -08006984 if (IS_IRONLAKE_M(dev))
6985 ironlake_disable_rc6(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +00006986
Kristian Høgsberg69341a52009-11-11 12:19:17 -05006987 mutex_unlock(&dev->struct_mutex);
6988
Daniel Vetter6c0d93502010-08-20 18:26:46 +02006989 /* Disable the irq before mode object teardown, for the irq might
6990 * enqueue unpin/hotplug work. */
6991 drm_irq_uninstall(dev);
6992 cancel_work_sync(&dev_priv->hotplug_work);
6993
Daniel Vetter3dec0092010-08-20 21:40:52 +02006994 /* Shut off idle work before the crtcs get freed. */
6995 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
6996 intel_crtc = to_intel_crtc(crtc);
6997 del_timer_sync(&intel_crtc->idle_timer);
6998 }
6999 del_timer_sync(&dev_priv->idle_timer);
7000 cancel_work_sync(&dev_priv->idle_work);
7001
Jesse Barnes79e53942008-11-07 14:24:08 -08007002 drm_mode_config_cleanup(dev);
7003}
7004
Dave Airlie28d52042009-09-21 14:33:58 +10007005/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +08007006 * Return which encoder is currently attached for connector.
7007 */
Chris Wilsondf0e9242010-09-09 16:20:55 +01007008struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -08007009{
Chris Wilsondf0e9242010-09-09 16:20:55 +01007010 return &intel_attached_encoder(connector)->base;
7011}
Jesse Barnes79e53942008-11-07 14:24:08 -08007012
Chris Wilsondf0e9242010-09-09 16:20:55 +01007013void intel_connector_attach_encoder(struct intel_connector *connector,
7014 struct intel_encoder *encoder)
7015{
7016 connector->encoder = encoder;
7017 drm_mode_connector_attach_encoder(&connector->base,
7018 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08007019}
Dave Airlie28d52042009-09-21 14:33:58 +10007020
7021/*
7022 * set vga decode state - true == enable VGA decode
7023 */
7024int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
7025{
7026 struct drm_i915_private *dev_priv = dev->dev_private;
7027 u16 gmch_ctrl;
7028
7029 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
7030 if (state)
7031 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
7032 else
7033 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
7034 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
7035 return 0;
7036}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00007037
7038#ifdef CONFIG_DEBUG_FS
7039#include <linux/seq_file.h>
7040
7041struct intel_display_error_state {
7042 struct intel_cursor_error_state {
7043 u32 control;
7044 u32 position;
7045 u32 base;
7046 u32 size;
7047 } cursor[2];
7048
7049 struct intel_pipe_error_state {
7050 u32 conf;
7051 u32 source;
7052
7053 u32 htotal;
7054 u32 hblank;
7055 u32 hsync;
7056 u32 vtotal;
7057 u32 vblank;
7058 u32 vsync;
7059 } pipe[2];
7060
7061 struct intel_plane_error_state {
7062 u32 control;
7063 u32 stride;
7064 u32 size;
7065 u32 pos;
7066 u32 addr;
7067 u32 surface;
7068 u32 tile_offset;
7069 } plane[2];
7070};
7071
7072struct intel_display_error_state *
7073intel_display_capture_error_state(struct drm_device *dev)
7074{
7075 drm_i915_private_t *dev_priv = dev->dev_private;
7076 struct intel_display_error_state *error;
7077 int i;
7078
7079 error = kmalloc(sizeof(*error), GFP_ATOMIC);
7080 if (error == NULL)
7081 return NULL;
7082
7083 for (i = 0; i < 2; i++) {
7084 error->cursor[i].control = I915_READ(CURCNTR(i));
7085 error->cursor[i].position = I915_READ(CURPOS(i));
7086 error->cursor[i].base = I915_READ(CURBASE(i));
7087
7088 error->plane[i].control = I915_READ(DSPCNTR(i));
7089 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
7090 error->plane[i].size = I915_READ(DSPSIZE(i));
7091 error->plane[i].pos= I915_READ(DSPPOS(i));
7092 error->plane[i].addr = I915_READ(DSPADDR(i));
7093 if (INTEL_INFO(dev)->gen >= 4) {
7094 error->plane[i].surface = I915_READ(DSPSURF(i));
7095 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
7096 }
7097
7098 error->pipe[i].conf = I915_READ(PIPECONF(i));
7099 error->pipe[i].source = I915_READ(PIPESRC(i));
7100 error->pipe[i].htotal = I915_READ(HTOTAL(i));
7101 error->pipe[i].hblank = I915_READ(HBLANK(i));
7102 error->pipe[i].hsync = I915_READ(HSYNC(i));
7103 error->pipe[i].vtotal = I915_READ(VTOTAL(i));
7104 error->pipe[i].vblank = I915_READ(VBLANK(i));
7105 error->pipe[i].vsync = I915_READ(VSYNC(i));
7106 }
7107
7108 return error;
7109}
7110
7111void
7112intel_display_print_error_state(struct seq_file *m,
7113 struct drm_device *dev,
7114 struct intel_display_error_state *error)
7115{
7116 int i;
7117
7118 for (i = 0; i < 2; i++) {
7119 seq_printf(m, "Pipe [%d]:\n", i);
7120 seq_printf(m, " CONF: %08x\n", error->pipe[i].conf);
7121 seq_printf(m, " SRC: %08x\n", error->pipe[i].source);
7122 seq_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
7123 seq_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
7124 seq_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
7125 seq_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
7126 seq_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
7127 seq_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
7128
7129 seq_printf(m, "Plane [%d]:\n", i);
7130 seq_printf(m, " CNTR: %08x\n", error->plane[i].control);
7131 seq_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
7132 seq_printf(m, " SIZE: %08x\n", error->plane[i].size);
7133 seq_printf(m, " POS: %08x\n", error->plane[i].pos);
7134 seq_printf(m, " ADDR: %08x\n", error->plane[i].addr);
7135 if (INTEL_INFO(dev)->gen >= 4) {
7136 seq_printf(m, " SURF: %08x\n", error->plane[i].surface);
7137 seq_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
7138 }
7139
7140 seq_printf(m, "Cursor [%d]:\n", i);
7141 seq_printf(m, " CNTR: %08x\n", error->cursor[i].control);
7142 seq_printf(m, " POS: %08x\n", error->cursor[i].position);
7143 seq_printf(m, " BASE: %08x\n", error->cursor[i].base);
7144 }
7145}
7146#endif