Subversion Repositories Kolibri OS

Rev

Rev 3037 | Rev 3243 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3037 Rev 3120
1
/*
1
/*
2
 * Copyright © 2006-2007 Intel Corporation
2
 * Copyright © 2006-2007 Intel Corporation
3
 *
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining a
4
 * Permission is hereby granted, free of charge, to any person obtaining a
5
 * copy of this software and associated documentation files (the "Software"),
5
 * copy of this software and associated documentation files (the "Software"),
6
 * to deal in the Software without restriction, including without limitation
6
 * to deal in the Software without restriction, including without limitation
7
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
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
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:
9
 * Software is furnished to do so, subject to the following conditions:
10
 *
10
 *
11
 * The above copyright notice and this permission notice (including the next
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
12
 * paragraph) shall be included in all copies or substantial portions of the
13
 * Software.
13
 * Software.
14
 *
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
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,
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
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
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
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
20
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21
 * DEALINGS IN THE SOFTWARE.
21
 * DEALINGS IN THE SOFTWARE.
22
 *
22
 *
23
 * Authors:
23
 * Authors:
24
 *  Eric Anholt 
24
 *  Eric Anholt 
25
 */
25
 */
26
 
26
 
27
//#include 
27
//#include 
28
#include 
28
#include 
29
//#include 
29
//#include 
30
#include 
30
#include 
31
#include 
31
#include 
32
#include 
32
#include 
33
//#include 
33
//#include 
34
#include 
34
#include 
35
#include 
35
#include 
36
#include "intel_drv.h"
36
#include "intel_drv.h"
37
#include 
37
#include 
38
#include "i915_drv.h"
38
#include "i915_drv.h"
39
#include "i915_trace.h"
39
#include "i915_trace.h"
40
#include 
40
#include 
41
#include 
41
#include 
42
//#include 
42
//#include 
43
 
43
 
44
phys_addr_t get_bus_addr(void);
44
phys_addr_t get_bus_addr(void);
45
 
45
 
46
static inline __attribute__((const))
46
static inline __attribute__((const))
47
bool is_power_of_2(unsigned long n)
47
bool is_power_of_2(unsigned long n)
48
{
48
{
49
    return (n != 0 && ((n & (n - 1)) == 0));
49
    return (n != 0 && ((n & (n - 1)) == 0));
50
}
50
}
51
 
51
 
52
#define MAX_ERRNO       4095
52
#define MAX_ERRNO       4095
53
 
53
 
54
#define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)
54
#define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)
55
 
55
 
56
static inline long IS_ERR(const void *ptr)
56
static inline long IS_ERR(const void *ptr)
57
{
57
{
58
    return IS_ERR_VALUE((unsigned long)ptr);
58
    return IS_ERR_VALUE((unsigned long)ptr);
59
}
59
}
60
 
60
 
61
static inline void *ERR_PTR(long error)
61
static inline void *ERR_PTR(long error)
62
{
62
{
63
    return (void *) error;
63
    return (void *) error;
64
}
64
}
65
 
65
 
66
 
66
 
67
#define HAS_eDP (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
67
#define HAS_eDP (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
68
 
68
 
69
bool intel_pipe_has_type(struct drm_crtc *crtc, int type);
69
bool intel_pipe_has_type(struct drm_crtc *crtc, int type);
70
static void intel_increase_pllclock(struct drm_crtc *crtc);
70
static void intel_increase_pllclock(struct drm_crtc *crtc);
71
//static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
71
//static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
72
 
72
 
73
typedef struct {
73
typedef struct {
74
    /* given values */
74
    /* given values */
75
    int n;
75
    int n;
76
    int m1, m2;
76
    int m1, m2;
77
    int p1, p2;
77
    int p1, p2;
78
    /* derived values */
78
    /* derived values */
79
    int dot;
79
    int dot;
80
    int vco;
80
    int vco;
81
    int m;
81
    int m;
82
    int p;
82
    int p;
83
} intel_clock_t;
83
} intel_clock_t;
84
 
84
 
85
typedef struct {
85
typedef struct {
86
    int min, max;
86
    int min, max;
87
} intel_range_t;
87
} intel_range_t;
88
 
88
 
89
typedef struct {
89
typedef struct {
90
    int dot_limit;
90
    int dot_limit;
91
    int p2_slow, p2_fast;
91
    int p2_slow, p2_fast;
92
} intel_p2_t;
92
} intel_p2_t;
93
 
93
 
94
#define INTEL_P2_NUM              2
94
#define INTEL_P2_NUM              2
95
typedef struct intel_limit intel_limit_t;
95
typedef struct intel_limit intel_limit_t;
96
struct intel_limit {
96
struct intel_limit {
97
    intel_range_t   dot, vco, n, m, m1, m2, p, p1;
97
    intel_range_t   dot, vco, n, m, m1, m2, p, p1;
98
    intel_p2_t      p2;
98
    intel_p2_t      p2;
99
    bool (* find_pll)(const intel_limit_t *, struct drm_crtc *,
99
    bool (* find_pll)(const intel_limit_t *, struct drm_crtc *,
100
			int, int, intel_clock_t *, intel_clock_t *);
100
			int, int, intel_clock_t *, intel_clock_t *);
101
};
101
};
102
 
102
 
103
/* FDI */
103
/* FDI */
104
#define IRONLAKE_FDI_FREQ       2700000 /* in kHz for mode->clock */
104
#define IRONLAKE_FDI_FREQ       2700000 /* in kHz for mode->clock */
105
 
105
 
106
static bool
106
static bool
107
intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
107
intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
108
		    int target, int refclk, intel_clock_t *match_clock,
108
		    int target, int refclk, intel_clock_t *match_clock,
109
		    intel_clock_t *best_clock);
109
		    intel_clock_t *best_clock);
110
static bool
110
static bool
111
intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
111
intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
112
			int target, int refclk, intel_clock_t *match_clock,
112
			int target, int refclk, intel_clock_t *match_clock,
113
			intel_clock_t *best_clock);
113
			intel_clock_t *best_clock);
114
 
114
 
115
static bool
115
static bool
116
intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
116
intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
117
		      int target, int refclk, intel_clock_t *match_clock,
117
		      int target, int refclk, intel_clock_t *match_clock,
118
		      intel_clock_t *best_clock);
118
		      intel_clock_t *best_clock);
119
static bool
119
static bool
120
intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,
120
intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,
121
			   int target, int refclk, intel_clock_t *match_clock,
121
			   int target, int refclk, intel_clock_t *match_clock,
122
			   intel_clock_t *best_clock);
122
			   intel_clock_t *best_clock);
123
 
123
 
124
static bool
124
static bool
125
intel_vlv_find_best_pll(const intel_limit_t *limit, struct drm_crtc *crtc,
125
intel_vlv_find_best_pll(const intel_limit_t *limit, struct drm_crtc *crtc,
126
			int target, int refclk, intel_clock_t *match_clock,
126
			int target, int refclk, intel_clock_t *match_clock,
127
			intel_clock_t *best_clock);
127
			intel_clock_t *best_clock);
128
 
128
 
129
static inline u32 /* units of 100MHz */
129
static inline u32 /* units of 100MHz */
130
intel_fdi_link_freq(struct drm_device *dev)
130
intel_fdi_link_freq(struct drm_device *dev)
131
{
131
{
132
	if (IS_GEN5(dev)) {
132
	if (IS_GEN5(dev)) {
133
		struct drm_i915_private *dev_priv = dev->dev_private;
133
		struct drm_i915_private *dev_priv = dev->dev_private;
134
		return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
134
		return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
135
	} else
135
	} else
136
		return 27;
136
		return 27;
137
}
137
}
138
 
138
 
139
static const intel_limit_t intel_limits_i8xx_dvo = {
139
static const intel_limit_t intel_limits_i8xx_dvo = {
140
        .dot = { .min = 25000, .max = 350000 },
140
        .dot = { .min = 25000, .max = 350000 },
141
        .vco = { .min = 930000, .max = 1400000 },
141
        .vco = { .min = 930000, .max = 1400000 },
142
        .n = { .min = 3, .max = 16 },
142
        .n = { .min = 3, .max = 16 },
143
        .m = { .min = 96, .max = 140 },
143
        .m = { .min = 96, .max = 140 },
144
        .m1 = { .min = 18, .max = 26 },
144
        .m1 = { .min = 18, .max = 26 },
145
        .m2 = { .min = 6, .max = 16 },
145
        .m2 = { .min = 6, .max = 16 },
146
        .p = { .min = 4, .max = 128 },
146
        .p = { .min = 4, .max = 128 },
147
        .p1 = { .min = 2, .max = 33 },
147
        .p1 = { .min = 2, .max = 33 },
148
	.p2 = { .dot_limit = 165000,
148
	.p2 = { .dot_limit = 165000,
149
		.p2_slow = 4, .p2_fast = 2 },
149
		.p2_slow = 4, .p2_fast = 2 },
150
	.find_pll = intel_find_best_PLL,
150
	.find_pll = intel_find_best_PLL,
151
};
151
};
152
 
152
 
153
static const intel_limit_t intel_limits_i8xx_lvds = {
153
static const intel_limit_t intel_limits_i8xx_lvds = {
154
        .dot = { .min = 25000, .max = 350000 },
154
        .dot = { .min = 25000, .max = 350000 },
155
        .vco = { .min = 930000, .max = 1400000 },
155
        .vco = { .min = 930000, .max = 1400000 },
156
        .n = { .min = 3, .max = 16 },
156
        .n = { .min = 3, .max = 16 },
157
        .m = { .min = 96, .max = 140 },
157
        .m = { .min = 96, .max = 140 },
158
        .m1 = { .min = 18, .max = 26 },
158
        .m1 = { .min = 18, .max = 26 },
159
        .m2 = { .min = 6, .max = 16 },
159
        .m2 = { .min = 6, .max = 16 },
160
        .p = { .min = 4, .max = 128 },
160
        .p = { .min = 4, .max = 128 },
161
        .p1 = { .min = 1, .max = 6 },
161
        .p1 = { .min = 1, .max = 6 },
162
	.p2 = { .dot_limit = 165000,
162
	.p2 = { .dot_limit = 165000,
163
		.p2_slow = 14, .p2_fast = 7 },
163
		.p2_slow = 14, .p2_fast = 7 },
164
	.find_pll = intel_find_best_PLL,
164
	.find_pll = intel_find_best_PLL,
165
};
165
};
166
 
166
 
167
static const intel_limit_t intel_limits_i9xx_sdvo = {
167
static const intel_limit_t intel_limits_i9xx_sdvo = {
168
        .dot = { .min = 20000, .max = 400000 },
168
        .dot = { .min = 20000, .max = 400000 },
169
        .vco = { .min = 1400000, .max = 2800000 },
169
        .vco = { .min = 1400000, .max = 2800000 },
170
        .n = { .min = 1, .max = 6 },
170
        .n = { .min = 1, .max = 6 },
171
        .m = { .min = 70, .max = 120 },
171
        .m = { .min = 70, .max = 120 },
172
        .m1 = { .min = 10, .max = 22 },
172
        .m1 = { .min = 10, .max = 22 },
173
        .m2 = { .min = 5, .max = 9 },
173
        .m2 = { .min = 5, .max = 9 },
174
        .p = { .min = 5, .max = 80 },
174
        .p = { .min = 5, .max = 80 },
175
        .p1 = { .min = 1, .max = 8 },
175
        .p1 = { .min = 1, .max = 8 },
176
	.p2 = { .dot_limit = 200000,
176
	.p2 = { .dot_limit = 200000,
177
		.p2_slow = 10, .p2_fast = 5 },
177
		.p2_slow = 10, .p2_fast = 5 },
178
	.find_pll = intel_find_best_PLL,
178
	.find_pll = intel_find_best_PLL,
179
};
179
};
180
 
180
 
181
static const intel_limit_t intel_limits_i9xx_lvds = {
181
static const intel_limit_t intel_limits_i9xx_lvds = {
182
        .dot = { .min = 20000, .max = 400000 },
182
        .dot = { .min = 20000, .max = 400000 },
183
        .vco = { .min = 1400000, .max = 2800000 },
183
        .vco = { .min = 1400000, .max = 2800000 },
184
        .n = { .min = 1, .max = 6 },
184
        .n = { .min = 1, .max = 6 },
185
        .m = { .min = 70, .max = 120 },
185
        .m = { .min = 70, .max = 120 },
186
        .m1 = { .min = 10, .max = 22 },
186
        .m1 = { .min = 10, .max = 22 },
187
        .m2 = { .min = 5, .max = 9 },
187
        .m2 = { .min = 5, .max = 9 },
188
        .p = { .min = 7, .max = 98 },
188
        .p = { .min = 7, .max = 98 },
189
        .p1 = { .min = 1, .max = 8 },
189
        .p1 = { .min = 1, .max = 8 },
190
	.p2 = { .dot_limit = 112000,
190
	.p2 = { .dot_limit = 112000,
191
		.p2_slow = 14, .p2_fast = 7 },
191
		.p2_slow = 14, .p2_fast = 7 },
192
	.find_pll = intel_find_best_PLL,
192
	.find_pll = intel_find_best_PLL,
193
};
193
};
194
 
194
 
195
 
195
 
196
static const intel_limit_t intel_limits_g4x_sdvo = {
196
static const intel_limit_t intel_limits_g4x_sdvo = {
197
	.dot = { .min = 25000, .max = 270000 },
197
	.dot = { .min = 25000, .max = 270000 },
198
	.vco = { .min = 1750000, .max = 3500000},
198
	.vco = { .min = 1750000, .max = 3500000},
199
	.n = { .min = 1, .max = 4 },
199
	.n = { .min = 1, .max = 4 },
200
	.m = { .min = 104, .max = 138 },
200
	.m = { .min = 104, .max = 138 },
201
	.m1 = { .min = 17, .max = 23 },
201
	.m1 = { .min = 17, .max = 23 },
202
	.m2 = { .min = 5, .max = 11 },
202
	.m2 = { .min = 5, .max = 11 },
203
	.p = { .min = 10, .max = 30 },
203
	.p = { .min = 10, .max = 30 },
204
	.p1 = { .min = 1, .max = 3},
204
	.p1 = { .min = 1, .max = 3},
205
	.p2 = { .dot_limit = 270000,
205
	.p2 = { .dot_limit = 270000,
206
		.p2_slow = 10,
206
		.p2_slow = 10,
207
		.p2_fast = 10
207
		.p2_fast = 10
208
	},
208
	},
209
	.find_pll = intel_g4x_find_best_PLL,
209
	.find_pll = intel_g4x_find_best_PLL,
210
};
210
};
211
 
211
 
212
static const intel_limit_t intel_limits_g4x_hdmi = {
212
static const intel_limit_t intel_limits_g4x_hdmi = {
213
	.dot = { .min = 22000, .max = 400000 },
213
	.dot = { .min = 22000, .max = 400000 },
214
	.vco = { .min = 1750000, .max = 3500000},
214
	.vco = { .min = 1750000, .max = 3500000},
215
	.n = { .min = 1, .max = 4 },
215
	.n = { .min = 1, .max = 4 },
216
	.m = { .min = 104, .max = 138 },
216
	.m = { .min = 104, .max = 138 },
217
	.m1 = { .min = 16, .max = 23 },
217
	.m1 = { .min = 16, .max = 23 },
218
	.m2 = { .min = 5, .max = 11 },
218
	.m2 = { .min = 5, .max = 11 },
219
	.p = { .min = 5, .max = 80 },
219
	.p = { .min = 5, .max = 80 },
220
	.p1 = { .min = 1, .max = 8},
220
	.p1 = { .min = 1, .max = 8},
221
	.p2 = { .dot_limit = 165000,
221
	.p2 = { .dot_limit = 165000,
222
		.p2_slow = 10, .p2_fast = 5 },
222
		.p2_slow = 10, .p2_fast = 5 },
223
	.find_pll = intel_g4x_find_best_PLL,
223
	.find_pll = intel_g4x_find_best_PLL,
224
};
224
};
225
 
225
 
226
static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
226
static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
227
	.dot = { .min = 20000, .max = 115000 },
227
	.dot = { .min = 20000, .max = 115000 },
228
	.vco = { .min = 1750000, .max = 3500000 },
228
	.vco = { .min = 1750000, .max = 3500000 },
229
	.n = { .min = 1, .max = 3 },
229
	.n = { .min = 1, .max = 3 },
230
	.m = { .min = 104, .max = 138 },
230
	.m = { .min = 104, .max = 138 },
231
	.m1 = { .min = 17, .max = 23 },
231
	.m1 = { .min = 17, .max = 23 },
232
	.m2 = { .min = 5, .max = 11 },
232
	.m2 = { .min = 5, .max = 11 },
233
	.p = { .min = 28, .max = 112 },
233
	.p = { .min = 28, .max = 112 },
234
	.p1 = { .min = 2, .max = 8 },
234
	.p1 = { .min = 2, .max = 8 },
235
	.p2 = { .dot_limit = 0,
235
	.p2 = { .dot_limit = 0,
236
		.p2_slow = 14, .p2_fast = 14
236
		.p2_slow = 14, .p2_fast = 14
237
	},
237
	},
238
	.find_pll = intel_g4x_find_best_PLL,
238
	.find_pll = intel_g4x_find_best_PLL,
239
};
239
};
240
 
240
 
241
static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
241
static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
242
	.dot = { .min = 80000, .max = 224000 },
242
	.dot = { .min = 80000, .max = 224000 },
243
	.vco = { .min = 1750000, .max = 3500000 },
243
	.vco = { .min = 1750000, .max = 3500000 },
244
	.n = { .min = 1, .max = 3 },
244
	.n = { .min = 1, .max = 3 },
245
	.m = { .min = 104, .max = 138 },
245
	.m = { .min = 104, .max = 138 },
246
	.m1 = { .min = 17, .max = 23 },
246
	.m1 = { .min = 17, .max = 23 },
247
	.m2 = { .min = 5, .max = 11 },
247
	.m2 = { .min = 5, .max = 11 },
248
	.p = { .min = 14, .max = 42 },
248
	.p = { .min = 14, .max = 42 },
249
	.p1 = { .min = 2, .max = 6 },
249
	.p1 = { .min = 2, .max = 6 },
250
	.p2 = { .dot_limit = 0,
250
	.p2 = { .dot_limit = 0,
251
		.p2_slow = 7, .p2_fast = 7
251
		.p2_slow = 7, .p2_fast = 7
252
	},
252
	},
253
	.find_pll = intel_g4x_find_best_PLL,
253
	.find_pll = intel_g4x_find_best_PLL,
254
};
254
};
255
 
255
 
256
static const intel_limit_t intel_limits_g4x_display_port = {
256
static const intel_limit_t intel_limits_g4x_display_port = {
257
        .dot = { .min = 161670, .max = 227000 },
257
        .dot = { .min = 161670, .max = 227000 },
258
        .vco = { .min = 1750000, .max = 3500000},
258
        .vco = { .min = 1750000, .max = 3500000},
259
        .n = { .min = 1, .max = 2 },
259
        .n = { .min = 1, .max = 2 },
260
        .m = { .min = 97, .max = 108 },
260
        .m = { .min = 97, .max = 108 },
261
        .m1 = { .min = 0x10, .max = 0x12 },
261
        .m1 = { .min = 0x10, .max = 0x12 },
262
        .m2 = { .min = 0x05, .max = 0x06 },
262
        .m2 = { .min = 0x05, .max = 0x06 },
263
        .p = { .min = 10, .max = 20 },
263
        .p = { .min = 10, .max = 20 },
264
        .p1 = { .min = 1, .max = 2},
264
        .p1 = { .min = 1, .max = 2},
265
        .p2 = { .dot_limit = 0,
265
        .p2 = { .dot_limit = 0,
266
		.p2_slow = 10, .p2_fast = 10 },
266
		.p2_slow = 10, .p2_fast = 10 },
267
        .find_pll = intel_find_pll_g4x_dp,
267
        .find_pll = intel_find_pll_g4x_dp,
268
};
268
};
269
 
269
 
270
static const intel_limit_t intel_limits_pineview_sdvo = {
270
static const intel_limit_t intel_limits_pineview_sdvo = {
271
        .dot = { .min = 20000, .max = 400000},
271
        .dot = { .min = 20000, .max = 400000},
272
        .vco = { .min = 1700000, .max = 3500000 },
272
        .vco = { .min = 1700000, .max = 3500000 },
273
	/* Pineview's Ncounter is a ring counter */
273
	/* Pineview's Ncounter is a ring counter */
274
        .n = { .min = 3, .max = 6 },
274
        .n = { .min = 3, .max = 6 },
275
        .m = { .min = 2, .max = 256 },
275
        .m = { .min = 2, .max = 256 },
276
	/* Pineview only has one combined m divider, which we treat as m2. */
276
	/* Pineview only has one combined m divider, which we treat as m2. */
277
        .m1 = { .min = 0, .max = 0 },
277
        .m1 = { .min = 0, .max = 0 },
278
        .m2 = { .min = 0, .max = 254 },
278
        .m2 = { .min = 0, .max = 254 },
279
        .p = { .min = 5, .max = 80 },
279
        .p = { .min = 5, .max = 80 },
280
        .p1 = { .min = 1, .max = 8 },
280
        .p1 = { .min = 1, .max = 8 },
281
	.p2 = { .dot_limit = 200000,
281
	.p2 = { .dot_limit = 200000,
282
		.p2_slow = 10, .p2_fast = 5 },
282
		.p2_slow = 10, .p2_fast = 5 },
283
	.find_pll = intel_find_best_PLL,
283
	.find_pll = intel_find_best_PLL,
284
};
284
};
285
 
285
 
286
static const intel_limit_t intel_limits_pineview_lvds = {
286
static const intel_limit_t intel_limits_pineview_lvds = {
287
        .dot = { .min = 20000, .max = 400000 },
287
        .dot = { .min = 20000, .max = 400000 },
288
        .vco = { .min = 1700000, .max = 3500000 },
288
        .vco = { .min = 1700000, .max = 3500000 },
289
        .n = { .min = 3, .max = 6 },
289
        .n = { .min = 3, .max = 6 },
290
        .m = { .min = 2, .max = 256 },
290
        .m = { .min = 2, .max = 256 },
291
        .m1 = { .min = 0, .max = 0 },
291
        .m1 = { .min = 0, .max = 0 },
292
        .m2 = { .min = 0, .max = 254 },
292
        .m2 = { .min = 0, .max = 254 },
293
        .p = { .min = 7, .max = 112 },
293
        .p = { .min = 7, .max = 112 },
294
        .p1 = { .min = 1, .max = 8 },
294
        .p1 = { .min = 1, .max = 8 },
295
	.p2 = { .dot_limit = 112000,
295
	.p2 = { .dot_limit = 112000,
296
		.p2_slow = 14, .p2_fast = 14 },
296
		.p2_slow = 14, .p2_fast = 14 },
297
	.find_pll = intel_find_best_PLL,
297
	.find_pll = intel_find_best_PLL,
298
};
298
};
299
 
299
 
300
/* Ironlake / Sandybridge
300
/* Ironlake / Sandybridge
301
 *
301
 *
302
 * We calculate clock using (register_value + 2) for N/M1/M2, so here
302
 * We calculate clock using (register_value + 2) for N/M1/M2, so here
303
 * the range value for them is (actual_value - 2).
303
 * the range value for them is (actual_value - 2).
304
 */
304
 */
305
static const intel_limit_t intel_limits_ironlake_dac = {
305
static const intel_limit_t intel_limits_ironlake_dac = {
306
	.dot = { .min = 25000, .max = 350000 },
306
	.dot = { .min = 25000, .max = 350000 },
307
	.vco = { .min = 1760000, .max = 3510000 },
307
	.vco = { .min = 1760000, .max = 3510000 },
308
	.n = { .min = 1, .max = 5 },
308
	.n = { .min = 1, .max = 5 },
309
	.m = { .min = 79, .max = 127 },
309
	.m = { .min = 79, .max = 127 },
310
	.m1 = { .min = 12, .max = 22 },
310
	.m1 = { .min = 12, .max = 22 },
311
	.m2 = { .min = 5, .max = 9 },
311
	.m2 = { .min = 5, .max = 9 },
312
	.p = { .min = 5, .max = 80 },
312
	.p = { .min = 5, .max = 80 },
313
	.p1 = { .min = 1, .max = 8 },
313
	.p1 = { .min = 1, .max = 8 },
314
	.p2 = { .dot_limit = 225000,
314
	.p2 = { .dot_limit = 225000,
315
		.p2_slow = 10, .p2_fast = 5 },
315
		.p2_slow = 10, .p2_fast = 5 },
316
	.find_pll = intel_g4x_find_best_PLL,
316
	.find_pll = intel_g4x_find_best_PLL,
317
};
317
};
318
 
318
 
319
static const intel_limit_t intel_limits_ironlake_single_lvds = {
319
static const intel_limit_t intel_limits_ironlake_single_lvds = {
320
	.dot = { .min = 25000, .max = 350000 },
320
	.dot = { .min = 25000, .max = 350000 },
321
	.vco = { .min = 1760000, .max = 3510000 },
321
	.vco = { .min = 1760000, .max = 3510000 },
322
	.n = { .min = 1, .max = 3 },
322
	.n = { .min = 1, .max = 3 },
323
	.m = { .min = 79, .max = 118 },
323
	.m = { .min = 79, .max = 118 },
324
	.m1 = { .min = 12, .max = 22 },
324
	.m1 = { .min = 12, .max = 22 },
325
	.m2 = { .min = 5, .max = 9 },
325
	.m2 = { .min = 5, .max = 9 },
326
	.p = { .min = 28, .max = 112 },
326
	.p = { .min = 28, .max = 112 },
327
	.p1 = { .min = 2, .max = 8 },
327
	.p1 = { .min = 2, .max = 8 },
328
	.p2 = { .dot_limit = 225000,
328
	.p2 = { .dot_limit = 225000,
329
		.p2_slow = 14, .p2_fast = 14 },
329
		.p2_slow = 14, .p2_fast = 14 },
330
	.find_pll = intel_g4x_find_best_PLL,
330
	.find_pll = intel_g4x_find_best_PLL,
331
};
331
};
332
 
332
 
333
static const intel_limit_t intel_limits_ironlake_dual_lvds = {
333
static const intel_limit_t intel_limits_ironlake_dual_lvds = {
334
	.dot = { .min = 25000, .max = 350000 },
334
	.dot = { .min = 25000, .max = 350000 },
335
	.vco = { .min = 1760000, .max = 3510000 },
335
	.vco = { .min = 1760000, .max = 3510000 },
336
	.n = { .min = 1, .max = 3 },
336
	.n = { .min = 1, .max = 3 },
337
	.m = { .min = 79, .max = 127 },
337
	.m = { .min = 79, .max = 127 },
338
	.m1 = { .min = 12, .max = 22 },
338
	.m1 = { .min = 12, .max = 22 },
339
	.m2 = { .min = 5, .max = 9 },
339
	.m2 = { .min = 5, .max = 9 },
340
	.p = { .min = 14, .max = 56 },
340
	.p = { .min = 14, .max = 56 },
341
	.p1 = { .min = 2, .max = 8 },
341
	.p1 = { .min = 2, .max = 8 },
342
	.p2 = { .dot_limit = 225000,
342
	.p2 = { .dot_limit = 225000,
343
		.p2_slow = 7, .p2_fast = 7 },
343
		.p2_slow = 7, .p2_fast = 7 },
344
	.find_pll = intel_g4x_find_best_PLL,
344
	.find_pll = intel_g4x_find_best_PLL,
345
};
345
};
346
 
346
 
347
/* LVDS 100mhz refclk limits. */
347
/* LVDS 100mhz refclk limits. */
348
static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
348
static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
349
	.dot = { .min = 25000, .max = 350000 },
349
	.dot = { .min = 25000, .max = 350000 },
350
	.vco = { .min = 1760000, .max = 3510000 },
350
	.vco = { .min = 1760000, .max = 3510000 },
351
	.n = { .min = 1, .max = 2 },
351
	.n = { .min = 1, .max = 2 },
352
	.m = { .min = 79, .max = 126 },
352
	.m = { .min = 79, .max = 126 },
353
	.m1 = { .min = 12, .max = 22 },
353
	.m1 = { .min = 12, .max = 22 },
354
	.m2 = { .min = 5, .max = 9 },
354
	.m2 = { .min = 5, .max = 9 },
355
	.p = { .min = 28, .max = 112 },
355
	.p = { .min = 28, .max = 112 },
356
	.p1 = { .min = 2, .max = 8 },
356
	.p1 = { .min = 2, .max = 8 },
357
	.p2 = { .dot_limit = 225000,
357
	.p2 = { .dot_limit = 225000,
358
		.p2_slow = 14, .p2_fast = 14 },
358
		.p2_slow = 14, .p2_fast = 14 },
359
	.find_pll = intel_g4x_find_best_PLL,
359
	.find_pll = intel_g4x_find_best_PLL,
360
};
360
};
361
 
361
 
362
static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
362
static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
363
	.dot = { .min = 25000, .max = 350000 },
363
	.dot = { .min = 25000, .max = 350000 },
364
	.vco = { .min = 1760000, .max = 3510000 },
364
	.vco = { .min = 1760000, .max = 3510000 },
365
	.n = { .min = 1, .max = 3 },
365
	.n = { .min = 1, .max = 3 },
366
	.m = { .min = 79, .max = 126 },
366
	.m = { .min = 79, .max = 126 },
367
	.m1 = { .min = 12, .max = 22 },
367
	.m1 = { .min = 12, .max = 22 },
368
	.m2 = { .min = 5, .max = 9 },
368
	.m2 = { .min = 5, .max = 9 },
369
	.p = { .min = 14, .max = 42 },
369
	.p = { .min = 14, .max = 42 },
370
	.p1 = { .min = 2, .max = 6 },
370
	.p1 = { .min = 2, .max = 6 },
371
	.p2 = { .dot_limit = 225000,
371
	.p2 = { .dot_limit = 225000,
372
		.p2_slow = 7, .p2_fast = 7 },
372
		.p2_slow = 7, .p2_fast = 7 },
373
	.find_pll = intel_g4x_find_best_PLL,
373
	.find_pll = intel_g4x_find_best_PLL,
374
};
374
};
375
 
375
 
376
static const intel_limit_t intel_limits_ironlake_display_port = {
376
static const intel_limit_t intel_limits_ironlake_display_port = {
377
        .dot = { .min = 25000, .max = 350000 },
377
        .dot = { .min = 25000, .max = 350000 },
378
        .vco = { .min = 1760000, .max = 3510000},
378
        .vco = { .min = 1760000, .max = 3510000},
379
        .n = { .min = 1, .max = 2 },
379
        .n = { .min = 1, .max = 2 },
380
        .m = { .min = 81, .max = 90 },
380
        .m = { .min = 81, .max = 90 },
381
        .m1 = { .min = 12, .max = 22 },
381
        .m1 = { .min = 12, .max = 22 },
382
        .m2 = { .min = 5, .max = 9 },
382
        .m2 = { .min = 5, .max = 9 },
383
        .p = { .min = 10, .max = 20 },
383
        .p = { .min = 10, .max = 20 },
384
        .p1 = { .min = 1, .max = 2},
384
        .p1 = { .min = 1, .max = 2},
385
        .p2 = { .dot_limit = 0,
385
        .p2 = { .dot_limit = 0,
386
		.p2_slow = 10, .p2_fast = 10 },
386
		.p2_slow = 10, .p2_fast = 10 },
387
        .find_pll = intel_find_pll_ironlake_dp,
387
        .find_pll = intel_find_pll_ironlake_dp,
388
};
388
};
389
 
389
 
390
static const intel_limit_t intel_limits_vlv_dac = {
390
static const intel_limit_t intel_limits_vlv_dac = {
391
	.dot = { .min = 25000, .max = 270000 },
391
	.dot = { .min = 25000, .max = 270000 },
392
	.vco = { .min = 4000000, .max = 6000000 },
392
	.vco = { .min = 4000000, .max = 6000000 },
393
	.n = { .min = 1, .max = 7 },
393
	.n = { .min = 1, .max = 7 },
394
	.m = { .min = 22, .max = 450 }, /* guess */
394
	.m = { .min = 22, .max = 450 }, /* guess */
395
	.m1 = { .min = 2, .max = 3 },
395
	.m1 = { .min = 2, .max = 3 },
396
	.m2 = { .min = 11, .max = 156 },
396
	.m2 = { .min = 11, .max = 156 },
397
	.p = { .min = 10, .max = 30 },
397
	.p = { .min = 10, .max = 30 },
398
	.p1 = { .min = 2, .max = 3 },
398
	.p1 = { .min = 2, .max = 3 },
399
	.p2 = { .dot_limit = 270000,
399
	.p2 = { .dot_limit = 270000,
400
		.p2_slow = 2, .p2_fast = 20 },
400
		.p2_slow = 2, .p2_fast = 20 },
401
	.find_pll = intel_vlv_find_best_pll,
401
	.find_pll = intel_vlv_find_best_pll,
402
};
402
};
403
 
403
 
404
static const intel_limit_t intel_limits_vlv_hdmi = {
404
static const intel_limit_t intel_limits_vlv_hdmi = {
405
	.dot = { .min = 20000, .max = 165000 },
405
	.dot = { .min = 20000, .max = 165000 },
406
	.vco = { .min = 5994000, .max = 4000000 },
406
	.vco = { .min = 5994000, .max = 4000000 },
407
	.n = { .min = 1, .max = 7 },
407
	.n = { .min = 1, .max = 7 },
408
	.m = { .min = 60, .max = 300 }, /* guess */
408
	.m = { .min = 60, .max = 300 }, /* guess */
409
	.m1 = { .min = 2, .max = 3 },
409
	.m1 = { .min = 2, .max = 3 },
410
	.m2 = { .min = 11, .max = 156 },
410
	.m2 = { .min = 11, .max = 156 },
411
	.p = { .min = 10, .max = 30 },
411
	.p = { .min = 10, .max = 30 },
412
	.p1 = { .min = 2, .max = 3 },
412
	.p1 = { .min = 2, .max = 3 },
413
	.p2 = { .dot_limit = 270000,
413
	.p2 = { .dot_limit = 270000,
414
		.p2_slow = 2, .p2_fast = 20 },
414
		.p2_slow = 2, .p2_fast = 20 },
415
	.find_pll = intel_vlv_find_best_pll,
415
	.find_pll = intel_vlv_find_best_pll,
416
};
416
};
417
 
417
 
418
static const intel_limit_t intel_limits_vlv_dp = {
418
static const intel_limit_t intel_limits_vlv_dp = {
419
	.dot = { .min = 162000, .max = 270000 },
419
	.dot = { .min = 162000, .max = 270000 },
420
	.vco = { .min = 5994000, .max = 4000000 },
420
	.vco = { .min = 5994000, .max = 4000000 },
421
	.n = { .min = 1, .max = 7 },
421
	.n = { .min = 1, .max = 7 },
422
	.m = { .min = 60, .max = 300 }, /* guess */
422
	.m = { .min = 60, .max = 300 }, /* guess */
423
	.m1 = { .min = 2, .max = 3 },
423
	.m1 = { .min = 2, .max = 3 },
424
	.m2 = { .min = 11, .max = 156 },
424
	.m2 = { .min = 11, .max = 156 },
425
	.p = { .min = 10, .max = 30 },
425
	.p = { .min = 10, .max = 30 },
426
	.p1 = { .min = 2, .max = 3 },
426
	.p1 = { .min = 2, .max = 3 },
427
	.p2 = { .dot_limit = 270000,
427
	.p2 = { .dot_limit = 270000,
428
		.p2_slow = 2, .p2_fast = 20 },
428
		.p2_slow = 2, .p2_fast = 20 },
429
	.find_pll = intel_vlv_find_best_pll,
429
	.find_pll = intel_vlv_find_best_pll,
430
};
430
};
431
 
431
 
432
u32 intel_dpio_read(struct drm_i915_private *dev_priv, int reg)
432
u32 intel_dpio_read(struct drm_i915_private *dev_priv, int reg)
433
{
433
{
434
	unsigned long flags;
434
	unsigned long flags;
435
	u32 val = 0;
435
	u32 val = 0;
436
 
436
 
437
	spin_lock_irqsave(&dev_priv->dpio_lock, flags);
437
	spin_lock_irqsave(&dev_priv->dpio_lock, flags);
438
	if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
438
	if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
439
		DRM_ERROR("DPIO idle wait timed out\n");
439
		DRM_ERROR("DPIO idle wait timed out\n");
440
		goto out_unlock;
440
		goto out_unlock;
441
	}
441
	}
442
 
442
 
443
	I915_WRITE(DPIO_REG, reg);
443
	I915_WRITE(DPIO_REG, reg);
444
	I915_WRITE(DPIO_PKT, DPIO_RID | DPIO_OP_READ | DPIO_PORTID |
444
	I915_WRITE(DPIO_PKT, DPIO_RID | DPIO_OP_READ | DPIO_PORTID |
445
		   DPIO_BYTE);
445
		   DPIO_BYTE);
446
	if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
446
	if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
447
		DRM_ERROR("DPIO read wait timed out\n");
447
		DRM_ERROR("DPIO read wait timed out\n");
448
		goto out_unlock;
448
		goto out_unlock;
449
	}
449
	}
450
	val = I915_READ(DPIO_DATA);
450
	val = I915_READ(DPIO_DATA);
451
 
451
 
452
out_unlock:
452
out_unlock:
453
	spin_unlock_irqrestore(&dev_priv->dpio_lock, flags);
453
	spin_unlock_irqrestore(&dev_priv->dpio_lock, flags);
454
	return val;
454
	return val;
455
}
455
}
456
 
456
 
457
static void intel_dpio_write(struct drm_i915_private *dev_priv, int reg,
457
static void intel_dpio_write(struct drm_i915_private *dev_priv, int reg,
458
			     u32 val)
458
			     u32 val)
459
{
459
{
460
	unsigned long flags;
460
	unsigned long flags;
461
 
461
 
462
	spin_lock_irqsave(&dev_priv->dpio_lock, flags);
462
	spin_lock_irqsave(&dev_priv->dpio_lock, flags);
463
	if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
463
	if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
464
		DRM_ERROR("DPIO idle wait timed out\n");
464
		DRM_ERROR("DPIO idle wait timed out\n");
465
		goto out_unlock;
465
		goto out_unlock;
466
	}
466
	}
467
 
467
 
468
	I915_WRITE(DPIO_DATA, val);
468
	I915_WRITE(DPIO_DATA, val);
469
	I915_WRITE(DPIO_REG, reg);
469
	I915_WRITE(DPIO_REG, reg);
470
	I915_WRITE(DPIO_PKT, DPIO_RID | DPIO_OP_WRITE | DPIO_PORTID |
470
	I915_WRITE(DPIO_PKT, DPIO_RID | DPIO_OP_WRITE | DPIO_PORTID |
471
		   DPIO_BYTE);
471
		   DPIO_BYTE);
472
	if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100))
472
	if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100))
473
		DRM_ERROR("DPIO write wait timed out\n");
473
		DRM_ERROR("DPIO write wait timed out\n");
474
 
474
 
475
out_unlock:
475
out_unlock:
476
       spin_unlock_irqrestore(&dev_priv->dpio_lock, flags);
476
       spin_unlock_irqrestore(&dev_priv->dpio_lock, flags);
477
}
477
}
478
 
478
 
479
static void vlv_init_dpio(struct drm_device *dev)
479
static void vlv_init_dpio(struct drm_device *dev)
480
{
480
{
481
	struct drm_i915_private *dev_priv = dev->dev_private;
481
	struct drm_i915_private *dev_priv = dev->dev_private;
482
 
482
 
483
	/* Reset the DPIO config */
483
	/* Reset the DPIO config */
484
	I915_WRITE(DPIO_CTL, 0);
484
	I915_WRITE(DPIO_CTL, 0);
485
	POSTING_READ(DPIO_CTL);
485
	POSTING_READ(DPIO_CTL);
486
	I915_WRITE(DPIO_CTL, 1);
486
	I915_WRITE(DPIO_CTL, 1);
487
	POSTING_READ(DPIO_CTL);
487
	POSTING_READ(DPIO_CTL);
488
}
488
}
489
 
489
 
490
static int intel_dual_link_lvds_callback(const struct dmi_system_id *id)
490
static int intel_dual_link_lvds_callback(const struct dmi_system_id *id)
491
{
491
{
492
	DRM_INFO("Forcing lvds to dual link mode on %s\n", id->ident);
492
	DRM_INFO("Forcing lvds to dual link mode on %s\n", id->ident);
493
	return 1;
493
	return 1;
494
}
494
}
495
 
495
 
496
static const struct dmi_system_id intel_dual_link_lvds[] = {
496
static const struct dmi_system_id intel_dual_link_lvds[] = {
497
	{
497
	{
498
		.callback = intel_dual_link_lvds_callback,
498
		.callback = intel_dual_link_lvds_callback,
499
		.ident = "Apple MacBook Pro (Core i5/i7 Series)",
499
		.ident = "Apple MacBook Pro (Core i5/i7 Series)",
500
		.matches = {
500
		.matches = {
501
			DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
501
			DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
502
			DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro8,2"),
502
			DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro8,2"),
503
		},
503
		},
504
	},
504
	},
505
	{ }	/* terminating entry */
505
	{ }	/* terminating entry */
506
};
506
};
507
 
507
 
508
static bool is_dual_link_lvds(struct drm_i915_private *dev_priv,
508
static bool is_dual_link_lvds(struct drm_i915_private *dev_priv,
509
			      unsigned int reg)
509
			      unsigned int reg)
510
{
510
{
511
	unsigned int val;
511
	unsigned int val;
512
 
512
 
513
	/* use the module option value if specified */
513
	/* use the module option value if specified */
514
	if (i915_lvds_channel_mode > 0)
514
	if (i915_lvds_channel_mode > 0)
515
		return i915_lvds_channel_mode == 2;
515
		return i915_lvds_channel_mode == 2;
516
 
516
 
517
//   if (dmi_check_system(intel_dual_link_lvds))
517
//   if (dmi_check_system(intel_dual_link_lvds))
518
//       return true;
518
//       return true;
519
 
519
 
520
	if (dev_priv->lvds_val)
520
	if (dev_priv->lvds_val)
521
		val = dev_priv->lvds_val;
521
		val = dev_priv->lvds_val;
522
	else {
522
	else {
523
		/* BIOS should set the proper LVDS register value at boot, but
523
		/* BIOS should set the proper LVDS register value at boot, but
524
		 * in reality, it doesn't set the value when the lid is closed;
524
		 * in reality, it doesn't set the value when the lid is closed;
525
		 * we need to check "the value to be set" in VBT when LVDS
525
		 * we need to check "the value to be set" in VBT when LVDS
526
		 * register is uninitialized.
526
		 * register is uninitialized.
527
		 */
527
		 */
528
		val = I915_READ(reg);
528
		val = I915_READ(reg);
529
		if (!(val & ~(LVDS_PIPE_MASK | LVDS_DETECTED)))
529
		if (!(val & ~(LVDS_PIPE_MASK | LVDS_DETECTED)))
530
			val = dev_priv->bios_lvds_val;
530
			val = dev_priv->bios_lvds_val;
531
		dev_priv->lvds_val = val;
531
		dev_priv->lvds_val = val;
532
	}
532
	}
533
	return (val & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP;
533
	return (val & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP;
534
}
534
}
535
 
535
 
536
static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
536
static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
537
						int refclk)
537
						int refclk)
538
{
538
{
539
	struct drm_device *dev = crtc->dev;
539
	struct drm_device *dev = crtc->dev;
540
	struct drm_i915_private *dev_priv = dev->dev_private;
540
	struct drm_i915_private *dev_priv = dev->dev_private;
541
	const intel_limit_t *limit;
541
	const intel_limit_t *limit;
542
 
542
 
543
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
543
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
544
		if (is_dual_link_lvds(dev_priv, PCH_LVDS)) {
544
		if (is_dual_link_lvds(dev_priv, PCH_LVDS)) {
545
			/* LVDS dual channel */
545
			/* LVDS dual channel */
546
			if (refclk == 100000)
546
			if (refclk == 100000)
547
				limit = &intel_limits_ironlake_dual_lvds_100m;
547
				limit = &intel_limits_ironlake_dual_lvds_100m;
548
			else
548
			else
549
				limit = &intel_limits_ironlake_dual_lvds;
549
				limit = &intel_limits_ironlake_dual_lvds;
550
		} else {
550
		} else {
551
			if (refclk == 100000)
551
			if (refclk == 100000)
552
				limit = &intel_limits_ironlake_single_lvds_100m;
552
				limit = &intel_limits_ironlake_single_lvds_100m;
553
			else
553
			else
554
				limit = &intel_limits_ironlake_single_lvds;
554
				limit = &intel_limits_ironlake_single_lvds;
555
		}
555
		}
556
	} else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
556
	} else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
557
			HAS_eDP)
557
			HAS_eDP)
558
		limit = &intel_limits_ironlake_display_port;
558
		limit = &intel_limits_ironlake_display_port;
559
	else
559
	else
560
		limit = &intel_limits_ironlake_dac;
560
		limit = &intel_limits_ironlake_dac;
561
 
561
 
562
	return limit;
562
	return limit;
563
}
563
}
564
 
564
 
565
static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
565
static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
566
{
566
{
567
	struct drm_device *dev = crtc->dev;
567
	struct drm_device *dev = crtc->dev;
568
	struct drm_i915_private *dev_priv = dev->dev_private;
568
	struct drm_i915_private *dev_priv = dev->dev_private;
569
	const intel_limit_t *limit;
569
	const intel_limit_t *limit;
570
 
570
 
571
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
571
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
572
		if (is_dual_link_lvds(dev_priv, LVDS))
572
		if (is_dual_link_lvds(dev_priv, LVDS))
573
			/* LVDS with dual channel */
573
			/* LVDS with dual channel */
574
			limit = &intel_limits_g4x_dual_channel_lvds;
574
			limit = &intel_limits_g4x_dual_channel_lvds;
575
		else
575
		else
576
			/* LVDS with dual channel */
576
			/* LVDS with dual channel */
577
			limit = &intel_limits_g4x_single_channel_lvds;
577
			limit = &intel_limits_g4x_single_channel_lvds;
578
	} else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
578
	} else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
579
		   intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
579
		   intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
580
		limit = &intel_limits_g4x_hdmi;
580
		limit = &intel_limits_g4x_hdmi;
581
	} else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
581
	} else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
582
		limit = &intel_limits_g4x_sdvo;
582
		limit = &intel_limits_g4x_sdvo;
583
	} else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
583
	} else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
584
		limit = &intel_limits_g4x_display_port;
584
		limit = &intel_limits_g4x_display_port;
585
	} else /* The option is for other outputs */
585
	} else /* The option is for other outputs */
586
		limit = &intel_limits_i9xx_sdvo;
586
		limit = &intel_limits_i9xx_sdvo;
587
 
587
 
588
	return limit;
588
	return limit;
589
}
589
}
590
 
590
 
591
static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
591
static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
592
{
592
{
593
	struct drm_device *dev = crtc->dev;
593
	struct drm_device *dev = crtc->dev;
594
	const intel_limit_t *limit;
594
	const intel_limit_t *limit;
595
 
595
 
596
	if (HAS_PCH_SPLIT(dev))
596
	if (HAS_PCH_SPLIT(dev))
597
		limit = intel_ironlake_limit(crtc, refclk);
597
		limit = intel_ironlake_limit(crtc, refclk);
598
	else if (IS_G4X(dev)) {
598
	else if (IS_G4X(dev)) {
599
		limit = intel_g4x_limit(crtc);
599
		limit = intel_g4x_limit(crtc);
600
	} else if (IS_PINEVIEW(dev)) {
600
	} else if (IS_PINEVIEW(dev)) {
601
		if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
601
		if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
602
			limit = &intel_limits_pineview_lvds;
602
			limit = &intel_limits_pineview_lvds;
603
		else
603
		else
604
			limit = &intel_limits_pineview_sdvo;
604
			limit = &intel_limits_pineview_sdvo;
605
	} else if (IS_VALLEYVIEW(dev)) {
605
	} else if (IS_VALLEYVIEW(dev)) {
606
		if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG))
606
		if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG))
607
			limit = &intel_limits_vlv_dac;
607
			limit = &intel_limits_vlv_dac;
608
		else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
608
		else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
609
			limit = &intel_limits_vlv_hdmi;
609
			limit = &intel_limits_vlv_hdmi;
610
		else
610
		else
611
			limit = &intel_limits_vlv_dp;
611
			limit = &intel_limits_vlv_dp;
612
	} else if (!IS_GEN2(dev)) {
612
	} else if (!IS_GEN2(dev)) {
613
		if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
613
		if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
614
			limit = &intel_limits_i9xx_lvds;
614
			limit = &intel_limits_i9xx_lvds;
615
		else
615
		else
616
			limit = &intel_limits_i9xx_sdvo;
616
			limit = &intel_limits_i9xx_sdvo;
617
	} else {
617
	} else {
618
		if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
618
		if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
619
			limit = &intel_limits_i8xx_lvds;
619
			limit = &intel_limits_i8xx_lvds;
620
		else
620
		else
621
			limit = &intel_limits_i8xx_dvo;
621
			limit = &intel_limits_i8xx_dvo;
622
	}
622
	}
623
	return limit;
623
	return limit;
624
}
624
}
625
 
625
 
626
/* m1 is reserved as 0 in Pineview, n is a ring counter */
626
/* m1 is reserved as 0 in Pineview, n is a ring counter */
627
static void pineview_clock(int refclk, intel_clock_t *clock)
627
static void pineview_clock(int refclk, intel_clock_t *clock)
628
{
628
{
629
	clock->m = clock->m2 + 2;
629
	clock->m = clock->m2 + 2;
630
	clock->p = clock->p1 * clock->p2;
630
	clock->p = clock->p1 * clock->p2;
631
	clock->vco = refclk * clock->m / clock->n;
631
	clock->vco = refclk * clock->m / clock->n;
632
	clock->dot = clock->vco / clock->p;
632
	clock->dot = clock->vco / clock->p;
633
}
633
}
634
 
634
 
635
static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
635
static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
636
{
636
{
637
	if (IS_PINEVIEW(dev)) {
637
	if (IS_PINEVIEW(dev)) {
638
		pineview_clock(refclk, clock);
638
		pineview_clock(refclk, clock);
639
		return;
639
		return;
640
	}
640
	}
641
	clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
641
	clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
642
	clock->p = clock->p1 * clock->p2;
642
	clock->p = clock->p1 * clock->p2;
643
	clock->vco = refclk * clock->m / (clock->n + 2);
643
	clock->vco = refclk * clock->m / (clock->n + 2);
644
	clock->dot = clock->vco / clock->p;
644
	clock->dot = clock->vco / clock->p;
645
}
645
}
646
 
646
 
647
/**
647
/**
648
 * Returns whether any output on the specified pipe is of the specified type
648
 * Returns whether any output on the specified pipe is of the specified type
649
 */
649
 */
650
bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
650
bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
651
{
651
{
652
	struct drm_device *dev = crtc->dev;
652
	struct drm_device *dev = crtc->dev;
653
	struct intel_encoder *encoder;
653
	struct intel_encoder *encoder;
654
 
654
 
655
	for_each_encoder_on_crtc(dev, crtc, encoder)
655
	for_each_encoder_on_crtc(dev, crtc, encoder)
656
		if (encoder->type == type)
656
		if (encoder->type == type)
657
			return true;
657
			return true;
658
 
658
 
659
	return false;
659
	return false;
660
}
660
}
661
 
661
 
662
#define INTELPllInvalid(s)   do { /* DRM_DEBUG(s); */ return false; } while (0)
662
#define INTELPllInvalid(s)   do { /* DRM_DEBUG(s); */ return false; } while (0)
663
/**
663
/**
664
 * Returns whether the given set of divisors are valid for a given refclk with
664
 * Returns whether the given set of divisors are valid for a given refclk with
665
 * the given connectors.
665
 * the given connectors.
666
 */
666
 */
667
 
667
 
668
static bool intel_PLL_is_valid(struct drm_device *dev,
668
static bool intel_PLL_is_valid(struct drm_device *dev,
669
			       const intel_limit_t *limit,
669
			       const intel_limit_t *limit,
670
			       const intel_clock_t *clock)
670
			       const intel_clock_t *clock)
671
{
671
{
672
	if (clock->p1  < limit->p1.min  || limit->p1.max  < clock->p1)
672
	if (clock->p1  < limit->p1.min  || limit->p1.max  < clock->p1)
673
		INTELPllInvalid("p1 out of range\n");
673
		INTELPllInvalid("p1 out of range\n");
674
	if (clock->p   < limit->p.min   || limit->p.max   < clock->p)
674
	if (clock->p   < limit->p.min   || limit->p.max   < clock->p)
675
		INTELPllInvalid("p out of range\n");
675
		INTELPllInvalid("p out of range\n");
676
	if (clock->m2  < limit->m2.min  || limit->m2.max  < clock->m2)
676
	if (clock->m2  < limit->m2.min  || limit->m2.max  < clock->m2)
677
		INTELPllInvalid("m2 out of range\n");
677
		INTELPllInvalid("m2 out of range\n");
678
	if (clock->m1  < limit->m1.min  || limit->m1.max  < clock->m1)
678
	if (clock->m1  < limit->m1.min  || limit->m1.max  < clock->m1)
679
		INTELPllInvalid("m1 out of range\n");
679
		INTELPllInvalid("m1 out of range\n");
680
	if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
680
	if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
681
		INTELPllInvalid("m1 <= m2\n");
681
		INTELPllInvalid("m1 <= m2\n");
682
	if (clock->m   < limit->m.min   || limit->m.max   < clock->m)
682
	if (clock->m   < limit->m.min   || limit->m.max   < clock->m)
683
		INTELPllInvalid("m out of range\n");
683
		INTELPllInvalid("m out of range\n");
684
	if (clock->n   < limit->n.min   || limit->n.max   < clock->n)
684
	if (clock->n   < limit->n.min   || limit->n.max   < clock->n)
685
		INTELPllInvalid("n out of range\n");
685
		INTELPllInvalid("n out of range\n");
686
	if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
686
	if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
687
		INTELPllInvalid("vco out of range\n");
687
		INTELPllInvalid("vco out of range\n");
688
	/* XXX: We may need to be checking "Dot clock" depending on the multiplier,
688
	/* XXX: We may need to be checking "Dot clock" depending on the multiplier,
689
	 * connector, etc., rather than just a single range.
689
	 * connector, etc., rather than just a single range.
690
	 */
690
	 */
691
	if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
691
	if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
692
		INTELPllInvalid("dot out of range\n");
692
		INTELPllInvalid("dot out of range\n");
693
 
693
 
694
	return true;
694
	return true;
695
}
695
}
696
 
696
 
697
static bool
697
static bool
698
intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
698
intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
699
		    int target, int refclk, intel_clock_t *match_clock,
699
		    int target, int refclk, intel_clock_t *match_clock,
700
		    intel_clock_t *best_clock)
700
		    intel_clock_t *best_clock)
701
 
701
 
702
{
702
{
703
	struct drm_device *dev = crtc->dev;
703
	struct drm_device *dev = crtc->dev;
704
	struct drm_i915_private *dev_priv = dev->dev_private;
704
	struct drm_i915_private *dev_priv = dev->dev_private;
705
	intel_clock_t clock;
705
	intel_clock_t clock;
706
	int err = target;
706
	int err = target;
707
 
707
 
708
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
708
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
709
	    (I915_READ(LVDS)) != 0) {
709
	    (I915_READ(LVDS)) != 0) {
710
		/*
710
		/*
711
		 * For LVDS, if the panel is on, just rely on its current
711
		 * For LVDS, if the panel is on, just rely on its current
712
		 * settings for dual-channel.  We haven't figured out how to
712
		 * settings for dual-channel.  We haven't figured out how to
713
		 * reliably set up different single/dual channel state, if we
713
		 * reliably set up different single/dual channel state, if we
714
		 * even can.
714
		 * even can.
715
		 */
715
		 */
716
		if (is_dual_link_lvds(dev_priv, LVDS))
716
		if (is_dual_link_lvds(dev_priv, LVDS))
717
			clock.p2 = limit->p2.p2_fast;
717
			clock.p2 = limit->p2.p2_fast;
718
		else
718
		else
719
			clock.p2 = limit->p2.p2_slow;
719
			clock.p2 = limit->p2.p2_slow;
720
	} else {
720
	} else {
721
		if (target < limit->p2.dot_limit)
721
		if (target < limit->p2.dot_limit)
722
			clock.p2 = limit->p2.p2_slow;
722
			clock.p2 = limit->p2.p2_slow;
723
		else
723
		else
724
			clock.p2 = limit->p2.p2_fast;
724
			clock.p2 = limit->p2.p2_fast;
725
	}
725
	}
726
 
726
 
727
	memset(best_clock, 0, sizeof(*best_clock));
727
	memset(best_clock, 0, sizeof(*best_clock));
728
 
728
 
729
	for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
729
	for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
730
	     clock.m1++) {
730
	     clock.m1++) {
731
		for (clock.m2 = limit->m2.min;
731
		for (clock.m2 = limit->m2.min;
732
		     clock.m2 <= limit->m2.max; clock.m2++) {
732
		     clock.m2 <= limit->m2.max; clock.m2++) {
733
			/* m1 is always 0 in Pineview */
733
			/* m1 is always 0 in Pineview */
734
			if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
734
			if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
735
				break;
735
				break;
736
			for (clock.n = limit->n.min;
736
			for (clock.n = limit->n.min;
737
			     clock.n <= limit->n.max; clock.n++) {
737
			     clock.n <= limit->n.max; clock.n++) {
738
				for (clock.p1 = limit->p1.min;
738
				for (clock.p1 = limit->p1.min;
739
					clock.p1 <= limit->p1.max; clock.p1++) {
739
					clock.p1 <= limit->p1.max; clock.p1++) {
740
					int this_err;
740
					int this_err;
741
 
741
 
742
					intel_clock(dev, refclk, &clock);
742
					intel_clock(dev, refclk, &clock);
743
					if (!intel_PLL_is_valid(dev, limit,
743
					if (!intel_PLL_is_valid(dev, limit,
744
								&clock))
744
								&clock))
745
						continue;
745
						continue;
746
					if (match_clock &&
746
					if (match_clock &&
747
					    clock.p != match_clock->p)
747
					    clock.p != match_clock->p)
748
						continue;
748
						continue;
749
 
749
 
750
					this_err = abs(clock.dot - target);
750
					this_err = abs(clock.dot - target);
751
					if (this_err < err) {
751
					if (this_err < err) {
752
						*best_clock = clock;
752
						*best_clock = clock;
753
						err = this_err;
753
						err = this_err;
754
					}
754
					}
755
				}
755
				}
756
			}
756
			}
757
		}
757
		}
758
	}
758
	}
759
 
759
 
760
	return (err != target);
760
	return (err != target);
761
}
761
}
762
 
762
 
763
static bool
763
static bool
764
intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
764
intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
765
			int target, int refclk, intel_clock_t *match_clock,
765
			int target, int refclk, intel_clock_t *match_clock,
766
			intel_clock_t *best_clock)
766
			intel_clock_t *best_clock)
767
{
767
{
768
	struct drm_device *dev = crtc->dev;
768
	struct drm_device *dev = crtc->dev;
769
	struct drm_i915_private *dev_priv = dev->dev_private;
769
	struct drm_i915_private *dev_priv = dev->dev_private;
770
	intel_clock_t clock;
770
	intel_clock_t clock;
771
	int max_n;
771
	int max_n;
772
	bool found;
772
	bool found;
773
	/* approximately equals target * 0.00585 */
773
	/* approximately equals target * 0.00585 */
774
	int err_most = (target >> 8) + (target >> 9);
774
	int err_most = (target >> 8) + (target >> 9);
775
	found = false;
775
	found = false;
776
 
776
 
777
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
777
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
778
		int lvds_reg;
778
		int lvds_reg;
779
 
779
 
780
		if (HAS_PCH_SPLIT(dev))
780
		if (HAS_PCH_SPLIT(dev))
781
			lvds_reg = PCH_LVDS;
781
			lvds_reg = PCH_LVDS;
782
		else
782
		else
783
			lvds_reg = LVDS;
783
			lvds_reg = LVDS;
784
		if ((I915_READ(lvds_reg) & LVDS_CLKB_POWER_MASK) ==
784
		if ((I915_READ(lvds_reg) & LVDS_CLKB_POWER_MASK) ==
785
		    LVDS_CLKB_POWER_UP)
785
		    LVDS_CLKB_POWER_UP)
786
			clock.p2 = limit->p2.p2_fast;
786
			clock.p2 = limit->p2.p2_fast;
787
		else
787
		else
788
			clock.p2 = limit->p2.p2_slow;
788
			clock.p2 = limit->p2.p2_slow;
789
	} else {
789
	} else {
790
		if (target < limit->p2.dot_limit)
790
		if (target < limit->p2.dot_limit)
791
			clock.p2 = limit->p2.p2_slow;
791
			clock.p2 = limit->p2.p2_slow;
792
		else
792
		else
793
			clock.p2 = limit->p2.p2_fast;
793
			clock.p2 = limit->p2.p2_fast;
794
	}
794
	}
795
 
795
 
796
	memset(best_clock, 0, sizeof(*best_clock));
796
	memset(best_clock, 0, sizeof(*best_clock));
797
	max_n = limit->n.max;
797
	max_n = limit->n.max;
798
	/* based on hardware requirement, prefer smaller n to precision */
798
	/* based on hardware requirement, prefer smaller n to precision */
799
	for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
799
	for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
800
		/* based on hardware requirement, prefere larger m1,m2 */
800
		/* based on hardware requirement, prefere larger m1,m2 */
801
		for (clock.m1 = limit->m1.max;
801
		for (clock.m1 = limit->m1.max;
802
		     clock.m1 >= limit->m1.min; clock.m1--) {
802
		     clock.m1 >= limit->m1.min; clock.m1--) {
803
			for (clock.m2 = limit->m2.max;
803
			for (clock.m2 = limit->m2.max;
804
			     clock.m2 >= limit->m2.min; clock.m2--) {
804
			     clock.m2 >= limit->m2.min; clock.m2--) {
805
				for (clock.p1 = limit->p1.max;
805
				for (clock.p1 = limit->p1.max;
806
				     clock.p1 >= limit->p1.min; clock.p1--) {
806
				     clock.p1 >= limit->p1.min; clock.p1--) {
807
					int this_err;
807
					int this_err;
808
 
808
 
809
					intel_clock(dev, refclk, &clock);
809
					intel_clock(dev, refclk, &clock);
810
					if (!intel_PLL_is_valid(dev, limit,
810
					if (!intel_PLL_is_valid(dev, limit,
811
								&clock))
811
								&clock))
812
						continue;
812
						continue;
813
					if (match_clock &&
813
					if (match_clock &&
814
					    clock.p != match_clock->p)
814
					    clock.p != match_clock->p)
815
						continue;
815
						continue;
816
 
816
 
817
					this_err = abs(clock.dot - target);
817
					this_err = abs(clock.dot - target);
818
					if (this_err < err_most) {
818
					if (this_err < err_most) {
819
						*best_clock = clock;
819
						*best_clock = clock;
820
						err_most = this_err;
820
						err_most = this_err;
821
						max_n = clock.n;
821
						max_n = clock.n;
822
						found = true;
822
						found = true;
823
					}
823
					}
824
				}
824
				}
825
			}
825
			}
826
		}
826
		}
827
	}
827
	}
828
	return found;
828
	return found;
829
}
829
}
830
 
830
 
831
static bool
831
static bool
832
intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
832
intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
833
			   int target, int refclk, intel_clock_t *match_clock,
833
			   int target, int refclk, intel_clock_t *match_clock,
834
			   intel_clock_t *best_clock)
834
			   intel_clock_t *best_clock)
835
{
835
{
836
	struct drm_device *dev = crtc->dev;
836
	struct drm_device *dev = crtc->dev;
837
	intel_clock_t clock;
837
	intel_clock_t clock;
838
 
838
 
839
	if (target < 200000) {
839
	if (target < 200000) {
840
		clock.n = 1;
840
		clock.n = 1;
841
		clock.p1 = 2;
841
		clock.p1 = 2;
842
		clock.p2 = 10;
842
		clock.p2 = 10;
843
		clock.m1 = 12;
843
		clock.m1 = 12;
844
		clock.m2 = 9;
844
		clock.m2 = 9;
845
	} else {
845
	} else {
846
		clock.n = 2;
846
		clock.n = 2;
847
		clock.p1 = 1;
847
		clock.p1 = 1;
848
		clock.p2 = 10;
848
		clock.p2 = 10;
849
		clock.m1 = 14;
849
		clock.m1 = 14;
850
		clock.m2 = 8;
850
		clock.m2 = 8;
851
	}
851
	}
852
	intel_clock(dev, refclk, &clock);
852
	intel_clock(dev, refclk, &clock);
853
	memcpy(best_clock, &clock, sizeof(intel_clock_t));
853
	memcpy(best_clock, &clock, sizeof(intel_clock_t));
854
	return true;
854
	return true;
855
}
855
}
856
 
856
 
857
/* DisplayPort has only two frequencies, 162MHz and 270MHz */
857
/* DisplayPort has only two frequencies, 162MHz and 270MHz */
858
static bool
858
static bool
859
intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
859
intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
860
		      int target, int refclk, intel_clock_t *match_clock,
860
		      int target, int refclk, intel_clock_t *match_clock,
861
		      intel_clock_t *best_clock)
861
		      intel_clock_t *best_clock)
862
{
862
{
863
	intel_clock_t clock;
863
	intel_clock_t clock;
864
	if (target < 200000) {
864
	if (target < 200000) {
865
		clock.p1 = 2;
865
		clock.p1 = 2;
866
		clock.p2 = 10;
866
		clock.p2 = 10;
867
		clock.n = 2;
867
		clock.n = 2;
868
		clock.m1 = 23;
868
		clock.m1 = 23;
869
		clock.m2 = 8;
869
		clock.m2 = 8;
870
	} else {
870
	} else {
871
		clock.p1 = 1;
871
		clock.p1 = 1;
872
		clock.p2 = 10;
872
		clock.p2 = 10;
873
		clock.n = 1;
873
		clock.n = 1;
874
		clock.m1 = 14;
874
		clock.m1 = 14;
875
		clock.m2 = 2;
875
		clock.m2 = 2;
876
	}
876
	}
877
	clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
877
	clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
878
	clock.p = (clock.p1 * clock.p2);
878
	clock.p = (clock.p1 * clock.p2);
879
	clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
879
	clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
880
	clock.vco = 0;
880
	clock.vco = 0;
881
	memcpy(best_clock, &clock, sizeof(intel_clock_t));
881
	memcpy(best_clock, &clock, sizeof(intel_clock_t));
882
	return true;
882
	return true;
883
}
883
}
884
static bool
884
static bool
885
intel_vlv_find_best_pll(const intel_limit_t *limit, struct drm_crtc *crtc,
885
intel_vlv_find_best_pll(const intel_limit_t *limit, struct drm_crtc *crtc,
886
			int target, int refclk, intel_clock_t *match_clock,
886
			int target, int refclk, intel_clock_t *match_clock,
887
			intel_clock_t *best_clock)
887
			intel_clock_t *best_clock)
888
{
888
{
889
	u32 p1, p2, m1, m2, vco, bestn, bestm1, bestm2, bestp1, bestp2;
889
	u32 p1, p2, m1, m2, vco, bestn, bestm1, bestm2, bestp1, bestp2;
890
	u32 m, n, fastclk;
890
	u32 m, n, fastclk;
891
	u32 updrate, minupdate, fracbits, p;
891
	u32 updrate, minupdate, fracbits, p;
892
	unsigned long bestppm, ppm, absppm;
892
	unsigned long bestppm, ppm, absppm;
893
	int dotclk, flag;
893
	int dotclk, flag;
894
 
894
 
895
	flag = 0;
895
	flag = 0;
896
	dotclk = target * 1000;
896
	dotclk = target * 1000;
897
	bestppm = 1000000;
897
	bestppm = 1000000;
898
	ppm = absppm = 0;
898
	ppm = absppm = 0;
899
	fastclk = dotclk / (2*100);
899
	fastclk = dotclk / (2*100);
900
	updrate = 0;
900
	updrate = 0;
901
	minupdate = 19200;
901
	minupdate = 19200;
902
	fracbits = 1;
902
	fracbits = 1;
903
	n = p = p1 = p2 = m = m1 = m2 = vco = bestn = 0;
903
	n = p = p1 = p2 = m = m1 = m2 = vco = bestn = 0;
904
	bestm1 = bestm2 = bestp1 = bestp2 = 0;
904
	bestm1 = bestm2 = bestp1 = bestp2 = 0;
905
 
905
 
906
	/* based on hardware requirement, prefer smaller n to precision */
906
	/* based on hardware requirement, prefer smaller n to precision */
907
	for (n = limit->n.min; n <= ((refclk) / minupdate); n++) {
907
	for (n = limit->n.min; n <= ((refclk) / minupdate); n++) {
908
		updrate = refclk / n;
908
		updrate = refclk / n;
909
		for (p1 = limit->p1.max; p1 > limit->p1.min; p1--) {
909
		for (p1 = limit->p1.max; p1 > limit->p1.min; p1--) {
910
			for (p2 = limit->p2.p2_fast+1; p2 > 0; p2--) {
910
			for (p2 = limit->p2.p2_fast+1; p2 > 0; p2--) {
911
				if (p2 > 10)
911
				if (p2 > 10)
912
					p2 = p2 - 1;
912
					p2 = p2 - 1;
913
				p = p1 * p2;
913
				p = p1 * p2;
914
				/* based on hardware requirement, prefer bigger m1,m2 values */
914
				/* based on hardware requirement, prefer bigger m1,m2 values */
915
				for (m1 = limit->m1.min; m1 <= limit->m1.max; m1++) {
915
				for (m1 = limit->m1.min; m1 <= limit->m1.max; m1++) {
916
					m2 = (((2*(fastclk * p * n / m1 )) +
916
					m2 = (((2*(fastclk * p * n / m1 )) +
917
					       refclk) / (2*refclk));
917
					       refclk) / (2*refclk));
918
					m = m1 * m2;
918
					m = m1 * m2;
919
					vco = updrate * m;
919
					vco = updrate * m;
920
					if (vco >= limit->vco.min && vco < limit->vco.max) {
920
					if (vco >= limit->vco.min && vco < limit->vco.max) {
921
						ppm = 1000000 * ((vco / p) - fastclk) / fastclk;
921
						ppm = 1000000 * ((vco / p) - fastclk) / fastclk;
922
						absppm = (ppm > 0) ? ppm : (-ppm);
922
						absppm = (ppm > 0) ? ppm : (-ppm);
923
						if (absppm < 100 && ((p1 * p2) > (bestp1 * bestp2))) {
923
						if (absppm < 100 && ((p1 * p2) > (bestp1 * bestp2))) {
924
							bestppm = 0;
924
							bestppm = 0;
925
							flag = 1;
925
							flag = 1;
926
						}
926
						}
927
						if (absppm < bestppm - 10) {
927
						if (absppm < bestppm - 10) {
928
							bestppm = absppm;
928
							bestppm = absppm;
929
							flag = 1;
929
							flag = 1;
930
						}
930
						}
931
						if (flag) {
931
						if (flag) {
932
							bestn = n;
932
							bestn = n;
933
							bestm1 = m1;
933
							bestm1 = m1;
934
							bestm2 = m2;
934
							bestm2 = m2;
935
							bestp1 = p1;
935
							bestp1 = p1;
936
							bestp2 = p2;
936
							bestp2 = p2;
937
							flag = 0;
937
							flag = 0;
938
						}
938
						}
939
					}
939
					}
940
				}
940
				}
941
			}
941
			}
942
		}
942
		}
943
	}
943
	}
944
	best_clock->n = bestn;
944
	best_clock->n = bestn;
945
	best_clock->m1 = bestm1;
945
	best_clock->m1 = bestm1;
946
	best_clock->m2 = bestm2;
946
	best_clock->m2 = bestm2;
947
	best_clock->p1 = bestp1;
947
	best_clock->p1 = bestp1;
948
	best_clock->p2 = bestp2;
948
	best_clock->p2 = bestp2;
949
 
949
 
950
	return true;
950
	return true;
951
}
951
}
952
 
952
 
953
static void ironlake_wait_for_vblank(struct drm_device *dev, int pipe)
953
static void ironlake_wait_for_vblank(struct drm_device *dev, int pipe)
954
{
954
{
955
	struct drm_i915_private *dev_priv = dev->dev_private;
955
	struct drm_i915_private *dev_priv = dev->dev_private;
956
	u32 frame, frame_reg = PIPEFRAME(pipe);
956
	u32 frame, frame_reg = PIPEFRAME(pipe);
957
 
957
 
958
	frame = I915_READ(frame_reg);
958
	frame = I915_READ(frame_reg);
959
 
959
 
960
	if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
960
	if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
961
		DRM_DEBUG_KMS("vblank wait timed out\n");
961
		DRM_DEBUG_KMS("vblank wait timed out\n");
962
}
962
}
963
 
963
 
964
/**
964
/**
965
 * intel_wait_for_vblank - wait for vblank on a given pipe
965
 * intel_wait_for_vblank - wait for vblank on a given pipe
966
 * @dev: drm device
966
 * @dev: drm device
967
 * @pipe: pipe to wait for
967
 * @pipe: pipe to wait for
968
 *
968
 *
969
 * Wait for vblank to occur on a given pipe.  Needed for various bits of
969
 * Wait for vblank to occur on a given pipe.  Needed for various bits of
970
 * mode setting code.
970
 * mode setting code.
971
 */
971
 */
972
void intel_wait_for_vblank(struct drm_device *dev, int pipe)
972
void intel_wait_for_vblank(struct drm_device *dev, int pipe)
973
{
973
{
974
	struct drm_i915_private *dev_priv = dev->dev_private;
974
	struct drm_i915_private *dev_priv = dev->dev_private;
975
	int pipestat_reg = PIPESTAT(pipe);
975
	int pipestat_reg = PIPESTAT(pipe);
976
 
976
 
977
	if (INTEL_INFO(dev)->gen >= 5) {
977
	if (INTEL_INFO(dev)->gen >= 5) {
978
		ironlake_wait_for_vblank(dev, pipe);
978
		ironlake_wait_for_vblank(dev, pipe);
979
		return;
979
		return;
980
	}
980
	}
981
 
981
 
982
	/* Clear existing vblank status. Note this will clear any other
982
	/* Clear existing vblank status. Note this will clear any other
983
	 * sticky status fields as well.
983
	 * sticky status fields as well.
984
	 *
984
	 *
985
	 * This races with i915_driver_irq_handler() with the result
985
	 * This races with i915_driver_irq_handler() with the result
986
	 * that either function could miss a vblank event.  Here it is not
986
	 * that either function could miss a vblank event.  Here it is not
987
	 * fatal, as we will either wait upon the next vblank interrupt or
987
	 * fatal, as we will either wait upon the next vblank interrupt or
988
	 * timeout.  Generally speaking intel_wait_for_vblank() is only
988
	 * timeout.  Generally speaking intel_wait_for_vblank() is only
989
	 * called during modeset at which time the GPU should be idle and
989
	 * called during modeset at which time the GPU should be idle and
990
	 * should *not* be performing page flips and thus not waiting on
990
	 * should *not* be performing page flips and thus not waiting on
991
	 * vblanks...
991
	 * vblanks...
992
	 * Currently, the result of us stealing a vblank from the irq
992
	 * Currently, the result of us stealing a vblank from the irq
993
	 * handler is that a single frame will be skipped during swapbuffers.
993
	 * handler is that a single frame will be skipped during swapbuffers.
994
	 */
994
	 */
995
	I915_WRITE(pipestat_reg,
995
	I915_WRITE(pipestat_reg,
996
		   I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
996
		   I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
997
 
997
 
998
	/* Wait for vblank interrupt bit to set */
998
	/* Wait for vblank interrupt bit to set */
999
	if (wait_for(I915_READ(pipestat_reg) &
999
	if (wait_for(I915_READ(pipestat_reg) &
1000
		     PIPE_VBLANK_INTERRUPT_STATUS,
1000
		     PIPE_VBLANK_INTERRUPT_STATUS,
1001
		     50))
1001
		     50))
1002
		DRM_DEBUG_KMS("vblank wait timed out\n");
1002
		DRM_DEBUG_KMS("vblank wait timed out\n");
1003
}
1003
}
1004
 
1004
 
1005
/*
1005
/*
1006
 * intel_wait_for_pipe_off - wait for pipe to turn off
1006
 * intel_wait_for_pipe_off - wait for pipe to turn off
1007
 * @dev: drm device
1007
 * @dev: drm device
1008
 * @pipe: pipe to wait for
1008
 * @pipe: pipe to wait for
1009
 *
1009
 *
1010
 * After disabling a pipe, we can't wait for vblank in the usual way,
1010
 * After disabling a pipe, we can't wait for vblank in the usual way,
1011
 * spinning on the vblank interrupt status bit, since we won't actually
1011
 * spinning on the vblank interrupt status bit, since we won't actually
1012
 * see an interrupt when the pipe is disabled.
1012
 * see an interrupt when the pipe is disabled.
1013
 *
1013
 *
1014
 * On Gen4 and above:
1014
 * On Gen4 and above:
1015
 *   wait for the pipe register state bit to turn off
1015
 *   wait for the pipe register state bit to turn off
1016
 *
1016
 *
1017
 * Otherwise:
1017
 * Otherwise:
1018
 *   wait for the display line value to settle (it usually
1018
 *   wait for the display line value to settle (it usually
1019
 *   ends up stopping at the start of the next frame).
1019
 *   ends up stopping at the start of the next frame).
1020
 *
1020
 *
1021
 */
1021
 */
1022
void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
1022
void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
1023
{
1023
{
1024
	struct drm_i915_private *dev_priv = dev->dev_private;
1024
	struct drm_i915_private *dev_priv = dev->dev_private;
1025
 
1025
 
1026
	if (INTEL_INFO(dev)->gen >= 4) {
1026
	if (INTEL_INFO(dev)->gen >= 4) {
1027
		int reg = PIPECONF(pipe);
1027
		int reg = PIPECONF(pipe);
1028
 
1028
 
1029
		/* Wait for the Pipe State to go off */
1029
		/* Wait for the Pipe State to go off */
1030
		if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1030
		if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1031
			     100))
1031
			     100))
1032
			WARN(1, "pipe_off wait timed out\n");
1032
			WARN(1, "pipe_off wait timed out\n");
1033
	} else {
1033
	} else {
1034
		u32 last_line, line_mask;
1034
		u32 last_line, line_mask;
1035
		int reg = PIPEDSL(pipe);
1035
		int reg = PIPEDSL(pipe);
1036
        unsigned long timeout = GetTimerTicks() + msecs_to_jiffies(100);
1036
        unsigned long timeout = GetTimerTicks() + msecs_to_jiffies(100);
1037
 
1037
 
1038
		if (IS_GEN2(dev))
1038
		if (IS_GEN2(dev))
1039
			line_mask = DSL_LINEMASK_GEN2;
1039
			line_mask = DSL_LINEMASK_GEN2;
1040
		else
1040
		else
1041
			line_mask = DSL_LINEMASK_GEN3;
1041
			line_mask = DSL_LINEMASK_GEN3;
1042
 
1042
 
1043
		/* Wait for the display line to settle */
1043
		/* Wait for the display line to settle */
1044
		do {
1044
		do {
1045
			last_line = I915_READ(reg) & line_mask;
1045
			last_line = I915_READ(reg) & line_mask;
1046
			mdelay(5);
1046
			mdelay(5);
1047
		} while (((I915_READ(reg) & line_mask) != last_line) &&
1047
		} while (((I915_READ(reg) & line_mask) != last_line) &&
1048
			 time_after(timeout, GetTimerTicks()));
1048
			 time_after(timeout, GetTimerTicks()));
1049
		if (time_after(GetTimerTicks(), timeout))
1049
		if (time_after(GetTimerTicks(), timeout))
1050
			WARN(1, "pipe_off wait timed out\n");
1050
			WARN(1, "pipe_off wait timed out\n");
1051
	}
1051
	}
1052
}
1052
}
1053
 
1053
 
1054
static const char *state_string(bool enabled)
1054
static const char *state_string(bool enabled)
1055
{
1055
{
1056
	return enabled ? "on" : "off";
1056
	return enabled ? "on" : "off";
1057
}
1057
}
1058
 
1058
 
1059
/* Only for pre-ILK configs */
1059
/* Only for pre-ILK configs */
1060
static void assert_pll(struct drm_i915_private *dev_priv,
1060
static void assert_pll(struct drm_i915_private *dev_priv,
1061
		       enum pipe pipe, bool state)
1061
		       enum pipe pipe, bool state)
1062
{
1062
{
1063
	int reg;
1063
	int reg;
1064
	u32 val;
1064
	u32 val;
1065
	bool cur_state;
1065
	bool cur_state;
1066
 
1066
 
1067
	reg = DPLL(pipe);
1067
	reg = DPLL(pipe);
1068
	val = I915_READ(reg);
1068
	val = I915_READ(reg);
1069
	cur_state = !!(val & DPLL_VCO_ENABLE);
1069
	cur_state = !!(val & DPLL_VCO_ENABLE);
1070
	WARN(cur_state != state,
1070
	WARN(cur_state != state,
1071
	     "PLL state assertion failure (expected %s, current %s)\n",
1071
	     "PLL state assertion failure (expected %s, current %s)\n",
1072
	     state_string(state), state_string(cur_state));
1072
	     state_string(state), state_string(cur_state));
1073
}
1073
}
1074
#define assert_pll_enabled(d, p) assert_pll(d, p, true)
1074
#define assert_pll_enabled(d, p) assert_pll(d, p, true)
1075
#define assert_pll_disabled(d, p) assert_pll(d, p, false)
1075
#define assert_pll_disabled(d, p) assert_pll(d, p, false)
1076
 
1076
 
1077
/* For ILK+ */
1077
/* For ILK+ */
1078
static void assert_pch_pll(struct drm_i915_private *dev_priv,
1078
static void assert_pch_pll(struct drm_i915_private *dev_priv,
1079
			   struct intel_pch_pll *pll,
1079
			   struct intel_pch_pll *pll,
1080
			   struct intel_crtc *crtc,
1080
			   struct intel_crtc *crtc,
1081
			   bool state)
1081
			   bool state)
1082
{
1082
{
1083
	u32 val;
1083
	u32 val;
1084
	bool cur_state;
1084
	bool cur_state;
1085
 
1085
 
1086
	if (HAS_PCH_LPT(dev_priv->dev)) {
1086
	if (HAS_PCH_LPT(dev_priv->dev)) {
1087
		DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
1087
		DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
1088
		return;
1088
		return;
1089
	}
1089
	}
1090
 
1090
 
1091
	if (WARN (!pll,
1091
	if (WARN (!pll,
1092
		  "asserting PCH PLL %s with no PLL\n", state_string(state)))
1092
		  "asserting PCH PLL %s with no PLL\n", state_string(state)))
1093
		return;
1093
		return;
1094
 
1094
 
1095
	val = I915_READ(pll->pll_reg);
1095
	val = I915_READ(pll->pll_reg);
1096
	cur_state = !!(val & DPLL_VCO_ENABLE);
1096
	cur_state = !!(val & DPLL_VCO_ENABLE);
1097
	WARN(cur_state != state,
1097
	WARN(cur_state != state,
1098
	     "PCH PLL state for reg %x assertion failure (expected %s, current %s), val=%08x\n",
1098
	     "PCH PLL state for reg %x assertion failure (expected %s, current %s), val=%08x\n",
1099
	     pll->pll_reg, state_string(state), state_string(cur_state), val);
1099
	     pll->pll_reg, state_string(state), state_string(cur_state), val);
1100
 
1100
 
1101
	/* Make sure the selected PLL is correctly attached to the transcoder */
1101
	/* Make sure the selected PLL is correctly attached to the transcoder */
1102
	if (crtc && HAS_PCH_CPT(dev_priv->dev)) {
1102
	if (crtc && HAS_PCH_CPT(dev_priv->dev)) {
1103
		u32 pch_dpll;
1103
		u32 pch_dpll;
1104
 
1104
 
1105
		pch_dpll = I915_READ(PCH_DPLL_SEL);
1105
		pch_dpll = I915_READ(PCH_DPLL_SEL);
1106
		cur_state = pll->pll_reg == _PCH_DPLL_B;
1106
		cur_state = pll->pll_reg == _PCH_DPLL_B;
1107
		if (!WARN(((pch_dpll >> (4 * crtc->pipe)) & 1) != cur_state,
1107
		if (!WARN(((pch_dpll >> (4 * crtc->pipe)) & 1) != cur_state,
1108
			  "PLL[%d] not attached to this transcoder %d: %08x\n",
1108
			  "PLL[%d] not attached to this transcoder %d: %08x\n",
1109
			  cur_state, crtc->pipe, pch_dpll)) {
1109
			  cur_state, crtc->pipe, pch_dpll)) {
1110
			cur_state = !!(val >> (4*crtc->pipe + 3));
1110
			cur_state = !!(val >> (4*crtc->pipe + 3));
1111
	WARN(cur_state != state,
1111
	WARN(cur_state != state,
1112
			     "PLL[%d] not %s on this transcoder %d: %08x\n",
1112
			     "PLL[%d] not %s on this transcoder %d: %08x\n",
1113
			     pll->pll_reg == _PCH_DPLL_B,
1113
			     pll->pll_reg == _PCH_DPLL_B,
1114
			     state_string(state),
1114
			     state_string(state),
1115
			     crtc->pipe,
1115
			     crtc->pipe,
1116
			     val);
1116
			     val);
1117
		}
1117
		}
1118
	}
1118
	}
1119
}
1119
}
1120
#define assert_pch_pll_enabled(d, p, c) assert_pch_pll(d, p, c, true)
1120
#define assert_pch_pll_enabled(d, p, c) assert_pch_pll(d, p, c, true)
1121
#define assert_pch_pll_disabled(d, p, c) assert_pch_pll(d, p, c, false)
1121
#define assert_pch_pll_disabled(d, p, c) assert_pch_pll(d, p, c, false)
1122
 
1122
 
1123
static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1123
static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1124
			  enum pipe pipe, bool state)
1124
			  enum pipe pipe, bool state)
1125
{
1125
{
1126
	int reg;
1126
	int reg;
1127
	u32 val;
1127
	u32 val;
1128
	bool cur_state;
1128
	bool cur_state;
1129
 
1129
 
1130
	if (IS_HASWELL(dev_priv->dev)) {
1130
	if (IS_HASWELL(dev_priv->dev)) {
1131
		/* On Haswell, DDI is used instead of FDI_TX_CTL */
1131
		/* On Haswell, DDI is used instead of FDI_TX_CTL */
1132
		reg = DDI_FUNC_CTL(pipe);
1132
		reg = DDI_FUNC_CTL(pipe);
1133
		val = I915_READ(reg);
1133
		val = I915_READ(reg);
1134
		cur_state = !!(val & PIPE_DDI_FUNC_ENABLE);
1134
		cur_state = !!(val & PIPE_DDI_FUNC_ENABLE);
1135
	} else {
1135
	} else {
1136
	reg = FDI_TX_CTL(pipe);
1136
	reg = FDI_TX_CTL(pipe);
1137
	val = I915_READ(reg);
1137
	val = I915_READ(reg);
1138
	cur_state = !!(val & FDI_TX_ENABLE);
1138
	cur_state = !!(val & FDI_TX_ENABLE);
1139
	}
1139
	}
1140
	WARN(cur_state != state,
1140
	WARN(cur_state != state,
1141
	     "FDI TX state assertion failure (expected %s, current %s)\n",
1141
	     "FDI TX state assertion failure (expected %s, current %s)\n",
1142
	     state_string(state), state_string(cur_state));
1142
	     state_string(state), state_string(cur_state));
1143
}
1143
}
1144
#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1144
#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1145
#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1145
#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1146
 
1146
 
1147
static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1147
static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1148
			  enum pipe pipe, bool state)
1148
			  enum pipe pipe, bool state)
1149
{
1149
{
1150
	int reg;
1150
	int reg;
1151
	u32 val;
1151
	u32 val;
1152
	bool cur_state;
1152
	bool cur_state;
1153
 
1153
 
1154
	if (IS_HASWELL(dev_priv->dev) && pipe > 0) {
1154
	if (IS_HASWELL(dev_priv->dev) && pipe > 0) {
1155
			DRM_ERROR("Attempting to enable FDI_RX on Haswell pipe > 0\n");
1155
			DRM_ERROR("Attempting to enable FDI_RX on Haswell pipe > 0\n");
1156
			return;
1156
			return;
1157
	} else {
1157
	} else {
1158
	reg = FDI_RX_CTL(pipe);
1158
	reg = FDI_RX_CTL(pipe);
1159
	val = I915_READ(reg);
1159
	val = I915_READ(reg);
1160
	cur_state = !!(val & FDI_RX_ENABLE);
1160
	cur_state = !!(val & FDI_RX_ENABLE);
1161
	}
1161
	}
1162
	WARN(cur_state != state,
1162
	WARN(cur_state != state,
1163
	     "FDI RX state assertion failure (expected %s, current %s)\n",
1163
	     "FDI RX state assertion failure (expected %s, current %s)\n",
1164
	     state_string(state), state_string(cur_state));
1164
	     state_string(state), state_string(cur_state));
1165
}
1165
}
1166
#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1166
#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1167
#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1167
#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1168
 
1168
 
1169
static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1169
static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1170
				      enum pipe pipe)
1170
				      enum pipe pipe)
1171
{
1171
{
1172
	int reg;
1172
	int reg;
1173
	u32 val;
1173
	u32 val;
1174
 
1174
 
1175
	/* ILK FDI PLL is always enabled */
1175
	/* ILK FDI PLL is always enabled */
1176
	if (dev_priv->info->gen == 5)
1176
	if (dev_priv->info->gen == 5)
1177
		return;
1177
		return;
1178
 
1178
 
1179
	/* On Haswell, DDI ports are responsible for the FDI PLL setup */
1179
	/* On Haswell, DDI ports are responsible for the FDI PLL setup */
1180
	if (IS_HASWELL(dev_priv->dev))
1180
	if (IS_HASWELL(dev_priv->dev))
1181
		return;
1181
		return;
1182
 
1182
 
1183
	reg = FDI_TX_CTL(pipe);
1183
	reg = FDI_TX_CTL(pipe);
1184
	val = I915_READ(reg);
1184
	val = I915_READ(reg);
1185
	WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1185
	WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1186
}
1186
}
1187
 
1187
 
1188
static void assert_fdi_rx_pll_enabled(struct drm_i915_private *dev_priv,
1188
static void assert_fdi_rx_pll_enabled(struct drm_i915_private *dev_priv,
1189
				      enum pipe pipe)
1189
				      enum pipe pipe)
1190
{
1190
{
1191
	int reg;
1191
	int reg;
1192
	u32 val;
1192
	u32 val;
1193
 
1193
 
1194
	if (IS_HASWELL(dev_priv->dev) && pipe > 0) {
1194
	if (IS_HASWELL(dev_priv->dev) && pipe > 0) {
1195
		DRM_ERROR("Attempting to enable FDI on Haswell with pipe > 0\n");
1195
		DRM_ERROR("Attempting to enable FDI on Haswell with pipe > 0\n");
1196
		return;
1196
		return;
1197
	}
1197
	}
1198
	reg = FDI_RX_CTL(pipe);
1198
	reg = FDI_RX_CTL(pipe);
1199
	val = I915_READ(reg);
1199
	val = I915_READ(reg);
1200
	WARN(!(val & FDI_RX_PLL_ENABLE), "FDI RX PLL assertion failure, should be active but is disabled\n");
1200
	WARN(!(val & FDI_RX_PLL_ENABLE), "FDI RX PLL assertion failure, should be active but is disabled\n");
1201
}
1201
}
1202
 
1202
 
1203
static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1203
static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1204
				  enum pipe pipe)
1204
				  enum pipe pipe)
1205
{
1205
{
1206
	int pp_reg, lvds_reg;
1206
	int pp_reg, lvds_reg;
1207
	u32 val;
1207
	u32 val;
1208
	enum pipe panel_pipe = PIPE_A;
1208
	enum pipe panel_pipe = PIPE_A;
1209
	bool locked = true;
1209
	bool locked = true;
1210
 
1210
 
1211
	if (HAS_PCH_SPLIT(dev_priv->dev)) {
1211
	if (HAS_PCH_SPLIT(dev_priv->dev)) {
1212
		pp_reg = PCH_PP_CONTROL;
1212
		pp_reg = PCH_PP_CONTROL;
1213
		lvds_reg = PCH_LVDS;
1213
		lvds_reg = PCH_LVDS;
1214
	} else {
1214
	} else {
1215
		pp_reg = PP_CONTROL;
1215
		pp_reg = PP_CONTROL;
1216
		lvds_reg = LVDS;
1216
		lvds_reg = LVDS;
1217
	}
1217
	}
1218
 
1218
 
1219
	val = I915_READ(pp_reg);
1219
	val = I915_READ(pp_reg);
1220
	if (!(val & PANEL_POWER_ON) ||
1220
	if (!(val & PANEL_POWER_ON) ||
1221
	    ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1221
	    ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1222
		locked = false;
1222
		locked = false;
1223
 
1223
 
1224
	if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1224
	if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1225
		panel_pipe = PIPE_B;
1225
		panel_pipe = PIPE_B;
1226
 
1226
 
1227
	WARN(panel_pipe == pipe && locked,
1227
	WARN(panel_pipe == pipe && locked,
1228
	     "panel assertion failure, pipe %c regs locked\n",
1228
	     "panel assertion failure, pipe %c regs locked\n",
1229
	     pipe_name(pipe));
1229
	     pipe_name(pipe));
1230
}
1230
}
1231
 
1231
 
1232
void assert_pipe(struct drm_i915_private *dev_priv,
1232
void assert_pipe(struct drm_i915_private *dev_priv,
1233
			enum pipe pipe, bool state)
1233
			enum pipe pipe, bool state)
1234
{
1234
{
1235
	int reg;
1235
	int reg;
1236
	u32 val;
1236
	u32 val;
1237
	bool cur_state;
1237
	bool cur_state;
1238
 
1238
 
1239
	/* if we need the pipe A quirk it must be always on */
1239
	/* if we need the pipe A quirk it must be always on */
1240
	if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1240
	if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1241
		state = true;
1241
		state = true;
1242
 
1242
 
1243
	reg = PIPECONF(pipe);
1243
	reg = PIPECONF(pipe);
1244
	val = I915_READ(reg);
1244
	val = I915_READ(reg);
1245
	cur_state = !!(val & PIPECONF_ENABLE);
1245
	cur_state = !!(val & PIPECONF_ENABLE);
1246
	WARN(cur_state != state,
1246
	WARN(cur_state != state,
1247
	     "pipe %c assertion failure (expected %s, current %s)\n",
1247
	     "pipe %c assertion failure (expected %s, current %s)\n",
1248
	     pipe_name(pipe), state_string(state), state_string(cur_state));
1248
	     pipe_name(pipe), state_string(state), state_string(cur_state));
1249
}
1249
}
1250
 
1250
 
1251
static void assert_plane(struct drm_i915_private *dev_priv,
1251
static void assert_plane(struct drm_i915_private *dev_priv,
1252
			 enum plane plane, bool state)
1252
			 enum plane plane, bool state)
1253
{
1253
{
1254
	int reg;
1254
	int reg;
1255
	u32 val;
1255
	u32 val;
1256
	bool cur_state;
1256
	bool cur_state;
1257
 
1257
 
1258
	reg = DSPCNTR(plane);
1258
	reg = DSPCNTR(plane);
1259
	val = I915_READ(reg);
1259
	val = I915_READ(reg);
1260
	cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1260
	cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1261
	WARN(cur_state != state,
1261
	WARN(cur_state != state,
1262
	     "plane %c assertion failure (expected %s, current %s)\n",
1262
	     "plane %c assertion failure (expected %s, current %s)\n",
1263
	     plane_name(plane), state_string(state), state_string(cur_state));
1263
	     plane_name(plane), state_string(state), state_string(cur_state));
1264
}
1264
}
1265
 
1265
 
1266
#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1266
#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1267
#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1267
#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1268
 
1268
 
1269
static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1269
static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1270
				   enum pipe pipe)
1270
				   enum pipe pipe)
1271
{
1271
{
1272
	int reg, i;
1272
	int reg, i;
1273
	u32 val;
1273
	u32 val;
1274
	int cur_pipe;
1274
	int cur_pipe;
1275
 
1275
 
1276
	/* Planes are fixed to pipes on ILK+ */
1276
	/* Planes are fixed to pipes on ILK+ */
1277
	if (HAS_PCH_SPLIT(dev_priv->dev)) {
1277
	if (HAS_PCH_SPLIT(dev_priv->dev)) {
1278
		reg = DSPCNTR(pipe);
1278
		reg = DSPCNTR(pipe);
1279
		val = I915_READ(reg);
1279
		val = I915_READ(reg);
1280
		WARN((val & DISPLAY_PLANE_ENABLE),
1280
		WARN((val & DISPLAY_PLANE_ENABLE),
1281
		     "plane %c assertion failure, should be disabled but not\n",
1281
		     "plane %c assertion failure, should be disabled but not\n",
1282
		     plane_name(pipe));
1282
		     plane_name(pipe));
1283
		return;
1283
		return;
1284
	}
1284
	}
1285
 
1285
 
1286
	/* Need to check both planes against the pipe */
1286
	/* Need to check both planes against the pipe */
1287
	for (i = 0; i < 2; i++) {
1287
	for (i = 0; i < 2; i++) {
1288
		reg = DSPCNTR(i);
1288
		reg = DSPCNTR(i);
1289
		val = I915_READ(reg);
1289
		val = I915_READ(reg);
1290
		cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1290
		cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1291
			DISPPLANE_SEL_PIPE_SHIFT;
1291
			DISPPLANE_SEL_PIPE_SHIFT;
1292
		WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
1292
		WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
1293
		     "plane %c assertion failure, should be off on pipe %c but is still active\n",
1293
		     "plane %c assertion failure, should be off on pipe %c but is still active\n",
1294
		     plane_name(i), pipe_name(pipe));
1294
		     plane_name(i), pipe_name(pipe));
1295
	}
1295
	}
1296
}
1296
}
1297
 
1297
 
1298
static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1298
static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1299
{
1299
{
1300
	u32 val;
1300
	u32 val;
1301
	bool enabled;
1301
	bool enabled;
1302
 
1302
 
1303
	if (HAS_PCH_LPT(dev_priv->dev)) {
1303
	if (HAS_PCH_LPT(dev_priv->dev)) {
1304
		DRM_DEBUG_DRIVER("LPT does not has PCH refclk, skipping check\n");
1304
		DRM_DEBUG_DRIVER("LPT does not has PCH refclk, skipping check\n");
1305
		return;
1305
		return;
1306
	}
1306
	}
1307
 
1307
 
1308
	val = I915_READ(PCH_DREF_CONTROL);
1308
	val = I915_READ(PCH_DREF_CONTROL);
1309
	enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1309
	enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1310
			    DREF_SUPERSPREAD_SOURCE_MASK));
1310
			    DREF_SUPERSPREAD_SOURCE_MASK));
1311
	WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1311
	WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1312
}
1312
}
1313
 
1313
 
1314
static void assert_transcoder_disabled(struct drm_i915_private *dev_priv,
1314
static void assert_transcoder_disabled(struct drm_i915_private *dev_priv,
1315
				       enum pipe pipe)
1315
				       enum pipe pipe)
1316
{
1316
{
1317
	int reg;
1317
	int reg;
1318
	u32 val;
1318
	u32 val;
1319
	bool enabled;
1319
	bool enabled;
1320
 
1320
 
1321
	reg = TRANSCONF(pipe);
1321
	reg = TRANSCONF(pipe);
1322
	val = I915_READ(reg);
1322
	val = I915_READ(reg);
1323
	enabled = !!(val & TRANS_ENABLE);
1323
	enabled = !!(val & TRANS_ENABLE);
1324
	WARN(enabled,
1324
	WARN(enabled,
1325
	     "transcoder assertion failed, should be off on pipe %c but is still active\n",
1325
	     "transcoder assertion failed, should be off on pipe %c but is still active\n",
1326
	     pipe_name(pipe));
1326
	     pipe_name(pipe));
1327
}
1327
}
1328
 
1328
 
1329
static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1329
static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1330
			    enum pipe pipe, u32 port_sel, u32 val)
1330
			    enum pipe pipe, u32 port_sel, u32 val)
1331
{
1331
{
1332
	if ((val & DP_PORT_EN) == 0)
1332
	if ((val & DP_PORT_EN) == 0)
1333
		return false;
1333
		return false;
1334
 
1334
 
1335
	if (HAS_PCH_CPT(dev_priv->dev)) {
1335
	if (HAS_PCH_CPT(dev_priv->dev)) {
1336
		u32	trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1336
		u32	trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1337
		u32	trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1337
		u32	trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1338
		if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1338
		if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1339
			return false;
1339
			return false;
1340
	} else {
1340
	} else {
1341
		if ((val & DP_PIPE_MASK) != (pipe << 30))
1341
		if ((val & DP_PIPE_MASK) != (pipe << 30))
1342
			return false;
1342
			return false;
1343
	}
1343
	}
1344
	return true;
1344
	return true;
1345
}
1345
}
1346
 
1346
 
1347
static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1347
static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1348
			      enum pipe pipe, u32 val)
1348
			      enum pipe pipe, u32 val)
1349
{
1349
{
1350
	if ((val & PORT_ENABLE) == 0)
1350
	if ((val & PORT_ENABLE) == 0)
1351
		return false;
1351
		return false;
1352
 
1352
 
1353
	if (HAS_PCH_CPT(dev_priv->dev)) {
1353
	if (HAS_PCH_CPT(dev_priv->dev)) {
1354
		if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1354
		if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1355
			return false;
1355
			return false;
1356
	} else {
1356
	} else {
1357
		if ((val & TRANSCODER_MASK) != TRANSCODER(pipe))
1357
		if ((val & TRANSCODER_MASK) != TRANSCODER(pipe))
1358
			return false;
1358
			return false;
1359
	}
1359
	}
1360
	return true;
1360
	return true;
1361
}
1361
}
1362
 
1362
 
1363
static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1363
static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1364
			      enum pipe pipe, u32 val)
1364
			      enum pipe pipe, u32 val)
1365
{
1365
{
1366
	if ((val & LVDS_PORT_EN) == 0)
1366
	if ((val & LVDS_PORT_EN) == 0)
1367
		return false;
1367
		return false;
1368
 
1368
 
1369
	if (HAS_PCH_CPT(dev_priv->dev)) {
1369
	if (HAS_PCH_CPT(dev_priv->dev)) {
1370
		if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1370
		if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1371
			return false;
1371
			return false;
1372
	} else {
1372
	} else {
1373
		if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1373
		if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1374
			return false;
1374
			return false;
1375
	}
1375
	}
1376
	return true;
1376
	return true;
1377
}
1377
}
1378
 
1378
 
1379
static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1379
static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1380
			      enum pipe pipe, u32 val)
1380
			      enum pipe pipe, u32 val)
1381
{
1381
{
1382
	if ((val & ADPA_DAC_ENABLE) == 0)
1382
	if ((val & ADPA_DAC_ENABLE) == 0)
1383
		return false;
1383
		return false;
1384
	if (HAS_PCH_CPT(dev_priv->dev)) {
1384
	if (HAS_PCH_CPT(dev_priv->dev)) {
1385
		if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1385
		if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1386
			return false;
1386
			return false;
1387
	} else {
1387
	} else {
1388
		if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1388
		if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1389
			return false;
1389
			return false;
1390
	}
1390
	}
1391
	return true;
1391
	return true;
1392
}
1392
}
1393
 
1393
 
1394
static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
1394
static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
1395
				   enum pipe pipe, int reg, u32 port_sel)
1395
				   enum pipe pipe, int reg, u32 port_sel)
1396
{
1396
{
1397
	u32 val = I915_READ(reg);
1397
	u32 val = I915_READ(reg);
1398
	WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
1398
	WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
1399
	     "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
1399
	     "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
1400
	     reg, pipe_name(pipe));
1400
	     reg, pipe_name(pipe));
1401
 
1401
 
1402
	WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1402
	WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1403
	     && (val & DP_PIPEB_SELECT),
1403
	     && (val & DP_PIPEB_SELECT),
1404
	     "IBX PCH dp port still using transcoder B\n");
1404
	     "IBX PCH dp port still using transcoder B\n");
1405
}
1405
}
1406
 
1406
 
1407
static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1407
static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1408
				     enum pipe pipe, int reg)
1408
				     enum pipe pipe, int reg)
1409
{
1409
{
1410
	u32 val = I915_READ(reg);
1410
	u32 val = I915_READ(reg);
1411
	WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
1411
	WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
1412
	     "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
1412
	     "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
1413
	     reg, pipe_name(pipe));
1413
	     reg, pipe_name(pipe));
1414
 
1414
 
1415
	WARN(HAS_PCH_IBX(dev_priv->dev) && (val & PORT_ENABLE) == 0
1415
	WARN(HAS_PCH_IBX(dev_priv->dev) && (val & PORT_ENABLE) == 0
1416
	     && (val & SDVO_PIPE_B_SELECT),
1416
	     && (val & SDVO_PIPE_B_SELECT),
1417
	     "IBX PCH hdmi port still using transcoder B\n");
1417
	     "IBX PCH hdmi port still using transcoder B\n");
1418
}
1418
}
1419
 
1419
 
1420
static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1420
static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1421
				      enum pipe pipe)
1421
				      enum pipe pipe)
1422
{
1422
{
1423
	int reg;
1423
	int reg;
1424
	u32 val;
1424
	u32 val;
1425
 
1425
 
1426
	assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1426
	assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1427
	assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1427
	assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1428
	assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
1428
	assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
1429
 
1429
 
1430
	reg = PCH_ADPA;
1430
	reg = PCH_ADPA;
1431
	val = I915_READ(reg);
1431
	val = I915_READ(reg);
1432
	WARN(adpa_pipe_enabled(dev_priv, pipe, val),
1432
	WARN(adpa_pipe_enabled(dev_priv, pipe, val),
1433
	     "PCH VGA enabled on transcoder %c, should be disabled\n",
1433
	     "PCH VGA enabled on transcoder %c, should be disabled\n",
1434
	     pipe_name(pipe));
1434
	     pipe_name(pipe));
1435
 
1435
 
1436
	reg = PCH_LVDS;
1436
	reg = PCH_LVDS;
1437
	val = I915_READ(reg);
1437
	val = I915_READ(reg);
1438
	WARN(lvds_pipe_enabled(dev_priv, pipe, val),
1438
	WARN(lvds_pipe_enabled(dev_priv, pipe, val),
1439
	     "PCH LVDS enabled on transcoder %c, should be disabled\n",
1439
	     "PCH LVDS enabled on transcoder %c, should be disabled\n",
1440
	     pipe_name(pipe));
1440
	     pipe_name(pipe));
1441
 
1441
 
1442
	assert_pch_hdmi_disabled(dev_priv, pipe, HDMIB);
1442
	assert_pch_hdmi_disabled(dev_priv, pipe, HDMIB);
1443
	assert_pch_hdmi_disabled(dev_priv, pipe, HDMIC);
1443
	assert_pch_hdmi_disabled(dev_priv, pipe, HDMIC);
1444
	assert_pch_hdmi_disabled(dev_priv, pipe, HDMID);
1444
	assert_pch_hdmi_disabled(dev_priv, pipe, HDMID);
1445
}
1445
}
1446
 
1446
 
1447
/**
1447
/**
1448
 * intel_enable_pll - enable a PLL
1448
 * intel_enable_pll - enable a PLL
1449
 * @dev_priv: i915 private structure
1449
 * @dev_priv: i915 private structure
1450
 * @pipe: pipe PLL to enable
1450
 * @pipe: pipe PLL to enable
1451
 *
1451
 *
1452
 * Enable @pipe's PLL so we can start pumping pixels from a plane.  Check to
1452
 * Enable @pipe's PLL so we can start pumping pixels from a plane.  Check to
1453
 * make sure the PLL reg is writable first though, since the panel write
1453
 * make sure the PLL reg is writable first though, since the panel write
1454
 * protect mechanism may be enabled.
1454
 * protect mechanism may be enabled.
1455
 *
1455
 *
1456
 * Note!  This is for pre-ILK only.
1456
 * Note!  This is for pre-ILK only.
1457
 *
1457
 *
1458
 * Unfortunately needed by dvo_ns2501 since the dvo depends on it running.
1458
 * Unfortunately needed by dvo_ns2501 since the dvo depends on it running.
1459
 */
1459
 */
1460
static void intel_enable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1460
static void intel_enable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1461
{
1461
{
1462
    int reg;
1462
    int reg;
1463
    u32 val;
1463
    u32 val;
1464
 
1464
 
1465
    /* No really, not for ILK+ */
1465
    /* No really, not for ILK+ */
1466
	BUG_ON(!IS_VALLEYVIEW(dev_priv->dev) && dev_priv->info->gen >= 5);
1466
	BUG_ON(!IS_VALLEYVIEW(dev_priv->dev) && dev_priv->info->gen >= 5);
1467
 
1467
 
1468
    /* PLL is protected by panel, make sure we can write it */
1468
    /* PLL is protected by panel, make sure we can write it */
1469
    if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
1469
    if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
1470
        assert_panel_unlocked(dev_priv, pipe);
1470
        assert_panel_unlocked(dev_priv, pipe);
1471
 
1471
 
1472
    reg = DPLL(pipe);
1472
    reg = DPLL(pipe);
1473
    val = I915_READ(reg);
1473
    val = I915_READ(reg);
1474
    val |= DPLL_VCO_ENABLE;
1474
    val |= DPLL_VCO_ENABLE;
1475
 
1475
 
1476
    /* We do this three times for luck */
1476
    /* We do this three times for luck */
1477
    I915_WRITE(reg, val);
1477
    I915_WRITE(reg, val);
1478
    POSTING_READ(reg);
1478
    POSTING_READ(reg);
1479
    udelay(150); /* wait for warmup */
1479
    udelay(150); /* wait for warmup */
1480
    I915_WRITE(reg, val);
1480
    I915_WRITE(reg, val);
1481
    POSTING_READ(reg);
1481
    POSTING_READ(reg);
1482
    udelay(150); /* wait for warmup */
1482
    udelay(150); /* wait for warmup */
1483
    I915_WRITE(reg, val);
1483
    I915_WRITE(reg, val);
1484
    POSTING_READ(reg);
1484
    POSTING_READ(reg);
1485
    udelay(150); /* wait for warmup */
1485
    udelay(150); /* wait for warmup */
1486
}
1486
}
1487
 
1487
 
1488
/**
1488
/**
1489
 * intel_disable_pll - disable a PLL
1489
 * intel_disable_pll - disable a PLL
1490
 * @dev_priv: i915 private structure
1490
 * @dev_priv: i915 private structure
1491
 * @pipe: pipe PLL to disable
1491
 * @pipe: pipe PLL to disable
1492
 *
1492
 *
1493
 * Disable the PLL for @pipe, making sure the pipe is off first.
1493
 * Disable the PLL for @pipe, making sure the pipe is off first.
1494
 *
1494
 *
1495
 * Note!  This is for pre-ILK only.
1495
 * Note!  This is for pre-ILK only.
1496
 */
1496
 */
1497
static void intel_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1497
static void intel_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1498
{
1498
{
1499
	int reg;
1499
	int reg;
1500
	u32 val;
1500
	u32 val;
1501
 
1501
 
1502
	/* Don't disable pipe A or pipe A PLLs if needed */
1502
	/* Don't disable pipe A or pipe A PLLs if needed */
1503
	if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1503
	if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1504
		return;
1504
		return;
1505
 
1505
 
1506
	/* Make sure the pipe isn't still relying on us */
1506
	/* Make sure the pipe isn't still relying on us */
1507
	assert_pipe_disabled(dev_priv, pipe);
1507
	assert_pipe_disabled(dev_priv, pipe);
1508
 
1508
 
1509
	reg = DPLL(pipe);
1509
	reg = DPLL(pipe);
1510
	val = I915_READ(reg);
1510
	val = I915_READ(reg);
1511
	val &= ~DPLL_VCO_ENABLE;
1511
	val &= ~DPLL_VCO_ENABLE;
1512
	I915_WRITE(reg, val);
1512
	I915_WRITE(reg, val);
1513
	POSTING_READ(reg);
1513
	POSTING_READ(reg);
1514
}
1514
}
1515
 
1515
 
1516
/* SBI access */
1516
/* SBI access */
1517
static void
1517
static void
1518
intel_sbi_write(struct drm_i915_private *dev_priv, u16 reg, u32 value)
1518
intel_sbi_write(struct drm_i915_private *dev_priv, u16 reg, u32 value)
1519
{
1519
{
1520
	unsigned long flags;
1520
	unsigned long flags;
1521
 
1521
 
1522
	spin_lock_irqsave(&dev_priv->dpio_lock, flags);
1522
	spin_lock_irqsave(&dev_priv->dpio_lock, flags);
1523
	if (wait_for((I915_READ(SBI_CTL_STAT) & SBI_BUSY) == 0,
1523
	if (wait_for((I915_READ(SBI_CTL_STAT) & SBI_BUSY) == 0,
1524
				100)) {
1524
				100)) {
1525
		DRM_ERROR("timeout waiting for SBI to become ready\n");
1525
		DRM_ERROR("timeout waiting for SBI to become ready\n");
1526
		goto out_unlock;
1526
		goto out_unlock;
1527
	}
1527
	}
1528
 
1528
 
1529
	I915_WRITE(SBI_ADDR,
1529
	I915_WRITE(SBI_ADDR,
1530
			(reg << 16));
1530
			(reg << 16));
1531
	I915_WRITE(SBI_DATA,
1531
	I915_WRITE(SBI_DATA,
1532
			value);
1532
			value);
1533
	I915_WRITE(SBI_CTL_STAT,
1533
	I915_WRITE(SBI_CTL_STAT,
1534
			SBI_BUSY |
1534
			SBI_BUSY |
1535
			SBI_CTL_OP_CRWR);
1535
			SBI_CTL_OP_CRWR);
1536
 
1536
 
1537
	if (wait_for((I915_READ(SBI_CTL_STAT) & (SBI_BUSY | SBI_RESPONSE_FAIL)) == 0,
1537
	if (wait_for((I915_READ(SBI_CTL_STAT) & (SBI_BUSY | SBI_RESPONSE_FAIL)) == 0,
1538
				100)) {
1538
				100)) {
1539
		DRM_ERROR("timeout waiting for SBI to complete write transaction\n");
1539
		DRM_ERROR("timeout waiting for SBI to complete write transaction\n");
1540
		goto out_unlock;
1540
		goto out_unlock;
1541
	}
1541
	}
1542
 
1542
 
1543
out_unlock:
1543
out_unlock:
1544
	spin_unlock_irqrestore(&dev_priv->dpio_lock, flags);
1544
	spin_unlock_irqrestore(&dev_priv->dpio_lock, flags);
1545
}
1545
}
1546
 
1546
 
1547
static u32
1547
static u32
1548
intel_sbi_read(struct drm_i915_private *dev_priv, u16 reg)
1548
intel_sbi_read(struct drm_i915_private *dev_priv, u16 reg)
1549
{
1549
{
1550
	unsigned long flags;
1550
	unsigned long flags;
1551
	u32 value = 0;
1551
	u32 value = 0;
1552
 
1552
 
1553
	spin_lock_irqsave(&dev_priv->dpio_lock, flags);
1553
	spin_lock_irqsave(&dev_priv->dpio_lock, flags);
1554
	if (wait_for((I915_READ(SBI_CTL_STAT) & SBI_BUSY) == 0,
1554
	if (wait_for((I915_READ(SBI_CTL_STAT) & SBI_BUSY) == 0,
1555
				100)) {
1555
				100)) {
1556
		DRM_ERROR("timeout waiting for SBI to become ready\n");
1556
		DRM_ERROR("timeout waiting for SBI to become ready\n");
1557
		goto out_unlock;
1557
		goto out_unlock;
1558
	}
1558
	}
1559
 
1559
 
1560
	I915_WRITE(SBI_ADDR,
1560
	I915_WRITE(SBI_ADDR,
1561
			(reg << 16));
1561
			(reg << 16));
1562
	I915_WRITE(SBI_CTL_STAT,
1562
	I915_WRITE(SBI_CTL_STAT,
1563
			SBI_BUSY |
1563
			SBI_BUSY |
1564
			SBI_CTL_OP_CRRD);
1564
			SBI_CTL_OP_CRRD);
1565
 
1565
 
1566
	if (wait_for((I915_READ(SBI_CTL_STAT) & (SBI_BUSY | SBI_RESPONSE_FAIL)) == 0,
1566
	if (wait_for((I915_READ(SBI_CTL_STAT) & (SBI_BUSY | SBI_RESPONSE_FAIL)) == 0,
1567
				100)) {
1567
				100)) {
1568
		DRM_ERROR("timeout waiting for SBI to complete read transaction\n");
1568
		DRM_ERROR("timeout waiting for SBI to complete read transaction\n");
1569
		goto out_unlock;
1569
		goto out_unlock;
1570
	}
1570
	}
1571
 
1571
 
1572
	value = I915_READ(SBI_DATA);
1572
	value = I915_READ(SBI_DATA);
1573
 
1573
 
1574
out_unlock:
1574
out_unlock:
1575
	spin_unlock_irqrestore(&dev_priv->dpio_lock, flags);
1575
	spin_unlock_irqrestore(&dev_priv->dpio_lock, flags);
1576
	return value;
1576
	return value;
1577
}
1577
}
1578
 
1578
 
1579
/**
1579
/**
1580
 * intel_enable_pch_pll - enable PCH PLL
1580
 * intel_enable_pch_pll - enable PCH PLL
1581
 * @dev_priv: i915 private structure
1581
 * @dev_priv: i915 private structure
1582
 * @pipe: pipe PLL to enable
1582
 * @pipe: pipe PLL to enable
1583
 *
1583
 *
1584
 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1584
 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1585
 * drives the transcoder clock.
1585
 * drives the transcoder clock.
1586
 */
1586
 */
1587
static void intel_enable_pch_pll(struct intel_crtc *intel_crtc)
1587
static void intel_enable_pch_pll(struct intel_crtc *intel_crtc)
1588
{
1588
{
1589
	struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
1589
	struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
1590
	struct intel_pch_pll *pll;
1590
	struct intel_pch_pll *pll;
1591
	int reg;
1591
	int reg;
1592
	u32 val;
1592
	u32 val;
1593
 
1593
 
1594
	/* PCH PLLs only available on ILK, SNB and IVB */
1594
	/* PCH PLLs only available on ILK, SNB and IVB */
1595
	BUG_ON(dev_priv->info->gen < 5);
1595
	BUG_ON(dev_priv->info->gen < 5);
1596
	pll = intel_crtc->pch_pll;
1596
	pll = intel_crtc->pch_pll;
1597
	if (pll == NULL)
1597
	if (pll == NULL)
1598
		return;
1598
		return;
1599
 
1599
 
1600
	if (WARN_ON(pll->refcount == 0))
1600
	if (WARN_ON(pll->refcount == 0))
1601
		return;
1601
		return;
1602
 
1602
 
1603
	DRM_DEBUG_KMS("enable PCH PLL %x (active %d, on? %d)for crtc %d\n",
1603
	DRM_DEBUG_KMS("enable PCH PLL %x (active %d, on? %d)for crtc %d\n",
1604
		      pll->pll_reg, pll->active, pll->on,
1604
		      pll->pll_reg, pll->active, pll->on,
1605
		      intel_crtc->base.base.id);
1605
		      intel_crtc->base.base.id);
1606
 
1606
 
1607
	/* PCH refclock must be enabled first */
1607
	/* PCH refclock must be enabled first */
1608
	assert_pch_refclk_enabled(dev_priv);
1608
	assert_pch_refclk_enabled(dev_priv);
1609
 
1609
 
1610
	if (pll->active++ && pll->on) {
1610
	if (pll->active++ && pll->on) {
1611
		assert_pch_pll_enabled(dev_priv, pll, NULL);
1611
		assert_pch_pll_enabled(dev_priv, pll, NULL);
1612
		return;
1612
		return;
1613
	}
1613
	}
1614
 
1614
 
1615
	DRM_DEBUG_KMS("enabling PCH PLL %x\n", pll->pll_reg);
1615
	DRM_DEBUG_KMS("enabling PCH PLL %x\n", pll->pll_reg);
1616
 
1616
 
1617
	reg = pll->pll_reg;
1617
	reg = pll->pll_reg;
1618
	val = I915_READ(reg);
1618
	val = I915_READ(reg);
1619
	val |= DPLL_VCO_ENABLE;
1619
	val |= DPLL_VCO_ENABLE;
1620
	I915_WRITE(reg, val);
1620
	I915_WRITE(reg, val);
1621
	POSTING_READ(reg);
1621
	POSTING_READ(reg);
1622
	udelay(200);
1622
	udelay(200);
1623
 
1623
 
1624
	pll->on = true;
1624
	pll->on = true;
1625
}
1625
}
1626
 
1626
 
1627
static void intel_disable_pch_pll(struct intel_crtc *intel_crtc)
1627
static void intel_disable_pch_pll(struct intel_crtc *intel_crtc)
1628
{
1628
{
1629
	struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
1629
	struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
1630
	struct intel_pch_pll *pll = intel_crtc->pch_pll;
1630
	struct intel_pch_pll *pll = intel_crtc->pch_pll;
1631
	int reg;
1631
	int reg;
1632
	u32 val;
1632
	u32 val;
1633
 
1633
 
1634
	/* PCH only available on ILK+ */
1634
	/* PCH only available on ILK+ */
1635
	BUG_ON(dev_priv->info->gen < 5);
1635
	BUG_ON(dev_priv->info->gen < 5);
1636
	if (pll == NULL)
1636
	if (pll == NULL)
1637
	       return;
1637
	       return;
1638
 
1638
 
1639
	if (WARN_ON(pll->refcount == 0))
1639
	if (WARN_ON(pll->refcount == 0))
1640
		return;
1640
		return;
1641
 
1641
 
1642
	DRM_DEBUG_KMS("disable PCH PLL %x (active %d, on? %d) for crtc %d\n",
1642
	DRM_DEBUG_KMS("disable PCH PLL %x (active %d, on? %d) for crtc %d\n",
1643
		      pll->pll_reg, pll->active, pll->on,
1643
		      pll->pll_reg, pll->active, pll->on,
1644
		      intel_crtc->base.base.id);
1644
		      intel_crtc->base.base.id);
1645
 
1645
 
1646
	if (WARN_ON(pll->active == 0)) {
1646
	if (WARN_ON(pll->active == 0)) {
1647
		assert_pch_pll_disabled(dev_priv, pll, NULL);
1647
		assert_pch_pll_disabled(dev_priv, pll, NULL);
1648
		return;
1648
		return;
1649
	}
1649
	}
1650
 
1650
 
1651
	if (--pll->active) {
1651
	if (--pll->active) {
1652
		assert_pch_pll_enabled(dev_priv, pll, NULL);
1652
		assert_pch_pll_enabled(dev_priv, pll, NULL);
1653
		return;
1653
		return;
1654
	}
1654
	}
1655
 
1655
 
1656
	DRM_DEBUG_KMS("disabling PCH PLL %x\n", pll->pll_reg);
1656
	DRM_DEBUG_KMS("disabling PCH PLL %x\n", pll->pll_reg);
1657
 
1657
 
1658
	/* Make sure transcoder isn't still depending on us */
1658
	/* Make sure transcoder isn't still depending on us */
1659
	assert_transcoder_disabled(dev_priv, intel_crtc->pipe);
1659
	assert_transcoder_disabled(dev_priv, intel_crtc->pipe);
1660
 
1660
 
1661
	reg = pll->pll_reg;
1661
	reg = pll->pll_reg;
1662
	val = I915_READ(reg);
1662
	val = I915_READ(reg);
1663
	val &= ~DPLL_VCO_ENABLE;
1663
	val &= ~DPLL_VCO_ENABLE;
1664
	I915_WRITE(reg, val);
1664
	I915_WRITE(reg, val);
1665
	POSTING_READ(reg);
1665
	POSTING_READ(reg);
1666
	udelay(200);
1666
	udelay(200);
1667
 
1667
 
1668
	pll->on = false;
1668
	pll->on = false;
1669
}
1669
}
1670
 
1670
 
1671
static void intel_enable_transcoder(struct drm_i915_private *dev_priv,
1671
static void intel_enable_transcoder(struct drm_i915_private *dev_priv,
1672
				    enum pipe pipe)
1672
				    enum pipe pipe)
1673
{
1673
{
1674
	int reg;
1674
	int reg;
1675
	u32 val, pipeconf_val;
1675
	u32 val, pipeconf_val;
1676
	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1676
	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1677
 
1677
 
1678
	/* PCH only available on ILK+ */
1678
	/* PCH only available on ILK+ */
1679
	BUG_ON(dev_priv->info->gen < 5);
1679
	BUG_ON(dev_priv->info->gen < 5);
1680
 
1680
 
1681
	/* Make sure PCH DPLL is enabled */
1681
	/* Make sure PCH DPLL is enabled */
1682
	assert_pch_pll_enabled(dev_priv,
1682
	assert_pch_pll_enabled(dev_priv,
1683
			       to_intel_crtc(crtc)->pch_pll,
1683
			       to_intel_crtc(crtc)->pch_pll,
1684
			       to_intel_crtc(crtc));
1684
			       to_intel_crtc(crtc));
1685
 
1685
 
1686
	/* FDI must be feeding us bits for PCH ports */
1686
	/* FDI must be feeding us bits for PCH ports */
1687
	assert_fdi_tx_enabled(dev_priv, pipe);
1687
	assert_fdi_tx_enabled(dev_priv, pipe);
1688
	assert_fdi_rx_enabled(dev_priv, pipe);
1688
	assert_fdi_rx_enabled(dev_priv, pipe);
1689
 
1689
 
1690
	if (IS_HASWELL(dev_priv->dev) && pipe > 0) {
1690
	if (IS_HASWELL(dev_priv->dev) && pipe > 0) {
1691
		DRM_ERROR("Attempting to enable transcoder on Haswell with pipe > 0\n");
1691
		DRM_ERROR("Attempting to enable transcoder on Haswell with pipe > 0\n");
1692
		return;
1692
		return;
1693
	}
1693
	}
1694
	reg = TRANSCONF(pipe);
1694
	reg = TRANSCONF(pipe);
1695
	val = I915_READ(reg);
1695
	val = I915_READ(reg);
1696
	pipeconf_val = I915_READ(PIPECONF(pipe));
1696
	pipeconf_val = I915_READ(PIPECONF(pipe));
1697
 
1697
 
1698
	if (HAS_PCH_IBX(dev_priv->dev)) {
1698
	if (HAS_PCH_IBX(dev_priv->dev)) {
1699
		/*
1699
		/*
1700
		 * make the BPC in transcoder be consistent with
1700
		 * make the BPC in transcoder be consistent with
1701
		 * that in pipeconf reg.
1701
		 * that in pipeconf reg.
1702
		 */
1702
		 */
1703
		val &= ~PIPE_BPC_MASK;
1703
		val &= ~PIPE_BPC_MASK;
1704
		val |= pipeconf_val & PIPE_BPC_MASK;
1704
		val |= pipeconf_val & PIPE_BPC_MASK;
1705
	}
1705
	}
1706
 
1706
 
1707
	val &= ~TRANS_INTERLACE_MASK;
1707
	val &= ~TRANS_INTERLACE_MASK;
1708
	if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
1708
	if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
1709
		if (HAS_PCH_IBX(dev_priv->dev) &&
1709
		if (HAS_PCH_IBX(dev_priv->dev) &&
1710
		    intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1710
		    intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1711
			val |= TRANS_LEGACY_INTERLACED_ILK;
1711
			val |= TRANS_LEGACY_INTERLACED_ILK;
1712
		else
1712
		else
1713
			val |= TRANS_INTERLACED;
1713
			val |= TRANS_INTERLACED;
1714
	else
1714
	else
1715
		val |= TRANS_PROGRESSIVE;
1715
		val |= TRANS_PROGRESSIVE;
1716
 
1716
 
1717
	I915_WRITE(reg, val | TRANS_ENABLE);
1717
	I915_WRITE(reg, val | TRANS_ENABLE);
1718
	if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
1718
	if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
1719
		DRM_ERROR("failed to enable transcoder %d\n", pipe);
1719
		DRM_ERROR("failed to enable transcoder %d\n", pipe);
1720
}
1720
}
1721
 
1721
 
1722
static void intel_disable_transcoder(struct drm_i915_private *dev_priv,
1722
static void intel_disable_transcoder(struct drm_i915_private *dev_priv,
1723
				     enum pipe pipe)
1723
				     enum pipe pipe)
1724
{
1724
{
1725
	int reg;
1725
	int reg;
1726
	u32 val;
1726
	u32 val;
1727
 
1727
 
1728
	/* FDI relies on the transcoder */
1728
	/* FDI relies on the transcoder */
1729
	assert_fdi_tx_disabled(dev_priv, pipe);
1729
	assert_fdi_tx_disabled(dev_priv, pipe);
1730
	assert_fdi_rx_disabled(dev_priv, pipe);
1730
	assert_fdi_rx_disabled(dev_priv, pipe);
1731
 
1731
 
1732
	/* Ports must be off as well */
1732
	/* Ports must be off as well */
1733
	assert_pch_ports_disabled(dev_priv, pipe);
1733
	assert_pch_ports_disabled(dev_priv, pipe);
1734
 
1734
 
1735
	reg = TRANSCONF(pipe);
1735
	reg = TRANSCONF(pipe);
1736
	val = I915_READ(reg);
1736
	val = I915_READ(reg);
1737
	val &= ~TRANS_ENABLE;
1737
	val &= ~TRANS_ENABLE;
1738
	I915_WRITE(reg, val);
1738
	I915_WRITE(reg, val);
1739
	/* wait for PCH transcoder off, transcoder state */
1739
	/* wait for PCH transcoder off, transcoder state */
1740
	if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
1740
	if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
1741
		DRM_ERROR("failed to disable transcoder %d\n", pipe);
1741
		DRM_ERROR("failed to disable transcoder %d\n", pipe);
1742
}
1742
}
1743
 
1743
 
1744
/**
1744
/**
1745
 * intel_enable_pipe - enable a pipe, asserting requirements
1745
 * intel_enable_pipe - enable a pipe, asserting requirements
1746
 * @dev_priv: i915 private structure
1746
 * @dev_priv: i915 private structure
1747
 * @pipe: pipe to enable
1747
 * @pipe: pipe to enable
1748
 * @pch_port: on ILK+, is this pipe driving a PCH port or not
1748
 * @pch_port: on ILK+, is this pipe driving a PCH port or not
1749
 *
1749
 *
1750
 * Enable @pipe, making sure that various hardware specific requirements
1750
 * Enable @pipe, making sure that various hardware specific requirements
1751
 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1751
 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1752
 *
1752
 *
1753
 * @pipe should be %PIPE_A or %PIPE_B.
1753
 * @pipe should be %PIPE_A or %PIPE_B.
1754
 *
1754
 *
1755
 * Will wait until the pipe is actually running (i.e. first vblank) before
1755
 * Will wait until the pipe is actually running (i.e. first vblank) before
1756
 * returning.
1756
 * returning.
1757
 */
1757
 */
1758
static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
1758
static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
1759
			      bool pch_port)
1759
			      bool pch_port)
1760
{
1760
{
1761
	int reg;
1761
	int reg;
1762
	u32 val;
1762
	u32 val;
1763
 
1763
 
1764
	/*
1764
	/*
1765
	 * A pipe without a PLL won't actually be able to drive bits from
1765
	 * A pipe without a PLL won't actually be able to drive bits from
1766
	 * a plane.  On ILK+ the pipe PLLs are integrated, so we don't
1766
	 * a plane.  On ILK+ the pipe PLLs are integrated, so we don't
1767
	 * need the check.
1767
	 * need the check.
1768
	 */
1768
	 */
1769
	if (!HAS_PCH_SPLIT(dev_priv->dev))
1769
	if (!HAS_PCH_SPLIT(dev_priv->dev))
1770
		assert_pll_enabled(dev_priv, pipe);
1770
		assert_pll_enabled(dev_priv, pipe);
1771
	else {
1771
	else {
1772
		if (pch_port) {
1772
		if (pch_port) {
1773
			/* if driving the PCH, we need FDI enabled */
1773
			/* if driving the PCH, we need FDI enabled */
1774
			assert_fdi_rx_pll_enabled(dev_priv, pipe);
1774
			assert_fdi_rx_pll_enabled(dev_priv, pipe);
1775
			assert_fdi_tx_pll_enabled(dev_priv, pipe);
1775
			assert_fdi_tx_pll_enabled(dev_priv, pipe);
1776
		}
1776
		}
1777
		/* FIXME: assert CPU port conditions for SNB+ */
1777
		/* FIXME: assert CPU port conditions for SNB+ */
1778
	}
1778
	}
1779
 
1779
 
1780
	reg = PIPECONF(pipe);
1780
	reg = PIPECONF(pipe);
1781
	val = I915_READ(reg);
1781
	val = I915_READ(reg);
1782
	if (val & PIPECONF_ENABLE)
1782
	if (val & PIPECONF_ENABLE)
1783
		return;
1783
		return;
1784
 
1784
 
1785
	I915_WRITE(reg, val | PIPECONF_ENABLE);
1785
	I915_WRITE(reg, val | PIPECONF_ENABLE);
1786
	intel_wait_for_vblank(dev_priv->dev, pipe);
1786
	intel_wait_for_vblank(dev_priv->dev, pipe);
1787
}
1787
}
1788
 
1788
 
1789
/**
1789
/**
1790
 * intel_disable_pipe - disable a pipe, asserting requirements
1790
 * intel_disable_pipe - disable a pipe, asserting requirements
1791
 * @dev_priv: i915 private structure
1791
 * @dev_priv: i915 private structure
1792
 * @pipe: pipe to disable
1792
 * @pipe: pipe to disable
1793
 *
1793
 *
1794
 * Disable @pipe, making sure that various hardware specific requirements
1794
 * Disable @pipe, making sure that various hardware specific requirements
1795
 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1795
 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1796
 *
1796
 *
1797
 * @pipe should be %PIPE_A or %PIPE_B.
1797
 * @pipe should be %PIPE_A or %PIPE_B.
1798
 *
1798
 *
1799
 * Will wait until the pipe has shut down before returning.
1799
 * Will wait until the pipe has shut down before returning.
1800
 */
1800
 */
1801
static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1801
static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1802
			       enum pipe pipe)
1802
			       enum pipe pipe)
1803
{
1803
{
1804
	int reg;
1804
	int reg;
1805
	u32 val;
1805
	u32 val;
1806
 
1806
 
1807
    /*
1807
    /*
1808
	 * Make sure planes won't keep trying to pump pixels to us,
1808
	 * Make sure planes won't keep trying to pump pixels to us,
1809
	 * or we might hang the display.
1809
	 * or we might hang the display.
1810
	 */
1810
	 */
1811
	assert_planes_disabled(dev_priv, pipe);
1811
	assert_planes_disabled(dev_priv, pipe);
1812
 
1812
 
1813
	/* Don't disable pipe A or pipe A PLLs if needed */
1813
	/* Don't disable pipe A or pipe A PLLs if needed */
1814
	if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1814
	if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1815
		return;
1815
		return;
1816
 
1816
 
1817
	reg = PIPECONF(pipe);
1817
	reg = PIPECONF(pipe);
1818
	val = I915_READ(reg);
1818
	val = I915_READ(reg);
1819
	if ((val & PIPECONF_ENABLE) == 0)
1819
	if ((val & PIPECONF_ENABLE) == 0)
1820
		return;
1820
		return;
1821
 
1821
 
1822
	I915_WRITE(reg, val & ~PIPECONF_ENABLE);
1822
	I915_WRITE(reg, val & ~PIPECONF_ENABLE);
1823
	intel_wait_for_pipe_off(dev_priv->dev, pipe);
1823
	intel_wait_for_pipe_off(dev_priv->dev, pipe);
1824
}
1824
}
1825
 
1825
 
1826
/*
1826
/*
1827
 * Plane regs are double buffered, going from enabled->disabled needs a
1827
 * Plane regs are double buffered, going from enabled->disabled needs a
1828
 * trigger in order to latch.  The display address reg provides this.
1828
 * trigger in order to latch.  The display address reg provides this.
1829
 */
1829
 */
1830
void intel_flush_display_plane(struct drm_i915_private *dev_priv,
1830
void intel_flush_display_plane(struct drm_i915_private *dev_priv,
1831
				      enum plane plane)
1831
				      enum plane plane)
1832
{
1832
{
1833
	I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
1833
	I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
1834
	I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
1834
	I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
1835
}
1835
}
1836
 
1836
 
1837
/**
1837
/**
1838
 * intel_enable_plane - enable a display plane on a given pipe
1838
 * intel_enable_plane - enable a display plane on a given pipe
1839
 * @dev_priv: i915 private structure
1839
 * @dev_priv: i915 private structure
1840
 * @plane: plane to enable
1840
 * @plane: plane to enable
1841
 * @pipe: pipe being fed
1841
 * @pipe: pipe being fed
1842
 *
1842
 *
1843
 * Enable @plane on @pipe, making sure that @pipe is running first.
1843
 * Enable @plane on @pipe, making sure that @pipe is running first.
1844
 */
1844
 */
1845
static void intel_enable_plane(struct drm_i915_private *dev_priv,
1845
static void intel_enable_plane(struct drm_i915_private *dev_priv,
1846
			       enum plane plane, enum pipe pipe)
1846
			       enum plane plane, enum pipe pipe)
1847
{
1847
{
1848
	int reg;
1848
	int reg;
1849
	u32 val;
1849
	u32 val;
1850
 
1850
 
1851
	/* If the pipe isn't enabled, we can't pump pixels and may hang */
1851
	/* If the pipe isn't enabled, we can't pump pixels and may hang */
1852
	assert_pipe_enabled(dev_priv, pipe);
1852
	assert_pipe_enabled(dev_priv, pipe);
1853
 
1853
 
1854
	reg = DSPCNTR(plane);
1854
	reg = DSPCNTR(plane);
1855
	val = I915_READ(reg);
1855
	val = I915_READ(reg);
1856
	if (val & DISPLAY_PLANE_ENABLE)
1856
	if (val & DISPLAY_PLANE_ENABLE)
1857
		return;
1857
		return;
1858
 
1858
 
1859
	I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
1859
	I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
1860
	intel_flush_display_plane(dev_priv, plane);
1860
	intel_flush_display_plane(dev_priv, plane);
1861
	intel_wait_for_vblank(dev_priv->dev, pipe);
1861
	intel_wait_for_vblank(dev_priv->dev, pipe);
1862
}
1862
}
1863
 
1863
 
1864
/**
1864
/**
1865
 * intel_disable_plane - disable a display plane
1865
 * intel_disable_plane - disable a display plane
1866
 * @dev_priv: i915 private structure
1866
 * @dev_priv: i915 private structure
1867
 * @plane: plane to disable
1867
 * @plane: plane to disable
1868
 * @pipe: pipe consuming the data
1868
 * @pipe: pipe consuming the data
1869
 *
1869
 *
1870
 * Disable @plane; should be an independent operation.
1870
 * Disable @plane; should be an independent operation.
1871
 */
1871
 */
1872
static void intel_disable_plane(struct drm_i915_private *dev_priv,
1872
static void intel_disable_plane(struct drm_i915_private *dev_priv,
1873
				enum plane plane, enum pipe pipe)
1873
				enum plane plane, enum pipe pipe)
1874
{
1874
{
1875
	int reg;
1875
	int reg;
1876
	u32 val;
1876
	u32 val;
1877
 
1877
 
1878
	reg = DSPCNTR(plane);
1878
	reg = DSPCNTR(plane);
1879
	val = I915_READ(reg);
1879
	val = I915_READ(reg);
1880
	if ((val & DISPLAY_PLANE_ENABLE) == 0)
1880
	if ((val & DISPLAY_PLANE_ENABLE) == 0)
1881
		return;
1881
		return;
1882
 
1882
 
1883
	I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
1883
	I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
1884
	intel_flush_display_plane(dev_priv, plane);
1884
	intel_flush_display_plane(dev_priv, plane);
1885
    intel_wait_for_vblank(dev_priv->dev, pipe);
1885
    intel_wait_for_vblank(dev_priv->dev, pipe);
1886
}
1886
}
1887
 
1887
 
1888
int
1888
int
1889
intel_pin_and_fence_fb_obj(struct drm_device *dev,
1889
intel_pin_and_fence_fb_obj(struct drm_device *dev,
1890
			   struct drm_i915_gem_object *obj,
1890
			   struct drm_i915_gem_object *obj,
1891
			   struct intel_ring_buffer *pipelined)
1891
			   struct intel_ring_buffer *pipelined)
1892
{
1892
{
1893
	struct drm_i915_private *dev_priv = dev->dev_private;
1893
	struct drm_i915_private *dev_priv = dev->dev_private;
1894
	u32 alignment;
1894
	u32 alignment;
1895
	int ret;
1895
	int ret;
1896
 
1896
 
1897
	switch (obj->tiling_mode) {
1897
	switch (obj->tiling_mode) {
1898
	case I915_TILING_NONE:
1898
	case I915_TILING_NONE:
1899
		if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1899
		if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1900
			alignment = 128 * 1024;
1900
			alignment = 128 * 1024;
1901
		else if (INTEL_INFO(dev)->gen >= 4)
1901
		else if (INTEL_INFO(dev)->gen >= 4)
1902
			alignment = 4 * 1024;
1902
			alignment = 4 * 1024;
1903
		else
1903
		else
1904
			alignment = 64 * 1024;
1904
			alignment = 64 * 1024;
1905
		break;
1905
		break;
1906
	case I915_TILING_X:
1906
	case I915_TILING_X:
1907
		/* pin() will align the object as required by fence */
1907
		/* pin() will align the object as required by fence */
1908
		alignment = 0;
1908
		alignment = 0;
1909
		break;
1909
		break;
1910
	case I915_TILING_Y:
1910
	case I915_TILING_Y:
1911
		/* FIXME: Is this true? */
1911
		/* FIXME: Is this true? */
1912
		DRM_ERROR("Y tiled not allowed for scan out buffers\n");
1912
		DRM_ERROR("Y tiled not allowed for scan out buffers\n");
1913
		return -EINVAL;
1913
		return -EINVAL;
1914
	default:
1914
	default:
1915
		BUG();
1915
		BUG();
1916
	}
1916
	}
1917
 
1917
 
1918
	dev_priv->mm.interruptible = false;
1918
	dev_priv->mm.interruptible = false;
1919
	ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
1919
	ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
1920
	if (ret)
1920
	if (ret)
1921
		goto err_interruptible;
1921
		goto err_interruptible;
1922
 
1922
 
1923
	/* Install a fence for tiled scan-out. Pre-i965 always needs a
1923
	/* Install a fence for tiled scan-out. Pre-i965 always needs a
1924
	 * fence, whereas 965+ only requires a fence if using
1924
	 * fence, whereas 965+ only requires a fence if using
1925
	 * framebuffer compression.  For simplicity, we always install
1925
	 * framebuffer compression.  For simplicity, we always install
1926
	 * a fence as the cost is not that onerous.
1926
	 * a fence as the cost is not that onerous.
1927
	 */
1927
	 */
1928
//	if (obj->tiling_mode != I915_TILING_NONE) {
1928
//	if (obj->tiling_mode != I915_TILING_NONE) {
1929
//		ret = i915_gem_object_get_fence(obj, pipelined);
1929
//		ret = i915_gem_object_get_fence(obj, pipelined);
1930
//		if (ret)
1930
//		if (ret)
1931
//			goto err_unpin;
1931
//			goto err_unpin;
1932
//	}
1932
//	}
1933
 
1933
 
1934
	dev_priv->mm.interruptible = true;
1934
	dev_priv->mm.interruptible = true;
1935
	return 0;
1935
	return 0;
1936
 
1936
 
1937
err_unpin:
1937
err_unpin:
1938
	i915_gem_object_unpin(obj);
1938
	i915_gem_object_unpin(obj);
1939
err_interruptible:
1939
err_interruptible:
1940
	dev_priv->mm.interruptible = true;
1940
	dev_priv->mm.interruptible = true;
1941
	return ret;
1941
	return ret;
1942
}
1942
}
1943
 
1943
 
1944
void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
1944
void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
1945
{
1945
{
1946
//	i915_gem_object_unpin_fence(obj);
1946
//	i915_gem_object_unpin_fence(obj);
1947
//	i915_gem_object_unpin(obj);
1947
//	i915_gem_object_unpin(obj);
1948
}
1948
}
1949
 
1949
 
1950
/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
1950
/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
1951
 * is assumed to be a power-of-two. */
1951
 * is assumed to be a power-of-two. */
1952
static unsigned long gen4_compute_dspaddr_offset_xtiled(int *x, int *y,
1952
static unsigned long gen4_compute_dspaddr_offset_xtiled(int *x, int *y,
1953
							unsigned int bpp,
1953
							unsigned int bpp,
1954
							unsigned int pitch)
1954
							unsigned int pitch)
1955
{
1955
{
1956
	int tile_rows, tiles;
1956
	int tile_rows, tiles;
1957
 
1957
 
1958
	tile_rows = *y / 8;
1958
	tile_rows = *y / 8;
1959
	*y %= 8;
1959
	*y %= 8;
1960
	tiles = *x / (512/bpp);
1960
	tiles = *x / (512/bpp);
1961
	*x %= 512/bpp;
1961
	*x %= 512/bpp;
1962
 
1962
 
1963
	return tile_rows * pitch * 8 + tiles * 4096;
1963
	return tile_rows * pitch * 8 + tiles * 4096;
1964
}
1964
}
1965
 
1965
 
1966
static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
1966
static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
1967
                 int x, int y)
1967
                 int x, int y)
1968
{
1968
{
1969
    struct drm_device *dev = crtc->dev;
1969
    struct drm_device *dev = crtc->dev;
1970
    struct drm_i915_private *dev_priv = dev->dev_private;
1970
    struct drm_i915_private *dev_priv = dev->dev_private;
1971
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1971
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1972
    struct intel_framebuffer *intel_fb;
1972
    struct intel_framebuffer *intel_fb;
1973
    struct drm_i915_gem_object *obj;
1973
    struct drm_i915_gem_object *obj;
1974
    int plane = intel_crtc->plane;
1974
    int plane = intel_crtc->plane;
1975
	unsigned long linear_offset;
1975
	unsigned long linear_offset;
1976
    u32 dspcntr;
1976
    u32 dspcntr;
1977
    u32 reg;
1977
    u32 reg;
1978
 
1978
 
1979
    switch (plane) {
1979
    switch (plane) {
1980
    case 0:
1980
    case 0:
1981
    case 1:
1981
    case 1:
1982
        break;
1982
        break;
1983
    default:
1983
    default:
1984
        DRM_ERROR("Can't update plane %d in SAREA\n", plane);
1984
        DRM_ERROR("Can't update plane %d in SAREA\n", plane);
1985
        return -EINVAL;
1985
        return -EINVAL;
1986
    }
1986
    }
1987
 
1987
 
1988
    intel_fb = to_intel_framebuffer(fb);
1988
    intel_fb = to_intel_framebuffer(fb);
1989
    obj = intel_fb->obj;
1989
    obj = intel_fb->obj;
1990
 
1990
 
1991
    reg = DSPCNTR(plane);
1991
    reg = DSPCNTR(plane);
1992
    dspcntr = I915_READ(reg);
1992
    dspcntr = I915_READ(reg);
1993
    /* Mask out pixel format bits in case we change it */
1993
    /* Mask out pixel format bits in case we change it */
1994
    dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
1994
    dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
1995
    switch (fb->bits_per_pixel) {
1995
    switch (fb->bits_per_pixel) {
1996
    case 8:
1996
    case 8:
1997
        dspcntr |= DISPPLANE_8BPP;
1997
        dspcntr |= DISPPLANE_8BPP;
1998
        break;
1998
        break;
1999
    case 16:
1999
    case 16:
2000
        if (fb->depth == 15)
2000
        if (fb->depth == 15)
2001
            dspcntr |= DISPPLANE_15_16BPP;
2001
            dspcntr |= DISPPLANE_15_16BPP;
2002
        else
2002
        else
2003
            dspcntr |= DISPPLANE_16BPP;
2003
            dspcntr |= DISPPLANE_16BPP;
2004
        break;
2004
        break;
2005
    case 24:
2005
    case 24:
2006
    case 32:
2006
    case 32:
2007
        dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
2007
        dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
2008
        break;
2008
        break;
2009
    default:
2009
    default:
2010
        DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
2010
        DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
2011
        return -EINVAL;
2011
        return -EINVAL;
2012
    }
2012
    }
2013
    if (INTEL_INFO(dev)->gen >= 4) {
2013
    if (INTEL_INFO(dev)->gen >= 4) {
2014
        if (obj->tiling_mode != I915_TILING_NONE)
2014
        if (obj->tiling_mode != I915_TILING_NONE)
2015
            dspcntr |= DISPPLANE_TILED;
2015
            dspcntr |= DISPPLANE_TILED;
2016
        else
2016
        else
2017
            dspcntr &= ~DISPPLANE_TILED;
2017
            dspcntr &= ~DISPPLANE_TILED;
2018
    }
2018
    }
2019
 
2019
 
2020
    I915_WRITE(reg, dspcntr);
2020
    I915_WRITE(reg, dspcntr);
2021
 
2021
 
2022
	linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
2022
	linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
2023
 
2023
 
2024
	if (INTEL_INFO(dev)->gen >= 4) {
2024
	if (INTEL_INFO(dev)->gen >= 4) {
2025
		intel_crtc->dspaddr_offset =
2025
		intel_crtc->dspaddr_offset =
2026
			gen4_compute_dspaddr_offset_xtiled(&x, &y,
2026
			gen4_compute_dspaddr_offset_xtiled(&x, &y,
2027
							   fb->bits_per_pixel / 8,
2027
							   fb->bits_per_pixel / 8,
2028
							   fb->pitches[0]);
2028
							   fb->pitches[0]);
2029
		linear_offset -= intel_crtc->dspaddr_offset;
2029
		linear_offset -= intel_crtc->dspaddr_offset;
2030
	} else {
2030
	} else {
2031
		intel_crtc->dspaddr_offset = linear_offset;
2031
		intel_crtc->dspaddr_offset = linear_offset;
2032
	}
2032
	}
2033
 
2033
 
2034
	DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
2034
	DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
2035
		      obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
2035
		      obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
2036
	I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
2036
	I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
2037
    if (INTEL_INFO(dev)->gen >= 4) {
2037
    if (INTEL_INFO(dev)->gen >= 4) {
2038
		I915_MODIFY_DISPBASE(DSPSURF(plane),
2038
		I915_MODIFY_DISPBASE(DSPSURF(plane),
2039
				     obj->gtt_offset + intel_crtc->dspaddr_offset);
2039
				     obj->gtt_offset + intel_crtc->dspaddr_offset);
2040
        I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2040
        I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2041
		I915_WRITE(DSPLINOFF(plane), linear_offset);
2041
		I915_WRITE(DSPLINOFF(plane), linear_offset);
2042
    } else
2042
    } else
2043
		I915_WRITE(DSPADDR(plane), obj->gtt_offset + linear_offset);
2043
		I915_WRITE(DSPADDR(plane), obj->gtt_offset + linear_offset);
2044
    POSTING_READ(reg);
2044
    POSTING_READ(reg);
2045
 
2045
 
2046
    return 0;
2046
    return 0;
2047
}
2047
}
2048
 
2048
 
2049
static int ironlake_update_plane(struct drm_crtc *crtc,
2049
static int ironlake_update_plane(struct drm_crtc *crtc,
2050
                 struct drm_framebuffer *fb, int x, int y)
2050
                 struct drm_framebuffer *fb, int x, int y)
2051
{
2051
{
2052
    struct drm_device *dev = crtc->dev;
2052
    struct drm_device *dev = crtc->dev;
2053
    struct drm_i915_private *dev_priv = dev->dev_private;
2053
    struct drm_i915_private *dev_priv = dev->dev_private;
2054
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2054
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2055
    struct intel_framebuffer *intel_fb;
2055
    struct intel_framebuffer *intel_fb;
2056
    struct drm_i915_gem_object *obj;
2056
    struct drm_i915_gem_object *obj;
2057
    int plane = intel_crtc->plane;
2057
    int plane = intel_crtc->plane;
2058
	unsigned long linear_offset;
2058
	unsigned long linear_offset;
2059
    u32 dspcntr;
2059
    u32 dspcntr;
2060
    u32 reg;
2060
    u32 reg;
2061
 
2061
 
2062
    switch (plane) {
2062
    switch (plane) {
2063
    case 0:
2063
    case 0:
2064
    case 1:
2064
    case 1:
2065
	case 2:
2065
	case 2:
2066
        break;
2066
        break;
2067
    default:
2067
    default:
2068
        DRM_ERROR("Can't update plane %d in SAREA\n", plane);
2068
        DRM_ERROR("Can't update plane %d in SAREA\n", plane);
2069
        return -EINVAL;
2069
        return -EINVAL;
2070
    }
2070
    }
2071
 
2071
 
2072
    intel_fb = to_intel_framebuffer(fb);
2072
    intel_fb = to_intel_framebuffer(fb);
2073
    obj = intel_fb->obj;
2073
    obj = intel_fb->obj;
2074
 
2074
 
2075
    reg = DSPCNTR(plane);
2075
    reg = DSPCNTR(plane);
2076
    dspcntr = I915_READ(reg);
2076
    dspcntr = I915_READ(reg);
2077
    /* Mask out pixel format bits in case we change it */
2077
    /* Mask out pixel format bits in case we change it */
2078
    dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
2078
    dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
2079
    switch (fb->bits_per_pixel) {
2079
    switch (fb->bits_per_pixel) {
2080
    case 8:
2080
    case 8:
2081
        dspcntr |= DISPPLANE_8BPP;
2081
        dspcntr |= DISPPLANE_8BPP;
2082
        break;
2082
        break;
2083
    case 16:
2083
    case 16:
2084
        if (fb->depth != 16)
2084
        if (fb->depth != 16)
2085
            return -EINVAL;
2085
            return -EINVAL;
2086
 
2086
 
2087
        dspcntr |= DISPPLANE_16BPP;
2087
        dspcntr |= DISPPLANE_16BPP;
2088
        break;
2088
        break;
2089
    case 24:
2089
    case 24:
2090
    case 32:
2090
    case 32:
2091
        if (fb->depth == 24)
2091
        if (fb->depth == 24)
2092
            dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
2092
            dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
2093
        else if (fb->depth == 30)
2093
        else if (fb->depth == 30)
2094
            dspcntr |= DISPPLANE_32BPP_30BIT_NO_ALPHA;
2094
            dspcntr |= DISPPLANE_32BPP_30BIT_NO_ALPHA;
2095
        else
2095
        else
2096
            return -EINVAL;
2096
            return -EINVAL;
2097
        break;
2097
        break;
2098
    default:
2098
    default:
2099
        DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
2099
        DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
2100
        return -EINVAL;
2100
        return -EINVAL;
2101
    }
2101
    }
2102
 
2102
 
2103
//    if (obj->tiling_mode != I915_TILING_NONE)
2103
//    if (obj->tiling_mode != I915_TILING_NONE)
2104
//        dspcntr |= DISPPLANE_TILED;
2104
//        dspcntr |= DISPPLANE_TILED;
2105
//    else
2105
//    else
2106
        dspcntr &= ~DISPPLANE_TILED;
2106
        dspcntr &= ~DISPPLANE_TILED;
2107
 
2107
 
2108
    /* must disable */
2108
    /* must disable */
2109
    dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2109
    dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2110
 
2110
 
2111
    I915_WRITE(reg, dspcntr);
2111
    I915_WRITE(reg, dspcntr);
2112
 
2112
 
2113
	linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
2113
	linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
2114
	intel_crtc->dspaddr_offset =
2114
	intel_crtc->dspaddr_offset =
2115
		gen4_compute_dspaddr_offset_xtiled(&x, &y,
2115
		gen4_compute_dspaddr_offset_xtiled(&x, &y,
2116
						   fb->bits_per_pixel / 8,
2116
						   fb->bits_per_pixel / 8,
2117
						   fb->pitches[0]);
2117
						   fb->pitches[0]);
2118
	linear_offset -= intel_crtc->dspaddr_offset;
2118
	linear_offset -= intel_crtc->dspaddr_offset;
2119
 
2119
 
2120
	DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
2120
	DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
2121
		      obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
2121
		      obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
2122
	I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
2122
	I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
2123
	I915_MODIFY_DISPBASE(DSPSURF(plane),
2123
	I915_MODIFY_DISPBASE(DSPSURF(plane),
2124
			     obj->gtt_offset + intel_crtc->dspaddr_offset);
2124
			     obj->gtt_offset + intel_crtc->dspaddr_offset);
2125
	I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2125
	I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2126
	I915_WRITE(DSPLINOFF(plane), linear_offset);
2126
	I915_WRITE(DSPLINOFF(plane), linear_offset);
2127
	POSTING_READ(reg);
2127
	POSTING_READ(reg);
2128
 
2128
 
2129
    return 0;
2129
    return 0;
2130
}
2130
}
2131
 
2131
 
2132
/* Assume fb object is pinned & idle & fenced and just update base pointers */
2132
/* Assume fb object is pinned & idle & fenced and just update base pointers */
2133
static int
2133
static int
2134
intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2134
intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2135
			   int x, int y, enum mode_set_atomic state)
2135
			   int x, int y, enum mode_set_atomic state)
2136
{
2136
{
2137
	struct drm_device *dev = crtc->dev;
2137
	struct drm_device *dev = crtc->dev;
2138
	struct drm_i915_private *dev_priv = dev->dev_private;
2138
	struct drm_i915_private *dev_priv = dev->dev_private;
2139
 
2139
 
2140
	if (dev_priv->display.disable_fbc)
2140
	if (dev_priv->display.disable_fbc)
2141
		dev_priv->display.disable_fbc(dev);
2141
		dev_priv->display.disable_fbc(dev);
2142
	intel_increase_pllclock(crtc);
2142
	intel_increase_pllclock(crtc);
2143
 
2143
 
2144
	return dev_priv->display.update_plane(crtc, fb, x, y);
2144
	return dev_priv->display.update_plane(crtc, fb, x, y);
2145
}
2145
}
2146
 
2146
 
2147
#if 0
2147
#if 0
2148
static int
2148
static int
2149
intel_finish_fb(struct drm_framebuffer *old_fb)
2149
intel_finish_fb(struct drm_framebuffer *old_fb)
2150
{
2150
{
2151
	struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2151
	struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2152
	struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2152
	struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2153
	bool was_interruptible = dev_priv->mm.interruptible;
2153
	bool was_interruptible = dev_priv->mm.interruptible;
2154
	int ret;
2154
	int ret;
2155
 
2155
 
2156
	wait_event(dev_priv->pending_flip_queue,
2156
	wait_event(dev_priv->pending_flip_queue,
2157
		   atomic_read(&dev_priv->mm.wedged) ||
2157
		   atomic_read(&dev_priv->mm.wedged) ||
2158
		   atomic_read(&obj->pending_flip) == 0);
2158
		   atomic_read(&obj->pending_flip) == 0);
2159
 
2159
 
2160
	/* Big Hammer, we also need to ensure that any pending
2160
	/* Big Hammer, we also need to ensure that any pending
2161
	 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2161
	 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2162
	 * current scanout is retired before unpinning the old
2162
	 * current scanout is retired before unpinning the old
2163
	 * framebuffer.
2163
	 * framebuffer.
2164
	 *
2164
	 *
2165
	 * This should only fail upon a hung GPU, in which case we
2165
	 * This should only fail upon a hung GPU, in which case we
2166
	 * can safely continue.
2166
	 * can safely continue.
2167
	 */
2167
	 */
2168
	dev_priv->mm.interruptible = false;
2168
	dev_priv->mm.interruptible = false;
2169
	ret = i915_gem_object_finish_gpu(obj);
2169
	ret = i915_gem_object_finish_gpu(obj);
2170
	dev_priv->mm.interruptible = was_interruptible;
2170
	dev_priv->mm.interruptible = was_interruptible;
2171
 
2171
 
2172
	return ret;
2172
	return ret;
2173
}
2173
}
2174
#endif
2174
#endif
2175
 
2175
 
2176
static int
2176
static int
2177
intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
2177
intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
2178
		    struct drm_framebuffer *fb)
2178
		    struct drm_framebuffer *fb)
2179
{
2179
{
2180
	struct drm_device *dev = crtc->dev;
2180
	struct drm_device *dev = crtc->dev;
2181
	struct drm_i915_private *dev_priv = dev->dev_private;
2181
	struct drm_i915_private *dev_priv = dev->dev_private;
2182
	struct drm_i915_master_private *master_priv;
2182
	struct drm_i915_master_private *master_priv;
2183
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2183
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2184
	struct drm_framebuffer *old_fb;
2184
	struct drm_framebuffer *old_fb;
2185
	int ret;
2185
	int ret;
2186
 
2186
 
2187
	/* no fb bound */
2187
	/* no fb bound */
2188
	if (!fb) {
2188
	if (!fb) {
2189
		DRM_ERROR("No FB bound\n");
2189
		DRM_ERROR("No FB bound\n");
2190
		return 0;
2190
		return 0;
2191
	}
2191
	}
2192
 
2192
 
2193
	if(intel_crtc->plane > dev_priv->num_pipe) {
2193
	if(intel_crtc->plane > dev_priv->num_pipe) {
2194
		DRM_ERROR("no plane for crtc: plane %d, num_pipes %d\n",
2194
		DRM_ERROR("no plane for crtc: plane %d, num_pipes %d\n",
2195
				intel_crtc->plane,
2195
				intel_crtc->plane,
2196
				dev_priv->num_pipe);
2196
				dev_priv->num_pipe);
2197
		return -EINVAL;
2197
		return -EINVAL;
2198
	}
2198
	}
2199
 
2199
 
2200
	mutex_lock(&dev->struct_mutex);
2200
	mutex_lock(&dev->struct_mutex);
2201
//   ret = intel_pin_and_fence_fb_obj(dev,
2201
//   ret = intel_pin_and_fence_fb_obj(dev,
2202
//                    to_intel_framebuffer(fb)->obj,
2202
//                    to_intel_framebuffer(fb)->obj,
2203
//                    NULL);
2203
//                    NULL);
2204
//   if (ret != 0) {
2204
//   if (ret != 0) {
2205
//       mutex_unlock(&dev->struct_mutex);
2205
//       mutex_unlock(&dev->struct_mutex);
2206
//       DRM_ERROR("pin & fence failed\n");
2206
//       DRM_ERROR("pin & fence failed\n");
2207
//       return ret;
2207
//       return ret;
2208
//   }
2208
//   }
2209
 
2209
 
2210
//   if (crtc->fb)
2210
//   if (crtc->fb)
2211
//       intel_finish_fb(crtc->fb);
2211
//       intel_finish_fb(crtc->fb);
2212
 
2212
 
2213
	ret = dev_priv->display.update_plane(crtc, fb, x, y);
2213
	ret = dev_priv->display.update_plane(crtc, fb, x, y);
2214
	if (ret) {
2214
	if (ret) {
2215
		intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
2215
		intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
2216
		mutex_unlock(&dev->struct_mutex);
2216
		mutex_unlock(&dev->struct_mutex);
2217
		DRM_ERROR("failed to update base address\n");
2217
		DRM_ERROR("failed to update base address\n");
2218
		return ret;
2218
		return ret;
2219
	}
2219
	}
2220
 
2220
 
2221
	old_fb = crtc->fb;
2221
	old_fb = crtc->fb;
2222
	crtc->fb = fb;
2222
	crtc->fb = fb;
2223
	crtc->x = x;
2223
	crtc->x = x;
2224
	crtc->y = y;
2224
	crtc->y = y;
2225
 
2225
 
2226
	if (old_fb) {
2226
	if (old_fb) {
2227
		intel_wait_for_vblank(dev, intel_crtc->pipe);
2227
		intel_wait_for_vblank(dev, intel_crtc->pipe);
2228
		intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
2228
		intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
2229
	}
2229
	}
2230
 
2230
 
2231
	intel_update_fbc(dev);
2231
	intel_update_fbc(dev);
2232
	mutex_unlock(&dev->struct_mutex);
2232
	mutex_unlock(&dev->struct_mutex);
2233
 
2233
 
2234
    return 0;
2234
    return 0;
2235
}
2235
}
2236
 
2236
 
2237
static void ironlake_set_pll_edp(struct drm_crtc *crtc, int clock)
2237
static void ironlake_set_pll_edp(struct drm_crtc *crtc, int clock)
2238
{
2238
{
2239
	struct drm_device *dev = crtc->dev;
2239
	struct drm_device *dev = crtc->dev;
2240
	struct drm_i915_private *dev_priv = dev->dev_private;
2240
	struct drm_i915_private *dev_priv = dev->dev_private;
2241
	u32 dpa_ctl;
2241
	u32 dpa_ctl;
2242
 
2242
 
2243
	DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", clock);
2243
	DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", clock);
2244
	dpa_ctl = I915_READ(DP_A);
2244
	dpa_ctl = I915_READ(DP_A);
2245
	dpa_ctl &= ~DP_PLL_FREQ_MASK;
2245
	dpa_ctl &= ~DP_PLL_FREQ_MASK;
2246
 
2246
 
2247
	if (clock < 200000) {
2247
	if (clock < 200000) {
2248
		u32 temp;
2248
		u32 temp;
2249
		dpa_ctl |= DP_PLL_FREQ_160MHZ;
2249
		dpa_ctl |= DP_PLL_FREQ_160MHZ;
2250
		/* workaround for 160Mhz:
2250
		/* workaround for 160Mhz:
2251
		   1) program 0x4600c bits 15:0 = 0x8124
2251
		   1) program 0x4600c bits 15:0 = 0x8124
2252
		   2) program 0x46010 bit 0 = 1
2252
		   2) program 0x46010 bit 0 = 1
2253
		   3) program 0x46034 bit 24 = 1
2253
		   3) program 0x46034 bit 24 = 1
2254
		   4) program 0x64000 bit 14 = 1
2254
		   4) program 0x64000 bit 14 = 1
2255
		   */
2255
		   */
2256
		temp = I915_READ(0x4600c);
2256
		temp = I915_READ(0x4600c);
2257
		temp &= 0xffff0000;
2257
		temp &= 0xffff0000;
2258
		I915_WRITE(0x4600c, temp | 0x8124);
2258
		I915_WRITE(0x4600c, temp | 0x8124);
2259
 
2259
 
2260
		temp = I915_READ(0x46010);
2260
		temp = I915_READ(0x46010);
2261
		I915_WRITE(0x46010, temp | 1);
2261
		I915_WRITE(0x46010, temp | 1);
2262
 
2262
 
2263
		temp = I915_READ(0x46034);
2263
		temp = I915_READ(0x46034);
2264
		I915_WRITE(0x46034, temp | (1 << 24));
2264
		I915_WRITE(0x46034, temp | (1 << 24));
2265
	} else {
2265
	} else {
2266
		dpa_ctl |= DP_PLL_FREQ_270MHZ;
2266
		dpa_ctl |= DP_PLL_FREQ_270MHZ;
2267
	}
2267
	}
2268
	I915_WRITE(DP_A, dpa_ctl);
2268
	I915_WRITE(DP_A, dpa_ctl);
2269
 
2269
 
2270
	POSTING_READ(DP_A);
2270
	POSTING_READ(DP_A);
2271
	udelay(500);
2271
	udelay(500);
2272
}
2272
}
2273
 
2273
 
2274
static void intel_fdi_normal_train(struct drm_crtc *crtc)
2274
static void intel_fdi_normal_train(struct drm_crtc *crtc)
2275
{
2275
{
2276
	struct drm_device *dev = crtc->dev;
2276
	struct drm_device *dev = crtc->dev;
2277
	struct drm_i915_private *dev_priv = dev->dev_private;
2277
	struct drm_i915_private *dev_priv = dev->dev_private;
2278
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2278
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2279
	int pipe = intel_crtc->pipe;
2279
	int pipe = intel_crtc->pipe;
2280
	u32 reg, temp;
2280
	u32 reg, temp;
2281
 
2281
 
2282
	/* enable normal train */
2282
	/* enable normal train */
2283
	reg = FDI_TX_CTL(pipe);
2283
	reg = FDI_TX_CTL(pipe);
2284
	temp = I915_READ(reg);
2284
	temp = I915_READ(reg);
2285
	if (IS_IVYBRIDGE(dev)) {
2285
	if (IS_IVYBRIDGE(dev)) {
2286
		temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2286
		temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2287
		temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
2287
		temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
2288
	} else {
2288
	} else {
2289
		temp &= ~FDI_LINK_TRAIN_NONE;
2289
		temp &= ~FDI_LINK_TRAIN_NONE;
2290
		temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
2290
		temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
2291
	}
2291
	}
2292
	I915_WRITE(reg, temp);
2292
	I915_WRITE(reg, temp);
2293
 
2293
 
2294
	reg = FDI_RX_CTL(pipe);
2294
	reg = FDI_RX_CTL(pipe);
2295
	temp = I915_READ(reg);
2295
	temp = I915_READ(reg);
2296
	if (HAS_PCH_CPT(dev)) {
2296
	if (HAS_PCH_CPT(dev)) {
2297
		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2297
		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2298
		temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2298
		temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2299
	} else {
2299
	} else {
2300
		temp &= ~FDI_LINK_TRAIN_NONE;
2300
		temp &= ~FDI_LINK_TRAIN_NONE;
2301
		temp |= FDI_LINK_TRAIN_NONE;
2301
		temp |= FDI_LINK_TRAIN_NONE;
2302
	}
2302
	}
2303
	I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2303
	I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2304
 
2304
 
2305
	/* wait one idle pattern time */
2305
	/* wait one idle pattern time */
2306
	POSTING_READ(reg);
2306
	POSTING_READ(reg);
2307
	udelay(1000);
2307
	udelay(1000);
2308
 
2308
 
2309
	/* IVB wants error correction enabled */
2309
	/* IVB wants error correction enabled */
2310
	if (IS_IVYBRIDGE(dev))
2310
	if (IS_IVYBRIDGE(dev))
2311
		I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2311
		I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2312
			   FDI_FE_ERRC_ENABLE);
2312
			   FDI_FE_ERRC_ENABLE);
2313
}
2313
}
2314
 
2314
 
2315
static void cpt_phase_pointer_enable(struct drm_device *dev, int pipe)
2315
static void cpt_phase_pointer_enable(struct drm_device *dev, int pipe)
2316
{
2316
{
2317
	struct drm_i915_private *dev_priv = dev->dev_private;
2317
	struct drm_i915_private *dev_priv = dev->dev_private;
2318
	u32 flags = I915_READ(SOUTH_CHICKEN1);
2318
	u32 flags = I915_READ(SOUTH_CHICKEN1);
2319
 
2319
 
2320
	flags |= FDI_PHASE_SYNC_OVR(pipe);
2320
	flags |= FDI_PHASE_SYNC_OVR(pipe);
2321
	I915_WRITE(SOUTH_CHICKEN1, flags); /* once to unlock... */
2321
	I915_WRITE(SOUTH_CHICKEN1, flags); /* once to unlock... */
2322
	flags |= FDI_PHASE_SYNC_EN(pipe);
2322
	flags |= FDI_PHASE_SYNC_EN(pipe);
2323
	I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to enable */
2323
	I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to enable */
2324
	POSTING_READ(SOUTH_CHICKEN1);
2324
	POSTING_READ(SOUTH_CHICKEN1);
2325
}
2325
}
2326
 
2326
 
2327
/* The FDI link training functions for ILK/Ibexpeak. */
2327
/* The FDI link training functions for ILK/Ibexpeak. */
2328
static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2328
static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2329
{
2329
{
2330
    struct drm_device *dev = crtc->dev;
2330
    struct drm_device *dev = crtc->dev;
2331
    struct drm_i915_private *dev_priv = dev->dev_private;
2331
    struct drm_i915_private *dev_priv = dev->dev_private;
2332
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2332
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2333
    int pipe = intel_crtc->pipe;
2333
    int pipe = intel_crtc->pipe;
2334
    int plane = intel_crtc->plane;
2334
    int plane = intel_crtc->plane;
2335
    u32 reg, temp, tries;
2335
    u32 reg, temp, tries;
2336
 
2336
 
2337
    /* FDI needs bits from pipe & plane first */
2337
    /* FDI needs bits from pipe & plane first */
2338
    assert_pipe_enabled(dev_priv, pipe);
2338
    assert_pipe_enabled(dev_priv, pipe);
2339
    assert_plane_enabled(dev_priv, plane);
2339
    assert_plane_enabled(dev_priv, plane);
2340
 
2340
 
2341
    /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2341
    /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2342
       for train result */
2342
       for train result */
2343
    reg = FDI_RX_IMR(pipe);
2343
    reg = FDI_RX_IMR(pipe);
2344
    temp = I915_READ(reg);
2344
    temp = I915_READ(reg);
2345
    temp &= ~FDI_RX_SYMBOL_LOCK;
2345
    temp &= ~FDI_RX_SYMBOL_LOCK;
2346
    temp &= ~FDI_RX_BIT_LOCK;
2346
    temp &= ~FDI_RX_BIT_LOCK;
2347
    I915_WRITE(reg, temp);
2347
    I915_WRITE(reg, temp);
2348
    I915_READ(reg);
2348
    I915_READ(reg);
2349
    udelay(150);
2349
    udelay(150);
2350
 
2350
 
2351
    /* enable CPU FDI TX and PCH FDI RX */
2351
    /* enable CPU FDI TX and PCH FDI RX */
2352
    reg = FDI_TX_CTL(pipe);
2352
    reg = FDI_TX_CTL(pipe);
2353
    temp = I915_READ(reg);
2353
    temp = I915_READ(reg);
2354
    temp &= ~(7 << 19);
2354
    temp &= ~(7 << 19);
2355
    temp |= (intel_crtc->fdi_lanes - 1) << 19;
2355
    temp |= (intel_crtc->fdi_lanes - 1) << 19;
2356
    temp &= ~FDI_LINK_TRAIN_NONE;
2356
    temp &= ~FDI_LINK_TRAIN_NONE;
2357
    temp |= FDI_LINK_TRAIN_PATTERN_1;
2357
    temp |= FDI_LINK_TRAIN_PATTERN_1;
2358
    I915_WRITE(reg, temp | FDI_TX_ENABLE);
2358
    I915_WRITE(reg, temp | FDI_TX_ENABLE);
2359
 
2359
 
2360
    reg = FDI_RX_CTL(pipe);
2360
    reg = FDI_RX_CTL(pipe);
2361
    temp = I915_READ(reg);
2361
    temp = I915_READ(reg);
2362
    temp &= ~FDI_LINK_TRAIN_NONE;
2362
    temp &= ~FDI_LINK_TRAIN_NONE;
2363
    temp |= FDI_LINK_TRAIN_PATTERN_1;
2363
    temp |= FDI_LINK_TRAIN_PATTERN_1;
2364
    I915_WRITE(reg, temp | FDI_RX_ENABLE);
2364
    I915_WRITE(reg, temp | FDI_RX_ENABLE);
2365
 
2365
 
2366
    POSTING_READ(reg);
2366
    POSTING_READ(reg);
2367
    udelay(150);
2367
    udelay(150);
2368
 
2368
 
2369
    /* Ironlake workaround, enable clock pointer after FDI enable*/
2369
    /* Ironlake workaround, enable clock pointer after FDI enable*/
2370
    if (HAS_PCH_IBX(dev)) {
2370
    if (HAS_PCH_IBX(dev)) {
2371
        I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2371
        I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2372
        I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2372
        I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2373
               FDI_RX_PHASE_SYNC_POINTER_EN);
2373
               FDI_RX_PHASE_SYNC_POINTER_EN);
2374
    }
2374
    }
2375
 
2375
 
2376
    reg = FDI_RX_IIR(pipe);
2376
    reg = FDI_RX_IIR(pipe);
2377
    for (tries = 0; tries < 5; tries++) {
2377
    for (tries = 0; tries < 5; tries++) {
2378
        temp = I915_READ(reg);
2378
        temp = I915_READ(reg);
2379
        DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2379
        DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2380
 
2380
 
2381
        if ((temp & FDI_RX_BIT_LOCK)) {
2381
        if ((temp & FDI_RX_BIT_LOCK)) {
2382
            DRM_DEBUG_KMS("FDI train 1 done.\n");
2382
            DRM_DEBUG_KMS("FDI train 1 done.\n");
2383
            I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2383
            I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2384
            break;
2384
            break;
2385
        }
2385
        }
2386
    }
2386
    }
2387
    if (tries == 5)
2387
    if (tries == 5)
2388
        DRM_ERROR("FDI train 1 fail!\n");
2388
        DRM_ERROR("FDI train 1 fail!\n");
2389
 
2389
 
2390
    /* Train 2 */
2390
    /* Train 2 */
2391
    reg = FDI_TX_CTL(pipe);
2391
    reg = FDI_TX_CTL(pipe);
2392
    temp = I915_READ(reg);
2392
    temp = I915_READ(reg);
2393
    temp &= ~FDI_LINK_TRAIN_NONE;
2393
    temp &= ~FDI_LINK_TRAIN_NONE;
2394
    temp |= FDI_LINK_TRAIN_PATTERN_2;
2394
    temp |= FDI_LINK_TRAIN_PATTERN_2;
2395
    I915_WRITE(reg, temp);
2395
    I915_WRITE(reg, temp);
2396
 
2396
 
2397
    reg = FDI_RX_CTL(pipe);
2397
    reg = FDI_RX_CTL(pipe);
2398
    temp = I915_READ(reg);
2398
    temp = I915_READ(reg);
2399
    temp &= ~FDI_LINK_TRAIN_NONE;
2399
    temp &= ~FDI_LINK_TRAIN_NONE;
2400
    temp |= FDI_LINK_TRAIN_PATTERN_2;
2400
    temp |= FDI_LINK_TRAIN_PATTERN_2;
2401
    I915_WRITE(reg, temp);
2401
    I915_WRITE(reg, temp);
2402
 
2402
 
2403
    POSTING_READ(reg);
2403
    POSTING_READ(reg);
2404
    udelay(150);
2404
    udelay(150);
2405
 
2405
 
2406
    reg = FDI_RX_IIR(pipe);
2406
    reg = FDI_RX_IIR(pipe);
2407
    for (tries = 0; tries < 5; tries++) {
2407
    for (tries = 0; tries < 5; tries++) {
2408
        temp = I915_READ(reg);
2408
        temp = I915_READ(reg);
2409
        DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2409
        DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2410
 
2410
 
2411
        if (temp & FDI_RX_SYMBOL_LOCK) {
2411
        if (temp & FDI_RX_SYMBOL_LOCK) {
2412
            I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2412
            I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2413
            DRM_DEBUG_KMS("FDI train 2 done.\n");
2413
            DRM_DEBUG_KMS("FDI train 2 done.\n");
2414
            break;
2414
            break;
2415
        }
2415
        }
2416
    }
2416
    }
2417
    if (tries == 5)
2417
    if (tries == 5)
2418
        DRM_ERROR("FDI train 2 fail!\n");
2418
        DRM_ERROR("FDI train 2 fail!\n");
2419
 
2419
 
2420
    DRM_DEBUG_KMS("FDI train done\n");
2420
    DRM_DEBUG_KMS("FDI train done\n");
2421
 
2421
 
2422
}
2422
}
2423
 
2423
 
2424
static const int snb_b_fdi_train_param[] = {
2424
static const int snb_b_fdi_train_param[] = {
2425
    FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2425
    FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2426
    FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2426
    FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2427
    FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2427
    FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2428
    FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2428
    FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2429
};
2429
};
2430
 
2430
 
2431
/* The FDI link training functions for SNB/Cougarpoint. */
2431
/* The FDI link training functions for SNB/Cougarpoint. */
2432
static void gen6_fdi_link_train(struct drm_crtc *crtc)
2432
static void gen6_fdi_link_train(struct drm_crtc *crtc)
2433
{
2433
{
2434
    struct drm_device *dev = crtc->dev;
2434
    struct drm_device *dev = crtc->dev;
2435
    struct drm_i915_private *dev_priv = dev->dev_private;
2435
    struct drm_i915_private *dev_priv = dev->dev_private;
2436
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2436
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2437
    int pipe = intel_crtc->pipe;
2437
    int pipe = intel_crtc->pipe;
2438
	u32 reg, temp, i, retry;
2438
	u32 reg, temp, i, retry;
2439
 
2439
 
2440
    /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2440
    /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2441
       for train result */
2441
       for train result */
2442
    reg = FDI_RX_IMR(pipe);
2442
    reg = FDI_RX_IMR(pipe);
2443
    temp = I915_READ(reg);
2443
    temp = I915_READ(reg);
2444
    temp &= ~FDI_RX_SYMBOL_LOCK;
2444
    temp &= ~FDI_RX_SYMBOL_LOCK;
2445
    temp &= ~FDI_RX_BIT_LOCK;
2445
    temp &= ~FDI_RX_BIT_LOCK;
2446
    I915_WRITE(reg, temp);
2446
    I915_WRITE(reg, temp);
2447
 
2447
 
2448
    POSTING_READ(reg);
2448
    POSTING_READ(reg);
2449
    udelay(150);
2449
    udelay(150);
2450
 
2450
 
2451
    /* enable CPU FDI TX and PCH FDI RX */
2451
    /* enable CPU FDI TX and PCH FDI RX */
2452
    reg = FDI_TX_CTL(pipe);
2452
    reg = FDI_TX_CTL(pipe);
2453
    temp = I915_READ(reg);
2453
    temp = I915_READ(reg);
2454
    temp &= ~(7 << 19);
2454
    temp &= ~(7 << 19);
2455
    temp |= (intel_crtc->fdi_lanes - 1) << 19;
2455
    temp |= (intel_crtc->fdi_lanes - 1) << 19;
2456
    temp &= ~FDI_LINK_TRAIN_NONE;
2456
    temp &= ~FDI_LINK_TRAIN_NONE;
2457
    temp |= FDI_LINK_TRAIN_PATTERN_1;
2457
    temp |= FDI_LINK_TRAIN_PATTERN_1;
2458
    temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2458
    temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2459
    /* SNB-B */
2459
    /* SNB-B */
2460
    temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2460
    temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2461
    I915_WRITE(reg, temp | FDI_TX_ENABLE);
2461
    I915_WRITE(reg, temp | FDI_TX_ENABLE);
2462
 
2462
 
2463
    reg = FDI_RX_CTL(pipe);
2463
    reg = FDI_RX_CTL(pipe);
2464
    temp = I915_READ(reg);
2464
    temp = I915_READ(reg);
2465
    if (HAS_PCH_CPT(dev)) {
2465
    if (HAS_PCH_CPT(dev)) {
2466
        temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2466
        temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2467
        temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2467
        temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2468
    } else {
2468
    } else {
2469
        temp &= ~FDI_LINK_TRAIN_NONE;
2469
        temp &= ~FDI_LINK_TRAIN_NONE;
2470
        temp |= FDI_LINK_TRAIN_PATTERN_1;
2470
        temp |= FDI_LINK_TRAIN_PATTERN_1;
2471
    }
2471
    }
2472
    I915_WRITE(reg, temp | FDI_RX_ENABLE);
2472
    I915_WRITE(reg, temp | FDI_RX_ENABLE);
2473
 
2473
 
2474
    POSTING_READ(reg);
2474
    POSTING_READ(reg);
2475
    udelay(150);
2475
    udelay(150);
2476
 
2476
 
2477
    if (HAS_PCH_CPT(dev))
2477
    if (HAS_PCH_CPT(dev))
2478
        cpt_phase_pointer_enable(dev, pipe);
2478
        cpt_phase_pointer_enable(dev, pipe);
2479
 
2479
 
2480
	for (i = 0; i < 4; i++) {
2480
	for (i = 0; i < 4; i++) {
2481
        reg = FDI_TX_CTL(pipe);
2481
        reg = FDI_TX_CTL(pipe);
2482
        temp = I915_READ(reg);
2482
        temp = I915_READ(reg);
2483
        temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2483
        temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2484
        temp |= snb_b_fdi_train_param[i];
2484
        temp |= snb_b_fdi_train_param[i];
2485
        I915_WRITE(reg, temp);
2485
        I915_WRITE(reg, temp);
2486
 
2486
 
2487
        POSTING_READ(reg);
2487
        POSTING_READ(reg);
2488
        udelay(500);
2488
        udelay(500);
2489
 
2489
 
2490
		for (retry = 0; retry < 5; retry++) {
2490
		for (retry = 0; retry < 5; retry++) {
2491
        reg = FDI_RX_IIR(pipe);
2491
        reg = FDI_RX_IIR(pipe);
2492
        temp = I915_READ(reg);
2492
        temp = I915_READ(reg);
2493
        DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2493
        DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2494
        if (temp & FDI_RX_BIT_LOCK) {
2494
        if (temp & FDI_RX_BIT_LOCK) {
2495
            I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2495
            I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2496
            DRM_DEBUG_KMS("FDI train 1 done.\n");
2496
            DRM_DEBUG_KMS("FDI train 1 done.\n");
2497
            break;
2497
            break;
2498
        }
2498
        }
2499
			udelay(50);
2499
			udelay(50);
2500
		}
2500
		}
2501
		if (retry < 5)
2501
		if (retry < 5)
2502
			break;
2502
			break;
2503
    }
2503
    }
2504
    if (i == 4)
2504
    if (i == 4)
2505
        DRM_ERROR("FDI train 1 fail!\n");
2505
        DRM_ERROR("FDI train 1 fail!\n");
2506
 
2506
 
2507
    /* Train 2 */
2507
    /* Train 2 */
2508
    reg = FDI_TX_CTL(pipe);
2508
    reg = FDI_TX_CTL(pipe);
2509
    temp = I915_READ(reg);
2509
    temp = I915_READ(reg);
2510
    temp &= ~FDI_LINK_TRAIN_NONE;
2510
    temp &= ~FDI_LINK_TRAIN_NONE;
2511
    temp |= FDI_LINK_TRAIN_PATTERN_2;
2511
    temp |= FDI_LINK_TRAIN_PATTERN_2;
2512
    if (IS_GEN6(dev)) {
2512
    if (IS_GEN6(dev)) {
2513
        temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2513
        temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2514
        /* SNB-B */
2514
        /* SNB-B */
2515
        temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2515
        temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2516
    }
2516
    }
2517
    I915_WRITE(reg, temp);
2517
    I915_WRITE(reg, temp);
2518
 
2518
 
2519
    reg = FDI_RX_CTL(pipe);
2519
    reg = FDI_RX_CTL(pipe);
2520
    temp = I915_READ(reg);
2520
    temp = I915_READ(reg);
2521
    if (HAS_PCH_CPT(dev)) {
2521
    if (HAS_PCH_CPT(dev)) {
2522
        temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2522
        temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2523
        temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2523
        temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2524
    } else {
2524
    } else {
2525
        temp &= ~FDI_LINK_TRAIN_NONE;
2525
        temp &= ~FDI_LINK_TRAIN_NONE;
2526
        temp |= FDI_LINK_TRAIN_PATTERN_2;
2526
        temp |= FDI_LINK_TRAIN_PATTERN_2;
2527
    }
2527
    }
2528
    I915_WRITE(reg, temp);
2528
    I915_WRITE(reg, temp);
2529
 
2529
 
2530
    POSTING_READ(reg);
2530
    POSTING_READ(reg);
2531
    udelay(150);
2531
    udelay(150);
2532
 
2532
 
2533
	for (i = 0; i < 4; i++) {
2533
	for (i = 0; i < 4; i++) {
2534
        reg = FDI_TX_CTL(pipe);
2534
        reg = FDI_TX_CTL(pipe);
2535
        temp = I915_READ(reg);
2535
        temp = I915_READ(reg);
2536
        temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2536
        temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2537
        temp |= snb_b_fdi_train_param[i];
2537
        temp |= snb_b_fdi_train_param[i];
2538
        I915_WRITE(reg, temp);
2538
        I915_WRITE(reg, temp);
2539
 
2539
 
2540
        POSTING_READ(reg);
2540
        POSTING_READ(reg);
2541
        udelay(500);
2541
        udelay(500);
2542
 
2542
 
2543
		for (retry = 0; retry < 5; retry++) {
2543
		for (retry = 0; retry < 5; retry++) {
2544
        reg = FDI_RX_IIR(pipe);
2544
        reg = FDI_RX_IIR(pipe);
2545
        temp = I915_READ(reg);
2545
        temp = I915_READ(reg);
2546
        DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2546
        DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2547
        if (temp & FDI_RX_SYMBOL_LOCK) {
2547
        if (temp & FDI_RX_SYMBOL_LOCK) {
2548
            I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2548
            I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2549
            DRM_DEBUG_KMS("FDI train 2 done.\n");
2549
            DRM_DEBUG_KMS("FDI train 2 done.\n");
2550
            break;
2550
            break;
2551
        }
2551
        }
2552
			udelay(50);
2552
			udelay(50);
2553
		}
2553
		}
2554
		if (retry < 5)
2554
		if (retry < 5)
2555
			break;
2555
			break;
2556
    }
2556
    }
2557
    if (i == 4)
2557
    if (i == 4)
2558
        DRM_ERROR("FDI train 2 fail!\n");
2558
        DRM_ERROR("FDI train 2 fail!\n");
2559
 
2559
 
2560
    DRM_DEBUG_KMS("FDI train done.\n");
2560
    DRM_DEBUG_KMS("FDI train done.\n");
2561
}
2561
}
2562
 
2562
 
2563
/* Manual link training for Ivy Bridge A0 parts */
2563
/* Manual link training for Ivy Bridge A0 parts */
2564
static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2564
static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2565
{
2565
{
2566
    struct drm_device *dev = crtc->dev;
2566
    struct drm_device *dev = crtc->dev;
2567
    struct drm_i915_private *dev_priv = dev->dev_private;
2567
    struct drm_i915_private *dev_priv = dev->dev_private;
2568
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2568
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2569
    int pipe = intel_crtc->pipe;
2569
    int pipe = intel_crtc->pipe;
2570
    u32 reg, temp, i;
2570
    u32 reg, temp, i;
2571
 
2571
 
2572
    /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2572
    /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2573
       for train result */
2573
       for train result */
2574
    reg = FDI_RX_IMR(pipe);
2574
    reg = FDI_RX_IMR(pipe);
2575
    temp = I915_READ(reg);
2575
    temp = I915_READ(reg);
2576
    temp &= ~FDI_RX_SYMBOL_LOCK;
2576
    temp &= ~FDI_RX_SYMBOL_LOCK;
2577
    temp &= ~FDI_RX_BIT_LOCK;
2577
    temp &= ~FDI_RX_BIT_LOCK;
2578
    I915_WRITE(reg, temp);
2578
    I915_WRITE(reg, temp);
2579
 
2579
 
2580
    POSTING_READ(reg);
2580
    POSTING_READ(reg);
2581
    udelay(150);
2581
    udelay(150);
2582
 
2582
 
2583
    /* enable CPU FDI TX and PCH FDI RX */
2583
    /* enable CPU FDI TX and PCH FDI RX */
2584
    reg = FDI_TX_CTL(pipe);
2584
    reg = FDI_TX_CTL(pipe);
2585
    temp = I915_READ(reg);
2585
    temp = I915_READ(reg);
2586
    temp &= ~(7 << 19);
2586
    temp &= ~(7 << 19);
2587
    temp |= (intel_crtc->fdi_lanes - 1) << 19;
2587
    temp |= (intel_crtc->fdi_lanes - 1) << 19;
2588
    temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2588
    temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2589
    temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
2589
    temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
2590
    temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2590
    temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2591
    temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2591
    temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2592
	temp |= FDI_COMPOSITE_SYNC;
2592
	temp |= FDI_COMPOSITE_SYNC;
2593
    I915_WRITE(reg, temp | FDI_TX_ENABLE);
2593
    I915_WRITE(reg, temp | FDI_TX_ENABLE);
2594
 
2594
 
2595
    reg = FDI_RX_CTL(pipe);
2595
    reg = FDI_RX_CTL(pipe);
2596
    temp = I915_READ(reg);
2596
    temp = I915_READ(reg);
2597
    temp &= ~FDI_LINK_TRAIN_AUTO;
2597
    temp &= ~FDI_LINK_TRAIN_AUTO;
2598
    temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2598
    temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2599
    temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2599
    temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2600
	temp |= FDI_COMPOSITE_SYNC;
2600
	temp |= FDI_COMPOSITE_SYNC;
2601
    I915_WRITE(reg, temp | FDI_RX_ENABLE);
2601
    I915_WRITE(reg, temp | FDI_RX_ENABLE);
2602
 
2602
 
2603
    POSTING_READ(reg);
2603
    POSTING_READ(reg);
2604
    udelay(150);
2604
    udelay(150);
2605
 
2605
 
2606
    if (HAS_PCH_CPT(dev))
2606
    if (HAS_PCH_CPT(dev))
2607
        cpt_phase_pointer_enable(dev, pipe);
2607
        cpt_phase_pointer_enable(dev, pipe);
2608
 
2608
 
2609
	for (i = 0; i < 4; i++) {
2609
	for (i = 0; i < 4; i++) {
2610
        reg = FDI_TX_CTL(pipe);
2610
        reg = FDI_TX_CTL(pipe);
2611
        temp = I915_READ(reg);
2611
        temp = I915_READ(reg);
2612
        temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2612
        temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2613
        temp |= snb_b_fdi_train_param[i];
2613
        temp |= snb_b_fdi_train_param[i];
2614
        I915_WRITE(reg, temp);
2614
        I915_WRITE(reg, temp);
2615
 
2615
 
2616
        POSTING_READ(reg);
2616
        POSTING_READ(reg);
2617
        udelay(500);
2617
        udelay(500);
2618
 
2618
 
2619
        reg = FDI_RX_IIR(pipe);
2619
        reg = FDI_RX_IIR(pipe);
2620
        temp = I915_READ(reg);
2620
        temp = I915_READ(reg);
2621
        DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2621
        DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2622
 
2622
 
2623
        if (temp & FDI_RX_BIT_LOCK ||
2623
        if (temp & FDI_RX_BIT_LOCK ||
2624
            (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2624
            (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2625
            I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2625
            I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2626
            DRM_DEBUG_KMS("FDI train 1 done.\n");
2626
            DRM_DEBUG_KMS("FDI train 1 done.\n");
2627
            break;
2627
            break;
2628
        }
2628
        }
2629
    }
2629
    }
2630
    if (i == 4)
2630
    if (i == 4)
2631
        DRM_ERROR("FDI train 1 fail!\n");
2631
        DRM_ERROR("FDI train 1 fail!\n");
2632
 
2632
 
2633
    /* Train 2 */
2633
    /* Train 2 */
2634
    reg = FDI_TX_CTL(pipe);
2634
    reg = FDI_TX_CTL(pipe);
2635
    temp = I915_READ(reg);
2635
    temp = I915_READ(reg);
2636
    temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2636
    temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2637
    temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2637
    temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2638
    temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2638
    temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2639
    temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2639
    temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2640
    I915_WRITE(reg, temp);
2640
    I915_WRITE(reg, temp);
2641
 
2641
 
2642
    reg = FDI_RX_CTL(pipe);
2642
    reg = FDI_RX_CTL(pipe);
2643
    temp = I915_READ(reg);
2643
    temp = I915_READ(reg);
2644
    temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2644
    temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2645
    temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2645
    temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2646
    I915_WRITE(reg, temp);
2646
    I915_WRITE(reg, temp);
2647
 
2647
 
2648
    POSTING_READ(reg);
2648
    POSTING_READ(reg);
2649
    udelay(150);
2649
    udelay(150);
2650
 
2650
 
2651
	for (i = 0; i < 4; i++) {
2651
	for (i = 0; i < 4; i++) {
2652
        reg = FDI_TX_CTL(pipe);
2652
        reg = FDI_TX_CTL(pipe);
2653
        temp = I915_READ(reg);
2653
        temp = I915_READ(reg);
2654
        temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2654
        temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2655
        temp |= snb_b_fdi_train_param[i];
2655
        temp |= snb_b_fdi_train_param[i];
2656
        I915_WRITE(reg, temp);
2656
        I915_WRITE(reg, temp);
2657
 
2657
 
2658
        POSTING_READ(reg);
2658
        POSTING_READ(reg);
2659
        udelay(500);
2659
        udelay(500);
2660
 
2660
 
2661
        reg = FDI_RX_IIR(pipe);
2661
        reg = FDI_RX_IIR(pipe);
2662
        temp = I915_READ(reg);
2662
        temp = I915_READ(reg);
2663
        DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2663
        DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2664
 
2664
 
2665
        if (temp & FDI_RX_SYMBOL_LOCK) {
2665
        if (temp & FDI_RX_SYMBOL_LOCK) {
2666
            I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2666
            I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2667
            DRM_DEBUG_KMS("FDI train 2 done.\n");
2667
            DRM_DEBUG_KMS("FDI train 2 done.\n");
2668
            break;
2668
            break;
2669
        }
2669
        }
2670
    }
2670
    }
2671
    if (i == 4)
2671
    if (i == 4)
2672
        DRM_ERROR("FDI train 2 fail!\n");
2672
        DRM_ERROR("FDI train 2 fail!\n");
2673
 
2673
 
2674
    DRM_DEBUG_KMS("FDI train done.\n");
2674
    DRM_DEBUG_KMS("FDI train done.\n");
2675
}
2675
}
2676
 
2676
 
2677
static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2677
static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2678
{
2678
{
2679
	struct drm_device *dev = intel_crtc->base.dev;
2679
	struct drm_device *dev = intel_crtc->base.dev;
2680
	struct drm_i915_private *dev_priv = dev->dev_private;
2680
	struct drm_i915_private *dev_priv = dev->dev_private;
2681
	int pipe = intel_crtc->pipe;
2681
	int pipe = intel_crtc->pipe;
2682
	u32 reg, temp;
2682
	u32 reg, temp;
2683
 
2683
 
2684
	/* Write the TU size bits so error detection works */
2684
	/* Write the TU size bits so error detection works */
2685
	I915_WRITE(FDI_RX_TUSIZE1(pipe),
2685
	I915_WRITE(FDI_RX_TUSIZE1(pipe),
2686
		   I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
2686
		   I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
2687
 
2687
 
2688
	/* enable PCH FDI RX PLL, wait warmup plus DMI latency */
2688
	/* enable PCH FDI RX PLL, wait warmup plus DMI latency */
2689
	reg = FDI_RX_CTL(pipe);
2689
	reg = FDI_RX_CTL(pipe);
2690
	temp = I915_READ(reg);
2690
	temp = I915_READ(reg);
2691
	temp &= ~((0x7 << 19) | (0x7 << 16));
2691
	temp &= ~((0x7 << 19) | (0x7 << 16));
2692
	temp |= (intel_crtc->fdi_lanes - 1) << 19;
2692
	temp |= (intel_crtc->fdi_lanes - 1) << 19;
2693
	temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2693
	temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2694
	I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2694
	I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2695
 
2695
 
2696
	POSTING_READ(reg);
2696
	POSTING_READ(reg);
2697
	udelay(200);
2697
	udelay(200);
2698
 
2698
 
2699
	/* Switch from Rawclk to PCDclk */
2699
	/* Switch from Rawclk to PCDclk */
2700
	temp = I915_READ(reg);
2700
	temp = I915_READ(reg);
2701
	I915_WRITE(reg, temp | FDI_PCDCLK);
2701
	I915_WRITE(reg, temp | FDI_PCDCLK);
2702
 
2702
 
2703
	POSTING_READ(reg);
2703
	POSTING_READ(reg);
2704
	udelay(200);
2704
	udelay(200);
2705
 
2705
 
2706
	/* On Haswell, the PLL configuration for ports and pipes is handled
2706
	/* On Haswell, the PLL configuration for ports and pipes is handled
2707
	 * separately, as part of DDI setup */
2707
	 * separately, as part of DDI setup */
2708
	if (!IS_HASWELL(dev)) {
2708
	if (!IS_HASWELL(dev)) {
2709
	/* Enable CPU FDI TX PLL, always on for Ironlake */
2709
	/* Enable CPU FDI TX PLL, always on for Ironlake */
2710
	reg = FDI_TX_CTL(pipe);
2710
	reg = FDI_TX_CTL(pipe);
2711
	temp = I915_READ(reg);
2711
	temp = I915_READ(reg);
2712
	if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2712
	if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2713
		I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
2713
		I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
2714
 
2714
 
2715
		POSTING_READ(reg);
2715
		POSTING_READ(reg);
2716
		udelay(100);
2716
		udelay(100);
2717
	}
2717
	}
2718
	}
2718
	}
2719
}
2719
}
2720
 
2720
 
2721
static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
2721
static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
2722
{
2722
{
2723
	struct drm_device *dev = intel_crtc->base.dev;
2723
	struct drm_device *dev = intel_crtc->base.dev;
2724
	struct drm_i915_private *dev_priv = dev->dev_private;
2724
	struct drm_i915_private *dev_priv = dev->dev_private;
2725
	int pipe = intel_crtc->pipe;
2725
	int pipe = intel_crtc->pipe;
2726
	u32 reg, temp;
2726
	u32 reg, temp;
2727
 
2727
 
2728
	/* Switch from PCDclk to Rawclk */
2728
	/* Switch from PCDclk to Rawclk */
2729
	reg = FDI_RX_CTL(pipe);
2729
	reg = FDI_RX_CTL(pipe);
2730
	temp = I915_READ(reg);
2730
	temp = I915_READ(reg);
2731
	I915_WRITE(reg, temp & ~FDI_PCDCLK);
2731
	I915_WRITE(reg, temp & ~FDI_PCDCLK);
2732
 
2732
 
2733
	/* Disable CPU FDI TX PLL */
2733
	/* Disable CPU FDI TX PLL */
2734
	reg = FDI_TX_CTL(pipe);
2734
	reg = FDI_TX_CTL(pipe);
2735
	temp = I915_READ(reg);
2735
	temp = I915_READ(reg);
2736
	I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2736
	I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2737
 
2737
 
2738
	POSTING_READ(reg);
2738
	POSTING_READ(reg);
2739
	udelay(100);
2739
	udelay(100);
2740
 
2740
 
2741
	reg = FDI_RX_CTL(pipe);
2741
	reg = FDI_RX_CTL(pipe);
2742
	temp = I915_READ(reg);
2742
	temp = I915_READ(reg);
2743
	I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2743
	I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2744
 
2744
 
2745
	/* Wait for the clocks to turn off. */
2745
	/* Wait for the clocks to turn off. */
2746
	POSTING_READ(reg);
2746
	POSTING_READ(reg);
2747
	udelay(100);
2747
	udelay(100);
2748
}
2748
}
2749
 
2749
 
2750
static void cpt_phase_pointer_disable(struct drm_device *dev, int pipe)
2750
static void cpt_phase_pointer_disable(struct drm_device *dev, int pipe)
2751
{
2751
{
2752
	struct drm_i915_private *dev_priv = dev->dev_private;
2752
	struct drm_i915_private *dev_priv = dev->dev_private;
2753
	u32 flags = I915_READ(SOUTH_CHICKEN1);
2753
	u32 flags = I915_READ(SOUTH_CHICKEN1);
2754
 
2754
 
2755
	flags &= ~(FDI_PHASE_SYNC_EN(pipe));
2755
	flags &= ~(FDI_PHASE_SYNC_EN(pipe));
2756
	I915_WRITE(SOUTH_CHICKEN1, flags); /* once to disable... */
2756
	I915_WRITE(SOUTH_CHICKEN1, flags); /* once to disable... */
2757
	flags &= ~(FDI_PHASE_SYNC_OVR(pipe));
2757
	flags &= ~(FDI_PHASE_SYNC_OVR(pipe));
2758
	I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to lock */
2758
	I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to lock */
2759
	POSTING_READ(SOUTH_CHICKEN1);
2759
	POSTING_READ(SOUTH_CHICKEN1);
2760
}
2760
}
2761
static void ironlake_fdi_disable(struct drm_crtc *crtc)
2761
static void ironlake_fdi_disable(struct drm_crtc *crtc)
2762
{
2762
{
2763
	struct drm_device *dev = crtc->dev;
2763
	struct drm_device *dev = crtc->dev;
2764
	struct drm_i915_private *dev_priv = dev->dev_private;
2764
	struct drm_i915_private *dev_priv = dev->dev_private;
2765
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2765
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2766
	int pipe = intel_crtc->pipe;
2766
	int pipe = intel_crtc->pipe;
2767
	u32 reg, temp;
2767
	u32 reg, temp;
2768
 
2768
 
2769
	/* disable CPU FDI tx and PCH FDI rx */
2769
	/* disable CPU FDI tx and PCH FDI rx */
2770
	reg = FDI_TX_CTL(pipe);
2770
	reg = FDI_TX_CTL(pipe);
2771
	temp = I915_READ(reg);
2771
	temp = I915_READ(reg);
2772
	I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2772
	I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2773
	POSTING_READ(reg);
2773
	POSTING_READ(reg);
2774
 
2774
 
2775
	reg = FDI_RX_CTL(pipe);
2775
	reg = FDI_RX_CTL(pipe);
2776
	temp = I915_READ(reg);
2776
	temp = I915_READ(reg);
2777
	temp &= ~(0x7 << 16);
2777
	temp &= ~(0x7 << 16);
2778
	temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2778
	temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2779
	I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2779
	I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2780
 
2780
 
2781
	POSTING_READ(reg);
2781
	POSTING_READ(reg);
2782
	udelay(100);
2782
	udelay(100);
2783
 
2783
 
2784
	/* Ironlake workaround, disable clock pointer after downing FDI */
2784
	/* Ironlake workaround, disable clock pointer after downing FDI */
2785
	if (HAS_PCH_IBX(dev)) {
2785
	if (HAS_PCH_IBX(dev)) {
2786
		I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2786
		I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2787
		I915_WRITE(FDI_RX_CHICKEN(pipe),
2787
		I915_WRITE(FDI_RX_CHICKEN(pipe),
2788
			   I915_READ(FDI_RX_CHICKEN(pipe) &
2788
			   I915_READ(FDI_RX_CHICKEN(pipe) &
2789
				     ~FDI_RX_PHASE_SYNC_POINTER_EN));
2789
				     ~FDI_RX_PHASE_SYNC_POINTER_EN));
2790
	} else if (HAS_PCH_CPT(dev)) {
2790
	} else if (HAS_PCH_CPT(dev)) {
2791
		cpt_phase_pointer_disable(dev, pipe);
2791
		cpt_phase_pointer_disable(dev, pipe);
2792
	}
2792
	}
2793
 
2793
 
2794
	/* still set train pattern 1 */
2794
	/* still set train pattern 1 */
2795
	reg = FDI_TX_CTL(pipe);
2795
	reg = FDI_TX_CTL(pipe);
2796
	temp = I915_READ(reg);
2796
	temp = I915_READ(reg);
2797
	temp &= ~FDI_LINK_TRAIN_NONE;
2797
	temp &= ~FDI_LINK_TRAIN_NONE;
2798
	temp |= FDI_LINK_TRAIN_PATTERN_1;
2798
	temp |= FDI_LINK_TRAIN_PATTERN_1;
2799
	I915_WRITE(reg, temp);
2799
	I915_WRITE(reg, temp);
2800
 
2800
 
2801
	reg = FDI_RX_CTL(pipe);
2801
	reg = FDI_RX_CTL(pipe);
2802
	temp = I915_READ(reg);
2802
	temp = I915_READ(reg);
2803
	if (HAS_PCH_CPT(dev)) {
2803
	if (HAS_PCH_CPT(dev)) {
2804
		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2804
		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2805
		temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2805
		temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2806
	} else {
2806
	} else {
2807
		temp &= ~FDI_LINK_TRAIN_NONE;
2807
		temp &= ~FDI_LINK_TRAIN_NONE;
2808
		temp |= FDI_LINK_TRAIN_PATTERN_1;
2808
		temp |= FDI_LINK_TRAIN_PATTERN_1;
2809
	}
2809
	}
2810
	/* BPC in FDI rx is consistent with that in PIPECONF */
2810
	/* BPC in FDI rx is consistent with that in PIPECONF */
2811
	temp &= ~(0x07 << 16);
2811
	temp &= ~(0x07 << 16);
2812
	temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2812
	temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2813
	I915_WRITE(reg, temp);
2813
	I915_WRITE(reg, temp);
2814
 
2814
 
2815
	POSTING_READ(reg);
2815
	POSTING_READ(reg);
2816
	udelay(100);
2816
	udelay(100);
2817
}
2817
}
2818
 
2818
 
2819
static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2819
static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2820
{
2820
{
2821
	struct drm_device *dev = crtc->dev;
2821
	struct drm_device *dev = crtc->dev;
2822
	struct drm_i915_private *dev_priv = dev->dev_private;
2822
	struct drm_i915_private *dev_priv = dev->dev_private;
2823
	unsigned long flags;
2823
	unsigned long flags;
2824
	bool pending;
2824
	bool pending;
2825
 
2825
 
2826
	if (atomic_read(&dev_priv->mm.wedged))
2826
	if (atomic_read(&dev_priv->mm.wedged))
2827
		return false;
2827
		return false;
2828
 
2828
 
2829
	spin_lock_irqsave(&dev->event_lock, flags);
2829
	spin_lock_irqsave(&dev->event_lock, flags);
2830
	pending = to_intel_crtc(crtc)->unpin_work != NULL;
2830
	pending = to_intel_crtc(crtc)->unpin_work != NULL;
2831
	spin_unlock_irqrestore(&dev->event_lock, flags);
2831
	spin_unlock_irqrestore(&dev->event_lock, flags);
2832
 
2832
 
2833
	return pending;
2833
	return pending;
2834
}
2834
}
2835
 
2835
 
2836
#if 0
2836
#if 0
2837
static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2837
static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2838
{
2838
{
2839
	struct drm_device *dev = crtc->dev;
2839
	struct drm_device *dev = crtc->dev;
2840
	struct drm_i915_private *dev_priv = dev->dev_private;
2840
	struct drm_i915_private *dev_priv = dev->dev_private;
2841
 
2841
 
2842
	if (crtc->fb == NULL)
2842
	if (crtc->fb == NULL)
2843
		return;
2843
		return;
2844
 
2844
 
2845
	wait_event(dev_priv->pending_flip_queue,
2845
	wait_event(dev_priv->pending_flip_queue,
2846
		   !intel_crtc_has_pending_flip(crtc));
2846
		   !intel_crtc_has_pending_flip(crtc));
2847
 
2847
 
2848
	mutex_lock(&dev->struct_mutex);
2848
	mutex_lock(&dev->struct_mutex);
2849
	intel_finish_fb(crtc->fb);
2849
	intel_finish_fb(crtc->fb);
2850
	mutex_unlock(&dev->struct_mutex);
2850
	mutex_unlock(&dev->struct_mutex);
2851
}
2851
}
2852
#endif
2852
#endif
2853
 
2853
 
2854
static bool intel_crtc_driving_pch(struct drm_crtc *crtc)
2854
static bool intel_crtc_driving_pch(struct drm_crtc *crtc)
2855
{
2855
{
2856
	struct drm_device *dev = crtc->dev;
2856
	struct drm_device *dev = crtc->dev;
2857
	struct intel_encoder *intel_encoder;
2857
	struct intel_encoder *intel_encoder;
2858
 
2858
 
2859
	/*
2859
	/*
2860
	 * If there's a non-PCH eDP on this crtc, it must be DP_A, and that
2860
	 * If there's a non-PCH eDP on this crtc, it must be DP_A, and that
2861
	 * must be driven by its own crtc; no sharing is possible.
2861
	 * must be driven by its own crtc; no sharing is possible.
2862
	 */
2862
	 */
2863
	for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
2863
	for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
2864
 
2864
 
2865
		/* On Haswell, LPT PCH handles the VGA connection via FDI, and Haswell
2865
		/* On Haswell, LPT PCH handles the VGA connection via FDI, and Haswell
2866
		 * CPU handles all others */
2866
		 * CPU handles all others */
2867
		if (IS_HASWELL(dev)) {
2867
		if (IS_HASWELL(dev)) {
2868
			/* It is still unclear how this will work on PPT, so throw up a warning */
2868
			/* It is still unclear how this will work on PPT, so throw up a warning */
2869
			WARN_ON(!HAS_PCH_LPT(dev));
2869
			WARN_ON(!HAS_PCH_LPT(dev));
2870
 
2870
 
2871
			if (intel_encoder->type == INTEL_OUTPUT_ANALOG) {
2871
			if (intel_encoder->type == INTEL_OUTPUT_ANALOG) {
2872
				DRM_DEBUG_KMS("Haswell detected DAC encoder, assuming is PCH\n");
2872
				DRM_DEBUG_KMS("Haswell detected DAC encoder, assuming is PCH\n");
2873
				return true;
2873
				return true;
2874
			} else {
2874
			} else {
2875
				DRM_DEBUG_KMS("Haswell detected encoder %d, assuming is CPU\n",
2875
				DRM_DEBUG_KMS("Haswell detected encoder %d, assuming is CPU\n",
2876
					      intel_encoder->type);
2876
					      intel_encoder->type);
2877
				return false;
2877
				return false;
2878
			}
2878
			}
2879
		}
2879
		}
2880
 
2880
 
2881
		switch (intel_encoder->type) {
2881
		switch (intel_encoder->type) {
2882
		case INTEL_OUTPUT_EDP:
2882
		case INTEL_OUTPUT_EDP:
2883
			if (!intel_encoder_is_pch_edp(&intel_encoder->base))
2883
			if (!intel_encoder_is_pch_edp(&intel_encoder->base))
2884
				return false;
2884
				return false;
2885
			continue;
2885
			continue;
2886
		}
2886
		}
2887
	}
2887
	}
2888
 
2888
 
2889
	return true;
2889
	return true;
2890
}
2890
}
2891
 
2891
 
2892
/* Program iCLKIP clock to the desired frequency */
2892
/* Program iCLKIP clock to the desired frequency */
2893
static void lpt_program_iclkip(struct drm_crtc *crtc)
2893
static void lpt_program_iclkip(struct drm_crtc *crtc)
2894
{
2894
{
2895
	struct drm_device *dev = crtc->dev;
2895
	struct drm_device *dev = crtc->dev;
2896
	struct drm_i915_private *dev_priv = dev->dev_private;
2896
	struct drm_i915_private *dev_priv = dev->dev_private;
2897
	u32 divsel, phaseinc, auxdiv, phasedir = 0;
2897
	u32 divsel, phaseinc, auxdiv, phasedir = 0;
2898
	u32 temp;
2898
	u32 temp;
2899
 
2899
 
2900
	/* It is necessary to ungate the pixclk gate prior to programming
2900
	/* It is necessary to ungate the pixclk gate prior to programming
2901
	 * the divisors, and gate it back when it is done.
2901
	 * the divisors, and gate it back when it is done.
2902
	 */
2902
	 */
2903
	I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
2903
	I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
2904
 
2904
 
2905
	/* Disable SSCCTL */
2905
	/* Disable SSCCTL */
2906
	intel_sbi_write(dev_priv, SBI_SSCCTL6,
2906
	intel_sbi_write(dev_priv, SBI_SSCCTL6,
2907
				intel_sbi_read(dev_priv, SBI_SSCCTL6) |
2907
				intel_sbi_read(dev_priv, SBI_SSCCTL6) |
2908
					SBI_SSCCTL_DISABLE);
2908
					SBI_SSCCTL_DISABLE);
2909
 
2909
 
2910
	/* 20MHz is a corner case which is out of range for the 7-bit divisor */
2910
	/* 20MHz is a corner case which is out of range for the 7-bit divisor */
2911
	if (crtc->mode.clock == 20000) {
2911
	if (crtc->mode.clock == 20000) {
2912
		auxdiv = 1;
2912
		auxdiv = 1;
2913
		divsel = 0x41;
2913
		divsel = 0x41;
2914
		phaseinc = 0x20;
2914
		phaseinc = 0x20;
2915
	} else {
2915
	} else {
2916
		/* The iCLK virtual clock root frequency is in MHz,
2916
		/* The iCLK virtual clock root frequency is in MHz,
2917
		 * but the crtc->mode.clock in in KHz. To get the divisors,
2917
		 * but the crtc->mode.clock in in KHz. To get the divisors,
2918
		 * it is necessary to divide one by another, so we
2918
		 * it is necessary to divide one by another, so we
2919
		 * convert the virtual clock precision to KHz here for higher
2919
		 * convert the virtual clock precision to KHz here for higher
2920
		 * precision.
2920
		 * precision.
2921
		 */
2921
		 */
2922
		u32 iclk_virtual_root_freq = 172800 * 1000;
2922
		u32 iclk_virtual_root_freq = 172800 * 1000;
2923
		u32 iclk_pi_range = 64;
2923
		u32 iclk_pi_range = 64;
2924
		u32 desired_divisor, msb_divisor_value, pi_value;
2924
		u32 desired_divisor, msb_divisor_value, pi_value;
2925
 
2925
 
2926
		desired_divisor = (iclk_virtual_root_freq / crtc->mode.clock);
2926
		desired_divisor = (iclk_virtual_root_freq / crtc->mode.clock);
2927
		msb_divisor_value = desired_divisor / iclk_pi_range;
2927
		msb_divisor_value = desired_divisor / iclk_pi_range;
2928
		pi_value = desired_divisor % iclk_pi_range;
2928
		pi_value = desired_divisor % iclk_pi_range;
2929
 
2929
 
2930
		auxdiv = 0;
2930
		auxdiv = 0;
2931
		divsel = msb_divisor_value - 2;
2931
		divsel = msb_divisor_value - 2;
2932
		phaseinc = pi_value;
2932
		phaseinc = pi_value;
2933
	}
2933
	}
2934
 
2934
 
2935
	/* This should not happen with any sane values */
2935
	/* This should not happen with any sane values */
2936
	WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
2936
	WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
2937
		~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
2937
		~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
2938
	WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
2938
	WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
2939
		~SBI_SSCDIVINTPHASE_INCVAL_MASK);
2939
		~SBI_SSCDIVINTPHASE_INCVAL_MASK);
2940
 
2940
 
2941
	DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
2941
	DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
2942
			crtc->mode.clock,
2942
			crtc->mode.clock,
2943
			auxdiv,
2943
			auxdiv,
2944
			divsel,
2944
			divsel,
2945
			phasedir,
2945
			phasedir,
2946
			phaseinc);
2946
			phaseinc);
2947
 
2947
 
2948
	/* Program SSCDIVINTPHASE6 */
2948
	/* Program SSCDIVINTPHASE6 */
2949
	temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6);
2949
	temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6);
2950
	temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
2950
	temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
2951
	temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
2951
	temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
2952
	temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
2952
	temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
2953
	temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
2953
	temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
2954
	temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
2954
	temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
2955
	temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
2955
	temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
2956
 
2956
 
2957
	intel_sbi_write(dev_priv,
2957
	intel_sbi_write(dev_priv,
2958
			SBI_SSCDIVINTPHASE6,
2958
			SBI_SSCDIVINTPHASE6,
2959
			temp);
2959
			temp);
2960
 
2960
 
2961
	/* Program SSCAUXDIV */
2961
	/* Program SSCAUXDIV */
2962
	temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6);
2962
	temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6);
2963
	temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
2963
	temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
2964
	temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
2964
	temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
2965
	intel_sbi_write(dev_priv,
2965
	intel_sbi_write(dev_priv,
2966
			SBI_SSCAUXDIV6,
2966
			SBI_SSCAUXDIV6,
2967
			temp);
2967
			temp);
2968
 
2968
 
2969
 
2969
 
2970
	/* Enable modulator and associated divider */
2970
	/* Enable modulator and associated divider */
2971
	temp = intel_sbi_read(dev_priv, SBI_SSCCTL6);
2971
	temp = intel_sbi_read(dev_priv, SBI_SSCCTL6);
2972
	temp &= ~SBI_SSCCTL_DISABLE;
2972
	temp &= ~SBI_SSCCTL_DISABLE;
2973
	intel_sbi_write(dev_priv,
2973
	intel_sbi_write(dev_priv,
2974
			SBI_SSCCTL6,
2974
			SBI_SSCCTL6,
2975
			temp);
2975
			temp);
2976
 
2976
 
2977
	/* Wait for initialization time */
2977
	/* Wait for initialization time */
2978
	udelay(24);
2978
	udelay(24);
2979
 
2979
 
2980
	I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
2980
	I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
2981
}
2981
}
2982
 
2982
 
2983
/*
2983
/*
2984
 * Enable PCH resources required for PCH ports:
2984
 * Enable PCH resources required for PCH ports:
2985
 *   - PCH PLLs
2985
 *   - PCH PLLs
2986
 *   - FDI training & RX/TX
2986
 *   - FDI training & RX/TX
2987
 *   - update transcoder timings
2987
 *   - update transcoder timings
2988
 *   - DP transcoding bits
2988
 *   - DP transcoding bits
2989
 *   - transcoder
2989
 *   - transcoder
2990
 */
2990
 */
2991
static void ironlake_pch_enable(struct drm_crtc *crtc)
2991
static void ironlake_pch_enable(struct drm_crtc *crtc)
2992
{
2992
{
2993
	struct drm_device *dev = crtc->dev;
2993
	struct drm_device *dev = crtc->dev;
2994
	struct drm_i915_private *dev_priv = dev->dev_private;
2994
	struct drm_i915_private *dev_priv = dev->dev_private;
2995
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2995
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2996
	int pipe = intel_crtc->pipe;
2996
	int pipe = intel_crtc->pipe;
2997
	u32 reg, temp;
2997
	u32 reg, temp;
2998
 
2998
 
2999
	assert_transcoder_disabled(dev_priv, pipe);
2999
	assert_transcoder_disabled(dev_priv, pipe);
3000
 
3000
 
3001
	/* For PCH output, training FDI link */
3001
	/* For PCH output, training FDI link */
3002
	dev_priv->display.fdi_link_train(crtc);
3002
	dev_priv->display.fdi_link_train(crtc);
3003
 
3003
 
3004
	intel_enable_pch_pll(intel_crtc);
3004
	intel_enable_pch_pll(intel_crtc);
3005
 
3005
 
3006
	if (HAS_PCH_LPT(dev)) {
3006
	if (HAS_PCH_LPT(dev)) {
3007
		DRM_DEBUG_KMS("LPT detected: programming iCLKIP\n");
3007
		DRM_DEBUG_KMS("LPT detected: programming iCLKIP\n");
3008
		lpt_program_iclkip(crtc);
3008
		lpt_program_iclkip(crtc);
3009
	} else if (HAS_PCH_CPT(dev)) {
3009
	} else if (HAS_PCH_CPT(dev)) {
3010
		u32 sel;
3010
		u32 sel;
3011
 
3011
 
3012
		temp = I915_READ(PCH_DPLL_SEL);
3012
		temp = I915_READ(PCH_DPLL_SEL);
3013
		switch (pipe) {
3013
		switch (pipe) {
3014
		default:
3014
		default:
3015
		case 0:
3015
		case 0:
3016
			temp |= TRANSA_DPLL_ENABLE;
3016
			temp |= TRANSA_DPLL_ENABLE;
3017
			sel = TRANSA_DPLLB_SEL;
3017
			sel = TRANSA_DPLLB_SEL;
3018
			break;
3018
			break;
3019
		case 1:
3019
		case 1:
3020
			temp |= TRANSB_DPLL_ENABLE;
3020
			temp |= TRANSB_DPLL_ENABLE;
3021
			sel = TRANSB_DPLLB_SEL;
3021
			sel = TRANSB_DPLLB_SEL;
3022
			break;
3022
			break;
3023
		case 2:
3023
		case 2:
3024
			temp |= TRANSC_DPLL_ENABLE;
3024
			temp |= TRANSC_DPLL_ENABLE;
3025
			sel = TRANSC_DPLLB_SEL;
3025
			sel = TRANSC_DPLLB_SEL;
3026
			break;
3026
			break;
3027
		}
3027
		}
3028
		if (intel_crtc->pch_pll->pll_reg == _PCH_DPLL_B)
3028
		if (intel_crtc->pch_pll->pll_reg == _PCH_DPLL_B)
3029
			temp |= sel;
3029
			temp |= sel;
3030
		else
3030
		else
3031
			temp &= ~sel;
3031
			temp &= ~sel;
3032
		I915_WRITE(PCH_DPLL_SEL, temp);
3032
		I915_WRITE(PCH_DPLL_SEL, temp);
3033
	}
3033
	}
3034
 
3034
 
3035
	/* set transcoder timing, panel must allow it */
3035
	/* set transcoder timing, panel must allow it */
3036
	assert_panel_unlocked(dev_priv, pipe);
3036
	assert_panel_unlocked(dev_priv, pipe);
3037
	I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
3037
	I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
3038
	I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
3038
	I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
3039
	I915_WRITE(TRANS_HSYNC(pipe),  I915_READ(HSYNC(pipe)));
3039
	I915_WRITE(TRANS_HSYNC(pipe),  I915_READ(HSYNC(pipe)));
3040
 
3040
 
3041
	I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
3041
	I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
3042
	I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
3042
	I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
3043
	I915_WRITE(TRANS_VSYNC(pipe),  I915_READ(VSYNC(pipe)));
3043
	I915_WRITE(TRANS_VSYNC(pipe),  I915_READ(VSYNC(pipe)));
3044
	I915_WRITE(TRANS_VSYNCSHIFT(pipe),  I915_READ(VSYNCSHIFT(pipe)));
3044
	I915_WRITE(TRANS_VSYNCSHIFT(pipe),  I915_READ(VSYNCSHIFT(pipe)));
3045
 
3045
 
3046
	if (!IS_HASWELL(dev))
3046
	if (!IS_HASWELL(dev))
3047
	intel_fdi_normal_train(crtc);
3047
	intel_fdi_normal_train(crtc);
3048
 
3048
 
3049
	/* For PCH DP, enable TRANS_DP_CTL */
3049
	/* For PCH DP, enable TRANS_DP_CTL */
3050
	if (HAS_PCH_CPT(dev) &&
3050
	if (HAS_PCH_CPT(dev) &&
3051
	    (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3051
	    (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3052
	     intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
3052
	     intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
3053
		u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) >> 5;
3053
		u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) >> 5;
3054
		reg = TRANS_DP_CTL(pipe);
3054
		reg = TRANS_DP_CTL(pipe);
3055
		temp = I915_READ(reg);
3055
		temp = I915_READ(reg);
3056
		temp &= ~(TRANS_DP_PORT_SEL_MASK |
3056
		temp &= ~(TRANS_DP_PORT_SEL_MASK |
3057
			  TRANS_DP_SYNC_MASK |
3057
			  TRANS_DP_SYNC_MASK |
3058
			  TRANS_DP_BPC_MASK);
3058
			  TRANS_DP_BPC_MASK);
3059
		temp |= (TRANS_DP_OUTPUT_ENABLE |
3059
		temp |= (TRANS_DP_OUTPUT_ENABLE |
3060
			 TRANS_DP_ENH_FRAMING);
3060
			 TRANS_DP_ENH_FRAMING);
3061
		temp |= bpc << 9; /* same format but at 11:9 */
3061
		temp |= bpc << 9; /* same format but at 11:9 */
3062
 
3062
 
3063
		if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
3063
		if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
3064
			temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
3064
			temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
3065
		if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
3065
		if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
3066
			temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
3066
			temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
3067
 
3067
 
3068
		switch (intel_trans_dp_port_sel(crtc)) {
3068
		switch (intel_trans_dp_port_sel(crtc)) {
3069
		case PCH_DP_B:
3069
		case PCH_DP_B:
3070
			temp |= TRANS_DP_PORT_SEL_B;
3070
			temp |= TRANS_DP_PORT_SEL_B;
3071
			break;
3071
			break;
3072
		case PCH_DP_C:
3072
		case PCH_DP_C:
3073
			temp |= TRANS_DP_PORT_SEL_C;
3073
			temp |= TRANS_DP_PORT_SEL_C;
3074
			break;
3074
			break;
3075
		case PCH_DP_D:
3075
		case PCH_DP_D:
3076
			temp |= TRANS_DP_PORT_SEL_D;
3076
			temp |= TRANS_DP_PORT_SEL_D;
3077
			break;
3077
			break;
3078
		default:
3078
		default:
3079
			DRM_DEBUG_KMS("Wrong PCH DP port return. Guess port B\n");
3079
			DRM_DEBUG_KMS("Wrong PCH DP port return. Guess port B\n");
3080
			temp |= TRANS_DP_PORT_SEL_B;
3080
			temp |= TRANS_DP_PORT_SEL_B;
3081
			break;
3081
			break;
3082
		}
3082
		}
3083
 
3083
 
3084
		I915_WRITE(reg, temp);
3084
		I915_WRITE(reg, temp);
3085
	}
3085
	}
3086
 
3086
 
3087
	intel_enable_transcoder(dev_priv, pipe);
3087
	intel_enable_transcoder(dev_priv, pipe);
3088
}
3088
}
3089
 
3089
 
3090
static void intel_put_pch_pll(struct intel_crtc *intel_crtc)
3090
static void intel_put_pch_pll(struct intel_crtc *intel_crtc)
3091
{
3091
{
3092
	struct intel_pch_pll *pll = intel_crtc->pch_pll;
3092
	struct intel_pch_pll *pll = intel_crtc->pch_pll;
3093
 
3093
 
3094
	if (pll == NULL)
3094
	if (pll == NULL)
3095
		return;
3095
		return;
3096
 
3096
 
3097
	if (pll->refcount == 0) {
3097
	if (pll->refcount == 0) {
3098
		WARN(1, "bad PCH PLL refcount\n");
3098
		WARN(1, "bad PCH PLL refcount\n");
3099
		return;
3099
		return;
3100
	}
3100
	}
3101
 
3101
 
3102
	--pll->refcount;
3102
	--pll->refcount;
3103
	intel_crtc->pch_pll = NULL;
3103
	intel_crtc->pch_pll = NULL;
3104
}
3104
}
3105
 
3105
 
3106
static struct intel_pch_pll *intel_get_pch_pll(struct intel_crtc *intel_crtc, u32 dpll, u32 fp)
3106
static struct intel_pch_pll *intel_get_pch_pll(struct intel_crtc *intel_crtc, u32 dpll, u32 fp)
3107
{
3107
{
3108
	struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
3108
	struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
3109
	struct intel_pch_pll *pll;
3109
	struct intel_pch_pll *pll;
3110
	int i;
3110
	int i;
3111
 
3111
 
3112
	pll = intel_crtc->pch_pll;
3112
	pll = intel_crtc->pch_pll;
3113
	if (pll) {
3113
	if (pll) {
3114
		DRM_DEBUG_KMS("CRTC:%d reusing existing PCH PLL %x\n",
3114
		DRM_DEBUG_KMS("CRTC:%d reusing existing PCH PLL %x\n",
3115
			      intel_crtc->base.base.id, pll->pll_reg);
3115
			      intel_crtc->base.base.id, pll->pll_reg);
3116
		goto prepare;
3116
		goto prepare;
3117
	}
3117
	}
3118
 
3118
 
3119
	if (HAS_PCH_IBX(dev_priv->dev)) {
3119
	if (HAS_PCH_IBX(dev_priv->dev)) {
3120
		/* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
3120
		/* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
3121
		i = intel_crtc->pipe;
3121
		i = intel_crtc->pipe;
3122
		pll = &dev_priv->pch_plls[i];
3122
		pll = &dev_priv->pch_plls[i];
3123
 
3123
 
3124
		DRM_DEBUG_KMS("CRTC:%d using pre-allocated PCH PLL %x\n",
3124
		DRM_DEBUG_KMS("CRTC:%d using pre-allocated PCH PLL %x\n",
3125
			      intel_crtc->base.base.id, pll->pll_reg);
3125
			      intel_crtc->base.base.id, pll->pll_reg);
3126
 
3126
 
3127
		goto found;
3127
		goto found;
3128
	}
3128
	}
3129
 
3129
 
3130
	for (i = 0; i < dev_priv->num_pch_pll; i++) {
3130
	for (i = 0; i < dev_priv->num_pch_pll; i++) {
3131
		pll = &dev_priv->pch_plls[i];
3131
		pll = &dev_priv->pch_plls[i];
3132
 
3132
 
3133
		/* Only want to check enabled timings first */
3133
		/* Only want to check enabled timings first */
3134
		if (pll->refcount == 0)
3134
		if (pll->refcount == 0)
3135
			continue;
3135
			continue;
3136
 
3136
 
3137
		if (dpll == (I915_READ(pll->pll_reg) & 0x7fffffff) &&
3137
		if (dpll == (I915_READ(pll->pll_reg) & 0x7fffffff) &&
3138
		    fp == I915_READ(pll->fp0_reg)) {
3138
		    fp == I915_READ(pll->fp0_reg)) {
3139
			DRM_DEBUG_KMS("CRTC:%d sharing existing PCH PLL %x (refcount %d, ative %d)\n",
3139
			DRM_DEBUG_KMS("CRTC:%d sharing existing PCH PLL %x (refcount %d, ative %d)\n",
3140
				      intel_crtc->base.base.id,
3140
				      intel_crtc->base.base.id,
3141
				      pll->pll_reg, pll->refcount, pll->active);
3141
				      pll->pll_reg, pll->refcount, pll->active);
3142
 
3142
 
3143
			goto found;
3143
			goto found;
3144
		}
3144
		}
3145
	}
3145
	}
3146
 
3146
 
3147
	/* Ok no matching timings, maybe there's a free one? */
3147
	/* Ok no matching timings, maybe there's a free one? */
3148
	for (i = 0; i < dev_priv->num_pch_pll; i++) {
3148
	for (i = 0; i < dev_priv->num_pch_pll; i++) {
3149
		pll = &dev_priv->pch_plls[i];
3149
		pll = &dev_priv->pch_plls[i];
3150
		if (pll->refcount == 0) {
3150
		if (pll->refcount == 0) {
3151
			DRM_DEBUG_KMS("CRTC:%d allocated PCH PLL %x\n",
3151
			DRM_DEBUG_KMS("CRTC:%d allocated PCH PLL %x\n",
3152
				      intel_crtc->base.base.id, pll->pll_reg);
3152
				      intel_crtc->base.base.id, pll->pll_reg);
3153
			goto found;
3153
			goto found;
3154
		}
3154
		}
3155
	}
3155
	}
3156
 
3156
 
3157
	return NULL;
3157
	return NULL;
3158
 
3158
 
3159
found:
3159
found:
3160
	intel_crtc->pch_pll = pll;
3160
	intel_crtc->pch_pll = pll;
3161
	pll->refcount++;
3161
	pll->refcount++;
3162
	DRM_DEBUG_DRIVER("using pll %d for pipe %d\n", i, intel_crtc->pipe);
3162
	DRM_DEBUG_DRIVER("using pll %d for pipe %d\n", i, intel_crtc->pipe);
3163
prepare: /* separate function? */
3163
prepare: /* separate function? */
3164
	DRM_DEBUG_DRIVER("switching PLL %x off\n", pll->pll_reg);
3164
	DRM_DEBUG_DRIVER("switching PLL %x off\n", pll->pll_reg);
3165
 
3165
 
3166
	/* Wait for the clocks to stabilize before rewriting the regs */
3166
	/* Wait for the clocks to stabilize before rewriting the regs */
3167
	I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
3167
	I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
3168
	POSTING_READ(pll->pll_reg);
3168
	POSTING_READ(pll->pll_reg);
3169
	udelay(150);
3169
	udelay(150);
3170
 
3170
 
3171
	I915_WRITE(pll->fp0_reg, fp);
3171
	I915_WRITE(pll->fp0_reg, fp);
3172
	I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
3172
	I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
3173
	pll->on = false;
3173
	pll->on = false;
3174
	return pll;
3174
	return pll;
3175
}
3175
}
3176
 
3176
 
3177
void intel_cpt_verify_modeset(struct drm_device *dev, int pipe)
3177
void intel_cpt_verify_modeset(struct drm_device *dev, int pipe)
3178
{
3178
{
3179
	struct drm_i915_private *dev_priv = dev->dev_private;
3179
	struct drm_i915_private *dev_priv = dev->dev_private;
3180
	int dslreg = PIPEDSL(pipe), tc2reg = TRANS_CHICKEN2(pipe);
3180
	int dslreg = PIPEDSL(pipe), tc2reg = TRANS_CHICKEN2(pipe);
3181
	u32 temp;
3181
	u32 temp;
3182
 
3182
 
3183
	temp = I915_READ(dslreg);
3183
	temp = I915_READ(dslreg);
3184
	udelay(500);
3184
	udelay(500);
3185
	if (wait_for(I915_READ(dslreg) != temp, 5)) {
3185
	if (wait_for(I915_READ(dslreg) != temp, 5)) {
3186
		/* Without this, mode sets may fail silently on FDI */
3186
		/* Without this, mode sets may fail silently on FDI */
3187
		I915_WRITE(tc2reg, TRANS_AUTOTRAIN_GEN_STALL_DIS);
3187
		I915_WRITE(tc2reg, TRANS_AUTOTRAIN_GEN_STALL_DIS);
3188
		udelay(250);
3188
		udelay(250);
3189
		I915_WRITE(tc2reg, 0);
3189
		I915_WRITE(tc2reg, 0);
3190
		if (wait_for(I915_READ(dslreg) != temp, 5))
3190
		if (wait_for(I915_READ(dslreg) != temp, 5))
3191
			DRM_ERROR("mode set failed: pipe %d stuck\n", pipe);
3191
			DRM_ERROR("mode set failed: pipe %d stuck\n", pipe);
3192
	}
3192
	}
3193
}
3193
}
3194
 
3194
 
3195
static void ironlake_crtc_enable(struct drm_crtc *crtc)
3195
static void ironlake_crtc_enable(struct drm_crtc *crtc)
3196
{
3196
{
3197
    struct drm_device *dev = crtc->dev;
3197
    struct drm_device *dev = crtc->dev;
3198
    struct drm_i915_private *dev_priv = dev->dev_private;
3198
    struct drm_i915_private *dev_priv = dev->dev_private;
3199
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3199
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3200
	struct intel_encoder *encoder;
3200
	struct intel_encoder *encoder;
3201
    int pipe = intel_crtc->pipe;
3201
    int pipe = intel_crtc->pipe;
3202
    int plane = intel_crtc->plane;
3202
    int plane = intel_crtc->plane;
3203
    u32 temp;
3203
    u32 temp;
3204
    bool is_pch_port;
3204
    bool is_pch_port;
3205
 
3205
 
3206
	WARN_ON(!crtc->enabled);
3206
	WARN_ON(!crtc->enabled);
3207
 
3207
 
3208
    if (intel_crtc->active)
3208
    if (intel_crtc->active)
3209
        return;
3209
        return;
3210
 
3210
 
3211
    intel_crtc->active = true;
3211
    intel_crtc->active = true;
3212
    intel_update_watermarks(dev);
3212
    intel_update_watermarks(dev);
3213
 
3213
 
3214
    if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
3214
    if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
3215
        temp = I915_READ(PCH_LVDS);
3215
        temp = I915_READ(PCH_LVDS);
3216
        if ((temp & LVDS_PORT_EN) == 0)
3216
        if ((temp & LVDS_PORT_EN) == 0)
3217
            I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
3217
            I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
3218
    }
3218
    }
3219
 
3219
 
3220
    is_pch_port = intel_crtc_driving_pch(crtc);
3220
    is_pch_port = intel_crtc_driving_pch(crtc);
3221
 
3221
 
3222
	if (is_pch_port) {
3222
	if (is_pch_port) {
3223
		ironlake_fdi_pll_enable(intel_crtc);
3223
		ironlake_fdi_pll_enable(intel_crtc);
3224
	} else {
3224
	} else {
3225
		assert_fdi_tx_disabled(dev_priv, pipe);
3225
		assert_fdi_tx_disabled(dev_priv, pipe);
3226
		assert_fdi_rx_disabled(dev_priv, pipe);
3226
		assert_fdi_rx_disabled(dev_priv, pipe);
3227
	}
3227
	}
3228
 
3228
 
3229
	for_each_encoder_on_crtc(dev, crtc, encoder)
3229
	for_each_encoder_on_crtc(dev, crtc, encoder)
3230
		if (encoder->pre_enable)
3230
		if (encoder->pre_enable)
3231
			encoder->pre_enable(encoder);
3231
			encoder->pre_enable(encoder);
3232
 
3232
 
3233
    /* Enable panel fitting for LVDS */
3233
    /* Enable panel fitting for LVDS */
3234
    if (dev_priv->pch_pf_size &&
3234
    if (dev_priv->pch_pf_size &&
3235
        (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) || HAS_eDP)) {
3235
        (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) || HAS_eDP)) {
3236
        /* Force use of hard-coded filter coefficients
3236
        /* Force use of hard-coded filter coefficients
3237
         * as some pre-programmed values are broken,
3237
         * as some pre-programmed values are broken,
3238
         * e.g. x201.
3238
         * e.g. x201.
3239
         */
3239
         */
3240
        I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3240
        I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3241
        I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
3241
        I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
3242
        I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
3242
        I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
3243
    }
3243
    }
3244
 
3244
 
3245
    /*
3245
    /*
3246
     * On ILK+ LUT must be loaded before the pipe is running but with
3246
     * On ILK+ LUT must be loaded before the pipe is running but with
3247
     * clocks enabled
3247
     * clocks enabled
3248
     */
3248
     */
3249
    intel_crtc_load_lut(crtc);
3249
    intel_crtc_load_lut(crtc);
3250
 
3250
 
3251
    intel_enable_pipe(dev_priv, pipe, is_pch_port);
3251
    intel_enable_pipe(dev_priv, pipe, is_pch_port);
3252
    intel_enable_plane(dev_priv, plane, pipe);
3252
    intel_enable_plane(dev_priv, plane, pipe);
3253
 
3253
 
3254
    if (is_pch_port)
3254
    if (is_pch_port)
3255
        ironlake_pch_enable(crtc);
3255
        ironlake_pch_enable(crtc);
3256
 
3256
 
3257
    mutex_lock(&dev->struct_mutex);
3257
    mutex_lock(&dev->struct_mutex);
3258
    intel_update_fbc(dev);
3258
    intel_update_fbc(dev);
3259
    mutex_unlock(&dev->struct_mutex);
3259
    mutex_unlock(&dev->struct_mutex);
3260
 
3260
 
3261
//    intel_crtc_update_cursor(crtc, true);
3261
//    intel_crtc_update_cursor(crtc, true);
3262
 
3262
 
3263
	for_each_encoder_on_crtc(dev, crtc, encoder)
3263
	for_each_encoder_on_crtc(dev, crtc, encoder)
3264
		encoder->enable(encoder);
3264
		encoder->enable(encoder);
3265
 
3265
 
3266
	if (HAS_PCH_CPT(dev))
3266
	if (HAS_PCH_CPT(dev))
3267
		intel_cpt_verify_modeset(dev, intel_crtc->pipe);
3267
		intel_cpt_verify_modeset(dev, intel_crtc->pipe);
3268
 
3268
 
3269
	/*
3269
	/*
3270
	 * There seems to be a race in PCH platform hw (at least on some
3270
	 * There seems to be a race in PCH platform hw (at least on some
3271
	 * outputs) where an enabled pipe still completes any pageflip right
3271
	 * outputs) where an enabled pipe still completes any pageflip right
3272
	 * away (as if the pipe is off) instead of waiting for vblank. As soon
3272
	 * away (as if the pipe is off) instead of waiting for vblank. As soon
3273
	 * as the first vblank happend, everything works as expected. Hence just
3273
	 * as the first vblank happend, everything works as expected. Hence just
3274
	 * wait for one vblank before returning to avoid strange things
3274
	 * wait for one vblank before returning to avoid strange things
3275
	 * happening.
3275
	 * happening.
3276
	 */
3276
	 */
3277
	intel_wait_for_vblank(dev, intel_crtc->pipe);
3277
	intel_wait_for_vblank(dev, intel_crtc->pipe);
3278
}
3278
}
3279
 
3279
 
3280
static void ironlake_crtc_disable(struct drm_crtc *crtc)
3280
static void ironlake_crtc_disable(struct drm_crtc *crtc)
3281
{
3281
{
3282
    struct drm_device *dev = crtc->dev;
3282
    struct drm_device *dev = crtc->dev;
3283
    struct drm_i915_private *dev_priv = dev->dev_private;
3283
    struct drm_i915_private *dev_priv = dev->dev_private;
3284
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3284
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3285
	struct intel_encoder *encoder;
3285
	struct intel_encoder *encoder;
3286
    int pipe = intel_crtc->pipe;
3286
    int pipe = intel_crtc->pipe;
3287
    int plane = intel_crtc->plane;
3287
    int plane = intel_crtc->plane;
3288
    u32 reg, temp;
3288
    u32 reg, temp;
3289
 
3289
 
3290
 
3290
 
3291
    if (!intel_crtc->active)
3291
    if (!intel_crtc->active)
3292
        return;
3292
        return;
3293
 
3293
 
3294
	for_each_encoder_on_crtc(dev, crtc, encoder)
3294
	for_each_encoder_on_crtc(dev, crtc, encoder)
3295
		encoder->disable(encoder);
3295
		encoder->disable(encoder);
3296
 
3296
 
3297
//    intel_crtc_wait_for_pending_flips(crtc);
3297
//    intel_crtc_wait_for_pending_flips(crtc);
3298
//    drm_vblank_off(dev, pipe);
3298
//    drm_vblank_off(dev, pipe);
3299
//    intel_crtc_update_cursor(crtc, false);
3299
//    intel_crtc_update_cursor(crtc, false);
3300
 
3300
 
3301
    intel_disable_plane(dev_priv, plane, pipe);
3301
    intel_disable_plane(dev_priv, plane, pipe);
3302
 
3302
 
3303
    if (dev_priv->cfb_plane == plane)
3303
    if (dev_priv->cfb_plane == plane)
3304
        intel_disable_fbc(dev);
3304
        intel_disable_fbc(dev);
3305
 
3305
 
3306
    intel_disable_pipe(dev_priv, pipe);
3306
    intel_disable_pipe(dev_priv, pipe);
3307
 
3307
 
3308
    /* Disable PF */
3308
    /* Disable PF */
3309
    I915_WRITE(PF_CTL(pipe), 0);
3309
    I915_WRITE(PF_CTL(pipe), 0);
3310
    I915_WRITE(PF_WIN_SZ(pipe), 0);
3310
    I915_WRITE(PF_WIN_SZ(pipe), 0);
3311
 
3311
 
3312
	for_each_encoder_on_crtc(dev, crtc, encoder)
3312
	for_each_encoder_on_crtc(dev, crtc, encoder)
3313
		if (encoder->post_disable)
3313
		if (encoder->post_disable)
3314
			encoder->post_disable(encoder);
3314
			encoder->post_disable(encoder);
3315
 
3315
 
3316
    ironlake_fdi_disable(crtc);
3316
    ironlake_fdi_disable(crtc);
3317
 
3317
 
3318
    intel_disable_transcoder(dev_priv, pipe);
3318
    intel_disable_transcoder(dev_priv, pipe);
3319
 
3319
 
3320
    if (HAS_PCH_CPT(dev)) {
3320
    if (HAS_PCH_CPT(dev)) {
3321
        /* disable TRANS_DP_CTL */
3321
        /* disable TRANS_DP_CTL */
3322
        reg = TRANS_DP_CTL(pipe);
3322
        reg = TRANS_DP_CTL(pipe);
3323
        temp = I915_READ(reg);
3323
        temp = I915_READ(reg);
3324
        temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
3324
        temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
3325
        temp |= TRANS_DP_PORT_SEL_NONE;
3325
        temp |= TRANS_DP_PORT_SEL_NONE;
3326
        I915_WRITE(reg, temp);
3326
        I915_WRITE(reg, temp);
3327
 
3327
 
3328
        /* disable DPLL_SEL */
3328
        /* disable DPLL_SEL */
3329
        temp = I915_READ(PCH_DPLL_SEL);
3329
        temp = I915_READ(PCH_DPLL_SEL);
3330
        switch (pipe) {
3330
        switch (pipe) {
3331
        case 0:
3331
        case 0:
3332
			temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
3332
			temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
3333
            break;
3333
            break;
3334
        case 1:
3334
        case 1:
3335
            temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
3335
            temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
3336
            break;
3336
            break;
3337
        case 2:
3337
        case 2:
3338
			/* C shares PLL A or B */
3338
			/* C shares PLL A or B */
3339
            temp &= ~(TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);
3339
            temp &= ~(TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);
3340
            break;
3340
            break;
3341
        default:
3341
        default:
3342
            BUG(); /* wtf */
3342
            BUG(); /* wtf */
3343
        }
3343
        }
3344
        I915_WRITE(PCH_DPLL_SEL, temp);
3344
        I915_WRITE(PCH_DPLL_SEL, temp);
3345
    }
3345
    }
3346
 
3346
 
3347
    /* disable PCH DPLL */
3347
    /* disable PCH DPLL */
3348
	intel_disable_pch_pll(intel_crtc);
3348
	intel_disable_pch_pll(intel_crtc);
3349
 
3349
 
3350
	ironlake_fdi_pll_disable(intel_crtc);
3350
	ironlake_fdi_pll_disable(intel_crtc);
3351
 
3351
 
3352
    intel_crtc->active = false;
3352
    intel_crtc->active = false;
3353
    intel_update_watermarks(dev);
3353
    intel_update_watermarks(dev);
3354
 
3354
 
3355
    mutex_lock(&dev->struct_mutex);
3355
    mutex_lock(&dev->struct_mutex);
3356
    intel_update_fbc(dev);
3356
    intel_update_fbc(dev);
3357
    mutex_unlock(&dev->struct_mutex);
3357
    mutex_unlock(&dev->struct_mutex);
3358
}
3358
}
3359
 
3359
 
3360
static void ironlake_crtc_off(struct drm_crtc *crtc)
3360
static void ironlake_crtc_off(struct drm_crtc *crtc)
3361
{
3361
{
3362
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3362
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3363
	intel_put_pch_pll(intel_crtc);
3363
	intel_put_pch_pll(intel_crtc);
3364
}
3364
}
3365
 
3365
 
3366
static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3366
static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3367
{
3367
{
3368
	if (!enable && intel_crtc->overlay) {
3368
	if (!enable && intel_crtc->overlay) {
3369
		struct drm_device *dev = intel_crtc->base.dev;
3369
		struct drm_device *dev = intel_crtc->base.dev;
3370
		struct drm_i915_private *dev_priv = dev->dev_private;
3370
		struct drm_i915_private *dev_priv = dev->dev_private;
3371
 
3371
 
3372
		mutex_lock(&dev->struct_mutex);
3372
		mutex_lock(&dev->struct_mutex);
3373
		dev_priv->mm.interruptible = false;
3373
		dev_priv->mm.interruptible = false;
3374
//       (void) intel_overlay_switch_off(intel_crtc->overlay);
3374
//       (void) intel_overlay_switch_off(intel_crtc->overlay);
3375
		dev_priv->mm.interruptible = true;
3375
		dev_priv->mm.interruptible = true;
3376
		mutex_unlock(&dev->struct_mutex);
3376
		mutex_unlock(&dev->struct_mutex);
3377
	}
3377
	}
3378
 
3378
 
3379
	/* Let userspace switch the overlay on again. In most cases userspace
3379
	/* Let userspace switch the overlay on again. In most cases userspace
3380
	 * has to recompute where to put it anyway.
3380
	 * has to recompute where to put it anyway.
3381
	 */
3381
	 */
3382
}
3382
}
3383
 
3383
 
3384
static void i9xx_crtc_enable(struct drm_crtc *crtc)
3384
static void i9xx_crtc_enable(struct drm_crtc *crtc)
3385
{
3385
{
3386
    struct drm_device *dev = crtc->dev;
3386
    struct drm_device *dev = crtc->dev;
3387
    struct drm_i915_private *dev_priv = dev->dev_private;
3387
    struct drm_i915_private *dev_priv = dev->dev_private;
3388
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3388
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3389
	struct intel_encoder *encoder;
3389
	struct intel_encoder *encoder;
3390
    int pipe = intel_crtc->pipe;
3390
    int pipe = intel_crtc->pipe;
3391
    int plane = intel_crtc->plane;
3391
    int plane = intel_crtc->plane;
3392
 
3392
 
3393
	WARN_ON(!crtc->enabled);
3393
	WARN_ON(!crtc->enabled);
3394
 
3394
 
3395
    if (intel_crtc->active)
3395
    if (intel_crtc->active)
3396
        return;
3396
        return;
3397
 
3397
 
3398
    intel_crtc->active = true;
3398
    intel_crtc->active = true;
3399
    intel_update_watermarks(dev);
3399
    intel_update_watermarks(dev);
3400
 
3400
 
3401
    intel_enable_pll(dev_priv, pipe);
3401
    intel_enable_pll(dev_priv, pipe);
3402
    intel_enable_pipe(dev_priv, pipe, false);
3402
    intel_enable_pipe(dev_priv, pipe, false);
3403
    intel_enable_plane(dev_priv, plane, pipe);
3403
    intel_enable_plane(dev_priv, plane, pipe);
3404
 
3404
 
3405
    intel_crtc_load_lut(crtc);
3405
    intel_crtc_load_lut(crtc);
3406
    intel_update_fbc(dev);
3406
    intel_update_fbc(dev);
3407
 
3407
 
3408
    /* Give the overlay scaler a chance to enable if it's on this pipe */
3408
    /* Give the overlay scaler a chance to enable if it's on this pipe */
3409
    intel_crtc_dpms_overlay(intel_crtc, true);
3409
    intel_crtc_dpms_overlay(intel_crtc, true);
3410
//    intel_crtc_update_cursor(crtc, true);
3410
//    intel_crtc_update_cursor(crtc, true);
3411
 
3411
 
3412
	for_each_encoder_on_crtc(dev, crtc, encoder)
3412
	for_each_encoder_on_crtc(dev, crtc, encoder)
3413
		encoder->enable(encoder);
3413
		encoder->enable(encoder);
3414
}
3414
}
3415
 
3415
 
3416
static void i9xx_crtc_disable(struct drm_crtc *crtc)
3416
static void i9xx_crtc_disable(struct drm_crtc *crtc)
3417
{
3417
{
3418
    struct drm_device *dev = crtc->dev;
3418
    struct drm_device *dev = crtc->dev;
3419
    struct drm_i915_private *dev_priv = dev->dev_private;
3419
    struct drm_i915_private *dev_priv = dev->dev_private;
3420
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3420
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3421
	struct intel_encoder *encoder;
3421
	struct intel_encoder *encoder;
3422
    int pipe = intel_crtc->pipe;
3422
    int pipe = intel_crtc->pipe;
3423
    int plane = intel_crtc->plane;
3423
    int plane = intel_crtc->plane;
3424
 
3424
 
3425
 
3425
 
3426
    if (!intel_crtc->active)
3426
    if (!intel_crtc->active)
3427
        return;
3427
        return;
3428
 
3428
 
3429
	for_each_encoder_on_crtc(dev, crtc, encoder)
3429
	for_each_encoder_on_crtc(dev, crtc, encoder)
3430
		encoder->disable(encoder);
3430
		encoder->disable(encoder);
3431
 
3431
 
3432
    /* Give the overlay scaler a chance to disable if it's on this pipe */
3432
    /* Give the overlay scaler a chance to disable if it's on this pipe */
3433
//    intel_crtc_wait_for_pending_flips(crtc);
3433
//    intel_crtc_wait_for_pending_flips(crtc);
3434
//    drm_vblank_off(dev, pipe);
3434
//    drm_vblank_off(dev, pipe);
3435
    intel_crtc_dpms_overlay(intel_crtc, false);
3435
    intel_crtc_dpms_overlay(intel_crtc, false);
3436
//    intel_crtc_update_cursor(crtc, false);
3436
//    intel_crtc_update_cursor(crtc, false);
3437
 
3437
 
3438
    if (dev_priv->cfb_plane == plane)
3438
    if (dev_priv->cfb_plane == plane)
3439
        intel_disable_fbc(dev);
3439
        intel_disable_fbc(dev);
3440
 
3440
 
3441
    intel_disable_plane(dev_priv, plane, pipe);
3441
    intel_disable_plane(dev_priv, plane, pipe);
3442
    intel_disable_pipe(dev_priv, pipe);
3442
    intel_disable_pipe(dev_priv, pipe);
3443
    intel_disable_pll(dev_priv, pipe);
3443
    intel_disable_pll(dev_priv, pipe);
3444
 
3444
 
3445
    intel_crtc->active = false;
3445
    intel_crtc->active = false;
3446
    intel_update_fbc(dev);
3446
    intel_update_fbc(dev);
3447
    intel_update_watermarks(dev);
3447
    intel_update_watermarks(dev);
3448
}
3448
}
3449
 
3449
 
3450
static void i9xx_crtc_off(struct drm_crtc *crtc)
3450
static void i9xx_crtc_off(struct drm_crtc *crtc)
3451
{
3451
{
3452
}
3452
}
3453
 
3453
 
3454
static void intel_crtc_update_sarea(struct drm_crtc *crtc,
3454
static void intel_crtc_update_sarea(struct drm_crtc *crtc,
3455
				    bool enabled)
3455
				    bool enabled)
3456
{
3456
{
3457
	struct drm_device *dev = crtc->dev;
3457
	struct drm_device *dev = crtc->dev;
3458
	struct drm_i915_master_private *master_priv;
3458
	struct drm_i915_master_private *master_priv;
3459
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3459
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3460
	int pipe = intel_crtc->pipe;
3460
	int pipe = intel_crtc->pipe;
3461
 
3461
 
3462
 
3462
 
3463
#if 0
3463
#if 0
3464
	if (!dev->primary->master)
3464
	if (!dev->primary->master)
3465
		return;
3465
		return;
3466
 
3466
 
3467
	master_priv = dev->primary->master->driver_priv;
3467
	master_priv = dev->primary->master->driver_priv;
3468
	if (!master_priv->sarea_priv)
3468
	if (!master_priv->sarea_priv)
3469
		return;
3469
		return;
3470
 
3470
 
3471
	switch (pipe) {
3471
	switch (pipe) {
3472
	case 0:
3472
	case 0:
3473
		master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
3473
		master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
3474
		master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
3474
		master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
3475
		break;
3475
		break;
3476
	case 1:
3476
	case 1:
3477
		master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
3477
		master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
3478
		master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
3478
		master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
3479
		break;
3479
		break;
3480
	default:
3480
	default:
3481
		DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
3481
		DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
3482
		break;
3482
		break;
3483
	}
3483
	}
3484
#endif
3484
#endif
3485
 
3485
 
3486
}
3486
}
3487
 
3487
 
3488
/**
3488
/**
3489
 * Sets the power management mode of the pipe and plane.
3489
 * Sets the power management mode of the pipe and plane.
3490
 */
3490
 */
3491
void intel_crtc_update_dpms(struct drm_crtc *crtc)
3491
void intel_crtc_update_dpms(struct drm_crtc *crtc)
3492
{
3492
{
3493
	struct drm_device *dev = crtc->dev;
3493
	struct drm_device *dev = crtc->dev;
3494
	struct drm_i915_private *dev_priv = dev->dev_private;
3494
	struct drm_i915_private *dev_priv = dev->dev_private;
3495
	struct intel_encoder *intel_encoder;
3495
	struct intel_encoder *intel_encoder;
3496
	bool enable = false;
3496
	bool enable = false;
3497
 
3497
 
3498
	for_each_encoder_on_crtc(dev, crtc, intel_encoder)
3498
	for_each_encoder_on_crtc(dev, crtc, intel_encoder)
3499
		enable |= intel_encoder->connectors_active;
3499
		enable |= intel_encoder->connectors_active;
3500
 
3500
 
3501
	if (enable)
3501
	if (enable)
3502
		dev_priv->display.crtc_enable(crtc);
3502
		dev_priv->display.crtc_enable(crtc);
3503
	else
3503
	else
3504
		dev_priv->display.crtc_disable(crtc);
3504
		dev_priv->display.crtc_disable(crtc);
3505
 
3505
 
3506
	intel_crtc_update_sarea(crtc, enable);
3506
	intel_crtc_update_sarea(crtc, enable);
3507
}
3507
}
3508
 
3508
 
3509
static void intel_crtc_noop(struct drm_crtc *crtc)
3509
static void intel_crtc_noop(struct drm_crtc *crtc)
3510
{
3510
{
3511
}
3511
}
3512
 
3512
 
3513
static void intel_crtc_disable(struct drm_crtc *crtc)
3513
static void intel_crtc_disable(struct drm_crtc *crtc)
3514
{
3514
{
3515
	struct drm_device *dev = crtc->dev;
3515
	struct drm_device *dev = crtc->dev;
3516
	struct drm_connector *connector;
3516
	struct drm_connector *connector;
3517
	struct drm_i915_private *dev_priv = dev->dev_private;
3517
	struct drm_i915_private *dev_priv = dev->dev_private;
3518
 
3518
 
3519
	/* crtc should still be enabled when we disable it. */
3519
	/* crtc should still be enabled when we disable it. */
3520
	WARN_ON(!crtc->enabled);
3520
	WARN_ON(!crtc->enabled);
3521
 
3521
 
3522
	dev_priv->display.crtc_disable(crtc);
3522
	dev_priv->display.crtc_disable(crtc);
3523
	intel_crtc_update_sarea(crtc, false);
3523
	intel_crtc_update_sarea(crtc, false);
3524
	dev_priv->display.off(crtc);
3524
	dev_priv->display.off(crtc);
3525
 
3525
 
3526
	assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
3526
	assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
3527
	assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
3527
	assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
3528
 
3528
 
3529
//	if (crtc->fb) {
3529
//	if (crtc->fb) {
3530
//		mutex_lock(&dev->struct_mutex);
3530
//		mutex_lock(&dev->struct_mutex);
3531
//		intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
3531
//		intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
3532
//		mutex_unlock(&dev->struct_mutex);
3532
//		mutex_unlock(&dev->struct_mutex);
3533
//		crtc->fb = NULL;
3533
//		crtc->fb = NULL;
3534
//	}
3534
//	}
3535
 
3535
 
3536
	/* Update computed state. */
3536
	/* Update computed state. */
3537
	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3537
	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3538
		if (!connector->encoder || !connector->encoder->crtc)
3538
		if (!connector->encoder || !connector->encoder->crtc)
3539
			continue;
3539
			continue;
3540
 
3540
 
3541
		if (connector->encoder->crtc != crtc)
3541
		if (connector->encoder->crtc != crtc)
3542
			continue;
3542
			continue;
3543
 
3543
 
3544
		connector->dpms = DRM_MODE_DPMS_OFF;
3544
		connector->dpms = DRM_MODE_DPMS_OFF;
3545
		to_intel_encoder(connector->encoder)->connectors_active = false;
3545
		to_intel_encoder(connector->encoder)->connectors_active = false;
3546
	}
3546
	}
3547
}
3547
}
3548
 
3548
 
3549
void intel_modeset_disable(struct drm_device *dev)
3549
void intel_modeset_disable(struct drm_device *dev)
3550
{
3550
{
3551
	struct drm_crtc *crtc;
3551
	struct drm_crtc *crtc;
3552
 
3552
 
3553
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3553
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3554
		if (crtc->enabled)
3554
		if (crtc->enabled)
3555
			intel_crtc_disable(crtc);
3555
			intel_crtc_disable(crtc);
3556
	}
3556
	}
3557
}
3557
}
3558
 
3558
 
3559
void intel_encoder_noop(struct drm_encoder *encoder)
3559
void intel_encoder_noop(struct drm_encoder *encoder)
3560
{
3560
{
3561
}
3561
}
3562
 
3562
 
3563
void intel_encoder_destroy(struct drm_encoder *encoder)
3563
void intel_encoder_destroy(struct drm_encoder *encoder)
3564
{
3564
{
3565
	struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
3565
	struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
3566
 
3566
 
3567
	drm_encoder_cleanup(encoder);
3567
	drm_encoder_cleanup(encoder);
3568
	kfree(intel_encoder);
3568
	kfree(intel_encoder);
3569
}
3569
}
3570
 
3570
 
3571
/* Simple dpms helper for encodres with just one connector, no cloning and only
3571
/* Simple dpms helper for encodres with just one connector, no cloning and only
3572
 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
3572
 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
3573
 * state of the entire output pipe. */
3573
 * state of the entire output pipe. */
3574
void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
3574
void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
3575
{
3575
{
3576
	if (mode == DRM_MODE_DPMS_ON) {
3576
	if (mode == DRM_MODE_DPMS_ON) {
3577
		encoder->connectors_active = true;
3577
		encoder->connectors_active = true;
3578
 
3578
 
3579
		intel_crtc_update_dpms(encoder->base.crtc);
3579
		intel_crtc_update_dpms(encoder->base.crtc);
3580
	} else {
3580
	} else {
3581
		encoder->connectors_active = false;
3581
		encoder->connectors_active = false;
3582
 
3582
 
3583
		intel_crtc_update_dpms(encoder->base.crtc);
3583
		intel_crtc_update_dpms(encoder->base.crtc);
3584
	}
3584
	}
3585
}
3585
}
3586
 
3586
 
3587
/* Cross check the actual hw state with our own modeset state tracking (and it's
3587
/* Cross check the actual hw state with our own modeset state tracking (and it's
3588
 * internal consistency). */
3588
 * internal consistency). */
3589
static void intel_connector_check_state(struct intel_connector *connector)
3589
static void intel_connector_check_state(struct intel_connector *connector)
3590
{
3590
{
3591
	if (connector->get_hw_state(connector)) {
3591
	if (connector->get_hw_state(connector)) {
3592
		struct intel_encoder *encoder = connector->encoder;
3592
		struct intel_encoder *encoder = connector->encoder;
3593
		struct drm_crtc *crtc;
3593
		struct drm_crtc *crtc;
3594
		bool encoder_enabled;
3594
		bool encoder_enabled;
3595
		enum pipe pipe;
3595
		enum pipe pipe;
3596
 
3596
 
3597
		DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
3597
		DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
3598
			      connector->base.base.id,
3598
			      connector->base.base.id,
3599
			      drm_get_connector_name(&connector->base));
3599
			      drm_get_connector_name(&connector->base));
3600
 
3600
 
3601
		WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
3601
		WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
3602
		     "wrong connector dpms state\n");
3602
		     "wrong connector dpms state\n");
3603
		WARN(connector->base.encoder != &encoder->base,
3603
		WARN(connector->base.encoder != &encoder->base,
3604
		     "active connector not linked to encoder\n");
3604
		     "active connector not linked to encoder\n");
3605
		WARN(!encoder->connectors_active,
3605
		WARN(!encoder->connectors_active,
3606
		     "encoder->connectors_active not set\n");
3606
		     "encoder->connectors_active not set\n");
3607
 
3607
 
3608
		encoder_enabled = encoder->get_hw_state(encoder, &pipe);
3608
		encoder_enabled = encoder->get_hw_state(encoder, &pipe);
3609
		WARN(!encoder_enabled, "encoder not enabled\n");
3609
		WARN(!encoder_enabled, "encoder not enabled\n");
3610
		if (WARN_ON(!encoder->base.crtc))
3610
		if (WARN_ON(!encoder->base.crtc))
3611
			return;
3611
			return;
3612
 
3612
 
3613
		crtc = encoder->base.crtc;
3613
		crtc = encoder->base.crtc;
3614
 
3614
 
3615
		WARN(!crtc->enabled, "crtc not enabled\n");
3615
		WARN(!crtc->enabled, "crtc not enabled\n");
3616
		WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
3616
		WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
3617
		WARN(pipe != to_intel_crtc(crtc)->pipe,
3617
		WARN(pipe != to_intel_crtc(crtc)->pipe,
3618
		     "encoder active on the wrong pipe\n");
3618
		     "encoder active on the wrong pipe\n");
3619
	}
3619
	}
3620
}
3620
}
3621
 
3621
 
3622
/* Even simpler default implementation, if there's really no special case to
3622
/* Even simpler default implementation, if there's really no special case to
3623
 * consider. */
3623
 * consider. */
3624
void intel_connector_dpms(struct drm_connector *connector, int mode)
3624
void intel_connector_dpms(struct drm_connector *connector, int mode)
3625
{
3625
{
3626
	struct intel_encoder *encoder = intel_attached_encoder(connector);
3626
	struct intel_encoder *encoder = intel_attached_encoder(connector);
3627
 
3627
 
3628
	/* All the simple cases only support two dpms states. */
3628
	/* All the simple cases only support two dpms states. */
3629
	if (mode != DRM_MODE_DPMS_ON)
3629
	if (mode != DRM_MODE_DPMS_ON)
3630
		mode = DRM_MODE_DPMS_OFF;
3630
		mode = DRM_MODE_DPMS_OFF;
3631
 
3631
 
3632
	if (mode == connector->dpms)
3632
	if (mode == connector->dpms)
3633
		return;
3633
		return;
3634
 
3634
 
3635
	connector->dpms = mode;
3635
	connector->dpms = mode;
3636
 
3636
 
3637
	/* Only need to change hw state when actually enabled */
3637
	/* Only need to change hw state when actually enabled */
3638
	if (encoder->base.crtc)
3638
	if (encoder->base.crtc)
3639
		intel_encoder_dpms(encoder, mode);
3639
		intel_encoder_dpms(encoder, mode);
3640
	else
3640
	else
3641
		WARN_ON(encoder->connectors_active != false);
3641
		WARN_ON(encoder->connectors_active != false);
3642
 
3642
 
3643
	intel_modeset_check_state(connector->dev);
3643
	intel_modeset_check_state(connector->dev);
3644
}
3644
}
3645
 
3645
 
3646
/* Simple connector->get_hw_state implementation for encoders that support only
3646
/* Simple connector->get_hw_state implementation for encoders that support only
3647
 * one connector and no cloning and hence the encoder state determines the state
3647
 * one connector and no cloning and hence the encoder state determines the state
3648
 * of the connector. */
3648
 * of the connector. */
3649
bool intel_connector_get_hw_state(struct intel_connector *connector)
3649
bool intel_connector_get_hw_state(struct intel_connector *connector)
3650
{
3650
{
3651
	enum pipe pipe = 0;
3651
	enum pipe pipe = 0;
3652
	struct intel_encoder *encoder = connector->encoder;
3652
	struct intel_encoder *encoder = connector->encoder;
3653
 
3653
 
3654
	return encoder->get_hw_state(encoder, &pipe);
3654
	return encoder->get_hw_state(encoder, &pipe);
3655
}
3655
}
3656
 
3656
 
3657
static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
3657
static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
3658
				  const struct drm_display_mode *mode,
3658
				  const struct drm_display_mode *mode,
3659
				  struct drm_display_mode *adjusted_mode)
3659
				  struct drm_display_mode *adjusted_mode)
3660
{
3660
{
3661
	struct drm_device *dev = crtc->dev;
3661
	struct drm_device *dev = crtc->dev;
3662
 
3662
 
3663
	if (HAS_PCH_SPLIT(dev)) {
3663
	if (HAS_PCH_SPLIT(dev)) {
3664
		/* FDI link clock is fixed at 2.7G */
3664
		/* FDI link clock is fixed at 2.7G */
3665
		if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
3665
		if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
3666
			return false;
3666
			return false;
3667
	}
3667
	}
3668
 
3668
 
3669
	/* All interlaced capable intel hw wants timings in frames. Note though
3669
	/* All interlaced capable intel hw wants timings in frames. Note though
3670
	 * that intel_lvds_mode_fixup does some funny tricks with the crtc
3670
	 * that intel_lvds_mode_fixup does some funny tricks with the crtc
3671
	 * timings, so we need to be careful not to clobber these.*/
3671
	 * timings, so we need to be careful not to clobber these.*/
3672
	if (!(adjusted_mode->private_flags & INTEL_MODE_CRTC_TIMINGS_SET))
3672
	if (!(adjusted_mode->private_flags & INTEL_MODE_CRTC_TIMINGS_SET))
3673
		drm_mode_set_crtcinfo(adjusted_mode, 0);
3673
		drm_mode_set_crtcinfo(adjusted_mode, 0);
3674
 
3674
 
3675
	/* WaPruneModeWithIncorrectHsyncOffset: Cantiga+ cannot handle modes
3675
	/* WaPruneModeWithIncorrectHsyncOffset: Cantiga+ cannot handle modes
3676
	 * with a hsync front porch of 0.
3676
	 * with a hsync front porch of 0.
3677
	 */
3677
	 */
3678
	if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
3678
	if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
3679
		adjusted_mode->hsync_start == adjusted_mode->hdisplay)
3679
		adjusted_mode->hsync_start == adjusted_mode->hdisplay)
3680
		return false;
3680
		return false;
3681
 
3681
 
3682
	return true;
3682
	return true;
3683
}
3683
}
3684
 
3684
 
3685
static int valleyview_get_display_clock_speed(struct drm_device *dev)
3685
static int valleyview_get_display_clock_speed(struct drm_device *dev)
3686
{
3686
{
3687
	return 400000; /* FIXME */
3687
	return 400000; /* FIXME */
3688
}
3688
}
3689
 
3689
 
3690
static int i945_get_display_clock_speed(struct drm_device *dev)
3690
static int i945_get_display_clock_speed(struct drm_device *dev)
3691
{
3691
{
3692
	return 400000;
3692
	return 400000;
3693
}
3693
}
3694
 
3694
 
3695
static int i915_get_display_clock_speed(struct drm_device *dev)
3695
static int i915_get_display_clock_speed(struct drm_device *dev)
3696
{
3696
{
3697
	return 333000;
3697
	return 333000;
3698
}
3698
}
3699
 
3699
 
3700
static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
3700
static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
3701
{
3701
{
3702
	return 200000;
3702
	return 200000;
3703
}
3703
}
3704
 
3704
 
3705
static int i915gm_get_display_clock_speed(struct drm_device *dev)
3705
static int i915gm_get_display_clock_speed(struct drm_device *dev)
3706
{
3706
{
3707
	u16 gcfgc = 0;
3707
	u16 gcfgc = 0;
3708
 
3708
 
3709
	pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
3709
	pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
3710
 
3710
 
3711
	if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
3711
	if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
3712
		return 133000;
3712
		return 133000;
3713
	else {
3713
	else {
3714
		switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
3714
		switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
3715
		case GC_DISPLAY_CLOCK_333_MHZ:
3715
		case GC_DISPLAY_CLOCK_333_MHZ:
3716
			return 333000;
3716
			return 333000;
3717
		default:
3717
		default:
3718
		case GC_DISPLAY_CLOCK_190_200_MHZ:
3718
		case GC_DISPLAY_CLOCK_190_200_MHZ:
3719
			return 190000;
3719
			return 190000;
3720
		}
3720
		}
3721
	}
3721
	}
3722
}
3722
}
3723
 
3723
 
3724
static int i865_get_display_clock_speed(struct drm_device *dev)
3724
static int i865_get_display_clock_speed(struct drm_device *dev)
3725
{
3725
{
3726
	return 266000;
3726
	return 266000;
3727
}
3727
}
3728
 
3728
 
3729
static int i855_get_display_clock_speed(struct drm_device *dev)
3729
static int i855_get_display_clock_speed(struct drm_device *dev)
3730
{
3730
{
3731
	u16 hpllcc = 0;
3731
	u16 hpllcc = 0;
3732
	/* Assume that the hardware is in the high speed state.  This
3732
	/* Assume that the hardware is in the high speed state.  This
3733
	 * should be the default.
3733
	 * should be the default.
3734
	 */
3734
	 */
3735
	switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
3735
	switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
3736
	case GC_CLOCK_133_200:
3736
	case GC_CLOCK_133_200:
3737
	case GC_CLOCK_100_200:
3737
	case GC_CLOCK_100_200:
3738
		return 200000;
3738
		return 200000;
3739
	case GC_CLOCK_166_250:
3739
	case GC_CLOCK_166_250:
3740
		return 250000;
3740
		return 250000;
3741
	case GC_CLOCK_100_133:
3741
	case GC_CLOCK_100_133:
3742
		return 133000;
3742
		return 133000;
3743
	}
3743
	}
3744
 
3744
 
3745
	/* Shouldn't happen */
3745
	/* Shouldn't happen */
3746
	return 0;
3746
	return 0;
3747
}
3747
}
3748
 
3748
 
3749
static int i830_get_display_clock_speed(struct drm_device *dev)
3749
static int i830_get_display_clock_speed(struct drm_device *dev)
3750
{
3750
{
3751
	return 133000;
3751
	return 133000;
3752
}
3752
}
3753
 
3753
 
3754
struct fdi_m_n {
3754
struct fdi_m_n {
3755
    u32        tu;
3755
    u32        tu;
3756
    u32        gmch_m;
3756
    u32        gmch_m;
3757
    u32        gmch_n;
3757
    u32        gmch_n;
3758
    u32        link_m;
3758
    u32        link_m;
3759
    u32        link_n;
3759
    u32        link_n;
3760
};
3760
};
3761
 
3761
 
3762
static void
3762
static void
3763
fdi_reduce_ratio(u32 *num, u32 *den)
3763
fdi_reduce_ratio(u32 *num, u32 *den)
3764
{
3764
{
3765
	while (*num > 0xffffff || *den > 0xffffff) {
3765
	while (*num > 0xffffff || *den > 0xffffff) {
3766
		*num >>= 1;
3766
		*num >>= 1;
3767
		*den >>= 1;
3767
		*den >>= 1;
3768
	}
3768
	}
3769
}
3769
}
3770
 
3770
 
3771
static void
3771
static void
3772
ironlake_compute_m_n(int bits_per_pixel, int nlanes, int pixel_clock,
3772
ironlake_compute_m_n(int bits_per_pixel, int nlanes, int pixel_clock,
3773
		     int link_clock, struct fdi_m_n *m_n)
3773
		     int link_clock, struct fdi_m_n *m_n)
3774
{
3774
{
3775
	m_n->tu = 64; /* default size */
3775
	m_n->tu = 64; /* default size */
3776
 
3776
 
3777
	/* BUG_ON(pixel_clock > INT_MAX / 36); */
3777
	/* BUG_ON(pixel_clock > INT_MAX / 36); */
3778
	m_n->gmch_m = bits_per_pixel * pixel_clock;
3778
	m_n->gmch_m = bits_per_pixel * pixel_clock;
3779
	m_n->gmch_n = link_clock * nlanes * 8;
3779
	m_n->gmch_n = link_clock * nlanes * 8;
3780
	fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
3780
	fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
3781
 
3781
 
3782
	m_n->link_m = pixel_clock;
3782
	m_n->link_m = pixel_clock;
3783
	m_n->link_n = link_clock;
3783
	m_n->link_n = link_clock;
3784
	fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
3784
	fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
3785
}
3785
}
3786
 
3786
 
3787
static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
3787
static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
3788
{
3788
{
3789
	if (i915_panel_use_ssc >= 0)
3789
	if (i915_panel_use_ssc >= 0)
3790
		return i915_panel_use_ssc != 0;
3790
		return i915_panel_use_ssc != 0;
3791
	return dev_priv->lvds_use_ssc
3791
	return dev_priv->lvds_use_ssc
3792
		&& !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
3792
		&& !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
3793
}
3793
}
3794
 
3794
 
3795
/**
3795
/**
3796
 * intel_choose_pipe_bpp_dither - figure out what color depth the pipe should send
3796
 * intel_choose_pipe_bpp_dither - figure out what color depth the pipe should send
3797
 * @crtc: CRTC structure
3797
 * @crtc: CRTC structure
3798
 * @mode: requested mode
3798
 * @mode: requested mode
3799
 *
3799
 *
3800
 * A pipe may be connected to one or more outputs.  Based on the depth of the
3800
 * A pipe may be connected to one or more outputs.  Based on the depth of the
3801
 * attached framebuffer, choose a good color depth to use on the pipe.
3801
 * attached framebuffer, choose a good color depth to use on the pipe.
3802
 *
3802
 *
3803
 * If possible, match the pipe depth to the fb depth.  In some cases, this
3803
 * If possible, match the pipe depth to the fb depth.  In some cases, this
3804
 * isn't ideal, because the connected output supports a lesser or restricted
3804
 * isn't ideal, because the connected output supports a lesser or restricted
3805
 * set of depths.  Resolve that here:
3805
 * set of depths.  Resolve that here:
3806
 *    LVDS typically supports only 6bpc, so clamp down in that case
3806
 *    LVDS typically supports only 6bpc, so clamp down in that case
3807
 *    HDMI supports only 8bpc or 12bpc, so clamp to 8bpc with dither for 10bpc
3807
 *    HDMI supports only 8bpc or 12bpc, so clamp to 8bpc with dither for 10bpc
3808
 *    Displays may support a restricted set as well, check EDID and clamp as
3808
 *    Displays may support a restricted set as well, check EDID and clamp as
3809
 *      appropriate.
3809
 *      appropriate.
3810
 *    DP may want to dither down to 6bpc to fit larger modes
3810
 *    DP may want to dither down to 6bpc to fit larger modes
3811
 *
3811
 *
3812
 * RETURNS:
3812
 * RETURNS:
3813
 * Dithering requirement (i.e. false if display bpc and pipe bpc match,
3813
 * Dithering requirement (i.e. false if display bpc and pipe bpc match,
3814
 * true if they don't match).
3814
 * true if they don't match).
3815
 */
3815
 */
3816
static bool intel_choose_pipe_bpp_dither(struct drm_crtc *crtc,
3816
static bool intel_choose_pipe_bpp_dither(struct drm_crtc *crtc,
3817
					 struct drm_framebuffer *fb,
3817
					 struct drm_framebuffer *fb,
3818
					 unsigned int *pipe_bpp,
3818
					 unsigned int *pipe_bpp,
3819
					 struct drm_display_mode *mode)
3819
					 struct drm_display_mode *mode)
3820
{
3820
{
3821
	struct drm_device *dev = crtc->dev;
3821
	struct drm_device *dev = crtc->dev;
3822
	struct drm_i915_private *dev_priv = dev->dev_private;
3822
	struct drm_i915_private *dev_priv = dev->dev_private;
3823
	struct drm_connector *connector;
3823
	struct drm_connector *connector;
3824
	struct intel_encoder *intel_encoder;
3824
	struct intel_encoder *intel_encoder;
3825
	unsigned int display_bpc = UINT_MAX, bpc;
3825
	unsigned int display_bpc = UINT_MAX, bpc;
3826
 
3826
 
3827
	/* Walk the encoders & connectors on this crtc, get min bpc */
3827
	/* Walk the encoders & connectors on this crtc, get min bpc */
3828
	for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
3828
	for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
3829
 
3829
 
3830
		if (intel_encoder->type == INTEL_OUTPUT_LVDS) {
3830
		if (intel_encoder->type == INTEL_OUTPUT_LVDS) {
3831
			unsigned int lvds_bpc;
3831
			unsigned int lvds_bpc;
3832
 
3832
 
3833
			if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) ==
3833
			if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) ==
3834
			    LVDS_A3_POWER_UP)
3834
			    LVDS_A3_POWER_UP)
3835
				lvds_bpc = 8;
3835
				lvds_bpc = 8;
3836
			else
3836
			else
3837
				lvds_bpc = 6;
3837
				lvds_bpc = 6;
3838
 
3838
 
3839
			if (lvds_bpc < display_bpc) {
3839
			if (lvds_bpc < display_bpc) {
3840
				DRM_DEBUG_KMS("clamping display bpc (was %d) to LVDS (%d)\n", display_bpc, lvds_bpc);
3840
				DRM_DEBUG_KMS("clamping display bpc (was %d) to LVDS (%d)\n", display_bpc, lvds_bpc);
3841
				display_bpc = lvds_bpc;
3841
				display_bpc = lvds_bpc;
3842
			}
3842
			}
3843
			continue;
3843
			continue;
3844
		}
3844
		}
3845
 
3845
 
3846
		/* Not one of the known troublemakers, check the EDID */
3846
		/* Not one of the known troublemakers, check the EDID */
3847
		list_for_each_entry(connector, &dev->mode_config.connector_list,
3847
		list_for_each_entry(connector, &dev->mode_config.connector_list,
3848
				    head) {
3848
				    head) {
3849
			if (connector->encoder != &intel_encoder->base)
3849
			if (connector->encoder != &intel_encoder->base)
3850
				continue;
3850
				continue;
3851
 
3851
 
3852
			/* Don't use an invalid EDID bpc value */
3852
			/* Don't use an invalid EDID bpc value */
3853
			if (connector->display_info.bpc &&
3853
			if (connector->display_info.bpc &&
3854
			    connector->display_info.bpc < display_bpc) {
3854
			    connector->display_info.bpc < display_bpc) {
3855
				DRM_DEBUG_KMS("clamping display bpc (was %d) to EDID reported max of %d\n", display_bpc, connector->display_info.bpc);
3855
				DRM_DEBUG_KMS("clamping display bpc (was %d) to EDID reported max of %d\n", display_bpc, connector->display_info.bpc);
3856
				display_bpc = connector->display_info.bpc;
3856
				display_bpc = connector->display_info.bpc;
3857
        }
3857
        }
3858
    }
3858
    }
-
 
3859
 
-
 
3860
		if (intel_encoder->type == INTEL_OUTPUT_EDP) {
-
 
3861
			/* Use VBT settings if we have an eDP panel */
-
 
3862
			unsigned int edp_bpc = dev_priv->edp.bpp / 3;
-
 
3863
 
-
 
3864
			if (edp_bpc < display_bpc) {
-
 
3865
				DRM_DEBUG_KMS("clamping display bpc (was %d) to eDP (%d)\n", display_bpc, edp_bpc);
-
 
3866
				display_bpc = edp_bpc;
-
 
3867
			}
-
 
3868
			continue;
-
 
3869
		}
3859
 
3870
 
3860
		/*
3871
		/*
3861
		 * HDMI is either 12 or 8, so if the display lets 10bpc sneak
3872
		 * HDMI is either 12 or 8, so if the display lets 10bpc sneak
3862
		 * through, clamp it down.  (Note: >12bpc will be caught below.)
3873
		 * through, clamp it down.  (Note: >12bpc will be caught below.)
3863
		 */
3874
		 */
3864
		if (intel_encoder->type == INTEL_OUTPUT_HDMI) {
3875
		if (intel_encoder->type == INTEL_OUTPUT_HDMI) {
3865
			if (display_bpc > 8 && display_bpc < 12) {
3876
			if (display_bpc > 8 && display_bpc < 12) {
3866
				DRM_DEBUG_KMS("forcing bpc to 12 for HDMI\n");
3877
				DRM_DEBUG_KMS("forcing bpc to 12 for HDMI\n");
3867
				display_bpc = 12;
3878
				display_bpc = 12;
3868
			} else {
3879
			} else {
3869
				DRM_DEBUG_KMS("forcing bpc to 8 for HDMI\n");
3880
				DRM_DEBUG_KMS("forcing bpc to 8 for HDMI\n");
3870
				display_bpc = 8;
3881
				display_bpc = 8;
3871
			}
3882
			}
3872
		}
3883
		}
3873
	}
3884
	}
3874
 
3885
 
3875
	if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
3886
	if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
3876
		DRM_DEBUG_KMS("Dithering DP to 6bpc\n");
3887
		DRM_DEBUG_KMS("Dithering DP to 6bpc\n");
3877
		display_bpc = 6;
3888
		display_bpc = 6;
3878
	}
3889
	}
3879
 
3890
 
3880
	/*
3891
	/*
3881
	 * We could just drive the pipe at the highest bpc all the time and
3892
	 * We could just drive the pipe at the highest bpc all the time and
3882
	 * enable dithering as needed, but that costs bandwidth.  So choose
3893
	 * enable dithering as needed, but that costs bandwidth.  So choose
3883
	 * the minimum value that expresses the full color range of the fb but
3894
	 * the minimum value that expresses the full color range of the fb but
3884
	 * also stays within the max display bpc discovered above.
3895
	 * also stays within the max display bpc discovered above.
3885
	 */
3896
	 */
3886
 
3897
 
3887
	switch (fb->depth) {
3898
	switch (fb->depth) {
3888
	case 8:
3899
	case 8:
3889
		bpc = 8; /* since we go through a colormap */
3900
		bpc = 8; /* since we go through a colormap */
3890
		break;
3901
		break;
3891
	case 15:
3902
	case 15:
3892
	case 16:
3903
	case 16:
3893
		bpc = 6; /* min is 18bpp */
3904
		bpc = 6; /* min is 18bpp */
3894
		break;
3905
		break;
3895
	case 24:
3906
	case 24:
3896
		bpc = 8;
3907
		bpc = 8;
3897
		break;
3908
		break;
3898
	case 30:
3909
	case 30:
3899
		bpc = 10;
3910
		bpc = 10;
3900
		break;
3911
		break;
3901
	case 48:
3912
	case 48:
3902
		bpc = 12;
3913
		bpc = 12;
3903
		break;
3914
		break;
3904
	default:
3915
	default:
3905
		DRM_DEBUG("unsupported depth, assuming 24 bits\n");
3916
		DRM_DEBUG("unsupported depth, assuming 24 bits\n");
3906
		bpc = min((unsigned int)8, display_bpc);
3917
		bpc = min((unsigned int)8, display_bpc);
3907
		break;
3918
		break;
3908
	}
3919
	}
3909
 
3920
 
3910
	display_bpc = min(display_bpc, bpc);
3921
	display_bpc = min(display_bpc, bpc);
3911
 
3922
 
3912
	DRM_DEBUG_KMS("setting pipe bpc to %d (max display bpc %d)\n",
3923
	DRM_DEBUG_KMS("setting pipe bpc to %d (max display bpc %d)\n",
3913
		      bpc, display_bpc);
3924
		      bpc, display_bpc);
3914
 
3925
 
3915
	*pipe_bpp = display_bpc * 3;
3926
	*pipe_bpp = display_bpc * 3;
3916
 
3927
 
3917
	return display_bpc != bpc;
3928
	return display_bpc != bpc;
3918
}
3929
}
3919
 
3930
 
3920
static int vlv_get_refclk(struct drm_crtc *crtc)
3931
static int vlv_get_refclk(struct drm_crtc *crtc)
3921
{
3932
{
3922
	struct drm_device *dev = crtc->dev;
3933
	struct drm_device *dev = crtc->dev;
3923
	struct drm_i915_private *dev_priv = dev->dev_private;
3934
	struct drm_i915_private *dev_priv = dev->dev_private;
3924
	int refclk = 27000; /* for DP & HDMI */
3935
	int refclk = 27000; /* for DP & HDMI */
3925
 
3936
 
3926
	return 100000; /* only one validated so far */
3937
	return 100000; /* only one validated so far */
3927
 
3938
 
3928
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
3939
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
3929
		refclk = 96000;
3940
		refclk = 96000;
3930
	} else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
3941
	} else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
3931
		if (intel_panel_use_ssc(dev_priv))
3942
		if (intel_panel_use_ssc(dev_priv))
3932
			refclk = 100000;
3943
			refclk = 100000;
3933
		else
3944
		else
3934
			refclk = 96000;
3945
			refclk = 96000;
3935
	} else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
3946
	} else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
3936
		refclk = 100000;
3947
		refclk = 100000;
3937
	}
3948
	}
3938
 
3949
 
3939
	return refclk;
3950
	return refclk;
3940
}
3951
}
3941
 
3952
 
3942
static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
3953
static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
3943
{
3954
{
3944
	struct drm_device *dev = crtc->dev;
3955
	struct drm_device *dev = crtc->dev;
3945
	struct drm_i915_private *dev_priv = dev->dev_private;
3956
	struct drm_i915_private *dev_priv = dev->dev_private;
3946
	int refclk;
3957
	int refclk;
3947
 
3958
 
3948
	if (IS_VALLEYVIEW(dev)) {
3959
	if (IS_VALLEYVIEW(dev)) {
3949
		refclk = vlv_get_refclk(crtc);
3960
		refclk = vlv_get_refclk(crtc);
3950
	} else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
3961
	} else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
3951
	    intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
3962
	    intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
3952
		refclk = dev_priv->lvds_ssc_freq * 1000;
3963
		refclk = dev_priv->lvds_ssc_freq * 1000;
3953
		DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
3964
		DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
3954
			      refclk / 1000);
3965
			      refclk / 1000);
3955
	} else if (!IS_GEN2(dev)) {
3966
	} else if (!IS_GEN2(dev)) {
3956
		refclk = 96000;
3967
		refclk = 96000;
3957
	} else {
3968
	} else {
3958
		refclk = 48000;
3969
		refclk = 48000;
3959
	}
3970
	}
3960
 
3971
 
3961
	return refclk;
3972
	return refclk;
3962
}
3973
}
3963
 
3974
 
3964
static void i9xx_adjust_sdvo_tv_clock(struct drm_display_mode *adjusted_mode,
3975
static void i9xx_adjust_sdvo_tv_clock(struct drm_display_mode *adjusted_mode,
3965
				      intel_clock_t *clock)
3976
				      intel_clock_t *clock)
3966
{
3977
{
3967
	/* SDVO TV has fixed PLL values depend on its clock range,
3978
	/* SDVO TV has fixed PLL values depend on its clock range,
3968
	   this mirrors vbios setting. */
3979
	   this mirrors vbios setting. */
3969
	if (adjusted_mode->clock >= 100000
3980
	if (adjusted_mode->clock >= 100000
3970
	    && adjusted_mode->clock < 140500) {
3981
	    && adjusted_mode->clock < 140500) {
3971
		clock->p1 = 2;
3982
		clock->p1 = 2;
3972
		clock->p2 = 10;
3983
		clock->p2 = 10;
3973
		clock->n = 3;
3984
		clock->n = 3;
3974
		clock->m1 = 16;
3985
		clock->m1 = 16;
3975
		clock->m2 = 8;
3986
		clock->m2 = 8;
3976
	} else if (adjusted_mode->clock >= 140500
3987
	} else if (adjusted_mode->clock >= 140500
3977
		   && adjusted_mode->clock <= 200000) {
3988
		   && adjusted_mode->clock <= 200000) {
3978
		clock->p1 = 1;
3989
		clock->p1 = 1;
3979
		clock->p2 = 10;
3990
		clock->p2 = 10;
3980
		clock->n = 6;
3991
		clock->n = 6;
3981
		clock->m1 = 12;
3992
		clock->m1 = 12;
3982
		clock->m2 = 8;
3993
		clock->m2 = 8;
3983
	}
3994
	}
3984
}
3995
}
3985
 
3996
 
3986
static void i9xx_update_pll_dividers(struct drm_crtc *crtc,
3997
static void i9xx_update_pll_dividers(struct drm_crtc *crtc,
3987
				     intel_clock_t *clock,
3998
				     intel_clock_t *clock,
3988
				     intel_clock_t *reduced_clock)
3999
				     intel_clock_t *reduced_clock)
3989
{
4000
{
3990
	struct drm_device *dev = crtc->dev;
4001
	struct drm_device *dev = crtc->dev;
3991
	struct drm_i915_private *dev_priv = dev->dev_private;
4002
	struct drm_i915_private *dev_priv = dev->dev_private;
3992
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4003
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3993
	int pipe = intel_crtc->pipe;
4004
	int pipe = intel_crtc->pipe;
3994
	u32 fp, fp2 = 0;
4005
	u32 fp, fp2 = 0;
3995
 
4006
 
3996
	if (IS_PINEVIEW(dev)) {
4007
	if (IS_PINEVIEW(dev)) {
3997
		fp = (1 << clock->n) << 16 | clock->m1 << 8 | clock->m2;
4008
		fp = (1 << clock->n) << 16 | clock->m1 << 8 | clock->m2;
3998
		if (reduced_clock)
4009
		if (reduced_clock)
3999
			fp2 = (1 << reduced_clock->n) << 16 |
4010
			fp2 = (1 << reduced_clock->n) << 16 |
4000
				reduced_clock->m1 << 8 | reduced_clock->m2;
4011
				reduced_clock->m1 << 8 | reduced_clock->m2;
4001
	} else {
4012
	} else {
4002
		fp = clock->n << 16 | clock->m1 << 8 | clock->m2;
4013
		fp = clock->n << 16 | clock->m1 << 8 | clock->m2;
4003
		if (reduced_clock)
4014
		if (reduced_clock)
4004
			fp2 = reduced_clock->n << 16 | reduced_clock->m1 << 8 |
4015
			fp2 = reduced_clock->n << 16 | reduced_clock->m1 << 8 |
4005
				reduced_clock->m2;
4016
				reduced_clock->m2;
4006
	}
4017
	}
4007
 
4018
 
4008
	I915_WRITE(FP0(pipe), fp);
4019
	I915_WRITE(FP0(pipe), fp);
4009
 
4020
 
4010
	intel_crtc->lowfreq_avail = false;
4021
	intel_crtc->lowfreq_avail = false;
4011
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
4022
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
4012
	    reduced_clock && i915_powersave) {
4023
	    reduced_clock && i915_powersave) {
4013
		I915_WRITE(FP1(pipe), fp2);
4024
		I915_WRITE(FP1(pipe), fp2);
4014
		intel_crtc->lowfreq_avail = true;
4025
		intel_crtc->lowfreq_avail = true;
4015
	} else {
4026
	} else {
4016
		I915_WRITE(FP1(pipe), fp);
4027
		I915_WRITE(FP1(pipe), fp);
4017
	}
4028
	}
4018
}
4029
}
4019
 
4030
 
4020
static void intel_update_lvds(struct drm_crtc *crtc, intel_clock_t *clock,
4031
static void intel_update_lvds(struct drm_crtc *crtc, intel_clock_t *clock,
4021
			      struct drm_display_mode *adjusted_mode)
4032
			      struct drm_display_mode *adjusted_mode)
4022
{
4033
{
4023
	struct drm_device *dev = crtc->dev;
4034
	struct drm_device *dev = crtc->dev;
4024
	struct drm_i915_private *dev_priv = dev->dev_private;
4035
	struct drm_i915_private *dev_priv = dev->dev_private;
4025
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4036
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4026
	int pipe = intel_crtc->pipe;
4037
	int pipe = intel_crtc->pipe;
4027
	u32 temp;
4038
	u32 temp;
4028
 
4039
 
4029
	temp = I915_READ(LVDS);
4040
	temp = I915_READ(LVDS);
4030
	temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
4041
	temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
4031
	if (pipe == 1) {
4042
	if (pipe == 1) {
4032
		temp |= LVDS_PIPEB_SELECT;
4043
		temp |= LVDS_PIPEB_SELECT;
4033
	} else {
4044
	} else {
4034
		temp &= ~LVDS_PIPEB_SELECT;
4045
		temp &= ~LVDS_PIPEB_SELECT;
4035
	}
4046
	}
4036
	/* set the corresponsding LVDS_BORDER bit */
4047
	/* set the corresponsding LVDS_BORDER bit */
4037
	temp |= dev_priv->lvds_border_bits;
4048
	temp |= dev_priv->lvds_border_bits;
4038
	/* Set the B0-B3 data pairs corresponding to whether we're going to
4049
	/* Set the B0-B3 data pairs corresponding to whether we're going to
4039
	 * set the DPLLs for dual-channel mode or not.
4050
	 * set the DPLLs for dual-channel mode or not.
4040
	 */
4051
	 */
4041
	if (clock->p2 == 7)
4052
	if (clock->p2 == 7)
4042
		temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
4053
		temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
4043
	else
4054
	else
4044
		temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
4055
		temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
4045
 
4056
 
4046
	/* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
4057
	/* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
4047
	 * appropriately here, but we need to look more thoroughly into how
4058
	 * appropriately here, but we need to look more thoroughly into how
4048
	 * panels behave in the two modes.
4059
	 * panels behave in the two modes.
4049
	 */
4060
	 */
4050
	/* set the dithering flag on LVDS as needed */
4061
	/* set the dithering flag on LVDS as needed */
4051
	if (INTEL_INFO(dev)->gen >= 4) {
4062
	if (INTEL_INFO(dev)->gen >= 4) {
4052
		if (dev_priv->lvds_dither)
4063
		if (dev_priv->lvds_dither)
4053
			temp |= LVDS_ENABLE_DITHER;
4064
			temp |= LVDS_ENABLE_DITHER;
4054
		else
4065
		else
4055
			temp &= ~LVDS_ENABLE_DITHER;
4066
			temp &= ~LVDS_ENABLE_DITHER;
4056
	}
4067
	}
4057
	temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
4068
	temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
4058
	if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
4069
	if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
4059
		temp |= LVDS_HSYNC_POLARITY;
4070
		temp |= LVDS_HSYNC_POLARITY;
4060
	if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
4071
	if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
4061
		temp |= LVDS_VSYNC_POLARITY;
4072
		temp |= LVDS_VSYNC_POLARITY;
4062
	I915_WRITE(LVDS, temp);
4073
	I915_WRITE(LVDS, temp);
4063
}
4074
}
4064
 
4075
 
4065
static void vlv_update_pll(struct drm_crtc *crtc,
4076
static void vlv_update_pll(struct drm_crtc *crtc,
4066
			   struct drm_display_mode *mode,
4077
			   struct drm_display_mode *mode,
4067
			   struct drm_display_mode *adjusted_mode,
4078
			   struct drm_display_mode *adjusted_mode,
4068
			   intel_clock_t *clock, intel_clock_t *reduced_clock,
4079
			   intel_clock_t *clock, intel_clock_t *reduced_clock,
4069
			   int refclk, int num_connectors)
4080
			   int refclk, int num_connectors)
4070
{
4081
{
4071
	struct drm_device *dev = crtc->dev;
4082
	struct drm_device *dev = crtc->dev;
4072
	struct drm_i915_private *dev_priv = dev->dev_private;
4083
	struct drm_i915_private *dev_priv = dev->dev_private;
4073
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4084
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4074
	int pipe = intel_crtc->pipe;
4085
	int pipe = intel_crtc->pipe;
4075
	u32 dpll, mdiv, pdiv;
4086
	u32 dpll, mdiv, pdiv;
4076
	u32 bestn, bestm1, bestm2, bestp1, bestp2;
4087
	u32 bestn, bestm1, bestm2, bestp1, bestp2;
4077
	bool is_hdmi;
4088
	bool is_hdmi;
4078
 
4089
 
4079
	is_hdmi = intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI);
4090
	is_hdmi = intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI);
4080
 
4091
 
4081
	bestn = clock->n;
4092
	bestn = clock->n;
4082
	bestm1 = clock->m1;
4093
	bestm1 = clock->m1;
4083
	bestm2 = clock->m2;
4094
	bestm2 = clock->m2;
4084
	bestp1 = clock->p1;
4095
	bestp1 = clock->p1;
4085
	bestp2 = clock->p2;
4096
	bestp2 = clock->p2;
4086
 
4097
 
4087
	/* Enable DPIO clock input */
4098
	/* Enable DPIO clock input */
4088
	dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
4099
	dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
4089
		DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
4100
		DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
4090
	I915_WRITE(DPLL(pipe), dpll);
4101
	I915_WRITE(DPLL(pipe), dpll);
4091
	POSTING_READ(DPLL(pipe));
4102
	POSTING_READ(DPLL(pipe));
4092
 
4103
 
4093
	mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
4104
	mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
4094
	mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
4105
	mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
4095
	mdiv |= ((bestn << DPIO_N_SHIFT));
4106
	mdiv |= ((bestn << DPIO_N_SHIFT));
4096
	mdiv |= (1 << DPIO_POST_DIV_SHIFT);
4107
	mdiv |= (1 << DPIO_POST_DIV_SHIFT);
4097
	mdiv |= (1 << DPIO_K_SHIFT);
4108
	mdiv |= (1 << DPIO_K_SHIFT);
4098
	mdiv |= DPIO_ENABLE_CALIBRATION;
4109
	mdiv |= DPIO_ENABLE_CALIBRATION;
4099
	intel_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
4110
	intel_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
4100
 
4111
 
4101
	intel_dpio_write(dev_priv, DPIO_CORE_CLK(pipe), 0x01000000);
4112
	intel_dpio_write(dev_priv, DPIO_CORE_CLK(pipe), 0x01000000);
4102
 
4113
 
4103
	pdiv = DPIO_REFSEL_OVERRIDE | (5 << DPIO_PLL_MODESEL_SHIFT) |
4114
	pdiv = DPIO_REFSEL_OVERRIDE | (5 << DPIO_PLL_MODESEL_SHIFT) |
4104
		(3 << DPIO_BIAS_CURRENT_CTL_SHIFT) | (1<<20) |
4115
		(3 << DPIO_BIAS_CURRENT_CTL_SHIFT) | (1<<20) |
4105
		(8 << DPIO_DRIVER_CTL_SHIFT) | (5 << DPIO_CLK_BIAS_CTL_SHIFT);
4116
		(8 << DPIO_DRIVER_CTL_SHIFT) | (5 << DPIO_CLK_BIAS_CTL_SHIFT);
4106
	intel_dpio_write(dev_priv, DPIO_REFSFR(pipe), pdiv);
4117
	intel_dpio_write(dev_priv, DPIO_REFSFR(pipe), pdiv);
4107
 
4118
 
4108
	intel_dpio_write(dev_priv, DPIO_LFP_COEFF(pipe), 0x009f0051);
4119
	intel_dpio_write(dev_priv, DPIO_LFP_COEFF(pipe), 0x009f0051);
4109
 
4120
 
4110
	dpll |= DPLL_VCO_ENABLE;
4121
	dpll |= DPLL_VCO_ENABLE;
4111
	I915_WRITE(DPLL(pipe), dpll);
4122
	I915_WRITE(DPLL(pipe), dpll);
4112
	POSTING_READ(DPLL(pipe));
4123
	POSTING_READ(DPLL(pipe));
4113
	if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
4124
	if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
4114
		DRM_ERROR("DPLL %d failed to lock\n", pipe);
4125
		DRM_ERROR("DPLL %d failed to lock\n", pipe);
4115
 
4126
 
4116
	if (is_hdmi) {
4127
	if (is_hdmi) {
4117
		u32 temp = intel_mode_get_pixel_multiplier(adjusted_mode);
4128
		u32 temp = intel_mode_get_pixel_multiplier(adjusted_mode);
4118
 
4129
 
4119
		if (temp > 1)
4130
		if (temp > 1)
4120
			temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
4131
			temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
4121
		else
4132
		else
4122
			temp = 0;
4133
			temp = 0;
4123
 
4134
 
4124
		I915_WRITE(DPLL_MD(pipe), temp);
4135
		I915_WRITE(DPLL_MD(pipe), temp);
4125
		POSTING_READ(DPLL_MD(pipe));
4136
		POSTING_READ(DPLL_MD(pipe));
4126
	}
4137
	}
4127
 
4138
 
4128
	intel_dpio_write(dev_priv, DPIO_FASTCLK_DISABLE, 0x641); /* ??? */
4139
	intel_dpio_write(dev_priv, DPIO_FASTCLK_DISABLE, 0x641); /* ??? */
4129
}
4140
}
4130
 
4141
 
4131
static void i9xx_update_pll(struct drm_crtc *crtc,
4142
static void i9xx_update_pll(struct drm_crtc *crtc,
4132
			    struct drm_display_mode *mode,
4143
			    struct drm_display_mode *mode,
4133
			    struct drm_display_mode *adjusted_mode,
4144
			    struct drm_display_mode *adjusted_mode,
4134
			    intel_clock_t *clock, intel_clock_t *reduced_clock,
4145
			    intel_clock_t *clock, intel_clock_t *reduced_clock,
4135
			    int num_connectors)
4146
			    int num_connectors)
4136
{
4147
{
4137
	struct drm_device *dev = crtc->dev;
4148
	struct drm_device *dev = crtc->dev;
4138
	struct drm_i915_private *dev_priv = dev->dev_private;
4149
	struct drm_i915_private *dev_priv = dev->dev_private;
4139
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4150
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4140
	int pipe = intel_crtc->pipe;
4151
	int pipe = intel_crtc->pipe;
4141
	u32 dpll;
4152
	u32 dpll;
4142
	bool is_sdvo;
4153
	bool is_sdvo;
4143
 
4154
 
4144
	is_sdvo = intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO) ||
4155
	is_sdvo = intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO) ||
4145
		intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI);
4156
		intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI);
4146
 
4157
 
4147
	dpll = DPLL_VGA_MODE_DIS;
4158
	dpll = DPLL_VGA_MODE_DIS;
4148
 
4159
 
4149
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
4160
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
4150
		dpll |= DPLLB_MODE_LVDS;
4161
		dpll |= DPLLB_MODE_LVDS;
4151
	else
4162
	else
4152
		dpll |= DPLLB_MODE_DAC_SERIAL;
4163
		dpll |= DPLLB_MODE_DAC_SERIAL;
4153
	if (is_sdvo) {
4164
	if (is_sdvo) {
4154
		int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
4165
		int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
4155
		if (pixel_multiplier > 1) {
4166
		if (pixel_multiplier > 1) {
4156
			if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4167
			if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4157
				dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
4168
				dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
4158
		}
4169
		}
4159
		dpll |= DPLL_DVO_HIGH_SPEED;
4170
		dpll |= DPLL_DVO_HIGH_SPEED;
4160
	}
4171
	}
4161
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
4172
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
4162
		dpll |= DPLL_DVO_HIGH_SPEED;
4173
		dpll |= DPLL_DVO_HIGH_SPEED;
4163
 
4174
 
4164
	/* compute bitmask from p1 value */
4175
	/* compute bitmask from p1 value */
4165
	if (IS_PINEVIEW(dev))
4176
	if (IS_PINEVIEW(dev))
4166
		dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
4177
		dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
4167
	else {
4178
	else {
4168
		dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4179
		dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4169
		if (IS_G4X(dev) && reduced_clock)
4180
		if (IS_G4X(dev) && reduced_clock)
4170
			dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
4181
			dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
4171
	}
4182
	}
4172
	switch (clock->p2) {
4183
	switch (clock->p2) {
4173
	case 5:
4184
	case 5:
4174
		dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4185
		dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4175
		break;
4186
		break;
4176
	case 7:
4187
	case 7:
4177
		dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4188
		dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4178
		break;
4189
		break;
4179
	case 10:
4190
	case 10:
4180
		dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4191
		dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4181
		break;
4192
		break;
4182
	case 14:
4193
	case 14:
4183
		dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4194
		dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4184
		break;
4195
		break;
4185
	}
4196
	}
4186
	if (INTEL_INFO(dev)->gen >= 4)
4197
	if (INTEL_INFO(dev)->gen >= 4)
4187
		dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
4198
		dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
4188
 
4199
 
4189
	if (is_sdvo && intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
4200
	if (is_sdvo && intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
4190
		dpll |= PLL_REF_INPUT_TVCLKINBC;
4201
		dpll |= PLL_REF_INPUT_TVCLKINBC;
4191
	else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
4202
	else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
4192
		/* XXX: just matching BIOS for now */
4203
		/* XXX: just matching BIOS for now */
4193
		/*	dpll |= PLL_REF_INPUT_TVCLKINBC; */
4204
		/*	dpll |= PLL_REF_INPUT_TVCLKINBC; */
4194
		dpll |= 3;
4205
		dpll |= 3;
4195
	else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
4206
	else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
4196
		 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4207
		 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4197
		dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4208
		dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4198
	else
4209
	else
4199
		dpll |= PLL_REF_INPUT_DREFCLK;
4210
		dpll |= PLL_REF_INPUT_DREFCLK;
4200
 
4211
 
4201
	dpll |= DPLL_VCO_ENABLE;
4212
	dpll |= DPLL_VCO_ENABLE;
4202
	I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
4213
	I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
4203
	POSTING_READ(DPLL(pipe));
4214
	POSTING_READ(DPLL(pipe));
4204
	udelay(150);
4215
	udelay(150);
4205
 
4216
 
4206
	/* The LVDS pin pair needs to be on before the DPLLs are enabled.
4217
	/* The LVDS pin pair needs to be on before the DPLLs are enabled.
4207
	 * This is an exception to the general rule that mode_set doesn't turn
4218
	 * This is an exception to the general rule that mode_set doesn't turn
4208
	 * things on.
4219
	 * things on.
4209
	 */
4220
	 */
4210
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
4221
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
4211
		intel_update_lvds(crtc, clock, adjusted_mode);
4222
		intel_update_lvds(crtc, clock, adjusted_mode);
4212
 
4223
 
4213
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
4224
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
4214
		intel_dp_set_m_n(crtc, mode, adjusted_mode);
4225
		intel_dp_set_m_n(crtc, mode, adjusted_mode);
4215
 
4226
 
4216
	I915_WRITE(DPLL(pipe), dpll);
4227
	I915_WRITE(DPLL(pipe), dpll);
4217
 
4228
 
4218
	/* Wait for the clocks to stabilize. */
4229
	/* Wait for the clocks to stabilize. */
4219
	POSTING_READ(DPLL(pipe));
4230
	POSTING_READ(DPLL(pipe));
4220
	udelay(150);
4231
	udelay(150);
4221
 
4232
 
4222
	if (INTEL_INFO(dev)->gen >= 4) {
4233
	if (INTEL_INFO(dev)->gen >= 4) {
4223
		u32 temp = 0;
4234
		u32 temp = 0;
4224
		if (is_sdvo) {
4235
		if (is_sdvo) {
4225
			temp = intel_mode_get_pixel_multiplier(adjusted_mode);
4236
			temp = intel_mode_get_pixel_multiplier(adjusted_mode);
4226
			if (temp > 1)
4237
			if (temp > 1)
4227
				temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
4238
				temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
4228
			else
4239
			else
4229
				temp = 0;
4240
				temp = 0;
4230
	}
4241
	}
4231
		I915_WRITE(DPLL_MD(pipe), temp);
4242
		I915_WRITE(DPLL_MD(pipe), temp);
4232
	} else {
4243
	} else {
4233
		/* The pixel multiplier can only be updated once the
4244
		/* The pixel multiplier can only be updated once the
4234
		 * DPLL is enabled and the clocks are stable.
4245
		 * DPLL is enabled and the clocks are stable.
4235
		 *
4246
		 *
4236
		 * So write it again.
4247
		 * So write it again.
4237
		 */
4248
		 */
4238
		I915_WRITE(DPLL(pipe), dpll);
4249
		I915_WRITE(DPLL(pipe), dpll);
4239
	}
4250
	}
4240
}
4251
}
4241
 
4252
 
4242
static void i8xx_update_pll(struct drm_crtc *crtc,
4253
static void i8xx_update_pll(struct drm_crtc *crtc,
4243
			    struct drm_display_mode *adjusted_mode,
4254
			    struct drm_display_mode *adjusted_mode,
4244
			    intel_clock_t *clock,
4255
			    intel_clock_t *clock,
4245
			    int num_connectors)
4256
			    int num_connectors)
4246
{
4257
{
4247
	struct drm_device *dev = crtc->dev;
4258
	struct drm_device *dev = crtc->dev;
4248
	struct drm_i915_private *dev_priv = dev->dev_private;
4259
	struct drm_i915_private *dev_priv = dev->dev_private;
4249
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4260
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4250
	int pipe = intel_crtc->pipe;
4261
	int pipe = intel_crtc->pipe;
4251
	u32 dpll;
4262
	u32 dpll;
4252
 
4263
 
4253
	dpll = DPLL_VGA_MODE_DIS;
4264
	dpll = DPLL_VGA_MODE_DIS;
4254
 
4265
 
4255
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
4266
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
4256
		dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4267
		dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4257
	} else {
4268
	} else {
4258
		if (clock->p1 == 2)
4269
		if (clock->p1 == 2)
4259
			dpll |= PLL_P1_DIVIDE_BY_TWO;
4270
			dpll |= PLL_P1_DIVIDE_BY_TWO;
4260
		else
4271
		else
4261
			dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4272
			dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4262
		if (clock->p2 == 4)
4273
		if (clock->p2 == 4)
4263
			dpll |= PLL_P2_DIVIDE_BY_4;
4274
			dpll |= PLL_P2_DIVIDE_BY_4;
4264
	}
4275
	}
4265
 
4276
 
4266
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
4277
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
4267
		/* XXX: just matching BIOS for now */
4278
		/* XXX: just matching BIOS for now */
4268
		/*	dpll |= PLL_REF_INPUT_TVCLKINBC; */
4279
		/*	dpll |= PLL_REF_INPUT_TVCLKINBC; */
4269
		dpll |= 3;
4280
		dpll |= 3;
4270
	else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
4281
	else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
4271
		 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4282
		 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4272
		dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4283
		dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4273
	else
4284
	else
4274
		dpll |= PLL_REF_INPUT_DREFCLK;
4285
		dpll |= PLL_REF_INPUT_DREFCLK;
4275
 
4286
 
4276
	dpll |= DPLL_VCO_ENABLE;
4287
	dpll |= DPLL_VCO_ENABLE;
4277
	I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
4288
	I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
4278
	POSTING_READ(DPLL(pipe));
4289
	POSTING_READ(DPLL(pipe));
4279
	udelay(150);
4290
	udelay(150);
4280
 
4291
 
4281
	/* The LVDS pin pair needs to be on before the DPLLs are enabled.
4292
	/* The LVDS pin pair needs to be on before the DPLLs are enabled.
4282
	 * This is an exception to the general rule that mode_set doesn't turn
4293
	 * This is an exception to the general rule that mode_set doesn't turn
4283
	 * things on.
4294
	 * things on.
4284
	 */
4295
	 */
4285
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
4296
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
4286
		intel_update_lvds(crtc, clock, adjusted_mode);
4297
		intel_update_lvds(crtc, clock, adjusted_mode);
4287
 
4298
 
4288
	I915_WRITE(DPLL(pipe), dpll);
4299
	I915_WRITE(DPLL(pipe), dpll);
4289
 
4300
 
4290
	/* Wait for the clocks to stabilize. */
4301
	/* Wait for the clocks to stabilize. */
4291
	POSTING_READ(DPLL(pipe));
4302
	POSTING_READ(DPLL(pipe));
4292
	udelay(150);
4303
	udelay(150);
4293
 
4304
 
4294
	/* The pixel multiplier can only be updated once the
4305
	/* The pixel multiplier can only be updated once the
4295
	 * DPLL is enabled and the clocks are stable.
4306
	 * DPLL is enabled and the clocks are stable.
4296
	 *
4307
	 *
4297
	 * So write it again.
4308
	 * So write it again.
4298
	 */
4309
	 */
4299
	I915_WRITE(DPLL(pipe), dpll);
4310
	I915_WRITE(DPLL(pipe), dpll);
4300
}
4311
}
4301
 
4312
 
4302
static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
4313
static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
4303
			      struct drm_display_mode *mode,
4314
			      struct drm_display_mode *mode,
4304
			      struct drm_display_mode *adjusted_mode,
4315
			      struct drm_display_mode *adjusted_mode,
4305
			      int x, int y,
4316
			      int x, int y,
4306
			      struct drm_framebuffer *fb)
4317
			      struct drm_framebuffer *fb)
4307
{
4318
{
4308
	struct drm_device *dev = crtc->dev;
4319
	struct drm_device *dev = crtc->dev;
4309
	struct drm_i915_private *dev_priv = dev->dev_private;
4320
	struct drm_i915_private *dev_priv = dev->dev_private;
4310
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4321
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4311
	int pipe = intel_crtc->pipe;
4322
	int pipe = intel_crtc->pipe;
4312
	int plane = intel_crtc->plane;
4323
	int plane = intel_crtc->plane;
4313
	int refclk, num_connectors = 0;
4324
	int refclk, num_connectors = 0;
4314
	intel_clock_t clock, reduced_clock;
4325
	intel_clock_t clock, reduced_clock;
4315
	u32 dspcntr, pipeconf, vsyncshift;
4326
	u32 dspcntr, pipeconf, vsyncshift;
4316
	bool ok, has_reduced_clock = false, is_sdvo = false;
4327
	bool ok, has_reduced_clock = false, is_sdvo = false;
4317
	bool is_lvds = false, is_tv = false, is_dp = false;
4328
	bool is_lvds = false, is_tv = false, is_dp = false;
4318
	struct intel_encoder *encoder;
4329
	struct intel_encoder *encoder;
4319
	const intel_limit_t *limit;
4330
	const intel_limit_t *limit;
4320
	int ret;
4331
	int ret;
4321
 
4332
 
4322
	for_each_encoder_on_crtc(dev, crtc, encoder) {
4333
	for_each_encoder_on_crtc(dev, crtc, encoder) {
4323
		switch (encoder->type) {
4334
		switch (encoder->type) {
4324
		case INTEL_OUTPUT_LVDS:
4335
		case INTEL_OUTPUT_LVDS:
4325
			is_lvds = true;
4336
			is_lvds = true;
4326
			break;
4337
			break;
4327
		case INTEL_OUTPUT_SDVO:
4338
		case INTEL_OUTPUT_SDVO:
4328
		case INTEL_OUTPUT_HDMI:
4339
		case INTEL_OUTPUT_HDMI:
4329
			is_sdvo = true;
4340
			is_sdvo = true;
4330
			if (encoder->needs_tv_clock)
4341
			if (encoder->needs_tv_clock)
4331
				is_tv = true;
4342
				is_tv = true;
4332
			break;
4343
			break;
4333
		case INTEL_OUTPUT_TVOUT:
4344
		case INTEL_OUTPUT_TVOUT:
4334
			is_tv = true;
4345
			is_tv = true;
4335
			break;
4346
			break;
4336
		case INTEL_OUTPUT_DISPLAYPORT:
4347
		case INTEL_OUTPUT_DISPLAYPORT:
4337
			is_dp = true;
4348
			is_dp = true;
4338
			break;
4349
			break;
4339
		}
4350
		}
4340
 
4351
 
4341
		num_connectors++;
4352
		num_connectors++;
4342
	}
4353
	}
4343
 
4354
 
4344
	refclk = i9xx_get_refclk(crtc, num_connectors);
4355
	refclk = i9xx_get_refclk(crtc, num_connectors);
4345
 
4356
 
4346
	/*
4357
	/*
4347
	 * Returns a set of divisors for the desired target clock with the given
4358
	 * Returns a set of divisors for the desired target clock with the given
4348
	 * refclk, or FALSE.  The returned values represent the clock equation:
4359
	 * refclk, or FALSE.  The returned values represent the clock equation:
4349
	 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
4360
	 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
4350
	 */
4361
	 */
4351
	limit = intel_limit(crtc, refclk);
4362
	limit = intel_limit(crtc, refclk);
4352
	ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
4363
	ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
4353
			     &clock);
4364
			     &clock);
4354
	if (!ok) {
4365
	if (!ok) {
4355
		DRM_ERROR("Couldn't find PLL settings for mode!\n");
4366
		DRM_ERROR("Couldn't find PLL settings for mode!\n");
4356
		return -EINVAL;
4367
		return -EINVAL;
4357
	}
4368
	}
4358
 
4369
 
4359
	/* Ensure that the cursor is valid for the new mode before changing... */
4370
	/* Ensure that the cursor is valid for the new mode before changing... */
4360
//   intel_crtc_update_cursor(crtc, true);
4371
//   intel_crtc_update_cursor(crtc, true);
4361
 
4372
 
4362
	if (is_lvds && dev_priv->lvds_downclock_avail) {
4373
	if (is_lvds && dev_priv->lvds_downclock_avail) {
4363
		/*
4374
		/*
4364
		 * Ensure we match the reduced clock's P to the target clock.
4375
		 * Ensure we match the reduced clock's P to the target clock.
4365
		 * If the clocks don't match, we can't switch the display clock
4376
		 * If the clocks don't match, we can't switch the display clock
4366
		 * by using the FP0/FP1. In such case we will disable the LVDS
4377
		 * by using the FP0/FP1. In such case we will disable the LVDS
4367
		 * downclock feature.
4378
		 * downclock feature.
4368
		*/
4379
		*/
4369
		has_reduced_clock = limit->find_pll(limit, crtc,
4380
		has_reduced_clock = limit->find_pll(limit, crtc,
4370
						    dev_priv->lvds_downclock,
4381
						    dev_priv->lvds_downclock,
4371
						    refclk,
4382
						    refclk,
4372
						    &clock,
4383
						    &clock,
4373
						    &reduced_clock);
4384
						    &reduced_clock);
4374
	}
4385
	}
4375
 
4386
 
4376
	if (is_sdvo && is_tv)
4387
	if (is_sdvo && is_tv)
4377
		i9xx_adjust_sdvo_tv_clock(adjusted_mode, &clock);
4388
		i9xx_adjust_sdvo_tv_clock(adjusted_mode, &clock);
4378
 
4389
 
4379
	i9xx_update_pll_dividers(crtc, &clock, has_reduced_clock ?
4390
	i9xx_update_pll_dividers(crtc, &clock, has_reduced_clock ?
4380
				 &reduced_clock : NULL);
4391
				 &reduced_clock : NULL);
4381
 
4392
 
4382
	if (IS_GEN2(dev))
4393
	if (IS_GEN2(dev))
4383
		i8xx_update_pll(crtc, adjusted_mode, &clock, num_connectors);
4394
		i8xx_update_pll(crtc, adjusted_mode, &clock, num_connectors);
4384
	else if (IS_VALLEYVIEW(dev))
4395
	else if (IS_VALLEYVIEW(dev))
4385
		vlv_update_pll(crtc, mode,adjusted_mode, &clock, NULL,
4396
		vlv_update_pll(crtc, mode,adjusted_mode, &clock, NULL,
4386
			       refclk, num_connectors);
4397
			       refclk, num_connectors);
4387
	else
4398
	else
4388
		i9xx_update_pll(crtc, mode, adjusted_mode, &clock,
4399
		i9xx_update_pll(crtc, mode, adjusted_mode, &clock,
4389
				has_reduced_clock ? &reduced_clock : NULL,
4400
				has_reduced_clock ? &reduced_clock : NULL,
4390
				num_connectors);
4401
				num_connectors);
4391
 
4402
 
4392
	/* setup pipeconf */
4403
	/* setup pipeconf */
4393
	pipeconf = I915_READ(PIPECONF(pipe));
4404
	pipeconf = I915_READ(PIPECONF(pipe));
4394
 
4405
 
4395
	/* Set up the display plane register */
4406
	/* Set up the display plane register */
4396
	dspcntr = DISPPLANE_GAMMA_ENABLE;
4407
	dspcntr = DISPPLANE_GAMMA_ENABLE;
4397
 
4408
 
4398
	if (pipe == 0)
4409
	if (pipe == 0)
4399
		dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
4410
		dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
4400
	else
4411
	else
4401
		dspcntr |= DISPPLANE_SEL_PIPE_B;
4412
		dspcntr |= DISPPLANE_SEL_PIPE_B;
4402
 
4413
 
4403
	if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
4414
	if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
4404
		/* Enable pixel doubling when the dot clock is > 90% of the (display)
4415
		/* Enable pixel doubling when the dot clock is > 90% of the (display)
4405
		 * core speed.
4416
		 * core speed.
4406
		 *
4417
		 *
4407
		 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
4418
		 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
4408
		 * pipe == 0 check?
4419
		 * pipe == 0 check?
4409
		 */
4420
		 */
4410
		if (mode->clock >
4421
		if (mode->clock >
4411
		    dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
4422
		    dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
4412
			pipeconf |= PIPECONF_DOUBLE_WIDE;
4423
			pipeconf |= PIPECONF_DOUBLE_WIDE;
4413
		else
4424
		else
4414
			pipeconf &= ~PIPECONF_DOUBLE_WIDE;
4425
			pipeconf &= ~PIPECONF_DOUBLE_WIDE;
4415
		}
4426
		}
4416
 
4427
 
4417
	/* default to 8bpc */
4428
	/* default to 8bpc */
4418
	pipeconf &= ~(PIPECONF_BPP_MASK | PIPECONF_DITHER_EN);
4429
	pipeconf &= ~(PIPECONF_BPP_MASK | PIPECONF_DITHER_EN);
4419
	if (is_dp) {
4430
	if (is_dp) {
4420
		if (adjusted_mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
4431
		if (adjusted_mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
4421
			pipeconf |= PIPECONF_BPP_6 |
4432
			pipeconf |= PIPECONF_BPP_6 |
4422
				    PIPECONF_DITHER_EN |
4433
				    PIPECONF_DITHER_EN |
4423
				    PIPECONF_DITHER_TYPE_SP;
4434
				    PIPECONF_DITHER_TYPE_SP;
4424
			}
4435
			}
4425
		}
4436
		}
4426
 
4437
 
4427
	DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
4438
	DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
4428
	drm_mode_debug_printmodeline(mode);
4439
	drm_mode_debug_printmodeline(mode);
4429
 
4440
 
4430
	if (HAS_PIPE_CXSR(dev)) {
4441
	if (HAS_PIPE_CXSR(dev)) {
4431
		if (intel_crtc->lowfreq_avail) {
4442
		if (intel_crtc->lowfreq_avail) {
4432
			DRM_DEBUG_KMS("enabling CxSR downclocking\n");
4443
			DRM_DEBUG_KMS("enabling CxSR downclocking\n");
4433
			pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
4444
			pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
4434
		} else {
4445
		} else {
4435
			DRM_DEBUG_KMS("disabling CxSR downclocking\n");
4446
			DRM_DEBUG_KMS("disabling CxSR downclocking\n");
4436
			pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
4447
			pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
4437
			}
4448
			}
4438
		}
4449
		}
4439
 
4450
 
4440
	pipeconf &= ~PIPECONF_INTERLACE_MASK;
4451
	pipeconf &= ~PIPECONF_INTERLACE_MASK;
4441
	if (!IS_GEN2(dev) &&
4452
	if (!IS_GEN2(dev) &&
4442
	    adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4453
	    adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4443
		pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
4454
		pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
4444
		/* the chip adds 2 halflines automatically */
4455
		/* the chip adds 2 halflines automatically */
4445
		adjusted_mode->crtc_vtotal -= 1;
4456
		adjusted_mode->crtc_vtotal -= 1;
4446
		adjusted_mode->crtc_vblank_end -= 1;
4457
		adjusted_mode->crtc_vblank_end -= 1;
4447
		vsyncshift = adjusted_mode->crtc_hsync_start
4458
		vsyncshift = adjusted_mode->crtc_hsync_start
4448
			     - adjusted_mode->crtc_htotal/2;
4459
			     - adjusted_mode->crtc_htotal/2;
4449
			} else {
4460
			} else {
4450
		pipeconf |= PIPECONF_PROGRESSIVE;
4461
		pipeconf |= PIPECONF_PROGRESSIVE;
4451
		vsyncshift = 0;
4462
		vsyncshift = 0;
4452
	}
4463
	}
4453
 
4464
 
4454
	if (!IS_GEN3(dev))
4465
	if (!IS_GEN3(dev))
4455
		I915_WRITE(VSYNCSHIFT(pipe), vsyncshift);
4466
		I915_WRITE(VSYNCSHIFT(pipe), vsyncshift);
4456
 
4467
 
4457
	I915_WRITE(HTOTAL(pipe),
4468
	I915_WRITE(HTOTAL(pipe),
4458
		   (adjusted_mode->crtc_hdisplay - 1) |
4469
		   (adjusted_mode->crtc_hdisplay - 1) |
4459
		   ((adjusted_mode->crtc_htotal - 1) << 16));
4470
		   ((adjusted_mode->crtc_htotal - 1) << 16));
4460
	I915_WRITE(HBLANK(pipe),
4471
	I915_WRITE(HBLANK(pipe),
4461
		   (adjusted_mode->crtc_hblank_start - 1) |
4472
		   (adjusted_mode->crtc_hblank_start - 1) |
4462
		   ((adjusted_mode->crtc_hblank_end - 1) << 16));
4473
		   ((adjusted_mode->crtc_hblank_end - 1) << 16));
4463
	I915_WRITE(HSYNC(pipe),
4474
	I915_WRITE(HSYNC(pipe),
4464
		   (adjusted_mode->crtc_hsync_start - 1) |
4475
		   (adjusted_mode->crtc_hsync_start - 1) |
4465
		   ((adjusted_mode->crtc_hsync_end - 1) << 16));
4476
		   ((adjusted_mode->crtc_hsync_end - 1) << 16));
4466
 
4477
 
4467
	I915_WRITE(VTOTAL(pipe),
4478
	I915_WRITE(VTOTAL(pipe),
4468
		   (adjusted_mode->crtc_vdisplay - 1) |
4479
		   (adjusted_mode->crtc_vdisplay - 1) |
4469
		   ((adjusted_mode->crtc_vtotal - 1) << 16));
4480
		   ((adjusted_mode->crtc_vtotal - 1) << 16));
4470
	I915_WRITE(VBLANK(pipe),
4481
	I915_WRITE(VBLANK(pipe),
4471
		   (adjusted_mode->crtc_vblank_start - 1) |
4482
		   (adjusted_mode->crtc_vblank_start - 1) |
4472
		   ((adjusted_mode->crtc_vblank_end - 1) << 16));
4483
		   ((adjusted_mode->crtc_vblank_end - 1) << 16));
4473
	I915_WRITE(VSYNC(pipe),
4484
	I915_WRITE(VSYNC(pipe),
4474
		   (adjusted_mode->crtc_vsync_start - 1) |
4485
		   (adjusted_mode->crtc_vsync_start - 1) |
4475
		   ((adjusted_mode->crtc_vsync_end - 1) << 16));
4486
		   ((adjusted_mode->crtc_vsync_end - 1) << 16));
4476
 
4487
 
4477
	/* pipesrc and dspsize control the size that is scaled from,
4488
	/* pipesrc and dspsize control the size that is scaled from,
4478
	 * which should always be the user's requested size.
4489
	 * which should always be the user's requested size.
4479
	 */
4490
	 */
4480
	I915_WRITE(DSPSIZE(plane),
4491
	I915_WRITE(DSPSIZE(plane),
4481
		   ((mode->vdisplay - 1) << 16) |
4492
		   ((mode->vdisplay - 1) << 16) |
4482
		   (mode->hdisplay - 1));
4493
		   (mode->hdisplay - 1));
4483
	I915_WRITE(DSPPOS(plane), 0);
4494
	I915_WRITE(DSPPOS(plane), 0);
4484
	I915_WRITE(PIPESRC(pipe),
4495
	I915_WRITE(PIPESRC(pipe),
4485
		   ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
4496
		   ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
4486
 
4497
 
4487
	I915_WRITE(PIPECONF(pipe), pipeconf);
4498
	I915_WRITE(PIPECONF(pipe), pipeconf);
4488
	POSTING_READ(PIPECONF(pipe));
4499
	POSTING_READ(PIPECONF(pipe));
4489
	intel_enable_pipe(dev_priv, pipe, false);
4500
	intel_enable_pipe(dev_priv, pipe, false);
4490
 
4501
 
4491
	intel_wait_for_vblank(dev, pipe);
4502
	intel_wait_for_vblank(dev, pipe);
4492
 
4503
 
4493
	I915_WRITE(DSPCNTR(plane), dspcntr);
4504
	I915_WRITE(DSPCNTR(plane), dspcntr);
4494
	POSTING_READ(DSPCNTR(plane));
4505
	POSTING_READ(DSPCNTR(plane));
4495
 
4506
 
4496
	ret = intel_pipe_set_base(crtc, x, y, fb);
4507
	ret = intel_pipe_set_base(crtc, x, y, fb);
4497
 
4508
 
4498
	intel_update_watermarks(dev);
4509
	intel_update_watermarks(dev);
4499
 
4510
 
4500
    return ret;
4511
    return ret;
4501
}
4512
}
4502
 
4513
 
4503
/*
4514
/*
4504
 * Initialize reference clocks when the driver loads
4515
 * Initialize reference clocks when the driver loads
4505
 */
4516
 */
4506
void ironlake_init_pch_refclk(struct drm_device *dev)
4517
void ironlake_init_pch_refclk(struct drm_device *dev)
4507
{
4518
{
4508
	struct drm_i915_private *dev_priv = dev->dev_private;
4519
	struct drm_i915_private *dev_priv = dev->dev_private;
4509
	struct drm_mode_config *mode_config = &dev->mode_config;
4520
	struct drm_mode_config *mode_config = &dev->mode_config;
4510
	struct intel_encoder *encoder;
4521
	struct intel_encoder *encoder;
4511
	u32 temp;
4522
	u32 temp;
4512
	bool has_lvds = false;
4523
	bool has_lvds = false;
4513
	bool has_cpu_edp = false;
4524
	bool has_cpu_edp = false;
4514
	bool has_pch_edp = false;
4525
	bool has_pch_edp = false;
4515
	bool has_panel = false;
4526
	bool has_panel = false;
4516
	bool has_ck505 = false;
4527
	bool has_ck505 = false;
4517
	bool can_ssc = false;
4528
	bool can_ssc = false;
4518
 
4529
 
4519
	/* We need to take the global config into account */
4530
	/* We need to take the global config into account */
4520
		list_for_each_entry(encoder, &mode_config->encoder_list,
4531
		list_for_each_entry(encoder, &mode_config->encoder_list,
4521
				    base.head) {
4532
				    base.head) {
4522
			switch (encoder->type) {
4533
			switch (encoder->type) {
4523
			case INTEL_OUTPUT_LVDS:
4534
			case INTEL_OUTPUT_LVDS:
4524
			has_panel = true;
4535
			has_panel = true;
4525
				has_lvds = true;
4536
				has_lvds = true;
4526
			break;
4537
			break;
4527
			case INTEL_OUTPUT_EDP:
4538
			case INTEL_OUTPUT_EDP:
4528
			has_panel = true;
4539
			has_panel = true;
4529
			if (intel_encoder_is_pch_edp(&encoder->base))
4540
			if (intel_encoder_is_pch_edp(&encoder->base))
4530
				has_pch_edp = true;
4541
				has_pch_edp = true;
4531
			else
4542
			else
4532
				has_cpu_edp = true;
4543
				has_cpu_edp = true;
4533
				break;
4544
				break;
4534
			}
4545
			}
4535
		}
4546
		}
4536
 
4547
 
4537
	if (HAS_PCH_IBX(dev)) {
4548
	if (HAS_PCH_IBX(dev)) {
4538
		has_ck505 = dev_priv->display_clock_mode;
4549
		has_ck505 = dev_priv->display_clock_mode;
4539
		can_ssc = has_ck505;
4550
		can_ssc = has_ck505;
4540
	} else {
4551
	} else {
4541
		has_ck505 = false;
4552
		has_ck505 = false;
4542
		can_ssc = true;
4553
		can_ssc = true;
4543
	}
4554
	}
4544
 
4555
 
4545
	DRM_DEBUG_KMS("has_panel %d has_lvds %d has_pch_edp %d has_cpu_edp %d has_ck505 %d\n",
4556
	DRM_DEBUG_KMS("has_panel %d has_lvds %d has_pch_edp %d has_cpu_edp %d has_ck505 %d\n",
4546
		      has_panel, has_lvds, has_pch_edp, has_cpu_edp,
4557
		      has_panel, has_lvds, has_pch_edp, has_cpu_edp,
4547
		      has_ck505);
4558
		      has_ck505);
4548
 
4559
 
4549
	/* Ironlake: try to setup display ref clock before DPLL
4560
	/* Ironlake: try to setup display ref clock before DPLL
4550
	 * enabling. This is only under driver's control after
4561
	 * enabling. This is only under driver's control after
4551
	 * PCH B stepping, previous chipset stepping should be
4562
	 * PCH B stepping, previous chipset stepping should be
4552
	 * ignoring this setting.
4563
	 * ignoring this setting.
4553
	 */
4564
	 */
4554
	temp = I915_READ(PCH_DREF_CONTROL);
4565
	temp = I915_READ(PCH_DREF_CONTROL);
4555
	/* Always enable nonspread source */
4566
	/* Always enable nonspread source */
4556
	temp &= ~DREF_NONSPREAD_SOURCE_MASK;
4567
	temp &= ~DREF_NONSPREAD_SOURCE_MASK;
4557
 
4568
 
4558
	if (has_ck505)
4569
	if (has_ck505)
4559
		temp |= DREF_NONSPREAD_CK505_ENABLE;
4570
		temp |= DREF_NONSPREAD_CK505_ENABLE;
4560
	else
4571
	else
4561
	temp |= DREF_NONSPREAD_SOURCE_ENABLE;
4572
	temp |= DREF_NONSPREAD_SOURCE_ENABLE;
4562
 
4573
 
4563
	if (has_panel) {
4574
	if (has_panel) {
4564
	temp &= ~DREF_SSC_SOURCE_MASK;
4575
	temp &= ~DREF_SSC_SOURCE_MASK;
4565
	temp |= DREF_SSC_SOURCE_ENABLE;
4576
	temp |= DREF_SSC_SOURCE_ENABLE;
4566
 
4577
 
4567
		/* SSC must be turned on before enabling the CPU output  */
4578
		/* SSC must be turned on before enabling the CPU output  */
4568
		if (intel_panel_use_ssc(dev_priv) && can_ssc) {
4579
		if (intel_panel_use_ssc(dev_priv) && can_ssc) {
4569
			DRM_DEBUG_KMS("Using SSC on panel\n");
4580
			DRM_DEBUG_KMS("Using SSC on panel\n");
4570
			temp |= DREF_SSC1_ENABLE;
4581
			temp |= DREF_SSC1_ENABLE;
4571
		} else
4582
		} else
4572
			temp &= ~DREF_SSC1_ENABLE;
4583
			temp &= ~DREF_SSC1_ENABLE;
4573
 
4584
 
4574
		/* Get SSC going before enabling the outputs */
4585
		/* Get SSC going before enabling the outputs */
4575
			I915_WRITE(PCH_DREF_CONTROL, temp);
4586
			I915_WRITE(PCH_DREF_CONTROL, temp);
4576
			POSTING_READ(PCH_DREF_CONTROL);
4587
			POSTING_READ(PCH_DREF_CONTROL);
4577
			udelay(200);
4588
			udelay(200);
4578
 
4589
 
4579
		temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
4590
		temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
4580
 
4591
 
4581
		/* Enable CPU source on CPU attached eDP */
4592
		/* Enable CPU source on CPU attached eDP */
4582
		if (has_cpu_edp) {
4593
		if (has_cpu_edp) {
4583
			if (intel_panel_use_ssc(dev_priv) && can_ssc) {
4594
			if (intel_panel_use_ssc(dev_priv) && can_ssc) {
4584
				DRM_DEBUG_KMS("Using SSC on eDP\n");
4595
				DRM_DEBUG_KMS("Using SSC on eDP\n");
4585
				temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
4596
				temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
4586
			}
4597
			}
4587
			else
4598
			else
4588
				temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
4599
				temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
4589
		} else
4600
		} else
4590
			temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
4601
			temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
4591
 
4602
 
4592
		I915_WRITE(PCH_DREF_CONTROL, temp);
4603
		I915_WRITE(PCH_DREF_CONTROL, temp);
4593
		POSTING_READ(PCH_DREF_CONTROL);
4604
		POSTING_READ(PCH_DREF_CONTROL);
4594
		udelay(200);
4605
		udelay(200);
4595
		} else {
4606
		} else {
4596
		DRM_DEBUG_KMS("Disabling SSC entirely\n");
4607
		DRM_DEBUG_KMS("Disabling SSC entirely\n");
4597
 
4608
 
4598
		temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
4609
		temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
4599
 
4610
 
4600
		/* Turn off CPU output */
4611
		/* Turn off CPU output */
4601
		temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
4612
		temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
4602
 
4613
 
4603
		I915_WRITE(PCH_DREF_CONTROL, temp);
4614
		I915_WRITE(PCH_DREF_CONTROL, temp);
4604
		POSTING_READ(PCH_DREF_CONTROL);
4615
		POSTING_READ(PCH_DREF_CONTROL);
4605
		udelay(200);
4616
		udelay(200);
4606
 
4617
 
4607
		/* Turn off the SSC source */
4618
		/* Turn off the SSC source */
4608
		temp &= ~DREF_SSC_SOURCE_MASK;
4619
		temp &= ~DREF_SSC_SOURCE_MASK;
4609
		temp |= DREF_SSC_SOURCE_DISABLE;
4620
		temp |= DREF_SSC_SOURCE_DISABLE;
4610
 
4621
 
4611
		/* Turn off SSC1 */
4622
		/* Turn off SSC1 */
4612
		temp &= ~ DREF_SSC1_ENABLE;
4623
		temp &= ~ DREF_SSC1_ENABLE;
4613
 
4624
 
4614
		I915_WRITE(PCH_DREF_CONTROL, temp);
4625
		I915_WRITE(PCH_DREF_CONTROL, temp);
4615
		POSTING_READ(PCH_DREF_CONTROL);
4626
		POSTING_READ(PCH_DREF_CONTROL);
4616
		udelay(200);
4627
		udelay(200);
4617
	}
4628
	}
4618
}
4629
}
4619
 
4630
 
4620
static int ironlake_get_refclk(struct drm_crtc *crtc)
4631
static int ironlake_get_refclk(struct drm_crtc *crtc)
4621
{
4632
{
4622
	struct drm_device *dev = crtc->dev;
4633
	struct drm_device *dev = crtc->dev;
4623
	struct drm_i915_private *dev_priv = dev->dev_private;
4634
	struct drm_i915_private *dev_priv = dev->dev_private;
4624
	struct intel_encoder *encoder;
4635
	struct intel_encoder *encoder;
4625
	struct intel_encoder *edp_encoder = NULL;
4636
	struct intel_encoder *edp_encoder = NULL;
4626
	int num_connectors = 0;
4637
	int num_connectors = 0;
4627
	bool is_lvds = false;
4638
	bool is_lvds = false;
4628
 
4639
 
4629
	for_each_encoder_on_crtc(dev, crtc, encoder) {
4640
	for_each_encoder_on_crtc(dev, crtc, encoder) {
4630
		switch (encoder->type) {
4641
		switch (encoder->type) {
4631
		case INTEL_OUTPUT_LVDS:
4642
		case INTEL_OUTPUT_LVDS:
4632
			is_lvds = true;
4643
			is_lvds = true;
4633
			break;
4644
			break;
4634
		case INTEL_OUTPUT_EDP:
4645
		case INTEL_OUTPUT_EDP:
4635
			edp_encoder = encoder;
4646
			edp_encoder = encoder;
4636
			break;
4647
			break;
4637
		}
4648
		}
4638
		num_connectors++;
4649
		num_connectors++;
4639
	}
4650
	}
4640
 
4651
 
4641
	if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
4652
	if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
4642
		DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
4653
		DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
4643
			      dev_priv->lvds_ssc_freq);
4654
			      dev_priv->lvds_ssc_freq);
4644
		return dev_priv->lvds_ssc_freq * 1000;
4655
		return dev_priv->lvds_ssc_freq * 1000;
4645
	}
4656
	}
4646
 
4657
 
4647
	return 120000;
4658
	return 120000;
4648
}
4659
}
4649
 
4660
 
4650
static void ironlake_set_pipeconf(struct drm_crtc *crtc,
4661
static void ironlake_set_pipeconf(struct drm_crtc *crtc,
4651
				  struct drm_display_mode *adjusted_mode,
4662
				  struct drm_display_mode *adjusted_mode,
4652
				  bool dither)
4663
				  bool dither)
4653
{
4664
{
4654
	struct drm_i915_private *dev_priv = crtc->dev->dev_private;
4665
	struct drm_i915_private *dev_priv = crtc->dev->dev_private;
4655
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4666
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4656
	int pipe = intel_crtc->pipe;
4667
	int pipe = intel_crtc->pipe;
4657
	uint32_t val;
4668
	uint32_t val;
4658
 
4669
 
4659
	val = I915_READ(PIPECONF(pipe));
4670
	val = I915_READ(PIPECONF(pipe));
4660
 
4671
 
4661
	val &= ~PIPE_BPC_MASK;
4672
	val &= ~PIPE_BPC_MASK;
4662
	switch (intel_crtc->bpp) {
4673
	switch (intel_crtc->bpp) {
4663
	case 18:
4674
	case 18:
4664
		val |= PIPE_6BPC;
4675
		val |= PIPE_6BPC;
4665
		break;
4676
		break;
4666
	case 24:
4677
	case 24:
4667
		val |= PIPE_8BPC;
4678
		val |= PIPE_8BPC;
4668
		break;
4679
		break;
4669
	case 30:
4680
	case 30:
4670
		val |= PIPE_10BPC;
4681
		val |= PIPE_10BPC;
4671
		break;
4682
		break;
4672
	case 36:
4683
	case 36:
4673
		val |= PIPE_12BPC;
4684
		val |= PIPE_12BPC;
4674
		break;
4685
		break;
4675
	default:
4686
	default:
4676
		val |= PIPE_8BPC;
4687
		val |= PIPE_8BPC;
4677
		break;
4688
		break;
4678
	}
4689
	}
4679
 
4690
 
4680
	val &= ~(PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
4691
	val &= ~(PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
4681
	if (dither)
4692
	if (dither)
4682
		val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
4693
		val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
4683
 
4694
 
4684
	val &= ~PIPECONF_INTERLACE_MASK;
4695
	val &= ~PIPECONF_INTERLACE_MASK;
4685
	if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
4696
	if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
4686
		val |= PIPECONF_INTERLACED_ILK;
4697
		val |= PIPECONF_INTERLACED_ILK;
4687
	else
4698
	else
4688
		val |= PIPECONF_PROGRESSIVE;
4699
		val |= PIPECONF_PROGRESSIVE;
4689
 
4700
 
4690
	I915_WRITE(PIPECONF(pipe), val);
4701
	I915_WRITE(PIPECONF(pipe), val);
4691
	POSTING_READ(PIPECONF(pipe));
4702
	POSTING_READ(PIPECONF(pipe));
4692
}
4703
}
4693
 
4704
 
4694
static bool ironlake_compute_clocks(struct drm_crtc *crtc,
4705
static bool ironlake_compute_clocks(struct drm_crtc *crtc,
4695
				    struct drm_display_mode *adjusted_mode,
4706
				    struct drm_display_mode *adjusted_mode,
4696
				    intel_clock_t *clock,
4707
				    intel_clock_t *clock,
4697
				    bool *has_reduced_clock,
4708
				    bool *has_reduced_clock,
4698
				    intel_clock_t *reduced_clock)
4709
				    intel_clock_t *reduced_clock)
4699
{
4710
{
4700
	struct drm_device *dev = crtc->dev;
4711
	struct drm_device *dev = crtc->dev;
4701
	struct drm_i915_private *dev_priv = dev->dev_private;
4712
	struct drm_i915_private *dev_priv = dev->dev_private;
4702
	struct intel_encoder *intel_encoder;
4713
	struct intel_encoder *intel_encoder;
4703
	int refclk;
4714
	int refclk;
4704
	const intel_limit_t *limit;
4715
	const intel_limit_t *limit;
4705
	bool ret, is_sdvo = false, is_tv = false, is_lvds = false;
4716
	bool ret, is_sdvo = false, is_tv = false, is_lvds = false;
4706
 
4717
 
4707
	for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
4718
	for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
4708
		switch (intel_encoder->type) {
4719
		switch (intel_encoder->type) {
4709
		case INTEL_OUTPUT_LVDS:
4720
		case INTEL_OUTPUT_LVDS:
4710
			is_lvds = true;
4721
			is_lvds = true;
4711
			break;
4722
			break;
4712
		case INTEL_OUTPUT_SDVO:
4723
		case INTEL_OUTPUT_SDVO:
4713
		case INTEL_OUTPUT_HDMI:
4724
		case INTEL_OUTPUT_HDMI:
4714
			is_sdvo = true;
4725
			is_sdvo = true;
4715
			if (intel_encoder->needs_tv_clock)
4726
			if (intel_encoder->needs_tv_clock)
4716
				is_tv = true;
4727
				is_tv = true;
4717
			break;
4728
			break;
4718
		case INTEL_OUTPUT_TVOUT:
4729
		case INTEL_OUTPUT_TVOUT:
4719
			is_tv = true;
4730
			is_tv = true;
4720
			break;
4731
			break;
4721
		}
4732
		}
4722
	}
4733
	}
4723
 
4734
 
4724
	refclk = ironlake_get_refclk(crtc);
4735
	refclk = ironlake_get_refclk(crtc);
4725
 
4736
 
4726
	/*
4737
	/*
4727
	 * Returns a set of divisors for the desired target clock with the given
4738
	 * Returns a set of divisors for the desired target clock with the given
4728
	 * refclk, or FALSE.  The returned values represent the clock equation:
4739
	 * refclk, or FALSE.  The returned values represent the clock equation:
4729
	 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
4740
	 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
4730
	 */
4741
	 */
4731
	limit = intel_limit(crtc, refclk);
4742
	limit = intel_limit(crtc, refclk);
4732
	ret = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
4743
	ret = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
4733
			      clock);
4744
			      clock);
4734
	if (!ret)
4745
	if (!ret)
4735
		return false;
4746
		return false;
4736
 
4747
 
4737
	if (is_lvds && dev_priv->lvds_downclock_avail) {
4748
	if (is_lvds && dev_priv->lvds_downclock_avail) {
4738
		/*
4749
		/*
4739
		 * Ensure we match the reduced clock's P to the target clock.
4750
		 * Ensure we match the reduced clock's P to the target clock.
4740
		 * If the clocks don't match, we can't switch the display clock
4751
		 * If the clocks don't match, we can't switch the display clock
4741
		 * by using the FP0/FP1. In such case we will disable the LVDS
4752
		 * by using the FP0/FP1. In such case we will disable the LVDS
4742
		 * downclock feature.
4753
		 * downclock feature.
4743
		*/
4754
		*/
4744
		*has_reduced_clock = limit->find_pll(limit, crtc,
4755
		*has_reduced_clock = limit->find_pll(limit, crtc,
4745
						     dev_priv->lvds_downclock,
4756
						     dev_priv->lvds_downclock,
4746
						     refclk,
4757
						     refclk,
4747
						     clock,
4758
						     clock,
4748
						     reduced_clock);
4759
						     reduced_clock);
4749
	}
4760
	}
4750
 
4761
 
4751
	if (is_sdvo && is_tv)
4762
	if (is_sdvo && is_tv)
4752
		i9xx_adjust_sdvo_tv_clock(adjusted_mode, clock);
4763
		i9xx_adjust_sdvo_tv_clock(adjusted_mode, clock);
4753
 
4764
 
4754
	return true;
4765
	return true;
4755
}
4766
}
4756
 
4767
 
4757
static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
4768
static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
4758
                  struct drm_display_mode *mode,
4769
                  struct drm_display_mode *mode,
4759
                  struct drm_display_mode *adjusted_mode,
4770
                  struct drm_display_mode *adjusted_mode,
4760
                  int x, int y,
4771
                  int x, int y,
4761
				  struct drm_framebuffer *fb)
4772
				  struct drm_framebuffer *fb)
4762
{
4773
{
4763
    struct drm_device *dev = crtc->dev;
4774
    struct drm_device *dev = crtc->dev;
4764
    struct drm_i915_private *dev_priv = dev->dev_private;
4775
    struct drm_i915_private *dev_priv = dev->dev_private;
4765
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4776
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4766
    int pipe = intel_crtc->pipe;
4777
    int pipe = intel_crtc->pipe;
4767
    int plane = intel_crtc->plane;
4778
    int plane = intel_crtc->plane;
4768
	int num_connectors = 0;
4779
	int num_connectors = 0;
4769
    intel_clock_t clock, reduced_clock;
4780
    intel_clock_t clock, reduced_clock;
4770
	u32 dpll, fp = 0, fp2 = 0;
4781
	u32 dpll, fp = 0, fp2 = 0;
4771
    bool ok, has_reduced_clock = false, is_sdvo = false;
4782
    bool ok, has_reduced_clock = false, is_sdvo = false;
4772
    bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
4783
    bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
4773
	struct intel_encoder *encoder, *edp_encoder = NULL;
4784
	struct intel_encoder *encoder, *edp_encoder = NULL;
4774
    int ret;
4785
    int ret;
4775
    struct fdi_m_n m_n = {0};
4786
    struct fdi_m_n m_n = {0};
4776
    u32 temp;
4787
    u32 temp;
4777
    int target_clock, pixel_multiplier, lane, link_bw, factor;
4788
    int target_clock, pixel_multiplier, lane, link_bw, factor;
4778
    unsigned int pipe_bpp;
4789
    unsigned int pipe_bpp;
4779
    bool dither;
4790
    bool dither;
4780
	bool is_cpu_edp = false, is_pch_edp = false;
4791
	bool is_cpu_edp = false, is_pch_edp = false;
4781
 
4792
 
4782
	for_each_encoder_on_crtc(dev, crtc, encoder) {
4793
	for_each_encoder_on_crtc(dev, crtc, encoder) {
4783
        switch (encoder->type) {
4794
        switch (encoder->type) {
4784
        case INTEL_OUTPUT_LVDS:
4795
        case INTEL_OUTPUT_LVDS:
4785
            is_lvds = true;
4796
            is_lvds = true;
4786
            break;
4797
            break;
4787
        case INTEL_OUTPUT_SDVO:
4798
        case INTEL_OUTPUT_SDVO:
4788
        case INTEL_OUTPUT_HDMI:
4799
        case INTEL_OUTPUT_HDMI:
4789
            is_sdvo = true;
4800
            is_sdvo = true;
4790
            if (encoder->needs_tv_clock)
4801
            if (encoder->needs_tv_clock)
4791
                is_tv = true;
4802
                is_tv = true;
4792
            break;
4803
            break;
4793
        case INTEL_OUTPUT_TVOUT:
4804
        case INTEL_OUTPUT_TVOUT:
4794
            is_tv = true;
4805
            is_tv = true;
4795
            break;
4806
            break;
4796
        case INTEL_OUTPUT_ANALOG:
4807
        case INTEL_OUTPUT_ANALOG:
4797
            is_crt = true;
4808
            is_crt = true;
4798
            break;
4809
            break;
4799
        case INTEL_OUTPUT_DISPLAYPORT:
4810
        case INTEL_OUTPUT_DISPLAYPORT:
4800
            is_dp = true;
4811
            is_dp = true;
4801
            break;
4812
            break;
4802
        case INTEL_OUTPUT_EDP:
4813
        case INTEL_OUTPUT_EDP:
4803
			is_dp = true;
4814
			is_dp = true;
4804
			if (intel_encoder_is_pch_edp(&encoder->base))
4815
			if (intel_encoder_is_pch_edp(&encoder->base))
4805
				is_pch_edp = true;
4816
				is_pch_edp = true;
4806
			else
4817
			else
4807
				is_cpu_edp = true;
4818
				is_cpu_edp = true;
4808
			edp_encoder = encoder;
4819
			edp_encoder = encoder;
4809
            break;
4820
            break;
4810
        }
4821
        }
4811
 
4822
 
4812
        num_connectors++;
4823
        num_connectors++;
4813
    }
4824
    }
4814
 
4825
 
4815
	ok = ironlake_compute_clocks(crtc, adjusted_mode, &clock,
4826
	ok = ironlake_compute_clocks(crtc, adjusted_mode, &clock,
4816
				     &has_reduced_clock, &reduced_clock);
4827
				     &has_reduced_clock, &reduced_clock);
4817
    if (!ok) {
4828
    if (!ok) {
4818
        DRM_ERROR("Couldn't find PLL settings for mode!\n");
4829
        DRM_ERROR("Couldn't find PLL settings for mode!\n");
4819
        return -EINVAL;
4830
        return -EINVAL;
4820
    }
4831
    }
4821
 
4832
 
4822
    /* Ensure that the cursor is valid for the new mode before changing... */
4833
    /* Ensure that the cursor is valid for the new mode before changing... */
4823
//    intel_crtc_update_cursor(crtc, true);
4834
//    intel_crtc_update_cursor(crtc, true);
4824
 
4835
 
4825
    /* FDI link */
4836
    /* FDI link */
4826
    pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
4837
    pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
4827
    lane = 0;
4838
    lane = 0;
4828
    /* CPU eDP doesn't require FDI link, so just set DP M/N
4839
    /* CPU eDP doesn't require FDI link, so just set DP M/N
4829
       according to current link config */
4840
       according to current link config */
4830
	if (is_cpu_edp) {
4841
	if (is_cpu_edp) {
4831
		intel_edp_link_config(edp_encoder, &lane, &link_bw);
4842
		intel_edp_link_config(edp_encoder, &lane, &link_bw);
4832
    } else {
4843
    } else {
4833
        /* FDI is a binary signal running at ~2.7GHz, encoding
4844
        /* FDI is a binary signal running at ~2.7GHz, encoding
4834
         * each output octet as 10 bits. The actual frequency
4845
         * each output octet as 10 bits. The actual frequency
4835
         * is stored as a divider into a 100MHz clock, and the
4846
         * is stored as a divider into a 100MHz clock, and the
4836
         * mode pixel clock is stored in units of 1KHz.
4847
         * mode pixel clock is stored in units of 1KHz.
4837
         * Hence the bw of each lane in terms of the mode signal
4848
         * Hence the bw of each lane in terms of the mode signal
4838
         * is:
4849
         * is:
4839
         */
4850
         */
4840
        link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
4851
        link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
4841
    }
4852
    }
4842
 
4853
 
4843
	/* [e]DP over FDI requires target mode clock instead of link clock. */
4854
	/* [e]DP over FDI requires target mode clock instead of link clock. */
4844
	if (edp_encoder)
4855
	if (edp_encoder)
4845
		target_clock = intel_edp_target_clock(edp_encoder, mode);
4856
		target_clock = intel_edp_target_clock(edp_encoder, mode);
4846
	else if (is_dp)
4857
	else if (is_dp)
4847
		target_clock = mode->clock;
4858
		target_clock = mode->clock;
4848
	else
4859
	else
4849
		target_clock = adjusted_mode->clock;
4860
		target_clock = adjusted_mode->clock;
4850
 
4861
 
4851
    /* determine panel color depth */
4862
    /* determine panel color depth */
4852
	dither = intel_choose_pipe_bpp_dither(crtc, fb, &pipe_bpp,
4863
	dither = intel_choose_pipe_bpp_dither(crtc, fb, &pipe_bpp,
4853
					      adjusted_mode);
4864
					      adjusted_mode);
4854
	if (is_lvds && dev_priv->lvds_dither)
4865
	if (is_lvds && dev_priv->lvds_dither)
4855
		dither = true;
4866
		dither = true;
4856
 
4867
 
4857
	if (pipe_bpp != 18 && pipe_bpp != 24 && pipe_bpp != 30 &&
4868
	if (pipe_bpp != 18 && pipe_bpp != 24 && pipe_bpp != 30 &&
4858
	    pipe_bpp != 36) {
4869
	    pipe_bpp != 36) {
4859
        WARN(1, "intel_choose_pipe_bpp returned invalid value %d\n",
4870
        WARN(1, "intel_choose_pipe_bpp returned invalid value %d\n",
4860
            pipe_bpp);
4871
            pipe_bpp);
4861
        pipe_bpp = 24;
4872
        pipe_bpp = 24;
4862
    }
4873
    }
4863
    intel_crtc->bpp = pipe_bpp;
4874
    intel_crtc->bpp = pipe_bpp;
4864
 
4875
 
4865
    if (!lane) {
4876
    if (!lane) {
4866
        /*
4877
        /*
4867
         * Account for spread spectrum to avoid
4878
         * Account for spread spectrum to avoid
4868
         * oversubscribing the link. Max center spread
4879
         * oversubscribing the link. Max center spread
4869
         * is 2.5%; use 5% for safety's sake.
4880
         * is 2.5%; use 5% for safety's sake.
4870
         */
4881
         */
4871
        u32 bps = target_clock * intel_crtc->bpp * 21 / 20;
4882
        u32 bps = target_clock * intel_crtc->bpp * 21 / 20;
4872
        lane = bps / (link_bw * 8) + 1;
4883
        lane = bps / (link_bw * 8) + 1;
4873
    }
4884
    }
4874
 
4885
 
4875
    intel_crtc->fdi_lanes = lane;
4886
    intel_crtc->fdi_lanes = lane;
4876
 
4887
 
4877
    if (pixel_multiplier > 1)
4888
    if (pixel_multiplier > 1)
4878
        link_bw *= pixel_multiplier;
4889
        link_bw *= pixel_multiplier;
4879
    ironlake_compute_m_n(intel_crtc->bpp, lane, target_clock, link_bw,
4890
    ironlake_compute_m_n(intel_crtc->bpp, lane, target_clock, link_bw,
4880
                 &m_n);
4891
                 &m_n);
4881
 
4892
 
4882
    fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
4893
    fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
4883
    if (has_reduced_clock)
4894
    if (has_reduced_clock)
4884
        fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
4895
        fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
4885
            reduced_clock.m2;
4896
            reduced_clock.m2;
4886
 
4897
 
4887
    /* Enable autotuning of the PLL clock (if permissible) */
4898
    /* Enable autotuning of the PLL clock (if permissible) */
4888
    factor = 21;
4899
    factor = 21;
4889
    if (is_lvds) {
4900
    if (is_lvds) {
4890
        if ((intel_panel_use_ssc(dev_priv) &&
4901
        if ((intel_panel_use_ssc(dev_priv) &&
4891
             dev_priv->lvds_ssc_freq == 100) ||
4902
             dev_priv->lvds_ssc_freq == 100) ||
4892
            (I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP)
4903
            (I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP)
4893
            factor = 25;
4904
            factor = 25;
4894
    } else if (is_sdvo && is_tv)
4905
    } else if (is_sdvo && is_tv)
4895
        factor = 20;
4906
        factor = 20;
4896
 
4907
 
4897
    if (clock.m < factor * clock.n)
4908
    if (clock.m < factor * clock.n)
4898
        fp |= FP_CB_TUNE;
4909
        fp |= FP_CB_TUNE;
4899
 
4910
 
4900
    dpll = 0;
4911
    dpll = 0;
4901
 
4912
 
4902
    if (is_lvds)
4913
    if (is_lvds)
4903
        dpll |= DPLLB_MODE_LVDS;
4914
        dpll |= DPLLB_MODE_LVDS;
4904
    else
4915
    else
4905
        dpll |= DPLLB_MODE_DAC_SERIAL;
4916
        dpll |= DPLLB_MODE_DAC_SERIAL;
4906
    if (is_sdvo) {
4917
    if (is_sdvo) {
4907
        int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
4918
        int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
4908
        if (pixel_multiplier > 1) {
4919
        if (pixel_multiplier > 1) {
4909
            dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
4920
            dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
4910
        }
4921
        }
4911
        dpll |= DPLL_DVO_HIGH_SPEED;
4922
        dpll |= DPLL_DVO_HIGH_SPEED;
4912
    }
4923
    }
4913
	if (is_dp && !is_cpu_edp)
4924
	if (is_dp && !is_cpu_edp)
4914
        dpll |= DPLL_DVO_HIGH_SPEED;
4925
        dpll |= DPLL_DVO_HIGH_SPEED;
4915
 
4926
 
4916
    /* compute bitmask from p1 value */
4927
    /* compute bitmask from p1 value */
4917
    dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4928
    dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4918
    /* also FPA1 */
4929
    /* also FPA1 */
4919
    dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
4930
    dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
4920
 
4931
 
4921
    switch (clock.p2) {
4932
    switch (clock.p2) {
4922
    case 5:
4933
    case 5:
4923
        dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4934
        dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4924
        break;
4935
        break;
4925
    case 7:
4936
    case 7:
4926
        dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4937
        dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4927
        break;
4938
        break;
4928
    case 10:
4939
    case 10:
4929
        dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4940
        dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4930
        break;
4941
        break;
4931
    case 14:
4942
    case 14:
4932
        dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4943
        dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4933
        break;
4944
        break;
4934
    }
4945
    }
4935
 
4946
 
4936
    if (is_sdvo && is_tv)
4947
    if (is_sdvo && is_tv)
4937
        dpll |= PLL_REF_INPUT_TVCLKINBC;
4948
        dpll |= PLL_REF_INPUT_TVCLKINBC;
4938
    else if (is_tv)
4949
    else if (is_tv)
4939
        /* XXX: just matching BIOS for now */
4950
        /* XXX: just matching BIOS for now */
4940
        /*  dpll |= PLL_REF_INPUT_TVCLKINBC; */
4951
        /*  dpll |= PLL_REF_INPUT_TVCLKINBC; */
4941
        dpll |= 3;
4952
        dpll |= 3;
4942
    else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4953
    else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4943
        dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4954
        dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4944
    else
4955
    else
4945
        dpll |= PLL_REF_INPUT_DREFCLK;
4956
        dpll |= PLL_REF_INPUT_DREFCLK;
4946
 
4957
 
4947
	DRM_DEBUG_KMS("Mode for pipe %d:\n", pipe);
4958
	DRM_DEBUG_KMS("Mode for pipe %d:\n", pipe);
4948
    drm_mode_debug_printmodeline(mode);
4959
    drm_mode_debug_printmodeline(mode);
4949
 
4960
 
4950
	/* CPU eDP is the only output that doesn't need a PCH PLL of its own on
4961
	/* CPU eDP is the only output that doesn't need a PCH PLL of its own on
4951
	 * pre-Haswell/LPT generation */
4962
	 * pre-Haswell/LPT generation */
4952
	if (HAS_PCH_LPT(dev)) {
4963
	if (HAS_PCH_LPT(dev)) {
4953
		DRM_DEBUG_KMS("LPT detected: no PLL for pipe %d necessary\n",
4964
		DRM_DEBUG_KMS("LPT detected: no PLL for pipe %d necessary\n",
4954
				pipe);
4965
				pipe);
4955
	} else if (!is_cpu_edp) {
4966
	} else if (!is_cpu_edp) {
4956
		struct intel_pch_pll *pll;
4967
		struct intel_pch_pll *pll;
4957
 
4968
 
4958
		pll = intel_get_pch_pll(intel_crtc, dpll, fp);
4969
		pll = intel_get_pch_pll(intel_crtc, dpll, fp);
4959
		if (pll == NULL) {
4970
		if (pll == NULL) {
4960
			DRM_DEBUG_DRIVER("failed to find PLL for pipe %d\n",
4971
			DRM_DEBUG_DRIVER("failed to find PLL for pipe %d\n",
4961
					 pipe);
4972
					 pipe);
4962
			return -EINVAL;
4973
			return -EINVAL;
4963
        }
4974
        }
4964
	} else
4975
	} else
4965
		intel_put_pch_pll(intel_crtc);
4976
		intel_put_pch_pll(intel_crtc);
4966
 
4977
 
4967
    /* The LVDS pin pair needs to be on before the DPLLs are enabled.
4978
    /* The LVDS pin pair needs to be on before the DPLLs are enabled.
4968
     * This is an exception to the general rule that mode_set doesn't turn
4979
     * This is an exception to the general rule that mode_set doesn't turn
4969
     * things on.
4980
     * things on.
4970
     */
4981
     */
4971
    if (is_lvds) {
4982
    if (is_lvds) {
4972
        temp = I915_READ(PCH_LVDS);
4983
        temp = I915_READ(PCH_LVDS);
4973
        temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
4984
        temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
4974
		if (HAS_PCH_CPT(dev)) {
4985
		if (HAS_PCH_CPT(dev)) {
4975
			temp &= ~PORT_TRANS_SEL_MASK;
4986
			temp &= ~PORT_TRANS_SEL_MASK;
4976
			temp |= PORT_TRANS_SEL_CPT(pipe);
4987
			temp |= PORT_TRANS_SEL_CPT(pipe);
4977
		} else {
4988
		} else {
4978
			if (pipe == 1)
4989
			if (pipe == 1)
4979
                temp |= LVDS_PIPEB_SELECT;
4990
                temp |= LVDS_PIPEB_SELECT;
4980
            else
4991
            else
4981
                temp &= ~LVDS_PIPEB_SELECT;
4992
                temp &= ~LVDS_PIPEB_SELECT;
4982
        }
4993
        }
4983
 
4994
 
4984
        /* set the corresponsding LVDS_BORDER bit */
4995
        /* set the corresponsding LVDS_BORDER bit */
4985
        temp |= dev_priv->lvds_border_bits;
4996
        temp |= dev_priv->lvds_border_bits;
4986
        /* Set the B0-B3 data pairs corresponding to whether we're going to
4997
        /* Set the B0-B3 data pairs corresponding to whether we're going to
4987
         * set the DPLLs for dual-channel mode or not.
4998
         * set the DPLLs for dual-channel mode or not.
4988
         */
4999
         */
4989
        if (clock.p2 == 7)
5000
        if (clock.p2 == 7)
4990
            temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
5001
            temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
4991
        else
5002
        else
4992
            temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
5003
            temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
4993
 
5004
 
4994
        /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
5005
        /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
4995
         * appropriately here, but we need to look more thoroughly into how
5006
         * appropriately here, but we need to look more thoroughly into how
4996
         * panels behave in the two modes.
5007
         * panels behave in the two modes.
4997
         */
5008
         */
4998
		temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
5009
		temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
4999
        if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
5010
        if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
5000
			temp |= LVDS_HSYNC_POLARITY;
5011
			temp |= LVDS_HSYNC_POLARITY;
5001
        if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
5012
        if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
5002
			temp |= LVDS_VSYNC_POLARITY;
5013
			temp |= LVDS_VSYNC_POLARITY;
5003
        I915_WRITE(PCH_LVDS, temp);
5014
        I915_WRITE(PCH_LVDS, temp);
5004
    }
5015
    }
5005
 
5016
 
5006
	if (is_dp && !is_cpu_edp) {
5017
	if (is_dp && !is_cpu_edp) {
5007
        intel_dp_set_m_n(crtc, mode, adjusted_mode);
5018
        intel_dp_set_m_n(crtc, mode, adjusted_mode);
5008
    } else {
5019
    } else {
5009
        /* For non-DP output, clear any trans DP clock recovery setting.*/
5020
        /* For non-DP output, clear any trans DP clock recovery setting.*/
5010
        I915_WRITE(TRANSDATA_M1(pipe), 0);
5021
        I915_WRITE(TRANSDATA_M1(pipe), 0);
5011
        I915_WRITE(TRANSDATA_N1(pipe), 0);
5022
        I915_WRITE(TRANSDATA_N1(pipe), 0);
5012
        I915_WRITE(TRANSDPLINK_M1(pipe), 0);
5023
        I915_WRITE(TRANSDPLINK_M1(pipe), 0);
5013
        I915_WRITE(TRANSDPLINK_N1(pipe), 0);
5024
        I915_WRITE(TRANSDPLINK_N1(pipe), 0);
5014
    }
5025
    }
5015
 
5026
 
5016
	if (intel_crtc->pch_pll) {
5027
	if (intel_crtc->pch_pll) {
5017
		I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
5028
		I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
5018
 
5029
 
5019
        /* Wait for the clocks to stabilize. */
5030
        /* Wait for the clocks to stabilize. */
5020
		POSTING_READ(intel_crtc->pch_pll->pll_reg);
5031
		POSTING_READ(intel_crtc->pch_pll->pll_reg);
5021
        udelay(150);
5032
        udelay(150);
5022
 
5033
 
5023
        /* The pixel multiplier can only be updated once the
5034
        /* The pixel multiplier can only be updated once the
5024
         * DPLL is enabled and the clocks are stable.
5035
         * DPLL is enabled and the clocks are stable.
5025
         *
5036
         *
5026
         * So write it again.
5037
         * So write it again.
5027
         */
5038
         */
5028
		I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
5039
		I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
5029
    }
5040
    }
5030
 
5041
 
5031
    intel_crtc->lowfreq_avail = false;
5042
    intel_crtc->lowfreq_avail = false;
5032
	if (intel_crtc->pch_pll) {
5043
	if (intel_crtc->pch_pll) {
5033
    if (is_lvds && has_reduced_clock && i915_powersave) {
5044
    if (is_lvds && has_reduced_clock && i915_powersave) {
5034
			I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp2);
5045
			I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp2);
5035
        intel_crtc->lowfreq_avail = true;
5046
        intel_crtc->lowfreq_avail = true;
5036
    } else {
5047
    } else {
5037
			I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp);
5048
			I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp);
5038
    }
5049
    }
5039
	}
5050
	}
5040
 
5051
 
5041
    if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
5052
    if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
5042
        /* the chip adds 2 halflines automatically */
5053
        /* the chip adds 2 halflines automatically */
5043
        adjusted_mode->crtc_vtotal -= 1;
5054
        adjusted_mode->crtc_vtotal -= 1;
5044
        adjusted_mode->crtc_vblank_end -= 1;
5055
        adjusted_mode->crtc_vblank_end -= 1;
5045
		I915_WRITE(VSYNCSHIFT(pipe),
5056
		I915_WRITE(VSYNCSHIFT(pipe),
5046
			   adjusted_mode->crtc_hsync_start
5057
			   adjusted_mode->crtc_hsync_start
5047
			   - adjusted_mode->crtc_htotal/2);
5058
			   - adjusted_mode->crtc_htotal/2);
5048
	} else {
5059
	} else {
5049
		I915_WRITE(VSYNCSHIFT(pipe), 0);
5060
		I915_WRITE(VSYNCSHIFT(pipe), 0);
5050
	}
5061
	}
5051
 
5062
 
5052
    I915_WRITE(HTOTAL(pipe),
5063
    I915_WRITE(HTOTAL(pipe),
5053
           (adjusted_mode->crtc_hdisplay - 1) |
5064
           (adjusted_mode->crtc_hdisplay - 1) |
5054
           ((adjusted_mode->crtc_htotal - 1) << 16));
5065
           ((adjusted_mode->crtc_htotal - 1) << 16));
5055
    I915_WRITE(HBLANK(pipe),
5066
    I915_WRITE(HBLANK(pipe),
5056
           (adjusted_mode->crtc_hblank_start - 1) |
5067
           (adjusted_mode->crtc_hblank_start - 1) |
5057
           ((adjusted_mode->crtc_hblank_end - 1) << 16));
5068
           ((adjusted_mode->crtc_hblank_end - 1) << 16));
5058
    I915_WRITE(HSYNC(pipe),
5069
    I915_WRITE(HSYNC(pipe),
5059
           (adjusted_mode->crtc_hsync_start - 1) |
5070
           (adjusted_mode->crtc_hsync_start - 1) |
5060
           ((adjusted_mode->crtc_hsync_end - 1) << 16));
5071
           ((adjusted_mode->crtc_hsync_end - 1) << 16));
5061
 
5072
 
5062
    I915_WRITE(VTOTAL(pipe),
5073
    I915_WRITE(VTOTAL(pipe),
5063
           (adjusted_mode->crtc_vdisplay - 1) |
5074
           (adjusted_mode->crtc_vdisplay - 1) |
5064
           ((adjusted_mode->crtc_vtotal - 1) << 16));
5075
           ((adjusted_mode->crtc_vtotal - 1) << 16));
5065
    I915_WRITE(VBLANK(pipe),
5076
    I915_WRITE(VBLANK(pipe),
5066
           (adjusted_mode->crtc_vblank_start - 1) |
5077
           (adjusted_mode->crtc_vblank_start - 1) |
5067
           ((adjusted_mode->crtc_vblank_end - 1) << 16));
5078
           ((adjusted_mode->crtc_vblank_end - 1) << 16));
5068
    I915_WRITE(VSYNC(pipe),
5079
    I915_WRITE(VSYNC(pipe),
5069
           (adjusted_mode->crtc_vsync_start - 1) |
5080
           (adjusted_mode->crtc_vsync_start - 1) |
5070
           ((adjusted_mode->crtc_vsync_end - 1) << 16));
5081
           ((adjusted_mode->crtc_vsync_end - 1) << 16));
5071
 
5082
 
5072
    /* pipesrc controls the size that is scaled from, which should
5083
    /* pipesrc controls the size that is scaled from, which should
5073
     * always be the user's requested size.
5084
     * always be the user's requested size.
5074
     */
5085
     */
5075
    I915_WRITE(PIPESRC(pipe),
5086
    I915_WRITE(PIPESRC(pipe),
5076
           ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
5087
           ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
5077
 
5088
 
5078
    I915_WRITE(PIPE_DATA_M1(pipe), TU_SIZE(m_n.tu) | m_n.gmch_m);
5089
    I915_WRITE(PIPE_DATA_M1(pipe), TU_SIZE(m_n.tu) | m_n.gmch_m);
5079
    I915_WRITE(PIPE_DATA_N1(pipe), m_n.gmch_n);
5090
    I915_WRITE(PIPE_DATA_N1(pipe), m_n.gmch_n);
5080
    I915_WRITE(PIPE_LINK_M1(pipe), m_n.link_m);
5091
    I915_WRITE(PIPE_LINK_M1(pipe), m_n.link_m);
5081
    I915_WRITE(PIPE_LINK_N1(pipe), m_n.link_n);
5092
    I915_WRITE(PIPE_LINK_N1(pipe), m_n.link_n);
5082
 
5093
 
5083
	if (is_cpu_edp)
5094
	if (is_cpu_edp)
5084
        ironlake_set_pll_edp(crtc, adjusted_mode->clock);
5095
        ironlake_set_pll_edp(crtc, adjusted_mode->clock);
5085
 
5096
 
5086
	ironlake_set_pipeconf(crtc, adjusted_mode, dither);
5097
	ironlake_set_pipeconf(crtc, adjusted_mode, dither);
5087
 
5098
 
5088
    intel_wait_for_vblank(dev, pipe);
5099
    intel_wait_for_vblank(dev, pipe);
5089
 
5100
 
5090
	/* Set up the display plane register */
5101
	/* Set up the display plane register */
5091
	I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
5102
	I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
5092
    POSTING_READ(DSPCNTR(plane));
5103
    POSTING_READ(DSPCNTR(plane));
5093
 
5104
 
5094
	ret = intel_pipe_set_base(crtc, x, y, fb);
5105
	ret = intel_pipe_set_base(crtc, x, y, fb);
5095
 
5106
 
5096
    intel_update_watermarks(dev);
5107
    intel_update_watermarks(dev);
5097
 
5108
 
5098
	intel_update_linetime_watermarks(dev, pipe, adjusted_mode);
5109
	intel_update_linetime_watermarks(dev, pipe, adjusted_mode);
5099
 
5110
 
5100
    return ret;
5111
    return ret;
5101
}
5112
}
5102
 
5113
 
5103
static int intel_crtc_mode_set(struct drm_crtc *crtc,
5114
static int intel_crtc_mode_set(struct drm_crtc *crtc,
5104
			       struct drm_display_mode *mode,
5115
			       struct drm_display_mode *mode,
5105
			       struct drm_display_mode *adjusted_mode,
5116
			       struct drm_display_mode *adjusted_mode,
5106
			       int x, int y,
5117
			       int x, int y,
5107
			       struct drm_framebuffer *fb)
5118
			       struct drm_framebuffer *fb)
5108
{
5119
{
5109
	struct drm_device *dev = crtc->dev;
5120
	struct drm_device *dev = crtc->dev;
5110
	struct drm_i915_private *dev_priv = dev->dev_private;
5121
	struct drm_i915_private *dev_priv = dev->dev_private;
5111
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5122
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5112
	int pipe = intel_crtc->pipe;
5123
	int pipe = intel_crtc->pipe;
5113
	int ret;
5124
	int ret;
5114
 
5125
 
5115
	drm_vblank_pre_modeset(dev, pipe);
5126
	drm_vblank_pre_modeset(dev, pipe);
5116
 
5127
 
5117
	ret = dev_priv->display.crtc_mode_set(crtc, mode, adjusted_mode,
5128
	ret = dev_priv->display.crtc_mode_set(crtc, mode, adjusted_mode,
5118
					      x, y, fb);
5129
					      x, y, fb);
5119
	drm_vblank_post_modeset(dev, pipe);
5130
	drm_vblank_post_modeset(dev, pipe);
5120
 
5131
 
5121
	return ret;
5132
	return ret;
5122
}
5133
}
5123
 
5134
 
5124
static bool intel_eld_uptodate(struct drm_connector *connector,
5135
static bool intel_eld_uptodate(struct drm_connector *connector,
5125
			       int reg_eldv, uint32_t bits_eldv,
5136
			       int reg_eldv, uint32_t bits_eldv,
5126
			       int reg_elda, uint32_t bits_elda,
5137
			       int reg_elda, uint32_t bits_elda,
5127
			       int reg_edid)
5138
			       int reg_edid)
5128
{
5139
{
5129
	struct drm_i915_private *dev_priv = connector->dev->dev_private;
5140
	struct drm_i915_private *dev_priv = connector->dev->dev_private;
5130
	uint8_t *eld = connector->eld;
5141
	uint8_t *eld = connector->eld;
5131
	uint32_t i;
5142
	uint32_t i;
5132
 
5143
 
5133
	i = I915_READ(reg_eldv);
5144
	i = I915_READ(reg_eldv);
5134
	i &= bits_eldv;
5145
	i &= bits_eldv;
5135
 
5146
 
5136
	if (!eld[0])
5147
	if (!eld[0])
5137
		return !i;
5148
		return !i;
5138
 
5149
 
5139
	if (!i)
5150
	if (!i)
5140
		return false;
5151
		return false;
5141
 
5152
 
5142
	i = I915_READ(reg_elda);
5153
	i = I915_READ(reg_elda);
5143
	i &= ~bits_elda;
5154
	i &= ~bits_elda;
5144
	I915_WRITE(reg_elda, i);
5155
	I915_WRITE(reg_elda, i);
5145
 
5156
 
5146
	for (i = 0; i < eld[2]; i++)
5157
	for (i = 0; i < eld[2]; i++)
5147
		if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
5158
		if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
5148
			return false;
5159
			return false;
5149
 
5160
 
5150
	return true;
5161
	return true;
5151
}
5162
}
5152
 
5163
 
5153
static void g4x_write_eld(struct drm_connector *connector,
5164
static void g4x_write_eld(struct drm_connector *connector,
5154
			  struct drm_crtc *crtc)
5165
			  struct drm_crtc *crtc)
5155
{
5166
{
5156
	struct drm_i915_private *dev_priv = connector->dev->dev_private;
5167
	struct drm_i915_private *dev_priv = connector->dev->dev_private;
5157
	uint8_t *eld = connector->eld;
5168
	uint8_t *eld = connector->eld;
5158
	uint32_t eldv;
5169
	uint32_t eldv;
5159
	uint32_t len;
5170
	uint32_t len;
5160
	uint32_t i;
5171
	uint32_t i;
5161
 
5172
 
5162
	i = I915_READ(G4X_AUD_VID_DID);
5173
	i = I915_READ(G4X_AUD_VID_DID);
5163
 
5174
 
5164
	if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
5175
	if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
5165
		eldv = G4X_ELDV_DEVCL_DEVBLC;
5176
		eldv = G4X_ELDV_DEVCL_DEVBLC;
5166
	else
5177
	else
5167
		eldv = G4X_ELDV_DEVCTG;
5178
		eldv = G4X_ELDV_DEVCTG;
5168
 
5179
 
5169
	if (intel_eld_uptodate(connector,
5180
	if (intel_eld_uptodate(connector,
5170
			       G4X_AUD_CNTL_ST, eldv,
5181
			       G4X_AUD_CNTL_ST, eldv,
5171
			       G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
5182
			       G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
5172
			       G4X_HDMIW_HDMIEDID))
5183
			       G4X_HDMIW_HDMIEDID))
5173
		return;
5184
		return;
5174
 
5185
 
5175
	i = I915_READ(G4X_AUD_CNTL_ST);
5186
	i = I915_READ(G4X_AUD_CNTL_ST);
5176
	i &= ~(eldv | G4X_ELD_ADDR);
5187
	i &= ~(eldv | G4X_ELD_ADDR);
5177
	len = (i >> 9) & 0x1f;		/* ELD buffer size */
5188
	len = (i >> 9) & 0x1f;		/* ELD buffer size */
5178
	I915_WRITE(G4X_AUD_CNTL_ST, i);
5189
	I915_WRITE(G4X_AUD_CNTL_ST, i);
5179
 
5190
 
5180
	if (!eld[0])
5191
	if (!eld[0])
5181
		return;
5192
		return;
5182
 
5193
 
5183
	len = min_t(uint8_t, eld[2], len);
5194
	len = min_t(uint8_t, eld[2], len);
5184
	DRM_DEBUG_DRIVER("ELD size %d\n", len);
5195
	DRM_DEBUG_DRIVER("ELD size %d\n", len);
5185
	for (i = 0; i < len; i++)
5196
	for (i = 0; i < len; i++)
5186
		I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
5197
		I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
5187
 
5198
 
5188
	i = I915_READ(G4X_AUD_CNTL_ST);
5199
	i = I915_READ(G4X_AUD_CNTL_ST);
5189
	i |= eldv;
5200
	i |= eldv;
5190
	I915_WRITE(G4X_AUD_CNTL_ST, i);
5201
	I915_WRITE(G4X_AUD_CNTL_ST, i);
5191
}
5202
}
5192
 
5203
 
5193
static void haswell_write_eld(struct drm_connector *connector,
5204
static void haswell_write_eld(struct drm_connector *connector,
5194
				     struct drm_crtc *crtc)
5205
				     struct drm_crtc *crtc)
5195
{
5206
{
5196
	struct drm_i915_private *dev_priv = connector->dev->dev_private;
5207
	struct drm_i915_private *dev_priv = connector->dev->dev_private;
5197
	uint8_t *eld = connector->eld;
5208
	uint8_t *eld = connector->eld;
5198
	struct drm_device *dev = crtc->dev;
5209
	struct drm_device *dev = crtc->dev;
5199
	uint32_t eldv;
5210
	uint32_t eldv;
5200
	uint32_t i;
5211
	uint32_t i;
5201
	int len;
5212
	int len;
5202
	int pipe = to_intel_crtc(crtc)->pipe;
5213
	int pipe = to_intel_crtc(crtc)->pipe;
5203
	int tmp;
5214
	int tmp;
5204
 
5215
 
5205
	int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
5216
	int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
5206
	int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
5217
	int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
5207
	int aud_config = HSW_AUD_CFG(pipe);
5218
	int aud_config = HSW_AUD_CFG(pipe);
5208
	int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
5219
	int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
5209
 
5220
 
5210
 
5221
 
5211
	DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
5222
	DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
5212
 
5223
 
5213
	/* Audio output enable */
5224
	/* Audio output enable */
5214
	DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
5225
	DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
5215
	tmp = I915_READ(aud_cntrl_st2);
5226
	tmp = I915_READ(aud_cntrl_st2);
5216
	tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
5227
	tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
5217
	I915_WRITE(aud_cntrl_st2, tmp);
5228
	I915_WRITE(aud_cntrl_st2, tmp);
5218
 
5229
 
5219
	/* Wait for 1 vertical blank */
5230
	/* Wait for 1 vertical blank */
5220
	intel_wait_for_vblank(dev, pipe);
5231
	intel_wait_for_vblank(dev, pipe);
5221
 
5232
 
5222
	/* Set ELD valid state */
5233
	/* Set ELD valid state */
5223
	tmp = I915_READ(aud_cntrl_st2);
5234
	tmp = I915_READ(aud_cntrl_st2);
5224
	DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%8x\n", tmp);
5235
	DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%8x\n", tmp);
5225
	tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
5236
	tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
5226
	I915_WRITE(aud_cntrl_st2, tmp);
5237
	I915_WRITE(aud_cntrl_st2, tmp);
5227
	tmp = I915_READ(aud_cntrl_st2);
5238
	tmp = I915_READ(aud_cntrl_st2);
5228
	DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%8x\n", tmp);
5239
	DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%8x\n", tmp);
5229
 
5240
 
5230
	/* Enable HDMI mode */
5241
	/* Enable HDMI mode */
5231
	tmp = I915_READ(aud_config);
5242
	tmp = I915_READ(aud_config);
5232
	DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%8x\n", tmp);
5243
	DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%8x\n", tmp);
5233
	/* clear N_programing_enable and N_value_index */
5244
	/* clear N_programing_enable and N_value_index */
5234
	tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
5245
	tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
5235
	I915_WRITE(aud_config, tmp);
5246
	I915_WRITE(aud_config, tmp);
5236
 
5247
 
5237
	DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
5248
	DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
5238
 
5249
 
5239
	eldv = AUDIO_ELD_VALID_A << (pipe * 4);
5250
	eldv = AUDIO_ELD_VALID_A << (pipe * 4);
5240
 
5251
 
5241
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
5252
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
5242
		DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
5253
		DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
5243
		eld[5] |= (1 << 2);	/* Conn_Type, 0x1 = DisplayPort */
5254
		eld[5] |= (1 << 2);	/* Conn_Type, 0x1 = DisplayPort */
5244
		I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
5255
		I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
5245
	} else
5256
	} else
5246
		I915_WRITE(aud_config, 0);
5257
		I915_WRITE(aud_config, 0);
5247
 
5258
 
5248
	if (intel_eld_uptodate(connector,
5259
	if (intel_eld_uptodate(connector,
5249
			       aud_cntrl_st2, eldv,
5260
			       aud_cntrl_st2, eldv,
5250
			       aud_cntl_st, IBX_ELD_ADDRESS,
5261
			       aud_cntl_st, IBX_ELD_ADDRESS,
5251
			       hdmiw_hdmiedid))
5262
			       hdmiw_hdmiedid))
5252
		return;
5263
		return;
5253
 
5264
 
5254
	i = I915_READ(aud_cntrl_st2);
5265
	i = I915_READ(aud_cntrl_st2);
5255
	i &= ~eldv;
5266
	i &= ~eldv;
5256
	I915_WRITE(aud_cntrl_st2, i);
5267
	I915_WRITE(aud_cntrl_st2, i);
5257
 
5268
 
5258
	if (!eld[0])
5269
	if (!eld[0])
5259
		return;
5270
		return;
5260
 
5271
 
5261
	i = I915_READ(aud_cntl_st);
5272
	i = I915_READ(aud_cntl_st);
5262
	i &= ~IBX_ELD_ADDRESS;
5273
	i &= ~IBX_ELD_ADDRESS;
5263
	I915_WRITE(aud_cntl_st, i);
5274
	I915_WRITE(aud_cntl_st, i);
5264
	i = (i >> 29) & DIP_PORT_SEL_MASK;		/* DIP_Port_Select, 0x1 = PortB */
5275
	i = (i >> 29) & DIP_PORT_SEL_MASK;		/* DIP_Port_Select, 0x1 = PortB */
5265
	DRM_DEBUG_DRIVER("port num:%d\n", i);
5276
	DRM_DEBUG_DRIVER("port num:%d\n", i);
5266
 
5277
 
5267
	len = min_t(uint8_t, eld[2], 21);	/* 84 bytes of hw ELD buffer */
5278
	len = min_t(uint8_t, eld[2], 21);	/* 84 bytes of hw ELD buffer */
5268
	DRM_DEBUG_DRIVER("ELD size %d\n", len);
5279
	DRM_DEBUG_DRIVER("ELD size %d\n", len);
5269
	for (i = 0; i < len; i++)
5280
	for (i = 0; i < len; i++)
5270
		I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
5281
		I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
5271
 
5282
 
5272
	i = I915_READ(aud_cntrl_st2);
5283
	i = I915_READ(aud_cntrl_st2);
5273
	i |= eldv;
5284
	i |= eldv;
5274
	I915_WRITE(aud_cntrl_st2, i);
5285
	I915_WRITE(aud_cntrl_st2, i);
5275
 
5286
 
5276
}
5287
}
5277
 
5288
 
5278
static void ironlake_write_eld(struct drm_connector *connector,
5289
static void ironlake_write_eld(struct drm_connector *connector,
5279
				     struct drm_crtc *crtc)
5290
				     struct drm_crtc *crtc)
5280
{
5291
{
5281
	struct drm_i915_private *dev_priv = connector->dev->dev_private;
5292
	struct drm_i915_private *dev_priv = connector->dev->dev_private;
5282
	uint8_t *eld = connector->eld;
5293
	uint8_t *eld = connector->eld;
5283
	uint32_t eldv;
5294
	uint32_t eldv;
5284
	uint32_t i;
5295
	uint32_t i;
5285
	int len;
5296
	int len;
5286
	int hdmiw_hdmiedid;
5297
	int hdmiw_hdmiedid;
5287
	int aud_config;
5298
	int aud_config;
5288
	int aud_cntl_st;
5299
	int aud_cntl_st;
5289
	int aud_cntrl_st2;
5300
	int aud_cntrl_st2;
5290
	int pipe = to_intel_crtc(crtc)->pipe;
5301
	int pipe = to_intel_crtc(crtc)->pipe;
5291
 
5302
 
5292
	if (HAS_PCH_IBX(connector->dev)) {
5303
	if (HAS_PCH_IBX(connector->dev)) {
5293
		hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
5304
		hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
5294
		aud_config = IBX_AUD_CFG(pipe);
5305
		aud_config = IBX_AUD_CFG(pipe);
5295
		aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
5306
		aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
5296
		aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
5307
		aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
5297
	} else {
5308
	} else {
5298
		hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
5309
		hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
5299
		aud_config = CPT_AUD_CFG(pipe);
5310
		aud_config = CPT_AUD_CFG(pipe);
5300
		aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
5311
		aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
5301
		aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
5312
		aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
5302
	}
5313
	}
5303
 
5314
 
5304
	DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
5315
	DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
5305
 
5316
 
5306
	i = I915_READ(aud_cntl_st);
5317
	i = I915_READ(aud_cntl_st);
5307
	i = (i >> 29) & DIP_PORT_SEL_MASK;		/* DIP_Port_Select, 0x1 = PortB */
5318
	i = (i >> 29) & DIP_PORT_SEL_MASK;		/* DIP_Port_Select, 0x1 = PortB */
5308
	if (!i) {
5319
	if (!i) {
5309
		DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
5320
		DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
5310
		/* operate blindly on all ports */
5321
		/* operate blindly on all ports */
5311
		eldv = IBX_ELD_VALIDB;
5322
		eldv = IBX_ELD_VALIDB;
5312
		eldv |= IBX_ELD_VALIDB << 4;
5323
		eldv |= IBX_ELD_VALIDB << 4;
5313
		eldv |= IBX_ELD_VALIDB << 8;
5324
		eldv |= IBX_ELD_VALIDB << 8;
5314
	} else {
5325
	} else {
5315
		DRM_DEBUG_DRIVER("ELD on port %c\n", 'A' + i);
5326
		DRM_DEBUG_DRIVER("ELD on port %c\n", 'A' + i);
5316
		eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
5327
		eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
5317
	}
5328
	}
5318
 
5329
 
5319
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
5330
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
5320
		DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
5331
		DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
5321
		eld[5] |= (1 << 2);	/* Conn_Type, 0x1 = DisplayPort */
5332
		eld[5] |= (1 << 2);	/* Conn_Type, 0x1 = DisplayPort */
5322
		I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
5333
		I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
5323
	} else
5334
	} else
5324
		I915_WRITE(aud_config, 0);
5335
		I915_WRITE(aud_config, 0);
5325
 
5336
 
5326
	if (intel_eld_uptodate(connector,
5337
	if (intel_eld_uptodate(connector,
5327
			       aud_cntrl_st2, eldv,
5338
			       aud_cntrl_st2, eldv,
5328
			       aud_cntl_st, IBX_ELD_ADDRESS,
5339
			       aud_cntl_st, IBX_ELD_ADDRESS,
5329
			       hdmiw_hdmiedid))
5340
			       hdmiw_hdmiedid))
5330
		return;
5341
		return;
5331
 
5342
 
5332
	i = I915_READ(aud_cntrl_st2);
5343
	i = I915_READ(aud_cntrl_st2);
5333
	i &= ~eldv;
5344
	i &= ~eldv;
5334
	I915_WRITE(aud_cntrl_st2, i);
5345
	I915_WRITE(aud_cntrl_st2, i);
5335
 
5346
 
5336
	if (!eld[0])
5347
	if (!eld[0])
5337
		return;
5348
		return;
5338
 
5349
 
5339
	i = I915_READ(aud_cntl_st);
5350
	i = I915_READ(aud_cntl_st);
5340
	i &= ~IBX_ELD_ADDRESS;
5351
	i &= ~IBX_ELD_ADDRESS;
5341
	I915_WRITE(aud_cntl_st, i);
5352
	I915_WRITE(aud_cntl_st, i);
5342
 
5353
 
5343
	len = min_t(uint8_t, eld[2], 21);	/* 84 bytes of hw ELD buffer */
5354
	len = min_t(uint8_t, eld[2], 21);	/* 84 bytes of hw ELD buffer */
5344
	DRM_DEBUG_DRIVER("ELD size %d\n", len);
5355
	DRM_DEBUG_DRIVER("ELD size %d\n", len);
5345
	for (i = 0; i < len; i++)
5356
	for (i = 0; i < len; i++)
5346
		I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
5357
		I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
5347
 
5358
 
5348
	i = I915_READ(aud_cntrl_st2);
5359
	i = I915_READ(aud_cntrl_st2);
5349
	i |= eldv;
5360
	i |= eldv;
5350
	I915_WRITE(aud_cntrl_st2, i);
5361
	I915_WRITE(aud_cntrl_st2, i);
5351
}
5362
}
5352
 
5363
 
5353
void intel_write_eld(struct drm_encoder *encoder,
5364
void intel_write_eld(struct drm_encoder *encoder,
5354
		     struct drm_display_mode *mode)
5365
		     struct drm_display_mode *mode)
5355
{
5366
{
5356
	struct drm_crtc *crtc = encoder->crtc;
5367
	struct drm_crtc *crtc = encoder->crtc;
5357
	struct drm_connector *connector;
5368
	struct drm_connector *connector;
5358
	struct drm_device *dev = encoder->dev;
5369
	struct drm_device *dev = encoder->dev;
5359
	struct drm_i915_private *dev_priv = dev->dev_private;
5370
	struct drm_i915_private *dev_priv = dev->dev_private;
5360
 
5371
 
5361
	connector = drm_select_eld(encoder, mode);
5372
	connector = drm_select_eld(encoder, mode);
5362
	if (!connector)
5373
	if (!connector)
5363
		return;
5374
		return;
5364
 
5375
 
5365
	DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
5376
	DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
5366
			 connector->base.id,
5377
			 connector->base.id,
5367
			 drm_get_connector_name(connector),
5378
			 drm_get_connector_name(connector),
5368
			 connector->encoder->base.id,
5379
			 connector->encoder->base.id,
5369
			 drm_get_encoder_name(connector->encoder));
5380
			 drm_get_encoder_name(connector->encoder));
5370
 
5381
 
5371
	connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
5382
	connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
5372
 
5383
 
5373
	if (dev_priv->display.write_eld)
5384
	if (dev_priv->display.write_eld)
5374
		dev_priv->display.write_eld(connector, crtc);
5385
		dev_priv->display.write_eld(connector, crtc);
5375
}
5386
}
5376
 
5387
 
5377
/** Loads the palette/gamma unit for the CRTC with the prepared values */
5388
/** Loads the palette/gamma unit for the CRTC with the prepared values */
5378
void intel_crtc_load_lut(struct drm_crtc *crtc)
5389
void intel_crtc_load_lut(struct drm_crtc *crtc)
5379
{
5390
{
5380
	struct drm_device *dev = crtc->dev;
5391
	struct drm_device *dev = crtc->dev;
5381
	struct drm_i915_private *dev_priv = dev->dev_private;
5392
	struct drm_i915_private *dev_priv = dev->dev_private;
5382
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5393
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5383
	int palreg = PALETTE(intel_crtc->pipe);
5394
	int palreg = PALETTE(intel_crtc->pipe);
5384
	int i;
5395
	int i;
5385
 
5396
 
5386
	/* The clocks have to be on to load the palette. */
5397
	/* The clocks have to be on to load the palette. */
5387
	if (!crtc->enabled || !intel_crtc->active)
5398
	if (!crtc->enabled || !intel_crtc->active)
5388
		return;
5399
		return;
5389
 
5400
 
5390
	/* use legacy palette for Ironlake */
5401
	/* use legacy palette for Ironlake */
5391
	if (HAS_PCH_SPLIT(dev))
5402
	if (HAS_PCH_SPLIT(dev))
5392
		palreg = LGC_PALETTE(intel_crtc->pipe);
5403
		palreg = LGC_PALETTE(intel_crtc->pipe);
5393
 
5404
 
5394
	for (i = 0; i < 256; i++) {
5405
	for (i = 0; i < 256; i++) {
5395
		I915_WRITE(palreg + 4 * i,
5406
		I915_WRITE(palreg + 4 * i,
5396
			   (intel_crtc->lut_r[i] << 16) |
5407
			   (intel_crtc->lut_r[i] << 16) |
5397
			   (intel_crtc->lut_g[i] << 8) |
5408
			   (intel_crtc->lut_g[i] << 8) |
5398
			   intel_crtc->lut_b[i]);
5409
			   intel_crtc->lut_b[i]);
5399
	}
5410
	}
5400
}
5411
}
5401
 
5412
 
5402
#if 0
5413
#if 0
5403
static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
5414
static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
5404
{
5415
{
5405
	struct drm_device *dev = crtc->dev;
5416
	struct drm_device *dev = crtc->dev;
5406
	struct drm_i915_private *dev_priv = dev->dev_private;
5417
	struct drm_i915_private *dev_priv = dev->dev_private;
5407
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5418
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5408
	bool visible = base != 0;
5419
	bool visible = base != 0;
5409
	u32 cntl;
5420
	u32 cntl;
5410
 
5421
 
5411
	if (intel_crtc->cursor_visible == visible)
5422
	if (intel_crtc->cursor_visible == visible)
5412
		return;
5423
		return;
5413
 
5424
 
5414
	cntl = I915_READ(_CURACNTR);
5425
	cntl = I915_READ(_CURACNTR);
5415
	if (visible) {
5426
	if (visible) {
5416
		/* On these chipsets we can only modify the base whilst
5427
		/* On these chipsets we can only modify the base whilst
5417
		 * the cursor is disabled.
5428
		 * the cursor is disabled.
5418
		 */
5429
		 */
5419
		I915_WRITE(_CURABASE, base);
5430
		I915_WRITE(_CURABASE, base);
5420
 
5431
 
5421
		cntl &= ~(CURSOR_FORMAT_MASK);
5432
		cntl &= ~(CURSOR_FORMAT_MASK);
5422
		/* XXX width must be 64, stride 256 => 0x00 << 28 */
5433
		/* XXX width must be 64, stride 256 => 0x00 << 28 */
5423
		cntl |= CURSOR_ENABLE |
5434
		cntl |= CURSOR_ENABLE |
5424
			CURSOR_GAMMA_ENABLE |
5435
			CURSOR_GAMMA_ENABLE |
5425
			CURSOR_FORMAT_ARGB;
5436
			CURSOR_FORMAT_ARGB;
5426
	} else
5437
	} else
5427
		cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
5438
		cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
5428
	I915_WRITE(_CURACNTR, cntl);
5439
	I915_WRITE(_CURACNTR, cntl);
5429
 
5440
 
5430
	intel_crtc->cursor_visible = visible;
5441
	intel_crtc->cursor_visible = visible;
5431
}
5442
}
5432
 
5443
 
5433
static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
5444
static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
5434
{
5445
{
5435
	struct drm_device *dev = crtc->dev;
5446
	struct drm_device *dev = crtc->dev;
5436
	struct drm_i915_private *dev_priv = dev->dev_private;
5447
	struct drm_i915_private *dev_priv = dev->dev_private;
5437
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5448
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5438
	int pipe = intel_crtc->pipe;
5449
	int pipe = intel_crtc->pipe;
5439
	bool visible = base != 0;
5450
	bool visible = base != 0;
5440
 
5451
 
5441
	if (intel_crtc->cursor_visible != visible) {
5452
	if (intel_crtc->cursor_visible != visible) {
5442
		uint32_t cntl = I915_READ(CURCNTR(pipe));
5453
		uint32_t cntl = I915_READ(CURCNTR(pipe));
5443
		if (base) {
5454
		if (base) {
5444
			cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
5455
			cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
5445
			cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
5456
			cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
5446
			cntl |= pipe << 28; /* Connect to correct pipe */
5457
			cntl |= pipe << 28; /* Connect to correct pipe */
5447
		} else {
5458
		} else {
5448
			cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
5459
			cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
5449
			cntl |= CURSOR_MODE_DISABLE;
5460
			cntl |= CURSOR_MODE_DISABLE;
5450
		}
5461
		}
5451
		I915_WRITE(CURCNTR(pipe), cntl);
5462
		I915_WRITE(CURCNTR(pipe), cntl);
5452
 
5463
 
5453
		intel_crtc->cursor_visible = visible;
5464
		intel_crtc->cursor_visible = visible;
5454
	}
5465
	}
5455
	/* and commit changes on next vblank */
5466
	/* and commit changes on next vblank */
5456
	I915_WRITE(CURBASE(pipe), base);
5467
	I915_WRITE(CURBASE(pipe), base);
5457
}
5468
}
5458
 
5469
 
5459
static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
5470
static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
5460
{
5471
{
5461
	struct drm_device *dev = crtc->dev;
5472
	struct drm_device *dev = crtc->dev;
5462
	struct drm_i915_private *dev_priv = dev->dev_private;
5473
	struct drm_i915_private *dev_priv = dev->dev_private;
5463
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5474
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5464
	int pipe = intel_crtc->pipe;
5475
	int pipe = intel_crtc->pipe;
5465
	bool visible = base != 0;
5476
	bool visible = base != 0;
5466
 
5477
 
5467
	if (intel_crtc->cursor_visible != visible) {
5478
	if (intel_crtc->cursor_visible != visible) {
5468
		uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
5479
		uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
5469
		if (base) {
5480
		if (base) {
5470
			cntl &= ~CURSOR_MODE;
5481
			cntl &= ~CURSOR_MODE;
5471
			cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
5482
			cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
5472
		} else {
5483
		} else {
5473
			cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
5484
			cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
5474
			cntl |= CURSOR_MODE_DISABLE;
5485
			cntl |= CURSOR_MODE_DISABLE;
5475
		}
5486
		}
5476
		I915_WRITE(CURCNTR_IVB(pipe), cntl);
5487
		I915_WRITE(CURCNTR_IVB(pipe), cntl);
5477
 
5488
 
5478
		intel_crtc->cursor_visible = visible;
5489
		intel_crtc->cursor_visible = visible;
5479
	}
5490
	}
5480
	/* and commit changes on next vblank */
5491
	/* and commit changes on next vblank */
5481
	I915_WRITE(CURBASE_IVB(pipe), base);
5492
	I915_WRITE(CURBASE_IVB(pipe), base);
5482
}
5493
}
5483
 
5494
 
5484
/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
5495
/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
5485
static void intel_crtc_update_cursor(struct drm_crtc *crtc,
5496
static void intel_crtc_update_cursor(struct drm_crtc *crtc,
5486
				     bool on)
5497
				     bool on)
5487
{
5498
{
5488
	struct drm_device *dev = crtc->dev;
5499
	struct drm_device *dev = crtc->dev;
5489
	struct drm_i915_private *dev_priv = dev->dev_private;
5500
	struct drm_i915_private *dev_priv = dev->dev_private;
5490
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5501
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5491
	int pipe = intel_crtc->pipe;
5502
	int pipe = intel_crtc->pipe;
5492
	int x = intel_crtc->cursor_x;
5503
	int x = intel_crtc->cursor_x;
5493
	int y = intel_crtc->cursor_y;
5504
	int y = intel_crtc->cursor_y;
5494
	u32 base, pos;
5505
	u32 base, pos;
5495
	bool visible;
5506
	bool visible;
5496
 
5507
 
5497
	pos = 0;
5508
	pos = 0;
5498
 
5509
 
5499
	if (on && crtc->enabled && crtc->fb) {
5510
	if (on && crtc->enabled && crtc->fb) {
5500
		base = intel_crtc->cursor_addr;
5511
		base = intel_crtc->cursor_addr;
5501
		if (x > (int) crtc->fb->width)
5512
		if (x > (int) crtc->fb->width)
5502
			base = 0;
5513
			base = 0;
5503
 
5514
 
5504
		if (y > (int) crtc->fb->height)
5515
		if (y > (int) crtc->fb->height)
5505
			base = 0;
5516
			base = 0;
5506
	} else
5517
	} else
5507
		base = 0;
5518
		base = 0;
5508
 
5519
 
5509
	if (x < 0) {
5520
	if (x < 0) {
5510
		if (x + intel_crtc->cursor_width < 0)
5521
		if (x + intel_crtc->cursor_width < 0)
5511
			base = 0;
5522
			base = 0;
5512
 
5523
 
5513
		pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
5524
		pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
5514
		x = -x;
5525
		x = -x;
5515
	}
5526
	}
5516
	pos |= x << CURSOR_X_SHIFT;
5527
	pos |= x << CURSOR_X_SHIFT;
5517
 
5528
 
5518
	if (y < 0) {
5529
	if (y < 0) {
5519
		if (y + intel_crtc->cursor_height < 0)
5530
		if (y + intel_crtc->cursor_height < 0)
5520
			base = 0;
5531
			base = 0;
5521
 
5532
 
5522
		pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
5533
		pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
5523
		y = -y;
5534
		y = -y;
5524
	}
5535
	}
5525
	pos |= y << CURSOR_Y_SHIFT;
5536
	pos |= y << CURSOR_Y_SHIFT;
5526
 
5537
 
5527
	visible = base != 0;
5538
	visible = base != 0;
5528
	if (!visible && !intel_crtc->cursor_visible)
5539
	if (!visible && !intel_crtc->cursor_visible)
5529
		return;
5540
		return;
5530
 
5541
 
5531
	if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
5542
	if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
5532
		I915_WRITE(CURPOS_IVB(pipe), pos);
5543
		I915_WRITE(CURPOS_IVB(pipe), pos);
5533
		ivb_update_cursor(crtc, base);
5544
		ivb_update_cursor(crtc, base);
5534
	} else {
5545
	} else {
5535
		I915_WRITE(CURPOS(pipe), pos);
5546
		I915_WRITE(CURPOS(pipe), pos);
5536
		if (IS_845G(dev) || IS_I865G(dev))
5547
		if (IS_845G(dev) || IS_I865G(dev))
5537
			i845_update_cursor(crtc, base);
5548
			i845_update_cursor(crtc, base);
5538
		else
5549
		else
5539
			i9xx_update_cursor(crtc, base);
5550
			i9xx_update_cursor(crtc, base);
5540
	}
5551
	}
5541
}
5552
}
5542
 
5553
 
5543
static int intel_crtc_cursor_set(struct drm_crtc *crtc,
5554
static int intel_crtc_cursor_set(struct drm_crtc *crtc,
5544
				 struct drm_file *file,
5555
				 struct drm_file *file,
5545
				 uint32_t handle,
5556
				 uint32_t handle,
5546
				 uint32_t width, uint32_t height)
5557
				 uint32_t width, uint32_t height)
5547
{
5558
{
5548
	struct drm_device *dev = crtc->dev;
5559
	struct drm_device *dev = crtc->dev;
5549
	struct drm_i915_private *dev_priv = dev->dev_private;
5560
	struct drm_i915_private *dev_priv = dev->dev_private;
5550
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5561
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5551
	struct drm_i915_gem_object *obj;
5562
	struct drm_i915_gem_object *obj;
5552
	uint32_t addr;
5563
	uint32_t addr;
5553
	int ret;
5564
	int ret;
5554
 
5565
 
5555
	/* if we want to turn off the cursor ignore width and height */
5566
	/* if we want to turn off the cursor ignore width and height */
5556
	if (!handle) {
5567
	if (!handle) {
5557
		DRM_DEBUG_KMS("cursor off\n");
5568
		DRM_DEBUG_KMS("cursor off\n");
5558
		addr = 0;
5569
		addr = 0;
5559
		obj = NULL;
5570
		obj = NULL;
5560
		mutex_lock(&dev->struct_mutex);
5571
		mutex_lock(&dev->struct_mutex);
5561
		goto finish;
5572
		goto finish;
5562
	}
5573
	}
5563
 
5574
 
5564
	/* Currently we only support 64x64 cursors */
5575
	/* Currently we only support 64x64 cursors */
5565
	if (width != 64 || height != 64) {
5576
	if (width != 64 || height != 64) {
5566
		DRM_ERROR("we currently only support 64x64 cursors\n");
5577
		DRM_ERROR("we currently only support 64x64 cursors\n");
5567
		return -EINVAL;
5578
		return -EINVAL;
5568
	}
5579
	}
5569
 
5580
 
5570
	obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
5581
	obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
5571
	if (&obj->base == NULL)
5582
	if (&obj->base == NULL)
5572
		return -ENOENT;
5583
		return -ENOENT;
5573
 
5584
 
5574
	if (obj->base.size < width * height * 4) {
5585
	if (obj->base.size < width * height * 4) {
5575
		DRM_ERROR("buffer is to small\n");
5586
		DRM_ERROR("buffer is to small\n");
5576
		ret = -ENOMEM;
5587
		ret = -ENOMEM;
5577
		goto fail;
5588
		goto fail;
5578
	}
5589
	}
5579
 
5590
 
5580
	/* we only need to pin inside GTT if cursor is non-phy */
5591
	/* we only need to pin inside GTT if cursor is non-phy */
5581
	mutex_lock(&dev->struct_mutex);
5592
	mutex_lock(&dev->struct_mutex);
5582
	if (!dev_priv->info->cursor_needs_physical) {
5593
	if (!dev_priv->info->cursor_needs_physical) {
5583
		if (obj->tiling_mode) {
5594
		if (obj->tiling_mode) {
5584
			DRM_ERROR("cursor cannot be tiled\n");
5595
			DRM_ERROR("cursor cannot be tiled\n");
5585
			ret = -EINVAL;
5596
			ret = -EINVAL;
5586
			goto fail_locked;
5597
			goto fail_locked;
5587
		}
5598
		}
5588
 
5599
 
5589
		ret = i915_gem_object_pin_to_display_plane(obj, 0, NULL);
5600
		ret = i915_gem_object_pin_to_display_plane(obj, 0, NULL);
5590
		if (ret) {
5601
		if (ret) {
5591
			DRM_ERROR("failed to move cursor bo into the GTT\n");
5602
			DRM_ERROR("failed to move cursor bo into the GTT\n");
5592
			goto fail_locked;
5603
			goto fail_locked;
5593
		}
5604
		}
5594
 
5605
 
5595
		ret = i915_gem_object_put_fence(obj);
5606
		ret = i915_gem_object_put_fence(obj);
5596
		if (ret) {
5607
		if (ret) {
5597
			DRM_ERROR("failed to release fence for cursor");
5608
			DRM_ERROR("failed to release fence for cursor");
5598
			goto fail_unpin;
5609
			goto fail_unpin;
5599
		}
5610
		}
5600
 
5611
 
5601
		addr = obj->gtt_offset;
5612
		addr = obj->gtt_offset;
5602
	} else {
5613
	} else {
5603
		int align = IS_I830(dev) ? 16 * 1024 : 256;
5614
		int align = IS_I830(dev) ? 16 * 1024 : 256;
5604
		ret = i915_gem_attach_phys_object(dev, obj,
5615
		ret = i915_gem_attach_phys_object(dev, obj,
5605
						  (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
5616
						  (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
5606
						  align);
5617
						  align);
5607
		if (ret) {
5618
		if (ret) {
5608
			DRM_ERROR("failed to attach phys object\n");
5619
			DRM_ERROR("failed to attach phys object\n");
5609
			goto fail_locked;
5620
			goto fail_locked;
5610
		}
5621
		}
5611
		addr = obj->phys_obj->handle->busaddr;
5622
		addr = obj->phys_obj->handle->busaddr;
5612
	}
5623
	}
5613
 
5624
 
5614
	if (IS_GEN2(dev))
5625
	if (IS_GEN2(dev))
5615
		I915_WRITE(CURSIZE, (height << 12) | width);
5626
		I915_WRITE(CURSIZE, (height << 12) | width);
5616
 
5627
 
5617
 finish:
5628
 finish:
5618
	if (intel_crtc->cursor_bo) {
5629
	if (intel_crtc->cursor_bo) {
5619
		if (dev_priv->info->cursor_needs_physical) {
5630
		if (dev_priv->info->cursor_needs_physical) {
5620
			if (intel_crtc->cursor_bo != obj)
5631
			if (intel_crtc->cursor_bo != obj)
5621
				i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
5632
				i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
5622
		} else
5633
		} else
5623
			i915_gem_object_unpin(intel_crtc->cursor_bo);
5634
			i915_gem_object_unpin(intel_crtc->cursor_bo);
5624
		drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
5635
		drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
5625
	}
5636
	}
5626
 
5637
 
5627
	mutex_unlock(&dev->struct_mutex);
5638
	mutex_unlock(&dev->struct_mutex);
5628
 
5639
 
5629
	intel_crtc->cursor_addr = addr;
5640
	intel_crtc->cursor_addr = addr;
5630
	intel_crtc->cursor_bo = obj;
5641
	intel_crtc->cursor_bo = obj;
5631
	intel_crtc->cursor_width = width;
5642
	intel_crtc->cursor_width = width;
5632
	intel_crtc->cursor_height = height;
5643
	intel_crtc->cursor_height = height;
5633
 
5644
 
5634
//   intel_crtc_update_cursor(crtc, true);
5645
//   intel_crtc_update_cursor(crtc, true);
5635
 
5646
 
5636
	return 0;
5647
	return 0;
5637
fail_unpin:
5648
fail_unpin:
5638
	i915_gem_object_unpin(obj);
5649
	i915_gem_object_unpin(obj);
5639
fail_locked:
5650
fail_locked:
5640
	mutex_unlock(&dev->struct_mutex);
5651
	mutex_unlock(&dev->struct_mutex);
5641
fail:
5652
fail:
5642
	drm_gem_object_unreference_unlocked(&obj->base);
5653
	drm_gem_object_unreference_unlocked(&obj->base);
5643
	return ret;
5654
	return ret;
5644
}
5655
}
5645
 
5656
 
5646
static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
5657
static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
5647
{
5658
{
5648
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5659
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5649
 
5660
 
5650
	intel_crtc->cursor_x = x;
5661
	intel_crtc->cursor_x = x;
5651
	intel_crtc->cursor_y = y;
5662
	intel_crtc->cursor_y = y;
5652
 
5663
 
5653
//   intel_crtc_update_cursor(crtc, true);
5664
//   intel_crtc_update_cursor(crtc, true);
5654
 
5665
 
5655
	return 0;
5666
	return 0;
5656
}
5667
}
5657
#endif
5668
#endif
5658
 
5669
 
5659
/** Sets the color ramps on behalf of RandR */
5670
/** Sets the color ramps on behalf of RandR */
5660
void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
5671
void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
5661
				 u16 blue, int regno)
5672
				 u16 blue, int regno)
5662
{
5673
{
5663
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5674
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5664
 
5675
 
5665
	intel_crtc->lut_r[regno] = red >> 8;
5676
	intel_crtc->lut_r[regno] = red >> 8;
5666
	intel_crtc->lut_g[regno] = green >> 8;
5677
	intel_crtc->lut_g[regno] = green >> 8;
5667
	intel_crtc->lut_b[regno] = blue >> 8;
5678
	intel_crtc->lut_b[regno] = blue >> 8;
5668
}
5679
}
5669
 
5680
 
5670
void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
5681
void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
5671
			     u16 *blue, int regno)
5682
			     u16 *blue, int regno)
5672
{
5683
{
5673
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5684
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5674
 
5685
 
5675
	*red = intel_crtc->lut_r[regno] << 8;
5686
	*red = intel_crtc->lut_r[regno] << 8;
5676
	*green = intel_crtc->lut_g[regno] << 8;
5687
	*green = intel_crtc->lut_g[regno] << 8;
5677
	*blue = intel_crtc->lut_b[regno] << 8;
5688
	*blue = intel_crtc->lut_b[regno] << 8;
5678
}
5689
}
5679
 
5690
 
5680
static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
5691
static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
5681
				 u16 *blue, uint32_t start, uint32_t size)
5692
				 u16 *blue, uint32_t start, uint32_t size)
5682
{
5693
{
5683
	int end = (start + size > 256) ? 256 : start + size, i;
5694
	int end = (start + size > 256) ? 256 : start + size, i;
5684
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5695
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5685
 
5696
 
5686
	for (i = start; i < end; i++) {
5697
	for (i = start; i < end; i++) {
5687
		intel_crtc->lut_r[i] = red[i] >> 8;
5698
		intel_crtc->lut_r[i] = red[i] >> 8;
5688
		intel_crtc->lut_g[i] = green[i] >> 8;
5699
		intel_crtc->lut_g[i] = green[i] >> 8;
5689
		intel_crtc->lut_b[i] = blue[i] >> 8;
5700
		intel_crtc->lut_b[i] = blue[i] >> 8;
5690
	}
5701
	}
5691
 
5702
 
5692
	intel_crtc_load_lut(crtc);
5703
	intel_crtc_load_lut(crtc);
5693
}
5704
}
5694
 
5705
 
5695
/**
5706
/**
5696
 * Get a pipe with a simple mode set on it for doing load-based monitor
5707
 * Get a pipe with a simple mode set on it for doing load-based monitor
5697
 * detection.
5708
 * detection.
5698
 *
5709
 *
5699
 * It will be up to the load-detect code to adjust the pipe as appropriate for
5710
 * It will be up to the load-detect code to adjust the pipe as appropriate for
5700
 * its requirements.  The pipe will be connected to no other encoders.
5711
 * its requirements.  The pipe will be connected to no other encoders.
5701
 *
5712
 *
5702
 * Currently this code will only succeed if there is a pipe with no encoders
5713
 * Currently this code will only succeed if there is a pipe with no encoders
5703
 * configured for it.  In the future, it could choose to temporarily disable
5714
 * configured for it.  In the future, it could choose to temporarily disable
5704
 * some outputs to free up a pipe for its use.
5715
 * some outputs to free up a pipe for its use.
5705
 *
5716
 *
5706
 * \return crtc, or NULL if no pipes are available.
5717
 * \return crtc, or NULL if no pipes are available.
5707
 */
5718
 */
5708
 
5719
 
5709
/* VESA 640x480x72Hz mode to set on the pipe */
5720
/* VESA 640x480x72Hz mode to set on the pipe */
5710
static struct drm_display_mode load_detect_mode = {
5721
static struct drm_display_mode load_detect_mode = {
5711
	DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
5722
	DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
5712
		 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
5723
		 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
5713
};
5724
};
5714
 
5725
 
5715
static struct drm_framebuffer *
5726
static struct drm_framebuffer *
5716
intel_framebuffer_create(struct drm_device *dev,
5727
intel_framebuffer_create(struct drm_device *dev,
5717
			 struct drm_mode_fb_cmd2 *mode_cmd,
5728
			 struct drm_mode_fb_cmd2 *mode_cmd,
5718
			 struct drm_i915_gem_object *obj)
5729
			 struct drm_i915_gem_object *obj)
5719
{
5730
{
5720
	struct intel_framebuffer *intel_fb;
5731
	struct intel_framebuffer *intel_fb;
5721
	int ret;
5732
	int ret;
5722
 
5733
 
5723
	intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
5734
	intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
5724
	if (!intel_fb) {
5735
	if (!intel_fb) {
5725
		drm_gem_object_unreference_unlocked(&obj->base);
5736
		drm_gem_object_unreference_unlocked(&obj->base);
5726
		return ERR_PTR(-ENOMEM);
5737
		return ERR_PTR(-ENOMEM);
5727
	}
5738
	}
5728
 
5739
 
5729
	ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
5740
	ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
5730
	if (ret) {
5741
	if (ret) {
5731
		drm_gem_object_unreference_unlocked(&obj->base);
5742
		drm_gem_object_unreference_unlocked(&obj->base);
5732
		kfree(intel_fb);
5743
		kfree(intel_fb);
5733
		return ERR_PTR(ret);
5744
		return ERR_PTR(ret);
5734
	}
5745
	}
5735
 
5746
 
5736
	return &intel_fb->base;
5747
	return &intel_fb->base;
5737
}
5748
}
5738
 
5749
 
5739
static u32
5750
static u32
5740
intel_framebuffer_pitch_for_width(int width, int bpp)
5751
intel_framebuffer_pitch_for_width(int width, int bpp)
5741
{
5752
{
5742
	u32 pitch = DIV_ROUND_UP(width * bpp, 8);
5753
	u32 pitch = DIV_ROUND_UP(width * bpp, 8);
5743
	return ALIGN(pitch, 64);
5754
	return ALIGN(pitch, 64);
5744
}
5755
}
5745
 
5756
 
5746
static u32
5757
static u32
5747
intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
5758
intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
5748
{
5759
{
5749
	u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
5760
	u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
5750
	return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
5761
	return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
5751
}
5762
}
5752
 
5763
 
5753
static struct drm_framebuffer *
5764
static struct drm_framebuffer *
5754
intel_framebuffer_create_for_mode(struct drm_device *dev,
5765
intel_framebuffer_create_for_mode(struct drm_device *dev,
5755
				  struct drm_display_mode *mode,
5766
				  struct drm_display_mode *mode,
5756
				  int depth, int bpp)
5767
				  int depth, int bpp)
5757
{
5768
{
5758
	struct drm_i915_gem_object *obj;
5769
	struct drm_i915_gem_object *obj;
5759
	struct drm_mode_fb_cmd2 mode_cmd;
5770
	struct drm_mode_fb_cmd2 mode_cmd;
5760
 
5771
 
5761
//	obj = i915_gem_alloc_object(dev,
5772
//	obj = i915_gem_alloc_object(dev,
5762
//				    intel_framebuffer_size_for_mode(mode, bpp));
5773
//				    intel_framebuffer_size_for_mode(mode, bpp));
5763
//	if (obj == NULL)
5774
//	if (obj == NULL)
5764
		return ERR_PTR(-ENOMEM);
5775
		return ERR_PTR(-ENOMEM);
5765
 
5776
 
5766
//	mode_cmd.width = mode->hdisplay;
5777
//	mode_cmd.width = mode->hdisplay;
5767
//	mode_cmd.height = mode->vdisplay;
5778
//	mode_cmd.height = mode->vdisplay;
5768
//	mode_cmd.depth = depth;
5779
//	mode_cmd.depth = depth;
5769
//	mode_cmd.bpp = bpp;
5780
//	mode_cmd.bpp = bpp;
5770
//	mode_cmd.pitch = intel_framebuffer_pitch_for_width(mode_cmd.width, bpp);
5781
//	mode_cmd.pitch = intel_framebuffer_pitch_for_width(mode_cmd.width, bpp);
5771
 
5782
 
5772
//	return intel_framebuffer_create(dev, &mode_cmd, obj);
5783
//	return intel_framebuffer_create(dev, &mode_cmd, obj);
5773
}
5784
}
5774
 
5785
 
5775
static struct drm_framebuffer *
5786
static struct drm_framebuffer *
5776
mode_fits_in_fbdev(struct drm_device *dev,
5787
mode_fits_in_fbdev(struct drm_device *dev,
5777
		   struct drm_display_mode *mode)
5788
		   struct drm_display_mode *mode)
5778
{
5789
{
5779
	struct drm_i915_private *dev_priv = dev->dev_private;
5790
	struct drm_i915_private *dev_priv = dev->dev_private;
5780
	struct drm_i915_gem_object *obj;
5791
	struct drm_i915_gem_object *obj;
5781
	struct drm_framebuffer *fb;
5792
	struct drm_framebuffer *fb;
5782
 
5793
 
5783
//	if (dev_priv->fbdev == NULL)
5794
//	if (dev_priv->fbdev == NULL)
5784
//		return NULL;
5795
//		return NULL;
5785
 
5796
 
5786
//	obj = dev_priv->fbdev->ifb.obj;
5797
//	obj = dev_priv->fbdev->ifb.obj;
5787
//	if (obj == NULL)
5798
//	if (obj == NULL)
5788
		return NULL;
5799
		return NULL;
5789
 
5800
 
5790
//	if (obj->base.size < mode->vdisplay * fb->pitch)
5801
//	if (obj->base.size < mode->vdisplay * fb->pitch)
5791
	if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
5802
	if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
5792
							       fb->bits_per_pixel))
5803
							       fb->bits_per_pixel))
5793
//		return NULL;
5804
//		return NULL;
5794
 
5805
 
5795
	if (obj->base.size < mode->vdisplay * fb->pitches[0])
5806
	if (obj->base.size < mode->vdisplay * fb->pitches[0])
5796
		return NULL;
5807
		return NULL;
5797
 
5808
 
5798
//	return fb;
5809
//	return fb;
5799
}
5810
}
5800
 
5811
 
5801
bool intel_get_load_detect_pipe(struct drm_connector *connector,
5812
bool intel_get_load_detect_pipe(struct drm_connector *connector,
5802
				struct drm_display_mode *mode,
5813
				struct drm_display_mode *mode,
5803
				struct intel_load_detect_pipe *old)
5814
				struct intel_load_detect_pipe *old)
5804
{
5815
{
5805
	struct intel_crtc *intel_crtc;
5816
	struct intel_crtc *intel_crtc;
5806
	struct intel_encoder *intel_encoder =
5817
	struct intel_encoder *intel_encoder =
5807
		intel_attached_encoder(connector);
5818
		intel_attached_encoder(connector);
5808
	struct drm_crtc *possible_crtc;
5819
	struct drm_crtc *possible_crtc;
5809
	struct drm_encoder *encoder = &intel_encoder->base;
5820
	struct drm_encoder *encoder = &intel_encoder->base;
5810
	struct drm_crtc *crtc = NULL;
5821
	struct drm_crtc *crtc = NULL;
5811
	struct drm_device *dev = encoder->dev;
5822
	struct drm_device *dev = encoder->dev;
5812
	struct drm_framebuffer *fb;
5823
	struct drm_framebuffer *fb;
5813
	int i = -1;
5824
	int i = -1;
5814
 
5825
 
5815
	DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
5826
	DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
5816
		      connector->base.id, drm_get_connector_name(connector),
5827
		      connector->base.id, drm_get_connector_name(connector),
5817
		      encoder->base.id, drm_get_encoder_name(encoder));
5828
		      encoder->base.id, drm_get_encoder_name(encoder));
5818
 
5829
 
5819
	/*
5830
	/*
5820
	 * Algorithm gets a little messy:
5831
	 * Algorithm gets a little messy:
5821
	 *
5832
	 *
5822
	 *   - if the connector already has an assigned crtc, use it (but make
5833
	 *   - if the connector already has an assigned crtc, use it (but make
5823
	 *     sure it's on first)
5834
	 *     sure it's on first)
5824
	 *
5835
	 *
5825
	 *   - try to find the first unused crtc that can drive this connector,
5836
	 *   - try to find the first unused crtc that can drive this connector,
5826
	 *     and use that if we find one
5837
	 *     and use that if we find one
5827
	 */
5838
	 */
5828
 
5839
 
5829
	/* See if we already have a CRTC for this connector */
5840
	/* See if we already have a CRTC for this connector */
5830
	if (encoder->crtc) {
5841
	if (encoder->crtc) {
5831
		crtc = encoder->crtc;
5842
		crtc = encoder->crtc;
5832
 
5843
 
5833
		old->dpms_mode = connector->dpms;
5844
		old->dpms_mode = connector->dpms;
5834
		old->load_detect_temp = false;
5845
		old->load_detect_temp = false;
5835
 
5846
 
5836
		/* Make sure the crtc and connector are running */
5847
		/* Make sure the crtc and connector are running */
5837
		if (connector->dpms != DRM_MODE_DPMS_ON)
5848
		if (connector->dpms != DRM_MODE_DPMS_ON)
5838
			connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
5849
			connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
5839
 
5850
 
5840
		return true;
5851
		return true;
5841
	}
5852
	}
5842
 
5853
 
5843
	/* Find an unused one (if possible) */
5854
	/* Find an unused one (if possible) */
5844
	list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
5855
	list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
5845
		i++;
5856
		i++;
5846
		if (!(encoder->possible_crtcs & (1 << i)))
5857
		if (!(encoder->possible_crtcs & (1 << i)))
5847
			continue;
5858
			continue;
5848
		if (!possible_crtc->enabled) {
5859
		if (!possible_crtc->enabled) {
5849
			crtc = possible_crtc;
5860
			crtc = possible_crtc;
5850
			break;
5861
			break;
5851
		}
5862
		}
5852
	}
5863
	}
5853
 
5864
 
5854
	/*
5865
	/*
5855
	 * If we didn't find an unused CRTC, don't use any.
5866
	 * If we didn't find an unused CRTC, don't use any.
5856
	 */
5867
	 */
5857
	if (!crtc) {
5868
	if (!crtc) {
5858
		DRM_DEBUG_KMS("no pipe available for load-detect\n");
5869
		DRM_DEBUG_KMS("no pipe available for load-detect\n");
5859
		return false;
5870
		return false;
5860
	}
5871
	}
5861
 
5872
 
5862
	intel_encoder->new_crtc = to_intel_crtc(crtc);
5873
	intel_encoder->new_crtc = to_intel_crtc(crtc);
5863
	to_intel_connector(connector)->new_encoder = intel_encoder;
5874
	to_intel_connector(connector)->new_encoder = intel_encoder;
5864
 
5875
 
5865
	intel_crtc = to_intel_crtc(crtc);
5876
	intel_crtc = to_intel_crtc(crtc);
5866
	old->dpms_mode = connector->dpms;
5877
	old->dpms_mode = connector->dpms;
5867
	old->load_detect_temp = true;
5878
	old->load_detect_temp = true;
5868
	old->release_fb = NULL;
5879
	old->release_fb = NULL;
5869
 
5880
 
5870
	if (!mode)
5881
	if (!mode)
5871
		mode = &load_detect_mode;
5882
		mode = &load_detect_mode;
5872
 
5883
 
5873
	/* We need a framebuffer large enough to accommodate all accesses
5884
	/* We need a framebuffer large enough to accommodate all accesses
5874
	 * that the plane may generate whilst we perform load detection.
5885
	 * that the plane may generate whilst we perform load detection.
5875
	 * We can not rely on the fbcon either being present (we get called
5886
	 * We can not rely on the fbcon either being present (we get called
5876
	 * during its initialisation to detect all boot displays, or it may
5887
	 * during its initialisation to detect all boot displays, or it may
5877
	 * not even exist) or that it is large enough to satisfy the
5888
	 * not even exist) or that it is large enough to satisfy the
5878
	 * requested mode.
5889
	 * requested mode.
5879
	 */
5890
	 */
5880
	fb = mode_fits_in_fbdev(dev, mode);
5891
	fb = mode_fits_in_fbdev(dev, mode);
5881
	if (fb == NULL) {
5892
	if (fb == NULL) {
5882
		DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
5893
		DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
5883
		fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
5894
		fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
5884
		old->release_fb = fb;
5895
		old->release_fb = fb;
5885
	} else
5896
	} else
5886
		DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
5897
		DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
5887
	if (IS_ERR(fb)) {
5898
	if (IS_ERR(fb)) {
5888
		DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
5899
		DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
5889
		goto fail;
5900
		goto fail;
5890
	}
5901
	}
5891
 
5902
 
5892
	if (!intel_set_mode(crtc, mode, 0, 0, fb)) {
5903
	if (!intel_set_mode(crtc, mode, 0, 0, fb)) {
5893
		DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
5904
		DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
5894
		if (old->release_fb)
5905
		if (old->release_fb)
5895
			old->release_fb->funcs->destroy(old->release_fb);
5906
			old->release_fb->funcs->destroy(old->release_fb);
5896
		goto fail;
5907
		goto fail;
5897
	}
5908
	}
5898
 
5909
 
5899
	/* let the connector get through one full cycle before testing */
5910
	/* let the connector get through one full cycle before testing */
5900
	intel_wait_for_vblank(dev, intel_crtc->pipe);
5911
	intel_wait_for_vblank(dev, intel_crtc->pipe);
5901
 
5912
 
5902
	return true;
5913
	return true;
5903
fail:
5914
fail:
5904
	connector->encoder = NULL;
5915
	connector->encoder = NULL;
5905
	encoder->crtc = NULL;
5916
	encoder->crtc = NULL;
5906
	return false;
5917
	return false;
5907
}
5918
}
5908
 
5919
 
5909
void intel_release_load_detect_pipe(struct drm_connector *connector,
5920
void intel_release_load_detect_pipe(struct drm_connector *connector,
5910
				    struct intel_load_detect_pipe *old)
5921
				    struct intel_load_detect_pipe *old)
5911
{
5922
{
5912
	struct intel_encoder *intel_encoder =
5923
	struct intel_encoder *intel_encoder =
5913
		intel_attached_encoder(connector);
5924
		intel_attached_encoder(connector);
5914
	struct drm_encoder *encoder = &intel_encoder->base;
5925
	struct drm_encoder *encoder = &intel_encoder->base;
5915
 
5926
 
5916
	DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
5927
	DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
5917
		      connector->base.id, drm_get_connector_name(connector),
5928
		      connector->base.id, drm_get_connector_name(connector),
5918
		      encoder->base.id, drm_get_encoder_name(encoder));
5929
		      encoder->base.id, drm_get_encoder_name(encoder));
5919
 
5930
 
5920
	if (old->load_detect_temp) {
5931
	if (old->load_detect_temp) {
5921
		struct drm_crtc *crtc = encoder->crtc;
5932
		struct drm_crtc *crtc = encoder->crtc;
5922
 
5933
 
5923
		to_intel_connector(connector)->new_encoder = NULL;
5934
		to_intel_connector(connector)->new_encoder = NULL;
5924
		intel_encoder->new_crtc = NULL;
5935
		intel_encoder->new_crtc = NULL;
5925
		intel_set_mode(crtc, NULL, 0, 0, NULL);
5936
		intel_set_mode(crtc, NULL, 0, 0, NULL);
5926
 
5937
 
5927
		if (old->release_fb)
5938
		if (old->release_fb)
5928
			old->release_fb->funcs->destroy(old->release_fb);
5939
			old->release_fb->funcs->destroy(old->release_fb);
5929
 
5940
 
5930
		return;
5941
		return;
5931
	}
5942
	}
5932
 
5943
 
5933
	/* Switch crtc and encoder back off if necessary */
5944
	/* Switch crtc and encoder back off if necessary */
5934
	if (old->dpms_mode != DRM_MODE_DPMS_ON)
5945
	if (old->dpms_mode != DRM_MODE_DPMS_ON)
5935
		connector->funcs->dpms(connector, old->dpms_mode);
5946
		connector->funcs->dpms(connector, old->dpms_mode);
5936
}
5947
}
5937
 
5948
 
5938
/* Returns the clock of the currently programmed mode of the given pipe. */
5949
/* Returns the clock of the currently programmed mode of the given pipe. */
5939
static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
5950
static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
5940
{
5951
{
5941
	struct drm_i915_private *dev_priv = dev->dev_private;
5952
	struct drm_i915_private *dev_priv = dev->dev_private;
5942
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5953
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5943
	int pipe = intel_crtc->pipe;
5954
	int pipe = intel_crtc->pipe;
5944
	u32 dpll = I915_READ(DPLL(pipe));
5955
	u32 dpll = I915_READ(DPLL(pipe));
5945
	u32 fp;
5956
	u32 fp;
5946
	intel_clock_t clock;
5957
	intel_clock_t clock;
5947
 
5958
 
5948
	if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
5959
	if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
5949
		fp = I915_READ(FP0(pipe));
5960
		fp = I915_READ(FP0(pipe));
5950
	else
5961
	else
5951
		fp = I915_READ(FP1(pipe));
5962
		fp = I915_READ(FP1(pipe));
5952
 
5963
 
5953
	clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
5964
	clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
5954
	if (IS_PINEVIEW(dev)) {
5965
	if (IS_PINEVIEW(dev)) {
5955
		clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
5966
		clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
5956
		clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
5967
		clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
5957
	} else {
5968
	} else {
5958
		clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
5969
		clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
5959
		clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
5970
		clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
5960
	}
5971
	}
5961
 
5972
 
5962
	if (!IS_GEN2(dev)) {
5973
	if (!IS_GEN2(dev)) {
5963
		if (IS_PINEVIEW(dev))
5974
		if (IS_PINEVIEW(dev))
5964
			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
5975
			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
5965
				DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
5976
				DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
5966
		else
5977
		else
5967
			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
5978
			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
5968
			       DPLL_FPA01_P1_POST_DIV_SHIFT);
5979
			       DPLL_FPA01_P1_POST_DIV_SHIFT);
5969
 
5980
 
5970
		switch (dpll & DPLL_MODE_MASK) {
5981
		switch (dpll & DPLL_MODE_MASK) {
5971
		case DPLLB_MODE_DAC_SERIAL:
5982
		case DPLLB_MODE_DAC_SERIAL:
5972
			clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
5983
			clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
5973
				5 : 10;
5984
				5 : 10;
5974
			break;
5985
			break;
5975
		case DPLLB_MODE_LVDS:
5986
		case DPLLB_MODE_LVDS:
5976
			clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
5987
			clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
5977
				7 : 14;
5988
				7 : 14;
5978
			break;
5989
			break;
5979
		default:
5990
		default:
5980
			DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
5991
			DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
5981
				  "mode\n", (int)(dpll & DPLL_MODE_MASK));
5992
				  "mode\n", (int)(dpll & DPLL_MODE_MASK));
5982
			return 0;
5993
			return 0;
5983
		}
5994
		}
5984
 
5995
 
5985
		/* XXX: Handle the 100Mhz refclk */
5996
		/* XXX: Handle the 100Mhz refclk */
5986
		intel_clock(dev, 96000, &clock);
5997
		intel_clock(dev, 96000, &clock);
5987
	} else {
5998
	} else {
5988
		bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
5999
		bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
5989
 
6000
 
5990
		if (is_lvds) {
6001
		if (is_lvds) {
5991
			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
6002
			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
5992
				       DPLL_FPA01_P1_POST_DIV_SHIFT);
6003
				       DPLL_FPA01_P1_POST_DIV_SHIFT);
5993
			clock.p2 = 14;
6004
			clock.p2 = 14;
5994
 
6005
 
5995
			if ((dpll & PLL_REF_INPUT_MASK) ==
6006
			if ((dpll & PLL_REF_INPUT_MASK) ==
5996
			    PLLB_REF_INPUT_SPREADSPECTRUMIN) {
6007
			    PLLB_REF_INPUT_SPREADSPECTRUMIN) {
5997
				/* XXX: might not be 66MHz */
6008
				/* XXX: might not be 66MHz */
5998
				intel_clock(dev, 66000, &clock);
6009
				intel_clock(dev, 66000, &clock);
5999
			} else
6010
			} else
6000
				intel_clock(dev, 48000, &clock);
6011
				intel_clock(dev, 48000, &clock);
6001
		} else {
6012
		} else {
6002
			if (dpll & PLL_P1_DIVIDE_BY_TWO)
6013
			if (dpll & PLL_P1_DIVIDE_BY_TWO)
6003
				clock.p1 = 2;
6014
				clock.p1 = 2;
6004
			else {
6015
			else {
6005
				clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
6016
				clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
6006
					    DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
6017
					    DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
6007
			}
6018
			}
6008
			if (dpll & PLL_P2_DIVIDE_BY_4)
6019
			if (dpll & PLL_P2_DIVIDE_BY_4)
6009
				clock.p2 = 4;
6020
				clock.p2 = 4;
6010
			else
6021
			else
6011
				clock.p2 = 2;
6022
				clock.p2 = 2;
6012
 
6023
 
6013
			intel_clock(dev, 48000, &clock);
6024
			intel_clock(dev, 48000, &clock);
6014
		}
6025
		}
6015
	}
6026
	}
6016
 
6027
 
6017
	/* XXX: It would be nice to validate the clocks, but we can't reuse
6028
	/* XXX: It would be nice to validate the clocks, but we can't reuse
6018
	 * i830PllIsValid() because it relies on the xf86_config connector
6029
	 * i830PllIsValid() because it relies on the xf86_config connector
6019
	 * configuration being accurate, which it isn't necessarily.
6030
	 * configuration being accurate, which it isn't necessarily.
6020
	 */
6031
	 */
6021
 
6032
 
6022
	return clock.dot;
6033
	return clock.dot;
6023
}
6034
}
6024
 
6035
 
6025
/** Returns the currently programmed mode of the given pipe. */
6036
/** Returns the currently programmed mode of the given pipe. */
6026
struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
6037
struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
6027
					     struct drm_crtc *crtc)
6038
					     struct drm_crtc *crtc)
6028
{
6039
{
6029
	struct drm_i915_private *dev_priv = dev->dev_private;
6040
	struct drm_i915_private *dev_priv = dev->dev_private;
6030
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6041
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6031
	int pipe = intel_crtc->pipe;
6042
	int pipe = intel_crtc->pipe;
6032
	struct drm_display_mode *mode;
6043
	struct drm_display_mode *mode;
6033
	int htot = I915_READ(HTOTAL(pipe));
6044
	int htot = I915_READ(HTOTAL(pipe));
6034
	int hsync = I915_READ(HSYNC(pipe));
6045
	int hsync = I915_READ(HSYNC(pipe));
6035
	int vtot = I915_READ(VTOTAL(pipe));
6046
	int vtot = I915_READ(VTOTAL(pipe));
6036
	int vsync = I915_READ(VSYNC(pipe));
6047
	int vsync = I915_READ(VSYNC(pipe));
6037
 
6048
 
6038
	mode = kzalloc(sizeof(*mode), GFP_KERNEL);
6049
	mode = kzalloc(sizeof(*mode), GFP_KERNEL);
6039
	if (!mode)
6050
	if (!mode)
6040
		return NULL;
6051
		return NULL;
6041
 
6052
 
6042
	mode->clock = intel_crtc_clock_get(dev, crtc);
6053
	mode->clock = intel_crtc_clock_get(dev, crtc);
6043
	mode->hdisplay = (htot & 0xffff) + 1;
6054
	mode->hdisplay = (htot & 0xffff) + 1;
6044
	mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
6055
	mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
6045
	mode->hsync_start = (hsync & 0xffff) + 1;
6056
	mode->hsync_start = (hsync & 0xffff) + 1;
6046
	mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
6057
	mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
6047
	mode->vdisplay = (vtot & 0xffff) + 1;
6058
	mode->vdisplay = (vtot & 0xffff) + 1;
6048
	mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
6059
	mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
6049
	mode->vsync_start = (vsync & 0xffff) + 1;
6060
	mode->vsync_start = (vsync & 0xffff) + 1;
6050
	mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
6061
	mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
6051
 
6062
 
6052
	drm_mode_set_name(mode);
6063
	drm_mode_set_name(mode);
6053
 
6064
 
6054
	return mode;
6065
	return mode;
6055
}
6066
}
6056
 
6067
 
6057
static void intel_increase_pllclock(struct drm_crtc *crtc)
6068
static void intel_increase_pllclock(struct drm_crtc *crtc)
6058
{
6069
{
6059
	struct drm_device *dev = crtc->dev;
6070
	struct drm_device *dev = crtc->dev;
6060
	drm_i915_private_t *dev_priv = dev->dev_private;
6071
	drm_i915_private_t *dev_priv = dev->dev_private;
6061
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6072
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6062
	int pipe = intel_crtc->pipe;
6073
	int pipe = intel_crtc->pipe;
6063
	int dpll_reg = DPLL(pipe);
6074
	int dpll_reg = DPLL(pipe);
6064
	int dpll;
6075
	int dpll;
6065
 
6076
 
6066
	if (HAS_PCH_SPLIT(dev))
6077
	if (HAS_PCH_SPLIT(dev))
6067
		return;
6078
		return;
6068
 
6079
 
6069
	if (!dev_priv->lvds_downclock_avail)
6080
	if (!dev_priv->lvds_downclock_avail)
6070
		return;
6081
		return;
6071
 
6082
 
6072
	dpll = I915_READ(dpll_reg);
6083
	dpll = I915_READ(dpll_reg);
6073
	if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
6084
	if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
6074
		DRM_DEBUG_DRIVER("upclocking LVDS\n");
6085
		DRM_DEBUG_DRIVER("upclocking LVDS\n");
6075
 
6086
 
6076
		assert_panel_unlocked(dev_priv, pipe);
6087
		assert_panel_unlocked(dev_priv, pipe);
6077
 
6088
 
6078
		dpll &= ~DISPLAY_RATE_SELECT_FPA1;
6089
		dpll &= ~DISPLAY_RATE_SELECT_FPA1;
6079
		I915_WRITE(dpll_reg, dpll);
6090
		I915_WRITE(dpll_reg, dpll);
6080
		intel_wait_for_vblank(dev, pipe);
6091
		intel_wait_for_vblank(dev, pipe);
6081
 
6092
 
6082
		dpll = I915_READ(dpll_reg);
6093
		dpll = I915_READ(dpll_reg);
6083
		if (dpll & DISPLAY_RATE_SELECT_FPA1)
6094
		if (dpll & DISPLAY_RATE_SELECT_FPA1)
6084
			DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
6095
			DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
6085
	}
6096
	}
6086
}
6097
}
6087
 
6098
 
6088
static void intel_decrease_pllclock(struct drm_crtc *crtc)
6099
static void intel_decrease_pllclock(struct drm_crtc *crtc)
6089
{
6100
{
6090
	struct drm_device *dev = crtc->dev;
6101
	struct drm_device *dev = crtc->dev;
6091
	drm_i915_private_t *dev_priv = dev->dev_private;
6102
	drm_i915_private_t *dev_priv = dev->dev_private;
6092
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6103
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6093
 
6104
 
6094
	if (HAS_PCH_SPLIT(dev))
6105
	if (HAS_PCH_SPLIT(dev))
6095
		return;
6106
		return;
6096
 
6107
 
6097
	if (!dev_priv->lvds_downclock_avail)
6108
	if (!dev_priv->lvds_downclock_avail)
6098
		return;
6109
		return;
6099
 
6110
 
6100
	/*
6111
	/*
6101
	 * Since this is called by a timer, we should never get here in
6112
	 * Since this is called by a timer, we should never get here in
6102
	 * the manual case.
6113
	 * the manual case.
6103
	 */
6114
	 */
6104
	if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
6115
	if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
6105
		int pipe = intel_crtc->pipe;
6116
		int pipe = intel_crtc->pipe;
6106
		int dpll_reg = DPLL(pipe);
6117
		int dpll_reg = DPLL(pipe);
6107
		int dpll;
6118
		int dpll;
6108
 
6119
 
6109
		DRM_DEBUG_DRIVER("downclocking LVDS\n");
6120
		DRM_DEBUG_DRIVER("downclocking LVDS\n");
6110
 
6121
 
6111
		assert_panel_unlocked(dev_priv, pipe);
6122
		assert_panel_unlocked(dev_priv, pipe);
6112
 
6123
 
6113
		dpll = I915_READ(dpll_reg);
6124
		dpll = I915_READ(dpll_reg);
6114
		dpll |= DISPLAY_RATE_SELECT_FPA1;
6125
		dpll |= DISPLAY_RATE_SELECT_FPA1;
6115
		I915_WRITE(dpll_reg, dpll);
6126
		I915_WRITE(dpll_reg, dpll);
6116
		intel_wait_for_vblank(dev, pipe);
6127
		intel_wait_for_vblank(dev, pipe);
6117
		dpll = I915_READ(dpll_reg);
6128
		dpll = I915_READ(dpll_reg);
6118
		if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
6129
		if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
6119
			DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
6130
			DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
6120
	}
6131
	}
6121
 
6132
 
6122
}
6133
}
6123
 
6134
 
6124
void intel_mark_busy(struct drm_device *dev)
6135
void intel_mark_busy(struct drm_device *dev)
6125
{
6136
{
6126
	i915_update_gfx_val(dev->dev_private);
6137
	i915_update_gfx_val(dev->dev_private);
6127
}
6138
}
6128
 
6139
 
6129
void intel_mark_idle(struct drm_device *dev)
6140
void intel_mark_idle(struct drm_device *dev)
6130
{
6141
{
6131
}
6142
}
6132
 
6143
 
6133
void intel_mark_fb_busy(struct drm_i915_gem_object *obj)
6144
void intel_mark_fb_busy(struct drm_i915_gem_object *obj)
6134
{
6145
{
6135
	struct drm_device *dev = obj->base.dev;
6146
	struct drm_device *dev = obj->base.dev;
6136
	struct drm_crtc *crtc;
6147
	struct drm_crtc *crtc;
6137
 
6148
 
6138
	if (!i915_powersave)
6149
	if (!i915_powersave)
6139
		return;
6150
		return;
6140
 
6151
 
6141
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
6152
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
6142
		if (!crtc->fb)
6153
		if (!crtc->fb)
6143
			continue;
6154
			continue;
6144
 
6155
 
6145
		if (to_intel_framebuffer(crtc->fb)->obj == obj)
6156
		if (to_intel_framebuffer(crtc->fb)->obj == obj)
6146
			intel_increase_pllclock(crtc);
6157
			intel_increase_pllclock(crtc);
6147
	}
6158
	}
6148
}
6159
}
6149
 
6160
 
6150
void intel_mark_fb_idle(struct drm_i915_gem_object *obj)
6161
void intel_mark_fb_idle(struct drm_i915_gem_object *obj)
6151
{
6162
{
6152
	struct drm_device *dev = obj->base.dev;
6163
	struct drm_device *dev = obj->base.dev;
6153
	struct drm_crtc *crtc;
6164
	struct drm_crtc *crtc;
6154
 
6165
 
6155
	if (!i915_powersave)
6166
	if (!i915_powersave)
6156
		return;
6167
		return;
6157
 
6168
 
6158
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
6169
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
6159
		if (!crtc->fb)
6170
		if (!crtc->fb)
6160
			continue;
6171
			continue;
6161
 
6172
 
6162
		if (to_intel_framebuffer(crtc->fb)->obj == obj)
6173
		if (to_intel_framebuffer(crtc->fb)->obj == obj)
6163
			intel_decrease_pllclock(crtc);
6174
			intel_decrease_pllclock(crtc);
6164
	}
6175
	}
6165
}
6176
}
6166
 
6177
 
6167
static void intel_crtc_destroy(struct drm_crtc *crtc)
6178
static void intel_crtc_destroy(struct drm_crtc *crtc)
6168
{
6179
{
6169
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6180
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6170
	struct drm_device *dev = crtc->dev;
6181
	struct drm_device *dev = crtc->dev;
6171
	struct intel_unpin_work *work;
6182
	struct intel_unpin_work *work;
6172
	unsigned long flags;
6183
	unsigned long flags;
6173
 
6184
 
6174
	spin_lock_irqsave(&dev->event_lock, flags);
6185
	spin_lock_irqsave(&dev->event_lock, flags);
6175
	work = intel_crtc->unpin_work;
6186
	work = intel_crtc->unpin_work;
6176
	intel_crtc->unpin_work = NULL;
6187
	intel_crtc->unpin_work = NULL;
6177
	spin_unlock_irqrestore(&dev->event_lock, flags);
6188
	spin_unlock_irqrestore(&dev->event_lock, flags);
6178
 
6189
 
6179
	if (work) {
6190
	if (work) {
6180
//		cancel_work_sync(&work->work);
6191
//		cancel_work_sync(&work->work);
6181
		kfree(work);
6192
		kfree(work);
6182
	}
6193
	}
6183
 
6194
 
6184
	drm_crtc_cleanup(crtc);
6195
	drm_crtc_cleanup(crtc);
6185
 
6196
 
6186
	kfree(intel_crtc);
6197
	kfree(intel_crtc);
6187
}
6198
}
6188
 
6199
 
6189
#if 0
6200
#if 0
6190
static void intel_unpin_work_fn(struct work_struct *__work)
6201
static void intel_unpin_work_fn(struct work_struct *__work)
6191
{
6202
{
6192
	struct intel_unpin_work *work =
6203
	struct intel_unpin_work *work =
6193
		container_of(__work, struct intel_unpin_work, work);
6204
		container_of(__work, struct intel_unpin_work, work);
6194
 
6205
 
6195
	mutex_lock(&work->dev->struct_mutex);
6206
	mutex_lock(&work->dev->struct_mutex);
6196
	intel_unpin_fb_obj(work->old_fb_obj);
6207
	intel_unpin_fb_obj(work->old_fb_obj);
6197
	drm_gem_object_unreference(&work->pending_flip_obj->base);
6208
	drm_gem_object_unreference(&work->pending_flip_obj->base);
6198
	drm_gem_object_unreference(&work->old_fb_obj->base);
6209
	drm_gem_object_unreference(&work->old_fb_obj->base);
6199
 
6210
 
6200
	intel_update_fbc(work->dev);
6211
	intel_update_fbc(work->dev);
6201
	mutex_unlock(&work->dev->struct_mutex);
6212
	mutex_unlock(&work->dev->struct_mutex);
6202
	kfree(work);
6213
	kfree(work);
6203
}
6214
}
6204
 
6215
 
6205
static void do_intel_finish_page_flip(struct drm_device *dev,
6216
static void do_intel_finish_page_flip(struct drm_device *dev,
6206
				      struct drm_crtc *crtc)
6217
				      struct drm_crtc *crtc)
6207
{
6218
{
6208
	drm_i915_private_t *dev_priv = dev->dev_private;
6219
	drm_i915_private_t *dev_priv = dev->dev_private;
6209
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6220
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6210
	struct intel_unpin_work *work;
6221
	struct intel_unpin_work *work;
6211
	struct drm_i915_gem_object *obj;
6222
	struct drm_i915_gem_object *obj;
6212
	struct drm_pending_vblank_event *e;
6223
	struct drm_pending_vblank_event *e;
6213
	struct timeval tvbl;
6224
	struct timeval tvbl;
6214
	unsigned long flags;
6225
	unsigned long flags;
6215
 
6226
 
6216
	/* Ignore early vblank irqs */
6227
	/* Ignore early vblank irqs */
6217
	if (intel_crtc == NULL)
6228
	if (intel_crtc == NULL)
6218
		return;
6229
		return;
6219
 
6230
 
6220
	spin_lock_irqsave(&dev->event_lock, flags);
6231
	spin_lock_irqsave(&dev->event_lock, flags);
6221
	work = intel_crtc->unpin_work;
6232
	work = intel_crtc->unpin_work;
6222
	if (work == NULL || !work->pending) {
6233
	if (work == NULL || !work->pending) {
6223
		spin_unlock_irqrestore(&dev->event_lock, flags);
6234
		spin_unlock_irqrestore(&dev->event_lock, flags);
6224
		return;
6235
		return;
6225
	}
6236
	}
6226
 
6237
 
6227
	intel_crtc->unpin_work = NULL;
6238
	intel_crtc->unpin_work = NULL;
6228
 
6239
 
6229
	if (work->event) {
6240
	if (work->event) {
6230
		e = work->event;
6241
		e = work->event;
6231
		e->event.sequence = drm_vblank_count_and_time(dev, intel_crtc->pipe, &tvbl);
6242
		e->event.sequence = drm_vblank_count_and_time(dev, intel_crtc->pipe, &tvbl);
6232
 
6243
 
6233
		e->event.tv_sec = tvbl.tv_sec;
6244
		e->event.tv_sec = tvbl.tv_sec;
6234
		e->event.tv_usec = tvbl.tv_usec;
6245
		e->event.tv_usec = tvbl.tv_usec;
6235
 
6246
 
6236
		list_add_tail(&e->base.link,
6247
		list_add_tail(&e->base.link,
6237
			      &e->base.file_priv->event_list);
6248
			      &e->base.file_priv->event_list);
6238
		wake_up_interruptible(&e->base.file_priv->event_wait);
6249
		wake_up_interruptible(&e->base.file_priv->event_wait);
6239
	}
6250
	}
6240
 
6251
 
6241
	drm_vblank_put(dev, intel_crtc->pipe);
6252
	drm_vblank_put(dev, intel_crtc->pipe);
6242
 
6253
 
6243
	spin_unlock_irqrestore(&dev->event_lock, flags);
6254
	spin_unlock_irqrestore(&dev->event_lock, flags);
6244
 
6255
 
6245
	obj = work->old_fb_obj;
6256
	obj = work->old_fb_obj;
6246
 
6257
 
6247
	atomic_clear_mask(1 << intel_crtc->plane,
6258
	atomic_clear_mask(1 << intel_crtc->plane,
6248
			  &obj->pending_flip.counter);
6259
			  &obj->pending_flip.counter);
6249
 
6260
 
6250
	wake_up(&dev_priv->pending_flip_queue);
6261
	wake_up(&dev_priv->pending_flip_queue);
6251
	schedule_work(&work->work);
6262
	schedule_work(&work->work);
6252
 
6263
 
6253
	trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
6264
	trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
6254
}
6265
}
6255
 
6266
 
6256
void intel_finish_page_flip(struct drm_device *dev, int pipe)
6267
void intel_finish_page_flip(struct drm_device *dev, int pipe)
6257
{
6268
{
6258
	drm_i915_private_t *dev_priv = dev->dev_private;
6269
	drm_i915_private_t *dev_priv = dev->dev_private;
6259
	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
6270
	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
6260
 
6271
 
6261
	do_intel_finish_page_flip(dev, crtc);
6272
	do_intel_finish_page_flip(dev, crtc);
6262
}
6273
}
6263
 
6274
 
6264
void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
6275
void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
6265
{
6276
{
6266
	drm_i915_private_t *dev_priv = dev->dev_private;
6277
	drm_i915_private_t *dev_priv = dev->dev_private;
6267
	struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
6278
	struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
6268
 
6279
 
6269
	do_intel_finish_page_flip(dev, crtc);
6280
	do_intel_finish_page_flip(dev, crtc);
6270
}
6281
}
6271
 
6282
 
6272
void intel_prepare_page_flip(struct drm_device *dev, int plane)
6283
void intel_prepare_page_flip(struct drm_device *dev, int plane)
6273
{
6284
{
6274
	drm_i915_private_t *dev_priv = dev->dev_private;
6285
	drm_i915_private_t *dev_priv = dev->dev_private;
6275
	struct intel_crtc *intel_crtc =
6286
	struct intel_crtc *intel_crtc =
6276
		to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
6287
		to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
6277
	unsigned long flags;
6288
	unsigned long flags;
6278
 
6289
 
6279
	spin_lock_irqsave(&dev->event_lock, flags);
6290
	spin_lock_irqsave(&dev->event_lock, flags);
6280
	if (intel_crtc->unpin_work) {
6291
	if (intel_crtc->unpin_work) {
6281
		if ((++intel_crtc->unpin_work->pending) > 1)
6292
		if ((++intel_crtc->unpin_work->pending) > 1)
6282
			DRM_ERROR("Prepared flip multiple times\n");
6293
			DRM_ERROR("Prepared flip multiple times\n");
6283
	} else {
6294
	} else {
6284
		DRM_DEBUG_DRIVER("preparing flip with no unpin work?\n");
6295
		DRM_DEBUG_DRIVER("preparing flip with no unpin work?\n");
6285
	}
6296
	}
6286
	spin_unlock_irqrestore(&dev->event_lock, flags);
6297
	spin_unlock_irqrestore(&dev->event_lock, flags);
6287
}
6298
}
6288
 
6299
 
6289
static int intel_gen2_queue_flip(struct drm_device *dev,
6300
static int intel_gen2_queue_flip(struct drm_device *dev,
6290
				 struct drm_crtc *crtc,
6301
				 struct drm_crtc *crtc,
6291
				 struct drm_framebuffer *fb,
6302
				 struct drm_framebuffer *fb,
6292
				 struct drm_i915_gem_object *obj)
6303
				 struct drm_i915_gem_object *obj)
6293
{
6304
{
6294
	struct drm_i915_private *dev_priv = dev->dev_private;
6305
	struct drm_i915_private *dev_priv = dev->dev_private;
6295
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6306
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6296
	u32 flip_mask;
6307
	u32 flip_mask;
6297
	struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
6308
	struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
6298
	int ret;
6309
	int ret;
6299
 
6310
 
6300
	ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
6311
	ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
6301
	if (ret)
6312
	if (ret)
6302
		goto err;
6313
		goto err;
6303
 
6314
 
6304
	ret = intel_ring_begin(ring, 6);
6315
	ret = intel_ring_begin(ring, 6);
6305
	if (ret)
6316
	if (ret)
6306
		goto err_unpin;
6317
		goto err_unpin;
6307
 
6318
 
6308
	/* Can't queue multiple flips, so wait for the previous
6319
	/* Can't queue multiple flips, so wait for the previous
6309
	 * one to finish before executing the next.
6320
	 * one to finish before executing the next.
6310
	 */
6321
	 */
6311
	if (intel_crtc->plane)
6322
	if (intel_crtc->plane)
6312
		flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
6323
		flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
6313
	else
6324
	else
6314
		flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6325
		flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6315
	intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
6326
	intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
6316
	intel_ring_emit(ring, MI_NOOP);
6327
	intel_ring_emit(ring, MI_NOOP);
6317
	intel_ring_emit(ring, MI_DISPLAY_FLIP |
6328
	intel_ring_emit(ring, MI_DISPLAY_FLIP |
6318
			MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
6329
			MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
6319
	intel_ring_emit(ring, fb->pitches[0]);
6330
	intel_ring_emit(ring, fb->pitches[0]);
6320
	intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
6331
	intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
6321
	intel_ring_emit(ring, 0); /* aux display base address, unused */
6332
	intel_ring_emit(ring, 0); /* aux display base address, unused */
6322
	intel_ring_advance(ring);
6333
	intel_ring_advance(ring);
6323
	return 0;
6334
	return 0;
6324
 
6335
 
6325
err_unpin:
6336
err_unpin:
6326
	intel_unpin_fb_obj(obj);
6337
	intel_unpin_fb_obj(obj);
6327
err:
6338
err:
6328
	return ret;
6339
	return ret;
6329
}
6340
}
6330
 
6341
 
6331
static int intel_gen3_queue_flip(struct drm_device *dev,
6342
static int intel_gen3_queue_flip(struct drm_device *dev,
6332
				 struct drm_crtc *crtc,
6343
				 struct drm_crtc *crtc,
6333
				 struct drm_framebuffer *fb,
6344
				 struct drm_framebuffer *fb,
6334
				 struct drm_i915_gem_object *obj)
6345
				 struct drm_i915_gem_object *obj)
6335
{
6346
{
6336
	struct drm_i915_private *dev_priv = dev->dev_private;
6347
	struct drm_i915_private *dev_priv = dev->dev_private;
6337
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6348
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6338
	u32 flip_mask;
6349
	u32 flip_mask;
6339
	struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
6350
	struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
6340
	int ret;
6351
	int ret;
6341
 
6352
 
6342
	ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
6353
	ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
6343
	if (ret)
6354
	if (ret)
6344
		goto err;
6355
		goto err;
6345
 
6356
 
6346
	ret = intel_ring_begin(ring, 6);
6357
	ret = intel_ring_begin(ring, 6);
6347
	if (ret)
6358
	if (ret)
6348
		goto err_unpin;
6359
		goto err_unpin;
6349
 
6360
 
6350
	if (intel_crtc->plane)
6361
	if (intel_crtc->plane)
6351
		flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
6362
		flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
6352
	else
6363
	else
6353
		flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6364
		flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6354
	intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
6365
	intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
6355
	intel_ring_emit(ring, MI_NOOP);
6366
	intel_ring_emit(ring, MI_NOOP);
6356
	intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
6367
	intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
6357
			MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
6368
			MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
6358
	intel_ring_emit(ring, fb->pitches[0]);
6369
	intel_ring_emit(ring, fb->pitches[0]);
6359
	intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
6370
	intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
6360
	intel_ring_emit(ring, MI_NOOP);
6371
	intel_ring_emit(ring, MI_NOOP);
6361
 
6372
 
6362
	intel_ring_advance(ring);
6373
	intel_ring_advance(ring);
6363
	return 0;
6374
	return 0;
6364
 
6375
 
6365
err_unpin:
6376
err_unpin:
6366
	intel_unpin_fb_obj(obj);
6377
	intel_unpin_fb_obj(obj);
6367
err:
6378
err:
6368
	return ret;
6379
	return ret;
6369
}
6380
}
6370
 
6381
 
6371
static int intel_gen4_queue_flip(struct drm_device *dev,
6382
static int intel_gen4_queue_flip(struct drm_device *dev,
6372
				 struct drm_crtc *crtc,
6383
				 struct drm_crtc *crtc,
6373
				 struct drm_framebuffer *fb,
6384
				 struct drm_framebuffer *fb,
6374
				 struct drm_i915_gem_object *obj)
6385
				 struct drm_i915_gem_object *obj)
6375
{
6386
{
6376
	struct drm_i915_private *dev_priv = dev->dev_private;
6387
	struct drm_i915_private *dev_priv = dev->dev_private;
6377
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6388
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6378
	uint32_t pf, pipesrc;
6389
	uint32_t pf, pipesrc;
6379
	struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
6390
	struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
6380
	int ret;
6391
	int ret;
6381
 
6392
 
6382
	ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
6393
	ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
6383
	if (ret)
6394
	if (ret)
6384
		goto err;
6395
		goto err;
6385
 
6396
 
6386
	ret = intel_ring_begin(ring, 4);
6397
	ret = intel_ring_begin(ring, 4);
6387
	if (ret)
6398
	if (ret)
6388
		goto err_unpin;
6399
		goto err_unpin;
6389
 
6400
 
6390
	/* i965+ uses the linear or tiled offsets from the
6401
	/* i965+ uses the linear or tiled offsets from the
6391
	 * Display Registers (which do not change across a page-flip)
6402
	 * Display Registers (which do not change across a page-flip)
6392
	 * so we need only reprogram the base address.
6403
	 * so we need only reprogram the base address.
6393
	 */
6404
	 */
6394
	intel_ring_emit(ring, MI_DISPLAY_FLIP |
6405
	intel_ring_emit(ring, MI_DISPLAY_FLIP |
6395
			MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
6406
			MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
6396
	intel_ring_emit(ring, fb->pitches[0]);
6407
	intel_ring_emit(ring, fb->pitches[0]);
6397
	intel_ring_emit(ring,
6408
	intel_ring_emit(ring,
6398
			(obj->gtt_offset + intel_crtc->dspaddr_offset) |
6409
			(obj->gtt_offset + intel_crtc->dspaddr_offset) |
6399
			obj->tiling_mode);
6410
			obj->tiling_mode);
6400
 
6411
 
6401
	/* XXX Enabling the panel-fitter across page-flip is so far
6412
	/* XXX Enabling the panel-fitter across page-flip is so far
6402
	 * untested on non-native modes, so ignore it for now.
6413
	 * untested on non-native modes, so ignore it for now.
6403
	 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
6414
	 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
6404
	 */
6415
	 */
6405
	pf = 0;
6416
	pf = 0;
6406
	pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6417
	pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6407
	intel_ring_emit(ring, pf | pipesrc);
6418
	intel_ring_emit(ring, pf | pipesrc);
6408
	intel_ring_advance(ring);
6419
	intel_ring_advance(ring);
6409
	return 0;
6420
	return 0;
6410
 
6421
 
6411
err_unpin:
6422
err_unpin:
6412
	intel_unpin_fb_obj(obj);
6423
	intel_unpin_fb_obj(obj);
6413
err:
6424
err:
6414
	return ret;
6425
	return ret;
6415
}
6426
}
6416
 
6427
 
6417
static int intel_gen6_queue_flip(struct drm_device *dev,
6428
static int intel_gen6_queue_flip(struct drm_device *dev,
6418
				 struct drm_crtc *crtc,
6429
				 struct drm_crtc *crtc,
6419
				 struct drm_framebuffer *fb,
6430
				 struct drm_framebuffer *fb,
6420
				 struct drm_i915_gem_object *obj)
6431
				 struct drm_i915_gem_object *obj)
6421
{
6432
{
6422
	struct drm_i915_private *dev_priv = dev->dev_private;
6433
	struct drm_i915_private *dev_priv = dev->dev_private;
6423
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6434
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6424
	struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
6435
	struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
6425
	uint32_t pf, pipesrc;
6436
	uint32_t pf, pipesrc;
6426
	int ret;
6437
	int ret;
6427
 
6438
 
6428
	ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
6439
	ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
6429
	if (ret)
6440
	if (ret)
6430
		goto err;
6441
		goto err;
6431
 
6442
 
6432
	ret = intel_ring_begin(ring, 4);
6443
	ret = intel_ring_begin(ring, 4);
6433
	if (ret)
6444
	if (ret)
6434
		goto err_unpin;
6445
		goto err_unpin;
6435
 
6446
 
6436
	intel_ring_emit(ring, MI_DISPLAY_FLIP |
6447
	intel_ring_emit(ring, MI_DISPLAY_FLIP |
6437
			MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
6448
			MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
6438
	intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
6449
	intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
6439
	intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
6450
	intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
6440
 
6451
 
6441
	/* Contrary to the suggestions in the documentation,
6452
	/* Contrary to the suggestions in the documentation,
6442
	 * "Enable Panel Fitter" does not seem to be required when page
6453
	 * "Enable Panel Fitter" does not seem to be required when page
6443
	 * flipping with a non-native mode, and worse causes a normal
6454
	 * flipping with a non-native mode, and worse causes a normal
6444
	 * modeset to fail.
6455
	 * modeset to fail.
6445
	 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
6456
	 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
6446
	 */
6457
	 */
6447
	pf = 0;
6458
	pf = 0;
6448
	pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6459
	pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6449
	intel_ring_emit(ring, pf | pipesrc);
6460
	intel_ring_emit(ring, pf | pipesrc);
6450
	intel_ring_advance(ring);
6461
	intel_ring_advance(ring);
6451
	return 0;
6462
	return 0;
6452
 
6463
 
6453
err_unpin:
6464
err_unpin:
6454
	intel_unpin_fb_obj(obj);
6465
	intel_unpin_fb_obj(obj);
6455
err:
6466
err:
6456
	return ret;
6467
	return ret;
6457
}
6468
}
6458
 
6469
 
6459
/*
6470
/*
6460
 * On gen7 we currently use the blit ring because (in early silicon at least)
6471
 * On gen7 we currently use the blit ring because (in early silicon at least)
6461
 * the render ring doesn't give us interrpts for page flip completion, which
6472
 * the render ring doesn't give us interrpts for page flip completion, which
6462
 * means clients will hang after the first flip is queued.  Fortunately the
6473
 * means clients will hang after the first flip is queued.  Fortunately the
6463
 * blit ring generates interrupts properly, so use it instead.
6474
 * blit ring generates interrupts properly, so use it instead.
6464
 */
6475
 */
6465
static int intel_gen7_queue_flip(struct drm_device *dev,
6476
static int intel_gen7_queue_flip(struct drm_device *dev,
6466
				 struct drm_crtc *crtc,
6477
				 struct drm_crtc *crtc,
6467
				 struct drm_framebuffer *fb,
6478
				 struct drm_framebuffer *fb,
6468
				 struct drm_i915_gem_object *obj)
6479
				 struct drm_i915_gem_object *obj)
6469
{
6480
{
6470
	struct drm_i915_private *dev_priv = dev->dev_private;
6481
	struct drm_i915_private *dev_priv = dev->dev_private;
6471
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6482
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6472
	struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
6483
	struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
6473
	uint32_t plane_bit = 0;
6484
	uint32_t plane_bit = 0;
6474
	int ret;
6485
	int ret;
6475
 
6486
 
6476
	ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
6487
	ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
6477
	if (ret)
6488
	if (ret)
6478
		goto err;
6489
		goto err;
6479
 
6490
 
6480
	switch(intel_crtc->plane) {
6491
	switch(intel_crtc->plane) {
6481
	case PLANE_A:
6492
	case PLANE_A:
6482
		plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
6493
		plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
6483
		break;
6494
		break;
6484
	case PLANE_B:
6495
	case PLANE_B:
6485
		plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
6496
		plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
6486
		break;
6497
		break;
6487
	case PLANE_C:
6498
	case PLANE_C:
6488
		plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
6499
		plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
6489
		break;
6500
		break;
6490
	default:
6501
	default:
6491
		WARN_ONCE(1, "unknown plane in flip command\n");
6502
		WARN_ONCE(1, "unknown plane in flip command\n");
6492
		ret = -ENODEV;
6503
		ret = -ENODEV;
6493
		goto err_unpin;
6504
		goto err_unpin;
6494
	}
6505
	}
6495
 
6506
 
6496
	ret = intel_ring_begin(ring, 4);
6507
	ret = intel_ring_begin(ring, 4);
6497
	if (ret)
6508
	if (ret)
6498
		goto err_unpin;
6509
		goto err_unpin;
6499
 
6510
 
6500
	intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
6511
	intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
6501
	intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
6512
	intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
6502
	intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
6513
	intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
6503
	intel_ring_emit(ring, (MI_NOOP));
6514
	intel_ring_emit(ring, (MI_NOOP));
6504
	intel_ring_advance(ring);
6515
	intel_ring_advance(ring);
6505
	return 0;
6516
	return 0;
6506
 
6517
 
6507
err_unpin:
6518
err_unpin:
6508
	intel_unpin_fb_obj(obj);
6519
	intel_unpin_fb_obj(obj);
6509
err:
6520
err:
6510
	return ret;
6521
	return ret;
6511
}
6522
}
6512
 
6523
 
6513
static int intel_default_queue_flip(struct drm_device *dev,
6524
static int intel_default_queue_flip(struct drm_device *dev,
6514
				    struct drm_crtc *crtc,
6525
				    struct drm_crtc *crtc,
6515
				    struct drm_framebuffer *fb,
6526
				    struct drm_framebuffer *fb,
6516
				    struct drm_i915_gem_object *obj)
6527
				    struct drm_i915_gem_object *obj)
6517
{
6528
{
6518
	return -ENODEV;
6529
	return -ENODEV;
6519
}
6530
}
6520
 
6531
 
6521
static int intel_crtc_page_flip(struct drm_crtc *crtc,
6532
static int intel_crtc_page_flip(struct drm_crtc *crtc,
6522
				struct drm_framebuffer *fb,
6533
				struct drm_framebuffer *fb,
6523
				struct drm_pending_vblank_event *event)
6534
				struct drm_pending_vblank_event *event)
6524
{
6535
{
6525
	struct drm_device *dev = crtc->dev;
6536
	struct drm_device *dev = crtc->dev;
6526
	struct drm_i915_private *dev_priv = dev->dev_private;
6537
	struct drm_i915_private *dev_priv = dev->dev_private;
6527
	struct intel_framebuffer *intel_fb;
6538
	struct intel_framebuffer *intel_fb;
6528
	struct drm_i915_gem_object *obj;
6539
	struct drm_i915_gem_object *obj;
6529
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6540
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6530
	struct intel_unpin_work *work;
6541
	struct intel_unpin_work *work;
6531
	unsigned long flags;
6542
	unsigned long flags;
6532
	int ret;
6543
	int ret;
6533
 
6544
 
6534
	/* Can't change pixel format via MI display flips. */
6545
	/* Can't change pixel format via MI display flips. */
6535
	if (fb->pixel_format != crtc->fb->pixel_format)
6546
	if (fb->pixel_format != crtc->fb->pixel_format)
6536
		return -EINVAL;
6547
		return -EINVAL;
6537
 
6548
 
6538
	/*
6549
	/*
6539
	 * TILEOFF/LINOFF registers can't be changed via MI display flips.
6550
	 * TILEOFF/LINOFF registers can't be changed via MI display flips.
6540
	 * Note that pitch changes could also affect these register.
6551
	 * Note that pitch changes could also affect these register.
6541
	 */
6552
	 */
6542
	if (INTEL_INFO(dev)->gen > 3 &&
6553
	if (INTEL_INFO(dev)->gen > 3 &&
6543
	    (fb->offsets[0] != crtc->fb->offsets[0] ||
6554
	    (fb->offsets[0] != crtc->fb->offsets[0] ||
6544
	     fb->pitches[0] != crtc->fb->pitches[0]))
6555
	     fb->pitches[0] != crtc->fb->pitches[0]))
6545
		return -EINVAL;
6556
		return -EINVAL;
6546
 
6557
 
6547
	work = kzalloc(sizeof *work, GFP_KERNEL);
6558
	work = kzalloc(sizeof *work, GFP_KERNEL);
6548
	if (work == NULL)
6559
	if (work == NULL)
6549
		return -ENOMEM;
6560
		return -ENOMEM;
6550
 
6561
 
6551
	work->event = event;
6562
	work->event = event;
6552
	work->dev = crtc->dev;
6563
	work->dev = crtc->dev;
6553
	intel_fb = to_intel_framebuffer(crtc->fb);
6564
	intel_fb = to_intel_framebuffer(crtc->fb);
6554
	work->old_fb_obj = intel_fb->obj;
6565
	work->old_fb_obj = intel_fb->obj;
6555
	INIT_WORK(&work->work, intel_unpin_work_fn);
6566
	INIT_WORK(&work->work, intel_unpin_work_fn);
6556
 
6567
 
6557
	ret = drm_vblank_get(dev, intel_crtc->pipe);
6568
	ret = drm_vblank_get(dev, intel_crtc->pipe);
6558
	if (ret)
6569
	if (ret)
6559
		goto free_work;
6570
		goto free_work;
6560
 
6571
 
6561
	/* We borrow the event spin lock for protecting unpin_work */
6572
	/* We borrow the event spin lock for protecting unpin_work */
6562
	spin_lock_irqsave(&dev->event_lock, flags);
6573
	spin_lock_irqsave(&dev->event_lock, flags);
6563
	if (intel_crtc->unpin_work) {
6574
	if (intel_crtc->unpin_work) {
6564
		spin_unlock_irqrestore(&dev->event_lock, flags);
6575
		spin_unlock_irqrestore(&dev->event_lock, flags);
6565
		kfree(work);
6576
		kfree(work);
6566
		drm_vblank_put(dev, intel_crtc->pipe);
6577
		drm_vblank_put(dev, intel_crtc->pipe);
6567
 
6578
 
6568
		DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
6579
		DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
6569
		return -EBUSY;
6580
		return -EBUSY;
6570
	}
6581
	}
6571
	intel_crtc->unpin_work = work;
6582
	intel_crtc->unpin_work = work;
6572
	spin_unlock_irqrestore(&dev->event_lock, flags);
6583
	spin_unlock_irqrestore(&dev->event_lock, flags);
6573
 
6584
 
6574
	intel_fb = to_intel_framebuffer(fb);
6585
	intel_fb = to_intel_framebuffer(fb);
6575
	obj = intel_fb->obj;
6586
	obj = intel_fb->obj;
6576
 
6587
 
6577
	ret = i915_mutex_lock_interruptible(dev);
6588
	ret = i915_mutex_lock_interruptible(dev);
6578
	if (ret)
6589
	if (ret)
6579
		goto cleanup;
6590
		goto cleanup;
6580
 
6591
 
6581
	/* Reference the objects for the scheduled work. */
6592
	/* Reference the objects for the scheduled work. */
6582
	drm_gem_object_reference(&work->old_fb_obj->base);
6593
	drm_gem_object_reference(&work->old_fb_obj->base);
6583
	drm_gem_object_reference(&obj->base);
6594
	drm_gem_object_reference(&obj->base);
6584
 
6595
 
6585
	crtc->fb = fb;
6596
	crtc->fb = fb;
6586
 
6597
 
6587
	work->pending_flip_obj = obj;
6598
	work->pending_flip_obj = obj;
6588
 
6599
 
6589
	work->enable_stall_check = true;
6600
	work->enable_stall_check = true;
6590
 
6601
 
6591
	/* Block clients from rendering to the new back buffer until
6602
	/* Block clients from rendering to the new back buffer until
6592
	 * the flip occurs and the object is no longer visible.
6603
	 * the flip occurs and the object is no longer visible.
6593
	 */
6604
	 */
6594
	atomic_add(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
6605
	atomic_add(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
6595
 
6606
 
6596
	ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
6607
	ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
6597
	if (ret)
6608
	if (ret)
6598
		goto cleanup_pending;
6609
		goto cleanup_pending;
6599
 
6610
 
6600
	intel_disable_fbc(dev);
6611
	intel_disable_fbc(dev);
6601
	intel_mark_fb_busy(obj);
6612
	intel_mark_fb_busy(obj);
6602
	mutex_unlock(&dev->struct_mutex);
6613
	mutex_unlock(&dev->struct_mutex);
6603
 
6614
 
6604
	trace_i915_flip_request(intel_crtc->plane, obj);
6615
	trace_i915_flip_request(intel_crtc->plane, obj);
6605
 
6616
 
6606
	return 0;
6617
	return 0;
6607
 
6618
 
6608
cleanup_pending:
6619
cleanup_pending:
6609
	atomic_sub(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
6620
	atomic_sub(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
6610
	drm_gem_object_unreference(&work->old_fb_obj->base);
6621
	drm_gem_object_unreference(&work->old_fb_obj->base);
6611
	drm_gem_object_unreference(&obj->base);
6622
	drm_gem_object_unreference(&obj->base);
6612
	mutex_unlock(&dev->struct_mutex);
6623
	mutex_unlock(&dev->struct_mutex);
6613
 
6624
 
6614
cleanup:
6625
cleanup:
6615
	spin_lock_irqsave(&dev->event_lock, flags);
6626
	spin_lock_irqsave(&dev->event_lock, flags);
6616
	intel_crtc->unpin_work = NULL;
6627
	intel_crtc->unpin_work = NULL;
6617
	spin_unlock_irqrestore(&dev->event_lock, flags);
6628
	spin_unlock_irqrestore(&dev->event_lock, flags);
6618
 
6629
 
6619
	drm_vblank_put(dev, intel_crtc->pipe);
6630
	drm_vblank_put(dev, intel_crtc->pipe);
6620
free_work:
6631
free_work:
6621
	kfree(work);
6632
	kfree(work);
6622
 
6633
 
6623
	return ret;
6634
	return ret;
6624
}
6635
}
6625
 
6636
 
6626
#endif
6637
#endif
6627
 
6638
 
6628
static struct drm_crtc_helper_funcs intel_helper_funcs = {
6639
static struct drm_crtc_helper_funcs intel_helper_funcs = {
6629
	.mode_set_base_atomic = intel_pipe_set_base_atomic,
6640
	.mode_set_base_atomic = intel_pipe_set_base_atomic,
6630
	.load_lut = intel_crtc_load_lut,
6641
	.load_lut = intel_crtc_load_lut,
6631
	.disable = intel_crtc_noop,
6642
	.disable = intel_crtc_noop,
6632
};
6643
};
6633
 
6644
 
6634
bool intel_encoder_check_is_cloned(struct intel_encoder *encoder)
6645
bool intel_encoder_check_is_cloned(struct intel_encoder *encoder)
6635
{
6646
{
6636
	struct intel_encoder *other_encoder;
6647
	struct intel_encoder *other_encoder;
6637
	struct drm_crtc *crtc = &encoder->new_crtc->base;
6648
	struct drm_crtc *crtc = &encoder->new_crtc->base;
6638
 
6649
 
6639
	if (WARN_ON(!crtc))
6650
	if (WARN_ON(!crtc))
6640
		return false;
6651
		return false;
6641
 
6652
 
6642
	list_for_each_entry(other_encoder,
6653
	list_for_each_entry(other_encoder,
6643
			    &crtc->dev->mode_config.encoder_list,
6654
			    &crtc->dev->mode_config.encoder_list,
6644
			    base.head) {
6655
			    base.head) {
6645
 
6656
 
6646
		if (&other_encoder->new_crtc->base != crtc ||
6657
		if (&other_encoder->new_crtc->base != crtc ||
6647
		    encoder == other_encoder)
6658
		    encoder == other_encoder)
6648
			continue;
6659
			continue;
6649
		else
6660
		else
6650
			return true;
6661
			return true;
6651
	}
6662
	}
6652
 
6663
 
6653
	return false;
6664
	return false;
6654
}
6665
}
6655
 
6666
 
6656
static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
6667
static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
6657
				  struct drm_crtc *crtc)
6668
				  struct drm_crtc *crtc)
6658
{
6669
{
6659
	struct drm_device *dev;
6670
	struct drm_device *dev;
6660
	struct drm_crtc *tmp;
6671
	struct drm_crtc *tmp;
6661
	int crtc_mask = 1;
6672
	int crtc_mask = 1;
6662
 
6673
 
6663
	WARN(!crtc, "checking null crtc?\n");
6674
	WARN(!crtc, "checking null crtc?\n");
6664
 
6675
 
6665
	dev = crtc->dev;
6676
	dev = crtc->dev;
6666
 
6677
 
6667
	list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
6678
	list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
6668
		if (tmp == crtc)
6679
		if (tmp == crtc)
6669
			break;
6680
			break;
6670
		crtc_mask <<= 1;
6681
		crtc_mask <<= 1;
6671
	}
6682
	}
6672
 
6683
 
6673
	if (encoder->possible_crtcs & crtc_mask)
6684
	if (encoder->possible_crtcs & crtc_mask)
6674
		return true;
6685
		return true;
6675
	return false;
6686
	return false;
6676
}
6687
}
6677
 
6688
 
6678
/**
6689
/**
6679
 * intel_modeset_update_staged_output_state
6690
 * intel_modeset_update_staged_output_state
6680
 *
6691
 *
6681
 * Updates the staged output configuration state, e.g. after we've read out the
6692
 * Updates the staged output configuration state, e.g. after we've read out the
6682
 * current hw state.
6693
 * current hw state.
6683
 */
6694
 */
6684
static void intel_modeset_update_staged_output_state(struct drm_device *dev)
6695
static void intel_modeset_update_staged_output_state(struct drm_device *dev)
6685
{
6696
{
6686
	struct intel_encoder *encoder;
6697
	struct intel_encoder *encoder;
6687
	struct intel_connector *connector;
6698
	struct intel_connector *connector;
6688
 
6699
 
6689
	list_for_each_entry(connector, &dev->mode_config.connector_list,
6700
	list_for_each_entry(connector, &dev->mode_config.connector_list,
6690
			    base.head) {
6701
			    base.head) {
6691
		connector->new_encoder =
6702
		connector->new_encoder =
6692
			to_intel_encoder(connector->base.encoder);
6703
			to_intel_encoder(connector->base.encoder);
6693
	}
6704
	}
6694
 
6705
 
6695
	list_for_each_entry(encoder, &dev->mode_config.encoder_list,
6706
	list_for_each_entry(encoder, &dev->mode_config.encoder_list,
6696
			    base.head) {
6707
			    base.head) {
6697
		encoder->new_crtc =
6708
		encoder->new_crtc =
6698
			to_intel_crtc(encoder->base.crtc);
6709
			to_intel_crtc(encoder->base.crtc);
6699
	}
6710
	}
6700
}
6711
}
6701
 
6712
 
6702
/**
6713
/**
6703
 * intel_modeset_commit_output_state
6714
 * intel_modeset_commit_output_state
6704
 *
6715
 *
6705
 * This function copies the stage display pipe configuration to the real one.
6716
 * This function copies the stage display pipe configuration to the real one.
6706
 */
6717
 */
6707
static void intel_modeset_commit_output_state(struct drm_device *dev)
6718
static void intel_modeset_commit_output_state(struct drm_device *dev)
6708
{
6719
{
6709
	struct intel_encoder *encoder;
6720
	struct intel_encoder *encoder;
6710
	struct intel_connector *connector;
6721
	struct intel_connector *connector;
6711
 
6722
 
6712
	list_for_each_entry(connector, &dev->mode_config.connector_list,
6723
	list_for_each_entry(connector, &dev->mode_config.connector_list,
6713
			    base.head) {
6724
			    base.head) {
6714
		connector->base.encoder = &connector->new_encoder->base;
6725
		connector->base.encoder = &connector->new_encoder->base;
6715
	}
6726
	}
6716
 
6727
 
6717
	list_for_each_entry(encoder, &dev->mode_config.encoder_list,
6728
	list_for_each_entry(encoder, &dev->mode_config.encoder_list,
6718
			    base.head) {
6729
			    base.head) {
6719
		encoder->base.crtc = &encoder->new_crtc->base;
6730
		encoder->base.crtc = &encoder->new_crtc->base;
6720
	}
6731
	}
6721
}
6732
}
6722
 
6733
 
6723
static struct drm_display_mode *
6734
static struct drm_display_mode *
6724
intel_modeset_adjusted_mode(struct drm_crtc *crtc,
6735
intel_modeset_adjusted_mode(struct drm_crtc *crtc,
6725
			    struct drm_display_mode *mode)
6736
			    struct drm_display_mode *mode)
6726
{
6737
{
6727
	struct drm_device *dev = crtc->dev;
6738
	struct drm_device *dev = crtc->dev;
6728
	struct drm_display_mode *adjusted_mode;
6739
	struct drm_display_mode *adjusted_mode;
6729
	struct drm_encoder_helper_funcs *encoder_funcs;
6740
	struct drm_encoder_helper_funcs *encoder_funcs;
6730
	struct intel_encoder *encoder;
6741
	struct intel_encoder *encoder;
6731
 
6742
 
6732
	adjusted_mode = drm_mode_duplicate(dev, mode);
6743
	adjusted_mode = drm_mode_duplicate(dev, mode);
6733
	if (!adjusted_mode)
6744
	if (!adjusted_mode)
6734
		return ERR_PTR(-ENOMEM);
6745
		return ERR_PTR(-ENOMEM);
6735
 
6746
 
6736
	/* Pass our mode to the connectors and the CRTC to give them a chance to
6747
	/* Pass our mode to the connectors and the CRTC to give them a chance to
6737
	 * adjust it according to limitations or connector properties, and also
6748
	 * adjust it according to limitations or connector properties, and also
6738
	 * a chance to reject the mode entirely.
6749
	 * a chance to reject the mode entirely.
6739
	 */
6750
	 */
6740
	list_for_each_entry(encoder, &dev->mode_config.encoder_list,
6751
	list_for_each_entry(encoder, &dev->mode_config.encoder_list,
6741
			    base.head) {
6752
			    base.head) {
6742
 
6753
 
6743
		if (&encoder->new_crtc->base != crtc)
6754
		if (&encoder->new_crtc->base != crtc)
6744
			continue;
6755
			continue;
6745
		encoder_funcs = encoder->base.helper_private;
6756
		encoder_funcs = encoder->base.helper_private;
6746
		if (!(encoder_funcs->mode_fixup(&encoder->base, mode,
6757
		if (!(encoder_funcs->mode_fixup(&encoder->base, mode,
6747
						adjusted_mode))) {
6758
						adjusted_mode))) {
6748
			DRM_DEBUG_KMS("Encoder fixup failed\n");
6759
			DRM_DEBUG_KMS("Encoder fixup failed\n");
6749
			goto fail;
6760
			goto fail;
6750
		}
6761
		}
6751
	}
6762
	}
6752
 
6763
 
6753
	if (!(intel_crtc_mode_fixup(crtc, mode, adjusted_mode))) {
6764
	if (!(intel_crtc_mode_fixup(crtc, mode, adjusted_mode))) {
6754
		DRM_DEBUG_KMS("CRTC fixup failed\n");
6765
		DRM_DEBUG_KMS("CRTC fixup failed\n");
6755
		goto fail;
6766
		goto fail;
6756
	}
6767
	}
6757
	DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
6768
	DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
6758
 
6769
 
6759
	return adjusted_mode;
6770
	return adjusted_mode;
6760
fail:
6771
fail:
6761
	drm_mode_destroy(dev, adjusted_mode);
6772
	drm_mode_destroy(dev, adjusted_mode);
6762
	return ERR_PTR(-EINVAL);
6773
	return ERR_PTR(-EINVAL);
6763
}
6774
}
6764
 
6775
 
6765
/* Computes which crtcs are affected and sets the relevant bits in the mask. For
6776
/* Computes which crtcs are affected and sets the relevant bits in the mask. For
6766
 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
6777
 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
6767
static void
6778
static void
6768
intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
6779
intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
6769
			     unsigned *prepare_pipes, unsigned *disable_pipes)
6780
			     unsigned *prepare_pipes, unsigned *disable_pipes)
6770
{
6781
{
6771
	struct intel_crtc *intel_crtc;
6782
	struct intel_crtc *intel_crtc;
6772
	struct drm_device *dev = crtc->dev;
6783
	struct drm_device *dev = crtc->dev;
6773
	struct intel_encoder *encoder;
6784
	struct intel_encoder *encoder;
6774
	struct intel_connector *connector;
6785
	struct intel_connector *connector;
6775
	struct drm_crtc *tmp_crtc;
6786
	struct drm_crtc *tmp_crtc;
6776
 
6787
 
6777
	*disable_pipes = *modeset_pipes = *prepare_pipes = 0;
6788
	*disable_pipes = *modeset_pipes = *prepare_pipes = 0;
6778
 
6789
 
6779
	/* Check which crtcs have changed outputs connected to them, these need
6790
	/* Check which crtcs have changed outputs connected to them, these need
6780
	 * to be part of the prepare_pipes mask. We don't (yet) support global
6791
	 * to be part of the prepare_pipes mask. We don't (yet) support global
6781
	 * modeset across multiple crtcs, so modeset_pipes will only have one
6792
	 * modeset across multiple crtcs, so modeset_pipes will only have one
6782
	 * bit set at most. */
6793
	 * bit set at most. */
6783
	list_for_each_entry(connector, &dev->mode_config.connector_list,
6794
	list_for_each_entry(connector, &dev->mode_config.connector_list,
6784
			    base.head) {
6795
			    base.head) {
6785
		if (connector->base.encoder == &connector->new_encoder->base)
6796
		if (connector->base.encoder == &connector->new_encoder->base)
6786
			continue;
6797
			continue;
6787
 
6798
 
6788
		if (connector->base.encoder) {
6799
		if (connector->base.encoder) {
6789
			tmp_crtc = connector->base.encoder->crtc;
6800
			tmp_crtc = connector->base.encoder->crtc;
6790
 
6801
 
6791
			*prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
6802
			*prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
6792
		}
6803
		}
6793
 
6804
 
6794
		if (connector->new_encoder)
6805
		if (connector->new_encoder)
6795
			*prepare_pipes |=
6806
			*prepare_pipes |=
6796
				1 << connector->new_encoder->new_crtc->pipe;
6807
				1 << connector->new_encoder->new_crtc->pipe;
6797
	}
6808
	}
6798
 
6809
 
6799
	list_for_each_entry(encoder, &dev->mode_config.encoder_list,
6810
	list_for_each_entry(encoder, &dev->mode_config.encoder_list,
6800
			    base.head) {
6811
			    base.head) {
6801
		if (encoder->base.crtc == &encoder->new_crtc->base)
6812
		if (encoder->base.crtc == &encoder->new_crtc->base)
6802
			continue;
6813
			continue;
6803
 
6814
 
6804
		if (encoder->base.crtc) {
6815
		if (encoder->base.crtc) {
6805
			tmp_crtc = encoder->base.crtc;
6816
			tmp_crtc = encoder->base.crtc;
6806
 
6817
 
6807
			*prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
6818
			*prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
6808
		}
6819
		}
6809
 
6820
 
6810
		if (encoder->new_crtc)
6821
		if (encoder->new_crtc)
6811
			*prepare_pipes |= 1 << encoder->new_crtc->pipe;
6822
			*prepare_pipes |= 1 << encoder->new_crtc->pipe;
6812
	}
6823
	}
6813
 
6824
 
6814
	/* Check for any pipes that will be fully disabled ... */
6825
	/* Check for any pipes that will be fully disabled ... */
6815
	list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
6826
	list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
6816
			    base.head) {
6827
			    base.head) {
6817
		bool used = false;
6828
		bool used = false;
6818
 
6829
 
6819
		/* Don't try to disable disabled crtcs. */
6830
		/* Don't try to disable disabled crtcs. */
6820
		if (!intel_crtc->base.enabled)
6831
		if (!intel_crtc->base.enabled)
6821
			continue;
6832
			continue;
6822
 
6833
 
6823
		list_for_each_entry(encoder, &dev->mode_config.encoder_list,
6834
		list_for_each_entry(encoder, &dev->mode_config.encoder_list,
6824
				    base.head) {
6835
				    base.head) {
6825
			if (encoder->new_crtc == intel_crtc)
6836
			if (encoder->new_crtc == intel_crtc)
6826
				used = true;
6837
				used = true;
6827
		}
6838
		}
6828
 
6839
 
6829
		if (!used)
6840
		if (!used)
6830
			*disable_pipes |= 1 << intel_crtc->pipe;
6841
			*disable_pipes |= 1 << intel_crtc->pipe;
6831
	}
6842
	}
6832
 
6843
 
6833
 
6844
 
6834
	/* set_mode is also used to update properties on life display pipes. */
6845
	/* set_mode is also used to update properties on life display pipes. */
6835
	intel_crtc = to_intel_crtc(crtc);
6846
	intel_crtc = to_intel_crtc(crtc);
6836
	if (crtc->enabled)
6847
	if (crtc->enabled)
6837
		*prepare_pipes |= 1 << intel_crtc->pipe;
6848
		*prepare_pipes |= 1 << intel_crtc->pipe;
6838
 
6849
 
6839
	/* We only support modeset on one single crtc, hence we need to do that
6850
	/* We only support modeset on one single crtc, hence we need to do that
6840
	 * only for the passed in crtc iff we change anything else than just
6851
	 * only for the passed in crtc iff we change anything else than just
6841
	 * disable crtcs.
6852
	 * disable crtcs.
6842
	 *
6853
	 *
6843
	 * This is actually not true, to be fully compatible with the old crtc
6854
	 * This is actually not true, to be fully compatible with the old crtc
6844
	 * helper we automatically disable _any_ output (i.e. doesn't need to be
6855
	 * helper we automatically disable _any_ output (i.e. doesn't need to be
6845
	 * connected to the crtc we're modesetting on) if it's disconnected.
6856
	 * connected to the crtc we're modesetting on) if it's disconnected.
6846
	 * Which is a rather nutty api (since changed the output configuration
6857
	 * Which is a rather nutty api (since changed the output configuration
6847
	 * without userspace's explicit request can lead to confusion), but
6858
	 * without userspace's explicit request can lead to confusion), but
6848
	 * alas. Hence we currently need to modeset on all pipes we prepare. */
6859
	 * alas. Hence we currently need to modeset on all pipes we prepare. */
6849
	if (*prepare_pipes)
6860
	if (*prepare_pipes)
6850
		*modeset_pipes = *prepare_pipes;
6861
		*modeset_pipes = *prepare_pipes;
6851
 
6862
 
6852
	/* ... and mask these out. */
6863
	/* ... and mask these out. */
6853
	*modeset_pipes &= ~(*disable_pipes);
6864
	*modeset_pipes &= ~(*disable_pipes);
6854
	*prepare_pipes &= ~(*disable_pipes);
6865
	*prepare_pipes &= ~(*disable_pipes);
6855
}
6866
}
6856
 
6867
 
6857
static bool intel_crtc_in_use(struct drm_crtc *crtc)
6868
static bool intel_crtc_in_use(struct drm_crtc *crtc)
6858
{
6869
{
6859
	struct drm_encoder *encoder;
6870
	struct drm_encoder *encoder;
6860
	struct drm_device *dev = crtc->dev;
6871
	struct drm_device *dev = crtc->dev;
6861
 
6872
 
6862
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
6873
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
6863
		if (encoder->crtc == crtc)
6874
		if (encoder->crtc == crtc)
6864
			return true;
6875
			return true;
6865
 
6876
 
6866
	return false;
6877
	return false;
6867
}
6878
}
6868
 
6879
 
6869
static void
6880
static void
6870
intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
6881
intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
6871
{
6882
{
6872
	struct intel_encoder *intel_encoder;
6883
	struct intel_encoder *intel_encoder;
6873
	struct intel_crtc *intel_crtc;
6884
	struct intel_crtc *intel_crtc;
6874
	struct drm_connector *connector;
6885
	struct drm_connector *connector;
6875
 
6886
 
6876
	list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
6887
	list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
6877
			    base.head) {
6888
			    base.head) {
6878
		if (!intel_encoder->base.crtc)
6889
		if (!intel_encoder->base.crtc)
6879
			continue;
6890
			continue;
6880
 
6891
 
6881
		intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
6892
		intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
6882
 
6893
 
6883
		if (prepare_pipes & (1 << intel_crtc->pipe))
6894
		if (prepare_pipes & (1 << intel_crtc->pipe))
6884
			intel_encoder->connectors_active = false;
6895
			intel_encoder->connectors_active = false;
6885
	}
6896
	}
6886
 
6897
 
6887
	intel_modeset_commit_output_state(dev);
6898
	intel_modeset_commit_output_state(dev);
6888
 
6899
 
6889
	/* Update computed state. */
6900
	/* Update computed state. */
6890
	list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
6901
	list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
6891
			    base.head) {
6902
			    base.head) {
6892
		intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
6903
		intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
6893
	}
6904
	}
6894
 
6905
 
6895
	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
6906
	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
6896
		if (!connector->encoder || !connector->encoder->crtc)
6907
		if (!connector->encoder || !connector->encoder->crtc)
6897
			continue;
6908
			continue;
6898
 
6909
 
6899
		intel_crtc = to_intel_crtc(connector->encoder->crtc);
6910
		intel_crtc = to_intel_crtc(connector->encoder->crtc);
6900
 
6911
 
6901
		if (prepare_pipes & (1 << intel_crtc->pipe)) {
6912
		if (prepare_pipes & (1 << intel_crtc->pipe)) {
6902
			struct drm_property *dpms_property =
6913
			struct drm_property *dpms_property =
6903
				dev->mode_config.dpms_property;
6914
				dev->mode_config.dpms_property;
6904
 
6915
 
6905
			connector->dpms = DRM_MODE_DPMS_ON;
6916
			connector->dpms = DRM_MODE_DPMS_ON;
6906
			drm_connector_property_set_value(connector,
6917
			drm_connector_property_set_value(connector,
6907
							 dpms_property,
6918
							 dpms_property,
6908
							 DRM_MODE_DPMS_ON);
6919
							 DRM_MODE_DPMS_ON);
6909
 
6920
 
6910
			intel_encoder = to_intel_encoder(connector->encoder);
6921
			intel_encoder = to_intel_encoder(connector->encoder);
6911
			intel_encoder->connectors_active = true;
6922
			intel_encoder->connectors_active = true;
6912
		}
6923
		}
6913
	}
6924
	}
6914
 
6925
 
6915
}
6926
}
6916
 
6927
 
6917
#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
6928
#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
6918
	list_for_each_entry((intel_crtc), \
6929
	list_for_each_entry((intel_crtc), \
6919
			    &(dev)->mode_config.crtc_list, \
6930
			    &(dev)->mode_config.crtc_list, \
6920
			    base.head) \
6931
			    base.head) \
6921
		if (mask & (1 <<(intel_crtc)->pipe)) \
6932
		if (mask & (1 <<(intel_crtc)->pipe)) \
6922
 
6933
 
6923
void
6934
void
6924
intel_modeset_check_state(struct drm_device *dev)
6935
intel_modeset_check_state(struct drm_device *dev)
6925
{
6936
{
6926
	struct intel_crtc *crtc;
6937
	struct intel_crtc *crtc;
6927
	struct intel_encoder *encoder;
6938
	struct intel_encoder *encoder;
6928
	struct intel_connector *connector;
6939
	struct intel_connector *connector;
6929
 
6940
 
6930
	list_for_each_entry(connector, &dev->mode_config.connector_list,
6941
	list_for_each_entry(connector, &dev->mode_config.connector_list,
6931
			    base.head) {
6942
			    base.head) {
6932
		/* This also checks the encoder/connector hw state with the
6943
		/* This also checks the encoder/connector hw state with the
6933
		 * ->get_hw_state callbacks. */
6944
		 * ->get_hw_state callbacks. */
6934
		intel_connector_check_state(connector);
6945
		intel_connector_check_state(connector);
6935
 
6946
 
6936
		WARN(&connector->new_encoder->base != connector->base.encoder,
6947
		WARN(&connector->new_encoder->base != connector->base.encoder,
6937
		     "connector's staged encoder doesn't match current encoder\n");
6948
		     "connector's staged encoder doesn't match current encoder\n");
6938
	}
6949
	}
6939
 
6950
 
6940
	list_for_each_entry(encoder, &dev->mode_config.encoder_list,
6951
	list_for_each_entry(encoder, &dev->mode_config.encoder_list,
6941
			    base.head) {
6952
			    base.head) {
6942
		bool enabled = false;
6953
		bool enabled = false;
6943
		bool active = false;
6954
		bool active = false;
6944
		enum pipe pipe, tracked_pipe;
6955
		enum pipe pipe, tracked_pipe;
6945
 
6956
 
6946
		DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
6957
		DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
6947
			      encoder->base.base.id,
6958
			      encoder->base.base.id,
6948
			      drm_get_encoder_name(&encoder->base));
6959
			      drm_get_encoder_name(&encoder->base));
6949
 
6960
 
6950
		WARN(&encoder->new_crtc->base != encoder->base.crtc,
6961
		WARN(&encoder->new_crtc->base != encoder->base.crtc,
6951
		     "encoder's stage crtc doesn't match current crtc\n");
6962
		     "encoder's stage crtc doesn't match current crtc\n");
6952
		WARN(encoder->connectors_active && !encoder->base.crtc,
6963
		WARN(encoder->connectors_active && !encoder->base.crtc,
6953
		     "encoder's active_connectors set, but no crtc\n");
6964
		     "encoder's active_connectors set, but no crtc\n");
6954
 
6965
 
6955
		list_for_each_entry(connector, &dev->mode_config.connector_list,
6966
		list_for_each_entry(connector, &dev->mode_config.connector_list,
6956
				    base.head) {
6967
				    base.head) {
6957
			if (connector->base.encoder != &encoder->base)
6968
			if (connector->base.encoder != &encoder->base)
6958
				continue;
6969
				continue;
6959
			enabled = true;
6970
			enabled = true;
6960
			if (connector->base.dpms != DRM_MODE_DPMS_OFF)
6971
			if (connector->base.dpms != DRM_MODE_DPMS_OFF)
6961
				active = true;
6972
				active = true;
6962
		}
6973
		}
6963
		WARN(!!encoder->base.crtc != enabled,
6974
		WARN(!!encoder->base.crtc != enabled,
6964
		     "encoder's enabled state mismatch "
6975
		     "encoder's enabled state mismatch "
6965
		     "(expected %i, found %i)\n",
6976
		     "(expected %i, found %i)\n",
6966
		     !!encoder->base.crtc, enabled);
6977
		     !!encoder->base.crtc, enabled);
6967
		WARN(active && !encoder->base.crtc,
6978
		WARN(active && !encoder->base.crtc,
6968
		     "active encoder with no crtc\n");
6979
		     "active encoder with no crtc\n");
6969
 
6980
 
6970
		WARN(encoder->connectors_active != active,
6981
		WARN(encoder->connectors_active != active,
6971
		     "encoder's computed active state doesn't match tracked active state "
6982
		     "encoder's computed active state doesn't match tracked active state "
6972
		     "(expected %i, found %i)\n", active, encoder->connectors_active);
6983
		     "(expected %i, found %i)\n", active, encoder->connectors_active);
6973
 
6984
 
6974
		active = encoder->get_hw_state(encoder, &pipe);
6985
		active = encoder->get_hw_state(encoder, &pipe);
6975
		WARN(active != encoder->connectors_active,
6986
		WARN(active != encoder->connectors_active,
6976
		     "encoder's hw state doesn't match sw tracking "
6987
		     "encoder's hw state doesn't match sw tracking "
6977
		     "(expected %i, found %i)\n",
6988
		     "(expected %i, found %i)\n",
6978
		     encoder->connectors_active, active);
6989
		     encoder->connectors_active, active);
6979
 
6990
 
6980
		if (!encoder->base.crtc)
6991
		if (!encoder->base.crtc)
6981
			continue;
6992
			continue;
6982
 
6993
 
6983
		tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
6994
		tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
6984
		WARN(active && pipe != tracked_pipe,
6995
		WARN(active && pipe != tracked_pipe,
6985
		     "active encoder's pipe doesn't match"
6996
		     "active encoder's pipe doesn't match"
6986
		     "(expected %i, found %i)\n",
6997
		     "(expected %i, found %i)\n",
6987
		     tracked_pipe, pipe);
6998
		     tracked_pipe, pipe);
6988
 
6999
 
6989
	}
7000
	}
6990
 
7001
 
6991
	list_for_each_entry(crtc, &dev->mode_config.crtc_list,
7002
	list_for_each_entry(crtc, &dev->mode_config.crtc_list,
6992
			    base.head) {
7003
			    base.head) {
6993
		bool enabled = false;
7004
		bool enabled = false;
6994
		bool active = false;
7005
		bool active = false;
6995
 
7006
 
6996
		DRM_DEBUG_KMS("[CRTC:%d]\n",
7007
		DRM_DEBUG_KMS("[CRTC:%d]\n",
6997
			      crtc->base.base.id);
7008
			      crtc->base.base.id);
6998
 
7009
 
6999
		WARN(crtc->active && !crtc->base.enabled,
7010
		WARN(crtc->active && !crtc->base.enabled,
7000
		     "active crtc, but not enabled in sw tracking\n");
7011
		     "active crtc, but not enabled in sw tracking\n");
7001
 
7012
 
7002
		list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7013
		list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7003
				    base.head) {
7014
				    base.head) {
7004
			if (encoder->base.crtc != &crtc->base)
7015
			if (encoder->base.crtc != &crtc->base)
7005
				continue;
7016
				continue;
7006
			enabled = true;
7017
			enabled = true;
7007
			if (encoder->connectors_active)
7018
			if (encoder->connectors_active)
7008
				active = true;
7019
				active = true;
7009
		}
7020
		}
7010
		WARN(active != crtc->active,
7021
		WARN(active != crtc->active,
7011
		     "crtc's computed active state doesn't match tracked active state "
7022
		     "crtc's computed active state doesn't match tracked active state "
7012
		     "(expected %i, found %i)\n", active, crtc->active);
7023
		     "(expected %i, found %i)\n", active, crtc->active);
7013
		WARN(enabled != crtc->base.enabled,
7024
		WARN(enabled != crtc->base.enabled,
7014
		     "crtc's computed enabled state doesn't match tracked enabled state "
7025
		     "crtc's computed enabled state doesn't match tracked enabled state "
7015
		     "(expected %i, found %i)\n", enabled, crtc->base.enabled);
7026
		     "(expected %i, found %i)\n", enabled, crtc->base.enabled);
7016
 
7027
 
7017
		assert_pipe(dev->dev_private, crtc->pipe, crtc->active);
7028
		assert_pipe(dev->dev_private, crtc->pipe, crtc->active);
7018
	}
7029
	}
7019
}
7030
}
7020
 
7031
 
7021
bool intel_set_mode(struct drm_crtc *crtc,
7032
bool intel_set_mode(struct drm_crtc *crtc,
7022
		    struct drm_display_mode *mode,
7033
		    struct drm_display_mode *mode,
7023
		    int x, int y, struct drm_framebuffer *fb)
7034
		    int x, int y, struct drm_framebuffer *fb)
7024
{
7035
{
7025
	struct drm_device *dev = crtc->dev;
7036
	struct drm_device *dev = crtc->dev;
7026
	drm_i915_private_t *dev_priv = dev->dev_private;
7037
	drm_i915_private_t *dev_priv = dev->dev_private;
7027
	struct drm_display_mode *adjusted_mode, saved_mode, saved_hwmode;
7038
	struct drm_display_mode *adjusted_mode, saved_mode, saved_hwmode;
7028
	struct drm_encoder_helper_funcs *encoder_funcs;
7039
	struct drm_encoder_helper_funcs *encoder_funcs;
7029
	struct drm_encoder *encoder;
7040
	struct drm_encoder *encoder;
7030
	struct intel_crtc *intel_crtc;
7041
	struct intel_crtc *intel_crtc;
7031
	unsigned disable_pipes, prepare_pipes, modeset_pipes;
7042
	unsigned disable_pipes, prepare_pipes, modeset_pipes;
7032
	bool ret = true;
7043
	bool ret = true;
7033
 
7044
 
7034
	intel_modeset_affected_pipes(crtc, &modeset_pipes,
7045
	intel_modeset_affected_pipes(crtc, &modeset_pipes,
7035
				     &prepare_pipes, &disable_pipes);
7046
				     &prepare_pipes, &disable_pipes);
7036
 
7047
 
7037
	DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
7048
	DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
7038
		      modeset_pipes, prepare_pipes, disable_pipes);
7049
		      modeset_pipes, prepare_pipes, disable_pipes);
7039
 
7050
 
7040
	for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
7051
	for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
7041
		intel_crtc_disable(&intel_crtc->base);
7052
		intel_crtc_disable(&intel_crtc->base);
7042
 
7053
 
7043
	saved_hwmode = crtc->hwmode;
7054
	saved_hwmode = crtc->hwmode;
7044
	saved_mode = crtc->mode;
7055
	saved_mode = crtc->mode;
7045
 
7056
 
7046
	/* Hack: Because we don't (yet) support global modeset on multiple
7057
	/* Hack: Because we don't (yet) support global modeset on multiple
7047
	 * crtcs, we don't keep track of the new mode for more than one crtc.
7058
	 * crtcs, we don't keep track of the new mode for more than one crtc.
7048
	 * Hence simply check whether any bit is set in modeset_pipes in all the
7059
	 * Hence simply check whether any bit is set in modeset_pipes in all the
7049
	 * pieces of code that are not yet converted to deal with mutliple crtcs
7060
	 * pieces of code that are not yet converted to deal with mutliple crtcs
7050
	 * changing their mode at the same time. */
7061
	 * changing their mode at the same time. */
7051
	adjusted_mode = NULL;
7062
	adjusted_mode = NULL;
7052
	if (modeset_pipes) {
7063
	if (modeset_pipes) {
7053
		adjusted_mode = intel_modeset_adjusted_mode(crtc, mode);
7064
		adjusted_mode = intel_modeset_adjusted_mode(crtc, mode);
7054
		if (IS_ERR(adjusted_mode)) {
7065
		if (IS_ERR(adjusted_mode)) {
7055
			return false;
7066
			return false;
7056
		}
7067
		}
7057
	}
7068
	}
7058
 
7069
 
7059
	for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
7070
	for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
7060
		if (intel_crtc->base.enabled)
7071
		if (intel_crtc->base.enabled)
7061
			dev_priv->display.crtc_disable(&intel_crtc->base);
7072
			dev_priv->display.crtc_disable(&intel_crtc->base);
7062
	}
7073
	}
7063
 
7074
 
7064
	/* crtc->mode is already used by the ->mode_set callbacks, hence we need
7075
	/* crtc->mode is already used by the ->mode_set callbacks, hence we need
7065
	 * to set it here already despite that we pass it down the callchain.
7076
	 * to set it here already despite that we pass it down the callchain.
7066
	 */
7077
	 */
7067
	if (modeset_pipes)
7078
	if (modeset_pipes)
7068
		crtc->mode = *mode;
7079
		crtc->mode = *mode;
7069
 
7080
 
7070
	/* Only after disabling all output pipelines that will be changed can we
7081
	/* Only after disabling all output pipelines that will be changed can we
7071
	 * update the the output configuration. */
7082
	 * update the the output configuration. */
7072
	intel_modeset_update_state(dev, prepare_pipes);
7083
	intel_modeset_update_state(dev, prepare_pipes);
7073
 
7084
 
7074
	/* Set up the DPLL and any encoders state that needs to adjust or depend
7085
	/* Set up the DPLL and any encoders state that needs to adjust or depend
7075
	 * on the DPLL.
7086
	 * on the DPLL.
7076
	 */
7087
	 */
7077
	for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
7088
	for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
7078
		ret = !intel_crtc_mode_set(&intel_crtc->base,
7089
		ret = !intel_crtc_mode_set(&intel_crtc->base,
7079
					   mode, adjusted_mode,
7090
					   mode, adjusted_mode,
7080
					   x, y, fb);
7091
					   x, y, fb);
7081
		if (!ret)
7092
		if (!ret)
7082
		    goto done;
7093
		    goto done;
7083
 
7094
 
7084
		list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
7095
		list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
7085
 
7096
 
7086
			if (encoder->crtc != &intel_crtc->base)
7097
			if (encoder->crtc != &intel_crtc->base)
7087
				continue;
7098
				continue;
7088
 
7099
 
7089
			DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
7100
			DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
7090
				encoder->base.id, drm_get_encoder_name(encoder),
7101
				encoder->base.id, drm_get_encoder_name(encoder),
7091
				mode->base.id, mode->name);
7102
				mode->base.id, mode->name);
7092
			encoder_funcs = encoder->helper_private;
7103
			encoder_funcs = encoder->helper_private;
7093
			encoder_funcs->mode_set(encoder, mode, adjusted_mode);
7104
			encoder_funcs->mode_set(encoder, mode, adjusted_mode);
7094
		}
7105
		}
7095
	}
7106
	}
7096
 
7107
 
7097
	/* Now enable the clocks, plane, pipe, and connectors that we set up. */
7108
	/* Now enable the clocks, plane, pipe, and connectors that we set up. */
7098
	for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
7109
	for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
7099
		dev_priv->display.crtc_enable(&intel_crtc->base);
7110
		dev_priv->display.crtc_enable(&intel_crtc->base);
7100
 
7111
 
7101
	if (modeset_pipes) {
7112
	if (modeset_pipes) {
7102
		/* Store real post-adjustment hardware mode. */
7113
		/* Store real post-adjustment hardware mode. */
7103
		crtc->hwmode = *adjusted_mode;
7114
		crtc->hwmode = *adjusted_mode;
7104
 
7115
 
7105
		/* Calculate and store various constants which
7116
		/* Calculate and store various constants which
7106
		 * are later needed by vblank and swap-completion
7117
		 * are later needed by vblank and swap-completion
7107
		 * timestamping. They are derived from true hwmode.
7118
		 * timestamping. They are derived from true hwmode.
7108
		 */
7119
		 */
7109
		drm_calc_timestamping_constants(crtc);
7120
		drm_calc_timestamping_constants(crtc);
7110
	}
7121
	}
7111
 
7122
 
7112
	/* FIXME: add subpixel order */
7123
	/* FIXME: add subpixel order */
7113
done:
7124
done:
7114
	drm_mode_destroy(dev, adjusted_mode);
7125
	drm_mode_destroy(dev, adjusted_mode);
7115
	if (!ret && crtc->enabled) {
7126
	if (!ret && crtc->enabled) {
7116
		crtc->hwmode = saved_hwmode;
7127
		crtc->hwmode = saved_hwmode;
7117
		crtc->mode = saved_mode;
7128
		crtc->mode = saved_mode;
7118
	} else {
7129
	} else {
7119
		intel_modeset_check_state(dev);
7130
		intel_modeset_check_state(dev);
7120
	}
7131
	}
7121
 
7132
 
7122
	return ret;
7133
	return ret;
7123
}
7134
}
7124
 
7135
 
7125
#undef for_each_intel_crtc_masked
7136
#undef for_each_intel_crtc_masked
7126
 
7137
 
7127
static void intel_set_config_free(struct intel_set_config *config)
7138
static void intel_set_config_free(struct intel_set_config *config)
7128
{
7139
{
7129
	if (!config)
7140
	if (!config)
7130
		return;
7141
		return;
7131
 
7142
 
7132
	kfree(config->save_connector_encoders);
7143
	kfree(config->save_connector_encoders);
7133
	kfree(config->save_encoder_crtcs);
7144
	kfree(config->save_encoder_crtcs);
7134
	kfree(config);
7145
	kfree(config);
7135
}
7146
}
7136
 
7147
 
7137
static int intel_set_config_save_state(struct drm_device *dev,
7148
static int intel_set_config_save_state(struct drm_device *dev,
7138
				       struct intel_set_config *config)
7149
				       struct intel_set_config *config)
7139
{
7150
{
7140
	struct drm_encoder *encoder;
7151
	struct drm_encoder *encoder;
7141
	struct drm_connector *connector;
7152
	struct drm_connector *connector;
7142
	int count;
7153
	int count;
7143
 
7154
 
7144
	config->save_encoder_crtcs =
7155
	config->save_encoder_crtcs =
7145
		kcalloc(dev->mode_config.num_encoder,
7156
		kcalloc(dev->mode_config.num_encoder,
7146
			sizeof(struct drm_crtc *), GFP_KERNEL);
7157
			sizeof(struct drm_crtc *), GFP_KERNEL);
7147
	if (!config->save_encoder_crtcs)
7158
	if (!config->save_encoder_crtcs)
7148
		return -ENOMEM;
7159
		return -ENOMEM;
7149
 
7160
 
7150
	config->save_connector_encoders =
7161
	config->save_connector_encoders =
7151
		kcalloc(dev->mode_config.num_connector,
7162
		kcalloc(dev->mode_config.num_connector,
7152
			sizeof(struct drm_encoder *), GFP_KERNEL);
7163
			sizeof(struct drm_encoder *), GFP_KERNEL);
7153
	if (!config->save_connector_encoders)
7164
	if (!config->save_connector_encoders)
7154
		return -ENOMEM;
7165
		return -ENOMEM;
7155
 
7166
 
7156
	/* Copy data. Note that driver private data is not affected.
7167
	/* Copy data. Note that driver private data is not affected.
7157
	 * Should anything bad happen only the expected state is
7168
	 * Should anything bad happen only the expected state is
7158
	 * restored, not the drivers personal bookkeeping.
7169
	 * restored, not the drivers personal bookkeeping.
7159
	 */
7170
	 */
7160
	count = 0;
7171
	count = 0;
7161
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
7172
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
7162
		config->save_encoder_crtcs[count++] = encoder->crtc;
7173
		config->save_encoder_crtcs[count++] = encoder->crtc;
7163
	}
7174
	}
7164
 
7175
 
7165
	count = 0;
7176
	count = 0;
7166
	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
7177
	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
7167
		config->save_connector_encoders[count++] = connector->encoder;
7178
		config->save_connector_encoders[count++] = connector->encoder;
7168
	}
7179
	}
7169
 
7180
 
7170
	return 0;
7181
	return 0;
7171
}
7182
}
7172
 
7183
 
7173
static void intel_set_config_restore_state(struct drm_device *dev,
7184
static void intel_set_config_restore_state(struct drm_device *dev,
7174
					   struct intel_set_config *config)
7185
					   struct intel_set_config *config)
7175
{
7186
{
7176
	struct intel_encoder *encoder;
7187
	struct intel_encoder *encoder;
7177
	struct intel_connector *connector;
7188
	struct intel_connector *connector;
7178
	int count;
7189
	int count;
7179
 
7190
 
7180
	count = 0;
7191
	count = 0;
7181
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
7192
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
7182
		encoder->new_crtc =
7193
		encoder->new_crtc =
7183
			to_intel_crtc(config->save_encoder_crtcs[count++]);
7194
			to_intel_crtc(config->save_encoder_crtcs[count++]);
7184
	}
7195
	}
7185
 
7196
 
7186
	count = 0;
7197
	count = 0;
7187
	list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
7198
	list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
7188
		connector->new_encoder =
7199
		connector->new_encoder =
7189
			to_intel_encoder(config->save_connector_encoders[count++]);
7200
			to_intel_encoder(config->save_connector_encoders[count++]);
7190
	}
7201
	}
7191
}
7202
}
7192
 
7203
 
7193
static void
7204
static void
7194
intel_set_config_compute_mode_changes(struct drm_mode_set *set,
7205
intel_set_config_compute_mode_changes(struct drm_mode_set *set,
7195
				      struct intel_set_config *config)
7206
				      struct intel_set_config *config)
7196
{
7207
{
7197
 
7208
 
7198
	/* We should be able to check here if the fb has the same properties
7209
	/* We should be able to check here if the fb has the same properties
7199
	 * and then just flip_or_move it */
7210
	 * and then just flip_or_move it */
7200
	if (set->crtc->fb != set->fb) {
7211
	if (set->crtc->fb != set->fb) {
7201
		/* If we have no fb then treat it as a full mode set */
7212
		/* If we have no fb then treat it as a full mode set */
7202
		if (set->crtc->fb == NULL) {
7213
		if (set->crtc->fb == NULL) {
7203
			DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
7214
			DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
7204
			config->mode_changed = true;
7215
			config->mode_changed = true;
7205
		} else if (set->fb == NULL) {
7216
		} else if (set->fb == NULL) {
7206
			config->mode_changed = true;
7217
			config->mode_changed = true;
7207
		} else if (set->fb->depth != set->crtc->fb->depth) {
7218
		} else if (set->fb->depth != set->crtc->fb->depth) {
7208
			config->mode_changed = true;
7219
			config->mode_changed = true;
7209
		} else if (set->fb->bits_per_pixel !=
7220
		} else if (set->fb->bits_per_pixel !=
7210
			   set->crtc->fb->bits_per_pixel) {
7221
			   set->crtc->fb->bits_per_pixel) {
7211
			config->mode_changed = true;
7222
			config->mode_changed = true;
7212
		} else
7223
		} else
7213
			config->fb_changed = true;
7224
			config->fb_changed = true;
7214
	}
7225
	}
7215
 
7226
 
7216
	if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
7227
	if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
7217
		config->fb_changed = true;
7228
		config->fb_changed = true;
7218
 
7229
 
7219
	if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
7230
	if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
7220
		DRM_DEBUG_KMS("modes are different, full mode set\n");
7231
		DRM_DEBUG_KMS("modes are different, full mode set\n");
7221
		drm_mode_debug_printmodeline(&set->crtc->mode);
7232
		drm_mode_debug_printmodeline(&set->crtc->mode);
7222
		drm_mode_debug_printmodeline(set->mode);
7233
		drm_mode_debug_printmodeline(set->mode);
7223
		config->mode_changed = true;
7234
		config->mode_changed = true;
7224
	}
7235
	}
7225
}
7236
}
7226
 
7237
 
7227
static int
7238
static int
7228
intel_modeset_stage_output_state(struct drm_device *dev,
7239
intel_modeset_stage_output_state(struct drm_device *dev,
7229
				 struct drm_mode_set *set,
7240
				 struct drm_mode_set *set,
7230
				 struct intel_set_config *config)
7241
				 struct intel_set_config *config)
7231
{
7242
{
7232
	struct drm_crtc *new_crtc;
7243
	struct drm_crtc *new_crtc;
7233
	struct intel_connector *connector;
7244
	struct intel_connector *connector;
7234
	struct intel_encoder *encoder;
7245
	struct intel_encoder *encoder;
7235
	int count, ro;
7246
	int count, ro;
7236
 
7247
 
7237
	/* The upper layers ensure that we either disabl a crtc or have a list
7248
	/* The upper layers ensure that we either disabl a crtc or have a list
7238
	 * of connectors. For paranoia, double-check this. */
7249
	 * of connectors. For paranoia, double-check this. */
7239
	WARN_ON(!set->fb && (set->num_connectors != 0));
7250
	WARN_ON(!set->fb && (set->num_connectors != 0));
7240
	WARN_ON(set->fb && (set->num_connectors == 0));
7251
	WARN_ON(set->fb && (set->num_connectors == 0));
7241
 
7252
 
7242
	count = 0;
7253
	count = 0;
7243
	list_for_each_entry(connector, &dev->mode_config.connector_list,
7254
	list_for_each_entry(connector, &dev->mode_config.connector_list,
7244
			    base.head) {
7255
			    base.head) {
7245
		/* Otherwise traverse passed in connector list and get encoders
7256
		/* Otherwise traverse passed in connector list and get encoders
7246
		 * for them. */
7257
		 * for them. */
7247
		for (ro = 0; ro < set->num_connectors; ro++) {
7258
		for (ro = 0; ro < set->num_connectors; ro++) {
7248
			if (set->connectors[ro] == &connector->base) {
7259
			if (set->connectors[ro] == &connector->base) {
7249
				connector->new_encoder = connector->encoder;
7260
				connector->new_encoder = connector->encoder;
7250
				break;
7261
				break;
7251
			}
7262
			}
7252
		}
7263
		}
7253
 
7264
 
7254
		/* If we disable the crtc, disable all its connectors. Also, if
7265
		/* If we disable the crtc, disable all its connectors. Also, if
7255
		 * the connector is on the changing crtc but not on the new
7266
		 * the connector is on the changing crtc but not on the new
7256
		 * connector list, disable it. */
7267
		 * connector list, disable it. */
7257
		if ((!set->fb || ro == set->num_connectors) &&
7268
		if ((!set->fb || ro == set->num_connectors) &&
7258
		    connector->base.encoder &&
7269
		    connector->base.encoder &&
7259
		    connector->base.encoder->crtc == set->crtc) {
7270
		    connector->base.encoder->crtc == set->crtc) {
7260
			connector->new_encoder = NULL;
7271
			connector->new_encoder = NULL;
7261
 
7272
 
7262
			DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
7273
			DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
7263
				connector->base.base.id,
7274
				connector->base.base.id,
7264
				drm_get_connector_name(&connector->base));
7275
				drm_get_connector_name(&connector->base));
7265
		}
7276
		}
7266
 
7277
 
7267
 
7278
 
7268
		if (&connector->new_encoder->base != connector->base.encoder) {
7279
		if (&connector->new_encoder->base != connector->base.encoder) {
7269
			DRM_DEBUG_KMS("encoder changed, full mode switch\n");
7280
			DRM_DEBUG_KMS("encoder changed, full mode switch\n");
7270
			config->mode_changed = true;
7281
			config->mode_changed = true;
7271
		}
7282
		}
7272
 
7283
 
7273
		/* Disable all disconnected encoders. */
7284
		/* Disable all disconnected encoders. */
7274
		if (connector->base.status == connector_status_disconnected)
7285
		if (connector->base.status == connector_status_disconnected)
7275
			connector->new_encoder = NULL;
7286
			connector->new_encoder = NULL;
7276
	}
7287
	}
7277
	/* connector->new_encoder is now updated for all connectors. */
7288
	/* connector->new_encoder is now updated for all connectors. */
7278
 
7289
 
7279
	/* Update crtc of enabled connectors. */
7290
	/* Update crtc of enabled connectors. */
7280
	count = 0;
7291
	count = 0;
7281
	list_for_each_entry(connector, &dev->mode_config.connector_list,
7292
	list_for_each_entry(connector, &dev->mode_config.connector_list,
7282
			    base.head) {
7293
			    base.head) {
7283
		if (!connector->new_encoder)
7294
		if (!connector->new_encoder)
7284
			continue;
7295
			continue;
7285
 
7296
 
7286
		new_crtc = connector->new_encoder->base.crtc;
7297
		new_crtc = connector->new_encoder->base.crtc;
7287
 
7298
 
7288
		for (ro = 0; ro < set->num_connectors; ro++) {
7299
		for (ro = 0; ro < set->num_connectors; ro++) {
7289
			if (set->connectors[ro] == &connector->base)
7300
			if (set->connectors[ro] == &connector->base)
7290
				new_crtc = set->crtc;
7301
				new_crtc = set->crtc;
7291
		}
7302
		}
7292
 
7303
 
7293
		/* Make sure the new CRTC will work with the encoder */
7304
		/* Make sure the new CRTC will work with the encoder */
7294
		if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
7305
		if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
7295
					   new_crtc)) {
7306
					   new_crtc)) {
7296
			return -EINVAL;
7307
			return -EINVAL;
7297
		}
7308
		}
7298
		connector->encoder->new_crtc = to_intel_crtc(new_crtc);
7309
		connector->encoder->new_crtc = to_intel_crtc(new_crtc);
7299
 
7310
 
7300
		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
7311
		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
7301
			connector->base.base.id,
7312
			connector->base.base.id,
7302
			drm_get_connector_name(&connector->base),
7313
			drm_get_connector_name(&connector->base),
7303
			new_crtc->base.id);
7314
			new_crtc->base.id);
7304
	}
7315
	}
7305
 
7316
 
7306
	/* Check for any encoders that needs to be disabled. */
7317
	/* Check for any encoders that needs to be disabled. */
7307
	list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7318
	list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7308
			    base.head) {
7319
			    base.head) {
7309
		list_for_each_entry(connector,
7320
		list_for_each_entry(connector,
7310
				    &dev->mode_config.connector_list,
7321
				    &dev->mode_config.connector_list,
7311
				    base.head) {
7322
				    base.head) {
7312
			if (connector->new_encoder == encoder) {
7323
			if (connector->new_encoder == encoder) {
7313
				WARN_ON(!connector->new_encoder->new_crtc);
7324
				WARN_ON(!connector->new_encoder->new_crtc);
7314
 
7325
 
7315
				goto next_encoder;
7326
				goto next_encoder;
7316
			}
7327
			}
7317
		}
7328
		}
7318
		encoder->new_crtc = NULL;
7329
		encoder->new_crtc = NULL;
7319
next_encoder:
7330
next_encoder:
7320
		/* Only now check for crtc changes so we don't miss encoders
7331
		/* Only now check for crtc changes so we don't miss encoders
7321
		 * that will be disabled. */
7332
		 * that will be disabled. */
7322
		if (&encoder->new_crtc->base != encoder->base.crtc) {
7333
		if (&encoder->new_crtc->base != encoder->base.crtc) {
7323
			DRM_DEBUG_KMS("crtc changed, full mode switch\n");
7334
			DRM_DEBUG_KMS("crtc changed, full mode switch\n");
7324
			config->mode_changed = true;
7335
			config->mode_changed = true;
7325
		}
7336
		}
7326
	}
7337
	}
7327
	/* Now we've also updated encoder->new_crtc for all encoders. */
7338
	/* Now we've also updated encoder->new_crtc for all encoders. */
7328
 
7339
 
7329
	return 0;
7340
	return 0;
7330
}
7341
}
7331
 
7342
 
7332
static int intel_crtc_set_config(struct drm_mode_set *set)
7343
static int intel_crtc_set_config(struct drm_mode_set *set)
7333
{
7344
{
7334
	struct drm_device *dev;
7345
	struct drm_device *dev;
7335
	struct drm_mode_set save_set;
7346
	struct drm_mode_set save_set;
7336
	struct intel_set_config *config;
7347
	struct intel_set_config *config;
7337
	int ret;
7348
	int ret;
7338
 
7349
 
7339
	BUG_ON(!set);
7350
	BUG_ON(!set);
7340
	BUG_ON(!set->crtc);
7351
	BUG_ON(!set->crtc);
7341
	BUG_ON(!set->crtc->helper_private);
7352
	BUG_ON(!set->crtc->helper_private);
7342
 
7353
 
7343
	if (!set->mode)
7354
	if (!set->mode)
7344
		set->fb = NULL;
7355
		set->fb = NULL;
7345
 
7356
 
7346
	/* The fb helper likes to play gross jokes with ->mode_set_config.
7357
	/* The fb helper likes to play gross jokes with ->mode_set_config.
7347
	 * Unfortunately the crtc helper doesn't do much at all for this case,
7358
	 * Unfortunately the crtc helper doesn't do much at all for this case,
7348
	 * so we have to cope with this madness until the fb helper is fixed up. */
7359
	 * so we have to cope with this madness until the fb helper is fixed up. */
7349
	if (set->fb && set->num_connectors == 0)
7360
	if (set->fb && set->num_connectors == 0)
7350
		return 0;
7361
		return 0;
7351
 
7362
 
7352
	if (set->fb) {
7363
	if (set->fb) {
7353
		DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
7364
		DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
7354
				set->crtc->base.id, set->fb->base.id,
7365
				set->crtc->base.id, set->fb->base.id,
7355
				(int)set->num_connectors, set->x, set->y);
7366
				(int)set->num_connectors, set->x, set->y);
7356
	} else {
7367
	} else {
7357
		DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
7368
		DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
7358
	}
7369
	}
7359
 
7370
 
7360
	dev = set->crtc->dev;
7371
	dev = set->crtc->dev;
7361
 
7372
 
7362
	ret = -ENOMEM;
7373
	ret = -ENOMEM;
7363
	config = kzalloc(sizeof(*config), GFP_KERNEL);
7374
	config = kzalloc(sizeof(*config), GFP_KERNEL);
7364
	if (!config)
7375
	if (!config)
7365
		goto out_config;
7376
		goto out_config;
7366
 
7377
 
7367
	ret = intel_set_config_save_state(dev, config);
7378
	ret = intel_set_config_save_state(dev, config);
7368
	if (ret)
7379
	if (ret)
7369
		goto out_config;
7380
		goto out_config;
7370
 
7381
 
7371
	save_set.crtc = set->crtc;
7382
	save_set.crtc = set->crtc;
7372
	save_set.mode = &set->crtc->mode;
7383
	save_set.mode = &set->crtc->mode;
7373
	save_set.x = set->crtc->x;
7384
	save_set.x = set->crtc->x;
7374
	save_set.y = set->crtc->y;
7385
	save_set.y = set->crtc->y;
7375
	save_set.fb = set->crtc->fb;
7386
	save_set.fb = set->crtc->fb;
7376
 
7387
 
7377
	/* Compute whether we need a full modeset, only an fb base update or no
7388
	/* Compute whether we need a full modeset, only an fb base update or no
7378
	 * change at all. In the future we might also check whether only the
7389
	 * change at all. In the future we might also check whether only the
7379
	 * mode changed, e.g. for LVDS where we only change the panel fitter in
7390
	 * mode changed, e.g. for LVDS where we only change the panel fitter in
7380
	 * such cases. */
7391
	 * such cases. */
7381
	intel_set_config_compute_mode_changes(set, config);
7392
	intel_set_config_compute_mode_changes(set, config);
7382
 
7393
 
7383
	ret = intel_modeset_stage_output_state(dev, set, config);
7394
	ret = intel_modeset_stage_output_state(dev, set, config);
7384
	if (ret)
7395
	if (ret)
7385
		goto fail;
7396
		goto fail;
7386
 
7397
 
7387
	if (config->mode_changed) {
7398
	if (config->mode_changed) {
7388
		if (set->mode) {
7399
		if (set->mode) {
7389
			DRM_DEBUG_KMS("attempting to set mode from"
7400
			DRM_DEBUG_KMS("attempting to set mode from"
7390
					" userspace\n");
7401
					" userspace\n");
7391
			drm_mode_debug_printmodeline(set->mode);
7402
			drm_mode_debug_printmodeline(set->mode);
7392
		}
7403
		}
7393
 
7404
 
7394
		if (!intel_set_mode(set->crtc, set->mode,
7405
		if (!intel_set_mode(set->crtc, set->mode,
7395
				    set->x, set->y, set->fb)) {
7406
				    set->x, set->y, set->fb)) {
7396
			DRM_ERROR("failed to set mode on [CRTC:%d]\n",
7407
			DRM_ERROR("failed to set mode on [CRTC:%d]\n",
7397
				  set->crtc->base.id);
7408
				  set->crtc->base.id);
7398
			ret = -EINVAL;
7409
			ret = -EINVAL;
7399
			goto fail;
7410
			goto fail;
7400
		}
7411
		}
7401
	} else if (config->fb_changed) {
7412
	} else if (config->fb_changed) {
7402
		ret = intel_pipe_set_base(set->crtc,
7413
		ret = intel_pipe_set_base(set->crtc,
7403
					  set->x, set->y, set->fb);
7414
					  set->x, set->y, set->fb);
7404
	}
7415
	}
7405
 
7416
 
7406
	intel_set_config_free(config);
7417
	intel_set_config_free(config);
7407
 
7418
 
7408
	return 0;
7419
	return 0;
7409
 
7420
 
7410
fail:
7421
fail:
7411
	intel_set_config_restore_state(dev, config);
7422
	intel_set_config_restore_state(dev, config);
7412
 
7423
 
7413
	/* Try to restore the config */
7424
	/* Try to restore the config */
7414
	if (config->mode_changed &&
7425
	if (config->mode_changed &&
7415
	    !intel_set_mode(save_set.crtc, save_set.mode,
7426
	    !intel_set_mode(save_set.crtc, save_set.mode,
7416
			    save_set.x, save_set.y, save_set.fb))
7427
			    save_set.x, save_set.y, save_set.fb))
7417
		DRM_ERROR("failed to restore config after modeset failure\n");
7428
		DRM_ERROR("failed to restore config after modeset failure\n");
7418
 
7429
 
7419
out_config:
7430
out_config:
7420
	intel_set_config_free(config);
7431
	intel_set_config_free(config);
7421
	return ret;
7432
	return ret;
7422
}
7433
}
7423
 
7434
 
7424
static const struct drm_crtc_funcs intel_crtc_funcs = {
7435
static const struct drm_crtc_funcs intel_crtc_funcs = {
7425
//	.cursor_set = intel_crtc_cursor_set,
7436
//	.cursor_set = intel_crtc_cursor_set,
7426
//	.cursor_move = intel_crtc_cursor_move,
7437
//	.cursor_move = intel_crtc_cursor_move,
7427
	.gamma_set = intel_crtc_gamma_set,
7438
	.gamma_set = intel_crtc_gamma_set,
7428
	.set_config = intel_crtc_set_config,
7439
	.set_config = intel_crtc_set_config,
7429
	.destroy = intel_crtc_destroy,
7440
	.destroy = intel_crtc_destroy,
7430
//	.page_flip = intel_crtc_page_flip,
7441
//	.page_flip = intel_crtc_page_flip,
7431
};
7442
};
7432
 
7443
 
7433
static void intel_pch_pll_init(struct drm_device *dev)
7444
static void intel_pch_pll_init(struct drm_device *dev)
7434
{
7445
{
7435
	drm_i915_private_t *dev_priv = dev->dev_private;
7446
	drm_i915_private_t *dev_priv = dev->dev_private;
7436
	int i;
7447
	int i;
7437
 
7448
 
7438
	if (dev_priv->num_pch_pll == 0) {
7449
	if (dev_priv->num_pch_pll == 0) {
7439
		DRM_DEBUG_KMS("No PCH PLLs on this hardware, skipping initialisation\n");
7450
		DRM_DEBUG_KMS("No PCH PLLs on this hardware, skipping initialisation\n");
7440
		return;
7451
		return;
7441
	}
7452
	}
7442
 
7453
 
7443
	for (i = 0; i < dev_priv->num_pch_pll; i++) {
7454
	for (i = 0; i < dev_priv->num_pch_pll; i++) {
7444
		dev_priv->pch_plls[i].pll_reg = _PCH_DPLL(i);
7455
		dev_priv->pch_plls[i].pll_reg = _PCH_DPLL(i);
7445
		dev_priv->pch_plls[i].fp0_reg = _PCH_FP0(i);
7456
		dev_priv->pch_plls[i].fp0_reg = _PCH_FP0(i);
7446
		dev_priv->pch_plls[i].fp1_reg = _PCH_FP1(i);
7457
		dev_priv->pch_plls[i].fp1_reg = _PCH_FP1(i);
7447
	}
7458
	}
7448
}
7459
}
7449
 
7460
 
7450
static void intel_crtc_init(struct drm_device *dev, int pipe)
7461
static void intel_crtc_init(struct drm_device *dev, int pipe)
7451
{
7462
{
7452
	drm_i915_private_t *dev_priv = dev->dev_private;
7463
	drm_i915_private_t *dev_priv = dev->dev_private;
7453
	struct intel_crtc *intel_crtc;
7464
	struct intel_crtc *intel_crtc;
7454
	int i;
7465
	int i;
7455
 
7466
 
7456
	intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
7467
	intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
7457
	if (intel_crtc == NULL)
7468
	if (intel_crtc == NULL)
7458
		return;
7469
		return;
7459
 
7470
 
7460
	drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
7471
	drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
7461
 
7472
 
7462
	drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
7473
	drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
7463
	for (i = 0; i < 256; i++) {
7474
	for (i = 0; i < 256; i++) {
7464
		intel_crtc->lut_r[i] = i;
7475
		intel_crtc->lut_r[i] = i;
7465
		intel_crtc->lut_g[i] = i;
7476
		intel_crtc->lut_g[i] = i;
7466
		intel_crtc->lut_b[i] = i;
7477
		intel_crtc->lut_b[i] = i;
7467
	}
7478
	}
7468
 
7479
 
7469
	/* Swap pipes & planes for FBC on pre-965 */
7480
	/* Swap pipes & planes for FBC on pre-965 */
7470
	intel_crtc->pipe = pipe;
7481
	intel_crtc->pipe = pipe;
7471
	intel_crtc->plane = pipe;
7482
	intel_crtc->plane = pipe;
7472
	if (IS_MOBILE(dev) && IS_GEN3(dev)) {
7483
	if (IS_MOBILE(dev) && IS_GEN3(dev)) {
7473
		DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
7484
		DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
7474
		intel_crtc->plane = !pipe;
7485
		intel_crtc->plane = !pipe;
7475
	}
7486
	}
7476
 
7487
 
7477
	BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
7488
	BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
7478
	       dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
7489
	       dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
7479
	dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
7490
	dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
7480
	dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
7491
	dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
7481
 
7492
 
7482
	intel_crtc->bpp = 24; /* default for pre-Ironlake */
7493
	intel_crtc->bpp = 24; /* default for pre-Ironlake */
7483
 
7494
 
7484
	drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
7495
	drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
7485
 
7496
 
7486
    DRM_DEBUG_KMS("CRTC %d mode %x FB %x enable %d\n",
7497
    DRM_DEBUG_KMS("CRTC %d mode %x FB %x enable %d\n",
7487
            intel_crtc->base.base.id, intel_crtc->base.mode,
7498
            intel_crtc->base.base.id, intel_crtc->base.mode,
7488
            intel_crtc->base.fb, intel_crtc->base.enabled);
7499
            intel_crtc->base.fb, intel_crtc->base.enabled);
7489
 
7500
 
7490
}
7501
}
7491
 
7502
 
7492
int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
7503
int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
7493
				struct drm_file *file)
7504
				struct drm_file *file)
7494
{
7505
{
7495
	struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7506
	struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7496
	struct drm_mode_object *drmmode_obj;
7507
	struct drm_mode_object *drmmode_obj;
7497
	struct intel_crtc *crtc;
7508
	struct intel_crtc *crtc;
7498
 
7509
 
7499
	drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
7510
	drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
7500
			DRM_MODE_OBJECT_CRTC);
7511
			DRM_MODE_OBJECT_CRTC);
7501
 
7512
 
7502
	if (!drmmode_obj) {
7513
	if (!drmmode_obj) {
7503
		DRM_ERROR("no such CRTC id\n");
7514
		DRM_ERROR("no such CRTC id\n");
7504
		return -EINVAL;
7515
		return -EINVAL;
7505
	}
7516
	}
7506
 
7517
 
7507
	crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
7518
	crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
7508
	pipe_from_crtc_id->pipe = crtc->pipe;
7519
	pipe_from_crtc_id->pipe = crtc->pipe;
7509
 
7520
 
7510
	return 0;
7521
	return 0;
7511
}
7522
}
7512
 
7523
 
7513
static int intel_encoder_clones(struct intel_encoder *encoder)
7524
static int intel_encoder_clones(struct intel_encoder *encoder)
7514
{
7525
{
7515
	struct drm_device *dev = encoder->base.dev;
7526
	struct drm_device *dev = encoder->base.dev;
7516
	struct intel_encoder *source_encoder;
7527
	struct intel_encoder *source_encoder;
7517
	int index_mask = 0;
7528
	int index_mask = 0;
7518
	int entry = 0;
7529
	int entry = 0;
7519
 
7530
 
7520
	list_for_each_entry(source_encoder,
7531
	list_for_each_entry(source_encoder,
7521
			    &dev->mode_config.encoder_list, base.head) {
7532
			    &dev->mode_config.encoder_list, base.head) {
7522
 
7533
 
7523
		if (encoder == source_encoder)
7534
		if (encoder == source_encoder)
7524
			index_mask |= (1 << entry);
7535
			index_mask |= (1 << entry);
7525
 
7536
 
7526
		/* Intel hw has only one MUX where enocoders could be cloned. */
7537
		/* Intel hw has only one MUX where enocoders could be cloned. */
7527
		if (encoder->cloneable && source_encoder->cloneable)
7538
		if (encoder->cloneable && source_encoder->cloneable)
7528
			index_mask |= (1 << entry);
7539
			index_mask |= (1 << entry);
7529
 
7540
 
7530
		entry++;
7541
		entry++;
7531
	}
7542
	}
7532
 
7543
 
7533
	return index_mask;
7544
	return index_mask;
7534
}
7545
}
7535
 
7546
 
7536
static bool has_edp_a(struct drm_device *dev)
7547
static bool has_edp_a(struct drm_device *dev)
7537
{
7548
{
7538
	struct drm_i915_private *dev_priv = dev->dev_private;
7549
	struct drm_i915_private *dev_priv = dev->dev_private;
7539
 
7550
 
7540
	if (!IS_MOBILE(dev))
7551
	if (!IS_MOBILE(dev))
7541
		return false;
7552
		return false;
7542
 
7553
 
7543
	if ((I915_READ(DP_A) & DP_DETECTED) == 0)
7554
	if ((I915_READ(DP_A) & DP_DETECTED) == 0)
7544
		return false;
7555
		return false;
7545
 
7556
 
7546
	if (IS_GEN5(dev) &&
7557
	if (IS_GEN5(dev) &&
7547
	    (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
7558
	    (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
7548
		return false;
7559
		return false;
7549
 
7560
 
7550
	return true;
7561
	return true;
7551
}
7562
}
7552
 
7563
 
7553
static void intel_setup_outputs(struct drm_device *dev)
7564
static void intel_setup_outputs(struct drm_device *dev)
7554
{
7565
{
7555
	struct drm_i915_private *dev_priv = dev->dev_private;
7566
	struct drm_i915_private *dev_priv = dev->dev_private;
7556
	struct intel_encoder *encoder;
7567
	struct intel_encoder *encoder;
7557
	bool dpd_is_edp = false;
7568
	bool dpd_is_edp = false;
7558
	bool has_lvds;
7569
	bool has_lvds;
7559
 
7570
 
7560
		has_lvds = intel_lvds_init(dev);
7571
		has_lvds = intel_lvds_init(dev);
7561
	if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
7572
	if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
7562
		/* disable the panel fitter on everything but LVDS */
7573
		/* disable the panel fitter on everything but LVDS */
7563
		I915_WRITE(PFIT_CONTROL, 0);
7574
		I915_WRITE(PFIT_CONTROL, 0);
7564
	}
7575
	}
7565
 
7576
 
7566
	if (HAS_PCH_SPLIT(dev)) {
7577
	if (HAS_PCH_SPLIT(dev)) {
7567
		dpd_is_edp = intel_dpd_is_edp(dev);
7578
		dpd_is_edp = intel_dpd_is_edp(dev);
7568
 
7579
 
7569
		if (has_edp_a(dev))
7580
		if (has_edp_a(dev))
7570
			intel_dp_init(dev, DP_A, PORT_A);
7581
			intel_dp_init(dev, DP_A, PORT_A);
7571
 
7582
 
7572
		if (dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
7583
		if (dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
7573
			intel_dp_init(dev, PCH_DP_D, PORT_D);
7584
			intel_dp_init(dev, PCH_DP_D, PORT_D);
7574
	}
7585
	}
7575
 
7586
 
7576
	intel_crt_init(dev);
7587
	intel_crt_init(dev);
7577
 
7588
 
7578
	if (IS_HASWELL(dev)) {
7589
	if (IS_HASWELL(dev)) {
7579
		int found;
7590
		int found;
7580
 
7591
 
7581
		/* Haswell uses DDI functions to detect digital outputs */
7592
		/* Haswell uses DDI functions to detect digital outputs */
7582
		found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
7593
		found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
7583
		/* DDI A only supports eDP */
7594
		/* DDI A only supports eDP */
7584
		if (found)
7595
		if (found)
7585
			intel_ddi_init(dev, PORT_A);
7596
			intel_ddi_init(dev, PORT_A);
7586
 
7597
 
7587
		/* DDI B, C and D detection is indicated by the SFUSE_STRAP
7598
		/* DDI B, C and D detection is indicated by the SFUSE_STRAP
7588
		 * register */
7599
		 * register */
7589
		found = I915_READ(SFUSE_STRAP);
7600
		found = I915_READ(SFUSE_STRAP);
7590
 
7601
 
7591
		if (found & SFUSE_STRAP_DDIB_DETECTED)
7602
		if (found & SFUSE_STRAP_DDIB_DETECTED)
7592
			intel_ddi_init(dev, PORT_B);
7603
			intel_ddi_init(dev, PORT_B);
7593
		if (found & SFUSE_STRAP_DDIC_DETECTED)
7604
		if (found & SFUSE_STRAP_DDIC_DETECTED)
7594
			intel_ddi_init(dev, PORT_C);
7605
			intel_ddi_init(dev, PORT_C);
7595
		if (found & SFUSE_STRAP_DDID_DETECTED)
7606
		if (found & SFUSE_STRAP_DDID_DETECTED)
7596
			intel_ddi_init(dev, PORT_D);
7607
			intel_ddi_init(dev, PORT_D);
7597
	} else if (HAS_PCH_SPLIT(dev)) {
7608
	} else if (HAS_PCH_SPLIT(dev)) {
7598
		int found;
7609
		int found;
7599
 
7610
 
7600
		if (I915_READ(HDMIB) & PORT_DETECTED) {
7611
		if (I915_READ(HDMIB) & PORT_DETECTED) {
7601
			/* PCH SDVOB multiplex with HDMIB */
7612
			/* PCH SDVOB multiplex with HDMIB */
7602
			found = intel_sdvo_init(dev, PCH_SDVOB, true);
7613
			found = intel_sdvo_init(dev, PCH_SDVOB, true);
7603
			if (!found)
7614
			if (!found)
7604
				intel_hdmi_init(dev, HDMIB, PORT_B);
7615
				intel_hdmi_init(dev, HDMIB, PORT_B);
7605
			if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
7616
			if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
7606
				intel_dp_init(dev, PCH_DP_B, PORT_B);
7617
				intel_dp_init(dev, PCH_DP_B, PORT_B);
7607
		}
7618
		}
7608
 
7619
 
7609
		if (I915_READ(HDMIC) & PORT_DETECTED)
7620
		if (I915_READ(HDMIC) & PORT_DETECTED)
7610
			intel_hdmi_init(dev, HDMIC, PORT_C);
7621
			intel_hdmi_init(dev, HDMIC, PORT_C);
7611
 
7622
 
7612
		if (!dpd_is_edp && I915_READ(HDMID) & PORT_DETECTED)
7623
		if (!dpd_is_edp && I915_READ(HDMID) & PORT_DETECTED)
7613
			intel_hdmi_init(dev, HDMID, PORT_D);
7624
			intel_hdmi_init(dev, HDMID, PORT_D);
7614
 
7625
 
7615
		if (I915_READ(PCH_DP_C) & DP_DETECTED)
7626
		if (I915_READ(PCH_DP_C) & DP_DETECTED)
7616
			intel_dp_init(dev, PCH_DP_C, PORT_C);
7627
			intel_dp_init(dev, PCH_DP_C, PORT_C);
7617
 
7628
 
7618
		if (!dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
7629
		if (!dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
7619
			intel_dp_init(dev, PCH_DP_D, PORT_D);
7630
			intel_dp_init(dev, PCH_DP_D, PORT_D);
7620
	} else if (IS_VALLEYVIEW(dev)) {
7631
	} else if (IS_VALLEYVIEW(dev)) {
7621
		int found;
7632
		int found;
7622
 
7633
 
7623
		if (I915_READ(SDVOB) & PORT_DETECTED) {
7634
		if (I915_READ(SDVOB) & PORT_DETECTED) {
7624
			/* SDVOB multiplex with HDMIB */
7635
			/* SDVOB multiplex with HDMIB */
7625
			found = intel_sdvo_init(dev, SDVOB, true);
7636
			found = intel_sdvo_init(dev, SDVOB, true);
7626
			if (!found)
7637
			if (!found)
7627
				intel_hdmi_init(dev, SDVOB, PORT_B);
7638
				intel_hdmi_init(dev, SDVOB, PORT_B);
7628
			if (!found && (I915_READ(DP_B) & DP_DETECTED))
7639
			if (!found && (I915_READ(DP_B) & DP_DETECTED))
7629
				intel_dp_init(dev, DP_B, PORT_B);
7640
				intel_dp_init(dev, DP_B, PORT_B);
7630
		}
7641
		}
7631
 
7642
 
7632
		if (I915_READ(SDVOC) & PORT_DETECTED)
7643
		if (I915_READ(SDVOC) & PORT_DETECTED)
7633
			intel_hdmi_init(dev, SDVOC, PORT_C);
7644
			intel_hdmi_init(dev, SDVOC, PORT_C);
7634
 
7645
 
7635
		/* Shares lanes with HDMI on SDVOC */
7646
		/* Shares lanes with HDMI on SDVOC */
7636
		if (I915_READ(DP_C) & DP_DETECTED)
7647
		if (I915_READ(DP_C) & DP_DETECTED)
7637
			intel_dp_init(dev, DP_C, PORT_C);
7648
			intel_dp_init(dev, DP_C, PORT_C);
7638
	} else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
7649
	} else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
7639
		bool found = false;
7650
		bool found = false;
7640
 
7651
 
7641
		if (I915_READ(SDVOB) & SDVO_DETECTED) {
7652
		if (I915_READ(SDVOB) & SDVO_DETECTED) {
7642
			DRM_DEBUG_KMS("probing SDVOB\n");
7653
			DRM_DEBUG_KMS("probing SDVOB\n");
7643
			found = intel_sdvo_init(dev, SDVOB, true);
7654
			found = intel_sdvo_init(dev, SDVOB, true);
7644
			if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
7655
			if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
7645
				DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
7656
				DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
7646
				intel_hdmi_init(dev, SDVOB, PORT_B);
7657
				intel_hdmi_init(dev, SDVOB, PORT_B);
7647
			}
7658
			}
7648
 
7659
 
7649
			if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
7660
			if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
7650
				DRM_DEBUG_KMS("probing DP_B\n");
7661
				DRM_DEBUG_KMS("probing DP_B\n");
7651
				intel_dp_init(dev, DP_B, PORT_B);
7662
				intel_dp_init(dev, DP_B, PORT_B);
7652
			}
7663
			}
7653
		}
7664
		}
7654
 
7665
 
7655
		/* Before G4X SDVOC doesn't have its own detect register */
7666
		/* Before G4X SDVOC doesn't have its own detect register */
7656
 
7667
 
7657
		if (I915_READ(SDVOB) & SDVO_DETECTED) {
7668
		if (I915_READ(SDVOB) & SDVO_DETECTED) {
7658
			DRM_DEBUG_KMS("probing SDVOC\n");
7669
			DRM_DEBUG_KMS("probing SDVOC\n");
7659
			found = intel_sdvo_init(dev, SDVOC, false);
7670
			found = intel_sdvo_init(dev, SDVOC, false);
7660
		}
7671
		}
7661
 
7672
 
7662
		if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
7673
		if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
7663
 
7674
 
7664
			if (SUPPORTS_INTEGRATED_HDMI(dev)) {
7675
			if (SUPPORTS_INTEGRATED_HDMI(dev)) {
7665
				DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
7676
				DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
7666
				intel_hdmi_init(dev, SDVOC, PORT_C);
7677
				intel_hdmi_init(dev, SDVOC, PORT_C);
7667
			}
7678
			}
7668
			if (SUPPORTS_INTEGRATED_DP(dev)) {
7679
			if (SUPPORTS_INTEGRATED_DP(dev)) {
7669
				DRM_DEBUG_KMS("probing DP_C\n");
7680
				DRM_DEBUG_KMS("probing DP_C\n");
7670
				intel_dp_init(dev, DP_C, PORT_C);
7681
				intel_dp_init(dev, DP_C, PORT_C);
7671
			}
7682
			}
7672
		}
7683
		}
7673
 
7684
 
7674
		if (SUPPORTS_INTEGRATED_DP(dev) &&
7685
		if (SUPPORTS_INTEGRATED_DP(dev) &&
7675
		    (I915_READ(DP_D) & DP_DETECTED)) {
7686
		    (I915_READ(DP_D) & DP_DETECTED)) {
7676
			DRM_DEBUG_KMS("probing DP_D\n");
7687
			DRM_DEBUG_KMS("probing DP_D\n");
7677
			intel_dp_init(dev, DP_D, PORT_D);
7688
			intel_dp_init(dev, DP_D, PORT_D);
7678
		}
7689
		}
7679
	} else if (IS_GEN2(dev))
7690
	} else if (IS_GEN2(dev))
7680
		intel_dvo_init(dev);
7691
		intel_dvo_init(dev);
7681
 
7692
 
7682
//   if (SUPPORTS_TV(dev))
7693
//   if (SUPPORTS_TV(dev))
7683
//       intel_tv_init(dev);
7694
//       intel_tv_init(dev);
7684
 
7695
 
7685
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
7696
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
7686
		encoder->base.possible_crtcs = encoder->crtc_mask;
7697
		encoder->base.possible_crtcs = encoder->crtc_mask;
7687
		encoder->base.possible_clones =
7698
		encoder->base.possible_clones =
7688
			intel_encoder_clones(encoder);
7699
			intel_encoder_clones(encoder);
7689
	}
7700
	}
7690
 
7701
 
7691
	if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
7702
	if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
7692
		ironlake_init_pch_refclk(dev);
7703
		ironlake_init_pch_refclk(dev);
7693
}
7704
}
7694
 
7705
 
7695
 
7706
 
7696
 
7707
 
7697
static const struct drm_framebuffer_funcs intel_fb_funcs = {
7708
static const struct drm_framebuffer_funcs intel_fb_funcs = {
7698
//	.destroy = intel_user_framebuffer_destroy,
7709
//	.destroy = intel_user_framebuffer_destroy,
7699
//	.create_handle = intel_user_framebuffer_create_handle,
7710
//	.create_handle = intel_user_framebuffer_create_handle,
7700
};
7711
};
7701
 
7712
 
7702
int intel_framebuffer_init(struct drm_device *dev,
7713
int intel_framebuffer_init(struct drm_device *dev,
7703
			   struct intel_framebuffer *intel_fb,
7714
			   struct intel_framebuffer *intel_fb,
7704
			   struct drm_mode_fb_cmd2 *mode_cmd,
7715
			   struct drm_mode_fb_cmd2 *mode_cmd,
7705
			   struct drm_i915_gem_object *obj)
7716
			   struct drm_i915_gem_object *obj)
7706
{
7717
{
7707
	int ret;
7718
	int ret;
7708
 
7719
 
7709
	if (obj->tiling_mode == I915_TILING_Y)
7720
	if (obj->tiling_mode == I915_TILING_Y)
7710
		return -EINVAL;
7721
		return -EINVAL;
7711
 
7722
 
7712
	if (mode_cmd->pitches[0] & 63)
7723
	if (mode_cmd->pitches[0] & 63)
7713
			return -EINVAL;
7724
			return -EINVAL;
7714
 
7725
 
7715
	switch (mode_cmd->pixel_format) {
7726
	switch (mode_cmd->pixel_format) {
7716
	case DRM_FORMAT_RGB332:
7727
	case DRM_FORMAT_RGB332:
7717
	case DRM_FORMAT_RGB565:
7728
	case DRM_FORMAT_RGB565:
7718
	case DRM_FORMAT_XRGB8888:
7729
	case DRM_FORMAT_XRGB8888:
7719
	case DRM_FORMAT_XBGR8888:
7730
	case DRM_FORMAT_XBGR8888:
7720
	case DRM_FORMAT_ARGB8888:
7731
	case DRM_FORMAT_ARGB8888:
7721
	case DRM_FORMAT_XRGB2101010:
7732
	case DRM_FORMAT_XRGB2101010:
7722
	case DRM_FORMAT_ARGB2101010:
7733
	case DRM_FORMAT_ARGB2101010:
7723
		/* RGB formats are common across chipsets */
7734
		/* RGB formats are common across chipsets */
7724
		break;
7735
		break;
7725
	case DRM_FORMAT_YUYV:
7736
	case DRM_FORMAT_YUYV:
7726
	case DRM_FORMAT_UYVY:
7737
	case DRM_FORMAT_UYVY:
7727
	case DRM_FORMAT_YVYU:
7738
	case DRM_FORMAT_YVYU:
7728
	case DRM_FORMAT_VYUY:
7739
	case DRM_FORMAT_VYUY:
7729
		break;
7740
		break;
7730
	default:
7741
	default:
7731
		DRM_DEBUG_KMS("unsupported pixel format %u\n",
7742
		DRM_DEBUG_KMS("unsupported pixel format %u\n",
7732
				mode_cmd->pixel_format);
7743
				mode_cmd->pixel_format);
7733
		return -EINVAL;
7744
		return -EINVAL;
7734
	}
7745
	}
7735
 
7746
 
7736
	ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
7747
	ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
7737
	if (ret) {
7748
	if (ret) {
7738
		DRM_ERROR("framebuffer init failed %d\n", ret);
7749
		DRM_ERROR("framebuffer init failed %d\n", ret);
7739
		return ret;
7750
		return ret;
7740
	}
7751
	}
7741
 
7752
 
7742
	drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
7753
	drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
7743
	intel_fb->obj = obj;
7754
	intel_fb->obj = obj;
7744
	return 0;
7755
	return 0;
7745
}
7756
}
7746
 
7757
 
7747
 
7758
 
7748
static const struct drm_mode_config_funcs intel_mode_funcs = {
7759
static const struct drm_mode_config_funcs intel_mode_funcs = {
7749
	.fb_create = NULL /*intel_user_framebuffer_create*/,
7760
	.fb_create = NULL /*intel_user_framebuffer_create*/,
7750
	.output_poll_changed = NULL /*intel_fb_output_poll_changed*/,
7761
	.output_poll_changed = NULL /*intel_fb_output_poll_changed*/,
7751
};
7762
};
7752
 
7763
 
7753
/* Set up chip specific display functions */
7764
/* Set up chip specific display functions */
7754
static void intel_init_display(struct drm_device *dev)
7765
static void intel_init_display(struct drm_device *dev)
7755
{
7766
{
7756
	struct drm_i915_private *dev_priv = dev->dev_private;
7767
	struct drm_i915_private *dev_priv = dev->dev_private;
7757
 
7768
 
7758
	/* We always want a DPMS function */
7769
	/* We always want a DPMS function */
7759
	if (HAS_PCH_SPLIT(dev)) {
7770
	if (HAS_PCH_SPLIT(dev)) {
7760
		dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
7771
		dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
7761
		dev_priv->display.crtc_enable = ironlake_crtc_enable;
7772
		dev_priv->display.crtc_enable = ironlake_crtc_enable;
7762
		dev_priv->display.crtc_disable = ironlake_crtc_disable;
7773
		dev_priv->display.crtc_disable = ironlake_crtc_disable;
7763
		dev_priv->display.off = ironlake_crtc_off;
7774
		dev_priv->display.off = ironlake_crtc_off;
7764
		dev_priv->display.update_plane = ironlake_update_plane;
7775
		dev_priv->display.update_plane = ironlake_update_plane;
7765
	} else {
7776
	} else {
7766
		dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
7777
		dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
7767
		dev_priv->display.crtc_enable = i9xx_crtc_enable;
7778
		dev_priv->display.crtc_enable = i9xx_crtc_enable;
7768
		dev_priv->display.crtc_disable = i9xx_crtc_disable;
7779
		dev_priv->display.crtc_disable = i9xx_crtc_disable;
7769
		dev_priv->display.off = i9xx_crtc_off;
7780
		dev_priv->display.off = i9xx_crtc_off;
7770
		dev_priv->display.update_plane = i9xx_update_plane;
7781
		dev_priv->display.update_plane = i9xx_update_plane;
7771
	}
7782
	}
7772
 
7783
 
7773
	/* Returns the core display clock speed */
7784
	/* Returns the core display clock speed */
7774
	if (IS_VALLEYVIEW(dev))
7785
	if (IS_VALLEYVIEW(dev))
7775
		dev_priv->display.get_display_clock_speed =
7786
		dev_priv->display.get_display_clock_speed =
7776
			valleyview_get_display_clock_speed;
7787
			valleyview_get_display_clock_speed;
7777
	else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
7788
	else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
7778
		dev_priv->display.get_display_clock_speed =
7789
		dev_priv->display.get_display_clock_speed =
7779
			i945_get_display_clock_speed;
7790
			i945_get_display_clock_speed;
7780
	else if (IS_I915G(dev))
7791
	else if (IS_I915G(dev))
7781
		dev_priv->display.get_display_clock_speed =
7792
		dev_priv->display.get_display_clock_speed =
7782
			i915_get_display_clock_speed;
7793
			i915_get_display_clock_speed;
7783
	else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
7794
	else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
7784
		dev_priv->display.get_display_clock_speed =
7795
		dev_priv->display.get_display_clock_speed =
7785
			i9xx_misc_get_display_clock_speed;
7796
			i9xx_misc_get_display_clock_speed;
7786
	else if (IS_I915GM(dev))
7797
	else if (IS_I915GM(dev))
7787
		dev_priv->display.get_display_clock_speed =
7798
		dev_priv->display.get_display_clock_speed =
7788
			i915gm_get_display_clock_speed;
7799
			i915gm_get_display_clock_speed;
7789
	else if (IS_I865G(dev))
7800
	else if (IS_I865G(dev))
7790
		dev_priv->display.get_display_clock_speed =
7801
		dev_priv->display.get_display_clock_speed =
7791
			i865_get_display_clock_speed;
7802
			i865_get_display_clock_speed;
7792
	else if (IS_I85X(dev))
7803
	else if (IS_I85X(dev))
7793
		dev_priv->display.get_display_clock_speed =
7804
		dev_priv->display.get_display_clock_speed =
7794
			i855_get_display_clock_speed;
7805
			i855_get_display_clock_speed;
7795
	else /* 852, 830 */
7806
	else /* 852, 830 */
7796
		dev_priv->display.get_display_clock_speed =
7807
		dev_priv->display.get_display_clock_speed =
7797
			i830_get_display_clock_speed;
7808
			i830_get_display_clock_speed;
7798
 
7809
 
7799
	if (HAS_PCH_SPLIT(dev)) {
7810
	if (HAS_PCH_SPLIT(dev)) {
7800
		if (IS_GEN5(dev)) {
7811
		if (IS_GEN5(dev)) {
7801
			dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
7812
			dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
7802
			dev_priv->display.write_eld = ironlake_write_eld;
7813
			dev_priv->display.write_eld = ironlake_write_eld;
7803
		} else if (IS_GEN6(dev)) {
7814
		} else if (IS_GEN6(dev)) {
7804
			dev_priv->display.fdi_link_train = gen6_fdi_link_train;
7815
			dev_priv->display.fdi_link_train = gen6_fdi_link_train;
7805
			dev_priv->display.write_eld = ironlake_write_eld;
7816
			dev_priv->display.write_eld = ironlake_write_eld;
7806
		} else if (IS_IVYBRIDGE(dev)) {
7817
		} else if (IS_IVYBRIDGE(dev)) {
7807
			/* FIXME: detect B0+ stepping and use auto training */
7818
			/* FIXME: detect B0+ stepping and use auto training */
7808
			dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
7819
			dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
7809
			dev_priv->display.write_eld = ironlake_write_eld;
7820
			dev_priv->display.write_eld = ironlake_write_eld;
7810
		} else if (IS_HASWELL(dev)) {
7821
		} else if (IS_HASWELL(dev)) {
7811
			dev_priv->display.fdi_link_train = hsw_fdi_link_train;
7822
			dev_priv->display.fdi_link_train = hsw_fdi_link_train;
7812
			dev_priv->display.write_eld = haswell_write_eld;
7823
			dev_priv->display.write_eld = haswell_write_eld;
7813
		} else
7824
		} else
7814
			dev_priv->display.update_wm = NULL;
7825
			dev_priv->display.update_wm = NULL;
7815
	} else if (IS_G4X(dev)) {
7826
	} else if (IS_G4X(dev)) {
7816
		dev_priv->display.write_eld = g4x_write_eld;
7827
		dev_priv->display.write_eld = g4x_write_eld;
7817
	}
7828
	}
7818
 
7829
 
7819
	/* Default just returns -ENODEV to indicate unsupported */
7830
	/* Default just returns -ENODEV to indicate unsupported */
7820
//	dev_priv->display.queue_flip = intel_default_queue_flip;
7831
//	dev_priv->display.queue_flip = intel_default_queue_flip;
7821
 
7832
 
7822
 
7833
 
7823
 
7834
 
7824
 
7835
 
7825
}
7836
}
7826
 
7837
 
7827
/*
7838
/*
7828
 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
7839
 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
7829
 * resume, or other times.  This quirk makes sure that's the case for
7840
 * resume, or other times.  This quirk makes sure that's the case for
7830
 * affected systems.
7841
 * affected systems.
7831
 */
7842
 */
7832
static void quirk_pipea_force(struct drm_device *dev)
7843
static void quirk_pipea_force(struct drm_device *dev)
7833
{
7844
{
7834
	struct drm_i915_private *dev_priv = dev->dev_private;
7845
	struct drm_i915_private *dev_priv = dev->dev_private;
7835
 
7846
 
7836
	dev_priv->quirks |= QUIRK_PIPEA_FORCE;
7847
	dev_priv->quirks |= QUIRK_PIPEA_FORCE;
7837
	DRM_INFO("applying pipe a force quirk\n");
7848
	DRM_INFO("applying pipe a force quirk\n");
7838
}
7849
}
7839
 
7850
 
7840
/*
7851
/*
7841
 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
7852
 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
7842
 */
7853
 */
7843
static void quirk_ssc_force_disable(struct drm_device *dev)
7854
static void quirk_ssc_force_disable(struct drm_device *dev)
7844
{
7855
{
7845
	struct drm_i915_private *dev_priv = dev->dev_private;
7856
	struct drm_i915_private *dev_priv = dev->dev_private;
7846
	dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
7857
	dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
7847
	DRM_INFO("applying lvds SSC disable quirk\n");
7858
	DRM_INFO("applying lvds SSC disable quirk\n");
7848
}
7859
}
7849
 
7860
 
7850
/*
7861
/*
7851
 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
7862
 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
7852
 * brightness value
7863
 * brightness value
7853
 */
7864
 */
7854
static void quirk_invert_brightness(struct drm_device *dev)
7865
static void quirk_invert_brightness(struct drm_device *dev)
7855
{
7866
{
7856
	struct drm_i915_private *dev_priv = dev->dev_private;
7867
	struct drm_i915_private *dev_priv = dev->dev_private;
7857
	dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
7868
	dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
7858
	DRM_INFO("applying inverted panel brightness quirk\n");
7869
	DRM_INFO("applying inverted panel brightness quirk\n");
7859
}
7870
}
7860
 
7871
 
7861
struct intel_quirk {
7872
struct intel_quirk {
7862
	int device;
7873
	int device;
7863
	int subsystem_vendor;
7874
	int subsystem_vendor;
7864
	int subsystem_device;
7875
	int subsystem_device;
7865
	void (*hook)(struct drm_device *dev);
7876
	void (*hook)(struct drm_device *dev);
7866
};
7877
};
7867
 
7878
 
7868
/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
7879
/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
7869
struct intel_dmi_quirk {
7880
struct intel_dmi_quirk {
7870
	void (*hook)(struct drm_device *dev);
7881
	void (*hook)(struct drm_device *dev);
7871
	const struct dmi_system_id (*dmi_id_list)[];
7882
	const struct dmi_system_id (*dmi_id_list)[];
7872
};
7883
};
7873
 
7884
 
7874
static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
7885
static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
7875
{
7886
{
7876
	DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
7887
	DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
7877
	return 1;
7888
	return 1;
7878
}
7889
}
7879
 
7890
 
7880
static const struct intel_dmi_quirk intel_dmi_quirks[] = {
7891
static const struct intel_dmi_quirk intel_dmi_quirks[] = {
7881
	{
7892
	{
7882
		.dmi_id_list = &(const struct dmi_system_id[]) {
7893
		.dmi_id_list = &(const struct dmi_system_id[]) {
7883
			{
7894
			{
7884
				.callback = intel_dmi_reverse_brightness,
7895
				.callback = intel_dmi_reverse_brightness,
7885
				.ident = "NCR Corporation",
7896
				.ident = "NCR Corporation",
7886
				.matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
7897
				.matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
7887
					    DMI_MATCH(DMI_PRODUCT_NAME, ""),
7898
					    DMI_MATCH(DMI_PRODUCT_NAME, ""),
7888
				},
7899
				},
7889
			},
7900
			},
7890
			{ }  /* terminating entry */
7901
			{ }  /* terminating entry */
7891
		},
7902
		},
7892
		.hook = quirk_invert_brightness,
7903
		.hook = quirk_invert_brightness,
7893
	},
7904
	},
7894
};
7905
};
7895
 
7906
 
7896
static struct intel_quirk intel_quirks[] = {
7907
static struct intel_quirk intel_quirks[] = {
7897
	/* HP Mini needs pipe A force quirk (LP: #322104) */
7908
	/* HP Mini needs pipe A force quirk (LP: #322104) */
7898
	{ 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
7909
	{ 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
7899
 
7910
 
7900
	/* Toshiba Protege R-205, S-209 needs pipe A force quirk */
7911
	/* Toshiba Protege R-205, S-209 needs pipe A force quirk */
7901
	{ 0x2592, 0x1179, 0x0001, quirk_pipea_force },
7912
	{ 0x2592, 0x1179, 0x0001, quirk_pipea_force },
7902
 
7913
 
7903
	/* ThinkPad T60 needs pipe A force quirk (bug #16494) */
7914
	/* ThinkPad T60 needs pipe A force quirk (bug #16494) */
7904
	{ 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
7915
	{ 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
7905
 
7916
 
7906
	/* 830/845 need to leave pipe A & dpll A up */
7917
	/* 830/845 need to leave pipe A & dpll A up */
7907
	{ 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
7918
	{ 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
7908
	{ 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
7919
	{ 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
7909
 
7920
 
7910
	/* Lenovo U160 cannot use SSC on LVDS */
7921
	/* Lenovo U160 cannot use SSC on LVDS */
7911
	{ 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
7922
	{ 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
7912
 
7923
 
7913
	/* Sony Vaio Y cannot use SSC on LVDS */
7924
	/* Sony Vaio Y cannot use SSC on LVDS */
7914
	{ 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
7925
	{ 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
7915
 
7926
 
7916
	/* Acer Aspire 5734Z must invert backlight brightness */
7927
	/* Acer Aspire 5734Z must invert backlight brightness */
7917
	{ 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
7928
	{ 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
7918
};
7929
};
7919
 
7930
 
7920
static void intel_init_quirks(struct drm_device *dev)
7931
static void intel_init_quirks(struct drm_device *dev)
7921
{
7932
{
7922
	struct pci_dev *d = dev->pdev;
7933
	struct pci_dev *d = dev->pdev;
7923
	int i;
7934
	int i;
7924
 
7935
 
7925
	for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
7936
	for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
7926
		struct intel_quirk *q = &intel_quirks[i];
7937
		struct intel_quirk *q = &intel_quirks[i];
7927
 
7938
 
7928
		if (d->device == q->device &&
7939
		if (d->device == q->device &&
7929
		    (d->subsystem_vendor == q->subsystem_vendor ||
7940
		    (d->subsystem_vendor == q->subsystem_vendor ||
7930
		     q->subsystem_vendor == PCI_ANY_ID) &&
7941
		     q->subsystem_vendor == PCI_ANY_ID) &&
7931
		    (d->subsystem_device == q->subsystem_device ||
7942
		    (d->subsystem_device == q->subsystem_device ||
7932
		     q->subsystem_device == PCI_ANY_ID))
7943
		     q->subsystem_device == PCI_ANY_ID))
7933
			q->hook(dev);
7944
			q->hook(dev);
7934
	}
7945
	}
7935
//	for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
7946
//	for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
7936
//		if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
7947
//		if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
7937
//			intel_dmi_quirks[i].hook(dev);
7948
//			intel_dmi_quirks[i].hook(dev);
7938
//	}
7949
//	}
7939
}
7950
}
7940
 
7951
 
7941
/* Disable the VGA plane that we never use */
7952
/* Disable the VGA plane that we never use */
7942
static void i915_disable_vga(struct drm_device *dev)
7953
static void i915_disable_vga(struct drm_device *dev)
7943
{
7954
{
7944
	struct drm_i915_private *dev_priv = dev->dev_private;
7955
	struct drm_i915_private *dev_priv = dev->dev_private;
7945
	u8 sr1;
7956
	u8 sr1;
7946
	u32 vga_reg;
7957
	u32 vga_reg;
7947
 
7958
 
7948
	if (HAS_PCH_SPLIT(dev))
7959
	if (HAS_PCH_SPLIT(dev))
7949
		vga_reg = CPU_VGACNTRL;
7960
		vga_reg = CPU_VGACNTRL;
7950
	else
7961
	else
7951
		vga_reg = VGACNTRL;
7962
		vga_reg = VGACNTRL;
7952
 
7963
 
7953
//   vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
7964
//   vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
7954
    out8(SR01, VGA_SR_INDEX);
7965
    out8(SR01, VGA_SR_INDEX);
7955
    sr1 = in8(VGA_SR_DATA);
7966
    sr1 = in8(VGA_SR_DATA);
7956
    out8(sr1 | 1<<5, VGA_SR_DATA);
7967
    out8(sr1 | 1<<5, VGA_SR_DATA);
7957
//   vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
7968
//   vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
7958
	udelay(300);
7969
	udelay(300);
7959
 
7970
 
7960
	I915_WRITE(vga_reg, VGA_DISP_DISABLE);
7971
	I915_WRITE(vga_reg, VGA_DISP_DISABLE);
7961
	POSTING_READ(vga_reg);
7972
	POSTING_READ(vga_reg);
7962
}
7973
}
7963
 
7974
 
7964
void intel_modeset_init_hw(struct drm_device *dev)
7975
void intel_modeset_init_hw(struct drm_device *dev)
7965
{
7976
{
7966
	/* We attempt to init the necessary power wells early in the initialization
7977
	/* We attempt to init the necessary power wells early in the initialization
7967
	 * time, so the subsystems that expect power to be enabled can work.
7978
	 * time, so the subsystems that expect power to be enabled can work.
7968
	 */
7979
	 */
7969
	intel_init_power_wells(dev);
7980
	intel_init_power_wells(dev);
7970
 
7981
 
7971
	intel_prepare_ddi(dev);
7982
	intel_prepare_ddi(dev);
7972
 
7983
 
7973
	intel_init_clock_gating(dev);
7984
	intel_init_clock_gating(dev);
7974
 
7985
 
7975
//   mutex_lock(&dev->struct_mutex);
7986
//   mutex_lock(&dev->struct_mutex);
7976
//   intel_enable_gt_powersave(dev);
7987
//   intel_enable_gt_powersave(dev);
7977
//   mutex_unlock(&dev->struct_mutex);
7988
//   mutex_unlock(&dev->struct_mutex);
7978
}
7989
}
7979
 
7990
 
7980
void intel_modeset_init(struct drm_device *dev)
7991
void intel_modeset_init(struct drm_device *dev)
7981
{
7992
{
7982
	struct drm_i915_private *dev_priv = dev->dev_private;
7993
	struct drm_i915_private *dev_priv = dev->dev_private;
7983
	int i, ret;
7994
	int i, ret;
7984
 
7995
 
7985
	drm_mode_config_init(dev);
7996
	drm_mode_config_init(dev);
7986
 
7997
 
7987
	dev->mode_config.min_width = 0;
7998
	dev->mode_config.min_width = 0;
7988
	dev->mode_config.min_height = 0;
7999
	dev->mode_config.min_height = 0;
7989
 
8000
 
7990
	dev->mode_config.preferred_depth = 24;
8001
	dev->mode_config.preferred_depth = 24;
7991
	dev->mode_config.prefer_shadow = 1;
8002
	dev->mode_config.prefer_shadow = 1;
7992
 
8003
 
7993
	dev->mode_config.funcs = &intel_mode_funcs;
8004
	dev->mode_config.funcs = &intel_mode_funcs;
7994
 
8005
 
7995
	intel_init_quirks(dev);
8006
	intel_init_quirks(dev);
7996
 
8007
 
7997
	intel_init_pm(dev);
8008
	intel_init_pm(dev);
7998
 
8009
 
7999
	intel_init_display(dev);
8010
	intel_init_display(dev);
8000
 
8011
 
8001
	if (IS_GEN2(dev)) {
8012
	if (IS_GEN2(dev)) {
8002
		dev->mode_config.max_width = 2048;
8013
		dev->mode_config.max_width = 2048;
8003
		dev->mode_config.max_height = 2048;
8014
		dev->mode_config.max_height = 2048;
8004
	} else if (IS_GEN3(dev)) {
8015
	} else if (IS_GEN3(dev)) {
8005
		dev->mode_config.max_width = 4096;
8016
		dev->mode_config.max_width = 4096;
8006
		dev->mode_config.max_height = 4096;
8017
		dev->mode_config.max_height = 4096;
8007
	} else {
8018
	} else {
8008
		dev->mode_config.max_width = 8192;
8019
		dev->mode_config.max_width = 8192;
8009
		dev->mode_config.max_height = 8192;
8020
		dev->mode_config.max_height = 8192;
8010
	}
8021
	}
8011
	dev->mode_config.fb_base = dev_priv->mm.gtt_base_addr;
8022
	dev->mode_config.fb_base = dev_priv->mm.gtt_base_addr;
8012
 
8023
 
8013
	DRM_DEBUG_KMS("%d display pipe%s available.\n",
8024
	DRM_DEBUG_KMS("%d display pipe%s available.\n",
8014
		      dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
8025
		      dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
8015
 
8026
 
8016
	for (i = 0; i < dev_priv->num_pipe; i++) {
8027
	for (i = 0; i < dev_priv->num_pipe; i++) {
8017
		intel_crtc_init(dev, i);
8028
		intel_crtc_init(dev, i);
8018
		ret = intel_plane_init(dev, i);
8029
		ret = intel_plane_init(dev, i);
8019
		if (ret)
8030
		if (ret)
8020
			DRM_DEBUG_KMS("plane %d init failed: %d\n", i, ret);
8031
			DRM_DEBUG_KMS("plane %d init failed: %d\n", i, ret);
8021
	}
8032
	}
8022
 
8033
 
8023
	intel_pch_pll_init(dev);
8034
	intel_pch_pll_init(dev);
8024
 
8035
 
8025
	/* Just disable it once at startup */
8036
	/* Just disable it once at startup */
8026
	i915_disable_vga(dev);
8037
	i915_disable_vga(dev);
8027
	intel_setup_outputs(dev);
8038
	intel_setup_outputs(dev);
8028
}
8039
}
8029
 
8040
 
8030
static void
8041
static void
8031
intel_connector_break_all_links(struct intel_connector *connector)
8042
intel_connector_break_all_links(struct intel_connector *connector)
8032
{
8043
{
8033
	connector->base.dpms = DRM_MODE_DPMS_OFF;
8044
	connector->base.dpms = DRM_MODE_DPMS_OFF;
8034
	connector->base.encoder = NULL;
8045
	connector->base.encoder = NULL;
8035
	connector->encoder->connectors_active = false;
8046
	connector->encoder->connectors_active = false;
8036
	connector->encoder->base.crtc = NULL;
8047
	connector->encoder->base.crtc = NULL;
8037
}
8048
}
8038
 
8049
 
8039
static void intel_enable_pipe_a(struct drm_device *dev)
8050
static void intel_enable_pipe_a(struct drm_device *dev)
8040
{
8051
{
8041
	struct intel_connector *connector;
8052
	struct intel_connector *connector;
8042
	struct drm_connector *crt = NULL;
8053
	struct drm_connector *crt = NULL;
8043
	struct intel_load_detect_pipe load_detect_temp;
8054
	struct intel_load_detect_pipe load_detect_temp;
8044
 
8055
 
8045
	/* We can't just switch on the pipe A, we need to set things up with a
8056
	/* We can't just switch on the pipe A, we need to set things up with a
8046
	 * proper mode and output configuration. As a gross hack, enable pipe A
8057
	 * proper mode and output configuration. As a gross hack, enable pipe A
8047
	 * by enabling the load detect pipe once. */
8058
	 * by enabling the load detect pipe once. */
8048
	list_for_each_entry(connector,
8059
	list_for_each_entry(connector,
8049
			    &dev->mode_config.connector_list,
8060
			    &dev->mode_config.connector_list,
8050
			    base.head) {
8061
			    base.head) {
8051
		if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
8062
		if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
8052
			crt = &connector->base;
8063
			crt = &connector->base;
8053
			break;
8064
			break;
8054
		}
8065
		}
8055
	}
8066
	}
8056
 
8067
 
8057
	if (!crt)
8068
	if (!crt)
8058
		return;
8069
		return;
8059
 
8070
 
8060
	if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
8071
	if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
8061
		intel_release_load_detect_pipe(crt, &load_detect_temp);
8072
		intel_release_load_detect_pipe(crt, &load_detect_temp);
8062
 
8073
 
8063
 
8074
 
8064
}
8075
}
8065
 
8076
 
8066
static bool
8077
static bool
8067
intel_check_plane_mapping(struct intel_crtc *crtc)
8078
intel_check_plane_mapping(struct intel_crtc *crtc)
8068
{
8079
{
8069
	struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
8080
	struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
8070
	u32 reg, val;
8081
	u32 reg, val;
8071
 
8082
 
8072
	if (dev_priv->num_pipe == 1)
8083
	if (dev_priv->num_pipe == 1)
8073
		return true;
8084
		return true;
8074
 
8085
 
8075
	reg = DSPCNTR(!crtc->plane);
8086
	reg = DSPCNTR(!crtc->plane);
8076
	val = I915_READ(reg);
8087
	val = I915_READ(reg);
8077
 
8088
 
8078
	if ((val & DISPLAY_PLANE_ENABLE) &&
8089
	if ((val & DISPLAY_PLANE_ENABLE) &&
8079
	    (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
8090
	    (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
8080
		return false;
8091
		return false;
8081
 
8092
 
8082
	return true;
8093
	return true;
8083
}
8094
}
8084
 
8095
 
8085
static void intel_sanitize_crtc(struct intel_crtc *crtc)
8096
static void intel_sanitize_crtc(struct intel_crtc *crtc)
8086
{
8097
{
8087
	struct drm_device *dev = crtc->base.dev;
8098
	struct drm_device *dev = crtc->base.dev;
8088
	struct drm_i915_private *dev_priv = dev->dev_private;
8099
	struct drm_i915_private *dev_priv = dev->dev_private;
8089
	u32 reg;
8100
	u32 reg;
8090
 
8101
 
8091
	/* Clear any frame start delays used for debugging left by the BIOS */
8102
	/* Clear any frame start delays used for debugging left by the BIOS */
8092
	reg = PIPECONF(crtc->pipe);
8103
	reg = PIPECONF(crtc->pipe);
8093
	I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
8104
	I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
8094
 
8105
 
8095
	/* We need to sanitize the plane -> pipe mapping first because this will
8106
	/* We need to sanitize the plane -> pipe mapping first because this will
8096
	 * disable the crtc (and hence change the state) if it is wrong. Note
8107
	 * disable the crtc (and hence change the state) if it is wrong. Note
8097
	 * that gen4+ has a fixed plane -> pipe mapping.  */
8108
	 * that gen4+ has a fixed plane -> pipe mapping.  */
8098
	if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
8109
	if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
8099
		struct intel_connector *connector;
8110
		struct intel_connector *connector;
8100
		bool plane;
8111
		bool plane;
8101
 
8112
 
8102
		DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
8113
		DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
8103
			      crtc->base.base.id);
8114
			      crtc->base.base.id);
8104
 
8115
 
8105
		/* Pipe has the wrong plane attached and the plane is active.
8116
		/* Pipe has the wrong plane attached and the plane is active.
8106
		 * Temporarily change the plane mapping and disable everything
8117
		 * Temporarily change the plane mapping and disable everything
8107
		 * ...  */
8118
		 * ...  */
8108
		plane = crtc->plane;
8119
		plane = crtc->plane;
8109
		crtc->plane = !plane;
8120
		crtc->plane = !plane;
8110
		dev_priv->display.crtc_disable(&crtc->base);
8121
		dev_priv->display.crtc_disable(&crtc->base);
8111
		crtc->plane = plane;
8122
		crtc->plane = plane;
8112
 
8123
 
8113
		/* ... and break all links. */
8124
		/* ... and break all links. */
8114
		list_for_each_entry(connector, &dev->mode_config.connector_list,
8125
		list_for_each_entry(connector, &dev->mode_config.connector_list,
8115
				    base.head) {
8126
				    base.head) {
8116
			if (connector->encoder->base.crtc != &crtc->base)
8127
			if (connector->encoder->base.crtc != &crtc->base)
8117
				continue;
8128
				continue;
8118
 
8129
 
8119
			intel_connector_break_all_links(connector);
8130
			intel_connector_break_all_links(connector);
8120
		}
8131
		}
8121
 
8132
 
8122
		WARN_ON(crtc->active);
8133
		WARN_ON(crtc->active);
8123
		crtc->base.enabled = false;
8134
		crtc->base.enabled = false;
8124
	}
8135
	}
8125
 
8136
 
8126
	if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
8137
	if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
8127
	    crtc->pipe == PIPE_A && !crtc->active) {
8138
	    crtc->pipe == PIPE_A && !crtc->active) {
8128
		/* BIOS forgot to enable pipe A, this mostly happens after
8139
		/* BIOS forgot to enable pipe A, this mostly happens after
8129
		 * resume. Force-enable the pipe to fix this, the update_dpms
8140
		 * resume. Force-enable the pipe to fix this, the update_dpms
8130
		 * call below we restore the pipe to the right state, but leave
8141
		 * call below we restore the pipe to the right state, but leave
8131
		 * the required bits on. */
8142
		 * the required bits on. */
8132
		intel_enable_pipe_a(dev);
8143
		intel_enable_pipe_a(dev);
8133
	}
8144
	}
8134
 
8145
 
8135
	/* Adjust the state of the output pipe according to whether we
8146
	/* Adjust the state of the output pipe according to whether we
8136
	 * have active connectors/encoders. */
8147
	 * have active connectors/encoders. */
8137
	intel_crtc_update_dpms(&crtc->base);
8148
	intel_crtc_update_dpms(&crtc->base);
8138
 
8149
 
8139
	if (crtc->active != crtc->base.enabled) {
8150
	if (crtc->active != crtc->base.enabled) {
8140
		struct intel_encoder *encoder;
8151
		struct intel_encoder *encoder;
8141
 
8152
 
8142
		/* This can happen either due to bugs in the get_hw_state
8153
		/* This can happen either due to bugs in the get_hw_state
8143
		 * functions or because the pipe is force-enabled due to the
8154
		 * functions or because the pipe is force-enabled due to the
8144
		 * pipe A quirk. */
8155
		 * pipe A quirk. */
8145
		DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
8156
		DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
8146
			      crtc->base.base.id,
8157
			      crtc->base.base.id,
8147
			      crtc->base.enabled ? "enabled" : "disabled",
8158
			      crtc->base.enabled ? "enabled" : "disabled",
8148
			      crtc->active ? "enabled" : "disabled");
8159
			      crtc->active ? "enabled" : "disabled");
8149
 
8160
 
8150
		crtc->base.enabled = crtc->active;
8161
		crtc->base.enabled = crtc->active;
8151
 
8162
 
8152
		/* Because we only establish the connector -> encoder ->
8163
		/* Because we only establish the connector -> encoder ->
8153
		 * crtc links if something is active, this means the
8164
		 * crtc links if something is active, this means the
8154
		 * crtc is now deactivated. Break the links. connector
8165
		 * crtc is now deactivated. Break the links. connector
8155
		 * -> encoder links are only establish when things are
8166
		 * -> encoder links are only establish when things are
8156
		 *  actually up, hence no need to break them. */
8167
		 *  actually up, hence no need to break them. */
8157
		WARN_ON(crtc->active);
8168
		WARN_ON(crtc->active);
8158
 
8169
 
8159
		for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
8170
		for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
8160
			WARN_ON(encoder->connectors_active);
8171
			WARN_ON(encoder->connectors_active);
8161
			encoder->base.crtc = NULL;
8172
			encoder->base.crtc = NULL;
8162
		}
8173
		}
8163
	}
8174
	}
8164
}
8175
}
8165
 
8176
 
8166
static void intel_sanitize_encoder(struct intel_encoder *encoder)
8177
static void intel_sanitize_encoder(struct intel_encoder *encoder)
8167
{
8178
{
8168
	struct intel_connector *connector;
8179
	struct intel_connector *connector;
8169
	struct drm_device *dev = encoder->base.dev;
8180
	struct drm_device *dev = encoder->base.dev;
8170
 
8181
 
8171
	/* We need to check both for a crtc link (meaning that the
8182
	/* We need to check both for a crtc link (meaning that the
8172
	 * encoder is active and trying to read from a pipe) and the
8183
	 * encoder is active and trying to read from a pipe) and the
8173
	 * pipe itself being active. */
8184
	 * pipe itself being active. */
8174
	bool has_active_crtc = encoder->base.crtc &&
8185
	bool has_active_crtc = encoder->base.crtc &&
8175
		to_intel_crtc(encoder->base.crtc)->active;
8186
		to_intel_crtc(encoder->base.crtc)->active;
8176
 
8187
 
8177
	if (encoder->connectors_active && !has_active_crtc) {
8188
	if (encoder->connectors_active && !has_active_crtc) {
8178
		DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
8189
		DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
8179
			      encoder->base.base.id,
8190
			      encoder->base.base.id,
8180
			      drm_get_encoder_name(&encoder->base));
8191
			      drm_get_encoder_name(&encoder->base));
8181
 
8192
 
8182
		/* Connector is active, but has no active pipe. This is
8193
		/* Connector is active, but has no active pipe. This is
8183
		 * fallout from our resume register restoring. Disable
8194
		 * fallout from our resume register restoring. Disable
8184
		 * the encoder manually again. */
8195
		 * the encoder manually again. */
8185
		if (encoder->base.crtc) {
8196
		if (encoder->base.crtc) {
8186
			DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
8197
			DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
8187
				      encoder->base.base.id,
8198
				      encoder->base.base.id,
8188
				      drm_get_encoder_name(&encoder->base));
8199
				      drm_get_encoder_name(&encoder->base));
8189
			encoder->disable(encoder);
8200
			encoder->disable(encoder);
8190
		}
8201
		}
8191
 
8202
 
8192
		/* Inconsistent output/port/pipe state happens presumably due to
8203
		/* Inconsistent output/port/pipe state happens presumably due to
8193
		 * a bug in one of the get_hw_state functions. Or someplace else
8204
		 * a bug in one of the get_hw_state functions. Or someplace else
8194
		 * in our code, like the register restore mess on resume. Clamp
8205
		 * in our code, like the register restore mess on resume. Clamp
8195
		 * things to off as a safer default. */
8206
		 * things to off as a safer default. */
8196
		list_for_each_entry(connector,
8207
		list_for_each_entry(connector,
8197
				    &dev->mode_config.connector_list,
8208
				    &dev->mode_config.connector_list,
8198
				    base.head) {
8209
				    base.head) {
8199
			if (connector->encoder != encoder)
8210
			if (connector->encoder != encoder)
8200
				continue;
8211
				continue;
8201
 
8212
 
8202
			intel_connector_break_all_links(connector);
8213
			intel_connector_break_all_links(connector);
8203
		}
8214
		}
8204
	}
8215
	}
8205
	/* Enabled encoders without active connectors will be fixed in
8216
	/* Enabled encoders without active connectors will be fixed in
8206
	 * the crtc fixup. */
8217
	 * the crtc fixup. */
8207
}
8218
}
8208
 
8219
 
8209
/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
8220
/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
8210
 * and i915 state tracking structures. */
8221
 * and i915 state tracking structures. */
8211
void intel_modeset_setup_hw_state(struct drm_device *dev)
8222
void intel_modeset_setup_hw_state(struct drm_device *dev)
8212
{
8223
{
8213
	struct drm_i915_private *dev_priv = dev->dev_private;
8224
	struct drm_i915_private *dev_priv = dev->dev_private;
8214
	enum pipe pipe;
8225
	enum pipe pipe;
8215
	u32 tmp;
8226
	u32 tmp;
8216
	struct intel_crtc *crtc;
8227
	struct intel_crtc *crtc;
8217
	struct intel_encoder *encoder;
8228
	struct intel_encoder *encoder;
8218
	struct intel_connector *connector;
8229
	struct intel_connector *connector;
8219
 
8230
 
8220
	for_each_pipe(pipe) {
8231
	for_each_pipe(pipe) {
8221
		crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
8232
		crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
8222
 
8233
 
8223
		tmp = I915_READ(PIPECONF(pipe));
8234
		tmp = I915_READ(PIPECONF(pipe));
8224
		if (tmp & PIPECONF_ENABLE)
8235
		if (tmp & PIPECONF_ENABLE)
8225
			crtc->active = true;
8236
			crtc->active = true;
8226
		else
8237
		else
8227
			crtc->active = false;
8238
			crtc->active = false;
8228
 
8239
 
8229
		crtc->base.enabled = crtc->active;
8240
		crtc->base.enabled = crtc->active;
8230
 
8241
 
8231
		DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
8242
		DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
8232
			      crtc->base.base.id,
8243
			      crtc->base.base.id,
8233
			      crtc->active ? "enabled" : "disabled");
8244
			      crtc->active ? "enabled" : "disabled");
8234
	}
8245
	}
8235
 
8246
 
8236
	list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8247
	list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8237
			    base.head) {
8248
			    base.head) {
8238
		pipe = 0;
8249
		pipe = 0;
8239
 
8250
 
8240
		if (encoder->get_hw_state(encoder, &pipe)) {
8251
		if (encoder->get_hw_state(encoder, &pipe)) {
8241
			encoder->base.crtc =
8252
			encoder->base.crtc =
8242
				dev_priv->pipe_to_crtc_mapping[pipe];
8253
				dev_priv->pipe_to_crtc_mapping[pipe];
8243
		} else {
8254
		} else {
8244
			encoder->base.crtc = NULL;
8255
			encoder->base.crtc = NULL;
8245
		}
8256
		}
8246
 
8257
 
8247
		encoder->connectors_active = false;
8258
		encoder->connectors_active = false;
8248
		DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe=%i\n",
8259
		DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe=%i\n",
8249
			      encoder->base.base.id,
8260
			      encoder->base.base.id,
8250
			      drm_get_encoder_name(&encoder->base),
8261
			      drm_get_encoder_name(&encoder->base),
8251
			      encoder->base.crtc ? "enabled" : "disabled",
8262
			      encoder->base.crtc ? "enabled" : "disabled",
8252
			      pipe);
8263
			      pipe);
8253
	}
8264
	}
8254
 
8265
 
8255
	list_for_each_entry(connector, &dev->mode_config.connector_list,
8266
	list_for_each_entry(connector, &dev->mode_config.connector_list,
8256
			    base.head) {
8267
			    base.head) {
8257
		if (connector->get_hw_state(connector)) {
8268
		if (connector->get_hw_state(connector)) {
8258
			connector->base.dpms = DRM_MODE_DPMS_ON;
8269
			connector->base.dpms = DRM_MODE_DPMS_ON;
8259
			connector->encoder->connectors_active = true;
8270
			connector->encoder->connectors_active = true;
8260
			connector->base.encoder = &connector->encoder->base;
8271
			connector->base.encoder = &connector->encoder->base;
8261
		} else {
8272
		} else {
8262
			connector->base.dpms = DRM_MODE_DPMS_OFF;
8273
			connector->base.dpms = DRM_MODE_DPMS_OFF;
8263
			connector->base.encoder = NULL;
8274
			connector->base.encoder = NULL;
8264
		}
8275
		}
8265
		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
8276
		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
8266
			      connector->base.base.id,
8277
			      connector->base.base.id,
8267
			      drm_get_connector_name(&connector->base),
8278
			      drm_get_connector_name(&connector->base),
8268
			      connector->base.encoder ? "enabled" : "disabled");
8279
			      connector->base.encoder ? "enabled" : "disabled");
8269
	}
8280
	}
8270
 
8281
 
8271
	/* HW state is read out, now we need to sanitize this mess. */
8282
	/* HW state is read out, now we need to sanitize this mess. */
8272
	list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8283
	list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8273
			    base.head) {
8284
			    base.head) {
8274
		intel_sanitize_encoder(encoder);
8285
		intel_sanitize_encoder(encoder);
8275
	}
8286
	}
8276
 
8287
 
8277
	for_each_pipe(pipe) {
8288
	for_each_pipe(pipe) {
8278
		crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
8289
		crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
8279
		intel_sanitize_crtc(crtc);
8290
		intel_sanitize_crtc(crtc);
8280
	}
8291
	}
8281
 
8292
 
8282
	intel_modeset_update_staged_output_state(dev);
8293
	intel_modeset_update_staged_output_state(dev);
8283
 
8294
 
8284
	intel_modeset_check_state(dev);
8295
	intel_modeset_check_state(dev);
8285
}
8296
}
8286
 
8297
 
8287
void intel_modeset_gem_init(struct drm_device *dev)
8298
void intel_modeset_gem_init(struct drm_device *dev)
8288
{
8299
{
8289
	intel_modeset_init_hw(dev);
8300
	intel_modeset_init_hw(dev);
8290
 
8301
 
8291
//   intel_setup_overlay(dev);
8302
//   intel_setup_overlay(dev);
8292
 
8303
 
8293
	intel_modeset_setup_hw_state(dev);
8304
	intel_modeset_setup_hw_state(dev);
8294
}
8305
}
8295
 
8306
 
8296
void intel_modeset_cleanup(struct drm_device *dev)
8307
void intel_modeset_cleanup(struct drm_device *dev)
8297
{
8308
{
8298
#if 0
8309
#if 0
8299
	struct drm_i915_private *dev_priv = dev->dev_private;
8310
	struct drm_i915_private *dev_priv = dev->dev_private;
8300
	struct drm_crtc *crtc;
8311
	struct drm_crtc *crtc;
8301
	struct intel_crtc *intel_crtc;
8312
	struct intel_crtc *intel_crtc;
8302
 
8313
 
8303
//   drm_kms_helper_poll_fini(dev);
8314
//   drm_kms_helper_poll_fini(dev);
8304
	mutex_lock(&dev->struct_mutex);
8315
	mutex_lock(&dev->struct_mutex);
8305
 
8316
 
8306
//   intel_unregister_dsm_handler();
8317
//   intel_unregister_dsm_handler();
8307
 
8318
 
8308
 
8319
 
8309
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
8320
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
8310
		/* Skip inactive CRTCs */
8321
		/* Skip inactive CRTCs */
8311
		if (!crtc->fb)
8322
		if (!crtc->fb)
8312
			continue;
8323
			continue;
8313
 
8324
 
8314
		intel_crtc = to_intel_crtc(crtc);
8325
		intel_crtc = to_intel_crtc(crtc);
8315
		intel_increase_pllclock(crtc);
8326
		intel_increase_pllclock(crtc);
8316
	}
8327
	}
8317
 
8328
 
8318
	intel_disable_fbc(dev);
8329
	intel_disable_fbc(dev);
8319
 
8330
 
8320
	intel_disable_gt_powersave(dev);
8331
	intel_disable_gt_powersave(dev);
8321
 
8332
 
8322
	ironlake_teardown_rc6(dev);
8333
	ironlake_teardown_rc6(dev);
8323
 
8334
 
8324
	if (IS_VALLEYVIEW(dev))
8335
	if (IS_VALLEYVIEW(dev))
8325
		vlv_init_dpio(dev);
8336
		vlv_init_dpio(dev);
8326
 
8337
 
8327
	mutex_unlock(&dev->struct_mutex);
8338
	mutex_unlock(&dev->struct_mutex);
8328
 
8339
 
8329
	/* Disable the irq before mode object teardown, for the irq might
8340
	/* Disable the irq before mode object teardown, for the irq might
8330
	 * enqueue unpin/hotplug work. */
8341
	 * enqueue unpin/hotplug work. */
8331
//   drm_irq_uninstall(dev);
8342
//   drm_irq_uninstall(dev);
8332
//   cancel_work_sync(&dev_priv->hotplug_work);
8343
//   cancel_work_sync(&dev_priv->hotplug_work);
8333
//   cancel_work_sync(&dev_priv->rps.work);
8344
//   cancel_work_sync(&dev_priv->rps.work);
8334
 
8345
 
8335
	/* flush any delayed tasks or pending work */
8346
	/* flush any delayed tasks or pending work */
8336
//   flush_scheduled_work();
8347
//   flush_scheduled_work();
8337
 
8348
 
8338
	drm_mode_config_cleanup(dev);
8349
	drm_mode_config_cleanup(dev);
8339
#endif
8350
#endif
8340
}
8351
}
8341
 
8352
 
8342
/*
8353
/*
8343
 * Return which encoder is currently attached for connector.
8354
 * Return which encoder is currently attached for connector.
8344
 */
8355
 */
8345
struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
8356
struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
8346
{
8357
{
8347
	return &intel_attached_encoder(connector)->base;
8358
	return &intel_attached_encoder(connector)->base;
8348
}
8359
}
8349
 
8360
 
8350
void intel_connector_attach_encoder(struct intel_connector *connector,
8361
void intel_connector_attach_encoder(struct intel_connector *connector,
8351
				    struct intel_encoder *encoder)
8362
				    struct intel_encoder *encoder)
8352
{
8363
{
8353
	connector->encoder = encoder;
8364
	connector->encoder = encoder;
8354
	drm_mode_connector_attach_encoder(&connector->base,
8365
	drm_mode_connector_attach_encoder(&connector->base,
8355
					  &encoder->base);
8366
					  &encoder->base);
8356
}
8367
}
8357
 
8368
 
8358
/*
8369
/*
8359
 * set vga decode state - true == enable VGA decode
8370
 * set vga decode state - true == enable VGA decode
8360
 */
8371
 */
8361
int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
8372
int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
8362
{
8373
{
8363
	struct drm_i915_private *dev_priv = dev->dev_private;
8374
	struct drm_i915_private *dev_priv = dev->dev_private;
8364
	u16 gmch_ctrl;
8375
	u16 gmch_ctrl;
8365
 
8376
 
8366
	pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
8377
	pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
8367
	if (state)
8378
	if (state)
8368
		gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
8379
		gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
8369
	else
8380
	else
8370
		gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
8381
		gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
8371
	pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
8382
	pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
8372
	return 0;
8383
	return 0;
8373
}
8384
}
8374
 
8385
 
8375
#ifdef CONFIG_DEBUG_FS
8386
#ifdef CONFIG_DEBUG_FS
8376
#include 
8387
#include 
8377
 
8388
 
8378
struct intel_display_error_state {
8389
struct intel_display_error_state {
8379
	struct intel_cursor_error_state {
8390
	struct intel_cursor_error_state {
8380
		u32 control;
8391
		u32 control;
8381
		u32 position;
8392
		u32 position;
8382
		u32 base;
8393
		u32 base;
8383
		u32 size;
8394
		u32 size;
8384
	} cursor[I915_MAX_PIPES];
8395
	} cursor[I915_MAX_PIPES];
8385
 
8396
 
8386
	struct intel_pipe_error_state {
8397
	struct intel_pipe_error_state {
8387
		u32 conf;
8398
		u32 conf;
8388
		u32 source;
8399
		u32 source;
8389
 
8400
 
8390
		u32 htotal;
8401
		u32 htotal;
8391
		u32 hblank;
8402
		u32 hblank;
8392
		u32 hsync;
8403
		u32 hsync;
8393
		u32 vtotal;
8404
		u32 vtotal;
8394
		u32 vblank;
8405
		u32 vblank;
8395
		u32 vsync;
8406
		u32 vsync;
8396
	} pipe[I915_MAX_PIPES];
8407
	} pipe[I915_MAX_PIPES];
8397
 
8408
 
8398
	struct intel_plane_error_state {
8409
	struct intel_plane_error_state {
8399
		u32 control;
8410
		u32 control;
8400
		u32 stride;
8411
		u32 stride;
8401
		u32 size;
8412
		u32 size;
8402
		u32 pos;
8413
		u32 pos;
8403
		u32 addr;
8414
		u32 addr;
8404
		u32 surface;
8415
		u32 surface;
8405
		u32 tile_offset;
8416
		u32 tile_offset;
8406
	} plane[I915_MAX_PIPES];
8417
	} plane[I915_MAX_PIPES];
8407
};
8418
};
8408
 
8419
 
8409
struct intel_display_error_state *
8420
struct intel_display_error_state *
8410
intel_display_capture_error_state(struct drm_device *dev)
8421
intel_display_capture_error_state(struct drm_device *dev)
8411
{
8422
{
8412
	drm_i915_private_t *dev_priv = dev->dev_private;
8423
	drm_i915_private_t *dev_priv = dev->dev_private;
8413
	struct intel_display_error_state *error;
8424
	struct intel_display_error_state *error;
8414
	int i;
8425
	int i;
8415
 
8426
 
8416
	error = kmalloc(sizeof(*error), GFP_ATOMIC);
8427
	error = kmalloc(sizeof(*error), GFP_ATOMIC);
8417
	if (error == NULL)
8428
	if (error == NULL)
8418
		return NULL;
8429
		return NULL;
8419
 
8430
 
8420
	for_each_pipe(i) {
8431
	for_each_pipe(i) {
8421
		error->cursor[i].control = I915_READ(CURCNTR(i));
8432
		error->cursor[i].control = I915_READ(CURCNTR(i));
8422
		error->cursor[i].position = I915_READ(CURPOS(i));
8433
		error->cursor[i].position = I915_READ(CURPOS(i));
8423
		error->cursor[i].base = I915_READ(CURBASE(i));
8434
		error->cursor[i].base = I915_READ(CURBASE(i));
8424
 
8435
 
8425
		error->plane[i].control = I915_READ(DSPCNTR(i));
8436
		error->plane[i].control = I915_READ(DSPCNTR(i));
8426
		error->plane[i].stride = I915_READ(DSPSTRIDE(i));
8437
		error->plane[i].stride = I915_READ(DSPSTRIDE(i));
8427
		error->plane[i].size = I915_READ(DSPSIZE(i));
8438
		error->plane[i].size = I915_READ(DSPSIZE(i));
8428
		error->plane[i].pos = I915_READ(DSPPOS(i));
8439
		error->plane[i].pos = I915_READ(DSPPOS(i));
8429
		error->plane[i].addr = I915_READ(DSPADDR(i));
8440
		error->plane[i].addr = I915_READ(DSPADDR(i));
8430
		if (INTEL_INFO(dev)->gen >= 4) {
8441
		if (INTEL_INFO(dev)->gen >= 4) {
8431
			error->plane[i].surface = I915_READ(DSPSURF(i));
8442
			error->plane[i].surface = I915_READ(DSPSURF(i));
8432
			error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
8443
			error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
8433
		}
8444
		}
8434
 
8445
 
8435
		error->pipe[i].conf = I915_READ(PIPECONF(i));
8446
		error->pipe[i].conf = I915_READ(PIPECONF(i));
8436
		error->pipe[i].source = I915_READ(PIPESRC(i));
8447
		error->pipe[i].source = I915_READ(PIPESRC(i));
8437
		error->pipe[i].htotal = I915_READ(HTOTAL(i));
8448
		error->pipe[i].htotal = I915_READ(HTOTAL(i));
8438
		error->pipe[i].hblank = I915_READ(HBLANK(i));
8449
		error->pipe[i].hblank = I915_READ(HBLANK(i));
8439
		error->pipe[i].hsync = I915_READ(HSYNC(i));
8450
		error->pipe[i].hsync = I915_READ(HSYNC(i));
8440
		error->pipe[i].vtotal = I915_READ(VTOTAL(i));
8451
		error->pipe[i].vtotal = I915_READ(VTOTAL(i));
8441
		error->pipe[i].vblank = I915_READ(VBLANK(i));
8452
		error->pipe[i].vblank = I915_READ(VBLANK(i));
8442
		error->pipe[i].vsync = I915_READ(VSYNC(i));
8453
		error->pipe[i].vsync = I915_READ(VSYNC(i));
8443
	}
8454
	}
8444
 
8455
 
8445
	return error;
8456
	return error;
8446
}
8457
}
8447
 
8458
 
8448
void
8459
void
8449
intel_display_print_error_state(struct seq_file *m,
8460
intel_display_print_error_state(struct seq_file *m,
8450
				struct drm_device *dev,
8461
				struct drm_device *dev,
8451
				struct intel_display_error_state *error)
8462
				struct intel_display_error_state *error)
8452
{
8463
{
8453
	drm_i915_private_t *dev_priv = dev->dev_private;
8464
	drm_i915_private_t *dev_priv = dev->dev_private;
8454
	int i;
8465
	int i;
8455
 
8466
 
8456
	seq_printf(m, "Num Pipes: %d\n", dev_priv->num_pipe);
8467
	seq_printf(m, "Num Pipes: %d\n", dev_priv->num_pipe);
8457
	for_each_pipe(i) {
8468
	for_each_pipe(i) {
8458
		seq_printf(m, "Pipe [%d]:\n", i);
8469
		seq_printf(m, "Pipe [%d]:\n", i);
8459
		seq_printf(m, "  CONF: %08x\n", error->pipe[i].conf);
8470
		seq_printf(m, "  CONF: %08x\n", error->pipe[i].conf);
8460
		seq_printf(m, "  SRC: %08x\n", error->pipe[i].source);
8471
		seq_printf(m, "  SRC: %08x\n", error->pipe[i].source);
8461
		seq_printf(m, "  HTOTAL: %08x\n", error->pipe[i].htotal);
8472
		seq_printf(m, "  HTOTAL: %08x\n", error->pipe[i].htotal);
8462
		seq_printf(m, "  HBLANK: %08x\n", error->pipe[i].hblank);
8473
		seq_printf(m, "  HBLANK: %08x\n", error->pipe[i].hblank);
8463
		seq_printf(m, "  HSYNC: %08x\n", error->pipe[i].hsync);
8474
		seq_printf(m, "  HSYNC: %08x\n", error->pipe[i].hsync);
8464
		seq_printf(m, "  VTOTAL: %08x\n", error->pipe[i].vtotal);
8475
		seq_printf(m, "  VTOTAL: %08x\n", error->pipe[i].vtotal);
8465
		seq_printf(m, "  VBLANK: %08x\n", error->pipe[i].vblank);
8476
		seq_printf(m, "  VBLANK: %08x\n", error->pipe[i].vblank);
8466
		seq_printf(m, "  VSYNC: %08x\n", error->pipe[i].vsync);
8477
		seq_printf(m, "  VSYNC: %08x\n", error->pipe[i].vsync);
8467
 
8478
 
8468
		seq_printf(m, "Plane [%d]:\n", i);
8479
		seq_printf(m, "Plane [%d]:\n", i);
8469
		seq_printf(m, "  CNTR: %08x\n", error->plane[i].control);
8480
		seq_printf(m, "  CNTR: %08x\n", error->plane[i].control);
8470
		seq_printf(m, "  STRIDE: %08x\n", error->plane[i].stride);
8481
		seq_printf(m, "  STRIDE: %08x\n", error->plane[i].stride);
8471
		seq_printf(m, "  SIZE: %08x\n", error->plane[i].size);
8482
		seq_printf(m, "  SIZE: %08x\n", error->plane[i].size);
8472
		seq_printf(m, "  POS: %08x\n", error->plane[i].pos);
8483
		seq_printf(m, "  POS: %08x\n", error->plane[i].pos);
8473
		seq_printf(m, "  ADDR: %08x\n", error->plane[i].addr);
8484
		seq_printf(m, "  ADDR: %08x\n", error->plane[i].addr);
8474
		if (INTEL_INFO(dev)->gen >= 4) {
8485
		if (INTEL_INFO(dev)->gen >= 4) {
8475
			seq_printf(m, "  SURF: %08x\n", error->plane[i].surface);
8486
			seq_printf(m, "  SURF: %08x\n", error->plane[i].surface);
8476
			seq_printf(m, "  TILEOFF: %08x\n", error->plane[i].tile_offset);
8487
			seq_printf(m, "  TILEOFF: %08x\n", error->plane[i].tile_offset);
8477
		}
8488
		}
8478
 
8489
 
8479
		seq_printf(m, "Cursor [%d]:\n", i);
8490
		seq_printf(m, "Cursor [%d]:\n", i);
8480
		seq_printf(m, "  CNTR: %08x\n", error->cursor[i].control);
8491
		seq_printf(m, "  CNTR: %08x\n", error->cursor[i].control);
8481
		seq_printf(m, "  POS: %08x\n", error->cursor[i].position);
8492
		seq_printf(m, "  POS: %08x\n", error->cursor[i].position);
8482
		seq_printf(m, "  BASE: %08x\n", error->cursor[i].base);
8493
		seq_printf(m, "  BASE: %08x\n", error->cursor[i].base);
8483
	}
8494
	}
8484
}
8495
}
8485
#endif
8496
#endif