drm/i915: FIFO watermark calculation fixes
[deliverable/linux.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
27#include <linux/i2c.h>
7662c8bd 28#include <linux/kernel.h>
79e53945
JB
29#include "drmP.h"
30#include "intel_drv.h"
31#include "i915_drm.h"
32#include "i915_drv.h"
a4fc5ed6 33#include "intel_dp.h"
79e53945
JB
34
35#include "drm_crtc_helper.h"
36
37bool intel_pipe_has_type (struct drm_crtc *crtc, int type);
7662c8bd 38static void intel_update_watermarks(struct drm_device *dev);
79e53945
JB
39
40typedef struct {
41 /* given values */
42 int n;
43 int m1, m2;
44 int p1, p2;
45 /* derived values */
46 int dot;
47 int vco;
48 int m;
49 int p;
50} intel_clock_t;
51
52typedef struct {
53 int min, max;
54} intel_range_t;
55
56typedef struct {
57 int dot_limit;
58 int p2_slow, p2_fast;
59} intel_p2_t;
60
61#define INTEL_P2_NUM 2
d4906093
ML
62typedef struct intel_limit intel_limit_t;
63struct intel_limit {
79e53945
JB
64 intel_range_t dot, vco, n, m, m1, m2, p, p1;
65 intel_p2_t p2;
d4906093
ML
66 bool (* find_pll)(const intel_limit_t *, struct drm_crtc *,
67 int, int, intel_clock_t *);
68};
79e53945
JB
69
70#define I8XX_DOT_MIN 25000
71#define I8XX_DOT_MAX 350000
72#define I8XX_VCO_MIN 930000
73#define I8XX_VCO_MAX 1400000
74#define I8XX_N_MIN 3
75#define I8XX_N_MAX 16
76#define I8XX_M_MIN 96
77#define I8XX_M_MAX 140
78#define I8XX_M1_MIN 18
79#define I8XX_M1_MAX 26
80#define I8XX_M2_MIN 6
81#define I8XX_M2_MAX 16
82#define I8XX_P_MIN 4
83#define I8XX_P_MAX 128
84#define I8XX_P1_MIN 2
85#define I8XX_P1_MAX 33
86#define I8XX_P1_LVDS_MIN 1
87#define I8XX_P1_LVDS_MAX 6
88#define I8XX_P2_SLOW 4
89#define I8XX_P2_FAST 2
90#define I8XX_P2_LVDS_SLOW 14
91#define I8XX_P2_LVDS_FAST 14 /* No fast option */
92#define I8XX_P2_SLOW_LIMIT 165000
93
94#define I9XX_DOT_MIN 20000
95#define I9XX_DOT_MAX 400000
96#define I9XX_VCO_MIN 1400000
97#define I9XX_VCO_MAX 2800000
2177832f
SL
98#define IGD_VCO_MIN 1700000
99#define IGD_VCO_MAX 3500000
f3cade5c
KH
100#define I9XX_N_MIN 1
101#define I9XX_N_MAX 6
2177832f
SL
102/* IGD's Ncounter is a ring counter */
103#define IGD_N_MIN 3
104#define IGD_N_MAX 6
79e53945
JB
105#define I9XX_M_MIN 70
106#define I9XX_M_MAX 120
2177832f
SL
107#define IGD_M_MIN 2
108#define IGD_M_MAX 256
79e53945 109#define I9XX_M1_MIN 10
f3cade5c 110#define I9XX_M1_MAX 22
79e53945
JB
111#define I9XX_M2_MIN 5
112#define I9XX_M2_MAX 9
2177832f
SL
113/* IGD M1 is reserved, and must be 0 */
114#define IGD_M1_MIN 0
115#define IGD_M1_MAX 0
116#define IGD_M2_MIN 0
117#define IGD_M2_MAX 254
79e53945
JB
118#define I9XX_P_SDVO_DAC_MIN 5
119#define I9XX_P_SDVO_DAC_MAX 80
120#define I9XX_P_LVDS_MIN 7
121#define I9XX_P_LVDS_MAX 98
2177832f
SL
122#define IGD_P_LVDS_MIN 7
123#define IGD_P_LVDS_MAX 112
79e53945
JB
124#define I9XX_P1_MIN 1
125#define I9XX_P1_MAX 8
126#define I9XX_P2_SDVO_DAC_SLOW 10
127#define I9XX_P2_SDVO_DAC_FAST 5
128#define I9XX_P2_SDVO_DAC_SLOW_LIMIT 200000
129#define I9XX_P2_LVDS_SLOW 14
130#define I9XX_P2_LVDS_FAST 7
131#define I9XX_P2_LVDS_SLOW_LIMIT 112000
132
044c7c41
ML
133/*The parameter is for SDVO on G4x platform*/
134#define G4X_DOT_SDVO_MIN 25000
135#define G4X_DOT_SDVO_MAX 270000
136#define G4X_VCO_MIN 1750000
137#define G4X_VCO_MAX 3500000
138#define G4X_N_SDVO_MIN 1
139#define G4X_N_SDVO_MAX 4
140#define G4X_M_SDVO_MIN 104
141#define G4X_M_SDVO_MAX 138
142#define G4X_M1_SDVO_MIN 17
143#define G4X_M1_SDVO_MAX 23
144#define G4X_M2_SDVO_MIN 5
145#define G4X_M2_SDVO_MAX 11
146#define G4X_P_SDVO_MIN 10
147#define G4X_P_SDVO_MAX 30
148#define G4X_P1_SDVO_MIN 1
149#define G4X_P1_SDVO_MAX 3
150#define G4X_P2_SDVO_SLOW 10
151#define G4X_P2_SDVO_FAST 10
152#define G4X_P2_SDVO_LIMIT 270000
153
154/*The parameter is for HDMI_DAC on G4x platform*/
155#define G4X_DOT_HDMI_DAC_MIN 22000
156#define G4X_DOT_HDMI_DAC_MAX 400000
157#define G4X_N_HDMI_DAC_MIN 1
158#define G4X_N_HDMI_DAC_MAX 4
159#define G4X_M_HDMI_DAC_MIN 104
160#define G4X_M_HDMI_DAC_MAX 138
161#define G4X_M1_HDMI_DAC_MIN 16
162#define G4X_M1_HDMI_DAC_MAX 23
163#define G4X_M2_HDMI_DAC_MIN 5
164#define G4X_M2_HDMI_DAC_MAX 11
165#define G4X_P_HDMI_DAC_MIN 5
166#define G4X_P_HDMI_DAC_MAX 80
167#define G4X_P1_HDMI_DAC_MIN 1
168#define G4X_P1_HDMI_DAC_MAX 8
169#define G4X_P2_HDMI_DAC_SLOW 10
170#define G4X_P2_HDMI_DAC_FAST 5
171#define G4X_P2_HDMI_DAC_LIMIT 165000
172
173/*The parameter is for SINGLE_CHANNEL_LVDS on G4x platform*/
174#define G4X_DOT_SINGLE_CHANNEL_LVDS_MIN 20000
175#define G4X_DOT_SINGLE_CHANNEL_LVDS_MAX 115000
176#define G4X_N_SINGLE_CHANNEL_LVDS_MIN 1
177#define G4X_N_SINGLE_CHANNEL_LVDS_MAX 3
178#define G4X_M_SINGLE_CHANNEL_LVDS_MIN 104
179#define G4X_M_SINGLE_CHANNEL_LVDS_MAX 138
180#define G4X_M1_SINGLE_CHANNEL_LVDS_MIN 17
181#define G4X_M1_SINGLE_CHANNEL_LVDS_MAX 23
182#define G4X_M2_SINGLE_CHANNEL_LVDS_MIN 5
183#define G4X_M2_SINGLE_CHANNEL_LVDS_MAX 11
184#define G4X_P_SINGLE_CHANNEL_LVDS_MIN 28
185#define G4X_P_SINGLE_CHANNEL_LVDS_MAX 112
186#define G4X_P1_SINGLE_CHANNEL_LVDS_MIN 2
187#define G4X_P1_SINGLE_CHANNEL_LVDS_MAX 8
188#define G4X_P2_SINGLE_CHANNEL_LVDS_SLOW 14
189#define G4X_P2_SINGLE_CHANNEL_LVDS_FAST 14
190#define G4X_P2_SINGLE_CHANNEL_LVDS_LIMIT 0
191
192/*The parameter is for DUAL_CHANNEL_LVDS on G4x platform*/
193#define G4X_DOT_DUAL_CHANNEL_LVDS_MIN 80000
194#define G4X_DOT_DUAL_CHANNEL_LVDS_MAX 224000
195#define G4X_N_DUAL_CHANNEL_LVDS_MIN 1
196#define G4X_N_DUAL_CHANNEL_LVDS_MAX 3
197#define G4X_M_DUAL_CHANNEL_LVDS_MIN 104
198#define G4X_M_DUAL_CHANNEL_LVDS_MAX 138
199#define G4X_M1_DUAL_CHANNEL_LVDS_MIN 17
200#define G4X_M1_DUAL_CHANNEL_LVDS_MAX 23
201#define G4X_M2_DUAL_CHANNEL_LVDS_MIN 5
202#define G4X_M2_DUAL_CHANNEL_LVDS_MAX 11
203#define G4X_P_DUAL_CHANNEL_LVDS_MIN 14
204#define G4X_P_DUAL_CHANNEL_LVDS_MAX 42
205#define G4X_P1_DUAL_CHANNEL_LVDS_MIN 2
206#define G4X_P1_DUAL_CHANNEL_LVDS_MAX 6
207#define G4X_P2_DUAL_CHANNEL_LVDS_SLOW 7
208#define G4X_P2_DUAL_CHANNEL_LVDS_FAST 7
209#define G4X_P2_DUAL_CHANNEL_LVDS_LIMIT 0
210
a4fc5ed6
KP
211/*The parameter is for DISPLAY PORT on G4x platform*/
212#define G4X_DOT_DISPLAY_PORT_MIN 161670
213#define G4X_DOT_DISPLAY_PORT_MAX 227000
214#define G4X_N_DISPLAY_PORT_MIN 1
215#define G4X_N_DISPLAY_PORT_MAX 2
216#define G4X_M_DISPLAY_PORT_MIN 97
217#define G4X_M_DISPLAY_PORT_MAX 108
218#define G4X_M1_DISPLAY_PORT_MIN 0x10
219#define G4X_M1_DISPLAY_PORT_MAX 0x12
220#define G4X_M2_DISPLAY_PORT_MIN 0x05
221#define G4X_M2_DISPLAY_PORT_MAX 0x06
222#define G4X_P_DISPLAY_PORT_MIN 10
223#define G4X_P_DISPLAY_PORT_MAX 20
224#define G4X_P1_DISPLAY_PORT_MIN 1
225#define G4X_P1_DISPLAY_PORT_MAX 2
226#define G4X_P2_DISPLAY_PORT_SLOW 10
227#define G4X_P2_DISPLAY_PORT_FAST 10
228#define G4X_P2_DISPLAY_PORT_LIMIT 0
229
2c07245f
ZW
230/* IGDNG */
231/* as we calculate clock using (register_value + 2) for
232 N/M1/M2, so here the range value for them is (actual_value-2).
233 */
234#define IGDNG_DOT_MIN 25000
235#define IGDNG_DOT_MAX 350000
236#define IGDNG_VCO_MIN 1760000
237#define IGDNG_VCO_MAX 3510000
238#define IGDNG_N_MIN 1
239#define IGDNG_N_MAX 5
240#define IGDNG_M_MIN 79
241#define IGDNG_M_MAX 118
242#define IGDNG_M1_MIN 12
243#define IGDNG_M1_MAX 23
244#define IGDNG_M2_MIN 5
245#define IGDNG_M2_MAX 9
246#define IGDNG_P_SDVO_DAC_MIN 5
247#define IGDNG_P_SDVO_DAC_MAX 80
248#define IGDNG_P_LVDS_MIN 28
249#define IGDNG_P_LVDS_MAX 112
250#define IGDNG_P1_MIN 1
251#define IGDNG_P1_MAX 8
252#define IGDNG_P2_SDVO_DAC_SLOW 10
253#define IGDNG_P2_SDVO_DAC_FAST 5
254#define IGDNG_P2_LVDS_SLOW 14 /* single channel */
255#define IGDNG_P2_LVDS_FAST 7 /* double channel */
256#define IGDNG_P2_DOT_LIMIT 225000 /* 225Mhz */
257
d4906093
ML
258static bool
259intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
260 int target, int refclk, intel_clock_t *best_clock);
261static bool
262intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
263 int target, int refclk, intel_clock_t *best_clock);
2c07245f
ZW
264static bool
265intel_igdng_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
266 int target, int refclk, intel_clock_t *best_clock);
79e53945 267
a4fc5ed6
KP
268static bool
269intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
270 int target, int refclk, intel_clock_t *best_clock);
271
e4b36699 272static const intel_limit_t intel_limits_i8xx_dvo = {
79e53945
JB
273 .dot = { .min = I8XX_DOT_MIN, .max = I8XX_DOT_MAX },
274 .vco = { .min = I8XX_VCO_MIN, .max = I8XX_VCO_MAX },
275 .n = { .min = I8XX_N_MIN, .max = I8XX_N_MAX },
276 .m = { .min = I8XX_M_MIN, .max = I8XX_M_MAX },
277 .m1 = { .min = I8XX_M1_MIN, .max = I8XX_M1_MAX },
278 .m2 = { .min = I8XX_M2_MIN, .max = I8XX_M2_MAX },
279 .p = { .min = I8XX_P_MIN, .max = I8XX_P_MAX },
280 .p1 = { .min = I8XX_P1_MIN, .max = I8XX_P1_MAX },
281 .p2 = { .dot_limit = I8XX_P2_SLOW_LIMIT,
282 .p2_slow = I8XX_P2_SLOW, .p2_fast = I8XX_P2_FAST },
d4906093 283 .find_pll = intel_find_best_PLL,
e4b36699
KP
284};
285
286static const intel_limit_t intel_limits_i8xx_lvds = {
79e53945
JB
287 .dot = { .min = I8XX_DOT_MIN, .max = I8XX_DOT_MAX },
288 .vco = { .min = I8XX_VCO_MIN, .max = I8XX_VCO_MAX },
289 .n = { .min = I8XX_N_MIN, .max = I8XX_N_MAX },
290 .m = { .min = I8XX_M_MIN, .max = I8XX_M_MAX },
291 .m1 = { .min = I8XX_M1_MIN, .max = I8XX_M1_MAX },
292 .m2 = { .min = I8XX_M2_MIN, .max = I8XX_M2_MAX },
293 .p = { .min = I8XX_P_MIN, .max = I8XX_P_MAX },
294 .p1 = { .min = I8XX_P1_LVDS_MIN, .max = I8XX_P1_LVDS_MAX },
295 .p2 = { .dot_limit = I8XX_P2_SLOW_LIMIT,
296 .p2_slow = I8XX_P2_LVDS_SLOW, .p2_fast = I8XX_P2_LVDS_FAST },
d4906093 297 .find_pll = intel_find_best_PLL,
e4b36699
KP
298};
299
300static const intel_limit_t intel_limits_i9xx_sdvo = {
79e53945
JB
301 .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
302 .vco = { .min = I9XX_VCO_MIN, .max = I9XX_VCO_MAX },
303 .n = { .min = I9XX_N_MIN, .max = I9XX_N_MAX },
304 .m = { .min = I9XX_M_MIN, .max = I9XX_M_MAX },
305 .m1 = { .min = I9XX_M1_MIN, .max = I9XX_M1_MAX },
306 .m2 = { .min = I9XX_M2_MIN, .max = I9XX_M2_MAX },
307 .p = { .min = I9XX_P_SDVO_DAC_MIN, .max = I9XX_P_SDVO_DAC_MAX },
308 .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
309 .p2 = { .dot_limit = I9XX_P2_SDVO_DAC_SLOW_LIMIT,
310 .p2_slow = I9XX_P2_SDVO_DAC_SLOW, .p2_fast = I9XX_P2_SDVO_DAC_FAST },
d4906093 311 .find_pll = intel_find_best_PLL,
e4b36699
KP
312};
313
314static const intel_limit_t intel_limits_i9xx_lvds = {
79e53945
JB
315 .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
316 .vco = { .min = I9XX_VCO_MIN, .max = I9XX_VCO_MAX },
317 .n = { .min = I9XX_N_MIN, .max = I9XX_N_MAX },
318 .m = { .min = I9XX_M_MIN, .max = I9XX_M_MAX },
319 .m1 = { .min = I9XX_M1_MIN, .max = I9XX_M1_MAX },
320 .m2 = { .min = I9XX_M2_MIN, .max = I9XX_M2_MAX },
321 .p = { .min = I9XX_P_LVDS_MIN, .max = I9XX_P_LVDS_MAX },
322 .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
323 /* The single-channel range is 25-112Mhz, and dual-channel
324 * is 80-224Mhz. Prefer single channel as much as possible.
325 */
326 .p2 = { .dot_limit = I9XX_P2_LVDS_SLOW_LIMIT,
327 .p2_slow = I9XX_P2_LVDS_SLOW, .p2_fast = I9XX_P2_LVDS_FAST },
d4906093 328 .find_pll = intel_find_best_PLL,
e4b36699
KP
329};
330
044c7c41 331 /* below parameter and function is for G4X Chipset Family*/
e4b36699 332static const intel_limit_t intel_limits_g4x_sdvo = {
044c7c41
ML
333 .dot = { .min = G4X_DOT_SDVO_MIN, .max = G4X_DOT_SDVO_MAX },
334 .vco = { .min = G4X_VCO_MIN, .max = G4X_VCO_MAX},
335 .n = { .min = G4X_N_SDVO_MIN, .max = G4X_N_SDVO_MAX },
336 .m = { .min = G4X_M_SDVO_MIN, .max = G4X_M_SDVO_MAX },
337 .m1 = { .min = G4X_M1_SDVO_MIN, .max = G4X_M1_SDVO_MAX },
338 .m2 = { .min = G4X_M2_SDVO_MIN, .max = G4X_M2_SDVO_MAX },
339 .p = { .min = G4X_P_SDVO_MIN, .max = G4X_P_SDVO_MAX },
340 .p1 = { .min = G4X_P1_SDVO_MIN, .max = G4X_P1_SDVO_MAX},
341 .p2 = { .dot_limit = G4X_P2_SDVO_LIMIT,
342 .p2_slow = G4X_P2_SDVO_SLOW,
343 .p2_fast = G4X_P2_SDVO_FAST
344 },
d4906093 345 .find_pll = intel_g4x_find_best_PLL,
e4b36699
KP
346};
347
348static const intel_limit_t intel_limits_g4x_hdmi = {
044c7c41
ML
349 .dot = { .min = G4X_DOT_HDMI_DAC_MIN, .max = G4X_DOT_HDMI_DAC_MAX },
350 .vco = { .min = G4X_VCO_MIN, .max = G4X_VCO_MAX},
351 .n = { .min = G4X_N_HDMI_DAC_MIN, .max = G4X_N_HDMI_DAC_MAX },
352 .m = { .min = G4X_M_HDMI_DAC_MIN, .max = G4X_M_HDMI_DAC_MAX },
353 .m1 = { .min = G4X_M1_HDMI_DAC_MIN, .max = G4X_M1_HDMI_DAC_MAX },
354 .m2 = { .min = G4X_M2_HDMI_DAC_MIN, .max = G4X_M2_HDMI_DAC_MAX },
355 .p = { .min = G4X_P_HDMI_DAC_MIN, .max = G4X_P_HDMI_DAC_MAX },
356 .p1 = { .min = G4X_P1_HDMI_DAC_MIN, .max = G4X_P1_HDMI_DAC_MAX},
357 .p2 = { .dot_limit = G4X_P2_HDMI_DAC_LIMIT,
358 .p2_slow = G4X_P2_HDMI_DAC_SLOW,
359 .p2_fast = G4X_P2_HDMI_DAC_FAST
360 },
d4906093 361 .find_pll = intel_g4x_find_best_PLL,
e4b36699
KP
362};
363
364static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
044c7c41
ML
365 .dot = { .min = G4X_DOT_SINGLE_CHANNEL_LVDS_MIN,
366 .max = G4X_DOT_SINGLE_CHANNEL_LVDS_MAX },
367 .vco = { .min = G4X_VCO_MIN,
368 .max = G4X_VCO_MAX },
369 .n = { .min = G4X_N_SINGLE_CHANNEL_LVDS_MIN,
370 .max = G4X_N_SINGLE_CHANNEL_LVDS_MAX },
371 .m = { .min = G4X_M_SINGLE_CHANNEL_LVDS_MIN,
372 .max = G4X_M_SINGLE_CHANNEL_LVDS_MAX },
373 .m1 = { .min = G4X_M1_SINGLE_CHANNEL_LVDS_MIN,
374 .max = G4X_M1_SINGLE_CHANNEL_LVDS_MAX },
375 .m2 = { .min = G4X_M2_SINGLE_CHANNEL_LVDS_MIN,
376 .max = G4X_M2_SINGLE_CHANNEL_LVDS_MAX },
377 .p = { .min = G4X_P_SINGLE_CHANNEL_LVDS_MIN,
378 .max = G4X_P_SINGLE_CHANNEL_LVDS_MAX },
379 .p1 = { .min = G4X_P1_SINGLE_CHANNEL_LVDS_MIN,
380 .max = G4X_P1_SINGLE_CHANNEL_LVDS_MAX },
381 .p2 = { .dot_limit = G4X_P2_SINGLE_CHANNEL_LVDS_LIMIT,
382 .p2_slow = G4X_P2_SINGLE_CHANNEL_LVDS_SLOW,
383 .p2_fast = G4X_P2_SINGLE_CHANNEL_LVDS_FAST
384 },
d4906093 385 .find_pll = intel_g4x_find_best_PLL,
e4b36699
KP
386};
387
388static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
044c7c41
ML
389 .dot = { .min = G4X_DOT_DUAL_CHANNEL_LVDS_MIN,
390 .max = G4X_DOT_DUAL_CHANNEL_LVDS_MAX },
391 .vco = { .min = G4X_VCO_MIN,
392 .max = G4X_VCO_MAX },
393 .n = { .min = G4X_N_DUAL_CHANNEL_LVDS_MIN,
394 .max = G4X_N_DUAL_CHANNEL_LVDS_MAX },
395 .m = { .min = G4X_M_DUAL_CHANNEL_LVDS_MIN,
396 .max = G4X_M_DUAL_CHANNEL_LVDS_MAX },
397 .m1 = { .min = G4X_M1_DUAL_CHANNEL_LVDS_MIN,
398 .max = G4X_M1_DUAL_CHANNEL_LVDS_MAX },
399 .m2 = { .min = G4X_M2_DUAL_CHANNEL_LVDS_MIN,
400 .max = G4X_M2_DUAL_CHANNEL_LVDS_MAX },
401 .p = { .min = G4X_P_DUAL_CHANNEL_LVDS_MIN,
402 .max = G4X_P_DUAL_CHANNEL_LVDS_MAX },
403 .p1 = { .min = G4X_P1_DUAL_CHANNEL_LVDS_MIN,
404 .max = G4X_P1_DUAL_CHANNEL_LVDS_MAX },
405 .p2 = { .dot_limit = G4X_P2_DUAL_CHANNEL_LVDS_LIMIT,
406 .p2_slow = G4X_P2_DUAL_CHANNEL_LVDS_SLOW,
407 .p2_fast = G4X_P2_DUAL_CHANNEL_LVDS_FAST
408 },
d4906093 409 .find_pll = intel_g4x_find_best_PLL,
e4b36699
KP
410};
411
412static const intel_limit_t intel_limits_g4x_display_port = {
a4fc5ed6
KP
413 .dot = { .min = G4X_DOT_DISPLAY_PORT_MIN,
414 .max = G4X_DOT_DISPLAY_PORT_MAX },
415 .vco = { .min = G4X_VCO_MIN,
416 .max = G4X_VCO_MAX},
417 .n = { .min = G4X_N_DISPLAY_PORT_MIN,
418 .max = G4X_N_DISPLAY_PORT_MAX },
419 .m = { .min = G4X_M_DISPLAY_PORT_MIN,
420 .max = G4X_M_DISPLAY_PORT_MAX },
421 .m1 = { .min = G4X_M1_DISPLAY_PORT_MIN,
422 .max = G4X_M1_DISPLAY_PORT_MAX },
423 .m2 = { .min = G4X_M2_DISPLAY_PORT_MIN,
424 .max = G4X_M2_DISPLAY_PORT_MAX },
425 .p = { .min = G4X_P_DISPLAY_PORT_MIN,
426 .max = G4X_P_DISPLAY_PORT_MAX },
427 .p1 = { .min = G4X_P1_DISPLAY_PORT_MIN,
428 .max = G4X_P1_DISPLAY_PORT_MAX},
429 .p2 = { .dot_limit = G4X_P2_DISPLAY_PORT_LIMIT,
430 .p2_slow = G4X_P2_DISPLAY_PORT_SLOW,
431 .p2_fast = G4X_P2_DISPLAY_PORT_FAST },
432 .find_pll = intel_find_pll_g4x_dp,
e4b36699
KP
433};
434
435static const intel_limit_t intel_limits_igd_sdvo = {
2177832f
SL
436 .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX},
437 .vco = { .min = IGD_VCO_MIN, .max = IGD_VCO_MAX },
438 .n = { .min = IGD_N_MIN, .max = IGD_N_MAX },
439 .m = { .min = IGD_M_MIN, .max = IGD_M_MAX },
440 .m1 = { .min = IGD_M1_MIN, .max = IGD_M1_MAX },
441 .m2 = { .min = IGD_M2_MIN, .max = IGD_M2_MAX },
442 .p = { .min = I9XX_P_SDVO_DAC_MIN, .max = I9XX_P_SDVO_DAC_MAX },
443 .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
444 .p2 = { .dot_limit = I9XX_P2_SDVO_DAC_SLOW_LIMIT,
445 .p2_slow = I9XX_P2_SDVO_DAC_SLOW, .p2_fast = I9XX_P2_SDVO_DAC_FAST },
6115707b 446 .find_pll = intel_find_best_PLL,
e4b36699
KP
447};
448
449static const intel_limit_t intel_limits_igd_lvds = {
2177832f
SL
450 .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
451 .vco = { .min = IGD_VCO_MIN, .max = IGD_VCO_MAX },
452 .n = { .min = IGD_N_MIN, .max = IGD_N_MAX },
453 .m = { .min = IGD_M_MIN, .max = IGD_M_MAX },
454 .m1 = { .min = IGD_M1_MIN, .max = IGD_M1_MAX },
455 .m2 = { .min = IGD_M2_MIN, .max = IGD_M2_MAX },
456 .p = { .min = IGD_P_LVDS_MIN, .max = IGD_P_LVDS_MAX },
457 .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
458 /* IGD only supports single-channel mode. */
459 .p2 = { .dot_limit = I9XX_P2_LVDS_SLOW_LIMIT,
460 .p2_slow = I9XX_P2_LVDS_SLOW, .p2_fast = I9XX_P2_LVDS_SLOW },
6115707b 461 .find_pll = intel_find_best_PLL,
e4b36699
KP
462};
463
464static const intel_limit_t intel_limits_igdng_sdvo = {
2c07245f
ZW
465 .dot = { .min = IGDNG_DOT_MIN, .max = IGDNG_DOT_MAX },
466 .vco = { .min = IGDNG_VCO_MIN, .max = IGDNG_VCO_MAX },
467 .n = { .min = IGDNG_N_MIN, .max = IGDNG_N_MAX },
468 .m = { .min = IGDNG_M_MIN, .max = IGDNG_M_MAX },
469 .m1 = { .min = IGDNG_M1_MIN, .max = IGDNG_M1_MAX },
470 .m2 = { .min = IGDNG_M2_MIN, .max = IGDNG_M2_MAX },
471 .p = { .min = IGDNG_P_SDVO_DAC_MIN, .max = IGDNG_P_SDVO_DAC_MAX },
472 .p1 = { .min = IGDNG_P1_MIN, .max = IGDNG_P1_MAX },
473 .p2 = { .dot_limit = IGDNG_P2_DOT_LIMIT,
474 .p2_slow = IGDNG_P2_SDVO_DAC_SLOW,
475 .p2_fast = IGDNG_P2_SDVO_DAC_FAST },
476 .find_pll = intel_igdng_find_best_PLL,
e4b36699
KP
477};
478
479static const intel_limit_t intel_limits_igdng_lvds = {
2c07245f
ZW
480 .dot = { .min = IGDNG_DOT_MIN, .max = IGDNG_DOT_MAX },
481 .vco = { .min = IGDNG_VCO_MIN, .max = IGDNG_VCO_MAX },
482 .n = { .min = IGDNG_N_MIN, .max = IGDNG_N_MAX },
483 .m = { .min = IGDNG_M_MIN, .max = IGDNG_M_MAX },
484 .m1 = { .min = IGDNG_M1_MIN, .max = IGDNG_M1_MAX },
485 .m2 = { .min = IGDNG_M2_MIN, .max = IGDNG_M2_MAX },
486 .p = { .min = IGDNG_P_LVDS_MIN, .max = IGDNG_P_LVDS_MAX },
487 .p1 = { .min = IGDNG_P1_MIN, .max = IGDNG_P1_MAX },
488 .p2 = { .dot_limit = IGDNG_P2_DOT_LIMIT,
489 .p2_slow = IGDNG_P2_LVDS_SLOW,
490 .p2_fast = IGDNG_P2_LVDS_FAST },
491 .find_pll = intel_igdng_find_best_PLL,
79e53945
JB
492};
493
2c07245f
ZW
494static const intel_limit_t *intel_igdng_limit(struct drm_crtc *crtc)
495{
496 const intel_limit_t *limit;
497 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
e4b36699 498 limit = &intel_limits_igdng_lvds;
2c07245f 499 else
e4b36699 500 limit = &intel_limits_igdng_sdvo;
2c07245f
ZW
501
502 return limit;
503}
504
044c7c41
ML
505static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
506{
507 struct drm_device *dev = crtc->dev;
508 struct drm_i915_private *dev_priv = dev->dev_private;
509 const intel_limit_t *limit;
510
511 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
512 if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
513 LVDS_CLKB_POWER_UP)
514 /* LVDS with dual channel */
e4b36699 515 limit = &intel_limits_g4x_dual_channel_lvds;
044c7c41
ML
516 else
517 /* LVDS with dual channel */
e4b36699 518 limit = &intel_limits_g4x_single_channel_lvds;
044c7c41
ML
519 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
520 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
e4b36699 521 limit = &intel_limits_g4x_hdmi;
044c7c41 522 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
e4b36699 523 limit = &intel_limits_g4x_sdvo;
a4fc5ed6 524 } else if (intel_pipe_has_type (crtc, INTEL_OUTPUT_DISPLAYPORT)) {
e4b36699 525 limit = &intel_limits_g4x_display_port;
044c7c41 526 } else /* The option is for other outputs */
e4b36699 527 limit = &intel_limits_i9xx_sdvo;
044c7c41
ML
528
529 return limit;
530}
531
79e53945
JB
532static const intel_limit_t *intel_limit(struct drm_crtc *crtc)
533{
534 struct drm_device *dev = crtc->dev;
535 const intel_limit_t *limit;
536
2c07245f
ZW
537 if (IS_IGDNG(dev))
538 limit = intel_igdng_limit(crtc);
539 else if (IS_G4X(dev)) {
044c7c41 540 limit = intel_g4x_limit(crtc);
2177832f 541 } else if (IS_I9XX(dev) && !IS_IGD(dev)) {
79e53945 542 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
e4b36699 543 limit = &intel_limits_i9xx_lvds;
79e53945 544 else
e4b36699 545 limit = &intel_limits_i9xx_sdvo;
2177832f
SL
546 } else if (IS_IGD(dev)) {
547 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
e4b36699 548 limit = &intel_limits_igd_lvds;
2177832f 549 else
e4b36699 550 limit = &intel_limits_igd_sdvo;
79e53945
JB
551 } else {
552 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
e4b36699 553 limit = &intel_limits_i8xx_lvds;
79e53945 554 else
e4b36699 555 limit = &intel_limits_i8xx_dvo;
79e53945
JB
556 }
557 return limit;
558}
559
2177832f
SL
560/* m1 is reserved as 0 in IGD, n is a ring counter */
561static void igd_clock(int refclk, intel_clock_t *clock)
79e53945 562{
2177832f
SL
563 clock->m = clock->m2 + 2;
564 clock->p = clock->p1 * clock->p2;
565 clock->vco = refclk * clock->m / clock->n;
566 clock->dot = clock->vco / clock->p;
567}
568
569static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
570{
571 if (IS_IGD(dev)) {
572 igd_clock(refclk, clock);
573 return;
574 }
79e53945
JB
575 clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
576 clock->p = clock->p1 * clock->p2;
577 clock->vco = refclk * clock->m / (clock->n + 2);
578 clock->dot = clock->vco / clock->p;
579}
580
79e53945
JB
581/**
582 * Returns whether any output on the specified pipe is of the specified type
583 */
584bool intel_pipe_has_type (struct drm_crtc *crtc, int type)
585{
586 struct drm_device *dev = crtc->dev;
587 struct drm_mode_config *mode_config = &dev->mode_config;
588 struct drm_connector *l_entry;
589
590 list_for_each_entry(l_entry, &mode_config->connector_list, head) {
591 if (l_entry->encoder &&
592 l_entry->encoder->crtc == crtc) {
593 struct intel_output *intel_output = to_intel_output(l_entry);
594 if (intel_output->type == type)
595 return true;
596 }
597 }
598 return false;
599}
600
7c04d1d9 601#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
602/**
603 * Returns whether the given set of divisors are valid for a given refclk with
604 * the given connectors.
605 */
606
607static bool intel_PLL_is_valid(struct drm_crtc *crtc, intel_clock_t *clock)
608{
609 const intel_limit_t *limit = intel_limit (crtc);
2177832f 610 struct drm_device *dev = crtc->dev;
79e53945
JB
611
612 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
613 INTELPllInvalid ("p1 out of range\n");
614 if (clock->p < limit->p.min || limit->p.max < clock->p)
615 INTELPllInvalid ("p out of range\n");
616 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
617 INTELPllInvalid ("m2 out of range\n");
618 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
619 INTELPllInvalid ("m1 out of range\n");
2177832f 620 if (clock->m1 <= clock->m2 && !IS_IGD(dev))
79e53945
JB
621 INTELPllInvalid ("m1 <= m2\n");
622 if (clock->m < limit->m.min || limit->m.max < clock->m)
623 INTELPllInvalid ("m out of range\n");
624 if (clock->n < limit->n.min || limit->n.max < clock->n)
625 INTELPllInvalid ("n out of range\n");
626 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
627 INTELPllInvalid ("vco out of range\n");
628 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
629 * connector, etc., rather than just a single range.
630 */
631 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
632 INTELPllInvalid ("dot out of range\n");
633
634 return true;
635}
636
d4906093
ML
637static bool
638intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
639 int target, int refclk, intel_clock_t *best_clock)
640
79e53945
JB
641{
642 struct drm_device *dev = crtc->dev;
643 struct drm_i915_private *dev_priv = dev->dev_private;
644 intel_clock_t clock;
79e53945
JB
645 int err = target;
646
647 if (IS_I9XX(dev) && intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
832cc28d 648 (I915_READ(LVDS)) != 0) {
79e53945
JB
649 /*
650 * For LVDS, if the panel is on, just rely on its current
651 * settings for dual-channel. We haven't figured out how to
652 * reliably set up different single/dual channel state, if we
653 * even can.
654 */
655 if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
656 LVDS_CLKB_POWER_UP)
657 clock.p2 = limit->p2.p2_fast;
658 else
659 clock.p2 = limit->p2.p2_slow;
660 } else {
661 if (target < limit->p2.dot_limit)
662 clock.p2 = limit->p2.p2_slow;
663 else
664 clock.p2 = limit->p2.p2_fast;
665 }
666
667 memset (best_clock, 0, sizeof (*best_clock));
668
669 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
2177832f
SL
670 for (clock.m2 = limit->m2.min; clock.m2 <= limit->m2.max; clock.m2++) {
671 /* m1 is always 0 in IGD */
672 if (clock.m2 >= clock.m1 && !IS_IGD(dev))
673 break;
79e53945
JB
674 for (clock.n = limit->n.min; clock.n <= limit->n.max;
675 clock.n++) {
676 for (clock.p1 = limit->p1.min;
677 clock.p1 <= limit->p1.max; clock.p1++) {
678 int this_err;
679
2177832f 680 intel_clock(dev, refclk, &clock);
79e53945
JB
681
682 if (!intel_PLL_is_valid(crtc, &clock))
683 continue;
684
685 this_err = abs(clock.dot - target);
686 if (this_err < err) {
687 *best_clock = clock;
688 err = this_err;
689 }
690 }
691 }
692 }
693 }
694
695 return (err != target);
696}
697
d4906093
ML
698static bool
699intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
700 int target, int refclk, intel_clock_t *best_clock)
701{
702 struct drm_device *dev = crtc->dev;
703 struct drm_i915_private *dev_priv = dev->dev_private;
704 intel_clock_t clock;
705 int max_n;
706 bool found;
707 /* approximately equals target * 0.00488 */
708 int err_most = (target >> 8) + (target >> 10);
709 found = false;
710
711 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
712 if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
713 LVDS_CLKB_POWER_UP)
714 clock.p2 = limit->p2.p2_fast;
715 else
716 clock.p2 = limit->p2.p2_slow;
717 } else {
718 if (target < limit->p2.dot_limit)
719 clock.p2 = limit->p2.p2_slow;
720 else
721 clock.p2 = limit->p2.p2_fast;
722 }
723
724 memset(best_clock, 0, sizeof(*best_clock));
725 max_n = limit->n.max;
726 /* based on hardware requriment prefer smaller n to precision */
727 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
728 /* based on hardware requirment prefere larger m1,m2, p1 */
729 for (clock.m1 = limit->m1.max;
730 clock.m1 >= limit->m1.min; clock.m1--) {
731 for (clock.m2 = limit->m2.max;
732 clock.m2 >= limit->m2.min; clock.m2--) {
733 for (clock.p1 = limit->p1.max;
734 clock.p1 >= limit->p1.min; clock.p1--) {
735 int this_err;
736
2177832f 737 intel_clock(dev, refclk, &clock);
d4906093
ML
738 if (!intel_PLL_is_valid(crtc, &clock))
739 continue;
740 this_err = abs(clock.dot - target) ;
741 if (this_err < err_most) {
742 *best_clock = clock;
743 err_most = this_err;
744 max_n = clock.n;
745 found = true;
746 }
747 }
748 }
749 }
750 }
2c07245f
ZW
751 return found;
752}
753
754static bool
755intel_igdng_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
756 int target, int refclk, intel_clock_t *best_clock)
757{
758 struct drm_device *dev = crtc->dev;
759 struct drm_i915_private *dev_priv = dev->dev_private;
760 intel_clock_t clock;
761 int max_n;
762 bool found;
763 int err_most = 47;
764 found = false;
765
766 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
767 if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
768 LVDS_CLKB_POWER_UP)
769 clock.p2 = limit->p2.p2_fast;
770 else
771 clock.p2 = limit->p2.p2_slow;
772 } else {
773 if (target < limit->p2.dot_limit)
774 clock.p2 = limit->p2.p2_slow;
775 else
776 clock.p2 = limit->p2.p2_fast;
777 }
778
779 memset(best_clock, 0, sizeof(*best_clock));
780 max_n = limit->n.max;
781 /* based on hardware requriment prefer smaller n to precision */
782 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
783 /* based on hardware requirment prefere larger m1,m2, p1 */
784 for (clock.m1 = limit->m1.max;
785 clock.m1 >= limit->m1.min; clock.m1--) {
786 for (clock.m2 = limit->m2.max;
787 clock.m2 >= limit->m2.min; clock.m2--) {
788 for (clock.p1 = limit->p1.max;
789 clock.p1 >= limit->p1.min; clock.p1--) {
790 int this_err;
d4906093 791
2c07245f
ZW
792 intel_clock(dev, refclk, &clock);
793 if (!intel_PLL_is_valid(crtc, &clock))
794 continue;
795 this_err = abs((10000 - (target*10000/clock.dot)));
796 if (this_err < err_most) {
797 *best_clock = clock;
798 err_most = this_err;
799 max_n = clock.n;
800 found = true;
801 /* found on first matching */
802 goto out;
803 }
804 }
805 }
806 }
807 }
808out:
d4906093
ML
809 return found;
810}
811
a4fc5ed6
KP
812/* DisplayPort has only two frequencies, 162MHz and 270MHz */
813static bool
814intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
815 int target, int refclk, intel_clock_t *best_clock)
816{
817 intel_clock_t clock;
818 if (target < 200000) {
a4fc5ed6
KP
819 clock.p1 = 2;
820 clock.p2 = 10;
b3d25495
KP
821 clock.n = 2;
822 clock.m1 = 23;
823 clock.m2 = 8;
a4fc5ed6 824 } else {
a4fc5ed6
KP
825 clock.p1 = 1;
826 clock.p2 = 10;
b3d25495
KP
827 clock.n = 1;
828 clock.m1 = 14;
829 clock.m2 = 2;
a4fc5ed6 830 }
b3d25495
KP
831 clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
832 clock.p = (clock.p1 * clock.p2);
833 clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
a4fc5ed6
KP
834 memcpy(best_clock, &clock, sizeof(intel_clock_t));
835 return true;
836}
837
79e53945
JB
838void
839intel_wait_for_vblank(struct drm_device *dev)
840{
841 /* Wait for 20ms, i.e. one cycle at 50hz. */
580982d3 842 mdelay(20);
79e53945
JB
843}
844
5c3b82e2 845static int
3c4fdcfb
KH
846intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
847 struct drm_framebuffer *old_fb)
79e53945
JB
848{
849 struct drm_device *dev = crtc->dev;
850 struct drm_i915_private *dev_priv = dev->dev_private;
851 struct drm_i915_master_private *master_priv;
852 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
853 struct intel_framebuffer *intel_fb;
854 struct drm_i915_gem_object *obj_priv;
855 struct drm_gem_object *obj;
856 int pipe = intel_crtc->pipe;
857 unsigned long Start, Offset;
858 int dspbase = (pipe == 0 ? DSPAADDR : DSPBADDR);
859 int dspsurf = (pipe == 0 ? DSPASURF : DSPBSURF);
860 int dspstride = (pipe == 0) ? DSPASTRIDE : DSPBSTRIDE;
f544847f 861 int dsptileoff = (pipe == 0 ? DSPATILEOFF : DSPBTILEOFF);
79e53945 862 int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
3c4fdcfb 863 u32 dspcntr, alignment;
5c3b82e2 864 int ret;
79e53945
JB
865
866 /* no fb bound */
867 if (!crtc->fb) {
868 DRM_DEBUG("No FB bound\n");
5c3b82e2
CW
869 return 0;
870 }
871
872 switch (pipe) {
873 case 0:
874 case 1:
875 break;
876 default:
877 DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
878 return -EINVAL;
79e53945
JB
879 }
880
881 intel_fb = to_intel_framebuffer(crtc->fb);
79e53945
JB
882 obj = intel_fb->obj;
883 obj_priv = obj->driver_private;
884
3c4fdcfb
KH
885 switch (obj_priv->tiling_mode) {
886 case I915_TILING_NONE:
887 alignment = 64 * 1024;
888 break;
889 case I915_TILING_X:
2ebed176
CW
890 /* pin() will align the object as required by fence */
891 alignment = 0;
3c4fdcfb
KH
892 break;
893 case I915_TILING_Y:
894 /* FIXME: Is this true? */
895 DRM_ERROR("Y tiled not allowed for scan out buffers\n");
5c3b82e2 896 return -EINVAL;
3c4fdcfb
KH
897 default:
898 BUG();
899 }
900
5c3b82e2 901 mutex_lock(&dev->struct_mutex);
8c4b8c3f 902 ret = i915_gem_object_pin(obj, alignment);
5c3b82e2
CW
903 if (ret != 0) {
904 mutex_unlock(&dev->struct_mutex);
905 return ret;
906 }
79e53945 907
8c4b8c3f 908 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
5c3b82e2 909 if (ret != 0) {
8c4b8c3f 910 i915_gem_object_unpin(obj);
5c3b82e2
CW
911 mutex_unlock(&dev->struct_mutex);
912 return ret;
913 }
79e53945 914
8c4b8c3f
CW
915 /* Pre-i965 needs to install a fence for tiled scan-out */
916 if (!IS_I965G(dev) &&
917 obj_priv->fence_reg == I915_FENCE_REG_NONE &&
918 obj_priv->tiling_mode != I915_TILING_NONE) {
919 ret = i915_gem_object_get_fence_reg(obj);
920 if (ret != 0) {
921 i915_gem_object_unpin(obj);
922 mutex_unlock(&dev->struct_mutex);
923 return ret;
924 }
925 }
926
79e53945 927 dspcntr = I915_READ(dspcntr_reg);
712531bf
JB
928 /* Mask out pixel format bits in case we change it */
929 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
79e53945
JB
930 switch (crtc->fb->bits_per_pixel) {
931 case 8:
932 dspcntr |= DISPPLANE_8BPP;
933 break;
934 case 16:
935 if (crtc->fb->depth == 15)
936 dspcntr |= DISPPLANE_15_16BPP;
937 else
938 dspcntr |= DISPPLANE_16BPP;
939 break;
940 case 24:
941 case 32:
942 dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
943 break;
944 default:
945 DRM_ERROR("Unknown color depth\n");
8c4b8c3f 946 i915_gem_object_unpin(obj);
5c3b82e2
CW
947 mutex_unlock(&dev->struct_mutex);
948 return -EINVAL;
79e53945 949 }
f544847f
JB
950 if (IS_I965G(dev)) {
951 if (obj_priv->tiling_mode != I915_TILING_NONE)
952 dspcntr |= DISPPLANE_TILED;
953 else
954 dspcntr &= ~DISPPLANE_TILED;
955 }
956
79e53945
JB
957 I915_WRITE(dspcntr_reg, dspcntr);
958
5c3b82e2
CW
959 Start = obj_priv->gtt_offset;
960 Offset = y * crtc->fb->pitch + x * (crtc->fb->bits_per_pixel / 8);
961
79e53945 962 DRM_DEBUG("Writing base %08lX %08lX %d %d\n", Start, Offset, x, y);
5c3b82e2 963 I915_WRITE(dspstride, crtc->fb->pitch);
79e53945
JB
964 if (IS_I965G(dev)) {
965 I915_WRITE(dspbase, Offset);
966 I915_READ(dspbase);
967 I915_WRITE(dspsurf, Start);
968 I915_READ(dspsurf);
f544847f 969 I915_WRITE(dsptileoff, (y << 16) | x);
79e53945
JB
970 } else {
971 I915_WRITE(dspbase, Start + Offset);
972 I915_READ(dspbase);
973 }
974
3c4fdcfb
KH
975 intel_wait_for_vblank(dev);
976
977 if (old_fb) {
978 intel_fb = to_intel_framebuffer(old_fb);
979 i915_gem_object_unpin(intel_fb->obj);
980 }
5c3b82e2 981 mutex_unlock(&dev->struct_mutex);
79e53945
JB
982
983 if (!dev->primary->master)
5c3b82e2 984 return 0;
79e53945
JB
985
986 master_priv = dev->primary->master->driver_priv;
987 if (!master_priv->sarea_priv)
5c3b82e2 988 return 0;
79e53945 989
5c3b82e2 990 if (pipe) {
79e53945
JB
991 master_priv->sarea_priv->pipeB_x = x;
992 master_priv->sarea_priv->pipeB_y = y;
5c3b82e2
CW
993 } else {
994 master_priv->sarea_priv->pipeA_x = x;
995 master_priv->sarea_priv->pipeA_y = y;
79e53945 996 }
5c3b82e2
CW
997
998 return 0;
79e53945
JB
999}
1000
2c07245f
ZW
1001static void igdng_crtc_dpms(struct drm_crtc *crtc, int mode)
1002{
1003 struct drm_device *dev = crtc->dev;
1004 struct drm_i915_private *dev_priv = dev->dev_private;
1005 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1006 int pipe = intel_crtc->pipe;
7662c8bd 1007 int plane = intel_crtc->plane;
2c07245f
ZW
1008 int pch_dpll_reg = (pipe == 0) ? PCH_DPLL_A : PCH_DPLL_B;
1009 int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
1010 int dspcntr_reg = (plane == 0) ? DSPACNTR : DSPBCNTR;
1011 int dspbase_reg = (plane == 0) ? DSPAADDR : DSPBADDR;
1012 int fdi_tx_reg = (pipe == 0) ? FDI_TXA_CTL : FDI_TXB_CTL;
1013 int fdi_rx_reg = (pipe == 0) ? FDI_RXA_CTL : FDI_RXB_CTL;
1014 int fdi_rx_iir_reg = (pipe == 0) ? FDI_RXA_IIR : FDI_RXB_IIR;
1015 int fdi_rx_imr_reg = (pipe == 0) ? FDI_RXA_IMR : FDI_RXB_IMR;
1016 int transconf_reg = (pipe == 0) ? TRANSACONF : TRANSBCONF;
1017 int pf_ctl_reg = (pipe == 0) ? PFA_CTL_1 : PFB_CTL_1;
1018 int cpu_htot_reg = (pipe == 0) ? HTOTAL_A : HTOTAL_B;
1019 int cpu_hblank_reg = (pipe == 0) ? HBLANK_A : HBLANK_B;
1020 int cpu_hsync_reg = (pipe == 0) ? HSYNC_A : HSYNC_B;
1021 int cpu_vtot_reg = (pipe == 0) ? VTOTAL_A : VTOTAL_B;
1022 int cpu_vblank_reg = (pipe == 0) ? VBLANK_A : VBLANK_B;
1023 int cpu_vsync_reg = (pipe == 0) ? VSYNC_A : VSYNC_B;
1024 int trans_htot_reg = (pipe == 0) ? TRANS_HTOTAL_A : TRANS_HTOTAL_B;
1025 int trans_hblank_reg = (pipe == 0) ? TRANS_HBLANK_A : TRANS_HBLANK_B;
1026 int trans_hsync_reg = (pipe == 0) ? TRANS_HSYNC_A : TRANS_HSYNC_B;
1027 int trans_vtot_reg = (pipe == 0) ? TRANS_VTOTAL_A : TRANS_VTOTAL_B;
1028 int trans_vblank_reg = (pipe == 0) ? TRANS_VBLANK_A : TRANS_VBLANK_B;
1029 int trans_vsync_reg = (pipe == 0) ? TRANS_VSYNC_A : TRANS_VSYNC_B;
1030 u32 temp;
1031 int tries = 5, j;
79e53945 1032
2c07245f
ZW
1033 /* XXX: When our outputs are all unaware of DPMS modes other than off
1034 * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
1035 */
1036 switch (mode) {
1037 case DRM_MODE_DPMS_ON:
1038 case DRM_MODE_DPMS_STANDBY:
1039 case DRM_MODE_DPMS_SUSPEND:
1040 DRM_DEBUG("crtc %d dpms on\n", pipe);
1041 /* enable PCH DPLL */
1042 temp = I915_READ(pch_dpll_reg);
1043 if ((temp & DPLL_VCO_ENABLE) == 0) {
1044 I915_WRITE(pch_dpll_reg, temp | DPLL_VCO_ENABLE);
1045 I915_READ(pch_dpll_reg);
1046 }
79e53945 1047
2c07245f
ZW
1048 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
1049 temp = I915_READ(fdi_rx_reg);
1050 I915_WRITE(fdi_rx_reg, temp | FDI_RX_PLL_ENABLE |
1051 FDI_SEL_PCDCLK |
1052 FDI_DP_PORT_WIDTH_X4); /* default 4 lanes */
1053 I915_READ(fdi_rx_reg);
1054 udelay(200);
1055
1056 /* Enable CPU FDI TX PLL, always on for IGDNG */
1057 temp = I915_READ(fdi_tx_reg);
1058 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
1059 I915_WRITE(fdi_tx_reg, temp | FDI_TX_PLL_ENABLE);
1060 I915_READ(fdi_tx_reg);
1061 udelay(100);
1062 }
1063
1064 /* Enable CPU pipe */
1065 temp = I915_READ(pipeconf_reg);
1066 if ((temp & PIPEACONF_ENABLE) == 0) {
1067 I915_WRITE(pipeconf_reg, temp | PIPEACONF_ENABLE);
1068 I915_READ(pipeconf_reg);
1069 udelay(100);
1070 }
1071
1072 /* configure and enable CPU plane */
1073 temp = I915_READ(dspcntr_reg);
1074 if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
1075 I915_WRITE(dspcntr_reg, temp | DISPLAY_PLANE_ENABLE);
1076 /* Flush the plane changes */
1077 I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
1078 }
1079
1080 /* enable CPU FDI TX and PCH FDI RX */
1081 temp = I915_READ(fdi_tx_reg);
1082 temp |= FDI_TX_ENABLE;
1083 temp |= FDI_DP_PORT_WIDTH_X4; /* default */
1084 temp &= ~FDI_LINK_TRAIN_NONE;
1085 temp |= FDI_LINK_TRAIN_PATTERN_1;
1086 I915_WRITE(fdi_tx_reg, temp);
1087 I915_READ(fdi_tx_reg);
1088
1089 temp = I915_READ(fdi_rx_reg);
1090 temp &= ~FDI_LINK_TRAIN_NONE;
1091 temp |= FDI_LINK_TRAIN_PATTERN_1;
1092 I915_WRITE(fdi_rx_reg, temp | FDI_RX_ENABLE);
1093 I915_READ(fdi_rx_reg);
1094
1095 udelay(150);
1096
1097 /* Train FDI. */
1098 /* umask FDI RX Interrupt symbol_lock and bit_lock bit
1099 for train result */
1100 temp = I915_READ(fdi_rx_imr_reg);
1101 temp &= ~FDI_RX_SYMBOL_LOCK;
1102 temp &= ~FDI_RX_BIT_LOCK;
1103 I915_WRITE(fdi_rx_imr_reg, temp);
1104 I915_READ(fdi_rx_imr_reg);
1105 udelay(150);
1106
1107 temp = I915_READ(fdi_rx_iir_reg);
1108 DRM_DEBUG("FDI_RX_IIR 0x%x\n", temp);
1109
1110 if ((temp & FDI_RX_BIT_LOCK) == 0) {
1111 for (j = 0; j < tries; j++) {
1112 temp = I915_READ(fdi_rx_iir_reg);
1113 DRM_DEBUG("FDI_RX_IIR 0x%x\n", temp);
1114 if (temp & FDI_RX_BIT_LOCK)
1115 break;
1116 udelay(200);
1117 }
1118 if (j != tries)
1119 I915_WRITE(fdi_rx_iir_reg,
1120 temp | FDI_RX_BIT_LOCK);
1121 else
1122 DRM_DEBUG("train 1 fail\n");
1123 } else {
1124 I915_WRITE(fdi_rx_iir_reg,
1125 temp | FDI_RX_BIT_LOCK);
1126 DRM_DEBUG("train 1 ok 2!\n");
1127 }
1128 temp = I915_READ(fdi_tx_reg);
1129 temp &= ~FDI_LINK_TRAIN_NONE;
1130 temp |= FDI_LINK_TRAIN_PATTERN_2;
1131 I915_WRITE(fdi_tx_reg, temp);
1132
1133 temp = I915_READ(fdi_rx_reg);
1134 temp &= ~FDI_LINK_TRAIN_NONE;
1135 temp |= FDI_LINK_TRAIN_PATTERN_2;
1136 I915_WRITE(fdi_rx_reg, temp);
1137
1138 udelay(150);
1139
1140 temp = I915_READ(fdi_rx_iir_reg);
1141 DRM_DEBUG("FDI_RX_IIR 0x%x\n", temp);
1142
1143 if ((temp & FDI_RX_SYMBOL_LOCK) == 0) {
1144 for (j = 0; j < tries; j++) {
1145 temp = I915_READ(fdi_rx_iir_reg);
1146 DRM_DEBUG("FDI_RX_IIR 0x%x\n", temp);
1147 if (temp & FDI_RX_SYMBOL_LOCK)
1148 break;
1149 udelay(200);
1150 }
1151 if (j != tries) {
1152 I915_WRITE(fdi_rx_iir_reg,
1153 temp | FDI_RX_SYMBOL_LOCK);
1154 DRM_DEBUG("train 2 ok 1!\n");
1155 } else
1156 DRM_DEBUG("train 2 fail\n");
1157 } else {
1158 I915_WRITE(fdi_rx_iir_reg, temp | FDI_RX_SYMBOL_LOCK);
1159 DRM_DEBUG("train 2 ok 2!\n");
1160 }
1161 DRM_DEBUG("train done\n");
1162
1163 /* set transcoder timing */
1164 I915_WRITE(trans_htot_reg, I915_READ(cpu_htot_reg));
1165 I915_WRITE(trans_hblank_reg, I915_READ(cpu_hblank_reg));
1166 I915_WRITE(trans_hsync_reg, I915_READ(cpu_hsync_reg));
1167
1168 I915_WRITE(trans_vtot_reg, I915_READ(cpu_vtot_reg));
1169 I915_WRITE(trans_vblank_reg, I915_READ(cpu_vblank_reg));
1170 I915_WRITE(trans_vsync_reg, I915_READ(cpu_vsync_reg));
1171
1172 /* enable PCH transcoder */
1173 temp = I915_READ(transconf_reg);
1174 I915_WRITE(transconf_reg, temp | TRANS_ENABLE);
1175 I915_READ(transconf_reg);
1176
1177 while ((I915_READ(transconf_reg) & TRANS_STATE_ENABLE) == 0)
1178 ;
1179
1180 /* enable normal */
1181
1182 temp = I915_READ(fdi_tx_reg);
1183 temp &= ~FDI_LINK_TRAIN_NONE;
1184 I915_WRITE(fdi_tx_reg, temp | FDI_LINK_TRAIN_NONE |
1185 FDI_TX_ENHANCE_FRAME_ENABLE);
1186 I915_READ(fdi_tx_reg);
1187
1188 temp = I915_READ(fdi_rx_reg);
1189 temp &= ~FDI_LINK_TRAIN_NONE;
1190 I915_WRITE(fdi_rx_reg, temp | FDI_LINK_TRAIN_NONE |
1191 FDI_RX_ENHANCE_FRAME_ENABLE);
1192 I915_READ(fdi_rx_reg);
1193
1194 /* wait one idle pattern time */
1195 udelay(100);
1196
1197 intel_crtc_load_lut(crtc);
1198
1199 break;
1200 case DRM_MODE_DPMS_OFF:
1201 DRM_DEBUG("crtc %d dpms off\n", pipe);
1202
1203 /* Disable the VGA plane that we never use */
1204 I915_WRITE(CPU_VGACNTRL, VGA_DISP_DISABLE);
1205
1206 /* Disable display plane */
1207 temp = I915_READ(dspcntr_reg);
1208 if ((temp & DISPLAY_PLANE_ENABLE) != 0) {
1209 I915_WRITE(dspcntr_reg, temp & ~DISPLAY_PLANE_ENABLE);
1210 /* Flush the plane changes */
1211 I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
1212 I915_READ(dspbase_reg);
1213 }
1214
1215 /* disable cpu pipe, disable after all planes disabled */
1216 temp = I915_READ(pipeconf_reg);
1217 if ((temp & PIPEACONF_ENABLE) != 0) {
1218 I915_WRITE(pipeconf_reg, temp & ~PIPEACONF_ENABLE);
1219 I915_READ(pipeconf_reg);
1220 /* wait for cpu pipe off, pipe state */
1221 while ((I915_READ(pipeconf_reg) & I965_PIPECONF_ACTIVE) != 0)
1222 ;
1223 } else
1224 DRM_DEBUG("crtc %d is disabled\n", pipe);
1225
1226 /* IGDNG-A : disable cpu panel fitter ? */
1227 temp = I915_READ(pf_ctl_reg);
1228 if ((temp & PF_ENABLE) != 0) {
1229 I915_WRITE(pf_ctl_reg, temp & ~PF_ENABLE);
1230 I915_READ(pf_ctl_reg);
1231 }
1232
1233 /* disable CPU FDI tx and PCH FDI rx */
1234 temp = I915_READ(fdi_tx_reg);
1235 I915_WRITE(fdi_tx_reg, temp & ~FDI_TX_ENABLE);
1236 I915_READ(fdi_tx_reg);
1237
1238 temp = I915_READ(fdi_rx_reg);
1239 I915_WRITE(fdi_rx_reg, temp & ~FDI_RX_ENABLE);
1240 I915_READ(fdi_rx_reg);
1241
1242 /* still set train pattern 1 */
1243 temp = I915_READ(fdi_tx_reg);
1244 temp &= ~FDI_LINK_TRAIN_NONE;
1245 temp |= FDI_LINK_TRAIN_PATTERN_1;
1246 I915_WRITE(fdi_tx_reg, temp);
1247
1248 temp = I915_READ(fdi_rx_reg);
1249 temp &= ~FDI_LINK_TRAIN_NONE;
1250 temp |= FDI_LINK_TRAIN_PATTERN_1;
1251 I915_WRITE(fdi_rx_reg, temp);
1252
1253 /* disable PCH transcoder */
1254 temp = I915_READ(transconf_reg);
1255 if ((temp & TRANS_ENABLE) != 0) {
1256 I915_WRITE(transconf_reg, temp & ~TRANS_ENABLE);
1257 I915_READ(transconf_reg);
1258 /* wait for PCH transcoder off, transcoder state */
1259 while ((I915_READ(transconf_reg) & TRANS_STATE_ENABLE) != 0)
1260 ;
1261 }
1262
1263 /* disable PCH DPLL */
1264 temp = I915_READ(pch_dpll_reg);
1265 if ((temp & DPLL_VCO_ENABLE) != 0) {
1266 I915_WRITE(pch_dpll_reg, temp & ~DPLL_VCO_ENABLE);
1267 I915_READ(pch_dpll_reg);
1268 }
1269
1270 temp = I915_READ(fdi_rx_reg);
1271 if ((temp & FDI_RX_PLL_ENABLE) != 0) {
1272 temp &= ~FDI_SEL_PCDCLK;
1273 temp &= ~FDI_RX_PLL_ENABLE;
1274 I915_WRITE(fdi_rx_reg, temp);
1275 I915_READ(fdi_rx_reg);
1276 }
1277
1278 /* Wait for the clocks to turn off. */
1279 udelay(150);
1280 break;
1281 }
1282}
1283
1284static void i9xx_crtc_dpms(struct drm_crtc *crtc, int mode)
79e53945
JB
1285{
1286 struct drm_device *dev = crtc->dev;
79e53945
JB
1287 struct drm_i915_private *dev_priv = dev->dev_private;
1288 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1289 int pipe = intel_crtc->pipe;
1290 int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
1291 int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
1292 int dspbase_reg = (pipe == 0) ? DSPAADDR : DSPBADDR;
1293 int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
1294 u32 temp;
79e53945
JB
1295
1296 /* XXX: When our outputs are all unaware of DPMS modes other than off
1297 * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
1298 */
1299 switch (mode) {
1300 case DRM_MODE_DPMS_ON:
1301 case DRM_MODE_DPMS_STANDBY:
1302 case DRM_MODE_DPMS_SUSPEND:
1303 /* Enable the DPLL */
1304 temp = I915_READ(dpll_reg);
1305 if ((temp & DPLL_VCO_ENABLE) == 0) {
1306 I915_WRITE(dpll_reg, temp);
1307 I915_READ(dpll_reg);
1308 /* Wait for the clocks to stabilize. */
1309 udelay(150);
1310 I915_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
1311 I915_READ(dpll_reg);
1312 /* Wait for the clocks to stabilize. */
1313 udelay(150);
1314 I915_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
1315 I915_READ(dpll_reg);
1316 /* Wait for the clocks to stabilize. */
1317 udelay(150);
1318 }
1319
1320 /* Enable the pipe */
1321 temp = I915_READ(pipeconf_reg);
1322 if ((temp & PIPEACONF_ENABLE) == 0)
1323 I915_WRITE(pipeconf_reg, temp | PIPEACONF_ENABLE);
1324
1325 /* Enable the plane */
1326 temp = I915_READ(dspcntr_reg);
1327 if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
1328 I915_WRITE(dspcntr_reg, temp | DISPLAY_PLANE_ENABLE);
1329 /* Flush the plane changes */
1330 I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
1331 }
1332
1333 intel_crtc_load_lut(crtc);
1334
1335 /* Give the overlay scaler a chance to enable if it's on this pipe */
1336 //intel_crtc_dpms_video(crtc, true); TODO
7662c8bd 1337 intel_update_watermarks(dev);
79e53945
JB
1338 break;
1339 case DRM_MODE_DPMS_OFF:
7662c8bd 1340 intel_update_watermarks(dev);
79e53945
JB
1341 /* Give the overlay scaler a chance to disable if it's on this pipe */
1342 //intel_crtc_dpms_video(crtc, FALSE); TODO
1343
1344 /* Disable the VGA plane that we never use */
1345 I915_WRITE(VGACNTRL, VGA_DISP_DISABLE);
1346
1347 /* Disable display plane */
1348 temp = I915_READ(dspcntr_reg);
1349 if ((temp & DISPLAY_PLANE_ENABLE) != 0) {
1350 I915_WRITE(dspcntr_reg, temp & ~DISPLAY_PLANE_ENABLE);
1351 /* Flush the plane changes */
1352 I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
1353 I915_READ(dspbase_reg);
1354 }
1355
1356 if (!IS_I9XX(dev)) {
1357 /* Wait for vblank for the disable to take effect */
1358 intel_wait_for_vblank(dev);
1359 }
1360
1361 /* Next, disable display pipes */
1362 temp = I915_READ(pipeconf_reg);
1363 if ((temp & PIPEACONF_ENABLE) != 0) {
1364 I915_WRITE(pipeconf_reg, temp & ~PIPEACONF_ENABLE);
1365 I915_READ(pipeconf_reg);
1366 }
1367
1368 /* Wait for vblank for the disable to take effect. */
1369 intel_wait_for_vblank(dev);
1370
1371 temp = I915_READ(dpll_reg);
1372 if ((temp & DPLL_VCO_ENABLE) != 0) {
1373 I915_WRITE(dpll_reg, temp & ~DPLL_VCO_ENABLE);
1374 I915_READ(dpll_reg);
1375 }
1376
1377 /* Wait for the clocks to turn off. */
1378 udelay(150);
1379 break;
1380 }
2c07245f
ZW
1381}
1382
1383/**
1384 * Sets the power management mode of the pipe and plane.
1385 *
1386 * This code should probably grow support for turning the cursor off and back
1387 * on appropriately at the same time as we're turning the pipe off/on.
1388 */
1389static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
1390{
1391 struct drm_device *dev = crtc->dev;
1392 struct drm_i915_master_private *master_priv;
1393 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1394 int pipe = intel_crtc->pipe;
1395 bool enabled;
1396
1397 if (IS_IGDNG(dev))
1398 igdng_crtc_dpms(crtc, mode);
1399 else
1400 i9xx_crtc_dpms(crtc, mode);
79e53945
JB
1401
1402 if (!dev->primary->master)
1403 return;
1404
1405 master_priv = dev->primary->master->driver_priv;
1406 if (!master_priv->sarea_priv)
1407 return;
1408
1409 enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
1410
1411 switch (pipe) {
1412 case 0:
1413 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
1414 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
1415 break;
1416 case 1:
1417 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
1418 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
1419 break;
1420 default:
1421 DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
1422 break;
1423 }
1424
1425 intel_crtc->dpms_mode = mode;
1426}
1427
1428static void intel_crtc_prepare (struct drm_crtc *crtc)
1429{
1430 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1431 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
1432}
1433
1434static void intel_crtc_commit (struct drm_crtc *crtc)
1435{
1436 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1437 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
1438}
1439
1440void intel_encoder_prepare (struct drm_encoder *encoder)
1441{
1442 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1443 /* lvds has its own version of prepare see intel_lvds_prepare */
1444 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
1445}
1446
1447void intel_encoder_commit (struct drm_encoder *encoder)
1448{
1449 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1450 /* lvds has its own version of commit see intel_lvds_commit */
1451 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
1452}
1453
1454static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
1455 struct drm_display_mode *mode,
1456 struct drm_display_mode *adjusted_mode)
1457{
2c07245f
ZW
1458 struct drm_device *dev = crtc->dev;
1459 if (IS_IGDNG(dev)) {
1460 /* FDI link clock is fixed at 2.7G */
1461 if (mode->clock * 3 > 27000 * 4)
1462 return MODE_CLOCK_HIGH;
1463 }
79e53945
JB
1464 return true;
1465}
1466
1467
1468/** Returns the core display clock speed for i830 - i945 */
1469static int intel_get_core_clock_speed(struct drm_device *dev)
1470{
1471
1472 /* Core clock values taken from the published datasheets.
1473 * The 830 may go up to 166 Mhz, which we should check.
1474 */
1475 if (IS_I945G(dev))
1476 return 400000;
1477 else if (IS_I915G(dev))
1478 return 333000;
2177832f 1479 else if (IS_I945GM(dev) || IS_845G(dev) || IS_IGDGM(dev))
79e53945
JB
1480 return 200000;
1481 else if (IS_I915GM(dev)) {
1482 u16 gcfgc = 0;
1483
1484 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
1485
1486 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
1487 return 133000;
1488 else {
1489 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
1490 case GC_DISPLAY_CLOCK_333_MHZ:
1491 return 333000;
1492 default:
1493 case GC_DISPLAY_CLOCK_190_200_MHZ:
1494 return 190000;
1495 }
1496 }
1497 } else if (IS_I865G(dev))
1498 return 266000;
1499 else if (IS_I855(dev)) {
1500 u16 hpllcc = 0;
1501 /* Assume that the hardware is in the high speed state. This
1502 * should be the default.
1503 */
1504 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
1505 case GC_CLOCK_133_200:
1506 case GC_CLOCK_100_200:
1507 return 200000;
1508 case GC_CLOCK_166_250:
1509 return 250000;
1510 case GC_CLOCK_100_133:
1511 return 133000;
1512 }
1513 } else /* 852, 830 */
1514 return 133000;
1515
1516 return 0; /* Silence gcc warning */
1517}
1518
79e53945
JB
1519/**
1520 * Return the pipe currently connected to the panel fitter,
1521 * or -1 if the panel fitter is not present or not in use
1522 */
1523static int intel_panel_fitter_pipe (struct drm_device *dev)
1524{
1525 struct drm_i915_private *dev_priv = dev->dev_private;
1526 u32 pfit_control;
1527
1528 /* i830 doesn't have a panel fitter */
1529 if (IS_I830(dev))
1530 return -1;
1531
1532 pfit_control = I915_READ(PFIT_CONTROL);
1533
1534 /* See if the panel fitter is in use */
1535 if ((pfit_control & PFIT_ENABLE) == 0)
1536 return -1;
1537
1538 /* 965 can place panel fitter on either pipe */
1539 if (IS_I965G(dev))
1540 return (pfit_control >> 29) & 0x3;
1541
1542 /* older chips can only use pipe 1 */
1543 return 1;
1544}
1545
2c07245f
ZW
1546struct fdi_m_n {
1547 u32 tu;
1548 u32 gmch_m;
1549 u32 gmch_n;
1550 u32 link_m;
1551 u32 link_n;
1552};
1553
1554static void
1555fdi_reduce_ratio(u32 *num, u32 *den)
1556{
1557 while (*num > 0xffffff || *den > 0xffffff) {
1558 *num >>= 1;
1559 *den >>= 1;
1560 }
1561}
1562
1563#define DATA_N 0x800000
1564#define LINK_N 0x80000
1565
1566static void
1567igdng_compute_m_n(int bytes_per_pixel, int nlanes,
1568 int pixel_clock, int link_clock,
1569 struct fdi_m_n *m_n)
1570{
1571 u64 temp;
1572
1573 m_n->tu = 64; /* default size */
1574
1575 temp = (u64) DATA_N * pixel_clock;
1576 temp = div_u64(temp, link_clock);
956dba3c 1577 m_n->gmch_m = div_u64(temp * bytes_per_pixel, nlanes);
2c07245f
ZW
1578 m_n->gmch_n = DATA_N;
1579 fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
1580
1581 temp = (u64) LINK_N * pixel_clock;
1582 m_n->link_m = div_u64(temp, link_clock);
1583 m_n->link_n = LINK_N;
1584 fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
1585}
1586
1587
7662c8bd
SL
1588struct intel_watermark_params {
1589 unsigned long fifo_size;
1590 unsigned long max_wm;
1591 unsigned long default_wm;
1592 unsigned long guard_size;
1593 unsigned long cacheline_size;
1594};
1595
1596/* IGD has different values for various configs */
1597static struct intel_watermark_params igd_display_wm = {
1598 IGD_DISPLAY_FIFO,
1599 IGD_MAX_WM,
1600 IGD_DFT_WM,
1601 IGD_GUARD_WM,
1602 IGD_FIFO_LINE_SIZE
1603};
1604static struct intel_watermark_params igd_display_hplloff_wm = {
1605 IGD_DISPLAY_FIFO,
1606 IGD_MAX_WM,
1607 IGD_DFT_HPLLOFF_WM,
1608 IGD_GUARD_WM,
1609 IGD_FIFO_LINE_SIZE
1610};
1611static struct intel_watermark_params igd_cursor_wm = {
1612 IGD_CURSOR_FIFO,
1613 IGD_CURSOR_MAX_WM,
1614 IGD_CURSOR_DFT_WM,
1615 IGD_CURSOR_GUARD_WM,
1616 IGD_FIFO_LINE_SIZE,
1617};
1618static struct intel_watermark_params igd_cursor_hplloff_wm = {
1619 IGD_CURSOR_FIFO,
1620 IGD_CURSOR_MAX_WM,
1621 IGD_CURSOR_DFT_WM,
1622 IGD_CURSOR_GUARD_WM,
1623 IGD_FIFO_LINE_SIZE
1624};
1625static struct intel_watermark_params i945_wm_info = {
dff33cfc 1626 I945_FIFO_SIZE,
7662c8bd
SL
1627 I915_MAX_WM,
1628 1,
dff33cfc
JB
1629 2,
1630 I915_FIFO_LINE_SIZE
7662c8bd
SL
1631};
1632static struct intel_watermark_params i915_wm_info = {
dff33cfc 1633 I915_FIFO_SIZE,
7662c8bd
SL
1634 I915_MAX_WM,
1635 1,
dff33cfc 1636 2,
7662c8bd
SL
1637 I915_FIFO_LINE_SIZE
1638};
1639static struct intel_watermark_params i855_wm_info = {
1640 I855GM_FIFO_SIZE,
1641 I915_MAX_WM,
1642 1,
dff33cfc 1643 2,
7662c8bd
SL
1644 I830_FIFO_LINE_SIZE
1645};
1646static struct intel_watermark_params i830_wm_info = {
1647 I830_FIFO_SIZE,
1648 I915_MAX_WM,
1649 1,
dff33cfc 1650 2,
7662c8bd
SL
1651 I830_FIFO_LINE_SIZE
1652};
1653
dff33cfc
JB
1654/**
1655 * intel_calculate_wm - calculate watermark level
1656 * @clock_in_khz: pixel clock
1657 * @wm: chip FIFO params
1658 * @pixel_size: display pixel size
1659 * @latency_ns: memory latency for the platform
1660 *
1661 * Calculate the watermark level (the level at which the display plane will
1662 * start fetching from memory again). Each chip has a different display
1663 * FIFO size and allocation, so the caller needs to figure that out and pass
1664 * in the correct intel_watermark_params structure.
1665 *
1666 * As the pixel clock runs, the FIFO will be drained at a rate that depends
1667 * on the pixel size. When it reaches the watermark level, it'll start
1668 * fetching FIFO line sized based chunks from memory until the FIFO fills
1669 * past the watermark point. If the FIFO drains completely, a FIFO underrun
1670 * will occur, and a display engine hang could result.
1671 */
7662c8bd
SL
1672static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
1673 struct intel_watermark_params *wm,
1674 int pixel_size,
1675 unsigned long latency_ns)
1676{
dff33cfc
JB
1677 unsigned long entries_required, wm_size;
1678
1679 entries_required = (clock_in_khz * pixel_size * latency_ns) / 1000000;
1680 entries_required /= wm->cacheline_size;
7662c8bd 1681
dff33cfc
JB
1682 DRM_DEBUG("FIFO entries required for mode: %d\n", entries_required);
1683
1684 wm_size = wm->fifo_size - (entries_required + wm->guard_size);
1685
1686 DRM_DEBUG("FIFO watermark level: %d\n", wm_size);
7662c8bd
SL
1687
1688 if (wm_size > wm->max_wm)
1689 wm_size = wm->max_wm;
1690 if (wm_size == 0)
1691 wm_size = wm->default_wm;
1692 return wm_size;
1693}
1694
1695struct cxsr_latency {
1696 int is_desktop;
1697 unsigned long fsb_freq;
1698 unsigned long mem_freq;
1699 unsigned long display_sr;
1700 unsigned long display_hpll_disable;
1701 unsigned long cursor_sr;
1702 unsigned long cursor_hpll_disable;
1703};
1704
1705static struct cxsr_latency cxsr_latency_table[] = {
1706 {1, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
1707 {1, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
1708 {1, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
1709
1710 {1, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
1711 {1, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
1712 {1, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
1713
1714 {1, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
1715 {1, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
1716 {1, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
1717
1718 {0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
1719 {0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
1720 {0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
1721
1722 {0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
1723 {0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
1724 {0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
1725
1726 {0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
1727 {0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
1728 {0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
1729};
1730
1731static struct cxsr_latency *intel_get_cxsr_latency(int is_desktop, int fsb,
1732 int mem)
1733{
1734 int i;
1735 struct cxsr_latency *latency;
1736
1737 if (fsb == 0 || mem == 0)
1738 return NULL;
1739
1740 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
1741 latency = &cxsr_latency_table[i];
1742 if (is_desktop == latency->is_desktop &&
1743 fsb == latency->fsb_freq && mem == latency->mem_freq)
1744 break;
1745 }
1746 if (i >= ARRAY_SIZE(cxsr_latency_table)) {
1747 DRM_DEBUG("Unknown FSB/MEM found, disable CxSR\n");
1748 return NULL;
1749 }
1750 return latency;
1751}
1752
1753static void igd_disable_cxsr(struct drm_device *dev)
1754{
1755 struct drm_i915_private *dev_priv = dev->dev_private;
1756 u32 reg;
1757
1758 /* deactivate cxsr */
1759 reg = I915_READ(DSPFW3);
1760 reg &= ~(IGD_SELF_REFRESH_EN);
1761 I915_WRITE(DSPFW3, reg);
1762 DRM_INFO("Big FIFO is disabled\n");
1763}
1764
1765static void igd_enable_cxsr(struct drm_device *dev, unsigned long clock,
1766 int pixel_size)
1767{
1768 struct drm_i915_private *dev_priv = dev->dev_private;
1769 u32 reg;
1770 unsigned long wm;
1771 struct cxsr_latency *latency;
1772
1773 latency = intel_get_cxsr_latency(IS_IGDG(dev), dev_priv->fsb_freq,
1774 dev_priv->mem_freq);
1775 if (!latency) {
1776 DRM_DEBUG("Unknown FSB/MEM found, disable CxSR\n");
1777 igd_disable_cxsr(dev);
1778 return;
1779 }
1780
1781 /* Display SR */
1782 wm = intel_calculate_wm(clock, &igd_display_wm, pixel_size,
1783 latency->display_sr);
1784 reg = I915_READ(DSPFW1);
1785 reg &= 0x7fffff;
1786 reg |= wm << 23;
1787 I915_WRITE(DSPFW1, reg);
1788 DRM_DEBUG("DSPFW1 register is %x\n", reg);
1789
1790 /* cursor SR */
1791 wm = intel_calculate_wm(clock, &igd_cursor_wm, pixel_size,
1792 latency->cursor_sr);
1793 reg = I915_READ(DSPFW3);
1794 reg &= ~(0x3f << 24);
1795 reg |= (wm & 0x3f) << 24;
1796 I915_WRITE(DSPFW3, reg);
1797
1798 /* Display HPLL off SR */
1799 wm = intel_calculate_wm(clock, &igd_display_hplloff_wm,
1800 latency->display_hpll_disable, I915_FIFO_LINE_SIZE);
1801 reg = I915_READ(DSPFW3);
1802 reg &= 0xfffffe00;
1803 reg |= wm & 0x1ff;
1804 I915_WRITE(DSPFW3, reg);
1805
1806 /* cursor HPLL off SR */
1807 wm = intel_calculate_wm(clock, &igd_cursor_hplloff_wm, pixel_size,
1808 latency->cursor_hpll_disable);
1809 reg = I915_READ(DSPFW3);
1810 reg &= ~(0x3f << 16);
1811 reg |= (wm & 0x3f) << 16;
1812 I915_WRITE(DSPFW3, reg);
1813 DRM_DEBUG("DSPFW3 register is %x\n", reg);
1814
1815 /* activate cxsr */
1816 reg = I915_READ(DSPFW3);
1817 reg |= IGD_SELF_REFRESH_EN;
1818 I915_WRITE(DSPFW3, reg);
1819
1820 DRM_INFO("Big FIFO is enabled\n");
1821
1822 return;
1823}
1824
dff33cfc 1825const static int latency_ns = 3000; /* default for non-igd platforms */
7662c8bd 1826
dff33cfc
JB
1827static int intel_get_fifo_size(struct drm_device *dev, int plane)
1828{
1829 struct drm_i915_private *dev_priv = dev->dev_private;
1830 uint32_t dsparb = I915_READ(DSPARB);
1831 int size;
1832
1833 if (IS_I9XX(dev)) {
1834 if (plane == 0)
1835 size = dsparb & 0x7f;
1836 else
1837 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) -
1838 (dsparb & 0x7f);
1839 } else if (IS_I85X(dev)) {
1840 if (plane == 0)
1841 size = dsparb & 0x1ff;
1842 else
1843 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) -
1844 (dsparb & 0x1ff);
1845 size >>= 1; /* Convert to cachelines */
1846 } else {
1847 size = dsparb & 0x7f;
1848 size >>= 1; /* Convert to cachelines */
1849 }
1850
1851 DRM_DEBUG("FIFO size - (0x%08x) %s: %d\n", dsparb, plane ? "B" : "A",
1852 size);
1853
1854 return size;
1855}
7662c8bd
SL
1856
1857static void i965_update_wm(struct drm_device *dev)
1858{
1859 struct drm_i915_private *dev_priv = dev->dev_private;
1860
1861 DRM_DEBUG("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR 8\n");
1862
1863 /* 965 has limitations... */
1864 I915_WRITE(DSPFW1, (8 << 16) | (8 << 8) | (8 << 0));
1865 I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
1866}
1867
1868static void i9xx_update_wm(struct drm_device *dev, int planea_clock,
1869 int planeb_clock, int sr_hdisplay, int pixel_size)
1870{
1871 struct drm_i915_private *dev_priv = dev->dev_private;
dff33cfc
JB
1872 uint32_t fwater_lo;
1873 uint32_t fwater_hi;
1874 int total_size, cacheline_size, cwm, srwm = 1;
1875 int planea_wm, planeb_wm;
1876 struct intel_watermark_params planea_params, planeb_params;
7662c8bd
SL
1877 unsigned long line_time_us;
1878 int sr_clock, sr_entries = 0;
1879
dff33cfc 1880 /* Create copies of the base settings for each pipe */
7662c8bd 1881 if (IS_I965GM(dev) || IS_I945GM(dev))
dff33cfc 1882 planea_params = planeb_params = i945_wm_info;
7662c8bd 1883 else if (IS_I9XX(dev))
dff33cfc 1884 planea_params = planeb_params = i915_wm_info;
7662c8bd 1885 else
dff33cfc 1886 planea_params = planeb_params = i855_wm_info;
7662c8bd 1887
dff33cfc
JB
1888 /* Grab a couple of global values before we overwrite them */
1889 total_size = planea_params.fifo_size;
1890 cacheline_size = planea_params.cacheline_size;
7662c8bd 1891
dff33cfc
JB
1892 /* Update per-plane FIFO sizes */
1893 planea_params.fifo_size = intel_get_fifo_size(dev, 0);
1894 planeb_params.fifo_size = intel_get_fifo_size(dev, 1);
7662c8bd 1895
dff33cfc
JB
1896 planea_wm = intel_calculate_wm(planea_clock, &planea_params,
1897 pixel_size, latency_ns);
1898 planeb_wm = intel_calculate_wm(planeb_clock, &planeb_params,
1899 pixel_size, latency_ns);
1900 DRM_DEBUG("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
7662c8bd
SL
1901
1902 /*
1903 * Overlay gets an aggressive default since video jitter is bad.
1904 */
1905 cwm = 2;
1906
dff33cfc 1907 /* Calc sr entries for one plane configs */
7662c8bd 1908 if (!planea_clock || !planeb_clock) {
dff33cfc
JB
1909 /* self-refresh has much higher latency */
1910 const static int sr_latency_ns = 6000;
1911
7662c8bd 1912 sr_clock = planea_clock ? planea_clock : planeb_clock;
dff33cfc
JB
1913 line_time_us = ((sr_hdisplay * 1000) / sr_clock);
1914
1915 /* Use ns/us then divide to preserve precision */
1916 sr_entries = (((sr_latency_ns / line_time_us) + 1) *
1917 pixel_size * sr_hdisplay) / 1000;
1918 sr_entries = roundup(sr_entries / cacheline_size, 1);
1919 DRM_DEBUG("self-refresh entries: %d\n", sr_entries);
1920 srwm = total_size - sr_entries;
1921 if (srwm < 0)
1922 srwm = 1;
7662c8bd
SL
1923 }
1924
1925 DRM_DEBUG("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
dff33cfc 1926 planea_wm, planeb_wm, cwm, srwm);
7662c8bd 1927
dff33cfc
JB
1928 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1929 fwater_hi = (cwm & 0x1f);
1930
1931 /* Set request length to 8 cachelines per fetch */
1932 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1933 fwater_hi = fwater_hi | (1 << 8);
7662c8bd
SL
1934
1935 I915_WRITE(FW_BLC, fwater_lo);
1936 I915_WRITE(FW_BLC2, fwater_hi);
1937 if (IS_I9XX(dev))
dff33cfc 1938 I915_WRITE(FW_BLC_SELF, (srwm & 0x3f));
7662c8bd
SL
1939}
1940
1941static void i830_update_wm(struct drm_device *dev, int planea_clock,
1942 int pixel_size)
1943{
1944 struct drm_i915_private *dev_priv = dev->dev_private;
7662c8bd 1945 uint32_t fwater_lo = I915_READ(FW_BLC) & MM_FIFO_WATERMARK;
dff33cfc 1946 int planea_wm;
7662c8bd 1947
dff33cfc 1948 i830_wm_info.fifo_size = intel_get_fifo_size(dev, 0);
7662c8bd 1949
dff33cfc
JB
1950 planea_wm = intel_calculate_wm(planea_clock, &i830_wm_info,
1951 pixel_size, latency_ns);
1952 fwater_lo = fwater_lo | planea_wm;
7662c8bd
SL
1953
1954 I915_WRITE(FW_BLC, fwater_lo);
1955}
1956
1957/**
1958 * intel_update_watermarks - update FIFO watermark values based on current modes
1959 *
1960 * Calculate watermark values for the various WM regs based on current mode
1961 * and plane configuration.
1962 *
1963 * There are several cases to deal with here:
1964 * - normal (i.e. non-self-refresh)
1965 * - self-refresh (SR) mode
1966 * - lines are large relative to FIFO size (buffer can hold up to 2)
1967 * - lines are small relative to FIFO size (buffer can hold more than 2
1968 * lines), so need to account for TLB latency
1969 *
1970 * The normal calculation is:
1971 * watermark = dotclock * bytes per pixel * latency
1972 * where latency is platform & configuration dependent (we assume pessimal
1973 * values here).
1974 *
1975 * The SR calculation is:
1976 * watermark = (trunc(latency/line time)+1) * surface width *
1977 * bytes per pixel
1978 * where
1979 * line time = htotal / dotclock
1980 * and latency is assumed to be high, as above.
1981 *
1982 * The final value programmed to the register should always be rounded up,
1983 * and include an extra 2 entries to account for clock crossings.
1984 *
1985 * We don't use the sprite, so we can ignore that. And on Crestline we have
1986 * to set the non-SR watermarks to 8.
1987 */
1988static void intel_update_watermarks(struct drm_device *dev)
1989{
1990 struct drm_crtc *crtc;
1991 struct intel_crtc *intel_crtc;
1992 int sr_hdisplay = 0;
1993 unsigned long planea_clock = 0, planeb_clock = 0, sr_clock = 0;
1994 int enabled = 0, pixel_size = 0;
1995
1996 if (DSPARB_HWCONTROL(dev))
1997 return;
1998
1999 /* Get the clock config from both planes */
2000 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2001 intel_crtc = to_intel_crtc(crtc);
2002 if (crtc->enabled) {
2003 enabled++;
2004 if (intel_crtc->plane == 0) {
2005 DRM_DEBUG("plane A (pipe %d) clock: %d\n",
2006 intel_crtc->pipe, crtc->mode.clock);
2007 planea_clock = crtc->mode.clock;
2008 } else {
2009 DRM_DEBUG("plane B (pipe %d) clock: %d\n",
2010 intel_crtc->pipe, crtc->mode.clock);
2011 planeb_clock = crtc->mode.clock;
2012 }
2013 sr_hdisplay = crtc->mode.hdisplay;
2014 sr_clock = crtc->mode.clock;
2015 if (crtc->fb)
2016 pixel_size = crtc->fb->bits_per_pixel / 8;
2017 else
2018 pixel_size = 4; /* by default */
2019 }
2020 }
2021
2022 if (enabled <= 0)
2023 return;
2024
dff33cfc 2025 /* Single plane configs can enable self refresh */
7662c8bd
SL
2026 if (enabled == 1 && IS_IGD(dev))
2027 igd_enable_cxsr(dev, sr_clock, pixel_size);
2028 else if (IS_IGD(dev))
2029 igd_disable_cxsr(dev);
2030
2031 if (IS_I965G(dev))
2032 i965_update_wm(dev);
2033 else if (IS_I9XX(dev) || IS_MOBILE(dev))
2034 i9xx_update_wm(dev, planea_clock, planeb_clock, sr_hdisplay,
2035 pixel_size);
2036 else
2037 i830_update_wm(dev, planea_clock, pixel_size);
2038}
2039
5c3b82e2
CW
2040static int intel_crtc_mode_set(struct drm_crtc *crtc,
2041 struct drm_display_mode *mode,
2042 struct drm_display_mode *adjusted_mode,
2043 int x, int y,
2044 struct drm_framebuffer *old_fb)
79e53945
JB
2045{
2046 struct drm_device *dev = crtc->dev;
2047 struct drm_i915_private *dev_priv = dev->dev_private;
2048 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2049 int pipe = intel_crtc->pipe;
2050 int fp_reg = (pipe == 0) ? FPA0 : FPB0;
2051 int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
2052 int dpll_md_reg = (intel_crtc->pipe == 0) ? DPLL_A_MD : DPLL_B_MD;
2053 int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
2054 int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
2055 int htot_reg = (pipe == 0) ? HTOTAL_A : HTOTAL_B;
2056 int hblank_reg = (pipe == 0) ? HBLANK_A : HBLANK_B;
2057 int hsync_reg = (pipe == 0) ? HSYNC_A : HSYNC_B;
2058 int vtot_reg = (pipe == 0) ? VTOTAL_A : VTOTAL_B;
2059 int vblank_reg = (pipe == 0) ? VBLANK_A : VBLANK_B;
2060 int vsync_reg = (pipe == 0) ? VSYNC_A : VSYNC_B;
2061 int dspsize_reg = (pipe == 0) ? DSPASIZE : DSPBSIZE;
2062 int dsppos_reg = (pipe == 0) ? DSPAPOS : DSPBPOS;
2063 int pipesrc_reg = (pipe == 0) ? PIPEASRC : PIPEBSRC;
43565a06 2064 int refclk, num_outputs = 0;
79e53945
JB
2065 intel_clock_t clock;
2066 u32 dpll = 0, fp = 0, dspcntr, pipeconf;
2067 bool ok, is_sdvo = false, is_dvo = false;
a4fc5ed6 2068 bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
79e53945
JB
2069 struct drm_mode_config *mode_config = &dev->mode_config;
2070 struct drm_connector *connector;
d4906093 2071 const intel_limit_t *limit;
5c3b82e2 2072 int ret;
2c07245f
ZW
2073 struct fdi_m_n m_n = {0};
2074 int data_m1_reg = (pipe == 0) ? PIPEA_DATA_M1 : PIPEB_DATA_M1;
2075 int data_n1_reg = (pipe == 0) ? PIPEA_DATA_N1 : PIPEB_DATA_N1;
2076 int link_m1_reg = (pipe == 0) ? PIPEA_LINK_M1 : PIPEB_LINK_M1;
2077 int link_n1_reg = (pipe == 0) ? PIPEA_LINK_N1 : PIPEB_LINK_N1;
2078 int pch_fp_reg = (pipe == 0) ? PCH_FPA0 : PCH_FPB0;
2079 int pch_dpll_reg = (pipe == 0) ? PCH_DPLL_A : PCH_DPLL_B;
2080 int fdi_rx_reg = (pipe == 0) ? FDI_RXA_CTL : FDI_RXB_CTL;
541998a1 2081 int lvds_reg = LVDS;
2c07245f
ZW
2082 u32 temp;
2083 int sdvo_pixel_multiply;
79e53945
JB
2084
2085 drm_vblank_pre_modeset(dev, pipe);
2086
2087 list_for_each_entry(connector, &mode_config->connector_list, head) {
2088 struct intel_output *intel_output = to_intel_output(connector);
2089
2090 if (!connector->encoder || connector->encoder->crtc != crtc)
2091 continue;
2092
2093 switch (intel_output->type) {
2094 case INTEL_OUTPUT_LVDS:
2095 is_lvds = true;
2096 break;
2097 case INTEL_OUTPUT_SDVO:
7d57382e 2098 case INTEL_OUTPUT_HDMI:
79e53945 2099 is_sdvo = true;
e2f0ba97
JB
2100 if (intel_output->needs_tv_clock)
2101 is_tv = true;
79e53945
JB
2102 break;
2103 case INTEL_OUTPUT_DVO:
2104 is_dvo = true;
2105 break;
2106 case INTEL_OUTPUT_TVOUT:
2107 is_tv = true;
2108 break;
2109 case INTEL_OUTPUT_ANALOG:
2110 is_crt = true;
2111 break;
a4fc5ed6
KP
2112 case INTEL_OUTPUT_DISPLAYPORT:
2113 is_dp = true;
2114 break;
79e53945 2115 }
43565a06
KH
2116
2117 num_outputs++;
79e53945
JB
2118 }
2119
43565a06
KH
2120 if (is_lvds && dev_priv->lvds_use_ssc && num_outputs < 2) {
2121 refclk = dev_priv->lvds_ssc_freq * 1000;
2122 DRM_DEBUG("using SSC reference clock of %d MHz\n", refclk / 1000);
2123 } else if (IS_I9XX(dev)) {
79e53945 2124 refclk = 96000;
2c07245f
ZW
2125 if (IS_IGDNG(dev))
2126 refclk = 120000; /* 120Mhz refclk */
79e53945
JB
2127 } else {
2128 refclk = 48000;
2129 }
a4fc5ed6 2130
79e53945 2131
d4906093
ML
2132 /*
2133 * Returns a set of divisors for the desired target clock with the given
2134 * refclk, or FALSE. The returned values represent the clock equation:
2135 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
2136 */
2137 limit = intel_limit(crtc);
2138 ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, &clock);
79e53945
JB
2139 if (!ok) {
2140 DRM_ERROR("Couldn't find PLL settings for mode!\n");
1f803ee5 2141 drm_vblank_post_modeset(dev, pipe);
5c3b82e2 2142 return -EINVAL;
79e53945
JB
2143 }
2144
7026d4ac
ZW
2145 /* SDVO TV has fixed PLL values depend on its clock range,
2146 this mirrors vbios setting. */
2147 if (is_sdvo && is_tv) {
2148 if (adjusted_mode->clock >= 100000
2149 && adjusted_mode->clock < 140500) {
2150 clock.p1 = 2;
2151 clock.p2 = 10;
2152 clock.n = 3;
2153 clock.m1 = 16;
2154 clock.m2 = 8;
2155 } else if (adjusted_mode->clock >= 140500
2156 && adjusted_mode->clock <= 200000) {
2157 clock.p1 = 1;
2158 clock.p2 = 10;
2159 clock.n = 6;
2160 clock.m1 = 12;
2161 clock.m2 = 8;
2162 }
2163 }
2164
2c07245f
ZW
2165 /* FDI link */
2166 if (IS_IGDNG(dev))
2167 igdng_compute_m_n(3, 4, /* lane num 4 */
2168 adjusted_mode->clock,
2169 270000, /* lane clock */
2170 &m_n);
2171
2177832f
SL
2172 if (IS_IGD(dev))
2173 fp = (1 << clock.n) << 16 | clock.m1 << 8 | clock.m2;
2174 else
2175 fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
79e53945 2176
2c07245f
ZW
2177 if (!IS_IGDNG(dev))
2178 dpll = DPLL_VGA_MODE_DIS;
2179
79e53945
JB
2180 if (IS_I9XX(dev)) {
2181 if (is_lvds)
2182 dpll |= DPLLB_MODE_LVDS;
2183 else
2184 dpll |= DPLLB_MODE_DAC_SERIAL;
2185 if (is_sdvo) {
2186 dpll |= DPLL_DVO_HIGH_SPEED;
2c07245f
ZW
2187 sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
2188 if (IS_I945G(dev) || IS_I945GM(dev))
79e53945 2189 dpll |= (sdvo_pixel_multiply - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
2c07245f
ZW
2190 else if (IS_IGDNG(dev))
2191 dpll |= (sdvo_pixel_multiply - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
79e53945 2192 }
a4fc5ed6
KP
2193 if (is_dp)
2194 dpll |= DPLL_DVO_HIGH_SPEED;
79e53945
JB
2195
2196 /* compute bitmask from p1 value */
2177832f
SL
2197 if (IS_IGD(dev))
2198 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_IGD;
2c07245f 2199 else {
2177832f 2200 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
2c07245f
ZW
2201 /* also FPA1 */
2202 if (IS_IGDNG(dev))
2203 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
2204 }
79e53945
JB
2205 switch (clock.p2) {
2206 case 5:
2207 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
2208 break;
2209 case 7:
2210 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
2211 break;
2212 case 10:
2213 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
2214 break;
2215 case 14:
2216 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
2217 break;
2218 }
2c07245f 2219 if (IS_I965G(dev) && !IS_IGDNG(dev))
79e53945
JB
2220 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
2221 } else {
2222 if (is_lvds) {
2223 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
2224 } else {
2225 if (clock.p1 == 2)
2226 dpll |= PLL_P1_DIVIDE_BY_TWO;
2227 else
2228 dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
2229 if (clock.p2 == 4)
2230 dpll |= PLL_P2_DIVIDE_BY_4;
2231 }
2232 }
2233
43565a06
KH
2234 if (is_sdvo && is_tv)
2235 dpll |= PLL_REF_INPUT_TVCLKINBC;
2236 else if (is_tv)
79e53945 2237 /* XXX: just matching BIOS for now */
43565a06 2238 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
79e53945 2239 dpll |= 3;
43565a06
KH
2240 else if (is_lvds && dev_priv->lvds_use_ssc && num_outputs < 2)
2241 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
2242 else
2243 dpll |= PLL_REF_INPUT_DREFCLK;
2244
2245 /* setup pipeconf */
2246 pipeconf = I915_READ(pipeconf_reg);
2247
2248 /* Set up the display plane register */
2249 dspcntr = DISPPLANE_GAMMA_ENABLE;
2250
2c07245f
ZW
2251 /* IGDNG's plane is forced to pipe, bit 24 is to
2252 enable color space conversion */
2253 if (!IS_IGDNG(dev)) {
2254 if (pipe == 0)
2255 dspcntr |= DISPPLANE_SEL_PIPE_A;
2256 else
2257 dspcntr |= DISPPLANE_SEL_PIPE_B;
2258 }
79e53945
JB
2259
2260 if (pipe == 0 && !IS_I965G(dev)) {
2261 /* Enable pixel doubling when the dot clock is > 90% of the (display)
2262 * core speed.
2263 *
2264 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
2265 * pipe == 0 check?
2266 */
2267 if (mode->clock > intel_get_core_clock_speed(dev) * 9 / 10)
2268 pipeconf |= PIPEACONF_DOUBLE_WIDE;
2269 else
2270 pipeconf &= ~PIPEACONF_DOUBLE_WIDE;
2271 }
2272
2273 dspcntr |= DISPLAY_PLANE_ENABLE;
2274 pipeconf |= PIPEACONF_ENABLE;
2275 dpll |= DPLL_VCO_ENABLE;
2276
2277
2278 /* Disable the panel fitter if it was on our pipe */
2c07245f 2279 if (!IS_IGDNG(dev) && intel_panel_fitter_pipe(dev) == pipe)
79e53945
JB
2280 I915_WRITE(PFIT_CONTROL, 0);
2281
2282 DRM_DEBUG("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
2283 drm_mode_debug_printmodeline(mode);
2284
2c07245f
ZW
2285 /* assign to IGDNG registers */
2286 if (IS_IGDNG(dev)) {
2287 fp_reg = pch_fp_reg;
2288 dpll_reg = pch_dpll_reg;
2289 }
79e53945
JB
2290
2291 if (dpll & DPLL_VCO_ENABLE) {
2292 I915_WRITE(fp_reg, fp);
2293 I915_WRITE(dpll_reg, dpll & ~DPLL_VCO_ENABLE);
2294 I915_READ(dpll_reg);
2295 udelay(150);
2296 }
2297
2c07245f
ZW
2298 if (IS_IGDNG(dev)) {
2299 /* enable PCH clock reference source */
2300 /* XXX need to change the setting for other outputs */
2301 u32 temp;
2302 temp = I915_READ(PCH_DREF_CONTROL);
2303 temp &= ~DREF_NONSPREAD_SOURCE_MASK;
2304 temp |= DREF_NONSPREAD_CK505_ENABLE;
2305 temp &= ~DREF_SSC_SOURCE_MASK;
2306 temp |= DREF_SSC_SOURCE_ENABLE;
2307 temp &= ~DREF_SSC1_ENABLE;
2308 /* if no eDP, disable source output to CPU */
2309 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
2310 temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
2311 I915_WRITE(PCH_DREF_CONTROL, temp);
2312 }
2313
79e53945
JB
2314 /* The LVDS pin pair needs to be on before the DPLLs are enabled.
2315 * This is an exception to the general rule that mode_set doesn't turn
2316 * things on.
2317 */
2318 if (is_lvds) {
541998a1 2319 u32 lvds;
79e53945 2320
541998a1
ZW
2321 if (IS_IGDNG(dev))
2322 lvds_reg = PCH_LVDS;
2323
2324 lvds = I915_READ(lvds_reg);
79e53945
JB
2325 lvds |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP | LVDS_PIPEB_SELECT;
2326 /* Set the B0-B3 data pairs corresponding to whether we're going to
2327 * set the DPLLs for dual-channel mode or not.
2328 */
2329 if (clock.p2 == 7)
2330 lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
2331 else
2332 lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
2333
2334 /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
2335 * appropriately here, but we need to look more thoroughly into how
2336 * panels behave in the two modes.
2337 */
2338
541998a1
ZW
2339 I915_WRITE(lvds_reg, lvds);
2340 I915_READ(lvds_reg);
79e53945 2341 }
a4fc5ed6
KP
2342 if (is_dp)
2343 intel_dp_set_m_n(crtc, mode, adjusted_mode);
79e53945
JB
2344
2345 I915_WRITE(fp_reg, fp);
2346 I915_WRITE(dpll_reg, dpll);
2347 I915_READ(dpll_reg);
2348 /* Wait for the clocks to stabilize. */
2349 udelay(150);
2350
2c07245f
ZW
2351 if (IS_I965G(dev) && !IS_IGDNG(dev)) {
2352 sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
79e53945
JB
2353 I915_WRITE(dpll_md_reg, (0 << DPLL_MD_UDI_DIVIDER_SHIFT) |
2354 ((sdvo_pixel_multiply - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT));
2355 } else {
2356 /* write it again -- the BIOS does, after all */
2357 I915_WRITE(dpll_reg, dpll);
2358 }
2359 I915_READ(dpll_reg);
2360 /* Wait for the clocks to stabilize. */
2361 udelay(150);
2362
2363 I915_WRITE(htot_reg, (adjusted_mode->crtc_hdisplay - 1) |
2364 ((adjusted_mode->crtc_htotal - 1) << 16));
2365 I915_WRITE(hblank_reg, (adjusted_mode->crtc_hblank_start - 1) |
2366 ((adjusted_mode->crtc_hblank_end - 1) << 16));
2367 I915_WRITE(hsync_reg, (adjusted_mode->crtc_hsync_start - 1) |
2368 ((adjusted_mode->crtc_hsync_end - 1) << 16));
2369 I915_WRITE(vtot_reg, (adjusted_mode->crtc_vdisplay - 1) |
2370 ((adjusted_mode->crtc_vtotal - 1) << 16));
2371 I915_WRITE(vblank_reg, (adjusted_mode->crtc_vblank_start - 1) |
2372 ((adjusted_mode->crtc_vblank_end - 1) << 16));
2373 I915_WRITE(vsync_reg, (adjusted_mode->crtc_vsync_start - 1) |
2374 ((adjusted_mode->crtc_vsync_end - 1) << 16));
2375 /* pipesrc and dspsize control the size that is scaled from, which should
2376 * always be the user's requested size.
2377 */
2c07245f
ZW
2378 if (!IS_IGDNG(dev)) {
2379 I915_WRITE(dspsize_reg, ((mode->vdisplay - 1) << 16) |
2380 (mode->hdisplay - 1));
2381 I915_WRITE(dsppos_reg, 0);
2382 }
79e53945 2383 I915_WRITE(pipesrc_reg, ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
2c07245f
ZW
2384
2385 if (IS_IGDNG(dev)) {
2386 I915_WRITE(data_m1_reg, TU_SIZE(m_n.tu) | m_n.gmch_m);
2387 I915_WRITE(data_n1_reg, TU_SIZE(m_n.tu) | m_n.gmch_n);
2388 I915_WRITE(link_m1_reg, m_n.link_m);
2389 I915_WRITE(link_n1_reg, m_n.link_n);
2390
2391 /* enable FDI RX PLL too */
2392 temp = I915_READ(fdi_rx_reg);
2393 I915_WRITE(fdi_rx_reg, temp | FDI_RX_PLL_ENABLE);
2394 udelay(200);
2395 }
2396
79e53945
JB
2397 I915_WRITE(pipeconf_reg, pipeconf);
2398 I915_READ(pipeconf_reg);
2399
2400 intel_wait_for_vblank(dev);
2401
2402 I915_WRITE(dspcntr_reg, dspcntr);
2403
2404 /* Flush the plane changes */
5c3b82e2 2405 ret = intel_pipe_set_base(crtc, x, y, old_fb);
7662c8bd
SL
2406
2407 intel_update_watermarks(dev);
2408
79e53945 2409 drm_vblank_post_modeset(dev, pipe);
5c3b82e2 2410
1f803ee5 2411 return ret;
79e53945
JB
2412}
2413
2414/** Loads the palette/gamma unit for the CRTC with the prepared values */
2415void intel_crtc_load_lut(struct drm_crtc *crtc)
2416{
2417 struct drm_device *dev = crtc->dev;
2418 struct drm_i915_private *dev_priv = dev->dev_private;
2419 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2420 int palreg = (intel_crtc->pipe == 0) ? PALETTE_A : PALETTE_B;
2421 int i;
2422
2423 /* The clocks have to be on to load the palette. */
2424 if (!crtc->enabled)
2425 return;
2426
2c07245f
ZW
2427 /* use legacy palette for IGDNG */
2428 if (IS_IGDNG(dev))
2429 palreg = (intel_crtc->pipe == 0) ? LGC_PALETTE_A :
2430 LGC_PALETTE_B;
2431
79e53945
JB
2432 for (i = 0; i < 256; i++) {
2433 I915_WRITE(palreg + 4 * i,
2434 (intel_crtc->lut_r[i] << 16) |
2435 (intel_crtc->lut_g[i] << 8) |
2436 intel_crtc->lut_b[i]);
2437 }
2438}
2439
2440static int intel_crtc_cursor_set(struct drm_crtc *crtc,
2441 struct drm_file *file_priv,
2442 uint32_t handle,
2443 uint32_t width, uint32_t height)
2444{
2445 struct drm_device *dev = crtc->dev;
2446 struct drm_i915_private *dev_priv = dev->dev_private;
2447 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2448 struct drm_gem_object *bo;
2449 struct drm_i915_gem_object *obj_priv;
2450 int pipe = intel_crtc->pipe;
2451 uint32_t control = (pipe == 0) ? CURACNTR : CURBCNTR;
2452 uint32_t base = (pipe == 0) ? CURABASE : CURBBASE;
14b60391 2453 uint32_t temp = I915_READ(control);
79e53945 2454 size_t addr;
3f8bc370 2455 int ret;
79e53945
JB
2456
2457 DRM_DEBUG("\n");
2458
2459 /* if we want to turn off the cursor ignore width and height */
2460 if (!handle) {
2461 DRM_DEBUG("cursor off\n");
14b60391
JB
2462 if (IS_MOBILE(dev) || IS_I9XX(dev)) {
2463 temp &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
2464 temp |= CURSOR_MODE_DISABLE;
2465 } else {
2466 temp &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
2467 }
3f8bc370
KH
2468 addr = 0;
2469 bo = NULL;
5004417d 2470 mutex_lock(&dev->struct_mutex);
3f8bc370 2471 goto finish;
79e53945
JB
2472 }
2473
2474 /* Currently we only support 64x64 cursors */
2475 if (width != 64 || height != 64) {
2476 DRM_ERROR("we currently only support 64x64 cursors\n");
2477 return -EINVAL;
2478 }
2479
2480 bo = drm_gem_object_lookup(dev, file_priv, handle);
2481 if (!bo)
2482 return -ENOENT;
2483
2484 obj_priv = bo->driver_private;
2485
2486 if (bo->size < width * height * 4) {
2487 DRM_ERROR("buffer is to small\n");
34b8686e
DA
2488 ret = -ENOMEM;
2489 goto fail;
79e53945
JB
2490 }
2491
71acb5eb 2492 /* we only need to pin inside GTT if cursor is non-phy */
7f9872e0 2493 mutex_lock(&dev->struct_mutex);
71acb5eb
DA
2494 if (!dev_priv->cursor_needs_physical) {
2495 ret = i915_gem_object_pin(bo, PAGE_SIZE);
2496 if (ret) {
2497 DRM_ERROR("failed to pin cursor bo\n");
7f9872e0 2498 goto fail_locked;
71acb5eb 2499 }
79e53945 2500 addr = obj_priv->gtt_offset;
71acb5eb
DA
2501 } else {
2502 ret = i915_gem_attach_phys_object(dev, bo, (pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1);
2503 if (ret) {
2504 DRM_ERROR("failed to attach phys object\n");
7f9872e0 2505 goto fail_locked;
71acb5eb
DA
2506 }
2507 addr = obj_priv->phys_obj->handle->busaddr;
3f8bc370
KH
2508 }
2509
14b60391
JB
2510 if (!IS_I9XX(dev))
2511 I915_WRITE(CURSIZE, (height << 12) | width);
2512
2513 /* Hooray for CUR*CNTR differences */
2514 if (IS_MOBILE(dev) || IS_I9XX(dev)) {
2515 temp &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
2516 temp |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
2517 temp |= (pipe << 28); /* Connect to correct pipe */
2518 } else {
2519 temp &= ~(CURSOR_FORMAT_MASK);
2520 temp |= CURSOR_ENABLE;
2521 temp |= CURSOR_FORMAT_ARGB | CURSOR_GAMMA_ENABLE;
2522 }
79e53945 2523
3f8bc370 2524 finish:
79e53945
JB
2525 I915_WRITE(control, temp);
2526 I915_WRITE(base, addr);
2527
3f8bc370 2528 if (intel_crtc->cursor_bo) {
71acb5eb
DA
2529 if (dev_priv->cursor_needs_physical) {
2530 if (intel_crtc->cursor_bo != bo)
2531 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
2532 } else
2533 i915_gem_object_unpin(intel_crtc->cursor_bo);
3f8bc370
KH
2534 drm_gem_object_unreference(intel_crtc->cursor_bo);
2535 }
7f9872e0 2536 mutex_unlock(&dev->struct_mutex);
3f8bc370
KH
2537
2538 intel_crtc->cursor_addr = addr;
2539 intel_crtc->cursor_bo = bo;
2540
79e53945 2541 return 0;
34b8686e
DA
2542fail:
2543 mutex_lock(&dev->struct_mutex);
7f9872e0 2544fail_locked:
34b8686e
DA
2545 drm_gem_object_unreference(bo);
2546 mutex_unlock(&dev->struct_mutex);
2547 return ret;
79e53945
JB
2548}
2549
2550static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
2551{
2552 struct drm_device *dev = crtc->dev;
2553 struct drm_i915_private *dev_priv = dev->dev_private;
2554 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2555 int pipe = intel_crtc->pipe;
2556 uint32_t temp = 0;
2557 uint32_t adder;
2558
2559 if (x < 0) {
2245fda8 2560 temp |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
79e53945
JB
2561 x = -x;
2562 }
2563 if (y < 0) {
2245fda8 2564 temp |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
79e53945
JB
2565 y = -y;
2566 }
2567
2245fda8
KP
2568 temp |= x << CURSOR_X_SHIFT;
2569 temp |= y << CURSOR_Y_SHIFT;
79e53945
JB
2570
2571 adder = intel_crtc->cursor_addr;
2572 I915_WRITE((pipe == 0) ? CURAPOS : CURBPOS, temp);
2573 I915_WRITE((pipe == 0) ? CURABASE : CURBBASE, adder);
2574
2575 return 0;
2576}
2577
2578/** Sets the color ramps on behalf of RandR */
2579void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
2580 u16 blue, int regno)
2581{
2582 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2583
2584 intel_crtc->lut_r[regno] = red >> 8;
2585 intel_crtc->lut_g[regno] = green >> 8;
2586 intel_crtc->lut_b[regno] = blue >> 8;
2587}
2588
2589static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
2590 u16 *blue, uint32_t size)
2591{
2592 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2593 int i;
2594
2595 if (size != 256)
2596 return;
2597
2598 for (i = 0; i < 256; i++) {
2599 intel_crtc->lut_r[i] = red[i] >> 8;
2600 intel_crtc->lut_g[i] = green[i] >> 8;
2601 intel_crtc->lut_b[i] = blue[i] >> 8;
2602 }
2603
2604 intel_crtc_load_lut(crtc);
2605}
2606
2607/**
2608 * Get a pipe with a simple mode set on it for doing load-based monitor
2609 * detection.
2610 *
2611 * It will be up to the load-detect code to adjust the pipe as appropriate for
2612 * its requirements. The pipe will be connected to no other outputs.
2613 *
2614 * Currently this code will only succeed if there is a pipe with no outputs
2615 * configured for it. In the future, it could choose to temporarily disable
2616 * some outputs to free up a pipe for its use.
2617 *
2618 * \return crtc, or NULL if no pipes are available.
2619 */
2620
2621/* VESA 640x480x72Hz mode to set on the pipe */
2622static struct drm_display_mode load_detect_mode = {
2623 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
2624 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
2625};
2626
2627struct drm_crtc *intel_get_load_detect_pipe(struct intel_output *intel_output,
2628 struct drm_display_mode *mode,
2629 int *dpms_mode)
2630{
2631 struct intel_crtc *intel_crtc;
2632 struct drm_crtc *possible_crtc;
2633 struct drm_crtc *supported_crtc =NULL;
2634 struct drm_encoder *encoder = &intel_output->enc;
2635 struct drm_crtc *crtc = NULL;
2636 struct drm_device *dev = encoder->dev;
2637 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
2638 struct drm_crtc_helper_funcs *crtc_funcs;
2639 int i = -1;
2640
2641 /*
2642 * Algorithm gets a little messy:
2643 * - if the connector already has an assigned crtc, use it (but make
2644 * sure it's on first)
2645 * - try to find the first unused crtc that can drive this connector,
2646 * and use that if we find one
2647 * - if there are no unused crtcs available, try to use the first
2648 * one we found that supports the connector
2649 */
2650
2651 /* See if we already have a CRTC for this connector */
2652 if (encoder->crtc) {
2653 crtc = encoder->crtc;
2654 /* Make sure the crtc and connector are running */
2655 intel_crtc = to_intel_crtc(crtc);
2656 *dpms_mode = intel_crtc->dpms_mode;
2657 if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
2658 crtc_funcs = crtc->helper_private;
2659 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
2660 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
2661 }
2662 return crtc;
2663 }
2664
2665 /* Find an unused one (if possible) */
2666 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
2667 i++;
2668 if (!(encoder->possible_crtcs & (1 << i)))
2669 continue;
2670 if (!possible_crtc->enabled) {
2671 crtc = possible_crtc;
2672 break;
2673 }
2674 if (!supported_crtc)
2675 supported_crtc = possible_crtc;
2676 }
2677
2678 /*
2679 * If we didn't find an unused CRTC, don't use any.
2680 */
2681 if (!crtc) {
2682 return NULL;
2683 }
2684
2685 encoder->crtc = crtc;
03d60699 2686 intel_output->base.encoder = encoder;
79e53945
JB
2687 intel_output->load_detect_temp = true;
2688
2689 intel_crtc = to_intel_crtc(crtc);
2690 *dpms_mode = intel_crtc->dpms_mode;
2691
2692 if (!crtc->enabled) {
2693 if (!mode)
2694 mode = &load_detect_mode;
3c4fdcfb 2695 drm_crtc_helper_set_mode(crtc, mode, 0, 0, crtc->fb);
79e53945
JB
2696 } else {
2697 if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
2698 crtc_funcs = crtc->helper_private;
2699 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
2700 }
2701
2702 /* Add this connector to the crtc */
2703 encoder_funcs->mode_set(encoder, &crtc->mode, &crtc->mode);
2704 encoder_funcs->commit(encoder);
2705 }
2706 /* let the connector get through one full cycle before testing */
2707 intel_wait_for_vblank(dev);
2708
2709 return crtc;
2710}
2711
2712void intel_release_load_detect_pipe(struct intel_output *intel_output, int dpms_mode)
2713{
2714 struct drm_encoder *encoder = &intel_output->enc;
2715 struct drm_device *dev = encoder->dev;
2716 struct drm_crtc *crtc = encoder->crtc;
2717 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
2718 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
2719
2720 if (intel_output->load_detect_temp) {
2721 encoder->crtc = NULL;
03d60699 2722 intel_output->base.encoder = NULL;
79e53945
JB
2723 intel_output->load_detect_temp = false;
2724 crtc->enabled = drm_helper_crtc_in_use(crtc);
2725 drm_helper_disable_unused_functions(dev);
2726 }
2727
2728 /* Switch crtc and output back off if necessary */
2729 if (crtc->enabled && dpms_mode != DRM_MODE_DPMS_ON) {
2730 if (encoder->crtc == crtc)
2731 encoder_funcs->dpms(encoder, dpms_mode);
2732 crtc_funcs->dpms(crtc, dpms_mode);
2733 }
2734}
2735
2736/* Returns the clock of the currently programmed mode of the given pipe. */
2737static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
2738{
2739 struct drm_i915_private *dev_priv = dev->dev_private;
2740 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2741 int pipe = intel_crtc->pipe;
2742 u32 dpll = I915_READ((pipe == 0) ? DPLL_A : DPLL_B);
2743 u32 fp;
2744 intel_clock_t clock;
2745
2746 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
2747 fp = I915_READ((pipe == 0) ? FPA0 : FPB0);
2748 else
2749 fp = I915_READ((pipe == 0) ? FPA1 : FPB1);
2750
2751 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
2177832f
SL
2752 if (IS_IGD(dev)) {
2753 clock.n = ffs((fp & FP_N_IGD_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
2754 clock.m2 = (fp & FP_M2_IGD_DIV_MASK) >> FP_M2_DIV_SHIFT;
2755 } else {
2756 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
2757 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
2758 }
2759
79e53945 2760 if (IS_I9XX(dev)) {
2177832f
SL
2761 if (IS_IGD(dev))
2762 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_IGD) >>
2763 DPLL_FPA01_P1_POST_DIV_SHIFT_IGD);
2764 else
2765 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
2766 DPLL_FPA01_P1_POST_DIV_SHIFT);
2767
2768 switch (dpll & DPLL_MODE_MASK) {
2769 case DPLLB_MODE_DAC_SERIAL:
2770 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
2771 5 : 10;
2772 break;
2773 case DPLLB_MODE_LVDS:
2774 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
2775 7 : 14;
2776 break;
2777 default:
2778 DRM_DEBUG("Unknown DPLL mode %08x in programmed "
2779 "mode\n", (int)(dpll & DPLL_MODE_MASK));
2780 return 0;
2781 }
2782
2783 /* XXX: Handle the 100Mhz refclk */
2177832f 2784 intel_clock(dev, 96000, &clock);
79e53945
JB
2785 } else {
2786 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
2787
2788 if (is_lvds) {
2789 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
2790 DPLL_FPA01_P1_POST_DIV_SHIFT);
2791 clock.p2 = 14;
2792
2793 if ((dpll & PLL_REF_INPUT_MASK) ==
2794 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
2795 /* XXX: might not be 66MHz */
2177832f 2796 intel_clock(dev, 66000, &clock);
79e53945 2797 } else
2177832f 2798 intel_clock(dev, 48000, &clock);
79e53945
JB
2799 } else {
2800 if (dpll & PLL_P1_DIVIDE_BY_TWO)
2801 clock.p1 = 2;
2802 else {
2803 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
2804 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
2805 }
2806 if (dpll & PLL_P2_DIVIDE_BY_4)
2807 clock.p2 = 4;
2808 else
2809 clock.p2 = 2;
2810
2177832f 2811 intel_clock(dev, 48000, &clock);
79e53945
JB
2812 }
2813 }
2814
2815 /* XXX: It would be nice to validate the clocks, but we can't reuse
2816 * i830PllIsValid() because it relies on the xf86_config connector
2817 * configuration being accurate, which it isn't necessarily.
2818 */
2819
2820 return clock.dot;
2821}
2822
2823/** Returns the currently programmed mode of the given pipe. */
2824struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
2825 struct drm_crtc *crtc)
2826{
2827 struct drm_i915_private *dev_priv = dev->dev_private;
2828 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2829 int pipe = intel_crtc->pipe;
2830 struct drm_display_mode *mode;
2831 int htot = I915_READ((pipe == 0) ? HTOTAL_A : HTOTAL_B);
2832 int hsync = I915_READ((pipe == 0) ? HSYNC_A : HSYNC_B);
2833 int vtot = I915_READ((pipe == 0) ? VTOTAL_A : VTOTAL_B);
2834 int vsync = I915_READ((pipe == 0) ? VSYNC_A : VSYNC_B);
2835
2836 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
2837 if (!mode)
2838 return NULL;
2839
2840 mode->clock = intel_crtc_clock_get(dev, crtc);
2841 mode->hdisplay = (htot & 0xffff) + 1;
2842 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
2843 mode->hsync_start = (hsync & 0xffff) + 1;
2844 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
2845 mode->vdisplay = (vtot & 0xffff) + 1;
2846 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
2847 mode->vsync_start = (vsync & 0xffff) + 1;
2848 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
2849
2850 drm_mode_set_name(mode);
2851 drm_mode_set_crtcinfo(mode, 0);
2852
2853 return mode;
2854}
2855
2856static void intel_crtc_destroy(struct drm_crtc *crtc)
2857{
2858 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2859
7ff14559
DA
2860 if (intel_crtc->mode_set.mode)
2861 drm_mode_destroy(crtc->dev, intel_crtc->mode_set.mode);
79e53945
JB
2862 drm_crtc_cleanup(crtc);
2863 kfree(intel_crtc);
2864}
2865
2866static const struct drm_crtc_helper_funcs intel_helper_funcs = {
2867 .dpms = intel_crtc_dpms,
2868 .mode_fixup = intel_crtc_mode_fixup,
2869 .mode_set = intel_crtc_mode_set,
2870 .mode_set_base = intel_pipe_set_base,
2871 .prepare = intel_crtc_prepare,
2872 .commit = intel_crtc_commit,
2873};
2874
2875static const struct drm_crtc_funcs intel_crtc_funcs = {
2876 .cursor_set = intel_crtc_cursor_set,
2877 .cursor_move = intel_crtc_cursor_move,
2878 .gamma_set = intel_crtc_gamma_set,
2879 .set_config = drm_crtc_helper_set_config,
2880 .destroy = intel_crtc_destroy,
2881};
2882
2883
b358d0a6 2884static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945
JB
2885{
2886 struct intel_crtc *intel_crtc;
2887 int i;
2888
2889 intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
2890 if (intel_crtc == NULL)
2891 return;
2892
2893 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
2894
2895 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
2896 intel_crtc->pipe = pipe;
7662c8bd 2897 intel_crtc->plane = pipe;
79e53945
JB
2898 for (i = 0; i < 256; i++) {
2899 intel_crtc->lut_r[i] = i;
2900 intel_crtc->lut_g[i] = i;
2901 intel_crtc->lut_b[i] = i;
2902 }
2903
2904 intel_crtc->cursor_addr = 0;
2905 intel_crtc->dpms_mode = DRM_MODE_DPMS_OFF;
2906 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
2907
2908 intel_crtc->mode_set.crtc = &intel_crtc->base;
2909 intel_crtc->mode_set.connectors = (struct drm_connector **)(intel_crtc + 1);
2910 intel_crtc->mode_set.num_connectors = 0;
2911
2912 if (i915_fbpercrtc) {
2913
2914
2915
2916 }
2917}
2918
08d7b3d1
CW
2919int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
2920 struct drm_file *file_priv)
2921{
2922 drm_i915_private_t *dev_priv = dev->dev_private;
2923 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
2924 struct drm_crtc *crtc = NULL;
2925 int pipe = -1;
2926
2927 if (!dev_priv) {
2928 DRM_ERROR("called with no initialization\n");
2929 return -EINVAL;
2930 }
2931
2932 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2933 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2934 if (crtc->base.id == pipe_from_crtc_id->crtc_id) {
2935 pipe = intel_crtc->pipe;
2936 break;
2937 }
2938 }
2939
2940 if (pipe == -1) {
2941 DRM_ERROR("no such CRTC id\n");
2942 return -EINVAL;
2943 }
2944
2945 pipe_from_crtc_id->pipe = pipe;
2946
2947 return 0;
2948}
2949
79e53945
JB
2950struct drm_crtc *intel_get_crtc_from_pipe(struct drm_device *dev, int pipe)
2951{
2952 struct drm_crtc *crtc = NULL;
2953
2954 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2955 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2956 if (intel_crtc->pipe == pipe)
2957 break;
2958 }
2959 return crtc;
2960}
2961
b358d0a6 2962static int intel_connector_clones(struct drm_device *dev, int type_mask)
79e53945
JB
2963{
2964 int index_mask = 0;
2965 struct drm_connector *connector;
2966 int entry = 0;
2967
2968 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2969 struct intel_output *intel_output = to_intel_output(connector);
2970 if (type_mask & (1 << intel_output->type))
2971 index_mask |= (1 << entry);
2972 entry++;
2973 }
2974 return index_mask;
2975}
2976
2977
2978static void intel_setup_outputs(struct drm_device *dev)
2979{
725e30ad 2980 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945
JB
2981 struct drm_connector *connector;
2982
2983 intel_crt_init(dev);
2984
2985 /* Set up integrated LVDS */
541998a1 2986 if (IS_MOBILE(dev) && !IS_I830(dev))
79e53945
JB
2987 intel_lvds_init(dev);
2988
2c07245f 2989 if (IS_IGDNG(dev)) {
30ad48b7
ZW
2990 int found;
2991
2992 if (I915_READ(HDMIB) & PORT_DETECTED) {
2993 /* check SDVOB */
2994 /* found = intel_sdvo_init(dev, HDMIB); */
2995 found = 0;
2996 if (!found)
2997 intel_hdmi_init(dev, HDMIB);
2998 }
2999
3000 if (I915_READ(HDMIC) & PORT_DETECTED)
3001 intel_hdmi_init(dev, HDMIC);
3002
3003 if (I915_READ(HDMID) & PORT_DETECTED)
3004 intel_hdmi_init(dev, HDMID);
3005
2c07245f 3006 } else if (IS_I9XX(dev)) {
7d57382e 3007 int found;
13520b05 3008 u32 reg;
7d57382e 3009
725e30ad
EA
3010 if (I915_READ(SDVOB) & SDVO_DETECTED) {
3011 found = intel_sdvo_init(dev, SDVOB);
3012 if (!found && SUPPORTS_INTEGRATED_HDMI(dev))
3013 intel_hdmi_init(dev, SDVOB);
a4fc5ed6
KP
3014 if (!found && SUPPORTS_INTEGRATED_DP(dev))
3015 intel_dp_init(dev, DP_B);
725e30ad 3016 }
13520b05
KH
3017
3018 /* Before G4X SDVOC doesn't have its own detect register */
3019 if (IS_G4X(dev))
3020 reg = SDVOC;
3021 else
3022 reg = SDVOB;
3023
3024 if (I915_READ(reg) & SDVO_DETECTED) {
725e30ad
EA
3025 found = intel_sdvo_init(dev, SDVOC);
3026 if (!found && SUPPORTS_INTEGRATED_HDMI(dev))
3027 intel_hdmi_init(dev, SDVOC);
a4fc5ed6
KP
3028 if (!found && SUPPORTS_INTEGRATED_DP(dev))
3029 intel_dp_init(dev, DP_C);
725e30ad 3030 }
a4fc5ed6
KP
3031 if (SUPPORTS_INTEGRATED_DP(dev) && (I915_READ(DP_D) & DP_DETECTED))
3032 intel_dp_init(dev, DP_D);
79e53945
JB
3033 } else
3034 intel_dvo_init(dev);
3035
2c07245f 3036 if (IS_I9XX(dev) && IS_MOBILE(dev) && !IS_IGDNG(dev))
79e53945
JB
3037 intel_tv_init(dev);
3038
3039 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3040 struct intel_output *intel_output = to_intel_output(connector);
3041 struct drm_encoder *encoder = &intel_output->enc;
3042 int crtc_mask = 0, clone_mask = 0;
3043
3044 /* valid crtcs */
3045 switch(intel_output->type) {
7d57382e
EA
3046 case INTEL_OUTPUT_HDMI:
3047 crtc_mask = ((1 << 0)|
3048 (1 << 1));
3049 clone_mask = ((1 << INTEL_OUTPUT_HDMI));
3050 break;
79e53945
JB
3051 case INTEL_OUTPUT_DVO:
3052 case INTEL_OUTPUT_SDVO:
3053 crtc_mask = ((1 << 0)|
3054 (1 << 1));
3055 clone_mask = ((1 << INTEL_OUTPUT_ANALOG) |
3056 (1 << INTEL_OUTPUT_DVO) |
3057 (1 << INTEL_OUTPUT_SDVO));
3058 break;
3059 case INTEL_OUTPUT_ANALOG:
3060 crtc_mask = ((1 << 0)|
3061 (1 << 1));
3062 clone_mask = ((1 << INTEL_OUTPUT_ANALOG) |
3063 (1 << INTEL_OUTPUT_DVO) |
3064 (1 << INTEL_OUTPUT_SDVO));
3065 break;
3066 case INTEL_OUTPUT_LVDS:
3067 crtc_mask = (1 << 1);
3068 clone_mask = (1 << INTEL_OUTPUT_LVDS);
3069 break;
3070 case INTEL_OUTPUT_TVOUT:
3071 crtc_mask = ((1 << 0) |
3072 (1 << 1));
3073 clone_mask = (1 << INTEL_OUTPUT_TVOUT);
3074 break;
a4fc5ed6
KP
3075 case INTEL_OUTPUT_DISPLAYPORT:
3076 crtc_mask = ((1 << 0) |
3077 (1 << 1));
3078 clone_mask = (1 << INTEL_OUTPUT_DISPLAYPORT);
3079 break;
79e53945
JB
3080 }
3081 encoder->possible_crtcs = crtc_mask;
3082 encoder->possible_clones = intel_connector_clones(dev, clone_mask);
3083 }
3084}
3085
3086static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
3087{
3088 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
3089 struct drm_device *dev = fb->dev;
3090
3091 if (fb->fbdev)
3092 intelfb_remove(dev, fb);
3093
3094 drm_framebuffer_cleanup(fb);
3095 mutex_lock(&dev->struct_mutex);
3096 drm_gem_object_unreference(intel_fb->obj);
3097 mutex_unlock(&dev->struct_mutex);
3098
3099 kfree(intel_fb);
3100}
3101
3102static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
3103 struct drm_file *file_priv,
3104 unsigned int *handle)
3105{
3106 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
3107 struct drm_gem_object *object = intel_fb->obj;
3108
3109 return drm_gem_handle_create(file_priv, object, handle);
3110}
3111
3112static const struct drm_framebuffer_funcs intel_fb_funcs = {
3113 .destroy = intel_user_framebuffer_destroy,
3114 .create_handle = intel_user_framebuffer_create_handle,
3115};
3116
3117int intel_framebuffer_create(struct drm_device *dev,
3118 struct drm_mode_fb_cmd *mode_cmd,
3119 struct drm_framebuffer **fb,
3120 struct drm_gem_object *obj)
3121{
3122 struct intel_framebuffer *intel_fb;
3123 int ret;
3124
3125 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
3126 if (!intel_fb)
3127 return -ENOMEM;
3128
3129 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
3130 if (ret) {
3131 DRM_ERROR("framebuffer init failed %d\n", ret);
3132 return ret;
3133 }
3134
3135 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
3136
3137 intel_fb->obj = obj;
3138
3139 *fb = &intel_fb->base;
3140
3141 return 0;
3142}
3143
3144
3145static struct drm_framebuffer *
3146intel_user_framebuffer_create(struct drm_device *dev,
3147 struct drm_file *filp,
3148 struct drm_mode_fb_cmd *mode_cmd)
3149{
3150 struct drm_gem_object *obj;
3151 struct drm_framebuffer *fb;
3152 int ret;
3153
3154 obj = drm_gem_object_lookup(dev, filp, mode_cmd->handle);
3155 if (!obj)
3156 return NULL;
3157
3158 ret = intel_framebuffer_create(dev, mode_cmd, &fb, obj);
3159 if (ret) {
496818f0 3160 mutex_lock(&dev->struct_mutex);
79e53945 3161 drm_gem_object_unreference(obj);
496818f0 3162 mutex_unlock(&dev->struct_mutex);
79e53945
JB
3163 return NULL;
3164 }
3165
3166 return fb;
3167}
3168
79e53945 3169static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945
JB
3170 .fb_create = intel_user_framebuffer_create,
3171 .fb_changed = intelfb_probe,
3172};
3173
3174void intel_modeset_init(struct drm_device *dev)
3175{
3176 int num_pipe;
3177 int i;
3178
3179 drm_mode_config_init(dev);
3180
3181 dev->mode_config.min_width = 0;
3182 dev->mode_config.min_height = 0;
3183
3184 dev->mode_config.funcs = (void *)&intel_mode_funcs;
3185
3186 if (IS_I965G(dev)) {
3187 dev->mode_config.max_width = 8192;
3188 dev->mode_config.max_height = 8192;
5e4d6fa7
KP
3189 } else if (IS_I9XX(dev)) {
3190 dev->mode_config.max_width = 4096;
3191 dev->mode_config.max_height = 4096;
79e53945
JB
3192 } else {
3193 dev->mode_config.max_width = 2048;
3194 dev->mode_config.max_height = 2048;
3195 }
3196
3197 /* set memory base */
3198 if (IS_I9XX(dev))
3199 dev->mode_config.fb_base = pci_resource_start(dev->pdev, 2);
3200 else
3201 dev->mode_config.fb_base = pci_resource_start(dev->pdev, 0);
3202
3203 if (IS_MOBILE(dev) || IS_I9XX(dev))
3204 num_pipe = 2;
3205 else
3206 num_pipe = 1;
3207 DRM_DEBUG("%d display pipe%s available.\n",
3208 num_pipe, num_pipe > 1 ? "s" : "");
3209
3210 for (i = 0; i < num_pipe; i++) {
3211 intel_crtc_init(dev, i);
3212 }
3213
3214 intel_setup_outputs(dev);
3215}
3216
3217void intel_modeset_cleanup(struct drm_device *dev)
3218{
3219 drm_mode_config_cleanup(dev);
3220}
3221
3222
3223/* current intel driver doesn't take advantage of encoders
3224 always give back the encoder for the connector
3225*/
3226struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
3227{
3228 struct intel_output *intel_output = to_intel_output(connector);
3229
3230 return &intel_output->enc;
3231}
This page took 0.323935 seconds and 5 git commands to generate.