blob: 92e0aae7070a97326eed3b5c32f55065e8040d77 [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>
Jesse Barnes79e53942008-11-07 14:24:08 -080031#include "drmP.h"
32#include "intel_drv.h"
33#include "i915_drm.h"
34#include "i915_drv.h"
Keith Packarda4fc5ed2009-04-07 16:16:42 -070035#include "intel_dp.h"
Jesse Barnes79e53942008-11-07 14:24:08 -080036
37#include "drm_crtc_helper.h"
38
Zhenyu Wang32f9d652009-07-24 01:00:32 +080039#define HAS_eDP (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
40
Jesse Barnes79e53942008-11-07 14:24:08 -080041bool intel_pipe_has_type (struct drm_crtc *crtc, int type);
Shaohua Li7662c8b2009-06-26 11:23:55 +080042static void intel_update_watermarks(struct drm_device *dev);
Jesse Barnes652c3932009-08-17 13:31:43 -070043static void intel_increase_pllclock(struct drm_crtc *crtc, bool schedule);
Jesse Barnes79e53942008-11-07 14:24:08 -080044
45typedef struct {
46 /* given values */
47 int n;
48 int m1, m2;
49 int p1, p2;
50 /* derived values */
51 int dot;
52 int vco;
53 int m;
54 int p;
55} intel_clock_t;
56
57typedef struct {
58 int min, max;
59} intel_range_t;
60
61typedef struct {
62 int dot_limit;
63 int p2_slow, p2_fast;
64} intel_p2_t;
65
66#define INTEL_P2_NUM 2
Ma Lingd4906092009-03-18 20:13:27 +080067typedef struct intel_limit intel_limit_t;
68struct intel_limit {
Jesse Barnes79e53942008-11-07 14:24:08 -080069 intel_range_t dot, vco, n, m, m1, m2, p, p1;
70 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +080071 bool (* find_pll)(const intel_limit_t *, struct drm_crtc *,
72 int, int, intel_clock_t *);
Jesse Barnes652c3932009-08-17 13:31:43 -070073 bool (* find_reduced_pll)(const intel_limit_t *, struct drm_crtc *,
74 int, int, intel_clock_t *);
Ma Lingd4906092009-03-18 20:13:27 +080075};
Jesse Barnes79e53942008-11-07 14:24:08 -080076
77#define I8XX_DOT_MIN 25000
78#define I8XX_DOT_MAX 350000
79#define I8XX_VCO_MIN 930000
80#define I8XX_VCO_MAX 1400000
81#define I8XX_N_MIN 3
82#define I8XX_N_MAX 16
83#define I8XX_M_MIN 96
84#define I8XX_M_MAX 140
85#define I8XX_M1_MIN 18
86#define I8XX_M1_MAX 26
87#define I8XX_M2_MIN 6
88#define I8XX_M2_MAX 16
89#define I8XX_P_MIN 4
90#define I8XX_P_MAX 128
91#define I8XX_P1_MIN 2
92#define I8XX_P1_MAX 33
93#define I8XX_P1_LVDS_MIN 1
94#define I8XX_P1_LVDS_MAX 6
95#define I8XX_P2_SLOW 4
96#define I8XX_P2_FAST 2
97#define I8XX_P2_LVDS_SLOW 14
ling.ma@intel.com0c2e3952009-07-17 11:44:30 +080098#define I8XX_P2_LVDS_FAST 7
Jesse Barnes79e53942008-11-07 14:24:08 -080099#define I8XX_P2_SLOW_LIMIT 165000
100
101#define I9XX_DOT_MIN 20000
102#define I9XX_DOT_MAX 400000
103#define I9XX_VCO_MIN 1400000
104#define I9XX_VCO_MAX 2800000
Shaohua Li21778322009-02-23 15:19:16 +0800105#define IGD_VCO_MIN 1700000
106#define IGD_VCO_MAX 3500000
Kristian Høgsbergf3cade52009-02-13 20:56:50 -0500107#define I9XX_N_MIN 1
108#define I9XX_N_MAX 6
Shaohua Li21778322009-02-23 15:19:16 +0800109/* IGD's Ncounter is a ring counter */
110#define IGD_N_MIN 3
111#define IGD_N_MAX 6
Jesse Barnes79e53942008-11-07 14:24:08 -0800112#define I9XX_M_MIN 70
113#define I9XX_M_MAX 120
Shaohua Li21778322009-02-23 15:19:16 +0800114#define IGD_M_MIN 2
115#define IGD_M_MAX 256
Jesse Barnes79e53942008-11-07 14:24:08 -0800116#define I9XX_M1_MIN 10
Kristian Høgsbergf3cade52009-02-13 20:56:50 -0500117#define I9XX_M1_MAX 22
Jesse Barnes79e53942008-11-07 14:24:08 -0800118#define I9XX_M2_MIN 5
119#define I9XX_M2_MAX 9
Shaohua Li21778322009-02-23 15:19:16 +0800120/* IGD M1 is reserved, and must be 0 */
121#define IGD_M1_MIN 0
122#define IGD_M1_MAX 0
123#define IGD_M2_MIN 0
124#define IGD_M2_MAX 254
Jesse Barnes79e53942008-11-07 14:24:08 -0800125#define I9XX_P_SDVO_DAC_MIN 5
126#define I9XX_P_SDVO_DAC_MAX 80
127#define I9XX_P_LVDS_MIN 7
128#define I9XX_P_LVDS_MAX 98
Shaohua Li21778322009-02-23 15:19:16 +0800129#define IGD_P_LVDS_MIN 7
130#define IGD_P_LVDS_MAX 112
Jesse Barnes79e53942008-11-07 14:24:08 -0800131#define I9XX_P1_MIN 1
132#define I9XX_P1_MAX 8
133#define I9XX_P2_SDVO_DAC_SLOW 10
134#define I9XX_P2_SDVO_DAC_FAST 5
135#define I9XX_P2_SDVO_DAC_SLOW_LIMIT 200000
136#define I9XX_P2_LVDS_SLOW 14
137#define I9XX_P2_LVDS_FAST 7
138#define I9XX_P2_LVDS_SLOW_LIMIT 112000
139
Ma Ling044c7c42009-03-18 20:13:23 +0800140/*The parameter is for SDVO on G4x platform*/
141#define G4X_DOT_SDVO_MIN 25000
142#define G4X_DOT_SDVO_MAX 270000
143#define G4X_VCO_MIN 1750000
144#define G4X_VCO_MAX 3500000
145#define G4X_N_SDVO_MIN 1
146#define G4X_N_SDVO_MAX 4
147#define G4X_M_SDVO_MIN 104
148#define G4X_M_SDVO_MAX 138
149#define G4X_M1_SDVO_MIN 17
150#define G4X_M1_SDVO_MAX 23
151#define G4X_M2_SDVO_MIN 5
152#define G4X_M2_SDVO_MAX 11
153#define G4X_P_SDVO_MIN 10
154#define G4X_P_SDVO_MAX 30
155#define G4X_P1_SDVO_MIN 1
156#define G4X_P1_SDVO_MAX 3
157#define G4X_P2_SDVO_SLOW 10
158#define G4X_P2_SDVO_FAST 10
159#define G4X_P2_SDVO_LIMIT 270000
160
161/*The parameter is for HDMI_DAC on G4x platform*/
162#define G4X_DOT_HDMI_DAC_MIN 22000
163#define G4X_DOT_HDMI_DAC_MAX 400000
164#define G4X_N_HDMI_DAC_MIN 1
165#define G4X_N_HDMI_DAC_MAX 4
166#define G4X_M_HDMI_DAC_MIN 104
167#define G4X_M_HDMI_DAC_MAX 138
168#define G4X_M1_HDMI_DAC_MIN 16
169#define G4X_M1_HDMI_DAC_MAX 23
170#define G4X_M2_HDMI_DAC_MIN 5
171#define G4X_M2_HDMI_DAC_MAX 11
172#define G4X_P_HDMI_DAC_MIN 5
173#define G4X_P_HDMI_DAC_MAX 80
174#define G4X_P1_HDMI_DAC_MIN 1
175#define G4X_P1_HDMI_DAC_MAX 8
176#define G4X_P2_HDMI_DAC_SLOW 10
177#define G4X_P2_HDMI_DAC_FAST 5
178#define G4X_P2_HDMI_DAC_LIMIT 165000
179
180/*The parameter is for SINGLE_CHANNEL_LVDS on G4x platform*/
181#define G4X_DOT_SINGLE_CHANNEL_LVDS_MIN 20000
182#define G4X_DOT_SINGLE_CHANNEL_LVDS_MAX 115000
183#define G4X_N_SINGLE_CHANNEL_LVDS_MIN 1
184#define G4X_N_SINGLE_CHANNEL_LVDS_MAX 3
185#define G4X_M_SINGLE_CHANNEL_LVDS_MIN 104
186#define G4X_M_SINGLE_CHANNEL_LVDS_MAX 138
187#define G4X_M1_SINGLE_CHANNEL_LVDS_MIN 17
188#define G4X_M1_SINGLE_CHANNEL_LVDS_MAX 23
189#define G4X_M2_SINGLE_CHANNEL_LVDS_MIN 5
190#define G4X_M2_SINGLE_CHANNEL_LVDS_MAX 11
191#define G4X_P_SINGLE_CHANNEL_LVDS_MIN 28
192#define G4X_P_SINGLE_CHANNEL_LVDS_MAX 112
193#define G4X_P1_SINGLE_CHANNEL_LVDS_MIN 2
194#define G4X_P1_SINGLE_CHANNEL_LVDS_MAX 8
195#define G4X_P2_SINGLE_CHANNEL_LVDS_SLOW 14
196#define G4X_P2_SINGLE_CHANNEL_LVDS_FAST 14
197#define G4X_P2_SINGLE_CHANNEL_LVDS_LIMIT 0
198
199/*The parameter is for DUAL_CHANNEL_LVDS on G4x platform*/
200#define G4X_DOT_DUAL_CHANNEL_LVDS_MIN 80000
201#define G4X_DOT_DUAL_CHANNEL_LVDS_MAX 224000
202#define G4X_N_DUAL_CHANNEL_LVDS_MIN 1
203#define G4X_N_DUAL_CHANNEL_LVDS_MAX 3
204#define G4X_M_DUAL_CHANNEL_LVDS_MIN 104
205#define G4X_M_DUAL_CHANNEL_LVDS_MAX 138
206#define G4X_M1_DUAL_CHANNEL_LVDS_MIN 17
207#define G4X_M1_DUAL_CHANNEL_LVDS_MAX 23
208#define G4X_M2_DUAL_CHANNEL_LVDS_MIN 5
209#define G4X_M2_DUAL_CHANNEL_LVDS_MAX 11
210#define G4X_P_DUAL_CHANNEL_LVDS_MIN 14
211#define G4X_P_DUAL_CHANNEL_LVDS_MAX 42
212#define G4X_P1_DUAL_CHANNEL_LVDS_MIN 2
213#define G4X_P1_DUAL_CHANNEL_LVDS_MAX 6
214#define G4X_P2_DUAL_CHANNEL_LVDS_SLOW 7
215#define G4X_P2_DUAL_CHANNEL_LVDS_FAST 7
216#define G4X_P2_DUAL_CHANNEL_LVDS_LIMIT 0
217
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700218/*The parameter is for DISPLAY PORT on G4x platform*/
219#define G4X_DOT_DISPLAY_PORT_MIN 161670
220#define G4X_DOT_DISPLAY_PORT_MAX 227000
221#define G4X_N_DISPLAY_PORT_MIN 1
222#define G4X_N_DISPLAY_PORT_MAX 2
223#define G4X_M_DISPLAY_PORT_MIN 97
224#define G4X_M_DISPLAY_PORT_MAX 108
225#define G4X_M1_DISPLAY_PORT_MIN 0x10
226#define G4X_M1_DISPLAY_PORT_MAX 0x12
227#define G4X_M2_DISPLAY_PORT_MIN 0x05
228#define G4X_M2_DISPLAY_PORT_MAX 0x06
229#define G4X_P_DISPLAY_PORT_MIN 10
230#define G4X_P_DISPLAY_PORT_MAX 20
231#define G4X_P1_DISPLAY_PORT_MIN 1
232#define G4X_P1_DISPLAY_PORT_MAX 2
233#define G4X_P2_DISPLAY_PORT_SLOW 10
234#define G4X_P2_DISPLAY_PORT_FAST 10
235#define G4X_P2_DISPLAY_PORT_LIMIT 0
236
Zhenyu Wang2c072452009-06-05 15:38:42 +0800237/* IGDNG */
238/* as we calculate clock using (register_value + 2) for
239 N/M1/M2, so here the range value for them is (actual_value-2).
240 */
241#define IGDNG_DOT_MIN 25000
242#define IGDNG_DOT_MAX 350000
243#define IGDNG_VCO_MIN 1760000
244#define IGDNG_VCO_MAX 3510000
245#define IGDNG_N_MIN 1
246#define IGDNG_N_MAX 5
247#define IGDNG_M_MIN 79
248#define IGDNG_M_MAX 118
249#define IGDNG_M1_MIN 12
250#define IGDNG_M1_MAX 23
251#define IGDNG_M2_MIN 5
252#define IGDNG_M2_MAX 9
253#define IGDNG_P_SDVO_DAC_MIN 5
254#define IGDNG_P_SDVO_DAC_MAX 80
255#define IGDNG_P_LVDS_MIN 28
256#define IGDNG_P_LVDS_MAX 112
257#define IGDNG_P1_MIN 1
258#define IGDNG_P1_MAX 8
259#define IGDNG_P2_SDVO_DAC_SLOW 10
260#define IGDNG_P2_SDVO_DAC_FAST 5
261#define IGDNG_P2_LVDS_SLOW 14 /* single channel */
262#define IGDNG_P2_LVDS_FAST 7 /* double channel */
263#define IGDNG_P2_DOT_LIMIT 225000 /* 225Mhz */
264
Ma Lingd4906092009-03-18 20:13:27 +0800265static bool
266intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
267 int target, int refclk, intel_clock_t *best_clock);
268static bool
Jesse Barnes652c3932009-08-17 13:31:43 -0700269intel_find_best_reduced_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
270 int target, int refclk, intel_clock_t *best_clock);
271static bool
Ma Lingd4906092009-03-18 20:13:27 +0800272intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
273 int target, int refclk, intel_clock_t *best_clock);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800274static bool
275intel_igdng_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
276 int target, int refclk, intel_clock_t *best_clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800277
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700278static bool
279intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
280 int target, int refclk, intel_clock_t *best_clock);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800281static bool
282intel_find_pll_igdng_dp(const intel_limit_t *, struct drm_crtc *crtc,
283 int target, int refclk, intel_clock_t *best_clock);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700284
Keith Packarde4b36692009-06-05 19:22:17 -0700285static const intel_limit_t intel_limits_i8xx_dvo = {
Jesse Barnes79e53942008-11-07 14:24:08 -0800286 .dot = { .min = I8XX_DOT_MIN, .max = I8XX_DOT_MAX },
287 .vco = { .min = I8XX_VCO_MIN, .max = I8XX_VCO_MAX },
288 .n = { .min = I8XX_N_MIN, .max = I8XX_N_MAX },
289 .m = { .min = I8XX_M_MIN, .max = I8XX_M_MAX },
290 .m1 = { .min = I8XX_M1_MIN, .max = I8XX_M1_MAX },
291 .m2 = { .min = I8XX_M2_MIN, .max = I8XX_M2_MAX },
292 .p = { .min = I8XX_P_MIN, .max = I8XX_P_MAX },
293 .p1 = { .min = I8XX_P1_MIN, .max = I8XX_P1_MAX },
294 .p2 = { .dot_limit = I8XX_P2_SLOW_LIMIT,
295 .p2_slow = I8XX_P2_SLOW, .p2_fast = I8XX_P2_FAST },
Ma Lingd4906092009-03-18 20:13:27 +0800296 .find_pll = intel_find_best_PLL,
Jesse Barnes652c3932009-08-17 13:31:43 -0700297 .find_reduced_pll = intel_find_best_reduced_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700298};
299
300static const intel_limit_t intel_limits_i8xx_lvds = {
Jesse Barnes79e53942008-11-07 14:24:08 -0800301 .dot = { .min = I8XX_DOT_MIN, .max = I8XX_DOT_MAX },
302 .vco = { .min = I8XX_VCO_MIN, .max = I8XX_VCO_MAX },
303 .n = { .min = I8XX_N_MIN, .max = I8XX_N_MAX },
304 .m = { .min = I8XX_M_MIN, .max = I8XX_M_MAX },
305 .m1 = { .min = I8XX_M1_MIN, .max = I8XX_M1_MAX },
306 .m2 = { .min = I8XX_M2_MIN, .max = I8XX_M2_MAX },
307 .p = { .min = I8XX_P_MIN, .max = I8XX_P_MAX },
308 .p1 = { .min = I8XX_P1_LVDS_MIN, .max = I8XX_P1_LVDS_MAX },
309 .p2 = { .dot_limit = I8XX_P2_SLOW_LIMIT,
310 .p2_slow = I8XX_P2_LVDS_SLOW, .p2_fast = I8XX_P2_LVDS_FAST },
Ma Lingd4906092009-03-18 20:13:27 +0800311 .find_pll = intel_find_best_PLL,
Jesse Barnes652c3932009-08-17 13:31:43 -0700312 .find_reduced_pll = intel_find_best_reduced_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700313};
314
315static const intel_limit_t intel_limits_i9xx_sdvo = {
Jesse Barnes79e53942008-11-07 14:24:08 -0800316 .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
317 .vco = { .min = I9XX_VCO_MIN, .max = I9XX_VCO_MAX },
318 .n = { .min = I9XX_N_MIN, .max = I9XX_N_MAX },
319 .m = { .min = I9XX_M_MIN, .max = I9XX_M_MAX },
320 .m1 = { .min = I9XX_M1_MIN, .max = I9XX_M1_MAX },
321 .m2 = { .min = I9XX_M2_MIN, .max = I9XX_M2_MAX },
322 .p = { .min = I9XX_P_SDVO_DAC_MIN, .max = I9XX_P_SDVO_DAC_MAX },
323 .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
324 .p2 = { .dot_limit = I9XX_P2_SDVO_DAC_SLOW_LIMIT,
325 .p2_slow = I9XX_P2_SDVO_DAC_SLOW, .p2_fast = I9XX_P2_SDVO_DAC_FAST },
Ma Lingd4906092009-03-18 20:13:27 +0800326 .find_pll = intel_find_best_PLL,
Jesse Barnes652c3932009-08-17 13:31:43 -0700327 .find_reduced_pll = intel_find_best_reduced_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700328};
329
330static const intel_limit_t intel_limits_i9xx_lvds = {
Jesse Barnes79e53942008-11-07 14:24:08 -0800331 .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
332 .vco = { .min = I9XX_VCO_MIN, .max = I9XX_VCO_MAX },
333 .n = { .min = I9XX_N_MIN, .max = I9XX_N_MAX },
334 .m = { .min = I9XX_M_MIN, .max = I9XX_M_MAX },
335 .m1 = { .min = I9XX_M1_MIN, .max = I9XX_M1_MAX },
336 .m2 = { .min = I9XX_M2_MIN, .max = I9XX_M2_MAX },
337 .p = { .min = I9XX_P_LVDS_MIN, .max = I9XX_P_LVDS_MAX },
338 .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
339 /* The single-channel range is 25-112Mhz, and dual-channel
340 * is 80-224Mhz. Prefer single channel as much as possible.
341 */
342 .p2 = { .dot_limit = I9XX_P2_LVDS_SLOW_LIMIT,
343 .p2_slow = I9XX_P2_LVDS_SLOW, .p2_fast = I9XX_P2_LVDS_FAST },
Ma Lingd4906092009-03-18 20:13:27 +0800344 .find_pll = intel_find_best_PLL,
Jesse Barnes652c3932009-08-17 13:31:43 -0700345 .find_reduced_pll = intel_find_best_reduced_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700346};
347
Ma Ling044c7c42009-03-18 20:13:23 +0800348 /* below parameter and function is for G4X Chipset Family*/
Keith Packarde4b36692009-06-05 19:22:17 -0700349static const intel_limit_t intel_limits_g4x_sdvo = {
Ma Ling044c7c42009-03-18 20:13:23 +0800350 .dot = { .min = G4X_DOT_SDVO_MIN, .max = G4X_DOT_SDVO_MAX },
351 .vco = { .min = G4X_VCO_MIN, .max = G4X_VCO_MAX},
352 .n = { .min = G4X_N_SDVO_MIN, .max = G4X_N_SDVO_MAX },
353 .m = { .min = G4X_M_SDVO_MIN, .max = G4X_M_SDVO_MAX },
354 .m1 = { .min = G4X_M1_SDVO_MIN, .max = G4X_M1_SDVO_MAX },
355 .m2 = { .min = G4X_M2_SDVO_MIN, .max = G4X_M2_SDVO_MAX },
356 .p = { .min = G4X_P_SDVO_MIN, .max = G4X_P_SDVO_MAX },
357 .p1 = { .min = G4X_P1_SDVO_MIN, .max = G4X_P1_SDVO_MAX},
358 .p2 = { .dot_limit = G4X_P2_SDVO_LIMIT,
359 .p2_slow = G4X_P2_SDVO_SLOW,
360 .p2_fast = G4X_P2_SDVO_FAST
361 },
Ma Lingd4906092009-03-18 20:13:27 +0800362 .find_pll = intel_g4x_find_best_PLL,
Jesse Barnes652c3932009-08-17 13:31:43 -0700363 .find_reduced_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700364};
365
366static const intel_limit_t intel_limits_g4x_hdmi = {
Ma Ling044c7c42009-03-18 20:13:23 +0800367 .dot = { .min = G4X_DOT_HDMI_DAC_MIN, .max = G4X_DOT_HDMI_DAC_MAX },
368 .vco = { .min = G4X_VCO_MIN, .max = G4X_VCO_MAX},
369 .n = { .min = G4X_N_HDMI_DAC_MIN, .max = G4X_N_HDMI_DAC_MAX },
370 .m = { .min = G4X_M_HDMI_DAC_MIN, .max = G4X_M_HDMI_DAC_MAX },
371 .m1 = { .min = G4X_M1_HDMI_DAC_MIN, .max = G4X_M1_HDMI_DAC_MAX },
372 .m2 = { .min = G4X_M2_HDMI_DAC_MIN, .max = G4X_M2_HDMI_DAC_MAX },
373 .p = { .min = G4X_P_HDMI_DAC_MIN, .max = G4X_P_HDMI_DAC_MAX },
374 .p1 = { .min = G4X_P1_HDMI_DAC_MIN, .max = G4X_P1_HDMI_DAC_MAX},
375 .p2 = { .dot_limit = G4X_P2_HDMI_DAC_LIMIT,
376 .p2_slow = G4X_P2_HDMI_DAC_SLOW,
377 .p2_fast = G4X_P2_HDMI_DAC_FAST
378 },
Ma Lingd4906092009-03-18 20:13:27 +0800379 .find_pll = intel_g4x_find_best_PLL,
Jesse Barnes652c3932009-08-17 13:31:43 -0700380 .find_reduced_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700381};
382
383static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Ma Ling044c7c42009-03-18 20:13:23 +0800384 .dot = { .min = G4X_DOT_SINGLE_CHANNEL_LVDS_MIN,
385 .max = G4X_DOT_SINGLE_CHANNEL_LVDS_MAX },
386 .vco = { .min = G4X_VCO_MIN,
387 .max = G4X_VCO_MAX },
388 .n = { .min = G4X_N_SINGLE_CHANNEL_LVDS_MIN,
389 .max = G4X_N_SINGLE_CHANNEL_LVDS_MAX },
390 .m = { .min = G4X_M_SINGLE_CHANNEL_LVDS_MIN,
391 .max = G4X_M_SINGLE_CHANNEL_LVDS_MAX },
392 .m1 = { .min = G4X_M1_SINGLE_CHANNEL_LVDS_MIN,
393 .max = G4X_M1_SINGLE_CHANNEL_LVDS_MAX },
394 .m2 = { .min = G4X_M2_SINGLE_CHANNEL_LVDS_MIN,
395 .max = G4X_M2_SINGLE_CHANNEL_LVDS_MAX },
396 .p = { .min = G4X_P_SINGLE_CHANNEL_LVDS_MIN,
397 .max = G4X_P_SINGLE_CHANNEL_LVDS_MAX },
398 .p1 = { .min = G4X_P1_SINGLE_CHANNEL_LVDS_MIN,
399 .max = G4X_P1_SINGLE_CHANNEL_LVDS_MAX },
400 .p2 = { .dot_limit = G4X_P2_SINGLE_CHANNEL_LVDS_LIMIT,
401 .p2_slow = G4X_P2_SINGLE_CHANNEL_LVDS_SLOW,
402 .p2_fast = G4X_P2_SINGLE_CHANNEL_LVDS_FAST
403 },
Ma Lingd4906092009-03-18 20:13:27 +0800404 .find_pll = intel_g4x_find_best_PLL,
Jesse Barnes652c3932009-08-17 13:31:43 -0700405 .find_reduced_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700406};
407
408static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Ma Ling044c7c42009-03-18 20:13:23 +0800409 .dot = { .min = G4X_DOT_DUAL_CHANNEL_LVDS_MIN,
410 .max = G4X_DOT_DUAL_CHANNEL_LVDS_MAX },
411 .vco = { .min = G4X_VCO_MIN,
412 .max = G4X_VCO_MAX },
413 .n = { .min = G4X_N_DUAL_CHANNEL_LVDS_MIN,
414 .max = G4X_N_DUAL_CHANNEL_LVDS_MAX },
415 .m = { .min = G4X_M_DUAL_CHANNEL_LVDS_MIN,
416 .max = G4X_M_DUAL_CHANNEL_LVDS_MAX },
417 .m1 = { .min = G4X_M1_DUAL_CHANNEL_LVDS_MIN,
418 .max = G4X_M1_DUAL_CHANNEL_LVDS_MAX },
419 .m2 = { .min = G4X_M2_DUAL_CHANNEL_LVDS_MIN,
420 .max = G4X_M2_DUAL_CHANNEL_LVDS_MAX },
421 .p = { .min = G4X_P_DUAL_CHANNEL_LVDS_MIN,
422 .max = G4X_P_DUAL_CHANNEL_LVDS_MAX },
423 .p1 = { .min = G4X_P1_DUAL_CHANNEL_LVDS_MIN,
424 .max = G4X_P1_DUAL_CHANNEL_LVDS_MAX },
425 .p2 = { .dot_limit = G4X_P2_DUAL_CHANNEL_LVDS_LIMIT,
426 .p2_slow = G4X_P2_DUAL_CHANNEL_LVDS_SLOW,
427 .p2_fast = G4X_P2_DUAL_CHANNEL_LVDS_FAST
428 },
Ma Lingd4906092009-03-18 20:13:27 +0800429 .find_pll = intel_g4x_find_best_PLL,
Jesse Barnes652c3932009-08-17 13:31:43 -0700430 .find_reduced_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700431};
432
433static const intel_limit_t intel_limits_g4x_display_port = {
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700434 .dot = { .min = G4X_DOT_DISPLAY_PORT_MIN,
435 .max = G4X_DOT_DISPLAY_PORT_MAX },
436 .vco = { .min = G4X_VCO_MIN,
437 .max = G4X_VCO_MAX},
438 .n = { .min = G4X_N_DISPLAY_PORT_MIN,
439 .max = G4X_N_DISPLAY_PORT_MAX },
440 .m = { .min = G4X_M_DISPLAY_PORT_MIN,
441 .max = G4X_M_DISPLAY_PORT_MAX },
442 .m1 = { .min = G4X_M1_DISPLAY_PORT_MIN,
443 .max = G4X_M1_DISPLAY_PORT_MAX },
444 .m2 = { .min = G4X_M2_DISPLAY_PORT_MIN,
445 .max = G4X_M2_DISPLAY_PORT_MAX },
446 .p = { .min = G4X_P_DISPLAY_PORT_MIN,
447 .max = G4X_P_DISPLAY_PORT_MAX },
448 .p1 = { .min = G4X_P1_DISPLAY_PORT_MIN,
449 .max = G4X_P1_DISPLAY_PORT_MAX},
450 .p2 = { .dot_limit = G4X_P2_DISPLAY_PORT_LIMIT,
451 .p2_slow = G4X_P2_DISPLAY_PORT_SLOW,
452 .p2_fast = G4X_P2_DISPLAY_PORT_FAST },
453 .find_pll = intel_find_pll_g4x_dp,
Keith Packarde4b36692009-06-05 19:22:17 -0700454};
455
456static const intel_limit_t intel_limits_igd_sdvo = {
Shaohua Li21778322009-02-23 15:19:16 +0800457 .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX},
458 .vco = { .min = IGD_VCO_MIN, .max = IGD_VCO_MAX },
459 .n = { .min = IGD_N_MIN, .max = IGD_N_MAX },
460 .m = { .min = IGD_M_MIN, .max = IGD_M_MAX },
461 .m1 = { .min = IGD_M1_MIN, .max = IGD_M1_MAX },
462 .m2 = { .min = IGD_M2_MIN, .max = IGD_M2_MAX },
463 .p = { .min = I9XX_P_SDVO_DAC_MIN, .max = I9XX_P_SDVO_DAC_MAX },
464 .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
465 .p2 = { .dot_limit = I9XX_P2_SDVO_DAC_SLOW_LIMIT,
466 .p2_slow = I9XX_P2_SDVO_DAC_SLOW, .p2_fast = I9XX_P2_SDVO_DAC_FAST },
Shaohua Li61157072009-04-03 15:24:43 +0800467 .find_pll = intel_find_best_PLL,
Jesse Barnes652c3932009-08-17 13:31:43 -0700468 .find_reduced_pll = intel_find_best_reduced_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700469};
470
471static const intel_limit_t intel_limits_igd_lvds = {
Shaohua Li21778322009-02-23 15:19:16 +0800472 .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
473 .vco = { .min = IGD_VCO_MIN, .max = IGD_VCO_MAX },
474 .n = { .min = IGD_N_MIN, .max = IGD_N_MAX },
475 .m = { .min = IGD_M_MIN, .max = IGD_M_MAX },
476 .m1 = { .min = IGD_M1_MIN, .max = IGD_M1_MAX },
477 .m2 = { .min = IGD_M2_MIN, .max = IGD_M2_MAX },
478 .p = { .min = IGD_P_LVDS_MIN, .max = IGD_P_LVDS_MAX },
479 .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
480 /* IGD only supports single-channel mode. */
481 .p2 = { .dot_limit = I9XX_P2_LVDS_SLOW_LIMIT,
482 .p2_slow = I9XX_P2_LVDS_SLOW, .p2_fast = I9XX_P2_LVDS_SLOW },
Shaohua Li61157072009-04-03 15:24:43 +0800483 .find_pll = intel_find_best_PLL,
Jesse Barnes652c3932009-08-17 13:31:43 -0700484 .find_reduced_pll = intel_find_best_reduced_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700485};
486
487static const intel_limit_t intel_limits_igdng_sdvo = {
Zhenyu Wang2c072452009-06-05 15:38:42 +0800488 .dot = { .min = IGDNG_DOT_MIN, .max = IGDNG_DOT_MAX },
489 .vco = { .min = IGDNG_VCO_MIN, .max = IGDNG_VCO_MAX },
490 .n = { .min = IGDNG_N_MIN, .max = IGDNG_N_MAX },
491 .m = { .min = IGDNG_M_MIN, .max = IGDNG_M_MAX },
492 .m1 = { .min = IGDNG_M1_MIN, .max = IGDNG_M1_MAX },
493 .m2 = { .min = IGDNG_M2_MIN, .max = IGDNG_M2_MAX },
494 .p = { .min = IGDNG_P_SDVO_DAC_MIN, .max = IGDNG_P_SDVO_DAC_MAX },
495 .p1 = { .min = IGDNG_P1_MIN, .max = IGDNG_P1_MAX },
496 .p2 = { .dot_limit = IGDNG_P2_DOT_LIMIT,
497 .p2_slow = IGDNG_P2_SDVO_DAC_SLOW,
498 .p2_fast = IGDNG_P2_SDVO_DAC_FAST },
499 .find_pll = intel_igdng_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700500};
501
502static const intel_limit_t intel_limits_igdng_lvds = {
Zhenyu Wang2c072452009-06-05 15:38:42 +0800503 .dot = { .min = IGDNG_DOT_MIN, .max = IGDNG_DOT_MAX },
504 .vco = { .min = IGDNG_VCO_MIN, .max = IGDNG_VCO_MAX },
505 .n = { .min = IGDNG_N_MIN, .max = IGDNG_N_MAX },
506 .m = { .min = IGDNG_M_MIN, .max = IGDNG_M_MAX },
507 .m1 = { .min = IGDNG_M1_MIN, .max = IGDNG_M1_MAX },
508 .m2 = { .min = IGDNG_M2_MIN, .max = IGDNG_M2_MAX },
509 .p = { .min = IGDNG_P_LVDS_MIN, .max = IGDNG_P_LVDS_MAX },
510 .p1 = { .min = IGDNG_P1_MIN, .max = IGDNG_P1_MAX },
511 .p2 = { .dot_limit = IGDNG_P2_DOT_LIMIT,
512 .p2_slow = IGDNG_P2_LVDS_SLOW,
513 .p2_fast = IGDNG_P2_LVDS_FAST },
514 .find_pll = intel_igdng_find_best_PLL,
Jesse Barnes79e53942008-11-07 14:24:08 -0800515};
516
Zhenyu Wang2c072452009-06-05 15:38:42 +0800517static const intel_limit_t *intel_igdng_limit(struct drm_crtc *crtc)
518{
519 const intel_limit_t *limit;
520 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700521 limit = &intel_limits_igdng_lvds;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800522 else
Keith Packarde4b36692009-06-05 19:22:17 -0700523 limit = &intel_limits_igdng_sdvo;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800524
525 return limit;
526}
527
Ma Ling044c7c42009-03-18 20:13:23 +0800528static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
529{
530 struct drm_device *dev = crtc->dev;
531 struct drm_i915_private *dev_priv = dev->dev_private;
532 const intel_limit_t *limit;
533
534 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
535 if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
536 LVDS_CLKB_POWER_UP)
537 /* LVDS with dual channel */
Keith Packarde4b36692009-06-05 19:22:17 -0700538 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800539 else
540 /* LVDS with dual channel */
Keith Packarde4b36692009-06-05 19:22:17 -0700541 limit = &intel_limits_g4x_single_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800542 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
543 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700544 limit = &intel_limits_g4x_hdmi;
Ma Ling044c7c42009-03-18 20:13:23 +0800545 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700546 limit = &intel_limits_g4x_sdvo;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700547 } else if (intel_pipe_has_type (crtc, INTEL_OUTPUT_DISPLAYPORT)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700548 limit = &intel_limits_g4x_display_port;
Ma Ling044c7c42009-03-18 20:13:23 +0800549 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700550 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800551
552 return limit;
553}
554
Jesse Barnes79e53942008-11-07 14:24:08 -0800555static const intel_limit_t *intel_limit(struct drm_crtc *crtc)
556{
557 struct drm_device *dev = crtc->dev;
558 const intel_limit_t *limit;
559
Zhenyu Wang2c072452009-06-05 15:38:42 +0800560 if (IS_IGDNG(dev))
561 limit = intel_igdng_limit(crtc);
562 else if (IS_G4X(dev)) {
Ma Ling044c7c42009-03-18 20:13:23 +0800563 limit = intel_g4x_limit(crtc);
Shaohua Li21778322009-02-23 15:19:16 +0800564 } else if (IS_I9XX(dev) && !IS_IGD(dev)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800565 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700566 limit = &intel_limits_i9xx_lvds;
Jesse Barnes79e53942008-11-07 14:24:08 -0800567 else
Keith Packarde4b36692009-06-05 19:22:17 -0700568 limit = &intel_limits_i9xx_sdvo;
Shaohua Li21778322009-02-23 15:19:16 +0800569 } else if (IS_IGD(dev)) {
570 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700571 limit = &intel_limits_igd_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800572 else
Keith Packarde4b36692009-06-05 19:22:17 -0700573 limit = &intel_limits_igd_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800574 } else {
575 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700576 limit = &intel_limits_i8xx_lvds;
Jesse Barnes79e53942008-11-07 14:24:08 -0800577 else
Keith Packarde4b36692009-06-05 19:22:17 -0700578 limit = &intel_limits_i8xx_dvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800579 }
580 return limit;
581}
582
Shaohua Li21778322009-02-23 15:19:16 +0800583/* m1 is reserved as 0 in IGD, n is a ring counter */
584static void igd_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800585{
Shaohua Li21778322009-02-23 15:19:16 +0800586 clock->m = clock->m2 + 2;
587 clock->p = clock->p1 * clock->p2;
588 clock->vco = refclk * clock->m / clock->n;
589 clock->dot = clock->vco / clock->p;
590}
591
592static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
593{
594 if (IS_IGD(dev)) {
595 igd_clock(refclk, clock);
596 return;
597 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800598 clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
599 clock->p = clock->p1 * clock->p2;
600 clock->vco = refclk * clock->m / (clock->n + 2);
601 clock->dot = clock->vco / clock->p;
602}
603
Jesse Barnes79e53942008-11-07 14:24:08 -0800604/**
605 * Returns whether any output on the specified pipe is of the specified type
606 */
607bool intel_pipe_has_type (struct drm_crtc *crtc, int type)
608{
609 struct drm_device *dev = crtc->dev;
610 struct drm_mode_config *mode_config = &dev->mode_config;
611 struct drm_connector *l_entry;
612
613 list_for_each_entry(l_entry, &mode_config->connector_list, head) {
614 if (l_entry->encoder &&
615 l_entry->encoder->crtc == crtc) {
616 struct intel_output *intel_output = to_intel_output(l_entry);
617 if (intel_output->type == type)
618 return true;
619 }
620 }
621 return false;
622}
623
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800624struct drm_connector *
625intel_pipe_get_output (struct drm_crtc *crtc)
626{
627 struct drm_device *dev = crtc->dev;
628 struct drm_mode_config *mode_config = &dev->mode_config;
629 struct drm_connector *l_entry, *ret = NULL;
630
631 list_for_each_entry(l_entry, &mode_config->connector_list, head) {
632 if (l_entry->encoder &&
633 l_entry->encoder->crtc == crtc) {
634 ret = l_entry;
635 break;
636 }
637 }
638 return ret;
639}
640
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800641#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800642/**
643 * Returns whether the given set of divisors are valid for a given refclk with
644 * the given connectors.
645 */
646
647static bool intel_PLL_is_valid(struct drm_crtc *crtc, intel_clock_t *clock)
648{
649 const intel_limit_t *limit = intel_limit (crtc);
Shaohua Li21778322009-02-23 15:19:16 +0800650 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800651
652 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
653 INTELPllInvalid ("p1 out of range\n");
654 if (clock->p < limit->p.min || limit->p.max < clock->p)
655 INTELPllInvalid ("p out of range\n");
656 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
657 INTELPllInvalid ("m2 out of range\n");
658 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
659 INTELPllInvalid ("m1 out of range\n");
Shaohua Li21778322009-02-23 15:19:16 +0800660 if (clock->m1 <= clock->m2 && !IS_IGD(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800661 INTELPllInvalid ("m1 <= m2\n");
662 if (clock->m < limit->m.min || limit->m.max < clock->m)
663 INTELPllInvalid ("m out of range\n");
664 if (clock->n < limit->n.min || limit->n.max < clock->n)
665 INTELPllInvalid ("n out of range\n");
666 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
667 INTELPllInvalid ("vco out of range\n");
668 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
669 * connector, etc., rather than just a single range.
670 */
671 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
672 INTELPllInvalid ("dot out of range\n");
673
674 return true;
675}
676
Ma Lingd4906092009-03-18 20:13:27 +0800677static bool
678intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
679 int target, int refclk, intel_clock_t *best_clock)
680
Jesse Barnes79e53942008-11-07 14:24:08 -0800681{
682 struct drm_device *dev = crtc->dev;
683 struct drm_i915_private *dev_priv = dev->dev_private;
684 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800685 int err = target;
686
Bruno Prémontbc5e5712009-08-08 13:01:17 +0200687 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Florian Mickler832cc282009-07-13 18:40:32 +0800688 (I915_READ(LVDS)) != 0) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800689 /*
690 * For LVDS, if the panel is on, just rely on its current
691 * settings for dual-channel. We haven't figured out how to
692 * reliably set up different single/dual channel state, if we
693 * even can.
694 */
695 if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
696 LVDS_CLKB_POWER_UP)
697 clock.p2 = limit->p2.p2_fast;
698 else
699 clock.p2 = limit->p2.p2_slow;
700 } else {
701 if (target < limit->p2.dot_limit)
702 clock.p2 = limit->p2.p2_slow;
703 else
704 clock.p2 = limit->p2.p2_fast;
705 }
706
707 memset (best_clock, 0, sizeof (*best_clock));
708
Jesse Barnes652c3932009-08-17 13:31:43 -0700709 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
710 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
711 clock.m1++) {
712 for (clock.m2 = limit->m2.min;
713 clock.m2 <= limit->m2.max; clock.m2++) {
714 /* m1 is always 0 in IGD */
715 if (clock.m2 >= clock.m1 && !IS_IGD(dev))
716 break;
717 for (clock.n = limit->n.min;
718 clock.n <= limit->n.max; clock.n++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800719 int this_err;
720
Shaohua Li21778322009-02-23 15:19:16 +0800721 intel_clock(dev, refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800722
723 if (!intel_PLL_is_valid(crtc, &clock))
724 continue;
725
726 this_err = abs(clock.dot - target);
727 if (this_err < err) {
728 *best_clock = clock;
729 err = this_err;
730 }
731 }
732 }
733 }
734 }
735
736 return (err != target);
737}
738
Jesse Barnes652c3932009-08-17 13:31:43 -0700739
740static bool
741intel_find_best_reduced_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
742 int target, int refclk, intel_clock_t *best_clock)
743
744{
745 struct drm_device *dev = crtc->dev;
746 intel_clock_t clock;
747 int err = target;
748 bool found = false;
749
750 memcpy(&clock, best_clock, sizeof(intel_clock_t));
751
752 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
753 for (clock.m2 = limit->m2.min; clock.m2 <= limit->m2.max; clock.m2++) {
754 /* m1 is always 0 in IGD */
755 if (clock.m2 >= clock.m1 && !IS_IGD(dev))
756 break;
757 for (clock.n = limit->n.min; clock.n <= limit->n.max;
758 clock.n++) {
759 int this_err;
760
761 intel_clock(dev, refclk, &clock);
762
763 if (!intel_PLL_is_valid(crtc, &clock))
764 continue;
765
766 this_err = abs(clock.dot - target);
767 if (this_err < err) {
768 *best_clock = clock;
769 err = this_err;
770 found = true;
771 }
772 }
773 }
774 }
775
776 return found;
777}
778
Ma Lingd4906092009-03-18 20:13:27 +0800779static bool
780intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
781 int target, int refclk, intel_clock_t *best_clock)
782{
783 struct drm_device *dev = crtc->dev;
784 struct drm_i915_private *dev_priv = dev->dev_private;
785 intel_clock_t clock;
786 int max_n;
787 bool found;
788 /* approximately equals target * 0.00488 */
789 int err_most = (target >> 8) + (target >> 10);
790 found = false;
791
792 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
793 if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
794 LVDS_CLKB_POWER_UP)
795 clock.p2 = limit->p2.p2_fast;
796 else
797 clock.p2 = limit->p2.p2_slow;
798 } else {
799 if (target < limit->p2.dot_limit)
800 clock.p2 = limit->p2.p2_slow;
801 else
802 clock.p2 = limit->p2.p2_fast;
803 }
804
805 memset(best_clock, 0, sizeof(*best_clock));
806 max_n = limit->n.max;
807 /* based on hardware requriment prefer smaller n to precision */
808 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Jesse Barnes652c3932009-08-17 13:31:43 -0700809 /* based on hardware requirment prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800810 for (clock.m1 = limit->m1.max;
811 clock.m1 >= limit->m1.min; clock.m1--) {
812 for (clock.m2 = limit->m2.max;
813 clock.m2 >= limit->m2.min; clock.m2--) {
814 for (clock.p1 = limit->p1.max;
815 clock.p1 >= limit->p1.min; clock.p1--) {
816 int this_err;
817
Shaohua Li21778322009-02-23 15:19:16 +0800818 intel_clock(dev, refclk, &clock);
Ma Lingd4906092009-03-18 20:13:27 +0800819 if (!intel_PLL_is_valid(crtc, &clock))
820 continue;
821 this_err = abs(clock.dot - target) ;
822 if (this_err < err_most) {
823 *best_clock = clock;
824 err_most = this_err;
825 max_n = clock.n;
826 found = true;
827 }
828 }
829 }
830 }
831 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800832 return found;
833}
Ma Lingd4906092009-03-18 20:13:27 +0800834
Zhenyu Wang2c072452009-06-05 15:38:42 +0800835static bool
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800836intel_find_pll_igdng_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
837 int target, int refclk, intel_clock_t *best_clock)
838{
839 struct drm_device *dev = crtc->dev;
840 intel_clock_t clock;
841 if (target < 200000) {
842 clock.n = 1;
843 clock.p1 = 2;
844 clock.p2 = 10;
845 clock.m1 = 12;
846 clock.m2 = 9;
847 } else {
848 clock.n = 2;
849 clock.p1 = 1;
850 clock.p2 = 10;
851 clock.m1 = 14;
852 clock.m2 = 8;
853 }
854 intel_clock(dev, refclk, &clock);
855 memcpy(best_clock, &clock, sizeof(intel_clock_t));
856 return true;
857}
858
859static bool
Zhenyu Wang2c072452009-06-05 15:38:42 +0800860intel_igdng_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
861 int target, int refclk, intel_clock_t *best_clock)
862{
863 struct drm_device *dev = crtc->dev;
864 struct drm_i915_private *dev_priv = dev->dev_private;
865 intel_clock_t clock;
866 int max_n;
867 bool found;
868 int err_most = 47;
869 found = false;
870
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800871 /* eDP has only 2 clock choice, no n/m/p setting */
872 if (HAS_eDP)
873 return true;
874
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800875 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
876 return intel_find_pll_igdng_dp(limit, crtc, target,
877 refclk, best_clock);
878
Zhenyu Wang2c072452009-06-05 15:38:42 +0800879 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
880 if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
881 LVDS_CLKB_POWER_UP)
882 clock.p2 = limit->p2.p2_fast;
883 else
884 clock.p2 = limit->p2.p2_slow;
885 } else {
886 if (target < limit->p2.dot_limit)
887 clock.p2 = limit->p2.p2_slow;
888 else
889 clock.p2 = limit->p2.p2_fast;
890 }
891
892 memset(best_clock, 0, sizeof(*best_clock));
893 max_n = limit->n.max;
Jesse Barnes652c3932009-08-17 13:31:43 -0700894 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
895 /* based on hardware requriment prefer smaller n to precision */
896 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
897 /* based on hardware requirment prefere larger m1,m2 */
898 for (clock.m1 = limit->m1.max;
899 clock.m1 >= limit->m1.min; clock.m1--) {
900 for (clock.m2 = limit->m2.max;
901 clock.m2 >= limit->m2.min; clock.m2--) {
Zhenyu Wang2c072452009-06-05 15:38:42 +0800902 int this_err;
903
904 intel_clock(dev, refclk, &clock);
905 if (!intel_PLL_is_valid(crtc, &clock))
906 continue;
907 this_err = abs((10000 - (target*10000/clock.dot)));
908 if (this_err < err_most) {
909 *best_clock = clock;
910 err_most = this_err;
911 max_n = clock.n;
912 found = true;
913 /* found on first matching */
914 goto out;
915 }
916 }
917 }
918 }
919 }
920out:
Ma Lingd4906092009-03-18 20:13:27 +0800921 return found;
922}
923
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700924/* DisplayPort has only two frequencies, 162MHz and 270MHz */
925static bool
926intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
927 int target, int refclk, intel_clock_t *best_clock)
928{
929 intel_clock_t clock;
930 if (target < 200000) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700931 clock.p1 = 2;
932 clock.p2 = 10;
Keith Packardb3d25492009-06-24 23:09:15 -0700933 clock.n = 2;
934 clock.m1 = 23;
935 clock.m2 = 8;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700936 } else {
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700937 clock.p1 = 1;
938 clock.p2 = 10;
Keith Packardb3d25492009-06-24 23:09:15 -0700939 clock.n = 1;
940 clock.m1 = 14;
941 clock.m2 = 2;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700942 }
Keith Packardb3d25492009-06-24 23:09:15 -0700943 clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
944 clock.p = (clock.p1 * clock.p2);
945 clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700946 memcpy(best_clock, &clock, sizeof(intel_clock_t));
947 return true;
948}
949
Jesse Barnes79e53942008-11-07 14:24:08 -0800950void
951intel_wait_for_vblank(struct drm_device *dev)
952{
953 /* Wait for 20ms, i.e. one cycle at 50hz. */
Arjan van de Ven580982d2009-03-23 13:36:25 -0700954 mdelay(20);
Jesse Barnes79e53942008-11-07 14:24:08 -0800955}
956
Chris Wilson5c3b82e2009-02-11 13:25:09 +0000957static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500958intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
959 struct drm_framebuffer *old_fb)
Jesse Barnes79e53942008-11-07 14:24:08 -0800960{
961 struct drm_device *dev = crtc->dev;
962 struct drm_i915_private *dev_priv = dev->dev_private;
963 struct drm_i915_master_private *master_priv;
964 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
965 struct intel_framebuffer *intel_fb;
966 struct drm_i915_gem_object *obj_priv;
967 struct drm_gem_object *obj;
968 int pipe = intel_crtc->pipe;
969 unsigned long Start, Offset;
970 int dspbase = (pipe == 0 ? DSPAADDR : DSPBADDR);
971 int dspsurf = (pipe == 0 ? DSPASURF : DSPBSURF);
972 int dspstride = (pipe == 0) ? DSPASTRIDE : DSPBSTRIDE;
Jesse Barnesf5448472009-04-14 14:17:47 -0700973 int dsptileoff = (pipe == 0 ? DSPATILEOFF : DSPBTILEOFF);
Jesse Barnes79e53942008-11-07 14:24:08 -0800974 int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500975 u32 dspcntr, alignment;
Chris Wilson5c3b82e2009-02-11 13:25:09 +0000976 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -0800977
978 /* no fb bound */
979 if (!crtc->fb) {
980 DRM_DEBUG("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +0000981 return 0;
982 }
983
984 switch (pipe) {
985 case 0:
986 case 1:
987 break;
988 default:
989 DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
990 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -0800991 }
992
993 intel_fb = to_intel_framebuffer(crtc->fb);
Jesse Barnes79e53942008-11-07 14:24:08 -0800994 obj = intel_fb->obj;
995 obj_priv = obj->driver_private;
996
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500997 switch (obj_priv->tiling_mode) {
998 case I915_TILING_NONE:
999 alignment = 64 * 1024;
1000 break;
1001 case I915_TILING_X:
Chris Wilson2ebed172009-02-11 14:26:30 +00001002 /* pin() will align the object as required by fence */
1003 alignment = 0;
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05001004 break;
1005 case I915_TILING_Y:
1006 /* FIXME: Is this true? */
1007 DRM_ERROR("Y tiled not allowed for scan out buffers\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001008 return -EINVAL;
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05001009 default:
1010 BUG();
1011 }
1012
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001013 mutex_lock(&dev->struct_mutex);
Chris Wilson8c4b8c32009-06-17 22:08:52 +01001014 ret = i915_gem_object_pin(obj, alignment);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001015 if (ret != 0) {
1016 mutex_unlock(&dev->struct_mutex);
1017 return ret;
1018 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05001019
Chris Wilson8c4b8c32009-06-17 22:08:52 +01001020 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001021 if (ret != 0) {
Chris Wilson8c4b8c32009-06-17 22:08:52 +01001022 i915_gem_object_unpin(obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001023 mutex_unlock(&dev->struct_mutex);
1024 return ret;
1025 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001026
Chris Wilson8c4b8c32009-06-17 22:08:52 +01001027 /* Pre-i965 needs to install a fence for tiled scan-out */
1028 if (!IS_I965G(dev) &&
1029 obj_priv->fence_reg == I915_FENCE_REG_NONE &&
1030 obj_priv->tiling_mode != I915_TILING_NONE) {
1031 ret = i915_gem_object_get_fence_reg(obj);
1032 if (ret != 0) {
1033 i915_gem_object_unpin(obj);
1034 mutex_unlock(&dev->struct_mutex);
1035 return ret;
1036 }
1037 }
1038
Jesse Barnes79e53942008-11-07 14:24:08 -08001039 dspcntr = I915_READ(dspcntr_reg);
Jesse Barnes712531b2009-01-09 13:56:14 -08001040 /* Mask out pixel format bits in case we change it */
1041 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Jesse Barnes79e53942008-11-07 14:24:08 -08001042 switch (crtc->fb->bits_per_pixel) {
1043 case 8:
1044 dspcntr |= DISPPLANE_8BPP;
1045 break;
1046 case 16:
1047 if (crtc->fb->depth == 15)
1048 dspcntr |= DISPPLANE_15_16BPP;
1049 else
1050 dspcntr |= DISPPLANE_16BPP;
1051 break;
1052 case 24:
1053 case 32:
1054 dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
1055 break;
1056 default:
1057 DRM_ERROR("Unknown color depth\n");
Chris Wilson8c4b8c32009-06-17 22:08:52 +01001058 i915_gem_object_unpin(obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001059 mutex_unlock(&dev->struct_mutex);
1060 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08001061 }
Jesse Barnesf5448472009-04-14 14:17:47 -07001062 if (IS_I965G(dev)) {
1063 if (obj_priv->tiling_mode != I915_TILING_NONE)
1064 dspcntr |= DISPPLANE_TILED;
1065 else
1066 dspcntr &= ~DISPPLANE_TILED;
1067 }
1068
Zhenyu Wang553bd142009-09-02 10:57:52 +08001069 if (IS_IGDNG(dev))
1070 /* must disable */
1071 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
1072
Jesse Barnes79e53942008-11-07 14:24:08 -08001073 I915_WRITE(dspcntr_reg, dspcntr);
1074
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001075 Start = obj_priv->gtt_offset;
1076 Offset = y * crtc->fb->pitch + x * (crtc->fb->bits_per_pixel / 8);
1077
Jesse Barnes79e53942008-11-07 14:24:08 -08001078 DRM_DEBUG("Writing base %08lX %08lX %d %d\n", Start, Offset, x, y);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001079 I915_WRITE(dspstride, crtc->fb->pitch);
Jesse Barnes79e53942008-11-07 14:24:08 -08001080 if (IS_I965G(dev)) {
1081 I915_WRITE(dspbase, Offset);
1082 I915_READ(dspbase);
1083 I915_WRITE(dspsurf, Start);
1084 I915_READ(dspsurf);
Jesse Barnesf5448472009-04-14 14:17:47 -07001085 I915_WRITE(dsptileoff, (y << 16) | x);
Jesse Barnes79e53942008-11-07 14:24:08 -08001086 } else {
1087 I915_WRITE(dspbase, Start + Offset);
1088 I915_READ(dspbase);
1089 }
1090
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05001091 intel_wait_for_vblank(dev);
1092
1093 if (old_fb) {
1094 intel_fb = to_intel_framebuffer(old_fb);
Jesse Barnes652c3932009-08-17 13:31:43 -07001095 obj_priv = intel_fb->obj->driver_private;
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05001096 i915_gem_object_unpin(intel_fb->obj);
1097 }
Jesse Barnes652c3932009-08-17 13:31:43 -07001098 intel_increase_pllclock(crtc, true);
1099
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001100 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08001101
1102 if (!dev->primary->master)
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001103 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08001104
1105 master_priv = dev->primary->master->driver_priv;
1106 if (!master_priv->sarea_priv)
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001107 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08001108
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001109 if (pipe) {
Jesse Barnes79e53942008-11-07 14:24:08 -08001110 master_priv->sarea_priv->pipeB_x = x;
1111 master_priv->sarea_priv->pipeB_y = y;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001112 } else {
1113 master_priv->sarea_priv->pipeA_x = x;
1114 master_priv->sarea_priv->pipeA_y = y;
Jesse Barnes79e53942008-11-07 14:24:08 -08001115 }
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001116
1117 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08001118}
1119
Zhenyu Wang24f119c2009-07-24 01:00:28 +08001120/* Disable the VGA plane that we never use */
1121static void i915_disable_vga (struct drm_device *dev)
1122{
1123 struct drm_i915_private *dev_priv = dev->dev_private;
1124 u8 sr1;
1125 u32 vga_reg;
1126
1127 if (IS_IGDNG(dev))
1128 vga_reg = CPU_VGACNTRL;
1129 else
1130 vga_reg = VGACNTRL;
1131
1132 if (I915_READ(vga_reg) & VGA_DISP_DISABLE)
1133 return;
1134
1135 I915_WRITE8(VGA_SR_INDEX, 1);
1136 sr1 = I915_READ8(VGA_SR_DATA);
1137 I915_WRITE8(VGA_SR_DATA, sr1 | (1 << 5));
1138 udelay(100);
1139
1140 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
1141}
1142
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001143static void igdng_disable_pll_edp (struct drm_crtc *crtc)
1144{
1145 struct drm_device *dev = crtc->dev;
1146 struct drm_i915_private *dev_priv = dev->dev_private;
1147 u32 dpa_ctl;
1148
1149 DRM_DEBUG("\n");
1150 dpa_ctl = I915_READ(DP_A);
1151 dpa_ctl &= ~DP_PLL_ENABLE;
1152 I915_WRITE(DP_A, dpa_ctl);
1153}
1154
1155static void igdng_enable_pll_edp (struct drm_crtc *crtc)
1156{
1157 struct drm_device *dev = crtc->dev;
1158 struct drm_i915_private *dev_priv = dev->dev_private;
1159 u32 dpa_ctl;
1160
1161 dpa_ctl = I915_READ(DP_A);
1162 dpa_ctl |= DP_PLL_ENABLE;
1163 I915_WRITE(DP_A, dpa_ctl);
1164 udelay(200);
1165}
1166
1167
1168static void igdng_set_pll_edp (struct drm_crtc *crtc, int clock)
1169{
1170 struct drm_device *dev = crtc->dev;
1171 struct drm_i915_private *dev_priv = dev->dev_private;
1172 u32 dpa_ctl;
1173
1174 DRM_DEBUG("eDP PLL enable for clock %d\n", clock);
1175 dpa_ctl = I915_READ(DP_A);
1176 dpa_ctl &= ~DP_PLL_FREQ_MASK;
1177
1178 if (clock < 200000) {
1179 u32 temp;
1180 dpa_ctl |= DP_PLL_FREQ_160MHZ;
1181 /* workaround for 160Mhz:
1182 1) program 0x4600c bits 15:0 = 0x8124
1183 2) program 0x46010 bit 0 = 1
1184 3) program 0x46034 bit 24 = 1
1185 4) program 0x64000 bit 14 = 1
1186 */
1187 temp = I915_READ(0x4600c);
1188 temp &= 0xffff0000;
1189 I915_WRITE(0x4600c, temp | 0x8124);
1190
1191 temp = I915_READ(0x46010);
1192 I915_WRITE(0x46010, temp | 1);
1193
1194 temp = I915_READ(0x46034);
1195 I915_WRITE(0x46034, temp | (1 << 24));
1196 } else {
1197 dpa_ctl |= DP_PLL_FREQ_270MHZ;
1198 }
1199 I915_WRITE(DP_A, dpa_ctl);
1200
1201 udelay(500);
1202}
1203
Zhenyu Wang2c072452009-06-05 15:38:42 +08001204static void igdng_crtc_dpms(struct drm_crtc *crtc, int mode)
Jesse Barnes79e53942008-11-07 14:24:08 -08001205{
1206 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08001207 struct drm_i915_private *dev_priv = dev->dev_private;
1208 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1209 int pipe = intel_crtc->pipe;
Shaohua Li7662c8b2009-06-26 11:23:55 +08001210 int plane = intel_crtc->plane;
Zhenyu Wang2c072452009-06-05 15:38:42 +08001211 int pch_dpll_reg = (pipe == 0) ? PCH_DPLL_A : PCH_DPLL_B;
1212 int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
1213 int dspcntr_reg = (plane == 0) ? DSPACNTR : DSPBCNTR;
1214 int dspbase_reg = (plane == 0) ? DSPAADDR : DSPBADDR;
1215 int fdi_tx_reg = (pipe == 0) ? FDI_TXA_CTL : FDI_TXB_CTL;
1216 int fdi_rx_reg = (pipe == 0) ? FDI_RXA_CTL : FDI_RXB_CTL;
1217 int fdi_rx_iir_reg = (pipe == 0) ? FDI_RXA_IIR : FDI_RXB_IIR;
1218 int fdi_rx_imr_reg = (pipe == 0) ? FDI_RXA_IMR : FDI_RXB_IMR;
1219 int transconf_reg = (pipe == 0) ? TRANSACONF : TRANSBCONF;
1220 int pf_ctl_reg = (pipe == 0) ? PFA_CTL_1 : PFB_CTL_1;
Zhenyu Wang249c0e62009-07-24 01:00:29 +08001221 int pf_win_size = (pipe == 0) ? PFA_WIN_SZ : PFB_WIN_SZ;
Zhenyu Wang2c072452009-06-05 15:38:42 +08001222 int cpu_htot_reg = (pipe == 0) ? HTOTAL_A : HTOTAL_B;
1223 int cpu_hblank_reg = (pipe == 0) ? HBLANK_A : HBLANK_B;
1224 int cpu_hsync_reg = (pipe == 0) ? HSYNC_A : HSYNC_B;
1225 int cpu_vtot_reg = (pipe == 0) ? VTOTAL_A : VTOTAL_B;
1226 int cpu_vblank_reg = (pipe == 0) ? VBLANK_A : VBLANK_B;
1227 int cpu_vsync_reg = (pipe == 0) ? VSYNC_A : VSYNC_B;
1228 int trans_htot_reg = (pipe == 0) ? TRANS_HTOTAL_A : TRANS_HTOTAL_B;
1229 int trans_hblank_reg = (pipe == 0) ? TRANS_HBLANK_A : TRANS_HBLANK_B;
1230 int trans_hsync_reg = (pipe == 0) ? TRANS_HSYNC_A : TRANS_HSYNC_B;
1231 int trans_vtot_reg = (pipe == 0) ? TRANS_VTOTAL_A : TRANS_VTOTAL_B;
1232 int trans_vblank_reg = (pipe == 0) ? TRANS_VBLANK_A : TRANS_VBLANK_B;
1233 int trans_vsync_reg = (pipe == 0) ? TRANS_VSYNC_A : TRANS_VSYNC_B;
1234 u32 temp;
Zhenyu Wang249c0e62009-07-24 01:00:29 +08001235 int tries = 5, j, n;
Zhenyu Wang2c072452009-06-05 15:38:42 +08001236
1237 /* XXX: When our outputs are all unaware of DPMS modes other than off
1238 * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
1239 */
1240 switch (mode) {
1241 case DRM_MODE_DPMS_ON:
1242 case DRM_MODE_DPMS_STANDBY:
1243 case DRM_MODE_DPMS_SUSPEND:
1244 DRM_DEBUG("crtc %d dpms on\n", pipe);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001245 if (HAS_eDP) {
1246 /* enable eDP PLL */
1247 igdng_enable_pll_edp(crtc);
1248 } else {
1249 /* enable PCH DPLL */
1250 temp = I915_READ(pch_dpll_reg);
1251 if ((temp & DPLL_VCO_ENABLE) == 0) {
1252 I915_WRITE(pch_dpll_reg, temp | DPLL_VCO_ENABLE);
1253 I915_READ(pch_dpll_reg);
1254 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08001255
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001256 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
1257 temp = I915_READ(fdi_rx_reg);
1258 I915_WRITE(fdi_rx_reg, temp | FDI_RX_PLL_ENABLE |
1259 FDI_SEL_PCDCLK |
1260 FDI_DP_PORT_WIDTH_X4); /* default 4 lanes */
1261 I915_READ(fdi_rx_reg);
1262 udelay(200);
Zhenyu Wang2c072452009-06-05 15:38:42 +08001263
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001264 /* Enable CPU FDI TX PLL, always on for IGDNG */
1265 temp = I915_READ(fdi_tx_reg);
1266 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
1267 I915_WRITE(fdi_tx_reg, temp | FDI_TX_PLL_ENABLE);
1268 I915_READ(fdi_tx_reg);
1269 udelay(100);
1270 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08001271 }
1272
1273 /* Enable CPU pipe */
1274 temp = I915_READ(pipeconf_reg);
1275 if ((temp & PIPEACONF_ENABLE) == 0) {
1276 I915_WRITE(pipeconf_reg, temp | PIPEACONF_ENABLE);
1277 I915_READ(pipeconf_reg);
1278 udelay(100);
1279 }
1280
1281 /* configure and enable CPU plane */
1282 temp = I915_READ(dspcntr_reg);
1283 if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
1284 I915_WRITE(dspcntr_reg, temp | DISPLAY_PLANE_ENABLE);
1285 /* Flush the plane changes */
1286 I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
1287 }
1288
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001289 if (!HAS_eDP) {
1290 /* enable CPU FDI TX and PCH FDI RX */
1291 temp = I915_READ(fdi_tx_reg);
1292 temp |= FDI_TX_ENABLE;
1293 temp |= FDI_DP_PORT_WIDTH_X4; /* default */
1294 temp &= ~FDI_LINK_TRAIN_NONE;
1295 temp |= FDI_LINK_TRAIN_PATTERN_1;
1296 I915_WRITE(fdi_tx_reg, temp);
1297 I915_READ(fdi_tx_reg);
Zhenyu Wang2c072452009-06-05 15:38:42 +08001298
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001299 temp = I915_READ(fdi_rx_reg);
1300 temp &= ~FDI_LINK_TRAIN_NONE;
1301 temp |= FDI_LINK_TRAIN_PATTERN_1;
1302 I915_WRITE(fdi_rx_reg, temp | FDI_RX_ENABLE);
1303 I915_READ(fdi_rx_reg);
Zhenyu Wang2c072452009-06-05 15:38:42 +08001304
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001305 udelay(150);
Zhenyu Wang2c072452009-06-05 15:38:42 +08001306
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001307 /* Train FDI. */
1308 /* umask FDI RX Interrupt symbol_lock and bit_lock bit
1309 for train result */
1310 temp = I915_READ(fdi_rx_imr_reg);
1311 temp &= ~FDI_RX_SYMBOL_LOCK;
1312 temp &= ~FDI_RX_BIT_LOCK;
1313 I915_WRITE(fdi_rx_imr_reg, temp);
1314 I915_READ(fdi_rx_imr_reg);
1315 udelay(150);
Zhenyu Wang2c072452009-06-05 15:38:42 +08001316
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001317 temp = I915_READ(fdi_rx_iir_reg);
1318 DRM_DEBUG("FDI_RX_IIR 0x%x\n", temp);
Zhenyu Wang2c072452009-06-05 15:38:42 +08001319
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001320 if ((temp & FDI_RX_BIT_LOCK) == 0) {
1321 for (j = 0; j < tries; j++) {
1322 temp = I915_READ(fdi_rx_iir_reg);
1323 DRM_DEBUG("FDI_RX_IIR 0x%x\n", temp);
1324 if (temp & FDI_RX_BIT_LOCK)
1325 break;
1326 udelay(200);
1327 }
1328 if (j != tries)
1329 I915_WRITE(fdi_rx_iir_reg,
1330 temp | FDI_RX_BIT_LOCK);
1331 else
1332 DRM_DEBUG("train 1 fail\n");
1333 } else {
Zhenyu Wang2c072452009-06-05 15:38:42 +08001334 I915_WRITE(fdi_rx_iir_reg,
1335 temp | FDI_RX_BIT_LOCK);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001336 DRM_DEBUG("train 1 ok 2!\n");
Zhenyu Wang2c072452009-06-05 15:38:42 +08001337 }
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001338 temp = I915_READ(fdi_tx_reg);
1339 temp &= ~FDI_LINK_TRAIN_NONE;
1340 temp |= FDI_LINK_TRAIN_PATTERN_2;
1341 I915_WRITE(fdi_tx_reg, temp);
1342
1343 temp = I915_READ(fdi_rx_reg);
1344 temp &= ~FDI_LINK_TRAIN_NONE;
1345 temp |= FDI_LINK_TRAIN_PATTERN_2;
1346 I915_WRITE(fdi_rx_reg, temp);
1347
1348 udelay(150);
1349
1350 temp = I915_READ(fdi_rx_iir_reg);
1351 DRM_DEBUG("FDI_RX_IIR 0x%x\n", temp);
1352
1353 if ((temp & FDI_RX_SYMBOL_LOCK) == 0) {
1354 for (j = 0; j < tries; j++) {
1355 temp = I915_READ(fdi_rx_iir_reg);
1356 DRM_DEBUG("FDI_RX_IIR 0x%x\n", temp);
1357 if (temp & FDI_RX_SYMBOL_LOCK)
1358 break;
1359 udelay(200);
1360 }
1361 if (j != tries) {
1362 I915_WRITE(fdi_rx_iir_reg,
1363 temp | FDI_RX_SYMBOL_LOCK);
1364 DRM_DEBUG("train 2 ok 1!\n");
1365 } else
1366 DRM_DEBUG("train 2 fail\n");
1367 } else {
Zhenyu Wang2c072452009-06-05 15:38:42 +08001368 I915_WRITE(fdi_rx_iir_reg,
1369 temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001370 DRM_DEBUG("train 2 ok 2!\n");
1371 }
1372 DRM_DEBUG("train done\n");
1373
1374 /* set transcoder timing */
1375 I915_WRITE(trans_htot_reg, I915_READ(cpu_htot_reg));
1376 I915_WRITE(trans_hblank_reg, I915_READ(cpu_hblank_reg));
1377 I915_WRITE(trans_hsync_reg, I915_READ(cpu_hsync_reg));
1378
1379 I915_WRITE(trans_vtot_reg, I915_READ(cpu_vtot_reg));
1380 I915_WRITE(trans_vblank_reg, I915_READ(cpu_vblank_reg));
1381 I915_WRITE(trans_vsync_reg, I915_READ(cpu_vsync_reg));
1382
1383 /* enable PCH transcoder */
1384 temp = I915_READ(transconf_reg);
1385 I915_WRITE(transconf_reg, temp | TRANS_ENABLE);
1386 I915_READ(transconf_reg);
1387
1388 while ((I915_READ(transconf_reg) & TRANS_STATE_ENABLE) == 0)
1389 ;
1390
1391 /* enable normal */
1392
1393 temp = I915_READ(fdi_tx_reg);
1394 temp &= ~FDI_LINK_TRAIN_NONE;
1395 I915_WRITE(fdi_tx_reg, temp | FDI_LINK_TRAIN_NONE |
1396 FDI_TX_ENHANCE_FRAME_ENABLE);
1397 I915_READ(fdi_tx_reg);
1398
1399 temp = I915_READ(fdi_rx_reg);
1400 temp &= ~FDI_LINK_TRAIN_NONE;
1401 I915_WRITE(fdi_rx_reg, temp | FDI_LINK_TRAIN_NONE |
1402 FDI_RX_ENHANCE_FRAME_ENABLE);
1403 I915_READ(fdi_rx_reg);
1404
1405 /* wait one idle pattern time */
1406 udelay(100);
1407
Zhenyu Wang2c072452009-06-05 15:38:42 +08001408 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08001409
1410 intel_crtc_load_lut(crtc);
1411
1412 break;
1413 case DRM_MODE_DPMS_OFF:
1414 DRM_DEBUG("crtc %d dpms off\n", pipe);
1415
Zhenyu Wang24f119c2009-07-24 01:00:28 +08001416 i915_disable_vga(dev);
Zhenyu Wang2c072452009-06-05 15:38:42 +08001417
1418 /* Disable display plane */
1419 temp = I915_READ(dspcntr_reg);
1420 if ((temp & DISPLAY_PLANE_ENABLE) != 0) {
1421 I915_WRITE(dspcntr_reg, temp & ~DISPLAY_PLANE_ENABLE);
1422 /* Flush the plane changes */
1423 I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
1424 I915_READ(dspbase_reg);
1425 }
1426
1427 /* disable cpu pipe, disable after all planes disabled */
1428 temp = I915_READ(pipeconf_reg);
1429 if ((temp & PIPEACONF_ENABLE) != 0) {
1430 I915_WRITE(pipeconf_reg, temp & ~PIPEACONF_ENABLE);
1431 I915_READ(pipeconf_reg);
Zhenyu Wang249c0e62009-07-24 01:00:29 +08001432 n = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08001433 /* wait for cpu pipe off, pipe state */
Zhenyu Wang249c0e62009-07-24 01:00:29 +08001434 while ((I915_READ(pipeconf_reg) & I965_PIPECONF_ACTIVE) != 0) {
1435 n++;
1436 if (n < 60) {
1437 udelay(500);
1438 continue;
1439 } else {
1440 DRM_DEBUG("pipe %d off delay\n", pipe);
1441 break;
1442 }
1443 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08001444 } else
1445 DRM_DEBUG("crtc %d is disabled\n", pipe);
1446
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001447 if (HAS_eDP) {
1448 igdng_disable_pll_edp(crtc);
1449 }
1450
Zhenyu Wang2c072452009-06-05 15:38:42 +08001451 /* disable CPU FDI tx and PCH FDI rx */
1452 temp = I915_READ(fdi_tx_reg);
1453 I915_WRITE(fdi_tx_reg, temp & ~FDI_TX_ENABLE);
1454 I915_READ(fdi_tx_reg);
1455
1456 temp = I915_READ(fdi_rx_reg);
1457 I915_WRITE(fdi_rx_reg, temp & ~FDI_RX_ENABLE);
1458 I915_READ(fdi_rx_reg);
1459
Zhenyu Wang249c0e62009-07-24 01:00:29 +08001460 udelay(100);
1461
Zhenyu Wang2c072452009-06-05 15:38:42 +08001462 /* still set train pattern 1 */
1463 temp = I915_READ(fdi_tx_reg);
1464 temp &= ~FDI_LINK_TRAIN_NONE;
1465 temp |= FDI_LINK_TRAIN_PATTERN_1;
1466 I915_WRITE(fdi_tx_reg, temp);
1467
1468 temp = I915_READ(fdi_rx_reg);
1469 temp &= ~FDI_LINK_TRAIN_NONE;
1470 temp |= FDI_LINK_TRAIN_PATTERN_1;
1471 I915_WRITE(fdi_rx_reg, temp);
1472
Zhenyu Wang249c0e62009-07-24 01:00:29 +08001473 udelay(100);
1474
Zhenyu Wang2c072452009-06-05 15:38:42 +08001475 /* disable PCH transcoder */
1476 temp = I915_READ(transconf_reg);
1477 if ((temp & TRANS_ENABLE) != 0) {
1478 I915_WRITE(transconf_reg, temp & ~TRANS_ENABLE);
1479 I915_READ(transconf_reg);
Zhenyu Wang249c0e62009-07-24 01:00:29 +08001480 n = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08001481 /* wait for PCH transcoder off, transcoder state */
Zhenyu Wang249c0e62009-07-24 01:00:29 +08001482 while ((I915_READ(transconf_reg) & TRANS_STATE_ENABLE) != 0) {
1483 n++;
1484 if (n < 60) {
1485 udelay(500);
1486 continue;
1487 } else {
1488 DRM_DEBUG("transcoder %d off delay\n", pipe);
1489 break;
1490 }
1491 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08001492 }
1493
1494 /* disable PCH DPLL */
1495 temp = I915_READ(pch_dpll_reg);
1496 if ((temp & DPLL_VCO_ENABLE) != 0) {
1497 I915_WRITE(pch_dpll_reg, temp & ~DPLL_VCO_ENABLE);
1498 I915_READ(pch_dpll_reg);
1499 }
1500
1501 temp = I915_READ(fdi_rx_reg);
1502 if ((temp & FDI_RX_PLL_ENABLE) != 0) {
1503 temp &= ~FDI_SEL_PCDCLK;
1504 temp &= ~FDI_RX_PLL_ENABLE;
1505 I915_WRITE(fdi_rx_reg, temp);
1506 I915_READ(fdi_rx_reg);
1507 }
1508
Zhenyu Wang249c0e62009-07-24 01:00:29 +08001509 /* Disable CPU FDI TX PLL */
1510 temp = I915_READ(fdi_tx_reg);
1511 if ((temp & FDI_TX_PLL_ENABLE) != 0) {
1512 I915_WRITE(fdi_tx_reg, temp & ~FDI_TX_PLL_ENABLE);
1513 I915_READ(fdi_tx_reg);
1514 udelay(100);
1515 }
1516
1517 /* Disable PF */
1518 temp = I915_READ(pf_ctl_reg);
1519 if ((temp & PF_ENABLE) != 0) {
1520 I915_WRITE(pf_ctl_reg, temp & ~PF_ENABLE);
1521 I915_READ(pf_ctl_reg);
1522 }
1523 I915_WRITE(pf_win_size, 0);
1524
Zhenyu Wang2c072452009-06-05 15:38:42 +08001525 /* Wait for the clocks to turn off. */
1526 udelay(150);
1527 break;
1528 }
1529}
1530
1531static void i9xx_crtc_dpms(struct drm_crtc *crtc, int mode)
1532{
1533 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08001534 struct drm_i915_private *dev_priv = dev->dev_private;
1535 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1536 int pipe = intel_crtc->pipe;
1537 int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
1538 int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
1539 int dspbase_reg = (pipe == 0) ? DSPAADDR : DSPBADDR;
1540 int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
1541 u32 temp;
Jesse Barnes79e53942008-11-07 14:24:08 -08001542
1543 /* XXX: When our outputs are all unaware of DPMS modes other than off
1544 * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
1545 */
1546 switch (mode) {
1547 case DRM_MODE_DPMS_ON:
1548 case DRM_MODE_DPMS_STANDBY:
1549 case DRM_MODE_DPMS_SUSPEND:
1550 /* Enable the DPLL */
1551 temp = I915_READ(dpll_reg);
1552 if ((temp & DPLL_VCO_ENABLE) == 0) {
1553 I915_WRITE(dpll_reg, temp);
1554 I915_READ(dpll_reg);
1555 /* Wait for the clocks to stabilize. */
1556 udelay(150);
1557 I915_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
1558 I915_READ(dpll_reg);
1559 /* Wait for the clocks to stabilize. */
1560 udelay(150);
1561 I915_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
1562 I915_READ(dpll_reg);
1563 /* Wait for the clocks to stabilize. */
1564 udelay(150);
1565 }
1566
1567 /* Enable the pipe */
1568 temp = I915_READ(pipeconf_reg);
1569 if ((temp & PIPEACONF_ENABLE) == 0)
1570 I915_WRITE(pipeconf_reg, temp | PIPEACONF_ENABLE);
1571
1572 /* Enable the plane */
1573 temp = I915_READ(dspcntr_reg);
1574 if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
1575 I915_WRITE(dspcntr_reg, temp | DISPLAY_PLANE_ENABLE);
1576 /* Flush the plane changes */
1577 I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
1578 }
1579
1580 intel_crtc_load_lut(crtc);
1581
1582 /* Give the overlay scaler a chance to enable if it's on this pipe */
1583 //intel_crtc_dpms_video(crtc, true); TODO
Shaohua Li7662c8b2009-06-26 11:23:55 +08001584 intel_update_watermarks(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001585 break;
1586 case DRM_MODE_DPMS_OFF:
Shaohua Li7662c8b2009-06-26 11:23:55 +08001587 intel_update_watermarks(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001588 /* Give the overlay scaler a chance to disable if it's on this pipe */
1589 //intel_crtc_dpms_video(crtc, FALSE); TODO
1590
1591 /* Disable the VGA plane that we never use */
Zhenyu Wang24f119c2009-07-24 01:00:28 +08001592 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001593
1594 /* Disable display plane */
1595 temp = I915_READ(dspcntr_reg);
1596 if ((temp & DISPLAY_PLANE_ENABLE) != 0) {
1597 I915_WRITE(dspcntr_reg, temp & ~DISPLAY_PLANE_ENABLE);
1598 /* Flush the plane changes */
1599 I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
1600 I915_READ(dspbase_reg);
1601 }
1602
1603 if (!IS_I9XX(dev)) {
1604 /* Wait for vblank for the disable to take effect */
1605 intel_wait_for_vblank(dev);
1606 }
1607
1608 /* Next, disable display pipes */
1609 temp = I915_READ(pipeconf_reg);
1610 if ((temp & PIPEACONF_ENABLE) != 0) {
1611 I915_WRITE(pipeconf_reg, temp & ~PIPEACONF_ENABLE);
1612 I915_READ(pipeconf_reg);
1613 }
1614
1615 /* Wait for vblank for the disable to take effect. */
1616 intel_wait_for_vblank(dev);
1617
1618 temp = I915_READ(dpll_reg);
1619 if ((temp & DPLL_VCO_ENABLE) != 0) {
1620 I915_WRITE(dpll_reg, temp & ~DPLL_VCO_ENABLE);
1621 I915_READ(dpll_reg);
1622 }
1623
1624 /* Wait for the clocks to turn off. */
1625 udelay(150);
1626 break;
1627 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08001628}
1629
1630/**
1631 * Sets the power management mode of the pipe and plane.
1632 *
1633 * This code should probably grow support for turning the cursor off and back
1634 * on appropriately at the same time as we're turning the pipe off/on.
1635 */
1636static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
1637{
1638 struct drm_device *dev = crtc->dev;
1639 struct drm_i915_master_private *master_priv;
1640 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1641 int pipe = intel_crtc->pipe;
1642 bool enabled;
1643
1644 if (IS_IGDNG(dev))
1645 igdng_crtc_dpms(crtc, mode);
1646 else
1647 i9xx_crtc_dpms(crtc, mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08001648
Daniel Vetter65655d42009-08-11 16:05:31 +02001649 intel_crtc->dpms_mode = mode;
1650
Jesse Barnes79e53942008-11-07 14:24:08 -08001651 if (!dev->primary->master)
1652 return;
1653
1654 master_priv = dev->primary->master->driver_priv;
1655 if (!master_priv->sarea_priv)
1656 return;
1657
1658 enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
1659
1660 switch (pipe) {
1661 case 0:
1662 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
1663 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
1664 break;
1665 case 1:
1666 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
1667 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
1668 break;
1669 default:
1670 DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
1671 break;
1672 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001673}
1674
1675static void intel_crtc_prepare (struct drm_crtc *crtc)
1676{
1677 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1678 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
1679}
1680
1681static void intel_crtc_commit (struct drm_crtc *crtc)
1682{
1683 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1684 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
1685}
1686
1687void intel_encoder_prepare (struct drm_encoder *encoder)
1688{
1689 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1690 /* lvds has its own version of prepare see intel_lvds_prepare */
1691 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
1692}
1693
1694void intel_encoder_commit (struct drm_encoder *encoder)
1695{
1696 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1697 /* lvds has its own version of commit see intel_lvds_commit */
1698 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
1699}
1700
1701static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
1702 struct drm_display_mode *mode,
1703 struct drm_display_mode *adjusted_mode)
1704{
Zhenyu Wang2c072452009-06-05 15:38:42 +08001705 struct drm_device *dev = crtc->dev;
1706 if (IS_IGDNG(dev)) {
1707 /* FDI link clock is fixed at 2.7G */
1708 if (mode->clock * 3 > 27000 * 4)
1709 return MODE_CLOCK_HIGH;
1710 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001711 return true;
1712}
1713
1714
1715/** Returns the core display clock speed for i830 - i945 */
1716static int intel_get_core_clock_speed(struct drm_device *dev)
1717{
1718
1719 /* Core clock values taken from the published datasheets.
1720 * The 830 may go up to 166 Mhz, which we should check.
1721 */
1722 if (IS_I945G(dev))
1723 return 400000;
1724 else if (IS_I915G(dev))
1725 return 333000;
Shaohua Li21778322009-02-23 15:19:16 +08001726 else if (IS_I945GM(dev) || IS_845G(dev) || IS_IGDGM(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08001727 return 200000;
1728 else if (IS_I915GM(dev)) {
1729 u16 gcfgc = 0;
1730
1731 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
1732
1733 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
1734 return 133000;
1735 else {
1736 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
1737 case GC_DISPLAY_CLOCK_333_MHZ:
1738 return 333000;
1739 default:
1740 case GC_DISPLAY_CLOCK_190_200_MHZ:
1741 return 190000;
1742 }
1743 }
1744 } else if (IS_I865G(dev))
1745 return 266000;
1746 else if (IS_I855(dev)) {
1747 u16 hpllcc = 0;
1748 /* Assume that the hardware is in the high speed state. This
1749 * should be the default.
1750 */
1751 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
1752 case GC_CLOCK_133_200:
1753 case GC_CLOCK_100_200:
1754 return 200000;
1755 case GC_CLOCK_166_250:
1756 return 250000;
1757 case GC_CLOCK_100_133:
1758 return 133000;
1759 }
1760 } else /* 852, 830 */
1761 return 133000;
1762
1763 return 0; /* Silence gcc warning */
1764}
1765
Jesse Barnes79e53942008-11-07 14:24:08 -08001766/**
1767 * Return the pipe currently connected to the panel fitter,
1768 * or -1 if the panel fitter is not present or not in use
1769 */
1770static int intel_panel_fitter_pipe (struct drm_device *dev)
1771{
1772 struct drm_i915_private *dev_priv = dev->dev_private;
1773 u32 pfit_control;
1774
1775 /* i830 doesn't have a panel fitter */
1776 if (IS_I830(dev))
1777 return -1;
1778
1779 pfit_control = I915_READ(PFIT_CONTROL);
1780
1781 /* See if the panel fitter is in use */
1782 if ((pfit_control & PFIT_ENABLE) == 0)
1783 return -1;
1784
1785 /* 965 can place panel fitter on either pipe */
1786 if (IS_I965G(dev))
1787 return (pfit_control >> 29) & 0x3;
1788
1789 /* older chips can only use pipe 1 */
1790 return 1;
1791}
1792
Zhenyu Wang2c072452009-06-05 15:38:42 +08001793struct fdi_m_n {
1794 u32 tu;
1795 u32 gmch_m;
1796 u32 gmch_n;
1797 u32 link_m;
1798 u32 link_n;
1799};
1800
1801static void
1802fdi_reduce_ratio(u32 *num, u32 *den)
1803{
1804 while (*num > 0xffffff || *den > 0xffffff) {
1805 *num >>= 1;
1806 *den >>= 1;
1807 }
1808}
1809
1810#define DATA_N 0x800000
1811#define LINK_N 0x80000
1812
1813static void
1814igdng_compute_m_n(int bytes_per_pixel, int nlanes,
1815 int pixel_clock, int link_clock,
1816 struct fdi_m_n *m_n)
1817{
1818 u64 temp;
1819
1820 m_n->tu = 64; /* default size */
1821
1822 temp = (u64) DATA_N * pixel_clock;
1823 temp = div_u64(temp, link_clock);
Andy Whitcroft956dba32009-07-01 15:20:59 +01001824 m_n->gmch_m = div_u64(temp * bytes_per_pixel, nlanes);
Zhenyu Wang2c072452009-06-05 15:38:42 +08001825 m_n->gmch_n = DATA_N;
1826 fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
1827
1828 temp = (u64) LINK_N * pixel_clock;
1829 m_n->link_m = div_u64(temp, link_clock);
1830 m_n->link_n = LINK_N;
1831 fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
1832}
1833
1834
Shaohua Li7662c8b2009-06-26 11:23:55 +08001835struct intel_watermark_params {
1836 unsigned long fifo_size;
1837 unsigned long max_wm;
1838 unsigned long default_wm;
1839 unsigned long guard_size;
1840 unsigned long cacheline_size;
1841};
1842
1843/* IGD has different values for various configs */
1844static struct intel_watermark_params igd_display_wm = {
1845 IGD_DISPLAY_FIFO,
1846 IGD_MAX_WM,
1847 IGD_DFT_WM,
1848 IGD_GUARD_WM,
1849 IGD_FIFO_LINE_SIZE
1850};
1851static struct intel_watermark_params igd_display_hplloff_wm = {
1852 IGD_DISPLAY_FIFO,
1853 IGD_MAX_WM,
1854 IGD_DFT_HPLLOFF_WM,
1855 IGD_GUARD_WM,
1856 IGD_FIFO_LINE_SIZE
1857};
1858static struct intel_watermark_params igd_cursor_wm = {
1859 IGD_CURSOR_FIFO,
1860 IGD_CURSOR_MAX_WM,
1861 IGD_CURSOR_DFT_WM,
1862 IGD_CURSOR_GUARD_WM,
1863 IGD_FIFO_LINE_SIZE,
1864};
1865static struct intel_watermark_params igd_cursor_hplloff_wm = {
1866 IGD_CURSOR_FIFO,
1867 IGD_CURSOR_MAX_WM,
1868 IGD_CURSOR_DFT_WM,
1869 IGD_CURSOR_GUARD_WM,
1870 IGD_FIFO_LINE_SIZE
1871};
1872static struct intel_watermark_params i945_wm_info = {
Shaohua Li7662c8b2009-06-26 11:23:55 +08001873 I945_FIFO_SIZE,
1874 I915_MAX_WM,
1875 1,
Jesse Barnesdff33cf2009-07-14 10:15:56 -07001876 2,
1877 I915_FIFO_LINE_SIZE
1878};
1879static struct intel_watermark_params i915_wm_info = {
1880 I915_FIFO_SIZE,
1881 I915_MAX_WM,
1882 1,
1883 2,
Shaohua Li7662c8b2009-06-26 11:23:55 +08001884 I915_FIFO_LINE_SIZE
1885};
1886static struct intel_watermark_params i855_wm_info = {
1887 I855GM_FIFO_SIZE,
1888 I915_MAX_WM,
1889 1,
Jesse Barnesdff33cf2009-07-14 10:15:56 -07001890 2,
Shaohua Li7662c8b2009-06-26 11:23:55 +08001891 I830_FIFO_LINE_SIZE
1892};
1893static struct intel_watermark_params i830_wm_info = {
1894 I830_FIFO_SIZE,
1895 I915_MAX_WM,
1896 1,
Jesse Barnesdff33cf2009-07-14 10:15:56 -07001897 2,
Shaohua Li7662c8b2009-06-26 11:23:55 +08001898 I830_FIFO_LINE_SIZE
1899};
1900
Jesse Barnesdff33cf2009-07-14 10:15:56 -07001901/**
1902 * intel_calculate_wm - calculate watermark level
1903 * @clock_in_khz: pixel clock
1904 * @wm: chip FIFO params
1905 * @pixel_size: display pixel size
1906 * @latency_ns: memory latency for the platform
1907 *
1908 * Calculate the watermark level (the level at which the display plane will
1909 * start fetching from memory again). Each chip has a different display
1910 * FIFO size and allocation, so the caller needs to figure that out and pass
1911 * in the correct intel_watermark_params structure.
1912 *
1913 * As the pixel clock runs, the FIFO will be drained at a rate that depends
1914 * on the pixel size. When it reaches the watermark level, it'll start
1915 * fetching FIFO line sized based chunks from memory until the FIFO fills
1916 * past the watermark point. If the FIFO drains completely, a FIFO underrun
1917 * will occur, and a display engine hang could result.
1918 */
Shaohua Li7662c8b2009-06-26 11:23:55 +08001919static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
1920 struct intel_watermark_params *wm,
1921 int pixel_size,
1922 unsigned long latency_ns)
1923{
Jesse Barnes390c4dd2009-07-16 13:01:01 -07001924 long entries_required, wm_size;
Shaohua Li7662c8b2009-06-26 11:23:55 +08001925
Jesse Barnesdff33cf2009-07-14 10:15:56 -07001926 entries_required = (clock_in_khz * pixel_size * latency_ns) / 1000000;
1927 entries_required /= wm->cacheline_size;
1928
1929 DRM_DEBUG("FIFO entries required for mode: %d\n", entries_required);
1930
1931 wm_size = wm->fifo_size - (entries_required + wm->guard_size);
1932
1933 DRM_DEBUG("FIFO watermark level: %d\n", wm_size);
Shaohua Li7662c8b2009-06-26 11:23:55 +08001934
Jesse Barnes390c4dd2009-07-16 13:01:01 -07001935 /* Don't promote wm_size to unsigned... */
1936 if (wm_size > (long)wm->max_wm)
Shaohua Li7662c8b2009-06-26 11:23:55 +08001937 wm_size = wm->max_wm;
Jesse Barnes390c4dd2009-07-16 13:01:01 -07001938 if (wm_size <= 0)
Shaohua Li7662c8b2009-06-26 11:23:55 +08001939 wm_size = wm->default_wm;
1940 return wm_size;
1941}
1942
1943struct cxsr_latency {
1944 int is_desktop;
1945 unsigned long fsb_freq;
1946 unsigned long mem_freq;
1947 unsigned long display_sr;
1948 unsigned long display_hpll_disable;
1949 unsigned long cursor_sr;
1950 unsigned long cursor_hpll_disable;
1951};
1952
1953static struct cxsr_latency cxsr_latency_table[] = {
1954 {1, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
1955 {1, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
1956 {1, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
1957
1958 {1, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
1959 {1, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
1960 {1, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
1961
1962 {1, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
1963 {1, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
1964 {1, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
1965
1966 {0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
1967 {0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
1968 {0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
1969
1970 {0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
1971 {0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
1972 {0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
1973
1974 {0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
1975 {0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
1976 {0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
1977};
1978
1979static struct cxsr_latency *intel_get_cxsr_latency(int is_desktop, int fsb,
1980 int mem)
1981{
1982 int i;
1983 struct cxsr_latency *latency;
1984
1985 if (fsb == 0 || mem == 0)
1986 return NULL;
1987
1988 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
1989 latency = &cxsr_latency_table[i];
1990 if (is_desktop == latency->is_desktop &&
1991 fsb == latency->fsb_freq && mem == latency->mem_freq)
1992 break;
1993 }
1994 if (i >= ARRAY_SIZE(cxsr_latency_table)) {
1995 DRM_DEBUG("Unknown FSB/MEM found, disable CxSR\n");
1996 return NULL;
1997 }
1998 return latency;
1999}
2000
2001static void igd_disable_cxsr(struct drm_device *dev)
2002{
2003 struct drm_i915_private *dev_priv = dev->dev_private;
2004 u32 reg;
2005
2006 /* deactivate cxsr */
2007 reg = I915_READ(DSPFW3);
2008 reg &= ~(IGD_SELF_REFRESH_EN);
2009 I915_WRITE(DSPFW3, reg);
2010 DRM_INFO("Big FIFO is disabled\n");
2011}
2012
2013static void igd_enable_cxsr(struct drm_device *dev, unsigned long clock,
2014 int pixel_size)
2015{
2016 struct drm_i915_private *dev_priv = dev->dev_private;
2017 u32 reg;
2018 unsigned long wm;
2019 struct cxsr_latency *latency;
2020
2021 latency = intel_get_cxsr_latency(IS_IGDG(dev), dev_priv->fsb_freq,
2022 dev_priv->mem_freq);
2023 if (!latency) {
2024 DRM_DEBUG("Unknown FSB/MEM found, disable CxSR\n");
2025 igd_disable_cxsr(dev);
2026 return;
2027 }
2028
2029 /* Display SR */
2030 wm = intel_calculate_wm(clock, &igd_display_wm, pixel_size,
2031 latency->display_sr);
2032 reg = I915_READ(DSPFW1);
2033 reg &= 0x7fffff;
2034 reg |= wm << 23;
2035 I915_WRITE(DSPFW1, reg);
2036 DRM_DEBUG("DSPFW1 register is %x\n", reg);
2037
2038 /* cursor SR */
2039 wm = intel_calculate_wm(clock, &igd_cursor_wm, pixel_size,
2040 latency->cursor_sr);
2041 reg = I915_READ(DSPFW3);
2042 reg &= ~(0x3f << 24);
2043 reg |= (wm & 0x3f) << 24;
2044 I915_WRITE(DSPFW3, reg);
2045
2046 /* Display HPLL off SR */
2047 wm = intel_calculate_wm(clock, &igd_display_hplloff_wm,
2048 latency->display_hpll_disable, I915_FIFO_LINE_SIZE);
2049 reg = I915_READ(DSPFW3);
2050 reg &= 0xfffffe00;
2051 reg |= wm & 0x1ff;
2052 I915_WRITE(DSPFW3, reg);
2053
2054 /* cursor HPLL off SR */
2055 wm = intel_calculate_wm(clock, &igd_cursor_hplloff_wm, pixel_size,
2056 latency->cursor_hpll_disable);
2057 reg = I915_READ(DSPFW3);
2058 reg &= ~(0x3f << 16);
2059 reg |= (wm & 0x3f) << 16;
2060 I915_WRITE(DSPFW3, reg);
2061 DRM_DEBUG("DSPFW3 register is %x\n", reg);
2062
2063 /* activate cxsr */
2064 reg = I915_READ(DSPFW3);
2065 reg |= IGD_SELF_REFRESH_EN;
2066 I915_WRITE(DSPFW3, reg);
2067
2068 DRM_INFO("Big FIFO is enabled\n");
2069
2070 return;
2071}
2072
Jesse Barnesbcc24fb2009-08-31 10:24:31 -07002073/*
2074 * Latency for FIFO fetches is dependent on several factors:
2075 * - memory configuration (speed, channels)
2076 * - chipset
2077 * - current MCH state
2078 * It can be fairly high in some situations, so here we assume a fairly
2079 * pessimal value. It's a tradeoff between extra memory fetches (if we
2080 * set this value too high, the FIFO will fetch frequently to stay full)
2081 * and power consumption (set it too low to save power and we might see
2082 * FIFO underruns and display "flicker").
2083 *
2084 * A value of 5us seems to be a good balance; safe for very low end
2085 * platforms but not overly aggressive on lower latency configs.
2086 */
2087const static int latency_ns = 5000;
Shaohua Li7662c8b2009-06-26 11:23:55 +08002088
Jesse Barnesdff33cf2009-07-14 10:15:56 -07002089static int intel_get_fifo_size(struct drm_device *dev, int plane)
2090{
2091 struct drm_i915_private *dev_priv = dev->dev_private;
2092 uint32_t dsparb = I915_READ(DSPARB);
2093 int size;
2094
2095 if (IS_I9XX(dev)) {
2096 if (plane == 0)
2097 size = dsparb & 0x7f;
2098 else
2099 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) -
2100 (dsparb & 0x7f);
2101 } else if (IS_I85X(dev)) {
2102 if (plane == 0)
2103 size = dsparb & 0x1ff;
2104 else
2105 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) -
2106 (dsparb & 0x1ff);
2107 size >>= 1; /* Convert to cachelines */
Jesse Barnesf3601322009-07-22 12:54:59 -07002108 } else if (IS_845G(dev)) {
2109 size = dsparb & 0x7f;
2110 size >>= 2; /* Convert to cachelines */
Jesse Barnesdff33cf2009-07-14 10:15:56 -07002111 } else {
2112 size = dsparb & 0x7f;
2113 size >>= 1; /* Convert to cachelines */
2114 }
2115
2116 DRM_DEBUG("FIFO size - (0x%08x) %s: %d\n", dsparb, plane ? "B" : "A",
2117 size);
2118
2119 return size;
2120}
Shaohua Li7662c8b2009-06-26 11:23:55 +08002121
Jesse Barnes652c3932009-08-17 13:31:43 -07002122static void g4x_update_wm(struct drm_device *dev)
2123{
2124 struct drm_i915_private *dev_priv = dev->dev_private;
2125 u32 fw_blc_self = I915_READ(FW_BLC_SELF);
2126
2127 if (i915_powersave)
2128 fw_blc_self |= FW_BLC_SELF_EN;
2129 else
2130 fw_blc_self &= ~FW_BLC_SELF_EN;
2131 I915_WRITE(FW_BLC_SELF, fw_blc_self);
2132}
2133
Shaohua Li7662c8b2009-06-26 11:23:55 +08002134static void i965_update_wm(struct drm_device *dev)
2135{
2136 struct drm_i915_private *dev_priv = dev->dev_private;
2137
2138 DRM_DEBUG("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR 8\n");
2139
2140 /* 965 has limitations... */
2141 I915_WRITE(DSPFW1, (8 << 16) | (8 << 8) | (8 << 0));
2142 I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
2143}
2144
2145static void i9xx_update_wm(struct drm_device *dev, int planea_clock,
2146 int planeb_clock, int sr_hdisplay, int pixel_size)
2147{
2148 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07002149 uint32_t fwater_lo;
2150 uint32_t fwater_hi;
2151 int total_size, cacheline_size, cwm, srwm = 1;
2152 int planea_wm, planeb_wm;
2153 struct intel_watermark_params planea_params, planeb_params;
Shaohua Li7662c8b2009-06-26 11:23:55 +08002154 unsigned long line_time_us;
2155 int sr_clock, sr_entries = 0;
2156
Jesse Barnesdff33cf2009-07-14 10:15:56 -07002157 /* Create copies of the base settings for each pipe */
Shaohua Li7662c8b2009-06-26 11:23:55 +08002158 if (IS_I965GM(dev) || IS_I945GM(dev))
Jesse Barnesdff33cf2009-07-14 10:15:56 -07002159 planea_params = planeb_params = i945_wm_info;
Shaohua Li7662c8b2009-06-26 11:23:55 +08002160 else if (IS_I9XX(dev))
Jesse Barnesdff33cf2009-07-14 10:15:56 -07002161 planea_params = planeb_params = i915_wm_info;
Shaohua Li7662c8b2009-06-26 11:23:55 +08002162 else
Jesse Barnesdff33cf2009-07-14 10:15:56 -07002163 planea_params = planeb_params = i855_wm_info;
Shaohua Li7662c8b2009-06-26 11:23:55 +08002164
Jesse Barnesdff33cf2009-07-14 10:15:56 -07002165 /* Grab a couple of global values before we overwrite them */
2166 total_size = planea_params.fifo_size;
2167 cacheline_size = planea_params.cacheline_size;
Shaohua Li7662c8b2009-06-26 11:23:55 +08002168
Jesse Barnesdff33cf2009-07-14 10:15:56 -07002169 /* Update per-plane FIFO sizes */
2170 planea_params.fifo_size = intel_get_fifo_size(dev, 0);
2171 planeb_params.fifo_size = intel_get_fifo_size(dev, 1);
Shaohua Li7662c8b2009-06-26 11:23:55 +08002172
Jesse Barnesdff33cf2009-07-14 10:15:56 -07002173 planea_wm = intel_calculate_wm(planea_clock, &planea_params,
2174 pixel_size, latency_ns);
2175 planeb_wm = intel_calculate_wm(planeb_clock, &planeb_params,
2176 pixel_size, latency_ns);
2177 DRM_DEBUG("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
Shaohua Li7662c8b2009-06-26 11:23:55 +08002178
2179 /*
2180 * Overlay gets an aggressive default since video jitter is bad.
2181 */
2182 cwm = 2;
2183
Jesse Barnesdff33cf2009-07-14 10:15:56 -07002184 /* Calc sr entries for one plane configs */
Jesse Barnes652c3932009-08-17 13:31:43 -07002185 if (HAS_FW_BLC(dev) && sr_hdisplay &&
2186 (!planea_clock || !planeb_clock)) {
Jesse Barnesdff33cf2009-07-14 10:15:56 -07002187 /* self-refresh has much higher latency */
2188 const static int sr_latency_ns = 6000;
2189
Shaohua Li7662c8b2009-06-26 11:23:55 +08002190 sr_clock = planea_clock ? planea_clock : planeb_clock;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07002191 line_time_us = ((sr_hdisplay * 1000) / sr_clock);
2192
2193 /* Use ns/us then divide to preserve precision */
2194 sr_entries = (((sr_latency_ns / line_time_us) + 1) *
2195 pixel_size * sr_hdisplay) / 1000;
2196 sr_entries = roundup(sr_entries / cacheline_size, 1);
2197 DRM_DEBUG("self-refresh entries: %d\n", sr_entries);
2198 srwm = total_size - sr_entries;
2199 if (srwm < 0)
2200 srwm = 1;
Jesse Barnes652c3932009-08-17 13:31:43 -07002201 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN | (srwm & 0x3f));
Shaohua Li7662c8b2009-06-26 11:23:55 +08002202 }
2203
2204 DRM_DEBUG("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
Jesse Barnesdff33cf2009-07-14 10:15:56 -07002205 planea_wm, planeb_wm, cwm, srwm);
Shaohua Li7662c8b2009-06-26 11:23:55 +08002206
Jesse Barnesdff33cf2009-07-14 10:15:56 -07002207 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
2208 fwater_hi = (cwm & 0x1f);
2209
2210 /* Set request length to 8 cachelines per fetch */
2211 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
2212 fwater_hi = fwater_hi | (1 << 8);
Shaohua Li7662c8b2009-06-26 11:23:55 +08002213
2214 I915_WRITE(FW_BLC, fwater_lo);
2215 I915_WRITE(FW_BLC2, fwater_hi);
Shaohua Li7662c8b2009-06-26 11:23:55 +08002216}
2217
2218static void i830_update_wm(struct drm_device *dev, int planea_clock,
2219 int pixel_size)
2220{
2221 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf3601322009-07-22 12:54:59 -07002222 uint32_t fwater_lo = I915_READ(FW_BLC) & ~0xfff;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07002223 int planea_wm;
Shaohua Li7662c8b2009-06-26 11:23:55 +08002224
Jesse Barnesdff33cf2009-07-14 10:15:56 -07002225 i830_wm_info.fifo_size = intel_get_fifo_size(dev, 0);
Shaohua Li7662c8b2009-06-26 11:23:55 +08002226
Jesse Barnesdff33cf2009-07-14 10:15:56 -07002227 planea_wm = intel_calculate_wm(planea_clock, &i830_wm_info,
2228 pixel_size, latency_ns);
Jesse Barnesf3601322009-07-22 12:54:59 -07002229 fwater_lo |= (3<<8) | planea_wm;
2230
2231 DRM_DEBUG("Setting FIFO watermarks - A: %d\n", planea_wm);
Shaohua Li7662c8b2009-06-26 11:23:55 +08002232
2233 I915_WRITE(FW_BLC, fwater_lo);
2234}
2235
2236/**
2237 * intel_update_watermarks - update FIFO watermark values based on current modes
2238 *
2239 * Calculate watermark values for the various WM regs based on current mode
2240 * and plane configuration.
2241 *
2242 * There are several cases to deal with here:
2243 * - normal (i.e. non-self-refresh)
2244 * - self-refresh (SR) mode
2245 * - lines are large relative to FIFO size (buffer can hold up to 2)
2246 * - lines are small relative to FIFO size (buffer can hold more than 2
2247 * lines), so need to account for TLB latency
2248 *
2249 * The normal calculation is:
2250 * watermark = dotclock * bytes per pixel * latency
2251 * where latency is platform & configuration dependent (we assume pessimal
2252 * values here).
2253 *
2254 * The SR calculation is:
2255 * watermark = (trunc(latency/line time)+1) * surface width *
2256 * bytes per pixel
2257 * where
2258 * line time = htotal / dotclock
2259 * and latency is assumed to be high, as above.
2260 *
2261 * The final value programmed to the register should always be rounded up,
2262 * and include an extra 2 entries to account for clock crossings.
2263 *
2264 * We don't use the sprite, so we can ignore that. And on Crestline we have
2265 * to set the non-SR watermarks to 8.
2266 */
2267static void intel_update_watermarks(struct drm_device *dev)
2268{
2269 struct drm_crtc *crtc;
2270 struct intel_crtc *intel_crtc;
2271 int sr_hdisplay = 0;
2272 unsigned long planea_clock = 0, planeb_clock = 0, sr_clock = 0;
2273 int enabled = 0, pixel_size = 0;
2274
Shaohua Li7662c8b2009-06-26 11:23:55 +08002275 /* Get the clock config from both planes */
2276 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2277 intel_crtc = to_intel_crtc(crtc);
2278 if (crtc->enabled) {
2279 enabled++;
2280 if (intel_crtc->plane == 0) {
2281 DRM_DEBUG("plane A (pipe %d) clock: %d\n",
2282 intel_crtc->pipe, crtc->mode.clock);
2283 planea_clock = crtc->mode.clock;
2284 } else {
2285 DRM_DEBUG("plane B (pipe %d) clock: %d\n",
2286 intel_crtc->pipe, crtc->mode.clock);
2287 planeb_clock = crtc->mode.clock;
2288 }
2289 sr_hdisplay = crtc->mode.hdisplay;
2290 sr_clock = crtc->mode.clock;
2291 if (crtc->fb)
2292 pixel_size = crtc->fb->bits_per_pixel / 8;
2293 else
2294 pixel_size = 4; /* by default */
2295 }
2296 }
2297
2298 if (enabled <= 0)
2299 return;
2300
Jesse Barnesdff33cf2009-07-14 10:15:56 -07002301 /* Single plane configs can enable self refresh */
Shaohua Li7662c8b2009-06-26 11:23:55 +08002302 if (enabled == 1 && IS_IGD(dev))
2303 igd_enable_cxsr(dev, sr_clock, pixel_size);
2304 else if (IS_IGD(dev))
2305 igd_disable_cxsr(dev);
2306
Jesse Barnes652c3932009-08-17 13:31:43 -07002307 if (IS_G4X(dev))
2308 g4x_update_wm(dev);
2309 else if (IS_I965G(dev))
Shaohua Li7662c8b2009-06-26 11:23:55 +08002310 i965_update_wm(dev);
2311 else if (IS_I9XX(dev) || IS_MOBILE(dev))
2312 i9xx_update_wm(dev, planea_clock, planeb_clock, sr_hdisplay,
2313 pixel_size);
2314 else
2315 i830_update_wm(dev, planea_clock, pixel_size);
2316}
2317
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002318static int intel_crtc_mode_set(struct drm_crtc *crtc,
2319 struct drm_display_mode *mode,
2320 struct drm_display_mode *adjusted_mode,
2321 int x, int y,
2322 struct drm_framebuffer *old_fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002323{
2324 struct drm_device *dev = crtc->dev;
2325 struct drm_i915_private *dev_priv = dev->dev_private;
2326 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2327 int pipe = intel_crtc->pipe;
2328 int fp_reg = (pipe == 0) ? FPA0 : FPB0;
2329 int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
2330 int dpll_md_reg = (intel_crtc->pipe == 0) ? DPLL_A_MD : DPLL_B_MD;
2331 int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
2332 int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
2333 int htot_reg = (pipe == 0) ? HTOTAL_A : HTOTAL_B;
2334 int hblank_reg = (pipe == 0) ? HBLANK_A : HBLANK_B;
2335 int hsync_reg = (pipe == 0) ? HSYNC_A : HSYNC_B;
2336 int vtot_reg = (pipe == 0) ? VTOTAL_A : VTOTAL_B;
2337 int vblank_reg = (pipe == 0) ? VBLANK_A : VBLANK_B;
2338 int vsync_reg = (pipe == 0) ? VSYNC_A : VSYNC_B;
2339 int dspsize_reg = (pipe == 0) ? DSPASIZE : DSPBSIZE;
2340 int dsppos_reg = (pipe == 0) ? DSPAPOS : DSPBPOS;
2341 int pipesrc_reg = (pipe == 0) ? PIPEASRC : PIPEBSRC;
Kristian Høgsberg43565a02009-02-13 20:56:52 -05002342 int refclk, num_outputs = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07002343 intel_clock_t clock, reduced_clock;
2344 u32 dpll = 0, fp = 0, fp2 = 0, dspcntr, pipeconf;
2345 bool ok, has_reduced_clock = false, is_sdvo = false, is_dvo = false;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002346 bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002347 bool is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08002348 struct drm_mode_config *mode_config = &dev->mode_config;
2349 struct drm_connector *connector;
Ma Lingd4906092009-03-18 20:13:27 +08002350 const intel_limit_t *limit;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002351 int ret;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002352 struct fdi_m_n m_n = {0};
2353 int data_m1_reg = (pipe == 0) ? PIPEA_DATA_M1 : PIPEB_DATA_M1;
2354 int data_n1_reg = (pipe == 0) ? PIPEA_DATA_N1 : PIPEB_DATA_N1;
2355 int link_m1_reg = (pipe == 0) ? PIPEA_LINK_M1 : PIPEB_LINK_M1;
2356 int link_n1_reg = (pipe == 0) ? PIPEA_LINK_N1 : PIPEB_LINK_N1;
2357 int pch_fp_reg = (pipe == 0) ? PCH_FPA0 : PCH_FPB0;
2358 int pch_dpll_reg = (pipe == 0) ? PCH_DPLL_A : PCH_DPLL_B;
2359 int fdi_rx_reg = (pipe == 0) ? FDI_RXA_CTL : FDI_RXB_CTL;
Zhenyu Wang541998a2009-06-05 15:38:44 +08002360 int lvds_reg = LVDS;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002361 u32 temp;
2362 int sdvo_pixel_multiply;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002363 int target_clock;
Jesse Barnes79e53942008-11-07 14:24:08 -08002364
2365 drm_vblank_pre_modeset(dev, pipe);
2366
2367 list_for_each_entry(connector, &mode_config->connector_list, head) {
2368 struct intel_output *intel_output = to_intel_output(connector);
2369
2370 if (!connector->encoder || connector->encoder->crtc != crtc)
2371 continue;
2372
2373 switch (intel_output->type) {
2374 case INTEL_OUTPUT_LVDS:
2375 is_lvds = true;
2376 break;
2377 case INTEL_OUTPUT_SDVO:
Eric Anholt7d573822009-01-02 13:33:00 -08002378 case INTEL_OUTPUT_HDMI:
Jesse Barnes79e53942008-11-07 14:24:08 -08002379 is_sdvo = true;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08002380 if (intel_output->needs_tv_clock)
2381 is_tv = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08002382 break;
2383 case INTEL_OUTPUT_DVO:
2384 is_dvo = true;
2385 break;
2386 case INTEL_OUTPUT_TVOUT:
2387 is_tv = true;
2388 break;
2389 case INTEL_OUTPUT_ANALOG:
2390 is_crt = true;
2391 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002392 case INTEL_OUTPUT_DISPLAYPORT:
2393 is_dp = true;
2394 break;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002395 case INTEL_OUTPUT_EDP:
2396 is_edp = true;
2397 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08002398 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05002399
2400 num_outputs++;
Jesse Barnes79e53942008-11-07 14:24:08 -08002401 }
2402
Kristian Høgsberg43565a02009-02-13 20:56:52 -05002403 if (is_lvds && dev_priv->lvds_use_ssc && num_outputs < 2) {
2404 refclk = dev_priv->lvds_ssc_freq * 1000;
2405 DRM_DEBUG("using SSC reference clock of %d MHz\n", refclk / 1000);
2406 } else if (IS_I9XX(dev)) {
Jesse Barnes79e53942008-11-07 14:24:08 -08002407 refclk = 96000;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002408 if (IS_IGDNG(dev))
2409 refclk = 120000; /* 120Mhz refclk */
Jesse Barnes79e53942008-11-07 14:24:08 -08002410 } else {
2411 refclk = 48000;
2412 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002413
Jesse Barnes79e53942008-11-07 14:24:08 -08002414
Ma Lingd4906092009-03-18 20:13:27 +08002415 /*
2416 * Returns a set of divisors for the desired target clock with the given
2417 * refclk, or FALSE. The returned values represent the clock equation:
2418 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
2419 */
2420 limit = intel_limit(crtc);
2421 ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08002422 if (!ok) {
2423 DRM_ERROR("Couldn't find PLL settings for mode!\n");
Chris Wilson1f803ee2009-06-06 09:45:59 +01002424 drm_vblank_post_modeset(dev, pipe);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002425 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002426 }
2427
Jesse Barnes652c3932009-08-17 13:31:43 -07002428 if (limit->find_reduced_pll && dev_priv->lvds_downclock_avail) {
2429 memcpy(&reduced_clock, &clock, sizeof(intel_clock_t));
2430 has_reduced_clock = limit->find_reduced_pll(limit, crtc,
2431 (adjusted_mode->clock*3/4),
2432 refclk,
2433 &reduced_clock);
2434 }
2435
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08002436 /* SDVO TV has fixed PLL values depend on its clock range,
2437 this mirrors vbios setting. */
2438 if (is_sdvo && is_tv) {
2439 if (adjusted_mode->clock >= 100000
2440 && adjusted_mode->clock < 140500) {
2441 clock.p1 = 2;
2442 clock.p2 = 10;
2443 clock.n = 3;
2444 clock.m1 = 16;
2445 clock.m2 = 8;
2446 } else if (adjusted_mode->clock >= 140500
2447 && adjusted_mode->clock <= 200000) {
2448 clock.p1 = 1;
2449 clock.p2 = 10;
2450 clock.n = 6;
2451 clock.m1 = 12;
2452 clock.m2 = 8;
2453 }
2454 }
2455
Zhenyu Wang2c072452009-06-05 15:38:42 +08002456 /* FDI link */
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002457 if (IS_IGDNG(dev)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002458 int lane, link_bw;
2459 /* eDP doesn't require FDI link, so just set DP M/N
2460 according to current link config */
2461 if (is_edp) {
2462 struct drm_connector *edp;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002463 target_clock = mode->clock;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002464 edp = intel_pipe_get_output(crtc);
2465 intel_edp_link_config(to_intel_output(edp),
2466 &lane, &link_bw);
2467 } else {
2468 /* DP over FDI requires target mode clock
2469 instead of link clock */
2470 if (is_dp)
2471 target_clock = mode->clock;
2472 else
2473 target_clock = adjusted_mode->clock;
2474 lane = 4;
2475 link_bw = 270000;
2476 }
2477 igdng_compute_m_n(3, lane, target_clock,
2478 link_bw, &m_n);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002479 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08002480
Jesse Barnes652c3932009-08-17 13:31:43 -07002481 if (IS_IGD(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +08002482 fp = (1 << clock.n) << 16 | clock.m1 << 8 | clock.m2;
Jesse Barnes652c3932009-08-17 13:31:43 -07002483 if (has_reduced_clock)
2484 fp2 = (1 << reduced_clock.n) << 16 |
2485 reduced_clock.m1 << 8 | reduced_clock.m2;
2486 } else {
Shaohua Li21778322009-02-23 15:19:16 +08002487 fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
Jesse Barnes652c3932009-08-17 13:31:43 -07002488 if (has_reduced_clock)
2489 fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
2490 reduced_clock.m2;
2491 }
Jesse Barnes79e53942008-11-07 14:24:08 -08002492
Zhenyu Wang2c072452009-06-05 15:38:42 +08002493 if (!IS_IGDNG(dev))
2494 dpll = DPLL_VGA_MODE_DIS;
2495
Jesse Barnes79e53942008-11-07 14:24:08 -08002496 if (IS_I9XX(dev)) {
2497 if (is_lvds)
2498 dpll |= DPLLB_MODE_LVDS;
2499 else
2500 dpll |= DPLLB_MODE_DAC_SERIAL;
2501 if (is_sdvo) {
2502 dpll |= DPLL_DVO_HIGH_SPEED;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002503 sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
Sean Young942642a2009-08-06 17:35:50 +08002504 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08002505 dpll |= (sdvo_pixel_multiply - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002506 else if (IS_IGDNG(dev))
2507 dpll |= (sdvo_pixel_multiply - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Jesse Barnes79e53942008-11-07 14:24:08 -08002508 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002509 if (is_dp)
2510 dpll |= DPLL_DVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08002511
2512 /* compute bitmask from p1 value */
Shaohua Li21778322009-02-23 15:19:16 +08002513 if (IS_IGD(dev))
2514 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_IGD;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002515 else {
Shaohua Li21778322009-02-23 15:19:16 +08002516 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002517 /* also FPA1 */
2518 if (IS_IGDNG(dev))
2519 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Jesse Barnes652c3932009-08-17 13:31:43 -07002520 if (IS_G4X(dev) && has_reduced_clock)
2521 dpll |= (1 << (reduced_clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002522 }
Jesse Barnes79e53942008-11-07 14:24:08 -08002523 switch (clock.p2) {
2524 case 5:
2525 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
2526 break;
2527 case 7:
2528 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
2529 break;
2530 case 10:
2531 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
2532 break;
2533 case 14:
2534 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
2535 break;
2536 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08002537 if (IS_I965G(dev) && !IS_IGDNG(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08002538 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
2539 } else {
2540 if (is_lvds) {
2541 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
2542 } else {
2543 if (clock.p1 == 2)
2544 dpll |= PLL_P1_DIVIDE_BY_TWO;
2545 else
2546 dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
2547 if (clock.p2 == 4)
2548 dpll |= PLL_P2_DIVIDE_BY_4;
2549 }
2550 }
2551
Kristian Høgsberg43565a02009-02-13 20:56:52 -05002552 if (is_sdvo && is_tv)
2553 dpll |= PLL_REF_INPUT_TVCLKINBC;
2554 else if (is_tv)
Jesse Barnes79e53942008-11-07 14:24:08 -08002555 /* XXX: just matching BIOS for now */
Kristian Høgsberg43565a02009-02-13 20:56:52 -05002556 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
Jesse Barnes79e53942008-11-07 14:24:08 -08002557 dpll |= 3;
Kristian Høgsberg43565a02009-02-13 20:56:52 -05002558 else if (is_lvds && dev_priv->lvds_use_ssc && num_outputs < 2)
2559 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08002560 else
2561 dpll |= PLL_REF_INPUT_DREFCLK;
2562
2563 /* setup pipeconf */
2564 pipeconf = I915_READ(pipeconf_reg);
2565
2566 /* Set up the display plane register */
2567 dspcntr = DISPPLANE_GAMMA_ENABLE;
2568
Zhenyu Wang2c072452009-06-05 15:38:42 +08002569 /* IGDNG's plane is forced to pipe, bit 24 is to
2570 enable color space conversion */
2571 if (!IS_IGDNG(dev)) {
2572 if (pipe == 0)
2573 dspcntr |= DISPPLANE_SEL_PIPE_A;
2574 else
2575 dspcntr |= DISPPLANE_SEL_PIPE_B;
2576 }
Jesse Barnes79e53942008-11-07 14:24:08 -08002577
2578 if (pipe == 0 && !IS_I965G(dev)) {
2579 /* Enable pixel doubling when the dot clock is > 90% of the (display)
2580 * core speed.
2581 *
2582 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
2583 * pipe == 0 check?
2584 */
2585 if (mode->clock > intel_get_core_clock_speed(dev) * 9 / 10)
2586 pipeconf |= PIPEACONF_DOUBLE_WIDE;
2587 else
2588 pipeconf &= ~PIPEACONF_DOUBLE_WIDE;
2589 }
2590
2591 dspcntr |= DISPLAY_PLANE_ENABLE;
2592 pipeconf |= PIPEACONF_ENABLE;
2593 dpll |= DPLL_VCO_ENABLE;
2594
2595
2596 /* Disable the panel fitter if it was on our pipe */
Zhenyu Wang2c072452009-06-05 15:38:42 +08002597 if (!IS_IGDNG(dev) && intel_panel_fitter_pipe(dev) == pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -08002598 I915_WRITE(PFIT_CONTROL, 0);
2599
2600 DRM_DEBUG("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
2601 drm_mode_debug_printmodeline(mode);
2602
Zhenyu Wang2c072452009-06-05 15:38:42 +08002603 /* assign to IGDNG registers */
2604 if (IS_IGDNG(dev)) {
2605 fp_reg = pch_fp_reg;
2606 dpll_reg = pch_dpll_reg;
2607 }
Jesse Barnes79e53942008-11-07 14:24:08 -08002608
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002609 if (is_edp) {
2610 igdng_disable_pll_edp(crtc);
2611 } else if ((dpll & DPLL_VCO_ENABLE)) {
Jesse Barnes79e53942008-11-07 14:24:08 -08002612 I915_WRITE(fp_reg, fp);
2613 I915_WRITE(dpll_reg, dpll & ~DPLL_VCO_ENABLE);
2614 I915_READ(dpll_reg);
2615 udelay(150);
2616 }
2617
2618 /* The LVDS pin pair needs to be on before the DPLLs are enabled.
2619 * This is an exception to the general rule that mode_set doesn't turn
2620 * things on.
2621 */
2622 if (is_lvds) {
Zhenyu Wang541998a2009-06-05 15:38:44 +08002623 u32 lvds;
Jesse Barnes79e53942008-11-07 14:24:08 -08002624
Zhenyu Wang541998a2009-06-05 15:38:44 +08002625 if (IS_IGDNG(dev))
2626 lvds_reg = PCH_LVDS;
2627
2628 lvds = I915_READ(lvds_reg);
Jesse Barnes79e53942008-11-07 14:24:08 -08002629 lvds |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP | LVDS_PIPEB_SELECT;
2630 /* Set the B0-B3 data pairs corresponding to whether we're going to
2631 * set the DPLLs for dual-channel mode or not.
2632 */
2633 if (clock.p2 == 7)
2634 lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
2635 else
2636 lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
2637
2638 /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
2639 * appropriately here, but we need to look more thoroughly into how
2640 * panels behave in the two modes.
2641 */
2642
Zhenyu Wang541998a2009-06-05 15:38:44 +08002643 I915_WRITE(lvds_reg, lvds);
2644 I915_READ(lvds_reg);
Jesse Barnes79e53942008-11-07 14:24:08 -08002645 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002646 if (is_dp)
2647 intel_dp_set_m_n(crtc, mode, adjusted_mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08002648
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002649 if (!is_edp) {
2650 I915_WRITE(fp_reg, fp);
Jesse Barnes79e53942008-11-07 14:24:08 -08002651 I915_WRITE(dpll_reg, dpll);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002652 I915_READ(dpll_reg);
2653 /* Wait for the clocks to stabilize. */
2654 udelay(150);
2655
2656 if (IS_I965G(dev) && !IS_IGDNG(dev)) {
Zhao Yakuibb66c512009-09-10 15:45:49 +08002657 if (is_sdvo) {
2658 sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
2659 I915_WRITE(dpll_md_reg, (0 << DPLL_MD_UDI_DIVIDER_SHIFT) |
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002660 ((sdvo_pixel_multiply - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT));
Zhao Yakuibb66c512009-09-10 15:45:49 +08002661 } else
2662 I915_WRITE(dpll_md_reg, 0);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002663 } else {
2664 /* write it again -- the BIOS does, after all */
2665 I915_WRITE(dpll_reg, dpll);
2666 }
2667 I915_READ(dpll_reg);
2668 /* Wait for the clocks to stabilize. */
2669 udelay(150);
Jesse Barnes79e53942008-11-07 14:24:08 -08002670 }
Jesse Barnes79e53942008-11-07 14:24:08 -08002671
Jesse Barnes652c3932009-08-17 13:31:43 -07002672 if (is_lvds && has_reduced_clock && i915_powersave) {
2673 I915_WRITE(fp_reg + 4, fp2);
2674 intel_crtc->lowfreq_avail = true;
2675 if (HAS_PIPE_CXSR(dev)) {
2676 DRM_DEBUG("enabling CxSR downclocking\n");
2677 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
2678 }
2679 } else {
2680 I915_WRITE(fp_reg + 4, fp);
2681 intel_crtc->lowfreq_avail = false;
2682 if (HAS_PIPE_CXSR(dev)) {
2683 DRM_DEBUG("disabling CxSR downclocking\n");
2684 pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
2685 }
2686 }
2687
Jesse Barnes79e53942008-11-07 14:24:08 -08002688 I915_WRITE(htot_reg, (adjusted_mode->crtc_hdisplay - 1) |
2689 ((adjusted_mode->crtc_htotal - 1) << 16));
2690 I915_WRITE(hblank_reg, (adjusted_mode->crtc_hblank_start - 1) |
2691 ((adjusted_mode->crtc_hblank_end - 1) << 16));
2692 I915_WRITE(hsync_reg, (adjusted_mode->crtc_hsync_start - 1) |
2693 ((adjusted_mode->crtc_hsync_end - 1) << 16));
2694 I915_WRITE(vtot_reg, (adjusted_mode->crtc_vdisplay - 1) |
2695 ((adjusted_mode->crtc_vtotal - 1) << 16));
2696 I915_WRITE(vblank_reg, (adjusted_mode->crtc_vblank_start - 1) |
2697 ((adjusted_mode->crtc_vblank_end - 1) << 16));
2698 I915_WRITE(vsync_reg, (adjusted_mode->crtc_vsync_start - 1) |
2699 ((adjusted_mode->crtc_vsync_end - 1) << 16));
2700 /* pipesrc and dspsize control the size that is scaled from, which should
2701 * always be the user's requested size.
2702 */
Zhenyu Wang2c072452009-06-05 15:38:42 +08002703 if (!IS_IGDNG(dev)) {
2704 I915_WRITE(dspsize_reg, ((mode->vdisplay - 1) << 16) |
2705 (mode->hdisplay - 1));
2706 I915_WRITE(dsppos_reg, 0);
2707 }
Jesse Barnes79e53942008-11-07 14:24:08 -08002708 I915_WRITE(pipesrc_reg, ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
Zhenyu Wang2c072452009-06-05 15:38:42 +08002709
2710 if (IS_IGDNG(dev)) {
2711 I915_WRITE(data_m1_reg, TU_SIZE(m_n.tu) | m_n.gmch_m);
2712 I915_WRITE(data_n1_reg, TU_SIZE(m_n.tu) | m_n.gmch_n);
2713 I915_WRITE(link_m1_reg, m_n.link_m);
2714 I915_WRITE(link_n1_reg, m_n.link_n);
2715
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002716 if (is_edp) {
2717 igdng_set_pll_edp(crtc, adjusted_mode->clock);
2718 } else {
2719 /* enable FDI RX PLL too */
2720 temp = I915_READ(fdi_rx_reg);
2721 I915_WRITE(fdi_rx_reg, temp | FDI_RX_PLL_ENABLE);
2722 udelay(200);
2723 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08002724 }
2725
Jesse Barnes79e53942008-11-07 14:24:08 -08002726 I915_WRITE(pipeconf_reg, pipeconf);
2727 I915_READ(pipeconf_reg);
2728
2729 intel_wait_for_vblank(dev);
2730
Zhenyu Wang553bd142009-09-02 10:57:52 +08002731 if (IS_IGDNG(dev)) {
2732 /* enable address swizzle for tiling buffer */
2733 temp = I915_READ(DISP_ARB_CTL);
2734 I915_WRITE(DISP_ARB_CTL, temp | DISP_TILE_SURFACE_SWIZZLING);
2735 }
2736
Jesse Barnes79e53942008-11-07 14:24:08 -08002737 I915_WRITE(dspcntr_reg, dspcntr);
2738
2739 /* Flush the plane changes */
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002740 ret = intel_pipe_set_base(crtc, x, y, old_fb);
Shaohua Li7662c8b2009-06-26 11:23:55 +08002741
2742 intel_update_watermarks(dev);
2743
Jesse Barnes79e53942008-11-07 14:24:08 -08002744 drm_vblank_post_modeset(dev, pipe);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002745
Chris Wilson1f803ee2009-06-06 09:45:59 +01002746 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002747}
2748
2749/** Loads the palette/gamma unit for the CRTC with the prepared values */
2750void intel_crtc_load_lut(struct drm_crtc *crtc)
2751{
2752 struct drm_device *dev = crtc->dev;
2753 struct drm_i915_private *dev_priv = dev->dev_private;
2754 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2755 int palreg = (intel_crtc->pipe == 0) ? PALETTE_A : PALETTE_B;
2756 int i;
2757
2758 /* The clocks have to be on to load the palette. */
2759 if (!crtc->enabled)
2760 return;
2761
Zhenyu Wang2c072452009-06-05 15:38:42 +08002762 /* use legacy palette for IGDNG */
2763 if (IS_IGDNG(dev))
2764 palreg = (intel_crtc->pipe == 0) ? LGC_PALETTE_A :
2765 LGC_PALETTE_B;
2766
Jesse Barnes79e53942008-11-07 14:24:08 -08002767 for (i = 0; i < 256; i++) {
2768 I915_WRITE(palreg + 4 * i,
2769 (intel_crtc->lut_r[i] << 16) |
2770 (intel_crtc->lut_g[i] << 8) |
2771 intel_crtc->lut_b[i]);
2772 }
2773}
2774
2775static int intel_crtc_cursor_set(struct drm_crtc *crtc,
2776 struct drm_file *file_priv,
2777 uint32_t handle,
2778 uint32_t width, uint32_t height)
2779{
2780 struct drm_device *dev = crtc->dev;
2781 struct drm_i915_private *dev_priv = dev->dev_private;
2782 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2783 struct drm_gem_object *bo;
2784 struct drm_i915_gem_object *obj_priv;
2785 int pipe = intel_crtc->pipe;
2786 uint32_t control = (pipe == 0) ? CURACNTR : CURBCNTR;
2787 uint32_t base = (pipe == 0) ? CURABASE : CURBBASE;
Jesse Barnes14b60392009-05-20 16:47:08 -04002788 uint32_t temp = I915_READ(control);
Jesse Barnes79e53942008-11-07 14:24:08 -08002789 size_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05002790 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002791
2792 DRM_DEBUG("\n");
2793
2794 /* if we want to turn off the cursor ignore width and height */
2795 if (!handle) {
2796 DRM_DEBUG("cursor off\n");
Jesse Barnes14b60392009-05-20 16:47:08 -04002797 if (IS_MOBILE(dev) || IS_I9XX(dev)) {
2798 temp &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
2799 temp |= CURSOR_MODE_DISABLE;
2800 } else {
2801 temp &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
2802 }
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05002803 addr = 0;
2804 bo = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10002805 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05002806 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08002807 }
2808
2809 /* Currently we only support 64x64 cursors */
2810 if (width != 64 || height != 64) {
2811 DRM_ERROR("we currently only support 64x64 cursors\n");
2812 return -EINVAL;
2813 }
2814
2815 bo = drm_gem_object_lookup(dev, file_priv, handle);
2816 if (!bo)
2817 return -ENOENT;
2818
2819 obj_priv = bo->driver_private;
2820
2821 if (bo->size < width * height * 4) {
2822 DRM_ERROR("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10002823 ret = -ENOMEM;
2824 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08002825 }
2826
Dave Airlie71acb5e2008-12-30 20:31:46 +10002827 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05002828 mutex_lock(&dev->struct_mutex);
Dave Airlie71acb5e2008-12-30 20:31:46 +10002829 if (!dev_priv->cursor_needs_physical) {
2830 ret = i915_gem_object_pin(bo, PAGE_SIZE);
2831 if (ret) {
2832 DRM_ERROR("failed to pin cursor bo\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05002833 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10002834 }
Jesse Barnes79e53942008-11-07 14:24:08 -08002835 addr = obj_priv->gtt_offset;
Dave Airlie71acb5e2008-12-30 20:31:46 +10002836 } else {
2837 ret = i915_gem_attach_phys_object(dev, bo, (pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1);
2838 if (ret) {
2839 DRM_ERROR("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05002840 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10002841 }
2842 addr = obj_priv->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05002843 }
2844
Jesse Barnes14b60392009-05-20 16:47:08 -04002845 if (!IS_I9XX(dev))
2846 I915_WRITE(CURSIZE, (height << 12) | width);
2847
2848 /* Hooray for CUR*CNTR differences */
2849 if (IS_MOBILE(dev) || IS_I9XX(dev)) {
2850 temp &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
2851 temp |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
2852 temp |= (pipe << 28); /* Connect to correct pipe */
2853 } else {
2854 temp &= ~(CURSOR_FORMAT_MASK);
2855 temp |= CURSOR_ENABLE;
2856 temp |= CURSOR_FORMAT_ARGB | CURSOR_GAMMA_ENABLE;
2857 }
Jesse Barnes79e53942008-11-07 14:24:08 -08002858
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05002859 finish:
Jesse Barnes79e53942008-11-07 14:24:08 -08002860 I915_WRITE(control, temp);
2861 I915_WRITE(base, addr);
2862
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05002863 if (intel_crtc->cursor_bo) {
Dave Airlie71acb5e2008-12-30 20:31:46 +10002864 if (dev_priv->cursor_needs_physical) {
2865 if (intel_crtc->cursor_bo != bo)
2866 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
2867 } else
2868 i915_gem_object_unpin(intel_crtc->cursor_bo);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05002869 drm_gem_object_unreference(intel_crtc->cursor_bo);
2870 }
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05002871 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05002872
2873 intel_crtc->cursor_addr = addr;
2874 intel_crtc->cursor_bo = bo;
2875
Jesse Barnes79e53942008-11-07 14:24:08 -08002876 return 0;
Dave Airlie34b8686e2009-01-15 14:03:07 +10002877fail:
2878 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05002879fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10002880 drm_gem_object_unreference(bo);
2881 mutex_unlock(&dev->struct_mutex);
2882 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002883}
2884
2885static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
2886{
2887 struct drm_device *dev = crtc->dev;
2888 struct drm_i915_private *dev_priv = dev->dev_private;
2889 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07002890 struct intel_framebuffer *intel_fb;
Jesse Barnes79e53942008-11-07 14:24:08 -08002891 int pipe = intel_crtc->pipe;
2892 uint32_t temp = 0;
2893 uint32_t adder;
2894
Jesse Barnes652c3932009-08-17 13:31:43 -07002895 if (crtc->fb) {
2896 intel_fb = to_intel_framebuffer(crtc->fb);
2897 intel_mark_busy(dev, intel_fb->obj);
2898 }
2899
Jesse Barnes79e53942008-11-07 14:24:08 -08002900 if (x < 0) {
Keith Packard2245fda2009-05-30 20:42:29 -07002901 temp |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
Jesse Barnes79e53942008-11-07 14:24:08 -08002902 x = -x;
2903 }
2904 if (y < 0) {
Keith Packard2245fda2009-05-30 20:42:29 -07002905 temp |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
Jesse Barnes79e53942008-11-07 14:24:08 -08002906 y = -y;
2907 }
2908
Keith Packard2245fda2009-05-30 20:42:29 -07002909 temp |= x << CURSOR_X_SHIFT;
2910 temp |= y << CURSOR_Y_SHIFT;
Jesse Barnes79e53942008-11-07 14:24:08 -08002911
2912 adder = intel_crtc->cursor_addr;
2913 I915_WRITE((pipe == 0) ? CURAPOS : CURBPOS, temp);
2914 I915_WRITE((pipe == 0) ? CURABASE : CURBBASE, adder);
2915
2916 return 0;
2917}
2918
2919/** Sets the color ramps on behalf of RandR */
2920void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
2921 u16 blue, int regno)
2922{
2923 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2924
2925 intel_crtc->lut_r[regno] = red >> 8;
2926 intel_crtc->lut_g[regno] = green >> 8;
2927 intel_crtc->lut_b[regno] = blue >> 8;
2928}
2929
2930static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
2931 u16 *blue, uint32_t size)
2932{
2933 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2934 int i;
2935
2936 if (size != 256)
2937 return;
2938
2939 for (i = 0; i < 256; i++) {
2940 intel_crtc->lut_r[i] = red[i] >> 8;
2941 intel_crtc->lut_g[i] = green[i] >> 8;
2942 intel_crtc->lut_b[i] = blue[i] >> 8;
2943 }
2944
2945 intel_crtc_load_lut(crtc);
2946}
2947
2948/**
2949 * Get a pipe with a simple mode set on it for doing load-based monitor
2950 * detection.
2951 *
2952 * It will be up to the load-detect code to adjust the pipe as appropriate for
2953 * its requirements. The pipe will be connected to no other outputs.
2954 *
2955 * Currently this code will only succeed if there is a pipe with no outputs
2956 * configured for it. In the future, it could choose to temporarily disable
2957 * some outputs to free up a pipe for its use.
2958 *
2959 * \return crtc, or NULL if no pipes are available.
2960 */
2961
2962/* VESA 640x480x72Hz mode to set on the pipe */
2963static struct drm_display_mode load_detect_mode = {
2964 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
2965 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
2966};
2967
2968struct drm_crtc *intel_get_load_detect_pipe(struct intel_output *intel_output,
2969 struct drm_display_mode *mode,
2970 int *dpms_mode)
2971{
2972 struct intel_crtc *intel_crtc;
2973 struct drm_crtc *possible_crtc;
2974 struct drm_crtc *supported_crtc =NULL;
2975 struct drm_encoder *encoder = &intel_output->enc;
2976 struct drm_crtc *crtc = NULL;
2977 struct drm_device *dev = encoder->dev;
2978 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
2979 struct drm_crtc_helper_funcs *crtc_funcs;
2980 int i = -1;
2981
2982 /*
2983 * Algorithm gets a little messy:
2984 * - if the connector already has an assigned crtc, use it (but make
2985 * sure it's on first)
2986 * - try to find the first unused crtc that can drive this connector,
2987 * and use that if we find one
2988 * - if there are no unused crtcs available, try to use the first
2989 * one we found that supports the connector
2990 */
2991
2992 /* See if we already have a CRTC for this connector */
2993 if (encoder->crtc) {
2994 crtc = encoder->crtc;
2995 /* Make sure the crtc and connector are running */
2996 intel_crtc = to_intel_crtc(crtc);
2997 *dpms_mode = intel_crtc->dpms_mode;
2998 if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
2999 crtc_funcs = crtc->helper_private;
3000 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
3001 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
3002 }
3003 return crtc;
3004 }
3005
3006 /* Find an unused one (if possible) */
3007 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
3008 i++;
3009 if (!(encoder->possible_crtcs & (1 << i)))
3010 continue;
3011 if (!possible_crtc->enabled) {
3012 crtc = possible_crtc;
3013 break;
3014 }
3015 if (!supported_crtc)
3016 supported_crtc = possible_crtc;
3017 }
3018
3019 /*
3020 * If we didn't find an unused CRTC, don't use any.
3021 */
3022 if (!crtc) {
3023 return NULL;
3024 }
3025
3026 encoder->crtc = crtc;
Keith Packard03d60692009-06-05 18:19:56 -07003027 intel_output->base.encoder = encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08003028 intel_output->load_detect_temp = true;
3029
3030 intel_crtc = to_intel_crtc(crtc);
3031 *dpms_mode = intel_crtc->dpms_mode;
3032
3033 if (!crtc->enabled) {
3034 if (!mode)
3035 mode = &load_detect_mode;
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05003036 drm_crtc_helper_set_mode(crtc, mode, 0, 0, crtc->fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08003037 } else {
3038 if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
3039 crtc_funcs = crtc->helper_private;
3040 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
3041 }
3042
3043 /* Add this connector to the crtc */
3044 encoder_funcs->mode_set(encoder, &crtc->mode, &crtc->mode);
3045 encoder_funcs->commit(encoder);
3046 }
3047 /* let the connector get through one full cycle before testing */
3048 intel_wait_for_vblank(dev);
3049
3050 return crtc;
3051}
3052
3053void intel_release_load_detect_pipe(struct intel_output *intel_output, int dpms_mode)
3054{
3055 struct drm_encoder *encoder = &intel_output->enc;
3056 struct drm_device *dev = encoder->dev;
3057 struct drm_crtc *crtc = encoder->crtc;
3058 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
3059 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
3060
3061 if (intel_output->load_detect_temp) {
3062 encoder->crtc = NULL;
Keith Packard03d60692009-06-05 18:19:56 -07003063 intel_output->base.encoder = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08003064 intel_output->load_detect_temp = false;
3065 crtc->enabled = drm_helper_crtc_in_use(crtc);
3066 drm_helper_disable_unused_functions(dev);
3067 }
3068
3069 /* Switch crtc and output back off if necessary */
3070 if (crtc->enabled && dpms_mode != DRM_MODE_DPMS_ON) {
3071 if (encoder->crtc == crtc)
3072 encoder_funcs->dpms(encoder, dpms_mode);
3073 crtc_funcs->dpms(crtc, dpms_mode);
3074 }
3075}
3076
3077/* Returns the clock of the currently programmed mode of the given pipe. */
3078static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
3079{
3080 struct drm_i915_private *dev_priv = dev->dev_private;
3081 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3082 int pipe = intel_crtc->pipe;
3083 u32 dpll = I915_READ((pipe == 0) ? DPLL_A : DPLL_B);
3084 u32 fp;
3085 intel_clock_t clock;
3086
3087 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
3088 fp = I915_READ((pipe == 0) ? FPA0 : FPB0);
3089 else
3090 fp = I915_READ((pipe == 0) ? FPA1 : FPB1);
3091
3092 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08003093 if (IS_IGD(dev)) {
3094 clock.n = ffs((fp & FP_N_IGD_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
3095 clock.m2 = (fp & FP_M2_IGD_DIV_MASK) >> FP_M2_DIV_SHIFT;
3096 } else {
3097 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
3098 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
3099 }
3100
Jesse Barnes79e53942008-11-07 14:24:08 -08003101 if (IS_I9XX(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +08003102 if (IS_IGD(dev))
3103 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_IGD) >>
3104 DPLL_FPA01_P1_POST_DIV_SHIFT_IGD);
3105 else
3106 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08003107 DPLL_FPA01_P1_POST_DIV_SHIFT);
3108
3109 switch (dpll & DPLL_MODE_MASK) {
3110 case DPLLB_MODE_DAC_SERIAL:
3111 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
3112 5 : 10;
3113 break;
3114 case DPLLB_MODE_LVDS:
3115 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
3116 7 : 14;
3117 break;
3118 default:
3119 DRM_DEBUG("Unknown DPLL mode %08x in programmed "
3120 "mode\n", (int)(dpll & DPLL_MODE_MASK));
3121 return 0;
3122 }
3123
3124 /* XXX: Handle the 100Mhz refclk */
Shaohua Li21778322009-02-23 15:19:16 +08003125 intel_clock(dev, 96000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08003126 } else {
3127 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
3128
3129 if (is_lvds) {
3130 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
3131 DPLL_FPA01_P1_POST_DIV_SHIFT);
3132 clock.p2 = 14;
3133
3134 if ((dpll & PLL_REF_INPUT_MASK) ==
3135 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
3136 /* XXX: might not be 66MHz */
Shaohua Li21778322009-02-23 15:19:16 +08003137 intel_clock(dev, 66000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08003138 } else
Shaohua Li21778322009-02-23 15:19:16 +08003139 intel_clock(dev, 48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08003140 } else {
3141 if (dpll & PLL_P1_DIVIDE_BY_TWO)
3142 clock.p1 = 2;
3143 else {
3144 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
3145 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
3146 }
3147 if (dpll & PLL_P2_DIVIDE_BY_4)
3148 clock.p2 = 4;
3149 else
3150 clock.p2 = 2;
3151
Shaohua Li21778322009-02-23 15:19:16 +08003152 intel_clock(dev, 48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08003153 }
3154 }
3155
3156 /* XXX: It would be nice to validate the clocks, but we can't reuse
3157 * i830PllIsValid() because it relies on the xf86_config connector
3158 * configuration being accurate, which it isn't necessarily.
3159 */
3160
3161 return clock.dot;
3162}
3163
3164/** Returns the currently programmed mode of the given pipe. */
3165struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
3166 struct drm_crtc *crtc)
3167{
3168 struct drm_i915_private *dev_priv = dev->dev_private;
3169 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3170 int pipe = intel_crtc->pipe;
3171 struct drm_display_mode *mode;
3172 int htot = I915_READ((pipe == 0) ? HTOTAL_A : HTOTAL_B);
3173 int hsync = I915_READ((pipe == 0) ? HSYNC_A : HSYNC_B);
3174 int vtot = I915_READ((pipe == 0) ? VTOTAL_A : VTOTAL_B);
3175 int vsync = I915_READ((pipe == 0) ? VSYNC_A : VSYNC_B);
3176
3177 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
3178 if (!mode)
3179 return NULL;
3180
3181 mode->clock = intel_crtc_clock_get(dev, crtc);
3182 mode->hdisplay = (htot & 0xffff) + 1;
3183 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
3184 mode->hsync_start = (hsync & 0xffff) + 1;
3185 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
3186 mode->vdisplay = (vtot & 0xffff) + 1;
3187 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
3188 mode->vsync_start = (vsync & 0xffff) + 1;
3189 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
3190
3191 drm_mode_set_name(mode);
3192 drm_mode_set_crtcinfo(mode, 0);
3193
3194 return mode;
3195}
3196
Jesse Barnes652c3932009-08-17 13:31:43 -07003197#define GPU_IDLE_TIMEOUT 500 /* ms */
3198
3199/* When this timer fires, we've been idle for awhile */
3200static void intel_gpu_idle_timer(unsigned long arg)
3201{
3202 struct drm_device *dev = (struct drm_device *)arg;
3203 drm_i915_private_t *dev_priv = dev->dev_private;
3204
3205 DRM_DEBUG("idle timer fired, downclocking\n");
3206
3207 dev_priv->busy = false;
3208
Eric Anholt01dfba92009-09-06 15:18:53 -07003209 queue_work(dev_priv->wq, &dev_priv->idle_work);
Jesse Barnes652c3932009-08-17 13:31:43 -07003210}
3211
3212void intel_increase_renderclock(struct drm_device *dev, bool schedule)
3213{
3214 drm_i915_private_t *dev_priv = dev->dev_private;
3215
3216 if (IS_IGDNG(dev))
3217 return;
3218
3219 if (!dev_priv->render_reclock_avail) {
Eric Anholt67cf7812009-08-31 08:52:02 -07003220 DRM_DEBUG("not reclocking render clock\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07003221 return;
3222 }
3223
3224 /* Restore render clock frequency to original value */
3225 if (IS_G4X(dev) || IS_I9XX(dev))
3226 pci_write_config_word(dev->pdev, GCFGC, dev_priv->orig_clock);
3227 else if (IS_I85X(dev))
3228 pci_write_config_word(dev->pdev, HPLLCC, dev_priv->orig_clock);
3229 DRM_DEBUG("increasing render clock frequency\n");
3230
3231 /* Schedule downclock */
3232 if (schedule)
3233 mod_timer(&dev_priv->idle_timer, jiffies +
3234 msecs_to_jiffies(GPU_IDLE_TIMEOUT));
3235}
3236
3237void intel_decrease_renderclock(struct drm_device *dev)
3238{
3239 drm_i915_private_t *dev_priv = dev->dev_private;
3240
3241 if (IS_IGDNG(dev))
3242 return;
3243
3244 if (!dev_priv->render_reclock_avail) {
Eric Anholt67cf7812009-08-31 08:52:02 -07003245 DRM_DEBUG("not reclocking render clock\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07003246 return;
3247 }
3248
3249 if (IS_G4X(dev)) {
3250 u16 gcfgc;
3251
3252 /* Adjust render clock... */
3253 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
3254
3255 /* Down to minimum... */
3256 gcfgc &= ~GM45_GC_RENDER_CLOCK_MASK;
3257 gcfgc |= GM45_GC_RENDER_CLOCK_266_MHZ;
3258
3259 pci_write_config_word(dev->pdev, GCFGC, gcfgc);
3260 } else if (IS_I965G(dev)) {
3261 u16 gcfgc;
3262
3263 /* Adjust render clock... */
3264 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
3265
3266 /* Down to minimum... */
3267 gcfgc &= ~I965_GC_RENDER_CLOCK_MASK;
3268 gcfgc |= I965_GC_RENDER_CLOCK_267_MHZ;
3269
3270 pci_write_config_word(dev->pdev, GCFGC, gcfgc);
3271 } else if (IS_I945G(dev) || IS_I945GM(dev)) {
3272 u16 gcfgc;
3273
3274 /* Adjust render clock... */
3275 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
3276
3277 /* Down to minimum... */
3278 gcfgc &= ~I945_GC_RENDER_CLOCK_MASK;
3279 gcfgc |= I945_GC_RENDER_CLOCK_166_MHZ;
3280
3281 pci_write_config_word(dev->pdev, GCFGC, gcfgc);
3282 } else if (IS_I915G(dev)) {
3283 u16 gcfgc;
3284
3285 /* Adjust render clock... */
3286 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
3287
3288 /* Down to minimum... */
3289 gcfgc &= ~I915_GC_RENDER_CLOCK_MASK;
3290 gcfgc |= I915_GC_RENDER_CLOCK_166_MHZ;
3291
3292 pci_write_config_word(dev->pdev, GCFGC, gcfgc);
3293 } else if (IS_I85X(dev)) {
3294 u16 hpllcc;
3295
3296 /* Adjust render clock... */
3297 pci_read_config_word(dev->pdev, HPLLCC, &hpllcc);
3298
3299 /* Up to maximum... */
3300 hpllcc &= ~GC_CLOCK_CONTROL_MASK;
3301 hpllcc |= GC_CLOCK_133_200;
3302
3303 pci_write_config_word(dev->pdev, HPLLCC, hpllcc);
3304 }
3305 DRM_DEBUG("decreasing render clock frequency\n");
3306}
3307
3308/* Note that no increase function is needed for this - increase_renderclock()
3309 * will also rewrite these bits
3310 */
3311void intel_decrease_displayclock(struct drm_device *dev)
3312{
3313 if (IS_IGDNG(dev))
3314 return;
3315
3316 if (IS_I945G(dev) || IS_I945GM(dev) || IS_I915G(dev) ||
3317 IS_I915GM(dev)) {
3318 u16 gcfgc;
3319
3320 /* Adjust render clock... */
3321 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
3322
3323 /* Down to minimum... */
3324 gcfgc &= ~0xf0;
3325 gcfgc |= 0x80;
3326
3327 pci_write_config_word(dev->pdev, GCFGC, gcfgc);
3328 }
3329}
3330
3331#define CRTC_IDLE_TIMEOUT 1000 /* ms */
3332
3333static void intel_crtc_idle_timer(unsigned long arg)
3334{
3335 struct intel_crtc *intel_crtc = (struct intel_crtc *)arg;
3336 struct drm_crtc *crtc = &intel_crtc->base;
3337 drm_i915_private_t *dev_priv = crtc->dev->dev_private;
3338
3339 DRM_DEBUG("idle timer fired, downclocking\n");
3340
3341 intel_crtc->busy = false;
3342
Eric Anholt01dfba92009-09-06 15:18:53 -07003343 queue_work(dev_priv->wq, &dev_priv->idle_work);
Jesse Barnes652c3932009-08-17 13:31:43 -07003344}
3345
3346static void intel_increase_pllclock(struct drm_crtc *crtc, bool schedule)
3347{
3348 struct drm_device *dev = crtc->dev;
3349 drm_i915_private_t *dev_priv = dev->dev_private;
3350 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3351 int pipe = intel_crtc->pipe;
3352 int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
3353 int dpll = I915_READ(dpll_reg);
3354
3355 if (IS_IGDNG(dev))
3356 return;
3357
3358 if (!dev_priv->lvds_downclock_avail)
3359 return;
3360
3361 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
3362 DRM_DEBUG("upclocking LVDS\n");
3363
3364 /* Unlock panel regs */
3365 I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) | (0xabcd << 16));
3366
3367 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
3368 I915_WRITE(dpll_reg, dpll);
3369 dpll = I915_READ(dpll_reg);
3370 intel_wait_for_vblank(dev);
3371 dpll = I915_READ(dpll_reg);
3372 if (dpll & DISPLAY_RATE_SELECT_FPA1)
3373 DRM_DEBUG("failed to upclock LVDS!\n");
3374
3375 /* ...and lock them again */
3376 I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
3377 }
3378
3379 /* Schedule downclock */
3380 if (schedule)
3381 mod_timer(&intel_crtc->idle_timer, jiffies +
3382 msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
3383}
3384
3385static void intel_decrease_pllclock(struct drm_crtc *crtc)
3386{
3387 struct drm_device *dev = crtc->dev;
3388 drm_i915_private_t *dev_priv = dev->dev_private;
3389 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3390 int pipe = intel_crtc->pipe;
3391 int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
3392 int dpll = I915_READ(dpll_reg);
3393
3394 if (IS_IGDNG(dev))
3395 return;
3396
3397 if (!dev_priv->lvds_downclock_avail)
3398 return;
3399
3400 /*
3401 * Since this is called by a timer, we should never get here in
3402 * the manual case.
3403 */
3404 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
3405 DRM_DEBUG("downclocking LVDS\n");
3406
3407 /* Unlock panel regs */
3408 I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) | (0xabcd << 16));
3409
3410 dpll |= DISPLAY_RATE_SELECT_FPA1;
3411 I915_WRITE(dpll_reg, dpll);
3412 dpll = I915_READ(dpll_reg);
3413 intel_wait_for_vblank(dev);
3414 dpll = I915_READ(dpll_reg);
3415 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
3416 DRM_DEBUG("failed to downclock LVDS!\n");
3417
3418 /* ...and lock them again */
3419 I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
3420 }
3421
3422}
3423
3424/**
3425 * intel_idle_update - adjust clocks for idleness
3426 * @work: work struct
3427 *
3428 * Either the GPU or display (or both) went idle. Check the busy status
3429 * here and adjust the CRTC and GPU clocks as necessary.
3430 */
3431static void intel_idle_update(struct work_struct *work)
3432{
3433 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
3434 idle_work);
3435 struct drm_device *dev = dev_priv->dev;
3436 struct drm_crtc *crtc;
3437 struct intel_crtc *intel_crtc;
3438
3439 if (!i915_powersave)
3440 return;
3441
3442 mutex_lock(&dev->struct_mutex);
3443
3444 /* GPU isn't processing, downclock it. */
3445 if (!dev_priv->busy) {
3446 intel_decrease_renderclock(dev);
3447 intel_decrease_displayclock(dev);
3448 }
3449
3450 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3451 /* Skip inactive CRTCs */
3452 if (!crtc->fb)
3453 continue;
3454
3455 intel_crtc = to_intel_crtc(crtc);
3456 if (!intel_crtc->busy)
3457 intel_decrease_pllclock(crtc);
3458 }
3459
3460 mutex_unlock(&dev->struct_mutex);
3461}
3462
3463/**
3464 * intel_mark_busy - mark the GPU and possibly the display busy
3465 * @dev: drm device
3466 * @obj: object we're operating on
3467 *
3468 * Callers can use this function to indicate that the GPU is busy processing
3469 * commands. If @obj matches one of the CRTC objects (i.e. it's a scanout
3470 * buffer), we'll also mark the display as busy, so we know to increase its
3471 * clock frequency.
3472 */
3473void intel_mark_busy(struct drm_device *dev, struct drm_gem_object *obj)
3474{
3475 drm_i915_private_t *dev_priv = dev->dev_private;
3476 struct drm_crtc *crtc = NULL;
3477 struct intel_framebuffer *intel_fb;
3478 struct intel_crtc *intel_crtc;
3479
Zhenyu Wang5e17ee72009-09-03 09:30:06 +08003480 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3481 return;
3482
Jesse Barnes652c3932009-08-17 13:31:43 -07003483 dev_priv->busy = true;
3484 intel_increase_renderclock(dev, true);
3485
3486 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3487 if (!crtc->fb)
3488 continue;
3489
3490 intel_crtc = to_intel_crtc(crtc);
3491 intel_fb = to_intel_framebuffer(crtc->fb);
3492 if (intel_fb->obj == obj) {
3493 if (!intel_crtc->busy) {
3494 /* Non-busy -> busy, upclock */
3495 intel_increase_pllclock(crtc, true);
3496 intel_crtc->busy = true;
3497 } else {
3498 /* Busy -> busy, put off timer */
3499 mod_timer(&intel_crtc->idle_timer, jiffies +
3500 msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
3501 }
3502 }
3503 }
3504}
3505
Jesse Barnes79e53942008-11-07 14:24:08 -08003506static void intel_crtc_destroy(struct drm_crtc *crtc)
3507{
3508 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3509
3510 drm_crtc_cleanup(crtc);
3511 kfree(intel_crtc);
3512}
3513
3514static const struct drm_crtc_helper_funcs intel_helper_funcs = {
3515 .dpms = intel_crtc_dpms,
3516 .mode_fixup = intel_crtc_mode_fixup,
3517 .mode_set = intel_crtc_mode_set,
3518 .mode_set_base = intel_pipe_set_base,
3519 .prepare = intel_crtc_prepare,
3520 .commit = intel_crtc_commit,
3521};
3522
3523static const struct drm_crtc_funcs intel_crtc_funcs = {
3524 .cursor_set = intel_crtc_cursor_set,
3525 .cursor_move = intel_crtc_cursor_move,
3526 .gamma_set = intel_crtc_gamma_set,
3527 .set_config = drm_crtc_helper_set_config,
3528 .destroy = intel_crtc_destroy,
3529};
3530
3531
Hannes Ederb358d0a2008-12-18 21:18:47 +01003532static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -08003533{
3534 struct intel_crtc *intel_crtc;
3535 int i;
3536
3537 intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
3538 if (intel_crtc == NULL)
3539 return;
3540
3541 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
3542
3543 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
3544 intel_crtc->pipe = pipe;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003545 intel_crtc->plane = pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08003546 for (i = 0; i < 256; i++) {
3547 intel_crtc->lut_r[i] = i;
3548 intel_crtc->lut_g[i] = i;
3549 intel_crtc->lut_b[i] = i;
3550 }
3551
3552 intel_crtc->cursor_addr = 0;
3553 intel_crtc->dpms_mode = DRM_MODE_DPMS_OFF;
3554 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
3555
Jesse Barnes652c3932009-08-17 13:31:43 -07003556 intel_crtc->busy = false;
3557
3558 setup_timer(&intel_crtc->idle_timer, intel_crtc_idle_timer,
3559 (unsigned long)intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08003560}
3561
Carl Worth08d7b3d2009-04-29 14:43:54 -07003562int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
3563 struct drm_file *file_priv)
3564{
3565 drm_i915_private_t *dev_priv = dev->dev_private;
3566 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +02003567 struct drm_mode_object *drmmode_obj;
3568 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -07003569
3570 if (!dev_priv) {
3571 DRM_ERROR("called with no initialization\n");
3572 return -EINVAL;
3573 }
3574
Daniel Vetterc05422d2009-08-11 16:05:30 +02003575 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
3576 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -07003577
Daniel Vetterc05422d2009-08-11 16:05:30 +02003578 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -07003579 DRM_ERROR("no such CRTC id\n");
3580 return -EINVAL;
3581 }
3582
Daniel Vetterc05422d2009-08-11 16:05:30 +02003583 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
3584 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -07003585
Daniel Vetterc05422d2009-08-11 16:05:30 +02003586 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -07003587}
3588
Jesse Barnes79e53942008-11-07 14:24:08 -08003589struct drm_crtc *intel_get_crtc_from_pipe(struct drm_device *dev, int pipe)
3590{
3591 struct drm_crtc *crtc = NULL;
3592
3593 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3594 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3595 if (intel_crtc->pipe == pipe)
3596 break;
3597 }
3598 return crtc;
3599}
3600
Hannes Ederb358d0a2008-12-18 21:18:47 +01003601static int intel_connector_clones(struct drm_device *dev, int type_mask)
Jesse Barnes79e53942008-11-07 14:24:08 -08003602{
3603 int index_mask = 0;
3604 struct drm_connector *connector;
3605 int entry = 0;
3606
3607 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3608 struct intel_output *intel_output = to_intel_output(connector);
Ma Lingf8aed702009-08-24 13:50:24 +08003609 if (type_mask & intel_output->clone_mask)
Jesse Barnes79e53942008-11-07 14:24:08 -08003610 index_mask |= (1 << entry);
3611 entry++;
3612 }
3613 return index_mask;
3614}
3615
3616
3617static void intel_setup_outputs(struct drm_device *dev)
3618{
Eric Anholt725e30a2009-01-22 13:01:02 -08003619 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08003620 struct drm_connector *connector;
3621
3622 intel_crt_init(dev);
3623
3624 /* Set up integrated LVDS */
Zhenyu Wang541998a2009-06-05 15:38:44 +08003625 if (IS_MOBILE(dev) && !IS_I830(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08003626 intel_lvds_init(dev);
3627
Zhenyu Wang2c072452009-06-05 15:38:42 +08003628 if (IS_IGDNG(dev)) {
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08003629 int found;
3630
Zhenyu Wang32f9d652009-07-24 01:00:32 +08003631 if (IS_MOBILE(dev) && (I915_READ(DP_A) & DP_DETECTED))
3632 intel_dp_init(dev, DP_A);
3633
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08003634 if (I915_READ(HDMIB) & PORT_DETECTED) {
3635 /* check SDVOB */
3636 /* found = intel_sdvo_init(dev, HDMIB); */
3637 found = 0;
3638 if (!found)
3639 intel_hdmi_init(dev, HDMIB);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08003640 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
3641 intel_dp_init(dev, PCH_DP_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08003642 }
3643
3644 if (I915_READ(HDMIC) & PORT_DETECTED)
3645 intel_hdmi_init(dev, HDMIC);
3646
3647 if (I915_READ(HDMID) & PORT_DETECTED)
3648 intel_hdmi_init(dev, HDMID);
3649
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08003650 if (I915_READ(PCH_DP_C) & DP_DETECTED)
3651 intel_dp_init(dev, PCH_DP_C);
3652
3653 if (I915_READ(PCH_DP_D) & DP_DETECTED)
3654 intel_dp_init(dev, PCH_DP_D);
3655
Zhenyu Wang2c072452009-06-05 15:38:42 +08003656 } else if (IS_I9XX(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +08003657 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -08003658
Eric Anholt725e30a2009-01-22 13:01:02 -08003659 if (I915_READ(SDVOB) & SDVO_DETECTED) {
3660 found = intel_sdvo_init(dev, SDVOB);
3661 if (!found && SUPPORTS_INTEGRATED_HDMI(dev))
3662 intel_hdmi_init(dev, SDVOB);
Ma Ling27185ae2009-08-24 13:50:23 +08003663
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003664 if (!found && SUPPORTS_INTEGRATED_DP(dev))
3665 intel_dp_init(dev, DP_B);
Eric Anholt725e30a2009-01-22 13:01:02 -08003666 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -04003667
3668 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -04003669
Ma Ling27185ae2009-08-24 13:50:23 +08003670 if (I915_READ(SDVOB) & SDVO_DETECTED)
Eric Anholt725e30a2009-01-22 13:01:02 -08003671 found = intel_sdvo_init(dev, SDVOC);
Ma Ling27185ae2009-08-24 13:50:23 +08003672
3673 if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
3674
3675 if (SUPPORTS_INTEGRATED_HDMI(dev))
Eric Anholt725e30a2009-01-22 13:01:02 -08003676 intel_hdmi_init(dev, SDVOC);
Ma Ling27185ae2009-08-24 13:50:23 +08003677 if (SUPPORTS_INTEGRATED_DP(dev))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003678 intel_dp_init(dev, DP_C);
Eric Anholt725e30a2009-01-22 13:01:02 -08003679 }
Ma Ling27185ae2009-08-24 13:50:23 +08003680
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003681 if (SUPPORTS_INTEGRATED_DP(dev) && (I915_READ(DP_D) & DP_DETECTED))
3682 intel_dp_init(dev, DP_D);
Jesse Barnes79e53942008-11-07 14:24:08 -08003683 } else
3684 intel_dvo_init(dev);
3685
Zhenyu Wang2c072452009-06-05 15:38:42 +08003686 if (IS_I9XX(dev) && IS_MOBILE(dev) && !IS_IGDNG(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08003687 intel_tv_init(dev);
3688
3689 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3690 struct intel_output *intel_output = to_intel_output(connector);
3691 struct drm_encoder *encoder = &intel_output->enc;
Jesse Barnes79e53942008-11-07 14:24:08 -08003692
Ma Lingf8aed702009-08-24 13:50:24 +08003693 encoder->possible_crtcs = intel_output->crtc_mask;
3694 encoder->possible_clones = intel_connector_clones(dev,
3695 intel_output->clone_mask);
Jesse Barnes79e53942008-11-07 14:24:08 -08003696 }
3697}
3698
3699static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
3700{
3701 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
3702 struct drm_device *dev = fb->dev;
3703
3704 if (fb->fbdev)
3705 intelfb_remove(dev, fb);
3706
3707 drm_framebuffer_cleanup(fb);
3708 mutex_lock(&dev->struct_mutex);
3709 drm_gem_object_unreference(intel_fb->obj);
3710 mutex_unlock(&dev->struct_mutex);
3711
3712 kfree(intel_fb);
3713}
3714
3715static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
3716 struct drm_file *file_priv,
3717 unsigned int *handle)
3718{
3719 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
3720 struct drm_gem_object *object = intel_fb->obj;
3721
3722 return drm_gem_handle_create(file_priv, object, handle);
3723}
3724
3725static const struct drm_framebuffer_funcs intel_fb_funcs = {
3726 .destroy = intel_user_framebuffer_destroy,
3727 .create_handle = intel_user_framebuffer_create_handle,
3728};
3729
3730int intel_framebuffer_create(struct drm_device *dev,
3731 struct drm_mode_fb_cmd *mode_cmd,
3732 struct drm_framebuffer **fb,
3733 struct drm_gem_object *obj)
3734{
3735 struct intel_framebuffer *intel_fb;
3736 int ret;
3737
3738 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
3739 if (!intel_fb)
3740 return -ENOMEM;
3741
3742 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
3743 if (ret) {
3744 DRM_ERROR("framebuffer init failed %d\n", ret);
3745 return ret;
3746 }
3747
3748 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
3749
3750 intel_fb->obj = obj;
3751
3752 *fb = &intel_fb->base;
3753
3754 return 0;
3755}
3756
3757
3758static struct drm_framebuffer *
3759intel_user_framebuffer_create(struct drm_device *dev,
3760 struct drm_file *filp,
3761 struct drm_mode_fb_cmd *mode_cmd)
3762{
3763 struct drm_gem_object *obj;
3764 struct drm_framebuffer *fb;
3765 int ret;
3766
3767 obj = drm_gem_object_lookup(dev, filp, mode_cmd->handle);
3768 if (!obj)
3769 return NULL;
3770
3771 ret = intel_framebuffer_create(dev, mode_cmd, &fb, obj);
3772 if (ret) {
Jesse Barnes496818f2009-02-11 13:28:14 -08003773 mutex_lock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08003774 drm_gem_object_unreference(obj);
Jesse Barnes496818f2009-02-11 13:28:14 -08003775 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08003776 return NULL;
3777 }
3778
3779 return fb;
3780}
3781
Jesse Barnes79e53942008-11-07 14:24:08 -08003782static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -08003783 .fb_create = intel_user_framebuffer_create,
3784 .fb_changed = intelfb_probe,
3785};
3786
Jesse Barnes652c3932009-08-17 13:31:43 -07003787void intel_init_clock_gating(struct drm_device *dev)
3788{
3789 struct drm_i915_private *dev_priv = dev->dev_private;
3790
3791 /*
3792 * Disable clock gating reported to work incorrectly according to the
3793 * specs, but enable as much else as we can.
3794 */
3795 if (IS_G4X(dev)) {
3796 uint32_t dspclk_gate;
3797 I915_WRITE(RENCLK_GATE_D1, 0);
3798 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
3799 GS_UNIT_CLOCK_GATE_DISABLE |
3800 CL_UNIT_CLOCK_GATE_DISABLE);
3801 I915_WRITE(RAMCLK_GATE_D, 0);
3802 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
3803 OVRUNIT_CLOCK_GATE_DISABLE |
3804 OVCUNIT_CLOCK_GATE_DISABLE;
3805 if (IS_GM45(dev))
3806 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
3807 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
3808 } else if (IS_I965GM(dev)) {
3809 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
3810 I915_WRITE(RENCLK_GATE_D2, 0);
3811 I915_WRITE(DSPCLK_GATE_D, 0);
3812 I915_WRITE(RAMCLK_GATE_D, 0);
3813 I915_WRITE16(DEUC, 0);
3814 } else if (IS_I965G(dev)) {
3815 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
3816 I965_RCC_CLOCK_GATE_DISABLE |
3817 I965_RCPB_CLOCK_GATE_DISABLE |
3818 I965_ISC_CLOCK_GATE_DISABLE |
3819 I965_FBC_CLOCK_GATE_DISABLE);
3820 I915_WRITE(RENCLK_GATE_D2, 0);
3821 } else if (IS_I9XX(dev)) {
3822 u32 dstate = I915_READ(D_STATE);
3823
3824 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
3825 DSTATE_DOT_CLOCK_GATING;
3826 I915_WRITE(D_STATE, dstate);
3827 } else if (IS_I855(dev) || IS_I865G(dev)) {
3828 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
3829 } else if (IS_I830(dev)) {
3830 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
3831 }
3832}
3833
Jesse Barnes79e53942008-11-07 14:24:08 -08003834void intel_modeset_init(struct drm_device *dev)
3835{
Jesse Barnes652c3932009-08-17 13:31:43 -07003836 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08003837 int num_pipe;
3838 int i;
3839
3840 drm_mode_config_init(dev);
3841
3842 dev->mode_config.min_width = 0;
3843 dev->mode_config.min_height = 0;
3844
3845 dev->mode_config.funcs = (void *)&intel_mode_funcs;
3846
3847 if (IS_I965G(dev)) {
3848 dev->mode_config.max_width = 8192;
3849 dev->mode_config.max_height = 8192;
Keith Packard5e4d6fa2009-07-12 23:53:17 -07003850 } else if (IS_I9XX(dev)) {
3851 dev->mode_config.max_width = 4096;
3852 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -08003853 } else {
3854 dev->mode_config.max_width = 2048;
3855 dev->mode_config.max_height = 2048;
3856 }
3857
3858 /* set memory base */
3859 if (IS_I9XX(dev))
3860 dev->mode_config.fb_base = pci_resource_start(dev->pdev, 2);
3861 else
3862 dev->mode_config.fb_base = pci_resource_start(dev->pdev, 0);
3863
3864 if (IS_MOBILE(dev) || IS_I9XX(dev))
3865 num_pipe = 2;
3866 else
3867 num_pipe = 1;
3868 DRM_DEBUG("%d display pipe%s available.\n",
3869 num_pipe, num_pipe > 1 ? "s" : "");
3870
Jesse Barnes652c3932009-08-17 13:31:43 -07003871 if (IS_I85X(dev))
3872 pci_read_config_word(dev->pdev, HPLLCC, &dev_priv->orig_clock);
3873 else if (IS_I9XX(dev) || IS_G4X(dev))
3874 pci_read_config_word(dev->pdev, GCFGC, &dev_priv->orig_clock);
3875
Jesse Barnes79e53942008-11-07 14:24:08 -08003876 for (i = 0; i < num_pipe; i++) {
3877 intel_crtc_init(dev, i);
3878 }
3879
3880 intel_setup_outputs(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -07003881
3882 intel_init_clock_gating(dev);
3883
3884 INIT_WORK(&dev_priv->idle_work, intel_idle_update);
3885 setup_timer(&dev_priv->idle_timer, intel_gpu_idle_timer,
3886 (unsigned long)dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08003887}
3888
3889void intel_modeset_cleanup(struct drm_device *dev)
3890{
Jesse Barnes652c3932009-08-17 13:31:43 -07003891 struct drm_i915_private *dev_priv = dev->dev_private;
3892 struct drm_crtc *crtc;
3893 struct intel_crtc *intel_crtc;
3894
3895 mutex_lock(&dev->struct_mutex);
3896
3897 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3898 /* Skip inactive CRTCs */
3899 if (!crtc->fb)
3900 continue;
3901
3902 intel_crtc = to_intel_crtc(crtc);
3903 intel_increase_pllclock(crtc, false);
3904 del_timer_sync(&intel_crtc->idle_timer);
3905 }
3906
3907 intel_increase_renderclock(dev, false);
3908 del_timer_sync(&dev_priv->idle_timer);
3909
3910 mutex_unlock(&dev->struct_mutex);
3911
Jesse Barnes79e53942008-11-07 14:24:08 -08003912 drm_mode_config_cleanup(dev);
3913}
3914
3915
3916/* current intel driver doesn't take advantage of encoders
3917 always give back the encoder for the connector
3918*/
3919struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
3920{
3921 struct intel_output *intel_output = to_intel_output(connector);
3922
3923 return &intel_output->enc;
3924}