]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/intel_display.c
drm: introduce drm_gem_object_[handle_]unreference_unlocked
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / i915 / intel_display.c
CommitLineData
79e53945
JB
1/*
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
c1c7af60
JB
27#include <linux/module.h>
28#include <linux/input.h>
79e53945 29#include <linux/i2c.h>
7662c8bd 30#include <linux/kernel.h>
79e53945
JB
31#include "drmP.h"
32#include "intel_drv.h"
33#include "i915_drm.h"
34#include "i915_drv.h"
ab2c0672 35#include "drm_dp_helper.h"
79e53945
JB
36
37#include "drm_crtc_helper.h"
38
32f9d658
ZW
39#define HAS_eDP (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
40
79e53945 41bool intel_pipe_has_type (struct drm_crtc *crtc, int type);
7662c8bd 42static void intel_update_watermarks(struct drm_device *dev);
652c393a 43static void intel_increase_pllclock(struct drm_crtc *crtc, bool schedule);
79e53945
JB
44
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
d4906093
ML
67typedef struct intel_limit intel_limit_t;
68struct intel_limit {
79e53945
JB
69 intel_range_t dot, vco, n, m, m1, m2, p, p1;
70 intel_p2_t p2;
d4906093
ML
71 bool (* find_pll)(const intel_limit_t *, struct drm_crtc *,
72 int, int, intel_clock_t *);
73};
79e53945
JB
74
75#define I8XX_DOT_MIN 25000
76#define I8XX_DOT_MAX 350000
77#define I8XX_VCO_MIN 930000
78#define I8XX_VCO_MAX 1400000
79#define I8XX_N_MIN 3
80#define I8XX_N_MAX 16
81#define I8XX_M_MIN 96
82#define I8XX_M_MAX 140
83#define I8XX_M1_MIN 18
84#define I8XX_M1_MAX 26
85#define I8XX_M2_MIN 6
86#define I8XX_M2_MAX 16
87#define I8XX_P_MIN 4
88#define I8XX_P_MAX 128
89#define I8XX_P1_MIN 2
90#define I8XX_P1_MAX 33
91#define I8XX_P1_LVDS_MIN 1
92#define I8XX_P1_LVDS_MAX 6
93#define I8XX_P2_SLOW 4
94#define I8XX_P2_FAST 2
95#define I8XX_P2_LVDS_SLOW 14
0c2e3952 96#define I8XX_P2_LVDS_FAST 7
79e53945
JB
97#define I8XX_P2_SLOW_LIMIT 165000
98
99#define I9XX_DOT_MIN 20000
100#define I9XX_DOT_MAX 400000
101#define I9XX_VCO_MIN 1400000
102#define I9XX_VCO_MAX 2800000
f2b115e6
AJ
103#define PINEVIEW_VCO_MIN 1700000
104#define PINEVIEW_VCO_MAX 3500000
f3cade5c
KH
105#define I9XX_N_MIN 1
106#define I9XX_N_MAX 6
f2b115e6
AJ
107/* Pineview's Ncounter is a ring counter */
108#define PINEVIEW_N_MIN 3
109#define PINEVIEW_N_MAX 6
79e53945
JB
110#define I9XX_M_MIN 70
111#define I9XX_M_MAX 120
f2b115e6
AJ
112#define PINEVIEW_M_MIN 2
113#define PINEVIEW_M_MAX 256
79e53945 114#define I9XX_M1_MIN 10
f3cade5c 115#define I9XX_M1_MAX 22
79e53945
JB
116#define I9XX_M2_MIN 5
117#define I9XX_M2_MAX 9
f2b115e6
AJ
118/* Pineview M1 is reserved, and must be 0 */
119#define PINEVIEW_M1_MIN 0
120#define PINEVIEW_M1_MAX 0
121#define PINEVIEW_M2_MIN 0
122#define PINEVIEW_M2_MAX 254
79e53945
JB
123#define I9XX_P_SDVO_DAC_MIN 5
124#define I9XX_P_SDVO_DAC_MAX 80
125#define I9XX_P_LVDS_MIN 7
126#define I9XX_P_LVDS_MAX 98
f2b115e6
AJ
127#define PINEVIEW_P_LVDS_MIN 7
128#define PINEVIEW_P_LVDS_MAX 112
79e53945
JB
129#define I9XX_P1_MIN 1
130#define I9XX_P1_MAX 8
131#define I9XX_P2_SDVO_DAC_SLOW 10
132#define I9XX_P2_SDVO_DAC_FAST 5
133#define I9XX_P2_SDVO_DAC_SLOW_LIMIT 200000
134#define I9XX_P2_LVDS_SLOW 14
135#define I9XX_P2_LVDS_FAST 7
136#define I9XX_P2_LVDS_SLOW_LIMIT 112000
137
044c7c41
ML
138/*The parameter is for SDVO on G4x platform*/
139#define G4X_DOT_SDVO_MIN 25000
140#define G4X_DOT_SDVO_MAX 270000
141#define G4X_VCO_MIN 1750000
142#define G4X_VCO_MAX 3500000
143#define G4X_N_SDVO_MIN 1
144#define G4X_N_SDVO_MAX 4
145#define G4X_M_SDVO_MIN 104
146#define G4X_M_SDVO_MAX 138
147#define G4X_M1_SDVO_MIN 17
148#define G4X_M1_SDVO_MAX 23
149#define G4X_M2_SDVO_MIN 5
150#define G4X_M2_SDVO_MAX 11
151#define G4X_P_SDVO_MIN 10
152#define G4X_P_SDVO_MAX 30
153#define G4X_P1_SDVO_MIN 1
154#define G4X_P1_SDVO_MAX 3
155#define G4X_P2_SDVO_SLOW 10
156#define G4X_P2_SDVO_FAST 10
157#define G4X_P2_SDVO_LIMIT 270000
158
159/*The parameter is for HDMI_DAC on G4x platform*/
160#define G4X_DOT_HDMI_DAC_MIN 22000
161#define G4X_DOT_HDMI_DAC_MAX 400000
162#define G4X_N_HDMI_DAC_MIN 1
163#define G4X_N_HDMI_DAC_MAX 4
164#define G4X_M_HDMI_DAC_MIN 104
165#define G4X_M_HDMI_DAC_MAX 138
166#define G4X_M1_HDMI_DAC_MIN 16
167#define G4X_M1_HDMI_DAC_MAX 23
168#define G4X_M2_HDMI_DAC_MIN 5
169#define G4X_M2_HDMI_DAC_MAX 11
170#define G4X_P_HDMI_DAC_MIN 5
171#define G4X_P_HDMI_DAC_MAX 80
172#define G4X_P1_HDMI_DAC_MIN 1
173#define G4X_P1_HDMI_DAC_MAX 8
174#define G4X_P2_HDMI_DAC_SLOW 10
175#define G4X_P2_HDMI_DAC_FAST 5
176#define G4X_P2_HDMI_DAC_LIMIT 165000
177
178/*The parameter is for SINGLE_CHANNEL_LVDS on G4x platform*/
179#define G4X_DOT_SINGLE_CHANNEL_LVDS_MIN 20000
180#define G4X_DOT_SINGLE_CHANNEL_LVDS_MAX 115000
181#define G4X_N_SINGLE_CHANNEL_LVDS_MIN 1
182#define G4X_N_SINGLE_CHANNEL_LVDS_MAX 3
183#define G4X_M_SINGLE_CHANNEL_LVDS_MIN 104
184#define G4X_M_SINGLE_CHANNEL_LVDS_MAX 138
185#define G4X_M1_SINGLE_CHANNEL_LVDS_MIN 17
186#define G4X_M1_SINGLE_CHANNEL_LVDS_MAX 23
187#define G4X_M2_SINGLE_CHANNEL_LVDS_MIN 5
188#define G4X_M2_SINGLE_CHANNEL_LVDS_MAX 11
189#define G4X_P_SINGLE_CHANNEL_LVDS_MIN 28
190#define G4X_P_SINGLE_CHANNEL_LVDS_MAX 112
191#define G4X_P1_SINGLE_CHANNEL_LVDS_MIN 2
192#define G4X_P1_SINGLE_CHANNEL_LVDS_MAX 8
193#define G4X_P2_SINGLE_CHANNEL_LVDS_SLOW 14
194#define G4X_P2_SINGLE_CHANNEL_LVDS_FAST 14
195#define G4X_P2_SINGLE_CHANNEL_LVDS_LIMIT 0
196
197/*The parameter is for DUAL_CHANNEL_LVDS on G4x platform*/
198#define G4X_DOT_DUAL_CHANNEL_LVDS_MIN 80000
199#define G4X_DOT_DUAL_CHANNEL_LVDS_MAX 224000
200#define G4X_N_DUAL_CHANNEL_LVDS_MIN 1
201#define G4X_N_DUAL_CHANNEL_LVDS_MAX 3
202#define G4X_M_DUAL_CHANNEL_LVDS_MIN 104
203#define G4X_M_DUAL_CHANNEL_LVDS_MAX 138
204#define G4X_M1_DUAL_CHANNEL_LVDS_MIN 17
205#define G4X_M1_DUAL_CHANNEL_LVDS_MAX 23
206#define G4X_M2_DUAL_CHANNEL_LVDS_MIN 5
207#define G4X_M2_DUAL_CHANNEL_LVDS_MAX 11
208#define G4X_P_DUAL_CHANNEL_LVDS_MIN 14
209#define G4X_P_DUAL_CHANNEL_LVDS_MAX 42
210#define G4X_P1_DUAL_CHANNEL_LVDS_MIN 2
211#define G4X_P1_DUAL_CHANNEL_LVDS_MAX 6
212#define G4X_P2_DUAL_CHANNEL_LVDS_SLOW 7
213#define G4X_P2_DUAL_CHANNEL_LVDS_FAST 7
214#define G4X_P2_DUAL_CHANNEL_LVDS_LIMIT 0
215
a4fc5ed6
KP
216/*The parameter is for DISPLAY PORT on G4x platform*/
217#define G4X_DOT_DISPLAY_PORT_MIN 161670
218#define G4X_DOT_DISPLAY_PORT_MAX 227000
219#define G4X_N_DISPLAY_PORT_MIN 1
220#define G4X_N_DISPLAY_PORT_MAX 2
221#define G4X_M_DISPLAY_PORT_MIN 97
222#define G4X_M_DISPLAY_PORT_MAX 108
223#define G4X_M1_DISPLAY_PORT_MIN 0x10
224#define G4X_M1_DISPLAY_PORT_MAX 0x12
225#define G4X_M2_DISPLAY_PORT_MIN 0x05
226#define G4X_M2_DISPLAY_PORT_MAX 0x06
227#define G4X_P_DISPLAY_PORT_MIN 10
228#define G4X_P_DISPLAY_PORT_MAX 20
229#define G4X_P1_DISPLAY_PORT_MIN 1
230#define G4X_P1_DISPLAY_PORT_MAX 2
231#define G4X_P2_DISPLAY_PORT_SLOW 10
232#define G4X_P2_DISPLAY_PORT_FAST 10
233#define G4X_P2_DISPLAY_PORT_LIMIT 0
234
f2b115e6 235/* Ironlake */
2c07245f
ZW
236/* as we calculate clock using (register_value + 2) for
237 N/M1/M2, so here the range value for them is (actual_value-2).
238 */
f2b115e6
AJ
239#define IRONLAKE_DOT_MIN 25000
240#define IRONLAKE_DOT_MAX 350000
241#define IRONLAKE_VCO_MIN 1760000
242#define IRONLAKE_VCO_MAX 3510000
243#define IRONLAKE_N_MIN 1
a59e385e 244#define IRONLAKE_N_MAX 6
f2b115e6 245#define IRONLAKE_M_MIN 79
a59e385e 246#define IRONLAKE_M_MAX 127
f2b115e6 247#define IRONLAKE_M1_MIN 12
a59e385e 248#define IRONLAKE_M1_MAX 22
f2b115e6
AJ
249#define IRONLAKE_M2_MIN 5
250#define IRONLAKE_M2_MAX 9
251#define IRONLAKE_P_SDVO_DAC_MIN 5
252#define IRONLAKE_P_SDVO_DAC_MAX 80
253#define IRONLAKE_P_LVDS_MIN 28
254#define IRONLAKE_P_LVDS_MAX 112
255#define IRONLAKE_P1_MIN 1
256#define IRONLAKE_P1_MAX 8
257#define IRONLAKE_P2_SDVO_DAC_SLOW 10
258#define IRONLAKE_P2_SDVO_DAC_FAST 5
259#define IRONLAKE_P2_LVDS_SLOW 14 /* single channel */
260#define IRONLAKE_P2_LVDS_FAST 7 /* double channel */
261#define IRONLAKE_P2_DOT_LIMIT 225000 /* 225Mhz */
2c07245f 262
4547668a
ZY
263#define IRONLAKE_P_DISPLAY_PORT_MIN 10
264#define IRONLAKE_P_DISPLAY_PORT_MAX 20
265#define IRONLAKE_P2_DISPLAY_PORT_FAST 10
266#define IRONLAKE_P2_DISPLAY_PORT_SLOW 10
267#define IRONLAKE_P2_DISPLAY_PORT_LIMIT 0
268#define IRONLAKE_P1_DISPLAY_PORT_MIN 1
269#define IRONLAKE_P1_DISPLAY_PORT_MAX 2
270
d4906093
ML
271static bool
272intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
273 int target, int refclk, intel_clock_t *best_clock);
274static bool
275intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
276 int target, int refclk, intel_clock_t *best_clock);
79e53945 277
a4fc5ed6
KP
278static bool
279intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
280 int target, int refclk, intel_clock_t *best_clock);
5eb08b69 281static bool
f2b115e6
AJ
282intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,
283 int target, int refclk, intel_clock_t *best_clock);
a4fc5ed6 284
e4b36699 285static const intel_limit_t intel_limits_i8xx_dvo = {
79e53945
JB
286 .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 },
d4906093 296 .find_pll = intel_find_best_PLL,
e4b36699
KP
297};
298
299static const intel_limit_t intel_limits_i8xx_lvds = {
79e53945
JB
300 .dot = { .min = I8XX_DOT_MIN, .max = I8XX_DOT_MAX },
301 .vco = { .min = I8XX_VCO_MIN, .max = I8XX_VCO_MAX },
302 .n = { .min = I8XX_N_MIN, .max = I8XX_N_MAX },
303 .m = { .min = I8XX_M_MIN, .max = I8XX_M_MAX },
304 .m1 = { .min = I8XX_M1_MIN, .max = I8XX_M1_MAX },
305 .m2 = { .min = I8XX_M2_MIN, .max = I8XX_M2_MAX },
306 .p = { .min = I8XX_P_MIN, .max = I8XX_P_MAX },
307 .p1 = { .min = I8XX_P1_LVDS_MIN, .max = I8XX_P1_LVDS_MAX },
308 .p2 = { .dot_limit = I8XX_P2_SLOW_LIMIT,
309 .p2_slow = I8XX_P2_LVDS_SLOW, .p2_fast = I8XX_P2_LVDS_FAST },
d4906093 310 .find_pll = intel_find_best_PLL,
e4b36699
KP
311};
312
313static const intel_limit_t intel_limits_i9xx_sdvo = {
79e53945
JB
314 .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
315 .vco = { .min = I9XX_VCO_MIN, .max = I9XX_VCO_MAX },
316 .n = { .min = I9XX_N_MIN, .max = I9XX_N_MAX },
317 .m = { .min = I9XX_M_MIN, .max = I9XX_M_MAX },
318 .m1 = { .min = I9XX_M1_MIN, .max = I9XX_M1_MAX },
319 .m2 = { .min = I9XX_M2_MIN, .max = I9XX_M2_MAX },
320 .p = { .min = I9XX_P_SDVO_DAC_MIN, .max = I9XX_P_SDVO_DAC_MAX },
321 .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
322 .p2 = { .dot_limit = I9XX_P2_SDVO_DAC_SLOW_LIMIT,
323 .p2_slow = I9XX_P2_SDVO_DAC_SLOW, .p2_fast = I9XX_P2_SDVO_DAC_FAST },
d4906093 324 .find_pll = intel_find_best_PLL,
e4b36699
KP
325};
326
327static const intel_limit_t intel_limits_i9xx_lvds = {
79e53945
JB
328 .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
329 .vco = { .min = I9XX_VCO_MIN, .max = I9XX_VCO_MAX },
330 .n = { .min = I9XX_N_MIN, .max = I9XX_N_MAX },
331 .m = { .min = I9XX_M_MIN, .max = I9XX_M_MAX },
332 .m1 = { .min = I9XX_M1_MIN, .max = I9XX_M1_MAX },
333 .m2 = { .min = I9XX_M2_MIN, .max = I9XX_M2_MAX },
334 .p = { .min = I9XX_P_LVDS_MIN, .max = I9XX_P_LVDS_MAX },
335 .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
336 /* The single-channel range is 25-112Mhz, and dual-channel
337 * is 80-224Mhz. Prefer single channel as much as possible.
338 */
339 .p2 = { .dot_limit = I9XX_P2_LVDS_SLOW_LIMIT,
340 .p2_slow = I9XX_P2_LVDS_SLOW, .p2_fast = I9XX_P2_LVDS_FAST },
d4906093 341 .find_pll = intel_find_best_PLL,
e4b36699
KP
342};
343
044c7c41 344 /* below parameter and function is for G4X Chipset Family*/
e4b36699 345static const intel_limit_t intel_limits_g4x_sdvo = {
044c7c41
ML
346 .dot = { .min = G4X_DOT_SDVO_MIN, .max = G4X_DOT_SDVO_MAX },
347 .vco = { .min = G4X_VCO_MIN, .max = G4X_VCO_MAX},
348 .n = { .min = G4X_N_SDVO_MIN, .max = G4X_N_SDVO_MAX },
349 .m = { .min = G4X_M_SDVO_MIN, .max = G4X_M_SDVO_MAX },
350 .m1 = { .min = G4X_M1_SDVO_MIN, .max = G4X_M1_SDVO_MAX },
351 .m2 = { .min = G4X_M2_SDVO_MIN, .max = G4X_M2_SDVO_MAX },
352 .p = { .min = G4X_P_SDVO_MIN, .max = G4X_P_SDVO_MAX },
353 .p1 = { .min = G4X_P1_SDVO_MIN, .max = G4X_P1_SDVO_MAX},
354 .p2 = { .dot_limit = G4X_P2_SDVO_LIMIT,
355 .p2_slow = G4X_P2_SDVO_SLOW,
356 .p2_fast = G4X_P2_SDVO_FAST
357 },
d4906093 358 .find_pll = intel_g4x_find_best_PLL,
e4b36699
KP
359};
360
361static const intel_limit_t intel_limits_g4x_hdmi = {
044c7c41
ML
362 .dot = { .min = G4X_DOT_HDMI_DAC_MIN, .max = G4X_DOT_HDMI_DAC_MAX },
363 .vco = { .min = G4X_VCO_MIN, .max = G4X_VCO_MAX},
364 .n = { .min = G4X_N_HDMI_DAC_MIN, .max = G4X_N_HDMI_DAC_MAX },
365 .m = { .min = G4X_M_HDMI_DAC_MIN, .max = G4X_M_HDMI_DAC_MAX },
366 .m1 = { .min = G4X_M1_HDMI_DAC_MIN, .max = G4X_M1_HDMI_DAC_MAX },
367 .m2 = { .min = G4X_M2_HDMI_DAC_MIN, .max = G4X_M2_HDMI_DAC_MAX },
368 .p = { .min = G4X_P_HDMI_DAC_MIN, .max = G4X_P_HDMI_DAC_MAX },
369 .p1 = { .min = G4X_P1_HDMI_DAC_MIN, .max = G4X_P1_HDMI_DAC_MAX},
370 .p2 = { .dot_limit = G4X_P2_HDMI_DAC_LIMIT,
371 .p2_slow = G4X_P2_HDMI_DAC_SLOW,
372 .p2_fast = G4X_P2_HDMI_DAC_FAST
373 },
d4906093 374 .find_pll = intel_g4x_find_best_PLL,
e4b36699
KP
375};
376
377static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
044c7c41
ML
378 .dot = { .min = G4X_DOT_SINGLE_CHANNEL_LVDS_MIN,
379 .max = G4X_DOT_SINGLE_CHANNEL_LVDS_MAX },
380 .vco = { .min = G4X_VCO_MIN,
381 .max = G4X_VCO_MAX },
382 .n = { .min = G4X_N_SINGLE_CHANNEL_LVDS_MIN,
383 .max = G4X_N_SINGLE_CHANNEL_LVDS_MAX },
384 .m = { .min = G4X_M_SINGLE_CHANNEL_LVDS_MIN,
385 .max = G4X_M_SINGLE_CHANNEL_LVDS_MAX },
386 .m1 = { .min = G4X_M1_SINGLE_CHANNEL_LVDS_MIN,
387 .max = G4X_M1_SINGLE_CHANNEL_LVDS_MAX },
388 .m2 = { .min = G4X_M2_SINGLE_CHANNEL_LVDS_MIN,
389 .max = G4X_M2_SINGLE_CHANNEL_LVDS_MAX },
390 .p = { .min = G4X_P_SINGLE_CHANNEL_LVDS_MIN,
391 .max = G4X_P_SINGLE_CHANNEL_LVDS_MAX },
392 .p1 = { .min = G4X_P1_SINGLE_CHANNEL_LVDS_MIN,
393 .max = G4X_P1_SINGLE_CHANNEL_LVDS_MAX },
394 .p2 = { .dot_limit = G4X_P2_SINGLE_CHANNEL_LVDS_LIMIT,
395 .p2_slow = G4X_P2_SINGLE_CHANNEL_LVDS_SLOW,
396 .p2_fast = G4X_P2_SINGLE_CHANNEL_LVDS_FAST
397 },
d4906093 398 .find_pll = intel_g4x_find_best_PLL,
e4b36699
KP
399};
400
401static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
044c7c41
ML
402 .dot = { .min = G4X_DOT_DUAL_CHANNEL_LVDS_MIN,
403 .max = G4X_DOT_DUAL_CHANNEL_LVDS_MAX },
404 .vco = { .min = G4X_VCO_MIN,
405 .max = G4X_VCO_MAX },
406 .n = { .min = G4X_N_DUAL_CHANNEL_LVDS_MIN,
407 .max = G4X_N_DUAL_CHANNEL_LVDS_MAX },
408 .m = { .min = G4X_M_DUAL_CHANNEL_LVDS_MIN,
409 .max = G4X_M_DUAL_CHANNEL_LVDS_MAX },
410 .m1 = { .min = G4X_M1_DUAL_CHANNEL_LVDS_MIN,
411 .max = G4X_M1_DUAL_CHANNEL_LVDS_MAX },
412 .m2 = { .min = G4X_M2_DUAL_CHANNEL_LVDS_MIN,
413 .max = G4X_M2_DUAL_CHANNEL_LVDS_MAX },
414 .p = { .min = G4X_P_DUAL_CHANNEL_LVDS_MIN,
415 .max = G4X_P_DUAL_CHANNEL_LVDS_MAX },
416 .p1 = { .min = G4X_P1_DUAL_CHANNEL_LVDS_MIN,
417 .max = G4X_P1_DUAL_CHANNEL_LVDS_MAX },
418 .p2 = { .dot_limit = G4X_P2_DUAL_CHANNEL_LVDS_LIMIT,
419 .p2_slow = G4X_P2_DUAL_CHANNEL_LVDS_SLOW,
420 .p2_fast = G4X_P2_DUAL_CHANNEL_LVDS_FAST
421 },
d4906093 422 .find_pll = intel_g4x_find_best_PLL,
e4b36699
KP
423};
424
425static const intel_limit_t intel_limits_g4x_display_port = {
a4fc5ed6
KP
426 .dot = { .min = G4X_DOT_DISPLAY_PORT_MIN,
427 .max = G4X_DOT_DISPLAY_PORT_MAX },
428 .vco = { .min = G4X_VCO_MIN,
429 .max = G4X_VCO_MAX},
430 .n = { .min = G4X_N_DISPLAY_PORT_MIN,
431 .max = G4X_N_DISPLAY_PORT_MAX },
432 .m = { .min = G4X_M_DISPLAY_PORT_MIN,
433 .max = G4X_M_DISPLAY_PORT_MAX },
434 .m1 = { .min = G4X_M1_DISPLAY_PORT_MIN,
435 .max = G4X_M1_DISPLAY_PORT_MAX },
436 .m2 = { .min = G4X_M2_DISPLAY_PORT_MIN,
437 .max = G4X_M2_DISPLAY_PORT_MAX },
438 .p = { .min = G4X_P_DISPLAY_PORT_MIN,
439 .max = G4X_P_DISPLAY_PORT_MAX },
440 .p1 = { .min = G4X_P1_DISPLAY_PORT_MIN,
441 .max = G4X_P1_DISPLAY_PORT_MAX},
442 .p2 = { .dot_limit = G4X_P2_DISPLAY_PORT_LIMIT,
443 .p2_slow = G4X_P2_DISPLAY_PORT_SLOW,
444 .p2_fast = G4X_P2_DISPLAY_PORT_FAST },
445 .find_pll = intel_find_pll_g4x_dp,
e4b36699
KP
446};
447
f2b115e6 448static const intel_limit_t intel_limits_pineview_sdvo = {
2177832f 449 .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX},
f2b115e6
AJ
450 .vco = { .min = PINEVIEW_VCO_MIN, .max = PINEVIEW_VCO_MAX },
451 .n = { .min = PINEVIEW_N_MIN, .max = PINEVIEW_N_MAX },
452 .m = { .min = PINEVIEW_M_MIN, .max = PINEVIEW_M_MAX },
453 .m1 = { .min = PINEVIEW_M1_MIN, .max = PINEVIEW_M1_MAX },
454 .m2 = { .min = PINEVIEW_M2_MIN, .max = PINEVIEW_M2_MAX },
2177832f
SL
455 .p = { .min = I9XX_P_SDVO_DAC_MIN, .max = I9XX_P_SDVO_DAC_MAX },
456 .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
457 .p2 = { .dot_limit = I9XX_P2_SDVO_DAC_SLOW_LIMIT,
458 .p2_slow = I9XX_P2_SDVO_DAC_SLOW, .p2_fast = I9XX_P2_SDVO_DAC_FAST },
6115707b 459 .find_pll = intel_find_best_PLL,
e4b36699
KP
460};
461
f2b115e6 462static const intel_limit_t intel_limits_pineview_lvds = {
2177832f 463 .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
f2b115e6
AJ
464 .vco = { .min = PINEVIEW_VCO_MIN, .max = PINEVIEW_VCO_MAX },
465 .n = { .min = PINEVIEW_N_MIN, .max = PINEVIEW_N_MAX },
466 .m = { .min = PINEVIEW_M_MIN, .max = PINEVIEW_M_MAX },
467 .m1 = { .min = PINEVIEW_M1_MIN, .max = PINEVIEW_M1_MAX },
468 .m2 = { .min = PINEVIEW_M2_MIN, .max = PINEVIEW_M2_MAX },
469 .p = { .min = PINEVIEW_P_LVDS_MIN, .max = PINEVIEW_P_LVDS_MAX },
2177832f 470 .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
f2b115e6 471 /* Pineview only supports single-channel mode. */
2177832f
SL
472 .p2 = { .dot_limit = I9XX_P2_LVDS_SLOW_LIMIT,
473 .p2_slow = I9XX_P2_LVDS_SLOW, .p2_fast = I9XX_P2_LVDS_SLOW },
6115707b 474 .find_pll = intel_find_best_PLL,
e4b36699
KP
475};
476
f2b115e6
AJ
477static const intel_limit_t intel_limits_ironlake_sdvo = {
478 .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
479 .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
480 .n = { .min = IRONLAKE_N_MIN, .max = IRONLAKE_N_MAX },
481 .m = { .min = IRONLAKE_M_MIN, .max = IRONLAKE_M_MAX },
482 .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
483 .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
484 .p = { .min = IRONLAKE_P_SDVO_DAC_MIN, .max = IRONLAKE_P_SDVO_DAC_MAX },
485 .p1 = { .min = IRONLAKE_P1_MIN, .max = IRONLAKE_P1_MAX },
486 .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
487 .p2_slow = IRONLAKE_P2_SDVO_DAC_SLOW,
488 .p2_fast = IRONLAKE_P2_SDVO_DAC_FAST },
4547668a 489 .find_pll = intel_g4x_find_best_PLL,
e4b36699
KP
490};
491
f2b115e6
AJ
492static const intel_limit_t intel_limits_ironlake_lvds = {
493 .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
494 .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
495 .n = { .min = IRONLAKE_N_MIN, .max = IRONLAKE_N_MAX },
496 .m = { .min = IRONLAKE_M_MIN, .max = IRONLAKE_M_MAX },
497 .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
498 .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
499 .p = { .min = IRONLAKE_P_LVDS_MIN, .max = IRONLAKE_P_LVDS_MAX },
500 .p1 = { .min = IRONLAKE_P1_MIN, .max = IRONLAKE_P1_MAX },
501 .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
502 .p2_slow = IRONLAKE_P2_LVDS_SLOW,
503 .p2_fast = IRONLAKE_P2_LVDS_FAST },
4547668a
ZY
504 .find_pll = intel_g4x_find_best_PLL,
505};
506
507static const intel_limit_t intel_limits_ironlake_display_port = {
508 .dot = { .min = IRONLAKE_DOT_MIN,
509 .max = IRONLAKE_DOT_MAX },
510 .vco = { .min = IRONLAKE_VCO_MIN,
511 .max = IRONLAKE_VCO_MAX},
512 .n = { .min = IRONLAKE_N_MIN,
513 .max = IRONLAKE_N_MAX },
514 .m = { .min = IRONLAKE_M_MIN,
515 .max = IRONLAKE_M_MAX },
516 .m1 = { .min = IRONLAKE_M1_MIN,
517 .max = IRONLAKE_M1_MAX },
518 .m2 = { .min = IRONLAKE_M2_MIN,
519 .max = IRONLAKE_M2_MAX },
520 .p = { .min = IRONLAKE_P_DISPLAY_PORT_MIN,
521 .max = IRONLAKE_P_DISPLAY_PORT_MAX },
522 .p1 = { .min = IRONLAKE_P1_DISPLAY_PORT_MIN,
523 .max = IRONLAKE_P1_DISPLAY_PORT_MAX},
524 .p2 = { .dot_limit = IRONLAKE_P2_DISPLAY_PORT_LIMIT,
525 .p2_slow = IRONLAKE_P2_DISPLAY_PORT_SLOW,
526 .p2_fast = IRONLAKE_P2_DISPLAY_PORT_FAST },
527 .find_pll = intel_find_pll_ironlake_dp,
79e53945
JB
528};
529
f2b115e6 530static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc)
2c07245f
ZW
531{
532 const intel_limit_t *limit;
533 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
f2b115e6 534 limit = &intel_limits_ironlake_lvds;
4547668a
ZY
535 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
536 HAS_eDP)
537 limit = &intel_limits_ironlake_display_port;
2c07245f 538 else
f2b115e6 539 limit = &intel_limits_ironlake_sdvo;
2c07245f
ZW
540
541 return limit;
542}
543
044c7c41
ML
544static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
545{
546 struct drm_device *dev = crtc->dev;
547 struct drm_i915_private *dev_priv = dev->dev_private;
548 const intel_limit_t *limit;
549
550 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
551 if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
552 LVDS_CLKB_POWER_UP)
553 /* LVDS with dual channel */
e4b36699 554 limit = &intel_limits_g4x_dual_channel_lvds;
044c7c41
ML
555 else
556 /* LVDS with dual channel */
e4b36699 557 limit = &intel_limits_g4x_single_channel_lvds;
044c7c41
ML
558 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
559 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
e4b36699 560 limit = &intel_limits_g4x_hdmi;
044c7c41 561 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
e4b36699 562 limit = &intel_limits_g4x_sdvo;
a4fc5ed6 563 } else if (intel_pipe_has_type (crtc, INTEL_OUTPUT_DISPLAYPORT)) {
e4b36699 564 limit = &intel_limits_g4x_display_port;
044c7c41 565 } else /* The option is for other outputs */
e4b36699 566 limit = &intel_limits_i9xx_sdvo;
044c7c41
ML
567
568 return limit;
569}
570
79e53945
JB
571static const intel_limit_t *intel_limit(struct drm_crtc *crtc)
572{
573 struct drm_device *dev = crtc->dev;
574 const intel_limit_t *limit;
575
f2b115e6
AJ
576 if (IS_IRONLAKE(dev))
577 limit = intel_ironlake_limit(crtc);
2c07245f 578 else if (IS_G4X(dev)) {
044c7c41 579 limit = intel_g4x_limit(crtc);
f2b115e6 580 } else if (IS_I9XX(dev) && !IS_PINEVIEW(dev)) {
79e53945 581 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
e4b36699 582 limit = &intel_limits_i9xx_lvds;
79e53945 583 else
e4b36699 584 limit = &intel_limits_i9xx_sdvo;
f2b115e6 585 } else if (IS_PINEVIEW(dev)) {
2177832f 586 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
f2b115e6 587 limit = &intel_limits_pineview_lvds;
2177832f 588 else
f2b115e6 589 limit = &intel_limits_pineview_sdvo;
79e53945
JB
590 } else {
591 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
e4b36699 592 limit = &intel_limits_i8xx_lvds;
79e53945 593 else
e4b36699 594 limit = &intel_limits_i8xx_dvo;
79e53945
JB
595 }
596 return limit;
597}
598
f2b115e6
AJ
599/* m1 is reserved as 0 in Pineview, n is a ring counter */
600static void pineview_clock(int refclk, intel_clock_t *clock)
79e53945 601{
2177832f
SL
602 clock->m = clock->m2 + 2;
603 clock->p = clock->p1 * clock->p2;
604 clock->vco = refclk * clock->m / clock->n;
605 clock->dot = clock->vco / clock->p;
606}
607
608static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
609{
f2b115e6
AJ
610 if (IS_PINEVIEW(dev)) {
611 pineview_clock(refclk, clock);
2177832f
SL
612 return;
613 }
79e53945
JB
614 clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
615 clock->p = clock->p1 * clock->p2;
616 clock->vco = refclk * clock->m / (clock->n + 2);
617 clock->dot = clock->vco / clock->p;
618}
619
79e53945
JB
620/**
621 * Returns whether any output on the specified pipe is of the specified type
622 */
623bool intel_pipe_has_type (struct drm_crtc *crtc, int type)
624{
625 struct drm_device *dev = crtc->dev;
626 struct drm_mode_config *mode_config = &dev->mode_config;
627 struct drm_connector *l_entry;
628
629 list_for_each_entry(l_entry, &mode_config->connector_list, head) {
630 if (l_entry->encoder &&
631 l_entry->encoder->crtc == crtc) {
632 struct intel_output *intel_output = to_intel_output(l_entry);
633 if (intel_output->type == type)
634 return true;
635 }
636 }
637 return false;
638}
639
32f9d658
ZW
640struct drm_connector *
641intel_pipe_get_output (struct drm_crtc *crtc)
642{
643 struct drm_device *dev = crtc->dev;
644 struct drm_mode_config *mode_config = &dev->mode_config;
645 struct drm_connector *l_entry, *ret = NULL;
646
647 list_for_each_entry(l_entry, &mode_config->connector_list, head) {
648 if (l_entry->encoder &&
649 l_entry->encoder->crtc == crtc) {
650 ret = l_entry;
651 break;
652 }
653 }
654 return ret;
655}
656
7c04d1d9 657#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
658/**
659 * Returns whether the given set of divisors are valid for a given refclk with
660 * the given connectors.
661 */
662
663static bool intel_PLL_is_valid(struct drm_crtc *crtc, intel_clock_t *clock)
664{
665 const intel_limit_t *limit = intel_limit (crtc);
2177832f 666 struct drm_device *dev = crtc->dev;
79e53945
JB
667
668 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
669 INTELPllInvalid ("p1 out of range\n");
670 if (clock->p < limit->p.min || limit->p.max < clock->p)
671 INTELPllInvalid ("p out of range\n");
672 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
673 INTELPllInvalid ("m2 out of range\n");
674 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
675 INTELPllInvalid ("m1 out of range\n");
f2b115e6 676 if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
79e53945
JB
677 INTELPllInvalid ("m1 <= m2\n");
678 if (clock->m < limit->m.min || limit->m.max < clock->m)
679 INTELPllInvalid ("m out of range\n");
680 if (clock->n < limit->n.min || limit->n.max < clock->n)
681 INTELPllInvalid ("n out of range\n");
682 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
683 INTELPllInvalid ("vco out of range\n");
684 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
685 * connector, etc., rather than just a single range.
686 */
687 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
688 INTELPllInvalid ("dot out of range\n");
689
690 return true;
691}
692
d4906093
ML
693static bool
694intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
695 int target, int refclk, intel_clock_t *best_clock)
696
79e53945
JB
697{
698 struct drm_device *dev = crtc->dev;
699 struct drm_i915_private *dev_priv = dev->dev_private;
700 intel_clock_t clock;
79e53945
JB
701 int err = target;
702
bc5e5718 703 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
832cc28d 704 (I915_READ(LVDS)) != 0) {
79e53945
JB
705 /*
706 * For LVDS, if the panel is on, just rely on its current
707 * settings for dual-channel. We haven't figured out how to
708 * reliably set up different single/dual channel state, if we
709 * even can.
710 */
711 if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
712 LVDS_CLKB_POWER_UP)
713 clock.p2 = limit->p2.p2_fast;
714 else
715 clock.p2 = limit->p2.p2_slow;
716 } else {
717 if (target < limit->p2.dot_limit)
718 clock.p2 = limit->p2.p2_slow;
719 else
720 clock.p2 = limit->p2.p2_fast;
721 }
722
723 memset (best_clock, 0, sizeof (*best_clock));
724
42158660
ZY
725 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
726 clock.m1++) {
727 for (clock.m2 = limit->m2.min;
728 clock.m2 <= limit->m2.max; clock.m2++) {
f2b115e6
AJ
729 /* m1 is always 0 in Pineview */
730 if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
42158660
ZY
731 break;
732 for (clock.n = limit->n.min;
733 clock.n <= limit->n.max; clock.n++) {
734 for (clock.p1 = limit->p1.min;
735 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
736 int this_err;
737
2177832f 738 intel_clock(dev, refclk, &clock);
79e53945
JB
739
740 if (!intel_PLL_is_valid(crtc, &clock))
741 continue;
742
743 this_err = abs(clock.dot - target);
744 if (this_err < err) {
745 *best_clock = clock;
746 err = this_err;
747 }
748 }
749 }
750 }
751 }
752
753 return (err != target);
754}
755
d4906093
ML
756static bool
757intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
758 int target, int refclk, intel_clock_t *best_clock)
759{
760 struct drm_device *dev = crtc->dev;
761 struct drm_i915_private *dev_priv = dev->dev_private;
762 intel_clock_t clock;
763 int max_n;
764 bool found;
765 /* approximately equals target * 0.00488 */
766 int err_most = (target >> 8) + (target >> 10);
767 found = false;
768
769 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
4547668a
ZY
770 int lvds_reg;
771
772 if (IS_IRONLAKE(dev))
773 lvds_reg = PCH_LVDS;
774 else
775 lvds_reg = LVDS;
776 if ((I915_READ(lvds_reg) & LVDS_CLKB_POWER_MASK) ==
d4906093
ML
777 LVDS_CLKB_POWER_UP)
778 clock.p2 = limit->p2.p2_fast;
779 else
780 clock.p2 = limit->p2.p2_slow;
781 } else {
782 if (target < limit->p2.dot_limit)
783 clock.p2 = limit->p2.p2_slow;
784 else
785 clock.p2 = limit->p2.p2_fast;
786 }
787
788 memset(best_clock, 0, sizeof(*best_clock));
789 max_n = limit->n.max;
790 /* based on hardware requriment prefer smaller n to precision */
791 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
652c393a 792 /* based on hardware requirment prefere larger m1,m2 */
d4906093
ML
793 for (clock.m1 = limit->m1.max;
794 clock.m1 >= limit->m1.min; clock.m1--) {
795 for (clock.m2 = limit->m2.max;
796 clock.m2 >= limit->m2.min; clock.m2--) {
797 for (clock.p1 = limit->p1.max;
798 clock.p1 >= limit->p1.min; clock.p1--) {
799 int this_err;
800
2177832f 801 intel_clock(dev, refclk, &clock);
d4906093
ML
802 if (!intel_PLL_is_valid(crtc, &clock))
803 continue;
804 this_err = abs(clock.dot - target) ;
805 if (this_err < err_most) {
806 *best_clock = clock;
807 err_most = this_err;
808 max_n = clock.n;
809 found = true;
810 }
811 }
812 }
813 }
814 }
2c07245f
ZW
815 return found;
816}
817
5eb08b69 818static bool
f2b115e6
AJ
819intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
820 int target, int refclk, intel_clock_t *best_clock)
5eb08b69
ZW
821{
822 struct drm_device *dev = crtc->dev;
823 intel_clock_t clock;
4547668a
ZY
824
825 /* return directly when it is eDP */
826 if (HAS_eDP)
827 return true;
828
5eb08b69
ZW
829 if (target < 200000) {
830 clock.n = 1;
831 clock.p1 = 2;
832 clock.p2 = 10;
833 clock.m1 = 12;
834 clock.m2 = 9;
835 } else {
836 clock.n = 2;
837 clock.p1 = 1;
838 clock.p2 = 10;
839 clock.m1 = 14;
840 clock.m2 = 8;
841 }
842 intel_clock(dev, refclk, &clock);
843 memcpy(best_clock, &clock, sizeof(intel_clock_t));
844 return true;
845}
846
a4fc5ed6
KP
847/* DisplayPort has only two frequencies, 162MHz and 270MHz */
848static bool
849intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
850 int target, int refclk, intel_clock_t *best_clock)
851{
852 intel_clock_t clock;
853 if (target < 200000) {
a4fc5ed6
KP
854 clock.p1 = 2;
855 clock.p2 = 10;
b3d25495
KP
856 clock.n = 2;
857 clock.m1 = 23;
858 clock.m2 = 8;
a4fc5ed6 859 } else {
a4fc5ed6
KP
860 clock.p1 = 1;
861 clock.p2 = 10;
b3d25495
KP
862 clock.n = 1;
863 clock.m1 = 14;
864 clock.m2 = 2;
a4fc5ed6 865 }
b3d25495
KP
866 clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
867 clock.p = (clock.p1 * clock.p2);
868 clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
fe798b97 869 clock.vco = 0;
a4fc5ed6
KP
870 memcpy(best_clock, &clock, sizeof(intel_clock_t));
871 return true;
872}
873
79e53945
JB
874void
875intel_wait_for_vblank(struct drm_device *dev)
876{
877 /* Wait for 20ms, i.e. one cycle at 50hz. */
311089d3 878 msleep(20);
79e53945
JB
879}
880
80824003
JB
881/* Parameters have changed, update FBC info */
882static void i8xx_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
883{
884 struct drm_device *dev = crtc->dev;
885 struct drm_i915_private *dev_priv = dev->dev_private;
886 struct drm_framebuffer *fb = crtc->fb;
887 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
888 struct drm_i915_gem_object *obj_priv = intel_fb->obj->driver_private;
889 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
890 int plane, i;
891 u32 fbc_ctl, fbc_ctl2;
892
893 dev_priv->cfb_pitch = dev_priv->cfb_size / FBC_LL_SIZE;
894
895 if (fb->pitch < dev_priv->cfb_pitch)
896 dev_priv->cfb_pitch = fb->pitch;
897
898 /* FBC_CTL wants 64B units */
899 dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
900 dev_priv->cfb_fence = obj_priv->fence_reg;
901 dev_priv->cfb_plane = intel_crtc->plane;
902 plane = dev_priv->cfb_plane == 0 ? FBC_CTL_PLANEA : FBC_CTL_PLANEB;
903
904 /* Clear old tags */
905 for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
906 I915_WRITE(FBC_TAG + (i * 4), 0);
907
908 /* Set it up... */
909 fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | plane;
910 if (obj_priv->tiling_mode != I915_TILING_NONE)
911 fbc_ctl2 |= FBC_CTL_CPU_FENCE;
912 I915_WRITE(FBC_CONTROL2, fbc_ctl2);
913 I915_WRITE(FBC_FENCE_OFF, crtc->y);
914
915 /* enable it... */
916 fbc_ctl = FBC_CTL_EN | FBC_CTL_PERIODIC;
917 fbc_ctl |= (dev_priv->cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
918 fbc_ctl |= (interval & 0x2fff) << FBC_CTL_INTERVAL_SHIFT;
919 if (obj_priv->tiling_mode != I915_TILING_NONE)
920 fbc_ctl |= dev_priv->cfb_fence;
921 I915_WRITE(FBC_CONTROL, fbc_ctl);
922
28c97730 923 DRM_DEBUG_KMS("enabled FBC, pitch %ld, yoff %d, plane %d, ",
80824003
JB
924 dev_priv->cfb_pitch, crtc->y, dev_priv->cfb_plane);
925}
926
927void i8xx_disable_fbc(struct drm_device *dev)
928{
929 struct drm_i915_private *dev_priv = dev->dev_private;
930 u32 fbc_ctl;
931
c1a1cdc1
JB
932 if (!I915_HAS_FBC(dev))
933 return;
934
80824003
JB
935 /* Disable compression */
936 fbc_ctl = I915_READ(FBC_CONTROL);
937 fbc_ctl &= ~FBC_CTL_EN;
938 I915_WRITE(FBC_CONTROL, fbc_ctl);
939
940 /* Wait for compressing bit to clear */
941 while (I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING)
942 ; /* nothing */
943
944 intel_wait_for_vblank(dev);
945
28c97730 946 DRM_DEBUG_KMS("disabled FBC\n");
80824003
JB
947}
948
949static bool i8xx_fbc_enabled(struct drm_crtc *crtc)
950{
951 struct drm_device *dev = crtc->dev;
952 struct drm_i915_private *dev_priv = dev->dev_private;
953
954 return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
955}
956
74dff282
JB
957static void g4x_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
958{
959 struct drm_device *dev = crtc->dev;
960 struct drm_i915_private *dev_priv = dev->dev_private;
961 struct drm_framebuffer *fb = crtc->fb;
962 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
963 struct drm_i915_gem_object *obj_priv = intel_fb->obj->driver_private;
964 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
965 int plane = (intel_crtc->plane == 0 ? DPFC_CTL_PLANEA :
966 DPFC_CTL_PLANEB);
967 unsigned long stall_watermark = 200;
968 u32 dpfc_ctl;
969
970 dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
971 dev_priv->cfb_fence = obj_priv->fence_reg;
972 dev_priv->cfb_plane = intel_crtc->plane;
973
974 dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;
975 if (obj_priv->tiling_mode != I915_TILING_NONE) {
976 dpfc_ctl |= DPFC_CTL_FENCE_EN | dev_priv->cfb_fence;
977 I915_WRITE(DPFC_CHICKEN, DPFC_HT_MODIFY);
978 } else {
979 I915_WRITE(DPFC_CHICKEN, ~DPFC_HT_MODIFY);
980 }
981
982 I915_WRITE(DPFC_CONTROL, dpfc_ctl);
983 I915_WRITE(DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
984 (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
985 (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
986 I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
987
988 /* enable it... */
989 I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);
990
28c97730 991 DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
74dff282
JB
992}
993
994void g4x_disable_fbc(struct drm_device *dev)
995{
996 struct drm_i915_private *dev_priv = dev->dev_private;
997 u32 dpfc_ctl;
998
999 /* Disable compression */
1000 dpfc_ctl = I915_READ(DPFC_CONTROL);
1001 dpfc_ctl &= ~DPFC_CTL_EN;
1002 I915_WRITE(DPFC_CONTROL, dpfc_ctl);
1003 intel_wait_for_vblank(dev);
1004
28c97730 1005 DRM_DEBUG_KMS("disabled FBC\n");
74dff282
JB
1006}
1007
1008static bool g4x_fbc_enabled(struct drm_crtc *crtc)
1009{
1010 struct drm_device *dev = crtc->dev;
1011 struct drm_i915_private *dev_priv = dev->dev_private;
1012
1013 return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
1014}
1015
80824003
JB
1016/**
1017 * intel_update_fbc - enable/disable FBC as needed
1018 * @crtc: CRTC to point the compressor at
1019 * @mode: mode in use
1020 *
1021 * Set up the framebuffer compression hardware at mode set time. We
1022 * enable it if possible:
1023 * - plane A only (on pre-965)
1024 * - no pixel mulitply/line duplication
1025 * - no alpha buffer discard
1026 * - no dual wide
1027 * - framebuffer <= 2048 in width, 1536 in height
1028 *
1029 * We can't assume that any compression will take place (worst case),
1030 * so the compressed buffer has to be the same size as the uncompressed
1031 * one. It also must reside (along with the line length buffer) in
1032 * stolen memory.
1033 *
1034 * We need to enable/disable FBC on a global basis.
1035 */
1036static void intel_update_fbc(struct drm_crtc *crtc,
1037 struct drm_display_mode *mode)
1038{
1039 struct drm_device *dev = crtc->dev;
1040 struct drm_i915_private *dev_priv = dev->dev_private;
1041 struct drm_framebuffer *fb = crtc->fb;
1042 struct intel_framebuffer *intel_fb;
1043 struct drm_i915_gem_object *obj_priv;
1044 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1045 int plane = intel_crtc->plane;
1046
1047 if (!i915_powersave)
1048 return;
1049
e70236a8
JB
1050 if (!dev_priv->display.fbc_enabled ||
1051 !dev_priv->display.enable_fbc ||
1052 !dev_priv->display.disable_fbc)
1053 return;
1054
80824003
JB
1055 if (!crtc->fb)
1056 return;
1057
1058 intel_fb = to_intel_framebuffer(fb);
1059 obj_priv = intel_fb->obj->driver_private;
1060
1061 /*
1062 * If FBC is already on, we just have to verify that we can
1063 * keep it that way...
1064 * Need to disable if:
1065 * - changing FBC params (stride, fence, mode)
1066 * - new fb is too large to fit in compressed buffer
1067 * - going to an unsupported config (interlace, pixel multiply, etc.)
1068 */
1069 if (intel_fb->obj->size > dev_priv->cfb_size) {
28c97730
ZY
1070 DRM_DEBUG_KMS("framebuffer too large, disabling "
1071 "compression\n");
80824003
JB
1072 goto out_disable;
1073 }
1074 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) ||
1075 (mode->flags & DRM_MODE_FLAG_DBLSCAN)) {
28c97730
ZY
1076 DRM_DEBUG_KMS("mode incompatible with compression, "
1077 "disabling\n");
80824003
JB
1078 goto out_disable;
1079 }
1080 if ((mode->hdisplay > 2048) ||
1081 (mode->vdisplay > 1536)) {
28c97730 1082 DRM_DEBUG_KMS("mode too large for compression, disabling\n");
80824003
JB
1083 goto out_disable;
1084 }
74dff282 1085 if ((IS_I915GM(dev) || IS_I945GM(dev)) && plane != 0) {
28c97730 1086 DRM_DEBUG_KMS("plane not 0, disabling compression\n");
80824003
JB
1087 goto out_disable;
1088 }
1089 if (obj_priv->tiling_mode != I915_TILING_X) {
28c97730 1090 DRM_DEBUG_KMS("framebuffer not tiled, disabling compression\n");
80824003
JB
1091 goto out_disable;
1092 }
1093
e70236a8 1094 if (dev_priv->display.fbc_enabled(crtc)) {
80824003
JB
1095 /* We can re-enable it in this case, but need to update pitch */
1096 if (fb->pitch > dev_priv->cfb_pitch)
e70236a8 1097 dev_priv->display.disable_fbc(dev);
80824003 1098 if (obj_priv->fence_reg != dev_priv->cfb_fence)
e70236a8 1099 dev_priv->display.disable_fbc(dev);
80824003 1100 if (plane != dev_priv->cfb_plane)
e70236a8 1101 dev_priv->display.disable_fbc(dev);
80824003
JB
1102 }
1103
e70236a8 1104 if (!dev_priv->display.fbc_enabled(crtc)) {
80824003 1105 /* Now try to turn it back on if possible */
e70236a8 1106 dev_priv->display.enable_fbc(crtc, 500);
80824003
JB
1107 }
1108
1109 return;
1110
1111out_disable:
28c97730 1112 DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
80824003 1113 /* Multiple disables should be harmless */
e70236a8
JB
1114 if (dev_priv->display.fbc_enabled(crtc))
1115 dev_priv->display.disable_fbc(dev);
80824003
JB
1116}
1117
6b95a207
KH
1118static int
1119intel_pin_and_fence_fb_obj(struct drm_device *dev, struct drm_gem_object *obj)
1120{
1121 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1122 u32 alignment;
1123 int ret;
1124
1125 switch (obj_priv->tiling_mode) {
1126 case I915_TILING_NONE:
1127 alignment = 64 * 1024;
1128 break;
1129 case I915_TILING_X:
1130 /* pin() will align the object as required by fence */
1131 alignment = 0;
1132 break;
1133 case I915_TILING_Y:
1134 /* FIXME: Is this true? */
1135 DRM_ERROR("Y tiled not allowed for scan out buffers\n");
1136 return -EINVAL;
1137 default:
1138 BUG();
1139 }
1140
6b95a207
KH
1141 ret = i915_gem_object_pin(obj, alignment);
1142 if (ret != 0)
1143 return ret;
1144
1145 /* Install a fence for tiled scan-out. Pre-i965 always needs a
1146 * fence, whereas 965+ only requires a fence if using
1147 * framebuffer compression. For simplicity, we always install
1148 * a fence as the cost is not that onerous.
1149 */
1150 if (obj_priv->fence_reg == I915_FENCE_REG_NONE &&
1151 obj_priv->tiling_mode != I915_TILING_NONE) {
1152 ret = i915_gem_object_get_fence_reg(obj);
1153 if (ret != 0) {
1154 i915_gem_object_unpin(obj);
1155 return ret;
1156 }
1157 }
1158
1159 return 0;
1160}
1161
5c3b82e2 1162static int
3c4fdcfb
KH
1163intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
1164 struct drm_framebuffer *old_fb)
79e53945
JB
1165{
1166 struct drm_device *dev = crtc->dev;
1167 struct drm_i915_private *dev_priv = dev->dev_private;
1168 struct drm_i915_master_private *master_priv;
1169 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1170 struct intel_framebuffer *intel_fb;
1171 struct drm_i915_gem_object *obj_priv;
1172 struct drm_gem_object *obj;
1173 int pipe = intel_crtc->pipe;
80824003 1174 int plane = intel_crtc->plane;
79e53945 1175 unsigned long Start, Offset;
80824003
JB
1176 int dspbase = (plane == 0 ? DSPAADDR : DSPBADDR);
1177 int dspsurf = (plane == 0 ? DSPASURF : DSPBSURF);
1178 int dspstride = (plane == 0) ? DSPASTRIDE : DSPBSTRIDE;
1179 int dsptileoff = (plane == 0 ? DSPATILEOFF : DSPBTILEOFF);
1180 int dspcntr_reg = (plane == 0) ? DSPACNTR : DSPBCNTR;
6b95a207 1181 u32 dspcntr;
5c3b82e2 1182 int ret;
79e53945
JB
1183
1184 /* no fb bound */
1185 if (!crtc->fb) {
28c97730 1186 DRM_DEBUG_KMS("No FB bound\n");
5c3b82e2
CW
1187 return 0;
1188 }
1189
80824003 1190 switch (plane) {
5c3b82e2
CW
1191 case 0:
1192 case 1:
1193 break;
1194 default:
80824003 1195 DRM_ERROR("Can't update plane %d in SAREA\n", plane);
5c3b82e2 1196 return -EINVAL;
79e53945
JB
1197 }
1198
1199 intel_fb = to_intel_framebuffer(crtc->fb);
79e53945
JB
1200 obj = intel_fb->obj;
1201 obj_priv = obj->driver_private;
1202
5c3b82e2 1203 mutex_lock(&dev->struct_mutex);
6b95a207 1204 ret = intel_pin_and_fence_fb_obj(dev, obj);
5c3b82e2
CW
1205 if (ret != 0) {
1206 mutex_unlock(&dev->struct_mutex);
1207 return ret;
1208 }
79e53945 1209
b9241ea3 1210 ret = i915_gem_object_set_to_display_plane(obj);
5c3b82e2 1211 if (ret != 0) {
8c4b8c3f 1212 i915_gem_object_unpin(obj);
5c3b82e2
CW
1213 mutex_unlock(&dev->struct_mutex);
1214 return ret;
1215 }
79e53945
JB
1216
1217 dspcntr = I915_READ(dspcntr_reg);
712531bf
JB
1218 /* Mask out pixel format bits in case we change it */
1219 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
79e53945
JB
1220 switch (crtc->fb->bits_per_pixel) {
1221 case 8:
1222 dspcntr |= DISPPLANE_8BPP;
1223 break;
1224 case 16:
1225 if (crtc->fb->depth == 15)
1226 dspcntr |= DISPPLANE_15_16BPP;
1227 else
1228 dspcntr |= DISPPLANE_16BPP;
1229 break;
1230 case 24:
1231 case 32:
a4f45cf1
KH
1232 if (crtc->fb->depth == 30)
1233 dspcntr |= DISPPLANE_32BPP_30BIT_NO_ALPHA;
1234 else
1235 dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
79e53945
JB
1236 break;
1237 default:
1238 DRM_ERROR("Unknown color depth\n");
8c4b8c3f 1239 i915_gem_object_unpin(obj);
5c3b82e2
CW
1240 mutex_unlock(&dev->struct_mutex);
1241 return -EINVAL;
79e53945 1242 }
f544847f
JB
1243 if (IS_I965G(dev)) {
1244 if (obj_priv->tiling_mode != I915_TILING_NONE)
1245 dspcntr |= DISPPLANE_TILED;
1246 else
1247 dspcntr &= ~DISPPLANE_TILED;
1248 }
1249
f2b115e6 1250 if (IS_IRONLAKE(dev))
553bd149
ZW
1251 /* must disable */
1252 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
1253
79e53945
JB
1254 I915_WRITE(dspcntr_reg, dspcntr);
1255
5c3b82e2
CW
1256 Start = obj_priv->gtt_offset;
1257 Offset = y * crtc->fb->pitch + x * (crtc->fb->bits_per_pixel / 8);
1258
28c97730 1259 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d\n", Start, Offset, x, y);
5c3b82e2 1260 I915_WRITE(dspstride, crtc->fb->pitch);
79e53945
JB
1261 if (IS_I965G(dev)) {
1262 I915_WRITE(dspbase, Offset);
1263 I915_READ(dspbase);
1264 I915_WRITE(dspsurf, Start);
1265 I915_READ(dspsurf);
f544847f 1266 I915_WRITE(dsptileoff, (y << 16) | x);
79e53945
JB
1267 } else {
1268 I915_WRITE(dspbase, Start + Offset);
1269 I915_READ(dspbase);
1270 }
1271
74dff282 1272 if ((IS_I965G(dev) || plane == 0))
edb81956
JB
1273 intel_update_fbc(crtc, &crtc->mode);
1274
3c4fdcfb
KH
1275 intel_wait_for_vblank(dev);
1276
1277 if (old_fb) {
1278 intel_fb = to_intel_framebuffer(old_fb);
652c393a 1279 obj_priv = intel_fb->obj->driver_private;
3c4fdcfb
KH
1280 i915_gem_object_unpin(intel_fb->obj);
1281 }
652c393a
JB
1282 intel_increase_pllclock(crtc, true);
1283
5c3b82e2 1284 mutex_unlock(&dev->struct_mutex);
79e53945
JB
1285
1286 if (!dev->primary->master)
5c3b82e2 1287 return 0;
79e53945
JB
1288
1289 master_priv = dev->primary->master->driver_priv;
1290 if (!master_priv->sarea_priv)
5c3b82e2 1291 return 0;
79e53945 1292
5c3b82e2 1293 if (pipe) {
79e53945
JB
1294 master_priv->sarea_priv->pipeB_x = x;
1295 master_priv->sarea_priv->pipeB_y = y;
5c3b82e2
CW
1296 } else {
1297 master_priv->sarea_priv->pipeA_x = x;
1298 master_priv->sarea_priv->pipeA_y = y;
79e53945 1299 }
5c3b82e2
CW
1300
1301 return 0;
79e53945
JB
1302}
1303
24f119c7
ZW
1304/* Disable the VGA plane that we never use */
1305static void i915_disable_vga (struct drm_device *dev)
1306{
1307 struct drm_i915_private *dev_priv = dev->dev_private;
1308 u8 sr1;
1309 u32 vga_reg;
1310
f2b115e6 1311 if (IS_IRONLAKE(dev))
24f119c7
ZW
1312 vga_reg = CPU_VGACNTRL;
1313 else
1314 vga_reg = VGACNTRL;
1315
1316 if (I915_READ(vga_reg) & VGA_DISP_DISABLE)
1317 return;
1318
1319 I915_WRITE8(VGA_SR_INDEX, 1);
1320 sr1 = I915_READ8(VGA_SR_DATA);
1321 I915_WRITE8(VGA_SR_DATA, sr1 | (1 << 5));
1322 udelay(100);
1323
1324 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
1325}
1326
f2b115e6 1327static void ironlake_disable_pll_edp (struct drm_crtc *crtc)
32f9d658
ZW
1328{
1329 struct drm_device *dev = crtc->dev;
1330 struct drm_i915_private *dev_priv = dev->dev_private;
1331 u32 dpa_ctl;
1332
28c97730 1333 DRM_DEBUG_KMS("\n");
32f9d658
ZW
1334 dpa_ctl = I915_READ(DP_A);
1335 dpa_ctl &= ~DP_PLL_ENABLE;
1336 I915_WRITE(DP_A, dpa_ctl);
1337}
1338
f2b115e6 1339static void ironlake_enable_pll_edp (struct drm_crtc *crtc)
32f9d658
ZW
1340{
1341 struct drm_device *dev = crtc->dev;
1342 struct drm_i915_private *dev_priv = dev->dev_private;
1343 u32 dpa_ctl;
1344
1345 dpa_ctl = I915_READ(DP_A);
1346 dpa_ctl |= DP_PLL_ENABLE;
1347 I915_WRITE(DP_A, dpa_ctl);
1348 udelay(200);
1349}
1350
1351
f2b115e6 1352static void ironlake_set_pll_edp (struct drm_crtc *crtc, int clock)
32f9d658
ZW
1353{
1354 struct drm_device *dev = crtc->dev;
1355 struct drm_i915_private *dev_priv = dev->dev_private;
1356 u32 dpa_ctl;
1357
28c97730 1358 DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", clock);
32f9d658
ZW
1359 dpa_ctl = I915_READ(DP_A);
1360 dpa_ctl &= ~DP_PLL_FREQ_MASK;
1361
1362 if (clock < 200000) {
1363 u32 temp;
1364 dpa_ctl |= DP_PLL_FREQ_160MHZ;
1365 /* workaround for 160Mhz:
1366 1) program 0x4600c bits 15:0 = 0x8124
1367 2) program 0x46010 bit 0 = 1
1368 3) program 0x46034 bit 24 = 1
1369 4) program 0x64000 bit 14 = 1
1370 */
1371 temp = I915_READ(0x4600c);
1372 temp &= 0xffff0000;
1373 I915_WRITE(0x4600c, temp | 0x8124);
1374
1375 temp = I915_READ(0x46010);
1376 I915_WRITE(0x46010, temp | 1);
1377
1378 temp = I915_READ(0x46034);
1379 I915_WRITE(0x46034, temp | (1 << 24));
1380 } else {
1381 dpa_ctl |= DP_PLL_FREQ_270MHZ;
1382 }
1383 I915_WRITE(DP_A, dpa_ctl);
1384
1385 udelay(500);
1386}
1387
f2b115e6 1388static void ironlake_crtc_dpms(struct drm_crtc *crtc, int mode)
2c07245f
ZW
1389{
1390 struct drm_device *dev = crtc->dev;
1391 struct drm_i915_private *dev_priv = dev->dev_private;
1392 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1393 int pipe = intel_crtc->pipe;
7662c8bd 1394 int plane = intel_crtc->plane;
2c07245f
ZW
1395 int pch_dpll_reg = (pipe == 0) ? PCH_DPLL_A : PCH_DPLL_B;
1396 int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
1397 int dspcntr_reg = (plane == 0) ? DSPACNTR : DSPBCNTR;
1398 int dspbase_reg = (plane == 0) ? DSPAADDR : DSPBADDR;
1399 int fdi_tx_reg = (pipe == 0) ? FDI_TXA_CTL : FDI_TXB_CTL;
1400 int fdi_rx_reg = (pipe == 0) ? FDI_RXA_CTL : FDI_RXB_CTL;
1401 int fdi_rx_iir_reg = (pipe == 0) ? FDI_RXA_IIR : FDI_RXB_IIR;
1402 int fdi_rx_imr_reg = (pipe == 0) ? FDI_RXA_IMR : FDI_RXB_IMR;
1403 int transconf_reg = (pipe == 0) ? TRANSACONF : TRANSBCONF;
1404 int pf_ctl_reg = (pipe == 0) ? PFA_CTL_1 : PFB_CTL_1;
249c0e64 1405 int pf_win_size = (pipe == 0) ? PFA_WIN_SZ : PFB_WIN_SZ;
8dd81a38 1406 int pf_win_pos = (pipe == 0) ? PFA_WIN_POS : PFB_WIN_POS;
2c07245f
ZW
1407 int cpu_htot_reg = (pipe == 0) ? HTOTAL_A : HTOTAL_B;
1408 int cpu_hblank_reg = (pipe == 0) ? HBLANK_A : HBLANK_B;
1409 int cpu_hsync_reg = (pipe == 0) ? HSYNC_A : HSYNC_B;
1410 int cpu_vtot_reg = (pipe == 0) ? VTOTAL_A : VTOTAL_B;
1411 int cpu_vblank_reg = (pipe == 0) ? VBLANK_A : VBLANK_B;
1412 int cpu_vsync_reg = (pipe == 0) ? VSYNC_A : VSYNC_B;
1413 int trans_htot_reg = (pipe == 0) ? TRANS_HTOTAL_A : TRANS_HTOTAL_B;
1414 int trans_hblank_reg = (pipe == 0) ? TRANS_HBLANK_A : TRANS_HBLANK_B;
1415 int trans_hsync_reg = (pipe == 0) ? TRANS_HSYNC_A : TRANS_HSYNC_B;
1416 int trans_vtot_reg = (pipe == 0) ? TRANS_VTOTAL_A : TRANS_VTOTAL_B;
1417 int trans_vblank_reg = (pipe == 0) ? TRANS_VBLANK_A : TRANS_VBLANK_B;
1418 int trans_vsync_reg = (pipe == 0) ? TRANS_VSYNC_A : TRANS_VSYNC_B;
1419 u32 temp;
249c0e64 1420 int tries = 5, j, n;
8faf3b31
ZY
1421 u32 pipe_bpc;
1422
1423 temp = I915_READ(pipeconf_reg);
1424 pipe_bpc = temp & PIPE_BPC_MASK;
79e53945 1425
2c07245f
ZW
1426 /* XXX: When our outputs are all unaware of DPMS modes other than off
1427 * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
1428 */
1429 switch (mode) {
1430 case DRM_MODE_DPMS_ON:
1431 case DRM_MODE_DPMS_STANDBY:
1432 case DRM_MODE_DPMS_SUSPEND:
28c97730 1433 DRM_DEBUG_KMS("crtc %d dpms on\n", pipe);
1b3c7a47
ZW
1434
1435 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
1436 temp = I915_READ(PCH_LVDS);
1437 if ((temp & LVDS_PORT_EN) == 0) {
1438 I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
1439 POSTING_READ(PCH_LVDS);
1440 }
1441 }
1442
32f9d658
ZW
1443 if (HAS_eDP) {
1444 /* enable eDP PLL */
f2b115e6 1445 ironlake_enable_pll_edp(crtc);
32f9d658
ZW
1446 } else {
1447 /* enable PCH DPLL */
1448 temp = I915_READ(pch_dpll_reg);
1449 if ((temp & DPLL_VCO_ENABLE) == 0) {
1450 I915_WRITE(pch_dpll_reg, temp | DPLL_VCO_ENABLE);
1451 I915_READ(pch_dpll_reg);
1452 }
2c07245f 1453
32f9d658
ZW
1454 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
1455 temp = I915_READ(fdi_rx_reg);
8faf3b31
ZY
1456 /*
1457 * make the BPC in FDI Rx be consistent with that in
1458 * pipeconf reg.
1459 */
1460 temp &= ~(0x7 << 16);
1461 temp |= (pipe_bpc << 11);
32f9d658
ZW
1462 I915_WRITE(fdi_rx_reg, temp | FDI_RX_PLL_ENABLE |
1463 FDI_SEL_PCDCLK |
1464 FDI_DP_PORT_WIDTH_X4); /* default 4 lanes */
1465 I915_READ(fdi_rx_reg);
1466 udelay(200);
1467
f2b115e6 1468 /* Enable CPU FDI TX PLL, always on for Ironlake */
32f9d658
ZW
1469 temp = I915_READ(fdi_tx_reg);
1470 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
1471 I915_WRITE(fdi_tx_reg, temp | FDI_TX_PLL_ENABLE);
1472 I915_READ(fdi_tx_reg);
1473 udelay(100);
1474 }
2c07245f
ZW
1475 }
1476
8dd81a38
ZW
1477 /* Enable panel fitting for LVDS */
1478 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
1479 temp = I915_READ(pf_ctl_reg);
b1f60b70 1480 I915_WRITE(pf_ctl_reg, temp | PF_ENABLE | PF_FILTER_MED_3x3);
8dd81a38
ZW
1481
1482 /* currently full aspect */
1483 I915_WRITE(pf_win_pos, 0);
1484
1485 I915_WRITE(pf_win_size,
1486 (dev_priv->panel_fixed_mode->hdisplay << 16) |
1487 (dev_priv->panel_fixed_mode->vdisplay));
1488 }
1489
2c07245f
ZW
1490 /* Enable CPU pipe */
1491 temp = I915_READ(pipeconf_reg);
1492 if ((temp & PIPEACONF_ENABLE) == 0) {
1493 I915_WRITE(pipeconf_reg, temp | PIPEACONF_ENABLE);
1494 I915_READ(pipeconf_reg);
1495 udelay(100);
1496 }
1497
1498 /* configure and enable CPU plane */
1499 temp = I915_READ(dspcntr_reg);
1500 if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
1501 I915_WRITE(dspcntr_reg, temp | DISPLAY_PLANE_ENABLE);
1502 /* Flush the plane changes */
1503 I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
1504 }
1505
32f9d658
ZW
1506 if (!HAS_eDP) {
1507 /* enable CPU FDI TX and PCH FDI RX */
1508 temp = I915_READ(fdi_tx_reg);
1509 temp |= FDI_TX_ENABLE;
1510 temp |= FDI_DP_PORT_WIDTH_X4; /* default */
1511 temp &= ~FDI_LINK_TRAIN_NONE;
1512 temp |= FDI_LINK_TRAIN_PATTERN_1;
1513 I915_WRITE(fdi_tx_reg, temp);
1514 I915_READ(fdi_tx_reg);
2c07245f 1515
32f9d658
ZW
1516 temp = I915_READ(fdi_rx_reg);
1517 temp &= ~FDI_LINK_TRAIN_NONE;
1518 temp |= FDI_LINK_TRAIN_PATTERN_1;
1519 I915_WRITE(fdi_rx_reg, temp | FDI_RX_ENABLE);
1520 I915_READ(fdi_rx_reg);
2c07245f 1521
32f9d658 1522 udelay(150);
2c07245f 1523
32f9d658
ZW
1524 /* Train FDI. */
1525 /* umask FDI RX Interrupt symbol_lock and bit_lock bit
1526 for train result */
1527 temp = I915_READ(fdi_rx_imr_reg);
1528 temp &= ~FDI_RX_SYMBOL_LOCK;
1529 temp &= ~FDI_RX_BIT_LOCK;
1530 I915_WRITE(fdi_rx_imr_reg, temp);
1531 I915_READ(fdi_rx_imr_reg);
1532 udelay(150);
2c07245f 1533
32f9d658 1534 temp = I915_READ(fdi_rx_iir_reg);
28c97730 1535 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2c07245f 1536
32f9d658
ZW
1537 if ((temp & FDI_RX_BIT_LOCK) == 0) {
1538 for (j = 0; j < tries; j++) {
1539 temp = I915_READ(fdi_rx_iir_reg);
28c97730
ZY
1540 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n",
1541 temp);
32f9d658
ZW
1542 if (temp & FDI_RX_BIT_LOCK)
1543 break;
1544 udelay(200);
1545 }
1546 if (j != tries)
1547 I915_WRITE(fdi_rx_iir_reg,
1548 temp | FDI_RX_BIT_LOCK);
1549 else
28c97730 1550 DRM_DEBUG_KMS("train 1 fail\n");
32f9d658 1551 } else {
2c07245f
ZW
1552 I915_WRITE(fdi_rx_iir_reg,
1553 temp | FDI_RX_BIT_LOCK);
28c97730 1554 DRM_DEBUG_KMS("train 1 ok 2!\n");
32f9d658
ZW
1555 }
1556 temp = I915_READ(fdi_tx_reg);
1557 temp &= ~FDI_LINK_TRAIN_NONE;
1558 temp |= FDI_LINK_TRAIN_PATTERN_2;
1559 I915_WRITE(fdi_tx_reg, temp);
1560
1561 temp = I915_READ(fdi_rx_reg);
1562 temp &= ~FDI_LINK_TRAIN_NONE;
1563 temp |= FDI_LINK_TRAIN_PATTERN_2;
1564 I915_WRITE(fdi_rx_reg, temp);
2c07245f 1565
32f9d658 1566 udelay(150);
2c07245f 1567
32f9d658 1568 temp = I915_READ(fdi_rx_iir_reg);
28c97730 1569 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2c07245f 1570
32f9d658
ZW
1571 if ((temp & FDI_RX_SYMBOL_LOCK) == 0) {
1572 for (j = 0; j < tries; j++) {
1573 temp = I915_READ(fdi_rx_iir_reg);
28c97730
ZY
1574 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n",
1575 temp);
32f9d658
ZW
1576 if (temp & FDI_RX_SYMBOL_LOCK)
1577 break;
1578 udelay(200);
1579 }
1580 if (j != tries) {
1581 I915_WRITE(fdi_rx_iir_reg,
1582 temp | FDI_RX_SYMBOL_LOCK);
28c97730 1583 DRM_DEBUG_KMS("train 2 ok 1!\n");
32f9d658 1584 } else
28c97730 1585 DRM_DEBUG_KMS("train 2 fail\n");
32f9d658 1586 } else {
2c07245f
ZW
1587 I915_WRITE(fdi_rx_iir_reg,
1588 temp | FDI_RX_SYMBOL_LOCK);
28c97730 1589 DRM_DEBUG_KMS("train 2 ok 2!\n");
32f9d658 1590 }
28c97730 1591 DRM_DEBUG_KMS("train done\n");
2c07245f 1592
32f9d658
ZW
1593 /* set transcoder timing */
1594 I915_WRITE(trans_htot_reg, I915_READ(cpu_htot_reg));
1595 I915_WRITE(trans_hblank_reg, I915_READ(cpu_hblank_reg));
1596 I915_WRITE(trans_hsync_reg, I915_READ(cpu_hsync_reg));
2c07245f 1597
32f9d658
ZW
1598 I915_WRITE(trans_vtot_reg, I915_READ(cpu_vtot_reg));
1599 I915_WRITE(trans_vblank_reg, I915_READ(cpu_vblank_reg));
1600 I915_WRITE(trans_vsync_reg, I915_READ(cpu_vsync_reg));
2c07245f 1601
32f9d658
ZW
1602 /* enable PCH transcoder */
1603 temp = I915_READ(transconf_reg);
8faf3b31
ZY
1604 /*
1605 * make the BPC in transcoder be consistent with
1606 * that in pipeconf reg.
1607 */
1608 temp &= ~PIPE_BPC_MASK;
1609 temp |= pipe_bpc;
32f9d658
ZW
1610 I915_WRITE(transconf_reg, temp | TRANS_ENABLE);
1611 I915_READ(transconf_reg);
2c07245f 1612
32f9d658
ZW
1613 while ((I915_READ(transconf_reg) & TRANS_STATE_ENABLE) == 0)
1614 ;
2c07245f 1615
32f9d658 1616 /* enable normal */
2c07245f 1617
32f9d658
ZW
1618 temp = I915_READ(fdi_tx_reg);
1619 temp &= ~FDI_LINK_TRAIN_NONE;
1620 I915_WRITE(fdi_tx_reg, temp | FDI_LINK_TRAIN_NONE |
1621 FDI_TX_ENHANCE_FRAME_ENABLE);
1622 I915_READ(fdi_tx_reg);
2c07245f 1623
32f9d658
ZW
1624 temp = I915_READ(fdi_rx_reg);
1625 temp &= ~FDI_LINK_TRAIN_NONE;
1626 I915_WRITE(fdi_rx_reg, temp | FDI_LINK_TRAIN_NONE |
1627 FDI_RX_ENHANCE_FRAME_ENABLE);
1628 I915_READ(fdi_rx_reg);
2c07245f 1629
32f9d658
ZW
1630 /* wait one idle pattern time */
1631 udelay(100);
1632
1633 }
2c07245f
ZW
1634
1635 intel_crtc_load_lut(crtc);
1636
1637 break;
1638 case DRM_MODE_DPMS_OFF:
28c97730 1639 DRM_DEBUG_KMS("crtc %d dpms off\n", pipe);
2c07245f 1640
c062df61 1641 drm_vblank_off(dev, pipe);
2c07245f
ZW
1642 /* Disable display plane */
1643 temp = I915_READ(dspcntr_reg);
1644 if ((temp & DISPLAY_PLANE_ENABLE) != 0) {
1645 I915_WRITE(dspcntr_reg, temp & ~DISPLAY_PLANE_ENABLE);
1646 /* Flush the plane changes */
1647 I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
1648 I915_READ(dspbase_reg);
1649 }
1650
1b3c7a47
ZW
1651 i915_disable_vga(dev);
1652
2c07245f
ZW
1653 /* disable cpu pipe, disable after all planes disabled */
1654 temp = I915_READ(pipeconf_reg);
1655 if ((temp & PIPEACONF_ENABLE) != 0) {
1656 I915_WRITE(pipeconf_reg, temp & ~PIPEACONF_ENABLE);
1657 I915_READ(pipeconf_reg);
249c0e64 1658 n = 0;
2c07245f 1659 /* wait for cpu pipe off, pipe state */
249c0e64
ZW
1660 while ((I915_READ(pipeconf_reg) & I965_PIPECONF_ACTIVE) != 0) {
1661 n++;
1662 if (n < 60) {
1663 udelay(500);
1664 continue;
1665 } else {
28c97730
ZY
1666 DRM_DEBUG_KMS("pipe %d off delay\n",
1667 pipe);
249c0e64
ZW
1668 break;
1669 }
1670 }
2c07245f 1671 } else
28c97730 1672 DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
2c07245f 1673
1b3c7a47
ZW
1674 udelay(100);
1675
1676 /* Disable PF */
1677 temp = I915_READ(pf_ctl_reg);
1678 if ((temp & PF_ENABLE) != 0) {
1679 I915_WRITE(pf_ctl_reg, temp & ~PF_ENABLE);
1680 I915_READ(pf_ctl_reg);
32f9d658 1681 }
1b3c7a47 1682 I915_WRITE(pf_win_size, 0);
32f9d658 1683
2c07245f
ZW
1684 /* disable CPU FDI tx and PCH FDI rx */
1685 temp = I915_READ(fdi_tx_reg);
1686 I915_WRITE(fdi_tx_reg, temp & ~FDI_TX_ENABLE);
1687 I915_READ(fdi_tx_reg);
1688
1689 temp = I915_READ(fdi_rx_reg);
8faf3b31
ZY
1690 /* BPC in FDI rx is consistent with that in pipeconf */
1691 temp &= ~(0x07 << 16);
1692 temp |= (pipe_bpc << 11);
2c07245f
ZW
1693 I915_WRITE(fdi_rx_reg, temp & ~FDI_RX_ENABLE);
1694 I915_READ(fdi_rx_reg);
1695
249c0e64
ZW
1696 udelay(100);
1697
2c07245f
ZW
1698 /* still set train pattern 1 */
1699 temp = I915_READ(fdi_tx_reg);
1700 temp &= ~FDI_LINK_TRAIN_NONE;
1701 temp |= FDI_LINK_TRAIN_PATTERN_1;
1702 I915_WRITE(fdi_tx_reg, temp);
1703
1704 temp = I915_READ(fdi_rx_reg);
1705 temp &= ~FDI_LINK_TRAIN_NONE;
1706 temp |= FDI_LINK_TRAIN_PATTERN_1;
1707 I915_WRITE(fdi_rx_reg, temp);
1708
249c0e64
ZW
1709 udelay(100);
1710
1b3c7a47
ZW
1711 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
1712 temp = I915_READ(PCH_LVDS);
1713 I915_WRITE(PCH_LVDS, temp & ~LVDS_PORT_EN);
1714 I915_READ(PCH_LVDS);
1715 udelay(100);
1716 }
1717
2c07245f
ZW
1718 /* disable PCH transcoder */
1719 temp = I915_READ(transconf_reg);
1720 if ((temp & TRANS_ENABLE) != 0) {
1721 I915_WRITE(transconf_reg, temp & ~TRANS_ENABLE);
1722 I915_READ(transconf_reg);
249c0e64 1723 n = 0;
2c07245f 1724 /* wait for PCH transcoder off, transcoder state */
249c0e64
ZW
1725 while ((I915_READ(transconf_reg) & TRANS_STATE_ENABLE) != 0) {
1726 n++;
1727 if (n < 60) {
1728 udelay(500);
1729 continue;
1730 } else {
28c97730
ZY
1731 DRM_DEBUG_KMS("transcoder %d off "
1732 "delay\n", pipe);
249c0e64
ZW
1733 break;
1734 }
1735 }
2c07245f 1736 }
8faf3b31
ZY
1737 temp = I915_READ(transconf_reg);
1738 /* BPC in transcoder is consistent with that in pipeconf */
1739 temp &= ~PIPE_BPC_MASK;
1740 temp |= pipe_bpc;
1741 I915_WRITE(transconf_reg, temp);
1742 I915_READ(transconf_reg);
1b3c7a47
ZW
1743 udelay(100);
1744
2c07245f
ZW
1745 /* disable PCH DPLL */
1746 temp = I915_READ(pch_dpll_reg);
1747 if ((temp & DPLL_VCO_ENABLE) != 0) {
1748 I915_WRITE(pch_dpll_reg, temp & ~DPLL_VCO_ENABLE);
1749 I915_READ(pch_dpll_reg);
1750 }
1751
1b3c7a47 1752 if (HAS_eDP) {
f2b115e6 1753 ironlake_disable_pll_edp(crtc);
2c07245f
ZW
1754 }
1755
1b3c7a47
ZW
1756 temp = I915_READ(fdi_rx_reg);
1757 temp &= ~FDI_SEL_PCDCLK;
1758 I915_WRITE(fdi_rx_reg, temp);
1759 I915_READ(fdi_rx_reg);
1760
1761 temp = I915_READ(fdi_rx_reg);
1762 temp &= ~FDI_RX_PLL_ENABLE;
1763 I915_WRITE(fdi_rx_reg, temp);
1764 I915_READ(fdi_rx_reg);
1765
249c0e64
ZW
1766 /* Disable CPU FDI TX PLL */
1767 temp = I915_READ(fdi_tx_reg);
1768 if ((temp & FDI_TX_PLL_ENABLE) != 0) {
1769 I915_WRITE(fdi_tx_reg, temp & ~FDI_TX_PLL_ENABLE);
1770 I915_READ(fdi_tx_reg);
1771 udelay(100);
1772 }
1773
2c07245f 1774 /* Wait for the clocks to turn off. */
1b3c7a47 1775 udelay(100);
2c07245f
ZW
1776 break;
1777 }
1778}
1779
02e792fb
DV
1780static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
1781{
1782 struct intel_overlay *overlay;
03f77ea5 1783 int ret;
02e792fb
DV
1784
1785 if (!enable && intel_crtc->overlay) {
1786 overlay = intel_crtc->overlay;
1787 mutex_lock(&overlay->dev->struct_mutex);
03f77ea5
DV
1788 for (;;) {
1789 ret = intel_overlay_switch_off(overlay);
1790 if (ret == 0)
1791 break;
1792
1793 ret = intel_overlay_recover_from_interrupt(overlay, 0);
1794 if (ret != 0) {
1795 /* overlay doesn't react anymore. Usually
1796 * results in a black screen and an unkillable
1797 * X server. */
1798 BUG();
1799 overlay->hw_wedged = HW_WEDGED;
1800 break;
1801 }
1802 }
02e792fb
DV
1803 mutex_unlock(&overlay->dev->struct_mutex);
1804 }
1805 /* Let userspace switch the overlay on again. In most cases userspace
1806 * has to recompute where to put it anyway. */
1807
1808 return;
1809}
1810
2c07245f 1811static void i9xx_crtc_dpms(struct drm_crtc *crtc, int mode)
79e53945
JB
1812{
1813 struct drm_device *dev = crtc->dev;
79e53945
JB
1814 struct drm_i915_private *dev_priv = dev->dev_private;
1815 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1816 int pipe = intel_crtc->pipe;
80824003 1817 int plane = intel_crtc->plane;
79e53945 1818 int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
80824003
JB
1819 int dspcntr_reg = (plane == 0) ? DSPACNTR : DSPBCNTR;
1820 int dspbase_reg = (plane == 0) ? DSPAADDR : DSPBADDR;
79e53945
JB
1821 int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
1822 u32 temp;
79e53945
JB
1823
1824 /* XXX: When our outputs are all unaware of DPMS modes other than off
1825 * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
1826 */
1827 switch (mode) {
1828 case DRM_MODE_DPMS_ON:
1829 case DRM_MODE_DPMS_STANDBY:
1830 case DRM_MODE_DPMS_SUSPEND:
629598da
JB
1831 intel_update_watermarks(dev);
1832
79e53945
JB
1833 /* Enable the DPLL */
1834 temp = I915_READ(dpll_reg);
1835 if ((temp & DPLL_VCO_ENABLE) == 0) {
1836 I915_WRITE(dpll_reg, temp);
1837 I915_READ(dpll_reg);
1838 /* Wait for the clocks to stabilize. */
1839 udelay(150);
1840 I915_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
1841 I915_READ(dpll_reg);
1842 /* Wait for the clocks to stabilize. */
1843 udelay(150);
1844 I915_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
1845 I915_READ(dpll_reg);
1846 /* Wait for the clocks to stabilize. */
1847 udelay(150);
1848 }
1849
1850 /* Enable the pipe */
1851 temp = I915_READ(pipeconf_reg);
1852 if ((temp & PIPEACONF_ENABLE) == 0)
1853 I915_WRITE(pipeconf_reg, temp | PIPEACONF_ENABLE);
1854
1855 /* Enable the plane */
1856 temp = I915_READ(dspcntr_reg);
1857 if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
1858 I915_WRITE(dspcntr_reg, temp | DISPLAY_PLANE_ENABLE);
1859 /* Flush the plane changes */
1860 I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
1861 }
1862
1863 intel_crtc_load_lut(crtc);
1864
74dff282
JB
1865 if ((IS_I965G(dev) || plane == 0))
1866 intel_update_fbc(crtc, &crtc->mode);
80824003 1867
79e53945 1868 /* Give the overlay scaler a chance to enable if it's on this pipe */
02e792fb 1869 intel_crtc_dpms_overlay(intel_crtc, true);
79e53945
JB
1870 break;
1871 case DRM_MODE_DPMS_OFF:
7662c8bd 1872 intel_update_watermarks(dev);
02e792fb 1873
79e53945 1874 /* Give the overlay scaler a chance to disable if it's on this pipe */
02e792fb 1875 intel_crtc_dpms_overlay(intel_crtc, false);
778c9026 1876 drm_vblank_off(dev, pipe);
79e53945 1877
e70236a8
JB
1878 if (dev_priv->cfb_plane == plane &&
1879 dev_priv->display.disable_fbc)
1880 dev_priv->display.disable_fbc(dev);
80824003 1881
79e53945 1882 /* Disable the VGA plane that we never use */
24f119c7 1883 i915_disable_vga(dev);
79e53945
JB
1884
1885 /* Disable display plane */
1886 temp = I915_READ(dspcntr_reg);
1887 if ((temp & DISPLAY_PLANE_ENABLE) != 0) {
1888 I915_WRITE(dspcntr_reg, temp & ~DISPLAY_PLANE_ENABLE);
1889 /* Flush the plane changes */
1890 I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
1891 I915_READ(dspbase_reg);
1892 }
1893
1894 if (!IS_I9XX(dev)) {
1895 /* Wait for vblank for the disable to take effect */
1896 intel_wait_for_vblank(dev);
1897 }
1898
1899 /* Next, disable display pipes */
1900 temp = I915_READ(pipeconf_reg);
1901 if ((temp & PIPEACONF_ENABLE) != 0) {
1902 I915_WRITE(pipeconf_reg, temp & ~PIPEACONF_ENABLE);
1903 I915_READ(pipeconf_reg);
1904 }
1905
1906 /* Wait for vblank for the disable to take effect. */
1907 intel_wait_for_vblank(dev);
1908
1909 temp = I915_READ(dpll_reg);
1910 if ((temp & DPLL_VCO_ENABLE) != 0) {
1911 I915_WRITE(dpll_reg, temp & ~DPLL_VCO_ENABLE);
1912 I915_READ(dpll_reg);
1913 }
1914
1915 /* Wait for the clocks to turn off. */
1916 udelay(150);
1917 break;
1918 }
2c07245f
ZW
1919}
1920
1921/**
1922 * Sets the power management mode of the pipe and plane.
1923 *
1924 * This code should probably grow support for turning the cursor off and back
1925 * on appropriately at the same time as we're turning the pipe off/on.
1926 */
1927static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
1928{
1929 struct drm_device *dev = crtc->dev;
e70236a8 1930 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f
ZW
1931 struct drm_i915_master_private *master_priv;
1932 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1933 int pipe = intel_crtc->pipe;
1934 bool enabled;
1935
e70236a8 1936 dev_priv->display.dpms(crtc, mode);
79e53945 1937
65655d4a
DV
1938 intel_crtc->dpms_mode = mode;
1939
79e53945
JB
1940 if (!dev->primary->master)
1941 return;
1942
1943 master_priv = dev->primary->master->driver_priv;
1944 if (!master_priv->sarea_priv)
1945 return;
1946
1947 enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
1948
1949 switch (pipe) {
1950 case 0:
1951 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
1952 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
1953 break;
1954 case 1:
1955 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
1956 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
1957 break;
1958 default:
1959 DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
1960 break;
1961 }
79e53945
JB
1962}
1963
1964static void intel_crtc_prepare (struct drm_crtc *crtc)
1965{
1966 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1967 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
1968}
1969
1970static void intel_crtc_commit (struct drm_crtc *crtc)
1971{
1972 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1973 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
1974}
1975
1976void intel_encoder_prepare (struct drm_encoder *encoder)
1977{
1978 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1979 /* lvds has its own version of prepare see intel_lvds_prepare */
1980 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
1981}
1982
1983void intel_encoder_commit (struct drm_encoder *encoder)
1984{
1985 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1986 /* lvds has its own version of commit see intel_lvds_commit */
1987 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
1988}
1989
1990static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
1991 struct drm_display_mode *mode,
1992 struct drm_display_mode *adjusted_mode)
1993{
2c07245f 1994 struct drm_device *dev = crtc->dev;
f2b115e6 1995 if (IS_IRONLAKE(dev)) {
2c07245f
ZW
1996 /* FDI link clock is fixed at 2.7G */
1997 if (mode->clock * 3 > 27000 * 4)
1998 return MODE_CLOCK_HIGH;
1999 }
79e53945
JB
2000 return true;
2001}
2002
e70236a8
JB
2003static int i945_get_display_clock_speed(struct drm_device *dev)
2004{
2005 return 400000;
2006}
79e53945 2007
e70236a8 2008static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 2009{
e70236a8
JB
2010 return 333000;
2011}
79e53945 2012
e70236a8
JB
2013static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
2014{
2015 return 200000;
2016}
79e53945 2017
e70236a8
JB
2018static int i915gm_get_display_clock_speed(struct drm_device *dev)
2019{
2020 u16 gcfgc = 0;
79e53945 2021
e70236a8
JB
2022 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
2023
2024 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
2025 return 133000;
2026 else {
2027 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
2028 case GC_DISPLAY_CLOCK_333_MHZ:
2029 return 333000;
2030 default:
2031 case GC_DISPLAY_CLOCK_190_200_MHZ:
2032 return 190000;
79e53945 2033 }
e70236a8
JB
2034 }
2035}
2036
2037static int i865_get_display_clock_speed(struct drm_device *dev)
2038{
2039 return 266000;
2040}
2041
2042static int i855_get_display_clock_speed(struct drm_device *dev)
2043{
2044 u16 hpllcc = 0;
2045 /* Assume that the hardware is in the high speed state. This
2046 * should be the default.
2047 */
2048 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
2049 case GC_CLOCK_133_200:
2050 case GC_CLOCK_100_200:
2051 return 200000;
2052 case GC_CLOCK_166_250:
2053 return 250000;
2054 case GC_CLOCK_100_133:
79e53945 2055 return 133000;
e70236a8 2056 }
79e53945 2057
e70236a8
JB
2058 /* Shouldn't happen */
2059 return 0;
2060}
79e53945 2061
e70236a8
JB
2062static int i830_get_display_clock_speed(struct drm_device *dev)
2063{
2064 return 133000;
79e53945
JB
2065}
2066
79e53945
JB
2067/**
2068 * Return the pipe currently connected to the panel fitter,
2069 * or -1 if the panel fitter is not present or not in use
2070 */
02e792fb 2071int intel_panel_fitter_pipe (struct drm_device *dev)
79e53945
JB
2072{
2073 struct drm_i915_private *dev_priv = dev->dev_private;
2074 u32 pfit_control;
2075
2076 /* i830 doesn't have a panel fitter */
2077 if (IS_I830(dev))
2078 return -1;
2079
2080 pfit_control = I915_READ(PFIT_CONTROL);
2081
2082 /* See if the panel fitter is in use */
2083 if ((pfit_control & PFIT_ENABLE) == 0)
2084 return -1;
2085
2086 /* 965 can place panel fitter on either pipe */
2087 if (IS_I965G(dev))
2088 return (pfit_control >> 29) & 0x3;
2089
2090 /* older chips can only use pipe 1 */
2091 return 1;
2092}
2093
2c07245f
ZW
2094struct fdi_m_n {
2095 u32 tu;
2096 u32 gmch_m;
2097 u32 gmch_n;
2098 u32 link_m;
2099 u32 link_n;
2100};
2101
2102static void
2103fdi_reduce_ratio(u32 *num, u32 *den)
2104{
2105 while (*num > 0xffffff || *den > 0xffffff) {
2106 *num >>= 1;
2107 *den >>= 1;
2108 }
2109}
2110
2111#define DATA_N 0x800000
2112#define LINK_N 0x80000
2113
2114static void
f2b115e6
AJ
2115ironlake_compute_m_n(int bits_per_pixel, int nlanes, int pixel_clock,
2116 int link_clock, struct fdi_m_n *m_n)
2c07245f
ZW
2117{
2118 u64 temp;
2119
2120 m_n->tu = 64; /* default size */
2121
2122 temp = (u64) DATA_N * pixel_clock;
2123 temp = div_u64(temp, link_clock);
58a27471
ZW
2124 m_n->gmch_m = div_u64(temp * bits_per_pixel, nlanes);
2125 m_n->gmch_m >>= 3; /* convert to bytes_per_pixel */
2c07245f
ZW
2126 m_n->gmch_n = DATA_N;
2127 fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
2128
2129 temp = (u64) LINK_N * pixel_clock;
2130 m_n->link_m = div_u64(temp, link_clock);
2131 m_n->link_n = LINK_N;
2132 fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
2133}
2134
2135
7662c8bd
SL
2136struct intel_watermark_params {
2137 unsigned long fifo_size;
2138 unsigned long max_wm;
2139 unsigned long default_wm;
2140 unsigned long guard_size;
2141 unsigned long cacheline_size;
2142};
2143
f2b115e6
AJ
2144/* Pineview has different values for various configs */
2145static struct intel_watermark_params pineview_display_wm = {
2146 PINEVIEW_DISPLAY_FIFO,
2147 PINEVIEW_MAX_WM,
2148 PINEVIEW_DFT_WM,
2149 PINEVIEW_GUARD_WM,
2150 PINEVIEW_FIFO_LINE_SIZE
7662c8bd 2151};
f2b115e6
AJ
2152static struct intel_watermark_params pineview_display_hplloff_wm = {
2153 PINEVIEW_DISPLAY_FIFO,
2154 PINEVIEW_MAX_WM,
2155 PINEVIEW_DFT_HPLLOFF_WM,
2156 PINEVIEW_GUARD_WM,
2157 PINEVIEW_FIFO_LINE_SIZE
7662c8bd 2158};
f2b115e6
AJ
2159static struct intel_watermark_params pineview_cursor_wm = {
2160 PINEVIEW_CURSOR_FIFO,
2161 PINEVIEW_CURSOR_MAX_WM,
2162 PINEVIEW_CURSOR_DFT_WM,
2163 PINEVIEW_CURSOR_GUARD_WM,
2164 PINEVIEW_FIFO_LINE_SIZE,
7662c8bd 2165};
f2b115e6
AJ
2166static struct intel_watermark_params pineview_cursor_hplloff_wm = {
2167 PINEVIEW_CURSOR_FIFO,
2168 PINEVIEW_CURSOR_MAX_WM,
2169 PINEVIEW_CURSOR_DFT_WM,
2170 PINEVIEW_CURSOR_GUARD_WM,
2171 PINEVIEW_FIFO_LINE_SIZE
7662c8bd 2172};
0e442c60
JB
2173static struct intel_watermark_params g4x_wm_info = {
2174 G4X_FIFO_SIZE,
2175 G4X_MAX_WM,
2176 G4X_MAX_WM,
2177 2,
2178 G4X_FIFO_LINE_SIZE,
2179};
7662c8bd 2180static struct intel_watermark_params i945_wm_info = {
dff33cfc 2181 I945_FIFO_SIZE,
7662c8bd
SL
2182 I915_MAX_WM,
2183 1,
dff33cfc
JB
2184 2,
2185 I915_FIFO_LINE_SIZE
7662c8bd
SL
2186};
2187static struct intel_watermark_params i915_wm_info = {
dff33cfc 2188 I915_FIFO_SIZE,
7662c8bd
SL
2189 I915_MAX_WM,
2190 1,
dff33cfc 2191 2,
7662c8bd
SL
2192 I915_FIFO_LINE_SIZE
2193};
2194static struct intel_watermark_params i855_wm_info = {
2195 I855GM_FIFO_SIZE,
2196 I915_MAX_WM,
2197 1,
dff33cfc 2198 2,
7662c8bd
SL
2199 I830_FIFO_LINE_SIZE
2200};
2201static struct intel_watermark_params i830_wm_info = {
2202 I830_FIFO_SIZE,
2203 I915_MAX_WM,
2204 1,
dff33cfc 2205 2,
7662c8bd
SL
2206 I830_FIFO_LINE_SIZE
2207};
2208
dff33cfc
JB
2209/**
2210 * intel_calculate_wm - calculate watermark level
2211 * @clock_in_khz: pixel clock
2212 * @wm: chip FIFO params
2213 * @pixel_size: display pixel size
2214 * @latency_ns: memory latency for the platform
2215 *
2216 * Calculate the watermark level (the level at which the display plane will
2217 * start fetching from memory again). Each chip has a different display
2218 * FIFO size and allocation, so the caller needs to figure that out and pass
2219 * in the correct intel_watermark_params structure.
2220 *
2221 * As the pixel clock runs, the FIFO will be drained at a rate that depends
2222 * on the pixel size. When it reaches the watermark level, it'll start
2223 * fetching FIFO line sized based chunks from memory until the FIFO fills
2224 * past the watermark point. If the FIFO drains completely, a FIFO underrun
2225 * will occur, and a display engine hang could result.
2226 */
7662c8bd
SL
2227static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
2228 struct intel_watermark_params *wm,
2229 int pixel_size,
2230 unsigned long latency_ns)
2231{
390c4dd4 2232 long entries_required, wm_size;
dff33cfc 2233
d660467c
JB
2234 /*
2235 * Note: we need to make sure we don't overflow for various clock &
2236 * latency values.
2237 * clocks go from a few thousand to several hundred thousand.
2238 * latency is usually a few thousand
2239 */
2240 entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
2241 1000;
dff33cfc 2242 entries_required /= wm->cacheline_size;
7662c8bd 2243
28c97730 2244 DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries_required);
dff33cfc
JB
2245
2246 wm_size = wm->fifo_size - (entries_required + wm->guard_size);
2247
28c97730 2248 DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
7662c8bd 2249
390c4dd4
JB
2250 /* Don't promote wm_size to unsigned... */
2251 if (wm_size > (long)wm->max_wm)
7662c8bd 2252 wm_size = wm->max_wm;
390c4dd4 2253 if (wm_size <= 0)
7662c8bd
SL
2254 wm_size = wm->default_wm;
2255 return wm_size;
2256}
2257
2258struct cxsr_latency {
2259 int is_desktop;
2260 unsigned long fsb_freq;
2261 unsigned long mem_freq;
2262 unsigned long display_sr;
2263 unsigned long display_hpll_disable;
2264 unsigned long cursor_sr;
2265 unsigned long cursor_hpll_disable;
2266};
2267
2268static struct cxsr_latency cxsr_latency_table[] = {
2269 {1, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
2270 {1, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
2271 {1, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
2272
2273 {1, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
2274 {1, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
2275 {1, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
2276
2277 {1, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
2278 {1, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
2279 {1, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
2280
2281 {0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
2282 {0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
2283 {0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
2284
2285 {0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
2286 {0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
2287 {0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
2288
2289 {0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
2290 {0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
2291 {0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
2292};
2293
2294static struct cxsr_latency *intel_get_cxsr_latency(int is_desktop, int fsb,
2295 int mem)
2296{
2297 int i;
2298 struct cxsr_latency *latency;
2299
2300 if (fsb == 0 || mem == 0)
2301 return NULL;
2302
2303 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
2304 latency = &cxsr_latency_table[i];
2305 if (is_desktop == latency->is_desktop &&
decbbcda
JSR
2306 fsb == latency->fsb_freq && mem == latency->mem_freq)
2307 return latency;
7662c8bd 2308 }
decbbcda 2309
28c97730 2310 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
decbbcda
JSR
2311
2312 return NULL;
7662c8bd
SL
2313}
2314
f2b115e6 2315static void pineview_disable_cxsr(struct drm_device *dev)
7662c8bd
SL
2316{
2317 struct drm_i915_private *dev_priv = dev->dev_private;
2318 u32 reg;
2319
2320 /* deactivate cxsr */
2321 reg = I915_READ(DSPFW3);
f2b115e6 2322 reg &= ~(PINEVIEW_SELF_REFRESH_EN);
7662c8bd
SL
2323 I915_WRITE(DSPFW3, reg);
2324 DRM_INFO("Big FIFO is disabled\n");
2325}
2326
f2b115e6
AJ
2327static void pineview_enable_cxsr(struct drm_device *dev, unsigned long clock,
2328 int pixel_size)
7662c8bd
SL
2329{
2330 struct drm_i915_private *dev_priv = dev->dev_private;
2331 u32 reg;
2332 unsigned long wm;
2333 struct cxsr_latency *latency;
2334
f2b115e6 2335 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->fsb_freq,
7662c8bd
SL
2336 dev_priv->mem_freq);
2337 if (!latency) {
28c97730 2338 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
f2b115e6 2339 pineview_disable_cxsr(dev);
7662c8bd
SL
2340 return;
2341 }
2342
2343 /* Display SR */
f2b115e6 2344 wm = intel_calculate_wm(clock, &pineview_display_wm, pixel_size,
7662c8bd
SL
2345 latency->display_sr);
2346 reg = I915_READ(DSPFW1);
2347 reg &= 0x7fffff;
2348 reg |= wm << 23;
2349 I915_WRITE(DSPFW1, reg);
28c97730 2350 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
7662c8bd
SL
2351
2352 /* cursor SR */
f2b115e6 2353 wm = intel_calculate_wm(clock, &pineview_cursor_wm, pixel_size,
7662c8bd
SL
2354 latency->cursor_sr);
2355 reg = I915_READ(DSPFW3);
2356 reg &= ~(0x3f << 24);
2357 reg |= (wm & 0x3f) << 24;
2358 I915_WRITE(DSPFW3, reg);
2359
2360 /* Display HPLL off SR */
f2b115e6 2361 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
7662c8bd
SL
2362 latency->display_hpll_disable, I915_FIFO_LINE_SIZE);
2363 reg = I915_READ(DSPFW3);
2364 reg &= 0xfffffe00;
2365 reg |= wm & 0x1ff;
2366 I915_WRITE(DSPFW3, reg);
2367
2368 /* cursor HPLL off SR */
f2b115e6 2369 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm, pixel_size,
7662c8bd
SL
2370 latency->cursor_hpll_disable);
2371 reg = I915_READ(DSPFW3);
2372 reg &= ~(0x3f << 16);
2373 reg |= (wm & 0x3f) << 16;
2374 I915_WRITE(DSPFW3, reg);
28c97730 2375 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
7662c8bd
SL
2376
2377 /* activate cxsr */
2378 reg = I915_READ(DSPFW3);
f2b115e6 2379 reg |= PINEVIEW_SELF_REFRESH_EN;
7662c8bd
SL
2380 I915_WRITE(DSPFW3, reg);
2381
2382 DRM_INFO("Big FIFO is enabled\n");
2383
2384 return;
2385}
2386
bcc24fb4
JB
2387/*
2388 * Latency for FIFO fetches is dependent on several factors:
2389 * - memory configuration (speed, channels)
2390 * - chipset
2391 * - current MCH state
2392 * It can be fairly high in some situations, so here we assume a fairly
2393 * pessimal value. It's a tradeoff between extra memory fetches (if we
2394 * set this value too high, the FIFO will fetch frequently to stay full)
2395 * and power consumption (set it too low to save power and we might see
2396 * FIFO underruns and display "flicker").
2397 *
2398 * A value of 5us seems to be a good balance; safe for very low end
2399 * platforms but not overly aggressive on lower latency configs.
2400 */
69e302a9 2401static const int latency_ns = 5000;
7662c8bd 2402
e70236a8 2403static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
dff33cfc
JB
2404{
2405 struct drm_i915_private *dev_priv = dev->dev_private;
2406 uint32_t dsparb = I915_READ(DSPARB);
2407 int size;
2408
e70236a8 2409 if (plane == 0)
f3601326 2410 size = dsparb & 0x7f;
e70236a8
JB
2411 else
2412 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) -
2413 (dsparb & 0x7f);
dff33cfc 2414
28c97730
ZY
2415 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
2416 plane ? "B" : "A", size);
dff33cfc
JB
2417
2418 return size;
2419}
7662c8bd 2420
e70236a8
JB
2421static int i85x_get_fifo_size(struct drm_device *dev, int plane)
2422{
2423 struct drm_i915_private *dev_priv = dev->dev_private;
2424 uint32_t dsparb = I915_READ(DSPARB);
2425 int size;
2426
2427 if (plane == 0)
2428 size = dsparb & 0x1ff;
2429 else
2430 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) -
2431 (dsparb & 0x1ff);
2432 size >>= 1; /* Convert to cachelines */
dff33cfc 2433
28c97730
ZY
2434 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
2435 plane ? "B" : "A", size);
dff33cfc
JB
2436
2437 return size;
2438}
7662c8bd 2439
e70236a8
JB
2440static int i845_get_fifo_size(struct drm_device *dev, int plane)
2441{
2442 struct drm_i915_private *dev_priv = dev->dev_private;
2443 uint32_t dsparb = I915_READ(DSPARB);
2444 int size;
2445
2446 size = dsparb & 0x7f;
2447 size >>= 2; /* Convert to cachelines */
2448
28c97730
ZY
2449 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
2450 plane ? "B" : "A",
e70236a8
JB
2451 size);
2452
2453 return size;
2454}
2455
2456static int i830_get_fifo_size(struct drm_device *dev, int plane)
2457{
2458 struct drm_i915_private *dev_priv = dev->dev_private;
2459 uint32_t dsparb = I915_READ(DSPARB);
2460 int size;
2461
2462 size = dsparb & 0x7f;
2463 size >>= 1; /* Convert to cachelines */
2464
28c97730
ZY
2465 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
2466 plane ? "B" : "A", size);
e70236a8
JB
2467
2468 return size;
2469}
2470
0e442c60
JB
2471static void g4x_update_wm(struct drm_device *dev, int planea_clock,
2472 int planeb_clock, int sr_hdisplay, int pixel_size)
652c393a
JB
2473{
2474 struct drm_i915_private *dev_priv = dev->dev_private;
0e442c60
JB
2475 int total_size, cacheline_size;
2476 int planea_wm, planeb_wm, cursora_wm, cursorb_wm, cursor_sr;
2477 struct intel_watermark_params planea_params, planeb_params;
2478 unsigned long line_time_us;
2479 int sr_clock, sr_entries = 0, entries_required;
652c393a 2480
0e442c60
JB
2481 /* Create copies of the base settings for each pipe */
2482 planea_params = planeb_params = g4x_wm_info;
2483
2484 /* Grab a couple of global values before we overwrite them */
2485 total_size = planea_params.fifo_size;
2486 cacheline_size = planea_params.cacheline_size;
2487
2488 /*
2489 * Note: we need to make sure we don't overflow for various clock &
2490 * latency values.
2491 * clocks go from a few thousand to several hundred thousand.
2492 * latency is usually a few thousand
2493 */
2494 entries_required = ((planea_clock / 1000) * pixel_size * latency_ns) /
2495 1000;
2496 entries_required /= G4X_FIFO_LINE_SIZE;
2497 planea_wm = entries_required + planea_params.guard_size;
2498
2499 entries_required = ((planeb_clock / 1000) * pixel_size * latency_ns) /
2500 1000;
2501 entries_required /= G4X_FIFO_LINE_SIZE;
2502 planeb_wm = entries_required + planeb_params.guard_size;
2503
2504 cursora_wm = cursorb_wm = 16;
2505 cursor_sr = 32;
2506
2507 DRM_DEBUG("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
2508
2509 /* Calc sr entries for one plane configs */
2510 if (sr_hdisplay && (!planea_clock || !planeb_clock)) {
2511 /* self-refresh has much higher latency */
69e302a9 2512 static const int sr_latency_ns = 12000;
0e442c60
JB
2513
2514 sr_clock = planea_clock ? planea_clock : planeb_clock;
2515 line_time_us = ((sr_hdisplay * 1000) / sr_clock);
2516
2517 /* Use ns/us then divide to preserve precision */
2518 sr_entries = (((sr_latency_ns / line_time_us) + 1) *
2519 pixel_size * sr_hdisplay) / 1000;
2520 sr_entries = roundup(sr_entries / cacheline_size, 1);
2521 DRM_DEBUG("self-refresh entries: %d\n", sr_entries);
2522 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
33c5fd12
DJ
2523 } else {
2524 /* Turn off self refresh if both pipes are enabled */
2525 I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
2526 & ~FW_BLC_SELF_EN);
0e442c60
JB
2527 }
2528
2529 DRM_DEBUG("Setting FIFO watermarks - A: %d, B: %d, SR %d\n",
2530 planea_wm, planeb_wm, sr_entries);
2531
2532 planea_wm &= 0x3f;
2533 planeb_wm &= 0x3f;
2534
2535 I915_WRITE(DSPFW1, (sr_entries << DSPFW_SR_SHIFT) |
2536 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
2537 (planeb_wm << DSPFW_PLANEB_SHIFT) | planea_wm);
2538 I915_WRITE(DSPFW2, (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
2539 (cursora_wm << DSPFW_CURSORA_SHIFT));
2540 /* HPLL off in SR has some issues on G4x... disable it */
2541 I915_WRITE(DSPFW3, (I915_READ(DSPFW3) & ~DSPFW_HPLL_SR_EN) |
2542 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
652c393a
JB
2543}
2544
1dc7546d
JB
2545static void i965_update_wm(struct drm_device *dev, int planea_clock,
2546 int planeb_clock, int sr_hdisplay, int pixel_size)
7662c8bd
SL
2547{
2548 struct drm_i915_private *dev_priv = dev->dev_private;
1dc7546d
JB
2549 unsigned long line_time_us;
2550 int sr_clock, sr_entries, srwm = 1;
2551
2552 /* Calc sr entries for one plane configs */
2553 if (sr_hdisplay && (!planea_clock || !planeb_clock)) {
2554 /* self-refresh has much higher latency */
69e302a9 2555 static const int sr_latency_ns = 12000;
1dc7546d
JB
2556
2557 sr_clock = planea_clock ? planea_clock : planeb_clock;
2558 line_time_us = ((sr_hdisplay * 1000) / sr_clock);
2559
2560 /* Use ns/us then divide to preserve precision */
2561 sr_entries = (((sr_latency_ns / line_time_us) + 1) *
2562 pixel_size * sr_hdisplay) / 1000;
2563 sr_entries = roundup(sr_entries / I915_FIFO_LINE_SIZE, 1);
2564 DRM_DEBUG("self-refresh entries: %d\n", sr_entries);
2565 srwm = I945_FIFO_SIZE - sr_entries;
2566 if (srwm < 0)
2567 srwm = 1;
2568 srwm &= 0x3f;
2569 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
33c5fd12
DJ
2570 } else {
2571 /* Turn off self refresh if both pipes are enabled */
2572 I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
2573 & ~FW_BLC_SELF_EN);
1dc7546d 2574 }
7662c8bd 2575
1dc7546d
JB
2576 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
2577 srwm);
7662c8bd
SL
2578
2579 /* 965 has limitations... */
1dc7546d
JB
2580 I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) | (8 << 16) | (8 << 8) |
2581 (8 << 0));
7662c8bd
SL
2582 I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
2583}
2584
2585static void i9xx_update_wm(struct drm_device *dev, int planea_clock,
2586 int planeb_clock, int sr_hdisplay, int pixel_size)
2587{
2588 struct drm_i915_private *dev_priv = dev->dev_private;
dff33cfc
JB
2589 uint32_t fwater_lo;
2590 uint32_t fwater_hi;
2591 int total_size, cacheline_size, cwm, srwm = 1;
2592 int planea_wm, planeb_wm;
2593 struct intel_watermark_params planea_params, planeb_params;
7662c8bd
SL
2594 unsigned long line_time_us;
2595 int sr_clock, sr_entries = 0;
2596
dff33cfc 2597 /* Create copies of the base settings for each pipe */
7662c8bd 2598 if (IS_I965GM(dev) || IS_I945GM(dev))
dff33cfc 2599 planea_params = planeb_params = i945_wm_info;
7662c8bd 2600 else if (IS_I9XX(dev))
dff33cfc 2601 planea_params = planeb_params = i915_wm_info;
7662c8bd 2602 else
dff33cfc 2603 planea_params = planeb_params = i855_wm_info;
7662c8bd 2604
dff33cfc
JB
2605 /* Grab a couple of global values before we overwrite them */
2606 total_size = planea_params.fifo_size;
2607 cacheline_size = planea_params.cacheline_size;
7662c8bd 2608
dff33cfc 2609 /* Update per-plane FIFO sizes */
e70236a8
JB
2610 planea_params.fifo_size = dev_priv->display.get_fifo_size(dev, 0);
2611 planeb_params.fifo_size = dev_priv->display.get_fifo_size(dev, 1);
7662c8bd 2612
dff33cfc
JB
2613 planea_wm = intel_calculate_wm(planea_clock, &planea_params,
2614 pixel_size, latency_ns);
2615 planeb_wm = intel_calculate_wm(planeb_clock, &planeb_params,
2616 pixel_size, latency_ns);
28c97730 2617 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
7662c8bd
SL
2618
2619 /*
2620 * Overlay gets an aggressive default since video jitter is bad.
2621 */
2622 cwm = 2;
2623
dff33cfc 2624 /* Calc sr entries for one plane configs */
652c393a
JB
2625 if (HAS_FW_BLC(dev) && sr_hdisplay &&
2626 (!planea_clock || !planeb_clock)) {
dff33cfc 2627 /* self-refresh has much higher latency */
69e302a9 2628 static const int sr_latency_ns = 6000;
dff33cfc 2629
7662c8bd 2630 sr_clock = planea_clock ? planea_clock : planeb_clock;
dff33cfc
JB
2631 line_time_us = ((sr_hdisplay * 1000) / sr_clock);
2632
2633 /* Use ns/us then divide to preserve precision */
2634 sr_entries = (((sr_latency_ns / line_time_us) + 1) *
2635 pixel_size * sr_hdisplay) / 1000;
2636 sr_entries = roundup(sr_entries / cacheline_size, 1);
28c97730 2637 DRM_DEBUG_KMS("self-refresh entries: %d\n", sr_entries);
dff33cfc
JB
2638 srwm = total_size - sr_entries;
2639 if (srwm < 0)
2640 srwm = 1;
652c393a 2641 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN | (srwm & 0x3f));
33c5fd12
DJ
2642 } else {
2643 /* Turn off self refresh if both pipes are enabled */
2644 I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
2645 & ~FW_BLC_SELF_EN);
7662c8bd
SL
2646 }
2647
28c97730 2648 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
dff33cfc 2649 planea_wm, planeb_wm, cwm, srwm);
7662c8bd 2650
dff33cfc
JB
2651 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
2652 fwater_hi = (cwm & 0x1f);
2653
2654 /* Set request length to 8 cachelines per fetch */
2655 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
2656 fwater_hi = fwater_hi | (1 << 8);
7662c8bd
SL
2657
2658 I915_WRITE(FW_BLC, fwater_lo);
2659 I915_WRITE(FW_BLC2, fwater_hi);
7662c8bd
SL
2660}
2661
e70236a8
JB
2662static void i830_update_wm(struct drm_device *dev, int planea_clock, int unused,
2663 int unused2, int pixel_size)
7662c8bd
SL
2664{
2665 struct drm_i915_private *dev_priv = dev->dev_private;
f3601326 2666 uint32_t fwater_lo = I915_READ(FW_BLC) & ~0xfff;
dff33cfc 2667 int planea_wm;
7662c8bd 2668
e70236a8 2669 i830_wm_info.fifo_size = dev_priv->display.get_fifo_size(dev, 0);
7662c8bd 2670
dff33cfc
JB
2671 planea_wm = intel_calculate_wm(planea_clock, &i830_wm_info,
2672 pixel_size, latency_ns);
f3601326
JB
2673 fwater_lo |= (3<<8) | planea_wm;
2674
28c97730 2675 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
7662c8bd
SL
2676
2677 I915_WRITE(FW_BLC, fwater_lo);
2678}
2679
2680/**
2681 * intel_update_watermarks - update FIFO watermark values based on current modes
2682 *
2683 * Calculate watermark values for the various WM regs based on current mode
2684 * and plane configuration.
2685 *
2686 * There are several cases to deal with here:
2687 * - normal (i.e. non-self-refresh)
2688 * - self-refresh (SR) mode
2689 * - lines are large relative to FIFO size (buffer can hold up to 2)
2690 * - lines are small relative to FIFO size (buffer can hold more than 2
2691 * lines), so need to account for TLB latency
2692 *
2693 * The normal calculation is:
2694 * watermark = dotclock * bytes per pixel * latency
2695 * where latency is platform & configuration dependent (we assume pessimal
2696 * values here).
2697 *
2698 * The SR calculation is:
2699 * watermark = (trunc(latency/line time)+1) * surface width *
2700 * bytes per pixel
2701 * where
2702 * line time = htotal / dotclock
2703 * and latency is assumed to be high, as above.
2704 *
2705 * The final value programmed to the register should always be rounded up,
2706 * and include an extra 2 entries to account for clock crossings.
2707 *
2708 * We don't use the sprite, so we can ignore that. And on Crestline we have
2709 * to set the non-SR watermarks to 8.
2710 */
2711static void intel_update_watermarks(struct drm_device *dev)
2712{
e70236a8 2713 struct drm_i915_private *dev_priv = dev->dev_private;
7662c8bd
SL
2714 struct drm_crtc *crtc;
2715 struct intel_crtc *intel_crtc;
2716 int sr_hdisplay = 0;
2717 unsigned long planea_clock = 0, planeb_clock = 0, sr_clock = 0;
2718 int enabled = 0, pixel_size = 0;
2719
c03342fa
ZW
2720 if (!dev_priv->display.update_wm)
2721 return;
2722
7662c8bd
SL
2723 /* Get the clock config from both planes */
2724 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2725 intel_crtc = to_intel_crtc(crtc);
2726 if (crtc->enabled) {
2727 enabled++;
2728 if (intel_crtc->plane == 0) {
28c97730 2729 DRM_DEBUG_KMS("plane A (pipe %d) clock: %d\n",
7662c8bd
SL
2730 intel_crtc->pipe, crtc->mode.clock);
2731 planea_clock = crtc->mode.clock;
2732 } else {
28c97730 2733 DRM_DEBUG_KMS("plane B (pipe %d) clock: %d\n",
7662c8bd
SL
2734 intel_crtc->pipe, crtc->mode.clock);
2735 planeb_clock = crtc->mode.clock;
2736 }
2737 sr_hdisplay = crtc->mode.hdisplay;
2738 sr_clock = crtc->mode.clock;
2739 if (crtc->fb)
2740 pixel_size = crtc->fb->bits_per_pixel / 8;
2741 else
2742 pixel_size = 4; /* by default */
2743 }
2744 }
2745
2746 if (enabled <= 0)
2747 return;
2748
dff33cfc 2749 /* Single plane configs can enable self refresh */
f2b115e6
AJ
2750 if (enabled == 1 && IS_PINEVIEW(dev))
2751 pineview_enable_cxsr(dev, sr_clock, pixel_size);
2752 else if (IS_PINEVIEW(dev))
2753 pineview_disable_cxsr(dev);
7662c8bd 2754
e70236a8
JB
2755 dev_priv->display.update_wm(dev, planea_clock, planeb_clock,
2756 sr_hdisplay, pixel_size);
7662c8bd
SL
2757}
2758
5c3b82e2
CW
2759static int intel_crtc_mode_set(struct drm_crtc *crtc,
2760 struct drm_display_mode *mode,
2761 struct drm_display_mode *adjusted_mode,
2762 int x, int y,
2763 struct drm_framebuffer *old_fb)
79e53945
JB
2764{
2765 struct drm_device *dev = crtc->dev;
2766 struct drm_i915_private *dev_priv = dev->dev_private;
2767 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2768 int pipe = intel_crtc->pipe;
80824003 2769 int plane = intel_crtc->plane;
79e53945
JB
2770 int fp_reg = (pipe == 0) ? FPA0 : FPB0;
2771 int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
2772 int dpll_md_reg = (intel_crtc->pipe == 0) ? DPLL_A_MD : DPLL_B_MD;
80824003 2773 int dspcntr_reg = (plane == 0) ? DSPACNTR : DSPBCNTR;
79e53945
JB
2774 int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
2775 int htot_reg = (pipe == 0) ? HTOTAL_A : HTOTAL_B;
2776 int hblank_reg = (pipe == 0) ? HBLANK_A : HBLANK_B;
2777 int hsync_reg = (pipe == 0) ? HSYNC_A : HSYNC_B;
2778 int vtot_reg = (pipe == 0) ? VTOTAL_A : VTOTAL_B;
2779 int vblank_reg = (pipe == 0) ? VBLANK_A : VBLANK_B;
2780 int vsync_reg = (pipe == 0) ? VSYNC_A : VSYNC_B;
80824003
JB
2781 int dspsize_reg = (plane == 0) ? DSPASIZE : DSPBSIZE;
2782 int dsppos_reg = (plane == 0) ? DSPAPOS : DSPBPOS;
79e53945 2783 int pipesrc_reg = (pipe == 0) ? PIPEASRC : PIPEBSRC;
43565a06 2784 int refclk, num_outputs = 0;
652c393a
JB
2785 intel_clock_t clock, reduced_clock;
2786 u32 dpll = 0, fp = 0, fp2 = 0, dspcntr, pipeconf;
2787 bool ok, has_reduced_clock = false, is_sdvo = false, is_dvo = false;
a4fc5ed6 2788 bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
32f9d658 2789 bool is_edp = false;
79e53945
JB
2790 struct drm_mode_config *mode_config = &dev->mode_config;
2791 struct drm_connector *connector;
d4906093 2792 const intel_limit_t *limit;
5c3b82e2 2793 int ret;
2c07245f
ZW
2794 struct fdi_m_n m_n = {0};
2795 int data_m1_reg = (pipe == 0) ? PIPEA_DATA_M1 : PIPEB_DATA_M1;
2796 int data_n1_reg = (pipe == 0) ? PIPEA_DATA_N1 : PIPEB_DATA_N1;
2797 int link_m1_reg = (pipe == 0) ? PIPEA_LINK_M1 : PIPEB_LINK_M1;
2798 int link_n1_reg = (pipe == 0) ? PIPEA_LINK_N1 : PIPEB_LINK_N1;
2799 int pch_fp_reg = (pipe == 0) ? PCH_FPA0 : PCH_FPB0;
2800 int pch_dpll_reg = (pipe == 0) ? PCH_DPLL_A : PCH_DPLL_B;
2801 int fdi_rx_reg = (pipe == 0) ? FDI_RXA_CTL : FDI_RXB_CTL;
541998a1 2802 int lvds_reg = LVDS;
2c07245f
ZW
2803 u32 temp;
2804 int sdvo_pixel_multiply;
5eb08b69 2805 int target_clock;
79e53945
JB
2806
2807 drm_vblank_pre_modeset(dev, pipe);
2808
2809 list_for_each_entry(connector, &mode_config->connector_list, head) {
2810 struct intel_output *intel_output = to_intel_output(connector);
2811
2812 if (!connector->encoder || connector->encoder->crtc != crtc)
2813 continue;
2814
2815 switch (intel_output->type) {
2816 case INTEL_OUTPUT_LVDS:
2817 is_lvds = true;
2818 break;
2819 case INTEL_OUTPUT_SDVO:
7d57382e 2820 case INTEL_OUTPUT_HDMI:
79e53945 2821 is_sdvo = true;
e2f0ba97
JB
2822 if (intel_output->needs_tv_clock)
2823 is_tv = true;
79e53945
JB
2824 break;
2825 case INTEL_OUTPUT_DVO:
2826 is_dvo = true;
2827 break;
2828 case INTEL_OUTPUT_TVOUT:
2829 is_tv = true;
2830 break;
2831 case INTEL_OUTPUT_ANALOG:
2832 is_crt = true;
2833 break;
a4fc5ed6
KP
2834 case INTEL_OUTPUT_DISPLAYPORT:
2835 is_dp = true;
2836 break;
32f9d658
ZW
2837 case INTEL_OUTPUT_EDP:
2838 is_edp = true;
2839 break;
79e53945 2840 }
43565a06
KH
2841
2842 num_outputs++;
79e53945
JB
2843 }
2844
43565a06
KH
2845 if (is_lvds && dev_priv->lvds_use_ssc && num_outputs < 2) {
2846 refclk = dev_priv->lvds_ssc_freq * 1000;
28c97730
ZY
2847 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
2848 refclk / 1000);
43565a06 2849 } else if (IS_I9XX(dev)) {
79e53945 2850 refclk = 96000;
f2b115e6 2851 if (IS_IRONLAKE(dev))
2c07245f 2852 refclk = 120000; /* 120Mhz refclk */
79e53945
JB
2853 } else {
2854 refclk = 48000;
2855 }
a4fc5ed6 2856
79e53945 2857
d4906093
ML
2858 /*
2859 * Returns a set of divisors for the desired target clock with the given
2860 * refclk, or FALSE. The returned values represent the clock equation:
2861 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
2862 */
2863 limit = intel_limit(crtc);
2864 ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, &clock);
79e53945
JB
2865 if (!ok) {
2866 DRM_ERROR("Couldn't find PLL settings for mode!\n");
1f803ee5 2867 drm_vblank_post_modeset(dev, pipe);
5c3b82e2 2868 return -EINVAL;
79e53945
JB
2869 }
2870
ddc9003c
ZY
2871 if (is_lvds && dev_priv->lvds_downclock_avail) {
2872 has_reduced_clock = limit->find_pll(limit, crtc,
18f9ed12 2873 dev_priv->lvds_downclock,
652c393a
JB
2874 refclk,
2875 &reduced_clock);
18f9ed12
ZY
2876 if (has_reduced_clock && (clock.p != reduced_clock.p)) {
2877 /*
2878 * If the different P is found, it means that we can't
2879 * switch the display clock by using the FP0/FP1.
2880 * In such case we will disable the LVDS downclock
2881 * feature.
2882 */
2883 DRM_DEBUG_KMS("Different P is found for "
2884 "LVDS clock/downclock\n");
2885 has_reduced_clock = 0;
2886 }
652c393a 2887 }
7026d4ac
ZW
2888 /* SDVO TV has fixed PLL values depend on its clock range,
2889 this mirrors vbios setting. */
2890 if (is_sdvo && is_tv) {
2891 if (adjusted_mode->clock >= 100000
2892 && adjusted_mode->clock < 140500) {
2893 clock.p1 = 2;
2894 clock.p2 = 10;
2895 clock.n = 3;
2896 clock.m1 = 16;
2897 clock.m2 = 8;
2898 } else if (adjusted_mode->clock >= 140500
2899 && adjusted_mode->clock <= 200000) {
2900 clock.p1 = 1;
2901 clock.p2 = 10;
2902 clock.n = 6;
2903 clock.m1 = 12;
2904 clock.m2 = 8;
2905 }
2906 }
2907
2c07245f 2908 /* FDI link */
f2b115e6 2909 if (IS_IRONLAKE(dev)) {
58a27471 2910 int lane, link_bw, bpp;
32f9d658
ZW
2911 /* eDP doesn't require FDI link, so just set DP M/N
2912 according to current link config */
2913 if (is_edp) {
2914 struct drm_connector *edp;
5eb08b69 2915 target_clock = mode->clock;
32f9d658
ZW
2916 edp = intel_pipe_get_output(crtc);
2917 intel_edp_link_config(to_intel_output(edp),
2918 &lane, &link_bw);
2919 } else {
2920 /* DP over FDI requires target mode clock
2921 instead of link clock */
2922 if (is_dp)
2923 target_clock = mode->clock;
2924 else
2925 target_clock = adjusted_mode->clock;
2926 lane = 4;
2927 link_bw = 270000;
2928 }
58a27471
ZW
2929
2930 /* determine panel color depth */
2931 temp = I915_READ(pipeconf_reg);
e5a95eb7
ZY
2932 temp &= ~PIPE_BPC_MASK;
2933 if (is_lvds) {
2934 int lvds_reg = I915_READ(PCH_LVDS);
2935 /* the BPC will be 6 if it is 18-bit LVDS panel */
2936 if ((lvds_reg & LVDS_A3_POWER_MASK) == LVDS_A3_POWER_UP)
2937 temp |= PIPE_8BPC;
2938 else
2939 temp |= PIPE_6BPC;
885a5fb5
ZW
2940 } else if (is_edp) {
2941 switch (dev_priv->edp_bpp/3) {
2942 case 8:
2943 temp |= PIPE_8BPC;
2944 break;
2945 case 10:
2946 temp |= PIPE_10BPC;
2947 break;
2948 case 6:
2949 temp |= PIPE_6BPC;
2950 break;
2951 case 12:
2952 temp |= PIPE_12BPC;
2953 break;
2954 }
e5a95eb7
ZY
2955 } else
2956 temp |= PIPE_8BPC;
2957 I915_WRITE(pipeconf_reg, temp);
2958 I915_READ(pipeconf_reg);
58a27471
ZW
2959
2960 switch (temp & PIPE_BPC_MASK) {
2961 case PIPE_8BPC:
2962 bpp = 24;
2963 break;
2964 case PIPE_10BPC:
2965 bpp = 30;
2966 break;
2967 case PIPE_6BPC:
2968 bpp = 18;
2969 break;
2970 case PIPE_12BPC:
2971 bpp = 36;
2972 break;
2973 default:
2974 DRM_ERROR("unknown pipe bpc value\n");
2975 bpp = 24;
2976 }
2977
f2b115e6 2978 ironlake_compute_m_n(bpp, lane, target_clock, link_bw, &m_n);
5eb08b69 2979 }
2c07245f 2980
c038e51e
ZW
2981 /* Ironlake: try to setup display ref clock before DPLL
2982 * enabling. This is only under driver's control after
2983 * PCH B stepping, previous chipset stepping should be
2984 * ignoring this setting.
2985 */
f2b115e6 2986 if (IS_IRONLAKE(dev)) {
c038e51e
ZW
2987 temp = I915_READ(PCH_DREF_CONTROL);
2988 /* Always enable nonspread source */
2989 temp &= ~DREF_NONSPREAD_SOURCE_MASK;
2990 temp |= DREF_NONSPREAD_SOURCE_ENABLE;
2991 I915_WRITE(PCH_DREF_CONTROL, temp);
2992 POSTING_READ(PCH_DREF_CONTROL);
2993
2994 temp &= ~DREF_SSC_SOURCE_MASK;
2995 temp |= DREF_SSC_SOURCE_ENABLE;
2996 I915_WRITE(PCH_DREF_CONTROL, temp);
2997 POSTING_READ(PCH_DREF_CONTROL);
2998
2999 udelay(200);
3000
3001 if (is_edp) {
3002 if (dev_priv->lvds_use_ssc) {
3003 temp |= DREF_SSC1_ENABLE;
3004 I915_WRITE(PCH_DREF_CONTROL, temp);
3005 POSTING_READ(PCH_DREF_CONTROL);
3006
3007 udelay(200);
3008
3009 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
3010 temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
3011 I915_WRITE(PCH_DREF_CONTROL, temp);
3012 POSTING_READ(PCH_DREF_CONTROL);
3013 } else {
3014 temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
3015 I915_WRITE(PCH_DREF_CONTROL, temp);
3016 POSTING_READ(PCH_DREF_CONTROL);
3017 }
3018 }
3019 }
3020
f2b115e6 3021 if (IS_PINEVIEW(dev)) {
2177832f 3022 fp = (1 << clock.n) << 16 | clock.m1 << 8 | clock.m2;
652c393a
JB
3023 if (has_reduced_clock)
3024 fp2 = (1 << reduced_clock.n) << 16 |
3025 reduced_clock.m1 << 8 | reduced_clock.m2;
3026 } else {
2177832f 3027 fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
652c393a
JB
3028 if (has_reduced_clock)
3029 fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
3030 reduced_clock.m2;
3031 }
79e53945 3032
f2b115e6 3033 if (!IS_IRONLAKE(dev))
2c07245f
ZW
3034 dpll = DPLL_VGA_MODE_DIS;
3035
79e53945
JB
3036 if (IS_I9XX(dev)) {
3037 if (is_lvds)
3038 dpll |= DPLLB_MODE_LVDS;
3039 else
3040 dpll |= DPLLB_MODE_DAC_SERIAL;
3041 if (is_sdvo) {
3042 dpll |= DPLL_DVO_HIGH_SPEED;
2c07245f 3043 sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
942642a4 3044 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
79e53945 3045 dpll |= (sdvo_pixel_multiply - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
f2b115e6 3046 else if (IS_IRONLAKE(dev))
2c07245f 3047 dpll |= (sdvo_pixel_multiply - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
79e53945 3048 }
a4fc5ed6
KP
3049 if (is_dp)
3050 dpll |= DPLL_DVO_HIGH_SPEED;
79e53945
JB
3051
3052 /* compute bitmask from p1 value */
f2b115e6
AJ
3053 if (IS_PINEVIEW(dev))
3054 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
2c07245f 3055 else {
2177832f 3056 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
2c07245f 3057 /* also FPA1 */
f2b115e6 3058 if (IS_IRONLAKE(dev))
2c07245f 3059 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
652c393a
JB
3060 if (IS_G4X(dev) && has_reduced_clock)
3061 dpll |= (1 << (reduced_clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
2c07245f 3062 }
79e53945
JB
3063 switch (clock.p2) {
3064 case 5:
3065 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
3066 break;
3067 case 7:
3068 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
3069 break;
3070 case 10:
3071 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
3072 break;
3073 case 14:
3074 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
3075 break;
3076 }
f2b115e6 3077 if (IS_I965G(dev) && !IS_IRONLAKE(dev))
79e53945
JB
3078 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
3079 } else {
3080 if (is_lvds) {
3081 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
3082 } else {
3083 if (clock.p1 == 2)
3084 dpll |= PLL_P1_DIVIDE_BY_TWO;
3085 else
3086 dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
3087 if (clock.p2 == 4)
3088 dpll |= PLL_P2_DIVIDE_BY_4;
3089 }
3090 }
3091
43565a06
KH
3092 if (is_sdvo && is_tv)
3093 dpll |= PLL_REF_INPUT_TVCLKINBC;
3094 else if (is_tv)
79e53945 3095 /* XXX: just matching BIOS for now */
43565a06 3096 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
79e53945 3097 dpll |= 3;
43565a06
KH
3098 else if (is_lvds && dev_priv->lvds_use_ssc && num_outputs < 2)
3099 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
3100 else
3101 dpll |= PLL_REF_INPUT_DREFCLK;
3102
3103 /* setup pipeconf */
3104 pipeconf = I915_READ(pipeconf_reg);
3105
3106 /* Set up the display plane register */
3107 dspcntr = DISPPLANE_GAMMA_ENABLE;
3108
f2b115e6 3109 /* Ironlake's plane is forced to pipe, bit 24 is to
2c07245f 3110 enable color space conversion */
f2b115e6 3111 if (!IS_IRONLAKE(dev)) {
2c07245f 3112 if (pipe == 0)
80824003 3113 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
2c07245f
ZW
3114 else
3115 dspcntr |= DISPPLANE_SEL_PIPE_B;
3116 }
79e53945
JB
3117
3118 if (pipe == 0 && !IS_I965G(dev)) {
3119 /* Enable pixel doubling when the dot clock is > 90% of the (display)
3120 * core speed.
3121 *
3122 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
3123 * pipe == 0 check?
3124 */
e70236a8
JB
3125 if (mode->clock >
3126 dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
79e53945
JB
3127 pipeconf |= PIPEACONF_DOUBLE_WIDE;
3128 else
3129 pipeconf &= ~PIPEACONF_DOUBLE_WIDE;
3130 }
3131
3132 dspcntr |= DISPLAY_PLANE_ENABLE;
3133 pipeconf |= PIPEACONF_ENABLE;
3134 dpll |= DPLL_VCO_ENABLE;
3135
3136
3137 /* Disable the panel fitter if it was on our pipe */
f2b115e6 3138 if (!IS_IRONLAKE(dev) && intel_panel_fitter_pipe(dev) == pipe)
79e53945
JB
3139 I915_WRITE(PFIT_CONTROL, 0);
3140
28c97730 3141 DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
79e53945
JB
3142 drm_mode_debug_printmodeline(mode);
3143
f2b115e6
AJ
3144 /* assign to Ironlake registers */
3145 if (IS_IRONLAKE(dev)) {
2c07245f
ZW
3146 fp_reg = pch_fp_reg;
3147 dpll_reg = pch_dpll_reg;
3148 }
79e53945 3149
32f9d658 3150 if (is_edp) {
f2b115e6 3151 ironlake_disable_pll_edp(crtc);
32f9d658 3152 } else if ((dpll & DPLL_VCO_ENABLE)) {
79e53945
JB
3153 I915_WRITE(fp_reg, fp);
3154 I915_WRITE(dpll_reg, dpll & ~DPLL_VCO_ENABLE);
3155 I915_READ(dpll_reg);
3156 udelay(150);
3157 }
3158
3159 /* The LVDS pin pair needs to be on before the DPLLs are enabled.
3160 * This is an exception to the general rule that mode_set doesn't turn
3161 * things on.
3162 */
3163 if (is_lvds) {
541998a1 3164 u32 lvds;
79e53945 3165
f2b115e6 3166 if (IS_IRONLAKE(dev))
541998a1
ZW
3167 lvds_reg = PCH_LVDS;
3168
3169 lvds = I915_READ(lvds_reg);
79e53945 3170 lvds |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP | LVDS_PIPEB_SELECT;
a3e17eb8
ZY
3171 /* set the corresponsding LVDS_BORDER bit */
3172 lvds |= dev_priv->lvds_border_bits;
79e53945
JB
3173 /* Set the B0-B3 data pairs corresponding to whether we're going to
3174 * set the DPLLs for dual-channel mode or not.
3175 */
3176 if (clock.p2 == 7)
3177 lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
3178 else
3179 lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
3180
3181 /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
3182 * appropriately here, but we need to look more thoroughly into how
3183 * panels behave in the two modes.
3184 */
898822ce
ZY
3185 /* set the dithering flag */
3186 if (IS_I965G(dev)) {
3187 if (dev_priv->lvds_dither) {
3188 if (IS_IRONLAKE(dev))
3189 pipeconf |= PIPE_ENABLE_DITHER;
3190 else
3191 lvds |= LVDS_ENABLE_DITHER;
3192 } else {
3193 if (IS_IRONLAKE(dev))
3194 pipeconf &= ~PIPE_ENABLE_DITHER;
3195 else
3196 lvds &= ~LVDS_ENABLE_DITHER;
3197 }
3198 }
541998a1
ZW
3199 I915_WRITE(lvds_reg, lvds);
3200 I915_READ(lvds_reg);
79e53945 3201 }
a4fc5ed6
KP
3202 if (is_dp)
3203 intel_dp_set_m_n(crtc, mode, adjusted_mode);
79e53945 3204
32f9d658
ZW
3205 if (!is_edp) {
3206 I915_WRITE(fp_reg, fp);
79e53945 3207 I915_WRITE(dpll_reg, dpll);
32f9d658
ZW
3208 I915_READ(dpll_reg);
3209 /* Wait for the clocks to stabilize. */
3210 udelay(150);
3211
f2b115e6 3212 if (IS_I965G(dev) && !IS_IRONLAKE(dev)) {
bb66c512
ZY
3213 if (is_sdvo) {
3214 sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
3215 I915_WRITE(dpll_md_reg, (0 << DPLL_MD_UDI_DIVIDER_SHIFT) |
32f9d658 3216 ((sdvo_pixel_multiply - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT));
bb66c512
ZY
3217 } else
3218 I915_WRITE(dpll_md_reg, 0);
32f9d658
ZW
3219 } else {
3220 /* write it again -- the BIOS does, after all */
3221 I915_WRITE(dpll_reg, dpll);
3222 }
3223 I915_READ(dpll_reg);
3224 /* Wait for the clocks to stabilize. */
3225 udelay(150);
79e53945 3226 }
79e53945 3227
652c393a
JB
3228 if (is_lvds && has_reduced_clock && i915_powersave) {
3229 I915_WRITE(fp_reg + 4, fp2);
3230 intel_crtc->lowfreq_avail = true;
3231 if (HAS_PIPE_CXSR(dev)) {
28c97730 3232 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
652c393a
JB
3233 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
3234 }
3235 } else {
3236 I915_WRITE(fp_reg + 4, fp);
3237 intel_crtc->lowfreq_avail = false;
3238 if (HAS_PIPE_CXSR(dev)) {
28c97730 3239 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
652c393a
JB
3240 pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
3241 }
3242 }
3243
79e53945
JB
3244 I915_WRITE(htot_reg, (adjusted_mode->crtc_hdisplay - 1) |
3245 ((adjusted_mode->crtc_htotal - 1) << 16));
3246 I915_WRITE(hblank_reg, (adjusted_mode->crtc_hblank_start - 1) |
3247 ((adjusted_mode->crtc_hblank_end - 1) << 16));
3248 I915_WRITE(hsync_reg, (adjusted_mode->crtc_hsync_start - 1) |
3249 ((adjusted_mode->crtc_hsync_end - 1) << 16));
3250 I915_WRITE(vtot_reg, (adjusted_mode->crtc_vdisplay - 1) |
3251 ((adjusted_mode->crtc_vtotal - 1) << 16));
3252 I915_WRITE(vblank_reg, (adjusted_mode->crtc_vblank_start - 1) |
3253 ((adjusted_mode->crtc_vblank_end - 1) << 16));
3254 I915_WRITE(vsync_reg, (adjusted_mode->crtc_vsync_start - 1) |
3255 ((adjusted_mode->crtc_vsync_end - 1) << 16));
3256 /* pipesrc and dspsize control the size that is scaled from, which should
3257 * always be the user's requested size.
3258 */
f2b115e6 3259 if (!IS_IRONLAKE(dev)) {
2c07245f
ZW
3260 I915_WRITE(dspsize_reg, ((mode->vdisplay - 1) << 16) |
3261 (mode->hdisplay - 1));
3262 I915_WRITE(dsppos_reg, 0);
3263 }
79e53945 3264 I915_WRITE(pipesrc_reg, ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
2c07245f 3265
f2b115e6 3266 if (IS_IRONLAKE(dev)) {
2c07245f
ZW
3267 I915_WRITE(data_m1_reg, TU_SIZE(m_n.tu) | m_n.gmch_m);
3268 I915_WRITE(data_n1_reg, TU_SIZE(m_n.tu) | m_n.gmch_n);
3269 I915_WRITE(link_m1_reg, m_n.link_m);
3270 I915_WRITE(link_n1_reg, m_n.link_n);
3271
32f9d658 3272 if (is_edp) {
f2b115e6 3273 ironlake_set_pll_edp(crtc, adjusted_mode->clock);
32f9d658
ZW
3274 } else {
3275 /* enable FDI RX PLL too */
3276 temp = I915_READ(fdi_rx_reg);
3277 I915_WRITE(fdi_rx_reg, temp | FDI_RX_PLL_ENABLE);
3278 udelay(200);
3279 }
2c07245f
ZW
3280 }
3281
79e53945
JB
3282 I915_WRITE(pipeconf_reg, pipeconf);
3283 I915_READ(pipeconf_reg);
3284
3285 intel_wait_for_vblank(dev);
3286
f2b115e6 3287 if (IS_IRONLAKE(dev)) {
553bd149
ZW
3288 /* enable address swizzle for tiling buffer */
3289 temp = I915_READ(DISP_ARB_CTL);
3290 I915_WRITE(DISP_ARB_CTL, temp | DISP_TILE_SURFACE_SWIZZLING);
3291 }
3292
79e53945
JB
3293 I915_WRITE(dspcntr_reg, dspcntr);
3294
3295 /* Flush the plane changes */
5c3b82e2 3296 ret = intel_pipe_set_base(crtc, x, y, old_fb);
7662c8bd 3297
74dff282
JB
3298 if ((IS_I965G(dev) || plane == 0))
3299 intel_update_fbc(crtc, &crtc->mode);
e70236a8 3300
7662c8bd
SL
3301 intel_update_watermarks(dev);
3302
79e53945 3303 drm_vblank_post_modeset(dev, pipe);
5c3b82e2 3304
1f803ee5 3305 return ret;
79e53945
JB
3306}
3307
3308/** Loads the palette/gamma unit for the CRTC with the prepared values */
3309void intel_crtc_load_lut(struct drm_crtc *crtc)
3310{
3311 struct drm_device *dev = crtc->dev;
3312 struct drm_i915_private *dev_priv = dev->dev_private;
3313 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3314 int palreg = (intel_crtc->pipe == 0) ? PALETTE_A : PALETTE_B;
3315 int i;
3316
3317 /* The clocks have to be on to load the palette. */
3318 if (!crtc->enabled)
3319 return;
3320
f2b115e6
AJ
3321 /* use legacy palette for Ironlake */
3322 if (IS_IRONLAKE(dev))
2c07245f
ZW
3323 palreg = (intel_crtc->pipe == 0) ? LGC_PALETTE_A :
3324 LGC_PALETTE_B;
3325
79e53945
JB
3326 for (i = 0; i < 256; i++) {
3327 I915_WRITE(palreg + 4 * i,
3328 (intel_crtc->lut_r[i] << 16) |
3329 (intel_crtc->lut_g[i] << 8) |
3330 intel_crtc->lut_b[i]);
3331 }
3332}
3333
3334static int intel_crtc_cursor_set(struct drm_crtc *crtc,
3335 struct drm_file *file_priv,
3336 uint32_t handle,
3337 uint32_t width, uint32_t height)
3338{
3339 struct drm_device *dev = crtc->dev;
3340 struct drm_i915_private *dev_priv = dev->dev_private;
3341 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3342 struct drm_gem_object *bo;
3343 struct drm_i915_gem_object *obj_priv;
3344 int pipe = intel_crtc->pipe;
3345 uint32_t control = (pipe == 0) ? CURACNTR : CURBCNTR;
3346 uint32_t base = (pipe == 0) ? CURABASE : CURBBASE;
14b60391 3347 uint32_t temp = I915_READ(control);
79e53945 3348 size_t addr;
3f8bc370 3349 int ret;
79e53945 3350
28c97730 3351 DRM_DEBUG_KMS("\n");
79e53945
JB
3352
3353 /* if we want to turn off the cursor ignore width and height */
3354 if (!handle) {
28c97730 3355 DRM_DEBUG_KMS("cursor off\n");
14b60391
JB
3356 if (IS_MOBILE(dev) || IS_I9XX(dev)) {
3357 temp &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
3358 temp |= CURSOR_MODE_DISABLE;
3359 } else {
3360 temp &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
3361 }
3f8bc370
KH
3362 addr = 0;
3363 bo = NULL;
5004417d 3364 mutex_lock(&dev->struct_mutex);
3f8bc370 3365 goto finish;
79e53945
JB
3366 }
3367
3368 /* Currently we only support 64x64 cursors */
3369 if (width != 64 || height != 64) {
3370 DRM_ERROR("we currently only support 64x64 cursors\n");
3371 return -EINVAL;
3372 }
3373
3374 bo = drm_gem_object_lookup(dev, file_priv, handle);
3375 if (!bo)
3376 return -ENOENT;
3377
3378 obj_priv = bo->driver_private;
3379
3380 if (bo->size < width * height * 4) {
3381 DRM_ERROR("buffer is to small\n");
34b8686e
DA
3382 ret = -ENOMEM;
3383 goto fail;
79e53945
JB
3384 }
3385
71acb5eb 3386 /* we only need to pin inside GTT if cursor is non-phy */
7f9872e0 3387 mutex_lock(&dev->struct_mutex);
b295d1b6 3388 if (!dev_priv->info->cursor_needs_physical) {
71acb5eb
DA
3389 ret = i915_gem_object_pin(bo, PAGE_SIZE);
3390 if (ret) {
3391 DRM_ERROR("failed to pin cursor bo\n");
7f9872e0 3392 goto fail_locked;
71acb5eb 3393 }
79e53945 3394 addr = obj_priv->gtt_offset;
71acb5eb
DA
3395 } else {
3396 ret = i915_gem_attach_phys_object(dev, bo, (pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1);
3397 if (ret) {
3398 DRM_ERROR("failed to attach phys object\n");
7f9872e0 3399 goto fail_locked;
71acb5eb
DA
3400 }
3401 addr = obj_priv->phys_obj->handle->busaddr;
3f8bc370
KH
3402 }
3403
14b60391
JB
3404 if (!IS_I9XX(dev))
3405 I915_WRITE(CURSIZE, (height << 12) | width);
3406
3407 /* Hooray for CUR*CNTR differences */
3408 if (IS_MOBILE(dev) || IS_I9XX(dev)) {
3409 temp &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
3410 temp |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
3411 temp |= (pipe << 28); /* Connect to correct pipe */
3412 } else {
3413 temp &= ~(CURSOR_FORMAT_MASK);
3414 temp |= CURSOR_ENABLE;
3415 temp |= CURSOR_FORMAT_ARGB | CURSOR_GAMMA_ENABLE;
3416 }
79e53945 3417
3f8bc370 3418 finish:
79e53945
JB
3419 I915_WRITE(control, temp);
3420 I915_WRITE(base, addr);
3421
3f8bc370 3422 if (intel_crtc->cursor_bo) {
b295d1b6 3423 if (dev_priv->info->cursor_needs_physical) {
71acb5eb
DA
3424 if (intel_crtc->cursor_bo != bo)
3425 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
3426 } else
3427 i915_gem_object_unpin(intel_crtc->cursor_bo);
3f8bc370
KH
3428 drm_gem_object_unreference(intel_crtc->cursor_bo);
3429 }
80824003 3430
7f9872e0 3431 mutex_unlock(&dev->struct_mutex);
3f8bc370
KH
3432
3433 intel_crtc->cursor_addr = addr;
3434 intel_crtc->cursor_bo = bo;
3435
79e53945 3436 return 0;
34b8686e
DA
3437fail:
3438 mutex_lock(&dev->struct_mutex);
7f9872e0 3439fail_locked:
34b8686e
DA
3440 drm_gem_object_unreference(bo);
3441 mutex_unlock(&dev->struct_mutex);
3442 return ret;
79e53945
JB
3443}
3444
3445static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
3446{
3447 struct drm_device *dev = crtc->dev;
3448 struct drm_i915_private *dev_priv = dev->dev_private;
3449 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
652c393a 3450 struct intel_framebuffer *intel_fb;
79e53945
JB
3451 int pipe = intel_crtc->pipe;
3452 uint32_t temp = 0;
3453 uint32_t adder;
3454
652c393a
JB
3455 if (crtc->fb) {
3456 intel_fb = to_intel_framebuffer(crtc->fb);
3457 intel_mark_busy(dev, intel_fb->obj);
3458 }
3459
79e53945 3460 if (x < 0) {
2245fda8 3461 temp |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
79e53945
JB
3462 x = -x;
3463 }
3464 if (y < 0) {
2245fda8 3465 temp |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
79e53945
JB
3466 y = -y;
3467 }
3468
2245fda8
KP
3469 temp |= x << CURSOR_X_SHIFT;
3470 temp |= y << CURSOR_Y_SHIFT;
79e53945
JB
3471
3472 adder = intel_crtc->cursor_addr;
3473 I915_WRITE((pipe == 0) ? CURAPOS : CURBPOS, temp);
3474 I915_WRITE((pipe == 0) ? CURABASE : CURBBASE, adder);
3475
3476 return 0;
3477}
3478
3479/** Sets the color ramps on behalf of RandR */
3480void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
3481 u16 blue, int regno)
3482{
3483 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3484
3485 intel_crtc->lut_r[regno] = red >> 8;
3486 intel_crtc->lut_g[regno] = green >> 8;
3487 intel_crtc->lut_b[regno] = blue >> 8;
3488}
3489
b8c00ac5
DA
3490void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
3491 u16 *blue, int regno)
3492{
3493 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3494
3495 *red = intel_crtc->lut_r[regno] << 8;
3496 *green = intel_crtc->lut_g[regno] << 8;
3497 *blue = intel_crtc->lut_b[regno] << 8;
3498}
3499
79e53945
JB
3500static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
3501 u16 *blue, uint32_t size)
3502{
3503 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3504 int i;
3505
3506 if (size != 256)
3507 return;
3508
3509 for (i = 0; i < 256; i++) {
3510 intel_crtc->lut_r[i] = red[i] >> 8;
3511 intel_crtc->lut_g[i] = green[i] >> 8;
3512 intel_crtc->lut_b[i] = blue[i] >> 8;
3513 }
3514
3515 intel_crtc_load_lut(crtc);
3516}
3517
3518/**
3519 * Get a pipe with a simple mode set on it for doing load-based monitor
3520 * detection.
3521 *
3522 * It will be up to the load-detect code to adjust the pipe as appropriate for
3523 * its requirements. The pipe will be connected to no other outputs.
3524 *
3525 * Currently this code will only succeed if there is a pipe with no outputs
3526 * configured for it. In the future, it could choose to temporarily disable
3527 * some outputs to free up a pipe for its use.
3528 *
3529 * \return crtc, or NULL if no pipes are available.
3530 */
3531
3532/* VESA 640x480x72Hz mode to set on the pipe */
3533static struct drm_display_mode load_detect_mode = {
3534 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
3535 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
3536};
3537
3538struct drm_crtc *intel_get_load_detect_pipe(struct intel_output *intel_output,
3539 struct drm_display_mode *mode,
3540 int *dpms_mode)
3541{
3542 struct intel_crtc *intel_crtc;
3543 struct drm_crtc *possible_crtc;
3544 struct drm_crtc *supported_crtc =NULL;
3545 struct drm_encoder *encoder = &intel_output->enc;
3546 struct drm_crtc *crtc = NULL;
3547 struct drm_device *dev = encoder->dev;
3548 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
3549 struct drm_crtc_helper_funcs *crtc_funcs;
3550 int i = -1;
3551
3552 /*
3553 * Algorithm gets a little messy:
3554 * - if the connector already has an assigned crtc, use it (but make
3555 * sure it's on first)
3556 * - try to find the first unused crtc that can drive this connector,
3557 * and use that if we find one
3558 * - if there are no unused crtcs available, try to use the first
3559 * one we found that supports the connector
3560 */
3561
3562 /* See if we already have a CRTC for this connector */
3563 if (encoder->crtc) {
3564 crtc = encoder->crtc;
3565 /* Make sure the crtc and connector are running */
3566 intel_crtc = to_intel_crtc(crtc);
3567 *dpms_mode = intel_crtc->dpms_mode;
3568 if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
3569 crtc_funcs = crtc->helper_private;
3570 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
3571 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
3572 }
3573 return crtc;
3574 }
3575
3576 /* Find an unused one (if possible) */
3577 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
3578 i++;
3579 if (!(encoder->possible_crtcs & (1 << i)))
3580 continue;
3581 if (!possible_crtc->enabled) {
3582 crtc = possible_crtc;
3583 break;
3584 }
3585 if (!supported_crtc)
3586 supported_crtc = possible_crtc;
3587 }
3588
3589 /*
3590 * If we didn't find an unused CRTC, don't use any.
3591 */
3592 if (!crtc) {
3593 return NULL;
3594 }
3595
3596 encoder->crtc = crtc;
03d60699 3597 intel_output->base.encoder = encoder;
79e53945
JB
3598 intel_output->load_detect_temp = true;
3599
3600 intel_crtc = to_intel_crtc(crtc);
3601 *dpms_mode = intel_crtc->dpms_mode;
3602
3603 if (!crtc->enabled) {
3604 if (!mode)
3605 mode = &load_detect_mode;
3c4fdcfb 3606 drm_crtc_helper_set_mode(crtc, mode, 0, 0, crtc->fb);
79e53945
JB
3607 } else {
3608 if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
3609 crtc_funcs = crtc->helper_private;
3610 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
3611 }
3612
3613 /* Add this connector to the crtc */
3614 encoder_funcs->mode_set(encoder, &crtc->mode, &crtc->mode);
3615 encoder_funcs->commit(encoder);
3616 }
3617 /* let the connector get through one full cycle before testing */
3618 intel_wait_for_vblank(dev);
3619
3620 return crtc;
3621}
3622
3623void intel_release_load_detect_pipe(struct intel_output *intel_output, int dpms_mode)
3624{
3625 struct drm_encoder *encoder = &intel_output->enc;
3626 struct drm_device *dev = encoder->dev;
3627 struct drm_crtc *crtc = encoder->crtc;
3628 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
3629 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
3630
3631 if (intel_output->load_detect_temp) {
3632 encoder->crtc = NULL;
03d60699 3633 intel_output->base.encoder = NULL;
79e53945
JB
3634 intel_output->load_detect_temp = false;
3635 crtc->enabled = drm_helper_crtc_in_use(crtc);
3636 drm_helper_disable_unused_functions(dev);
3637 }
3638
3639 /* Switch crtc and output back off if necessary */
3640 if (crtc->enabled && dpms_mode != DRM_MODE_DPMS_ON) {
3641 if (encoder->crtc == crtc)
3642 encoder_funcs->dpms(encoder, dpms_mode);
3643 crtc_funcs->dpms(crtc, dpms_mode);
3644 }
3645}
3646
3647/* Returns the clock of the currently programmed mode of the given pipe. */
3648static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
3649{
3650 struct drm_i915_private *dev_priv = dev->dev_private;
3651 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3652 int pipe = intel_crtc->pipe;
3653 u32 dpll = I915_READ((pipe == 0) ? DPLL_A : DPLL_B);
3654 u32 fp;
3655 intel_clock_t clock;
3656
3657 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
3658 fp = I915_READ((pipe == 0) ? FPA0 : FPB0);
3659 else
3660 fp = I915_READ((pipe == 0) ? FPA1 : FPB1);
3661
3662 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
3663 if (IS_PINEVIEW(dev)) {
3664 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
3665 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
3666 } else {
3667 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
3668 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
3669 }
3670
79e53945 3671 if (IS_I9XX(dev)) {
f2b115e6
AJ
3672 if (IS_PINEVIEW(dev))
3673 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
3674 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
3675 else
3676 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
3677 DPLL_FPA01_P1_POST_DIV_SHIFT);
3678
3679 switch (dpll & DPLL_MODE_MASK) {
3680 case DPLLB_MODE_DAC_SERIAL:
3681 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
3682 5 : 10;
3683 break;
3684 case DPLLB_MODE_LVDS:
3685 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
3686 7 : 14;
3687 break;
3688 default:
28c97730 3689 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945
JB
3690 "mode\n", (int)(dpll & DPLL_MODE_MASK));
3691 return 0;
3692 }
3693
3694 /* XXX: Handle the 100Mhz refclk */
2177832f 3695 intel_clock(dev, 96000, &clock);
79e53945
JB
3696 } else {
3697 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
3698
3699 if (is_lvds) {
3700 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
3701 DPLL_FPA01_P1_POST_DIV_SHIFT);
3702 clock.p2 = 14;
3703
3704 if ((dpll & PLL_REF_INPUT_MASK) ==
3705 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
3706 /* XXX: might not be 66MHz */
2177832f 3707 intel_clock(dev, 66000, &clock);
79e53945 3708 } else
2177832f 3709 intel_clock(dev, 48000, &clock);
79e53945
JB
3710 } else {
3711 if (dpll & PLL_P1_DIVIDE_BY_TWO)
3712 clock.p1 = 2;
3713 else {
3714 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
3715 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
3716 }
3717 if (dpll & PLL_P2_DIVIDE_BY_4)
3718 clock.p2 = 4;
3719 else
3720 clock.p2 = 2;
3721
2177832f 3722 intel_clock(dev, 48000, &clock);
79e53945
JB
3723 }
3724 }
3725
3726 /* XXX: It would be nice to validate the clocks, but we can't reuse
3727 * i830PllIsValid() because it relies on the xf86_config connector
3728 * configuration being accurate, which it isn't necessarily.
3729 */
3730
3731 return clock.dot;
3732}
3733
3734/** Returns the currently programmed mode of the given pipe. */
3735struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
3736 struct drm_crtc *crtc)
3737{
3738 struct drm_i915_private *dev_priv = dev->dev_private;
3739 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3740 int pipe = intel_crtc->pipe;
3741 struct drm_display_mode *mode;
3742 int htot = I915_READ((pipe == 0) ? HTOTAL_A : HTOTAL_B);
3743 int hsync = I915_READ((pipe == 0) ? HSYNC_A : HSYNC_B);
3744 int vtot = I915_READ((pipe == 0) ? VTOTAL_A : VTOTAL_B);
3745 int vsync = I915_READ((pipe == 0) ? VSYNC_A : VSYNC_B);
3746
3747 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
3748 if (!mode)
3749 return NULL;
3750
3751 mode->clock = intel_crtc_clock_get(dev, crtc);
3752 mode->hdisplay = (htot & 0xffff) + 1;
3753 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
3754 mode->hsync_start = (hsync & 0xffff) + 1;
3755 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
3756 mode->vdisplay = (vtot & 0xffff) + 1;
3757 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
3758 mode->vsync_start = (vsync & 0xffff) + 1;
3759 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
3760
3761 drm_mode_set_name(mode);
3762 drm_mode_set_crtcinfo(mode, 0);
3763
3764 return mode;
3765}
3766
652c393a
JB
3767#define GPU_IDLE_TIMEOUT 500 /* ms */
3768
3769/* When this timer fires, we've been idle for awhile */
3770static void intel_gpu_idle_timer(unsigned long arg)
3771{
3772 struct drm_device *dev = (struct drm_device *)arg;
3773 drm_i915_private_t *dev_priv = dev->dev_private;
3774
44d98a61 3775 DRM_DEBUG_DRIVER("idle timer fired, downclocking\n");
652c393a
JB
3776
3777 dev_priv->busy = false;
3778
01dfba93 3779 queue_work(dev_priv->wq, &dev_priv->idle_work);
652c393a
JB
3780}
3781
652c393a
JB
3782#define CRTC_IDLE_TIMEOUT 1000 /* ms */
3783
3784static void intel_crtc_idle_timer(unsigned long arg)
3785{
3786 struct intel_crtc *intel_crtc = (struct intel_crtc *)arg;
3787 struct drm_crtc *crtc = &intel_crtc->base;
3788 drm_i915_private_t *dev_priv = crtc->dev->dev_private;
3789
44d98a61 3790 DRM_DEBUG_DRIVER("idle timer fired, downclocking\n");
652c393a
JB
3791
3792 intel_crtc->busy = false;
3793
01dfba93 3794 queue_work(dev_priv->wq, &dev_priv->idle_work);
652c393a
JB
3795}
3796
3797static void intel_increase_pllclock(struct drm_crtc *crtc, bool schedule)
3798{
3799 struct drm_device *dev = crtc->dev;
3800 drm_i915_private_t *dev_priv = dev->dev_private;
3801 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3802 int pipe = intel_crtc->pipe;
3803 int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
3804 int dpll = I915_READ(dpll_reg);
3805
f2b115e6 3806 if (IS_IRONLAKE(dev))
652c393a
JB
3807 return;
3808
3809 if (!dev_priv->lvds_downclock_avail)
3810 return;
3811
3812 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
44d98a61 3813 DRM_DEBUG_DRIVER("upclocking LVDS\n");
652c393a
JB
3814
3815 /* Unlock panel regs */
3816 I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) | (0xabcd << 16));
3817
3818 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
3819 I915_WRITE(dpll_reg, dpll);
3820 dpll = I915_READ(dpll_reg);
3821 intel_wait_for_vblank(dev);
3822 dpll = I915_READ(dpll_reg);
3823 if (dpll & DISPLAY_RATE_SELECT_FPA1)
44d98a61 3824 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
652c393a
JB
3825
3826 /* ...and lock them again */
3827 I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
3828 }
3829
3830 /* Schedule downclock */
3831 if (schedule)
3832 mod_timer(&intel_crtc->idle_timer, jiffies +
3833 msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
3834}
3835
3836static void intel_decrease_pllclock(struct drm_crtc *crtc)
3837{
3838 struct drm_device *dev = crtc->dev;
3839 drm_i915_private_t *dev_priv = dev->dev_private;
3840 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3841 int pipe = intel_crtc->pipe;
3842 int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
3843 int dpll = I915_READ(dpll_reg);
3844
f2b115e6 3845 if (IS_IRONLAKE(dev))
652c393a
JB
3846 return;
3847
3848 if (!dev_priv->lvds_downclock_avail)
3849 return;
3850
3851 /*
3852 * Since this is called by a timer, we should never get here in
3853 * the manual case.
3854 */
3855 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
44d98a61 3856 DRM_DEBUG_DRIVER("downclocking LVDS\n");
652c393a
JB
3857
3858 /* Unlock panel regs */
3859 I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) | (0xabcd << 16));
3860
3861 dpll |= DISPLAY_RATE_SELECT_FPA1;
3862 I915_WRITE(dpll_reg, dpll);
3863 dpll = I915_READ(dpll_reg);
3864 intel_wait_for_vblank(dev);
3865 dpll = I915_READ(dpll_reg);
3866 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
44d98a61 3867 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
652c393a
JB
3868
3869 /* ...and lock them again */
3870 I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
3871 }
3872
3873}
3874
3875/**
3876 * intel_idle_update - adjust clocks for idleness
3877 * @work: work struct
3878 *
3879 * Either the GPU or display (or both) went idle. Check the busy status
3880 * here and adjust the CRTC and GPU clocks as necessary.
3881 */
3882static void intel_idle_update(struct work_struct *work)
3883{
3884 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
3885 idle_work);
3886 struct drm_device *dev = dev_priv->dev;
3887 struct drm_crtc *crtc;
3888 struct intel_crtc *intel_crtc;
3889
3890 if (!i915_powersave)
3891 return;
3892
3893 mutex_lock(&dev->struct_mutex);
3894
652c393a
JB
3895 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3896 /* Skip inactive CRTCs */
3897 if (!crtc->fb)
3898 continue;
3899
3900 intel_crtc = to_intel_crtc(crtc);
3901 if (!intel_crtc->busy)
3902 intel_decrease_pllclock(crtc);
3903 }
3904
3905 mutex_unlock(&dev->struct_mutex);
3906}
3907
3908/**
3909 * intel_mark_busy - mark the GPU and possibly the display busy
3910 * @dev: drm device
3911 * @obj: object we're operating on
3912 *
3913 * Callers can use this function to indicate that the GPU is busy processing
3914 * commands. If @obj matches one of the CRTC objects (i.e. it's a scanout
3915 * buffer), we'll also mark the display as busy, so we know to increase its
3916 * clock frequency.
3917 */
3918void intel_mark_busy(struct drm_device *dev, struct drm_gem_object *obj)
3919{
3920 drm_i915_private_t *dev_priv = dev->dev_private;
3921 struct drm_crtc *crtc = NULL;
3922 struct intel_framebuffer *intel_fb;
3923 struct intel_crtc *intel_crtc;
3924
5e17ee74
ZW
3925 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3926 return;
3927
cda9d05c 3928 if (!dev_priv->busy)
28cf798f 3929 dev_priv->busy = true;
cda9d05c 3930 else
28cf798f
CW
3931 mod_timer(&dev_priv->idle_timer, jiffies +
3932 msecs_to_jiffies(GPU_IDLE_TIMEOUT));
652c393a
JB
3933
3934 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3935 if (!crtc->fb)
3936 continue;
3937
3938 intel_crtc = to_intel_crtc(crtc);
3939 intel_fb = to_intel_framebuffer(crtc->fb);
3940 if (intel_fb->obj == obj) {
3941 if (!intel_crtc->busy) {
3942 /* Non-busy -> busy, upclock */
3943 intel_increase_pllclock(crtc, true);
3944 intel_crtc->busy = true;
3945 } else {
3946 /* Busy -> busy, put off timer */
3947 mod_timer(&intel_crtc->idle_timer, jiffies +
3948 msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
3949 }
3950 }
3951 }
3952}
3953
79e53945
JB
3954static void intel_crtc_destroy(struct drm_crtc *crtc)
3955{
3956 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3957
3958 drm_crtc_cleanup(crtc);
3959 kfree(intel_crtc);
3960}
3961
6b95a207
KH
3962struct intel_unpin_work {
3963 struct work_struct work;
3964 struct drm_device *dev;
3965 struct drm_gem_object *obj;
3966 struct drm_pending_vblank_event *event;
3967 int pending;
3968};
3969
3970static void intel_unpin_work_fn(struct work_struct *__work)
3971{
3972 struct intel_unpin_work *work =
3973 container_of(__work, struct intel_unpin_work, work);
3974
3975 mutex_lock(&work->dev->struct_mutex);
3976 i915_gem_object_unpin(work->obj);
3977 drm_gem_object_unreference(work->obj);
3978 mutex_unlock(&work->dev->struct_mutex);
3979 kfree(work);
3980}
3981
3982void intel_finish_page_flip(struct drm_device *dev, int pipe)
3983{
3984 drm_i915_private_t *dev_priv = dev->dev_private;
3985 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
3986 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3987 struct intel_unpin_work *work;
3988 struct drm_i915_gem_object *obj_priv;
3989 struct drm_pending_vblank_event *e;
3990 struct timeval now;
3991 unsigned long flags;
3992
3993 /* Ignore early vblank irqs */
3994 if (intel_crtc == NULL)
3995 return;
3996
3997 spin_lock_irqsave(&dev->event_lock, flags);
3998 work = intel_crtc->unpin_work;
3999 if (work == NULL || !work->pending) {
de3f440f
JB
4000 if (work && !work->pending) {
4001 obj_priv = work->obj->driver_private;
4002 DRM_DEBUG_DRIVER("flip finish: %p (%d) not pending?\n",
4003 obj_priv,
4004 atomic_read(&obj_priv->pending_flip));
4005 }
6b95a207
KH
4006 spin_unlock_irqrestore(&dev->event_lock, flags);
4007 return;
4008 }
4009
4010 intel_crtc->unpin_work = NULL;
4011 drm_vblank_put(dev, intel_crtc->pipe);
4012
4013 if (work->event) {
4014 e = work->event;
4015 do_gettimeofday(&now);
4016 e->event.sequence = drm_vblank_count(dev, intel_crtc->pipe);
4017 e->event.tv_sec = now.tv_sec;
4018 e->event.tv_usec = now.tv_usec;
4019 list_add_tail(&e->base.link,
4020 &e->base.file_priv->event_list);
4021 wake_up_interruptible(&e->base.file_priv->event_wait);
4022 }
4023
4024 spin_unlock_irqrestore(&dev->event_lock, flags);
4025
4026 obj_priv = work->obj->driver_private;
de3f440f
JB
4027
4028 /* Initial scanout buffer will have a 0 pending flip count */
4029 if ((atomic_read(&obj_priv->pending_flip) == 0) ||
4030 atomic_dec_and_test(&obj_priv->pending_flip))
6b95a207
KH
4031 DRM_WAKEUP(&dev_priv->pending_flip_queue);
4032 schedule_work(&work->work);
4033}
4034
4035void intel_prepare_page_flip(struct drm_device *dev, int plane)
4036{
4037 drm_i915_private_t *dev_priv = dev->dev_private;
4038 struct intel_crtc *intel_crtc =
4039 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
4040 unsigned long flags;
4041
4042 spin_lock_irqsave(&dev->event_lock, flags);
de3f440f 4043 if (intel_crtc->unpin_work) {
6b95a207 4044 intel_crtc->unpin_work->pending = 1;
de3f440f
JB
4045 } else {
4046 DRM_DEBUG_DRIVER("preparing flip with no unpin work?\n");
4047 }
6b95a207
KH
4048 spin_unlock_irqrestore(&dev->event_lock, flags);
4049}
4050
4051static int intel_crtc_page_flip(struct drm_crtc *crtc,
4052 struct drm_framebuffer *fb,
4053 struct drm_pending_vblank_event *event)
4054{
4055 struct drm_device *dev = crtc->dev;
4056 struct drm_i915_private *dev_priv = dev->dev_private;
4057 struct intel_framebuffer *intel_fb;
4058 struct drm_i915_gem_object *obj_priv;
4059 struct drm_gem_object *obj;
4060 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4061 struct intel_unpin_work *work;
4062 unsigned long flags;
4063 int ret;
4064 RING_LOCALS;
4065
4066 work = kzalloc(sizeof *work, GFP_KERNEL);
4067 if (work == NULL)
4068 return -ENOMEM;
4069
4070 mutex_lock(&dev->struct_mutex);
4071
4072 work->event = event;
4073 work->dev = crtc->dev;
4074 intel_fb = to_intel_framebuffer(crtc->fb);
4075 work->obj = intel_fb->obj;
4076 INIT_WORK(&work->work, intel_unpin_work_fn);
4077
4078 /* We borrow the event spin lock for protecting unpin_work */
4079 spin_lock_irqsave(&dev->event_lock, flags);
4080 if (intel_crtc->unpin_work) {
de3f440f 4081 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
6b95a207
KH
4082 spin_unlock_irqrestore(&dev->event_lock, flags);
4083 kfree(work);
4084 mutex_unlock(&dev->struct_mutex);
4085 return -EBUSY;
4086 }
4087 intel_crtc->unpin_work = work;
4088 spin_unlock_irqrestore(&dev->event_lock, flags);
4089
4090 intel_fb = to_intel_framebuffer(fb);
4091 obj = intel_fb->obj;
4092
4093 ret = intel_pin_and_fence_fb_obj(dev, obj);
4094 if (ret != 0) {
de3f440f
JB
4095 DRM_DEBUG_DRIVER("flip queue: %p pin & fence failed\n",
4096 obj->driver_private);
6b95a207 4097 kfree(work);
de3f440f 4098 intel_crtc->unpin_work = NULL;
6b95a207
KH
4099 mutex_unlock(&dev->struct_mutex);
4100 return ret;
4101 }
4102
4103 /* Reference the old fb object for the scheduled work. */
4104 drm_gem_object_reference(work->obj);
4105
4106 crtc->fb = fb;
4107 i915_gem_object_flush_write_domain(obj);
4108 drm_vblank_get(dev, intel_crtc->pipe);
4109 obj_priv = obj->driver_private;
4110 atomic_inc(&obj_priv->pending_flip);
4111
4112 BEGIN_LP_RING(4);
4113 OUT_RING(MI_DISPLAY_FLIP |
4114 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
4115 OUT_RING(fb->pitch);
22fd0fab
JB
4116 if (IS_I965G(dev)) {
4117 OUT_RING(obj_priv->gtt_offset | obj_priv->tiling_mode);
4118 OUT_RING((fb->width << 16) | fb->height);
4119 } else {
4120 OUT_RING(obj_priv->gtt_offset);
4121 OUT_RING(MI_NOOP);
4122 }
6b95a207
KH
4123 ADVANCE_LP_RING();
4124
4125 mutex_unlock(&dev->struct_mutex);
4126
4127 return 0;
4128}
4129
79e53945
JB
4130static const struct drm_crtc_helper_funcs intel_helper_funcs = {
4131 .dpms = intel_crtc_dpms,
4132 .mode_fixup = intel_crtc_mode_fixup,
4133 .mode_set = intel_crtc_mode_set,
4134 .mode_set_base = intel_pipe_set_base,
4135 .prepare = intel_crtc_prepare,
4136 .commit = intel_crtc_commit,
068143d3 4137 .load_lut = intel_crtc_load_lut,
79e53945
JB
4138};
4139
4140static const struct drm_crtc_funcs intel_crtc_funcs = {
4141 .cursor_set = intel_crtc_cursor_set,
4142 .cursor_move = intel_crtc_cursor_move,
4143 .gamma_set = intel_crtc_gamma_set,
4144 .set_config = drm_crtc_helper_set_config,
4145 .destroy = intel_crtc_destroy,
6b95a207 4146 .page_flip = intel_crtc_page_flip,
79e53945
JB
4147};
4148
4149
b358d0a6 4150static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 4151{
22fd0fab 4152 drm_i915_private_t *dev_priv = dev->dev_private;
79e53945
JB
4153 struct intel_crtc *intel_crtc;
4154 int i;
4155
4156 intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
4157 if (intel_crtc == NULL)
4158 return;
4159
4160 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
4161
4162 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
4163 intel_crtc->pipe = pipe;
7662c8bd 4164 intel_crtc->plane = pipe;
79e53945
JB
4165 for (i = 0; i < 256; i++) {
4166 intel_crtc->lut_r[i] = i;
4167 intel_crtc->lut_g[i] = i;
4168 intel_crtc->lut_b[i] = i;
4169 }
4170
80824003
JB
4171 /* Swap pipes & planes for FBC on pre-965 */
4172 intel_crtc->pipe = pipe;
4173 intel_crtc->plane = pipe;
4174 if (IS_MOBILE(dev) && (IS_I9XX(dev) && !IS_I965G(dev))) {
28c97730 4175 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
80824003
JB
4176 intel_crtc->plane = ((pipe == 0) ? 1 : 0);
4177 }
4178
22fd0fab
JB
4179 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
4180 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
4181 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
4182 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
4183
79e53945
JB
4184 intel_crtc->cursor_addr = 0;
4185 intel_crtc->dpms_mode = DRM_MODE_DPMS_OFF;
4186 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
4187
652c393a
JB
4188 intel_crtc->busy = false;
4189
4190 setup_timer(&intel_crtc->idle_timer, intel_crtc_idle_timer,
4191 (unsigned long)intel_crtc);
79e53945
JB
4192}
4193
08d7b3d1
CW
4194int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
4195 struct drm_file *file_priv)
4196{
4197 drm_i915_private_t *dev_priv = dev->dev_private;
4198 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
c05422d5
DV
4199 struct drm_mode_object *drmmode_obj;
4200 struct intel_crtc *crtc;
08d7b3d1
CW
4201
4202 if (!dev_priv) {
4203 DRM_ERROR("called with no initialization\n");
4204 return -EINVAL;
4205 }
4206
c05422d5
DV
4207 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
4208 DRM_MODE_OBJECT_CRTC);
08d7b3d1 4209
c05422d5 4210 if (!drmmode_obj) {
08d7b3d1
CW
4211 DRM_ERROR("no such CRTC id\n");
4212 return -EINVAL;
4213 }
4214
c05422d5
DV
4215 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
4216 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 4217
c05422d5 4218 return 0;
08d7b3d1
CW
4219}
4220
79e53945
JB
4221struct drm_crtc *intel_get_crtc_from_pipe(struct drm_device *dev, int pipe)
4222{
4223 struct drm_crtc *crtc = NULL;
4224
4225 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
4226 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4227 if (intel_crtc->pipe == pipe)
4228 break;
4229 }
4230 return crtc;
4231}
4232
b358d0a6 4233static int intel_connector_clones(struct drm_device *dev, int type_mask)
79e53945
JB
4234{
4235 int index_mask = 0;
4236 struct drm_connector *connector;
4237 int entry = 0;
4238
4239 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4240 struct intel_output *intel_output = to_intel_output(connector);
f8aed700 4241 if (type_mask & intel_output->clone_mask)
79e53945
JB
4242 index_mask |= (1 << entry);
4243 entry++;
4244 }
4245 return index_mask;
4246}
4247
4248
4249static void intel_setup_outputs(struct drm_device *dev)
4250{
725e30ad 4251 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945
JB
4252 struct drm_connector *connector;
4253
4254 intel_crt_init(dev);
4255
4256 /* Set up integrated LVDS */
541998a1 4257 if (IS_MOBILE(dev) && !IS_I830(dev))
79e53945
JB
4258 intel_lvds_init(dev);
4259
f2b115e6 4260 if (IS_IRONLAKE(dev)) {
30ad48b7
ZW
4261 int found;
4262
32f9d658
ZW
4263 if (IS_MOBILE(dev) && (I915_READ(DP_A) & DP_DETECTED))
4264 intel_dp_init(dev, DP_A);
4265
30ad48b7
ZW
4266 if (I915_READ(HDMIB) & PORT_DETECTED) {
4267 /* check SDVOB */
4268 /* found = intel_sdvo_init(dev, HDMIB); */
4269 found = 0;
4270 if (!found)
4271 intel_hdmi_init(dev, HDMIB);
5eb08b69
ZW
4272 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
4273 intel_dp_init(dev, PCH_DP_B);
30ad48b7
ZW
4274 }
4275
4276 if (I915_READ(HDMIC) & PORT_DETECTED)
4277 intel_hdmi_init(dev, HDMIC);
4278
4279 if (I915_READ(HDMID) & PORT_DETECTED)
4280 intel_hdmi_init(dev, HDMID);
4281
5eb08b69
ZW
4282 if (I915_READ(PCH_DP_C) & DP_DETECTED)
4283 intel_dp_init(dev, PCH_DP_C);
4284
4285 if (I915_READ(PCH_DP_D) & DP_DETECTED)
4286 intel_dp_init(dev, PCH_DP_D);
4287
103a196f 4288 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
27185ae1 4289 bool found = false;
7d57382e 4290
725e30ad 4291 if (I915_READ(SDVOB) & SDVO_DETECTED) {
b01f2c3a 4292 DRM_DEBUG_KMS("probing SDVOB\n");
725e30ad 4293 found = intel_sdvo_init(dev, SDVOB);
b01f2c3a
JB
4294 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
4295 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
725e30ad 4296 intel_hdmi_init(dev, SDVOB);
b01f2c3a 4297 }
27185ae1 4298
b01f2c3a
JB
4299 if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
4300 DRM_DEBUG_KMS("probing DP_B\n");
a4fc5ed6 4301 intel_dp_init(dev, DP_B);
b01f2c3a 4302 }
725e30ad 4303 }
13520b05
KH
4304
4305 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 4306
b01f2c3a
JB
4307 if (I915_READ(SDVOB) & SDVO_DETECTED) {
4308 DRM_DEBUG_KMS("probing SDVOC\n");
725e30ad 4309 found = intel_sdvo_init(dev, SDVOC);
b01f2c3a 4310 }
27185ae1
ML
4311
4312 if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
4313
b01f2c3a
JB
4314 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
4315 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
725e30ad 4316 intel_hdmi_init(dev, SDVOC);
b01f2c3a
JB
4317 }
4318 if (SUPPORTS_INTEGRATED_DP(dev)) {
4319 DRM_DEBUG_KMS("probing DP_C\n");
a4fc5ed6 4320 intel_dp_init(dev, DP_C);
b01f2c3a 4321 }
725e30ad 4322 }
27185ae1 4323
b01f2c3a
JB
4324 if (SUPPORTS_INTEGRATED_DP(dev) &&
4325 (I915_READ(DP_D) & DP_DETECTED)) {
4326 DRM_DEBUG_KMS("probing DP_D\n");
a4fc5ed6 4327 intel_dp_init(dev, DP_D);
b01f2c3a 4328 }
103a196f 4329 } else if (IS_I8XX(dev))
79e53945
JB
4330 intel_dvo_init(dev);
4331
103a196f 4332 if (SUPPORTS_TV(dev))
79e53945
JB
4333 intel_tv_init(dev);
4334
4335 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4336 struct intel_output *intel_output = to_intel_output(connector);
4337 struct drm_encoder *encoder = &intel_output->enc;
79e53945 4338
f8aed700
ML
4339 encoder->possible_crtcs = intel_output->crtc_mask;
4340 encoder->possible_clones = intel_connector_clones(dev,
4341 intel_output->clone_mask);
79e53945
JB
4342 }
4343}
4344
4345static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
4346{
4347 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
4348 struct drm_device *dev = fb->dev;
4349
4350 if (fb->fbdev)
4351 intelfb_remove(dev, fb);
4352
4353 drm_framebuffer_cleanup(fb);
4354 mutex_lock(&dev->struct_mutex);
4355 drm_gem_object_unreference(intel_fb->obj);
4356 mutex_unlock(&dev->struct_mutex);
4357
4358 kfree(intel_fb);
4359}
4360
4361static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
4362 struct drm_file *file_priv,
4363 unsigned int *handle)
4364{
4365 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
4366 struct drm_gem_object *object = intel_fb->obj;
4367
4368 return drm_gem_handle_create(file_priv, object, handle);
4369}
4370
4371static const struct drm_framebuffer_funcs intel_fb_funcs = {
4372 .destroy = intel_user_framebuffer_destroy,
4373 .create_handle = intel_user_framebuffer_create_handle,
4374};
4375
4376int intel_framebuffer_create(struct drm_device *dev,
4377 struct drm_mode_fb_cmd *mode_cmd,
4378 struct drm_framebuffer **fb,
4379 struct drm_gem_object *obj)
4380{
4381 struct intel_framebuffer *intel_fb;
4382 int ret;
4383
4384 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
4385 if (!intel_fb)
4386 return -ENOMEM;
4387
4388 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
4389 if (ret) {
4390 DRM_ERROR("framebuffer init failed %d\n", ret);
4391 return ret;
4392 }
4393
4394 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
4395
4396 intel_fb->obj = obj;
4397
4398 *fb = &intel_fb->base;
4399
4400 return 0;
4401}
4402
4403
4404static struct drm_framebuffer *
4405intel_user_framebuffer_create(struct drm_device *dev,
4406 struct drm_file *filp,
4407 struct drm_mode_fb_cmd *mode_cmd)
4408{
4409 struct drm_gem_object *obj;
4410 struct drm_framebuffer *fb;
4411 int ret;
4412
4413 obj = drm_gem_object_lookup(dev, filp, mode_cmd->handle);
4414 if (!obj)
4415 return NULL;
4416
4417 ret = intel_framebuffer_create(dev, mode_cmd, &fb, obj);
4418 if (ret) {
496818f0 4419 mutex_lock(&dev->struct_mutex);
79e53945 4420 drm_gem_object_unreference(obj);
496818f0 4421 mutex_unlock(&dev->struct_mutex);
79e53945
JB
4422 return NULL;
4423 }
4424
4425 return fb;
4426}
4427
79e53945 4428static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945
JB
4429 .fb_create = intel_user_framebuffer_create,
4430 .fb_changed = intelfb_probe,
4431};
4432
9ea8d059
CW
4433static struct drm_gem_object *
4434intel_alloc_power_context(struct drm_device *dev)
4435{
4436 struct drm_gem_object *pwrctx;
4437 int ret;
4438
4439 pwrctx = drm_gem_object_alloc(dev, 4096);
4440 if (!pwrctx) {
4441 DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
4442 return NULL;
4443 }
4444
4445 mutex_lock(&dev->struct_mutex);
4446 ret = i915_gem_object_pin(pwrctx, 4096);
4447 if (ret) {
4448 DRM_ERROR("failed to pin power context: %d\n", ret);
4449 goto err_unref;
4450 }
4451
4452 ret = i915_gem_object_set_to_gtt_domain(pwrctx, 1);
4453 if (ret) {
4454 DRM_ERROR("failed to set-domain on power context: %d\n", ret);
4455 goto err_unpin;
4456 }
4457 mutex_unlock(&dev->struct_mutex);
4458
4459 return pwrctx;
4460
4461err_unpin:
4462 i915_gem_object_unpin(pwrctx);
4463err_unref:
4464 drm_gem_object_unreference(pwrctx);
4465 mutex_unlock(&dev->struct_mutex);
4466 return NULL;
4467}
4468
652c393a
JB
4469void intel_init_clock_gating(struct drm_device *dev)
4470{
4471 struct drm_i915_private *dev_priv = dev->dev_private;
4472
4473 /*
4474 * Disable clock gating reported to work incorrectly according to the
4475 * specs, but enable as much else as we can.
4476 */
f2b115e6 4477 if (IS_IRONLAKE(dev)) {
c03342fa
ZW
4478 return;
4479 } else if (IS_G4X(dev)) {
652c393a
JB
4480 uint32_t dspclk_gate;
4481 I915_WRITE(RENCLK_GATE_D1, 0);
4482 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
4483 GS_UNIT_CLOCK_GATE_DISABLE |
4484 CL_UNIT_CLOCK_GATE_DISABLE);
4485 I915_WRITE(RAMCLK_GATE_D, 0);
4486 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
4487 OVRUNIT_CLOCK_GATE_DISABLE |
4488 OVCUNIT_CLOCK_GATE_DISABLE;
4489 if (IS_GM45(dev))
4490 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
4491 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
4492 } else if (IS_I965GM(dev)) {
4493 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
4494 I915_WRITE(RENCLK_GATE_D2, 0);
4495 I915_WRITE(DSPCLK_GATE_D, 0);
4496 I915_WRITE(RAMCLK_GATE_D, 0);
4497 I915_WRITE16(DEUC, 0);
4498 } else if (IS_I965G(dev)) {
4499 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
4500 I965_RCC_CLOCK_GATE_DISABLE |
4501 I965_RCPB_CLOCK_GATE_DISABLE |
4502 I965_ISC_CLOCK_GATE_DISABLE |
4503 I965_FBC_CLOCK_GATE_DISABLE);
4504 I915_WRITE(RENCLK_GATE_D2, 0);
4505 } else if (IS_I9XX(dev)) {
4506 u32 dstate = I915_READ(D_STATE);
4507
4508 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
4509 DSTATE_DOT_CLOCK_GATING;
4510 I915_WRITE(D_STATE, dstate);
f0f8a9ce 4511 } else if (IS_I85X(dev) || IS_I865G(dev)) {
652c393a
JB
4512 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
4513 } else if (IS_I830(dev)) {
4514 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
4515 }
97f5ab66
JB
4516
4517 /*
4518 * GPU can automatically power down the render unit if given a page
4519 * to save state.
4520 */
1d3c36ad 4521 if (I915_HAS_RC6(dev) && drm_core_check_feature(dev, DRIVER_MODESET)) {
9ea8d059 4522 struct drm_i915_gem_object *obj_priv = NULL;
97f5ab66 4523
7e8b60fa
AL
4524 if (dev_priv->pwrctx) {
4525 obj_priv = dev_priv->pwrctx->driver_private;
4526 } else {
9ea8d059 4527 struct drm_gem_object *pwrctx;
97f5ab66 4528
9ea8d059
CW
4529 pwrctx = intel_alloc_power_context(dev);
4530 if (pwrctx) {
4531 dev_priv->pwrctx = pwrctx;
4532 obj_priv = pwrctx->driver_private;
7e8b60fa 4533 }
7e8b60fa 4534 }
97f5ab66 4535
9ea8d059
CW
4536 if (obj_priv) {
4537 I915_WRITE(PWRCTXA, obj_priv->gtt_offset | PWRCTX_EN);
4538 I915_WRITE(MCHBAR_RENDER_STANDBY,
4539 I915_READ(MCHBAR_RENDER_STANDBY) & ~RCX_SW_EXIT);
4540 }
97f5ab66 4541 }
652c393a
JB
4542}
4543
e70236a8
JB
4544/* Set up chip specific display functions */
4545static void intel_init_display(struct drm_device *dev)
4546{
4547 struct drm_i915_private *dev_priv = dev->dev_private;
4548
4549 /* We always want a DPMS function */
f2b115e6
AJ
4550 if (IS_IRONLAKE(dev))
4551 dev_priv->display.dpms = ironlake_crtc_dpms;
e70236a8
JB
4552 else
4553 dev_priv->display.dpms = i9xx_crtc_dpms;
4554
4555 /* Only mobile has FBC, leave pointers NULL for other chips */
4556 if (IS_MOBILE(dev)) {
74dff282
JB
4557 if (IS_GM45(dev)) {
4558 dev_priv->display.fbc_enabled = g4x_fbc_enabled;
4559 dev_priv->display.enable_fbc = g4x_enable_fbc;
4560 dev_priv->display.disable_fbc = g4x_disable_fbc;
4561 } else if (IS_I965GM(dev) || IS_I945GM(dev) || IS_I915GM(dev)) {
e70236a8
JB
4562 dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
4563 dev_priv->display.enable_fbc = i8xx_enable_fbc;
4564 dev_priv->display.disable_fbc = i8xx_disable_fbc;
4565 }
74dff282 4566 /* 855GM needs testing */
e70236a8
JB
4567 }
4568
4569 /* Returns the core display clock speed */
f2b115e6 4570 if (IS_I945G(dev) || (IS_G33(dev) && ! IS_PINEVIEW_M(dev)))
e70236a8
JB
4571 dev_priv->display.get_display_clock_speed =
4572 i945_get_display_clock_speed;
4573 else if (IS_I915G(dev))
4574 dev_priv->display.get_display_clock_speed =
4575 i915_get_display_clock_speed;
f2b115e6 4576 else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
e70236a8
JB
4577 dev_priv->display.get_display_clock_speed =
4578 i9xx_misc_get_display_clock_speed;
4579 else if (IS_I915GM(dev))
4580 dev_priv->display.get_display_clock_speed =
4581 i915gm_get_display_clock_speed;
4582 else if (IS_I865G(dev))
4583 dev_priv->display.get_display_clock_speed =
4584 i865_get_display_clock_speed;
f0f8a9ce 4585 else if (IS_I85X(dev))
e70236a8
JB
4586 dev_priv->display.get_display_clock_speed =
4587 i855_get_display_clock_speed;
4588 else /* 852, 830 */
4589 dev_priv->display.get_display_clock_speed =
4590 i830_get_display_clock_speed;
4591
4592 /* For FIFO watermark updates */
f2b115e6 4593 if (IS_IRONLAKE(dev))
c03342fa
ZW
4594 dev_priv->display.update_wm = NULL;
4595 else if (IS_G4X(dev))
e70236a8
JB
4596 dev_priv->display.update_wm = g4x_update_wm;
4597 else if (IS_I965G(dev))
4598 dev_priv->display.update_wm = i965_update_wm;
4599 else if (IS_I9XX(dev) || IS_MOBILE(dev)) {
4600 dev_priv->display.update_wm = i9xx_update_wm;
4601 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
4602 } else {
4603 if (IS_I85X(dev))
4604 dev_priv->display.get_fifo_size = i85x_get_fifo_size;
4605 else if (IS_845G(dev))
4606 dev_priv->display.get_fifo_size = i845_get_fifo_size;
4607 else
4608 dev_priv->display.get_fifo_size = i830_get_fifo_size;
4609 dev_priv->display.update_wm = i830_update_wm;
4610 }
4611}
4612
79e53945
JB
4613void intel_modeset_init(struct drm_device *dev)
4614{
652c393a 4615 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945
JB
4616 int num_pipe;
4617 int i;
4618
4619 drm_mode_config_init(dev);
4620
4621 dev->mode_config.min_width = 0;
4622 dev->mode_config.min_height = 0;
4623
4624 dev->mode_config.funcs = (void *)&intel_mode_funcs;
4625
e70236a8
JB
4626 intel_init_display(dev);
4627
79e53945
JB
4628 if (IS_I965G(dev)) {
4629 dev->mode_config.max_width = 8192;
4630 dev->mode_config.max_height = 8192;
5e4d6fa7
KP
4631 } else if (IS_I9XX(dev)) {
4632 dev->mode_config.max_width = 4096;
4633 dev->mode_config.max_height = 4096;
79e53945
JB
4634 } else {
4635 dev->mode_config.max_width = 2048;
4636 dev->mode_config.max_height = 2048;
4637 }
4638
4639 /* set memory base */
4640 if (IS_I9XX(dev))
4641 dev->mode_config.fb_base = pci_resource_start(dev->pdev, 2);
4642 else
4643 dev->mode_config.fb_base = pci_resource_start(dev->pdev, 0);
4644
4645 if (IS_MOBILE(dev) || IS_I9XX(dev))
4646 num_pipe = 2;
4647 else
4648 num_pipe = 1;
28c97730 4649 DRM_DEBUG_KMS("%d display pipe%s available.\n",
79e53945
JB
4650 num_pipe, num_pipe > 1 ? "s" : "");
4651
652c393a
JB
4652 if (IS_I85X(dev))
4653 pci_read_config_word(dev->pdev, HPLLCC, &dev_priv->orig_clock);
4654 else if (IS_I9XX(dev) || IS_G4X(dev))
4655 pci_read_config_word(dev->pdev, GCFGC, &dev_priv->orig_clock);
4656
79e53945
JB
4657 for (i = 0; i < num_pipe; i++) {
4658 intel_crtc_init(dev, i);
4659 }
4660
4661 intel_setup_outputs(dev);
652c393a
JB
4662
4663 intel_init_clock_gating(dev);
4664
4665 INIT_WORK(&dev_priv->idle_work, intel_idle_update);
4666 setup_timer(&dev_priv->idle_timer, intel_gpu_idle_timer,
4667 (unsigned long)dev);
02e792fb
DV
4668
4669 intel_setup_overlay(dev);
85364905 4670
f2b115e6
AJ
4671 if (IS_PINEVIEW(dev) && !intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
4672 dev_priv->fsb_freq,
4673 dev_priv->mem_freq))
85364905
JB
4674 DRM_INFO("failed to find known CxSR latency "
4675 "(found fsb freq %d, mem freq %d), disabling CxSR\n",
4676 dev_priv->fsb_freq, dev_priv->mem_freq);
79e53945
JB
4677}
4678
4679void intel_modeset_cleanup(struct drm_device *dev)
4680{
652c393a
JB
4681 struct drm_i915_private *dev_priv = dev->dev_private;
4682 struct drm_crtc *crtc;
4683 struct intel_crtc *intel_crtc;
4684
4685 mutex_lock(&dev->struct_mutex);
4686
4687 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
4688 /* Skip inactive CRTCs */
4689 if (!crtc->fb)
4690 continue;
4691
4692 intel_crtc = to_intel_crtc(crtc);
4693 intel_increase_pllclock(crtc, false);
4694 del_timer_sync(&intel_crtc->idle_timer);
4695 }
4696
652c393a
JB
4697 del_timer_sync(&dev_priv->idle_timer);
4698
e70236a8
JB
4699 if (dev_priv->display.disable_fbc)
4700 dev_priv->display.disable_fbc(dev);
4701
97f5ab66 4702 if (dev_priv->pwrctx) {
c1b5dea0
KH
4703 struct drm_i915_gem_object *obj_priv;
4704
4705 obj_priv = dev_priv->pwrctx->driver_private;
4706 I915_WRITE(PWRCTXA, obj_priv->gtt_offset &~ PWRCTX_EN);
4707 I915_READ(PWRCTXA);
97f5ab66
JB
4708 i915_gem_object_unpin(dev_priv->pwrctx);
4709 drm_gem_object_unreference(dev_priv->pwrctx);
4710 }
4711
69341a5e
KH
4712 mutex_unlock(&dev->struct_mutex);
4713
79e53945
JB
4714 drm_mode_config_cleanup(dev);
4715}
4716
4717
4718/* current intel driver doesn't take advantage of encoders
4719 always give back the encoder for the connector
4720*/
4721struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
4722{
4723 struct intel_output *intel_output = to_intel_output(connector);
4724
4725 return &intel_output->enc;
4726}
28d52043
DA
4727
4728/*
4729 * set vga decode state - true == enable VGA decode
4730 */
4731int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
4732{
4733 struct drm_i915_private *dev_priv = dev->dev_private;
4734 u16 gmch_ctrl;
4735
4736 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
4737 if (state)
4738 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
4739 else
4740 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
4741 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
4742 return 0;
4743}