Subversion Repositories Kolibri OS

Rev

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

Rev 4392 Rev 4398
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
#define MAX_ERRNO       4095
44
#define MAX_ERRNO       4095
45
phys_addr_t get_bus_addr(void);
45
phys_addr_t get_bus_addr(void);
46
 
46
 
47
bool intel_pipe_has_type(struct drm_crtc *crtc, int type);
47
bool intel_pipe_has_type(struct drm_crtc *crtc, int type);
48
static void intel_increase_pllclock(struct drm_crtc *crtc);
48
static void intel_increase_pllclock(struct drm_crtc *crtc);
49
static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
49
static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
50
 
50
 
51
static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
51
static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
52
				struct intel_crtc_config *pipe_config);
52
				struct intel_crtc_config *pipe_config);
53
static void ironlake_crtc_clock_get(struct intel_crtc *crtc,
53
static void ironlake_crtc_clock_get(struct intel_crtc *crtc,
54
				    struct intel_crtc_config *pipe_config);
54
				    struct intel_crtc_config *pipe_config);
55
 
55
 
56
static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
56
static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
57
			  int x, int y, struct drm_framebuffer *old_fb);
57
			  int x, int y, struct drm_framebuffer *old_fb);
58
 
58
 
59
 
59
 
60
typedef struct {
60
typedef struct {
61
    int min, max;
61
    int min, max;
62
} intel_range_t;
62
} intel_range_t;
63
 
63
 
64
typedef struct {
64
typedef struct {
65
    int dot_limit;
65
    int dot_limit;
66
    int p2_slow, p2_fast;
66
    int p2_slow, p2_fast;
67
} intel_p2_t;
67
} intel_p2_t;
68
 
68
 
69
typedef struct intel_limit intel_limit_t;
69
typedef struct intel_limit intel_limit_t;
70
struct intel_limit {
70
struct intel_limit {
71
    intel_range_t   dot, vco, n, m, m1, m2, p, p1;
71
    intel_range_t   dot, vco, n, m, m1, m2, p, p1;
72
    intel_p2_t      p2;
72
    intel_p2_t      p2;
73
};
73
};
74
 
74
 
75
/* FDI */
75
/* FDI */
76
#define IRONLAKE_FDI_FREQ       2700000 /* in kHz for mode->clock */
76
#define IRONLAKE_FDI_FREQ       2700000 /* in kHz for mode->clock */
77
 
77
 
78
int
78
int
79
intel_pch_rawclk(struct drm_device *dev)
79
intel_pch_rawclk(struct drm_device *dev)
80
{
80
{
81
	struct drm_i915_private *dev_priv = dev->dev_private;
81
	struct drm_i915_private *dev_priv = dev->dev_private;
82
 
82
 
83
	WARN_ON(!HAS_PCH_SPLIT(dev));
83
	WARN_ON(!HAS_PCH_SPLIT(dev));
84
 
84
 
85
	return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
85
	return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
86
}
86
}
87
 
87
 
88
static inline u32 /* units of 100MHz */
88
static inline u32 /* units of 100MHz */
89
intel_fdi_link_freq(struct drm_device *dev)
89
intel_fdi_link_freq(struct drm_device *dev)
90
{
90
{
91
	if (IS_GEN5(dev)) {
91
	if (IS_GEN5(dev)) {
92
		struct drm_i915_private *dev_priv = dev->dev_private;
92
		struct drm_i915_private *dev_priv = dev->dev_private;
93
		return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
93
		return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
94
	} else
94
	} else
95
		return 27;
95
		return 27;
96
}
96
}
97
 
97
 
98
static const intel_limit_t intel_limits_i8xx_dac = {
98
static const intel_limit_t intel_limits_i8xx_dac = {
99
	.dot = { .min = 25000, .max = 350000 },
99
	.dot = { .min = 25000, .max = 350000 },
100
	.vco = { .min = 930000, .max = 1400000 },
100
	.vco = { .min = 930000, .max = 1400000 },
101
	.n = { .min = 3, .max = 16 },
101
	.n = { .min = 3, .max = 16 },
102
	.m = { .min = 96, .max = 140 },
102
	.m = { .min = 96, .max = 140 },
103
	.m1 = { .min = 18, .max = 26 },
103
	.m1 = { .min = 18, .max = 26 },
104
	.m2 = { .min = 6, .max = 16 },
104
	.m2 = { .min = 6, .max = 16 },
105
	.p = { .min = 4, .max = 128 },
105
	.p = { .min = 4, .max = 128 },
106
	.p1 = { .min = 2, .max = 33 },
106
	.p1 = { .min = 2, .max = 33 },
107
	.p2 = { .dot_limit = 165000,
107
	.p2 = { .dot_limit = 165000,
108
		.p2_slow = 4, .p2_fast = 2 },
108
		.p2_slow = 4, .p2_fast = 2 },
109
};
109
};
110
 
110
 
111
static const intel_limit_t intel_limits_i8xx_dvo = {
111
static const intel_limit_t intel_limits_i8xx_dvo = {
112
        .dot = { .min = 25000, .max = 350000 },
112
        .dot = { .min = 25000, .max = 350000 },
113
        .vco = { .min = 930000, .max = 1400000 },
113
        .vco = { .min = 930000, .max = 1400000 },
114
        .n = { .min = 3, .max = 16 },
114
        .n = { .min = 3, .max = 16 },
115
        .m = { .min = 96, .max = 140 },
115
        .m = { .min = 96, .max = 140 },
116
        .m1 = { .min = 18, .max = 26 },
116
        .m1 = { .min = 18, .max = 26 },
117
        .m2 = { .min = 6, .max = 16 },
117
        .m2 = { .min = 6, .max = 16 },
118
        .p = { .min = 4, .max = 128 },
118
        .p = { .min = 4, .max = 128 },
119
        .p1 = { .min = 2, .max = 33 },
119
        .p1 = { .min = 2, .max = 33 },
120
	.p2 = { .dot_limit = 165000,
120
	.p2 = { .dot_limit = 165000,
121
		.p2_slow = 4, .p2_fast = 4 },
121
		.p2_slow = 4, .p2_fast = 4 },
122
};
122
};
123
 
123
 
124
static const intel_limit_t intel_limits_i8xx_lvds = {
124
static const intel_limit_t intel_limits_i8xx_lvds = {
125
        .dot = { .min = 25000, .max = 350000 },
125
        .dot = { .min = 25000, .max = 350000 },
126
        .vco = { .min = 930000, .max = 1400000 },
126
        .vco = { .min = 930000, .max = 1400000 },
127
        .n = { .min = 3, .max = 16 },
127
        .n = { .min = 3, .max = 16 },
128
        .m = { .min = 96, .max = 140 },
128
        .m = { .min = 96, .max = 140 },
129
        .m1 = { .min = 18, .max = 26 },
129
        .m1 = { .min = 18, .max = 26 },
130
        .m2 = { .min = 6, .max = 16 },
130
        .m2 = { .min = 6, .max = 16 },
131
        .p = { .min = 4, .max = 128 },
131
        .p = { .min = 4, .max = 128 },
132
        .p1 = { .min = 1, .max = 6 },
132
        .p1 = { .min = 1, .max = 6 },
133
	.p2 = { .dot_limit = 165000,
133
	.p2 = { .dot_limit = 165000,
134
		.p2_slow = 14, .p2_fast = 7 },
134
		.p2_slow = 14, .p2_fast = 7 },
135
};
135
};
136
 
136
 
137
static const intel_limit_t intel_limits_i9xx_sdvo = {
137
static const intel_limit_t intel_limits_i9xx_sdvo = {
138
        .dot = { .min = 20000, .max = 400000 },
138
        .dot = { .min = 20000, .max = 400000 },
139
        .vco = { .min = 1400000, .max = 2800000 },
139
        .vco = { .min = 1400000, .max = 2800000 },
140
        .n = { .min = 1, .max = 6 },
140
        .n = { .min = 1, .max = 6 },
141
        .m = { .min = 70, .max = 120 },
141
        .m = { .min = 70, .max = 120 },
142
	.m1 = { .min = 8, .max = 18 },
142
	.m1 = { .min = 8, .max = 18 },
143
	.m2 = { .min = 3, .max = 7 },
143
	.m2 = { .min = 3, .max = 7 },
144
        .p = { .min = 5, .max = 80 },
144
        .p = { .min = 5, .max = 80 },
145
        .p1 = { .min = 1, .max = 8 },
145
        .p1 = { .min = 1, .max = 8 },
146
	.p2 = { .dot_limit = 200000,
146
	.p2 = { .dot_limit = 200000,
147
		.p2_slow = 10, .p2_fast = 5 },
147
		.p2_slow = 10, .p2_fast = 5 },
148
};
148
};
149
 
149
 
150
static const intel_limit_t intel_limits_i9xx_lvds = {
150
static const intel_limit_t intel_limits_i9xx_lvds = {
151
        .dot = { .min = 20000, .max = 400000 },
151
        .dot = { .min = 20000, .max = 400000 },
152
        .vco = { .min = 1400000, .max = 2800000 },
152
        .vco = { .min = 1400000, .max = 2800000 },
153
        .n = { .min = 1, .max = 6 },
153
        .n = { .min = 1, .max = 6 },
154
        .m = { .min = 70, .max = 120 },
154
        .m = { .min = 70, .max = 120 },
155
	.m1 = { .min = 8, .max = 18 },
155
	.m1 = { .min = 8, .max = 18 },
156
	.m2 = { .min = 3, .max = 7 },
156
	.m2 = { .min = 3, .max = 7 },
157
        .p = { .min = 7, .max = 98 },
157
        .p = { .min = 7, .max = 98 },
158
        .p1 = { .min = 1, .max = 8 },
158
        .p1 = { .min = 1, .max = 8 },
159
	.p2 = { .dot_limit = 112000,
159
	.p2 = { .dot_limit = 112000,
160
		.p2_slow = 14, .p2_fast = 7 },
160
		.p2_slow = 14, .p2_fast = 7 },
161
};
161
};
162
 
162
 
163
 
163
 
164
static const intel_limit_t intel_limits_g4x_sdvo = {
164
static const intel_limit_t intel_limits_g4x_sdvo = {
165
	.dot = { .min = 25000, .max = 270000 },
165
	.dot = { .min = 25000, .max = 270000 },
166
	.vco = { .min = 1750000, .max = 3500000},
166
	.vco = { .min = 1750000, .max = 3500000},
167
	.n = { .min = 1, .max = 4 },
167
	.n = { .min = 1, .max = 4 },
168
	.m = { .min = 104, .max = 138 },
168
	.m = { .min = 104, .max = 138 },
169
	.m1 = { .min = 17, .max = 23 },
169
	.m1 = { .min = 17, .max = 23 },
170
	.m2 = { .min = 5, .max = 11 },
170
	.m2 = { .min = 5, .max = 11 },
171
	.p = { .min = 10, .max = 30 },
171
	.p = { .min = 10, .max = 30 },
172
	.p1 = { .min = 1, .max = 3},
172
	.p1 = { .min = 1, .max = 3},
173
	.p2 = { .dot_limit = 270000,
173
	.p2 = { .dot_limit = 270000,
174
		.p2_slow = 10,
174
		.p2_slow = 10,
175
		.p2_fast = 10
175
		.p2_fast = 10
176
	},
176
	},
177
};
177
};
178
 
178
 
179
static const intel_limit_t intel_limits_g4x_hdmi = {
179
static const intel_limit_t intel_limits_g4x_hdmi = {
180
	.dot = { .min = 22000, .max = 400000 },
180
	.dot = { .min = 22000, .max = 400000 },
181
	.vco = { .min = 1750000, .max = 3500000},
181
	.vco = { .min = 1750000, .max = 3500000},
182
	.n = { .min = 1, .max = 4 },
182
	.n = { .min = 1, .max = 4 },
183
	.m = { .min = 104, .max = 138 },
183
	.m = { .min = 104, .max = 138 },
184
	.m1 = { .min = 16, .max = 23 },
184
	.m1 = { .min = 16, .max = 23 },
185
	.m2 = { .min = 5, .max = 11 },
185
	.m2 = { .min = 5, .max = 11 },
186
	.p = { .min = 5, .max = 80 },
186
	.p = { .min = 5, .max = 80 },
187
	.p1 = { .min = 1, .max = 8},
187
	.p1 = { .min = 1, .max = 8},
188
	.p2 = { .dot_limit = 165000,
188
	.p2 = { .dot_limit = 165000,
189
		.p2_slow = 10, .p2_fast = 5 },
189
		.p2_slow = 10, .p2_fast = 5 },
190
};
190
};
191
 
191
 
192
static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
192
static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
193
	.dot = { .min = 20000, .max = 115000 },
193
	.dot = { .min = 20000, .max = 115000 },
194
	.vco = { .min = 1750000, .max = 3500000 },
194
	.vco = { .min = 1750000, .max = 3500000 },
195
	.n = { .min = 1, .max = 3 },
195
	.n = { .min = 1, .max = 3 },
196
	.m = { .min = 104, .max = 138 },
196
	.m = { .min = 104, .max = 138 },
197
	.m1 = { .min = 17, .max = 23 },
197
	.m1 = { .min = 17, .max = 23 },
198
	.m2 = { .min = 5, .max = 11 },
198
	.m2 = { .min = 5, .max = 11 },
199
	.p = { .min = 28, .max = 112 },
199
	.p = { .min = 28, .max = 112 },
200
	.p1 = { .min = 2, .max = 8 },
200
	.p1 = { .min = 2, .max = 8 },
201
	.p2 = { .dot_limit = 0,
201
	.p2 = { .dot_limit = 0,
202
		.p2_slow = 14, .p2_fast = 14
202
		.p2_slow = 14, .p2_fast = 14
203
	},
203
	},
204
};
204
};
205
 
205
 
206
static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
206
static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
207
	.dot = { .min = 80000, .max = 224000 },
207
	.dot = { .min = 80000, .max = 224000 },
208
	.vco = { .min = 1750000, .max = 3500000 },
208
	.vco = { .min = 1750000, .max = 3500000 },
209
	.n = { .min = 1, .max = 3 },
209
	.n = { .min = 1, .max = 3 },
210
	.m = { .min = 104, .max = 138 },
210
	.m = { .min = 104, .max = 138 },
211
	.m1 = { .min = 17, .max = 23 },
211
	.m1 = { .min = 17, .max = 23 },
212
	.m2 = { .min = 5, .max = 11 },
212
	.m2 = { .min = 5, .max = 11 },
213
	.p = { .min = 14, .max = 42 },
213
	.p = { .min = 14, .max = 42 },
214
	.p1 = { .min = 2, .max = 6 },
214
	.p1 = { .min = 2, .max = 6 },
215
	.p2 = { .dot_limit = 0,
215
	.p2 = { .dot_limit = 0,
216
		.p2_slow = 7, .p2_fast = 7
216
		.p2_slow = 7, .p2_fast = 7
217
	},
217
	},
218
};
218
};
219
 
219
 
220
static const intel_limit_t intel_limits_pineview_sdvo = {
220
static const intel_limit_t intel_limits_pineview_sdvo = {
221
        .dot = { .min = 20000, .max = 400000},
221
        .dot = { .min = 20000, .max = 400000},
222
        .vco = { .min = 1700000, .max = 3500000 },
222
        .vco = { .min = 1700000, .max = 3500000 },
223
	/* Pineview's Ncounter is a ring counter */
223
	/* Pineview's Ncounter is a ring counter */
224
        .n = { .min = 3, .max = 6 },
224
        .n = { .min = 3, .max = 6 },
225
        .m = { .min = 2, .max = 256 },
225
        .m = { .min = 2, .max = 256 },
226
	/* Pineview only has one combined m divider, which we treat as m2. */
226
	/* Pineview only has one combined m divider, which we treat as m2. */
227
        .m1 = { .min = 0, .max = 0 },
227
        .m1 = { .min = 0, .max = 0 },
228
        .m2 = { .min = 0, .max = 254 },
228
        .m2 = { .min = 0, .max = 254 },
229
        .p = { .min = 5, .max = 80 },
229
        .p = { .min = 5, .max = 80 },
230
        .p1 = { .min = 1, .max = 8 },
230
        .p1 = { .min = 1, .max = 8 },
231
	.p2 = { .dot_limit = 200000,
231
	.p2 = { .dot_limit = 200000,
232
		.p2_slow = 10, .p2_fast = 5 },
232
		.p2_slow = 10, .p2_fast = 5 },
233
};
233
};
234
 
234
 
235
static const intel_limit_t intel_limits_pineview_lvds = {
235
static const intel_limit_t intel_limits_pineview_lvds = {
236
        .dot = { .min = 20000, .max = 400000 },
236
        .dot = { .min = 20000, .max = 400000 },
237
        .vco = { .min = 1700000, .max = 3500000 },
237
        .vco = { .min = 1700000, .max = 3500000 },
238
        .n = { .min = 3, .max = 6 },
238
        .n = { .min = 3, .max = 6 },
239
        .m = { .min = 2, .max = 256 },
239
        .m = { .min = 2, .max = 256 },
240
        .m1 = { .min = 0, .max = 0 },
240
        .m1 = { .min = 0, .max = 0 },
241
        .m2 = { .min = 0, .max = 254 },
241
        .m2 = { .min = 0, .max = 254 },
242
        .p = { .min = 7, .max = 112 },
242
        .p = { .min = 7, .max = 112 },
243
        .p1 = { .min = 1, .max = 8 },
243
        .p1 = { .min = 1, .max = 8 },
244
	.p2 = { .dot_limit = 112000,
244
	.p2 = { .dot_limit = 112000,
245
		.p2_slow = 14, .p2_fast = 14 },
245
		.p2_slow = 14, .p2_fast = 14 },
246
};
246
};
247
 
247
 
248
/* Ironlake / Sandybridge
248
/* Ironlake / Sandybridge
249
 *
249
 *
250
 * We calculate clock using (register_value + 2) for N/M1/M2, so here
250
 * We calculate clock using (register_value + 2) for N/M1/M2, so here
251
 * the range value for them is (actual_value - 2).
251
 * the range value for them is (actual_value - 2).
252
 */
252
 */
253
static const intel_limit_t intel_limits_ironlake_dac = {
253
static const intel_limit_t intel_limits_ironlake_dac = {
254
	.dot = { .min = 25000, .max = 350000 },
254
	.dot = { .min = 25000, .max = 350000 },
255
	.vco = { .min = 1760000, .max = 3510000 },
255
	.vco = { .min = 1760000, .max = 3510000 },
256
	.n = { .min = 1, .max = 5 },
256
	.n = { .min = 1, .max = 5 },
257
	.m = { .min = 79, .max = 127 },
257
	.m = { .min = 79, .max = 127 },
258
	.m1 = { .min = 12, .max = 22 },
258
	.m1 = { .min = 12, .max = 22 },
259
	.m2 = { .min = 5, .max = 9 },
259
	.m2 = { .min = 5, .max = 9 },
260
	.p = { .min = 5, .max = 80 },
260
	.p = { .min = 5, .max = 80 },
261
	.p1 = { .min = 1, .max = 8 },
261
	.p1 = { .min = 1, .max = 8 },
262
	.p2 = { .dot_limit = 225000,
262
	.p2 = { .dot_limit = 225000,
263
		.p2_slow = 10, .p2_fast = 5 },
263
		.p2_slow = 10, .p2_fast = 5 },
264
};
264
};
265
 
265
 
266
static const intel_limit_t intel_limits_ironlake_single_lvds = {
266
static const intel_limit_t intel_limits_ironlake_single_lvds = {
267
	.dot = { .min = 25000, .max = 350000 },
267
	.dot = { .min = 25000, .max = 350000 },
268
	.vco = { .min = 1760000, .max = 3510000 },
268
	.vco = { .min = 1760000, .max = 3510000 },
269
	.n = { .min = 1, .max = 3 },
269
	.n = { .min = 1, .max = 3 },
270
	.m = { .min = 79, .max = 118 },
270
	.m = { .min = 79, .max = 118 },
271
	.m1 = { .min = 12, .max = 22 },
271
	.m1 = { .min = 12, .max = 22 },
272
	.m2 = { .min = 5, .max = 9 },
272
	.m2 = { .min = 5, .max = 9 },
273
	.p = { .min = 28, .max = 112 },
273
	.p = { .min = 28, .max = 112 },
274
	.p1 = { .min = 2, .max = 8 },
274
	.p1 = { .min = 2, .max = 8 },
275
	.p2 = { .dot_limit = 225000,
275
	.p2 = { .dot_limit = 225000,
276
		.p2_slow = 14, .p2_fast = 14 },
276
		.p2_slow = 14, .p2_fast = 14 },
277
};
277
};
278
 
278
 
279
static const intel_limit_t intel_limits_ironlake_dual_lvds = {
279
static const intel_limit_t intel_limits_ironlake_dual_lvds = {
280
	.dot = { .min = 25000, .max = 350000 },
280
	.dot = { .min = 25000, .max = 350000 },
281
	.vco = { .min = 1760000, .max = 3510000 },
281
	.vco = { .min = 1760000, .max = 3510000 },
282
	.n = { .min = 1, .max = 3 },
282
	.n = { .min = 1, .max = 3 },
283
	.m = { .min = 79, .max = 127 },
283
	.m = { .min = 79, .max = 127 },
284
	.m1 = { .min = 12, .max = 22 },
284
	.m1 = { .min = 12, .max = 22 },
285
	.m2 = { .min = 5, .max = 9 },
285
	.m2 = { .min = 5, .max = 9 },
286
	.p = { .min = 14, .max = 56 },
286
	.p = { .min = 14, .max = 56 },
287
	.p1 = { .min = 2, .max = 8 },
287
	.p1 = { .min = 2, .max = 8 },
288
	.p2 = { .dot_limit = 225000,
288
	.p2 = { .dot_limit = 225000,
289
		.p2_slow = 7, .p2_fast = 7 },
289
		.p2_slow = 7, .p2_fast = 7 },
290
};
290
};
291
 
291
 
292
/* LVDS 100mhz refclk limits. */
292
/* LVDS 100mhz refclk limits. */
293
static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
293
static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
294
	.dot = { .min = 25000, .max = 350000 },
294
	.dot = { .min = 25000, .max = 350000 },
295
	.vco = { .min = 1760000, .max = 3510000 },
295
	.vco = { .min = 1760000, .max = 3510000 },
296
	.n = { .min = 1, .max = 2 },
296
	.n = { .min = 1, .max = 2 },
297
	.m = { .min = 79, .max = 126 },
297
	.m = { .min = 79, .max = 126 },
298
	.m1 = { .min = 12, .max = 22 },
298
	.m1 = { .min = 12, .max = 22 },
299
	.m2 = { .min = 5, .max = 9 },
299
	.m2 = { .min = 5, .max = 9 },
300
	.p = { .min = 28, .max = 112 },
300
	.p = { .min = 28, .max = 112 },
301
	.p1 = { .min = 2, .max = 8 },
301
	.p1 = { .min = 2, .max = 8 },
302
	.p2 = { .dot_limit = 225000,
302
	.p2 = { .dot_limit = 225000,
303
		.p2_slow = 14, .p2_fast = 14 },
303
		.p2_slow = 14, .p2_fast = 14 },
304
};
304
};
305
 
305
 
306
static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
306
static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
307
	.dot = { .min = 25000, .max = 350000 },
307
	.dot = { .min = 25000, .max = 350000 },
308
	.vco = { .min = 1760000, .max = 3510000 },
308
	.vco = { .min = 1760000, .max = 3510000 },
309
	.n = { .min = 1, .max = 3 },
309
	.n = { .min = 1, .max = 3 },
310
	.m = { .min = 79, .max = 126 },
310
	.m = { .min = 79, .max = 126 },
311
	.m1 = { .min = 12, .max = 22 },
311
	.m1 = { .min = 12, .max = 22 },
312
	.m2 = { .min = 5, .max = 9 },
312
	.m2 = { .min = 5, .max = 9 },
313
	.p = { .min = 14, .max = 42 },
313
	.p = { .min = 14, .max = 42 },
314
	.p1 = { .min = 2, .max = 6 },
314
	.p1 = { .min = 2, .max = 6 },
315
	.p2 = { .dot_limit = 225000,
315
	.p2 = { .dot_limit = 225000,
316
		.p2_slow = 7, .p2_fast = 7 },
316
		.p2_slow = 7, .p2_fast = 7 },
317
};
317
};
318
 
318
 
319
static const intel_limit_t intel_limits_vlv_dac = {
319
static const intel_limit_t intel_limits_vlv_dac = {
320
	.dot = { .min = 25000, .max = 270000 },
320
	.dot = { .min = 25000, .max = 270000 },
321
	.vco = { .min = 4000000, .max = 6000000 },
321
	.vco = { .min = 4000000, .max = 6000000 },
322
	.n = { .min = 1, .max = 7 },
322
	.n = { .min = 1, .max = 7 },
323
	.m = { .min = 22, .max = 450 }, /* guess */
323
	.m = { .min = 22, .max = 450 }, /* guess */
324
	.m1 = { .min = 2, .max = 3 },
324
	.m1 = { .min = 2, .max = 3 },
325
	.m2 = { .min = 11, .max = 156 },
325
	.m2 = { .min = 11, .max = 156 },
326
	.p = { .min = 10, .max = 30 },
326
	.p = { .min = 10, .max = 30 },
327
	.p1 = { .min = 1, .max = 3 },
327
	.p1 = { .min = 1, .max = 3 },
328
	.p2 = { .dot_limit = 270000,
328
	.p2 = { .dot_limit = 270000,
329
		.p2_slow = 2, .p2_fast = 20 },
329
		.p2_slow = 2, .p2_fast = 20 },
330
};
330
};
331
 
331
 
332
static const intel_limit_t intel_limits_vlv_hdmi = {
332
static const intel_limit_t intel_limits_vlv_hdmi = {
333
	.dot = { .min = 25000, .max = 270000 },
333
	.dot = { .min = 25000, .max = 270000 },
334
	.vco = { .min = 4000000, .max = 6000000 },
334
	.vco = { .min = 4000000, .max = 6000000 },
335
	.n = { .min = 1, .max = 7 },
335
	.n = { .min = 1, .max = 7 },
336
	.m = { .min = 60, .max = 300 }, /* guess */
336
	.m = { .min = 60, .max = 300 }, /* guess */
337
	.m1 = { .min = 2, .max = 3 },
337
	.m1 = { .min = 2, .max = 3 },
338
	.m2 = { .min = 11, .max = 156 },
338
	.m2 = { .min = 11, .max = 156 },
339
	.p = { .min = 10, .max = 30 },
339
	.p = { .min = 10, .max = 30 },
340
	.p1 = { .min = 2, .max = 3 },
340
	.p1 = { .min = 2, .max = 3 },
341
	.p2 = { .dot_limit = 270000,
341
	.p2 = { .dot_limit = 270000,
342
		.p2_slow = 2, .p2_fast = 20 },
342
		.p2_slow = 2, .p2_fast = 20 },
343
};
343
};
344
 
344
 
345
static const intel_limit_t intel_limits_vlv_dp = {
345
static const intel_limit_t intel_limits_vlv_dp = {
346
	.dot = { .min = 25000, .max = 270000 },
346
	.dot = { .min = 25000, .max = 270000 },
347
	.vco = { .min = 4000000, .max = 6000000 },
347
	.vco = { .min = 4000000, .max = 6000000 },
348
	.n = { .min = 1, .max = 7 },
348
	.n = { .min = 1, .max = 7 },
349
	.m = { .min = 22, .max = 450 },
349
	.m = { .min = 22, .max = 450 },
350
	.m1 = { .min = 2, .max = 3 },
350
	.m1 = { .min = 2, .max = 3 },
351
	.m2 = { .min = 11, .max = 156 },
351
	.m2 = { .min = 11, .max = 156 },
352
	.p = { .min = 10, .max = 30 },
352
	.p = { .min = 10, .max = 30 },
353
	.p1 = { .min = 1, .max = 3 },
353
	.p1 = { .min = 1, .max = 3 },
354
	.p2 = { .dot_limit = 270000,
354
	.p2 = { .dot_limit = 270000,
355
		.p2_slow = 2, .p2_fast = 20 },
355
		.p2_slow = 2, .p2_fast = 20 },
356
};
356
};
357
 
357
 
358
static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
358
static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
359
						int refclk)
359
						int refclk)
360
{
360
{
361
	struct drm_device *dev = crtc->dev;
361
	struct drm_device *dev = crtc->dev;
362
	const intel_limit_t *limit;
362
	const intel_limit_t *limit;
363
 
363
 
364
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
364
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
365
		if (intel_is_dual_link_lvds(dev)) {
365
		if (intel_is_dual_link_lvds(dev)) {
366
			if (refclk == 100000)
366
			if (refclk == 100000)
367
				limit = &intel_limits_ironlake_dual_lvds_100m;
367
				limit = &intel_limits_ironlake_dual_lvds_100m;
368
			else
368
			else
369
				limit = &intel_limits_ironlake_dual_lvds;
369
				limit = &intel_limits_ironlake_dual_lvds;
370
		} else {
370
		} else {
371
			if (refclk == 100000)
371
			if (refclk == 100000)
372
				limit = &intel_limits_ironlake_single_lvds_100m;
372
				limit = &intel_limits_ironlake_single_lvds_100m;
373
			else
373
			else
374
				limit = &intel_limits_ironlake_single_lvds;
374
				limit = &intel_limits_ironlake_single_lvds;
375
		}
375
		}
376
	} else
376
	} else
377
		limit = &intel_limits_ironlake_dac;
377
		limit = &intel_limits_ironlake_dac;
378
 
378
 
379
	return limit;
379
	return limit;
380
}
380
}
381
 
381
 
382
static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
382
static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
383
{
383
{
384
	struct drm_device *dev = crtc->dev;
384
	struct drm_device *dev = crtc->dev;
385
	const intel_limit_t *limit;
385
	const intel_limit_t *limit;
386
 
386
 
387
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
387
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
388
		if (intel_is_dual_link_lvds(dev))
388
		if (intel_is_dual_link_lvds(dev))
389
			limit = &intel_limits_g4x_dual_channel_lvds;
389
			limit = &intel_limits_g4x_dual_channel_lvds;
390
		else
390
		else
391
			limit = &intel_limits_g4x_single_channel_lvds;
391
			limit = &intel_limits_g4x_single_channel_lvds;
392
	} else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
392
	} else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
393
		   intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
393
		   intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
394
		limit = &intel_limits_g4x_hdmi;
394
		limit = &intel_limits_g4x_hdmi;
395
	} else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
395
	} else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
396
		limit = &intel_limits_g4x_sdvo;
396
		limit = &intel_limits_g4x_sdvo;
397
	} else /* The option is for other outputs */
397
	} else /* The option is for other outputs */
398
		limit = &intel_limits_i9xx_sdvo;
398
		limit = &intel_limits_i9xx_sdvo;
399
 
399
 
400
	return limit;
400
	return limit;
401
}
401
}
402
 
402
 
403
static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
403
static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
404
{
404
{
405
	struct drm_device *dev = crtc->dev;
405
	struct drm_device *dev = crtc->dev;
406
	const intel_limit_t *limit;
406
	const intel_limit_t *limit;
407
 
407
 
408
	if (HAS_PCH_SPLIT(dev))
408
	if (HAS_PCH_SPLIT(dev))
409
		limit = intel_ironlake_limit(crtc, refclk);
409
		limit = intel_ironlake_limit(crtc, refclk);
410
	else if (IS_G4X(dev)) {
410
	else if (IS_G4X(dev)) {
411
		limit = intel_g4x_limit(crtc);
411
		limit = intel_g4x_limit(crtc);
412
	} else if (IS_PINEVIEW(dev)) {
412
	} else if (IS_PINEVIEW(dev)) {
413
		if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
413
		if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
414
			limit = &intel_limits_pineview_lvds;
414
			limit = &intel_limits_pineview_lvds;
415
		else
415
		else
416
			limit = &intel_limits_pineview_sdvo;
416
			limit = &intel_limits_pineview_sdvo;
417
	} else if (IS_VALLEYVIEW(dev)) {
417
	} else if (IS_VALLEYVIEW(dev)) {
418
		if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG))
418
		if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG))
419
			limit = &intel_limits_vlv_dac;
419
			limit = &intel_limits_vlv_dac;
420
		else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
420
		else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
421
			limit = &intel_limits_vlv_hdmi;
421
			limit = &intel_limits_vlv_hdmi;
422
		else
422
		else
423
			limit = &intel_limits_vlv_dp;
423
			limit = &intel_limits_vlv_dp;
424
	} else if (!IS_GEN2(dev)) {
424
	} else if (!IS_GEN2(dev)) {
425
		if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
425
		if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
426
			limit = &intel_limits_i9xx_lvds;
426
			limit = &intel_limits_i9xx_lvds;
427
		else
427
		else
428
			limit = &intel_limits_i9xx_sdvo;
428
			limit = &intel_limits_i9xx_sdvo;
429
	} else {
429
	} else {
430
		if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
430
		if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
431
			limit = &intel_limits_i8xx_lvds;
431
			limit = &intel_limits_i8xx_lvds;
432
		else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
432
		else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
433
			limit = &intel_limits_i8xx_dvo;
433
			limit = &intel_limits_i8xx_dvo;
434
		else
434
		else
435
			limit = &intel_limits_i8xx_dac;
435
			limit = &intel_limits_i8xx_dac;
436
	}
436
	}
437
	return limit;
437
	return limit;
438
}
438
}
439
 
439
 
440
/* m1 is reserved as 0 in Pineview, n is a ring counter */
440
/* m1 is reserved as 0 in Pineview, n is a ring counter */
441
static void pineview_clock(int refclk, intel_clock_t *clock)
441
static void pineview_clock(int refclk, intel_clock_t *clock)
442
{
442
{
443
	clock->m = clock->m2 + 2;
443
	clock->m = clock->m2 + 2;
444
	clock->p = clock->p1 * clock->p2;
444
	clock->p = clock->p1 * clock->p2;
445
	clock->vco = refclk * clock->m / clock->n;
445
	clock->vco = refclk * clock->m / clock->n;
446
	clock->dot = clock->vco / clock->p;
446
	clock->dot = clock->vco / clock->p;
447
}
447
}
448
 
448
 
449
static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
449
static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
450
{
450
{
451
	return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
451
	return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
452
}
452
}
453
 
453
 
454
static void i9xx_clock(int refclk, intel_clock_t *clock)
454
static void i9xx_clock(int refclk, intel_clock_t *clock)
455
{
455
{
456
	clock->m = i9xx_dpll_compute_m(clock);
456
	clock->m = i9xx_dpll_compute_m(clock);
457
	clock->p = clock->p1 * clock->p2;
457
	clock->p = clock->p1 * clock->p2;
458
	clock->vco = refclk * clock->m / (clock->n + 2);
458
	clock->vco = refclk * clock->m / (clock->n + 2);
459
	clock->dot = clock->vco / clock->p;
459
	clock->dot = clock->vco / clock->p;
460
}
460
}
461
 
461
 
462
/**
462
/**
463
 * Returns whether any output on the specified pipe is of the specified type
463
 * Returns whether any output on the specified pipe is of the specified type
464
 */
464
 */
465
bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
465
bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
466
{
466
{
467
	struct drm_device *dev = crtc->dev;
467
	struct drm_device *dev = crtc->dev;
468
	struct intel_encoder *encoder;
468
	struct intel_encoder *encoder;
469
 
469
 
470
	for_each_encoder_on_crtc(dev, crtc, encoder)
470
	for_each_encoder_on_crtc(dev, crtc, encoder)
471
		if (encoder->type == type)
471
		if (encoder->type == type)
472
			return true;
472
			return true;
473
 
473
 
474
	return false;
474
	return false;
475
}
475
}
476
 
476
 
477
#define INTELPllInvalid(s)   do { /* DRM_DEBUG(s); */ return false; } while (0)
477
#define INTELPllInvalid(s)   do { /* DRM_DEBUG(s); */ return false; } while (0)
478
/**
478
/**
479
 * Returns whether the given set of divisors are valid for a given refclk with
479
 * Returns whether the given set of divisors are valid for a given refclk with
480
 * the given connectors.
480
 * the given connectors.
481
 */
481
 */
482
 
482
 
483
static bool intel_PLL_is_valid(struct drm_device *dev,
483
static bool intel_PLL_is_valid(struct drm_device *dev,
484
			       const intel_limit_t *limit,
484
			       const intel_limit_t *limit,
485
			       const intel_clock_t *clock)
485
			       const intel_clock_t *clock)
486
{
486
{
487
	if (clock->p1  < limit->p1.min  || limit->p1.max  < clock->p1)
487
	if (clock->p1  < limit->p1.min  || limit->p1.max  < clock->p1)
488
		INTELPllInvalid("p1 out of range\n");
488
		INTELPllInvalid("p1 out of range\n");
489
	if (clock->p   < limit->p.min   || limit->p.max   < clock->p)
489
	if (clock->p   < limit->p.min   || limit->p.max   < clock->p)
490
		INTELPllInvalid("p out of range\n");
490
		INTELPllInvalid("p out of range\n");
491
	if (clock->m2  < limit->m2.min  || limit->m2.max  < clock->m2)
491
	if (clock->m2  < limit->m2.min  || limit->m2.max  < clock->m2)
492
		INTELPllInvalid("m2 out of range\n");
492
		INTELPllInvalid("m2 out of range\n");
493
	if (clock->m1  < limit->m1.min  || limit->m1.max  < clock->m1)
493
	if (clock->m1  < limit->m1.min  || limit->m1.max  < clock->m1)
494
		INTELPllInvalid("m1 out of range\n");
494
		INTELPllInvalid("m1 out of range\n");
495
	if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
495
	if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
496
		INTELPllInvalid("m1 <= m2\n");
496
		INTELPllInvalid("m1 <= m2\n");
497
	if (clock->m   < limit->m.min   || limit->m.max   < clock->m)
497
	if (clock->m   < limit->m.min   || limit->m.max   < clock->m)
498
		INTELPllInvalid("m out of range\n");
498
		INTELPllInvalid("m out of range\n");
499
	if (clock->n   < limit->n.min   || limit->n.max   < clock->n)
499
	if (clock->n   < limit->n.min   || limit->n.max   < clock->n)
500
		INTELPllInvalid("n out of range\n");
500
		INTELPllInvalid("n out of range\n");
501
	if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
501
	if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
502
		INTELPllInvalid("vco out of range\n");
502
		INTELPllInvalid("vco out of range\n");
503
	/* XXX: We may need to be checking "Dot clock" depending on the multiplier,
503
	/* XXX: We may need to be checking "Dot clock" depending on the multiplier,
504
	 * connector, etc., rather than just a single range.
504
	 * connector, etc., rather than just a single range.
505
	 */
505
	 */
506
	if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
506
	if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
507
		INTELPllInvalid("dot out of range\n");
507
		INTELPllInvalid("dot out of range\n");
508
 
508
 
509
	return true;
509
	return true;
510
}
510
}
511
 
511
 
512
static bool
512
static bool
513
i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
513
i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
514
		    int target, int refclk, intel_clock_t *match_clock,
514
		    int target, int refclk, intel_clock_t *match_clock,
515
		    intel_clock_t *best_clock)
515
		    intel_clock_t *best_clock)
516
{
516
{
517
	struct drm_device *dev = crtc->dev;
517
	struct drm_device *dev = crtc->dev;
518
	intel_clock_t clock;
518
	intel_clock_t clock;
519
	int err = target;
519
	int err = target;
520
 
520
 
521
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
521
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
522
		/*
522
		/*
523
		 * For LVDS just rely on its current settings for dual-channel.
523
		 * For LVDS just rely on its current settings for dual-channel.
524
		 * We haven't figured out how to reliably set up different
524
		 * We haven't figured out how to reliably set up different
525
		 * single/dual channel state, if we even can.
525
		 * single/dual channel state, if we even can.
526
		 */
526
		 */
527
		if (intel_is_dual_link_lvds(dev))
527
		if (intel_is_dual_link_lvds(dev))
528
			clock.p2 = limit->p2.p2_fast;
528
			clock.p2 = limit->p2.p2_fast;
529
		else
529
		else
530
			clock.p2 = limit->p2.p2_slow;
530
			clock.p2 = limit->p2.p2_slow;
531
	} else {
531
	} else {
532
		if (target < limit->p2.dot_limit)
532
		if (target < limit->p2.dot_limit)
533
			clock.p2 = limit->p2.p2_slow;
533
			clock.p2 = limit->p2.p2_slow;
534
		else
534
		else
535
			clock.p2 = limit->p2.p2_fast;
535
			clock.p2 = limit->p2.p2_fast;
536
	}
536
	}
537
 
537
 
538
	memset(best_clock, 0, sizeof(*best_clock));
538
	memset(best_clock, 0, sizeof(*best_clock));
539
 
539
 
540
	for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
540
	for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
541
	     clock.m1++) {
541
	     clock.m1++) {
542
		for (clock.m2 = limit->m2.min;
542
		for (clock.m2 = limit->m2.min;
543
		     clock.m2 <= limit->m2.max; clock.m2++) {
543
		     clock.m2 <= limit->m2.max; clock.m2++) {
544
			if (clock.m2 >= clock.m1)
544
			if (clock.m2 >= clock.m1)
545
				break;
545
				break;
546
			for (clock.n = limit->n.min;
546
			for (clock.n = limit->n.min;
547
			     clock.n <= limit->n.max; clock.n++) {
547
			     clock.n <= limit->n.max; clock.n++) {
548
				for (clock.p1 = limit->p1.min;
548
				for (clock.p1 = limit->p1.min;
549
					clock.p1 <= limit->p1.max; clock.p1++) {
549
					clock.p1 <= limit->p1.max; clock.p1++) {
550
					int this_err;
550
					int this_err;
551
 
551
 
552
					i9xx_clock(refclk, &clock);
552
					i9xx_clock(refclk, &clock);
553
					if (!intel_PLL_is_valid(dev, limit,
553
					if (!intel_PLL_is_valid(dev, limit,
554
								&clock))
554
								&clock))
555
						continue;
555
						continue;
556
					if (match_clock &&
556
					if (match_clock &&
557
					    clock.p != match_clock->p)
557
					    clock.p != match_clock->p)
558
						continue;
558
						continue;
559
 
559
 
560
					this_err = abs(clock.dot - target);
560
					this_err = abs(clock.dot - target);
561
					if (this_err < err) {
561
					if (this_err < err) {
562
						*best_clock = clock;
562
						*best_clock = clock;
563
						err = this_err;
563
						err = this_err;
564
					}
564
					}
565
				}
565
				}
566
			}
566
			}
567
		}
567
		}
568
	}
568
	}
569
 
569
 
570
	return (err != target);
570
	return (err != target);
571
}
571
}
572
 
572
 
573
static bool
573
static bool
574
pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
574
pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
575
		   int target, int refclk, intel_clock_t *match_clock,
575
		   int target, int refclk, intel_clock_t *match_clock,
576
		   intel_clock_t *best_clock)
576
		   intel_clock_t *best_clock)
577
{
577
{
578
	struct drm_device *dev = crtc->dev;
578
	struct drm_device *dev = crtc->dev;
579
	intel_clock_t clock;
579
	intel_clock_t clock;
580
	int err = target;
580
	int err = target;
581
 
581
 
582
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
582
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
583
		/*
583
		/*
584
		 * For LVDS just rely on its current settings for dual-channel.
584
		 * For LVDS just rely on its current settings for dual-channel.
585
		 * We haven't figured out how to reliably set up different
585
		 * We haven't figured out how to reliably set up different
586
		 * single/dual channel state, if we even can.
586
		 * single/dual channel state, if we even can.
587
		 */
587
		 */
588
		if (intel_is_dual_link_lvds(dev))
588
		if (intel_is_dual_link_lvds(dev))
589
			clock.p2 = limit->p2.p2_fast;
589
			clock.p2 = limit->p2.p2_fast;
590
		else
590
		else
591
			clock.p2 = limit->p2.p2_slow;
591
			clock.p2 = limit->p2.p2_slow;
592
	} else {
592
	} else {
593
		if (target < limit->p2.dot_limit)
593
		if (target < limit->p2.dot_limit)
594
			clock.p2 = limit->p2.p2_slow;
594
			clock.p2 = limit->p2.p2_slow;
595
		else
595
		else
596
			clock.p2 = limit->p2.p2_fast;
596
			clock.p2 = limit->p2.p2_fast;
597
	}
597
	}
598
 
598
 
599
	memset(best_clock, 0, sizeof(*best_clock));
599
	memset(best_clock, 0, sizeof(*best_clock));
600
 
600
 
601
	for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
601
	for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
602
	     clock.m1++) {
602
	     clock.m1++) {
603
		for (clock.m2 = limit->m2.min;
603
		for (clock.m2 = limit->m2.min;
604
		     clock.m2 <= limit->m2.max; clock.m2++) {
604
		     clock.m2 <= limit->m2.max; clock.m2++) {
605
			for (clock.n = limit->n.min;
605
			for (clock.n = limit->n.min;
606
			     clock.n <= limit->n.max; clock.n++) {
606
			     clock.n <= limit->n.max; clock.n++) {
607
				for (clock.p1 = limit->p1.min;
607
				for (clock.p1 = limit->p1.min;
608
					clock.p1 <= limit->p1.max; clock.p1++) {
608
					clock.p1 <= limit->p1.max; clock.p1++) {
609
					int this_err;
609
					int this_err;
610
 
610
 
611
					pineview_clock(refclk, &clock);
611
					pineview_clock(refclk, &clock);
612
					if (!intel_PLL_is_valid(dev, limit,
612
					if (!intel_PLL_is_valid(dev, limit,
613
								&clock))
613
								&clock))
614
						continue;
614
						continue;
615
					if (match_clock &&
615
					if (match_clock &&
616
					    clock.p != match_clock->p)
616
					    clock.p != match_clock->p)
617
						continue;
617
						continue;
618
 
618
 
619
					this_err = abs(clock.dot - target);
619
					this_err = abs(clock.dot - target);
620
					if (this_err < err) {
620
					if (this_err < err) {
621
						*best_clock = clock;
621
						*best_clock = clock;
622
						err = this_err;
622
						err = this_err;
623
					}
623
					}
624
				}
624
				}
625
			}
625
			}
626
		}
626
		}
627
	}
627
	}
628
 
628
 
629
	return (err != target);
629
	return (err != target);
630
}
630
}
631
 
631
 
632
static bool
632
static bool
633
g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
633
g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
634
			int target, int refclk, intel_clock_t *match_clock,
634
			int target, int refclk, intel_clock_t *match_clock,
635
			intel_clock_t *best_clock)
635
			intel_clock_t *best_clock)
636
{
636
{
637
	struct drm_device *dev = crtc->dev;
637
	struct drm_device *dev = crtc->dev;
638
	intel_clock_t clock;
638
	intel_clock_t clock;
639
	int max_n;
639
	int max_n;
640
	bool found;
640
	bool found;
641
	/* approximately equals target * 0.00585 */
641
	/* approximately equals target * 0.00585 */
642
	int err_most = (target >> 8) + (target >> 9);
642
	int err_most = (target >> 8) + (target >> 9);
643
	found = false;
643
	found = false;
644
 
644
 
645
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
645
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
646
		if (intel_is_dual_link_lvds(dev))
646
		if (intel_is_dual_link_lvds(dev))
647
			clock.p2 = limit->p2.p2_fast;
647
			clock.p2 = limit->p2.p2_fast;
648
		else
648
		else
649
			clock.p2 = limit->p2.p2_slow;
649
			clock.p2 = limit->p2.p2_slow;
650
	} else {
650
	} else {
651
		if (target < limit->p2.dot_limit)
651
		if (target < limit->p2.dot_limit)
652
			clock.p2 = limit->p2.p2_slow;
652
			clock.p2 = limit->p2.p2_slow;
653
		else
653
		else
654
			clock.p2 = limit->p2.p2_fast;
654
			clock.p2 = limit->p2.p2_fast;
655
	}
655
	}
656
 
656
 
657
	memset(best_clock, 0, sizeof(*best_clock));
657
	memset(best_clock, 0, sizeof(*best_clock));
658
	max_n = limit->n.max;
658
	max_n = limit->n.max;
659
	/* based on hardware requirement, prefer smaller n to precision */
659
	/* based on hardware requirement, prefer smaller n to precision */
660
	for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
660
	for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
661
		/* based on hardware requirement, prefere larger m1,m2 */
661
		/* based on hardware requirement, prefere larger m1,m2 */
662
		for (clock.m1 = limit->m1.max;
662
		for (clock.m1 = limit->m1.max;
663
		     clock.m1 >= limit->m1.min; clock.m1--) {
663
		     clock.m1 >= limit->m1.min; clock.m1--) {
664
			for (clock.m2 = limit->m2.max;
664
			for (clock.m2 = limit->m2.max;
665
			     clock.m2 >= limit->m2.min; clock.m2--) {
665
			     clock.m2 >= limit->m2.min; clock.m2--) {
666
				for (clock.p1 = limit->p1.max;
666
				for (clock.p1 = limit->p1.max;
667
				     clock.p1 >= limit->p1.min; clock.p1--) {
667
				     clock.p1 >= limit->p1.min; clock.p1--) {
668
					int this_err;
668
					int this_err;
669
 
669
 
670
					i9xx_clock(refclk, &clock);
670
					i9xx_clock(refclk, &clock);
671
					if (!intel_PLL_is_valid(dev, limit,
671
					if (!intel_PLL_is_valid(dev, limit,
672
								&clock))
672
								&clock))
673
						continue;
673
						continue;
674
 
674
 
675
					this_err = abs(clock.dot - target);
675
					this_err = abs(clock.dot - target);
676
					if (this_err < err_most) {
676
					if (this_err < err_most) {
677
						*best_clock = clock;
677
						*best_clock = clock;
678
						err_most = this_err;
678
						err_most = this_err;
679
						max_n = clock.n;
679
						max_n = clock.n;
680
						found = true;
680
						found = true;
681
					}
681
					}
682
				}
682
				}
683
			}
683
			}
684
		}
684
		}
685
	}
685
	}
686
	return found;
686
	return found;
687
}
687
}
688
 
688
 
689
static bool
689
static bool
690
vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
690
vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
691
			int target, int refclk, intel_clock_t *match_clock,
691
			int target, int refclk, intel_clock_t *match_clock,
692
			intel_clock_t *best_clock)
692
			intel_clock_t *best_clock)
693
{
693
{
694
	u32 p1, p2, m1, m2, vco, bestn, bestm1, bestm2, bestp1, bestp2;
694
	u32 p1, p2, m1, m2, vco, bestn, bestm1, bestm2, bestp1, bestp2;
695
	u32 m, n, fastclk;
695
	u32 m, n, fastclk;
696
	u32 updrate, minupdate, p;
696
	u32 updrate, minupdate, p;
697
	unsigned long bestppm, ppm, absppm;
697
	unsigned long bestppm, ppm, absppm;
698
	int dotclk, flag;
698
	int dotclk, flag;
699
 
699
 
700
	flag = 0;
700
	flag = 0;
701
	dotclk = target * 1000;
701
	dotclk = target * 1000;
702
	bestppm = 1000000;
702
	bestppm = 1000000;
703
	ppm = absppm = 0;
703
	ppm = absppm = 0;
704
	fastclk = dotclk / (2*100);
704
	fastclk = dotclk / (2*100);
705
	updrate = 0;
705
	updrate = 0;
706
	minupdate = 19200;
706
	minupdate = 19200;
707
	n = p = p1 = p2 = m = m1 = m2 = vco = bestn = 0;
707
	n = p = p1 = p2 = m = m1 = m2 = vco = bestn = 0;
708
	bestm1 = bestm2 = bestp1 = bestp2 = 0;
708
	bestm1 = bestm2 = bestp1 = bestp2 = 0;
709
 
709
 
710
	/* based on hardware requirement, prefer smaller n to precision */
710
	/* based on hardware requirement, prefer smaller n to precision */
711
	for (n = limit->n.min; n <= ((refclk) / minupdate); n++) {
711
	for (n = limit->n.min; n <= ((refclk) / minupdate); n++) {
712
		updrate = refclk / n;
712
		updrate = refclk / n;
713
		for (p1 = limit->p1.max; p1 > limit->p1.min; p1--) {
713
		for (p1 = limit->p1.max; p1 > limit->p1.min; p1--) {
714
			for (p2 = limit->p2.p2_fast+1; p2 > 0; p2--) {
714
			for (p2 = limit->p2.p2_fast+1; p2 > 0; p2--) {
715
				if (p2 > 10)
715
				if (p2 > 10)
716
					p2 = p2 - 1;
716
					p2 = p2 - 1;
717
				p = p1 * p2;
717
				p = p1 * p2;
718
				/* based on hardware requirement, prefer bigger m1,m2 values */
718
				/* based on hardware requirement, prefer bigger m1,m2 values */
719
				for (m1 = limit->m1.min; m1 <= limit->m1.max; m1++) {
719
				for (m1 = limit->m1.min; m1 <= limit->m1.max; m1++) {
720
					m2 = (((2*(fastclk * p * n / m1 )) +
720
					m2 = (((2*(fastclk * p * n / m1 )) +
721
					       refclk) / (2*refclk));
721
					       refclk) / (2*refclk));
722
					m = m1 * m2;
722
					m = m1 * m2;
723
					vco = updrate * m;
723
					vco = updrate * m;
724
					if (vco >= limit->vco.min && vco < limit->vco.max) {
724
					if (vco >= limit->vco.min && vco < limit->vco.max) {
725
						ppm = 1000000 * ((vco / p) - fastclk) / fastclk;
725
						ppm = 1000000 * ((vco / p) - fastclk) / fastclk;
726
						absppm = (ppm > 0) ? ppm : (-ppm);
726
						absppm = (ppm > 0) ? ppm : (-ppm);
727
						if (absppm < 100 && ((p1 * p2) > (bestp1 * bestp2))) {
727
						if (absppm < 100 && ((p1 * p2) > (bestp1 * bestp2))) {
728
							bestppm = 0;
728
							bestppm = 0;
729
							flag = 1;
729
							flag = 1;
730
						}
730
						}
731
						if (absppm < bestppm - 10) {
731
						if (absppm < bestppm - 10) {
732
							bestppm = absppm;
732
							bestppm = absppm;
733
							flag = 1;
733
							flag = 1;
734
						}
734
						}
735
						if (flag) {
735
						if (flag) {
736
							bestn = n;
736
							bestn = n;
737
							bestm1 = m1;
737
							bestm1 = m1;
738
							bestm2 = m2;
738
							bestm2 = m2;
739
							bestp1 = p1;
739
							bestp1 = p1;
740
							bestp2 = p2;
740
							bestp2 = p2;
741
							flag = 0;
741
							flag = 0;
742
						}
742
						}
743
					}
743
					}
744
				}
744
				}
745
			}
745
			}
746
		}
746
		}
747
	}
747
	}
748
	best_clock->n = bestn;
748
	best_clock->n = bestn;
749
	best_clock->m1 = bestm1;
749
	best_clock->m1 = bestm1;
750
	best_clock->m2 = bestm2;
750
	best_clock->m2 = bestm2;
751
	best_clock->p1 = bestp1;
751
	best_clock->p1 = bestp1;
752
	best_clock->p2 = bestp2;
752
	best_clock->p2 = bestp2;
753
 
753
 
754
	return true;
754
	return true;
755
}
755
}
756
 
756
 
757
enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
757
enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
758
					     enum pipe pipe)
758
					     enum pipe pipe)
759
{
759
{
760
	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
760
	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
761
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
761
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
762
 
762
 
763
	return intel_crtc->config.cpu_transcoder;
763
	return intel_crtc->config.cpu_transcoder;
764
}
764
}
765
 
765
 
766
static void ironlake_wait_for_vblank(struct drm_device *dev, int pipe)
766
static void ironlake_wait_for_vblank(struct drm_device *dev, int pipe)
767
{
767
{
768
	struct drm_i915_private *dev_priv = dev->dev_private;
768
	struct drm_i915_private *dev_priv = dev->dev_private;
769
	u32 frame, frame_reg = PIPEFRAME(pipe);
769
	u32 frame, frame_reg = PIPEFRAME(pipe);
770
 
770
 
771
	frame = I915_READ(frame_reg);
771
	frame = I915_READ(frame_reg);
772
 
772
 
773
	if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
773
	if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
774
		DRM_DEBUG_KMS("vblank wait timed out\n");
774
		DRM_DEBUG_KMS("vblank wait timed out\n");
775
}
775
}
776
 
776
 
777
/**
777
/**
778
 * intel_wait_for_vblank - wait for vblank on a given pipe
778
 * intel_wait_for_vblank - wait for vblank on a given pipe
779
 * @dev: drm device
779
 * @dev: drm device
780
 * @pipe: pipe to wait for
780
 * @pipe: pipe to wait for
781
 *
781
 *
782
 * Wait for vblank to occur on a given pipe.  Needed for various bits of
782
 * Wait for vblank to occur on a given pipe.  Needed for various bits of
783
 * mode setting code.
783
 * mode setting code.
784
 */
784
 */
785
void intel_wait_for_vblank(struct drm_device *dev, int pipe)
785
void intel_wait_for_vblank(struct drm_device *dev, int pipe)
786
{
786
{
787
	struct drm_i915_private *dev_priv = dev->dev_private;
787
	struct drm_i915_private *dev_priv = dev->dev_private;
788
	int pipestat_reg = PIPESTAT(pipe);
788
	int pipestat_reg = PIPESTAT(pipe);
789
 
789
 
790
	if (INTEL_INFO(dev)->gen >= 5) {
790
	if (INTEL_INFO(dev)->gen >= 5) {
791
		ironlake_wait_for_vblank(dev, pipe);
791
		ironlake_wait_for_vblank(dev, pipe);
792
		return;
792
		return;
793
	}
793
	}
794
 
794
 
795
	/* Clear existing vblank status. Note this will clear any other
795
	/* Clear existing vblank status. Note this will clear any other
796
	 * sticky status fields as well.
796
	 * sticky status fields as well.
797
	 *
797
	 *
798
	 * This races with i915_driver_irq_handler() with the result
798
	 * This races with i915_driver_irq_handler() with the result
799
	 * that either function could miss a vblank event.  Here it is not
799
	 * that either function could miss a vblank event.  Here it is not
800
	 * fatal, as we will either wait upon the next vblank interrupt or
800
	 * fatal, as we will either wait upon the next vblank interrupt or
801
	 * timeout.  Generally speaking intel_wait_for_vblank() is only
801
	 * timeout.  Generally speaking intel_wait_for_vblank() is only
802
	 * called during modeset at which time the GPU should be idle and
802
	 * called during modeset at which time the GPU should be idle and
803
	 * should *not* be performing page flips and thus not waiting on
803
	 * should *not* be performing page flips and thus not waiting on
804
	 * vblanks...
804
	 * vblanks...
805
	 * Currently, the result of us stealing a vblank from the irq
805
	 * Currently, the result of us stealing a vblank from the irq
806
	 * handler is that a single frame will be skipped during swapbuffers.
806
	 * handler is that a single frame will be skipped during swapbuffers.
807
	 */
807
	 */
808
	I915_WRITE(pipestat_reg,
808
	I915_WRITE(pipestat_reg,
809
		   I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
809
		   I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
810
 
810
 
811
	/* Wait for vblank interrupt bit to set */
811
	/* Wait for vblank interrupt bit to set */
812
	if (wait_for(I915_READ(pipestat_reg) &
812
	if (wait_for(I915_READ(pipestat_reg) &
813
		     PIPE_VBLANK_INTERRUPT_STATUS,
813
		     PIPE_VBLANK_INTERRUPT_STATUS,
814
		     50))
814
		     50))
815
		DRM_DEBUG_KMS("vblank wait timed out\n");
815
		DRM_DEBUG_KMS("vblank wait timed out\n");
816
}
816
}
817
 
817
 
818
/*
818
/*
819
 * intel_wait_for_pipe_off - wait for pipe to turn off
819
 * intel_wait_for_pipe_off - wait for pipe to turn off
820
 * @dev: drm device
820
 * @dev: drm device
821
 * @pipe: pipe to wait for
821
 * @pipe: pipe to wait for
822
 *
822
 *
823
 * After disabling a pipe, we can't wait for vblank in the usual way,
823
 * After disabling a pipe, we can't wait for vblank in the usual way,
824
 * spinning on the vblank interrupt status bit, since we won't actually
824
 * spinning on the vblank interrupt status bit, since we won't actually
825
 * see an interrupt when the pipe is disabled.
825
 * see an interrupt when the pipe is disabled.
826
 *
826
 *
827
 * On Gen4 and above:
827
 * On Gen4 and above:
828
 *   wait for the pipe register state bit to turn off
828
 *   wait for the pipe register state bit to turn off
829
 *
829
 *
830
 * Otherwise:
830
 * Otherwise:
831
 *   wait for the display line value to settle (it usually
831
 *   wait for the display line value to settle (it usually
832
 *   ends up stopping at the start of the next frame).
832
 *   ends up stopping at the start of the next frame).
833
 *
833
 *
834
 */
834
 */
835
void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
835
void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
836
{
836
{
837
	struct drm_i915_private *dev_priv = dev->dev_private;
837
	struct drm_i915_private *dev_priv = dev->dev_private;
838
	enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
838
	enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
839
								      pipe);
839
								      pipe);
840
 
840
 
841
	if (INTEL_INFO(dev)->gen >= 4) {
841
	if (INTEL_INFO(dev)->gen >= 4) {
842
		int reg = PIPECONF(cpu_transcoder);
842
		int reg = PIPECONF(cpu_transcoder);
843
 
843
 
844
		/* Wait for the Pipe State to go off */
844
		/* Wait for the Pipe State to go off */
845
		if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
845
		if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
846
			     100))
846
			     100))
847
			WARN(1, "pipe_off wait timed out\n");
847
			WARN(1, "pipe_off wait timed out\n");
848
	} else {
848
	} else {
849
		u32 last_line, line_mask;
849
		u32 last_line, line_mask;
850
		int reg = PIPEDSL(pipe);
850
		int reg = PIPEDSL(pipe);
851
		unsigned long timeout = GetTimerTicks() + msecs_to_jiffies(100);
851
		unsigned long timeout = GetTimerTicks() + msecs_to_jiffies(100);
852
 
852
 
853
		if (IS_GEN2(dev))
853
		if (IS_GEN2(dev))
854
			line_mask = DSL_LINEMASK_GEN2;
854
			line_mask = DSL_LINEMASK_GEN2;
855
		else
855
		else
856
			line_mask = DSL_LINEMASK_GEN3;
856
			line_mask = DSL_LINEMASK_GEN3;
857
 
857
 
858
		/* Wait for the display line to settle */
858
		/* Wait for the display line to settle */
859
		do {
859
		do {
860
			last_line = I915_READ(reg) & line_mask;
860
			last_line = I915_READ(reg) & line_mask;
861
			mdelay(5);
861
			mdelay(5);
862
		} while (((I915_READ(reg) & line_mask) != last_line) &&
862
		} while (((I915_READ(reg) & line_mask) != last_line) &&
863
			 time_after(timeout, GetTimerTicks()));
863
			 time_after(timeout, GetTimerTicks()));
864
		if (time_after(GetTimerTicks(), timeout))
864
		if (time_after(GetTimerTicks(), timeout))
865
			WARN(1, "pipe_off wait timed out\n");
865
			WARN(1, "pipe_off wait timed out\n");
866
	}
866
	}
867
}
867
}
868
 
868
 
869
/*
869
/*
870
 * ibx_digital_port_connected - is the specified port connected?
870
 * ibx_digital_port_connected - is the specified port connected?
871
 * @dev_priv: i915 private structure
871
 * @dev_priv: i915 private structure
872
 * @port: the port to test
872
 * @port: the port to test
873
 *
873
 *
874
 * Returns true if @port is connected, false otherwise.
874
 * Returns true if @port is connected, false otherwise.
875
 */
875
 */
876
bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
876
bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
877
				struct intel_digital_port *port)
877
				struct intel_digital_port *port)
878
{
878
{
879
	u32 bit;
879
	u32 bit;
880
 
880
 
881
	if (HAS_PCH_IBX(dev_priv->dev)) {
881
	if (HAS_PCH_IBX(dev_priv->dev)) {
882
		switch(port->port) {
882
		switch(port->port) {
883
		case PORT_B:
883
		case PORT_B:
884
			bit = SDE_PORTB_HOTPLUG;
884
			bit = SDE_PORTB_HOTPLUG;
885
			break;
885
			break;
886
		case PORT_C:
886
		case PORT_C:
887
			bit = SDE_PORTC_HOTPLUG;
887
			bit = SDE_PORTC_HOTPLUG;
888
			break;
888
			break;
889
		case PORT_D:
889
		case PORT_D:
890
			bit = SDE_PORTD_HOTPLUG;
890
			bit = SDE_PORTD_HOTPLUG;
891
			break;
891
			break;
892
		default:
892
		default:
893
			return true;
893
			return true;
894
		}
894
		}
895
	} else {
895
	} else {
896
		switch(port->port) {
896
		switch(port->port) {
897
		case PORT_B:
897
		case PORT_B:
898
			bit = SDE_PORTB_HOTPLUG_CPT;
898
			bit = SDE_PORTB_HOTPLUG_CPT;
899
			break;
899
			break;
900
		case PORT_C:
900
		case PORT_C:
901
			bit = SDE_PORTC_HOTPLUG_CPT;
901
			bit = SDE_PORTC_HOTPLUG_CPT;
902
			break;
902
			break;
903
		case PORT_D:
903
		case PORT_D:
904
			bit = SDE_PORTD_HOTPLUG_CPT;
904
			bit = SDE_PORTD_HOTPLUG_CPT;
905
			break;
905
			break;
906
		default:
906
		default:
907
			return true;
907
			return true;
908
		}
908
		}
909
	}
909
	}
910
 
910
 
911
	return I915_READ(SDEISR) & bit;
911
	return I915_READ(SDEISR) & bit;
912
}
912
}
913
 
913
 
914
static const char *state_string(bool enabled)
914
static const char *state_string(bool enabled)
915
{
915
{
916
	return enabled ? "on" : "off";
916
	return enabled ? "on" : "off";
917
}
917
}
918
 
918
 
919
/* Only for pre-ILK configs */
919
/* Only for pre-ILK configs */
920
void assert_pll(struct drm_i915_private *dev_priv,
920
void assert_pll(struct drm_i915_private *dev_priv,
921
		       enum pipe pipe, bool state)
921
		       enum pipe pipe, bool state)
922
{
922
{
923
	int reg;
923
	int reg;
924
	u32 val;
924
	u32 val;
925
	bool cur_state;
925
	bool cur_state;
926
 
926
 
927
	reg = DPLL(pipe);
927
	reg = DPLL(pipe);
928
	val = I915_READ(reg);
928
	val = I915_READ(reg);
929
	cur_state = !!(val & DPLL_VCO_ENABLE);
929
	cur_state = !!(val & DPLL_VCO_ENABLE);
930
	WARN(cur_state != state,
930
	WARN(cur_state != state,
931
	     "PLL state assertion failure (expected %s, current %s)\n",
931
	     "PLL state assertion failure (expected %s, current %s)\n",
932
	     state_string(state), state_string(cur_state));
932
	     state_string(state), state_string(cur_state));
933
}
933
}
934
 
934
 
935
struct intel_shared_dpll *
935
struct intel_shared_dpll *
936
intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
936
intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
937
{
937
{
938
	struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
938
	struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
939
 
939
 
940
	if (crtc->config.shared_dpll < 0)
940
	if (crtc->config.shared_dpll < 0)
941
		return NULL;
941
		return NULL;
942
 
942
 
943
	return &dev_priv->shared_dplls[crtc->config.shared_dpll];
943
	return &dev_priv->shared_dplls[crtc->config.shared_dpll];
944
}
944
}
945
 
945
 
946
/* For ILK+ */
946
/* For ILK+ */
947
void assert_shared_dpll(struct drm_i915_private *dev_priv,
947
void assert_shared_dpll(struct drm_i915_private *dev_priv,
948
			       struct intel_shared_dpll *pll,
948
			       struct intel_shared_dpll *pll,
949
			   bool state)
949
			   bool state)
950
{
950
{
951
	bool cur_state;
951
	bool cur_state;
952
	struct intel_dpll_hw_state hw_state;
952
	struct intel_dpll_hw_state hw_state;
953
 
953
 
954
	if (HAS_PCH_LPT(dev_priv->dev)) {
954
	if (HAS_PCH_LPT(dev_priv->dev)) {
955
		DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
955
		DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
956
		return;
956
		return;
957
	}
957
	}
958
 
958
 
959
	if (WARN (!pll,
959
	if (WARN (!pll,
960
		  "asserting DPLL %s with no DPLL\n", state_string(state)))
960
		  "asserting DPLL %s with no DPLL\n", state_string(state)))
961
		return;
961
		return;
962
 
962
 
963
	cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
963
	cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
964
	WARN(cur_state != state,
964
	WARN(cur_state != state,
965
	     "%s assertion failure (expected %s, current %s)\n",
965
	     "%s assertion failure (expected %s, current %s)\n",
966
	     pll->name, state_string(state), state_string(cur_state));
966
	     pll->name, state_string(state), state_string(cur_state));
967
}
967
}
968
 
968
 
969
static void assert_fdi_tx(struct drm_i915_private *dev_priv,
969
static void assert_fdi_tx(struct drm_i915_private *dev_priv,
970
			  enum pipe pipe, bool state)
970
			  enum pipe pipe, bool state)
971
{
971
{
972
	int reg;
972
	int reg;
973
	u32 val;
973
	u32 val;
974
	bool cur_state;
974
	bool cur_state;
975
	enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
975
	enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
976
								      pipe);
976
								      pipe);
977
 
977
 
978
	if (HAS_DDI(dev_priv->dev)) {
978
	if (HAS_DDI(dev_priv->dev)) {
979
		/* DDI does not have a specific FDI_TX register */
979
		/* DDI does not have a specific FDI_TX register */
980
		reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
980
		reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
981
		val = I915_READ(reg);
981
		val = I915_READ(reg);
982
		cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
982
		cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
983
	} else {
983
	} else {
984
	reg = FDI_TX_CTL(pipe);
984
	reg = FDI_TX_CTL(pipe);
985
	val = I915_READ(reg);
985
	val = I915_READ(reg);
986
	cur_state = !!(val & FDI_TX_ENABLE);
986
	cur_state = !!(val & FDI_TX_ENABLE);
987
	}
987
	}
988
	WARN(cur_state != state,
988
	WARN(cur_state != state,
989
	     "FDI TX state assertion failure (expected %s, current %s)\n",
989
	     "FDI TX state assertion failure (expected %s, current %s)\n",
990
	     state_string(state), state_string(cur_state));
990
	     state_string(state), state_string(cur_state));
991
}
991
}
992
#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
992
#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
993
#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
993
#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
994
 
994
 
995
static void assert_fdi_rx(struct drm_i915_private *dev_priv,
995
static void assert_fdi_rx(struct drm_i915_private *dev_priv,
996
			  enum pipe pipe, bool state)
996
			  enum pipe pipe, bool state)
997
{
997
{
998
	int reg;
998
	int reg;
999
	u32 val;
999
	u32 val;
1000
	bool cur_state;
1000
	bool cur_state;
1001
 
1001
 
1002
	reg = FDI_RX_CTL(pipe);
1002
	reg = FDI_RX_CTL(pipe);
1003
	val = I915_READ(reg);
1003
	val = I915_READ(reg);
1004
	cur_state = !!(val & FDI_RX_ENABLE);
1004
	cur_state = !!(val & FDI_RX_ENABLE);
1005
	WARN(cur_state != state,
1005
	WARN(cur_state != state,
1006
	     "FDI RX state assertion failure (expected %s, current %s)\n",
1006
	     "FDI RX state assertion failure (expected %s, current %s)\n",
1007
	     state_string(state), state_string(cur_state));
1007
	     state_string(state), state_string(cur_state));
1008
}
1008
}
1009
#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1009
#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1010
#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1010
#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1011
 
1011
 
1012
static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1012
static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1013
				      enum pipe pipe)
1013
				      enum pipe pipe)
1014
{
1014
{
1015
	int reg;
1015
	int reg;
1016
	u32 val;
1016
	u32 val;
1017
 
1017
 
1018
	/* ILK FDI PLL is always enabled */
1018
	/* ILK FDI PLL is always enabled */
1019
	if (dev_priv->info->gen == 5)
1019
	if (dev_priv->info->gen == 5)
1020
		return;
1020
		return;
1021
 
1021
 
1022
	/* On Haswell, DDI ports are responsible for the FDI PLL setup */
1022
	/* On Haswell, DDI ports are responsible for the FDI PLL setup */
1023
	if (HAS_DDI(dev_priv->dev))
1023
	if (HAS_DDI(dev_priv->dev))
1024
		return;
1024
		return;
1025
 
1025
 
1026
	reg = FDI_TX_CTL(pipe);
1026
	reg = FDI_TX_CTL(pipe);
1027
	val = I915_READ(reg);
1027
	val = I915_READ(reg);
1028
	WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1028
	WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1029
}
1029
}
1030
 
1030
 
1031
void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1031
void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1032
		       enum pipe pipe, bool state)
1032
		       enum pipe pipe, bool state)
1033
{
1033
{
1034
	int reg;
1034
	int reg;
1035
	u32 val;
1035
	u32 val;
1036
	bool cur_state;
1036
	bool cur_state;
1037
 
1037
 
1038
	reg = FDI_RX_CTL(pipe);
1038
	reg = FDI_RX_CTL(pipe);
1039
	val = I915_READ(reg);
1039
	val = I915_READ(reg);
1040
	cur_state = !!(val & FDI_RX_PLL_ENABLE);
1040
	cur_state = !!(val & FDI_RX_PLL_ENABLE);
1041
	WARN(cur_state != state,
1041
	WARN(cur_state != state,
1042
	     "FDI RX PLL assertion failure (expected %s, current %s)\n",
1042
	     "FDI RX PLL assertion failure (expected %s, current %s)\n",
1043
	     state_string(state), state_string(cur_state));
1043
	     state_string(state), state_string(cur_state));
1044
}
1044
}
1045
 
1045
 
1046
static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1046
static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1047
				  enum pipe pipe)
1047
				  enum pipe pipe)
1048
{
1048
{
1049
	int pp_reg, lvds_reg;
1049
	int pp_reg, lvds_reg;
1050
	u32 val;
1050
	u32 val;
1051
	enum pipe panel_pipe = PIPE_A;
1051
	enum pipe panel_pipe = PIPE_A;
1052
	bool locked = true;
1052
	bool locked = true;
1053
 
1053
 
1054
	if (HAS_PCH_SPLIT(dev_priv->dev)) {
1054
	if (HAS_PCH_SPLIT(dev_priv->dev)) {
1055
		pp_reg = PCH_PP_CONTROL;
1055
		pp_reg = PCH_PP_CONTROL;
1056
		lvds_reg = PCH_LVDS;
1056
		lvds_reg = PCH_LVDS;
1057
	} else {
1057
	} else {
1058
		pp_reg = PP_CONTROL;
1058
		pp_reg = PP_CONTROL;
1059
		lvds_reg = LVDS;
1059
		lvds_reg = LVDS;
1060
	}
1060
	}
1061
 
1061
 
1062
	val = I915_READ(pp_reg);
1062
	val = I915_READ(pp_reg);
1063
	if (!(val & PANEL_POWER_ON) ||
1063
	if (!(val & PANEL_POWER_ON) ||
1064
	    ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1064
	    ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1065
		locked = false;
1065
		locked = false;
1066
 
1066
 
1067
	if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1067
	if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1068
		panel_pipe = PIPE_B;
1068
		panel_pipe = PIPE_B;
1069
 
1069
 
1070
	WARN(panel_pipe == pipe && locked,
1070
	WARN(panel_pipe == pipe && locked,
1071
	     "panel assertion failure, pipe %c regs locked\n",
1071
	     "panel assertion failure, pipe %c regs locked\n",
1072
	     pipe_name(pipe));
1072
	     pipe_name(pipe));
1073
}
1073
}
1074
 
1074
 
1075
void assert_pipe(struct drm_i915_private *dev_priv,
1075
void assert_pipe(struct drm_i915_private *dev_priv,
1076
			enum pipe pipe, bool state)
1076
			enum pipe pipe, bool state)
1077
{
1077
{
1078
	int reg;
1078
	int reg;
1079
	u32 val;
1079
	u32 val;
1080
	bool cur_state;
1080
	bool cur_state;
1081
	enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1081
	enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1082
								      pipe);
1082
								      pipe);
1083
 
1083
 
1084
	/* if we need the pipe A quirk it must be always on */
1084
	/* if we need the pipe A quirk it must be always on */
1085
	if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1085
	if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1086
		state = true;
1086
		state = true;
1087
 
1087
 
1088
	if (!intel_display_power_enabled(dev_priv->dev,
1088
	if (!intel_display_power_enabled(dev_priv->dev,
1089
				POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
1089
				POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
1090
		cur_state = false;
1090
		cur_state = false;
1091
	} else {
1091
	} else {
1092
	reg = PIPECONF(cpu_transcoder);
1092
	reg = PIPECONF(cpu_transcoder);
1093
	val = I915_READ(reg);
1093
	val = I915_READ(reg);
1094
	cur_state = !!(val & PIPECONF_ENABLE);
1094
	cur_state = !!(val & PIPECONF_ENABLE);
1095
	}
1095
	}
1096
 
1096
 
1097
	WARN(cur_state != state,
1097
	WARN(cur_state != state,
1098
	     "pipe %c assertion failure (expected %s, current %s)\n",
1098
	     "pipe %c assertion failure (expected %s, current %s)\n",
1099
	     pipe_name(pipe), state_string(state), state_string(cur_state));
1099
	     pipe_name(pipe), state_string(state), state_string(cur_state));
1100
}
1100
}
1101
 
1101
 
1102
static void assert_plane(struct drm_i915_private *dev_priv,
1102
static void assert_plane(struct drm_i915_private *dev_priv,
1103
			 enum plane plane, bool state)
1103
			 enum plane plane, bool state)
1104
{
1104
{
1105
	int reg;
1105
	int reg;
1106
	u32 val;
1106
	u32 val;
1107
	bool cur_state;
1107
	bool cur_state;
1108
 
1108
 
1109
	reg = DSPCNTR(plane);
1109
	reg = DSPCNTR(plane);
1110
	val = I915_READ(reg);
1110
	val = I915_READ(reg);
1111
	cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1111
	cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1112
	WARN(cur_state != state,
1112
	WARN(cur_state != state,
1113
	     "plane %c assertion failure (expected %s, current %s)\n",
1113
	     "plane %c assertion failure (expected %s, current %s)\n",
1114
	     plane_name(plane), state_string(state), state_string(cur_state));
1114
	     plane_name(plane), state_string(state), state_string(cur_state));
1115
}
1115
}
1116
 
1116
 
1117
#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1117
#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1118
#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1118
#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1119
 
1119
 
1120
static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1120
static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1121
				   enum pipe pipe)
1121
				   enum pipe pipe)
1122
{
1122
{
1123
	struct drm_device *dev = dev_priv->dev;
1123
	struct drm_device *dev = dev_priv->dev;
1124
	int reg, i;
1124
	int reg, i;
1125
	u32 val;
1125
	u32 val;
1126
	int cur_pipe;
1126
	int cur_pipe;
1127
 
1127
 
1128
	/* Primary planes are fixed to pipes on gen4+ */
1128
	/* Primary planes are fixed to pipes on gen4+ */
1129
	if (INTEL_INFO(dev)->gen >= 4) {
1129
	if (INTEL_INFO(dev)->gen >= 4) {
1130
		reg = DSPCNTR(pipe);
1130
		reg = DSPCNTR(pipe);
1131
		val = I915_READ(reg);
1131
		val = I915_READ(reg);
1132
		WARN((val & DISPLAY_PLANE_ENABLE),
1132
		WARN((val & DISPLAY_PLANE_ENABLE),
1133
		     "plane %c assertion failure, should be disabled but not\n",
1133
		     "plane %c assertion failure, should be disabled but not\n",
1134
		     plane_name(pipe));
1134
		     plane_name(pipe));
1135
		return;
1135
		return;
1136
	}
1136
	}
1137
 
1137
 
1138
	/* Need to check both planes against the pipe */
1138
	/* Need to check both planes against the pipe */
1139
	for_each_pipe(i) {
1139
	for_each_pipe(i) {
1140
		reg = DSPCNTR(i);
1140
		reg = DSPCNTR(i);
1141
		val = I915_READ(reg);
1141
		val = I915_READ(reg);
1142
		cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1142
		cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1143
			DISPPLANE_SEL_PIPE_SHIFT;
1143
			DISPPLANE_SEL_PIPE_SHIFT;
1144
		WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
1144
		WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
1145
		     "plane %c assertion failure, should be off on pipe %c but is still active\n",
1145
		     "plane %c assertion failure, should be off on pipe %c but is still active\n",
1146
		     plane_name(i), pipe_name(pipe));
1146
		     plane_name(i), pipe_name(pipe));
1147
	}
1147
	}
1148
}
1148
}
1149
 
1149
 
1150
static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1150
static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1151
				    enum pipe pipe)
1151
				    enum pipe pipe)
1152
{
1152
{
1153
	struct drm_device *dev = dev_priv->dev;
1153
	struct drm_device *dev = dev_priv->dev;
1154
	int reg, i;
1154
	int reg, i;
1155
	u32 val;
1155
	u32 val;
1156
 
1156
 
1157
	if (IS_VALLEYVIEW(dev)) {
1157
	if (IS_VALLEYVIEW(dev)) {
1158
	for (i = 0; i < dev_priv->num_plane; i++) {
1158
	for (i = 0; i < dev_priv->num_plane; i++) {
1159
		reg = SPCNTR(pipe, i);
1159
		reg = SPCNTR(pipe, i);
1160
		val = I915_READ(reg);
1160
		val = I915_READ(reg);
1161
		WARN((val & SP_ENABLE),
1161
		WARN((val & SP_ENABLE),
1162
			     "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1162
			     "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1163
			     sprite_name(pipe, i), pipe_name(pipe));
1163
			     sprite_name(pipe, i), pipe_name(pipe));
1164
		}
1164
		}
1165
	} else if (INTEL_INFO(dev)->gen >= 7) {
1165
	} else if (INTEL_INFO(dev)->gen >= 7) {
1166
		reg = SPRCTL(pipe);
1166
		reg = SPRCTL(pipe);
1167
		val = I915_READ(reg);
1167
		val = I915_READ(reg);
1168
		WARN((val & SPRITE_ENABLE),
1168
		WARN((val & SPRITE_ENABLE),
1169
		     "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1169
		     "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1170
		     plane_name(pipe), pipe_name(pipe));
1170
		     plane_name(pipe), pipe_name(pipe));
1171
	} else if (INTEL_INFO(dev)->gen >= 5) {
1171
	} else if (INTEL_INFO(dev)->gen >= 5) {
1172
		reg = DVSCNTR(pipe);
1172
		reg = DVSCNTR(pipe);
1173
		val = I915_READ(reg);
1173
		val = I915_READ(reg);
1174
		WARN((val & DVS_ENABLE),
1174
		WARN((val & DVS_ENABLE),
1175
		     "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1175
		     "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1176
		     plane_name(pipe), pipe_name(pipe));
1176
		     plane_name(pipe), pipe_name(pipe));
1177
	}
1177
	}
1178
}
1178
}
1179
 
1179
 
1180
static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1180
static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1181
{
1181
{
1182
	u32 val;
1182
	u32 val;
1183
	bool enabled;
1183
	bool enabled;
1184
 
1184
 
1185
	if (HAS_PCH_LPT(dev_priv->dev)) {
1185
	if (HAS_PCH_LPT(dev_priv->dev)) {
1186
		DRM_DEBUG_DRIVER("LPT does not has PCH refclk, skipping check\n");
1186
		DRM_DEBUG_DRIVER("LPT does not has PCH refclk, skipping check\n");
1187
		return;
1187
		return;
1188
	}
1188
	}
1189
 
1189
 
1190
	val = I915_READ(PCH_DREF_CONTROL);
1190
	val = I915_READ(PCH_DREF_CONTROL);
1191
	enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1191
	enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1192
			    DREF_SUPERSPREAD_SOURCE_MASK));
1192
			    DREF_SUPERSPREAD_SOURCE_MASK));
1193
	WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1193
	WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1194
}
1194
}
1195
 
1195
 
1196
static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1196
static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1197
				       enum pipe pipe)
1197
				       enum pipe pipe)
1198
{
1198
{
1199
	int reg;
1199
	int reg;
1200
	u32 val;
1200
	u32 val;
1201
	bool enabled;
1201
	bool enabled;
1202
 
1202
 
1203
	reg = PCH_TRANSCONF(pipe);
1203
	reg = PCH_TRANSCONF(pipe);
1204
	val = I915_READ(reg);
1204
	val = I915_READ(reg);
1205
	enabled = !!(val & TRANS_ENABLE);
1205
	enabled = !!(val & TRANS_ENABLE);
1206
	WARN(enabled,
1206
	WARN(enabled,
1207
	     "transcoder assertion failed, should be off on pipe %c but is still active\n",
1207
	     "transcoder assertion failed, should be off on pipe %c but is still active\n",
1208
	     pipe_name(pipe));
1208
	     pipe_name(pipe));
1209
}
1209
}
1210
 
1210
 
1211
static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1211
static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1212
			    enum pipe pipe, u32 port_sel, u32 val)
1212
			    enum pipe pipe, u32 port_sel, u32 val)
1213
{
1213
{
1214
	if ((val & DP_PORT_EN) == 0)
1214
	if ((val & DP_PORT_EN) == 0)
1215
		return false;
1215
		return false;
1216
 
1216
 
1217
	if (HAS_PCH_CPT(dev_priv->dev)) {
1217
	if (HAS_PCH_CPT(dev_priv->dev)) {
1218
		u32	trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1218
		u32	trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1219
		u32	trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1219
		u32	trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1220
		if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1220
		if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1221
			return false;
1221
			return false;
1222
	} else {
1222
	} else {
1223
		if ((val & DP_PIPE_MASK) != (pipe << 30))
1223
		if ((val & DP_PIPE_MASK) != (pipe << 30))
1224
			return false;
1224
			return false;
1225
	}
1225
	}
1226
	return true;
1226
	return true;
1227
}
1227
}
1228
 
1228
 
1229
static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1229
static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1230
			      enum pipe pipe, u32 val)
1230
			      enum pipe pipe, u32 val)
1231
{
1231
{
1232
	if ((val & SDVO_ENABLE) == 0)
1232
	if ((val & SDVO_ENABLE) == 0)
1233
		return false;
1233
		return false;
1234
 
1234
 
1235
	if (HAS_PCH_CPT(dev_priv->dev)) {
1235
	if (HAS_PCH_CPT(dev_priv->dev)) {
1236
		if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1236
		if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1237
			return false;
1237
			return false;
1238
	} else {
1238
	} else {
1239
		if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1239
		if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1240
			return false;
1240
			return false;
1241
	}
1241
	}
1242
	return true;
1242
	return true;
1243
}
1243
}
1244
 
1244
 
1245
static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1245
static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1246
			      enum pipe pipe, u32 val)
1246
			      enum pipe pipe, u32 val)
1247
{
1247
{
1248
	if ((val & LVDS_PORT_EN) == 0)
1248
	if ((val & LVDS_PORT_EN) == 0)
1249
		return false;
1249
		return false;
1250
 
1250
 
1251
	if (HAS_PCH_CPT(dev_priv->dev)) {
1251
	if (HAS_PCH_CPT(dev_priv->dev)) {
1252
		if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1252
		if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1253
			return false;
1253
			return false;
1254
	} else {
1254
	} else {
1255
		if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1255
		if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1256
			return false;
1256
			return false;
1257
	}
1257
	}
1258
	return true;
1258
	return true;
1259
}
1259
}
1260
 
1260
 
1261
static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1261
static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1262
			      enum pipe pipe, u32 val)
1262
			      enum pipe pipe, u32 val)
1263
{
1263
{
1264
	if ((val & ADPA_DAC_ENABLE) == 0)
1264
	if ((val & ADPA_DAC_ENABLE) == 0)
1265
		return false;
1265
		return false;
1266
	if (HAS_PCH_CPT(dev_priv->dev)) {
1266
	if (HAS_PCH_CPT(dev_priv->dev)) {
1267
		if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1267
		if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1268
			return false;
1268
			return false;
1269
	} else {
1269
	} else {
1270
		if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1270
		if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1271
			return false;
1271
			return false;
1272
	}
1272
	}
1273
	return true;
1273
	return true;
1274
}
1274
}
1275
 
1275
 
1276
static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
1276
static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
1277
				   enum pipe pipe, int reg, u32 port_sel)
1277
				   enum pipe pipe, int reg, u32 port_sel)
1278
{
1278
{
1279
	u32 val = I915_READ(reg);
1279
	u32 val = I915_READ(reg);
1280
	WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
1280
	WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
1281
	     "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
1281
	     "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
1282
	     reg, pipe_name(pipe));
1282
	     reg, pipe_name(pipe));
1283
 
1283
 
1284
	WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1284
	WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1285
	     && (val & DP_PIPEB_SELECT),
1285
	     && (val & DP_PIPEB_SELECT),
1286
	     "IBX PCH dp port still using transcoder B\n");
1286
	     "IBX PCH dp port still using transcoder B\n");
1287
}
1287
}
1288
 
1288
 
1289
static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1289
static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1290
				     enum pipe pipe, int reg)
1290
				     enum pipe pipe, int reg)
1291
{
1291
{
1292
	u32 val = I915_READ(reg);
1292
	u32 val = I915_READ(reg);
1293
	WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
1293
	WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
1294
	     "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
1294
	     "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
1295
	     reg, pipe_name(pipe));
1295
	     reg, pipe_name(pipe));
1296
 
1296
 
1297
	WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
1297
	WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
1298
	     && (val & SDVO_PIPE_B_SELECT),
1298
	     && (val & SDVO_PIPE_B_SELECT),
1299
	     "IBX PCH hdmi port still using transcoder B\n");
1299
	     "IBX PCH hdmi port still using transcoder B\n");
1300
}
1300
}
1301
 
1301
 
1302
static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1302
static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1303
				      enum pipe pipe)
1303
				      enum pipe pipe)
1304
{
1304
{
1305
	int reg;
1305
	int reg;
1306
	u32 val;
1306
	u32 val;
1307
 
1307
 
1308
	assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1308
	assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1309
	assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1309
	assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1310
	assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
1310
	assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
1311
 
1311
 
1312
	reg = PCH_ADPA;
1312
	reg = PCH_ADPA;
1313
	val = I915_READ(reg);
1313
	val = I915_READ(reg);
1314
	WARN(adpa_pipe_enabled(dev_priv, pipe, val),
1314
	WARN(adpa_pipe_enabled(dev_priv, pipe, val),
1315
	     "PCH VGA enabled on transcoder %c, should be disabled\n",
1315
	     "PCH VGA enabled on transcoder %c, should be disabled\n",
1316
	     pipe_name(pipe));
1316
	     pipe_name(pipe));
1317
 
1317
 
1318
	reg = PCH_LVDS;
1318
	reg = PCH_LVDS;
1319
	val = I915_READ(reg);
1319
	val = I915_READ(reg);
1320
	WARN(lvds_pipe_enabled(dev_priv, pipe, val),
1320
	WARN(lvds_pipe_enabled(dev_priv, pipe, val),
1321
	     "PCH LVDS enabled on transcoder %c, should be disabled\n",
1321
	     "PCH LVDS enabled on transcoder %c, should be disabled\n",
1322
	     pipe_name(pipe));
1322
	     pipe_name(pipe));
1323
 
1323
 
1324
	assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1324
	assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1325
	assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1325
	assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1326
	assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
1326
	assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
1327
}
1327
}
1328
 
1328
 
1329
static void vlv_enable_pll(struct intel_crtc *crtc)
1329
static void vlv_enable_pll(struct intel_crtc *crtc)
1330
{
1330
{
1331
	struct drm_device *dev = crtc->base.dev;
1331
	struct drm_device *dev = crtc->base.dev;
1332
	struct drm_i915_private *dev_priv = dev->dev_private;
1332
	struct drm_i915_private *dev_priv = dev->dev_private;
1333
	int reg = DPLL(crtc->pipe);
1333
	int reg = DPLL(crtc->pipe);
1334
	u32 dpll = crtc->config.dpll_hw_state.dpll;
1334
	u32 dpll = crtc->config.dpll_hw_state.dpll;
1335
 
1335
 
1336
	assert_pipe_disabled(dev_priv, crtc->pipe);
1336
	assert_pipe_disabled(dev_priv, crtc->pipe);
1337
 
1337
 
1338
    /* No really, not for ILK+ */
1338
    /* No really, not for ILK+ */
1339
	BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1339
	BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1340
 
1340
 
1341
    /* PLL is protected by panel, make sure we can write it */
1341
    /* PLL is protected by panel, make sure we can write it */
1342
    if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
1342
    if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
1343
		assert_panel_unlocked(dev_priv, crtc->pipe);
1343
		assert_panel_unlocked(dev_priv, crtc->pipe);
1344
 
1344
 
1345
	I915_WRITE(reg, dpll);
1345
	I915_WRITE(reg, dpll);
1346
	POSTING_READ(reg);
1346
	POSTING_READ(reg);
1347
	udelay(150);
1347
	udelay(150);
1348
 
1348
 
1349
	if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1349
	if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1350
		DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1350
		DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1351
 
1351
 
1352
	I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
1352
	I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
1353
	POSTING_READ(DPLL_MD(crtc->pipe));
1353
	POSTING_READ(DPLL_MD(crtc->pipe));
1354
 
1354
 
1355
	/* We do this three times for luck */
1355
	/* We do this three times for luck */
1356
	I915_WRITE(reg, dpll);
1356
	I915_WRITE(reg, dpll);
1357
	POSTING_READ(reg);
1357
	POSTING_READ(reg);
1358
	udelay(150); /* wait for warmup */
1358
	udelay(150); /* wait for warmup */
1359
	I915_WRITE(reg, dpll);
1359
	I915_WRITE(reg, dpll);
1360
	POSTING_READ(reg);
1360
	POSTING_READ(reg);
1361
	udelay(150); /* wait for warmup */
1361
	udelay(150); /* wait for warmup */
1362
	I915_WRITE(reg, dpll);
1362
	I915_WRITE(reg, dpll);
1363
	POSTING_READ(reg);
1363
	POSTING_READ(reg);
1364
	udelay(150); /* wait for warmup */
1364
	udelay(150); /* wait for warmup */
1365
}
1365
}
1366
 
1366
 
1367
static void i9xx_enable_pll(struct intel_crtc *crtc)
1367
static void i9xx_enable_pll(struct intel_crtc *crtc)
1368
{
1368
{
1369
	struct drm_device *dev = crtc->base.dev;
1369
	struct drm_device *dev = crtc->base.dev;
1370
	struct drm_i915_private *dev_priv = dev->dev_private;
1370
	struct drm_i915_private *dev_priv = dev->dev_private;
1371
	int reg = DPLL(crtc->pipe);
1371
	int reg = DPLL(crtc->pipe);
1372
	u32 dpll = crtc->config.dpll_hw_state.dpll;
1372
	u32 dpll = crtc->config.dpll_hw_state.dpll;
1373
 
1373
 
1374
	assert_pipe_disabled(dev_priv, crtc->pipe);
1374
	assert_pipe_disabled(dev_priv, crtc->pipe);
1375
 
1375
 
1376
	/* No really, not for ILK+ */
1376
	/* No really, not for ILK+ */
1377
	BUG_ON(dev_priv->info->gen >= 5);
1377
	BUG_ON(dev_priv->info->gen >= 5);
1378
 
1378
 
1379
	/* PLL is protected by panel, make sure we can write it */
1379
	/* PLL is protected by panel, make sure we can write it */
1380
	if (IS_MOBILE(dev) && !IS_I830(dev))
1380
	if (IS_MOBILE(dev) && !IS_I830(dev))
1381
		assert_panel_unlocked(dev_priv, crtc->pipe);
1381
		assert_panel_unlocked(dev_priv, crtc->pipe);
1382
 
1382
 
1383
	I915_WRITE(reg, dpll);
1383
	I915_WRITE(reg, dpll);
1384
 
1384
 
1385
	/* Wait for the clocks to stabilize. */
1385
	/* Wait for the clocks to stabilize. */
1386
	POSTING_READ(reg);
1386
	POSTING_READ(reg);
1387
	udelay(150);
1387
	udelay(150);
1388
 
1388
 
1389
	if (INTEL_INFO(dev)->gen >= 4) {
1389
	if (INTEL_INFO(dev)->gen >= 4) {
1390
		I915_WRITE(DPLL_MD(crtc->pipe),
1390
		I915_WRITE(DPLL_MD(crtc->pipe),
1391
			   crtc->config.dpll_hw_state.dpll_md);
1391
			   crtc->config.dpll_hw_state.dpll_md);
1392
	} else {
1392
	} else {
1393
		/* The pixel multiplier can only be updated once the
1393
		/* The pixel multiplier can only be updated once the
1394
		 * DPLL is enabled and the clocks are stable.
1394
		 * DPLL is enabled and the clocks are stable.
1395
		 *
1395
		 *
1396
		 * So write it again.
1396
		 * So write it again.
1397
		 */
1397
		 */
1398
		I915_WRITE(reg, dpll);
1398
		I915_WRITE(reg, dpll);
1399
	}
1399
	}
1400
 
1400
 
1401
    /* We do this three times for luck */
1401
    /* We do this three times for luck */
1402
	I915_WRITE(reg, dpll);
1402
	I915_WRITE(reg, dpll);
1403
    POSTING_READ(reg);
1403
    POSTING_READ(reg);
1404
    udelay(150); /* wait for warmup */
1404
    udelay(150); /* wait for warmup */
1405
	I915_WRITE(reg, dpll);
1405
	I915_WRITE(reg, dpll);
1406
    POSTING_READ(reg);
1406
    POSTING_READ(reg);
1407
    udelay(150); /* wait for warmup */
1407
    udelay(150); /* wait for warmup */
1408
	I915_WRITE(reg, dpll);
1408
	I915_WRITE(reg, dpll);
1409
    POSTING_READ(reg);
1409
    POSTING_READ(reg);
1410
    udelay(150); /* wait for warmup */
1410
    udelay(150); /* wait for warmup */
1411
}
1411
}
1412
 
1412
 
1413
/**
1413
/**
1414
 * i9xx_disable_pll - disable a PLL
1414
 * i9xx_disable_pll - disable a PLL
1415
 * @dev_priv: i915 private structure
1415
 * @dev_priv: i915 private structure
1416
 * @pipe: pipe PLL to disable
1416
 * @pipe: pipe PLL to disable
1417
 *
1417
 *
1418
 * Disable the PLL for @pipe, making sure the pipe is off first.
1418
 * Disable the PLL for @pipe, making sure the pipe is off first.
1419
 *
1419
 *
1420
 * Note!  This is for pre-ILK only.
1420
 * Note!  This is for pre-ILK only.
1421
 */
1421
 */
1422
static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1422
static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1423
{
1423
{
1424
	/* Don't disable pipe A or pipe A PLLs if needed */
1424
	/* Don't disable pipe A or pipe A PLLs if needed */
1425
	if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1425
	if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1426
		return;
1426
		return;
1427
 
1427
 
1428
	/* Make sure the pipe isn't still relying on us */
1428
	/* Make sure the pipe isn't still relying on us */
1429
	assert_pipe_disabled(dev_priv, pipe);
1429
	assert_pipe_disabled(dev_priv, pipe);
1430
 
1430
 
1431
	I915_WRITE(DPLL(pipe), 0);
1431
	I915_WRITE(DPLL(pipe), 0);
1432
	POSTING_READ(DPLL(pipe));
1432
	POSTING_READ(DPLL(pipe));
1433
}
1433
}
1434
 
1434
 
1435
void vlv_wait_port_ready(struct drm_i915_private *dev_priv, int port)
1435
void vlv_wait_port_ready(struct drm_i915_private *dev_priv, int port)
1436
{
1436
{
1437
	u32 port_mask;
1437
	u32 port_mask;
1438
 
1438
 
1439
	if (!port)
1439
	if (!port)
1440
		port_mask = DPLL_PORTB_READY_MASK;
1440
		port_mask = DPLL_PORTB_READY_MASK;
1441
	else
1441
	else
1442
		port_mask = DPLL_PORTC_READY_MASK;
1442
		port_mask = DPLL_PORTC_READY_MASK;
1443
 
1443
 
1444
	if (wait_for((I915_READ(DPLL(0)) & port_mask) == 0, 1000))
1444
	if (wait_for((I915_READ(DPLL(0)) & port_mask) == 0, 1000))
1445
		WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
1445
		WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
1446
		     'B' + port, I915_READ(DPLL(0)));
1446
		     'B' + port, I915_READ(DPLL(0)));
1447
}
1447
}
1448
 
1448
 
1449
/**
1449
/**
1450
 * ironlake_enable_shared_dpll - enable PCH PLL
1450
 * ironlake_enable_shared_dpll - enable PCH PLL
1451
 * @dev_priv: i915 private structure
1451
 * @dev_priv: i915 private structure
1452
 * @pipe: pipe PLL to enable
1452
 * @pipe: pipe PLL to enable
1453
 *
1453
 *
1454
 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1454
 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1455
 * drives the transcoder clock.
1455
 * drives the transcoder clock.
1456
 */
1456
 */
1457
static void ironlake_enable_shared_dpll(struct intel_crtc *crtc)
1457
static void ironlake_enable_shared_dpll(struct intel_crtc *crtc)
1458
{
1458
{
1459
	struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1459
	struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1460
	struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1460
	struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1461
 
1461
 
1462
	/* PCH PLLs only available on ILK, SNB and IVB */
1462
	/* PCH PLLs only available on ILK, SNB and IVB */
1463
	BUG_ON(dev_priv->info->gen < 5);
1463
	BUG_ON(dev_priv->info->gen < 5);
1464
	if (WARN_ON(pll == NULL))
1464
	if (WARN_ON(pll == NULL))
1465
		return;
1465
		return;
1466
 
1466
 
1467
	if (WARN_ON(pll->refcount == 0))
1467
	if (WARN_ON(pll->refcount == 0))
1468
		return;
1468
		return;
1469
 
1469
 
1470
	DRM_DEBUG_KMS("enable %s (active %d, on? %d)for crtc %d\n",
1470
	DRM_DEBUG_KMS("enable %s (active %d, on? %d)for crtc %d\n",
1471
		      pll->name, pll->active, pll->on,
1471
		      pll->name, pll->active, pll->on,
1472
		      crtc->base.base.id);
1472
		      crtc->base.base.id);
1473
 
1473
 
1474
	if (pll->active++) {
1474
	if (pll->active++) {
1475
		WARN_ON(!pll->on);
1475
		WARN_ON(!pll->on);
1476
		assert_shared_dpll_enabled(dev_priv, pll);
1476
		assert_shared_dpll_enabled(dev_priv, pll);
1477
		return;
1477
		return;
1478
	}
1478
	}
1479
	WARN_ON(pll->on);
1479
	WARN_ON(pll->on);
1480
 
1480
 
1481
	DRM_DEBUG_KMS("enabling %s\n", pll->name);
1481
	DRM_DEBUG_KMS("enabling %s\n", pll->name);
1482
	pll->enable(dev_priv, pll);
1482
	pll->enable(dev_priv, pll);
1483
	pll->on = true;
1483
	pll->on = true;
1484
}
1484
}
1485
 
1485
 
1486
static void intel_disable_shared_dpll(struct intel_crtc *crtc)
1486
static void intel_disable_shared_dpll(struct intel_crtc *crtc)
1487
{
1487
{
1488
	struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1488
	struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1489
	struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1489
	struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1490
 
1490
 
1491
	/* PCH only available on ILK+ */
1491
	/* PCH only available on ILK+ */
1492
	BUG_ON(dev_priv->info->gen < 5);
1492
	BUG_ON(dev_priv->info->gen < 5);
1493
	if (WARN_ON(pll == NULL))
1493
	if (WARN_ON(pll == NULL))
1494
	       return;
1494
	       return;
1495
 
1495
 
1496
	if (WARN_ON(pll->refcount == 0))
1496
	if (WARN_ON(pll->refcount == 0))
1497
		return;
1497
		return;
1498
 
1498
 
1499
	DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1499
	DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1500
		      pll->name, pll->active, pll->on,
1500
		      pll->name, pll->active, pll->on,
1501
		      crtc->base.base.id);
1501
		      crtc->base.base.id);
1502
 
1502
 
1503
	if (WARN_ON(pll->active == 0)) {
1503
	if (WARN_ON(pll->active == 0)) {
1504
		assert_shared_dpll_disabled(dev_priv, pll);
1504
		assert_shared_dpll_disabled(dev_priv, pll);
1505
		return;
1505
		return;
1506
	}
1506
	}
1507
 
1507
 
1508
	assert_shared_dpll_enabled(dev_priv, pll);
1508
	assert_shared_dpll_enabled(dev_priv, pll);
1509
	WARN_ON(!pll->on);
1509
	WARN_ON(!pll->on);
1510
	if (--pll->active)
1510
	if (--pll->active)
1511
		return;
1511
		return;
1512
 
1512
 
1513
	DRM_DEBUG_KMS("disabling %s\n", pll->name);
1513
	DRM_DEBUG_KMS("disabling %s\n", pll->name);
1514
	pll->disable(dev_priv, pll);
1514
	pll->disable(dev_priv, pll);
1515
	pll->on = false;
1515
	pll->on = false;
1516
}
1516
}
1517
 
1517
 
1518
static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1518
static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1519
				    enum pipe pipe)
1519
				    enum pipe pipe)
1520
{
1520
{
1521
	struct drm_device *dev = dev_priv->dev;
1521
	struct drm_device *dev = dev_priv->dev;
1522
	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1522
	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1523
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1523
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1524
	uint32_t reg, val, pipeconf_val;
1524
	uint32_t reg, val, pipeconf_val;
1525
 
1525
 
1526
	/* PCH only available on ILK+ */
1526
	/* PCH only available on ILK+ */
1527
	BUG_ON(dev_priv->info->gen < 5);
1527
	BUG_ON(dev_priv->info->gen < 5);
1528
 
1528
 
1529
	/* Make sure PCH DPLL is enabled */
1529
	/* Make sure PCH DPLL is enabled */
1530
	assert_shared_dpll_enabled(dev_priv,
1530
	assert_shared_dpll_enabled(dev_priv,
1531
				   intel_crtc_to_shared_dpll(intel_crtc));
1531
				   intel_crtc_to_shared_dpll(intel_crtc));
1532
 
1532
 
1533
	/* FDI must be feeding us bits for PCH ports */
1533
	/* FDI must be feeding us bits for PCH ports */
1534
	assert_fdi_tx_enabled(dev_priv, pipe);
1534
	assert_fdi_tx_enabled(dev_priv, pipe);
1535
	assert_fdi_rx_enabled(dev_priv, pipe);
1535
	assert_fdi_rx_enabled(dev_priv, pipe);
1536
 
1536
 
1537
	if (HAS_PCH_CPT(dev)) {
1537
	if (HAS_PCH_CPT(dev)) {
1538
		/* Workaround: Set the timing override bit before enabling the
1538
		/* Workaround: Set the timing override bit before enabling the
1539
		 * pch transcoder. */
1539
		 * pch transcoder. */
1540
		reg = TRANS_CHICKEN2(pipe);
1540
		reg = TRANS_CHICKEN2(pipe);
1541
		val = I915_READ(reg);
1541
		val = I915_READ(reg);
1542
		val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1542
		val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1543
		I915_WRITE(reg, val);
1543
		I915_WRITE(reg, val);
1544
	}
1544
	}
1545
 
1545
 
1546
	reg = PCH_TRANSCONF(pipe);
1546
	reg = PCH_TRANSCONF(pipe);
1547
	val = I915_READ(reg);
1547
	val = I915_READ(reg);
1548
	pipeconf_val = I915_READ(PIPECONF(pipe));
1548
	pipeconf_val = I915_READ(PIPECONF(pipe));
1549
 
1549
 
1550
	if (HAS_PCH_IBX(dev_priv->dev)) {
1550
	if (HAS_PCH_IBX(dev_priv->dev)) {
1551
		/*
1551
		/*
1552
		 * make the BPC in transcoder be consistent with
1552
		 * make the BPC in transcoder be consistent with
1553
		 * that in pipeconf reg.
1553
		 * that in pipeconf reg.
1554
		 */
1554
		 */
1555
		val &= ~PIPECONF_BPC_MASK;
1555
		val &= ~PIPECONF_BPC_MASK;
1556
		val |= pipeconf_val & PIPECONF_BPC_MASK;
1556
		val |= pipeconf_val & PIPECONF_BPC_MASK;
1557
	}
1557
	}
1558
 
1558
 
1559
	val &= ~TRANS_INTERLACE_MASK;
1559
	val &= ~TRANS_INTERLACE_MASK;
1560
	if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
1560
	if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
1561
		if (HAS_PCH_IBX(dev_priv->dev) &&
1561
		if (HAS_PCH_IBX(dev_priv->dev) &&
1562
		    intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1562
		    intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1563
			val |= TRANS_LEGACY_INTERLACED_ILK;
1563
			val |= TRANS_LEGACY_INTERLACED_ILK;
1564
		else
1564
		else
1565
			val |= TRANS_INTERLACED;
1565
			val |= TRANS_INTERLACED;
1566
	else
1566
	else
1567
		val |= TRANS_PROGRESSIVE;
1567
		val |= TRANS_PROGRESSIVE;
1568
 
1568
 
1569
	I915_WRITE(reg, val | TRANS_ENABLE);
1569
	I915_WRITE(reg, val | TRANS_ENABLE);
1570
	if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
1570
	if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
1571
		DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
1571
		DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
1572
}
1572
}
1573
 
1573
 
1574
static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1574
static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1575
				      enum transcoder cpu_transcoder)
1575
				      enum transcoder cpu_transcoder)
1576
{
1576
{
1577
	u32 val, pipeconf_val;
1577
	u32 val, pipeconf_val;
1578
 
1578
 
1579
	/* PCH only available on ILK+ */
1579
	/* PCH only available on ILK+ */
1580
	BUG_ON(dev_priv->info->gen < 5);
1580
	BUG_ON(dev_priv->info->gen < 5);
1581
 
1581
 
1582
	/* FDI must be feeding us bits for PCH ports */
1582
	/* FDI must be feeding us bits for PCH ports */
1583
	assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
1583
	assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
1584
	assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
1584
	assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
1585
 
1585
 
1586
	/* Workaround: set timing override bit. */
1586
	/* Workaround: set timing override bit. */
1587
	val = I915_READ(_TRANSA_CHICKEN2);
1587
	val = I915_READ(_TRANSA_CHICKEN2);
1588
	val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1588
	val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1589
	I915_WRITE(_TRANSA_CHICKEN2, val);
1589
	I915_WRITE(_TRANSA_CHICKEN2, val);
1590
 
1590
 
1591
	val = TRANS_ENABLE;
1591
	val = TRANS_ENABLE;
1592
	pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
1592
	pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
1593
 
1593
 
1594
	if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1594
	if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1595
	    PIPECONF_INTERLACED_ILK)
1595
	    PIPECONF_INTERLACED_ILK)
1596
		val |= TRANS_INTERLACED;
1596
		val |= TRANS_INTERLACED;
1597
	else
1597
	else
1598
		val |= TRANS_PROGRESSIVE;
1598
		val |= TRANS_PROGRESSIVE;
1599
 
1599
 
1600
	I915_WRITE(LPT_TRANSCONF, val);
1600
	I915_WRITE(LPT_TRANSCONF, val);
1601
	if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
1601
	if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
1602
		DRM_ERROR("Failed to enable PCH transcoder\n");
1602
		DRM_ERROR("Failed to enable PCH transcoder\n");
1603
}
1603
}
1604
 
1604
 
1605
static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1605
static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1606
				     enum pipe pipe)
1606
				     enum pipe pipe)
1607
{
1607
{
1608
	struct drm_device *dev = dev_priv->dev;
1608
	struct drm_device *dev = dev_priv->dev;
1609
	uint32_t reg, val;
1609
	uint32_t reg, val;
1610
 
1610
 
1611
	/* FDI relies on the transcoder */
1611
	/* FDI relies on the transcoder */
1612
	assert_fdi_tx_disabled(dev_priv, pipe);
1612
	assert_fdi_tx_disabled(dev_priv, pipe);
1613
	assert_fdi_rx_disabled(dev_priv, pipe);
1613
	assert_fdi_rx_disabled(dev_priv, pipe);
1614
 
1614
 
1615
	/* Ports must be off as well */
1615
	/* Ports must be off as well */
1616
	assert_pch_ports_disabled(dev_priv, pipe);
1616
	assert_pch_ports_disabled(dev_priv, pipe);
1617
 
1617
 
1618
	reg = PCH_TRANSCONF(pipe);
1618
	reg = PCH_TRANSCONF(pipe);
1619
	val = I915_READ(reg);
1619
	val = I915_READ(reg);
1620
	val &= ~TRANS_ENABLE;
1620
	val &= ~TRANS_ENABLE;
1621
	I915_WRITE(reg, val);
1621
	I915_WRITE(reg, val);
1622
	/* wait for PCH transcoder off, transcoder state */
1622
	/* wait for PCH transcoder off, transcoder state */
1623
	if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
1623
	if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
1624
		DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
1624
		DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
1625
 
1625
 
1626
	if (!HAS_PCH_IBX(dev)) {
1626
	if (!HAS_PCH_IBX(dev)) {
1627
		/* Workaround: Clear the timing override chicken bit again. */
1627
		/* Workaround: Clear the timing override chicken bit again. */
1628
		reg = TRANS_CHICKEN2(pipe);
1628
		reg = TRANS_CHICKEN2(pipe);
1629
		val = I915_READ(reg);
1629
		val = I915_READ(reg);
1630
		val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1630
		val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1631
		I915_WRITE(reg, val);
1631
		I915_WRITE(reg, val);
1632
	}
1632
	}
1633
}
1633
}
1634
 
1634
 
1635
static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
1635
static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
1636
{
1636
{
1637
	u32 val;
1637
	u32 val;
1638
 
1638
 
1639
	val = I915_READ(LPT_TRANSCONF);
1639
	val = I915_READ(LPT_TRANSCONF);
1640
	val &= ~TRANS_ENABLE;
1640
	val &= ~TRANS_ENABLE;
1641
	I915_WRITE(LPT_TRANSCONF, val);
1641
	I915_WRITE(LPT_TRANSCONF, val);
1642
	/* wait for PCH transcoder off, transcoder state */
1642
	/* wait for PCH transcoder off, transcoder state */
1643
	if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
1643
	if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
1644
		DRM_ERROR("Failed to disable PCH transcoder\n");
1644
		DRM_ERROR("Failed to disable PCH transcoder\n");
1645
 
1645
 
1646
	/* Workaround: clear timing override bit. */
1646
	/* Workaround: clear timing override bit. */
1647
	val = I915_READ(_TRANSA_CHICKEN2);
1647
	val = I915_READ(_TRANSA_CHICKEN2);
1648
	val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1648
	val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1649
	I915_WRITE(_TRANSA_CHICKEN2, val);
1649
	I915_WRITE(_TRANSA_CHICKEN2, val);
1650
}
1650
}
1651
 
1651
 
1652
/**
1652
/**
1653
 * intel_enable_pipe - enable a pipe, asserting requirements
1653
 * intel_enable_pipe - enable a pipe, asserting requirements
1654
 * @dev_priv: i915 private structure
1654
 * @dev_priv: i915 private structure
1655
 * @pipe: pipe to enable
1655
 * @pipe: pipe to enable
1656
 * @pch_port: on ILK+, is this pipe driving a PCH port or not
1656
 * @pch_port: on ILK+, is this pipe driving a PCH port or not
1657
 *
1657
 *
1658
 * Enable @pipe, making sure that various hardware specific requirements
1658
 * Enable @pipe, making sure that various hardware specific requirements
1659
 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1659
 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1660
 *
1660
 *
1661
 * @pipe should be %PIPE_A or %PIPE_B.
1661
 * @pipe should be %PIPE_A or %PIPE_B.
1662
 *
1662
 *
1663
 * Will wait until the pipe is actually running (i.e. first vblank) before
1663
 * Will wait until the pipe is actually running (i.e. first vblank) before
1664
 * returning.
1664
 * returning.
1665
 */
1665
 */
1666
static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
1666
static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
1667
			      bool pch_port)
1667
			      bool pch_port)
1668
{
1668
{
1669
	enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1669
	enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1670
								      pipe);
1670
								      pipe);
1671
	enum pipe pch_transcoder;
1671
	enum pipe pch_transcoder;
1672
	int reg;
1672
	int reg;
1673
	u32 val;
1673
	u32 val;
1674
 
1674
 
1675
	assert_planes_disabled(dev_priv, pipe);
1675
	assert_planes_disabled(dev_priv, pipe);
1676
	assert_sprites_disabled(dev_priv, pipe);
1676
	assert_sprites_disabled(dev_priv, pipe);
1677
 
1677
 
1678
	if (HAS_PCH_LPT(dev_priv->dev))
1678
	if (HAS_PCH_LPT(dev_priv->dev))
1679
		pch_transcoder = TRANSCODER_A;
1679
		pch_transcoder = TRANSCODER_A;
1680
	else
1680
	else
1681
		pch_transcoder = pipe;
1681
		pch_transcoder = pipe;
1682
 
1682
 
1683
	/*
1683
	/*
1684
	 * A pipe without a PLL won't actually be able to drive bits from
1684
	 * A pipe without a PLL won't actually be able to drive bits from
1685
	 * a plane.  On ILK+ the pipe PLLs are integrated, so we don't
1685
	 * a plane.  On ILK+ the pipe PLLs are integrated, so we don't
1686
	 * need the check.
1686
	 * need the check.
1687
	 */
1687
	 */
1688
	if (!HAS_PCH_SPLIT(dev_priv->dev))
1688
	if (!HAS_PCH_SPLIT(dev_priv->dev))
1689
		assert_pll_enabled(dev_priv, pipe);
1689
		assert_pll_enabled(dev_priv, pipe);
1690
	else {
1690
	else {
1691
		if (pch_port) {
1691
		if (pch_port) {
1692
			/* if driving the PCH, we need FDI enabled */
1692
			/* if driving the PCH, we need FDI enabled */
1693
			assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1693
			assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1694
			assert_fdi_tx_pll_enabled(dev_priv,
1694
			assert_fdi_tx_pll_enabled(dev_priv,
1695
						  (enum pipe) cpu_transcoder);
1695
						  (enum pipe) cpu_transcoder);
1696
		}
1696
		}
1697
		/* FIXME: assert CPU port conditions for SNB+ */
1697
		/* FIXME: assert CPU port conditions for SNB+ */
1698
	}
1698
	}
1699
 
1699
 
1700
	reg = PIPECONF(cpu_transcoder);
1700
	reg = PIPECONF(cpu_transcoder);
1701
	val = I915_READ(reg);
1701
	val = I915_READ(reg);
1702
	if (val & PIPECONF_ENABLE)
1702
	if (val & PIPECONF_ENABLE)
1703
		return;
1703
		return;
1704
 
1704
 
1705
	I915_WRITE(reg, val | PIPECONF_ENABLE);
1705
	I915_WRITE(reg, val | PIPECONF_ENABLE);
1706
	intel_wait_for_vblank(dev_priv->dev, pipe);
1706
	intel_wait_for_vblank(dev_priv->dev, pipe);
1707
}
1707
}
1708
 
1708
 
1709
/**
1709
/**
1710
 * intel_disable_pipe - disable a pipe, asserting requirements
1710
 * intel_disable_pipe - disable a pipe, asserting requirements
1711
 * @dev_priv: i915 private structure
1711
 * @dev_priv: i915 private structure
1712
 * @pipe: pipe to disable
1712
 * @pipe: pipe to disable
1713
 *
1713
 *
1714
 * Disable @pipe, making sure that various hardware specific requirements
1714
 * Disable @pipe, making sure that various hardware specific requirements
1715
 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1715
 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1716
 *
1716
 *
1717
 * @pipe should be %PIPE_A or %PIPE_B.
1717
 * @pipe should be %PIPE_A or %PIPE_B.
1718
 *
1718
 *
1719
 * Will wait until the pipe has shut down before returning.
1719
 * Will wait until the pipe has shut down before returning.
1720
 */
1720
 */
1721
static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1721
static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1722
			       enum pipe pipe)
1722
			       enum pipe pipe)
1723
{
1723
{
1724
	enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1724
	enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1725
								      pipe);
1725
								      pipe);
1726
	int reg;
1726
	int reg;
1727
	u32 val;
1727
	u32 val;
1728
 
1728
 
1729
    /*
1729
    /*
1730
	 * Make sure planes won't keep trying to pump pixels to us,
1730
	 * Make sure planes won't keep trying to pump pixels to us,
1731
	 * or we might hang the display.
1731
	 * or we might hang the display.
1732
	 */
1732
	 */
1733
	assert_planes_disabled(dev_priv, pipe);
1733
	assert_planes_disabled(dev_priv, pipe);
1734
	assert_sprites_disabled(dev_priv, pipe);
1734
	assert_sprites_disabled(dev_priv, pipe);
1735
 
1735
 
1736
	/* Don't disable pipe A or pipe A PLLs if needed */
1736
	/* Don't disable pipe A or pipe A PLLs if needed */
1737
	if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1737
	if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1738
		return;
1738
		return;
1739
 
1739
 
1740
	reg = PIPECONF(cpu_transcoder);
1740
	reg = PIPECONF(cpu_transcoder);
1741
	val = I915_READ(reg);
1741
	val = I915_READ(reg);
1742
	if ((val & PIPECONF_ENABLE) == 0)
1742
	if ((val & PIPECONF_ENABLE) == 0)
1743
		return;
1743
		return;
1744
 
1744
 
1745
	I915_WRITE(reg, val & ~PIPECONF_ENABLE);
1745
	I915_WRITE(reg, val & ~PIPECONF_ENABLE);
1746
	intel_wait_for_pipe_off(dev_priv->dev, pipe);
1746
	intel_wait_for_pipe_off(dev_priv->dev, pipe);
1747
}
1747
}
1748
 
1748
 
1749
/*
1749
/*
1750
 * Plane regs are double buffered, going from enabled->disabled needs a
1750
 * Plane regs are double buffered, going from enabled->disabled needs a
1751
 * trigger in order to latch.  The display address reg provides this.
1751
 * trigger in order to latch.  The display address reg provides this.
1752
 */
1752
 */
1753
void intel_flush_display_plane(struct drm_i915_private *dev_priv,
1753
void intel_flush_display_plane(struct drm_i915_private *dev_priv,
1754
				      enum plane plane)
1754
				      enum plane plane)
1755
{
1755
{
1756
	if (dev_priv->info->gen >= 4)
1756
	if (dev_priv->info->gen >= 4)
1757
		I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
1757
		I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
1758
	else
1758
	else
1759
	I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
1759
	I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
1760
}
1760
}
1761
 
1761
 
1762
/**
1762
/**
1763
 * intel_enable_plane - enable a display plane on a given pipe
1763
 * intel_enable_plane - enable a display plane on a given pipe
1764
 * @dev_priv: i915 private structure
1764
 * @dev_priv: i915 private structure
1765
 * @plane: plane to enable
1765
 * @plane: plane to enable
1766
 * @pipe: pipe being fed
1766
 * @pipe: pipe being fed
1767
 *
1767
 *
1768
 * Enable @plane on @pipe, making sure that @pipe is running first.
1768
 * Enable @plane on @pipe, making sure that @pipe is running first.
1769
 */
1769
 */
1770
static void intel_enable_plane(struct drm_i915_private *dev_priv,
1770
static void intel_enable_plane(struct drm_i915_private *dev_priv,
1771
			       enum plane plane, enum pipe pipe)
1771
			       enum plane plane, enum pipe pipe)
1772
{
1772
{
1773
	int reg;
1773
	int reg;
1774
	u32 val;
1774
	u32 val;
1775
 
1775
 
1776
	/* If the pipe isn't enabled, we can't pump pixels and may hang */
1776
	/* If the pipe isn't enabled, we can't pump pixels and may hang */
1777
	assert_pipe_enabled(dev_priv, pipe);
1777
	assert_pipe_enabled(dev_priv, pipe);
1778
 
1778
 
1779
	reg = DSPCNTR(plane);
1779
	reg = DSPCNTR(plane);
1780
	val = I915_READ(reg);
1780
	val = I915_READ(reg);
1781
	if (val & DISPLAY_PLANE_ENABLE)
1781
	if (val & DISPLAY_PLANE_ENABLE)
1782
		return;
1782
		return;
1783
 
1783
 
1784
	I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
1784
	I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
1785
	intel_flush_display_plane(dev_priv, plane);
1785
	intel_flush_display_plane(dev_priv, plane);
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_plane - disable a display plane
1790
 * intel_disable_plane - disable a display plane
1791
 * @dev_priv: i915 private structure
1791
 * @dev_priv: i915 private structure
1792
 * @plane: plane to disable
1792
 * @plane: plane to disable
1793
 * @pipe: pipe consuming the data
1793
 * @pipe: pipe consuming the data
1794
 *
1794
 *
1795
 * Disable @plane; should be an independent operation.
1795
 * Disable @plane; should be an independent operation.
1796
 */
1796
 */
1797
static void intel_disable_plane(struct drm_i915_private *dev_priv,
1797
static void intel_disable_plane(struct drm_i915_private *dev_priv,
1798
				enum plane plane, enum pipe pipe)
1798
				enum plane plane, enum pipe pipe)
1799
{
1799
{
1800
	int reg;
1800
	int reg;
1801
	u32 val;
1801
	u32 val;
1802
 
1802
 
1803
	reg = DSPCNTR(plane);
1803
	reg = DSPCNTR(plane);
1804
	val = I915_READ(reg);
1804
	val = I915_READ(reg);
1805
	if ((val & DISPLAY_PLANE_ENABLE) == 0)
1805
	if ((val & DISPLAY_PLANE_ENABLE) == 0)
1806
		return;
1806
		return;
1807
 
1807
 
1808
	I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
1808
	I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
1809
	intel_flush_display_plane(dev_priv, plane);
1809
	intel_flush_display_plane(dev_priv, plane);
1810
    intel_wait_for_vblank(dev_priv->dev, pipe);
1810
    intel_wait_for_vblank(dev_priv->dev, pipe);
1811
}
1811
}
1812
 
1812
 
1813
static bool need_vtd_wa(struct drm_device *dev)
1813
static bool need_vtd_wa(struct drm_device *dev)
1814
{
1814
{
1815
#ifdef CONFIG_INTEL_IOMMU
1815
#ifdef CONFIG_INTEL_IOMMU
1816
	if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
1816
	if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
1817
		return true;
1817
		return true;
1818
#endif
1818
#endif
1819
	return false;
1819
	return false;
1820
}
1820
}
1821
 
1821
 
1822
int
1822
int
1823
intel_pin_and_fence_fb_obj(struct drm_device *dev,
1823
intel_pin_and_fence_fb_obj(struct drm_device *dev,
1824
			   struct drm_i915_gem_object *obj,
1824
			   struct drm_i915_gem_object *obj,
1825
			   struct intel_ring_buffer *pipelined)
1825
			   struct intel_ring_buffer *pipelined)
1826
{
1826
{
1827
	struct drm_i915_private *dev_priv = dev->dev_private;
1827
	struct drm_i915_private *dev_priv = dev->dev_private;
1828
	u32 alignment;
1828
	u32 alignment;
1829
	int ret;
1829
	int ret;
1830
 
1830
 
1831
	switch (obj->tiling_mode) {
1831
	switch (obj->tiling_mode) {
1832
	case I915_TILING_NONE:
1832
	case I915_TILING_NONE:
1833
		if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1833
		if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1834
			alignment = 128 * 1024;
1834
			alignment = 128 * 1024;
1835
		else if (INTEL_INFO(dev)->gen >= 4)
1835
		else if (INTEL_INFO(dev)->gen >= 4)
1836
			alignment = 4 * 1024;
1836
			alignment = 4 * 1024;
1837
		else
1837
		else
1838
			alignment = 64 * 1024;
1838
			alignment = 64 * 1024;
1839
		break;
1839
		break;
1840
	case I915_TILING_X:
1840
	case I915_TILING_X:
1841
		/* pin() will align the object as required by fence */
1841
		/* pin() will align the object as required by fence */
1842
		alignment = 0;
1842
		alignment = 0;
1843
		break;
1843
		break;
1844
	case I915_TILING_Y:
1844
	case I915_TILING_Y:
1845
		/* Despite that we check this in framebuffer_init userspace can
1845
		/* Despite that we check this in framebuffer_init userspace can
1846
		 * screw us over and change the tiling after the fact. Only
1846
		 * screw us over and change the tiling after the fact. Only
1847
		 * pinned buffers can't change their tiling. */
1847
		 * pinned buffers can't change their tiling. */
1848
		DRM_DEBUG_DRIVER("Y tiled not allowed for scan out buffers\n");
1848
		DRM_DEBUG_DRIVER("Y tiled not allowed for scan out buffers\n");
1849
		return -EINVAL;
1849
		return -EINVAL;
1850
	default:
1850
	default:
1851
		BUG();
1851
		BUG();
1852
	}
1852
	}
1853
 
1853
 
1854
	/* Note that the w/a also requires 64 PTE of padding following the
1854
	/* Note that the w/a also requires 64 PTE of padding following the
1855
	 * bo. We currently fill all unused PTE with the shadow page and so
1855
	 * bo. We currently fill all unused PTE with the shadow page and so
1856
	 * we should always have valid PTE following the scanout preventing
1856
	 * we should always have valid PTE following the scanout preventing
1857
	 * the VT-d warning.
1857
	 * the VT-d warning.
1858
	 */
1858
	 */
1859
	if (need_vtd_wa(dev) && alignment < 256 * 1024)
1859
	if (need_vtd_wa(dev) && alignment < 256 * 1024)
1860
		alignment = 256 * 1024;
1860
		alignment = 256 * 1024;
1861
 
1861
 
1862
	dev_priv->mm.interruptible = false;
1862
	dev_priv->mm.interruptible = false;
1863
	ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
1863
	ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
1864
	if (ret)
1864
	if (ret)
1865
		goto err_interruptible;
1865
		goto err_interruptible;
1866
 
1866
 
1867
	/* Install a fence for tiled scan-out. Pre-i965 always needs a
1867
	/* Install a fence for tiled scan-out. Pre-i965 always needs a
1868
	 * fence, whereas 965+ only requires a fence if using
1868
	 * fence, whereas 965+ only requires a fence if using
1869
	 * framebuffer compression.  For simplicity, we always install
1869
	 * framebuffer compression.  For simplicity, we always install
1870
	 * a fence as the cost is not that onerous.
1870
	 * a fence as the cost is not that onerous.
1871
	 */
1871
	 */
1872
	ret = i915_gem_object_get_fence(obj);
1872
	ret = i915_gem_object_get_fence(obj);
1873
	if (ret)
1873
	if (ret)
1874
		goto err_unpin;
1874
		goto err_unpin;
1875
 
1875
 
1876
	i915_gem_object_pin_fence(obj);
1876
	i915_gem_object_pin_fence(obj);
1877
 
1877
 
1878
	dev_priv->mm.interruptible = true;
1878
	dev_priv->mm.interruptible = true;
1879
	return 0;
1879
	return 0;
1880
 
1880
 
1881
err_unpin:
1881
err_unpin:
1882
	i915_gem_object_unpin_from_display_plane(obj);
1882
	i915_gem_object_unpin_from_display_plane(obj);
1883
err_interruptible:
1883
err_interruptible:
1884
	dev_priv->mm.interruptible = true;
1884
	dev_priv->mm.interruptible = true;
1885
	return ret;
1885
	return ret;
1886
}
1886
}
1887
 
1887
 
1888
void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
1888
void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
1889
{
1889
{
1890
//	i915_gem_object_unpin_fence(obj);
1890
//	i915_gem_object_unpin_fence(obj);
1891
//	i915_gem_object_unpin(obj);
1891
//	i915_gem_object_unpin(obj);
1892
}
1892
}
1893
 
1893
 
1894
/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
1894
/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
1895
 * is assumed to be a power-of-two. */
1895
 * is assumed to be a power-of-two. */
1896
unsigned long intel_gen4_compute_page_offset(int *x, int *y,
1896
unsigned long intel_gen4_compute_page_offset(int *x, int *y,
1897
					     unsigned int tiling_mode,
1897
					     unsigned int tiling_mode,
1898
					     unsigned int cpp,
1898
					     unsigned int cpp,
1899
							unsigned int pitch)
1899
							unsigned int pitch)
1900
{
1900
{
1901
	if (tiling_mode != I915_TILING_NONE) {
1901
	if (tiling_mode != I915_TILING_NONE) {
1902
		unsigned int tile_rows, tiles;
1902
		unsigned int tile_rows, tiles;
1903
 
1903
 
1904
	tile_rows = *y / 8;
1904
	tile_rows = *y / 8;
1905
	*y %= 8;
1905
	*y %= 8;
1906
 
1906
 
1907
		tiles = *x / (512/cpp);
1907
		tiles = *x / (512/cpp);
1908
		*x %= 512/cpp;
1908
		*x %= 512/cpp;
1909
 
1909
 
1910
	return tile_rows * pitch * 8 + tiles * 4096;
1910
	return tile_rows * pitch * 8 + tiles * 4096;
1911
	} else {
1911
	} else {
1912
		unsigned int offset;
1912
		unsigned int offset;
1913
 
1913
 
1914
		offset = *y * pitch + *x * cpp;
1914
		offset = *y * pitch + *x * cpp;
1915
		*y = 0;
1915
		*y = 0;
1916
		*x = (offset & 4095) / cpp;
1916
		*x = (offset & 4095) / cpp;
1917
		return offset & -4096;
1917
		return offset & -4096;
1918
	}
1918
	}
1919
}
1919
}
1920
 
1920
 
1921
static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
1921
static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
1922
                 int x, int y)
1922
                 int x, int y)
1923
{
1923
{
1924
    struct drm_device *dev = crtc->dev;
1924
    struct drm_device *dev = crtc->dev;
1925
    struct drm_i915_private *dev_priv = dev->dev_private;
1925
    struct drm_i915_private *dev_priv = dev->dev_private;
1926
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1926
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1927
    struct intel_framebuffer *intel_fb;
1927
    struct intel_framebuffer *intel_fb;
1928
    struct drm_i915_gem_object *obj;
1928
    struct drm_i915_gem_object *obj;
1929
    int plane = intel_crtc->plane;
1929
    int plane = intel_crtc->plane;
1930
	unsigned long linear_offset;
1930
	unsigned long linear_offset;
1931
    u32 dspcntr;
1931
    u32 dspcntr;
1932
    u32 reg;
1932
    u32 reg;
1933
 
1933
 
1934
    switch (plane) {
1934
    switch (plane) {
1935
    case 0:
1935
    case 0:
1936
    case 1:
1936
    case 1:
1937
        break;
1937
        break;
1938
    default:
1938
    default:
1939
		DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
1939
		DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
1940
        return -EINVAL;
1940
        return -EINVAL;
1941
    }
1941
    }
1942
 
1942
 
1943
    intel_fb = to_intel_framebuffer(fb);
1943
    intel_fb = to_intel_framebuffer(fb);
1944
    obj = intel_fb->obj;
1944
    obj = intel_fb->obj;
1945
 
1945
 
1946
    reg = DSPCNTR(plane);
1946
    reg = DSPCNTR(plane);
1947
    dspcntr = I915_READ(reg);
1947
    dspcntr = I915_READ(reg);
1948
    /* Mask out pixel format bits in case we change it */
1948
    /* Mask out pixel format bits in case we change it */
1949
    dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
1949
    dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
1950
	switch (fb->pixel_format) {
1950
	switch (fb->pixel_format) {
1951
	case DRM_FORMAT_C8:
1951
	case DRM_FORMAT_C8:
1952
        dspcntr |= DISPPLANE_8BPP;
1952
        dspcntr |= DISPPLANE_8BPP;
1953
        break;
1953
        break;
1954
	case DRM_FORMAT_XRGB1555:
1954
	case DRM_FORMAT_XRGB1555:
1955
	case DRM_FORMAT_ARGB1555:
1955
	case DRM_FORMAT_ARGB1555:
1956
		dspcntr |= DISPPLANE_BGRX555;
1956
		dspcntr |= DISPPLANE_BGRX555;
1957
		break;
1957
		break;
1958
	case DRM_FORMAT_RGB565:
1958
	case DRM_FORMAT_RGB565:
1959
		dspcntr |= DISPPLANE_BGRX565;
1959
		dspcntr |= DISPPLANE_BGRX565;
1960
		break;
1960
		break;
1961
	case DRM_FORMAT_XRGB8888:
1961
	case DRM_FORMAT_XRGB8888:
1962
	case DRM_FORMAT_ARGB8888:
1962
	case DRM_FORMAT_ARGB8888:
1963
		dspcntr |= DISPPLANE_BGRX888;
1963
		dspcntr |= DISPPLANE_BGRX888;
1964
		break;
1964
		break;
1965
	case DRM_FORMAT_XBGR8888:
1965
	case DRM_FORMAT_XBGR8888:
1966
	case DRM_FORMAT_ABGR8888:
1966
	case DRM_FORMAT_ABGR8888:
1967
		dspcntr |= DISPPLANE_RGBX888;
1967
		dspcntr |= DISPPLANE_RGBX888;
1968
		break;
1968
		break;
1969
	case DRM_FORMAT_XRGB2101010:
1969
	case DRM_FORMAT_XRGB2101010:
1970
	case DRM_FORMAT_ARGB2101010:
1970
	case DRM_FORMAT_ARGB2101010:
1971
		dspcntr |= DISPPLANE_BGRX101010;
1971
		dspcntr |= DISPPLANE_BGRX101010;
1972
        break;
1972
        break;
1973
	case DRM_FORMAT_XBGR2101010:
1973
	case DRM_FORMAT_XBGR2101010:
1974
	case DRM_FORMAT_ABGR2101010:
1974
	case DRM_FORMAT_ABGR2101010:
1975
		dspcntr |= DISPPLANE_RGBX101010;
1975
		dspcntr |= DISPPLANE_RGBX101010;
1976
        break;
1976
        break;
1977
    default:
1977
    default:
1978
		BUG();
1978
		BUG();
1979
    }
1979
    }
1980
 
1980
 
1981
    if (INTEL_INFO(dev)->gen >= 4) {
1981
    if (INTEL_INFO(dev)->gen >= 4) {
1982
        if (obj->tiling_mode != I915_TILING_NONE)
1982
        if (obj->tiling_mode != I915_TILING_NONE)
1983
            dspcntr |= DISPPLANE_TILED;
1983
            dspcntr |= DISPPLANE_TILED;
1984
        else
1984
        else
1985
            dspcntr &= ~DISPPLANE_TILED;
1985
            dspcntr &= ~DISPPLANE_TILED;
1986
    }
1986
    }
1987
 
1987
 
1988
	if (IS_G4X(dev))
1988
	if (IS_G4X(dev))
1989
		dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
1989
		dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
1990
 
1990
 
1991
    I915_WRITE(reg, dspcntr);
1991
    I915_WRITE(reg, dspcntr);
1992
 
1992
 
1993
	linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
1993
	linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
1994
 
1994
 
1995
	if (INTEL_INFO(dev)->gen >= 4) {
1995
	if (INTEL_INFO(dev)->gen >= 4) {
1996
		intel_crtc->dspaddr_offset =
1996
		intel_crtc->dspaddr_offset =
1997
			intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
1997
			intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
1998
							   fb->bits_per_pixel / 8,
1998
							   fb->bits_per_pixel / 8,
1999
							   fb->pitches[0]);
1999
							   fb->pitches[0]);
2000
		linear_offset -= intel_crtc->dspaddr_offset;
2000
		linear_offset -= intel_crtc->dspaddr_offset;
2001
	} else {
2001
	} else {
2002
		intel_crtc->dspaddr_offset = linear_offset;
2002
		intel_crtc->dspaddr_offset = linear_offset;
2003
	}
2003
	}
2004
 
2004
 
2005
	DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2005
	DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2006
		      i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2006
		      i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2007
		      fb->pitches[0]);
2007
		      fb->pitches[0]);
2008
	I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
2008
	I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
2009
    if (INTEL_INFO(dev)->gen >= 4) {
2009
    if (INTEL_INFO(dev)->gen >= 4) {
2010
		I915_MODIFY_DISPBASE(DSPSURF(plane),
2010
		I915_MODIFY_DISPBASE(DSPSURF(plane),
2011
				     i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
2011
				     i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
2012
        I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2012
        I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2013
		I915_WRITE(DSPLINOFF(plane), linear_offset);
2013
		I915_WRITE(DSPLINOFF(plane), linear_offset);
2014
    } else
2014
    } else
2015
		I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
2015
		I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
2016
    POSTING_READ(reg);
2016
    POSTING_READ(reg);
2017
 
2017
 
2018
    return 0;
2018
    return 0;
2019
}
2019
}
2020
 
2020
 
2021
static int ironlake_update_plane(struct drm_crtc *crtc,
2021
static int ironlake_update_plane(struct drm_crtc *crtc,
2022
                 struct drm_framebuffer *fb, int x, int y)
2022
                 struct drm_framebuffer *fb, int x, int y)
2023
{
2023
{
2024
    struct drm_device *dev = crtc->dev;
2024
    struct drm_device *dev = crtc->dev;
2025
    struct drm_i915_private *dev_priv = dev->dev_private;
2025
    struct drm_i915_private *dev_priv = dev->dev_private;
2026
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2026
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2027
    struct intel_framebuffer *intel_fb;
2027
    struct intel_framebuffer *intel_fb;
2028
    struct drm_i915_gem_object *obj;
2028
    struct drm_i915_gem_object *obj;
2029
    int plane = intel_crtc->plane;
2029
    int plane = intel_crtc->plane;
2030
	unsigned long linear_offset;
2030
	unsigned long linear_offset;
2031
    u32 dspcntr;
2031
    u32 dspcntr;
2032
    u32 reg;
2032
    u32 reg;
2033
 
2033
 
2034
    switch (plane) {
2034
    switch (plane) {
2035
    case 0:
2035
    case 0:
2036
    case 1:
2036
    case 1:
2037
	case 2:
2037
	case 2:
2038
        break;
2038
        break;
2039
    default:
2039
    default:
2040
		DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
2040
		DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
2041
        return -EINVAL;
2041
        return -EINVAL;
2042
    }
2042
    }
2043
 
2043
 
2044
    intel_fb = to_intel_framebuffer(fb);
2044
    intel_fb = to_intel_framebuffer(fb);
2045
    obj = intel_fb->obj;
2045
    obj = intel_fb->obj;
2046
 
2046
 
2047
    reg = DSPCNTR(plane);
2047
    reg = DSPCNTR(plane);
2048
    dspcntr = I915_READ(reg);
2048
    dspcntr = I915_READ(reg);
2049
    /* Mask out pixel format bits in case we change it */
2049
    /* Mask out pixel format bits in case we change it */
2050
    dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
2050
    dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
2051
	switch (fb->pixel_format) {
2051
	switch (fb->pixel_format) {
2052
	case DRM_FORMAT_C8:
2052
	case DRM_FORMAT_C8:
2053
        dspcntr |= DISPPLANE_8BPP;
2053
        dspcntr |= DISPPLANE_8BPP;
2054
        break;
2054
        break;
2055
	case DRM_FORMAT_RGB565:
2055
	case DRM_FORMAT_RGB565:
2056
		dspcntr |= DISPPLANE_BGRX565;
2056
		dspcntr |= DISPPLANE_BGRX565;
2057
        break;
2057
        break;
2058
	case DRM_FORMAT_XRGB8888:
2058
	case DRM_FORMAT_XRGB8888:
2059
	case DRM_FORMAT_ARGB8888:
2059
	case DRM_FORMAT_ARGB8888:
2060
		dspcntr |= DISPPLANE_BGRX888;
2060
		dspcntr |= DISPPLANE_BGRX888;
2061
		break;
2061
		break;
2062
	case DRM_FORMAT_XBGR8888:
2062
	case DRM_FORMAT_XBGR8888:
2063
	case DRM_FORMAT_ABGR8888:
2063
	case DRM_FORMAT_ABGR8888:
2064
		dspcntr |= DISPPLANE_RGBX888;
2064
		dspcntr |= DISPPLANE_RGBX888;
2065
		break;
2065
		break;
2066
	case DRM_FORMAT_XRGB2101010:
2066
	case DRM_FORMAT_XRGB2101010:
2067
	case DRM_FORMAT_ARGB2101010:
2067
	case DRM_FORMAT_ARGB2101010:
2068
		dspcntr |= DISPPLANE_BGRX101010;
2068
		dspcntr |= DISPPLANE_BGRX101010;
2069
		break;
2069
		break;
2070
	case DRM_FORMAT_XBGR2101010:
2070
	case DRM_FORMAT_XBGR2101010:
2071
	case DRM_FORMAT_ABGR2101010:
2071
	case DRM_FORMAT_ABGR2101010:
2072
		dspcntr |= DISPPLANE_RGBX101010;
2072
		dspcntr |= DISPPLANE_RGBX101010;
2073
        break;
2073
        break;
2074
    default:
2074
    default:
2075
		BUG();
2075
		BUG();
2076
    }
2076
    }
2077
 
2077
 
2078
	if (obj->tiling_mode != I915_TILING_NONE)
2078
	if (obj->tiling_mode != I915_TILING_NONE)
2079
		dspcntr |= DISPPLANE_TILED;
2079
		dspcntr |= DISPPLANE_TILED;
2080
	else
2080
	else
2081
        dspcntr &= ~DISPPLANE_TILED;
2081
        dspcntr &= ~DISPPLANE_TILED;
2082
 
2082
 
2083
	if (IS_HASWELL(dev))
2083
	if (IS_HASWELL(dev))
2084
		dspcntr &= ~DISPPLANE_TRICKLE_FEED_DISABLE;
2084
		dspcntr &= ~DISPPLANE_TRICKLE_FEED_DISABLE;
2085
	else
2085
	else
2086
    dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2086
    dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2087
 
2087
 
2088
    I915_WRITE(reg, dspcntr);
2088
    I915_WRITE(reg, dspcntr);
2089
 
2089
 
2090
	linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
2090
	linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
2091
	intel_crtc->dspaddr_offset =
2091
	intel_crtc->dspaddr_offset =
2092
		intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2092
		intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2093
						   fb->bits_per_pixel / 8,
2093
						   fb->bits_per_pixel / 8,
2094
						   fb->pitches[0]);
2094
						   fb->pitches[0]);
2095
	linear_offset -= intel_crtc->dspaddr_offset;
2095
	linear_offset -= intel_crtc->dspaddr_offset;
2096
 
2096
 
2097
	DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2097
	DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2098
		      i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2098
		      i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2099
		      fb->pitches[0]);
2099
		      fb->pitches[0]);
2100
	I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
2100
	I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
2101
	I915_MODIFY_DISPBASE(DSPSURF(plane),
2101
	I915_MODIFY_DISPBASE(DSPSURF(plane),
2102
			     i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
2102
			     i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
2103
	if (IS_HASWELL(dev)) {
2103
	if (IS_HASWELL(dev)) {
2104
		I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2104
		I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2105
	} else {
2105
	} else {
2106
	I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2106
	I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2107
	I915_WRITE(DSPLINOFF(plane), linear_offset);
2107
	I915_WRITE(DSPLINOFF(plane), linear_offset);
2108
	}
2108
	}
2109
	POSTING_READ(reg);
2109
	POSTING_READ(reg);
2110
 
2110
 
2111
    return 0;
2111
    return 0;
2112
}
2112
}
2113
 
2113
 
2114
/* Assume fb object is pinned & idle & fenced and just update base pointers */
2114
/* Assume fb object is pinned & idle & fenced and just update base pointers */
2115
static int
2115
static int
2116
intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2116
intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2117
			   int x, int y, enum mode_set_atomic state)
2117
			   int x, int y, enum mode_set_atomic state)
2118
{
2118
{
2119
	struct drm_device *dev = crtc->dev;
2119
	struct drm_device *dev = crtc->dev;
2120
	struct drm_i915_private *dev_priv = dev->dev_private;
2120
	struct drm_i915_private *dev_priv = dev->dev_private;
2121
 
2121
 
2122
	if (dev_priv->display.disable_fbc)
2122
	if (dev_priv->display.disable_fbc)
2123
		dev_priv->display.disable_fbc(dev);
2123
		dev_priv->display.disable_fbc(dev);
2124
	intel_increase_pllclock(crtc);
2124
	intel_increase_pllclock(crtc);
2125
 
2125
 
2126
	return dev_priv->display.update_plane(crtc, fb, x, y);
2126
	return dev_priv->display.update_plane(crtc, fb, x, y);
2127
}
2127
}
2128
 
2128
 
2129
#if 0
2129
#if 0
2130
void intel_display_handle_reset(struct drm_device *dev)
2130
void intel_display_handle_reset(struct drm_device *dev)
2131
{
2131
{
2132
	struct drm_i915_private *dev_priv = dev->dev_private;
2132
	struct drm_i915_private *dev_priv = dev->dev_private;
2133
	struct drm_crtc *crtc;
2133
	struct drm_crtc *crtc;
2134
 
2134
 
2135
	/*
2135
	/*
2136
	 * Flips in the rings have been nuked by the reset,
2136
	 * Flips in the rings have been nuked by the reset,
2137
	 * so complete all pending flips so that user space
2137
	 * so complete all pending flips so that user space
2138
	 * will get its events and not get stuck.
2138
	 * will get its events and not get stuck.
2139
	 *
2139
	 *
2140
	 * Also update the base address of all primary
2140
	 * Also update the base address of all primary
2141
	 * planes to the the last fb to make sure we're
2141
	 * planes to the the last fb to make sure we're
2142
	 * showing the correct fb after a reset.
2142
	 * showing the correct fb after a reset.
2143
	 *
2143
	 *
2144
	 * Need to make two loops over the crtcs so that we
2144
	 * Need to make two loops over the crtcs so that we
2145
	 * don't try to grab a crtc mutex before the
2145
	 * don't try to grab a crtc mutex before the
2146
	 * pending_flip_queue really got woken up.
2146
	 * pending_flip_queue really got woken up.
2147
	 */
2147
	 */
2148
 
2148
 
2149
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2149
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2150
		struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2150
		struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2151
		enum plane plane = intel_crtc->plane;
2151
		enum plane plane = intel_crtc->plane;
2152
 
2152
 
2153
		intel_prepare_page_flip(dev, plane);
2153
		intel_prepare_page_flip(dev, plane);
2154
		intel_finish_page_flip_plane(dev, plane);
2154
		intel_finish_page_flip_plane(dev, plane);
2155
	}
2155
	}
2156
 
2156
 
2157
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2157
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2158
		struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2158
		struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2159
 
2159
 
2160
		mutex_lock(&crtc->mutex);
2160
		mutex_lock(&crtc->mutex);
2161
		if (intel_crtc->active)
2161
		if (intel_crtc->active)
2162
			dev_priv->display.update_plane(crtc, crtc->fb,
2162
			dev_priv->display.update_plane(crtc, crtc->fb,
2163
						       crtc->x, crtc->y);
2163
						       crtc->x, crtc->y);
2164
		mutex_unlock(&crtc->mutex);
2164
		mutex_unlock(&crtc->mutex);
2165
	}
2165
	}
2166
}
2166
}
2167
 
2167
 
2168
static int
2168
static int
2169
intel_finish_fb(struct drm_framebuffer *old_fb)
2169
intel_finish_fb(struct drm_framebuffer *old_fb)
2170
{
2170
{
2171
	struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2171
	struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2172
	struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2172
	struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2173
	bool was_interruptible = dev_priv->mm.interruptible;
2173
	bool was_interruptible = dev_priv->mm.interruptible;
2174
	int ret;
2174
	int ret;
2175
 
2175
 
2176
	/* Big Hammer, we also need to ensure that any pending
2176
	/* Big Hammer, we also need to ensure that any pending
2177
	 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2177
	 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2178
	 * current scanout is retired before unpinning the old
2178
	 * current scanout is retired before unpinning the old
2179
	 * framebuffer.
2179
	 * framebuffer.
2180
	 *
2180
	 *
2181
	 * This should only fail upon a hung GPU, in which case we
2181
	 * This should only fail upon a hung GPU, in which case we
2182
	 * can safely continue.
2182
	 * can safely continue.
2183
	 */
2183
	 */
2184
	dev_priv->mm.interruptible = false;
2184
	dev_priv->mm.interruptible = false;
2185
	ret = i915_gem_object_finish_gpu(obj);
2185
	ret = i915_gem_object_finish_gpu(obj);
2186
	dev_priv->mm.interruptible = was_interruptible;
2186
	dev_priv->mm.interruptible = was_interruptible;
2187
 
2187
 
2188
	return ret;
2188
	return ret;
2189
}
2189
}
2190
 
2190
 
2191
static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
2191
static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
2192
{
2192
{
2193
	struct drm_device *dev = crtc->dev;
2193
	struct drm_device *dev = crtc->dev;
2194
	struct drm_i915_master_private *master_priv;
2194
	struct drm_i915_master_private *master_priv;
2195
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2195
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2196
 
2196
 
2197
	if (!dev->primary->master)
2197
	if (!dev->primary->master)
2198
		return;
2198
		return;
2199
 
2199
 
2200
	master_priv = dev->primary->master->driver_priv;
2200
	master_priv = dev->primary->master->driver_priv;
2201
	if (!master_priv->sarea_priv)
2201
	if (!master_priv->sarea_priv)
2202
		return;
2202
		return;
2203
 
2203
 
2204
	switch (intel_crtc->pipe) {
2204
	switch (intel_crtc->pipe) {
2205
	case 0:
2205
	case 0:
2206
		master_priv->sarea_priv->pipeA_x = x;
2206
		master_priv->sarea_priv->pipeA_x = x;
2207
		master_priv->sarea_priv->pipeA_y = y;
2207
		master_priv->sarea_priv->pipeA_y = y;
2208
		break;
2208
		break;
2209
	case 1:
2209
	case 1:
2210
		master_priv->sarea_priv->pipeB_x = x;
2210
		master_priv->sarea_priv->pipeB_x = x;
2211
		master_priv->sarea_priv->pipeB_y = y;
2211
		master_priv->sarea_priv->pipeB_y = y;
2212
		break;
2212
		break;
2213
	default:
2213
	default:
2214
		break;
2214
		break;
2215
	}
2215
	}
2216
}
2216
}
2217
#endif
2217
#endif
2218
 
2218
 
2219
static int
2219
static int
2220
intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
2220
intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
2221
		    struct drm_framebuffer *fb)
2221
		    struct drm_framebuffer *fb)
2222
{
2222
{
2223
	struct drm_device *dev = crtc->dev;
2223
	struct drm_device *dev = crtc->dev;
2224
	struct drm_i915_private *dev_priv = dev->dev_private;
2224
	struct drm_i915_private *dev_priv = dev->dev_private;
2225
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2225
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2226
	struct drm_framebuffer *old_fb;
2226
	struct drm_framebuffer *old_fb;
2227
	int ret;
2227
	int ret;
2228
 
2228
 
2229
	/* no fb bound */
2229
	/* no fb bound */
2230
	if (!fb) {
2230
	if (!fb) {
2231
		DRM_ERROR("No FB bound\n");
2231
		DRM_ERROR("No FB bound\n");
2232
		return 0;
2232
		return 0;
2233
	}
2233
	}
2234
 
2234
 
2235
	if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
2235
	if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
2236
		DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2236
		DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2237
			  plane_name(intel_crtc->plane),
2237
			  plane_name(intel_crtc->plane),
2238
				INTEL_INFO(dev)->num_pipes);
2238
				INTEL_INFO(dev)->num_pipes);
2239
		return -EINVAL;
2239
		return -EINVAL;
2240
	}
2240
	}
2241
 
2241
 
2242
	mutex_lock(&dev->struct_mutex);
2242
	mutex_lock(&dev->struct_mutex);
2243
    ret = intel_pin_and_fence_fb_obj(dev,
2243
    ret = intel_pin_and_fence_fb_obj(dev,
2244
                    to_intel_framebuffer(fb)->obj,
2244
                    to_intel_framebuffer(fb)->obj,
2245
                    NULL);
2245
                    NULL);
2246
    if (ret != 0) {
2246
    if (ret != 0) {
2247
       mutex_unlock(&dev->struct_mutex);
2247
       mutex_unlock(&dev->struct_mutex);
2248
       DRM_ERROR("pin & fence failed\n");
2248
       DRM_ERROR("pin & fence failed\n");
2249
       return ret;
2249
       return ret;
2250
    }
2250
    }
2251
 
2251
 
2252
	/* Update pipe size and adjust fitter if needed */
2252
	/* Update pipe size and adjust fitter if needed */
2253
	if (i915_fastboot) {
2253
	if (i915_fastboot) {
2254
		I915_WRITE(PIPESRC(intel_crtc->pipe),
2254
		I915_WRITE(PIPESRC(intel_crtc->pipe),
2255
			   ((crtc->mode.hdisplay - 1) << 16) |
2255
			   ((crtc->mode.hdisplay - 1) << 16) |
2256
			   (crtc->mode.vdisplay - 1));
2256
			   (crtc->mode.vdisplay - 1));
2257
		if (!intel_crtc->config.pch_pfit.enabled &&
2257
		if (!intel_crtc->config.pch_pfit.enabled &&
2258
		    (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
2258
		    (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
2259
		     intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2259
		     intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2260
			I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
2260
			I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
2261
			I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
2261
			I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
2262
			I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
2262
			I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
2263
		}
2263
		}
2264
	}
2264
	}
2265
 
2265
 
2266
	ret = dev_priv->display.update_plane(crtc, fb, x, y);
2266
	ret = dev_priv->display.update_plane(crtc, fb, x, y);
2267
	if (ret) {
2267
	if (ret) {
2268
		intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
2268
		intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
2269
		mutex_unlock(&dev->struct_mutex);
2269
		mutex_unlock(&dev->struct_mutex);
2270
		DRM_ERROR("failed to update base address\n");
2270
		DRM_ERROR("failed to update base address\n");
2271
        return ret;
2271
        return ret;
2272
	}
2272
	}
2273
 
2273
 
2274
	old_fb = crtc->fb;
2274
	old_fb = crtc->fb;
2275
	crtc->fb = fb;
2275
	crtc->fb = fb;
2276
	crtc->x = x;
2276
	crtc->x = x;
2277
	crtc->y = y;
2277
	crtc->y = y;
2278
 
2278
 
2279
	if (old_fb) {
2279
	if (old_fb) {
2280
		if (intel_crtc->active && old_fb != fb)
2280
		if (intel_crtc->active && old_fb != fb)
2281
		intel_wait_for_vblank(dev, intel_crtc->pipe);
2281
		intel_wait_for_vblank(dev, intel_crtc->pipe);
2282
		intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
2282
		intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
2283
	}
2283
	}
2284
 
2284
 
2285
	intel_update_fbc(dev);
2285
	intel_update_fbc(dev);
2286
	intel_edp_psr_update(dev);
2286
	intel_edp_psr_update(dev);
2287
	mutex_unlock(&dev->struct_mutex);
2287
	mutex_unlock(&dev->struct_mutex);
2288
 
2288
 
2289
    return 0;
2289
    return 0;
2290
}
2290
}
2291
 
2291
 
2292
static void intel_fdi_normal_train(struct drm_crtc *crtc)
2292
static void intel_fdi_normal_train(struct drm_crtc *crtc)
2293
{
2293
{
2294
	struct drm_device *dev = crtc->dev;
2294
	struct drm_device *dev = crtc->dev;
2295
	struct drm_i915_private *dev_priv = dev->dev_private;
2295
	struct drm_i915_private *dev_priv = dev->dev_private;
2296
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2296
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2297
	int pipe = intel_crtc->pipe;
2297
	int pipe = intel_crtc->pipe;
2298
	u32 reg, temp;
2298
	u32 reg, temp;
2299
 
2299
 
2300
	/* enable normal train */
2300
	/* enable normal train */
2301
	reg = FDI_TX_CTL(pipe);
2301
	reg = FDI_TX_CTL(pipe);
2302
	temp = I915_READ(reg);
2302
	temp = I915_READ(reg);
2303
	if (IS_IVYBRIDGE(dev)) {
2303
	if (IS_IVYBRIDGE(dev)) {
2304
		temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2304
		temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2305
		temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
2305
		temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
2306
	} else {
2306
	} else {
2307
		temp &= ~FDI_LINK_TRAIN_NONE;
2307
		temp &= ~FDI_LINK_TRAIN_NONE;
2308
		temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
2308
		temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
2309
	}
2309
	}
2310
	I915_WRITE(reg, temp);
2310
	I915_WRITE(reg, temp);
2311
 
2311
 
2312
	reg = FDI_RX_CTL(pipe);
2312
	reg = FDI_RX_CTL(pipe);
2313
	temp = I915_READ(reg);
2313
	temp = I915_READ(reg);
2314
	if (HAS_PCH_CPT(dev)) {
2314
	if (HAS_PCH_CPT(dev)) {
2315
		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2315
		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2316
		temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2316
		temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2317
	} else {
2317
	} else {
2318
		temp &= ~FDI_LINK_TRAIN_NONE;
2318
		temp &= ~FDI_LINK_TRAIN_NONE;
2319
		temp |= FDI_LINK_TRAIN_NONE;
2319
		temp |= FDI_LINK_TRAIN_NONE;
2320
	}
2320
	}
2321
	I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2321
	I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2322
 
2322
 
2323
	/* wait one idle pattern time */
2323
	/* wait one idle pattern time */
2324
	POSTING_READ(reg);
2324
	POSTING_READ(reg);
2325
	udelay(1000);
2325
	udelay(1000);
2326
 
2326
 
2327
	/* IVB wants error correction enabled */
2327
	/* IVB wants error correction enabled */
2328
	if (IS_IVYBRIDGE(dev))
2328
	if (IS_IVYBRIDGE(dev))
2329
		I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2329
		I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2330
			   FDI_FE_ERRC_ENABLE);
2330
			   FDI_FE_ERRC_ENABLE);
2331
}
2331
}
2332
 
2332
 
2333
static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
2333
static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
2334
{
2334
{
2335
	return crtc->base.enabled && crtc->active &&
2335
	return crtc->base.enabled && crtc->active &&
2336
		crtc->config.has_pch_encoder;
2336
		crtc->config.has_pch_encoder;
2337
}
2337
}
2338
 
2338
 
2339
static void ivb_modeset_global_resources(struct drm_device *dev)
2339
static void ivb_modeset_global_resources(struct drm_device *dev)
2340
{
2340
{
2341
	struct drm_i915_private *dev_priv = dev->dev_private;
2341
	struct drm_i915_private *dev_priv = dev->dev_private;
2342
	struct intel_crtc *pipe_B_crtc =
2342
	struct intel_crtc *pipe_B_crtc =
2343
		to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2343
		to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2344
	struct intel_crtc *pipe_C_crtc =
2344
	struct intel_crtc *pipe_C_crtc =
2345
		to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2345
		to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2346
	uint32_t temp;
2346
	uint32_t temp;
2347
 
2347
 
2348
	/*
2348
	/*
2349
	 * When everything is off disable fdi C so that we could enable fdi B
2349
	 * When everything is off disable fdi C so that we could enable fdi B
2350
	 * with all lanes. Note that we don't care about enabled pipes without
2350
	 * with all lanes. Note that we don't care about enabled pipes without
2351
	 * an enabled pch encoder.
2351
	 * an enabled pch encoder.
2352
	 */
2352
	 */
2353
	if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2353
	if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2354
	    !pipe_has_enabled_pch(pipe_C_crtc)) {
2354
	    !pipe_has_enabled_pch(pipe_C_crtc)) {
2355
		WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2355
		WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2356
		WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2356
		WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2357
 
2357
 
2358
		temp = I915_READ(SOUTH_CHICKEN1);
2358
		temp = I915_READ(SOUTH_CHICKEN1);
2359
		temp &= ~FDI_BC_BIFURCATION_SELECT;
2359
		temp &= ~FDI_BC_BIFURCATION_SELECT;
2360
		DRM_DEBUG_KMS("disabling fdi C rx\n");
2360
		DRM_DEBUG_KMS("disabling fdi C rx\n");
2361
		I915_WRITE(SOUTH_CHICKEN1, temp);
2361
		I915_WRITE(SOUTH_CHICKEN1, temp);
2362
	}
2362
	}
2363
}
2363
}
2364
 
2364
 
2365
/* The FDI link training functions for ILK/Ibexpeak. */
2365
/* The FDI link training functions for ILK/Ibexpeak. */
2366
static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2366
static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2367
{
2367
{
2368
    struct drm_device *dev = crtc->dev;
2368
    struct drm_device *dev = crtc->dev;
2369
    struct drm_i915_private *dev_priv = dev->dev_private;
2369
    struct drm_i915_private *dev_priv = dev->dev_private;
2370
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2370
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2371
    int pipe = intel_crtc->pipe;
2371
    int pipe = intel_crtc->pipe;
2372
    int plane = intel_crtc->plane;
2372
    int plane = intel_crtc->plane;
2373
    u32 reg, temp, tries;
2373
    u32 reg, temp, tries;
2374
 
2374
 
2375
    /* FDI needs bits from pipe & plane first */
2375
    /* FDI needs bits from pipe & plane first */
2376
    assert_pipe_enabled(dev_priv, pipe);
2376
    assert_pipe_enabled(dev_priv, pipe);
2377
    assert_plane_enabled(dev_priv, plane);
2377
    assert_plane_enabled(dev_priv, plane);
2378
 
2378
 
2379
    /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2379
    /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2380
       for train result */
2380
       for train result */
2381
    reg = FDI_RX_IMR(pipe);
2381
    reg = FDI_RX_IMR(pipe);
2382
    temp = I915_READ(reg);
2382
    temp = I915_READ(reg);
2383
    temp &= ~FDI_RX_SYMBOL_LOCK;
2383
    temp &= ~FDI_RX_SYMBOL_LOCK;
2384
    temp &= ~FDI_RX_BIT_LOCK;
2384
    temp &= ~FDI_RX_BIT_LOCK;
2385
    I915_WRITE(reg, temp);
2385
    I915_WRITE(reg, temp);
2386
    I915_READ(reg);
2386
    I915_READ(reg);
2387
    udelay(150);
2387
    udelay(150);
2388
 
2388
 
2389
    /* enable CPU FDI TX and PCH FDI RX */
2389
    /* enable CPU FDI TX and PCH FDI RX */
2390
    reg = FDI_TX_CTL(pipe);
2390
    reg = FDI_TX_CTL(pipe);
2391
    temp = I915_READ(reg);
2391
    temp = I915_READ(reg);
2392
	temp &= ~FDI_DP_PORT_WIDTH_MASK;
2392
	temp &= ~FDI_DP_PORT_WIDTH_MASK;
2393
	temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
2393
	temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
2394
    temp &= ~FDI_LINK_TRAIN_NONE;
2394
    temp &= ~FDI_LINK_TRAIN_NONE;
2395
    temp |= FDI_LINK_TRAIN_PATTERN_1;
2395
    temp |= FDI_LINK_TRAIN_PATTERN_1;
2396
    I915_WRITE(reg, temp | FDI_TX_ENABLE);
2396
    I915_WRITE(reg, temp | FDI_TX_ENABLE);
2397
 
2397
 
2398
    reg = FDI_RX_CTL(pipe);
2398
    reg = FDI_RX_CTL(pipe);
2399
    temp = I915_READ(reg);
2399
    temp = I915_READ(reg);
2400
    temp &= ~FDI_LINK_TRAIN_NONE;
2400
    temp &= ~FDI_LINK_TRAIN_NONE;
2401
    temp |= FDI_LINK_TRAIN_PATTERN_1;
2401
    temp |= FDI_LINK_TRAIN_PATTERN_1;
2402
    I915_WRITE(reg, temp | FDI_RX_ENABLE);
2402
    I915_WRITE(reg, temp | FDI_RX_ENABLE);
2403
 
2403
 
2404
    POSTING_READ(reg);
2404
    POSTING_READ(reg);
2405
    udelay(150);
2405
    udelay(150);
2406
 
2406
 
2407
    /* Ironlake workaround, enable clock pointer after FDI enable*/
2407
    /* Ironlake workaround, enable clock pointer after FDI enable*/
2408
        I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2408
        I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2409
        I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2409
        I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2410
               FDI_RX_PHASE_SYNC_POINTER_EN);
2410
               FDI_RX_PHASE_SYNC_POINTER_EN);
2411
 
2411
 
2412
    reg = FDI_RX_IIR(pipe);
2412
    reg = FDI_RX_IIR(pipe);
2413
    for (tries = 0; tries < 5; tries++) {
2413
    for (tries = 0; tries < 5; tries++) {
2414
        temp = I915_READ(reg);
2414
        temp = I915_READ(reg);
2415
        DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2415
        DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2416
 
2416
 
2417
        if ((temp & FDI_RX_BIT_LOCK)) {
2417
        if ((temp & FDI_RX_BIT_LOCK)) {
2418
            DRM_DEBUG_KMS("FDI train 1 done.\n");
2418
            DRM_DEBUG_KMS("FDI train 1 done.\n");
2419
            I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2419
            I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2420
            break;
2420
            break;
2421
        }
2421
        }
2422
    }
2422
    }
2423
    if (tries == 5)
2423
    if (tries == 5)
2424
        DRM_ERROR("FDI train 1 fail!\n");
2424
        DRM_ERROR("FDI train 1 fail!\n");
2425
 
2425
 
2426
    /* Train 2 */
2426
    /* Train 2 */
2427
    reg = FDI_TX_CTL(pipe);
2427
    reg = FDI_TX_CTL(pipe);
2428
    temp = I915_READ(reg);
2428
    temp = I915_READ(reg);
2429
    temp &= ~FDI_LINK_TRAIN_NONE;
2429
    temp &= ~FDI_LINK_TRAIN_NONE;
2430
    temp |= FDI_LINK_TRAIN_PATTERN_2;
2430
    temp |= FDI_LINK_TRAIN_PATTERN_2;
2431
    I915_WRITE(reg, temp);
2431
    I915_WRITE(reg, temp);
2432
 
2432
 
2433
    reg = FDI_RX_CTL(pipe);
2433
    reg = FDI_RX_CTL(pipe);
2434
    temp = I915_READ(reg);
2434
    temp = I915_READ(reg);
2435
    temp &= ~FDI_LINK_TRAIN_NONE;
2435
    temp &= ~FDI_LINK_TRAIN_NONE;
2436
    temp |= FDI_LINK_TRAIN_PATTERN_2;
2436
    temp |= FDI_LINK_TRAIN_PATTERN_2;
2437
    I915_WRITE(reg, temp);
2437
    I915_WRITE(reg, temp);
2438
 
2438
 
2439
    POSTING_READ(reg);
2439
    POSTING_READ(reg);
2440
    udelay(150);
2440
    udelay(150);
2441
 
2441
 
2442
    reg = FDI_RX_IIR(pipe);
2442
    reg = FDI_RX_IIR(pipe);
2443
    for (tries = 0; tries < 5; tries++) {
2443
    for (tries = 0; tries < 5; tries++) {
2444
        temp = I915_READ(reg);
2444
        temp = I915_READ(reg);
2445
        DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2445
        DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2446
 
2446
 
2447
        if (temp & FDI_RX_SYMBOL_LOCK) {
2447
        if (temp & FDI_RX_SYMBOL_LOCK) {
2448
            I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2448
            I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2449
            DRM_DEBUG_KMS("FDI train 2 done.\n");
2449
            DRM_DEBUG_KMS("FDI train 2 done.\n");
2450
            break;
2450
            break;
2451
        }
2451
        }
2452
    }
2452
    }
2453
    if (tries == 5)
2453
    if (tries == 5)
2454
        DRM_ERROR("FDI train 2 fail!\n");
2454
        DRM_ERROR("FDI train 2 fail!\n");
2455
 
2455
 
2456
    DRM_DEBUG_KMS("FDI train done\n");
2456
    DRM_DEBUG_KMS("FDI train done\n");
2457
 
2457
 
2458
}
2458
}
2459
 
2459
 
2460
static const int snb_b_fdi_train_param[] = {
2460
static const int snb_b_fdi_train_param[] = {
2461
    FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2461
    FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2462
    FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2462
    FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2463
    FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2463
    FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2464
    FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2464
    FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2465
};
2465
};
2466
 
2466
 
2467
/* The FDI link training functions for SNB/Cougarpoint. */
2467
/* The FDI link training functions for SNB/Cougarpoint. */
2468
static void gen6_fdi_link_train(struct drm_crtc *crtc)
2468
static void gen6_fdi_link_train(struct drm_crtc *crtc)
2469
{
2469
{
2470
    struct drm_device *dev = crtc->dev;
2470
    struct drm_device *dev = crtc->dev;
2471
    struct drm_i915_private *dev_priv = dev->dev_private;
2471
    struct drm_i915_private *dev_priv = dev->dev_private;
2472
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2472
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2473
    int pipe = intel_crtc->pipe;
2473
    int pipe = intel_crtc->pipe;
2474
	u32 reg, temp, i, retry;
2474
	u32 reg, temp, i, retry;
2475
 
2475
 
2476
    /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2476
    /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2477
       for train result */
2477
       for train result */
2478
    reg = FDI_RX_IMR(pipe);
2478
    reg = FDI_RX_IMR(pipe);
2479
    temp = I915_READ(reg);
2479
    temp = I915_READ(reg);
2480
    temp &= ~FDI_RX_SYMBOL_LOCK;
2480
    temp &= ~FDI_RX_SYMBOL_LOCK;
2481
    temp &= ~FDI_RX_BIT_LOCK;
2481
    temp &= ~FDI_RX_BIT_LOCK;
2482
    I915_WRITE(reg, temp);
2482
    I915_WRITE(reg, temp);
2483
 
2483
 
2484
    POSTING_READ(reg);
2484
    POSTING_READ(reg);
2485
    udelay(150);
2485
    udelay(150);
2486
 
2486
 
2487
    /* enable CPU FDI TX and PCH FDI RX */
2487
    /* enable CPU FDI TX and PCH FDI RX */
2488
    reg = FDI_TX_CTL(pipe);
2488
    reg = FDI_TX_CTL(pipe);
2489
    temp = I915_READ(reg);
2489
    temp = I915_READ(reg);
2490
	temp &= ~FDI_DP_PORT_WIDTH_MASK;
2490
	temp &= ~FDI_DP_PORT_WIDTH_MASK;
2491
	temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
2491
	temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
2492
    temp &= ~FDI_LINK_TRAIN_NONE;
2492
    temp &= ~FDI_LINK_TRAIN_NONE;
2493
    temp |= FDI_LINK_TRAIN_PATTERN_1;
2493
    temp |= FDI_LINK_TRAIN_PATTERN_1;
2494
    temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2494
    temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2495
    /* SNB-B */
2495
    /* SNB-B */
2496
    temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2496
    temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2497
    I915_WRITE(reg, temp | FDI_TX_ENABLE);
2497
    I915_WRITE(reg, temp | FDI_TX_ENABLE);
2498
 
2498
 
2499
	I915_WRITE(FDI_RX_MISC(pipe),
2499
	I915_WRITE(FDI_RX_MISC(pipe),
2500
		   FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2500
		   FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2501
 
2501
 
2502
    reg = FDI_RX_CTL(pipe);
2502
    reg = FDI_RX_CTL(pipe);
2503
    temp = I915_READ(reg);
2503
    temp = I915_READ(reg);
2504
    if (HAS_PCH_CPT(dev)) {
2504
    if (HAS_PCH_CPT(dev)) {
2505
        temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2505
        temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2506
        temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2506
        temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2507
    } else {
2507
    } else {
2508
        temp &= ~FDI_LINK_TRAIN_NONE;
2508
        temp &= ~FDI_LINK_TRAIN_NONE;
2509
        temp |= FDI_LINK_TRAIN_PATTERN_1;
2509
        temp |= FDI_LINK_TRAIN_PATTERN_1;
2510
    }
2510
    }
2511
    I915_WRITE(reg, temp | FDI_RX_ENABLE);
2511
    I915_WRITE(reg, temp | FDI_RX_ENABLE);
2512
 
2512
 
2513
    POSTING_READ(reg);
2513
    POSTING_READ(reg);
2514
    udelay(150);
2514
    udelay(150);
2515
 
2515
 
2516
	for (i = 0; i < 4; i++) {
2516
	for (i = 0; i < 4; i++) {
2517
        reg = FDI_TX_CTL(pipe);
2517
        reg = FDI_TX_CTL(pipe);
2518
        temp = I915_READ(reg);
2518
        temp = I915_READ(reg);
2519
        temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2519
        temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2520
        temp |= snb_b_fdi_train_param[i];
2520
        temp |= snb_b_fdi_train_param[i];
2521
        I915_WRITE(reg, temp);
2521
        I915_WRITE(reg, temp);
2522
 
2522
 
2523
        POSTING_READ(reg);
2523
        POSTING_READ(reg);
2524
        udelay(500);
2524
        udelay(500);
2525
 
2525
 
2526
		for (retry = 0; retry < 5; retry++) {
2526
		for (retry = 0; retry < 5; retry++) {
2527
        reg = FDI_RX_IIR(pipe);
2527
        reg = FDI_RX_IIR(pipe);
2528
        temp = I915_READ(reg);
2528
        temp = I915_READ(reg);
2529
        DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2529
        DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2530
        if (temp & FDI_RX_BIT_LOCK) {
2530
        if (temp & FDI_RX_BIT_LOCK) {
2531
            I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2531
            I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2532
            DRM_DEBUG_KMS("FDI train 1 done.\n");
2532
            DRM_DEBUG_KMS("FDI train 1 done.\n");
2533
            break;
2533
            break;
2534
        }
2534
        }
2535
			udelay(50);
2535
			udelay(50);
2536
		}
2536
		}
2537
		if (retry < 5)
2537
		if (retry < 5)
2538
			break;
2538
			break;
2539
    }
2539
    }
2540
    if (i == 4)
2540
    if (i == 4)
2541
        DRM_ERROR("FDI train 1 fail!\n");
2541
        DRM_ERROR("FDI train 1 fail!\n");
2542
 
2542
 
2543
    /* Train 2 */
2543
    /* Train 2 */
2544
    reg = FDI_TX_CTL(pipe);
2544
    reg = FDI_TX_CTL(pipe);
2545
    temp = I915_READ(reg);
2545
    temp = I915_READ(reg);
2546
    temp &= ~FDI_LINK_TRAIN_NONE;
2546
    temp &= ~FDI_LINK_TRAIN_NONE;
2547
    temp |= FDI_LINK_TRAIN_PATTERN_2;
2547
    temp |= FDI_LINK_TRAIN_PATTERN_2;
2548
    if (IS_GEN6(dev)) {
2548
    if (IS_GEN6(dev)) {
2549
        temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2549
        temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2550
        /* SNB-B */
2550
        /* SNB-B */
2551
        temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2551
        temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2552
    }
2552
    }
2553
    I915_WRITE(reg, temp);
2553
    I915_WRITE(reg, temp);
2554
 
2554
 
2555
    reg = FDI_RX_CTL(pipe);
2555
    reg = FDI_RX_CTL(pipe);
2556
    temp = I915_READ(reg);
2556
    temp = I915_READ(reg);
2557
    if (HAS_PCH_CPT(dev)) {
2557
    if (HAS_PCH_CPT(dev)) {
2558
        temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2558
        temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2559
        temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2559
        temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2560
    } else {
2560
    } else {
2561
        temp &= ~FDI_LINK_TRAIN_NONE;
2561
        temp &= ~FDI_LINK_TRAIN_NONE;
2562
        temp |= FDI_LINK_TRAIN_PATTERN_2;
2562
        temp |= FDI_LINK_TRAIN_PATTERN_2;
2563
    }
2563
    }
2564
    I915_WRITE(reg, temp);
2564
    I915_WRITE(reg, temp);
2565
 
2565
 
2566
    POSTING_READ(reg);
2566
    POSTING_READ(reg);
2567
    udelay(150);
2567
    udelay(150);
2568
 
2568
 
2569
	for (i = 0; i < 4; i++) {
2569
	for (i = 0; i < 4; i++) {
2570
        reg = FDI_TX_CTL(pipe);
2570
        reg = FDI_TX_CTL(pipe);
2571
        temp = I915_READ(reg);
2571
        temp = I915_READ(reg);
2572
        temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2572
        temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2573
        temp |= snb_b_fdi_train_param[i];
2573
        temp |= snb_b_fdi_train_param[i];
2574
        I915_WRITE(reg, temp);
2574
        I915_WRITE(reg, temp);
2575
 
2575
 
2576
        POSTING_READ(reg);
2576
        POSTING_READ(reg);
2577
        udelay(500);
2577
        udelay(500);
2578
 
2578
 
2579
		for (retry = 0; retry < 5; retry++) {
2579
		for (retry = 0; retry < 5; retry++) {
2580
        reg = FDI_RX_IIR(pipe);
2580
        reg = FDI_RX_IIR(pipe);
2581
        temp = I915_READ(reg);
2581
        temp = I915_READ(reg);
2582
        DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2582
        DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2583
        if (temp & FDI_RX_SYMBOL_LOCK) {
2583
        if (temp & FDI_RX_SYMBOL_LOCK) {
2584
            I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2584
            I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2585
            DRM_DEBUG_KMS("FDI train 2 done.\n");
2585
            DRM_DEBUG_KMS("FDI train 2 done.\n");
2586
            break;
2586
            break;
2587
        }
2587
        }
2588
			udelay(50);
2588
			udelay(50);
2589
		}
2589
		}
2590
		if (retry < 5)
2590
		if (retry < 5)
2591
			break;
2591
			break;
2592
    }
2592
    }
2593
    if (i == 4)
2593
    if (i == 4)
2594
        DRM_ERROR("FDI train 2 fail!\n");
2594
        DRM_ERROR("FDI train 2 fail!\n");
2595
 
2595
 
2596
    DRM_DEBUG_KMS("FDI train done.\n");
2596
    DRM_DEBUG_KMS("FDI train done.\n");
2597
}
2597
}
2598
 
2598
 
2599
/* Manual link training for Ivy Bridge A0 parts */
2599
/* Manual link training for Ivy Bridge A0 parts */
2600
static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2600
static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2601
{
2601
{
2602
    struct drm_device *dev = crtc->dev;
2602
    struct drm_device *dev = crtc->dev;
2603
    struct drm_i915_private *dev_priv = dev->dev_private;
2603
    struct drm_i915_private *dev_priv = dev->dev_private;
2604
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2604
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2605
    int pipe = intel_crtc->pipe;
2605
    int pipe = intel_crtc->pipe;
2606
	u32 reg, temp, i, j;
2606
	u32 reg, temp, i, j;
2607
 
2607
 
2608
    /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2608
    /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2609
       for train result */
2609
       for train result */
2610
    reg = FDI_RX_IMR(pipe);
2610
    reg = FDI_RX_IMR(pipe);
2611
    temp = I915_READ(reg);
2611
    temp = I915_READ(reg);
2612
    temp &= ~FDI_RX_SYMBOL_LOCK;
2612
    temp &= ~FDI_RX_SYMBOL_LOCK;
2613
    temp &= ~FDI_RX_BIT_LOCK;
2613
    temp &= ~FDI_RX_BIT_LOCK;
2614
    I915_WRITE(reg, temp);
2614
    I915_WRITE(reg, temp);
2615
 
2615
 
2616
    POSTING_READ(reg);
2616
    POSTING_READ(reg);
2617
    udelay(150);
2617
    udelay(150);
2618
 
2618
 
2619
	DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
2619
	DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
2620
		      I915_READ(FDI_RX_IIR(pipe)));
2620
		      I915_READ(FDI_RX_IIR(pipe)));
2621
 
2621
 
2622
	/* Try each vswing and preemphasis setting twice before moving on */
2622
	/* Try each vswing and preemphasis setting twice before moving on */
2623
	for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
2623
	for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
2624
		/* disable first in case we need to retry */
2624
		/* disable first in case we need to retry */
2625
		reg = FDI_TX_CTL(pipe);
2625
		reg = FDI_TX_CTL(pipe);
2626
		temp = I915_READ(reg);
2626
		temp = I915_READ(reg);
2627
		temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2627
		temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2628
		temp &= ~FDI_TX_ENABLE;
2628
		temp &= ~FDI_TX_ENABLE;
2629
		I915_WRITE(reg, temp);
2629
		I915_WRITE(reg, temp);
2630
 
2630
 
2631
		reg = FDI_RX_CTL(pipe);
2631
		reg = FDI_RX_CTL(pipe);
2632
		temp = I915_READ(reg);
2632
		temp = I915_READ(reg);
2633
		temp &= ~FDI_LINK_TRAIN_AUTO;
2633
		temp &= ~FDI_LINK_TRAIN_AUTO;
2634
		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2634
		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2635
		temp &= ~FDI_RX_ENABLE;
2635
		temp &= ~FDI_RX_ENABLE;
2636
		I915_WRITE(reg, temp);
2636
		I915_WRITE(reg, temp);
2637
 
2637
 
2638
    /* enable CPU FDI TX and PCH FDI RX */
2638
    /* enable CPU FDI TX and PCH FDI RX */
2639
    reg = FDI_TX_CTL(pipe);
2639
    reg = FDI_TX_CTL(pipe);
2640
    temp = I915_READ(reg);
2640
    temp = I915_READ(reg);
2641
	temp &= ~FDI_DP_PORT_WIDTH_MASK;
2641
	temp &= ~FDI_DP_PORT_WIDTH_MASK;
2642
	temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
2642
	temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
2643
    temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
2643
    temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
2644
    temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2644
    temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2645
		temp |= snb_b_fdi_train_param[j/2];
2645
		temp |= snb_b_fdi_train_param[j/2];
2646
	temp |= FDI_COMPOSITE_SYNC;
2646
	temp |= FDI_COMPOSITE_SYNC;
2647
    I915_WRITE(reg, temp | FDI_TX_ENABLE);
2647
    I915_WRITE(reg, temp | FDI_TX_ENABLE);
2648
 
2648
 
2649
	I915_WRITE(FDI_RX_MISC(pipe),
2649
	I915_WRITE(FDI_RX_MISC(pipe),
2650
		   FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2650
		   FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2651
 
2651
 
2652
    reg = FDI_RX_CTL(pipe);
2652
    reg = FDI_RX_CTL(pipe);
2653
    temp = I915_READ(reg);
2653
    temp = I915_READ(reg);
2654
    temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2654
    temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2655
	temp |= FDI_COMPOSITE_SYNC;
2655
	temp |= FDI_COMPOSITE_SYNC;
2656
    I915_WRITE(reg, temp | FDI_RX_ENABLE);
2656
    I915_WRITE(reg, temp | FDI_RX_ENABLE);
2657
 
2657
 
2658
    POSTING_READ(reg);
2658
    POSTING_READ(reg);
2659
		udelay(1); /* should be 0.5us */
2659
		udelay(1); /* should be 0.5us */
2660
 
2660
 
2661
	for (i = 0; i < 4; i++) {
2661
	for (i = 0; i < 4; i++) {
2662
        reg = FDI_RX_IIR(pipe);
2662
        reg = FDI_RX_IIR(pipe);
2663
        temp = I915_READ(reg);
2663
        temp = I915_READ(reg);
2664
        DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2664
        DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2665
 
2665
 
2666
        if (temp & FDI_RX_BIT_LOCK ||
2666
        if (temp & FDI_RX_BIT_LOCK ||
2667
            (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2667
            (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2668
            I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2668
            I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2669
				DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
2669
				DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
2670
					      i);
2670
					      i);
2671
            break;
2671
            break;
2672
        }
2672
        }
2673
			udelay(1); /* should be 0.5us */
2673
			udelay(1); /* should be 0.5us */
2674
		}
2674
		}
2675
		if (i == 4) {
2675
		if (i == 4) {
2676
			DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
2676
			DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
2677
			continue;
2677
			continue;
2678
    }
2678
    }
2679
 
2679
 
2680
    /* Train 2 */
2680
    /* Train 2 */
2681
    reg = FDI_TX_CTL(pipe);
2681
    reg = FDI_TX_CTL(pipe);
2682
    temp = I915_READ(reg);
2682
    temp = I915_READ(reg);
2683
    temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2683
    temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2684
    temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2684
    temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2685
    I915_WRITE(reg, temp);
2685
    I915_WRITE(reg, temp);
2686
 
2686
 
2687
    reg = FDI_RX_CTL(pipe);
2687
    reg = FDI_RX_CTL(pipe);
2688
    temp = I915_READ(reg);
2688
    temp = I915_READ(reg);
2689
    temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2689
    temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2690
    temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2690
    temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2691
    I915_WRITE(reg, temp);
2691
    I915_WRITE(reg, temp);
2692
 
2692
 
2693
    POSTING_READ(reg);
2693
    POSTING_READ(reg);
2694
		udelay(2); /* should be 1.5us */
2694
		udelay(2); /* should be 1.5us */
2695
 
2695
 
2696
	for (i = 0; i < 4; i++) {
2696
	for (i = 0; i < 4; i++) {
2697
        reg = FDI_RX_IIR(pipe);
2697
        reg = FDI_RX_IIR(pipe);
2698
        temp = I915_READ(reg);
2698
        temp = I915_READ(reg);
2699
        DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2699
        DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2700
 
2700
 
2701
			if (temp & FDI_RX_SYMBOL_LOCK ||
2701
			if (temp & FDI_RX_SYMBOL_LOCK ||
2702
			    (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
2702
			    (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
2703
            I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2703
            I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2704
				DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
2704
				DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
2705
					      i);
2705
					      i);
2706
				goto train_done;
2706
				goto train_done;
2707
        }
2707
        }
2708
			udelay(2); /* should be 1.5us */
2708
			udelay(2); /* should be 1.5us */
2709
    }
2709
    }
2710
    if (i == 4)
2710
    if (i == 4)
2711
			DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
2711
			DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
2712
	}
2712
	}
2713
 
2713
 
2714
train_done:
2714
train_done:
2715
    DRM_DEBUG_KMS("FDI train done.\n");
2715
    DRM_DEBUG_KMS("FDI train done.\n");
2716
}
2716
}
2717
 
2717
 
2718
static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2718
static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2719
{
2719
{
2720
	struct drm_device *dev = intel_crtc->base.dev;
2720
	struct drm_device *dev = intel_crtc->base.dev;
2721
	struct drm_i915_private *dev_priv = dev->dev_private;
2721
	struct drm_i915_private *dev_priv = dev->dev_private;
2722
	int pipe = intel_crtc->pipe;
2722
	int pipe = intel_crtc->pipe;
2723
	u32 reg, temp;
2723
	u32 reg, temp;
2724
 
2724
 
2725
 
2725
 
2726
	/* enable PCH FDI RX PLL, wait warmup plus DMI latency */
2726
	/* enable PCH FDI RX PLL, wait warmup plus DMI latency */
2727
	reg = FDI_RX_CTL(pipe);
2727
	reg = FDI_RX_CTL(pipe);
2728
	temp = I915_READ(reg);
2728
	temp = I915_READ(reg);
2729
	temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
2729
	temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
2730
	temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
2730
	temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
2731
	temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
2731
	temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
2732
	I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2732
	I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2733
 
2733
 
2734
	POSTING_READ(reg);
2734
	POSTING_READ(reg);
2735
	udelay(200);
2735
	udelay(200);
2736
 
2736
 
2737
	/* Switch from Rawclk to PCDclk */
2737
	/* Switch from Rawclk to PCDclk */
2738
	temp = I915_READ(reg);
2738
	temp = I915_READ(reg);
2739
	I915_WRITE(reg, temp | FDI_PCDCLK);
2739
	I915_WRITE(reg, temp | FDI_PCDCLK);
2740
 
2740
 
2741
	POSTING_READ(reg);
2741
	POSTING_READ(reg);
2742
	udelay(200);
2742
	udelay(200);
2743
 
2743
 
2744
	/* Enable CPU FDI TX PLL, always on for Ironlake */
2744
	/* Enable CPU FDI TX PLL, always on for Ironlake */
2745
	reg = FDI_TX_CTL(pipe);
2745
	reg = FDI_TX_CTL(pipe);
2746
	temp = I915_READ(reg);
2746
	temp = I915_READ(reg);
2747
	if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2747
	if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2748
		I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
2748
		I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
2749
 
2749
 
2750
		POSTING_READ(reg);
2750
		POSTING_READ(reg);
2751
		udelay(100);
2751
		udelay(100);
2752
	}
2752
	}
2753
}
2753
}
2754
 
2754
 
2755
static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
2755
static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
2756
{
2756
{
2757
	struct drm_device *dev = intel_crtc->base.dev;
2757
	struct drm_device *dev = intel_crtc->base.dev;
2758
	struct drm_i915_private *dev_priv = dev->dev_private;
2758
	struct drm_i915_private *dev_priv = dev->dev_private;
2759
	int pipe = intel_crtc->pipe;
2759
	int pipe = intel_crtc->pipe;
2760
	u32 reg, temp;
2760
	u32 reg, temp;
2761
 
2761
 
2762
	/* Switch from PCDclk to Rawclk */
2762
	/* Switch from PCDclk to Rawclk */
2763
	reg = FDI_RX_CTL(pipe);
2763
	reg = FDI_RX_CTL(pipe);
2764
	temp = I915_READ(reg);
2764
	temp = I915_READ(reg);
2765
	I915_WRITE(reg, temp & ~FDI_PCDCLK);
2765
	I915_WRITE(reg, temp & ~FDI_PCDCLK);
2766
 
2766
 
2767
	/* Disable CPU FDI TX PLL */
2767
	/* Disable CPU FDI TX PLL */
2768
	reg = FDI_TX_CTL(pipe);
2768
	reg = FDI_TX_CTL(pipe);
2769
	temp = I915_READ(reg);
2769
	temp = I915_READ(reg);
2770
	I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2770
	I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2771
 
2771
 
2772
	POSTING_READ(reg);
2772
	POSTING_READ(reg);
2773
	udelay(100);
2773
	udelay(100);
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
	I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2777
	I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2778
 
2778
 
2779
	/* Wait for the clocks to turn off. */
2779
	/* Wait for the clocks to turn off. */
2780
	POSTING_READ(reg);
2780
	POSTING_READ(reg);
2781
	udelay(100);
2781
	udelay(100);
2782
}
2782
}
2783
 
2783
 
2784
static void ironlake_fdi_disable(struct drm_crtc *crtc)
2784
static void ironlake_fdi_disable(struct drm_crtc *crtc)
2785
{
2785
{
2786
	struct drm_device *dev = crtc->dev;
2786
	struct drm_device *dev = crtc->dev;
2787
	struct drm_i915_private *dev_priv = dev->dev_private;
2787
	struct drm_i915_private *dev_priv = dev->dev_private;
2788
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2788
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2789
	int pipe = intel_crtc->pipe;
2789
	int pipe = intel_crtc->pipe;
2790
	u32 reg, temp;
2790
	u32 reg, temp;
2791
 
2791
 
2792
	/* disable CPU FDI tx and PCH FDI rx */
2792
	/* disable CPU FDI tx and PCH FDI rx */
2793
	reg = FDI_TX_CTL(pipe);
2793
	reg = FDI_TX_CTL(pipe);
2794
	temp = I915_READ(reg);
2794
	temp = I915_READ(reg);
2795
	I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2795
	I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2796
	POSTING_READ(reg);
2796
	POSTING_READ(reg);
2797
 
2797
 
2798
	reg = FDI_RX_CTL(pipe);
2798
	reg = FDI_RX_CTL(pipe);
2799
	temp = I915_READ(reg);
2799
	temp = I915_READ(reg);
2800
	temp &= ~(0x7 << 16);
2800
	temp &= ~(0x7 << 16);
2801
	temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
2801
	temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
2802
	I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2802
	I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2803
 
2803
 
2804
	POSTING_READ(reg);
2804
	POSTING_READ(reg);
2805
	udelay(100);
2805
	udelay(100);
2806
 
2806
 
2807
	/* Ironlake workaround, disable clock pointer after downing FDI */
2807
	/* Ironlake workaround, disable clock pointer after downing FDI */
2808
	if (HAS_PCH_IBX(dev)) {
2808
	if (HAS_PCH_IBX(dev)) {
2809
		I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2809
		I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2810
	}
2810
	}
2811
 
2811
 
2812
	/* still set train pattern 1 */
2812
	/* still set train pattern 1 */
2813
	reg = FDI_TX_CTL(pipe);
2813
	reg = FDI_TX_CTL(pipe);
2814
	temp = I915_READ(reg);
2814
	temp = I915_READ(reg);
2815
	temp &= ~FDI_LINK_TRAIN_NONE;
2815
	temp &= ~FDI_LINK_TRAIN_NONE;
2816
	temp |= FDI_LINK_TRAIN_PATTERN_1;
2816
	temp |= FDI_LINK_TRAIN_PATTERN_1;
2817
	I915_WRITE(reg, temp);
2817
	I915_WRITE(reg, temp);
2818
 
2818
 
2819
	reg = FDI_RX_CTL(pipe);
2819
	reg = FDI_RX_CTL(pipe);
2820
	temp = I915_READ(reg);
2820
	temp = I915_READ(reg);
2821
	if (HAS_PCH_CPT(dev)) {
2821
	if (HAS_PCH_CPT(dev)) {
2822
		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2822
		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2823
		temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2823
		temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2824
	} else {
2824
	} else {
2825
		temp &= ~FDI_LINK_TRAIN_NONE;
2825
		temp &= ~FDI_LINK_TRAIN_NONE;
2826
		temp |= FDI_LINK_TRAIN_PATTERN_1;
2826
		temp |= FDI_LINK_TRAIN_PATTERN_1;
2827
	}
2827
	}
2828
	/* BPC in FDI rx is consistent with that in PIPECONF */
2828
	/* BPC in FDI rx is consistent with that in PIPECONF */
2829
	temp &= ~(0x07 << 16);
2829
	temp &= ~(0x07 << 16);
2830
	temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
2830
	temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
2831
	I915_WRITE(reg, temp);
2831
	I915_WRITE(reg, temp);
2832
 
2832
 
2833
	POSTING_READ(reg);
2833
	POSTING_READ(reg);
2834
	udelay(100);
2834
	udelay(100);
2835
}
2835
}
2836
 
2836
 
2837
static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2837
static bool intel_crtc_has_pending_flip(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
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2841
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2842
	unsigned long flags;
2842
	unsigned long flags;
2843
	bool pending;
2843
	bool pending;
2844
 
2844
 
2845
	if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2845
	if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2846
	    intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
2846
	    intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
2847
		return false;
2847
		return false;
2848
 
2848
 
2849
	spin_lock_irqsave(&dev->event_lock, flags);
2849
	spin_lock_irqsave(&dev->event_lock, flags);
2850
	pending = to_intel_crtc(crtc)->unpin_work != NULL;
2850
	pending = to_intel_crtc(crtc)->unpin_work != NULL;
2851
	spin_unlock_irqrestore(&dev->event_lock, flags);
2851
	spin_unlock_irqrestore(&dev->event_lock, flags);
2852
 
2852
 
2853
	return pending;
2853
	return pending;
2854
}
2854
}
2855
 
2855
 
2856
#if 0
2856
#if 0
2857
static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2857
static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2858
{
2858
{
2859
	struct drm_device *dev = crtc->dev;
2859
	struct drm_device *dev = crtc->dev;
2860
	struct drm_i915_private *dev_priv = dev->dev_private;
2860
	struct drm_i915_private *dev_priv = dev->dev_private;
2861
 
2861
 
2862
	if (crtc->fb == NULL)
2862
	if (crtc->fb == NULL)
2863
		return;
2863
		return;
2864
 
2864
 
2865
	WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
2865
	WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
2866
 
2866
 
2867
	wait_event(dev_priv->pending_flip_queue,
2867
	wait_event(dev_priv->pending_flip_queue,
2868
		   !intel_crtc_has_pending_flip(crtc));
2868
		   !intel_crtc_has_pending_flip(crtc));
2869
 
2869
 
2870
	mutex_lock(&dev->struct_mutex);
2870
	mutex_lock(&dev->struct_mutex);
2871
	intel_finish_fb(crtc->fb);
2871
	intel_finish_fb(crtc->fb);
2872
	mutex_unlock(&dev->struct_mutex);
2872
	mutex_unlock(&dev->struct_mutex);
2873
}
2873
}
2874
#endif
2874
#endif
2875
 
2875
 
2876
/* Program iCLKIP clock to the desired frequency */
2876
/* Program iCLKIP clock to the desired frequency */
2877
static void lpt_program_iclkip(struct drm_crtc *crtc)
2877
static void lpt_program_iclkip(struct drm_crtc *crtc)
2878
{
2878
{
2879
	struct drm_device *dev = crtc->dev;
2879
	struct drm_device *dev = crtc->dev;
2880
	struct drm_i915_private *dev_priv = dev->dev_private;
2880
	struct drm_i915_private *dev_priv = dev->dev_private;
2881
	u32 divsel, phaseinc, auxdiv, phasedir = 0;
2881
	u32 divsel, phaseinc, auxdiv, phasedir = 0;
2882
	u32 temp;
2882
	u32 temp;
2883
 
2883
 
2884
	mutex_lock(&dev_priv->dpio_lock);
2884
	mutex_lock(&dev_priv->dpio_lock);
2885
 
2885
 
2886
	/* It is necessary to ungate the pixclk gate prior to programming
2886
	/* It is necessary to ungate the pixclk gate prior to programming
2887
	 * the divisors, and gate it back when it is done.
2887
	 * the divisors, and gate it back when it is done.
2888
	 */
2888
	 */
2889
	I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
2889
	I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
2890
 
2890
 
2891
	/* Disable SSCCTL */
2891
	/* Disable SSCCTL */
2892
	intel_sbi_write(dev_priv, SBI_SSCCTL6,
2892
	intel_sbi_write(dev_priv, SBI_SSCCTL6,
2893
			intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
2893
			intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
2894
				SBI_SSCCTL_DISABLE,
2894
				SBI_SSCCTL_DISABLE,
2895
			SBI_ICLK);
2895
			SBI_ICLK);
2896
 
2896
 
2897
	/* 20MHz is a corner case which is out of range for the 7-bit divisor */
2897
	/* 20MHz is a corner case which is out of range for the 7-bit divisor */
2898
	if (crtc->mode.clock == 20000) {
2898
	if (crtc->mode.clock == 20000) {
2899
		auxdiv = 1;
2899
		auxdiv = 1;
2900
		divsel = 0x41;
2900
		divsel = 0x41;
2901
		phaseinc = 0x20;
2901
		phaseinc = 0x20;
2902
	} else {
2902
	} else {
2903
		/* The iCLK virtual clock root frequency is in MHz,
2903
		/* The iCLK virtual clock root frequency is in MHz,
2904
		 * but the crtc->mode.clock in in KHz. To get the divisors,
2904
		 * but the crtc->mode.clock in in KHz. To get the divisors,
2905
		 * it is necessary to divide one by another, so we
2905
		 * it is necessary to divide one by another, so we
2906
		 * convert the virtual clock precision to KHz here for higher
2906
		 * convert the virtual clock precision to KHz here for higher
2907
		 * precision.
2907
		 * precision.
2908
		 */
2908
		 */
2909
		u32 iclk_virtual_root_freq = 172800 * 1000;
2909
		u32 iclk_virtual_root_freq = 172800 * 1000;
2910
		u32 iclk_pi_range = 64;
2910
		u32 iclk_pi_range = 64;
2911
		u32 desired_divisor, msb_divisor_value, pi_value;
2911
		u32 desired_divisor, msb_divisor_value, pi_value;
2912
 
2912
 
2913
		desired_divisor = (iclk_virtual_root_freq / crtc->mode.clock);
2913
		desired_divisor = (iclk_virtual_root_freq / crtc->mode.clock);
2914
		msb_divisor_value = desired_divisor / iclk_pi_range;
2914
		msb_divisor_value = desired_divisor / iclk_pi_range;
2915
		pi_value = desired_divisor % iclk_pi_range;
2915
		pi_value = desired_divisor % iclk_pi_range;
2916
 
2916
 
2917
		auxdiv = 0;
2917
		auxdiv = 0;
2918
		divsel = msb_divisor_value - 2;
2918
		divsel = msb_divisor_value - 2;
2919
		phaseinc = pi_value;
2919
		phaseinc = pi_value;
2920
	}
2920
	}
2921
 
2921
 
2922
	/* This should not happen with any sane values */
2922
	/* This should not happen with any sane values */
2923
	WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
2923
	WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
2924
		~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
2924
		~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
2925
	WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
2925
	WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
2926
		~SBI_SSCDIVINTPHASE_INCVAL_MASK);
2926
		~SBI_SSCDIVINTPHASE_INCVAL_MASK);
2927
 
2927
 
2928
	DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
2928
	DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
2929
			crtc->mode.clock,
2929
			crtc->mode.clock,
2930
			auxdiv,
2930
			auxdiv,
2931
			divsel,
2931
			divsel,
2932
			phasedir,
2932
			phasedir,
2933
			phaseinc);
2933
			phaseinc);
2934
 
2934
 
2935
	/* Program SSCDIVINTPHASE6 */
2935
	/* Program SSCDIVINTPHASE6 */
2936
	temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
2936
	temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
2937
	temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
2937
	temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
2938
	temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
2938
	temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
2939
	temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
2939
	temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
2940
	temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
2940
	temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
2941
	temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
2941
	temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
2942
	temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
2942
	temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
2943
	intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
2943
	intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
2944
 
2944
 
2945
	/* Program SSCAUXDIV */
2945
	/* Program SSCAUXDIV */
2946
	temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
2946
	temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
2947
	temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
2947
	temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
2948
	temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
2948
	temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
2949
	intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
2949
	intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
2950
 
2950
 
2951
	/* Enable modulator and associated divider */
2951
	/* Enable modulator and associated divider */
2952
	temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
2952
	temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
2953
	temp &= ~SBI_SSCCTL_DISABLE;
2953
	temp &= ~SBI_SSCCTL_DISABLE;
2954
	intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
2954
	intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
2955
 
2955
 
2956
	/* Wait for initialization time */
2956
	/* Wait for initialization time */
2957
	udelay(24);
2957
	udelay(24);
2958
 
2958
 
2959
	I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
2959
	I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
2960
 
2960
 
2961
	mutex_unlock(&dev_priv->dpio_lock);
2961
	mutex_unlock(&dev_priv->dpio_lock);
2962
}
2962
}
2963
 
2963
 
2964
static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
2964
static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
2965
						enum pipe pch_transcoder)
2965
						enum pipe pch_transcoder)
2966
{
2966
{
2967
	struct drm_device *dev = crtc->base.dev;
2967
	struct drm_device *dev = crtc->base.dev;
2968
	struct drm_i915_private *dev_priv = dev->dev_private;
2968
	struct drm_i915_private *dev_priv = dev->dev_private;
2969
	enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
2969
	enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
2970
 
2970
 
2971
	I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
2971
	I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
2972
		   I915_READ(HTOTAL(cpu_transcoder)));
2972
		   I915_READ(HTOTAL(cpu_transcoder)));
2973
	I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
2973
	I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
2974
		   I915_READ(HBLANK(cpu_transcoder)));
2974
		   I915_READ(HBLANK(cpu_transcoder)));
2975
	I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
2975
	I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
2976
		   I915_READ(HSYNC(cpu_transcoder)));
2976
		   I915_READ(HSYNC(cpu_transcoder)));
2977
 
2977
 
2978
	I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
2978
	I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
2979
		   I915_READ(VTOTAL(cpu_transcoder)));
2979
		   I915_READ(VTOTAL(cpu_transcoder)));
2980
	I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
2980
	I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
2981
		   I915_READ(VBLANK(cpu_transcoder)));
2981
		   I915_READ(VBLANK(cpu_transcoder)));
2982
	I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
2982
	I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
2983
		   I915_READ(VSYNC(cpu_transcoder)));
2983
		   I915_READ(VSYNC(cpu_transcoder)));
2984
	I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
2984
	I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
2985
		   I915_READ(VSYNCSHIFT(cpu_transcoder)));
2985
		   I915_READ(VSYNCSHIFT(cpu_transcoder)));
2986
}
2986
}
2987
 
2987
 
2988
static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
2988
static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
2989
{
2989
{
2990
	struct drm_i915_private *dev_priv = dev->dev_private;
2990
	struct drm_i915_private *dev_priv = dev->dev_private;
2991
	uint32_t temp;
2991
	uint32_t temp;
2992
 
2992
 
2993
	temp = I915_READ(SOUTH_CHICKEN1);
2993
	temp = I915_READ(SOUTH_CHICKEN1);
2994
	if (temp & FDI_BC_BIFURCATION_SELECT)
2994
	if (temp & FDI_BC_BIFURCATION_SELECT)
2995
		return;
2995
		return;
2996
 
2996
 
2997
	WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2997
	WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2998
	WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2998
	WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2999
 
2999
 
3000
	temp |= FDI_BC_BIFURCATION_SELECT;
3000
	temp |= FDI_BC_BIFURCATION_SELECT;
3001
	DRM_DEBUG_KMS("enabling fdi C rx\n");
3001
	DRM_DEBUG_KMS("enabling fdi C rx\n");
3002
	I915_WRITE(SOUTH_CHICKEN1, temp);
3002
	I915_WRITE(SOUTH_CHICKEN1, temp);
3003
	POSTING_READ(SOUTH_CHICKEN1);
3003
	POSTING_READ(SOUTH_CHICKEN1);
3004
}
3004
}
3005
 
3005
 
3006
static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3006
static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3007
{
3007
{
3008
	struct drm_device *dev = intel_crtc->base.dev;
3008
	struct drm_device *dev = intel_crtc->base.dev;
3009
	struct drm_i915_private *dev_priv = dev->dev_private;
3009
	struct drm_i915_private *dev_priv = dev->dev_private;
3010
 
3010
 
3011
	switch (intel_crtc->pipe) {
3011
	switch (intel_crtc->pipe) {
3012
	case PIPE_A:
3012
	case PIPE_A:
3013
		break;
3013
		break;
3014
	case PIPE_B:
3014
	case PIPE_B:
3015
		if (intel_crtc->config.fdi_lanes > 2)
3015
		if (intel_crtc->config.fdi_lanes > 2)
3016
			WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
3016
			WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
3017
		else
3017
		else
3018
			cpt_enable_fdi_bc_bifurcation(dev);
3018
			cpt_enable_fdi_bc_bifurcation(dev);
3019
 
3019
 
3020
		break;
3020
		break;
3021
	case PIPE_C:
3021
	case PIPE_C:
3022
		cpt_enable_fdi_bc_bifurcation(dev);
3022
		cpt_enable_fdi_bc_bifurcation(dev);
3023
 
3023
 
3024
		break;
3024
		break;
3025
	default:
3025
	default:
3026
		BUG();
3026
		BUG();
3027
	}
3027
	}
3028
}
3028
}
3029
 
3029
 
3030
/*
3030
/*
3031
 * Enable PCH resources required for PCH ports:
3031
 * Enable PCH resources required for PCH ports:
3032
 *   - PCH PLLs
3032
 *   - PCH PLLs
3033
 *   - FDI training & RX/TX
3033
 *   - FDI training & RX/TX
3034
 *   - update transcoder timings
3034
 *   - update transcoder timings
3035
 *   - DP transcoding bits
3035
 *   - DP transcoding bits
3036
 *   - transcoder
3036
 *   - transcoder
3037
 */
3037
 */
3038
static void ironlake_pch_enable(struct drm_crtc *crtc)
3038
static void ironlake_pch_enable(struct drm_crtc *crtc)
3039
{
3039
{
3040
	struct drm_device *dev = crtc->dev;
3040
	struct drm_device *dev = crtc->dev;
3041
	struct drm_i915_private *dev_priv = dev->dev_private;
3041
	struct drm_i915_private *dev_priv = dev->dev_private;
3042
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3042
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3043
	int pipe = intel_crtc->pipe;
3043
	int pipe = intel_crtc->pipe;
3044
	u32 reg, temp;
3044
	u32 reg, temp;
3045
 
3045
 
3046
	assert_pch_transcoder_disabled(dev_priv, pipe);
3046
	assert_pch_transcoder_disabled(dev_priv, pipe);
3047
 
3047
 
3048
	if (IS_IVYBRIDGE(dev))
3048
	if (IS_IVYBRIDGE(dev))
3049
		ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3049
		ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3050
 
3050
 
3051
	/* Write the TU size bits before fdi link training, so that error
3051
	/* Write the TU size bits before fdi link training, so that error
3052
	 * detection works. */
3052
	 * detection works. */
3053
	I915_WRITE(FDI_RX_TUSIZE1(pipe),
3053
	I915_WRITE(FDI_RX_TUSIZE1(pipe),
3054
		   I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3054
		   I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3055
 
3055
 
3056
	/* For PCH output, training FDI link */
3056
	/* For PCH output, training FDI link */
3057
	dev_priv->display.fdi_link_train(crtc);
3057
	dev_priv->display.fdi_link_train(crtc);
3058
 
3058
 
3059
	/* We need to program the right clock selection before writing the pixel
3059
	/* We need to program the right clock selection before writing the pixel
3060
	 * mutliplier into the DPLL. */
3060
	 * mutliplier into the DPLL. */
3061
	if (HAS_PCH_CPT(dev)) {
3061
	if (HAS_PCH_CPT(dev)) {
3062
		u32 sel;
3062
		u32 sel;
3063
 
3063
 
3064
		temp = I915_READ(PCH_DPLL_SEL);
3064
		temp = I915_READ(PCH_DPLL_SEL);
3065
		temp |= TRANS_DPLL_ENABLE(pipe);
3065
		temp |= TRANS_DPLL_ENABLE(pipe);
3066
		sel = TRANS_DPLLB_SEL(pipe);
3066
		sel = TRANS_DPLLB_SEL(pipe);
3067
		if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
3067
		if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
3068
			temp |= sel;
3068
			temp |= sel;
3069
		else
3069
		else
3070
			temp &= ~sel;
3070
			temp &= ~sel;
3071
		I915_WRITE(PCH_DPLL_SEL, temp);
3071
		I915_WRITE(PCH_DPLL_SEL, temp);
3072
	}
3072
	}
3073
 
3073
 
3074
	/* XXX: pch pll's can be enabled any time before we enable the PCH
3074
	/* XXX: pch pll's can be enabled any time before we enable the PCH
3075
	 * transcoder, and we actually should do this to not upset any PCH
3075
	 * transcoder, and we actually should do this to not upset any PCH
3076
	 * transcoder that already use the clock when we share it.
3076
	 * transcoder that already use the clock when we share it.
3077
	 *
3077
	 *
3078
	 * Note that enable_shared_dpll tries to do the right thing, but
3078
	 * Note that enable_shared_dpll tries to do the right thing, but
3079
	 * get_shared_dpll unconditionally resets the pll - we need that to have
3079
	 * get_shared_dpll unconditionally resets the pll - we need that to have
3080
	 * the right LVDS enable sequence. */
3080
	 * the right LVDS enable sequence. */
3081
	ironlake_enable_shared_dpll(intel_crtc);
3081
	ironlake_enable_shared_dpll(intel_crtc);
3082
 
3082
 
3083
	/* set transcoder timing, panel must allow it */
3083
	/* set transcoder timing, panel must allow it */
3084
	assert_panel_unlocked(dev_priv, pipe);
3084
	assert_panel_unlocked(dev_priv, pipe);
3085
	ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
3085
	ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
3086
 
3086
 
3087
	intel_fdi_normal_train(crtc);
3087
	intel_fdi_normal_train(crtc);
3088
 
3088
 
3089
	/* For PCH DP, enable TRANS_DP_CTL */
3089
	/* For PCH DP, enable TRANS_DP_CTL */
3090
	if (HAS_PCH_CPT(dev) &&
3090
	if (HAS_PCH_CPT(dev) &&
3091
	    (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3091
	    (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3092
	     intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
3092
	     intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
3093
		u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
3093
		u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
3094
		reg = TRANS_DP_CTL(pipe);
3094
		reg = TRANS_DP_CTL(pipe);
3095
		temp = I915_READ(reg);
3095
		temp = I915_READ(reg);
3096
		temp &= ~(TRANS_DP_PORT_SEL_MASK |
3096
		temp &= ~(TRANS_DP_PORT_SEL_MASK |
3097
			  TRANS_DP_SYNC_MASK |
3097
			  TRANS_DP_SYNC_MASK |
3098
			  TRANS_DP_BPC_MASK);
3098
			  TRANS_DP_BPC_MASK);
3099
		temp |= (TRANS_DP_OUTPUT_ENABLE |
3099
		temp |= (TRANS_DP_OUTPUT_ENABLE |
3100
			 TRANS_DP_ENH_FRAMING);
3100
			 TRANS_DP_ENH_FRAMING);
3101
		temp |= bpc << 9; /* same format but at 11:9 */
3101
		temp |= bpc << 9; /* same format but at 11:9 */
3102
 
3102
 
3103
		if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
3103
		if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
3104
			temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
3104
			temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
3105
		if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
3105
		if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
3106
			temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
3106
			temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
3107
 
3107
 
3108
		switch (intel_trans_dp_port_sel(crtc)) {
3108
		switch (intel_trans_dp_port_sel(crtc)) {
3109
		case PCH_DP_B:
3109
		case PCH_DP_B:
3110
			temp |= TRANS_DP_PORT_SEL_B;
3110
			temp |= TRANS_DP_PORT_SEL_B;
3111
			break;
3111
			break;
3112
		case PCH_DP_C:
3112
		case PCH_DP_C:
3113
			temp |= TRANS_DP_PORT_SEL_C;
3113
			temp |= TRANS_DP_PORT_SEL_C;
3114
			break;
3114
			break;
3115
		case PCH_DP_D:
3115
		case PCH_DP_D:
3116
			temp |= TRANS_DP_PORT_SEL_D;
3116
			temp |= TRANS_DP_PORT_SEL_D;
3117
			break;
3117
			break;
3118
		default:
3118
		default:
3119
			BUG();
3119
			BUG();
3120
		}
3120
		}
3121
 
3121
 
3122
		I915_WRITE(reg, temp);
3122
		I915_WRITE(reg, temp);
3123
	}
3123
	}
3124
 
3124
 
3125
	ironlake_enable_pch_transcoder(dev_priv, pipe);
3125
	ironlake_enable_pch_transcoder(dev_priv, pipe);
3126
}
3126
}
3127
 
3127
 
3128
static void lpt_pch_enable(struct drm_crtc *crtc)
3128
static void lpt_pch_enable(struct drm_crtc *crtc)
3129
{
3129
{
3130
	struct drm_device *dev = crtc->dev;
3130
	struct drm_device *dev = crtc->dev;
3131
	struct drm_i915_private *dev_priv = dev->dev_private;
3131
	struct drm_i915_private *dev_priv = dev->dev_private;
3132
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3132
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3133
	enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
3133
	enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
3134
 
3134
 
3135
	assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
3135
	assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
3136
 
3136
 
3137
	lpt_program_iclkip(crtc);
3137
	lpt_program_iclkip(crtc);
3138
 
3138
 
3139
	/* Set transcoder timing. */
3139
	/* Set transcoder timing. */
3140
	ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
3140
	ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
3141
 
3141
 
3142
	lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
3142
	lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
3143
}
3143
}
3144
 
3144
 
3145
static void intel_put_shared_dpll(struct intel_crtc *crtc)
3145
static void intel_put_shared_dpll(struct intel_crtc *crtc)
3146
{
3146
{
3147
	struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3147
	struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3148
 
3148
 
3149
	if (pll == NULL)
3149
	if (pll == NULL)
3150
		return;
3150
		return;
3151
 
3151
 
3152
	if (pll->refcount == 0) {
3152
	if (pll->refcount == 0) {
3153
		WARN(1, "bad %s refcount\n", pll->name);
3153
		WARN(1, "bad %s refcount\n", pll->name);
3154
		return;
3154
		return;
3155
	}
3155
	}
3156
 
3156
 
3157
	if (--pll->refcount == 0) {
3157
	if (--pll->refcount == 0) {
3158
		WARN_ON(pll->on);
3158
		WARN_ON(pll->on);
3159
		WARN_ON(pll->active);
3159
		WARN_ON(pll->active);
3160
	}
3160
	}
3161
 
3161
 
3162
	crtc->config.shared_dpll = DPLL_ID_PRIVATE;
3162
	crtc->config.shared_dpll = DPLL_ID_PRIVATE;
3163
}
3163
}
3164
 
3164
 
3165
static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
3165
static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
3166
{
3166
{
3167
	struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3167
	struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3168
	struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3168
	struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3169
	enum intel_dpll_id i;
3169
	enum intel_dpll_id i;
3170
 
3170
 
3171
	if (pll) {
3171
	if (pll) {
3172
		DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3172
		DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3173
			      crtc->base.base.id, pll->name);
3173
			      crtc->base.base.id, pll->name);
3174
		intel_put_shared_dpll(crtc);
3174
		intel_put_shared_dpll(crtc);
3175
	}
3175
	}
3176
 
3176
 
3177
	if (HAS_PCH_IBX(dev_priv->dev)) {
3177
	if (HAS_PCH_IBX(dev_priv->dev)) {
3178
		/* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
3178
		/* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
3179
		i = (enum intel_dpll_id) crtc->pipe;
3179
		i = (enum intel_dpll_id) crtc->pipe;
3180
		pll = &dev_priv->shared_dplls[i];
3180
		pll = &dev_priv->shared_dplls[i];
3181
 
3181
 
3182
		DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3182
		DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3183
			      crtc->base.base.id, pll->name);
3183
			      crtc->base.base.id, pll->name);
3184
 
3184
 
3185
		goto found;
3185
		goto found;
3186
	}
3186
	}
3187
 
3187
 
3188
	for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3188
	for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3189
		pll = &dev_priv->shared_dplls[i];
3189
		pll = &dev_priv->shared_dplls[i];
3190
 
3190
 
3191
		/* Only want to check enabled timings first */
3191
		/* Only want to check enabled timings first */
3192
		if (pll->refcount == 0)
3192
		if (pll->refcount == 0)
3193
			continue;
3193
			continue;
3194
 
3194
 
3195
		if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3195
		if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3196
			   sizeof(pll->hw_state)) == 0) {
3196
			   sizeof(pll->hw_state)) == 0) {
3197
			DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
3197
			DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
3198
				      crtc->base.base.id,
3198
				      crtc->base.base.id,
3199
				      pll->name, pll->refcount, pll->active);
3199
				      pll->name, pll->refcount, pll->active);
3200
 
3200
 
3201
			goto found;
3201
			goto found;
3202
		}
3202
		}
3203
	}
3203
	}
3204
 
3204
 
3205
	/* Ok no matching timings, maybe there's a free one? */
3205
	/* Ok no matching timings, maybe there's a free one? */
3206
	for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3206
	for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3207
		pll = &dev_priv->shared_dplls[i];
3207
		pll = &dev_priv->shared_dplls[i];
3208
		if (pll->refcount == 0) {
3208
		if (pll->refcount == 0) {
3209
			DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3209
			DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3210
				      crtc->base.base.id, pll->name);
3210
				      crtc->base.base.id, pll->name);
3211
			goto found;
3211
			goto found;
3212
		}
3212
		}
3213
	}
3213
	}
3214
 
3214
 
3215
	return NULL;
3215
	return NULL;
3216
 
3216
 
3217
found:
3217
found:
3218
	crtc->config.shared_dpll = i;
3218
	crtc->config.shared_dpll = i;
3219
	DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3219
	DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3220
			 pipe_name(crtc->pipe));
3220
			 pipe_name(crtc->pipe));
3221
 
3221
 
3222
	if (pll->active == 0) {
3222
	if (pll->active == 0) {
3223
		memcpy(&pll->hw_state, &crtc->config.dpll_hw_state,
3223
		memcpy(&pll->hw_state, &crtc->config.dpll_hw_state,
3224
		       sizeof(pll->hw_state));
3224
		       sizeof(pll->hw_state));
3225
 
3225
 
3226
		DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
3226
		DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
3227
		WARN_ON(pll->on);
3227
		WARN_ON(pll->on);
3228
		assert_shared_dpll_disabled(dev_priv, pll);
3228
		assert_shared_dpll_disabled(dev_priv, pll);
3229
 
3229
 
3230
		pll->mode_set(dev_priv, pll);
3230
		pll->mode_set(dev_priv, pll);
3231
	}
3231
	}
3232
	pll->refcount++;
3232
	pll->refcount++;
3233
 
3233
 
3234
	return pll;
3234
	return pll;
3235
}
3235
}
3236
 
3236
 
3237
static void cpt_verify_modeset(struct drm_device *dev, int pipe)
3237
static void cpt_verify_modeset(struct drm_device *dev, int pipe)
3238
{
3238
{
3239
	struct drm_i915_private *dev_priv = dev->dev_private;
3239
	struct drm_i915_private *dev_priv = dev->dev_private;
3240
	int dslreg = PIPEDSL(pipe);
3240
	int dslreg = PIPEDSL(pipe);
3241
	u32 temp;
3241
	u32 temp;
3242
 
3242
 
3243
	temp = I915_READ(dslreg);
3243
	temp = I915_READ(dslreg);
3244
	udelay(500);
3244
	udelay(500);
3245
	if (wait_for(I915_READ(dslreg) != temp, 5)) {
3245
	if (wait_for(I915_READ(dslreg) != temp, 5)) {
3246
		if (wait_for(I915_READ(dslreg) != temp, 5))
3246
		if (wait_for(I915_READ(dslreg) != temp, 5))
3247
			DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
3247
			DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
3248
	}
3248
	}
3249
}
3249
}
3250
 
3250
 
3251
static void ironlake_pfit_enable(struct intel_crtc *crtc)
3251
static void ironlake_pfit_enable(struct intel_crtc *crtc)
3252
{
3252
{
3253
	struct drm_device *dev = crtc->base.dev;
3253
	struct drm_device *dev = crtc->base.dev;
3254
	struct drm_i915_private *dev_priv = dev->dev_private;
3254
	struct drm_i915_private *dev_priv = dev->dev_private;
3255
	int pipe = crtc->pipe;
3255
	int pipe = crtc->pipe;
3256
 
3256
 
3257
	if (crtc->config.pch_pfit.enabled) {
3257
	if (crtc->config.pch_pfit.enabled) {
3258
		/* Force use of hard-coded filter coefficients
3258
		/* Force use of hard-coded filter coefficients
3259
		 * as some pre-programmed values are broken,
3259
		 * as some pre-programmed values are broken,
3260
		 * e.g. x201.
3260
		 * e.g. x201.
3261
		 */
3261
		 */
3262
		if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3262
		if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3263
			I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3263
			I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3264
						 PF_PIPE_SEL_IVB(pipe));
3264
						 PF_PIPE_SEL_IVB(pipe));
3265
		else
3265
		else
3266
			I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3266
			I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3267
		I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3267
		I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3268
		I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
3268
		I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
3269
	}
3269
	}
3270
}
3270
}
3271
 
3271
 
3272
static void intel_enable_planes(struct drm_crtc *crtc)
3272
static void intel_enable_planes(struct drm_crtc *crtc)
3273
{
3273
{
3274
	struct drm_device *dev = crtc->dev;
3274
	struct drm_device *dev = crtc->dev;
3275
	enum pipe pipe = to_intel_crtc(crtc)->pipe;
3275
	enum pipe pipe = to_intel_crtc(crtc)->pipe;
3276
	struct intel_plane *intel_plane;
3276
	struct intel_plane *intel_plane;
3277
 
3277
 
3278
	list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3278
	list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3279
		if (intel_plane->pipe == pipe)
3279
		if (intel_plane->pipe == pipe)
3280
			intel_plane_restore(&intel_plane->base);
3280
			intel_plane_restore(&intel_plane->base);
3281
}
3281
}
3282
 
3282
 
3283
static void intel_disable_planes(struct drm_crtc *crtc)
3283
static void intel_disable_planes(struct drm_crtc *crtc)
3284
{
3284
{
3285
	struct drm_device *dev = crtc->dev;
3285
	struct drm_device *dev = crtc->dev;
3286
	enum pipe pipe = to_intel_crtc(crtc)->pipe;
3286
	enum pipe pipe = to_intel_crtc(crtc)->pipe;
3287
	struct intel_plane *intel_plane;
3287
	struct intel_plane *intel_plane;
3288
 
3288
 
3289
	list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3289
	list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3290
		if (intel_plane->pipe == pipe)
3290
		if (intel_plane->pipe == pipe)
3291
			intel_plane_disable(&intel_plane->base);
3291
			intel_plane_disable(&intel_plane->base);
3292
}
3292
}
3293
 
3293
 
3294
static void ironlake_crtc_enable(struct drm_crtc *crtc)
3294
static void ironlake_crtc_enable(struct drm_crtc *crtc)
3295
{
3295
{
3296
    struct drm_device *dev = crtc->dev;
3296
    struct drm_device *dev = crtc->dev;
3297
    struct drm_i915_private *dev_priv = dev->dev_private;
3297
    struct drm_i915_private *dev_priv = dev->dev_private;
3298
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3298
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3299
	struct intel_encoder *encoder;
3299
	struct intel_encoder *encoder;
3300
    int pipe = intel_crtc->pipe;
3300
    int pipe = intel_crtc->pipe;
3301
    int plane = intel_crtc->plane;
3301
    int plane = intel_crtc->plane;
3302
 
3302
 
3303
	WARN_ON(!crtc->enabled);
3303
	WARN_ON(!crtc->enabled);
3304
 
3304
 
3305
    if (intel_crtc->active)
3305
    if (intel_crtc->active)
3306
        return;
3306
        return;
3307
 
3307
 
3308
    intel_crtc->active = true;
3308
    intel_crtc->active = true;
3309
 
3309
 
3310
	intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3310
	intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3311
	intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
3311
	intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
3312
 
3312
 
3313
    intel_update_watermarks(dev);
3313
    intel_update_watermarks(dev);
3314
 
3314
 
3315
	for_each_encoder_on_crtc(dev, crtc, encoder)
3315
	for_each_encoder_on_crtc(dev, crtc, encoder)
3316
		if (encoder->pre_enable)
3316
		if (encoder->pre_enable)
3317
			encoder->pre_enable(encoder);
3317
			encoder->pre_enable(encoder);
3318
 
3318
 
3319
	if (intel_crtc->config.has_pch_encoder) {
3319
	if (intel_crtc->config.has_pch_encoder) {
3320
		/* Note: FDI PLL enabling _must_ be done before we enable the
3320
		/* Note: FDI PLL enabling _must_ be done before we enable the
3321
		 * cpu pipes, hence this is separate from all the other fdi/pch
3321
		 * cpu pipes, hence this is separate from all the other fdi/pch
3322
		 * enabling. */
3322
		 * enabling. */
3323
		ironlake_fdi_pll_enable(intel_crtc);
3323
		ironlake_fdi_pll_enable(intel_crtc);
3324
	} else {
3324
	} else {
3325
		assert_fdi_tx_disabled(dev_priv, pipe);
3325
		assert_fdi_tx_disabled(dev_priv, pipe);
3326
		assert_fdi_rx_disabled(dev_priv, pipe);
3326
		assert_fdi_rx_disabled(dev_priv, pipe);
3327
	}
3327
	}
3328
 
3328
 
3329
	ironlake_pfit_enable(intel_crtc);
3329
	ironlake_pfit_enable(intel_crtc);
3330
 
3330
 
3331
    /*
3331
    /*
3332
     * On ILK+ LUT must be loaded before the pipe is running but with
3332
     * On ILK+ LUT must be loaded before the pipe is running but with
3333
     * clocks enabled
3333
     * clocks enabled
3334
     */
3334
     */
3335
    intel_crtc_load_lut(crtc);
3335
    intel_crtc_load_lut(crtc);
3336
 
3336
 
3337
	intel_enable_pipe(dev_priv, pipe,
3337
	intel_enable_pipe(dev_priv, pipe,
3338
			  intel_crtc->config.has_pch_encoder);
3338
			  intel_crtc->config.has_pch_encoder);
3339
    intel_enable_plane(dev_priv, plane, pipe);
3339
    intel_enable_plane(dev_priv, plane, pipe);
3340
	intel_enable_planes(crtc);
3340
	intel_enable_planes(crtc);
3341
//	intel_crtc_update_cursor(crtc, true);
3341
//	intel_crtc_update_cursor(crtc, true);
3342
 
3342
 
3343
	if (intel_crtc->config.has_pch_encoder)
3343
	if (intel_crtc->config.has_pch_encoder)
3344
        ironlake_pch_enable(crtc);
3344
        ironlake_pch_enable(crtc);
3345
 
3345
 
3346
    mutex_lock(&dev->struct_mutex);
3346
    mutex_lock(&dev->struct_mutex);
3347
    intel_update_fbc(dev);
3347
    intel_update_fbc(dev);
3348
    mutex_unlock(&dev->struct_mutex);
3348
    mutex_unlock(&dev->struct_mutex);
3349
 
3349
 
3350
	for_each_encoder_on_crtc(dev, crtc, encoder)
3350
	for_each_encoder_on_crtc(dev, crtc, encoder)
3351
		encoder->enable(encoder);
3351
		encoder->enable(encoder);
3352
 
3352
 
3353
	if (HAS_PCH_CPT(dev))
3353
	if (HAS_PCH_CPT(dev))
3354
		cpt_verify_modeset(dev, intel_crtc->pipe);
3354
		cpt_verify_modeset(dev, intel_crtc->pipe);
3355
 
3355
 
3356
	/*
3356
	/*
3357
	 * There seems to be a race in PCH platform hw (at least on some
3357
	 * There seems to be a race in PCH platform hw (at least on some
3358
	 * outputs) where an enabled pipe still completes any pageflip right
3358
	 * outputs) where an enabled pipe still completes any pageflip right
3359
	 * away (as if the pipe is off) instead of waiting for vblank. As soon
3359
	 * away (as if the pipe is off) instead of waiting for vblank. As soon
3360
	 * as the first vblank happend, everything works as expected. Hence just
3360
	 * as the first vblank happend, everything works as expected. Hence just
3361
	 * wait for one vblank before returning to avoid strange things
3361
	 * wait for one vblank before returning to avoid strange things
3362
	 * happening.
3362
	 * happening.
3363
	 */
3363
	 */
3364
	intel_wait_for_vblank(dev, intel_crtc->pipe);
3364
	intel_wait_for_vblank(dev, intel_crtc->pipe);
3365
}
3365
}
3366
 
3366
 
3367
/* IPS only exists on ULT machines and is tied to pipe A. */
3367
/* IPS only exists on ULT machines and is tied to pipe A. */
3368
static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
3368
static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
3369
{
3369
{
3370
	return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
3370
	return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
3371
}
3371
}
3372
 
3372
 
3373
static void hsw_enable_ips(struct intel_crtc *crtc)
3373
static void hsw_enable_ips(struct intel_crtc *crtc)
3374
{
3374
{
3375
	struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3375
	struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3376
 
3376
 
3377
	if (!crtc->config.ips_enabled)
3377
	if (!crtc->config.ips_enabled)
3378
		return;
3378
		return;
3379
 
3379
 
3380
	/* We can only enable IPS after we enable a plane and wait for a vblank.
3380
	/* We can only enable IPS after we enable a plane and wait for a vblank.
3381
	 * We guarantee that the plane is enabled by calling intel_enable_ips
3381
	 * We guarantee that the plane is enabled by calling intel_enable_ips
3382
	 * only after intel_enable_plane. And intel_enable_plane already waits
3382
	 * only after intel_enable_plane. And intel_enable_plane already waits
3383
	 * for a vblank, so all we need to do here is to enable the IPS bit. */
3383
	 * for a vblank, so all we need to do here is to enable the IPS bit. */
3384
	assert_plane_enabled(dev_priv, crtc->plane);
3384
	assert_plane_enabled(dev_priv, crtc->plane);
3385
	I915_WRITE(IPS_CTL, IPS_ENABLE);
3385
	I915_WRITE(IPS_CTL, IPS_ENABLE);
3386
}
3386
}
3387
 
3387
 
3388
static void hsw_disable_ips(struct intel_crtc *crtc)
3388
static void hsw_disable_ips(struct intel_crtc *crtc)
3389
{
3389
{
3390
	struct drm_device *dev = crtc->base.dev;
3390
	struct drm_device *dev = crtc->base.dev;
3391
	struct drm_i915_private *dev_priv = dev->dev_private;
3391
	struct drm_i915_private *dev_priv = dev->dev_private;
3392
 
3392
 
3393
	if (!crtc->config.ips_enabled)
3393
	if (!crtc->config.ips_enabled)
3394
		return;
3394
		return;
3395
 
3395
 
3396
	assert_plane_enabled(dev_priv, crtc->plane);
3396
	assert_plane_enabled(dev_priv, crtc->plane);
3397
	I915_WRITE(IPS_CTL, 0);
3397
	I915_WRITE(IPS_CTL, 0);
3398
 
3398
 
3399
	/* We need to wait for a vblank before we can disable the plane. */
3399
	/* We need to wait for a vblank before we can disable the plane. */
3400
	intel_wait_for_vblank(dev, crtc->pipe);
3400
	intel_wait_for_vblank(dev, crtc->pipe);
3401
}
3401
}
3402
 
3402
 
3403
static void haswell_crtc_enable(struct drm_crtc *crtc)
3403
static void haswell_crtc_enable(struct drm_crtc *crtc)
3404
{
3404
{
3405
	struct drm_device *dev = crtc->dev;
3405
	struct drm_device *dev = crtc->dev;
3406
	struct drm_i915_private *dev_priv = dev->dev_private;
3406
	struct drm_i915_private *dev_priv = dev->dev_private;
3407
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3407
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3408
	struct intel_encoder *encoder;
3408
	struct intel_encoder *encoder;
3409
	int pipe = intel_crtc->pipe;
3409
	int pipe = intel_crtc->pipe;
3410
	int plane = intel_crtc->plane;
3410
	int plane = intel_crtc->plane;
3411
 
3411
 
3412
	WARN_ON(!crtc->enabled);
3412
	WARN_ON(!crtc->enabled);
3413
 
3413
 
3414
	if (intel_crtc->active)
3414
	if (intel_crtc->active)
3415
		return;
3415
		return;
3416
 
3416
 
3417
	intel_crtc->active = true;
3417
	intel_crtc->active = true;
3418
 
3418
 
3419
	intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3419
	intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3420
	if (intel_crtc->config.has_pch_encoder)
3420
	if (intel_crtc->config.has_pch_encoder)
3421
		intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
3421
		intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
3422
 
3422
 
3423
	intel_update_watermarks(dev);
3423
	intel_update_watermarks(dev);
3424
 
3424
 
3425
	if (intel_crtc->config.has_pch_encoder)
3425
	if (intel_crtc->config.has_pch_encoder)
3426
		dev_priv->display.fdi_link_train(crtc);
3426
		dev_priv->display.fdi_link_train(crtc);
3427
 
3427
 
3428
	for_each_encoder_on_crtc(dev, crtc, encoder)
3428
	for_each_encoder_on_crtc(dev, crtc, encoder)
3429
		if (encoder->pre_enable)
3429
		if (encoder->pre_enable)
3430
			encoder->pre_enable(encoder);
3430
			encoder->pre_enable(encoder);
3431
 
3431
 
3432
	intel_ddi_enable_pipe_clock(intel_crtc);
3432
	intel_ddi_enable_pipe_clock(intel_crtc);
3433
 
3433
 
3434
	ironlake_pfit_enable(intel_crtc);
3434
	ironlake_pfit_enable(intel_crtc);
3435
 
3435
 
3436
	/*
3436
	/*
3437
	 * On ILK+ LUT must be loaded before the pipe is running but with
3437
	 * On ILK+ LUT must be loaded before the pipe is running but with
3438
	 * clocks enabled
3438
	 * clocks enabled
3439
	 */
3439
	 */
3440
	intel_crtc_load_lut(crtc);
3440
	intel_crtc_load_lut(crtc);
3441
 
3441
 
3442
	intel_ddi_set_pipe_settings(crtc);
3442
	intel_ddi_set_pipe_settings(crtc);
3443
	intel_ddi_enable_transcoder_func(crtc);
3443
	intel_ddi_enable_transcoder_func(crtc);
3444
 
3444
 
3445
	intel_enable_pipe(dev_priv, pipe,
3445
	intel_enable_pipe(dev_priv, pipe,
3446
			  intel_crtc->config.has_pch_encoder);
3446
			  intel_crtc->config.has_pch_encoder);
3447
	intel_enable_plane(dev_priv, plane, pipe);
3447
	intel_enable_plane(dev_priv, plane, pipe);
3448
	intel_enable_planes(crtc);
3448
	intel_enable_planes(crtc);
3449
//	intel_crtc_update_cursor(crtc, true);
3449
//	intel_crtc_update_cursor(crtc, true);
3450
 
3450
 
3451
	hsw_enable_ips(intel_crtc);
3451
	hsw_enable_ips(intel_crtc);
3452
 
3452
 
3453
	if (intel_crtc->config.has_pch_encoder)
3453
	if (intel_crtc->config.has_pch_encoder)
3454
		lpt_pch_enable(crtc);
3454
		lpt_pch_enable(crtc);
3455
 
3455
 
3456
	mutex_lock(&dev->struct_mutex);
3456
	mutex_lock(&dev->struct_mutex);
3457
	intel_update_fbc(dev);
3457
	intel_update_fbc(dev);
3458
	mutex_unlock(&dev->struct_mutex);
3458
	mutex_unlock(&dev->struct_mutex);
3459
 
3459
 
3460
	for_each_encoder_on_crtc(dev, crtc, encoder)
3460
	for_each_encoder_on_crtc(dev, crtc, encoder)
3461
		encoder->enable(encoder);
3461
		encoder->enable(encoder);
3462
 
3462
 
3463
	/*
3463
	/*
3464
	 * There seems to be a race in PCH platform hw (at least on some
3464
	 * There seems to be a race in PCH platform hw (at least on some
3465
	 * outputs) where an enabled pipe still completes any pageflip right
3465
	 * outputs) where an enabled pipe still completes any pageflip right
3466
	 * away (as if the pipe is off) instead of waiting for vblank. As soon
3466
	 * away (as if the pipe is off) instead of waiting for vblank. As soon
3467
	 * as the first vblank happend, everything works as expected. Hence just
3467
	 * as the first vblank happend, everything works as expected. Hence just
3468
	 * wait for one vblank before returning to avoid strange things
3468
	 * wait for one vblank before returning to avoid strange things
3469
	 * happening.
3469
	 * happening.
3470
	 */
3470
	 */
3471
	intel_wait_for_vblank(dev, intel_crtc->pipe);
3471
	intel_wait_for_vblank(dev, intel_crtc->pipe);
3472
}
3472
}
3473
 
3473
 
3474
static void ironlake_pfit_disable(struct intel_crtc *crtc)
3474
static void ironlake_pfit_disable(struct intel_crtc *crtc)
3475
{
3475
{
3476
	struct drm_device *dev = crtc->base.dev;
3476
	struct drm_device *dev = crtc->base.dev;
3477
	struct drm_i915_private *dev_priv = dev->dev_private;
3477
	struct drm_i915_private *dev_priv = dev->dev_private;
3478
	int pipe = crtc->pipe;
3478
	int pipe = crtc->pipe;
3479
 
3479
 
3480
	/* To avoid upsetting the power well on haswell only disable the pfit if
3480
	/* To avoid upsetting the power well on haswell only disable the pfit if
3481
	 * it's in use. The hw state code will make sure we get this right. */
3481
	 * it's in use. The hw state code will make sure we get this right. */
3482
	if (crtc->config.pch_pfit.enabled) {
3482
	if (crtc->config.pch_pfit.enabled) {
3483
		I915_WRITE(PF_CTL(pipe), 0);
3483
		I915_WRITE(PF_CTL(pipe), 0);
3484
		I915_WRITE(PF_WIN_POS(pipe), 0);
3484
		I915_WRITE(PF_WIN_POS(pipe), 0);
3485
		I915_WRITE(PF_WIN_SZ(pipe), 0);
3485
		I915_WRITE(PF_WIN_SZ(pipe), 0);
3486
	}
3486
	}
3487
}
3487
}
3488
 
3488
 
3489
static void ironlake_crtc_disable(struct drm_crtc *crtc)
3489
static void ironlake_crtc_disable(struct drm_crtc *crtc)
3490
{
3490
{
3491
    struct drm_device *dev = crtc->dev;
3491
    struct drm_device *dev = crtc->dev;
3492
    struct drm_i915_private *dev_priv = dev->dev_private;
3492
    struct drm_i915_private *dev_priv = dev->dev_private;
3493
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3493
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3494
	struct intel_encoder *encoder;
3494
	struct intel_encoder *encoder;
3495
    int pipe = intel_crtc->pipe;
3495
    int pipe = intel_crtc->pipe;
3496
    int plane = intel_crtc->plane;
3496
    int plane = intel_crtc->plane;
3497
    u32 reg, temp;
3497
    u32 reg, temp;
3498
 
3498
 
3499
 
3499
 
3500
    if (!intel_crtc->active)
3500
    if (!intel_crtc->active)
3501
        return;
3501
        return;
3502
 
3502
 
3503
	for_each_encoder_on_crtc(dev, crtc, encoder)
3503
	for_each_encoder_on_crtc(dev, crtc, encoder)
3504
		encoder->disable(encoder);
3504
		encoder->disable(encoder);
3505
 
3505
 
3506
//    intel_crtc_wait_for_pending_flips(crtc);
3506
//    intel_crtc_wait_for_pending_flips(crtc);
3507
//    drm_vblank_off(dev, pipe);
3507
//    drm_vblank_off(dev, pipe);
3508
 
3508
 
3509
	if (dev_priv->fbc.plane == plane)
3509
	if (dev_priv->fbc.plane == plane)
3510
		intel_disable_fbc(dev);
3510
		intel_disable_fbc(dev);
3511
 
3511
 
3512
//	intel_crtc_update_cursor(crtc, false);
3512
//	intel_crtc_update_cursor(crtc, false);
3513
	intel_disable_planes(crtc);
3513
	intel_disable_planes(crtc);
3514
    intel_disable_plane(dev_priv, plane, pipe);
3514
    intel_disable_plane(dev_priv, plane, pipe);
3515
 
3515
 
3516
	if (intel_crtc->config.has_pch_encoder)
3516
	if (intel_crtc->config.has_pch_encoder)
3517
		intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
3517
		intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
3518
 
3518
 
3519
    intel_disable_pipe(dev_priv, pipe);
3519
    intel_disable_pipe(dev_priv, pipe);
3520
 
3520
 
3521
	ironlake_pfit_disable(intel_crtc);
3521
	ironlake_pfit_disable(intel_crtc);
3522
 
3522
 
3523
	for_each_encoder_on_crtc(dev, crtc, encoder)
3523
	for_each_encoder_on_crtc(dev, crtc, encoder)
3524
		if (encoder->post_disable)
3524
		if (encoder->post_disable)
3525
			encoder->post_disable(encoder);
3525
			encoder->post_disable(encoder);
3526
 
3526
 
3527
	if (intel_crtc->config.has_pch_encoder) {
3527
	if (intel_crtc->config.has_pch_encoder) {
3528
    ironlake_fdi_disable(crtc);
3528
    ironlake_fdi_disable(crtc);
3529
 
3529
 
3530
	ironlake_disable_pch_transcoder(dev_priv, pipe);
3530
	ironlake_disable_pch_transcoder(dev_priv, pipe);
3531
		intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
3531
		intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
3532
 
3532
 
3533
    if (HAS_PCH_CPT(dev)) {
3533
    if (HAS_PCH_CPT(dev)) {
3534
        /* disable TRANS_DP_CTL */
3534
        /* disable TRANS_DP_CTL */
3535
        reg = TRANS_DP_CTL(pipe);
3535
        reg = TRANS_DP_CTL(pipe);
3536
        temp = I915_READ(reg);
3536
        temp = I915_READ(reg);
3537
			temp &= ~(TRANS_DP_OUTPUT_ENABLE |
3537
			temp &= ~(TRANS_DP_OUTPUT_ENABLE |
3538
				  TRANS_DP_PORT_SEL_MASK);
3538
				  TRANS_DP_PORT_SEL_MASK);
3539
        temp |= TRANS_DP_PORT_SEL_NONE;
3539
        temp |= TRANS_DP_PORT_SEL_NONE;
3540
        I915_WRITE(reg, temp);
3540
        I915_WRITE(reg, temp);
3541
 
3541
 
3542
        /* disable DPLL_SEL */
3542
        /* disable DPLL_SEL */
3543
        temp = I915_READ(PCH_DPLL_SEL);
3543
        temp = I915_READ(PCH_DPLL_SEL);
3544
			temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
3544
			temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
3545
        I915_WRITE(PCH_DPLL_SEL, temp);
3545
        I915_WRITE(PCH_DPLL_SEL, temp);
3546
    }
3546
    }
3547
 
3547
 
3548
    /* disable PCH DPLL */
3548
    /* disable PCH DPLL */
3549
		intel_disable_shared_dpll(intel_crtc);
3549
		intel_disable_shared_dpll(intel_crtc);
3550
 
3550
 
3551
	ironlake_fdi_pll_disable(intel_crtc);
3551
	ironlake_fdi_pll_disable(intel_crtc);
3552
	}
3552
	}
3553
 
3553
 
3554
    intel_crtc->active = false;
3554
    intel_crtc->active = false;
3555
    intel_update_watermarks(dev);
3555
    intel_update_watermarks(dev);
3556
 
3556
 
3557
    mutex_lock(&dev->struct_mutex);
3557
    mutex_lock(&dev->struct_mutex);
3558
    intel_update_fbc(dev);
3558
    intel_update_fbc(dev);
3559
    mutex_unlock(&dev->struct_mutex);
3559
    mutex_unlock(&dev->struct_mutex);
3560
}
3560
}
3561
 
3561
 
3562
static void haswell_crtc_disable(struct drm_crtc *crtc)
3562
static void haswell_crtc_disable(struct drm_crtc *crtc)
3563
{
3563
{
3564
	struct drm_device *dev = crtc->dev;
3564
	struct drm_device *dev = crtc->dev;
3565
	struct drm_i915_private *dev_priv = dev->dev_private;
3565
	struct drm_i915_private *dev_priv = dev->dev_private;
3566
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3566
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3567
	struct intel_encoder *encoder;
3567
	struct intel_encoder *encoder;
3568
	int pipe = intel_crtc->pipe;
3568
	int pipe = intel_crtc->pipe;
3569
	int plane = intel_crtc->plane;
3569
	int plane = intel_crtc->plane;
3570
	enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
3570
	enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
3571
 
3571
 
3572
	if (!intel_crtc->active)
3572
	if (!intel_crtc->active)
3573
		return;
3573
		return;
3574
 
3574
 
3575
	for_each_encoder_on_crtc(dev, crtc, encoder)
3575
	for_each_encoder_on_crtc(dev, crtc, encoder)
3576
		encoder->disable(encoder);
3576
		encoder->disable(encoder);
3577
 
3577
 
3578
 
3578
 
3579
	/* FBC must be disabled before disabling the plane on HSW. */
3579
	/* FBC must be disabled before disabling the plane on HSW. */
3580
	if (dev_priv->fbc.plane == plane)
3580
	if (dev_priv->fbc.plane == plane)
3581
		intel_disable_fbc(dev);
3581
		intel_disable_fbc(dev);
3582
 
3582
 
3583
	hsw_disable_ips(intel_crtc);
3583
	hsw_disable_ips(intel_crtc);
3584
 
3584
 
3585
//	intel_crtc_update_cursor(crtc, false);
3585
//	intel_crtc_update_cursor(crtc, false);
3586
	intel_disable_planes(crtc);
3586
	intel_disable_planes(crtc);
3587
	intel_disable_plane(dev_priv, plane, pipe);
3587
	intel_disable_plane(dev_priv, plane, pipe);
3588
 
3588
 
3589
	if (intel_crtc->config.has_pch_encoder)
3589
	if (intel_crtc->config.has_pch_encoder)
3590
		intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
3590
		intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
3591
	intel_disable_pipe(dev_priv, pipe);
3591
	intel_disable_pipe(dev_priv, pipe);
3592
 
3592
 
3593
	intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
3593
	intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
3594
 
3594
 
3595
	ironlake_pfit_disable(intel_crtc);
3595
	ironlake_pfit_disable(intel_crtc);
3596
 
3596
 
3597
	intel_ddi_disable_pipe_clock(intel_crtc);
3597
	intel_ddi_disable_pipe_clock(intel_crtc);
3598
 
3598
 
3599
	for_each_encoder_on_crtc(dev, crtc, encoder)
3599
	for_each_encoder_on_crtc(dev, crtc, encoder)
3600
		if (encoder->post_disable)
3600
		if (encoder->post_disable)
3601
			encoder->post_disable(encoder);
3601
			encoder->post_disable(encoder);
3602
 
3602
 
3603
	if (intel_crtc->config.has_pch_encoder) {
3603
	if (intel_crtc->config.has_pch_encoder) {
3604
		lpt_disable_pch_transcoder(dev_priv);
3604
		lpt_disable_pch_transcoder(dev_priv);
3605
		intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
3605
		intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
3606
		intel_ddi_fdi_disable(crtc);
3606
		intel_ddi_fdi_disable(crtc);
3607
	}
3607
	}
3608
 
3608
 
3609
	intel_crtc->active = false;
3609
	intel_crtc->active = false;
3610
	intel_update_watermarks(dev);
3610
	intel_update_watermarks(dev);
3611
 
3611
 
3612
	mutex_lock(&dev->struct_mutex);
3612
	mutex_lock(&dev->struct_mutex);
3613
	intel_update_fbc(dev);
3613
	intel_update_fbc(dev);
3614
	mutex_unlock(&dev->struct_mutex);
3614
	mutex_unlock(&dev->struct_mutex);
3615
}
3615
}
3616
 
3616
 
3617
static void ironlake_crtc_off(struct drm_crtc *crtc)
3617
static void ironlake_crtc_off(struct drm_crtc *crtc)
3618
{
3618
{
3619
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3619
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3620
	intel_put_shared_dpll(intel_crtc);
3620
	intel_put_shared_dpll(intel_crtc);
3621
}
3621
}
3622
 
3622
 
3623
static void haswell_crtc_off(struct drm_crtc *crtc)
3623
static void haswell_crtc_off(struct drm_crtc *crtc)
3624
{
3624
{
3625
	intel_ddi_put_crtc_pll(crtc);
3625
	intel_ddi_put_crtc_pll(crtc);
3626
}
3626
}
3627
 
3627
 
3628
static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3628
static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3629
{
3629
{
3630
	if (!enable && intel_crtc->overlay) {
3630
	if (!enable && intel_crtc->overlay) {
3631
		struct drm_device *dev = intel_crtc->base.dev;
3631
		struct drm_device *dev = intel_crtc->base.dev;
3632
		struct drm_i915_private *dev_priv = dev->dev_private;
3632
		struct drm_i915_private *dev_priv = dev->dev_private;
3633
 
3633
 
3634
		mutex_lock(&dev->struct_mutex);
3634
		mutex_lock(&dev->struct_mutex);
3635
		dev_priv->mm.interruptible = false;
3635
		dev_priv->mm.interruptible = false;
3636
//       (void) intel_overlay_switch_off(intel_crtc->overlay);
3636
//       (void) intel_overlay_switch_off(intel_crtc->overlay);
3637
		dev_priv->mm.interruptible = true;
3637
		dev_priv->mm.interruptible = true;
3638
		mutex_unlock(&dev->struct_mutex);
3638
		mutex_unlock(&dev->struct_mutex);
3639
	}
3639
	}
3640
 
3640
 
3641
	/* Let userspace switch the overlay on again. In most cases userspace
3641
	/* Let userspace switch the overlay on again. In most cases userspace
3642
	 * has to recompute where to put it anyway.
3642
	 * has to recompute where to put it anyway.
3643
	 */
3643
	 */
3644
}
3644
}
3645
 
3645
 
3646
/**
3646
/**
3647
 * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
3647
 * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
3648
 * cursor plane briefly if not already running after enabling the display
3648
 * cursor plane briefly if not already running after enabling the display
3649
 * plane.
3649
 * plane.
3650
 * This workaround avoids occasional blank screens when self refresh is
3650
 * This workaround avoids occasional blank screens when self refresh is
3651
 * enabled.
3651
 * enabled.
3652
 */
3652
 */
3653
static void
3653
static void
3654
g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
3654
g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
3655
{
3655
{
3656
	u32 cntl = I915_READ(CURCNTR(pipe));
3656
	u32 cntl = I915_READ(CURCNTR(pipe));
3657
 
3657
 
3658
	if ((cntl & CURSOR_MODE) == 0) {
3658
	if ((cntl & CURSOR_MODE) == 0) {
3659
		u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
3659
		u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
3660
 
3660
 
3661
		I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
3661
		I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
3662
		I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
3662
		I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
3663
		intel_wait_for_vblank(dev_priv->dev, pipe);
3663
		intel_wait_for_vblank(dev_priv->dev, pipe);
3664
		I915_WRITE(CURCNTR(pipe), cntl);
3664
		I915_WRITE(CURCNTR(pipe), cntl);
3665
		I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3665
		I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3666
		I915_WRITE(FW_BLC_SELF, fw_bcl_self);
3666
		I915_WRITE(FW_BLC_SELF, fw_bcl_self);
3667
	}
3667
	}
3668
}
3668
}
3669
 
3669
 
3670
static void i9xx_pfit_enable(struct intel_crtc *crtc)
3670
static void i9xx_pfit_enable(struct intel_crtc *crtc)
3671
{
3671
{
3672
	struct drm_device *dev = crtc->base.dev;
3672
	struct drm_device *dev = crtc->base.dev;
3673
	struct drm_i915_private *dev_priv = dev->dev_private;
3673
	struct drm_i915_private *dev_priv = dev->dev_private;
3674
	struct intel_crtc_config *pipe_config = &crtc->config;
3674
	struct intel_crtc_config *pipe_config = &crtc->config;
3675
 
3675
 
3676
	if (!crtc->config.gmch_pfit.control)
3676
	if (!crtc->config.gmch_pfit.control)
3677
		return;
3677
		return;
3678
 
3678
 
3679
	/*
3679
	/*
3680
	 * The panel fitter should only be adjusted whilst the pipe is disabled,
3680
	 * The panel fitter should only be adjusted whilst the pipe is disabled,
3681
	 * according to register description and PRM.
3681
	 * according to register description and PRM.
3682
	 */
3682
	 */
3683
	WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
3683
	WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
3684
	assert_pipe_disabled(dev_priv, crtc->pipe);
3684
	assert_pipe_disabled(dev_priv, crtc->pipe);
3685
 
3685
 
3686
	I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
3686
	I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
3687
	I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
3687
	I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
3688
 
3688
 
3689
	/* Border color in case we don't scale up to the full screen. Black by
3689
	/* Border color in case we don't scale up to the full screen. Black by
3690
	 * default, change to something else for debugging. */
3690
	 * default, change to something else for debugging. */
3691
	I915_WRITE(BCLRPAT(crtc->pipe), 0);
3691
	I915_WRITE(BCLRPAT(crtc->pipe), 0);
3692
}
3692
}
3693
 
3693
 
3694
static void valleyview_crtc_enable(struct drm_crtc *crtc)
3694
static void valleyview_crtc_enable(struct drm_crtc *crtc)
3695
{
3695
{
3696
	struct drm_device *dev = crtc->dev;
3696
	struct drm_device *dev = crtc->dev;
3697
	struct drm_i915_private *dev_priv = dev->dev_private;
3697
	struct drm_i915_private *dev_priv = dev->dev_private;
3698
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3698
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3699
	struct intel_encoder *encoder;
3699
	struct intel_encoder *encoder;
3700
	int pipe = intel_crtc->pipe;
3700
	int pipe = intel_crtc->pipe;
3701
	int plane = intel_crtc->plane;
3701
	int plane = intel_crtc->plane;
3702
 
3702
 
3703
	WARN_ON(!crtc->enabled);
3703
	WARN_ON(!crtc->enabled);
3704
 
3704
 
3705
	if (intel_crtc->active)
3705
	if (intel_crtc->active)
3706
		return;
3706
		return;
3707
 
3707
 
3708
	intel_crtc->active = true;
3708
	intel_crtc->active = true;
3709
	intel_update_watermarks(dev);
3709
	intel_update_watermarks(dev);
3710
 
3710
 
3711
	for_each_encoder_on_crtc(dev, crtc, encoder)
3711
	for_each_encoder_on_crtc(dev, crtc, encoder)
3712
		if (encoder->pre_pll_enable)
3712
		if (encoder->pre_pll_enable)
3713
			encoder->pre_pll_enable(encoder);
3713
			encoder->pre_pll_enable(encoder);
3714
 
3714
 
3715
	vlv_enable_pll(intel_crtc);
3715
	vlv_enable_pll(intel_crtc);
3716
 
3716
 
3717
	for_each_encoder_on_crtc(dev, crtc, encoder)
3717
	for_each_encoder_on_crtc(dev, crtc, encoder)
3718
		if (encoder->pre_enable)
3718
		if (encoder->pre_enable)
3719
			encoder->pre_enable(encoder);
3719
			encoder->pre_enable(encoder);
3720
 
3720
 
3721
	i9xx_pfit_enable(intel_crtc);
3721
	i9xx_pfit_enable(intel_crtc);
3722
 
3722
 
3723
	intel_crtc_load_lut(crtc);
3723
	intel_crtc_load_lut(crtc);
3724
 
3724
 
3725
	intel_enable_pipe(dev_priv, pipe, false);
3725
	intel_enable_pipe(dev_priv, pipe, false);
3726
	intel_enable_plane(dev_priv, plane, pipe);
3726
	intel_enable_plane(dev_priv, plane, pipe);
3727
	intel_enable_planes(crtc);
3727
	intel_enable_planes(crtc);
3728
//	intel_crtc_update_cursor(crtc, true);
3728
//	intel_crtc_update_cursor(crtc, true);
3729
 
3729
 
3730
	intel_update_fbc(dev);
3730
	intel_update_fbc(dev);
3731
 
3731
 
3732
	for_each_encoder_on_crtc(dev, crtc, encoder)
3732
	for_each_encoder_on_crtc(dev, crtc, encoder)
3733
		encoder->enable(encoder);
3733
		encoder->enable(encoder);
3734
}
3734
}
3735
 
3735
 
3736
static void i9xx_crtc_enable(struct drm_crtc *crtc)
3736
static void i9xx_crtc_enable(struct drm_crtc *crtc)
3737
{
3737
{
3738
    struct drm_device *dev = crtc->dev;
3738
    struct drm_device *dev = crtc->dev;
3739
    struct drm_i915_private *dev_priv = dev->dev_private;
3739
    struct drm_i915_private *dev_priv = dev->dev_private;
3740
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3740
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3741
	struct intel_encoder *encoder;
3741
	struct intel_encoder *encoder;
3742
    int pipe = intel_crtc->pipe;
3742
    int pipe = intel_crtc->pipe;
3743
    int plane = intel_crtc->plane;
3743
    int plane = intel_crtc->plane;
3744
 
3744
 
3745
	WARN_ON(!crtc->enabled);
3745
	WARN_ON(!crtc->enabled);
3746
 
3746
 
3747
    if (intel_crtc->active)
3747
    if (intel_crtc->active)
3748
        return;
3748
        return;
3749
 
3749
 
3750
    intel_crtc->active = true;
3750
    intel_crtc->active = true;
3751
    intel_update_watermarks(dev);
3751
    intel_update_watermarks(dev);
3752
 
3752
 
3753
	for_each_encoder_on_crtc(dev, crtc, encoder)
3753
	for_each_encoder_on_crtc(dev, crtc, encoder)
3754
		if (encoder->pre_enable)
3754
		if (encoder->pre_enable)
3755
			encoder->pre_enable(encoder);
3755
			encoder->pre_enable(encoder);
3756
 
3756
 
3757
	i9xx_enable_pll(intel_crtc);
3757
	i9xx_enable_pll(intel_crtc);
3758
 
3758
 
3759
	i9xx_pfit_enable(intel_crtc);
3759
	i9xx_pfit_enable(intel_crtc);
3760
 
3760
 
3761
	intel_crtc_load_lut(crtc);
3761
	intel_crtc_load_lut(crtc);
3762
 
3762
 
3763
    intel_enable_pipe(dev_priv, pipe, false);
3763
    intel_enable_pipe(dev_priv, pipe, false);
3764
    intel_enable_plane(dev_priv, plane, pipe);
3764
    intel_enable_plane(dev_priv, plane, pipe);
3765
	intel_enable_planes(crtc);
3765
	intel_enable_planes(crtc);
3766
	/* The fixup needs to happen before cursor is enabled */
3766
	/* The fixup needs to happen before cursor is enabled */
3767
	if (IS_G4X(dev))
3767
	if (IS_G4X(dev))
3768
		g4x_fixup_plane(dev_priv, pipe);
3768
		g4x_fixup_plane(dev_priv, pipe);
3769
//	intel_crtc_update_cursor(crtc, true);
3769
//	intel_crtc_update_cursor(crtc, true);
3770
 
3770
 
3771
    /* Give the overlay scaler a chance to enable if it's on this pipe */
3771
    /* Give the overlay scaler a chance to enable if it's on this pipe */
3772
    intel_crtc_dpms_overlay(intel_crtc, true);
3772
    intel_crtc_dpms_overlay(intel_crtc, true);
3773
 
3773
 
3774
	intel_update_fbc(dev);
3774
	intel_update_fbc(dev);
3775
 
3775
 
3776
	for_each_encoder_on_crtc(dev, crtc, encoder)
3776
	for_each_encoder_on_crtc(dev, crtc, encoder)
3777
		encoder->enable(encoder);
3777
		encoder->enable(encoder);
3778
}
3778
}
3779
 
3779
 
3780
static void i9xx_pfit_disable(struct intel_crtc *crtc)
3780
static void i9xx_pfit_disable(struct intel_crtc *crtc)
3781
{
3781
{
3782
	struct drm_device *dev = crtc->base.dev;
3782
	struct drm_device *dev = crtc->base.dev;
3783
	struct drm_i915_private *dev_priv = dev->dev_private;
3783
	struct drm_i915_private *dev_priv = dev->dev_private;
3784
 
3784
 
3785
	if (!crtc->config.gmch_pfit.control)
3785
	if (!crtc->config.gmch_pfit.control)
3786
		return;
3786
		return;
3787
 
3787
 
3788
	assert_pipe_disabled(dev_priv, crtc->pipe);
3788
	assert_pipe_disabled(dev_priv, crtc->pipe);
3789
 
3789
 
3790
	DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
3790
	DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
3791
			 I915_READ(PFIT_CONTROL));
3791
			 I915_READ(PFIT_CONTROL));
3792
		I915_WRITE(PFIT_CONTROL, 0);
3792
		I915_WRITE(PFIT_CONTROL, 0);
3793
}
3793
}
3794
 
3794
 
3795
static void i9xx_crtc_disable(struct drm_crtc *crtc)
3795
static void i9xx_crtc_disable(struct drm_crtc *crtc)
3796
{
3796
{
3797
    struct drm_device *dev = crtc->dev;
3797
    struct drm_device *dev = crtc->dev;
3798
    struct drm_i915_private *dev_priv = dev->dev_private;
3798
    struct drm_i915_private *dev_priv = dev->dev_private;
3799
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3799
    struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3800
	struct intel_encoder *encoder;
3800
	struct intel_encoder *encoder;
3801
    int pipe = intel_crtc->pipe;
3801
    int pipe = intel_crtc->pipe;
3802
    int plane = intel_crtc->plane;
3802
    int plane = intel_crtc->plane;
3803
 
3803
 
3804
    if (!intel_crtc->active)
3804
    if (!intel_crtc->active)
3805
        return;
3805
        return;
3806
 
3806
 
3807
	for_each_encoder_on_crtc(dev, crtc, encoder)
3807
	for_each_encoder_on_crtc(dev, crtc, encoder)
3808
		encoder->disable(encoder);
3808
		encoder->disable(encoder);
3809
 
3809
 
3810
    /* Give the overlay scaler a chance to disable if it's on this pipe */
3810
    /* Give the overlay scaler a chance to disable if it's on this pipe */
3811
//    intel_crtc_wait_for_pending_flips(crtc);
3811
//    intel_crtc_wait_for_pending_flips(crtc);
3812
//    drm_vblank_off(dev, pipe);
3812
//    drm_vblank_off(dev, pipe);
3813
 
3813
 
3814
	if (dev_priv->fbc.plane == plane)
3814
	if (dev_priv->fbc.plane == plane)
3815
        intel_disable_fbc(dev);
3815
        intel_disable_fbc(dev);
3816
 
3816
 
3817
	intel_crtc_dpms_overlay(intel_crtc, false);
3817
	intel_crtc_dpms_overlay(intel_crtc, false);
3818
//	intel_crtc_update_cursor(crtc, false);
3818
//	intel_crtc_update_cursor(crtc, false);
3819
	intel_disable_planes(crtc);
3819
	intel_disable_planes(crtc);
3820
    intel_disable_plane(dev_priv, plane, pipe);
3820
    intel_disable_plane(dev_priv, plane, pipe);
3821
 
3821
 
3822
    intel_disable_pipe(dev_priv, pipe);
3822
    intel_disable_pipe(dev_priv, pipe);
3823
 
3823
 
3824
	i9xx_pfit_disable(intel_crtc);
3824
	i9xx_pfit_disable(intel_crtc);
3825
 
3825
 
3826
	for_each_encoder_on_crtc(dev, crtc, encoder)
3826
	for_each_encoder_on_crtc(dev, crtc, encoder)
3827
		if (encoder->post_disable)
3827
		if (encoder->post_disable)
3828
			encoder->post_disable(encoder);
3828
			encoder->post_disable(encoder);
3829
 
3829
 
3830
	i9xx_disable_pll(dev_priv, pipe);
3830
	i9xx_disable_pll(dev_priv, pipe);
3831
 
3831
 
3832
    intel_crtc->active = false;
3832
    intel_crtc->active = false;
3833
    intel_update_fbc(dev);
3833
    intel_update_fbc(dev);
3834
    intel_update_watermarks(dev);
3834
    intel_update_watermarks(dev);
3835
}
3835
}
3836
 
3836
 
3837
static void i9xx_crtc_off(struct drm_crtc *crtc)
3837
static void i9xx_crtc_off(struct drm_crtc *crtc)
3838
{
3838
{
3839
}
3839
}
3840
 
3840
 
3841
static void intel_crtc_update_sarea(struct drm_crtc *crtc,
3841
static void intel_crtc_update_sarea(struct drm_crtc *crtc,
3842
				    bool enabled)
3842
				    bool enabled)
3843
{
3843
{
3844
	struct drm_device *dev = crtc->dev;
3844
	struct drm_device *dev = crtc->dev;
3845
	struct drm_i915_master_private *master_priv;
3845
	struct drm_i915_master_private *master_priv;
3846
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3846
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3847
	int pipe = intel_crtc->pipe;
3847
	int pipe = intel_crtc->pipe;
3848
 
3848
 
3849
 
3849
 
3850
#if 0
3850
#if 0
3851
	if (!dev->primary->master)
3851
	if (!dev->primary->master)
3852
		return;
3852
		return;
3853
 
3853
 
3854
	master_priv = dev->primary->master->driver_priv;
3854
	master_priv = dev->primary->master->driver_priv;
3855
	if (!master_priv->sarea_priv)
3855
	if (!master_priv->sarea_priv)
3856
		return;
3856
		return;
3857
 
3857
 
3858
	switch (pipe) {
3858
	switch (pipe) {
3859
	case 0:
3859
	case 0:
3860
		master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
3860
		master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
3861
		master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
3861
		master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
3862
		break;
3862
		break;
3863
	case 1:
3863
	case 1:
3864
		master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
3864
		master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
3865
		master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
3865
		master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
3866
		break;
3866
		break;
3867
	default:
3867
	default:
3868
		DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
3868
		DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
3869
		break;
3869
		break;
3870
	}
3870
	}
3871
#endif
3871
#endif
3872
 
3872
 
3873
}
3873
}
3874
 
3874
 
3875
/**
3875
/**
3876
 * Sets the power management mode of the pipe and plane.
3876
 * Sets the power management mode of the pipe and plane.
3877
 */
3877
 */
3878
void intel_crtc_update_dpms(struct drm_crtc *crtc)
3878
void intel_crtc_update_dpms(struct drm_crtc *crtc)
3879
{
3879
{
3880
	struct drm_device *dev = crtc->dev;
3880
	struct drm_device *dev = crtc->dev;
3881
	struct drm_i915_private *dev_priv = dev->dev_private;
3881
	struct drm_i915_private *dev_priv = dev->dev_private;
3882
	struct intel_encoder *intel_encoder;
3882
	struct intel_encoder *intel_encoder;
3883
	bool enable = false;
3883
	bool enable = false;
3884
 
3884
 
3885
	for_each_encoder_on_crtc(dev, crtc, intel_encoder)
3885
	for_each_encoder_on_crtc(dev, crtc, intel_encoder)
3886
		enable |= intel_encoder->connectors_active;
3886
		enable |= intel_encoder->connectors_active;
3887
 
3887
 
3888
	if (enable)
3888
	if (enable)
3889
		dev_priv->display.crtc_enable(crtc);
3889
		dev_priv->display.crtc_enable(crtc);
3890
	else
3890
	else
3891
		dev_priv->display.crtc_disable(crtc);
3891
		dev_priv->display.crtc_disable(crtc);
3892
 
3892
 
3893
	intel_crtc_update_sarea(crtc, enable);
3893
	intel_crtc_update_sarea(crtc, enable);
3894
}
3894
}
3895
 
3895
 
3896
static void intel_crtc_disable(struct drm_crtc *crtc)
3896
static void intel_crtc_disable(struct drm_crtc *crtc)
3897
{
3897
{
3898
	struct drm_device *dev = crtc->dev;
3898
	struct drm_device *dev = crtc->dev;
3899
	struct drm_connector *connector;
3899
	struct drm_connector *connector;
3900
	struct drm_i915_private *dev_priv = dev->dev_private;
3900
	struct drm_i915_private *dev_priv = dev->dev_private;
3901
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3901
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3902
 
3902
 
3903
	/* crtc should still be enabled when we disable it. */
3903
	/* crtc should still be enabled when we disable it. */
3904
	WARN_ON(!crtc->enabled);
3904
	WARN_ON(!crtc->enabled);
3905
 
3905
 
3906
	dev_priv->display.crtc_disable(crtc);
3906
	dev_priv->display.crtc_disable(crtc);
3907
	intel_crtc->eld_vld = false;
3907
	intel_crtc->eld_vld = false;
3908
	intel_crtc_update_sarea(crtc, false);
3908
	intel_crtc_update_sarea(crtc, false);
3909
	dev_priv->display.off(crtc);
3909
	dev_priv->display.off(crtc);
3910
 
3910
 
3911
	assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
3911
	assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
3912
	assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
3912
	assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
3913
 
3913
 
3914
	if (crtc->fb) {
3914
	if (crtc->fb) {
3915
		mutex_lock(&dev->struct_mutex);
3915
		mutex_lock(&dev->struct_mutex);
3916
		intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
3916
		intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
3917
		mutex_unlock(&dev->struct_mutex);
3917
		mutex_unlock(&dev->struct_mutex);
3918
		crtc->fb = NULL;
3918
		crtc->fb = NULL;
3919
	}
3919
	}
3920
 
3920
 
3921
	/* Update computed state. */
3921
	/* Update computed state. */
3922
	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3922
	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3923
		if (!connector->encoder || !connector->encoder->crtc)
3923
		if (!connector->encoder || !connector->encoder->crtc)
3924
			continue;
3924
			continue;
3925
 
3925
 
3926
		if (connector->encoder->crtc != crtc)
3926
		if (connector->encoder->crtc != crtc)
3927
			continue;
3927
			continue;
3928
 
3928
 
3929
		connector->dpms = DRM_MODE_DPMS_OFF;
3929
		connector->dpms = DRM_MODE_DPMS_OFF;
3930
		to_intel_encoder(connector->encoder)->connectors_active = false;
3930
		to_intel_encoder(connector->encoder)->connectors_active = false;
3931
	}
3931
	}
3932
}
3932
}
3933
 
3933
 
3934
void intel_encoder_destroy(struct drm_encoder *encoder)
3934
void intel_encoder_destroy(struct drm_encoder *encoder)
3935
{
3935
{
3936
	struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
3936
	struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
3937
 
3937
 
3938
	drm_encoder_cleanup(encoder);
3938
	drm_encoder_cleanup(encoder);
3939
	kfree(intel_encoder);
3939
	kfree(intel_encoder);
3940
}
3940
}
3941
 
3941
 
3942
/* Simple dpms helper for encoders with just one connector, no cloning and only
3942
/* Simple dpms helper for encoders with just one connector, no cloning and only
3943
 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
3943
 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
3944
 * state of the entire output pipe. */
3944
 * state of the entire output pipe. */
3945
static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
3945
static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
3946
{
3946
{
3947
	if (mode == DRM_MODE_DPMS_ON) {
3947
	if (mode == DRM_MODE_DPMS_ON) {
3948
		encoder->connectors_active = true;
3948
		encoder->connectors_active = true;
3949
 
3949
 
3950
		intel_crtc_update_dpms(encoder->base.crtc);
3950
		intel_crtc_update_dpms(encoder->base.crtc);
3951
	} else {
3951
	} else {
3952
		encoder->connectors_active = false;
3952
		encoder->connectors_active = false;
3953
 
3953
 
3954
		intel_crtc_update_dpms(encoder->base.crtc);
3954
		intel_crtc_update_dpms(encoder->base.crtc);
3955
	}
3955
	}
3956
}
3956
}
3957
 
3957
 
3958
/* Cross check the actual hw state with our own modeset state tracking (and it's
3958
/* Cross check the actual hw state with our own modeset state tracking (and it's
3959
 * internal consistency). */
3959
 * internal consistency). */
3960
static void intel_connector_check_state(struct intel_connector *connector)
3960
static void intel_connector_check_state(struct intel_connector *connector)
3961
{
3961
{
3962
	if (connector->get_hw_state(connector)) {
3962
	if (connector->get_hw_state(connector)) {
3963
		struct intel_encoder *encoder = connector->encoder;
3963
		struct intel_encoder *encoder = connector->encoder;
3964
		struct drm_crtc *crtc;
3964
		struct drm_crtc *crtc;
3965
		bool encoder_enabled;
3965
		bool encoder_enabled;
3966
		enum pipe pipe;
3966
		enum pipe pipe;
3967
 
3967
 
3968
		DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
3968
		DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
3969
			      connector->base.base.id,
3969
			      connector->base.base.id,
3970
			      drm_get_connector_name(&connector->base));
3970
			      drm_get_connector_name(&connector->base));
3971
 
3971
 
3972
		WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
3972
		WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
3973
		     "wrong connector dpms state\n");
3973
		     "wrong connector dpms state\n");
3974
		WARN(connector->base.encoder != &encoder->base,
3974
		WARN(connector->base.encoder != &encoder->base,
3975
		     "active connector not linked to encoder\n");
3975
		     "active connector not linked to encoder\n");
3976
		WARN(!encoder->connectors_active,
3976
		WARN(!encoder->connectors_active,
3977
		     "encoder->connectors_active not set\n");
3977
		     "encoder->connectors_active not set\n");
3978
 
3978
 
3979
		encoder_enabled = encoder->get_hw_state(encoder, &pipe);
3979
		encoder_enabled = encoder->get_hw_state(encoder, &pipe);
3980
		WARN(!encoder_enabled, "encoder not enabled\n");
3980
		WARN(!encoder_enabled, "encoder not enabled\n");
3981
		if (WARN_ON(!encoder->base.crtc))
3981
		if (WARN_ON(!encoder->base.crtc))
3982
			return;
3982
			return;
3983
 
3983
 
3984
		crtc = encoder->base.crtc;
3984
		crtc = encoder->base.crtc;
3985
 
3985
 
3986
		WARN(!crtc->enabled, "crtc not enabled\n");
3986
		WARN(!crtc->enabled, "crtc not enabled\n");
3987
		WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
3987
		WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
3988
		WARN(pipe != to_intel_crtc(crtc)->pipe,
3988
		WARN(pipe != to_intel_crtc(crtc)->pipe,
3989
		     "encoder active on the wrong pipe\n");
3989
		     "encoder active on the wrong pipe\n");
3990
	}
3990
	}
3991
}
3991
}
3992
 
3992
 
3993
/* Even simpler default implementation, if there's really no special case to
3993
/* Even simpler default implementation, if there's really no special case to
3994
 * consider. */
3994
 * consider. */
3995
void intel_connector_dpms(struct drm_connector *connector, int mode)
3995
void intel_connector_dpms(struct drm_connector *connector, int mode)
3996
{
3996
{
3997
	/* All the simple cases only support two dpms states. */
3997
	/* All the simple cases only support two dpms states. */
3998
	if (mode != DRM_MODE_DPMS_ON)
3998
	if (mode != DRM_MODE_DPMS_ON)
3999
		mode = DRM_MODE_DPMS_OFF;
3999
		mode = DRM_MODE_DPMS_OFF;
4000
 
4000
 
4001
	if (mode == connector->dpms)
4001
	if (mode == connector->dpms)
4002
		return;
4002
		return;
4003
 
4003
 
4004
	connector->dpms = mode;
4004
	connector->dpms = mode;
4005
 
4005
 
4006
	/* Only need to change hw state when actually enabled */
4006
	/* Only need to change hw state when actually enabled */
4007
	if (connector->encoder)
4007
	if (connector->encoder)
4008
		intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
4008
		intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
4009
 
4009
 
4010
	intel_modeset_check_state(connector->dev);
4010
	intel_modeset_check_state(connector->dev);
4011
}
4011
}
4012
 
4012
 
4013
/* Simple connector->get_hw_state implementation for encoders that support only
4013
/* Simple connector->get_hw_state implementation for encoders that support only
4014
 * one connector and no cloning and hence the encoder state determines the state
4014
 * one connector and no cloning and hence the encoder state determines the state
4015
 * of the connector. */
4015
 * of the connector. */
4016
bool intel_connector_get_hw_state(struct intel_connector *connector)
4016
bool intel_connector_get_hw_state(struct intel_connector *connector)
4017
{
4017
{
4018
	enum pipe pipe = 0;
4018
	enum pipe pipe = 0;
4019
	struct intel_encoder *encoder = connector->encoder;
4019
	struct intel_encoder *encoder = connector->encoder;
4020
 
4020
 
4021
	return encoder->get_hw_state(encoder, &pipe);
4021
	return encoder->get_hw_state(encoder, &pipe);
4022
}
4022
}
4023
 
4023
 
4024
static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
4024
static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
4025
				     struct intel_crtc_config *pipe_config)
4025
				     struct intel_crtc_config *pipe_config)
4026
{
4026
{
4027
	struct drm_i915_private *dev_priv = dev->dev_private;
4027
	struct drm_i915_private *dev_priv = dev->dev_private;
4028
	struct intel_crtc *pipe_B_crtc =
4028
	struct intel_crtc *pipe_B_crtc =
4029
		to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
4029
		to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
4030
 
4030
 
4031
	DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
4031
	DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
4032
		      pipe_name(pipe), pipe_config->fdi_lanes);
4032
		      pipe_name(pipe), pipe_config->fdi_lanes);
4033
	if (pipe_config->fdi_lanes > 4) {
4033
	if (pipe_config->fdi_lanes > 4) {
4034
		DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
4034
		DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
4035
			      pipe_name(pipe), pipe_config->fdi_lanes);
4035
			      pipe_name(pipe), pipe_config->fdi_lanes);
4036
		return false;
4036
		return false;
4037
	}
4037
	}
4038
 
4038
 
4039
	if (IS_HASWELL(dev)) {
4039
	if (IS_HASWELL(dev)) {
4040
		if (pipe_config->fdi_lanes > 2) {
4040
		if (pipe_config->fdi_lanes > 2) {
4041
			DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
4041
			DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
4042
				      pipe_config->fdi_lanes);
4042
				      pipe_config->fdi_lanes);
4043
			return false;
4043
			return false;
4044
		} else {
4044
		} else {
4045
			return true;
4045
			return true;
4046
		}
4046
		}
4047
	}
4047
	}
4048
 
4048
 
4049
	if (INTEL_INFO(dev)->num_pipes == 2)
4049
	if (INTEL_INFO(dev)->num_pipes == 2)
4050
		return true;
4050
		return true;
4051
 
4051
 
4052
	/* Ivybridge 3 pipe is really complicated */
4052
	/* Ivybridge 3 pipe is really complicated */
4053
	switch (pipe) {
4053
	switch (pipe) {
4054
	case PIPE_A:
4054
	case PIPE_A:
4055
		return true;
4055
		return true;
4056
	case PIPE_B:
4056
	case PIPE_B:
4057
		if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
4057
		if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
4058
		    pipe_config->fdi_lanes > 2) {
4058
		    pipe_config->fdi_lanes > 2) {
4059
			DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4059
			DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4060
				      pipe_name(pipe), pipe_config->fdi_lanes);
4060
				      pipe_name(pipe), pipe_config->fdi_lanes);
4061
			return false;
4061
			return false;
4062
		}
4062
		}
4063
		return true;
4063
		return true;
4064
	case PIPE_C:
4064
	case PIPE_C:
4065
		if (!pipe_has_enabled_pch(pipe_B_crtc) ||
4065
		if (!pipe_has_enabled_pch(pipe_B_crtc) ||
4066
		    pipe_B_crtc->config.fdi_lanes <= 2) {
4066
		    pipe_B_crtc->config.fdi_lanes <= 2) {
4067
			if (pipe_config->fdi_lanes > 2) {
4067
			if (pipe_config->fdi_lanes > 2) {
4068
				DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4068
				DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4069
					      pipe_name(pipe), pipe_config->fdi_lanes);
4069
					      pipe_name(pipe), pipe_config->fdi_lanes);
4070
				return false;
4070
				return false;
4071
			}
4071
			}
4072
		} else {
4072
		} else {
4073
			DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
4073
			DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
4074
			return false;
4074
			return false;
4075
		}
4075
		}
4076
		return true;
4076
		return true;
4077
	default:
4077
	default:
4078
		BUG();
4078
		BUG();
4079
	}
4079
	}
4080
}
4080
}
4081
 
4081
 
4082
#define RETRY 1
4082
#define RETRY 1
4083
static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
4083
static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
4084
				      struct intel_crtc_config *pipe_config)
4084
				      struct intel_crtc_config *pipe_config)
4085
{
4085
{
4086
	struct drm_device *dev = intel_crtc->base.dev;
4086
	struct drm_device *dev = intel_crtc->base.dev;
4087
	struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
4087
	struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
4088
	int lane, link_bw, fdi_dotclock;
4088
	int lane, link_bw, fdi_dotclock;
4089
	bool setup_ok, needs_recompute = false;
4089
	bool setup_ok, needs_recompute = false;
4090
 
4090
 
4091
retry:
4091
retry:
4092
	/* FDI is a binary signal running at ~2.7GHz, encoding
4092
	/* FDI is a binary signal running at ~2.7GHz, encoding
4093
	 * each output octet as 10 bits. The actual frequency
4093
	 * each output octet as 10 bits. The actual frequency
4094
	 * is stored as a divider into a 100MHz clock, and the
4094
	 * is stored as a divider into a 100MHz clock, and the
4095
	 * mode pixel clock is stored in units of 1KHz.
4095
	 * mode pixel clock is stored in units of 1KHz.
4096
	 * Hence the bw of each lane in terms of the mode signal
4096
	 * Hence the bw of each lane in terms of the mode signal
4097
	 * is:
4097
	 * is:
4098
	 */
4098
	 */
4099
	link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
4099
	link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
4100
 
4100
 
4101
	fdi_dotclock = adjusted_mode->clock;
4101
	fdi_dotclock = adjusted_mode->clock;
4102
	fdi_dotclock /= pipe_config->pixel_multiplier;
4102
	fdi_dotclock /= pipe_config->pixel_multiplier;
4103
 
4103
 
4104
	lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
4104
	lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
4105
					   pipe_config->pipe_bpp);
4105
					   pipe_config->pipe_bpp);
4106
 
4106
 
4107
	pipe_config->fdi_lanes = lane;
4107
	pipe_config->fdi_lanes = lane;
4108
 
4108
 
4109
	intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
4109
	intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
4110
			       link_bw, &pipe_config->fdi_m_n);
4110
			       link_bw, &pipe_config->fdi_m_n);
4111
 
4111
 
4112
	setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
4112
	setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
4113
					    intel_crtc->pipe, pipe_config);
4113
					    intel_crtc->pipe, pipe_config);
4114
	if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
4114
	if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
4115
		pipe_config->pipe_bpp -= 2*3;
4115
		pipe_config->pipe_bpp -= 2*3;
4116
		DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
4116
		DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
4117
			      pipe_config->pipe_bpp);
4117
			      pipe_config->pipe_bpp);
4118
		needs_recompute = true;
4118
		needs_recompute = true;
4119
		pipe_config->bw_constrained = true;
4119
		pipe_config->bw_constrained = true;
4120
 
4120
 
4121
		goto retry;
4121
		goto retry;
4122
	}
4122
	}
4123
 
4123
 
4124
	if (needs_recompute)
4124
	if (needs_recompute)
4125
		return RETRY;
4125
		return RETRY;
4126
 
4126
 
4127
	return setup_ok ? 0 : -EINVAL;
4127
	return setup_ok ? 0 : -EINVAL;
4128
}
4128
}
4129
 
4129
 
4130
static void hsw_compute_ips_config(struct intel_crtc *crtc,
4130
static void hsw_compute_ips_config(struct intel_crtc *crtc,
4131
				   struct intel_crtc_config *pipe_config)
4131
				   struct intel_crtc_config *pipe_config)
4132
{
4132
{
4133
	pipe_config->ips_enabled = i915_enable_ips &&
4133
	pipe_config->ips_enabled = i915_enable_ips &&
4134
				   hsw_crtc_supports_ips(crtc) &&
4134
				   hsw_crtc_supports_ips(crtc) &&
4135
				   pipe_config->pipe_bpp <= 24;
4135
				   pipe_config->pipe_bpp <= 24;
4136
}
4136
}
4137
 
4137
 
4138
static int intel_crtc_compute_config(struct intel_crtc *crtc,
4138
static int intel_crtc_compute_config(struct intel_crtc *crtc,
4139
				     struct intel_crtc_config *pipe_config)
4139
				     struct intel_crtc_config *pipe_config)
4140
{
4140
{
4141
	struct drm_device *dev = crtc->base.dev;
4141
	struct drm_device *dev = crtc->base.dev;
4142
	struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
4142
	struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
4143
 
4143
 
4144
	if (HAS_PCH_SPLIT(dev)) {
4144
	if (HAS_PCH_SPLIT(dev)) {
4145
		/* FDI link clock is fixed at 2.7G */
4145
		/* FDI link clock is fixed at 2.7G */
4146
		if (pipe_config->requested_mode.clock * 3
4146
		if (pipe_config->requested_mode.clock * 3
4147
		    > IRONLAKE_FDI_FREQ * 4)
4147
		    > IRONLAKE_FDI_FREQ * 4)
4148
			return -EINVAL;
4148
			return -EINVAL;
4149
	}
4149
	}
4150
 
4150
 
4151
	/* Cantiga+ cannot handle modes with a hsync front porch of 0.
4151
	/* Cantiga+ cannot handle modes with a hsync front porch of 0.
4152
	 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
4152
	 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
4153
	 */
4153
	 */
4154
	if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
4154
	if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
4155
		adjusted_mode->hsync_start == adjusted_mode->hdisplay)
4155
		adjusted_mode->hsync_start == adjusted_mode->hdisplay)
4156
		return -EINVAL;
4156
		return -EINVAL;
4157
 
4157
 
4158
	if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
4158
	if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
4159
		pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
4159
		pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
4160
	} else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
4160
	} else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
4161
		/* only a 8bpc pipe, with 6bpc dither through the panel fitter
4161
		/* only a 8bpc pipe, with 6bpc dither through the panel fitter
4162
		 * for lvds. */
4162
		 * for lvds. */
4163
		pipe_config->pipe_bpp = 8*3;
4163
		pipe_config->pipe_bpp = 8*3;
4164
	}
4164
	}
4165
 
4165
 
4166
	if (HAS_IPS(dev))
4166
	if (HAS_IPS(dev))
4167
		hsw_compute_ips_config(crtc, pipe_config);
4167
		hsw_compute_ips_config(crtc, pipe_config);
4168
 
4168
 
4169
	/* XXX: PCH clock sharing is done in ->mode_set, so make sure the old
4169
	/* XXX: PCH clock sharing is done in ->mode_set, so make sure the old
4170
	 * clock survives for now. */
4170
	 * clock survives for now. */
4171
	if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
4171
	if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
4172
		pipe_config->shared_dpll = crtc->config.shared_dpll;
4172
		pipe_config->shared_dpll = crtc->config.shared_dpll;
4173
 
4173
 
4174
	if (pipe_config->has_pch_encoder)
4174
	if (pipe_config->has_pch_encoder)
4175
		return ironlake_fdi_compute_config(crtc, pipe_config);
4175
		return ironlake_fdi_compute_config(crtc, pipe_config);
4176
 
4176
 
4177
	return 0;
4177
	return 0;
4178
}
4178
}
4179
 
4179
 
4180
static int valleyview_get_display_clock_speed(struct drm_device *dev)
4180
static int valleyview_get_display_clock_speed(struct drm_device *dev)
4181
{
4181
{
4182
	return 400000; /* FIXME */
4182
	return 400000; /* FIXME */
4183
}
4183
}
4184
 
4184
 
4185
static int i945_get_display_clock_speed(struct drm_device *dev)
4185
static int i945_get_display_clock_speed(struct drm_device *dev)
4186
{
4186
{
4187
	return 400000;
4187
	return 400000;
4188
}
4188
}
4189
 
4189
 
4190
static int i915_get_display_clock_speed(struct drm_device *dev)
4190
static int i915_get_display_clock_speed(struct drm_device *dev)
4191
{
4191
{
4192
	return 333000;
4192
	return 333000;
4193
}
4193
}
4194
 
4194
 
4195
static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
4195
static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
4196
{
4196
{
4197
	return 200000;
4197
	return 200000;
4198
}
4198
}
4199
 
4199
 
4200
static int pnv_get_display_clock_speed(struct drm_device *dev)
4200
static int pnv_get_display_clock_speed(struct drm_device *dev)
4201
{
4201
{
4202
	u16 gcfgc = 0;
4202
	u16 gcfgc = 0;
4203
 
4203
 
4204
	pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4204
	pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4205
 
4205
 
4206
	switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4206
	switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4207
	case GC_DISPLAY_CLOCK_267_MHZ_PNV:
4207
	case GC_DISPLAY_CLOCK_267_MHZ_PNV:
4208
		return 267000;
4208
		return 267000;
4209
	case GC_DISPLAY_CLOCK_333_MHZ_PNV:
4209
	case GC_DISPLAY_CLOCK_333_MHZ_PNV:
4210
		return 333000;
4210
		return 333000;
4211
	case GC_DISPLAY_CLOCK_444_MHZ_PNV:
4211
	case GC_DISPLAY_CLOCK_444_MHZ_PNV:
4212
		return 444000;
4212
		return 444000;
4213
	case GC_DISPLAY_CLOCK_200_MHZ_PNV:
4213
	case GC_DISPLAY_CLOCK_200_MHZ_PNV:
4214
		return 200000;
4214
		return 200000;
4215
	default:
4215
	default:
4216
		DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
4216
		DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
4217
	case GC_DISPLAY_CLOCK_133_MHZ_PNV:
4217
	case GC_DISPLAY_CLOCK_133_MHZ_PNV:
4218
		return 133000;
4218
		return 133000;
4219
	case GC_DISPLAY_CLOCK_167_MHZ_PNV:
4219
	case GC_DISPLAY_CLOCK_167_MHZ_PNV:
4220
		return 167000;
4220
		return 167000;
4221
	}
4221
	}
4222
}
4222
}
4223
 
4223
 
4224
static int i915gm_get_display_clock_speed(struct drm_device *dev)
4224
static int i915gm_get_display_clock_speed(struct drm_device *dev)
4225
{
4225
{
4226
	u16 gcfgc = 0;
4226
	u16 gcfgc = 0;
4227
 
4227
 
4228
	pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4228
	pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4229
 
4229
 
4230
	if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
4230
	if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
4231
		return 133000;
4231
		return 133000;
4232
	else {
4232
	else {
4233
		switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4233
		switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4234
		case GC_DISPLAY_CLOCK_333_MHZ:
4234
		case GC_DISPLAY_CLOCK_333_MHZ:
4235
			return 333000;
4235
			return 333000;
4236
		default:
4236
		default:
4237
		case GC_DISPLAY_CLOCK_190_200_MHZ:
4237
		case GC_DISPLAY_CLOCK_190_200_MHZ:
4238
			return 190000;
4238
			return 190000;
4239
		}
4239
		}
4240
	}
4240
	}
4241
}
4241
}
4242
 
4242
 
4243
static int i865_get_display_clock_speed(struct drm_device *dev)
4243
static int i865_get_display_clock_speed(struct drm_device *dev)
4244
{
4244
{
4245
	return 266000;
4245
	return 266000;
4246
}
4246
}
4247
 
4247
 
4248
static int i855_get_display_clock_speed(struct drm_device *dev)
4248
static int i855_get_display_clock_speed(struct drm_device *dev)
4249
{
4249
{
4250
	u16 hpllcc = 0;
4250
	u16 hpllcc = 0;
4251
	/* Assume that the hardware is in the high speed state.  This
4251
	/* Assume that the hardware is in the high speed state.  This
4252
	 * should be the default.
4252
	 * should be the default.
4253
	 */
4253
	 */
4254
	switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
4254
	switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
4255
	case GC_CLOCK_133_200:
4255
	case GC_CLOCK_133_200:
4256
	case GC_CLOCK_100_200:
4256
	case GC_CLOCK_100_200:
4257
		return 200000;
4257
		return 200000;
4258
	case GC_CLOCK_166_250:
4258
	case GC_CLOCK_166_250:
4259
		return 250000;
4259
		return 250000;
4260
	case GC_CLOCK_100_133:
4260
	case GC_CLOCK_100_133:
4261
		return 133000;
4261
		return 133000;
4262
	}
4262
	}
4263
 
4263
 
4264
	/* Shouldn't happen */
4264
	/* Shouldn't happen */
4265
	return 0;
4265
	return 0;
4266
}
4266
}
4267
 
4267
 
4268
static int i830_get_display_clock_speed(struct drm_device *dev)
4268
static int i830_get_display_clock_speed(struct drm_device *dev)
4269
{
4269
{
4270
	return 133000;
4270
	return 133000;
4271
}
4271
}
4272
 
4272
 
4273
static void
4273
static void
4274
intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
4274
intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
4275
{
4275
{
4276
	while (*num > DATA_LINK_M_N_MASK ||
4276
	while (*num > DATA_LINK_M_N_MASK ||
4277
	       *den > DATA_LINK_M_N_MASK) {
4277
	       *den > DATA_LINK_M_N_MASK) {
4278
		*num >>= 1;
4278
		*num >>= 1;
4279
		*den >>= 1;
4279
		*den >>= 1;
4280
	}
4280
	}
4281
}
4281
}
4282
 
4282
 
4283
static void compute_m_n(unsigned int m, unsigned int n,
4283
static void compute_m_n(unsigned int m, unsigned int n,
4284
			uint32_t *ret_m, uint32_t *ret_n)
4284
			uint32_t *ret_m, uint32_t *ret_n)
4285
{
4285
{
4286
	*ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
4286
	*ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
4287
	*ret_m = div_u64((uint64_t) m * *ret_n, n);
4287
	*ret_m = div_u64((uint64_t) m * *ret_n, n);
4288
	intel_reduce_m_n_ratio(ret_m, ret_n);
4288
	intel_reduce_m_n_ratio(ret_m, ret_n);
4289
}
4289
}
4290
 
4290
 
4291
void
4291
void
4292
intel_link_compute_m_n(int bits_per_pixel, int nlanes,
4292
intel_link_compute_m_n(int bits_per_pixel, int nlanes,
4293
		       int pixel_clock, int link_clock,
4293
		       int pixel_clock, int link_clock,
4294
		       struct intel_link_m_n *m_n)
4294
		       struct intel_link_m_n *m_n)
4295
{
4295
{
4296
	m_n->tu = 64;
4296
	m_n->tu = 64;
4297
 
4297
 
4298
	compute_m_n(bits_per_pixel * pixel_clock,
4298
	compute_m_n(bits_per_pixel * pixel_clock,
4299
		    link_clock * nlanes * 8,
4299
		    link_clock * nlanes * 8,
4300
		    &m_n->gmch_m, &m_n->gmch_n);
4300
		    &m_n->gmch_m, &m_n->gmch_n);
4301
 
4301
 
4302
	compute_m_n(pixel_clock, link_clock,
4302
	compute_m_n(pixel_clock, link_clock,
4303
		    &m_n->link_m, &m_n->link_n);
4303
		    &m_n->link_m, &m_n->link_n);
4304
}
4304
}
4305
 
4305
 
4306
static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
4306
static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
4307
{
4307
{
4308
	if (i915_panel_use_ssc >= 0)
4308
	if (i915_panel_use_ssc >= 0)
4309
		return i915_panel_use_ssc != 0;
4309
		return i915_panel_use_ssc != 0;
4310
	return dev_priv->vbt.lvds_use_ssc
4310
	return dev_priv->vbt.lvds_use_ssc
4311
		&& !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
4311
		&& !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
4312
}
4312
}
4313
 
4313
 
4314
static int vlv_get_refclk(struct drm_crtc *crtc)
4314
static int vlv_get_refclk(struct drm_crtc *crtc)
4315
{
4315
{
4316
	struct drm_device *dev = crtc->dev;
4316
	struct drm_device *dev = crtc->dev;
4317
	struct drm_i915_private *dev_priv = dev->dev_private;
4317
	struct drm_i915_private *dev_priv = dev->dev_private;
4318
	int refclk = 27000; /* for DP & HDMI */
4318
	int refclk = 27000; /* for DP & HDMI */
4319
 
4319
 
4320
	return 100000; /* only one validated so far */
4320
	return 100000; /* only one validated so far */
4321
 
4321
 
4322
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
4322
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
4323
		refclk = 96000;
4323
		refclk = 96000;
4324
	} else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
4324
	} else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
4325
		if (intel_panel_use_ssc(dev_priv))
4325
		if (intel_panel_use_ssc(dev_priv))
4326
			refclk = 100000;
4326
			refclk = 100000;
4327
		else
4327
		else
4328
			refclk = 96000;
4328
			refclk = 96000;
4329
	} else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
4329
	} else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
4330
		refclk = 100000;
4330
		refclk = 100000;
4331
	}
4331
	}
4332
 
4332
 
4333
	return refclk;
4333
	return refclk;
4334
}
4334
}
4335
 
4335
 
4336
static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
4336
static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
4337
{
4337
{
4338
	struct drm_device *dev = crtc->dev;
4338
	struct drm_device *dev = crtc->dev;
4339
	struct drm_i915_private *dev_priv = dev->dev_private;
4339
	struct drm_i915_private *dev_priv = dev->dev_private;
4340
	int refclk;
4340
	int refclk;
4341
 
4341
 
4342
	if (IS_VALLEYVIEW(dev)) {
4342
	if (IS_VALLEYVIEW(dev)) {
4343
		refclk = vlv_get_refclk(crtc);
4343
		refclk = vlv_get_refclk(crtc);
4344
	} else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
4344
	} else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
4345
	    intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
4345
	    intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
4346
		refclk = dev_priv->vbt.lvds_ssc_freq * 1000;
4346
		refclk = dev_priv->vbt.lvds_ssc_freq * 1000;
4347
		DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
4347
		DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
4348
			      refclk / 1000);
4348
			      refclk / 1000);
4349
	} else if (!IS_GEN2(dev)) {
4349
	} else if (!IS_GEN2(dev)) {
4350
		refclk = 96000;
4350
		refclk = 96000;
4351
	} else {
4351
	} else {
4352
		refclk = 48000;
4352
		refclk = 48000;
4353
	}
4353
	}
4354
 
4354
 
4355
	return refclk;
4355
	return refclk;
4356
}
4356
}
4357
 
4357
 
4358
static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
4358
static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
4359
{
4359
{
4360
	return (1 << dpll->n) << 16 | dpll->m2;
4360
	return (1 << dpll->n) << 16 | dpll->m2;
4361
}
4361
}
4362
 
4362
 
4363
static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
4363
static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
4364
{
4364
{
4365
	return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
4365
	return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
4366
}
4366
}
4367
 
4367
 
4368
static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
4368
static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
4369
				     intel_clock_t *reduced_clock)
4369
				     intel_clock_t *reduced_clock)
4370
{
4370
{
4371
	struct drm_device *dev = crtc->base.dev;
4371
	struct drm_device *dev = crtc->base.dev;
4372
	struct drm_i915_private *dev_priv = dev->dev_private;
4372
	struct drm_i915_private *dev_priv = dev->dev_private;
4373
	int pipe = crtc->pipe;
4373
	int pipe = crtc->pipe;
4374
	u32 fp, fp2 = 0;
4374
	u32 fp, fp2 = 0;
4375
 
4375
 
4376
	if (IS_PINEVIEW(dev)) {
4376
	if (IS_PINEVIEW(dev)) {
4377
		fp = pnv_dpll_compute_fp(&crtc->config.dpll);
4377
		fp = pnv_dpll_compute_fp(&crtc->config.dpll);
4378
		if (reduced_clock)
4378
		if (reduced_clock)
4379
			fp2 = pnv_dpll_compute_fp(reduced_clock);
4379
			fp2 = pnv_dpll_compute_fp(reduced_clock);
4380
	} else {
4380
	} else {
4381
		fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
4381
		fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
4382
		if (reduced_clock)
4382
		if (reduced_clock)
4383
			fp2 = i9xx_dpll_compute_fp(reduced_clock);
4383
			fp2 = i9xx_dpll_compute_fp(reduced_clock);
4384
	}
4384
	}
4385
 
4385
 
4386
	I915_WRITE(FP0(pipe), fp);
4386
	I915_WRITE(FP0(pipe), fp);
4387
	crtc->config.dpll_hw_state.fp0 = fp;
4387
	crtc->config.dpll_hw_state.fp0 = fp;
4388
 
4388
 
4389
	crtc->lowfreq_avail = false;
4389
	crtc->lowfreq_avail = false;
4390
	if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
4390
	if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
4391
	    reduced_clock && i915_powersave) {
4391
	    reduced_clock && i915_powersave) {
4392
		I915_WRITE(FP1(pipe), fp2);
4392
		I915_WRITE(FP1(pipe), fp2);
4393
		crtc->config.dpll_hw_state.fp1 = fp2;
4393
		crtc->config.dpll_hw_state.fp1 = fp2;
4394
		crtc->lowfreq_avail = true;
4394
		crtc->lowfreq_avail = true;
4395
	} else {
4395
	} else {
4396
		I915_WRITE(FP1(pipe), fp);
4396
		I915_WRITE(FP1(pipe), fp);
4397
		crtc->config.dpll_hw_state.fp1 = fp;
4397
		crtc->config.dpll_hw_state.fp1 = fp;
4398
	}
4398
	}
4399
}
4399
}
4400
 
4400
 
4401
static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv)
4401
static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv)
4402
{
4402
{
4403
	u32 reg_val;
4403
	u32 reg_val;
4404
 
4404
 
4405
	/*
4405
	/*
4406
	 * PLLB opamp always calibrates to max value of 0x3f, force enable it
4406
	 * PLLB opamp always calibrates to max value of 0x3f, force enable it
4407
	 * and set it to a reasonable value instead.
4407
	 * and set it to a reasonable value instead.
4408
	 */
4408
	 */
4409
	reg_val = vlv_dpio_read(dev_priv, DPIO_IREF(1));
4409
	reg_val = vlv_dpio_read(dev_priv, DPIO_IREF(1));
4410
	reg_val &= 0xffffff00;
4410
	reg_val &= 0xffffff00;
4411
	reg_val |= 0x00000030;
4411
	reg_val |= 0x00000030;
4412
	vlv_dpio_write(dev_priv, DPIO_IREF(1), reg_val);
4412
	vlv_dpio_write(dev_priv, DPIO_IREF(1), reg_val);
4413
 
4413
 
4414
	reg_val = vlv_dpio_read(dev_priv, DPIO_CALIBRATION);
4414
	reg_val = vlv_dpio_read(dev_priv, DPIO_CALIBRATION);
4415
	reg_val &= 0x8cffffff;
4415
	reg_val &= 0x8cffffff;
4416
	reg_val = 0x8c000000;
4416
	reg_val = 0x8c000000;
4417
	vlv_dpio_write(dev_priv, DPIO_CALIBRATION, reg_val);
4417
	vlv_dpio_write(dev_priv, DPIO_CALIBRATION, reg_val);
4418
 
4418
 
4419
	reg_val = vlv_dpio_read(dev_priv, DPIO_IREF(1));
4419
	reg_val = vlv_dpio_read(dev_priv, DPIO_IREF(1));
4420
	reg_val &= 0xffffff00;
4420
	reg_val &= 0xffffff00;
4421
	vlv_dpio_write(dev_priv, DPIO_IREF(1), reg_val);
4421
	vlv_dpio_write(dev_priv, DPIO_IREF(1), reg_val);
4422
 
4422
 
4423
	reg_val = vlv_dpio_read(dev_priv, DPIO_CALIBRATION);
4423
	reg_val = vlv_dpio_read(dev_priv, DPIO_CALIBRATION);
4424
	reg_val &= 0x00ffffff;
4424
	reg_val &= 0x00ffffff;
4425
	reg_val |= 0xb0000000;
4425
	reg_val |= 0xb0000000;
4426
	vlv_dpio_write(dev_priv, DPIO_CALIBRATION, reg_val);
4426
	vlv_dpio_write(dev_priv, DPIO_CALIBRATION, reg_val);
4427
}
4427
}
4428
 
4428
 
4429
static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
4429
static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
4430
					 struct intel_link_m_n *m_n)
4430
					 struct intel_link_m_n *m_n)
4431
{
4431
{
4432
	struct drm_device *dev = crtc->base.dev;
4432
	struct drm_device *dev = crtc->base.dev;
4433
	struct drm_i915_private *dev_priv = dev->dev_private;
4433
	struct drm_i915_private *dev_priv = dev->dev_private;
4434
	int pipe = crtc->pipe;
4434
	int pipe = crtc->pipe;
4435
 
4435
 
4436
	I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4436
	I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4437
	I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
4437
	I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
4438
	I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
4438
	I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
4439
	I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
4439
	I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
4440
}
4440
}
4441
 
4441
 
4442
static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
4442
static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
4443
					 struct intel_link_m_n *m_n)
4443
					 struct intel_link_m_n *m_n)
4444
{
4444
{
4445
	struct drm_device *dev = crtc->base.dev;
4445
	struct drm_device *dev = crtc->base.dev;
4446
	struct drm_i915_private *dev_priv = dev->dev_private;
4446
	struct drm_i915_private *dev_priv = dev->dev_private;
4447
	int pipe = crtc->pipe;
4447
	int pipe = crtc->pipe;
4448
	enum transcoder transcoder = crtc->config.cpu_transcoder;
4448
	enum transcoder transcoder = crtc->config.cpu_transcoder;
4449
 
4449
 
4450
	if (INTEL_INFO(dev)->gen >= 5) {
4450
	if (INTEL_INFO(dev)->gen >= 5) {
4451
		I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
4451
		I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
4452
		I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
4452
		I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
4453
		I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
4453
		I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
4454
		I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
4454
		I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
4455
	} else {
4455
	} else {
4456
		I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4456
		I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4457
		I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
4457
		I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
4458
		I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
4458
		I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
4459
		I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
4459
		I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
4460
	}
4460
	}
4461
}
4461
}
4462
 
4462
 
4463
static void intel_dp_set_m_n(struct intel_crtc *crtc)
4463
static void intel_dp_set_m_n(struct intel_crtc *crtc)
4464
{
4464
{
4465
	if (crtc->config.has_pch_encoder)
4465
	if (crtc->config.has_pch_encoder)
4466
		intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4466
		intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4467
	else
4467
	else
4468
		intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4468
		intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4469
}
4469
}
4470
 
4470
 
4471
static void vlv_update_pll(struct intel_crtc *crtc)
4471
static void vlv_update_pll(struct intel_crtc *crtc)
4472
{
4472
{
4473
	struct drm_device *dev = crtc->base.dev;
4473
	struct drm_device *dev = crtc->base.dev;
4474
	struct drm_i915_private *dev_priv = dev->dev_private;
4474
	struct drm_i915_private *dev_priv = dev->dev_private;
4475
	int pipe = crtc->pipe;
4475
	int pipe = crtc->pipe;
4476
	u32 dpll, mdiv;
4476
	u32 dpll, mdiv;
4477
	u32 bestn, bestm1, bestm2, bestp1, bestp2;
4477
	u32 bestn, bestm1, bestm2, bestp1, bestp2;
4478
	u32 coreclk, reg_val, dpll_md;
4478
	u32 coreclk, reg_val, dpll_md;
4479
 
4479
 
4480
	mutex_lock(&dev_priv->dpio_lock);
4480
	mutex_lock(&dev_priv->dpio_lock);
4481
 
4481
 
4482
	bestn = crtc->config.dpll.n;
4482
	bestn = crtc->config.dpll.n;
4483
	bestm1 = crtc->config.dpll.m1;
4483
	bestm1 = crtc->config.dpll.m1;
4484
	bestm2 = crtc->config.dpll.m2;
4484
	bestm2 = crtc->config.dpll.m2;
4485
	bestp1 = crtc->config.dpll.p1;
4485
	bestp1 = crtc->config.dpll.p1;
4486
	bestp2 = crtc->config.dpll.p2;
4486
	bestp2 = crtc->config.dpll.p2;
4487
 
4487
 
4488
	/* See eDP HDMI DPIO driver vbios notes doc */
4488
	/* See eDP HDMI DPIO driver vbios notes doc */
4489
 
4489
 
4490
	/* PLL B needs special handling */
4490
	/* PLL B needs special handling */
4491
	if (pipe)
4491
	if (pipe)
4492
		vlv_pllb_recal_opamp(dev_priv);
4492
		vlv_pllb_recal_opamp(dev_priv);
4493
 
4493
 
4494
	/* Set up Tx target for periodic Rcomp update */
4494
	/* Set up Tx target for periodic Rcomp update */
4495
	vlv_dpio_write(dev_priv, DPIO_IREF_BCAST, 0x0100000f);
4495
	vlv_dpio_write(dev_priv, DPIO_IREF_BCAST, 0x0100000f);
4496
 
4496
 
4497
	/* Disable target IRef on PLL */
4497
	/* Disable target IRef on PLL */
4498
	reg_val = vlv_dpio_read(dev_priv, DPIO_IREF_CTL(pipe));
4498
	reg_val = vlv_dpio_read(dev_priv, DPIO_IREF_CTL(pipe));
4499
	reg_val &= 0x00ffffff;
4499
	reg_val &= 0x00ffffff;
4500
	vlv_dpio_write(dev_priv, DPIO_IREF_CTL(pipe), reg_val);
4500
	vlv_dpio_write(dev_priv, DPIO_IREF_CTL(pipe), reg_val);
4501
 
4501
 
4502
	/* Disable fast lock */
4502
	/* Disable fast lock */
4503
	vlv_dpio_write(dev_priv, DPIO_FASTCLK_DISABLE, 0x610);
4503
	vlv_dpio_write(dev_priv, DPIO_FASTCLK_DISABLE, 0x610);
4504
 
4504
 
4505
	/* Set idtafcrecal before PLL is enabled */
4505
	/* Set idtafcrecal before PLL is enabled */
4506
	mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
4506
	mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
4507
	mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
4507
	mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
4508
	mdiv |= ((bestn << DPIO_N_SHIFT));
4508
	mdiv |= ((bestn << DPIO_N_SHIFT));
4509
	mdiv |= (1 << DPIO_K_SHIFT);
4509
	mdiv |= (1 << DPIO_K_SHIFT);
4510
 
4510
 
4511
	/*
4511
	/*
4512
	 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
4512
	 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
4513
	 * but we don't support that).
4513
	 * but we don't support that).
4514
	 * Note: don't use the DAC post divider as it seems unstable.
4514
	 * Note: don't use the DAC post divider as it seems unstable.
4515
	 */
4515
	 */
4516
	mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
4516
	mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
4517
	vlv_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
4517
	vlv_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
4518
 
4518
 
4519
	mdiv |= DPIO_ENABLE_CALIBRATION;
4519
	mdiv |= DPIO_ENABLE_CALIBRATION;
4520
	vlv_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
4520
	vlv_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
4521
 
4521
 
4522
	/* Set HBR and RBR LPF coefficients */
4522
	/* Set HBR and RBR LPF coefficients */
4523
	if (crtc->config.port_clock == 162000 ||
4523
	if (crtc->config.port_clock == 162000 ||
4524
	    intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
4524
	    intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
4525
	    intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
4525
	    intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
4526
		vlv_dpio_write(dev_priv, DPIO_LPF_COEFF(pipe),
4526
		vlv_dpio_write(dev_priv, DPIO_LPF_COEFF(pipe),
4527
				 0x009f0003);
4527
				 0x009f0003);
4528
	else
4528
	else
4529
		vlv_dpio_write(dev_priv, DPIO_LPF_COEFF(pipe),
4529
		vlv_dpio_write(dev_priv, DPIO_LPF_COEFF(pipe),
4530
				 0x00d0000f);
4530
				 0x00d0000f);
4531
 
4531
 
4532
	if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
4532
	if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
4533
	    intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
4533
	    intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
4534
		/* Use SSC source */
4534
		/* Use SSC source */
4535
		if (!pipe)
4535
		if (!pipe)
4536
			vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
4536
			vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
4537
					 0x0df40000);
4537
					 0x0df40000);
4538
		else
4538
		else
4539
			vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
4539
			vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
4540
					 0x0df70000);
4540
					 0x0df70000);
4541
	} else { /* HDMI or VGA */
4541
	} else { /* HDMI or VGA */
4542
		/* Use bend source */
4542
		/* Use bend source */
4543
		if (!pipe)
4543
		if (!pipe)
4544
			vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
4544
			vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
4545
					 0x0df70000);
4545
					 0x0df70000);
4546
		else
4546
		else
4547
			vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
4547
			vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
4548
					 0x0df40000);
4548
					 0x0df40000);
4549
	}
4549
	}
4550
 
4550
 
4551
	coreclk = vlv_dpio_read(dev_priv, DPIO_CORE_CLK(pipe));
4551
	coreclk = vlv_dpio_read(dev_priv, DPIO_CORE_CLK(pipe));
4552
	coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
4552
	coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
4553
	if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
4553
	if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
4554
	    intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
4554
	    intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
4555
		coreclk |= 0x01000000;
4555
		coreclk |= 0x01000000;
4556
	vlv_dpio_write(dev_priv, DPIO_CORE_CLK(pipe), coreclk);
4556
	vlv_dpio_write(dev_priv, DPIO_CORE_CLK(pipe), coreclk);
4557
 
4557
 
4558
	vlv_dpio_write(dev_priv, DPIO_PLL_CML(pipe), 0x87871000);
4558
	vlv_dpio_write(dev_priv, DPIO_PLL_CML(pipe), 0x87871000);
4559
 
4559
 
4560
	/* Enable DPIO clock input */
4560
	/* Enable DPIO clock input */
4561
	dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
4561
	dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
4562
		DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
4562
		DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
-
 
4563
	/* We should never disable this, set it here for state tracking */
4563
	if (pipe)
4564
	if (pipe == PIPE_B)
4564
		dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
4565
		dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
4565
 
-
 
4566
	dpll |= DPLL_VCO_ENABLE;
4566
	dpll |= DPLL_VCO_ENABLE;
4567
	crtc->config.dpll_hw_state.dpll = dpll;
4567
	crtc->config.dpll_hw_state.dpll = dpll;
4568
 
4568
 
4569
	dpll_md = (crtc->config.pixel_multiplier - 1)
4569
	dpll_md = (crtc->config.pixel_multiplier - 1)
4570
		<< DPLL_MD_UDI_MULTIPLIER_SHIFT;
4570
		<< DPLL_MD_UDI_MULTIPLIER_SHIFT;
4571
	crtc->config.dpll_hw_state.dpll_md = dpll_md;
4571
	crtc->config.dpll_hw_state.dpll_md = dpll_md;
4572
 
4572
 
4573
	if (crtc->config.has_dp_encoder)
4573
	if (crtc->config.has_dp_encoder)
4574
		intel_dp_set_m_n(crtc);
4574
		intel_dp_set_m_n(crtc);
4575
 
4575
 
4576
	mutex_unlock(&dev_priv->dpio_lock);
4576
	mutex_unlock(&dev_priv->dpio_lock);
4577
}
4577
}
4578
 
4578
 
4579
static void i9xx_update_pll(struct intel_crtc *crtc,
4579
static void i9xx_update_pll(struct intel_crtc *crtc,
4580
			    intel_clock_t *reduced_clock,
4580
			    intel_clock_t *reduced_clock,
4581
			    int num_connectors)
4581
			    int num_connectors)
4582
{
4582
{
4583
	struct drm_device *dev = crtc->base.dev;
4583
	struct drm_device *dev = crtc->base.dev;
4584
	struct drm_i915_private *dev_priv = dev->dev_private;
4584
	struct drm_i915_private *dev_priv = dev->dev_private;
4585
	u32 dpll;
4585
	u32 dpll;
4586
	bool is_sdvo;
4586
	bool is_sdvo;
4587
	struct dpll *clock = &crtc->config.dpll;
4587
	struct dpll *clock = &crtc->config.dpll;
4588
 
4588
 
4589
	i9xx_update_pll_dividers(crtc, reduced_clock);
4589
	i9xx_update_pll_dividers(crtc, reduced_clock);
4590
 
4590
 
4591
	is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
4591
	is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
4592
		intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
4592
		intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
4593
 
4593
 
4594
	dpll = DPLL_VGA_MODE_DIS;
4594
	dpll = DPLL_VGA_MODE_DIS;
4595
 
4595
 
4596
	if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
4596
	if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
4597
		dpll |= DPLLB_MODE_LVDS;
4597
		dpll |= DPLLB_MODE_LVDS;
4598
	else
4598
	else
4599
		dpll |= DPLLB_MODE_DAC_SERIAL;
4599
		dpll |= DPLLB_MODE_DAC_SERIAL;
4600
 
4600
 
4601
	if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
4601
	if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
4602
			dpll |= (crtc->config.pixel_multiplier - 1)
4602
			dpll |= (crtc->config.pixel_multiplier - 1)
4603
				<< SDVO_MULTIPLIER_SHIFT_HIRES;
4603
				<< SDVO_MULTIPLIER_SHIFT_HIRES;
4604
		}
4604
		}
4605
 
4605
 
4606
	if (is_sdvo)
4606
	if (is_sdvo)
4607
		dpll |= DPLL_SDVO_HIGH_SPEED;
4607
		dpll |= DPLL_SDVO_HIGH_SPEED;
4608
 
4608
 
4609
	if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
4609
	if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
4610
		dpll |= DPLL_SDVO_HIGH_SPEED;
4610
		dpll |= DPLL_SDVO_HIGH_SPEED;
4611
 
4611
 
4612
	/* compute bitmask from p1 value */
4612
	/* compute bitmask from p1 value */
4613
	if (IS_PINEVIEW(dev))
4613
	if (IS_PINEVIEW(dev))
4614
		dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
4614
		dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
4615
	else {
4615
	else {
4616
		dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4616
		dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4617
		if (IS_G4X(dev) && reduced_clock)
4617
		if (IS_G4X(dev) && reduced_clock)
4618
			dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
4618
			dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
4619
	}
4619
	}
4620
	switch (clock->p2) {
4620
	switch (clock->p2) {
4621
	case 5:
4621
	case 5:
4622
		dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4622
		dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4623
		break;
4623
		break;
4624
	case 7:
4624
	case 7:
4625
		dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4625
		dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4626
		break;
4626
		break;
4627
	case 10:
4627
	case 10:
4628
		dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4628
		dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4629
		break;
4629
		break;
4630
	case 14:
4630
	case 14:
4631
		dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4631
		dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4632
		break;
4632
		break;
4633
	}
4633
	}
4634
	if (INTEL_INFO(dev)->gen >= 4)
4634
	if (INTEL_INFO(dev)->gen >= 4)
4635
		dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
4635
		dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
4636
 
4636
 
4637
	if (crtc->config.sdvo_tv_clock)
4637
	if (crtc->config.sdvo_tv_clock)
4638
		dpll |= PLL_REF_INPUT_TVCLKINBC;
4638
		dpll |= PLL_REF_INPUT_TVCLKINBC;
4639
	else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
4639
	else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
4640
		 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4640
		 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4641
		dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4641
		dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4642
	else
4642
	else
4643
		dpll |= PLL_REF_INPUT_DREFCLK;
4643
		dpll |= PLL_REF_INPUT_DREFCLK;
4644
 
4644
 
4645
	dpll |= DPLL_VCO_ENABLE;
4645
	dpll |= DPLL_VCO_ENABLE;
4646
	crtc->config.dpll_hw_state.dpll = dpll;
4646
	crtc->config.dpll_hw_state.dpll = dpll;
4647
 
4647
 
4648
	if (INTEL_INFO(dev)->gen >= 4) {
4648
	if (INTEL_INFO(dev)->gen >= 4) {
4649
		u32 dpll_md = (crtc->config.pixel_multiplier - 1)
4649
		u32 dpll_md = (crtc->config.pixel_multiplier - 1)
4650
					<< DPLL_MD_UDI_MULTIPLIER_SHIFT;
4650
					<< DPLL_MD_UDI_MULTIPLIER_SHIFT;
4651
		crtc->config.dpll_hw_state.dpll_md = dpll_md;
4651
		crtc->config.dpll_hw_state.dpll_md = dpll_md;
4652
	}
4652
	}
4653
 
4653
 
4654
	if (crtc->config.has_dp_encoder)
4654
	if (crtc->config.has_dp_encoder)
4655
		intel_dp_set_m_n(crtc);
4655
		intel_dp_set_m_n(crtc);
4656
}
4656
}
4657
 
4657
 
4658
static void i8xx_update_pll(struct intel_crtc *crtc,
4658
static void i8xx_update_pll(struct intel_crtc *crtc,
4659
			    intel_clock_t *reduced_clock,
4659
			    intel_clock_t *reduced_clock,
4660
			    int num_connectors)
4660
			    int num_connectors)
4661
{
4661
{
4662
	struct drm_device *dev = crtc->base.dev;
4662
	struct drm_device *dev = crtc->base.dev;
4663
	struct drm_i915_private *dev_priv = dev->dev_private;
4663
	struct drm_i915_private *dev_priv = dev->dev_private;
4664
	u32 dpll;
4664
	u32 dpll;
4665
	struct dpll *clock = &crtc->config.dpll;
4665
	struct dpll *clock = &crtc->config.dpll;
4666
 
4666
 
4667
	i9xx_update_pll_dividers(crtc, reduced_clock);
4667
	i9xx_update_pll_dividers(crtc, reduced_clock);
4668
 
4668
 
4669
	dpll = DPLL_VGA_MODE_DIS;
4669
	dpll = DPLL_VGA_MODE_DIS;
4670
 
4670
 
4671
	if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
4671
	if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
4672
		dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4672
		dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4673
	} else {
4673
	} else {
4674
		if (clock->p1 == 2)
4674
		if (clock->p1 == 2)
4675
			dpll |= PLL_P1_DIVIDE_BY_TWO;
4675
			dpll |= PLL_P1_DIVIDE_BY_TWO;
4676
		else
4676
		else
4677
			dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4677
			dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4678
		if (clock->p2 == 4)
4678
		if (clock->p2 == 4)
4679
			dpll |= PLL_P2_DIVIDE_BY_4;
4679
			dpll |= PLL_P2_DIVIDE_BY_4;
4680
	}
4680
	}
4681
 
4681
 
4682
	if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
4682
	if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
4683
		dpll |= DPLL_DVO_2X_MODE;
4683
		dpll |= DPLL_DVO_2X_MODE;
4684
 
4684
 
4685
	if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
4685
	if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
4686
		 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4686
		 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4687
		dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4687
		dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4688
	else
4688
	else
4689
		dpll |= PLL_REF_INPUT_DREFCLK;
4689
		dpll |= PLL_REF_INPUT_DREFCLK;
4690
 
4690
 
4691
	dpll |= DPLL_VCO_ENABLE;
4691
	dpll |= DPLL_VCO_ENABLE;
4692
	crtc->config.dpll_hw_state.dpll = dpll;
4692
	crtc->config.dpll_hw_state.dpll = dpll;
4693
}
4693
}
4694
 
4694
 
4695
static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
4695
static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
4696
{
4696
{
4697
	struct drm_device *dev = intel_crtc->base.dev;
4697
	struct drm_device *dev = intel_crtc->base.dev;
4698
	struct drm_i915_private *dev_priv = dev->dev_private;
4698
	struct drm_i915_private *dev_priv = dev->dev_private;
4699
	enum pipe pipe = intel_crtc->pipe;
4699
	enum pipe pipe = intel_crtc->pipe;
4700
	enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
4700
	enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
4701
	struct drm_display_mode *adjusted_mode =
4701
	struct drm_display_mode *adjusted_mode =
4702
		&intel_crtc->config.adjusted_mode;
4702
		&intel_crtc->config.adjusted_mode;
4703
	struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
4703
	struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
4704
	uint32_t vsyncshift, crtc_vtotal, crtc_vblank_end;
4704
	uint32_t vsyncshift, crtc_vtotal, crtc_vblank_end;
4705
 
4705
 
4706
	/* We need to be careful not to changed the adjusted mode, for otherwise
4706
	/* We need to be careful not to changed the adjusted mode, for otherwise
4707
	 * the hw state checker will get angry at the mismatch. */
4707
	 * the hw state checker will get angry at the mismatch. */
4708
	crtc_vtotal = adjusted_mode->crtc_vtotal;
4708
	crtc_vtotal = adjusted_mode->crtc_vtotal;
4709
	crtc_vblank_end = adjusted_mode->crtc_vblank_end;
4709
	crtc_vblank_end = adjusted_mode->crtc_vblank_end;
4710
 
4710
 
4711
	if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4711
	if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4712
		/* the chip adds 2 halflines automatically */
4712
		/* the chip adds 2 halflines automatically */
4713
		crtc_vtotal -= 1;
4713
		crtc_vtotal -= 1;
4714
		crtc_vblank_end -= 1;
4714
		crtc_vblank_end -= 1;
4715
		vsyncshift = adjusted_mode->crtc_hsync_start
4715
		vsyncshift = adjusted_mode->crtc_hsync_start
4716
			     - adjusted_mode->crtc_htotal / 2;
4716
			     - adjusted_mode->crtc_htotal / 2;
4717
	} else {
4717
	} else {
4718
		vsyncshift = 0;
4718
		vsyncshift = 0;
4719
	}
4719
	}
4720
 
4720
 
4721
	if (INTEL_INFO(dev)->gen > 3)
4721
	if (INTEL_INFO(dev)->gen > 3)
4722
		I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
4722
		I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
4723
 
4723
 
4724
	I915_WRITE(HTOTAL(cpu_transcoder),
4724
	I915_WRITE(HTOTAL(cpu_transcoder),
4725
		   (adjusted_mode->crtc_hdisplay - 1) |
4725
		   (adjusted_mode->crtc_hdisplay - 1) |
4726
		   ((adjusted_mode->crtc_htotal - 1) << 16));
4726
		   ((adjusted_mode->crtc_htotal - 1) << 16));
4727
	I915_WRITE(HBLANK(cpu_transcoder),
4727
	I915_WRITE(HBLANK(cpu_transcoder),
4728
		   (adjusted_mode->crtc_hblank_start - 1) |
4728
		   (adjusted_mode->crtc_hblank_start - 1) |
4729
		   ((adjusted_mode->crtc_hblank_end - 1) << 16));
4729
		   ((adjusted_mode->crtc_hblank_end - 1) << 16));
4730
	I915_WRITE(HSYNC(cpu_transcoder),
4730
	I915_WRITE(HSYNC(cpu_transcoder),
4731
		   (adjusted_mode->crtc_hsync_start - 1) |
4731
		   (adjusted_mode->crtc_hsync_start - 1) |
4732
		   ((adjusted_mode->crtc_hsync_end - 1) << 16));
4732
		   ((adjusted_mode->crtc_hsync_end - 1) << 16));
4733
 
4733
 
4734
	I915_WRITE(VTOTAL(cpu_transcoder),
4734
	I915_WRITE(VTOTAL(cpu_transcoder),
4735
		   (adjusted_mode->crtc_vdisplay - 1) |
4735
		   (adjusted_mode->crtc_vdisplay - 1) |
4736
		   ((crtc_vtotal - 1) << 16));
4736
		   ((crtc_vtotal - 1) << 16));
4737
	I915_WRITE(VBLANK(cpu_transcoder),
4737
	I915_WRITE(VBLANK(cpu_transcoder),
4738
		   (adjusted_mode->crtc_vblank_start - 1) |
4738
		   (adjusted_mode->crtc_vblank_start - 1) |
4739
		   ((crtc_vblank_end - 1) << 16));
4739
		   ((crtc_vblank_end - 1) << 16));
4740
	I915_WRITE(VSYNC(cpu_transcoder),
4740
	I915_WRITE(VSYNC(cpu_transcoder),
4741
		   (adjusted_mode->crtc_vsync_start - 1) |
4741
		   (adjusted_mode->crtc_vsync_start - 1) |
4742
		   ((adjusted_mode->crtc_vsync_end - 1) << 16));
4742
		   ((adjusted_mode->crtc_vsync_end - 1) << 16));
4743
 
4743
 
4744
	/* Workaround: when the EDP input selection is B, the VTOTAL_B must be
4744
	/* Workaround: when the EDP input selection is B, the VTOTAL_B must be
4745
	 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
4745
	 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
4746
	 * documented on the DDI_FUNC_CTL register description, EDP Input Select
4746
	 * documented on the DDI_FUNC_CTL register description, EDP Input Select
4747
	 * bits. */
4747
	 * bits. */
4748
	if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
4748
	if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
4749
	    (pipe == PIPE_B || pipe == PIPE_C))
4749
	    (pipe == PIPE_B || pipe == PIPE_C))
4750
		I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
4750
		I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
4751
 
4751
 
4752
	/* pipesrc controls the size that is scaled from, which should
4752
	/* pipesrc controls the size that is scaled from, which should
4753
	 * always be the user's requested size.
4753
	 * always be the user's requested size.
4754
	 */
4754
	 */
4755
	I915_WRITE(PIPESRC(pipe),
4755
	I915_WRITE(PIPESRC(pipe),
4756
		   ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
4756
		   ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
4757
}
4757
}
4758
 
4758
 
4759
static void intel_get_pipe_timings(struct intel_crtc *crtc,
4759
static void intel_get_pipe_timings(struct intel_crtc *crtc,
4760
				   struct intel_crtc_config *pipe_config)
4760
				   struct intel_crtc_config *pipe_config)
4761
{
4761
{
4762
	struct drm_device *dev = crtc->base.dev;
4762
	struct drm_device *dev = crtc->base.dev;
4763
	struct drm_i915_private *dev_priv = dev->dev_private;
4763
	struct drm_i915_private *dev_priv = dev->dev_private;
4764
	enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
4764
	enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
4765
	uint32_t tmp;
4765
	uint32_t tmp;
4766
 
4766
 
4767
	tmp = I915_READ(HTOTAL(cpu_transcoder));
4767
	tmp = I915_READ(HTOTAL(cpu_transcoder));
4768
	pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
4768
	pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
4769
	pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
4769
	pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
4770
	tmp = I915_READ(HBLANK(cpu_transcoder));
4770
	tmp = I915_READ(HBLANK(cpu_transcoder));
4771
	pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
4771
	pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
4772
	pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
4772
	pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
4773
	tmp = I915_READ(HSYNC(cpu_transcoder));
4773
	tmp = I915_READ(HSYNC(cpu_transcoder));
4774
	pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
4774
	pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
4775
	pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
4775
	pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
4776
 
4776
 
4777
	tmp = I915_READ(VTOTAL(cpu_transcoder));
4777
	tmp = I915_READ(VTOTAL(cpu_transcoder));
4778
	pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
4778
	pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
4779
	pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
4779
	pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
4780
	tmp = I915_READ(VBLANK(cpu_transcoder));
4780
	tmp = I915_READ(VBLANK(cpu_transcoder));
4781
	pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
4781
	pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
4782
	pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
4782
	pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
4783
	tmp = I915_READ(VSYNC(cpu_transcoder));
4783
	tmp = I915_READ(VSYNC(cpu_transcoder));
4784
	pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
4784
	pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
4785
	pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
4785
	pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
4786
 
4786
 
4787
	if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
4787
	if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
4788
		pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
4788
		pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
4789
		pipe_config->adjusted_mode.crtc_vtotal += 1;
4789
		pipe_config->adjusted_mode.crtc_vtotal += 1;
4790
		pipe_config->adjusted_mode.crtc_vblank_end += 1;
4790
		pipe_config->adjusted_mode.crtc_vblank_end += 1;
4791
	}
4791
	}
4792
 
4792
 
4793
	tmp = I915_READ(PIPESRC(crtc->pipe));
4793
	tmp = I915_READ(PIPESRC(crtc->pipe));
4794
	pipe_config->requested_mode.vdisplay = (tmp & 0xffff) + 1;
4794
	pipe_config->requested_mode.vdisplay = (tmp & 0xffff) + 1;
4795
	pipe_config->requested_mode.hdisplay = ((tmp >> 16) & 0xffff) + 1;
4795
	pipe_config->requested_mode.hdisplay = ((tmp >> 16) & 0xffff) + 1;
4796
}
4796
}
4797
 
4797
 
4798
static void intel_crtc_mode_from_pipe_config(struct intel_crtc *intel_crtc,
4798
static void intel_crtc_mode_from_pipe_config(struct intel_crtc *intel_crtc,
4799
					     struct intel_crtc_config *pipe_config)
4799
					     struct intel_crtc_config *pipe_config)
4800
{
4800
{
4801
	struct drm_crtc *crtc = &intel_crtc->base;
4801
	struct drm_crtc *crtc = &intel_crtc->base;
4802
 
4802
 
4803
	crtc->mode.hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
4803
	crtc->mode.hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
4804
	crtc->mode.htotal = pipe_config->adjusted_mode.crtc_htotal;
4804
	crtc->mode.htotal = pipe_config->adjusted_mode.crtc_htotal;
4805
	crtc->mode.hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
4805
	crtc->mode.hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
4806
	crtc->mode.hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
4806
	crtc->mode.hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
4807
 
4807
 
4808
	crtc->mode.vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
4808
	crtc->mode.vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
4809
	crtc->mode.vtotal = pipe_config->adjusted_mode.crtc_vtotal;
4809
	crtc->mode.vtotal = pipe_config->adjusted_mode.crtc_vtotal;
4810
	crtc->mode.vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
4810
	crtc->mode.vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
4811
	crtc->mode.vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
4811
	crtc->mode.vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
4812
 
4812
 
4813
	crtc->mode.flags = pipe_config->adjusted_mode.flags;
4813
	crtc->mode.flags = pipe_config->adjusted_mode.flags;
4814
 
4814
 
4815
	crtc->mode.clock = pipe_config->adjusted_mode.clock;
4815
	crtc->mode.clock = pipe_config->adjusted_mode.clock;
4816
	crtc->mode.flags |= pipe_config->adjusted_mode.flags;
4816
	crtc->mode.flags |= pipe_config->adjusted_mode.flags;
4817
}
4817
}
4818
 
4818
 
4819
static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
4819
static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
4820
{
4820
{
4821
	struct drm_device *dev = intel_crtc->base.dev;
4821
	struct drm_device *dev = intel_crtc->base.dev;
4822
	struct drm_i915_private *dev_priv = dev->dev_private;
4822
	struct drm_i915_private *dev_priv = dev->dev_private;
4823
	uint32_t pipeconf;
4823
	uint32_t pipeconf;
4824
 
4824
 
4825
	pipeconf = 0;
4825
	pipeconf = 0;
4826
 
4826
 
4827
	if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
4827
	if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
4828
	    I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE)
4828
	    I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE)
4829
		pipeconf |= PIPECONF_ENABLE;
4829
		pipeconf |= PIPECONF_ENABLE;
4830
 
4830
 
4831
	if (intel_crtc->pipe == 0 && INTEL_INFO(dev)->gen < 4) {
4831
	if (intel_crtc->pipe == 0 && INTEL_INFO(dev)->gen < 4) {
4832
		/* Enable pixel doubling when the dot clock is > 90% of the (display)
4832
		/* Enable pixel doubling when the dot clock is > 90% of the (display)
4833
		 * core speed.
4833
		 * core speed.
4834
		 *
4834
		 *
4835
		 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
4835
		 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
4836
		 * pipe == 0 check?
4836
		 * pipe == 0 check?
4837
		 */
4837
		 */
4838
		if (intel_crtc->config.requested_mode.clock >
4838
		if (intel_crtc->config.requested_mode.clock >
4839
		    dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
4839
		    dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
4840
			pipeconf |= PIPECONF_DOUBLE_WIDE;
4840
			pipeconf |= PIPECONF_DOUBLE_WIDE;
4841
	}
4841
	}
4842
 
4842
 
4843
	/* only g4x and later have fancy bpc/dither controls */
4843
	/* only g4x and later have fancy bpc/dither controls */
4844
	if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
4844
	if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
4845
		/* Bspec claims that we can't use dithering for 30bpp pipes. */
4845
		/* Bspec claims that we can't use dithering for 30bpp pipes. */
4846
		if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
4846
		if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
4847
			pipeconf |= PIPECONF_DITHER_EN |
4847
			pipeconf |= PIPECONF_DITHER_EN |
4848
				    PIPECONF_DITHER_TYPE_SP;
4848
				    PIPECONF_DITHER_TYPE_SP;
4849
 
4849
 
4850
		switch (intel_crtc->config.pipe_bpp) {
4850
		switch (intel_crtc->config.pipe_bpp) {
4851
		case 18:
4851
		case 18:
4852
			pipeconf |= PIPECONF_6BPC;
4852
			pipeconf |= PIPECONF_6BPC;
4853
			break;
4853
			break;
4854
		case 24:
4854
		case 24:
4855
			pipeconf |= PIPECONF_8BPC;
4855
			pipeconf |= PIPECONF_8BPC;
4856
			break;
4856
			break;
4857
		case 30:
4857
		case 30:
4858
			pipeconf |= PIPECONF_10BPC;
4858
			pipeconf |= PIPECONF_10BPC;
4859
			break;
4859
			break;
4860
		default:
4860
		default:
4861
			/* Case prevented by intel_choose_pipe_bpp_dither. */
4861
			/* Case prevented by intel_choose_pipe_bpp_dither. */
4862
			BUG();
4862
			BUG();
4863
		}
4863
		}
4864
	}
4864
	}
4865
 
4865
 
4866
	if (HAS_PIPE_CXSR(dev)) {
4866
	if (HAS_PIPE_CXSR(dev)) {
4867
		if (intel_crtc->lowfreq_avail) {
4867
		if (intel_crtc->lowfreq_avail) {
4868
			DRM_DEBUG_KMS("enabling CxSR downclocking\n");
4868
			DRM_DEBUG_KMS("enabling CxSR downclocking\n");
4869
			pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
4869
			pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
4870
		} else {
4870
		} else {
4871
			DRM_DEBUG_KMS("disabling CxSR downclocking\n");
4871
			DRM_DEBUG_KMS("disabling CxSR downclocking\n");
4872
		}
4872
		}
4873
	}
4873
	}
4874
 
4874
 
4875
	if (!IS_GEN2(dev) &&
4875
	if (!IS_GEN2(dev) &&
4876
	    intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
4876
	    intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
4877
		pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
4877
		pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
4878
	else
4878
	else
4879
		pipeconf |= PIPECONF_PROGRESSIVE;
4879
		pipeconf |= PIPECONF_PROGRESSIVE;
4880
 
4880
 
4881
	if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
4881
	if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
4882
			pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
4882
			pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
4883
 
4883
 
4884
	I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
4884
	I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
4885
	POSTING_READ(PIPECONF(intel_crtc->pipe));
4885
	POSTING_READ(PIPECONF(intel_crtc->pipe));
4886
}
4886
}
4887
 
4887
 
4888
static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
4888
static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
4889
			      int x, int y,
4889
			      int x, int y,
4890
			      struct drm_framebuffer *fb)
4890
			      struct drm_framebuffer *fb)
4891
{
4891
{
4892
	struct drm_device *dev = crtc->dev;
4892
	struct drm_device *dev = crtc->dev;
4893
	struct drm_i915_private *dev_priv = dev->dev_private;
4893
	struct drm_i915_private *dev_priv = dev->dev_private;
4894
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4894
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4895
	struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
4895
	struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
4896
	int pipe = intel_crtc->pipe;
4896
	int pipe = intel_crtc->pipe;
4897
	int plane = intel_crtc->plane;
4897
	int plane = intel_crtc->plane;
4898
	int refclk, num_connectors = 0;
4898
	int refclk, num_connectors = 0;
4899
	intel_clock_t clock, reduced_clock;
4899
	intel_clock_t clock, reduced_clock;
4900
	u32 dspcntr;
4900
	u32 dspcntr;
4901
	bool ok, has_reduced_clock = false;
4901
	bool ok, has_reduced_clock = false;
4902
	bool is_lvds = false;
4902
	bool is_lvds = false;
4903
	struct intel_encoder *encoder;
4903
	struct intel_encoder *encoder;
4904
	const intel_limit_t *limit;
4904
	const intel_limit_t *limit;
4905
	int ret;
4905
	int ret;
4906
 
4906
 
4907
	for_each_encoder_on_crtc(dev, crtc, encoder) {
4907
	for_each_encoder_on_crtc(dev, crtc, encoder) {
4908
		switch (encoder->type) {
4908
		switch (encoder->type) {
4909
		case INTEL_OUTPUT_LVDS:
4909
		case INTEL_OUTPUT_LVDS:
4910
			is_lvds = true;
4910
			is_lvds = true;
4911
			break;
4911
			break;
4912
		}
4912
		}
4913
 
4913
 
4914
		num_connectors++;
4914
		num_connectors++;
4915
	}
4915
	}
4916
 
4916
 
4917
	refclk = i9xx_get_refclk(crtc, num_connectors);
4917
	refclk = i9xx_get_refclk(crtc, num_connectors);
4918
 
4918
 
4919
	/*
4919
	/*
4920
	 * Returns a set of divisors for the desired target clock with the given
4920
	 * Returns a set of divisors for the desired target clock with the given
4921
	 * refclk, or FALSE.  The returned values represent the clock equation:
4921
	 * refclk, or FALSE.  The returned values represent the clock equation:
4922
	 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
4922
	 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
4923
	 */
4923
	 */
4924
	limit = intel_limit(crtc, refclk);
4924
	limit = intel_limit(crtc, refclk);
4925
	ok = dev_priv->display.find_dpll(limit, crtc,
4925
	ok = dev_priv->display.find_dpll(limit, crtc,
4926
					 intel_crtc->config.port_clock,
4926
					 intel_crtc->config.port_clock,
4927
					 refclk, NULL, &clock);
4927
					 refclk, NULL, &clock);
4928
	if (!ok && !intel_crtc->config.clock_set) {
4928
	if (!ok && !intel_crtc->config.clock_set) {
4929
		DRM_ERROR("Couldn't find PLL settings for mode!\n");
4929
		DRM_ERROR("Couldn't find PLL settings for mode!\n");
4930
		return -EINVAL;
4930
		return -EINVAL;
4931
	}
4931
	}
4932
 
4932
 
4933
	if (is_lvds && dev_priv->lvds_downclock_avail) {
4933
	if (is_lvds && dev_priv->lvds_downclock_avail) {
4934
		/*
4934
		/*
4935
		 * Ensure we match the reduced clock's P to the target clock.
4935
		 * Ensure we match the reduced clock's P to the target clock.
4936
		 * If the clocks don't match, we can't switch the display clock
4936
		 * If the clocks don't match, we can't switch the display clock
4937
		 * by using the FP0/FP1. In such case we will disable the LVDS
4937
		 * by using the FP0/FP1. In such case we will disable the LVDS
4938
		 * downclock feature.
4938
		 * downclock feature.
4939
		*/
4939
		*/
4940
		has_reduced_clock =
4940
		has_reduced_clock =
4941
			dev_priv->display.find_dpll(limit, crtc,
4941
			dev_priv->display.find_dpll(limit, crtc,
4942
						    dev_priv->lvds_downclock,
4942
						    dev_priv->lvds_downclock,
4943
						    refclk, &clock,
4943
						    refclk, &clock,
4944
						    &reduced_clock);
4944
						    &reduced_clock);
4945
	}
4945
	}
4946
	/* Compat-code for transition, will disappear. */
4946
	/* Compat-code for transition, will disappear. */
4947
	if (!intel_crtc->config.clock_set) {
4947
	if (!intel_crtc->config.clock_set) {
4948
		intel_crtc->config.dpll.n = clock.n;
4948
		intel_crtc->config.dpll.n = clock.n;
4949
		intel_crtc->config.dpll.m1 = clock.m1;
4949
		intel_crtc->config.dpll.m1 = clock.m1;
4950
		intel_crtc->config.dpll.m2 = clock.m2;
4950
		intel_crtc->config.dpll.m2 = clock.m2;
4951
		intel_crtc->config.dpll.p1 = clock.p1;
4951
		intel_crtc->config.dpll.p1 = clock.p1;
4952
		intel_crtc->config.dpll.p2 = clock.p2;
4952
		intel_crtc->config.dpll.p2 = clock.p2;
4953
	}
4953
	}
4954
 
4954
 
4955
	if (IS_GEN2(dev))
4955
	if (IS_GEN2(dev))
4956
		i8xx_update_pll(intel_crtc,
4956
		i8xx_update_pll(intel_crtc,
4957
				has_reduced_clock ? &reduced_clock : NULL,
4957
				has_reduced_clock ? &reduced_clock : NULL,
4958
				num_connectors);
4958
				num_connectors);
4959
	else if (IS_VALLEYVIEW(dev))
4959
	else if (IS_VALLEYVIEW(dev))
4960
		vlv_update_pll(intel_crtc);
4960
		vlv_update_pll(intel_crtc);
4961
	else
4961
	else
4962
		i9xx_update_pll(intel_crtc,
4962
		i9xx_update_pll(intel_crtc,
4963
				has_reduced_clock ? &reduced_clock : NULL,
4963
				has_reduced_clock ? &reduced_clock : NULL,
4964
				num_connectors);
4964
				num_connectors);
4965
 
4965
 
4966
	/* Set up the display plane register */
4966
	/* Set up the display plane register */
4967
	dspcntr = DISPPLANE_GAMMA_ENABLE;
4967
	dspcntr = DISPPLANE_GAMMA_ENABLE;
4968
 
4968
 
4969
	if (!IS_VALLEYVIEW(dev)) {
4969
	if (!IS_VALLEYVIEW(dev)) {
4970
	if (pipe == 0)
4970
	if (pipe == 0)
4971
		dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
4971
		dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
4972
	else
4972
	else
4973
		dspcntr |= DISPPLANE_SEL_PIPE_B;
4973
		dspcntr |= DISPPLANE_SEL_PIPE_B;
4974
	}
4974
	}
4975
 
4975
 
4976
	intel_set_pipe_timings(intel_crtc);
4976
	intel_set_pipe_timings(intel_crtc);
4977
 
4977
 
4978
	/* pipesrc and dspsize control the size that is scaled from,
4978
	/* pipesrc and dspsize control the size that is scaled from,
4979
	 * which should always be the user's requested size.
4979
	 * which should always be the user's requested size.
4980
	 */
4980
	 */
4981
	I915_WRITE(DSPSIZE(plane),
4981
	I915_WRITE(DSPSIZE(plane),
4982
		   ((mode->vdisplay - 1) << 16) |
4982
		   ((mode->vdisplay - 1) << 16) |
4983
		   (mode->hdisplay - 1));
4983
		   (mode->hdisplay - 1));
4984
	I915_WRITE(DSPPOS(plane), 0);
4984
	I915_WRITE(DSPPOS(plane), 0);
4985
 
4985
 
4986
	i9xx_set_pipeconf(intel_crtc);
4986
	i9xx_set_pipeconf(intel_crtc);
4987
 
4987
 
4988
	I915_WRITE(DSPCNTR(plane), dspcntr);
4988
	I915_WRITE(DSPCNTR(plane), dspcntr);
4989
	POSTING_READ(DSPCNTR(plane));
4989
	POSTING_READ(DSPCNTR(plane));
4990
 
4990
 
4991
	ret = intel_pipe_set_base(crtc, x, y, fb);
4991
	ret = intel_pipe_set_base(crtc, x, y, fb);
4992
 
4992
 
4993
	intel_update_watermarks(dev);
4993
	intel_update_watermarks(dev);
4994
 
4994
 
4995
    return ret;
4995
    return ret;
4996
}
4996
}
4997
 
4997
 
4998
static void i9xx_get_pfit_config(struct intel_crtc *crtc,
4998
static void i9xx_get_pfit_config(struct intel_crtc *crtc,
4999
				 struct intel_crtc_config *pipe_config)
4999
				 struct intel_crtc_config *pipe_config)
5000
{
5000
{
5001
	struct drm_device *dev = crtc->base.dev;
5001
	struct drm_device *dev = crtc->base.dev;
5002
	struct drm_i915_private *dev_priv = dev->dev_private;
5002
	struct drm_i915_private *dev_priv = dev->dev_private;
5003
	uint32_t tmp;
5003
	uint32_t tmp;
5004
 
5004
 
5005
	tmp = I915_READ(PFIT_CONTROL);
5005
	tmp = I915_READ(PFIT_CONTROL);
5006
	if (!(tmp & PFIT_ENABLE))
5006
	if (!(tmp & PFIT_ENABLE))
5007
		return;
5007
		return;
5008
 
5008
 
5009
	/* Check whether the pfit is attached to our pipe. */
5009
	/* Check whether the pfit is attached to our pipe. */
5010
	if (INTEL_INFO(dev)->gen < 4) {
5010
	if (INTEL_INFO(dev)->gen < 4) {
5011
		if (crtc->pipe != PIPE_B)
5011
		if (crtc->pipe != PIPE_B)
5012
			return;
5012
			return;
5013
	} else {
5013
	} else {
5014
		if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
5014
		if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
5015
			return;
5015
			return;
5016
	}
5016
	}
5017
 
5017
 
5018
	pipe_config->gmch_pfit.control = tmp;
5018
	pipe_config->gmch_pfit.control = tmp;
5019
	pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
5019
	pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
5020
	if (INTEL_INFO(dev)->gen < 5)
5020
	if (INTEL_INFO(dev)->gen < 5)
5021
		pipe_config->gmch_pfit.lvds_border_bits =
5021
		pipe_config->gmch_pfit.lvds_border_bits =
5022
			I915_READ(LVDS) & LVDS_BORDER_ENABLE;
5022
			I915_READ(LVDS) & LVDS_BORDER_ENABLE;
5023
}
5023
}
-
 
5024
 
-
 
5025
static void vlv_crtc_clock_get(struct intel_crtc *crtc,
-
 
5026
			       struct intel_crtc_config *pipe_config)
-
 
5027
{
-
 
5028
	struct drm_device *dev = crtc->base.dev;
-
 
5029
	struct drm_i915_private *dev_priv = dev->dev_private;
-
 
5030
	int pipe = pipe_config->cpu_transcoder;
-
 
5031
	intel_clock_t clock;
-
 
5032
	u32 mdiv;
-
 
5033
	int refclk = 100000;
-
 
5034
 
-
 
5035
	mutex_lock(&dev_priv->dpio_lock);
-
 
5036
	mdiv = vlv_dpio_read(dev_priv, DPIO_DIV(pipe));
-
 
5037
	mutex_unlock(&dev_priv->dpio_lock);
-
 
5038
 
-
 
5039
	clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
-
 
5040
	clock.m2 = mdiv & DPIO_M2DIV_MASK;
-
 
5041
	clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
-
 
5042
	clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
-
 
5043
	clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
-
 
5044
 
-
 
5045
	clock.vco = refclk * clock.m1 * clock.m2 / clock.n;
-
 
5046
	clock.dot = 2 * clock.vco / (clock.p1 * clock.p2);
-
 
5047
 
-
 
5048
	pipe_config->adjusted_mode.clock = clock.dot / 10;
-
 
5049
}
5024
 
5050
 
5025
static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5051
static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5026
				 struct intel_crtc_config *pipe_config)
5052
				 struct intel_crtc_config *pipe_config)
5027
{
5053
{
5028
	struct drm_device *dev = crtc->base.dev;
5054
	struct drm_device *dev = crtc->base.dev;
5029
	struct drm_i915_private *dev_priv = dev->dev_private;
5055
	struct drm_i915_private *dev_priv = dev->dev_private;
5030
	uint32_t tmp;
5056
	uint32_t tmp;
5031
 
5057
 
5032
	pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
5058
	pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
5033
	pipe_config->shared_dpll = DPLL_ID_PRIVATE;
5059
	pipe_config->shared_dpll = DPLL_ID_PRIVATE;
5034
 
5060
 
5035
	tmp = I915_READ(PIPECONF(crtc->pipe));
5061
	tmp = I915_READ(PIPECONF(crtc->pipe));
5036
	if (!(tmp & PIPECONF_ENABLE))
5062
	if (!(tmp & PIPECONF_ENABLE))
5037
		return false;
5063
		return false;
5038
 
5064
 
5039
	if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
5065
	if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
5040
		switch (tmp & PIPECONF_BPC_MASK) {
5066
		switch (tmp & PIPECONF_BPC_MASK) {
5041
		case PIPECONF_6BPC:
5067
		case PIPECONF_6BPC:
5042
			pipe_config->pipe_bpp = 18;
5068
			pipe_config->pipe_bpp = 18;
5043
			break;
5069
			break;
5044
		case PIPECONF_8BPC:
5070
		case PIPECONF_8BPC:
5045
			pipe_config->pipe_bpp = 24;
5071
			pipe_config->pipe_bpp = 24;
5046
			break;
5072
			break;
5047
		case PIPECONF_10BPC:
5073
		case PIPECONF_10BPC:
5048
			pipe_config->pipe_bpp = 30;
5074
			pipe_config->pipe_bpp = 30;
5049
			break;
5075
			break;
5050
		default:
5076
		default:
5051
			break;
5077
			break;
5052
		}
5078
		}
5053
	}
5079
	}
5054
 
5080
 
5055
	intel_get_pipe_timings(crtc, pipe_config);
5081
	intel_get_pipe_timings(crtc, pipe_config);
5056
 
5082
 
5057
	i9xx_get_pfit_config(crtc, pipe_config);
5083
	i9xx_get_pfit_config(crtc, pipe_config);
5058
 
5084
 
5059
	if (INTEL_INFO(dev)->gen >= 4) {
5085
	if (INTEL_INFO(dev)->gen >= 4) {
5060
		tmp = I915_READ(DPLL_MD(crtc->pipe));
5086
		tmp = I915_READ(DPLL_MD(crtc->pipe));
5061
		pipe_config->pixel_multiplier =
5087
		pipe_config->pixel_multiplier =
5062
			((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
5088
			((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
5063
			 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
5089
			 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
5064
		pipe_config->dpll_hw_state.dpll_md = tmp;
5090
		pipe_config->dpll_hw_state.dpll_md = tmp;
5065
	} else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
5091
	} else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
5066
		tmp = I915_READ(DPLL(crtc->pipe));
5092
		tmp = I915_READ(DPLL(crtc->pipe));
5067
		pipe_config->pixel_multiplier =
5093
		pipe_config->pixel_multiplier =
5068
			((tmp & SDVO_MULTIPLIER_MASK)
5094
			((tmp & SDVO_MULTIPLIER_MASK)
5069
			 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
5095
			 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
5070
	} else {
5096
	} else {
5071
		/* Note that on i915G/GM the pixel multiplier is in the sdvo
5097
		/* Note that on i915G/GM the pixel multiplier is in the sdvo
5072
		 * port and will be fixed up in the encoder->get_config
5098
		 * port and will be fixed up in the encoder->get_config
5073
		 * function. */
5099
		 * function. */
5074
		pipe_config->pixel_multiplier = 1;
5100
		pipe_config->pixel_multiplier = 1;
5075
	}
5101
	}
5076
	pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
5102
	pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
5077
	if (!IS_VALLEYVIEW(dev)) {
5103
	if (!IS_VALLEYVIEW(dev)) {
5078
		pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
5104
		pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
5079
		pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
5105
		pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
5080
	} else {
5106
	} else {
5081
		/* Mask out read-only status bits. */
5107
		/* Mask out read-only status bits. */
5082
		pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
5108
		pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
5083
						     DPLL_PORTC_READY_MASK |
5109
						     DPLL_PORTC_READY_MASK |
5084
						     DPLL_PORTB_READY_MASK);
5110
						     DPLL_PORTB_READY_MASK);
5085
	}
5111
	}
5086
 
5112
 
5087
	return true;
5113
	return true;
5088
}
5114
}
5089
 
5115
 
5090
static void ironlake_init_pch_refclk(struct drm_device *dev)
5116
static void ironlake_init_pch_refclk(struct drm_device *dev)
5091
{
5117
{
5092
	struct drm_i915_private *dev_priv = dev->dev_private;
5118
	struct drm_i915_private *dev_priv = dev->dev_private;
5093
	struct drm_mode_config *mode_config = &dev->mode_config;
5119
	struct drm_mode_config *mode_config = &dev->mode_config;
5094
	struct intel_encoder *encoder;
5120
	struct intel_encoder *encoder;
5095
	u32 val, final;
5121
	u32 val, final;
5096
	bool has_lvds = false;
5122
	bool has_lvds = false;
5097
	bool has_cpu_edp = false;
5123
	bool has_cpu_edp = false;
5098
	bool has_panel = false;
5124
	bool has_panel = false;
5099
	bool has_ck505 = false;
5125
	bool has_ck505 = false;
5100
	bool can_ssc = false;
5126
	bool can_ssc = false;
5101
 
5127
 
5102
	/* We need to take the global config into account */
5128
	/* We need to take the global config into account */
5103
		list_for_each_entry(encoder, &mode_config->encoder_list,
5129
		list_for_each_entry(encoder, &mode_config->encoder_list,
5104
				    base.head) {
5130
				    base.head) {
5105
			switch (encoder->type) {
5131
			switch (encoder->type) {
5106
			case INTEL_OUTPUT_LVDS:
5132
			case INTEL_OUTPUT_LVDS:
5107
			has_panel = true;
5133
			has_panel = true;
5108
				has_lvds = true;
5134
				has_lvds = true;
5109
			break;
5135
			break;
5110
			case INTEL_OUTPUT_EDP:
5136
			case INTEL_OUTPUT_EDP:
5111
			has_panel = true;
5137
			has_panel = true;
5112
			if (enc_to_dig_port(&encoder->base)->port == PORT_A)
5138
			if (enc_to_dig_port(&encoder->base)->port == PORT_A)
5113
				has_cpu_edp = true;
5139
				has_cpu_edp = true;
5114
				break;
5140
				break;
5115
			}
5141
			}
5116
		}
5142
		}
5117
 
5143
 
5118
	if (HAS_PCH_IBX(dev)) {
5144
	if (HAS_PCH_IBX(dev)) {
5119
		has_ck505 = dev_priv->vbt.display_clock_mode;
5145
		has_ck505 = dev_priv->vbt.display_clock_mode;
5120
		can_ssc = has_ck505;
5146
		can_ssc = has_ck505;
5121
	} else {
5147
	} else {
5122
		has_ck505 = false;
5148
		has_ck505 = false;
5123
		can_ssc = true;
5149
		can_ssc = true;
5124
	}
5150
	}
5125
 
5151
 
5126
	DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
5152
	DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
5127
		      has_panel, has_lvds, has_ck505);
5153
		      has_panel, has_lvds, has_ck505);
5128
 
5154
 
5129
	/* Ironlake: try to setup display ref clock before DPLL
5155
	/* Ironlake: try to setup display ref clock before DPLL
5130
	 * enabling. This is only under driver's control after
5156
	 * enabling. This is only under driver's control after
5131
	 * PCH B stepping, previous chipset stepping should be
5157
	 * PCH B stepping, previous chipset stepping should be
5132
	 * ignoring this setting.
5158
	 * ignoring this setting.
5133
	 */
5159
	 */
5134
	val = I915_READ(PCH_DREF_CONTROL);
5160
	val = I915_READ(PCH_DREF_CONTROL);
5135
 
5161
 
5136
	/* As we must carefully and slowly disable/enable each source in turn,
5162
	/* As we must carefully and slowly disable/enable each source in turn,
5137
	 * compute the final state we want first and check if we need to
5163
	 * compute the final state we want first and check if we need to
5138
	 * make any changes at all.
5164
	 * make any changes at all.
5139
	 */
5165
	 */
5140
	final = val;
5166
	final = val;
5141
	final &= ~DREF_NONSPREAD_SOURCE_MASK;
5167
	final &= ~DREF_NONSPREAD_SOURCE_MASK;
5142
	if (has_ck505)
5168
	if (has_ck505)
5143
		final |= DREF_NONSPREAD_CK505_ENABLE;
5169
		final |= DREF_NONSPREAD_CK505_ENABLE;
5144
	else
5170
	else
5145
		final |= DREF_NONSPREAD_SOURCE_ENABLE;
5171
		final |= DREF_NONSPREAD_SOURCE_ENABLE;
5146
 
5172
 
5147
	final &= ~DREF_SSC_SOURCE_MASK;
5173
	final &= ~DREF_SSC_SOURCE_MASK;
5148
	final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5174
	final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5149
	final &= ~DREF_SSC1_ENABLE;
5175
	final &= ~DREF_SSC1_ENABLE;
5150
 
5176
 
5151
	if (has_panel) {
5177
	if (has_panel) {
5152
		final |= DREF_SSC_SOURCE_ENABLE;
5178
		final |= DREF_SSC_SOURCE_ENABLE;
5153
 
5179
 
5154
		if (intel_panel_use_ssc(dev_priv) && can_ssc)
5180
		if (intel_panel_use_ssc(dev_priv) && can_ssc)
5155
			final |= DREF_SSC1_ENABLE;
5181
			final |= DREF_SSC1_ENABLE;
5156
 
5182
 
5157
		if (has_cpu_edp) {
5183
		if (has_cpu_edp) {
5158
			if (intel_panel_use_ssc(dev_priv) && can_ssc)
5184
			if (intel_panel_use_ssc(dev_priv) && can_ssc)
5159
				final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
5185
				final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
5160
			else
5186
			else
5161
				final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
5187
				final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
5162
		} else
5188
		} else
5163
			final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5189
			final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5164
	} else {
5190
	} else {
5165
		final |= DREF_SSC_SOURCE_DISABLE;
5191
		final |= DREF_SSC_SOURCE_DISABLE;
5166
		final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5192
		final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5167
	}
5193
	}
5168
 
5194
 
5169
	if (final == val)
5195
	if (final == val)
5170
		return;
5196
		return;
5171
 
5197
 
5172
	/* Always enable nonspread source */
5198
	/* Always enable nonspread source */
5173
	val &= ~DREF_NONSPREAD_SOURCE_MASK;
5199
	val &= ~DREF_NONSPREAD_SOURCE_MASK;
5174
 
5200
 
5175
	if (has_ck505)
5201
	if (has_ck505)
5176
		val |= DREF_NONSPREAD_CK505_ENABLE;
5202
		val |= DREF_NONSPREAD_CK505_ENABLE;
5177
	else
5203
	else
5178
		val |= DREF_NONSPREAD_SOURCE_ENABLE;
5204
		val |= DREF_NONSPREAD_SOURCE_ENABLE;
5179
 
5205
 
5180
	if (has_panel) {
5206
	if (has_panel) {
5181
		val &= ~DREF_SSC_SOURCE_MASK;
5207
		val &= ~DREF_SSC_SOURCE_MASK;
5182
		val |= DREF_SSC_SOURCE_ENABLE;
5208
		val |= DREF_SSC_SOURCE_ENABLE;
5183
 
5209
 
5184
		/* SSC must be turned on before enabling the CPU output  */
5210
		/* SSC must be turned on before enabling the CPU output  */
5185
		if (intel_panel_use_ssc(dev_priv) && can_ssc) {
5211
		if (intel_panel_use_ssc(dev_priv) && can_ssc) {
5186
			DRM_DEBUG_KMS("Using SSC on panel\n");
5212
			DRM_DEBUG_KMS("Using SSC on panel\n");
5187
			val |= DREF_SSC1_ENABLE;
5213
			val |= DREF_SSC1_ENABLE;
5188
		} else
5214
		} else
5189
			val &= ~DREF_SSC1_ENABLE;
5215
			val &= ~DREF_SSC1_ENABLE;
5190
 
5216
 
5191
		/* Get SSC going before enabling the outputs */
5217
		/* Get SSC going before enabling the outputs */
5192
		I915_WRITE(PCH_DREF_CONTROL, val);
5218
		I915_WRITE(PCH_DREF_CONTROL, val);
5193
			POSTING_READ(PCH_DREF_CONTROL);
5219
			POSTING_READ(PCH_DREF_CONTROL);
5194
			udelay(200);
5220
			udelay(200);
5195
 
5221
 
5196
		val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5222
		val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5197
 
5223
 
5198
		/* Enable CPU source on CPU attached eDP */
5224
		/* Enable CPU source on CPU attached eDP */
5199
		if (has_cpu_edp) {
5225
		if (has_cpu_edp) {
5200
			if (intel_panel_use_ssc(dev_priv) && can_ssc) {
5226
			if (intel_panel_use_ssc(dev_priv) && can_ssc) {
5201
				DRM_DEBUG_KMS("Using SSC on eDP\n");
5227
				DRM_DEBUG_KMS("Using SSC on eDP\n");
5202
				val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
5228
				val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
5203
			}
5229
			}
5204
			else
5230
			else
5205
				val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
5231
				val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
5206
		} else
5232
		} else
5207
			val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5233
			val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5208
 
5234
 
5209
		I915_WRITE(PCH_DREF_CONTROL, val);
5235
		I915_WRITE(PCH_DREF_CONTROL, val);
5210
		POSTING_READ(PCH_DREF_CONTROL);
5236
		POSTING_READ(PCH_DREF_CONTROL);
5211
		udelay(200);
5237
		udelay(200);
5212
		} else {
5238
		} else {
5213
		DRM_DEBUG_KMS("Disabling SSC entirely\n");
5239
		DRM_DEBUG_KMS("Disabling SSC entirely\n");
5214
 
5240
 
5215
		val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5241
		val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5216
 
5242
 
5217
		/* Turn off CPU output */
5243
		/* Turn off CPU output */
5218
		val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5244
		val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5219
 
5245
 
5220
		I915_WRITE(PCH_DREF_CONTROL, val);
5246
		I915_WRITE(PCH_DREF_CONTROL, val);
5221
		POSTING_READ(PCH_DREF_CONTROL);
5247
		POSTING_READ(PCH_DREF_CONTROL);
5222
		udelay(200);
5248
		udelay(200);
5223
 
5249
 
5224
		/* Turn off the SSC source */
5250
		/* Turn off the SSC source */
5225
		val &= ~DREF_SSC_SOURCE_MASK;
5251
		val &= ~DREF_SSC_SOURCE_MASK;
5226
		val |= DREF_SSC_SOURCE_DISABLE;
5252
		val |= DREF_SSC_SOURCE_DISABLE;
5227
 
5253
 
5228
		/* Turn off SSC1 */
5254
		/* Turn off SSC1 */
5229
		val &= ~DREF_SSC1_ENABLE;
5255
		val &= ~DREF_SSC1_ENABLE;
5230
 
5256
 
5231
		I915_WRITE(PCH_DREF_CONTROL, val);
5257
		I915_WRITE(PCH_DREF_CONTROL, val);
5232
		POSTING_READ(PCH_DREF_CONTROL);
5258
		POSTING_READ(PCH_DREF_CONTROL);
5233
		udelay(200);
5259
		udelay(200);
5234
	}
5260
	}
5235
 
5261
 
5236
	BUG_ON(val != final);
5262
	BUG_ON(val != final);
5237
}
5263
}
5238
 
5264
 
5239
static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
5265
static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
5240
{
5266
{
5241
	uint32_t tmp;
5267
	uint32_t tmp;
5242
 
5268
 
5243
		tmp = I915_READ(SOUTH_CHICKEN2);
5269
		tmp = I915_READ(SOUTH_CHICKEN2);
5244
		tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
5270
		tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
5245
		I915_WRITE(SOUTH_CHICKEN2, tmp);
5271
		I915_WRITE(SOUTH_CHICKEN2, tmp);
5246
 
5272
 
5247
		if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
5273
		if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
5248
				       FDI_MPHY_IOSFSB_RESET_STATUS, 100))
5274
				       FDI_MPHY_IOSFSB_RESET_STATUS, 100))
5249
			DRM_ERROR("FDI mPHY reset assert timeout\n");
5275
			DRM_ERROR("FDI mPHY reset assert timeout\n");
5250
 
5276
 
5251
		tmp = I915_READ(SOUTH_CHICKEN2);
5277
		tmp = I915_READ(SOUTH_CHICKEN2);
5252
		tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
5278
		tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
5253
		I915_WRITE(SOUTH_CHICKEN2, tmp);
5279
		I915_WRITE(SOUTH_CHICKEN2, tmp);
5254
 
5280
 
5255
		if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
5281
		if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
5256
				FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
5282
				FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
5257
			DRM_ERROR("FDI mPHY reset de-assert timeout\n");
5283
			DRM_ERROR("FDI mPHY reset de-assert timeout\n");
5258
	}
5284
	}
5259
 
5285
 
5260
/* WaMPhyProgramming:hsw */
5286
/* WaMPhyProgramming:hsw */
5261
static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
5287
static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
5262
{
5288
{
5263
	uint32_t tmp;
5289
	uint32_t tmp;
5264
 
5290
 
5265
	tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
5291
	tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
5266
	tmp &= ~(0xFF << 24);
5292
	tmp &= ~(0xFF << 24);
5267
	tmp |= (0x12 << 24);
5293
	tmp |= (0x12 << 24);
5268
	intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
5294
	intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
5269
 
5295
 
5270
	tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
5296
	tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
5271
	tmp |= (1 << 11);
5297
	tmp |= (1 << 11);
5272
	intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
5298
	intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
5273
 
5299
 
5274
	tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
5300
	tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
5275
	tmp |= (1 << 11);
5301
	tmp |= (1 << 11);
5276
	intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
5302
	intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
5277
 
5303
 
5278
	tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
5304
	tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
5279
	tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5305
	tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5280
	intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
5306
	intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
5281
 
5307
 
5282
	tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
5308
	tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
5283
	tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5309
	tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5284
	intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
5310
	intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
5285
 
5311
 
5286
		tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
5312
		tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
5287
		tmp &= ~(7 << 13);
5313
		tmp &= ~(7 << 13);
5288
		tmp |= (5 << 13);
5314
		tmp |= (5 << 13);
5289
		intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
5315
		intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
5290
 
5316
 
5291
		tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
5317
		tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
5292
		tmp &= ~(7 << 13);
5318
		tmp &= ~(7 << 13);
5293
		tmp |= (5 << 13);
5319
		tmp |= (5 << 13);
5294
		intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
5320
		intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
5295
 
5321
 
5296
	tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
5322
	tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
5297
	tmp &= ~0xFF;
5323
	tmp &= ~0xFF;
5298
	tmp |= 0x1C;
5324
	tmp |= 0x1C;
5299
	intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
5325
	intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
5300
 
5326
 
5301
	tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
5327
	tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
5302
	tmp &= ~0xFF;
5328
	tmp &= ~0xFF;
5303
	tmp |= 0x1C;
5329
	tmp |= 0x1C;
5304
	intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
5330
	intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
5305
 
5331
 
5306
	tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
5332
	tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
5307
	tmp &= ~(0xFF << 16);
5333
	tmp &= ~(0xFF << 16);
5308
	tmp |= (0x1C << 16);
5334
	tmp |= (0x1C << 16);
5309
	intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
5335
	intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
5310
 
5336
 
5311
	tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
5337
	tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
5312
	tmp &= ~(0xFF << 16);
5338
	tmp &= ~(0xFF << 16);
5313
	tmp |= (0x1C << 16);
5339
	tmp |= (0x1C << 16);
5314
	intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
5340
	intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
5315
 
5341
 
5316
		tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
5342
		tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
5317
		tmp |= (1 << 27);
5343
		tmp |= (1 << 27);
5318
		intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
5344
		intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
5319
 
5345
 
5320
		tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
5346
		tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
5321
		tmp |= (1 << 27);
5347
		tmp |= (1 << 27);
5322
		intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
5348
		intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
5323
 
5349
 
5324
		tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
5350
		tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
5325
		tmp &= ~(0xF << 28);
5351
		tmp &= ~(0xF << 28);
5326
		tmp |= (4 << 28);
5352
		tmp |= (4 << 28);
5327
		intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
5353
		intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
5328
 
5354
 
5329
		tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
5355
		tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
5330
		tmp &= ~(0xF << 28);
5356
		tmp &= ~(0xF << 28);
5331
		tmp |= (4 << 28);
5357
		tmp |= (4 << 28);
5332
		intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
5358
		intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
5333
	}
5359
	}
5334
 
5360
 
5335
/* Implements 3 different sequences from BSpec chapter "Display iCLK
5361
/* Implements 3 different sequences from BSpec chapter "Display iCLK
5336
 * Programming" based on the parameters passed:
5362
 * Programming" based on the parameters passed:
5337
 * - Sequence to enable CLKOUT_DP
5363
 * - Sequence to enable CLKOUT_DP
5338
 * - Sequence to enable CLKOUT_DP without spread
5364
 * - Sequence to enable CLKOUT_DP without spread
5339
 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
5365
 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
5340
 */
5366
 */
5341
static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
5367
static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
5342
				 bool with_fdi)
5368
				 bool with_fdi)
5343
{
5369
{
5344
	struct drm_i915_private *dev_priv = dev->dev_private;
5370
	struct drm_i915_private *dev_priv = dev->dev_private;
5345
	uint32_t reg, tmp;
5371
	uint32_t reg, tmp;
5346
 
5372
 
5347
	if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
5373
	if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
5348
		with_spread = true;
5374
		with_spread = true;
5349
	if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
5375
	if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
5350
		 with_fdi, "LP PCH doesn't have FDI\n"))
5376
		 with_fdi, "LP PCH doesn't have FDI\n"))
5351
		with_fdi = false;
5377
		with_fdi = false;
5352
 
5378
 
5353
	mutex_lock(&dev_priv->dpio_lock);
5379
	mutex_lock(&dev_priv->dpio_lock);
5354
 
5380
 
5355
	tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5381
	tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5356
	tmp &= ~SBI_SSCCTL_DISABLE;
5382
	tmp &= ~SBI_SSCCTL_DISABLE;
5357
	tmp |= SBI_SSCCTL_PATHALT;
5383
	tmp |= SBI_SSCCTL_PATHALT;
5358
	intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5384
	intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5359
 
5385
 
5360
	udelay(24);
5386
	udelay(24);
5361
 
5387
 
5362
	if (with_spread) {
5388
	if (with_spread) {
5363
		tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5389
		tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5364
		tmp &= ~SBI_SSCCTL_PATHALT;
5390
		tmp &= ~SBI_SSCCTL_PATHALT;
5365
		intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5391
		intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5366
 
5392
 
5367
		if (with_fdi) {
5393
		if (with_fdi) {
5368
			lpt_reset_fdi_mphy(dev_priv);
5394
			lpt_reset_fdi_mphy(dev_priv);
5369
			lpt_program_fdi_mphy(dev_priv);
5395
			lpt_program_fdi_mphy(dev_priv);
5370
		}
5396
		}
5371
	}
5397
	}
5372
 
5398
 
5373
	reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
5399
	reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
5374
	       SBI_GEN0 : SBI_DBUFF0;
5400
	       SBI_GEN0 : SBI_DBUFF0;
5375
	tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
5401
	tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
5376
	tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
5402
	tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
5377
	intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
5403
	intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
5378
 
5404
 
5379
	mutex_unlock(&dev_priv->dpio_lock);
5405
	mutex_unlock(&dev_priv->dpio_lock);
5380
}
5406
}
5381
 
5407
 
5382
/* Sequence to disable CLKOUT_DP */
5408
/* Sequence to disable CLKOUT_DP */
5383
static void lpt_disable_clkout_dp(struct drm_device *dev)
5409
static void lpt_disable_clkout_dp(struct drm_device *dev)
5384
{
5410
{
5385
	struct drm_i915_private *dev_priv = dev->dev_private;
5411
	struct drm_i915_private *dev_priv = dev->dev_private;
5386
	uint32_t reg, tmp;
5412
	uint32_t reg, tmp;
5387
 
5413
 
5388
	mutex_lock(&dev_priv->dpio_lock);
5414
	mutex_lock(&dev_priv->dpio_lock);
5389
 
5415
 
5390
	reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
5416
	reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
5391
	       SBI_GEN0 : SBI_DBUFF0;
5417
	       SBI_GEN0 : SBI_DBUFF0;
5392
	tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
5418
	tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
5393
	tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
5419
	tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
5394
	intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
5420
	intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
5395
 
5421
 
5396
	tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5422
	tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5397
	if (!(tmp & SBI_SSCCTL_DISABLE)) {
5423
	if (!(tmp & SBI_SSCCTL_DISABLE)) {
5398
		if (!(tmp & SBI_SSCCTL_PATHALT)) {
5424
		if (!(tmp & SBI_SSCCTL_PATHALT)) {
5399
			tmp |= SBI_SSCCTL_PATHALT;
5425
			tmp |= SBI_SSCCTL_PATHALT;
5400
			intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5426
			intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5401
			udelay(32);
5427
			udelay(32);
5402
		}
5428
		}
5403
		tmp |= SBI_SSCCTL_DISABLE;
5429
		tmp |= SBI_SSCCTL_DISABLE;
5404
		intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5430
		intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5405
	}
5431
	}
5406
 
5432
 
5407
	mutex_unlock(&dev_priv->dpio_lock);
5433
	mutex_unlock(&dev_priv->dpio_lock);
5408
}
5434
}
5409
 
5435
 
5410
static void lpt_init_pch_refclk(struct drm_device *dev)
5436
static void lpt_init_pch_refclk(struct drm_device *dev)
5411
{
5437
{
5412
	struct drm_mode_config *mode_config = &dev->mode_config;
5438
	struct drm_mode_config *mode_config = &dev->mode_config;
5413
	struct intel_encoder *encoder;
5439
	struct intel_encoder *encoder;
5414
	bool has_vga = false;
5440
	bool has_vga = false;
5415
 
5441
 
5416
	list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5442
	list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5417
		switch (encoder->type) {
5443
		switch (encoder->type) {
5418
		case INTEL_OUTPUT_ANALOG:
5444
		case INTEL_OUTPUT_ANALOG:
5419
			has_vga = true;
5445
			has_vga = true;
5420
			break;
5446
			break;
5421
		}
5447
		}
5422
	}
5448
	}
5423
 
5449
 
5424
	if (has_vga)
5450
	if (has_vga)
5425
		lpt_enable_clkout_dp(dev, true, true);
5451
		lpt_enable_clkout_dp(dev, true, true);
5426
	else
5452
	else
5427
		lpt_disable_clkout_dp(dev);
5453
		lpt_disable_clkout_dp(dev);
5428
}
5454
}
5429
 
5455
 
5430
/*
5456
/*
5431
 * Initialize reference clocks when the driver loads
5457
 * Initialize reference clocks when the driver loads
5432
 */
5458
 */
5433
void intel_init_pch_refclk(struct drm_device *dev)
5459
void intel_init_pch_refclk(struct drm_device *dev)
5434
{
5460
{
5435
	if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
5461
	if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
5436
		ironlake_init_pch_refclk(dev);
5462
		ironlake_init_pch_refclk(dev);
5437
	else if (HAS_PCH_LPT(dev))
5463
	else if (HAS_PCH_LPT(dev))
5438
		lpt_init_pch_refclk(dev);
5464
		lpt_init_pch_refclk(dev);
5439
}
5465
}
5440
 
5466
 
5441
static int ironlake_get_refclk(struct drm_crtc *crtc)
5467
static int ironlake_get_refclk(struct drm_crtc *crtc)
5442
{
5468
{
5443
	struct drm_device *dev = crtc->dev;
5469
	struct drm_device *dev = crtc->dev;
5444
	struct drm_i915_private *dev_priv = dev->dev_private;
5470
	struct drm_i915_private *dev_priv = dev->dev_private;
5445
	struct intel_encoder *encoder;
5471
	struct intel_encoder *encoder;
5446
	int num_connectors = 0;
5472
	int num_connectors = 0;
5447
	bool is_lvds = false;
5473
	bool is_lvds = false;
5448
 
5474
 
5449
	for_each_encoder_on_crtc(dev, crtc, encoder) {
5475
	for_each_encoder_on_crtc(dev, crtc, encoder) {
5450
		switch (encoder->type) {
5476
		switch (encoder->type) {
5451
		case INTEL_OUTPUT_LVDS:
5477
		case INTEL_OUTPUT_LVDS:
5452
			is_lvds = true;
5478
			is_lvds = true;
5453
			break;
5479
			break;
5454
		}
5480
		}
5455
		num_connectors++;
5481
		num_connectors++;
5456
	}
5482
	}
5457
 
5483
 
5458
	if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
5484
	if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
5459
		DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
5485
		DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
5460
			      dev_priv->vbt.lvds_ssc_freq);
5486
			      dev_priv->vbt.lvds_ssc_freq);
5461
		return dev_priv->vbt.lvds_ssc_freq * 1000;
5487
		return dev_priv->vbt.lvds_ssc_freq * 1000;
5462
	}
5488
	}
5463
 
5489
 
5464
	return 120000;
5490
	return 120000;
5465
}
5491
}
5466
 
5492
 
5467
static void ironlake_set_pipeconf(struct drm_crtc *crtc)
5493
static void ironlake_set_pipeconf(struct drm_crtc *crtc)
5468
{
5494
{
5469
	struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5495
	struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5470
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5496
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5471
	int pipe = intel_crtc->pipe;
5497
	int pipe = intel_crtc->pipe;
5472
	uint32_t val;
5498
	uint32_t val;
5473
 
5499
 
5474
	val = 0;
5500
	val = 0;
5475
 
5501
 
5476
	switch (intel_crtc->config.pipe_bpp) {
5502
	switch (intel_crtc->config.pipe_bpp) {
5477
	case 18:
5503
	case 18:
5478
		val |= PIPECONF_6BPC;
5504
		val |= PIPECONF_6BPC;
5479
		break;
5505
		break;
5480
	case 24:
5506
	case 24:
5481
		val |= PIPECONF_8BPC;
5507
		val |= PIPECONF_8BPC;
5482
		break;
5508
		break;
5483
	case 30:
5509
	case 30:
5484
		val |= PIPECONF_10BPC;
5510
		val |= PIPECONF_10BPC;
5485
		break;
5511
		break;
5486
	case 36:
5512
	case 36:
5487
		val |= PIPECONF_12BPC;
5513
		val |= PIPECONF_12BPC;
5488
		break;
5514
		break;
5489
	default:
5515
	default:
5490
		/* Case prevented by intel_choose_pipe_bpp_dither. */
5516
		/* Case prevented by intel_choose_pipe_bpp_dither. */
5491
		BUG();
5517
		BUG();
5492
	}
5518
	}
5493
 
5519
 
5494
	if (intel_crtc->config.dither)
5520
	if (intel_crtc->config.dither)
5495
		val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5521
		val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5496
 
5522
 
5497
	if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
5523
	if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
5498
		val |= PIPECONF_INTERLACED_ILK;
5524
		val |= PIPECONF_INTERLACED_ILK;
5499
	else
5525
	else
5500
		val |= PIPECONF_PROGRESSIVE;
5526
		val |= PIPECONF_PROGRESSIVE;
5501
 
5527
 
5502
	if (intel_crtc->config.limited_color_range)
5528
	if (intel_crtc->config.limited_color_range)
5503
		val |= PIPECONF_COLOR_RANGE_SELECT;
5529
		val |= PIPECONF_COLOR_RANGE_SELECT;
5504
 
5530
 
5505
	I915_WRITE(PIPECONF(pipe), val);
5531
	I915_WRITE(PIPECONF(pipe), val);
5506
	POSTING_READ(PIPECONF(pipe));
5532
	POSTING_READ(PIPECONF(pipe));
5507
}
5533
}
5508
 
5534
 
5509
/*
5535
/*
5510
 * Set up the pipe CSC unit.
5536
 * Set up the pipe CSC unit.
5511
 *
5537
 *
5512
 * Currently only full range RGB to limited range RGB conversion
5538
 * Currently only full range RGB to limited range RGB conversion
5513
 * is supported, but eventually this should handle various
5539
 * is supported, but eventually this should handle various
5514
 * RGB<->YCbCr scenarios as well.
5540
 * RGB<->YCbCr scenarios as well.
5515
 */
5541
 */
5516
static void intel_set_pipe_csc(struct drm_crtc *crtc)
5542
static void intel_set_pipe_csc(struct drm_crtc *crtc)
5517
{
5543
{
5518
	struct drm_device *dev = crtc->dev;
5544
	struct drm_device *dev = crtc->dev;
5519
	struct drm_i915_private *dev_priv = dev->dev_private;
5545
	struct drm_i915_private *dev_priv = dev->dev_private;
5520
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5546
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5521
	int pipe = intel_crtc->pipe;
5547
	int pipe = intel_crtc->pipe;
5522
	uint16_t coeff = 0x7800; /* 1.0 */
5548
	uint16_t coeff = 0x7800; /* 1.0 */
5523
 
5549
 
5524
	/*
5550
	/*
5525
	 * TODO: Check what kind of values actually come out of the pipe
5551
	 * TODO: Check what kind of values actually come out of the pipe
5526
	 * with these coeff/postoff values and adjust to get the best
5552
	 * with these coeff/postoff values and adjust to get the best
5527
	 * accuracy. Perhaps we even need to take the bpc value into
5553
	 * accuracy. Perhaps we even need to take the bpc value into
5528
	 * consideration.
5554
	 * consideration.
5529
	 */
5555
	 */
5530
 
5556
 
5531
	if (intel_crtc->config.limited_color_range)
5557
	if (intel_crtc->config.limited_color_range)
5532
		coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
5558
		coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
5533
 
5559
 
5534
	/*
5560
	/*
5535
	 * GY/GU and RY/RU should be the other way around according
5561
	 * GY/GU and RY/RU should be the other way around according
5536
	 * to BSpec, but reality doesn't agree. Just set them up in
5562
	 * to BSpec, but reality doesn't agree. Just set them up in
5537
	 * a way that results in the correct picture.
5563
	 * a way that results in the correct picture.
5538
	 */
5564
	 */
5539
	I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
5565
	I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
5540
	I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
5566
	I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
5541
 
5567
 
5542
	I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
5568
	I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
5543
	I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
5569
	I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
5544
 
5570
 
5545
	I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
5571
	I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
5546
	I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
5572
	I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
5547
 
5573
 
5548
	I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
5574
	I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
5549
	I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
5575
	I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
5550
	I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
5576
	I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
5551
 
5577
 
5552
	if (INTEL_INFO(dev)->gen > 6) {
5578
	if (INTEL_INFO(dev)->gen > 6) {
5553
		uint16_t postoff = 0;
5579
		uint16_t postoff = 0;
5554
 
5580
 
5555
		if (intel_crtc->config.limited_color_range)
5581
		if (intel_crtc->config.limited_color_range)
5556
			postoff = (16 * (1 << 13) / 255) & 0x1fff;
5582
			postoff = (16 * (1 << 12) / 255) & 0x1fff;
5557
 
5583
 
5558
		I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
5584
		I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
5559
		I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
5585
		I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
5560
		I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
5586
		I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
5561
 
5587
 
5562
		I915_WRITE(PIPE_CSC_MODE(pipe), 0);
5588
		I915_WRITE(PIPE_CSC_MODE(pipe), 0);
5563
	} else {
5589
	} else {
5564
		uint32_t mode = CSC_MODE_YUV_TO_RGB;
5590
		uint32_t mode = CSC_MODE_YUV_TO_RGB;
5565
 
5591
 
5566
		if (intel_crtc->config.limited_color_range)
5592
		if (intel_crtc->config.limited_color_range)
5567
			mode |= CSC_BLACK_SCREEN_OFFSET;
5593
			mode |= CSC_BLACK_SCREEN_OFFSET;
5568
 
5594
 
5569
		I915_WRITE(PIPE_CSC_MODE(pipe), mode);
5595
		I915_WRITE(PIPE_CSC_MODE(pipe), mode);
5570
	}
5596
	}
5571
}
5597
}
5572
 
5598
 
5573
static void haswell_set_pipeconf(struct drm_crtc *crtc)
5599
static void haswell_set_pipeconf(struct drm_crtc *crtc)
5574
{
5600
{
5575
	struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5601
	struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5576
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5602
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5577
	enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
5603
	enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
5578
	uint32_t val;
5604
	uint32_t val;
5579
 
5605
 
5580
	val = 0;
5606
	val = 0;
5581
 
5607
 
5582
	if (intel_crtc->config.dither)
5608
	if (intel_crtc->config.dither)
5583
		val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5609
		val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5584
 
5610
 
5585
	if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
5611
	if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
5586
		val |= PIPECONF_INTERLACED_ILK;
5612
		val |= PIPECONF_INTERLACED_ILK;
5587
	else
5613
	else
5588
		val |= PIPECONF_PROGRESSIVE;
5614
		val |= PIPECONF_PROGRESSIVE;
5589
 
5615
 
5590
	I915_WRITE(PIPECONF(cpu_transcoder), val);
5616
	I915_WRITE(PIPECONF(cpu_transcoder), val);
5591
	POSTING_READ(PIPECONF(cpu_transcoder));
5617
	POSTING_READ(PIPECONF(cpu_transcoder));
5592
 
5618
 
5593
	I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
5619
	I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
5594
	POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
5620
	POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
5595
}
5621
}
5596
 
5622
 
5597
static bool ironlake_compute_clocks(struct drm_crtc *crtc,
5623
static bool ironlake_compute_clocks(struct drm_crtc *crtc,
5598
				    intel_clock_t *clock,
5624
				    intel_clock_t *clock,
5599
				    bool *has_reduced_clock,
5625
				    bool *has_reduced_clock,
5600
				    intel_clock_t *reduced_clock)
5626
				    intel_clock_t *reduced_clock)
5601
{
5627
{
5602
	struct drm_device *dev = crtc->dev;
5628
	struct drm_device *dev = crtc->dev;
5603
	struct drm_i915_private *dev_priv = dev->dev_private;
5629
	struct drm_i915_private *dev_priv = dev->dev_private;
5604
	struct intel_encoder *intel_encoder;
5630
	struct intel_encoder *intel_encoder;
5605
	int refclk;
5631
	int refclk;
5606
	const intel_limit_t *limit;
5632
	const intel_limit_t *limit;
5607
	bool ret, is_lvds = false;
5633
	bool ret, is_lvds = false;
5608
 
5634
 
5609
	for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5635
	for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5610
		switch (intel_encoder->type) {
5636
		switch (intel_encoder->type) {
5611
		case INTEL_OUTPUT_LVDS:
5637
		case INTEL_OUTPUT_LVDS:
5612
			is_lvds = true;
5638
			is_lvds = true;
5613
			break;
5639
			break;
5614
		}
5640
		}
5615
	}
5641
	}
5616
 
5642
 
5617
	refclk = ironlake_get_refclk(crtc);
5643
	refclk = ironlake_get_refclk(crtc);
5618
 
5644
 
5619
	/*
5645
	/*
5620
	 * Returns a set of divisors for the desired target clock with the given
5646
	 * Returns a set of divisors for the desired target clock with the given
5621
	 * refclk, or FALSE.  The returned values represent the clock equation:
5647
	 * refclk, or FALSE.  The returned values represent the clock equation:
5622
	 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
5648
	 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
5623
	 */
5649
	 */
5624
	limit = intel_limit(crtc, refclk);
5650
	limit = intel_limit(crtc, refclk);
5625
	ret = dev_priv->display.find_dpll(limit, crtc,
5651
	ret = dev_priv->display.find_dpll(limit, crtc,
5626
					  to_intel_crtc(crtc)->config.port_clock,
5652
					  to_intel_crtc(crtc)->config.port_clock,
5627
					  refclk, NULL, clock);
5653
					  refclk, NULL, clock);
5628
	if (!ret)
5654
	if (!ret)
5629
		return false;
5655
		return false;
5630
 
5656
 
5631
	if (is_lvds && dev_priv->lvds_downclock_avail) {
5657
	if (is_lvds && dev_priv->lvds_downclock_avail) {
5632
		/*
5658
		/*
5633
		 * Ensure we match the reduced clock's P to the target clock.
5659
		 * Ensure we match the reduced clock's P to the target clock.
5634
		 * If the clocks don't match, we can't switch the display clock
5660
		 * If the clocks don't match, we can't switch the display clock
5635
		 * by using the FP0/FP1. In such case we will disable the LVDS
5661
		 * by using the FP0/FP1. In such case we will disable the LVDS
5636
		 * downclock feature.
5662
		 * downclock feature.
5637
		*/
5663
		*/
5638
		*has_reduced_clock =
5664
		*has_reduced_clock =
5639
			dev_priv->display.find_dpll(limit, crtc,
5665
			dev_priv->display.find_dpll(limit, crtc,
5640
						     dev_priv->lvds_downclock,
5666
						     dev_priv->lvds_downclock,
5641
						    refclk, clock,
5667
						    refclk, clock,
5642
						     reduced_clock);
5668
						     reduced_clock);
5643
	}
5669
	}
5644
 
5670
 
5645
	return true;
5671
	return true;
5646
}
5672
}
5647
 
5673
 
5648
int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
5674
int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
5649
{
5675
{
5650
	/*
5676
	/*
5651
	 * Account for spread spectrum to avoid
5677
	 * Account for spread spectrum to avoid
5652
	 * oversubscribing the link. Max center spread
5678
	 * oversubscribing the link. Max center spread
5653
	 * is 2.5%; use 5% for safety's sake.
5679
	 * is 2.5%; use 5% for safety's sake.
5654
	 */
5680
	 */
5655
	u32 bps = target_clock * bpp * 21 / 20;
5681
	u32 bps = target_clock * bpp * 21 / 20;
5656
	return bps / (link_bw * 8) + 1;
5682
	return bps / (link_bw * 8) + 1;
5657
}
5683
}
5658
 
5684
 
5659
static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
5685
static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
5660
{
5686
{
5661
	return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
5687
	return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
5662
}
5688
}
5663
 
5689
 
5664
static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
5690
static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
5665
				      u32 *fp,
5691
				      u32 *fp,
5666
				      intel_clock_t *reduced_clock, u32 *fp2)
5692
				      intel_clock_t *reduced_clock, u32 *fp2)
5667
{
5693
{
5668
	struct drm_crtc *crtc = &intel_crtc->base;
5694
	struct drm_crtc *crtc = &intel_crtc->base;
5669
	struct drm_device *dev = crtc->dev;
5695
	struct drm_device *dev = crtc->dev;
5670
	struct drm_i915_private *dev_priv = dev->dev_private;
5696
	struct drm_i915_private *dev_priv = dev->dev_private;
5671
	struct intel_encoder *intel_encoder;
5697
	struct intel_encoder *intel_encoder;
5672
	uint32_t dpll;
5698
	uint32_t dpll;
5673
	int factor, num_connectors = 0;
5699
	int factor, num_connectors = 0;
5674
	bool is_lvds = false, is_sdvo = false;
5700
	bool is_lvds = false, is_sdvo = false;
5675
 
5701
 
5676
	for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5702
	for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5677
		switch (intel_encoder->type) {
5703
		switch (intel_encoder->type) {
5678
		case INTEL_OUTPUT_LVDS:
5704
		case INTEL_OUTPUT_LVDS:
5679
			is_lvds = true;
5705
			is_lvds = true;
5680
			break;
5706
			break;
5681
		case INTEL_OUTPUT_SDVO:
5707
		case INTEL_OUTPUT_SDVO:
5682
		case INTEL_OUTPUT_HDMI:
5708
		case INTEL_OUTPUT_HDMI:
5683
			is_sdvo = true;
5709
			is_sdvo = true;
5684
			break;
5710
			break;
5685
		}
5711
		}
5686
 
5712
 
5687
		num_connectors++;
5713
		num_connectors++;
5688
	}
5714
	}
5689
 
5715
 
5690
    /* Enable autotuning of the PLL clock (if permissible) */
5716
    /* Enable autotuning of the PLL clock (if permissible) */
5691
    factor = 21;
5717
    factor = 21;
5692
    if (is_lvds) {
5718
    if (is_lvds) {
5693
        if ((intel_panel_use_ssc(dev_priv) &&
5719
        if ((intel_panel_use_ssc(dev_priv) &&
5694
		     dev_priv->vbt.lvds_ssc_freq == 100) ||
5720
		     dev_priv->vbt.lvds_ssc_freq == 100) ||
5695
		    (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
5721
		    (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
5696
            factor = 25;
5722
            factor = 25;
5697
	} else if (intel_crtc->config.sdvo_tv_clock)
5723
	} else if (intel_crtc->config.sdvo_tv_clock)
5698
        factor = 20;
5724
        factor = 20;
5699
 
5725
 
5700
	if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
5726
	if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
5701
		*fp |= FP_CB_TUNE;
5727
		*fp |= FP_CB_TUNE;
5702
 
5728
 
5703
	if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
5729
	if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
5704
		*fp2 |= FP_CB_TUNE;
5730
		*fp2 |= FP_CB_TUNE;
5705
 
5731
 
5706
    dpll = 0;
5732
    dpll = 0;
5707
 
5733
 
5708
    if (is_lvds)
5734
    if (is_lvds)
5709
        dpll |= DPLLB_MODE_LVDS;
5735
        dpll |= DPLLB_MODE_LVDS;
5710
    else
5736
    else
5711
        dpll |= DPLLB_MODE_DAC_SERIAL;
5737
        dpll |= DPLLB_MODE_DAC_SERIAL;
5712
 
5738
 
5713
			dpll |= (intel_crtc->config.pixel_multiplier - 1)
5739
			dpll |= (intel_crtc->config.pixel_multiplier - 1)
5714
				<< PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
5740
				<< PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
5715
 
5741
 
5716
	if (is_sdvo)
5742
	if (is_sdvo)
5717
		dpll |= DPLL_SDVO_HIGH_SPEED;
5743
		dpll |= DPLL_SDVO_HIGH_SPEED;
5718
	if (intel_crtc->config.has_dp_encoder)
5744
	if (intel_crtc->config.has_dp_encoder)
5719
		dpll |= DPLL_SDVO_HIGH_SPEED;
5745
		dpll |= DPLL_SDVO_HIGH_SPEED;
5720
 
5746
 
5721
    /* compute bitmask from p1 value */
5747
    /* compute bitmask from p1 value */
5722
	dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5748
	dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5723
    /* also FPA1 */
5749
    /* also FPA1 */
5724
	dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5750
	dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5725
 
5751
 
5726
	switch (intel_crtc->config.dpll.p2) {
5752
	switch (intel_crtc->config.dpll.p2) {
5727
    case 5:
5753
    case 5:
5728
        dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5754
        dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5729
        break;
5755
        break;
5730
    case 7:
5756
    case 7:
5731
        dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5757
        dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5732
        break;
5758
        break;
5733
    case 10:
5759
    case 10:
5734
        dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5760
        dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5735
        break;
5761
        break;
5736
    case 14:
5762
    case 14:
5737
        dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5763
        dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5738
        break;
5764
        break;
5739
    }
5765
    }
5740
 
5766
 
5741
	if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5767
	if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5742
        dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5768
        dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5743
    else
5769
    else
5744
        dpll |= PLL_REF_INPUT_DREFCLK;
5770
        dpll |= PLL_REF_INPUT_DREFCLK;
5745
 
5771
 
5746
	return dpll | DPLL_VCO_ENABLE;
5772
	return dpll | DPLL_VCO_ENABLE;
5747
}
5773
}
5748
 
5774
 
5749
static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
5775
static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
5750
				  int x, int y,
5776
				  int x, int y,
5751
				  struct drm_framebuffer *fb)
5777
				  struct drm_framebuffer *fb)
5752
{
5778
{
5753
	struct drm_device *dev = crtc->dev;
5779
	struct drm_device *dev = crtc->dev;
5754
	struct drm_i915_private *dev_priv = dev->dev_private;
5780
	struct drm_i915_private *dev_priv = dev->dev_private;
5755
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5781
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5756
	int pipe = intel_crtc->pipe;
5782
	int pipe = intel_crtc->pipe;
5757
	int plane = intel_crtc->plane;
5783
	int plane = intel_crtc->plane;
5758
	int num_connectors = 0;
5784
	int num_connectors = 0;
5759
	intel_clock_t clock, reduced_clock;
5785
	intel_clock_t clock, reduced_clock;
5760
	u32 dpll = 0, fp = 0, fp2 = 0;
5786
	u32 dpll = 0, fp = 0, fp2 = 0;
5761
	bool ok, has_reduced_clock = false;
5787
	bool ok, has_reduced_clock = false;
5762
	bool is_lvds = false;
5788
	bool is_lvds = false;
5763
	struct intel_encoder *encoder;
5789
	struct intel_encoder *encoder;
5764
	struct intel_shared_dpll *pll;
5790
	struct intel_shared_dpll *pll;
5765
	int ret;
5791
	int ret;
5766
 
5792
 
5767
	for_each_encoder_on_crtc(dev, crtc, encoder) {
5793
	for_each_encoder_on_crtc(dev, crtc, encoder) {
5768
		switch (encoder->type) {
5794
		switch (encoder->type) {
5769
		case INTEL_OUTPUT_LVDS:
5795
		case INTEL_OUTPUT_LVDS:
5770
			is_lvds = true;
5796
			is_lvds = true;
5771
			break;
5797
			break;
5772
		}
5798
		}
5773
 
5799
 
5774
		num_connectors++;
5800
		num_connectors++;
5775
	}
5801
	}
5776
 
5802
 
5777
	WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
5803
	WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
5778
	     "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
5804
	     "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
5779
 
5805
 
5780
	ok = ironlake_compute_clocks(crtc, &clock,
5806
	ok = ironlake_compute_clocks(crtc, &clock,
5781
				     &has_reduced_clock, &reduced_clock);
5807
				     &has_reduced_clock, &reduced_clock);
5782
	if (!ok && !intel_crtc->config.clock_set) {
5808
	if (!ok && !intel_crtc->config.clock_set) {
5783
		DRM_ERROR("Couldn't find PLL settings for mode!\n");
5809
		DRM_ERROR("Couldn't find PLL settings for mode!\n");
5784
		return -EINVAL;
5810
		return -EINVAL;
5785
	}
5811
	}
5786
	/* Compat-code for transition, will disappear. */
5812
	/* Compat-code for transition, will disappear. */
5787
	if (!intel_crtc->config.clock_set) {
5813
	if (!intel_crtc->config.clock_set) {
5788
		intel_crtc->config.dpll.n = clock.n;
5814
		intel_crtc->config.dpll.n = clock.n;
5789
		intel_crtc->config.dpll.m1 = clock.m1;
5815
		intel_crtc->config.dpll.m1 = clock.m1;
5790
		intel_crtc->config.dpll.m2 = clock.m2;
5816
		intel_crtc->config.dpll.m2 = clock.m2;
5791
		intel_crtc->config.dpll.p1 = clock.p1;
5817
		intel_crtc->config.dpll.p1 = clock.p1;
5792
		intel_crtc->config.dpll.p2 = clock.p2;
5818
		intel_crtc->config.dpll.p2 = clock.p2;
5793
	}
5819
	}
5794
 
5820
 
5795
	/* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
5821
	/* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
5796
	if (intel_crtc->config.has_pch_encoder) {
5822
	if (intel_crtc->config.has_pch_encoder) {
5797
		fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
5823
		fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
5798
	if (has_reduced_clock)
5824
	if (has_reduced_clock)
5799
			fp2 = i9xx_dpll_compute_fp(&reduced_clock);
5825
			fp2 = i9xx_dpll_compute_fp(&reduced_clock);
5800
 
5826
 
5801
		dpll = ironlake_compute_dpll(intel_crtc,
5827
		dpll = ironlake_compute_dpll(intel_crtc,
5802
					     &fp, &reduced_clock,
5828
					     &fp, &reduced_clock,
5803
				     has_reduced_clock ? &fp2 : NULL);
5829
				     has_reduced_clock ? &fp2 : NULL);
5804
 
5830
 
5805
		intel_crtc->config.dpll_hw_state.dpll = dpll;
5831
		intel_crtc->config.dpll_hw_state.dpll = dpll;
5806
		intel_crtc->config.dpll_hw_state.fp0 = fp;
5832
		intel_crtc->config.dpll_hw_state.fp0 = fp;
5807
		if (has_reduced_clock)
5833
		if (has_reduced_clock)
5808
			intel_crtc->config.dpll_hw_state.fp1 = fp2;
5834
			intel_crtc->config.dpll_hw_state.fp1 = fp2;
5809
		else
5835
		else
5810
			intel_crtc->config.dpll_hw_state.fp1 = fp;
5836
			intel_crtc->config.dpll_hw_state.fp1 = fp;
5811
 
5837
 
5812
		pll = intel_get_shared_dpll(intel_crtc);
5838
		pll = intel_get_shared_dpll(intel_crtc);
5813
		if (pll == NULL) {
5839
		if (pll == NULL) {
5814
			DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
5840
			DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
5815
					 pipe_name(pipe));
5841
					 pipe_name(pipe));
5816
			return -EINVAL;
5842
			return -EINVAL;
5817
        }
5843
        }
5818
	} else
5844
	} else
5819
		intel_put_shared_dpll(intel_crtc);
5845
		intel_put_shared_dpll(intel_crtc);
5820
 
5846
 
5821
	if (intel_crtc->config.has_dp_encoder)
5847
	if (intel_crtc->config.has_dp_encoder)
5822
		intel_dp_set_m_n(intel_crtc);
5848
		intel_dp_set_m_n(intel_crtc);
5823
 
5849
 
5824
	if (is_lvds && has_reduced_clock && i915_powersave)
5850
	if (is_lvds && has_reduced_clock && i915_powersave)
5825
		intel_crtc->lowfreq_avail = true;
5851
		intel_crtc->lowfreq_avail = true;
5826
	else
5852
	else
5827
		intel_crtc->lowfreq_avail = false;
5853
		intel_crtc->lowfreq_avail = false;
5828
 
5854
 
5829
	if (intel_crtc->config.has_pch_encoder) {
5855
	if (intel_crtc->config.has_pch_encoder) {
5830
		pll = intel_crtc_to_shared_dpll(intel_crtc);
5856
		pll = intel_crtc_to_shared_dpll(intel_crtc);
5831
 
5857
 
5832
	}
5858
	}
5833
 
5859
 
5834
	intel_set_pipe_timings(intel_crtc);
5860
	intel_set_pipe_timings(intel_crtc);
5835
 
5861
 
5836
	if (intel_crtc->config.has_pch_encoder) {
5862
	if (intel_crtc->config.has_pch_encoder) {
5837
		intel_cpu_transcoder_set_m_n(intel_crtc,
5863
		intel_cpu_transcoder_set_m_n(intel_crtc,
5838
					     &intel_crtc->config.fdi_m_n);
5864
					     &intel_crtc->config.fdi_m_n);
5839
	}
5865
	}
5840
 
5866
 
5841
	ironlake_set_pipeconf(crtc);
5867
	ironlake_set_pipeconf(crtc);
5842
 
5868
 
5843
	/* Set up the display plane register */
5869
	/* Set up the display plane register */
5844
	I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
5870
	I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
5845
	POSTING_READ(DSPCNTR(plane));
5871
	POSTING_READ(DSPCNTR(plane));
5846
 
5872
 
5847
	ret = intel_pipe_set_base(crtc, x, y, fb);
5873
	ret = intel_pipe_set_base(crtc, x, y, fb);
5848
 
5874
 
5849
	intel_update_watermarks(dev);
5875
	intel_update_watermarks(dev);
5850
 
5876
 
5851
	return ret;
5877
	return ret;
5852
}
5878
}
5853
 
5879
 
5854
static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5880
static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5855
					struct intel_crtc_config *pipe_config)
5881
					struct intel_crtc_config *pipe_config)
5856
{
5882
{
5857
	struct drm_device *dev = crtc->base.dev;
5883
	struct drm_device *dev = crtc->base.dev;
5858
	struct drm_i915_private *dev_priv = dev->dev_private;
5884
	struct drm_i915_private *dev_priv = dev->dev_private;
5859
	enum transcoder transcoder = pipe_config->cpu_transcoder;
5885
	enum transcoder transcoder = pipe_config->cpu_transcoder;
5860
 
5886
 
5861
	pipe_config->fdi_m_n.link_m = I915_READ(PIPE_LINK_M1(transcoder));
5887
	pipe_config->fdi_m_n.link_m = I915_READ(PIPE_LINK_M1(transcoder));
5862
	pipe_config->fdi_m_n.link_n = I915_READ(PIPE_LINK_N1(transcoder));
5888
	pipe_config->fdi_m_n.link_n = I915_READ(PIPE_LINK_N1(transcoder));
5863
	pipe_config->fdi_m_n.gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
5889
	pipe_config->fdi_m_n.gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
5864
					& ~TU_SIZE_MASK;
5890
					& ~TU_SIZE_MASK;
5865
	pipe_config->fdi_m_n.gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
5891
	pipe_config->fdi_m_n.gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
5866
	pipe_config->fdi_m_n.tu = ((I915_READ(PIPE_DATA_M1(transcoder))
5892
	pipe_config->fdi_m_n.tu = ((I915_READ(PIPE_DATA_M1(transcoder))
5867
				   & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
5893
				   & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
5868
}
5894
}
5869
 
5895
 
5870
static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5896
static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5871
				     struct intel_crtc_config *pipe_config)
5897
				     struct intel_crtc_config *pipe_config)
5872
{
5898
{
5873
	struct drm_device *dev = crtc->base.dev;
5899
	struct drm_device *dev = crtc->base.dev;
5874
	struct drm_i915_private *dev_priv = dev->dev_private;
5900
	struct drm_i915_private *dev_priv = dev->dev_private;
5875
	uint32_t tmp;
5901
	uint32_t tmp;
5876
 
5902
 
5877
	tmp = I915_READ(PF_CTL(crtc->pipe));
5903
	tmp = I915_READ(PF_CTL(crtc->pipe));
5878
 
5904
 
5879
	if (tmp & PF_ENABLE) {
5905
	if (tmp & PF_ENABLE) {
5880
		pipe_config->pch_pfit.enabled = true;
5906
		pipe_config->pch_pfit.enabled = true;
5881
		pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
5907
		pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
5882
		pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
5908
		pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
5883
 
5909
 
5884
		/* We currently do not free assignements of panel fitters on
5910
		/* We currently do not free assignements of panel fitters on
5885
		 * ivb/hsw (since we don't use the higher upscaling modes which
5911
		 * ivb/hsw (since we don't use the higher upscaling modes which
5886
		 * differentiates them) so just WARN about this case for now. */
5912
		 * differentiates them) so just WARN about this case for now. */
5887
		if (IS_GEN7(dev)) {
5913
		if (IS_GEN7(dev)) {
5888
			WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
5914
			WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
5889
				PF_PIPE_SEL_IVB(crtc->pipe));
5915
				PF_PIPE_SEL_IVB(crtc->pipe));
5890
		}
5916
		}
5891
	}
5917
	}
5892
}
5918
}
5893
 
5919
 
5894
static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5920
static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5895
				     struct intel_crtc_config *pipe_config)
5921
				     struct intel_crtc_config *pipe_config)
5896
{
5922
{
5897
	struct drm_device *dev = crtc->base.dev;
5923
	struct drm_device *dev = crtc->base.dev;
5898
	struct drm_i915_private *dev_priv = dev->dev_private;
5924
	struct drm_i915_private *dev_priv = dev->dev_private;
5899
	uint32_t tmp;
5925
	uint32_t tmp;
5900
 
5926
 
5901
	pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
5927
	pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
5902
	pipe_config->shared_dpll = DPLL_ID_PRIVATE;
5928
	pipe_config->shared_dpll = DPLL_ID_PRIVATE;
5903
 
5929
 
5904
	tmp = I915_READ(PIPECONF(crtc->pipe));
5930
	tmp = I915_READ(PIPECONF(crtc->pipe));
5905
	if (!(tmp & PIPECONF_ENABLE))
5931
	if (!(tmp & PIPECONF_ENABLE))
5906
		return false;
5932
		return false;
5907
 
5933
 
5908
	switch (tmp & PIPECONF_BPC_MASK) {
5934
	switch (tmp & PIPECONF_BPC_MASK) {
5909
	case PIPECONF_6BPC:
5935
	case PIPECONF_6BPC:
5910
		pipe_config->pipe_bpp = 18;
5936
		pipe_config->pipe_bpp = 18;
5911
		break;
5937
		break;
5912
	case PIPECONF_8BPC:
5938
	case PIPECONF_8BPC:
5913
		pipe_config->pipe_bpp = 24;
5939
		pipe_config->pipe_bpp = 24;
5914
		break;
5940
		break;
5915
	case PIPECONF_10BPC:
5941
	case PIPECONF_10BPC:
5916
		pipe_config->pipe_bpp = 30;
5942
		pipe_config->pipe_bpp = 30;
5917
		break;
5943
		break;
5918
	case PIPECONF_12BPC:
5944
	case PIPECONF_12BPC:
5919
		pipe_config->pipe_bpp = 36;
5945
		pipe_config->pipe_bpp = 36;
5920
		break;
5946
		break;
5921
	default:
5947
	default:
5922
		break;
5948
		break;
5923
	}
5949
	}
5924
 
5950
 
5925
	if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
5951
	if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
5926
		struct intel_shared_dpll *pll;
5952
		struct intel_shared_dpll *pll;
5927
 
5953
 
5928
		pipe_config->has_pch_encoder = true;
5954
		pipe_config->has_pch_encoder = true;
5929
 
5955
 
5930
		tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
5956
		tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
5931
		pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
5957
		pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
5932
					  FDI_DP_PORT_WIDTH_SHIFT) + 1;
5958
					  FDI_DP_PORT_WIDTH_SHIFT) + 1;
5933
 
5959
 
5934
		ironlake_get_fdi_m_n_config(crtc, pipe_config);
5960
		ironlake_get_fdi_m_n_config(crtc, pipe_config);
5935
 
5961
 
5936
		if (HAS_PCH_IBX(dev_priv->dev)) {
5962
		if (HAS_PCH_IBX(dev_priv->dev)) {
5937
			pipe_config->shared_dpll =
5963
			pipe_config->shared_dpll =
5938
				(enum intel_dpll_id) crtc->pipe;
5964
				(enum intel_dpll_id) crtc->pipe;
5939
		} else {
5965
		} else {
5940
			tmp = I915_READ(PCH_DPLL_SEL);
5966
			tmp = I915_READ(PCH_DPLL_SEL);
5941
			if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
5967
			if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
5942
				pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
5968
				pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
5943
			else
5969
			else
5944
				pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
5970
				pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
5945
		}
5971
		}
5946
 
5972
 
5947
		pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
5973
		pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
5948
 
5974
 
5949
		WARN_ON(!pll->get_hw_state(dev_priv, pll,
5975
		WARN_ON(!pll->get_hw_state(dev_priv, pll,
5950
					   &pipe_config->dpll_hw_state));
5976
					   &pipe_config->dpll_hw_state));
5951
 
5977
 
5952
		tmp = pipe_config->dpll_hw_state.dpll;
5978
		tmp = pipe_config->dpll_hw_state.dpll;
5953
		pipe_config->pixel_multiplier =
5979
		pipe_config->pixel_multiplier =
5954
			((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
5980
			((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
5955
			 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
5981
			 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
5956
	} else {
5982
	} else {
5957
		pipe_config->pixel_multiplier = 1;
5983
		pipe_config->pixel_multiplier = 1;
5958
	}
5984
	}
5959
 
5985
 
5960
	intel_get_pipe_timings(crtc, pipe_config);
5986
	intel_get_pipe_timings(crtc, pipe_config);
5961
 
5987
 
5962
	ironlake_get_pfit_config(crtc, pipe_config);
5988
	ironlake_get_pfit_config(crtc, pipe_config);
5963
 
5989
 
5964
	return true;
5990
	return true;
5965
}
5991
}
5966
 
5992
 
5967
static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
5993
static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
5968
{
5994
{
5969
	struct drm_device *dev = dev_priv->dev;
5995
	struct drm_device *dev = dev_priv->dev;
5970
	struct intel_ddi_plls *plls = &dev_priv->ddi_plls;
5996
	struct intel_ddi_plls *plls = &dev_priv->ddi_plls;
5971
	struct intel_crtc *crtc;
5997
	struct intel_crtc *crtc;
5972
	unsigned long irqflags;
5998
	unsigned long irqflags;
5973
	uint32_t val;
5999
	uint32_t val;
5974
 
6000
 
5975
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
6001
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
5976
		WARN(crtc->base.enabled, "CRTC for pipe %c enabled\n",
6002
		WARN(crtc->base.enabled, "CRTC for pipe %c enabled\n",
5977
		     pipe_name(crtc->pipe));
6003
		     pipe_name(crtc->pipe));
5978
 
6004
 
5979
	WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
6005
	WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
5980
	WARN(plls->spll_refcount, "SPLL enabled\n");
6006
	WARN(plls->spll_refcount, "SPLL enabled\n");
5981
	WARN(plls->wrpll1_refcount, "WRPLL1 enabled\n");
6007
	WARN(plls->wrpll1_refcount, "WRPLL1 enabled\n");
5982
	WARN(plls->wrpll2_refcount, "WRPLL2 enabled\n");
6008
	WARN(plls->wrpll2_refcount, "WRPLL2 enabled\n");
5983
	WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
6009
	WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
5984
	WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
6010
	WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
5985
	     "CPU PWM1 enabled\n");
6011
	     "CPU PWM1 enabled\n");
5986
	WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
6012
	WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
5987
	     "CPU PWM2 enabled\n");
6013
	     "CPU PWM2 enabled\n");
5988
	WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
6014
	WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
5989
	     "PCH PWM1 enabled\n");
6015
	     "PCH PWM1 enabled\n");
5990
	WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
6016
	WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
5991
	     "Utility pin enabled\n");
6017
	     "Utility pin enabled\n");
5992
	WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
6018
	WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
5993
 
6019
 
5994
	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
6020
	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
5995
	val = I915_READ(DEIMR);
6021
	val = I915_READ(DEIMR);
5996
	WARN((val & ~DE_PCH_EVENT_IVB) != val,
6022
	WARN((val & ~DE_PCH_EVENT_IVB) != val,
5997
	     "Unexpected DEIMR bits enabled: 0x%x\n", val);
6023
	     "Unexpected DEIMR bits enabled: 0x%x\n", val);
5998
	val = I915_READ(SDEIMR);
6024
	val = I915_READ(SDEIMR);
5999
	WARN((val | SDE_HOTPLUG_MASK_CPT) != 0xffffffff,
6025
	WARN((val | SDE_HOTPLUG_MASK_CPT) != 0xffffffff,
6000
	     "Unexpected SDEIMR bits enabled: 0x%x\n", val);
6026
	     "Unexpected SDEIMR bits enabled: 0x%x\n", val);
6001
	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
6027
	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
6002
}
6028
}
6003
 
6029
 
6004
/*
6030
/*
6005
 * This function implements pieces of two sequences from BSpec:
6031
 * This function implements pieces of two sequences from BSpec:
6006
 * - Sequence for display software to disable LCPLL
6032
 * - Sequence for display software to disable LCPLL
6007
 * - Sequence for display software to allow package C8+
6033
 * - Sequence for display software to allow package C8+
6008
 * The steps implemented here are just the steps that actually touch the LCPLL
6034
 * The steps implemented here are just the steps that actually touch the LCPLL
6009
 * register. Callers should take care of disabling all the display engine
6035
 * register. Callers should take care of disabling all the display engine
6010
 * functions, doing the mode unset, fixing interrupts, etc.
6036
 * functions, doing the mode unset, fixing interrupts, etc.
6011
 */
6037
 */
6012
void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
6038
void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
6013
		       bool switch_to_fclk, bool allow_power_down)
6039
		       bool switch_to_fclk, bool allow_power_down)
6014
{
6040
{
6015
	uint32_t val;
6041
	uint32_t val;
6016
 
6042
 
6017
	assert_can_disable_lcpll(dev_priv);
6043
	assert_can_disable_lcpll(dev_priv);
6018
 
6044
 
6019
	val = I915_READ(LCPLL_CTL);
6045
	val = I915_READ(LCPLL_CTL);
6020
 
6046
 
6021
	if (switch_to_fclk) {
6047
	if (switch_to_fclk) {
6022
		val |= LCPLL_CD_SOURCE_FCLK;
6048
		val |= LCPLL_CD_SOURCE_FCLK;
6023
		I915_WRITE(LCPLL_CTL, val);
6049
		I915_WRITE(LCPLL_CTL, val);
6024
 
6050
 
6025
		if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
6051
		if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
6026
				       LCPLL_CD_SOURCE_FCLK_DONE, 1))
6052
				       LCPLL_CD_SOURCE_FCLK_DONE, 1))
6027
			DRM_ERROR("Switching to FCLK failed\n");
6053
			DRM_ERROR("Switching to FCLK failed\n");
6028
 
6054
 
6029
		val = I915_READ(LCPLL_CTL);
6055
		val = I915_READ(LCPLL_CTL);
6030
	}
6056
	}
6031
 
6057
 
6032
	val |= LCPLL_PLL_DISABLE;
6058
	val |= LCPLL_PLL_DISABLE;
6033
	I915_WRITE(LCPLL_CTL, val);
6059
	I915_WRITE(LCPLL_CTL, val);
6034
	POSTING_READ(LCPLL_CTL);
6060
	POSTING_READ(LCPLL_CTL);
6035
 
6061
 
6036
	if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
6062
	if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
6037
		DRM_ERROR("LCPLL still locked\n");
6063
		DRM_ERROR("LCPLL still locked\n");
6038
 
6064
 
6039
	val = I915_READ(D_COMP);
6065
	val = I915_READ(D_COMP);
6040
	val |= D_COMP_COMP_DISABLE;
6066
	val |= D_COMP_COMP_DISABLE;
6041
	I915_WRITE(D_COMP, val);
6067
	I915_WRITE(D_COMP, val);
6042
	POSTING_READ(D_COMP);
6068
	POSTING_READ(D_COMP);
6043
    udelay(100);
6069
    udelay(100);
6044
 
6070
 
6045
	if (wait_for((I915_READ(D_COMP) & D_COMP_RCOMP_IN_PROGRESS) == 0, 1))
6071
	if (wait_for((I915_READ(D_COMP) & D_COMP_RCOMP_IN_PROGRESS) == 0, 1))
6046
		DRM_ERROR("D_COMP RCOMP still in progress\n");
6072
		DRM_ERROR("D_COMP RCOMP still in progress\n");
6047
 
6073
 
6048
	if (allow_power_down) {
6074
	if (allow_power_down) {
6049
		val = I915_READ(LCPLL_CTL);
6075
		val = I915_READ(LCPLL_CTL);
6050
		val |= LCPLL_POWER_DOWN_ALLOW;
6076
		val |= LCPLL_POWER_DOWN_ALLOW;
6051
		I915_WRITE(LCPLL_CTL, val);
6077
		I915_WRITE(LCPLL_CTL, val);
6052
		POSTING_READ(LCPLL_CTL);
6078
		POSTING_READ(LCPLL_CTL);
6053
	}
6079
	}
6054
}
6080
}
6055
 
6081
 
6056
/*
6082
/*
6057
 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
6083
 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
6058
 * source.
6084
 * source.
6059
 */
6085
 */
6060
void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
6086
void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
6061
{
6087
{
6062
	uint32_t val;
6088
	uint32_t val;
6063
 
6089
 
6064
	val = I915_READ(LCPLL_CTL);
6090
	val = I915_READ(LCPLL_CTL);
6065
 
6091
 
6066
	if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
6092
	if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
6067
		    LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
6093
		    LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
6068
		return;
6094
		return;
6069
 
6095
 
6070
	/* Make sure we're not on PC8 state before disabling PC8, otherwise
6096
	/* Make sure we're not on PC8 state before disabling PC8, otherwise
6071
	 * we'll hang the machine! */
6097
	 * we'll hang the machine! */
6072
	dev_priv->uncore.funcs.force_wake_get(dev_priv);
6098
	gen6_gt_force_wake_get(dev_priv);
6073
 
6099
 
6074
	if (val & LCPLL_POWER_DOWN_ALLOW) {
6100
	if (val & LCPLL_POWER_DOWN_ALLOW) {
6075
		val &= ~LCPLL_POWER_DOWN_ALLOW;
6101
		val &= ~LCPLL_POWER_DOWN_ALLOW;
6076
		I915_WRITE(LCPLL_CTL, val);
6102
		I915_WRITE(LCPLL_CTL, val);
6077
		POSTING_READ(LCPLL_CTL);
6103
		POSTING_READ(LCPLL_CTL);
6078
	}
6104
	}
6079
 
6105
 
6080
	val = I915_READ(D_COMP);
6106
	val = I915_READ(D_COMP);
6081
	val |= D_COMP_COMP_FORCE;
6107
	val |= D_COMP_COMP_FORCE;
6082
	val &= ~D_COMP_COMP_DISABLE;
6108
	val &= ~D_COMP_COMP_DISABLE;
6083
	I915_WRITE(D_COMP, val);
6109
	I915_WRITE(D_COMP, val);
6084
	POSTING_READ(D_COMP);
6110
	POSTING_READ(D_COMP);
6085
 
6111
 
6086
	val = I915_READ(LCPLL_CTL);
6112
	val = I915_READ(LCPLL_CTL);
6087
	val &= ~LCPLL_PLL_DISABLE;
6113
	val &= ~LCPLL_PLL_DISABLE;
6088
	I915_WRITE(LCPLL_CTL, val);
6114
	I915_WRITE(LCPLL_CTL, val);
6089
 
6115
 
6090
	if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
6116
	if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
6091
		DRM_ERROR("LCPLL not locked yet\n");
6117
		DRM_ERROR("LCPLL not locked yet\n");
6092
 
6118
 
6093
	if (val & LCPLL_CD_SOURCE_FCLK) {
6119
	if (val & LCPLL_CD_SOURCE_FCLK) {
6094
		val = I915_READ(LCPLL_CTL);
6120
		val = I915_READ(LCPLL_CTL);
6095
		val &= ~LCPLL_CD_SOURCE_FCLK;
6121
		val &= ~LCPLL_CD_SOURCE_FCLK;
6096
		I915_WRITE(LCPLL_CTL, val);
6122
		I915_WRITE(LCPLL_CTL, val);
6097
 
6123
 
6098
		if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
6124
		if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
6099
					LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
6125
					LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
6100
			DRM_ERROR("Switching back to LCPLL failed\n");
6126
			DRM_ERROR("Switching back to LCPLL failed\n");
6101
	}
6127
	}
6102
 
6128
 
6103
	dev_priv->uncore.funcs.force_wake_put(dev_priv);
6129
	gen6_gt_force_wake_put(dev_priv);
6104
}
6130
}
6105
 
6131
 
6106
void hsw_enable_pc8_work(struct work_struct *__work)
6132
void hsw_enable_pc8_work(struct work_struct *__work)
6107
{
6133
{
6108
	struct drm_i915_private *dev_priv =
6134
	struct drm_i915_private *dev_priv =
6109
		container_of(to_delayed_work(__work), struct drm_i915_private,
6135
		container_of(to_delayed_work(__work), struct drm_i915_private,
6110
			     pc8.enable_work);
6136
			     pc8.enable_work);
6111
	struct drm_device *dev = dev_priv->dev;
6137
	struct drm_device *dev = dev_priv->dev;
6112
	uint32_t val;
6138
	uint32_t val;
6113
 
6139
 
6114
	if (dev_priv->pc8.enabled)
6140
	if (dev_priv->pc8.enabled)
6115
		return;
6141
		return;
6116
 
6142
 
6117
	DRM_DEBUG_KMS("Enabling package C8+\n");
6143
	DRM_DEBUG_KMS("Enabling package C8+\n");
6118
 
6144
 
6119
	dev_priv->pc8.enabled = true;
6145
	dev_priv->pc8.enabled = true;
6120
 
6146
 
6121
	if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
6147
	if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
6122
		val = I915_READ(SOUTH_DSPCLK_GATE_D);
6148
		val = I915_READ(SOUTH_DSPCLK_GATE_D);
6123
		val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
6149
		val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
6124
		I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6150
		I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6125
	}
6151
	}
6126
 
6152
 
6127
	lpt_disable_clkout_dp(dev);
6153
	lpt_disable_clkout_dp(dev);
6128
	hsw_pc8_disable_interrupts(dev);
6154
	hsw_pc8_disable_interrupts(dev);
6129
	hsw_disable_lcpll(dev_priv, true, true);
6155
	hsw_disable_lcpll(dev_priv, true, true);
6130
}
6156
}
6131
 
6157
 
6132
static void __hsw_enable_package_c8(struct drm_i915_private *dev_priv)
6158
static void __hsw_enable_package_c8(struct drm_i915_private *dev_priv)
6133
{
6159
{
6134
	WARN_ON(!mutex_is_locked(&dev_priv->pc8.lock));
6160
	WARN_ON(!mutex_is_locked(&dev_priv->pc8.lock));
6135
	WARN(dev_priv->pc8.disable_count < 1,
6161
	WARN(dev_priv->pc8.disable_count < 1,
6136
	     "pc8.disable_count: %d\n", dev_priv->pc8.disable_count);
6162
	     "pc8.disable_count: %d\n", dev_priv->pc8.disable_count);
6137
 
6163
 
6138
	dev_priv->pc8.disable_count--;
6164
	dev_priv->pc8.disable_count--;
6139
	if (dev_priv->pc8.disable_count != 0)
6165
	if (dev_priv->pc8.disable_count != 0)
6140
		return;
6166
		return;
6141
 
6167
 
6142
	schedule_delayed_work(&dev_priv->pc8.enable_work,
6168
	schedule_delayed_work(&dev_priv->pc8.enable_work,
6143
			      msecs_to_jiffies(i915_pc8_timeout));
6169
			      msecs_to_jiffies(i915_pc8_timeout));
6144
}
6170
}
6145
 
6171
 
6146
static void __hsw_disable_package_c8(struct drm_i915_private *dev_priv)
6172
static void __hsw_disable_package_c8(struct drm_i915_private *dev_priv)
6147
{
6173
{
6148
	struct drm_device *dev = dev_priv->dev;
6174
	struct drm_device *dev = dev_priv->dev;
6149
	uint32_t val;
6175
	uint32_t val;
6150
 
6176
 
6151
	WARN_ON(!mutex_is_locked(&dev_priv->pc8.lock));
6177
	WARN_ON(!mutex_is_locked(&dev_priv->pc8.lock));
6152
	WARN(dev_priv->pc8.disable_count < 0,
6178
	WARN(dev_priv->pc8.disable_count < 0,
6153
	     "pc8.disable_count: %d\n", dev_priv->pc8.disable_count);
6179
	     "pc8.disable_count: %d\n", dev_priv->pc8.disable_count);
6154
 
6180
 
6155
	dev_priv->pc8.disable_count++;
6181
	dev_priv->pc8.disable_count++;
6156
	if (dev_priv->pc8.disable_count != 1)
6182
	if (dev_priv->pc8.disable_count != 1)
6157
		return;
6183
		return;
6158
 
6184
 
6159
	cancel_delayed_work_sync(&dev_priv->pc8.enable_work);
6185
	cancel_delayed_work_sync(&dev_priv->pc8.enable_work);
6160
	if (!dev_priv->pc8.enabled)
6186
	if (!dev_priv->pc8.enabled)
6161
		return;
6187
		return;
6162
 
6188
 
6163
	DRM_DEBUG_KMS("Disabling package C8+\n");
6189
	DRM_DEBUG_KMS("Disabling package C8+\n");
6164
 
6190
 
6165
	hsw_restore_lcpll(dev_priv);
6191
	hsw_restore_lcpll(dev_priv);
6166
	hsw_pc8_restore_interrupts(dev);
6192
	hsw_pc8_restore_interrupts(dev);
6167
	lpt_init_pch_refclk(dev);
6193
	lpt_init_pch_refclk(dev);
6168
 
6194
 
6169
	if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
6195
	if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
6170
		val = I915_READ(SOUTH_DSPCLK_GATE_D);
6196
		val = I915_READ(SOUTH_DSPCLK_GATE_D);
6171
		val |= PCH_LP_PARTITION_LEVEL_DISABLE;
6197
		val |= PCH_LP_PARTITION_LEVEL_DISABLE;
6172
		I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6198
		I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6173
	}
6199
	}
6174
 
6200
 
6175
	intel_prepare_ddi(dev);
6201
	intel_prepare_ddi(dev);
6176
	i915_gem_init_swizzling(dev);
6202
	i915_gem_init_swizzling(dev);
6177
	mutex_lock(&dev_priv->rps.hw_lock);
6203
	mutex_lock(&dev_priv->rps.hw_lock);
6178
	gen6_update_ring_freq(dev);
6204
	gen6_update_ring_freq(dev);
6179
	mutex_unlock(&dev_priv->rps.hw_lock);
6205
	mutex_unlock(&dev_priv->rps.hw_lock);
6180
	dev_priv->pc8.enabled = false;
6206
	dev_priv->pc8.enabled = false;
6181
}
6207
}
6182
 
6208
 
6183
void hsw_enable_package_c8(struct drm_i915_private *dev_priv)
6209
void hsw_enable_package_c8(struct drm_i915_private *dev_priv)
6184
{
6210
{
6185
	mutex_lock(&dev_priv->pc8.lock);
6211
	mutex_lock(&dev_priv->pc8.lock);
6186
	__hsw_enable_package_c8(dev_priv);
6212
	__hsw_enable_package_c8(dev_priv);
6187
	mutex_unlock(&dev_priv->pc8.lock);
6213
	mutex_unlock(&dev_priv->pc8.lock);
6188
}
6214
}
6189
 
6215
 
6190
void hsw_disable_package_c8(struct drm_i915_private *dev_priv)
6216
void hsw_disable_package_c8(struct drm_i915_private *dev_priv)
6191
{
6217
{
6192
	mutex_lock(&dev_priv->pc8.lock);
6218
	mutex_lock(&dev_priv->pc8.lock);
6193
	__hsw_disable_package_c8(dev_priv);
6219
	__hsw_disable_package_c8(dev_priv);
6194
	mutex_unlock(&dev_priv->pc8.lock);
6220
	mutex_unlock(&dev_priv->pc8.lock);
6195
}
6221
}
6196
 
6222
 
6197
static bool hsw_can_enable_package_c8(struct drm_i915_private *dev_priv)
6223
static bool hsw_can_enable_package_c8(struct drm_i915_private *dev_priv)
6198
{
6224
{
6199
	struct drm_device *dev = dev_priv->dev;
6225
	struct drm_device *dev = dev_priv->dev;
6200
	struct intel_crtc *crtc;
6226
	struct intel_crtc *crtc;
6201
	uint32_t val;
6227
	uint32_t val;
6202
 
6228
 
6203
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
6229
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
6204
		if (crtc->base.enabled)
6230
		if (crtc->base.enabled)
6205
			return false;
6231
			return false;
6206
 
6232
 
6207
	/* This case is still possible since we have the i915.disable_power_well
6233
	/* This case is still possible since we have the i915.disable_power_well
6208
	 * parameter and also the KVMr or something else might be requesting the
6234
	 * parameter and also the KVMr or something else might be requesting the
6209
	 * power well. */
6235
	 * power well. */
6210
	val = I915_READ(HSW_PWR_WELL_DRIVER);
6236
	val = I915_READ(HSW_PWR_WELL_DRIVER);
6211
	if (val != 0) {
6237
	if (val != 0) {
6212
		DRM_DEBUG_KMS("Not enabling PC8: power well on\n");
6238
		DRM_DEBUG_KMS("Not enabling PC8: power well on\n");
6213
		return false;
6239
		return false;
6214
	}
6240
	}
6215
 
6241
 
6216
	return true;
6242
	return true;
6217
}
6243
}
6218
 
6244
 
6219
/* Since we're called from modeset_global_resources there's no way to
6245
/* Since we're called from modeset_global_resources there's no way to
6220
 * symmetrically increase and decrease the refcount, so we use
6246
 * symmetrically increase and decrease the refcount, so we use
6221
 * dev_priv->pc8.requirements_met to track whether we already have the refcount
6247
 * dev_priv->pc8.requirements_met to track whether we already have the refcount
6222
 * or not.
6248
 * or not.
6223
 */
6249
 */
6224
static void hsw_update_package_c8(struct drm_device *dev)
6250
static void hsw_update_package_c8(struct drm_device *dev)
6225
{
6251
{
6226
	struct drm_i915_private *dev_priv = dev->dev_private;
6252
	struct drm_i915_private *dev_priv = dev->dev_private;
6227
	bool allow;
6253
	bool allow;
6228
 
6254
 
6229
	if (!i915_enable_pc8)
6255
	if (!i915_enable_pc8)
6230
		return;
6256
		return;
6231
 
6257
 
6232
	mutex_lock(&dev_priv->pc8.lock);
6258
	mutex_lock(&dev_priv->pc8.lock);
6233
 
6259
 
6234
	allow = hsw_can_enable_package_c8(dev_priv);
6260
	allow = hsw_can_enable_package_c8(dev_priv);
6235
 
6261
 
6236
	if (allow == dev_priv->pc8.requirements_met)
6262
	if (allow == dev_priv->pc8.requirements_met)
6237
		goto done;
6263
		goto done;
6238
 
6264
 
6239
	dev_priv->pc8.requirements_met = allow;
6265
	dev_priv->pc8.requirements_met = allow;
6240
 
6266
 
6241
	if (allow)
6267
	if (allow)
6242
		__hsw_enable_package_c8(dev_priv);
6268
		__hsw_enable_package_c8(dev_priv);
6243
	else
6269
	else
6244
		__hsw_disable_package_c8(dev_priv);
6270
		__hsw_disable_package_c8(dev_priv);
6245
 
6271
 
6246
done:
6272
done:
6247
	mutex_unlock(&dev_priv->pc8.lock);
6273
	mutex_unlock(&dev_priv->pc8.lock);
6248
}
6274
}
6249
 
6275
 
6250
static void hsw_package_c8_gpu_idle(struct drm_i915_private *dev_priv)
6276
static void hsw_package_c8_gpu_idle(struct drm_i915_private *dev_priv)
6251
{
6277
{
6252
	if (!dev_priv->pc8.gpu_idle) {
6278
	if (!dev_priv->pc8.gpu_idle) {
6253
		dev_priv->pc8.gpu_idle = true;
6279
		dev_priv->pc8.gpu_idle = true;
6254
		hsw_enable_package_c8(dev_priv);
6280
		hsw_enable_package_c8(dev_priv);
6255
	}
6281
	}
6256
}
6282
}
6257
 
6283
 
6258
static void hsw_package_c8_gpu_busy(struct drm_i915_private *dev_priv)
6284
static void hsw_package_c8_gpu_busy(struct drm_i915_private *dev_priv)
6259
{
6285
{
6260
	if (dev_priv->pc8.gpu_idle) {
6286
	if (dev_priv->pc8.gpu_idle) {
6261
		dev_priv->pc8.gpu_idle = false;
6287
		dev_priv->pc8.gpu_idle = false;
6262
		hsw_disable_package_c8(dev_priv);
6288
		hsw_disable_package_c8(dev_priv);
6263
	}
6289
	}
6264
}
6290
}
6265
 
6291
 
6266
static void haswell_modeset_global_resources(struct drm_device *dev)
6292
static void haswell_modeset_global_resources(struct drm_device *dev)
6267
{
6293
{
6268
	bool enable = false;
6294
	bool enable = false;
6269
	struct intel_crtc *crtc;
6295
	struct intel_crtc *crtc;
6270
 
6296
 
6271
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
6297
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
6272
		if (!crtc->base.enabled)
6298
		if (!crtc->base.enabled)
6273
			continue;
6299
			continue;
6274
 
6300
 
6275
		if (crtc->pipe != PIPE_A || crtc->config.pch_pfit.enabled ||
6301
		if (crtc->pipe != PIPE_A || crtc->config.pch_pfit.enabled ||
6276
		    crtc->config.cpu_transcoder != TRANSCODER_EDP)
6302
		    crtc->config.cpu_transcoder != TRANSCODER_EDP)
6277
			enable = true;
6303
			enable = true;
6278
	}
6304
	}
6279
 
6305
 
6280
	intel_set_power_well(dev, enable);
6306
	intel_set_power_well(dev, enable);
6281
 
6307
 
6282
	hsw_update_package_c8(dev);
6308
	hsw_update_package_c8(dev);
6283
}
6309
}
6284
 
6310
 
6285
static int haswell_crtc_mode_set(struct drm_crtc *crtc,
6311
static int haswell_crtc_mode_set(struct drm_crtc *crtc,
6286
				 int x, int y,
6312
				 int x, int y,
6287
				 struct drm_framebuffer *fb)
6313
				 struct drm_framebuffer *fb)
6288
{
6314
{
6289
	struct drm_device *dev = crtc->dev;
6315
	struct drm_device *dev = crtc->dev;
6290
	struct drm_i915_private *dev_priv = dev->dev_private;
6316
	struct drm_i915_private *dev_priv = dev->dev_private;
6291
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6317
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6292
	int plane = intel_crtc->plane;
6318
	int plane = intel_crtc->plane;
6293
	int ret;
6319
	int ret;
6294
 
6320
 
6295
	if (!intel_ddi_pll_mode_set(crtc))
6321
	if (!intel_ddi_pll_mode_set(crtc))
6296
		return -EINVAL;
6322
		return -EINVAL;
6297
 
6323
 
6298
	if (intel_crtc->config.has_dp_encoder)
6324
	if (intel_crtc->config.has_dp_encoder)
6299
		intel_dp_set_m_n(intel_crtc);
6325
		intel_dp_set_m_n(intel_crtc);
6300
 
6326
 
6301
	intel_crtc->lowfreq_avail = false;
6327
	intel_crtc->lowfreq_avail = false;
6302
 
6328
 
6303
	intel_set_pipe_timings(intel_crtc);
6329
	intel_set_pipe_timings(intel_crtc);
6304
 
6330
 
6305
	if (intel_crtc->config.has_pch_encoder) {
6331
	if (intel_crtc->config.has_pch_encoder) {
6306
		intel_cpu_transcoder_set_m_n(intel_crtc,
6332
		intel_cpu_transcoder_set_m_n(intel_crtc,
6307
					     &intel_crtc->config.fdi_m_n);
6333
					     &intel_crtc->config.fdi_m_n);
6308
	}
6334
	}
6309
 
6335
 
6310
	haswell_set_pipeconf(crtc);
6336
	haswell_set_pipeconf(crtc);
6311
 
6337
 
6312
	intel_set_pipe_csc(crtc);
6338
	intel_set_pipe_csc(crtc);
6313
 
6339
 
6314
	/* Set up the display plane register */
6340
	/* Set up the display plane register */
6315
	I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
6341
	I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
6316
    POSTING_READ(DSPCNTR(plane));
6342
    POSTING_READ(DSPCNTR(plane));
6317
 
6343
 
6318
	ret = intel_pipe_set_base(crtc, x, y, fb);
6344
	ret = intel_pipe_set_base(crtc, x, y, fb);
6319
 
6345
 
6320
    intel_update_watermarks(dev);
6346
    intel_update_watermarks(dev);
6321
 
6347
 
6322
    return ret;
6348
    return ret;
6323
}
6349
}
6324
 
6350
 
6325
static bool haswell_get_pipe_config(struct intel_crtc *crtc,
6351
static bool haswell_get_pipe_config(struct intel_crtc *crtc,
6326
				    struct intel_crtc_config *pipe_config)
6352
				    struct intel_crtc_config *pipe_config)
6327
{
6353
{
6328
	struct drm_device *dev = crtc->base.dev;
6354
	struct drm_device *dev = crtc->base.dev;
6329
	struct drm_i915_private *dev_priv = dev->dev_private;
6355
	struct drm_i915_private *dev_priv = dev->dev_private;
6330
	enum intel_display_power_domain pfit_domain;
6356
	enum intel_display_power_domain pfit_domain;
6331
	uint32_t tmp;
6357
	uint32_t tmp;
6332
 
6358
 
6333
	pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
6359
	pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
6334
	pipe_config->shared_dpll = DPLL_ID_PRIVATE;
6360
	pipe_config->shared_dpll = DPLL_ID_PRIVATE;
6335
 
6361
 
6336
	tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
6362
	tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
6337
	if (tmp & TRANS_DDI_FUNC_ENABLE) {
6363
	if (tmp & TRANS_DDI_FUNC_ENABLE) {
6338
		enum pipe trans_edp_pipe;
6364
		enum pipe trans_edp_pipe;
6339
		switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
6365
		switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
6340
		default:
6366
		default:
6341
			WARN(1, "unknown pipe linked to edp transcoder\n");
6367
			WARN(1, "unknown pipe linked to edp transcoder\n");
6342
		case TRANS_DDI_EDP_INPUT_A_ONOFF:
6368
		case TRANS_DDI_EDP_INPUT_A_ONOFF:
6343
		case TRANS_DDI_EDP_INPUT_A_ON:
6369
		case TRANS_DDI_EDP_INPUT_A_ON:
6344
			trans_edp_pipe = PIPE_A;
6370
			trans_edp_pipe = PIPE_A;
6345
			break;
6371
			break;
6346
		case TRANS_DDI_EDP_INPUT_B_ONOFF:
6372
		case TRANS_DDI_EDP_INPUT_B_ONOFF:
6347
			trans_edp_pipe = PIPE_B;
6373
			trans_edp_pipe = PIPE_B;
6348
			break;
6374
			break;
6349
		case TRANS_DDI_EDP_INPUT_C_ONOFF:
6375
		case TRANS_DDI_EDP_INPUT_C_ONOFF:
6350
			trans_edp_pipe = PIPE_C;
6376
			trans_edp_pipe = PIPE_C;
6351
			break;
6377
			break;
6352
		}
6378
		}
6353
 
6379
 
6354
		if (trans_edp_pipe == crtc->pipe)
6380
		if (trans_edp_pipe == crtc->pipe)
6355
			pipe_config->cpu_transcoder = TRANSCODER_EDP;
6381
			pipe_config->cpu_transcoder = TRANSCODER_EDP;
6356
	}
6382
	}
6357
 
6383
 
6358
	if (!intel_display_power_enabled(dev,
6384
	if (!intel_display_power_enabled(dev,
6359
			POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
6385
			POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
6360
		return false;
6386
		return false;
6361
 
6387
 
6362
	tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
6388
	tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
6363
	if (!(tmp & PIPECONF_ENABLE))
6389
	if (!(tmp & PIPECONF_ENABLE))
6364
		return false;
6390
		return false;
6365
 
6391
 
6366
	/*
6392
	/*
6367
	 * Haswell has only FDI/PCH transcoder A. It is which is connected to
6393
	 * Haswell has only FDI/PCH transcoder A. It is which is connected to
6368
	 * DDI E. So just check whether this pipe is wired to DDI E and whether
6394
	 * DDI E. So just check whether this pipe is wired to DDI E and whether
6369
	 * the PCH transcoder is on.
6395
	 * the PCH transcoder is on.
6370
	 */
6396
	 */
6371
	tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
6397
	tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
6372
	if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
6398
	if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
6373
	    I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
6399
	    I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
6374
		pipe_config->has_pch_encoder = true;
6400
		pipe_config->has_pch_encoder = true;
6375
 
6401
 
6376
		tmp = I915_READ(FDI_RX_CTL(PIPE_A));
6402
		tmp = I915_READ(FDI_RX_CTL(PIPE_A));
6377
		pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6403
		pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6378
					  FDI_DP_PORT_WIDTH_SHIFT) + 1;
6404
					  FDI_DP_PORT_WIDTH_SHIFT) + 1;
6379
 
6405
 
6380
		ironlake_get_fdi_m_n_config(crtc, pipe_config);
6406
		ironlake_get_fdi_m_n_config(crtc, pipe_config);
6381
	}
6407
	}
6382
 
6408
 
6383
	intel_get_pipe_timings(crtc, pipe_config);
6409
	intel_get_pipe_timings(crtc, pipe_config);
6384
 
6410
 
6385
	pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
6411
	pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
6386
	if (intel_display_power_enabled(dev, pfit_domain))
6412
	if (intel_display_power_enabled(dev, pfit_domain))
6387
		ironlake_get_pfit_config(crtc, pipe_config);
6413
		ironlake_get_pfit_config(crtc, pipe_config);
6388
 
6414
 
6389
	pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
6415
	pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
6390
				   (I915_READ(IPS_CTL) & IPS_ENABLE);
6416
				   (I915_READ(IPS_CTL) & IPS_ENABLE);
6391
 
6417
 
6392
	pipe_config->pixel_multiplier = 1;
6418
	pipe_config->pixel_multiplier = 1;
6393
 
6419
 
6394
	return true;
6420
	return true;
6395
}
6421
}
6396
 
6422
 
6397
static int intel_crtc_mode_set(struct drm_crtc *crtc,
6423
static int intel_crtc_mode_set(struct drm_crtc *crtc,
6398
			       int x, int y,
6424
			       int x, int y,
6399
			       struct drm_framebuffer *fb)
6425
			       struct drm_framebuffer *fb)
6400
{
6426
{
6401
	struct drm_device *dev = crtc->dev;
6427
	struct drm_device *dev = crtc->dev;
6402
	struct drm_i915_private *dev_priv = dev->dev_private;
6428
	struct drm_i915_private *dev_priv = dev->dev_private;
6403
	struct intel_encoder *encoder;
6429
	struct intel_encoder *encoder;
6404
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6430
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6405
	struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
6431
	struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
6406
	int pipe = intel_crtc->pipe;
6432
	int pipe = intel_crtc->pipe;
6407
	int ret;
6433
	int ret;
6408
 
6434
 
6409
	drm_vblank_pre_modeset(dev, pipe);
6435
	drm_vblank_pre_modeset(dev, pipe);
6410
 
6436
 
6411
	ret = dev_priv->display.crtc_mode_set(crtc, x, y, fb);
6437
	ret = dev_priv->display.crtc_mode_set(crtc, x, y, fb);
6412
 
6438
 
6413
	drm_vblank_post_modeset(dev, pipe);
6439
	drm_vblank_post_modeset(dev, pipe);
6414
 
6440
 
6415
	if (ret != 0)
6441
	if (ret != 0)
6416
	return ret;
6442
	return ret;
6417
 
6443
 
6418
	for_each_encoder_on_crtc(dev, crtc, encoder) {
6444
	for_each_encoder_on_crtc(dev, crtc, encoder) {
6419
		DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
6445
		DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
6420
			encoder->base.base.id,
6446
			encoder->base.base.id,
6421
			drm_get_encoder_name(&encoder->base),
6447
			drm_get_encoder_name(&encoder->base),
6422
			mode->base.id, mode->name);
6448
			mode->base.id, mode->name);
6423
			encoder->mode_set(encoder);
6449
			encoder->mode_set(encoder);
6424
	}
6450
	}
6425
 
6451
 
6426
	return 0;
6452
	return 0;
6427
}
6453
}
6428
 
6454
 
6429
static bool intel_eld_uptodate(struct drm_connector *connector,
6455
static bool intel_eld_uptodate(struct drm_connector *connector,
6430
			       int reg_eldv, uint32_t bits_eldv,
6456
			       int reg_eldv, uint32_t bits_eldv,
6431
			       int reg_elda, uint32_t bits_elda,
6457
			       int reg_elda, uint32_t bits_elda,
6432
			       int reg_edid)
6458
			       int reg_edid)
6433
{
6459
{
6434
	struct drm_i915_private *dev_priv = connector->dev->dev_private;
6460
	struct drm_i915_private *dev_priv = connector->dev->dev_private;
6435
	uint8_t *eld = connector->eld;
6461
	uint8_t *eld = connector->eld;
6436
	uint32_t i;
6462
	uint32_t i;
6437
 
6463
 
6438
	i = I915_READ(reg_eldv);
6464
	i = I915_READ(reg_eldv);
6439
	i &= bits_eldv;
6465
	i &= bits_eldv;
6440
 
6466
 
6441
	if (!eld[0])
6467
	if (!eld[0])
6442
		return !i;
6468
		return !i;
6443
 
6469
 
6444
	if (!i)
6470
	if (!i)
6445
		return false;
6471
		return false;
6446
 
6472
 
6447
	i = I915_READ(reg_elda);
6473
	i = I915_READ(reg_elda);
6448
	i &= ~bits_elda;
6474
	i &= ~bits_elda;
6449
	I915_WRITE(reg_elda, i);
6475
	I915_WRITE(reg_elda, i);
6450
 
6476
 
6451
	for (i = 0; i < eld[2]; i++)
6477
	for (i = 0; i < eld[2]; i++)
6452
		if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
6478
		if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
6453
			return false;
6479
			return false;
6454
 
6480
 
6455
	return true;
6481
	return true;
6456
}
6482
}
6457
 
6483
 
6458
static void g4x_write_eld(struct drm_connector *connector,
6484
static void g4x_write_eld(struct drm_connector *connector,
6459
			  struct drm_crtc *crtc)
6485
			  struct drm_crtc *crtc)
6460
{
6486
{
6461
	struct drm_i915_private *dev_priv = connector->dev->dev_private;
6487
	struct drm_i915_private *dev_priv = connector->dev->dev_private;
6462
	uint8_t *eld = connector->eld;
6488
	uint8_t *eld = connector->eld;
6463
	uint32_t eldv;
6489
	uint32_t eldv;
6464
	uint32_t len;
6490
	uint32_t len;
6465
	uint32_t i;
6491
	uint32_t i;
6466
 
6492
 
6467
	i = I915_READ(G4X_AUD_VID_DID);
6493
	i = I915_READ(G4X_AUD_VID_DID);
6468
 
6494
 
6469
	if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
6495
	if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
6470
		eldv = G4X_ELDV_DEVCL_DEVBLC;
6496
		eldv = G4X_ELDV_DEVCL_DEVBLC;
6471
	else
6497
	else
6472
		eldv = G4X_ELDV_DEVCTG;
6498
		eldv = G4X_ELDV_DEVCTG;
6473
 
6499
 
6474
	if (intel_eld_uptodate(connector,
6500
	if (intel_eld_uptodate(connector,
6475
			       G4X_AUD_CNTL_ST, eldv,
6501
			       G4X_AUD_CNTL_ST, eldv,
6476
			       G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
6502
			       G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
6477
			       G4X_HDMIW_HDMIEDID))
6503
			       G4X_HDMIW_HDMIEDID))
6478
		return;
6504
		return;
6479
 
6505
 
6480
	i = I915_READ(G4X_AUD_CNTL_ST);
6506
	i = I915_READ(G4X_AUD_CNTL_ST);
6481
	i &= ~(eldv | G4X_ELD_ADDR);
6507
	i &= ~(eldv | G4X_ELD_ADDR);
6482
	len = (i >> 9) & 0x1f;		/* ELD buffer size */
6508
	len = (i >> 9) & 0x1f;		/* ELD buffer size */
6483
	I915_WRITE(G4X_AUD_CNTL_ST, i);
6509
	I915_WRITE(G4X_AUD_CNTL_ST, i);
6484
 
6510
 
6485
	if (!eld[0])
6511
	if (!eld[0])
6486
		return;
6512
		return;
6487
 
6513
 
6488
	len = min_t(uint8_t, eld[2], len);
6514
	len = min_t(uint8_t, eld[2], len);
6489
	DRM_DEBUG_DRIVER("ELD size %d\n", len);
6515
	DRM_DEBUG_DRIVER("ELD size %d\n", len);
6490
	for (i = 0; i < len; i++)
6516
	for (i = 0; i < len; i++)
6491
		I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
6517
		I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
6492
 
6518
 
6493
	i = I915_READ(G4X_AUD_CNTL_ST);
6519
	i = I915_READ(G4X_AUD_CNTL_ST);
6494
	i |= eldv;
6520
	i |= eldv;
6495
	I915_WRITE(G4X_AUD_CNTL_ST, i);
6521
	I915_WRITE(G4X_AUD_CNTL_ST, i);
6496
}
6522
}
6497
 
6523
 
6498
static void haswell_write_eld(struct drm_connector *connector,
6524
static void haswell_write_eld(struct drm_connector *connector,
6499
				     struct drm_crtc *crtc)
6525
				     struct drm_crtc *crtc)
6500
{
6526
{
6501
	struct drm_i915_private *dev_priv = connector->dev->dev_private;
6527
	struct drm_i915_private *dev_priv = connector->dev->dev_private;
6502
	uint8_t *eld = connector->eld;
6528
	uint8_t *eld = connector->eld;
6503
	struct drm_device *dev = crtc->dev;
6529
	struct drm_device *dev = crtc->dev;
6504
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6530
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6505
	uint32_t eldv;
6531
	uint32_t eldv;
6506
	uint32_t i;
6532
	uint32_t i;
6507
	int len;
6533
	int len;
6508
	int pipe = to_intel_crtc(crtc)->pipe;
6534
	int pipe = to_intel_crtc(crtc)->pipe;
6509
	int tmp;
6535
	int tmp;
6510
 
6536
 
6511
	int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
6537
	int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
6512
	int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
6538
	int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
6513
	int aud_config = HSW_AUD_CFG(pipe);
6539
	int aud_config = HSW_AUD_CFG(pipe);
6514
	int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
6540
	int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
6515
 
6541
 
6516
 
6542
 
6517
	DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
6543
	DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
6518
 
6544
 
6519
	/* Audio output enable */
6545
	/* Audio output enable */
6520
	DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
6546
	DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
6521
	tmp = I915_READ(aud_cntrl_st2);
6547
	tmp = I915_READ(aud_cntrl_st2);
6522
	tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
6548
	tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
6523
	I915_WRITE(aud_cntrl_st2, tmp);
6549
	I915_WRITE(aud_cntrl_st2, tmp);
6524
 
6550
 
6525
	/* Wait for 1 vertical blank */
6551
	/* Wait for 1 vertical blank */
6526
	intel_wait_for_vblank(dev, pipe);
6552
	intel_wait_for_vblank(dev, pipe);
6527
 
6553
 
6528
	/* Set ELD valid state */
6554
	/* Set ELD valid state */
6529
	tmp = I915_READ(aud_cntrl_st2);
6555
	tmp = I915_READ(aud_cntrl_st2);
6530
	DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
6556
	DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
6531
	tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
6557
	tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
6532
	I915_WRITE(aud_cntrl_st2, tmp);
6558
	I915_WRITE(aud_cntrl_st2, tmp);
6533
	tmp = I915_READ(aud_cntrl_st2);
6559
	tmp = I915_READ(aud_cntrl_st2);
6534
	DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
6560
	DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
6535
 
6561
 
6536
	/* Enable HDMI mode */
6562
	/* Enable HDMI mode */
6537
	tmp = I915_READ(aud_config);
6563
	tmp = I915_READ(aud_config);
6538
	DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
6564
	DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
6539
	/* clear N_programing_enable and N_value_index */
6565
	/* clear N_programing_enable and N_value_index */
6540
	tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
6566
	tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
6541
	I915_WRITE(aud_config, tmp);
6567
	I915_WRITE(aud_config, tmp);
6542
 
6568
 
6543
	DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
6569
	DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
6544
 
6570
 
6545
	eldv = AUDIO_ELD_VALID_A << (pipe * 4);
6571
	eldv = AUDIO_ELD_VALID_A << (pipe * 4);
6546
	intel_crtc->eld_vld = true;
6572
	intel_crtc->eld_vld = true;
6547
 
6573
 
6548
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6574
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6549
		DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6575
		DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6550
		eld[5] |= (1 << 2);	/* Conn_Type, 0x1 = DisplayPort */
6576
		eld[5] |= (1 << 2);	/* Conn_Type, 0x1 = DisplayPort */
6551
		I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6577
		I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6552
	} else
6578
	} else
6553
		I915_WRITE(aud_config, 0);
6579
		I915_WRITE(aud_config, 0);
6554
 
6580
 
6555
	if (intel_eld_uptodate(connector,
6581
	if (intel_eld_uptodate(connector,
6556
			       aud_cntrl_st2, eldv,
6582
			       aud_cntrl_st2, eldv,
6557
			       aud_cntl_st, IBX_ELD_ADDRESS,
6583
			       aud_cntl_st, IBX_ELD_ADDRESS,
6558
			       hdmiw_hdmiedid))
6584
			       hdmiw_hdmiedid))
6559
		return;
6585
		return;
6560
 
6586
 
6561
	i = I915_READ(aud_cntrl_st2);
6587
	i = I915_READ(aud_cntrl_st2);
6562
	i &= ~eldv;
6588
	i &= ~eldv;
6563
	I915_WRITE(aud_cntrl_st2, i);
6589
	I915_WRITE(aud_cntrl_st2, i);
6564
 
6590
 
6565
	if (!eld[0])
6591
	if (!eld[0])
6566
		return;
6592
		return;
6567
 
6593
 
6568
	i = I915_READ(aud_cntl_st);
6594
	i = I915_READ(aud_cntl_st);
6569
	i &= ~IBX_ELD_ADDRESS;
6595
	i &= ~IBX_ELD_ADDRESS;
6570
	I915_WRITE(aud_cntl_st, i);
6596
	I915_WRITE(aud_cntl_st, i);
6571
	i = (i >> 29) & DIP_PORT_SEL_MASK;		/* DIP_Port_Select, 0x1 = PortB */
6597
	i = (i >> 29) & DIP_PORT_SEL_MASK;		/* DIP_Port_Select, 0x1 = PortB */
6572
	DRM_DEBUG_DRIVER("port num:%d\n", i);
6598
	DRM_DEBUG_DRIVER("port num:%d\n", i);
6573
 
6599
 
6574
	len = min_t(uint8_t, eld[2], 21);	/* 84 bytes of hw ELD buffer */
6600
	len = min_t(uint8_t, eld[2], 21);	/* 84 bytes of hw ELD buffer */
6575
	DRM_DEBUG_DRIVER("ELD size %d\n", len);
6601
	DRM_DEBUG_DRIVER("ELD size %d\n", len);
6576
	for (i = 0; i < len; i++)
6602
	for (i = 0; i < len; i++)
6577
		I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6603
		I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6578
 
6604
 
6579
	i = I915_READ(aud_cntrl_st2);
6605
	i = I915_READ(aud_cntrl_st2);
6580
	i |= eldv;
6606
	i |= eldv;
6581
	I915_WRITE(aud_cntrl_st2, i);
6607
	I915_WRITE(aud_cntrl_st2, i);
6582
 
6608
 
6583
}
6609
}
6584
 
6610
 
6585
static void ironlake_write_eld(struct drm_connector *connector,
6611
static void ironlake_write_eld(struct drm_connector *connector,
6586
				     struct drm_crtc *crtc)
6612
				     struct drm_crtc *crtc)
6587
{
6613
{
6588
	struct drm_i915_private *dev_priv = connector->dev->dev_private;
6614
	struct drm_i915_private *dev_priv = connector->dev->dev_private;
6589
	uint8_t *eld = connector->eld;
6615
	uint8_t *eld = connector->eld;
6590
	uint32_t eldv;
6616
	uint32_t eldv;
6591
	uint32_t i;
6617
	uint32_t i;
6592
	int len;
6618
	int len;
6593
	int hdmiw_hdmiedid;
6619
	int hdmiw_hdmiedid;
6594
	int aud_config;
6620
	int aud_config;
6595
	int aud_cntl_st;
6621
	int aud_cntl_st;
6596
	int aud_cntrl_st2;
6622
	int aud_cntrl_st2;
6597
	int pipe = to_intel_crtc(crtc)->pipe;
6623
	int pipe = to_intel_crtc(crtc)->pipe;
6598
 
6624
 
6599
	if (HAS_PCH_IBX(connector->dev)) {
6625
	if (HAS_PCH_IBX(connector->dev)) {
6600
		hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
6626
		hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
6601
		aud_config = IBX_AUD_CFG(pipe);
6627
		aud_config = IBX_AUD_CFG(pipe);
6602
		aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
6628
		aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
6603
		aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
6629
		aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
6604
	} else {
6630
	} else {
6605
		hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
6631
		hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
6606
		aud_config = CPT_AUD_CFG(pipe);
6632
		aud_config = CPT_AUD_CFG(pipe);
6607
		aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
6633
		aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
6608
		aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
6634
		aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
6609
	}
6635
	}
6610
 
6636
 
6611
	DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
6637
	DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
6612
 
6638
 
6613
	i = I915_READ(aud_cntl_st);
6639
	i = I915_READ(aud_cntl_st);
6614
	i = (i >> 29) & DIP_PORT_SEL_MASK;		/* DIP_Port_Select, 0x1 = PortB */
6640
	i = (i >> 29) & DIP_PORT_SEL_MASK;		/* DIP_Port_Select, 0x1 = PortB */
6615
	if (!i) {
6641
	if (!i) {
6616
		DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
6642
		DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
6617
		/* operate blindly on all ports */
6643
		/* operate blindly on all ports */
6618
		eldv = IBX_ELD_VALIDB;
6644
		eldv = IBX_ELD_VALIDB;
6619
		eldv |= IBX_ELD_VALIDB << 4;
6645
		eldv |= IBX_ELD_VALIDB << 4;
6620
		eldv |= IBX_ELD_VALIDB << 8;
6646
		eldv |= IBX_ELD_VALIDB << 8;
6621
	} else {
6647
	} else {
6622
		DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
6648
		DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
6623
		eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
6649
		eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
6624
	}
6650
	}
6625
 
6651
 
6626
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6652
	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6627
		DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6653
		DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6628
		eld[5] |= (1 << 2);	/* Conn_Type, 0x1 = DisplayPort */
6654
		eld[5] |= (1 << 2);	/* Conn_Type, 0x1 = DisplayPort */
6629
		I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6655
		I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6630
	} else
6656
	} else
6631
		I915_WRITE(aud_config, 0);
6657
		I915_WRITE(aud_config, 0);
6632
 
6658
 
6633
	if (intel_eld_uptodate(connector,
6659
	if (intel_eld_uptodate(connector,
6634
			       aud_cntrl_st2, eldv,
6660
			       aud_cntrl_st2, eldv,
6635
			       aud_cntl_st, IBX_ELD_ADDRESS,
6661
			       aud_cntl_st, IBX_ELD_ADDRESS,
6636
			       hdmiw_hdmiedid))
6662
			       hdmiw_hdmiedid))
6637
		return;
6663
		return;
6638
 
6664
 
6639
	i = I915_READ(aud_cntrl_st2);
6665
	i = I915_READ(aud_cntrl_st2);
6640
	i &= ~eldv;
6666
	i &= ~eldv;
6641
	I915_WRITE(aud_cntrl_st2, i);
6667
	I915_WRITE(aud_cntrl_st2, i);
6642
 
6668
 
6643
	if (!eld[0])
6669
	if (!eld[0])
6644
		return;
6670
		return;
6645
 
6671
 
6646
	i = I915_READ(aud_cntl_st);
6672
	i = I915_READ(aud_cntl_st);
6647
	i &= ~IBX_ELD_ADDRESS;
6673
	i &= ~IBX_ELD_ADDRESS;
6648
	I915_WRITE(aud_cntl_st, i);
6674
	I915_WRITE(aud_cntl_st, i);
6649
 
6675
 
6650
	len = min_t(uint8_t, eld[2], 21);	/* 84 bytes of hw ELD buffer */
6676
	len = min_t(uint8_t, eld[2], 21);	/* 84 bytes of hw ELD buffer */
6651
	DRM_DEBUG_DRIVER("ELD size %d\n", len);
6677
	DRM_DEBUG_DRIVER("ELD size %d\n", len);
6652
	for (i = 0; i < len; i++)
6678
	for (i = 0; i < len; i++)
6653
		I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6679
		I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6654
 
6680
 
6655
	i = I915_READ(aud_cntrl_st2);
6681
	i = I915_READ(aud_cntrl_st2);
6656
	i |= eldv;
6682
	i |= eldv;
6657
	I915_WRITE(aud_cntrl_st2, i);
6683
	I915_WRITE(aud_cntrl_st2, i);
6658
}
6684
}
6659
 
6685
 
6660
void intel_write_eld(struct drm_encoder *encoder,
6686
void intel_write_eld(struct drm_encoder *encoder,
6661
		     struct drm_display_mode *mode)
6687
		     struct drm_display_mode *mode)
6662
{
6688
{
6663
	struct drm_crtc *crtc = encoder->crtc;
6689
	struct drm_crtc *crtc = encoder->crtc;
6664
	struct drm_connector *connector;
6690
	struct drm_connector *connector;
6665
	struct drm_device *dev = encoder->dev;
6691
	struct drm_device *dev = encoder->dev;
6666
	struct drm_i915_private *dev_priv = dev->dev_private;
6692
	struct drm_i915_private *dev_priv = dev->dev_private;
6667
 
6693
 
6668
	connector = drm_select_eld(encoder, mode);
6694
	connector = drm_select_eld(encoder, mode);
6669
	if (!connector)
6695
	if (!connector)
6670
		return;
6696
		return;
6671
 
6697
 
6672
	DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6698
	DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6673
			 connector->base.id,
6699
			 connector->base.id,
6674
			 drm_get_connector_name(connector),
6700
			 drm_get_connector_name(connector),
6675
			 connector->encoder->base.id,
6701
			 connector->encoder->base.id,
6676
			 drm_get_encoder_name(connector->encoder));
6702
			 drm_get_encoder_name(connector->encoder));
6677
 
6703
 
6678
	connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
6704
	connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
6679
 
6705
 
6680
	if (dev_priv->display.write_eld)
6706
	if (dev_priv->display.write_eld)
6681
		dev_priv->display.write_eld(connector, crtc);
6707
		dev_priv->display.write_eld(connector, crtc);
6682
}
6708
}
6683
 
6709
 
6684
/** Loads the palette/gamma unit for the CRTC with the prepared values */
6710
/** Loads the palette/gamma unit for the CRTC with the prepared values */
6685
void intel_crtc_load_lut(struct drm_crtc *crtc)
6711
void intel_crtc_load_lut(struct drm_crtc *crtc)
6686
{
6712
{
6687
	struct drm_device *dev = crtc->dev;
6713
	struct drm_device *dev = crtc->dev;
6688
	struct drm_i915_private *dev_priv = dev->dev_private;
6714
	struct drm_i915_private *dev_priv = dev->dev_private;
6689
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6715
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6690
	enum pipe pipe = intel_crtc->pipe;
6716
	enum pipe pipe = intel_crtc->pipe;
6691
	int palreg = PALETTE(pipe);
6717
	int palreg = PALETTE(pipe);
6692
	int i;
6718
	int i;
6693
	bool reenable_ips = false;
6719
	bool reenable_ips = false;
6694
 
6720
 
6695
	/* The clocks have to be on to load the palette. */
6721
	/* The clocks have to be on to load the palette. */
6696
	if (!crtc->enabled || !intel_crtc->active)
6722
	if (!crtc->enabled || !intel_crtc->active)
6697
		return;
6723
		return;
6698
 
6724
 
6699
	if (!HAS_PCH_SPLIT(dev_priv->dev))
6725
	if (!HAS_PCH_SPLIT(dev_priv->dev))
6700
		assert_pll_enabled(dev_priv, pipe);
6726
		assert_pll_enabled(dev_priv, pipe);
6701
 
6727
 
6702
	/* use legacy palette for Ironlake */
6728
	/* use legacy palette for Ironlake */
6703
	if (HAS_PCH_SPLIT(dev))
6729
	if (HAS_PCH_SPLIT(dev))
6704
		palreg = LGC_PALETTE(pipe);
6730
		palreg = LGC_PALETTE(pipe);
6705
 
6731
 
6706
	/* Workaround : Do not read or write the pipe palette/gamma data while
6732
	/* Workaround : Do not read or write the pipe palette/gamma data while
6707
	 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
6733
	 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
6708
	 */
6734
	 */
6709
	if (intel_crtc->config.ips_enabled &&
6735
	if (intel_crtc->config.ips_enabled &&
6710
	    ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
6736
	    ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
6711
	     GAMMA_MODE_MODE_SPLIT)) {
6737
	     GAMMA_MODE_MODE_SPLIT)) {
6712
		hsw_disable_ips(intel_crtc);
6738
		hsw_disable_ips(intel_crtc);
6713
		reenable_ips = true;
6739
		reenable_ips = true;
6714
	}
6740
	}
6715
 
6741
 
6716
	for (i = 0; i < 256; i++) {
6742
	for (i = 0; i < 256; i++) {
6717
		I915_WRITE(palreg + 4 * i,
6743
		I915_WRITE(palreg + 4 * i,
6718
			   (intel_crtc->lut_r[i] << 16) |
6744
			   (intel_crtc->lut_r[i] << 16) |
6719
			   (intel_crtc->lut_g[i] << 8) |
6745
			   (intel_crtc->lut_g[i] << 8) |
6720
			   intel_crtc->lut_b[i]);
6746
			   intel_crtc->lut_b[i]);
6721
	}
6747
	}
6722
 
6748
 
6723
	if (reenable_ips)
6749
	if (reenable_ips)
6724
		hsw_enable_ips(intel_crtc);
6750
		hsw_enable_ips(intel_crtc);
6725
}
6751
}
6726
 
6752
 
6727
#if 0
6753
#if 0
6728
static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
6754
static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
6729
{
6755
{
6730
	struct drm_device *dev = crtc->dev;
6756
	struct drm_device *dev = crtc->dev;
6731
	struct drm_i915_private *dev_priv = dev->dev_private;
6757
	struct drm_i915_private *dev_priv = dev->dev_private;
6732
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6758
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6733
	bool visible = base != 0;
6759
	bool visible = base != 0;
6734
	u32 cntl;
6760
	u32 cntl;
6735
 
6761
 
6736
	if (intel_crtc->cursor_visible == visible)
6762
	if (intel_crtc->cursor_visible == visible)
6737
		return;
6763
		return;
6738
 
6764
 
6739
	cntl = I915_READ(_CURACNTR);
6765
	cntl = I915_READ(_CURACNTR);
6740
	if (visible) {
6766
	if (visible) {
6741
		/* On these chipsets we can only modify the base whilst
6767
		/* On these chipsets we can only modify the base whilst
6742
		 * the cursor is disabled.
6768
		 * the cursor is disabled.
6743
		 */
6769
		 */
6744
		I915_WRITE(_CURABASE, base);
6770
		I915_WRITE(_CURABASE, base);
6745
 
6771
 
6746
		cntl &= ~(CURSOR_FORMAT_MASK);
6772
		cntl &= ~(CURSOR_FORMAT_MASK);
6747
		/* XXX width must be 64, stride 256 => 0x00 << 28 */
6773
		/* XXX width must be 64, stride 256 => 0x00 << 28 */
6748
		cntl |= CURSOR_ENABLE |
6774
		cntl |= CURSOR_ENABLE |
6749
			CURSOR_GAMMA_ENABLE |
6775
			CURSOR_GAMMA_ENABLE |
6750
			CURSOR_FORMAT_ARGB;
6776
			CURSOR_FORMAT_ARGB;
6751
	} else
6777
	} else
6752
		cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
6778
		cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
6753
	I915_WRITE(_CURACNTR, cntl);
6779
	I915_WRITE(_CURACNTR, cntl);
6754
 
6780
 
6755
	intel_crtc->cursor_visible = visible;
6781
	intel_crtc->cursor_visible = visible;
6756
}
6782
}
6757
 
6783
 
6758
static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
6784
static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
6759
{
6785
{
6760
	struct drm_device *dev = crtc->dev;
6786
	struct drm_device *dev = crtc->dev;
6761
	struct drm_i915_private *dev_priv = dev->dev_private;
6787
	struct drm_i915_private *dev_priv = dev->dev_private;
6762
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6788
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6763
	int pipe = intel_crtc->pipe;
6789
	int pipe = intel_crtc->pipe;
6764
	bool visible = base != 0;
6790
	bool visible = base != 0;
6765
 
6791
 
6766
	if (intel_crtc->cursor_visible != visible) {
6792
	if (intel_crtc->cursor_visible != visible) {
6767
		uint32_t cntl = I915_READ(CURCNTR(pipe));
6793
		uint32_t cntl = I915_READ(CURCNTR(pipe));
6768
		if (base) {
6794
		if (base) {
6769
			cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
6795
			cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
6770
			cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6796
			cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6771
			cntl |= pipe << 28; /* Connect to correct pipe */
6797
			cntl |= pipe << 28; /* Connect to correct pipe */
6772
		} else {
6798
		} else {
6773
			cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6799
			cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6774
			cntl |= CURSOR_MODE_DISABLE;
6800
			cntl |= CURSOR_MODE_DISABLE;
6775
		}
6801
		}
6776
		I915_WRITE(CURCNTR(pipe), cntl);
6802
		I915_WRITE(CURCNTR(pipe), cntl);
6777
 
6803
 
6778
		intel_crtc->cursor_visible = visible;
6804
		intel_crtc->cursor_visible = visible;
6779
	}
6805
	}
6780
	/* and commit changes on next vblank */
6806
	/* and commit changes on next vblank */
6781
	POSTING_READ(CURCNTR(pipe));
6807
	POSTING_READ(CURCNTR(pipe));
6782
	I915_WRITE(CURBASE(pipe), base);
6808
	I915_WRITE(CURBASE(pipe), base);
6783
	POSTING_READ(CURBASE(pipe));
6809
	POSTING_READ(CURBASE(pipe));
6784
}
6810
}
6785
 
6811
 
6786
static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
6812
static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
6787
{
6813
{
6788
	struct drm_device *dev = crtc->dev;
6814
	struct drm_device *dev = crtc->dev;
6789
	struct drm_i915_private *dev_priv = dev->dev_private;
6815
	struct drm_i915_private *dev_priv = dev->dev_private;
6790
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6816
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6791
	int pipe = intel_crtc->pipe;
6817
	int pipe = intel_crtc->pipe;
6792
	bool visible = base != 0;
6818
	bool visible = base != 0;
6793
 
6819
 
6794
	if (intel_crtc->cursor_visible != visible) {
6820
	if (intel_crtc->cursor_visible != visible) {
6795
		uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
6821
		uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
6796
		if (base) {
6822
		if (base) {
6797
			cntl &= ~CURSOR_MODE;
6823
			cntl &= ~CURSOR_MODE;
6798
			cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6824
			cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6799
		} else {
6825
		} else {
6800
			cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6826
			cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6801
			cntl |= CURSOR_MODE_DISABLE;
6827
			cntl |= CURSOR_MODE_DISABLE;
6802
		}
6828
		}
6803
		if (IS_HASWELL(dev)) {
6829
		if (IS_HASWELL(dev)) {
6804
			cntl |= CURSOR_PIPE_CSC_ENABLE;
6830
			cntl |= CURSOR_PIPE_CSC_ENABLE;
6805
			cntl &= ~CURSOR_TRICKLE_FEED_DISABLE;
6831
			cntl &= ~CURSOR_TRICKLE_FEED_DISABLE;
6806
		}
6832
		}
6807
		I915_WRITE(CURCNTR_IVB(pipe), cntl);
6833
		I915_WRITE(CURCNTR_IVB(pipe), cntl);
6808
 
6834
 
6809
		intel_crtc->cursor_visible = visible;
6835
		intel_crtc->cursor_visible = visible;
6810
	}
6836
	}
6811
	/* and commit changes on next vblank */
6837
	/* and commit changes on next vblank */
6812
	POSTING_READ(CURCNTR_IVB(pipe));
6838
	POSTING_READ(CURCNTR_IVB(pipe));
6813
	I915_WRITE(CURBASE_IVB(pipe), base);
6839
	I915_WRITE(CURBASE_IVB(pipe), base);
6814
	POSTING_READ(CURBASE_IVB(pipe));
6840
	POSTING_READ(CURBASE_IVB(pipe));
6815
}
6841
}
6816
 
6842
 
6817
/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6843
/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6818
static void intel_crtc_update_cursor(struct drm_crtc *crtc,
6844
static void intel_crtc_update_cursor(struct drm_crtc *crtc,
6819
				     bool on)
6845
				     bool on)
6820
{
6846
{
6821
	struct drm_device *dev = crtc->dev;
6847
	struct drm_device *dev = crtc->dev;
6822
	struct drm_i915_private *dev_priv = dev->dev_private;
6848
	struct drm_i915_private *dev_priv = dev->dev_private;
6823
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6849
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6824
	int pipe = intel_crtc->pipe;
6850
	int pipe = intel_crtc->pipe;
6825
	int x = intel_crtc->cursor_x;
6851
	int x = intel_crtc->cursor_x;
6826
	int y = intel_crtc->cursor_y;
6852
	int y = intel_crtc->cursor_y;
6827
	u32 base, pos;
6853
	u32 base, pos;
6828
	bool visible;
6854
	bool visible;
6829
 
6855
 
6830
	pos = 0;
6856
	pos = 0;
6831
 
6857
 
6832
	if (on && crtc->enabled && crtc->fb) {
6858
	if (on && crtc->enabled && crtc->fb) {
6833
		base = intel_crtc->cursor_addr;
6859
		base = intel_crtc->cursor_addr;
6834
		if (x > (int) crtc->fb->width)
6860
		if (x > (int) crtc->fb->width)
6835
			base = 0;
6861
			base = 0;
6836
 
6862
 
6837
		if (y > (int) crtc->fb->height)
6863
		if (y > (int) crtc->fb->height)
6838
			base = 0;
6864
			base = 0;
6839
	} else
6865
	} else
6840
		base = 0;
6866
		base = 0;
6841
 
6867
 
6842
	if (x < 0) {
6868
	if (x < 0) {
6843
		if (x + intel_crtc->cursor_width < 0)
6869
		if (x + intel_crtc->cursor_width < 0)
6844
			base = 0;
6870
			base = 0;
6845
 
6871
 
6846
		pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
6872
		pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
6847
		x = -x;
6873
		x = -x;
6848
	}
6874
	}
6849
	pos |= x << CURSOR_X_SHIFT;
6875
	pos |= x << CURSOR_X_SHIFT;
6850
 
6876
 
6851
	if (y < 0) {
6877
	if (y < 0) {
6852
		if (y + intel_crtc->cursor_height < 0)
6878
		if (y + intel_crtc->cursor_height < 0)
6853
			base = 0;
6879
			base = 0;
6854
 
6880
 
6855
		pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
6881
		pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
6856
		y = -y;
6882
		y = -y;
6857
	}
6883
	}
6858
	pos |= y << CURSOR_Y_SHIFT;
6884
	pos |= y << CURSOR_Y_SHIFT;
6859
 
6885
 
6860
	visible = base != 0;
6886
	visible = base != 0;
6861
	if (!visible && !intel_crtc->cursor_visible)
6887
	if (!visible && !intel_crtc->cursor_visible)
6862
		return;
6888
		return;
6863
 
6889
 
6864
	if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
6890
	if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
6865
		I915_WRITE(CURPOS_IVB(pipe), pos);
6891
		I915_WRITE(CURPOS_IVB(pipe), pos);
6866
		ivb_update_cursor(crtc, base);
6892
		ivb_update_cursor(crtc, base);
6867
	} else {
6893
	} else {
6868
		I915_WRITE(CURPOS(pipe), pos);
6894
		I915_WRITE(CURPOS(pipe), pos);
6869
		if (IS_845G(dev) || IS_I865G(dev))
6895
		if (IS_845G(dev) || IS_I865G(dev))
6870
			i845_update_cursor(crtc, base);
6896
			i845_update_cursor(crtc, base);
6871
		else
6897
		else
6872
			i9xx_update_cursor(crtc, base);
6898
			i9xx_update_cursor(crtc, base);
6873
	}
6899
	}
6874
}
6900
}
6875
 
6901
 
6876
static int intel_crtc_cursor_set(struct drm_crtc *crtc,
6902
static int intel_crtc_cursor_set(struct drm_crtc *crtc,
6877
				 struct drm_file *file,
6903
				 struct drm_file *file,
6878
				 uint32_t handle,
6904
				 uint32_t handle,
6879
				 uint32_t width, uint32_t height)
6905
				 uint32_t width, uint32_t height)
6880
{
6906
{
6881
	struct drm_device *dev = crtc->dev;
6907
	struct drm_device *dev = crtc->dev;
6882
	struct drm_i915_private *dev_priv = dev->dev_private;
6908
	struct drm_i915_private *dev_priv = dev->dev_private;
6883
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6909
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6884
	struct drm_i915_gem_object *obj;
6910
	struct drm_i915_gem_object *obj;
6885
	uint32_t addr;
6911
	uint32_t addr;
6886
	int ret;
6912
	int ret;
6887
 
6913
 
6888
	/* if we want to turn off the cursor ignore width and height */
6914
	/* if we want to turn off the cursor ignore width and height */
6889
	if (!handle) {
6915
	if (!handle) {
6890
		DRM_DEBUG_KMS("cursor off\n");
6916
		DRM_DEBUG_KMS("cursor off\n");
6891
		addr = 0;
6917
		addr = 0;
6892
		obj = NULL;
6918
		obj = NULL;
6893
		mutex_lock(&dev->struct_mutex);
6919
		mutex_lock(&dev->struct_mutex);
6894
		goto finish;
6920
		goto finish;
6895
	}
6921
	}
6896
 
6922
 
6897
	/* Currently we only support 64x64 cursors */
6923
	/* Currently we only support 64x64 cursors */
6898
	if (width != 64 || height != 64) {
6924
	if (width != 64 || height != 64) {
6899
		DRM_ERROR("we currently only support 64x64 cursors\n");
6925
		DRM_ERROR("we currently only support 64x64 cursors\n");
6900
		return -EINVAL;
6926
		return -EINVAL;
6901
	}
6927
	}
6902
 
6928
 
6903
	obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
6929
	obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
6904
	if (&obj->base == NULL)
6930
	if (&obj->base == NULL)
6905
		return -ENOENT;
6931
		return -ENOENT;
6906
 
6932
 
6907
	if (obj->base.size < width * height * 4) {
6933
	if (obj->base.size < width * height * 4) {
6908
		DRM_ERROR("buffer is to small\n");
6934
		DRM_ERROR("buffer is to small\n");
6909
		ret = -ENOMEM;
6935
		ret = -ENOMEM;
6910
		goto fail;
6936
		goto fail;
6911
	}
6937
	}
6912
 
6938
 
6913
	/* we only need to pin inside GTT if cursor is non-phy */
6939
	/* we only need to pin inside GTT if cursor is non-phy */
6914
	mutex_lock(&dev->struct_mutex);
6940
	mutex_lock(&dev->struct_mutex);
6915
	if (!dev_priv->info->cursor_needs_physical) {
6941
	if (!dev_priv->info->cursor_needs_physical) {
6916
		unsigned alignment;
6942
		unsigned alignment;
6917
 
6943
 
6918
		if (obj->tiling_mode) {
6944
		if (obj->tiling_mode) {
6919
			DRM_ERROR("cursor cannot be tiled\n");
6945
			DRM_ERROR("cursor cannot be tiled\n");
6920
			ret = -EINVAL;
6946
			ret = -EINVAL;
6921
			goto fail_locked;
6947
			goto fail_locked;
6922
		}
6948
		}
6923
 
6949
 
6924
		/* Note that the w/a also requires 2 PTE of padding following
6950
		/* Note that the w/a also requires 2 PTE of padding following
6925
		 * the bo. We currently fill all unused PTE with the shadow
6951
		 * the bo. We currently fill all unused PTE with the shadow
6926
		 * page and so we should always have valid PTE following the
6952
		 * page and so we should always have valid PTE following the
6927
		 * cursor preventing the VT-d warning.
6953
		 * cursor preventing the VT-d warning.
6928
		 */
6954
		 */
6929
		alignment = 0;
6955
		alignment = 0;
6930
		if (need_vtd_wa(dev))
6956
		if (need_vtd_wa(dev))
6931
			alignment = 64*1024;
6957
			alignment = 64*1024;
6932
 
6958
 
6933
		ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
6959
		ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
6934
		if (ret) {
6960
		if (ret) {
6935
			DRM_ERROR("failed to move cursor bo into the GTT\n");
6961
			DRM_ERROR("failed to move cursor bo into the GTT\n");
6936
			goto fail_locked;
6962
			goto fail_locked;
6937
		}
6963
		}
6938
 
6964
 
6939
		ret = i915_gem_object_put_fence(obj);
6965
		ret = i915_gem_object_put_fence(obj);
6940
		if (ret) {
6966
		if (ret) {
6941
			DRM_ERROR("failed to release fence for cursor");
6967
			DRM_ERROR("failed to release fence for cursor");
6942
			goto fail_unpin;
6968
			goto fail_unpin;
6943
		}
6969
		}
6944
 
6970
 
6945
		addr = i915_gem_obj_ggtt_offset(obj);
6971
		addr = i915_gem_obj_ggtt_offset(obj);
6946
	} else {
6972
	} else {
6947
		int align = IS_I830(dev) ? 16 * 1024 : 256;
6973
		int align = IS_I830(dev) ? 16 * 1024 : 256;
6948
		ret = i915_gem_attach_phys_object(dev, obj,
6974
		ret = i915_gem_attach_phys_object(dev, obj,
6949
						  (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
6975
						  (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
6950
						  align);
6976
						  align);
6951
		if (ret) {
6977
		if (ret) {
6952
			DRM_ERROR("failed to attach phys object\n");
6978
			DRM_ERROR("failed to attach phys object\n");
6953
			goto fail_locked;
6979
			goto fail_locked;
6954
		}
6980
		}
6955
		addr = obj->phys_obj->handle->busaddr;
6981
		addr = obj->phys_obj->handle->busaddr;
6956
	}
6982
	}
6957
 
6983
 
6958
	if (IS_GEN2(dev))
6984
	if (IS_GEN2(dev))
6959
		I915_WRITE(CURSIZE, (height << 12) | width);
6985
		I915_WRITE(CURSIZE, (height << 12) | width);
6960
 
6986
 
6961
 finish:
6987
 finish:
6962
	if (intel_crtc->cursor_bo) {
6988
	if (intel_crtc->cursor_bo) {
6963
		if (dev_priv->info->cursor_needs_physical) {
6989
		if (dev_priv->info->cursor_needs_physical) {
6964
			if (intel_crtc->cursor_bo != obj)
6990
			if (intel_crtc->cursor_bo != obj)
6965
				i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
6991
				i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
6966
		} else
6992
		} else
6967
			i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
6993
			i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
6968
		drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
6994
		drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
6969
	}
6995
	}
6970
 
6996
 
6971
	mutex_unlock(&dev->struct_mutex);
6997
	mutex_unlock(&dev->struct_mutex);
6972
 
6998
 
6973
	intel_crtc->cursor_addr = addr;
6999
	intel_crtc->cursor_addr = addr;
6974
	intel_crtc->cursor_bo = obj;
7000
	intel_crtc->cursor_bo = obj;
6975
	intel_crtc->cursor_width = width;
7001
	intel_crtc->cursor_width = width;
6976
	intel_crtc->cursor_height = height;
7002
	intel_crtc->cursor_height = height;
6977
 
7003
 
6978
	if (intel_crtc->active)
7004
	if (intel_crtc->active)
6979
		intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
7005
		intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
6980
 
7006
 
6981
	return 0;
7007
	return 0;
6982
fail_unpin:
7008
fail_unpin:
6983
	i915_gem_object_unpin_from_display_plane(obj);
7009
	i915_gem_object_unpin_from_display_plane(obj);
6984
fail_locked:
7010
fail_locked:
6985
	mutex_unlock(&dev->struct_mutex);
7011
	mutex_unlock(&dev->struct_mutex);
6986
fail:
7012
fail:
6987
	drm_gem_object_unreference_unlocked(&obj->base);
7013
	drm_gem_object_unreference_unlocked(&obj->base);
6988
	return ret;
7014
	return ret;
6989
}
7015
}
6990
 
7016
 
6991
static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
7017
static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
6992
{
7018
{
6993
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7019
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6994
 
7020
 
6995
	intel_crtc->cursor_x = x;
7021
	intel_crtc->cursor_x = x;
6996
	intel_crtc->cursor_y = y;
7022
	intel_crtc->cursor_y = y;
6997
 
7023
 
6998
	if (intel_crtc->active)
7024
	if (intel_crtc->active)
6999
		intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
7025
		intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
7000
 
7026
 
7001
	return 0;
7027
	return 0;
7002
}
7028
}
7003
#endif
7029
#endif
7004
 
7030
 
7005
/** Sets the color ramps on behalf of RandR */
7031
/** Sets the color ramps on behalf of RandR */
7006
void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
7032
void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
7007
				 u16 blue, int regno)
7033
				 u16 blue, int regno)
7008
{
7034
{
7009
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7035
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7010
 
7036
 
7011
	intel_crtc->lut_r[regno] = red >> 8;
7037
	intel_crtc->lut_r[regno] = red >> 8;
7012
	intel_crtc->lut_g[regno] = green >> 8;
7038
	intel_crtc->lut_g[regno] = green >> 8;
7013
	intel_crtc->lut_b[regno] = blue >> 8;
7039
	intel_crtc->lut_b[regno] = blue >> 8;
7014
}
7040
}
7015
 
7041
 
7016
void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
7042
void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
7017
			     u16 *blue, int regno)
7043
			     u16 *blue, int regno)
7018
{
7044
{
7019
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7045
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7020
 
7046
 
7021
	*red = intel_crtc->lut_r[regno] << 8;
7047
	*red = intel_crtc->lut_r[regno] << 8;
7022
	*green = intel_crtc->lut_g[regno] << 8;
7048
	*green = intel_crtc->lut_g[regno] << 8;
7023
	*blue = intel_crtc->lut_b[regno] << 8;
7049
	*blue = intel_crtc->lut_b[regno] << 8;
7024
}
7050
}
7025
 
7051
 
7026
static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
7052
static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
7027
				 u16 *blue, uint32_t start, uint32_t size)
7053
				 u16 *blue, uint32_t start, uint32_t size)
7028
{
7054
{
7029
	int end = (start + size > 256) ? 256 : start + size, i;
7055
	int end = (start + size > 256) ? 256 : start + size, i;
7030
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7056
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7031
 
7057
 
7032
	for (i = start; i < end; i++) {
7058
	for (i = start; i < end; i++) {
7033
		intel_crtc->lut_r[i] = red[i] >> 8;
7059
		intel_crtc->lut_r[i] = red[i] >> 8;
7034
		intel_crtc->lut_g[i] = green[i] >> 8;
7060
		intel_crtc->lut_g[i] = green[i] >> 8;
7035
		intel_crtc->lut_b[i] = blue[i] >> 8;
7061
		intel_crtc->lut_b[i] = blue[i] >> 8;
7036
	}
7062
	}
7037
 
7063
 
7038
	intel_crtc_load_lut(crtc);
7064
	intel_crtc_load_lut(crtc);
7039
}
7065
}
7040
 
7066
 
7041
/* VESA 640x480x72Hz mode to set on the pipe */
7067
/* VESA 640x480x72Hz mode to set on the pipe */
7042
static struct drm_display_mode load_detect_mode = {
7068
static struct drm_display_mode load_detect_mode = {
7043
	DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
7069
	DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
7044
		 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
7070
		 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
7045
};
7071
};
7046
 
7072
 
7047
static struct drm_framebuffer *
7073
static struct drm_framebuffer *
7048
intel_framebuffer_create(struct drm_device *dev,
7074
intel_framebuffer_create(struct drm_device *dev,
7049
			 struct drm_mode_fb_cmd2 *mode_cmd,
7075
			 struct drm_mode_fb_cmd2 *mode_cmd,
7050
			 struct drm_i915_gem_object *obj)
7076
			 struct drm_i915_gem_object *obj)
7051
{
7077
{
7052
	struct intel_framebuffer *intel_fb;
7078
	struct intel_framebuffer *intel_fb;
7053
	int ret;
7079
	int ret;
7054
 
7080
 
7055
	intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
7081
	intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
7056
	if (!intel_fb) {
7082
	if (!intel_fb) {
7057
		drm_gem_object_unreference_unlocked(&obj->base);
7083
		drm_gem_object_unreference_unlocked(&obj->base);
7058
		return ERR_PTR(-ENOMEM);
7084
		return ERR_PTR(-ENOMEM);
7059
	}
7085
	}
7060
 
7086
 
7061
	ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
7087
	ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
7062
	if (ret) {
7088
	if (ret) {
7063
		drm_gem_object_unreference_unlocked(&obj->base);
7089
		drm_gem_object_unreference_unlocked(&obj->base);
7064
		kfree(intel_fb);
7090
		kfree(intel_fb);
7065
		return ERR_PTR(ret);
7091
		return ERR_PTR(ret);
7066
	}
7092
	}
7067
 
7093
 
7068
	return &intel_fb->base;
7094
	return &intel_fb->base;
7069
}
7095
}
7070
 
7096
 
7071
static u32
7097
static u32
7072
intel_framebuffer_pitch_for_width(int width, int bpp)
7098
intel_framebuffer_pitch_for_width(int width, int bpp)
7073
{
7099
{
7074
	u32 pitch = DIV_ROUND_UP(width * bpp, 8);
7100
	u32 pitch = DIV_ROUND_UP(width * bpp, 8);
7075
	return ALIGN(pitch, 64);
7101
	return ALIGN(pitch, 64);
7076
}
7102
}
7077
 
7103
 
7078
static u32
7104
static u32
7079
intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
7105
intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
7080
{
7106
{
7081
	u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
7107
	u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
7082
	return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
7108
	return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
7083
}
7109
}
7084
 
7110
 
7085
static struct drm_framebuffer *
7111
static struct drm_framebuffer *
7086
intel_framebuffer_create_for_mode(struct drm_device *dev,
7112
intel_framebuffer_create_for_mode(struct drm_device *dev,
7087
				  struct drm_display_mode *mode,
7113
				  struct drm_display_mode *mode,
7088
				  int depth, int bpp)
7114
				  int depth, int bpp)
7089
{
7115
{
7090
	struct drm_i915_gem_object *obj;
7116
	struct drm_i915_gem_object *obj;
7091
	struct drm_mode_fb_cmd2 mode_cmd = { 0 };
7117
	struct drm_mode_fb_cmd2 mode_cmd = { 0 };
7092
 
7118
 
7093
	return NULL;
7119
	return NULL;
7094
}
7120
}
7095
 
7121
 
7096
static struct drm_framebuffer *
7122
static struct drm_framebuffer *
7097
mode_fits_in_fbdev(struct drm_device *dev,
7123
mode_fits_in_fbdev(struct drm_device *dev,
7098
		   struct drm_display_mode *mode)
7124
		   struct drm_display_mode *mode)
7099
{
7125
{
7100
	struct drm_i915_private *dev_priv = dev->dev_private;
7126
	struct drm_i915_private *dev_priv = dev->dev_private;
7101
	struct drm_i915_gem_object *obj;
7127
	struct drm_i915_gem_object *obj;
7102
	struct drm_framebuffer *fb;
7128
	struct drm_framebuffer *fb;
7103
 
7129
 
7104
	if (dev_priv->fbdev == NULL)
7130
	if (dev_priv->fbdev == NULL)
7105
		return NULL;
7131
		return NULL;
7106
 
7132
 
7107
	obj = dev_priv->fbdev->ifb.obj;
7133
	obj = dev_priv->fbdev->ifb.obj;
7108
	if (obj == NULL)
7134
	if (obj == NULL)
7109
		return NULL;
7135
		return NULL;
7110
 
7136
 
7111
	fb = &dev_priv->fbdev->ifb.base;
7137
	fb = &dev_priv->fbdev->ifb.base;
7112
	if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
7138
	if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
7113
							       fb->bits_per_pixel))
7139
							       fb->bits_per_pixel))
7114
		return NULL;
7140
		return NULL;
7115
 
7141
 
7116
	if (obj->base.size < mode->vdisplay * fb->pitches[0])
7142
	if (obj->base.size < mode->vdisplay * fb->pitches[0])
7117
		return NULL;
7143
		return NULL;
7118
 
7144
 
7119
	return fb;
7145
	return fb;
7120
}
7146
}
7121
 
7147
 
7122
bool intel_get_load_detect_pipe(struct drm_connector *connector,
7148
bool intel_get_load_detect_pipe(struct drm_connector *connector,
7123
				struct drm_display_mode *mode,
7149
				struct drm_display_mode *mode,
7124
				struct intel_load_detect_pipe *old)
7150
				struct intel_load_detect_pipe *old)
7125
{
7151
{
7126
	struct intel_crtc *intel_crtc;
7152
	struct intel_crtc *intel_crtc;
7127
	struct intel_encoder *intel_encoder =
7153
	struct intel_encoder *intel_encoder =
7128
		intel_attached_encoder(connector);
7154
		intel_attached_encoder(connector);
7129
	struct drm_crtc *possible_crtc;
7155
	struct drm_crtc *possible_crtc;
7130
	struct drm_encoder *encoder = &intel_encoder->base;
7156
	struct drm_encoder *encoder = &intel_encoder->base;
7131
	struct drm_crtc *crtc = NULL;
7157
	struct drm_crtc *crtc = NULL;
7132
	struct drm_device *dev = encoder->dev;
7158
	struct drm_device *dev = encoder->dev;
7133
	struct drm_framebuffer *fb;
7159
	struct drm_framebuffer *fb;
7134
	int i = -1;
7160
	int i = -1;
7135
 
7161
 
7136
	DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7162
	DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7137
		      connector->base.id, drm_get_connector_name(connector),
7163
		      connector->base.id, drm_get_connector_name(connector),
7138
		      encoder->base.id, drm_get_encoder_name(encoder));
7164
		      encoder->base.id, drm_get_encoder_name(encoder));
7139
 
7165
 
7140
	/*
7166
	/*
7141
	 * Algorithm gets a little messy:
7167
	 * Algorithm gets a little messy:
7142
	 *
7168
	 *
7143
	 *   - if the connector already has an assigned crtc, use it (but make
7169
	 *   - if the connector already has an assigned crtc, use it (but make
7144
	 *     sure it's on first)
7170
	 *     sure it's on first)
7145
	 *
7171
	 *
7146
	 *   - try to find the first unused crtc that can drive this connector,
7172
	 *   - try to find the first unused crtc that can drive this connector,
7147
	 *     and use that if we find one
7173
	 *     and use that if we find one
7148
	 */
7174
	 */
7149
 
7175
 
7150
	/* See if we already have a CRTC for this connector */
7176
	/* See if we already have a CRTC for this connector */
7151
	if (encoder->crtc) {
7177
	if (encoder->crtc) {
7152
		crtc = encoder->crtc;
7178
		crtc = encoder->crtc;
7153
 
7179
 
7154
		mutex_lock(&crtc->mutex);
7180
		mutex_lock(&crtc->mutex);
7155
 
7181
 
7156
		old->dpms_mode = connector->dpms;
7182
		old->dpms_mode = connector->dpms;
7157
		old->load_detect_temp = false;
7183
		old->load_detect_temp = false;
7158
 
7184
 
7159
		/* Make sure the crtc and connector are running */
7185
		/* Make sure the crtc and connector are running */
7160
		if (connector->dpms != DRM_MODE_DPMS_ON)
7186
		if (connector->dpms != DRM_MODE_DPMS_ON)
7161
			connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
7187
			connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
7162
 
7188
 
7163
		return true;
7189
		return true;
7164
	}
7190
	}
7165
 
7191
 
7166
	/* Find an unused one (if possible) */
7192
	/* Find an unused one (if possible) */
7167
	list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
7193
	list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
7168
		i++;
7194
		i++;
7169
		if (!(encoder->possible_crtcs & (1 << i)))
7195
		if (!(encoder->possible_crtcs & (1 << i)))
7170
			continue;
7196
			continue;
7171
		if (!possible_crtc->enabled) {
7197
		if (!possible_crtc->enabled) {
7172
			crtc = possible_crtc;
7198
			crtc = possible_crtc;
7173
			break;
7199
			break;
7174
		}
7200
		}
7175
	}
7201
	}
7176
 
7202
 
7177
	/*
7203
	/*
7178
	 * If we didn't find an unused CRTC, don't use any.
7204
	 * If we didn't find an unused CRTC, don't use any.
7179
	 */
7205
	 */
7180
	if (!crtc) {
7206
	if (!crtc) {
7181
		DRM_DEBUG_KMS("no pipe available for load-detect\n");
7207
		DRM_DEBUG_KMS("no pipe available for load-detect\n");
7182
		return false;
7208
		return false;
7183
	}
7209
	}
7184
 
7210
 
7185
	mutex_lock(&crtc->mutex);
7211
	mutex_lock(&crtc->mutex);
7186
	intel_encoder->new_crtc = to_intel_crtc(crtc);
7212
	intel_encoder->new_crtc = to_intel_crtc(crtc);
7187
	to_intel_connector(connector)->new_encoder = intel_encoder;
7213
	to_intel_connector(connector)->new_encoder = intel_encoder;
7188
 
7214
 
7189
	intel_crtc = to_intel_crtc(crtc);
7215
	intel_crtc = to_intel_crtc(crtc);
7190
	old->dpms_mode = connector->dpms;
7216
	old->dpms_mode = connector->dpms;
7191
	old->load_detect_temp = true;
7217
	old->load_detect_temp = true;
7192
	old->release_fb = NULL;
7218
	old->release_fb = NULL;
7193
 
7219
 
7194
	if (!mode)
7220
	if (!mode)
7195
		mode = &load_detect_mode;
7221
		mode = &load_detect_mode;
7196
 
7222
 
7197
	/* We need a framebuffer large enough to accommodate all accesses
7223
	/* We need a framebuffer large enough to accommodate all accesses
7198
	 * that the plane may generate whilst we perform load detection.
7224
	 * that the plane may generate whilst we perform load detection.
7199
	 * We can not rely on the fbcon either being present (we get called
7225
	 * We can not rely on the fbcon either being present (we get called
7200
	 * during its initialisation to detect all boot displays, or it may
7226
	 * during its initialisation to detect all boot displays, or it may
7201
	 * not even exist) or that it is large enough to satisfy the
7227
	 * not even exist) or that it is large enough to satisfy the
7202
	 * requested mode.
7228
	 * requested mode.
7203
	 */
7229
	 */
7204
	fb = mode_fits_in_fbdev(dev, mode);
7230
	fb = mode_fits_in_fbdev(dev, mode);
7205
	if (fb == NULL) {
7231
	if (fb == NULL) {
7206
		DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
7232
		DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
7207
		fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
7233
		fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
7208
		old->release_fb = fb;
7234
		old->release_fb = fb;
7209
	} else
7235
	} else
7210
		DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
7236
		DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
7211
	if (IS_ERR(fb)) {
7237
	if (IS_ERR(fb)) {
7212
		DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
7238
		DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
7213
		mutex_unlock(&crtc->mutex);
7239
		mutex_unlock(&crtc->mutex);
7214
		return false;
7240
		return false;
7215
	}
7241
	}
7216
 
7242
 
7217
	if (intel_set_mode(crtc, mode, 0, 0, fb)) {
7243
	if (intel_set_mode(crtc, mode, 0, 0, fb)) {
7218
		DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
7244
		DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
7219
		if (old->release_fb)
7245
		if (old->release_fb)
7220
			old->release_fb->funcs->destroy(old->release_fb);
7246
			old->release_fb->funcs->destroy(old->release_fb);
7221
		mutex_unlock(&crtc->mutex);
7247
		mutex_unlock(&crtc->mutex);
7222
		return false;
7248
		return false;
7223
	}
7249
	}
7224
 
7250
 
7225
	/* let the connector get through one full cycle before testing */
7251
	/* let the connector get through one full cycle before testing */
7226
	intel_wait_for_vblank(dev, intel_crtc->pipe);
7252
	intel_wait_for_vblank(dev, intel_crtc->pipe);
7227
	return true;
7253
	return true;
7228
}
7254
}
7229
 
7255
 
7230
void intel_release_load_detect_pipe(struct drm_connector *connector,
7256
void intel_release_load_detect_pipe(struct drm_connector *connector,
7231
				    struct intel_load_detect_pipe *old)
7257
				    struct intel_load_detect_pipe *old)
7232
{
7258
{
7233
	struct intel_encoder *intel_encoder =
7259
	struct intel_encoder *intel_encoder =
7234
		intel_attached_encoder(connector);
7260
		intel_attached_encoder(connector);
7235
	struct drm_encoder *encoder = &intel_encoder->base;
7261
	struct drm_encoder *encoder = &intel_encoder->base;
7236
	struct drm_crtc *crtc = encoder->crtc;
7262
	struct drm_crtc *crtc = encoder->crtc;
7237
 
7263
 
7238
	DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7264
	DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7239
		      connector->base.id, drm_get_connector_name(connector),
7265
		      connector->base.id, drm_get_connector_name(connector),
7240
		      encoder->base.id, drm_get_encoder_name(encoder));
7266
		      encoder->base.id, drm_get_encoder_name(encoder));
7241
 
7267
 
7242
	if (old->load_detect_temp) {
7268
	if (old->load_detect_temp) {
7243
		to_intel_connector(connector)->new_encoder = NULL;
7269
		to_intel_connector(connector)->new_encoder = NULL;
7244
		intel_encoder->new_crtc = NULL;
7270
		intel_encoder->new_crtc = NULL;
7245
		intel_set_mode(crtc, NULL, 0, 0, NULL);
7271
		intel_set_mode(crtc, NULL, 0, 0, NULL);
7246
 
7272
 
7247
		if (old->release_fb) {
7273
		if (old->release_fb) {
7248
			drm_framebuffer_unregister_private(old->release_fb);
7274
			drm_framebuffer_unregister_private(old->release_fb);
7249
			drm_framebuffer_unreference(old->release_fb);
7275
			drm_framebuffer_unreference(old->release_fb);
7250
		}
7276
		}
7251
 
7277
 
7252
		mutex_unlock(&crtc->mutex);
7278
		mutex_unlock(&crtc->mutex);
7253
		return;
7279
		return;
7254
	}
7280
	}
7255
 
7281
 
7256
	/* Switch crtc and encoder back off if necessary */
7282
	/* Switch crtc and encoder back off if necessary */
7257
	if (old->dpms_mode != DRM_MODE_DPMS_ON)
7283
	if (old->dpms_mode != DRM_MODE_DPMS_ON)
7258
		connector->funcs->dpms(connector, old->dpms_mode);
7284
		connector->funcs->dpms(connector, old->dpms_mode);
7259
 
7285
 
7260
	mutex_unlock(&crtc->mutex);
7286
	mutex_unlock(&crtc->mutex);
7261
}
7287
}
7262
 
7288
 
7263
/* Returns the clock of the currently programmed mode of the given pipe. */
7289
/* Returns the clock of the currently programmed mode of the given pipe. */
7264
static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
7290
static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
7265
				struct intel_crtc_config *pipe_config)
7291
				struct intel_crtc_config *pipe_config)
7266
{
7292
{
7267
	struct drm_device *dev = crtc->base.dev;
7293
	struct drm_device *dev = crtc->base.dev;
7268
	struct drm_i915_private *dev_priv = dev->dev_private;
7294
	struct drm_i915_private *dev_priv = dev->dev_private;
7269
	int pipe = pipe_config->cpu_transcoder;
7295
	int pipe = pipe_config->cpu_transcoder;
7270
	u32 dpll = I915_READ(DPLL(pipe));
7296
	u32 dpll = I915_READ(DPLL(pipe));
7271
	u32 fp;
7297
	u32 fp;
7272
	intel_clock_t clock;
7298
	intel_clock_t clock;
7273
 
7299
 
7274
	if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
7300
	if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
7275
		fp = I915_READ(FP0(pipe));
7301
		fp = I915_READ(FP0(pipe));
7276
	else
7302
	else
7277
		fp = I915_READ(FP1(pipe));
7303
		fp = I915_READ(FP1(pipe));
7278
 
7304
 
7279
	clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
7305
	clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
7280
	if (IS_PINEVIEW(dev)) {
7306
	if (IS_PINEVIEW(dev)) {
7281
		clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
7307
		clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
7282
		clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
7308
		clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
7283
	} else {
7309
	} else {
7284
		clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
7310
		clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
7285
		clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
7311
		clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
7286
	}
7312
	}
7287
 
7313
 
7288
	if (!IS_GEN2(dev)) {
7314
	if (!IS_GEN2(dev)) {
7289
		if (IS_PINEVIEW(dev))
7315
		if (IS_PINEVIEW(dev))
7290
			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
7316
			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
7291
				DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
7317
				DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
7292
		else
7318
		else
7293
			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
7319
			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
7294
			       DPLL_FPA01_P1_POST_DIV_SHIFT);
7320
			       DPLL_FPA01_P1_POST_DIV_SHIFT);
7295
 
7321
 
7296
		switch (dpll & DPLL_MODE_MASK) {
7322
		switch (dpll & DPLL_MODE_MASK) {
7297
		case DPLLB_MODE_DAC_SERIAL:
7323
		case DPLLB_MODE_DAC_SERIAL:
7298
			clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
7324
			clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
7299
				5 : 10;
7325
				5 : 10;
7300
			break;
7326
			break;
7301
		case DPLLB_MODE_LVDS:
7327
		case DPLLB_MODE_LVDS:
7302
			clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
7328
			clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
7303
				7 : 14;
7329
				7 : 14;
7304
			break;
7330
			break;
7305
		default:
7331
		default:
7306
			DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
7332
			DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
7307
				  "mode\n", (int)(dpll & DPLL_MODE_MASK));
7333
				  "mode\n", (int)(dpll & DPLL_MODE_MASK));
7308
			pipe_config->adjusted_mode.clock = 0;
7334
			pipe_config->adjusted_mode.clock = 0;
7309
			return;
7335
			return;
7310
		}
7336
		}
7311
 
7337
 
7312
		if (IS_PINEVIEW(dev))
7338
		if (IS_PINEVIEW(dev))
7313
			pineview_clock(96000, &clock);
7339
			pineview_clock(96000, &clock);
7314
		else
7340
		else
7315
			i9xx_clock(96000, &clock);
7341
			i9xx_clock(96000, &clock);
7316
	} else {
7342
	} else {
7317
		bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
7343
		bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
7318
 
7344
 
7319
		if (is_lvds) {
7345
		if (is_lvds) {
7320
			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
7346
			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
7321
				       DPLL_FPA01_P1_POST_DIV_SHIFT);
7347
				       DPLL_FPA01_P1_POST_DIV_SHIFT);
7322
			clock.p2 = 14;
7348
			clock.p2 = 14;
7323
 
7349
 
7324
			if ((dpll & PLL_REF_INPUT_MASK) ==
7350
			if ((dpll & PLL_REF_INPUT_MASK) ==
7325
			    PLLB_REF_INPUT_SPREADSPECTRUMIN) {
7351
			    PLLB_REF_INPUT_SPREADSPECTRUMIN) {
7326
				/* XXX: might not be 66MHz */
7352
				/* XXX: might not be 66MHz */
7327
				i9xx_clock(66000, &clock);
7353
				i9xx_clock(66000, &clock);
7328
			} else
7354
			} else
7329
				i9xx_clock(48000, &clock);
7355
				i9xx_clock(48000, &clock);
7330
		} else {
7356
		} else {
7331
			if (dpll & PLL_P1_DIVIDE_BY_TWO)
7357
			if (dpll & PLL_P1_DIVIDE_BY_TWO)
7332
				clock.p1 = 2;
7358
				clock.p1 = 2;
7333
			else {
7359
			else {
7334
				clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
7360
				clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
7335
					    DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
7361
					    DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
7336
			}
7362
			}
7337
			if (dpll & PLL_P2_DIVIDE_BY_4)
7363
			if (dpll & PLL_P2_DIVIDE_BY_4)
7338
				clock.p2 = 4;
7364
				clock.p2 = 4;
7339
			else
7365
			else
7340
				clock.p2 = 2;
7366
				clock.p2 = 2;
7341
 
7367
 
7342
			i9xx_clock(48000, &clock);
7368
			i9xx_clock(48000, &clock);
7343
		}
7369
		}
7344
	}
7370
	}
7345
 
7371
 
7346
	pipe_config->adjusted_mode.clock = clock.dot;
7372
	pipe_config->adjusted_mode.clock = clock.dot;
7347
}
7373
}
7348
 
7374
 
7349
static void ironlake_crtc_clock_get(struct intel_crtc *crtc,
7375
static void ironlake_crtc_clock_get(struct intel_crtc *crtc,
7350
				    struct intel_crtc_config *pipe_config)
7376
				    struct intel_crtc_config *pipe_config)
7351
{
7377
{
7352
	struct drm_device *dev = crtc->base.dev;
7378
	struct drm_device *dev = crtc->base.dev;
7353
	struct drm_i915_private *dev_priv = dev->dev_private;
7379
	struct drm_i915_private *dev_priv = dev->dev_private;
7354
	enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7380
	enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7355
	int link_freq, repeat;
7381
	int link_freq, repeat;
7356
	u64 clock;
7382
	u64 clock;
7357
	u32 link_m, link_n;
7383
	u32 link_m, link_n;
7358
 
7384
 
7359
	repeat = pipe_config->pixel_multiplier;
7385
	repeat = pipe_config->pixel_multiplier;
7360
 
7386
 
7361
	/*
7387
	/*
7362
	 * The calculation for the data clock is:
7388
	 * The calculation for the data clock is:
7363
	 * pixel_clock = ((m/n)*(link_clock * nr_lanes * repeat))/bpp
7389
	 * pixel_clock = ((m/n)*(link_clock * nr_lanes * repeat))/bpp
7364
	 * But we want to avoid losing precison if possible, so:
7390
	 * But we want to avoid losing precison if possible, so:
7365
	 * pixel_clock = ((m * link_clock * nr_lanes * repeat)/(n*bpp))
7391
	 * pixel_clock = ((m * link_clock * nr_lanes * repeat)/(n*bpp))
7366
	 *
7392
	 *
7367
	 * and the link clock is simpler:
7393
	 * and the link clock is simpler:
7368
	 * link_clock = (m * link_clock * repeat) / n
7394
	 * link_clock = (m * link_clock * repeat) / n
7369
	 */
7395
	 */
7370
 
7396
 
7371
	/*
7397
	/*
7372
	 * We need to get the FDI or DP link clock here to derive
7398
	 * We need to get the FDI or DP link clock here to derive
7373
	 * the M/N dividers.
7399
	 * the M/N dividers.
7374
	 *
7400
	 *
7375
	 * For FDI, we read it from the BIOS or use a fixed 2.7GHz.
7401
	 * For FDI, we read it from the BIOS or use a fixed 2.7GHz.
7376
	 * For DP, it's either 1.62GHz or 2.7GHz.
7402
	 * For DP, it's either 1.62GHz or 2.7GHz.
7377
	 * We do our calculations in 10*MHz since we don't need much precison.
7403
	 * We do our calculations in 10*MHz since we don't need much precison.
7378
	 */
7404
	 */
7379
	if (pipe_config->has_pch_encoder)
7405
	if (pipe_config->has_pch_encoder)
7380
		link_freq = intel_fdi_link_freq(dev) * 10000;
7406
		link_freq = intel_fdi_link_freq(dev) * 10000;
7381
	else
7407
	else
7382
		link_freq = pipe_config->port_clock;
7408
		link_freq = pipe_config->port_clock;
7383
 
7409
 
7384
	link_m = I915_READ(PIPE_LINK_M1(cpu_transcoder));
7410
	link_m = I915_READ(PIPE_LINK_M1(cpu_transcoder));
7385
	link_n = I915_READ(PIPE_LINK_N1(cpu_transcoder));
7411
	link_n = I915_READ(PIPE_LINK_N1(cpu_transcoder));
7386
 
7412
 
7387
	if (!link_m || !link_n)
7413
	if (!link_m || !link_n)
7388
		return;
7414
		return;
7389
 
7415
 
7390
	clock = ((u64)link_m * (u64)link_freq * (u64)repeat);
7416
	clock = ((u64)link_m * (u64)link_freq * (u64)repeat);
7391
	do_div(clock, link_n);
7417
	do_div(clock, link_n);
7392
 
7418
 
7393
	pipe_config->adjusted_mode.clock = clock;
7419
	pipe_config->adjusted_mode.clock = clock;
7394
}
7420
}
7395
 
7421
 
7396
/** Returns the currently programmed mode of the given pipe. */
7422
/** Returns the currently programmed mode of the given pipe. */
7397
struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
7423
struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
7398
					     struct drm_crtc *crtc)
7424
					     struct drm_crtc *crtc)
7399
{
7425
{
7400
	struct drm_i915_private *dev_priv = dev->dev_private;
7426
	struct drm_i915_private *dev_priv = dev->dev_private;
7401
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7427
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7402
	enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
7428
	enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
7403
	struct drm_display_mode *mode;
7429
	struct drm_display_mode *mode;
7404
	struct intel_crtc_config pipe_config;
7430
	struct intel_crtc_config pipe_config;
7405
	int htot = I915_READ(HTOTAL(cpu_transcoder));
7431
	int htot = I915_READ(HTOTAL(cpu_transcoder));
7406
	int hsync = I915_READ(HSYNC(cpu_transcoder));
7432
	int hsync = I915_READ(HSYNC(cpu_transcoder));
7407
	int vtot = I915_READ(VTOTAL(cpu_transcoder));
7433
	int vtot = I915_READ(VTOTAL(cpu_transcoder));
7408
	int vsync = I915_READ(VSYNC(cpu_transcoder));
7434
	int vsync = I915_READ(VSYNC(cpu_transcoder));
7409
 
7435
 
7410
	mode = kzalloc(sizeof(*mode), GFP_KERNEL);
7436
	mode = kzalloc(sizeof(*mode), GFP_KERNEL);
7411
	if (!mode)
7437
	if (!mode)
7412
		return NULL;
7438
		return NULL;
7413
 
7439
 
7414
	/*
7440
	/*
7415
	 * Construct a pipe_config sufficient for getting the clock info
7441
	 * Construct a pipe_config sufficient for getting the clock info
7416
	 * back out of crtc_clock_get.
7442
	 * back out of crtc_clock_get.
7417
	 *
7443
	 *
7418
	 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
7444
	 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
7419
	 * to use a real value here instead.
7445
	 * to use a real value here instead.
7420
	 */
7446
	 */
7421
	pipe_config.cpu_transcoder = (enum transcoder) intel_crtc->pipe;
7447
	pipe_config.cpu_transcoder = (enum transcoder) intel_crtc->pipe;
7422
	pipe_config.pixel_multiplier = 1;
7448
	pipe_config.pixel_multiplier = 1;
7423
	i9xx_crtc_clock_get(intel_crtc, &pipe_config);
7449
	i9xx_crtc_clock_get(intel_crtc, &pipe_config);
7424
 
7450
 
7425
	mode->clock = pipe_config.adjusted_mode.clock;
7451
	mode->clock = pipe_config.adjusted_mode.clock;
7426
	mode->hdisplay = (htot & 0xffff) + 1;
7452
	mode->hdisplay = (htot & 0xffff) + 1;
7427
	mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
7453
	mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
7428
	mode->hsync_start = (hsync & 0xffff) + 1;
7454
	mode->hsync_start = (hsync & 0xffff) + 1;
7429
	mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
7455
	mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
7430
	mode->vdisplay = (vtot & 0xffff) + 1;
7456
	mode->vdisplay = (vtot & 0xffff) + 1;
7431
	mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
7457
	mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
7432
	mode->vsync_start = (vsync & 0xffff) + 1;
7458
	mode->vsync_start = (vsync & 0xffff) + 1;
7433
	mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
7459
	mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
7434
 
7460
 
7435
	drm_mode_set_name(mode);
7461
	drm_mode_set_name(mode);
7436
 
7462
 
7437
	return mode;
7463
	return mode;
7438
}
7464
}
7439
 
7465
 
7440
static void intel_increase_pllclock(struct drm_crtc *crtc)
7466
static void intel_increase_pllclock(struct drm_crtc *crtc)
7441
{
7467
{
7442
	struct drm_device *dev = crtc->dev;
7468
	struct drm_device *dev = crtc->dev;
7443
	drm_i915_private_t *dev_priv = dev->dev_private;
7469
	drm_i915_private_t *dev_priv = dev->dev_private;
7444
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7470
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7445
	int pipe = intel_crtc->pipe;
7471
	int pipe = intel_crtc->pipe;
7446
	int dpll_reg = DPLL(pipe);
7472
	int dpll_reg = DPLL(pipe);
7447
	int dpll;
7473
	int dpll;
7448
 
7474
 
7449
	if (HAS_PCH_SPLIT(dev))
7475
	if (HAS_PCH_SPLIT(dev))
7450
		return;
7476
		return;
7451
 
7477
 
7452
	if (!dev_priv->lvds_downclock_avail)
7478
	if (!dev_priv->lvds_downclock_avail)
7453
		return;
7479
		return;
7454
 
7480
 
7455
	dpll = I915_READ(dpll_reg);
7481
	dpll = I915_READ(dpll_reg);
7456
	if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
7482
	if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
7457
		DRM_DEBUG_DRIVER("upclocking LVDS\n");
7483
		DRM_DEBUG_DRIVER("upclocking LVDS\n");
7458
 
7484
 
7459
		assert_panel_unlocked(dev_priv, pipe);
7485
		assert_panel_unlocked(dev_priv, pipe);
7460
 
7486
 
7461
		dpll &= ~DISPLAY_RATE_SELECT_FPA1;
7487
		dpll &= ~DISPLAY_RATE_SELECT_FPA1;
7462
		I915_WRITE(dpll_reg, dpll);
7488
		I915_WRITE(dpll_reg, dpll);
7463
		intel_wait_for_vblank(dev, pipe);
7489
		intel_wait_for_vblank(dev, pipe);
7464
 
7490
 
7465
		dpll = I915_READ(dpll_reg);
7491
		dpll = I915_READ(dpll_reg);
7466
		if (dpll & DISPLAY_RATE_SELECT_FPA1)
7492
		if (dpll & DISPLAY_RATE_SELECT_FPA1)
7467
			DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
7493
			DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
7468
	}
7494
	}
7469
}
7495
}
7470
 
7496
 
7471
static void intel_decrease_pllclock(struct drm_crtc *crtc)
7497
static void intel_decrease_pllclock(struct drm_crtc *crtc)
7472
{
7498
{
7473
	struct drm_device *dev = crtc->dev;
7499
	struct drm_device *dev = crtc->dev;
7474
	drm_i915_private_t *dev_priv = dev->dev_private;
7500
	drm_i915_private_t *dev_priv = dev->dev_private;
7475
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7501
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7476
 
7502
 
7477
	if (HAS_PCH_SPLIT(dev))
7503
	if (HAS_PCH_SPLIT(dev))
7478
		return;
7504
		return;
7479
 
7505
 
7480
	if (!dev_priv->lvds_downclock_avail)
7506
	if (!dev_priv->lvds_downclock_avail)
7481
		return;
7507
		return;
7482
 
7508
 
7483
	/*
7509
	/*
7484
	 * Since this is called by a timer, we should never get here in
7510
	 * Since this is called by a timer, we should never get here in
7485
	 * the manual case.
7511
	 * the manual case.
7486
	 */
7512
	 */
7487
	if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
7513
	if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
7488
		int pipe = intel_crtc->pipe;
7514
		int pipe = intel_crtc->pipe;
7489
		int dpll_reg = DPLL(pipe);
7515
		int dpll_reg = DPLL(pipe);
7490
		int dpll;
7516
		int dpll;
7491
 
7517
 
7492
		DRM_DEBUG_DRIVER("downclocking LVDS\n");
7518
		DRM_DEBUG_DRIVER("downclocking LVDS\n");
7493
 
7519
 
7494
		assert_panel_unlocked(dev_priv, pipe);
7520
		assert_panel_unlocked(dev_priv, pipe);
7495
 
7521
 
7496
		dpll = I915_READ(dpll_reg);
7522
		dpll = I915_READ(dpll_reg);
7497
		dpll |= DISPLAY_RATE_SELECT_FPA1;
7523
		dpll |= DISPLAY_RATE_SELECT_FPA1;
7498
		I915_WRITE(dpll_reg, dpll);
7524
		I915_WRITE(dpll_reg, dpll);
7499
		intel_wait_for_vblank(dev, pipe);
7525
		intel_wait_for_vblank(dev, pipe);
7500
		dpll = I915_READ(dpll_reg);
7526
		dpll = I915_READ(dpll_reg);
7501
		if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
7527
		if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
7502
			DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
7528
			DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
7503
	}
7529
	}
7504
 
7530
 
7505
}
7531
}
7506
 
7532
 
7507
void intel_mark_busy(struct drm_device *dev)
7533
void intel_mark_busy(struct drm_device *dev)
7508
{
7534
{
7509
	struct drm_i915_private *dev_priv = dev->dev_private;
7535
	struct drm_i915_private *dev_priv = dev->dev_private;
7510
 
7536
 
7511
	hsw_package_c8_gpu_busy(dev_priv);
7537
	hsw_package_c8_gpu_busy(dev_priv);
7512
	i915_update_gfx_val(dev_priv);
7538
	i915_update_gfx_val(dev_priv);
7513
}
7539
}
7514
 
7540
 
7515
void intel_mark_idle(struct drm_device *dev)
7541
void intel_mark_idle(struct drm_device *dev)
7516
{
7542
{
7517
	struct drm_i915_private *dev_priv = dev->dev_private;
7543
	struct drm_i915_private *dev_priv = dev->dev_private;
7518
	struct drm_crtc *crtc;
7544
	struct drm_crtc *crtc;
7519
 
7545
 
7520
	hsw_package_c8_gpu_idle(dev_priv);
7546
	hsw_package_c8_gpu_idle(dev_priv);
7521
 
7547
 
7522
	if (!i915_powersave)
7548
	if (!i915_powersave)
7523
		return;
7549
		return;
7524
 
7550
 
7525
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
7551
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
7526
		if (!crtc->fb)
7552
		if (!crtc->fb)
7527
			continue;
7553
			continue;
7528
 
7554
 
7529
		intel_decrease_pllclock(crtc);
7555
		intel_decrease_pllclock(crtc);
7530
	}
7556
	}
7531
}
7557
}
7532
 
7558
 
7533
void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
7559
void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
7534
			struct intel_ring_buffer *ring)
7560
			struct intel_ring_buffer *ring)
7535
{
7561
{
7536
	struct drm_device *dev = obj->base.dev;
7562
	struct drm_device *dev = obj->base.dev;
7537
	struct drm_crtc *crtc;
7563
	struct drm_crtc *crtc;
7538
 
7564
 
7539
	if (!i915_powersave)
7565
	if (!i915_powersave)
7540
		return;
7566
		return;
7541
 
7567
 
7542
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
7568
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
7543
		if (!crtc->fb)
7569
		if (!crtc->fb)
7544
			continue;
7570
			continue;
7545
 
7571
 
7546
		if (to_intel_framebuffer(crtc->fb)->obj != obj)
7572
		if (to_intel_framebuffer(crtc->fb)->obj != obj)
7547
			continue;
7573
			continue;
7548
 
7574
 
7549
			intel_increase_pllclock(crtc);
7575
			intel_increase_pllclock(crtc);
7550
		if (ring && intel_fbc_enabled(dev))
7576
		if (ring && intel_fbc_enabled(dev))
7551
			ring->fbc_dirty = true;
7577
			ring->fbc_dirty = true;
7552
	}
7578
	}
7553
}
7579
}
7554
 
7580
 
7555
static void intel_crtc_destroy(struct drm_crtc *crtc)
7581
static void intel_crtc_destroy(struct drm_crtc *crtc)
7556
{
7582
{
7557
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7583
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7558
	struct drm_device *dev = crtc->dev;
7584
	struct drm_device *dev = crtc->dev;
7559
	struct intel_unpin_work *work;
7585
	struct intel_unpin_work *work;
7560
	unsigned long flags;
7586
	unsigned long flags;
7561
 
7587
 
7562
	spin_lock_irqsave(&dev->event_lock, flags);
7588
	spin_lock_irqsave(&dev->event_lock, flags);
7563
	work = intel_crtc->unpin_work;
7589
	work = intel_crtc->unpin_work;
7564
	intel_crtc->unpin_work = NULL;
7590
	intel_crtc->unpin_work = NULL;
7565
	spin_unlock_irqrestore(&dev->event_lock, flags);
7591
	spin_unlock_irqrestore(&dev->event_lock, flags);
7566
 
7592
 
7567
	if (work) {
7593
	if (work) {
7568
		cancel_work_sync(&work->work);
7594
		cancel_work_sync(&work->work);
7569
		kfree(work);
7595
		kfree(work);
7570
	}
7596
	}
7571
 
7597
 
7572
	drm_crtc_cleanup(crtc);
7598
	drm_crtc_cleanup(crtc);
7573
 
7599
 
7574
	kfree(intel_crtc);
7600
	kfree(intel_crtc);
7575
}
7601
}
7576
 
7602
 
7577
#if 0
7603
#if 0
7578
static void intel_unpin_work_fn(struct work_struct *__work)
7604
static void intel_unpin_work_fn(struct work_struct *__work)
7579
{
7605
{
7580
	struct intel_unpin_work *work =
7606
	struct intel_unpin_work *work =
7581
		container_of(__work, struct intel_unpin_work, work);
7607
		container_of(__work, struct intel_unpin_work, work);
7582
	struct drm_device *dev = work->crtc->dev;
7608
	struct drm_device *dev = work->crtc->dev;
7583
 
7609
 
7584
	mutex_lock(&dev->struct_mutex);
7610
	mutex_lock(&dev->struct_mutex);
7585
	intel_unpin_fb_obj(work->old_fb_obj);
7611
	intel_unpin_fb_obj(work->old_fb_obj);
7586
	drm_gem_object_unreference(&work->pending_flip_obj->base);
7612
	drm_gem_object_unreference(&work->pending_flip_obj->base);
7587
	drm_gem_object_unreference(&work->old_fb_obj->base);
7613
	drm_gem_object_unreference(&work->old_fb_obj->base);
7588
 
7614
 
7589
	intel_update_fbc(dev);
7615
	intel_update_fbc(dev);
7590
	mutex_unlock(&dev->struct_mutex);
7616
	mutex_unlock(&dev->struct_mutex);
7591
 
7617
 
7592
	BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
7618
	BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
7593
	atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
7619
	atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
7594
 
7620
 
7595
	kfree(work);
7621
	kfree(work);
7596
}
7622
}
7597
 
7623
 
7598
static void do_intel_finish_page_flip(struct drm_device *dev,
7624
static void do_intel_finish_page_flip(struct drm_device *dev,
7599
				      struct drm_crtc *crtc)
7625
				      struct drm_crtc *crtc)
7600
{
7626
{
7601
	drm_i915_private_t *dev_priv = dev->dev_private;
7627
	drm_i915_private_t *dev_priv = dev->dev_private;
7602
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7628
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7603
	struct intel_unpin_work *work;
7629
	struct intel_unpin_work *work;
7604
	unsigned long flags;
7630
	unsigned long flags;
7605
 
7631
 
7606
	/* Ignore early vblank irqs */
7632
	/* Ignore early vblank irqs */
7607
	if (intel_crtc == NULL)
7633
	if (intel_crtc == NULL)
7608
		return;
7634
		return;
7609
 
7635
 
7610
	spin_lock_irqsave(&dev->event_lock, flags);
7636
	spin_lock_irqsave(&dev->event_lock, flags);
7611
	work = intel_crtc->unpin_work;
7637
	work = intel_crtc->unpin_work;
7612
 
7638
 
7613
	/* Ensure we don't miss a work->pending update ... */
7639
	/* Ensure we don't miss a work->pending update ... */
7614
	smp_rmb();
7640
	smp_rmb();
7615
 
7641
 
7616
	if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
7642
	if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
7617
		spin_unlock_irqrestore(&dev->event_lock, flags);
7643
		spin_unlock_irqrestore(&dev->event_lock, flags);
7618
		return;
7644
		return;
7619
	}
7645
	}
7620
 
7646
 
7621
	/* and that the unpin work is consistent wrt ->pending. */
7647
	/* and that the unpin work is consistent wrt ->pending. */
7622
	smp_rmb();
7648
	smp_rmb();
7623
 
7649
 
7624
	intel_crtc->unpin_work = NULL;
7650
	intel_crtc->unpin_work = NULL;
7625
 
7651
 
7626
	if (work->event)
7652
	if (work->event)
7627
		drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
7653
		drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
7628
 
7654
 
7629
	drm_vblank_put(dev, intel_crtc->pipe);
7655
	drm_vblank_put(dev, intel_crtc->pipe);
7630
 
7656
 
7631
	spin_unlock_irqrestore(&dev->event_lock, flags);
7657
	spin_unlock_irqrestore(&dev->event_lock, flags);
7632
 
7658
 
7633
	wake_up_all(&dev_priv->pending_flip_queue);
7659
	wake_up_all(&dev_priv->pending_flip_queue);
7634
 
7660
 
7635
	queue_work(dev_priv->wq, &work->work);
7661
	queue_work(dev_priv->wq, &work->work);
7636
 
7662
 
7637
	trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
7663
	trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
7638
}
7664
}
7639
 
7665
 
7640
void intel_finish_page_flip(struct drm_device *dev, int pipe)
7666
void intel_finish_page_flip(struct drm_device *dev, int pipe)
7641
{
7667
{
7642
	drm_i915_private_t *dev_priv = dev->dev_private;
7668
	drm_i915_private_t *dev_priv = dev->dev_private;
7643
	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
7669
	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
7644
 
7670
 
7645
	do_intel_finish_page_flip(dev, crtc);
7671
	do_intel_finish_page_flip(dev, crtc);
7646
}
7672
}
7647
 
7673
 
7648
void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
7674
void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
7649
{
7675
{
7650
	drm_i915_private_t *dev_priv = dev->dev_private;
7676
	drm_i915_private_t *dev_priv = dev->dev_private;
7651
	struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
7677
	struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
7652
 
7678
 
7653
	do_intel_finish_page_flip(dev, crtc);
7679
	do_intel_finish_page_flip(dev, crtc);
7654
}
7680
}
7655
 
7681
 
7656
void intel_prepare_page_flip(struct drm_device *dev, int plane)
7682
void intel_prepare_page_flip(struct drm_device *dev, int plane)
7657
{
7683
{
7658
	drm_i915_private_t *dev_priv = dev->dev_private;
7684
	drm_i915_private_t *dev_priv = dev->dev_private;
7659
	struct intel_crtc *intel_crtc =
7685
	struct intel_crtc *intel_crtc =
7660
		to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
7686
		to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
7661
	unsigned long flags;
7687
	unsigned long flags;
7662
 
7688
 
7663
	/* NB: An MMIO update of the plane base pointer will also
7689
	/* NB: An MMIO update of the plane base pointer will also
7664
	 * generate a page-flip completion irq, i.e. every modeset
7690
	 * generate a page-flip completion irq, i.e. every modeset
7665
	 * is also accompanied by a spurious intel_prepare_page_flip().
7691
	 * is also accompanied by a spurious intel_prepare_page_flip().
7666
	 */
7692
	 */
7667
	spin_lock_irqsave(&dev->event_lock, flags);
7693
	spin_lock_irqsave(&dev->event_lock, flags);
7668
	if (intel_crtc->unpin_work)
7694
	if (intel_crtc->unpin_work)
7669
		atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
7695
		atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
7670
	spin_unlock_irqrestore(&dev->event_lock, flags);
7696
	spin_unlock_irqrestore(&dev->event_lock, flags);
7671
}
7697
}
7672
 
7698
 
7673
inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
7699
inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
7674
{
7700
{
7675
	/* Ensure that the work item is consistent when activating it ... */
7701
	/* Ensure that the work item is consistent when activating it ... */
7676
	smp_wmb();
7702
	smp_wmb();
7677
	atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
7703
	atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
7678
	/* and that it is marked active as soon as the irq could fire. */
7704
	/* and that it is marked active as soon as the irq could fire. */
7679
	smp_wmb();
7705
	smp_wmb();
7680
}
7706
}
7681
 
7707
 
7682
static int intel_gen2_queue_flip(struct drm_device *dev,
7708
static int intel_gen2_queue_flip(struct drm_device *dev,
7683
				 struct drm_crtc *crtc,
7709
				 struct drm_crtc *crtc,
7684
				 struct drm_framebuffer *fb,
7710
				 struct drm_framebuffer *fb,
7685
				 struct drm_i915_gem_object *obj,
7711
				 struct drm_i915_gem_object *obj,
7686
				 uint32_t flags)
7712
				 uint32_t flags)
7687
{
7713
{
7688
	struct drm_i915_private *dev_priv = dev->dev_private;
7714
	struct drm_i915_private *dev_priv = dev->dev_private;
7689
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7715
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7690
	u32 flip_mask;
7716
	u32 flip_mask;
7691
	struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
7717
	struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
7692
	int ret;
7718
	int ret;
7693
 
7719
 
7694
	ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7720
	ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7695
	if (ret)
7721
	if (ret)
7696
		goto err;
7722
		goto err;
7697
 
7723
 
7698
	ret = intel_ring_begin(ring, 6);
7724
	ret = intel_ring_begin(ring, 6);
7699
	if (ret)
7725
	if (ret)
7700
		goto err_unpin;
7726
		goto err_unpin;
7701
 
7727
 
7702
	/* Can't queue multiple flips, so wait for the previous
7728
	/* Can't queue multiple flips, so wait for the previous
7703
	 * one to finish before executing the next.
7729
	 * one to finish before executing the next.
7704
	 */
7730
	 */
7705
	if (intel_crtc->plane)
7731
	if (intel_crtc->plane)
7706
		flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7732
		flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7707
	else
7733
	else
7708
		flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
7734
		flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
7709
	intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7735
	intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7710
	intel_ring_emit(ring, MI_NOOP);
7736
	intel_ring_emit(ring, MI_NOOP);
7711
	intel_ring_emit(ring, MI_DISPLAY_FLIP |
7737
	intel_ring_emit(ring, MI_DISPLAY_FLIP |
7712
			MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7738
			MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7713
	intel_ring_emit(ring, fb->pitches[0]);
7739
	intel_ring_emit(ring, fb->pitches[0]);
7714
	intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
7740
	intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
7715
	intel_ring_emit(ring, 0); /* aux display base address, unused */
7741
	intel_ring_emit(ring, 0); /* aux display base address, unused */
7716
 
7742
 
7717
	intel_mark_page_flip_active(intel_crtc);
7743
	intel_mark_page_flip_active(intel_crtc);
7718
	intel_ring_advance(ring);
7744
	intel_ring_advance(ring);
7719
	return 0;
7745
	return 0;
7720
 
7746
 
7721
err_unpin:
7747
err_unpin:
7722
	intel_unpin_fb_obj(obj);
7748
	intel_unpin_fb_obj(obj);
7723
err:
7749
err:
7724
	return ret;
7750
	return ret;
7725
}
7751
}
7726
 
7752
 
7727
static int intel_gen3_queue_flip(struct drm_device *dev,
7753
static int intel_gen3_queue_flip(struct drm_device *dev,
7728
				 struct drm_crtc *crtc,
7754
				 struct drm_crtc *crtc,
7729
				 struct drm_framebuffer *fb,
7755
				 struct drm_framebuffer *fb,
7730
				 struct drm_i915_gem_object *obj,
7756
				 struct drm_i915_gem_object *obj,
7731
				 uint32_t flags)
7757
				 uint32_t flags)
7732
{
7758
{
7733
	struct drm_i915_private *dev_priv = dev->dev_private;
7759
	struct drm_i915_private *dev_priv = dev->dev_private;
7734
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7760
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7735
	u32 flip_mask;
7761
	u32 flip_mask;
7736
	struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
7762
	struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
7737
	int ret;
7763
	int ret;
7738
 
7764
 
7739
	ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7765
	ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7740
	if (ret)
7766
	if (ret)
7741
		goto err;
7767
		goto err;
7742
 
7768
 
7743
	ret = intel_ring_begin(ring, 6);
7769
	ret = intel_ring_begin(ring, 6);
7744
	if (ret)
7770
	if (ret)
7745
		goto err_unpin;
7771
		goto err_unpin;
7746
 
7772
 
7747
	if (intel_crtc->plane)
7773
	if (intel_crtc->plane)
7748
		flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7774
		flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7749
	else
7775
	else
7750
		flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
7776
		flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
7751
	intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7777
	intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7752
	intel_ring_emit(ring, MI_NOOP);
7778
	intel_ring_emit(ring, MI_NOOP);
7753
	intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
7779
	intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
7754
			MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7780
			MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7755
	intel_ring_emit(ring, fb->pitches[0]);
7781
	intel_ring_emit(ring, fb->pitches[0]);
7756
	intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
7782
	intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
7757
	intel_ring_emit(ring, MI_NOOP);
7783
	intel_ring_emit(ring, MI_NOOP);
7758
 
7784
 
7759
	intel_mark_page_flip_active(intel_crtc);
7785
	intel_mark_page_flip_active(intel_crtc);
7760
	intel_ring_advance(ring);
7786
	intel_ring_advance(ring);
7761
	return 0;
7787
	return 0;
7762
 
7788
 
7763
err_unpin:
7789
err_unpin:
7764
	intel_unpin_fb_obj(obj);
7790
	intel_unpin_fb_obj(obj);
7765
err:
7791
err:
7766
	return ret;
7792
	return ret;
7767
}
7793
}
7768
 
7794
 
7769
static int intel_gen4_queue_flip(struct drm_device *dev,
7795
static int intel_gen4_queue_flip(struct drm_device *dev,
7770
				 struct drm_crtc *crtc,
7796
				 struct drm_crtc *crtc,
7771
				 struct drm_framebuffer *fb,
7797
				 struct drm_framebuffer *fb,
7772
				 struct drm_i915_gem_object *obj,
7798
				 struct drm_i915_gem_object *obj,
7773
				 uint32_t flags)
7799
				 uint32_t flags)
7774
{
7800
{
7775
	struct drm_i915_private *dev_priv = dev->dev_private;
7801
	struct drm_i915_private *dev_priv = dev->dev_private;
7776
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7802
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7777
	uint32_t pf, pipesrc;
7803
	uint32_t pf, pipesrc;
7778
	struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
7804
	struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
7779
	int ret;
7805
	int ret;
7780
 
7806
 
7781
	ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7807
	ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7782
	if (ret)
7808
	if (ret)
7783
		goto err;
7809
		goto err;
7784
 
7810
 
7785
	ret = intel_ring_begin(ring, 4);
7811
	ret = intel_ring_begin(ring, 4);
7786
	if (ret)
7812
	if (ret)
7787
		goto err_unpin;
7813
		goto err_unpin;
7788
 
7814
 
7789
	/* i965+ uses the linear or tiled offsets from the
7815
	/* i965+ uses the linear or tiled offsets from the
7790
	 * Display Registers (which do not change across a page-flip)
7816
	 * Display Registers (which do not change across a page-flip)
7791
	 * so we need only reprogram the base address.
7817
	 * so we need only reprogram the base address.
7792
	 */
7818
	 */
7793
	intel_ring_emit(ring, MI_DISPLAY_FLIP |
7819
	intel_ring_emit(ring, MI_DISPLAY_FLIP |
7794
			MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7820
			MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7795
	intel_ring_emit(ring, fb->pitches[0]);
7821
	intel_ring_emit(ring, fb->pitches[0]);
7796
	intel_ring_emit(ring,
7822
	intel_ring_emit(ring,
7797
			(i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset) |
7823
			(i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset) |
7798
			obj->tiling_mode);
7824
			obj->tiling_mode);
7799
 
7825
 
7800
	/* XXX Enabling the panel-fitter across page-flip is so far
7826
	/* XXX Enabling the panel-fitter across page-flip is so far
7801
	 * untested on non-native modes, so ignore it for now.
7827
	 * untested on non-native modes, so ignore it for now.
7802
	 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
7828
	 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
7803
	 */
7829
	 */
7804
	pf = 0;
7830
	pf = 0;
7805
	pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
7831
	pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
7806
	intel_ring_emit(ring, pf | pipesrc);
7832
	intel_ring_emit(ring, pf | pipesrc);
7807
 
7833
 
7808
	intel_mark_page_flip_active(intel_crtc);
7834
	intel_mark_page_flip_active(intel_crtc);
7809
	intel_ring_advance(ring);
7835
	intel_ring_advance(ring);
7810
	return 0;
7836
	return 0;
7811
 
7837
 
7812
err_unpin:
7838
err_unpin:
7813
	intel_unpin_fb_obj(obj);
7839
	intel_unpin_fb_obj(obj);
7814
err:
7840
err:
7815
	return ret;
7841
	return ret;
7816
}
7842
}
7817
 
7843
 
7818
static int intel_gen6_queue_flip(struct drm_device *dev,
7844
static int intel_gen6_queue_flip(struct drm_device *dev,
7819
				 struct drm_crtc *crtc,
7845
				 struct drm_crtc *crtc,
7820
				 struct drm_framebuffer *fb,
7846
				 struct drm_framebuffer *fb,
7821
				 struct drm_i915_gem_object *obj,
7847
				 struct drm_i915_gem_object *obj,
7822
				 uint32_t flags)
7848
				 uint32_t flags)
7823
{
7849
{
7824
	struct drm_i915_private *dev_priv = dev->dev_private;
7850
	struct drm_i915_private *dev_priv = dev->dev_private;
7825
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7851
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7826
	struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
7852
	struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
7827
	uint32_t pf, pipesrc;
7853
	uint32_t pf, pipesrc;
7828
	int ret;
7854
	int ret;
7829
 
7855
 
7830
	ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7856
	ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7831
	if (ret)
7857
	if (ret)
7832
		goto err;
7858
		goto err;
7833
 
7859
 
7834
	ret = intel_ring_begin(ring, 4);
7860
	ret = intel_ring_begin(ring, 4);
7835
	if (ret)
7861
	if (ret)
7836
		goto err_unpin;
7862
		goto err_unpin;
7837
 
7863
 
7838
	intel_ring_emit(ring, MI_DISPLAY_FLIP |
7864
	intel_ring_emit(ring, MI_DISPLAY_FLIP |
7839
			MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7865
			MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7840
	intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
7866
	intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
7841
	intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
7867
	intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
7842
 
7868
 
7843
	/* Contrary to the suggestions in the documentation,
7869
	/* Contrary to the suggestions in the documentation,
7844
	 * "Enable Panel Fitter" does not seem to be required when page
7870
	 * "Enable Panel Fitter" does not seem to be required when page
7845
	 * flipping with a non-native mode, and worse causes a normal
7871
	 * flipping with a non-native mode, and worse causes a normal
7846
	 * modeset to fail.
7872
	 * modeset to fail.
7847
	 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
7873
	 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
7848
	 */
7874
	 */
7849
	pf = 0;
7875
	pf = 0;
7850
	pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
7876
	pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
7851
	intel_ring_emit(ring, pf | pipesrc);
7877
	intel_ring_emit(ring, pf | pipesrc);
7852
 
7878
 
7853
	intel_mark_page_flip_active(intel_crtc);
7879
	intel_mark_page_flip_active(intel_crtc);
7854
	intel_ring_advance(ring);
7880
	intel_ring_advance(ring);
7855
	return 0;
7881
	return 0;
7856
 
7882
 
7857
err_unpin:
7883
err_unpin:
7858
	intel_unpin_fb_obj(obj);
7884
	intel_unpin_fb_obj(obj);
7859
err:
7885
err:
7860
	return ret;
7886
	return ret;
7861
}
7887
}
7862
 
7888
 
7863
static int intel_gen7_queue_flip(struct drm_device *dev,
7889
static int intel_gen7_queue_flip(struct drm_device *dev,
7864
				 struct drm_crtc *crtc,
7890
				 struct drm_crtc *crtc,
7865
				 struct drm_framebuffer *fb,
7891
				 struct drm_framebuffer *fb,
7866
				 struct drm_i915_gem_object *obj,
7892
				 struct drm_i915_gem_object *obj,
7867
				 uint32_t flags)
7893
				 uint32_t flags)
7868
{
7894
{
7869
	struct drm_i915_private *dev_priv = dev->dev_private;
7895
	struct drm_i915_private *dev_priv = dev->dev_private;
7870
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7896
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7871
	struct intel_ring_buffer *ring;
7897
	struct intel_ring_buffer *ring;
7872
	uint32_t plane_bit = 0;
7898
	uint32_t plane_bit = 0;
7873
	int len, ret;
7899
	int len, ret;
7874
 
7900
 
7875
	ring = obj->ring;
7901
	ring = obj->ring;
7876
	if (IS_VALLEYVIEW(dev) || ring == NULL || ring->id != RCS)
7902
	if (IS_VALLEYVIEW(dev) || ring == NULL || ring->id != RCS)
7877
		ring = &dev_priv->ring[BCS];
7903
		ring = &dev_priv->ring[BCS];
7878
 
7904
 
7879
	ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7905
	ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7880
	if (ret)
7906
	if (ret)
7881
		goto err;
7907
		goto err;
7882
 
7908
 
7883
	switch(intel_crtc->plane) {
7909
	switch(intel_crtc->plane) {
7884
	case PLANE_A:
7910
	case PLANE_A:
7885
		plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
7911
		plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
7886
		break;
7912
		break;
7887
	case PLANE_B:
7913
	case PLANE_B:
7888
		plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
7914
		plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
7889
		break;
7915
		break;
7890
	case PLANE_C:
7916
	case PLANE_C:
7891
		plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
7917
		plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
7892
		break;
7918
		break;
7893
	default:
7919
	default:
7894
		WARN_ONCE(1, "unknown plane in flip command\n");
7920
		WARN_ONCE(1, "unknown plane in flip command\n");
7895
		ret = -ENODEV;
7921
		ret = -ENODEV;
7896
		goto err_unpin;
7922
		goto err_unpin;
7897
	}
7923
	}
7898
 
7924
 
7899
	len = 4;
7925
	len = 4;
7900
	if (ring->id == RCS)
7926
	if (ring->id == RCS)
7901
		len += 6;
7927
		len += 6;
7902
 
7928
 
7903
	ret = intel_ring_begin(ring, len);
7929
	ret = intel_ring_begin(ring, len);
7904
	if (ret)
7930
	if (ret)
7905
		goto err_unpin;
7931
		goto err_unpin;
7906
 
7932
 
7907
	/* Unmask the flip-done completion message. Note that the bspec says that
7933
	/* Unmask the flip-done completion message. Note that the bspec says that
7908
	 * we should do this for both the BCS and RCS, and that we must not unmask
7934
	 * we should do this for both the BCS and RCS, and that we must not unmask
7909
	 * more than one flip event at any time (or ensure that one flip message
7935
	 * more than one flip event at any time (or ensure that one flip message
7910
	 * can be sent by waiting for flip-done prior to queueing new flips).
7936
	 * can be sent by waiting for flip-done prior to queueing new flips).
7911
	 * Experimentation says that BCS works despite DERRMR masking all
7937
	 * Experimentation says that BCS works despite DERRMR masking all
7912
	 * flip-done completion events and that unmasking all planes at once
7938
	 * flip-done completion events and that unmasking all planes at once
7913
	 * for the RCS also doesn't appear to drop events. Setting the DERRMR
7939
	 * for the RCS also doesn't appear to drop events. Setting the DERRMR
7914
	 * to zero does lead to lockups within MI_DISPLAY_FLIP.
7940
	 * to zero does lead to lockups within MI_DISPLAY_FLIP.
7915
	 */
7941
	 */
7916
	if (ring->id == RCS) {
7942
	if (ring->id == RCS) {
7917
		intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
7943
		intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
7918
		intel_ring_emit(ring, DERRMR);
7944
		intel_ring_emit(ring, DERRMR);
7919
		intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
7945
		intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
7920
					DERRMR_PIPEB_PRI_FLIP_DONE |
7946
					DERRMR_PIPEB_PRI_FLIP_DONE |
7921
					DERRMR_PIPEC_PRI_FLIP_DONE));
7947
					DERRMR_PIPEC_PRI_FLIP_DONE));
7922
		intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1));
7948
		intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1));
7923
		intel_ring_emit(ring, DERRMR);
7949
		intel_ring_emit(ring, DERRMR);
7924
		intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
7950
		intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
7925
	}
7951
	}
7926
 
7952
 
7927
	intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
7953
	intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
7928
	intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
7954
	intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
7929
	intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
7955
	intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
7930
	intel_ring_emit(ring, (MI_NOOP));
7956
	intel_ring_emit(ring, (MI_NOOP));
7931
 
7957
 
7932
	intel_mark_page_flip_active(intel_crtc);
7958
	intel_mark_page_flip_active(intel_crtc);
7933
	intel_ring_advance(ring);
7959
	intel_ring_advance(ring);
7934
	return 0;
7960
	return 0;
7935
 
7961
 
7936
err_unpin:
7962
err_unpin:
7937
	intel_unpin_fb_obj(obj);
7963
	intel_unpin_fb_obj(obj);
7938
err:
7964
err:
7939
	return ret;
7965
	return ret;
7940
}
7966
}
7941
 
7967
 
7942
static int intel_default_queue_flip(struct drm_device *dev,
7968
static int intel_default_queue_flip(struct drm_device *dev,
7943
				    struct drm_crtc *crtc,
7969
				    struct drm_crtc *crtc,
7944
				    struct drm_framebuffer *fb,
7970
				    struct drm_framebuffer *fb,
7945
				    struct drm_i915_gem_object *obj,
7971
				    struct drm_i915_gem_object *obj,
7946
				    uint32_t flags)
7972
				    uint32_t flags)
7947
{
7973
{
7948
	return -ENODEV;
7974
	return -ENODEV;
7949
}
7975
}
7950
 
7976
 
7951
static int intel_crtc_page_flip(struct drm_crtc *crtc,
7977
static int intel_crtc_page_flip(struct drm_crtc *crtc,
7952
				struct drm_framebuffer *fb,
7978
				struct drm_framebuffer *fb,
7953
				struct drm_pending_vblank_event *event,
7979
				struct drm_pending_vblank_event *event,
7954
				uint32_t page_flip_flags)
7980
				uint32_t page_flip_flags)
7955
{
7981
{
7956
	struct drm_device *dev = crtc->dev;
7982
	struct drm_device *dev = crtc->dev;
7957
	struct drm_i915_private *dev_priv = dev->dev_private;
7983
	struct drm_i915_private *dev_priv = dev->dev_private;
7958
	struct drm_framebuffer *old_fb = crtc->fb;
7984
	struct drm_framebuffer *old_fb = crtc->fb;
7959
	struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
7985
	struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
7960
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7986
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7961
	struct intel_unpin_work *work;
7987
	struct intel_unpin_work *work;
7962
	unsigned long flags;
7988
	unsigned long flags;
7963
	int ret;
7989
	int ret;
7964
 
7990
 
7965
	/* Can't change pixel format via MI display flips. */
7991
	/* Can't change pixel format via MI display flips. */
7966
	if (fb->pixel_format != crtc->fb->pixel_format)
7992
	if (fb->pixel_format != crtc->fb->pixel_format)
7967
		return -EINVAL;
7993
		return -EINVAL;
7968
 
7994
 
7969
	/*
7995
	/*
7970
	 * TILEOFF/LINOFF registers can't be changed via MI display flips.
7996
	 * TILEOFF/LINOFF registers can't be changed via MI display flips.
7971
	 * Note that pitch changes could also affect these register.
7997
	 * Note that pitch changes could also affect these register.
7972
	 */
7998
	 */
7973
	if (INTEL_INFO(dev)->gen > 3 &&
7999
	if (INTEL_INFO(dev)->gen > 3 &&
7974
	    (fb->offsets[0] != crtc->fb->offsets[0] ||
8000
	    (fb->offsets[0] != crtc->fb->offsets[0] ||
7975
	     fb->pitches[0] != crtc->fb->pitches[0]))
8001
	     fb->pitches[0] != crtc->fb->pitches[0]))
7976
		return -EINVAL;
8002
		return -EINVAL;
7977
 
8003
 
7978
	work = kzalloc(sizeof *work, GFP_KERNEL);
8004
	work = kzalloc(sizeof *work, GFP_KERNEL);
7979
	if (work == NULL)
8005
	if (work == NULL)
7980
		return -ENOMEM;
8006
		return -ENOMEM;
7981
 
8007
 
7982
	work->event = event;
8008
	work->event = event;
7983
	work->crtc = crtc;
8009
	work->crtc = crtc;
7984
	work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
8010
	work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
7985
	INIT_WORK(&work->work, intel_unpin_work_fn);
8011
	INIT_WORK(&work->work, intel_unpin_work_fn);
7986
 
8012
 
7987
	ret = drm_vblank_get(dev, intel_crtc->pipe);
8013
	ret = drm_vblank_get(dev, intel_crtc->pipe);
7988
	if (ret)
8014
	if (ret)
7989
		goto free_work;
8015
		goto free_work;
7990
 
8016
 
7991
	/* We borrow the event spin lock for protecting unpin_work */
8017
	/* We borrow the event spin lock for protecting unpin_work */
7992
	spin_lock_irqsave(&dev->event_lock, flags);
8018
	spin_lock_irqsave(&dev->event_lock, flags);
7993
	if (intel_crtc->unpin_work) {
8019
	if (intel_crtc->unpin_work) {
7994
		spin_unlock_irqrestore(&dev->event_lock, flags);
8020
		spin_unlock_irqrestore(&dev->event_lock, flags);
7995
		kfree(work);
8021
		kfree(work);
7996
		drm_vblank_put(dev, intel_crtc->pipe);
8022
		drm_vblank_put(dev, intel_crtc->pipe);
7997
 
8023
 
7998
		DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
8024
		DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
7999
		return -EBUSY;
8025
		return -EBUSY;
8000
	}
8026
	}
8001
	intel_crtc->unpin_work = work;
8027
	intel_crtc->unpin_work = work;
8002
	spin_unlock_irqrestore(&dev->event_lock, flags);
8028
	spin_unlock_irqrestore(&dev->event_lock, flags);
8003
 
8029
 
8004
	if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
8030
	if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
8005
		flush_workqueue(dev_priv->wq);
8031
		flush_workqueue(dev_priv->wq);
8006
 
8032
 
8007
	ret = i915_mutex_lock_interruptible(dev);
8033
	ret = i915_mutex_lock_interruptible(dev);
8008
	if (ret)
8034
	if (ret)
8009
		goto cleanup;
8035
		goto cleanup;
8010
 
8036
 
8011
	/* Reference the objects for the scheduled work. */
8037
	/* Reference the objects for the scheduled work. */
8012
	drm_gem_object_reference(&work->old_fb_obj->base);
8038
	drm_gem_object_reference(&work->old_fb_obj->base);
8013
	drm_gem_object_reference(&obj->base);
8039
	drm_gem_object_reference(&obj->base);
8014
 
8040
 
8015
	crtc->fb = fb;
8041
	crtc->fb = fb;
8016
 
8042
 
8017
	work->pending_flip_obj = obj;
8043
	work->pending_flip_obj = obj;
8018
 
8044
 
8019
	work->enable_stall_check = true;
8045
	work->enable_stall_check = true;
8020
 
8046
 
8021
	atomic_inc(&intel_crtc->unpin_work_count);
8047
	atomic_inc(&intel_crtc->unpin_work_count);
8022
	intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
8048
	intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
8023
 
8049
 
8024
	ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, page_flip_flags);
8050
	ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, page_flip_flags);
8025
	if (ret)
8051
	if (ret)
8026
		goto cleanup_pending;
8052
		goto cleanup_pending;
8027
 
8053
 
8028
	intel_disable_fbc(dev);
8054
	intel_disable_fbc(dev);
8029
	intel_mark_fb_busy(obj, NULL);
8055
	intel_mark_fb_busy(obj, NULL);
8030
	mutex_unlock(&dev->struct_mutex);
8056
	mutex_unlock(&dev->struct_mutex);
8031
 
8057
 
8032
	trace_i915_flip_request(intel_crtc->plane, obj);
8058
	trace_i915_flip_request(intel_crtc->plane, obj);
8033
 
8059
 
8034
	return 0;
8060
	return 0;
8035
 
8061
 
8036
cleanup_pending:
8062
cleanup_pending:
8037
	atomic_dec(&intel_crtc->unpin_work_count);
8063
	atomic_dec(&intel_crtc->unpin_work_count);
8038
	crtc->fb = old_fb;
8064
	crtc->fb = old_fb;
8039
	drm_gem_object_unreference(&work->old_fb_obj->base);
8065
	drm_gem_object_unreference(&work->old_fb_obj->base);
8040
	drm_gem_object_unreference(&obj->base);
8066
	drm_gem_object_unreference(&obj->base);
8041
	mutex_unlock(&dev->struct_mutex);
8067
	mutex_unlock(&dev->struct_mutex);
8042
 
8068
 
8043
cleanup:
8069
cleanup:
8044
	spin_lock_irqsave(&dev->event_lock, flags);
8070
	spin_lock_irqsave(&dev->event_lock, flags);
8045
	intel_crtc->unpin_work = NULL;
8071
	intel_crtc->unpin_work = NULL;
8046
	spin_unlock_irqrestore(&dev->event_lock, flags);
8072
	spin_unlock_irqrestore(&dev->event_lock, flags);
8047
 
8073
 
8048
	drm_vblank_put(dev, intel_crtc->pipe);
8074
	drm_vblank_put(dev, intel_crtc->pipe);
8049
free_work:
8075
free_work:
8050
	kfree(work);
8076
	kfree(work);
8051
 
8077
 
8052
	return ret;
8078
	return ret;
8053
}
8079
}
8054
#endif
8080
#endif
8055
 
8081
 
8056
static struct drm_crtc_helper_funcs intel_helper_funcs = {
8082
static struct drm_crtc_helper_funcs intel_helper_funcs = {
8057
	.mode_set_base_atomic = intel_pipe_set_base_atomic,
8083
	.mode_set_base_atomic = intel_pipe_set_base_atomic,
8058
	.load_lut = intel_crtc_load_lut,
8084
	.load_lut = intel_crtc_load_lut,
8059
};
8085
};
8060
 
8086
 
8061
static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
8087
static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
8062
				  struct drm_crtc *crtc)
8088
				  struct drm_crtc *crtc)
8063
{
8089
{
8064
	struct drm_device *dev;
8090
	struct drm_device *dev;
8065
	struct drm_crtc *tmp;
8091
	struct drm_crtc *tmp;
8066
	int crtc_mask = 1;
8092
	int crtc_mask = 1;
8067
 
8093
 
8068
	WARN(!crtc, "checking null crtc?\n");
8094
	WARN(!crtc, "checking null crtc?\n");
8069
 
8095
 
8070
	dev = crtc->dev;
8096
	dev = crtc->dev;
8071
 
8097
 
8072
	list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
8098
	list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
8073
		if (tmp == crtc)
8099
		if (tmp == crtc)
8074
			break;
8100
			break;
8075
		crtc_mask <<= 1;
8101
		crtc_mask <<= 1;
8076
	}
8102
	}
8077
 
8103
 
8078
	if (encoder->possible_crtcs & crtc_mask)
8104
	if (encoder->possible_crtcs & crtc_mask)
8079
		return true;
8105
		return true;
8080
	return false;
8106
	return false;
8081
}
8107
}
8082
 
8108
 
8083
/**
8109
/**
8084
 * intel_modeset_update_staged_output_state
8110
 * intel_modeset_update_staged_output_state
8085
 *
8111
 *
8086
 * Updates the staged output configuration state, e.g. after we've read out the
8112
 * Updates the staged output configuration state, e.g. after we've read out the
8087
 * current hw state.
8113
 * current hw state.
8088
 */
8114
 */
8089
static void intel_modeset_update_staged_output_state(struct drm_device *dev)
8115
static void intel_modeset_update_staged_output_state(struct drm_device *dev)
8090
{
8116
{
8091
	struct intel_encoder *encoder;
8117
	struct intel_encoder *encoder;
8092
	struct intel_connector *connector;
8118
	struct intel_connector *connector;
8093
 
8119
 
8094
	list_for_each_entry(connector, &dev->mode_config.connector_list,
8120
	list_for_each_entry(connector, &dev->mode_config.connector_list,
8095
			    base.head) {
8121
			    base.head) {
8096
		connector->new_encoder =
8122
		connector->new_encoder =
8097
			to_intel_encoder(connector->base.encoder);
8123
			to_intel_encoder(connector->base.encoder);
8098
	}
8124
	}
8099
 
8125
 
8100
	list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8126
	list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8101
			    base.head) {
8127
			    base.head) {
8102
		encoder->new_crtc =
8128
		encoder->new_crtc =
8103
			to_intel_crtc(encoder->base.crtc);
8129
			to_intel_crtc(encoder->base.crtc);
8104
	}
8130
	}
8105
}
8131
}
8106
 
8132
 
8107
/**
8133
/**
8108
 * intel_modeset_commit_output_state
8134
 * intel_modeset_commit_output_state
8109
 *
8135
 *
8110
 * This function copies the stage display pipe configuration to the real one.
8136
 * This function copies the stage display pipe configuration to the real one.
8111
 */
8137
 */
8112
static void intel_modeset_commit_output_state(struct drm_device *dev)
8138
static void intel_modeset_commit_output_state(struct drm_device *dev)
8113
{
8139
{
8114
	struct intel_encoder *encoder;
8140
	struct intel_encoder *encoder;
8115
	struct intel_connector *connector;
8141
	struct intel_connector *connector;
8116
 
8142
 
8117
	list_for_each_entry(connector, &dev->mode_config.connector_list,
8143
	list_for_each_entry(connector, &dev->mode_config.connector_list,
8118
			    base.head) {
8144
			    base.head) {
8119
		connector->base.encoder = &connector->new_encoder->base;
8145
		connector->base.encoder = &connector->new_encoder->base;
8120
	}
8146
	}
8121
 
8147
 
8122
	list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8148
	list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8123
			    base.head) {
8149
			    base.head) {
8124
		encoder->base.crtc = &encoder->new_crtc->base;
8150
		encoder->base.crtc = &encoder->new_crtc->base;
8125
	}
8151
	}
8126
}
8152
}
8127
 
8153
 
8128
static void
8154
static void
8129
connected_sink_compute_bpp(struct intel_connector * connector,
8155
connected_sink_compute_bpp(struct intel_connector * connector,
8130
			   struct intel_crtc_config *pipe_config)
8156
			   struct intel_crtc_config *pipe_config)
8131
{
8157
{
8132
	int bpp = pipe_config->pipe_bpp;
8158
	int bpp = pipe_config->pipe_bpp;
8133
 
8159
 
8134
	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
8160
	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
8135
		connector->base.base.id,
8161
		connector->base.base.id,
8136
		drm_get_connector_name(&connector->base));
8162
		drm_get_connector_name(&connector->base));
8137
 
8163
 
8138
	/* Don't use an invalid EDID bpc value */
8164
	/* Don't use an invalid EDID bpc value */
8139
	if (connector->base.display_info.bpc &&
8165
	if (connector->base.display_info.bpc &&
8140
	    connector->base.display_info.bpc * 3 < bpp) {
8166
	    connector->base.display_info.bpc * 3 < bpp) {
8141
		DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
8167
		DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
8142
			      bpp, connector->base.display_info.bpc*3);
8168
			      bpp, connector->base.display_info.bpc*3);
8143
		pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
8169
		pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
8144
	}
8170
	}
8145
 
8171
 
8146
	/* Clamp bpp to 8 on screens without EDID 1.4 */
8172
	/* Clamp bpp to 8 on screens without EDID 1.4 */
8147
	if (connector->base.display_info.bpc == 0 && bpp > 24) {
8173
	if (connector->base.display_info.bpc == 0 && bpp > 24) {
8148
		DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
8174
		DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
8149
			      bpp);
8175
			      bpp);
8150
		pipe_config->pipe_bpp = 24;
8176
		pipe_config->pipe_bpp = 24;
8151
	}
8177
	}
8152
}
8178
}
8153
 
8179
 
8154
static int
8180
static int
8155
compute_baseline_pipe_bpp(struct intel_crtc *crtc,
8181
compute_baseline_pipe_bpp(struct intel_crtc *crtc,
8156
		    struct drm_framebuffer *fb,
8182
		    struct drm_framebuffer *fb,
8157
		    struct intel_crtc_config *pipe_config)
8183
		    struct intel_crtc_config *pipe_config)
8158
{
8184
{
8159
	struct drm_device *dev = crtc->base.dev;
8185
	struct drm_device *dev = crtc->base.dev;
8160
	struct intel_connector *connector;
8186
	struct intel_connector *connector;
8161
	int bpp;
8187
	int bpp;
8162
 
8188
 
8163
	switch (fb->pixel_format) {
8189
	switch (fb->pixel_format) {
8164
	case DRM_FORMAT_C8:
8190
	case DRM_FORMAT_C8:
8165
		bpp = 8*3; /* since we go through a colormap */
8191
		bpp = 8*3; /* since we go through a colormap */
8166
		break;
8192
		break;
8167
	case DRM_FORMAT_XRGB1555:
8193
	case DRM_FORMAT_XRGB1555:
8168
	case DRM_FORMAT_ARGB1555:
8194
	case DRM_FORMAT_ARGB1555:
8169
		/* checked in intel_framebuffer_init already */
8195
		/* checked in intel_framebuffer_init already */
8170
		if (WARN_ON(INTEL_INFO(dev)->gen > 3))
8196
		if (WARN_ON(INTEL_INFO(dev)->gen > 3))
8171
			return -EINVAL;
8197
			return -EINVAL;
8172
	case DRM_FORMAT_RGB565:
8198
	case DRM_FORMAT_RGB565:
8173
		bpp = 6*3; /* min is 18bpp */
8199
		bpp = 6*3; /* min is 18bpp */
8174
		break;
8200
		break;
8175
	case DRM_FORMAT_XBGR8888:
8201
	case DRM_FORMAT_XBGR8888:
8176
	case DRM_FORMAT_ABGR8888:
8202
	case DRM_FORMAT_ABGR8888:
8177
		/* checked in intel_framebuffer_init already */
8203
		/* checked in intel_framebuffer_init already */
8178
		if (WARN_ON(INTEL_INFO(dev)->gen < 4))
8204
		if (WARN_ON(INTEL_INFO(dev)->gen < 4))
8179
			return -EINVAL;
8205
			return -EINVAL;
8180
	case DRM_FORMAT_XRGB8888:
8206
	case DRM_FORMAT_XRGB8888:
8181
	case DRM_FORMAT_ARGB8888:
8207
	case DRM_FORMAT_ARGB8888:
8182
		bpp = 8*3;
8208
		bpp = 8*3;
8183
		break;
8209
		break;
8184
	case DRM_FORMAT_XRGB2101010:
8210
	case DRM_FORMAT_XRGB2101010:
8185
	case DRM_FORMAT_ARGB2101010:
8211
	case DRM_FORMAT_ARGB2101010:
8186
	case DRM_FORMAT_XBGR2101010:
8212
	case DRM_FORMAT_XBGR2101010:
8187
	case DRM_FORMAT_ABGR2101010:
8213
	case DRM_FORMAT_ABGR2101010:
8188
		/* checked in intel_framebuffer_init already */
8214
		/* checked in intel_framebuffer_init already */
8189
		if (WARN_ON(INTEL_INFO(dev)->gen < 4))
8215
		if (WARN_ON(INTEL_INFO(dev)->gen < 4))
8190
			return -EINVAL;
8216
			return -EINVAL;
8191
		bpp = 10*3;
8217
		bpp = 10*3;
8192
		break;
8218
		break;
8193
	/* TODO: gen4+ supports 16 bpc floating point, too. */
8219
	/* TODO: gen4+ supports 16 bpc floating point, too. */
8194
	default:
8220
	default:
8195
		DRM_DEBUG_KMS("unsupported depth\n");
8221
		DRM_DEBUG_KMS("unsupported depth\n");
8196
		return -EINVAL;
8222
		return -EINVAL;
8197
	}
8223
	}
8198
 
8224
 
8199
	pipe_config->pipe_bpp = bpp;
8225
	pipe_config->pipe_bpp = bpp;
8200
 
8226
 
8201
	/* Clamp display bpp to EDID value */
8227
	/* Clamp display bpp to EDID value */
8202
	list_for_each_entry(connector, &dev->mode_config.connector_list,
8228
	list_for_each_entry(connector, &dev->mode_config.connector_list,
8203
			    base.head) {
8229
			    base.head) {
8204
		if (!connector->new_encoder ||
8230
		if (!connector->new_encoder ||
8205
		    connector->new_encoder->new_crtc != crtc)
8231
		    connector->new_encoder->new_crtc != crtc)
8206
			continue;
8232
			continue;
8207
 
8233
 
8208
		connected_sink_compute_bpp(connector, pipe_config);
8234
		connected_sink_compute_bpp(connector, pipe_config);
8209
	}
8235
	}
8210
 
8236
 
8211
	return bpp;
8237
	return bpp;
8212
}
8238
}
8213
 
8239
 
8214
static void intel_dump_pipe_config(struct intel_crtc *crtc,
8240
static void intel_dump_pipe_config(struct intel_crtc *crtc,
8215
				   struct intel_crtc_config *pipe_config,
8241
				   struct intel_crtc_config *pipe_config,
8216
				   const char *context)
8242
				   const char *context)
8217
{
8243
{
8218
	DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
8244
	DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
8219
		      context, pipe_name(crtc->pipe));
8245
		      context, pipe_name(crtc->pipe));
8220
 
8246
 
8221
	DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
8247
	DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
8222
	DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
8248
	DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
8223
		      pipe_config->pipe_bpp, pipe_config->dither);
8249
		      pipe_config->pipe_bpp, pipe_config->dither);
8224
	DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
8250
	DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
8225
		      pipe_config->has_pch_encoder,
8251
		      pipe_config->has_pch_encoder,
8226
		      pipe_config->fdi_lanes,
8252
		      pipe_config->fdi_lanes,
8227
		      pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
8253
		      pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
8228
		      pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
8254
		      pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
8229
		      pipe_config->fdi_m_n.tu);
8255
		      pipe_config->fdi_m_n.tu);
8230
	DRM_DEBUG_KMS("requested mode:\n");
8256
	DRM_DEBUG_KMS("requested mode:\n");
8231
	drm_mode_debug_printmodeline(&pipe_config->requested_mode);
8257
	drm_mode_debug_printmodeline(&pipe_config->requested_mode);
8232
	DRM_DEBUG_KMS("adjusted mode:\n");
8258
	DRM_DEBUG_KMS("adjusted mode:\n");
8233
	drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
8259
	drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
8234
	DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
8260
	DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
8235
		      pipe_config->gmch_pfit.control,
8261
		      pipe_config->gmch_pfit.control,
8236
		      pipe_config->gmch_pfit.pgm_ratios,
8262
		      pipe_config->gmch_pfit.pgm_ratios,
8237
		      pipe_config->gmch_pfit.lvds_border_bits);
8263
		      pipe_config->gmch_pfit.lvds_border_bits);
8238
	DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
8264
	DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
8239
		      pipe_config->pch_pfit.pos,
8265
		      pipe_config->pch_pfit.pos,
8240
		      pipe_config->pch_pfit.size,
8266
		      pipe_config->pch_pfit.size,
8241
		      pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
8267
		      pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
8242
	DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
8268
	DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
8243
}
8269
}
8244
 
8270
 
8245
static bool check_encoder_cloning(struct drm_crtc *crtc)
8271
static bool check_encoder_cloning(struct drm_crtc *crtc)
8246
{
8272
{
8247
	int num_encoders = 0;
8273
	int num_encoders = 0;
8248
	bool uncloneable_encoders = false;
8274
	bool uncloneable_encoders = false;
8249
	struct intel_encoder *encoder;
8275
	struct intel_encoder *encoder;
8250
 
8276
 
8251
	list_for_each_entry(encoder, &crtc->dev->mode_config.encoder_list,
8277
	list_for_each_entry(encoder, &crtc->dev->mode_config.encoder_list,
8252
			    base.head) {
8278
			    base.head) {
8253
		if (&encoder->new_crtc->base != crtc)
8279
		if (&encoder->new_crtc->base != crtc)
8254
			continue;
8280
			continue;
8255
 
8281
 
8256
		num_encoders++;
8282
		num_encoders++;
8257
		if (!encoder->cloneable)
8283
		if (!encoder->cloneable)
8258
			uncloneable_encoders = true;
8284
			uncloneable_encoders = true;
8259
	}
8285
	}
8260
 
8286
 
8261
	return !(num_encoders > 1 && uncloneable_encoders);
8287
	return !(num_encoders > 1 && uncloneable_encoders);
8262
}
8288
}
8263
 
8289
 
8264
static struct intel_crtc_config *
8290
static struct intel_crtc_config *
8265
intel_modeset_pipe_config(struct drm_crtc *crtc,
8291
intel_modeset_pipe_config(struct drm_crtc *crtc,
8266
			  struct drm_framebuffer *fb,
8292
			  struct drm_framebuffer *fb,
8267
			    struct drm_display_mode *mode)
8293
			    struct drm_display_mode *mode)
8268
{
8294
{
8269
	struct drm_device *dev = crtc->dev;
8295
	struct drm_device *dev = crtc->dev;
8270
	struct intel_encoder *encoder;
8296
	struct intel_encoder *encoder;
8271
	struct intel_crtc_config *pipe_config;
8297
	struct intel_crtc_config *pipe_config;
8272
	int plane_bpp, ret = -EINVAL;
8298
	int plane_bpp, ret = -EINVAL;
8273
	bool retry = true;
8299
	bool retry = true;
8274
 
8300
 
8275
	if (!check_encoder_cloning(crtc)) {
8301
	if (!check_encoder_cloning(crtc)) {
8276
		DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
8302
		DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
8277
		return ERR_PTR(-EINVAL);
8303
		return ERR_PTR(-EINVAL);
8278
	}
8304
	}
8279
 
8305
 
8280
	pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
8306
	pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
8281
	if (!pipe_config)
8307
	if (!pipe_config)
8282
		return ERR_PTR(-ENOMEM);
8308
		return ERR_PTR(-ENOMEM);
8283
 
8309
 
8284
	drm_mode_copy(&pipe_config->adjusted_mode, mode);
8310
	drm_mode_copy(&pipe_config->adjusted_mode, mode);
8285
	drm_mode_copy(&pipe_config->requested_mode, mode);
8311
	drm_mode_copy(&pipe_config->requested_mode, mode);
8286
	pipe_config->cpu_transcoder =
8312
	pipe_config->cpu_transcoder =
8287
		(enum transcoder) to_intel_crtc(crtc)->pipe;
8313
		(enum transcoder) to_intel_crtc(crtc)->pipe;
8288
	pipe_config->shared_dpll = DPLL_ID_PRIVATE;
8314
	pipe_config->shared_dpll = DPLL_ID_PRIVATE;
8289
 
8315
 
8290
	/*
8316
	/*
8291
	 * Sanitize sync polarity flags based on requested ones. If neither
8317
	 * Sanitize sync polarity flags based on requested ones. If neither
8292
	 * positive or negative polarity is requested, treat this as meaning
8318
	 * positive or negative polarity is requested, treat this as meaning
8293
	 * negative polarity.
8319
	 * negative polarity.
8294
	 */
8320
	 */
8295
	if (!(pipe_config->adjusted_mode.flags &
8321
	if (!(pipe_config->adjusted_mode.flags &
8296
	      (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
8322
	      (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
8297
		pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
8323
		pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
8298
 
8324
 
8299
	if (!(pipe_config->adjusted_mode.flags &
8325
	if (!(pipe_config->adjusted_mode.flags &
8300
	      (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
8326
	      (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
8301
		pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
8327
		pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
8302
 
8328
 
8303
	/* Compute a starting value for pipe_config->pipe_bpp taking the source
8329
	/* Compute a starting value for pipe_config->pipe_bpp taking the source
8304
	 * plane pixel format and any sink constraints into account. Returns the
8330
	 * plane pixel format and any sink constraints into account. Returns the
8305
	 * source plane bpp so that dithering can be selected on mismatches
8331
	 * source plane bpp so that dithering can be selected on mismatches
8306
	 * after encoders and crtc also have had their say. */
8332
	 * after encoders and crtc also have had their say. */
8307
	plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
8333
	plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
8308
					      fb, pipe_config);
8334
					      fb, pipe_config);
8309
	if (plane_bpp < 0)
8335
	if (plane_bpp < 0)
8310
		goto fail;
8336
		goto fail;
8311
 
8337
 
8312
encoder_retry:
8338
encoder_retry:
8313
	/* Ensure the port clock defaults are reset when retrying. */
8339
	/* Ensure the port clock defaults are reset when retrying. */
8314
	pipe_config->port_clock = 0;
8340
	pipe_config->port_clock = 0;
8315
	pipe_config->pixel_multiplier = 1;
8341
	pipe_config->pixel_multiplier = 1;
8316
 
8342
 
8317
	/* Fill in default crtc timings, allow encoders to overwrite them. */
8343
	/* Fill in default crtc timings, allow encoders to overwrite them. */
8318
	drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, 0);
8344
	drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, 0);
8319
 
8345
 
8320
	/* Pass our mode to the connectors and the CRTC to give them a chance to
8346
	/* Pass our mode to the connectors and the CRTC to give them a chance to
8321
	 * adjust it according to limitations or connector properties, and also
8347
	 * adjust it according to limitations or connector properties, and also
8322
	 * a chance to reject the mode entirely.
8348
	 * a chance to reject the mode entirely.
8323
	 */
8349
	 */
8324
	list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8350
	list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8325
			    base.head) {
8351
			    base.head) {
8326
 
8352
 
8327
		if (&encoder->new_crtc->base != crtc)
8353
		if (&encoder->new_crtc->base != crtc)
8328
			continue;
8354
			continue;
8329
 
8355
 
8330
			if (!(encoder->compute_config(encoder, pipe_config))) {
8356
			if (!(encoder->compute_config(encoder, pipe_config))) {
8331
				DRM_DEBUG_KMS("Encoder config failure\n");
8357
				DRM_DEBUG_KMS("Encoder config failure\n");
8332
				goto fail;
8358
				goto fail;
8333
			}
8359
			}
8334
		}
8360
		}
8335
 
8361
 
8336
	/* Set default port clock if not overwritten by the encoder. Needs to be
8362
	/* Set default port clock if not overwritten by the encoder. Needs to be
8337
	 * done afterwards in case the encoder adjusts the mode. */
8363
	 * done afterwards in case the encoder adjusts the mode. */
8338
	if (!pipe_config->port_clock)
8364
	if (!pipe_config->port_clock)
8339
		pipe_config->port_clock = pipe_config->adjusted_mode.clock;
8365
		pipe_config->port_clock = pipe_config->adjusted_mode.clock;
8340
 
8366
 
8341
	ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
8367
	ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
8342
	if (ret < 0) {
8368
	if (ret < 0) {
8343
		DRM_DEBUG_KMS("CRTC fixup failed\n");
8369
		DRM_DEBUG_KMS("CRTC fixup failed\n");
8344
		goto fail;
8370
		goto fail;
8345
	}
8371
	}
8346
 
8372
 
8347
	if (ret == RETRY) {
8373
	if (ret == RETRY) {
8348
		if (WARN(!retry, "loop in pipe configuration computation\n")) {
8374
		if (WARN(!retry, "loop in pipe configuration computation\n")) {
8349
			ret = -EINVAL;
8375
			ret = -EINVAL;
8350
			goto fail;
8376
			goto fail;
8351
		}
8377
		}
8352
 
8378
 
8353
		DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
8379
		DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
8354
		retry = false;
8380
		retry = false;
8355
		goto encoder_retry;
8381
		goto encoder_retry;
8356
	}
8382
	}
8357
 
8383
 
8358
	pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
8384
	pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
8359
	DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
8385
	DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
8360
		      plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
8386
		      plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
8361
 
8387
 
8362
	return pipe_config;
8388
	return pipe_config;
8363
fail:
8389
fail:
8364
	kfree(pipe_config);
8390
	kfree(pipe_config);
8365
	return ERR_PTR(ret);
8391
	return ERR_PTR(ret);
8366
}
8392
}
8367
 
8393
 
8368
/* Computes which crtcs are affected and sets the relevant bits in the mask. For
8394
/* Computes which crtcs are affected and sets the relevant bits in the mask. For
8369
 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
8395
 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
8370
static void
8396
static void
8371
intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
8397
intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
8372
			     unsigned *prepare_pipes, unsigned *disable_pipes)
8398
			     unsigned *prepare_pipes, unsigned *disable_pipes)
8373
{
8399
{
8374
	struct intel_crtc *intel_crtc;
8400
	struct intel_crtc *intel_crtc;
8375
	struct drm_device *dev = crtc->dev;
8401
	struct drm_device *dev = crtc->dev;
8376
	struct intel_encoder *encoder;
8402
	struct intel_encoder *encoder;
8377
	struct intel_connector *connector;
8403
	struct intel_connector *connector;
8378
	struct drm_crtc *tmp_crtc;
8404
	struct drm_crtc *tmp_crtc;
8379
 
8405
 
8380
	*disable_pipes = *modeset_pipes = *prepare_pipes = 0;
8406
	*disable_pipes = *modeset_pipes = *prepare_pipes = 0;
8381
 
8407
 
8382
	/* Check which crtcs have changed outputs connected to them, these need
8408
	/* Check which crtcs have changed outputs connected to them, these need
8383
	 * to be part of the prepare_pipes mask. We don't (yet) support global
8409
	 * to be part of the prepare_pipes mask. We don't (yet) support global
8384
	 * modeset across multiple crtcs, so modeset_pipes will only have one
8410
	 * modeset across multiple crtcs, so modeset_pipes will only have one
8385
	 * bit set at most. */
8411
	 * bit set at most. */
8386
	list_for_each_entry(connector, &dev->mode_config.connector_list,
8412
	list_for_each_entry(connector, &dev->mode_config.connector_list,
8387
			    base.head) {
8413
			    base.head) {
8388
		if (connector->base.encoder == &connector->new_encoder->base)
8414
		if (connector->base.encoder == &connector->new_encoder->base)
8389
			continue;
8415
			continue;
8390
 
8416
 
8391
		if (connector->base.encoder) {
8417
		if (connector->base.encoder) {
8392
			tmp_crtc = connector->base.encoder->crtc;
8418
			tmp_crtc = connector->base.encoder->crtc;
8393
 
8419
 
8394
			*prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
8420
			*prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
8395
		}
8421
		}
8396
 
8422
 
8397
		if (connector->new_encoder)
8423
		if (connector->new_encoder)
8398
			*prepare_pipes |=
8424
			*prepare_pipes |=
8399
				1 << connector->new_encoder->new_crtc->pipe;
8425
				1 << connector->new_encoder->new_crtc->pipe;
8400
	}
8426
	}
8401
 
8427
 
8402
	list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8428
	list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8403
			    base.head) {
8429
			    base.head) {
8404
		if (encoder->base.crtc == &encoder->new_crtc->base)
8430
		if (encoder->base.crtc == &encoder->new_crtc->base)
8405
			continue;
8431
			continue;
8406
 
8432
 
8407
		if (encoder->base.crtc) {
8433
		if (encoder->base.crtc) {
8408
			tmp_crtc = encoder->base.crtc;
8434
			tmp_crtc = encoder->base.crtc;
8409
 
8435
 
8410
			*prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
8436
			*prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
8411
		}
8437
		}
8412
 
8438
 
8413
		if (encoder->new_crtc)
8439
		if (encoder->new_crtc)
8414
			*prepare_pipes |= 1 << encoder->new_crtc->pipe;
8440
			*prepare_pipes |= 1 << encoder->new_crtc->pipe;
8415
	}
8441
	}
8416
 
8442
 
8417
	/* Check for any pipes that will be fully disabled ... */
8443
	/* Check for any pipes that will be fully disabled ... */
8418
	list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
8444
	list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
8419
			    base.head) {
8445
			    base.head) {
8420
		bool used = false;
8446
		bool used = false;
8421
 
8447
 
8422
		/* Don't try to disable disabled crtcs. */
8448
		/* Don't try to disable disabled crtcs. */
8423
		if (!intel_crtc->base.enabled)
8449
		if (!intel_crtc->base.enabled)
8424
			continue;
8450
			continue;
8425
 
8451
 
8426
		list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8452
		list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8427
				    base.head) {
8453
				    base.head) {
8428
			if (encoder->new_crtc == intel_crtc)
8454
			if (encoder->new_crtc == intel_crtc)
8429
				used = true;
8455
				used = true;
8430
		}
8456
		}
8431
 
8457
 
8432
		if (!used)
8458
		if (!used)
8433
			*disable_pipes |= 1 << intel_crtc->pipe;
8459
			*disable_pipes |= 1 << intel_crtc->pipe;
8434
	}
8460
	}
8435
 
8461
 
8436
 
8462
 
8437
	/* set_mode is also used to update properties on life display pipes. */
8463
	/* set_mode is also used to update properties on life display pipes. */
8438
	intel_crtc = to_intel_crtc(crtc);
8464
	intel_crtc = to_intel_crtc(crtc);
8439
	if (crtc->enabled)
8465
	if (crtc->enabled)
8440
		*prepare_pipes |= 1 << intel_crtc->pipe;
8466
		*prepare_pipes |= 1 << intel_crtc->pipe;
8441
 
8467
 
8442
	/*
8468
	/*
8443
	 * For simplicity do a full modeset on any pipe where the output routing
8469
	 * For simplicity do a full modeset on any pipe where the output routing
8444
	 * changed. We could be more clever, but that would require us to be
8470
	 * changed. We could be more clever, but that would require us to be
8445
	 * more careful with calling the relevant encoder->mode_set functions.
8471
	 * more careful with calling the relevant encoder->mode_set functions.
8446
	 */
8472
	 */
8447
	if (*prepare_pipes)
8473
	if (*prepare_pipes)
8448
		*modeset_pipes = *prepare_pipes;
8474
		*modeset_pipes = *prepare_pipes;
8449
 
8475
 
8450
	/* ... and mask these out. */
8476
	/* ... and mask these out. */
8451
	*modeset_pipes &= ~(*disable_pipes);
8477
	*modeset_pipes &= ~(*disable_pipes);
8452
	*prepare_pipes &= ~(*disable_pipes);
8478
	*prepare_pipes &= ~(*disable_pipes);
8453
 
8479
 
8454
	/*
8480
	/*
8455
	 * HACK: We don't (yet) fully support global modesets. intel_set_config
8481
	 * HACK: We don't (yet) fully support global modesets. intel_set_config
8456
	 * obies this rule, but the modeset restore mode of
8482
	 * obies this rule, but the modeset restore mode of
8457
	 * intel_modeset_setup_hw_state does not.
8483
	 * intel_modeset_setup_hw_state does not.
8458
	 */
8484
	 */
8459
	*modeset_pipes &= 1 << intel_crtc->pipe;
8485
	*modeset_pipes &= 1 << intel_crtc->pipe;
8460
	*prepare_pipes &= 1 << intel_crtc->pipe;
8486
	*prepare_pipes &= 1 << intel_crtc->pipe;
8461
 
8487
 
8462
	DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
8488
	DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
8463
		      *modeset_pipes, *prepare_pipes, *disable_pipes);
8489
		      *modeset_pipes, *prepare_pipes, *disable_pipes);
8464
}
8490
}
8465
 
8491
 
8466
static bool intel_crtc_in_use(struct drm_crtc *crtc)
8492
static bool intel_crtc_in_use(struct drm_crtc *crtc)
8467
{
8493
{
8468
	struct drm_encoder *encoder;
8494
	struct drm_encoder *encoder;
8469
	struct drm_device *dev = crtc->dev;
8495
	struct drm_device *dev = crtc->dev;
8470
 
8496
 
8471
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
8497
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
8472
		if (encoder->crtc == crtc)
8498
		if (encoder->crtc == crtc)
8473
			return true;
8499
			return true;
8474
 
8500
 
8475
	return false;
8501
	return false;
8476
}
8502
}
8477
 
8503
 
8478
static void
8504
static void
8479
intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
8505
intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
8480
{
8506
{
8481
	struct intel_encoder *intel_encoder;
8507
	struct intel_encoder *intel_encoder;
8482
	struct intel_crtc *intel_crtc;
8508
	struct intel_crtc *intel_crtc;
8483
	struct drm_connector *connector;
8509
	struct drm_connector *connector;
8484
 
8510
 
8485
	list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
8511
	list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
8486
			    base.head) {
8512
			    base.head) {
8487
		if (!intel_encoder->base.crtc)
8513
		if (!intel_encoder->base.crtc)
8488
			continue;
8514
			continue;
8489
 
8515
 
8490
		intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
8516
		intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
8491
 
8517
 
8492
		if (prepare_pipes & (1 << intel_crtc->pipe))
8518
		if (prepare_pipes & (1 << intel_crtc->pipe))
8493
			intel_encoder->connectors_active = false;
8519
			intel_encoder->connectors_active = false;
8494
	}
8520
	}
8495
 
8521
 
8496
	intel_modeset_commit_output_state(dev);
8522
	intel_modeset_commit_output_state(dev);
8497
 
8523
 
8498
	/* Update computed state. */
8524
	/* Update computed state. */
8499
	list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
8525
	list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
8500
			    base.head) {
8526
			    base.head) {
8501
		intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
8527
		intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
8502
	}
8528
	}
8503
 
8529
 
8504
	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
8530
	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
8505
		if (!connector->encoder || !connector->encoder->crtc)
8531
		if (!connector->encoder || !connector->encoder->crtc)
8506
			continue;
8532
			continue;
8507
 
8533
 
8508
		intel_crtc = to_intel_crtc(connector->encoder->crtc);
8534
		intel_crtc = to_intel_crtc(connector->encoder->crtc);
8509
 
8535
 
8510
		if (prepare_pipes & (1 << intel_crtc->pipe)) {
8536
		if (prepare_pipes & (1 << intel_crtc->pipe)) {
8511
			struct drm_property *dpms_property =
8537
			struct drm_property *dpms_property =
8512
				dev->mode_config.dpms_property;
8538
				dev->mode_config.dpms_property;
8513
 
8539
 
8514
			connector->dpms = DRM_MODE_DPMS_ON;
8540
			connector->dpms = DRM_MODE_DPMS_ON;
8515
			drm_object_property_set_value(&connector->base,
8541
			drm_object_property_set_value(&connector->base,
8516
							 dpms_property,
8542
							 dpms_property,
8517
							 DRM_MODE_DPMS_ON);
8543
							 DRM_MODE_DPMS_ON);
8518
 
8544
 
8519
			intel_encoder = to_intel_encoder(connector->encoder);
8545
			intel_encoder = to_intel_encoder(connector->encoder);
8520
			intel_encoder->connectors_active = true;
8546
			intel_encoder->connectors_active = true;
8521
		}
8547
		}
8522
	}
8548
	}
8523
 
8549
 
8524
}
8550
}
8525
 
8551
 
8526
static bool intel_fuzzy_clock_check(struct intel_crtc_config *cur,
8552
static bool intel_fuzzy_clock_check(struct intel_crtc_config *cur,
8527
				    struct intel_crtc_config *new)
8553
				    struct intel_crtc_config *new)
8528
{
8554
{
8529
	int clock1, clock2, diff;
8555
	int clock1, clock2, diff;
8530
 
8556
 
8531
	clock1 = cur->adjusted_mode.clock;
8557
	clock1 = cur->adjusted_mode.clock;
8532
	clock2 = new->adjusted_mode.clock;
8558
	clock2 = new->adjusted_mode.clock;
8533
 
8559
 
8534
	if (clock1 == clock2)
8560
	if (clock1 == clock2)
8535
		return true;
8561
		return true;
8536
 
8562
 
8537
	if (!clock1 || !clock2)
8563
	if (!clock1 || !clock2)
8538
		return false;
8564
		return false;
8539
 
8565
 
8540
	diff = abs(clock1 - clock2);
8566
	diff = abs(clock1 - clock2);
8541
 
8567
 
8542
	if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
8568
	if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
8543
		return true;
8569
		return true;
8544
 
8570
 
8545
	return false;
8571
	return false;
8546
}
8572
}
8547
 
8573
 
8548
#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
8574
#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
8549
	list_for_each_entry((intel_crtc), \
8575
	list_for_each_entry((intel_crtc), \
8550
			    &(dev)->mode_config.crtc_list, \
8576
			    &(dev)->mode_config.crtc_list, \
8551
			    base.head) \
8577
			    base.head) \
8552
		if (mask & (1 <<(intel_crtc)->pipe))
8578
		if (mask & (1 <<(intel_crtc)->pipe))
8553
 
8579
 
8554
static bool
8580
static bool
8555
intel_pipe_config_compare(struct drm_device *dev,
8581
intel_pipe_config_compare(struct drm_device *dev,
8556
			  struct intel_crtc_config *current_config,
8582
			  struct intel_crtc_config *current_config,
8557
			  struct intel_crtc_config *pipe_config)
8583
			  struct intel_crtc_config *pipe_config)
8558
{
8584
{
8559
#define PIPE_CONF_CHECK_X(name)	\
8585
#define PIPE_CONF_CHECK_X(name)	\
8560
	if (current_config->name != pipe_config->name) { \
8586
	if (current_config->name != pipe_config->name) { \
8561
		DRM_ERROR("mismatch in " #name " " \
8587
		DRM_ERROR("mismatch in " #name " " \
8562
			  "(expected 0x%08x, found 0x%08x)\n", \
8588
			  "(expected 0x%08x, found 0x%08x)\n", \
8563
			  current_config->name, \
8589
			  current_config->name, \
8564
			  pipe_config->name); \
8590
			  pipe_config->name); \
8565
		return false; \
8591
		return false; \
8566
	}
8592
	}
8567
 
8593
 
8568
#define PIPE_CONF_CHECK_I(name)	\
8594
#define PIPE_CONF_CHECK_I(name)	\
8569
	if (current_config->name != pipe_config->name) { \
8595
	if (current_config->name != pipe_config->name) { \
8570
		DRM_ERROR("mismatch in " #name " " \
8596
		DRM_ERROR("mismatch in " #name " " \
8571
			  "(expected %i, found %i)\n", \
8597
			  "(expected %i, found %i)\n", \
8572
			  current_config->name, \
8598
			  current_config->name, \
8573
			  pipe_config->name); \
8599
			  pipe_config->name); \
8574
		return false; \
8600
		return false; \
8575
	}
8601
	}
8576
 
8602
 
8577
#define PIPE_CONF_CHECK_FLAGS(name, mask)	\
8603
#define PIPE_CONF_CHECK_FLAGS(name, mask)	\
8578
	if ((current_config->name ^ pipe_config->name) & (mask)) { \
8604
	if ((current_config->name ^ pipe_config->name) & (mask)) { \
8579
		DRM_ERROR("mismatch in " #name "(" #mask ") "	   \
8605
		DRM_ERROR("mismatch in " #name "(" #mask ") "	   \
8580
			  "(expected %i, found %i)\n", \
8606
			  "(expected %i, found %i)\n", \
8581
			  current_config->name & (mask), \
8607
			  current_config->name & (mask), \
8582
			  pipe_config->name & (mask)); \
8608
			  pipe_config->name & (mask)); \
8583
		return false; \
8609
		return false; \
8584
	}
8610
	}
8585
 
8611
 
8586
#define PIPE_CONF_QUIRK(quirk)	\
8612
#define PIPE_CONF_QUIRK(quirk)	\
8587
	((current_config->quirks | pipe_config->quirks) & (quirk))
8613
	((current_config->quirks | pipe_config->quirks) & (quirk))
8588
 
8614
 
8589
	PIPE_CONF_CHECK_I(cpu_transcoder);
8615
	PIPE_CONF_CHECK_I(cpu_transcoder);
8590
 
8616
 
8591
	PIPE_CONF_CHECK_I(has_pch_encoder);
8617
	PIPE_CONF_CHECK_I(has_pch_encoder);
8592
	PIPE_CONF_CHECK_I(fdi_lanes);
8618
	PIPE_CONF_CHECK_I(fdi_lanes);
8593
	PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
8619
	PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
8594
	PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
8620
	PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
8595
	PIPE_CONF_CHECK_I(fdi_m_n.link_m);
8621
	PIPE_CONF_CHECK_I(fdi_m_n.link_m);
8596
	PIPE_CONF_CHECK_I(fdi_m_n.link_n);
8622
	PIPE_CONF_CHECK_I(fdi_m_n.link_n);
8597
	PIPE_CONF_CHECK_I(fdi_m_n.tu);
8623
	PIPE_CONF_CHECK_I(fdi_m_n.tu);
8598
 
8624
 
8599
	PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
8625
	PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
8600
	PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
8626
	PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
8601
	PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
8627
	PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
8602
	PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
8628
	PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
8603
	PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
8629
	PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
8604
	PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
8630
	PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
8605
 
8631
 
8606
	PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
8632
	PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
8607
	PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
8633
	PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
8608
	PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
8634
	PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
8609
	PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
8635
	PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
8610
	PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
8636
	PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
8611
	PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
8637
	PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
8612
 
8638
 
8613
		PIPE_CONF_CHECK_I(pixel_multiplier);
8639
		PIPE_CONF_CHECK_I(pixel_multiplier);
8614
 
8640
 
8615
	PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8641
	PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8616
			      DRM_MODE_FLAG_INTERLACE);
8642
			      DRM_MODE_FLAG_INTERLACE);
8617
 
8643
 
8618
	if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
8644
	if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
8619
		PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8645
		PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8620
				      DRM_MODE_FLAG_PHSYNC);
8646
				      DRM_MODE_FLAG_PHSYNC);
8621
		PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8647
		PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8622
				      DRM_MODE_FLAG_NHSYNC);
8648
				      DRM_MODE_FLAG_NHSYNC);
8623
		PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8649
		PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8624
				      DRM_MODE_FLAG_PVSYNC);
8650
				      DRM_MODE_FLAG_PVSYNC);
8625
		PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8651
		PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8626
				      DRM_MODE_FLAG_NVSYNC);
8652
				      DRM_MODE_FLAG_NVSYNC);
8627
	}
8653
	}
8628
 
8654
 
8629
	PIPE_CONF_CHECK_I(requested_mode.hdisplay);
8655
	PIPE_CONF_CHECK_I(requested_mode.hdisplay);
8630
	PIPE_CONF_CHECK_I(requested_mode.vdisplay);
8656
	PIPE_CONF_CHECK_I(requested_mode.vdisplay);
8631
 
8657
 
8632
	PIPE_CONF_CHECK_I(gmch_pfit.control);
8658
	PIPE_CONF_CHECK_I(gmch_pfit.control);
8633
	/* pfit ratios are autocomputed by the hw on gen4+ */
8659
	/* pfit ratios are autocomputed by the hw on gen4+ */
8634
	if (INTEL_INFO(dev)->gen < 4)
8660
	if (INTEL_INFO(dev)->gen < 4)
8635
		PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
8661
		PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
8636
	PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
8662
	PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
8637
	PIPE_CONF_CHECK_I(pch_pfit.enabled);
8663
	PIPE_CONF_CHECK_I(pch_pfit.enabled);
8638
	if (current_config->pch_pfit.enabled) {
8664
	if (current_config->pch_pfit.enabled) {
8639
	PIPE_CONF_CHECK_I(pch_pfit.pos);
8665
	PIPE_CONF_CHECK_I(pch_pfit.pos);
8640
	PIPE_CONF_CHECK_I(pch_pfit.size);
8666
	PIPE_CONF_CHECK_I(pch_pfit.size);
8641
	}
8667
	}
8642
 
8668
 
8643
	PIPE_CONF_CHECK_I(ips_enabled);
8669
	PIPE_CONF_CHECK_I(ips_enabled);
8644
 
8670
 
8645
	PIPE_CONF_CHECK_I(shared_dpll);
8671
	PIPE_CONF_CHECK_I(shared_dpll);
8646
	PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8672
	PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8647
	PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
8673
	PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
8648
	PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
8674
	PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
8649
	PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
8675
	PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
8650
 
8676
 
8651
	if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
8677
	if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
8652
		PIPE_CONF_CHECK_I(pipe_bpp);
8678
		PIPE_CONF_CHECK_I(pipe_bpp);
8653
 
8679
 
8654
#undef PIPE_CONF_CHECK_X
8680
#undef PIPE_CONF_CHECK_X
8655
#undef PIPE_CONF_CHECK_I
8681
#undef PIPE_CONF_CHECK_I
8656
#undef PIPE_CONF_CHECK_FLAGS
8682
#undef PIPE_CONF_CHECK_FLAGS
8657
#undef PIPE_CONF_QUIRK
8683
#undef PIPE_CONF_QUIRK
8658
 
8684
 
8659
	if (!IS_HASWELL(dev)) {
8685
	if (!IS_HASWELL(dev)) {
8660
		if (!intel_fuzzy_clock_check(current_config, pipe_config)) {
8686
		if (!intel_fuzzy_clock_check(current_config, pipe_config)) {
8661
			DRM_ERROR("mismatch in clock (expected %d, found %d)\n",
8687
			DRM_ERROR("mismatch in clock (expected %d, found %d)\n",
8662
				  current_config->adjusted_mode.clock,
8688
				  current_config->adjusted_mode.clock,
8663
				  pipe_config->adjusted_mode.clock);
8689
				  pipe_config->adjusted_mode.clock);
8664
			return false;
8690
			return false;
8665
		}
8691
		}
8666
	}
8692
	}
8667
 
8693
 
8668
	return true;
8694
	return true;
8669
}
8695
}
8670
 
8696
 
8671
static void
8697
static void
8672
check_connector_state(struct drm_device *dev)
8698
check_connector_state(struct drm_device *dev)
8673
{
8699
{
8674
	struct intel_connector *connector;
8700
	struct intel_connector *connector;
8675
 
8701
 
8676
	list_for_each_entry(connector, &dev->mode_config.connector_list,
8702
	list_for_each_entry(connector, &dev->mode_config.connector_list,
8677
			    base.head) {
8703
			    base.head) {
8678
		/* This also checks the encoder/connector hw state with the
8704
		/* This also checks the encoder/connector hw state with the
8679
		 * ->get_hw_state callbacks. */
8705
		 * ->get_hw_state callbacks. */
8680
		intel_connector_check_state(connector);
8706
		intel_connector_check_state(connector);
8681
 
8707
 
8682
		WARN(&connector->new_encoder->base != connector->base.encoder,
8708
		WARN(&connector->new_encoder->base != connector->base.encoder,
8683
		     "connector's staged encoder doesn't match current encoder\n");
8709
		     "connector's staged encoder doesn't match current encoder\n");
8684
	}
8710
	}
8685
}
8711
}
8686
 
8712
 
8687
static void
8713
static void
8688
check_encoder_state(struct drm_device *dev)
8714
check_encoder_state(struct drm_device *dev)
8689
{
8715
{
8690
	struct intel_encoder *encoder;
8716
	struct intel_encoder *encoder;
8691
	struct intel_connector *connector;
8717
	struct intel_connector *connector;
8692
 
8718
 
8693
	list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8719
	list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8694
			    base.head) {
8720
			    base.head) {
8695
		bool enabled = false;
8721
		bool enabled = false;
8696
		bool active = false;
8722
		bool active = false;
8697
		enum pipe pipe, tracked_pipe;
8723
		enum pipe pipe, tracked_pipe;
8698
 
8724
 
8699
		DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
8725
		DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
8700
			      encoder->base.base.id,
8726
			      encoder->base.base.id,
8701
			      drm_get_encoder_name(&encoder->base));
8727
			      drm_get_encoder_name(&encoder->base));
8702
 
8728
 
8703
		WARN(&encoder->new_crtc->base != encoder->base.crtc,
8729
		WARN(&encoder->new_crtc->base != encoder->base.crtc,
8704
		     "encoder's stage crtc doesn't match current crtc\n");
8730
		     "encoder's stage crtc doesn't match current crtc\n");
8705
		WARN(encoder->connectors_active && !encoder->base.crtc,
8731
		WARN(encoder->connectors_active && !encoder->base.crtc,
8706
		     "encoder's active_connectors set, but no crtc\n");
8732
		     "encoder's active_connectors set, but no crtc\n");
8707
 
8733
 
8708
		list_for_each_entry(connector, &dev->mode_config.connector_list,
8734
		list_for_each_entry(connector, &dev->mode_config.connector_list,
8709
				    base.head) {
8735
				    base.head) {
8710
			if (connector->base.encoder != &encoder->base)
8736
			if (connector->base.encoder != &encoder->base)
8711
				continue;
8737
				continue;
8712
			enabled = true;
8738
			enabled = true;
8713
			if (connector->base.dpms != DRM_MODE_DPMS_OFF)
8739
			if (connector->base.dpms != DRM_MODE_DPMS_OFF)
8714
				active = true;
8740
				active = true;
8715
		}
8741
		}
8716
		WARN(!!encoder->base.crtc != enabled,
8742
		WARN(!!encoder->base.crtc != enabled,
8717
		     "encoder's enabled state mismatch "
8743
		     "encoder's enabled state mismatch "
8718
		     "(expected %i, found %i)\n",
8744
		     "(expected %i, found %i)\n",
8719
		     !!encoder->base.crtc, enabled);
8745
		     !!encoder->base.crtc, enabled);
8720
		WARN(active && !encoder->base.crtc,
8746
		WARN(active && !encoder->base.crtc,
8721
		     "active encoder with no crtc\n");
8747
		     "active encoder with no crtc\n");
8722
 
8748
 
8723
		WARN(encoder->connectors_active != active,
8749
		WARN(encoder->connectors_active != active,
8724
		     "encoder's computed active state doesn't match tracked active state "
8750
		     "encoder's computed active state doesn't match tracked active state "
8725
		     "(expected %i, found %i)\n", active, encoder->connectors_active);
8751
		     "(expected %i, found %i)\n", active, encoder->connectors_active);
8726
 
8752
 
8727
		active = encoder->get_hw_state(encoder, &pipe);
8753
		active = encoder->get_hw_state(encoder, &pipe);
8728
		WARN(active != encoder->connectors_active,
8754
		WARN(active != encoder->connectors_active,
8729
		     "encoder's hw state doesn't match sw tracking "
8755
		     "encoder's hw state doesn't match sw tracking "
8730
		     "(expected %i, found %i)\n",
8756
		     "(expected %i, found %i)\n",
8731
		     encoder->connectors_active, active);
8757
		     encoder->connectors_active, active);
8732
 
8758
 
8733
		if (!encoder->base.crtc)
8759
		if (!encoder->base.crtc)
8734
			continue;
8760
			continue;
8735
 
8761
 
8736
		tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
8762
		tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
8737
		WARN(active && pipe != tracked_pipe,
8763
		WARN(active && pipe != tracked_pipe,
8738
		     "active encoder's pipe doesn't match"
8764
		     "active encoder's pipe doesn't match"
8739
		     "(expected %i, found %i)\n",
8765
		     "(expected %i, found %i)\n",
8740
		     tracked_pipe, pipe);
8766
		     tracked_pipe, pipe);
8741
 
8767
 
8742
	}
8768
	}
8743
}
8769
}
8744
 
8770
 
8745
static void
8771
static void
8746
check_crtc_state(struct drm_device *dev)
8772
check_crtc_state(struct drm_device *dev)
8747
{
8773
{
8748
	drm_i915_private_t *dev_priv = dev->dev_private;
8774
	drm_i915_private_t *dev_priv = dev->dev_private;
8749
	struct intel_crtc *crtc;
8775
	struct intel_crtc *crtc;
8750
	struct intel_encoder *encoder;
8776
	struct intel_encoder *encoder;
8751
	struct intel_crtc_config pipe_config;
8777
	struct intel_crtc_config pipe_config;
8752
 
8778
 
8753
	list_for_each_entry(crtc, &dev->mode_config.crtc_list,
8779
	list_for_each_entry(crtc, &dev->mode_config.crtc_list,
8754
			    base.head) {
8780
			    base.head) {
8755
		bool enabled = false;
8781
		bool enabled = false;
8756
		bool active = false;
8782
		bool active = false;
8757
 
8783
 
8758
		memset(&pipe_config, 0, sizeof(pipe_config));
8784
		memset(&pipe_config, 0, sizeof(pipe_config));
8759
 
8785
 
8760
		DRM_DEBUG_KMS("[CRTC:%d]\n",
8786
		DRM_DEBUG_KMS("[CRTC:%d]\n",
8761
			      crtc->base.base.id);
8787
			      crtc->base.base.id);
8762
 
8788
 
8763
		WARN(crtc->active && !crtc->base.enabled,
8789
		WARN(crtc->active && !crtc->base.enabled,
8764
		     "active crtc, but not enabled in sw tracking\n");
8790
		     "active crtc, but not enabled in sw tracking\n");
8765
 
8791
 
8766
		list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8792
		list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8767
				    base.head) {
8793
				    base.head) {
8768
			if (encoder->base.crtc != &crtc->base)
8794
			if (encoder->base.crtc != &crtc->base)
8769
				continue;
8795
				continue;
8770
			enabled = true;
8796
			enabled = true;
8771
			if (encoder->connectors_active)
8797
			if (encoder->connectors_active)
8772
				active = true;
8798
				active = true;
8773
		}
8799
		}
8774
 
8800
 
8775
		WARN(active != crtc->active,
8801
		WARN(active != crtc->active,
8776
		     "crtc's computed active state doesn't match tracked active state "
8802
		     "crtc's computed active state doesn't match tracked active state "
8777
		     "(expected %i, found %i)\n", active, crtc->active);
8803
		     "(expected %i, found %i)\n", active, crtc->active);
8778
		WARN(enabled != crtc->base.enabled,
8804
		WARN(enabled != crtc->base.enabled,
8779
		     "crtc's computed enabled state doesn't match tracked enabled state "
8805
		     "crtc's computed enabled state doesn't match tracked enabled state "
8780
		     "(expected %i, found %i)\n", enabled, crtc->base.enabled);
8806
		     "(expected %i, found %i)\n", enabled, crtc->base.enabled);
8781
 
8807
 
8782
		active = dev_priv->display.get_pipe_config(crtc,
8808
		active = dev_priv->display.get_pipe_config(crtc,
8783
							   &pipe_config);
8809
							   &pipe_config);
8784
 
8810
 
8785
		/* hw state is inconsistent with the pipe A quirk */
8811
		/* hw state is inconsistent with the pipe A quirk */
8786
		if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
8812
		if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
8787
			active = crtc->active;
8813
			active = crtc->active;
8788
 
8814
 
8789
		list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8815
		list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8790
				    base.head) {
8816
				    base.head) {
8791
			enum pipe pipe;
8817
			enum pipe pipe;
8792
			if (encoder->base.crtc != &crtc->base)
8818
			if (encoder->base.crtc != &crtc->base)
8793
				continue;
8819
				continue;
8794
			if (encoder->get_config &&
8820
			if (encoder->get_config &&
8795
			    encoder->get_hw_state(encoder, &pipe))
8821
			    encoder->get_hw_state(encoder, &pipe))
8796
				encoder->get_config(encoder, &pipe_config);
8822
				encoder->get_config(encoder, &pipe_config);
8797
		}
8823
		}
8798
 
8824
 
8799
		if (dev_priv->display.get_clock)
8825
		if (dev_priv->display.get_clock)
8800
			dev_priv->display.get_clock(crtc, &pipe_config);
8826
			dev_priv->display.get_clock(crtc, &pipe_config);
8801
 
8827
 
8802
		WARN(crtc->active != active,
8828
		WARN(crtc->active != active,
8803
		     "crtc active state doesn't match with hw state "
8829
		     "crtc active state doesn't match with hw state "
8804
		     "(expected %i, found %i)\n", crtc->active, active);
8830
		     "(expected %i, found %i)\n", crtc->active, active);
8805
 
8831
 
8806
		if (active &&
8832
		if (active &&
8807
		    !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
8833
		    !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
8808
			WARN(1, "pipe state doesn't match!\n");
8834
			WARN(1, "pipe state doesn't match!\n");
8809
			intel_dump_pipe_config(crtc, &pipe_config,
8835
			intel_dump_pipe_config(crtc, &pipe_config,
8810
					       "[hw state]");
8836
					       "[hw state]");
8811
			intel_dump_pipe_config(crtc, &crtc->config,
8837
			intel_dump_pipe_config(crtc, &crtc->config,
8812
					       "[sw state]");
8838
					       "[sw state]");
8813
		}
8839
		}
8814
	}
8840
	}
8815
}
8841
}
8816
 
8842
 
8817
static void
8843
static void
8818
check_shared_dpll_state(struct drm_device *dev)
8844
check_shared_dpll_state(struct drm_device *dev)
8819
{
8845
{
8820
	drm_i915_private_t *dev_priv = dev->dev_private;
8846
	drm_i915_private_t *dev_priv = dev->dev_private;
8821
	struct intel_crtc *crtc;
8847
	struct intel_crtc *crtc;
8822
	struct intel_dpll_hw_state dpll_hw_state;
8848
	struct intel_dpll_hw_state dpll_hw_state;
8823
	int i;
8849
	int i;
8824
 
8850
 
8825
	for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8851
	for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8826
		struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
8852
		struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
8827
		int enabled_crtcs = 0, active_crtcs = 0;
8853
		int enabled_crtcs = 0, active_crtcs = 0;
8828
		bool active;
8854
		bool active;
8829
 
8855
 
8830
		memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
8856
		memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
8831
 
8857
 
8832
		DRM_DEBUG_KMS("%s\n", pll->name);
8858
		DRM_DEBUG_KMS("%s\n", pll->name);
8833
 
8859
 
8834
		active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
8860
		active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
8835
 
8861
 
8836
		WARN(pll->active > pll->refcount,
8862
		WARN(pll->active > pll->refcount,
8837
		     "more active pll users than references: %i vs %i\n",
8863
		     "more active pll users than references: %i vs %i\n",
8838
		     pll->active, pll->refcount);
8864
		     pll->active, pll->refcount);
8839
		WARN(pll->active && !pll->on,
8865
		WARN(pll->active && !pll->on,
8840
		     "pll in active use but not on in sw tracking\n");
8866
		     "pll in active use but not on in sw tracking\n");
8841
		WARN(pll->on && !pll->active,
8867
		WARN(pll->on && !pll->active,
8842
		     "pll in on but not on in use in sw tracking\n");
8868
		     "pll in on but not on in use in sw tracking\n");
8843
		WARN(pll->on != active,
8869
		WARN(pll->on != active,
8844
		     "pll on state mismatch (expected %i, found %i)\n",
8870
		     "pll on state mismatch (expected %i, found %i)\n",
8845
		     pll->on, active);
8871
		     pll->on, active);
8846
 
8872
 
8847
		list_for_each_entry(crtc, &dev->mode_config.crtc_list,
8873
		list_for_each_entry(crtc, &dev->mode_config.crtc_list,
8848
				    base.head) {
8874
				    base.head) {
8849
			if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
8875
			if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
8850
				enabled_crtcs++;
8876
				enabled_crtcs++;
8851
			if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
8877
			if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
8852
				active_crtcs++;
8878
				active_crtcs++;
8853
		}
8879
		}
8854
		WARN(pll->active != active_crtcs,
8880
		WARN(pll->active != active_crtcs,
8855
		     "pll active crtcs mismatch (expected %i, found %i)\n",
8881
		     "pll active crtcs mismatch (expected %i, found %i)\n",
8856
		     pll->active, active_crtcs);
8882
		     pll->active, active_crtcs);
8857
		WARN(pll->refcount != enabled_crtcs,
8883
		WARN(pll->refcount != enabled_crtcs,
8858
		     "pll enabled crtcs mismatch (expected %i, found %i)\n",
8884
		     "pll enabled crtcs mismatch (expected %i, found %i)\n",
8859
		     pll->refcount, enabled_crtcs);
8885
		     pll->refcount, enabled_crtcs);
8860
 
8886
 
8861
		WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
8887
		WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
8862
				       sizeof(dpll_hw_state)),
8888
				       sizeof(dpll_hw_state)),
8863
		     "pll hw state mismatch\n");
8889
		     "pll hw state mismatch\n");
8864
	}
8890
	}
8865
}
8891
}
8866
 
8892
 
8867
void
8893
void
8868
intel_modeset_check_state(struct drm_device *dev)
8894
intel_modeset_check_state(struct drm_device *dev)
8869
{
8895
{
8870
	check_connector_state(dev);
8896
	check_connector_state(dev);
8871
	check_encoder_state(dev);
8897
	check_encoder_state(dev);
8872
	check_crtc_state(dev);
8898
	check_crtc_state(dev);
8873
	check_shared_dpll_state(dev);
8899
	check_shared_dpll_state(dev);
8874
}
8900
}
8875
 
8901
 
8876
static int __intel_set_mode(struct drm_crtc *crtc,
8902
static int __intel_set_mode(struct drm_crtc *crtc,
8877
		    struct drm_display_mode *mode,
8903
		    struct drm_display_mode *mode,
8878
		    int x, int y, struct drm_framebuffer *fb)
8904
		    int x, int y, struct drm_framebuffer *fb)
8879
{
8905
{
8880
	struct drm_device *dev = crtc->dev;
8906
	struct drm_device *dev = crtc->dev;
8881
	drm_i915_private_t *dev_priv = dev->dev_private;
8907
	drm_i915_private_t *dev_priv = dev->dev_private;
8882
	struct drm_display_mode *saved_mode, *saved_hwmode;
8908
	struct drm_display_mode *saved_mode, *saved_hwmode;
8883
	struct intel_crtc_config *pipe_config = NULL;
8909
	struct intel_crtc_config *pipe_config = NULL;
8884
	struct intel_crtc *intel_crtc;
8910
	struct intel_crtc *intel_crtc;
8885
	unsigned disable_pipes, prepare_pipes, modeset_pipes;
8911
	unsigned disable_pipes, prepare_pipes, modeset_pipes;
8886
	int ret = 0;
8912
	int ret = 0;
8887
 
8913
 
8888
	saved_mode = kmalloc(2 * sizeof(*saved_mode), GFP_KERNEL);
8914
	saved_mode = kmalloc(2 * sizeof(*saved_mode), GFP_KERNEL);
8889
	if (!saved_mode)
8915
	if (!saved_mode)
8890
		return -ENOMEM;
8916
		return -ENOMEM;
8891
	saved_hwmode = saved_mode + 1;
8917
	saved_hwmode = saved_mode + 1;
8892
 
8918
 
8893
	intel_modeset_affected_pipes(crtc, &modeset_pipes,
8919
	intel_modeset_affected_pipes(crtc, &modeset_pipes,
8894
				     &prepare_pipes, &disable_pipes);
8920
				     &prepare_pipes, &disable_pipes);
8895
 
8921
 
8896
	*saved_hwmode = crtc->hwmode;
8922
	*saved_hwmode = crtc->hwmode;
8897
	*saved_mode = crtc->mode;
8923
	*saved_mode = crtc->mode;
8898
 
8924
 
8899
	/* Hack: Because we don't (yet) support global modeset on multiple
8925
	/* Hack: Because we don't (yet) support global modeset on multiple
8900
	 * crtcs, we don't keep track of the new mode for more than one crtc.
8926
	 * crtcs, we don't keep track of the new mode for more than one crtc.
8901
	 * Hence simply check whether any bit is set in modeset_pipes in all the
8927
	 * Hence simply check whether any bit is set in modeset_pipes in all the
8902
	 * pieces of code that are not yet converted to deal with mutliple crtcs
8928
	 * pieces of code that are not yet converted to deal with mutliple crtcs
8903
	 * changing their mode at the same time. */
8929
	 * changing their mode at the same time. */
8904
	if (modeset_pipes) {
8930
	if (modeset_pipes) {
8905
		pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
8931
		pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
8906
		if (IS_ERR(pipe_config)) {
8932
		if (IS_ERR(pipe_config)) {
8907
			ret = PTR_ERR(pipe_config);
8933
			ret = PTR_ERR(pipe_config);
8908
			pipe_config = NULL;
8934
			pipe_config = NULL;
8909
 
8935
 
8910
			goto out;
8936
			goto out;
8911
		}
8937
		}
8912
		intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
8938
		intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
8913
				       "[modeset]");
8939
				       "[modeset]");
8914
	}
8940
	}
8915
 
8941
 
8916
	for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
8942
	for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
8917
		intel_crtc_disable(&intel_crtc->base);
8943
		intel_crtc_disable(&intel_crtc->base);
8918
 
8944
 
8919
	for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
8945
	for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
8920
		if (intel_crtc->base.enabled)
8946
		if (intel_crtc->base.enabled)
8921
			dev_priv->display.crtc_disable(&intel_crtc->base);
8947
			dev_priv->display.crtc_disable(&intel_crtc->base);
8922
	}
8948
	}
8923
 
8949
 
8924
	/* crtc->mode is already used by the ->mode_set callbacks, hence we need
8950
	/* crtc->mode is already used by the ->mode_set callbacks, hence we need
8925
	 * to set it here already despite that we pass it down the callchain.
8951
	 * to set it here already despite that we pass it down the callchain.
8926
	 */
8952
	 */
8927
	if (modeset_pipes) {
8953
	if (modeset_pipes) {
8928
		crtc->mode = *mode;
8954
		crtc->mode = *mode;
8929
		/* mode_set/enable/disable functions rely on a correct pipe
8955
		/* mode_set/enable/disable functions rely on a correct pipe
8930
		 * config. */
8956
		 * config. */
8931
		to_intel_crtc(crtc)->config = *pipe_config;
8957
		to_intel_crtc(crtc)->config = *pipe_config;
8932
	}
8958
	}
8933
 
8959
 
8934
	/* Only after disabling all output pipelines that will be changed can we
8960
	/* Only after disabling all output pipelines that will be changed can we
8935
	 * update the the output configuration. */
8961
	 * update the the output configuration. */
8936
	intel_modeset_update_state(dev, prepare_pipes);
8962
	intel_modeset_update_state(dev, prepare_pipes);
8937
 
8963
 
8938
	if (dev_priv->display.modeset_global_resources)
8964
	if (dev_priv->display.modeset_global_resources)
8939
		dev_priv->display.modeset_global_resources(dev);
8965
		dev_priv->display.modeset_global_resources(dev);
8940
 
8966
 
8941
	/* Set up the DPLL and any encoders state that needs to adjust or depend
8967
	/* Set up the DPLL and any encoders state that needs to adjust or depend
8942
	 * on the DPLL.
8968
	 * on the DPLL.
8943
	 */
8969
	 */
8944
	for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
8970
	for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
8945
		ret = intel_crtc_mode_set(&intel_crtc->base,
8971
		ret = intel_crtc_mode_set(&intel_crtc->base,
8946
					   x, y, fb);
8972
					   x, y, fb);
8947
		if (ret)
8973
		if (ret)
8948
		    goto done;
8974
		    goto done;
8949
	}
8975
	}
8950
 
8976
 
8951
	/* Now enable the clocks, plane, pipe, and connectors that we set up. */
8977
	/* Now enable the clocks, plane, pipe, and connectors that we set up. */
8952
	for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
8978
	for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
8953
		dev_priv->display.crtc_enable(&intel_crtc->base);
8979
		dev_priv->display.crtc_enable(&intel_crtc->base);
8954
 
8980
 
8955
	if (modeset_pipes) {
8981
	if (modeset_pipes) {
8956
		/* Store real post-adjustment hardware mode. */
8982
		/* Store real post-adjustment hardware mode. */
8957
		crtc->hwmode = pipe_config->adjusted_mode;
8983
		crtc->hwmode = pipe_config->adjusted_mode;
8958
 
8984
 
8959
		/* Calculate and store various constants which
8985
		/* Calculate and store various constants which
8960
		 * are later needed by vblank and swap-completion
8986
		 * are later needed by vblank and swap-completion
8961
		 * timestamping. They are derived from true hwmode.
8987
		 * timestamping. They are derived from true hwmode.
8962
		 */
8988
		 */
8963
		drm_calc_timestamping_constants(crtc);
8989
		drm_calc_timestamping_constants(crtc);
8964
	}
8990
	}
8965
 
8991
 
8966
	/* FIXME: add subpixel order */
8992
	/* FIXME: add subpixel order */
8967
done:
8993
done:
8968
	if (ret && crtc->enabled) {
8994
	if (ret && crtc->enabled) {
8969
		crtc->hwmode = *saved_hwmode;
8995
		crtc->hwmode = *saved_hwmode;
8970
		crtc->mode = *saved_mode;
8996
		crtc->mode = *saved_mode;
8971
	}
8997
	}
8972
 
8998
 
8973
out:
8999
out:
8974
	kfree(pipe_config);
9000
	kfree(pipe_config);
8975
	kfree(saved_mode);
9001
	kfree(saved_mode);
8976
	return ret;
9002
	return ret;
8977
}
9003
}
8978
 
9004
 
8979
static int intel_set_mode(struct drm_crtc *crtc,
9005
static int intel_set_mode(struct drm_crtc *crtc,
8980
		     struct drm_display_mode *mode,
9006
		     struct drm_display_mode *mode,
8981
		     int x, int y, struct drm_framebuffer *fb)
9007
		     int x, int y, struct drm_framebuffer *fb)
8982
{
9008
{
8983
	int ret;
9009
	int ret;
8984
 
9010
 
8985
	ret = __intel_set_mode(crtc, mode, x, y, fb);
9011
	ret = __intel_set_mode(crtc, mode, x, y, fb);
8986
 
9012
 
8987
	if (ret == 0)
9013
	if (ret == 0)
8988
		intel_modeset_check_state(crtc->dev);
9014
		intel_modeset_check_state(crtc->dev);
8989
 
9015
 
8990
	return ret;
9016
	return ret;
8991
}
9017
}
8992
 
9018
 
8993
void intel_crtc_restore_mode(struct drm_crtc *crtc)
9019
void intel_crtc_restore_mode(struct drm_crtc *crtc)
8994
{
9020
{
8995
	intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->fb);
9021
	intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->fb);
8996
}
9022
}
8997
 
9023
 
8998
#undef for_each_intel_crtc_masked
9024
#undef for_each_intel_crtc_masked
8999
 
9025
 
9000
static void intel_set_config_free(struct intel_set_config *config)
9026
static void intel_set_config_free(struct intel_set_config *config)
9001
{
9027
{
9002
	if (!config)
9028
	if (!config)
9003
		return;
9029
		return;
9004
 
9030
 
9005
	kfree(config->save_connector_encoders);
9031
	kfree(config->save_connector_encoders);
9006
	kfree(config->save_encoder_crtcs);
9032
	kfree(config->save_encoder_crtcs);
9007
	kfree(config);
9033
	kfree(config);
9008
}
9034
}
9009
 
9035
 
9010
static int intel_set_config_save_state(struct drm_device *dev,
9036
static int intel_set_config_save_state(struct drm_device *dev,
9011
				       struct intel_set_config *config)
9037
				       struct intel_set_config *config)
9012
{
9038
{
9013
	struct drm_encoder *encoder;
9039
	struct drm_encoder *encoder;
9014
	struct drm_connector *connector;
9040
	struct drm_connector *connector;
9015
	int count;
9041
	int count;
9016
 
9042
 
9017
	config->save_encoder_crtcs =
9043
	config->save_encoder_crtcs =
9018
		kcalloc(dev->mode_config.num_encoder,
9044
		kcalloc(dev->mode_config.num_encoder,
9019
			sizeof(struct drm_crtc *), GFP_KERNEL);
9045
			sizeof(struct drm_crtc *), GFP_KERNEL);
9020
	if (!config->save_encoder_crtcs)
9046
	if (!config->save_encoder_crtcs)
9021
		return -ENOMEM;
9047
		return -ENOMEM;
9022
 
9048
 
9023
	config->save_connector_encoders =
9049
	config->save_connector_encoders =
9024
		kcalloc(dev->mode_config.num_connector,
9050
		kcalloc(dev->mode_config.num_connector,
9025
			sizeof(struct drm_encoder *), GFP_KERNEL);
9051
			sizeof(struct drm_encoder *), GFP_KERNEL);
9026
	if (!config->save_connector_encoders)
9052
	if (!config->save_connector_encoders)
9027
		return -ENOMEM;
9053
		return -ENOMEM;
9028
 
9054
 
9029
	/* Copy data. Note that driver private data is not affected.
9055
	/* Copy data. Note that driver private data is not affected.
9030
	 * Should anything bad happen only the expected state is
9056
	 * Should anything bad happen only the expected state is
9031
	 * restored, not the drivers personal bookkeeping.
9057
	 * restored, not the drivers personal bookkeeping.
9032
	 */
9058
	 */
9033
	count = 0;
9059
	count = 0;
9034
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
9060
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
9035
		config->save_encoder_crtcs[count++] = encoder->crtc;
9061
		config->save_encoder_crtcs[count++] = encoder->crtc;
9036
	}
9062
	}
9037
 
9063
 
9038
	count = 0;
9064
	count = 0;
9039
	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
9065
	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
9040
		config->save_connector_encoders[count++] = connector->encoder;
9066
		config->save_connector_encoders[count++] = connector->encoder;
9041
	}
9067
	}
9042
 
9068
 
9043
	return 0;
9069
	return 0;
9044
}
9070
}
9045
 
9071
 
9046
static void intel_set_config_restore_state(struct drm_device *dev,
9072
static void intel_set_config_restore_state(struct drm_device *dev,
9047
					   struct intel_set_config *config)
9073
					   struct intel_set_config *config)
9048
{
9074
{
9049
	struct intel_encoder *encoder;
9075
	struct intel_encoder *encoder;
9050
	struct intel_connector *connector;
9076
	struct intel_connector *connector;
9051
	int count;
9077
	int count;
9052
 
9078
 
9053
	count = 0;
9079
	count = 0;
9054
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
9080
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
9055
		encoder->new_crtc =
9081
		encoder->new_crtc =
9056
			to_intel_crtc(config->save_encoder_crtcs[count++]);
9082
			to_intel_crtc(config->save_encoder_crtcs[count++]);
9057
	}
9083
	}
9058
 
9084
 
9059
	count = 0;
9085
	count = 0;
9060
	list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
9086
	list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
9061
		connector->new_encoder =
9087
		connector->new_encoder =
9062
			to_intel_encoder(config->save_connector_encoders[count++]);
9088
			to_intel_encoder(config->save_connector_encoders[count++]);
9063
	}
9089
	}
9064
}
9090
}
9065
 
9091
 
9066
static bool
9092
static bool
9067
is_crtc_connector_off(struct drm_mode_set *set)
9093
is_crtc_connector_off(struct drm_mode_set *set)
9068
{
9094
{
9069
	int i;
9095
	int i;
9070
 
9096
 
9071
	if (set->num_connectors == 0)
9097
	if (set->num_connectors == 0)
9072
		return false;
9098
		return false;
9073
 
9099
 
9074
	if (WARN_ON(set->connectors == NULL))
9100
	if (WARN_ON(set->connectors == NULL))
9075
		return false;
9101
		return false;
9076
 
9102
 
9077
	for (i = 0; i < set->num_connectors; i++)
9103
	for (i = 0; i < set->num_connectors; i++)
9078
		if (set->connectors[i]->encoder &&
9104
		if (set->connectors[i]->encoder &&
9079
		    set->connectors[i]->encoder->crtc == set->crtc &&
9105
		    set->connectors[i]->encoder->crtc == set->crtc &&
9080
		    set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
9106
		    set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
9081
			return true;
9107
			return true;
9082
 
9108
 
9083
	return false;
9109
	return false;
9084
}
9110
}
9085
 
9111
 
9086
static void
9112
static void
9087
intel_set_config_compute_mode_changes(struct drm_mode_set *set,
9113
intel_set_config_compute_mode_changes(struct drm_mode_set *set,
9088
				      struct intel_set_config *config)
9114
				      struct intel_set_config *config)
9089
{
9115
{
9090
 
9116
 
9091
	/* We should be able to check here if the fb has the same properties
9117
	/* We should be able to check here if the fb has the same properties
9092
	 * and then just flip_or_move it */
9118
	 * and then just flip_or_move it */
9093
	if (is_crtc_connector_off(set)) {
9119
	if (is_crtc_connector_off(set)) {
9094
			config->mode_changed = true;
9120
			config->mode_changed = true;
9095
	} else if (set->crtc->fb != set->fb) {
9121
	} else if (set->crtc->fb != set->fb) {
9096
		/* If we have no fb then treat it as a full mode set */
9122
		/* If we have no fb then treat it as a full mode set */
9097
		if (set->crtc->fb == NULL) {
9123
		if (set->crtc->fb == NULL) {
9098
			struct intel_crtc *intel_crtc =
9124
			struct intel_crtc *intel_crtc =
9099
				to_intel_crtc(set->crtc);
9125
				to_intel_crtc(set->crtc);
9100
 
9126
 
9101
			if (intel_crtc->active && i915_fastboot) {
9127
			if (intel_crtc->active && i915_fastboot) {
9102
				DRM_DEBUG_KMS("crtc has no fb, will flip\n");
9128
				DRM_DEBUG_KMS("crtc has no fb, will flip\n");
9103
				config->fb_changed = true;
9129
				config->fb_changed = true;
9104
			} else {
9130
			} else {
9105
				DRM_DEBUG_KMS("inactive crtc, full mode set\n");
9131
				DRM_DEBUG_KMS("inactive crtc, full mode set\n");
9106
			config->mode_changed = true;
9132
			config->mode_changed = true;
9107
			}
9133
			}
9108
		} else if (set->fb == NULL) {
9134
		} else if (set->fb == NULL) {
9109
			config->mode_changed = true;
9135
			config->mode_changed = true;
9110
		} else if (set->fb->pixel_format !=
9136
		} else if (set->fb->pixel_format !=
9111
			   set->crtc->fb->pixel_format) {
9137
			   set->crtc->fb->pixel_format) {
9112
			config->mode_changed = true;
9138
			config->mode_changed = true;
9113
		} else {
9139
		} else {
9114
			config->fb_changed = true;
9140
			config->fb_changed = true;
9115
	}
9141
	}
9116
	}
9142
	}
9117
 
9143
 
9118
	if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
9144
	if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
9119
		config->fb_changed = true;
9145
		config->fb_changed = true;
9120
 
9146
 
9121
	if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
9147
	if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
9122
		DRM_DEBUG_KMS("modes are different, full mode set\n");
9148
		DRM_DEBUG_KMS("modes are different, full mode set\n");
9123
		drm_mode_debug_printmodeline(&set->crtc->mode);
9149
		drm_mode_debug_printmodeline(&set->crtc->mode);
9124
		drm_mode_debug_printmodeline(set->mode);
9150
		drm_mode_debug_printmodeline(set->mode);
9125
		config->mode_changed = true;
9151
		config->mode_changed = true;
9126
	}
9152
	}
9127
 
9153
 
9128
	DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
9154
	DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
9129
			set->crtc->base.id, config->mode_changed, config->fb_changed);
9155
			set->crtc->base.id, config->mode_changed, config->fb_changed);
9130
}
9156
}
9131
 
9157
 
9132
static int
9158
static int
9133
intel_modeset_stage_output_state(struct drm_device *dev,
9159
intel_modeset_stage_output_state(struct drm_device *dev,
9134
				 struct drm_mode_set *set,
9160
				 struct drm_mode_set *set,
9135
				 struct intel_set_config *config)
9161
				 struct intel_set_config *config)
9136
{
9162
{
9137
	struct drm_crtc *new_crtc;
9163
	struct drm_crtc *new_crtc;
9138
	struct intel_connector *connector;
9164
	struct intel_connector *connector;
9139
	struct intel_encoder *encoder;
9165
	struct intel_encoder *encoder;
9140
	int ro;
9166
	int ro;
9141
 
9167
 
9142
	/* The upper layers ensure that we either disable a crtc or have a list
9168
	/* The upper layers ensure that we either disable a crtc or have a list
9143
	 * of connectors. For paranoia, double-check this. */
9169
	 * of connectors. For paranoia, double-check this. */
9144
	WARN_ON(!set->fb && (set->num_connectors != 0));
9170
	WARN_ON(!set->fb && (set->num_connectors != 0));
9145
	WARN_ON(set->fb && (set->num_connectors == 0));
9171
	WARN_ON(set->fb && (set->num_connectors == 0));
9146
 
9172
 
9147
	list_for_each_entry(connector, &dev->mode_config.connector_list,
9173
	list_for_each_entry(connector, &dev->mode_config.connector_list,
9148
			    base.head) {
9174
			    base.head) {
9149
		/* Otherwise traverse passed in connector list and get encoders
9175
		/* Otherwise traverse passed in connector list and get encoders
9150
		 * for them. */
9176
		 * for them. */
9151
		for (ro = 0; ro < set->num_connectors; ro++) {
9177
		for (ro = 0; ro < set->num_connectors; ro++) {
9152
			if (set->connectors[ro] == &connector->base) {
9178
			if (set->connectors[ro] == &connector->base) {
9153
				connector->new_encoder = connector->encoder;
9179
				connector->new_encoder = connector->encoder;
9154
				break;
9180
				break;
9155
			}
9181
			}
9156
		}
9182
		}
9157
 
9183
 
9158
		/* If we disable the crtc, disable all its connectors. Also, if
9184
		/* If we disable the crtc, disable all its connectors. Also, if
9159
		 * the connector is on the changing crtc but not on the new
9185
		 * the connector is on the changing crtc but not on the new
9160
		 * connector list, disable it. */
9186
		 * connector list, disable it. */
9161
		if ((!set->fb || ro == set->num_connectors) &&
9187
		if ((!set->fb || ro == set->num_connectors) &&
9162
		    connector->base.encoder &&
9188
		    connector->base.encoder &&
9163
		    connector->base.encoder->crtc == set->crtc) {
9189
		    connector->base.encoder->crtc == set->crtc) {
9164
			connector->new_encoder = NULL;
9190
			connector->new_encoder = NULL;
9165
 
9191
 
9166
			DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
9192
			DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
9167
				connector->base.base.id,
9193
				connector->base.base.id,
9168
				drm_get_connector_name(&connector->base));
9194
				drm_get_connector_name(&connector->base));
9169
		}
9195
		}
9170
 
9196
 
9171
 
9197
 
9172
		if (&connector->new_encoder->base != connector->base.encoder) {
9198
		if (&connector->new_encoder->base != connector->base.encoder) {
9173
			DRM_DEBUG_KMS("encoder changed, full mode switch\n");
9199
			DRM_DEBUG_KMS("encoder changed, full mode switch\n");
9174
			config->mode_changed = true;
9200
			config->mode_changed = true;
9175
		}
9201
		}
9176
	}
9202
	}
9177
	/* connector->new_encoder is now updated for all connectors. */
9203
	/* connector->new_encoder is now updated for all connectors. */
9178
 
9204
 
9179
	/* Update crtc of enabled connectors. */
9205
	/* Update crtc of enabled connectors. */
9180
	list_for_each_entry(connector, &dev->mode_config.connector_list,
9206
	list_for_each_entry(connector, &dev->mode_config.connector_list,
9181
			    base.head) {
9207
			    base.head) {
9182
		if (!connector->new_encoder)
9208
		if (!connector->new_encoder)
9183
			continue;
9209
			continue;
9184
 
9210
 
9185
		new_crtc = connector->new_encoder->base.crtc;
9211
		new_crtc = connector->new_encoder->base.crtc;
9186
 
9212
 
9187
		for (ro = 0; ro < set->num_connectors; ro++) {
9213
		for (ro = 0; ro < set->num_connectors; ro++) {
9188
			if (set->connectors[ro] == &connector->base)
9214
			if (set->connectors[ro] == &connector->base)
9189
				new_crtc = set->crtc;
9215
				new_crtc = set->crtc;
9190
		}
9216
		}
9191
 
9217
 
9192
		/* Make sure the new CRTC will work with the encoder */
9218
		/* Make sure the new CRTC will work with the encoder */
9193
		if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
9219
		if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
9194
					   new_crtc)) {
9220
					   new_crtc)) {
9195
			return -EINVAL;
9221
			return -EINVAL;
9196
		}
9222
		}
9197
		connector->encoder->new_crtc = to_intel_crtc(new_crtc);
9223
		connector->encoder->new_crtc = to_intel_crtc(new_crtc);
9198
 
9224
 
9199
		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
9225
		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
9200
			connector->base.base.id,
9226
			connector->base.base.id,
9201
			drm_get_connector_name(&connector->base),
9227
			drm_get_connector_name(&connector->base),
9202
			new_crtc->base.id);
9228
			new_crtc->base.id);
9203
	}
9229
	}
9204
 
9230
 
9205
	/* Check for any encoders that needs to be disabled. */
9231
	/* Check for any encoders that needs to be disabled. */
9206
	list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9232
	list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9207
			    base.head) {
9233
			    base.head) {
9208
		list_for_each_entry(connector,
9234
		list_for_each_entry(connector,
9209
				    &dev->mode_config.connector_list,
9235
				    &dev->mode_config.connector_list,
9210
				    base.head) {
9236
				    base.head) {
9211
			if (connector->new_encoder == encoder) {
9237
			if (connector->new_encoder == encoder) {
9212
				WARN_ON(!connector->new_encoder->new_crtc);
9238
				WARN_ON(!connector->new_encoder->new_crtc);
9213
 
9239
 
9214
				goto next_encoder;
9240
				goto next_encoder;
9215
			}
9241
			}
9216
		}
9242
		}
9217
		encoder->new_crtc = NULL;
9243
		encoder->new_crtc = NULL;
9218
next_encoder:
9244
next_encoder:
9219
		/* Only now check for crtc changes so we don't miss encoders
9245
		/* Only now check for crtc changes so we don't miss encoders
9220
		 * that will be disabled. */
9246
		 * that will be disabled. */
9221
		if (&encoder->new_crtc->base != encoder->base.crtc) {
9247
		if (&encoder->new_crtc->base != encoder->base.crtc) {
9222
			DRM_DEBUG_KMS("crtc changed, full mode switch\n");
9248
			DRM_DEBUG_KMS("crtc changed, full mode switch\n");
9223
			config->mode_changed = true;
9249
			config->mode_changed = true;
9224
		}
9250
		}
9225
	}
9251
	}
9226
	/* Now we've also updated encoder->new_crtc for all encoders. */
9252
	/* Now we've also updated encoder->new_crtc for all encoders. */
9227
 
9253
 
9228
	return 0;
9254
	return 0;
9229
}
9255
}
9230
 
9256
 
9231
static int intel_crtc_set_config(struct drm_mode_set *set)
9257
static int intel_crtc_set_config(struct drm_mode_set *set)
9232
{
9258
{
9233
	struct drm_device *dev;
9259
	struct drm_device *dev;
9234
	struct drm_mode_set save_set;
9260
	struct drm_mode_set save_set;
9235
	struct intel_set_config *config;
9261
	struct intel_set_config *config;
9236
	int ret;
9262
	int ret;
9237
 
9263
 
9238
	BUG_ON(!set);
9264
	BUG_ON(!set);
9239
	BUG_ON(!set->crtc);
9265
	BUG_ON(!set->crtc);
9240
	BUG_ON(!set->crtc->helper_private);
9266
	BUG_ON(!set->crtc->helper_private);
9241
 
9267
 
9242
	/* Enforce sane interface api - has been abused by the fb helper. */
9268
	/* Enforce sane interface api - has been abused by the fb helper. */
9243
	BUG_ON(!set->mode && set->fb);
9269
	BUG_ON(!set->mode && set->fb);
9244
	BUG_ON(set->fb && set->num_connectors == 0);
9270
	BUG_ON(set->fb && set->num_connectors == 0);
9245
 
9271
 
9246
	if (set->fb) {
9272
	if (set->fb) {
9247
		DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
9273
		DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
9248
				set->crtc->base.id, set->fb->base.id,
9274
				set->crtc->base.id, set->fb->base.id,
9249
				(int)set->num_connectors, set->x, set->y);
9275
				(int)set->num_connectors, set->x, set->y);
9250
	} else {
9276
	} else {
9251
		DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
9277
		DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
9252
	}
9278
	}
9253
 
9279
 
9254
	dev = set->crtc->dev;
9280
	dev = set->crtc->dev;
9255
 
9281
 
9256
	ret = -ENOMEM;
9282
	ret = -ENOMEM;
9257
	config = kzalloc(sizeof(*config), GFP_KERNEL);
9283
	config = kzalloc(sizeof(*config), GFP_KERNEL);
9258
	if (!config)
9284
	if (!config)
9259
		goto out_config;
9285
		goto out_config;
9260
 
9286
 
9261
	ret = intel_set_config_save_state(dev, config);
9287
	ret = intel_set_config_save_state(dev, config);
9262
	if (ret)
9288
	if (ret)
9263
		goto out_config;
9289
		goto out_config;
9264
 
9290
 
9265
	save_set.crtc = set->crtc;
9291
	save_set.crtc = set->crtc;
9266
	save_set.mode = &set->crtc->mode;
9292
	save_set.mode = &set->crtc->mode;
9267
	save_set.x = set->crtc->x;
9293
	save_set.x = set->crtc->x;
9268
	save_set.y = set->crtc->y;
9294
	save_set.y = set->crtc->y;
9269
	save_set.fb = set->crtc->fb;
9295
	save_set.fb = set->crtc->fb;
9270
 
9296
 
9271
	/* Compute whether we need a full modeset, only an fb base update or no
9297
	/* Compute whether we need a full modeset, only an fb base update or no
9272
	 * change at all. In the future we might also check whether only the
9298
	 * change at all. In the future we might also check whether only the
9273
	 * mode changed, e.g. for LVDS where we only change the panel fitter in
9299
	 * mode changed, e.g. for LVDS where we only change the panel fitter in
9274
	 * such cases. */
9300
	 * such cases. */
9275
	intel_set_config_compute_mode_changes(set, config);
9301
	intel_set_config_compute_mode_changes(set, config);
9276
 
9302
 
9277
	ret = intel_modeset_stage_output_state(dev, set, config);
9303
	ret = intel_modeset_stage_output_state(dev, set, config);
9278
	if (ret)
9304
	if (ret)
9279
		goto fail;
9305
		goto fail;
9280
 
9306
 
9281
	if (config->mode_changed) {
9307
	if (config->mode_changed) {
9282
		ret = intel_set_mode(set->crtc, set->mode,
9308
		ret = intel_set_mode(set->crtc, set->mode,
9283
				     set->x, set->y, set->fb);
9309
				     set->x, set->y, set->fb);
9284
	} else if (config->fb_changed) {
9310
	} else if (config->fb_changed) {
9285
//       intel_crtc_wait_for_pending_flips(set->crtc);
9311
//       intel_crtc_wait_for_pending_flips(set->crtc);
9286
 
9312
 
9287
		ret = intel_pipe_set_base(set->crtc,
9313
		ret = intel_pipe_set_base(set->crtc,
9288
					  set->x, set->y, set->fb);
9314
					  set->x, set->y, set->fb);
9289
	}
9315
	}
9290
 
9316
 
9291
	if (ret) {
9317
	if (ret) {
9292
		DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
9318
		DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
9293
			  set->crtc->base.id, ret);
9319
			  set->crtc->base.id, ret);
9294
fail:
9320
fail:
9295
	intel_set_config_restore_state(dev, config);
9321
	intel_set_config_restore_state(dev, config);
9296
 
9322
 
9297
	/* Try to restore the config */
9323
	/* Try to restore the config */
9298
	if (config->mode_changed &&
9324
	if (config->mode_changed &&
9299
	    intel_set_mode(save_set.crtc, save_set.mode,
9325
	    intel_set_mode(save_set.crtc, save_set.mode,
9300
			    save_set.x, save_set.y, save_set.fb))
9326
			    save_set.x, save_set.y, save_set.fb))
9301
		DRM_ERROR("failed to restore config after modeset failure\n");
9327
		DRM_ERROR("failed to restore config after modeset failure\n");
9302
	}
9328
	}
9303
 
9329
 
9304
out_config:
9330
out_config:
9305
	intel_set_config_free(config);
9331
	intel_set_config_free(config);
9306
	return ret;
9332
	return ret;
9307
}
9333
}
9308
 
9334
 
9309
static const struct drm_crtc_funcs intel_crtc_funcs = {
9335
static const struct drm_crtc_funcs intel_crtc_funcs = {
9310
//	.cursor_set = intel_crtc_cursor_set,
9336
//	.cursor_set = intel_crtc_cursor_set,
9311
//	.cursor_move = intel_crtc_cursor_move,
9337
//	.cursor_move = intel_crtc_cursor_move,
9312
	.gamma_set = intel_crtc_gamma_set,
9338
	.gamma_set = intel_crtc_gamma_set,
9313
	.set_config = intel_crtc_set_config,
9339
	.set_config = intel_crtc_set_config,
9314
	.destroy = intel_crtc_destroy,
9340
	.destroy = intel_crtc_destroy,
9315
//	.page_flip = intel_crtc_page_flip,
9341
//	.page_flip = intel_crtc_page_flip,
9316
};
9342
};
9317
 
9343
 
9318
static void intel_cpu_pll_init(struct drm_device *dev)
9344
static void intel_cpu_pll_init(struct drm_device *dev)
9319
{
9345
{
9320
	if (HAS_DDI(dev))
9346
	if (HAS_DDI(dev))
9321
		intel_ddi_pll_init(dev);
9347
		intel_ddi_pll_init(dev);
9322
}
9348
}
9323
 
9349
 
9324
static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
9350
static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
9325
				      struct intel_shared_dpll *pll,
9351
				      struct intel_shared_dpll *pll,
9326
				      struct intel_dpll_hw_state *hw_state)
9352
				      struct intel_dpll_hw_state *hw_state)
9327
{
9353
{
9328
	uint32_t val;
9354
	uint32_t val;
9329
 
9355
 
9330
	val = I915_READ(PCH_DPLL(pll->id));
9356
	val = I915_READ(PCH_DPLL(pll->id));
9331
	hw_state->dpll = val;
9357
	hw_state->dpll = val;
9332
	hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
9358
	hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
9333
	hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
9359
	hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
9334
 
9360
 
9335
	return val & DPLL_VCO_ENABLE;
9361
	return val & DPLL_VCO_ENABLE;
9336
}
9362
}
9337
 
9363
 
9338
static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
9364
static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
9339
				  struct intel_shared_dpll *pll)
9365
				  struct intel_shared_dpll *pll)
9340
{
9366
{
9341
	I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
9367
	I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
9342
	I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
9368
	I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
9343
}
9369
}
9344
 
9370
 
9345
static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
9371
static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
9346
				struct intel_shared_dpll *pll)
9372
				struct intel_shared_dpll *pll)
9347
{
9373
{
9348
	/* PCH refclock must be enabled first */
9374
	/* PCH refclock must be enabled first */
9349
	assert_pch_refclk_enabled(dev_priv);
9375
	assert_pch_refclk_enabled(dev_priv);
9350
 
9376
 
9351
	I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
9377
	I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
9352
 
9378
 
9353
	/* Wait for the clocks to stabilize. */
9379
	/* Wait for the clocks to stabilize. */
9354
	POSTING_READ(PCH_DPLL(pll->id));
9380
	POSTING_READ(PCH_DPLL(pll->id));
9355
	udelay(150);
9381
	udelay(150);
9356
 
9382
 
9357
	/* The pixel multiplier can only be updated once the
9383
	/* The pixel multiplier can only be updated once the
9358
	 * DPLL is enabled and the clocks are stable.
9384
	 * DPLL is enabled and the clocks are stable.
9359
	 *
9385
	 *
9360
	 * So write it again.
9386
	 * So write it again.
9361
	 */
9387
	 */
9362
	I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
9388
	I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
9363
	POSTING_READ(PCH_DPLL(pll->id));
9389
	POSTING_READ(PCH_DPLL(pll->id));
9364
	udelay(200);
9390
	udelay(200);
9365
}
9391
}
9366
 
9392
 
9367
static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
9393
static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
9368
				 struct intel_shared_dpll *pll)
9394
				 struct intel_shared_dpll *pll)
9369
{
9395
{
9370
	struct drm_device *dev = dev_priv->dev;
9396
	struct drm_device *dev = dev_priv->dev;
9371
	struct intel_crtc *crtc;
9397
	struct intel_crtc *crtc;
9372
 
9398
 
9373
	/* Make sure no transcoder isn't still depending on us. */
9399
	/* Make sure no transcoder isn't still depending on us. */
9374
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
9400
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
9375
		if (intel_crtc_to_shared_dpll(crtc) == pll)
9401
		if (intel_crtc_to_shared_dpll(crtc) == pll)
9376
			assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
9402
			assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
9377
	}
9403
	}
9378
 
9404
 
9379
	I915_WRITE(PCH_DPLL(pll->id), 0);
9405
	I915_WRITE(PCH_DPLL(pll->id), 0);
9380
	POSTING_READ(PCH_DPLL(pll->id));
9406
	POSTING_READ(PCH_DPLL(pll->id));
9381
	udelay(200);
9407
	udelay(200);
9382
}
9408
}
9383
 
9409
 
9384
static char *ibx_pch_dpll_names[] = {
9410
static char *ibx_pch_dpll_names[] = {
9385
	"PCH DPLL A",
9411
	"PCH DPLL A",
9386
	"PCH DPLL B",
9412
	"PCH DPLL B",
9387
};
9413
};
9388
 
9414
 
9389
static void ibx_pch_dpll_init(struct drm_device *dev)
9415
static void ibx_pch_dpll_init(struct drm_device *dev)
9390
{
9416
{
9391
	struct drm_i915_private *dev_priv = dev->dev_private;
9417
	struct drm_i915_private *dev_priv = dev->dev_private;
9392
	int i;
9418
	int i;
9393
 
9419
 
9394
	dev_priv->num_shared_dpll = 2;
9420
	dev_priv->num_shared_dpll = 2;
9395
 
9421
 
9396
	for (i = 0; i < dev_priv->num_shared_dpll; i++) {
9422
	for (i = 0; i < dev_priv->num_shared_dpll; i++) {
9397
		dev_priv->shared_dplls[i].id = i;
9423
		dev_priv->shared_dplls[i].id = i;
9398
		dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
9424
		dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
9399
		dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
9425
		dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
9400
		dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
9426
		dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
9401
		dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
9427
		dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
9402
		dev_priv->shared_dplls[i].get_hw_state =
9428
		dev_priv->shared_dplls[i].get_hw_state =
9403
			ibx_pch_dpll_get_hw_state;
9429
			ibx_pch_dpll_get_hw_state;
9404
	}
9430
	}
9405
}
9431
}
9406
 
9432
 
9407
static void intel_shared_dpll_init(struct drm_device *dev)
9433
static void intel_shared_dpll_init(struct drm_device *dev)
9408
{
9434
{
9409
	struct drm_i915_private *dev_priv = dev->dev_private;
9435
	struct drm_i915_private *dev_priv = dev->dev_private;
9410
 
9436
 
9411
	if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
9437
	if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
9412
		ibx_pch_dpll_init(dev);
9438
		ibx_pch_dpll_init(dev);
9413
	else
9439
	else
9414
		dev_priv->num_shared_dpll = 0;
9440
		dev_priv->num_shared_dpll = 0;
9415
 
9441
 
9416
	BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
9442
	BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
9417
	DRM_DEBUG_KMS("%i shared PLLs initialized\n",
9443
	DRM_DEBUG_KMS("%i shared PLLs initialized\n",
9418
		      dev_priv->num_shared_dpll);
9444
		      dev_priv->num_shared_dpll);
9419
}
9445
}
9420
 
9446
 
9421
static void intel_crtc_init(struct drm_device *dev, int pipe)
9447
static void intel_crtc_init(struct drm_device *dev, int pipe)
9422
{
9448
{
9423
	drm_i915_private_t *dev_priv = dev->dev_private;
9449
	drm_i915_private_t *dev_priv = dev->dev_private;
9424
	struct intel_crtc *intel_crtc;
9450
	struct intel_crtc *intel_crtc;
9425
	int i;
9451
	int i;
9426
 
9452
 
9427
	intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
9453
	intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
9428
	if (intel_crtc == NULL)
9454
	if (intel_crtc == NULL)
9429
		return;
9455
		return;
9430
 
9456
 
9431
	drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
9457
	drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
9432
 
9458
 
9433
	drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
9459
	drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
9434
	for (i = 0; i < 256; i++) {
9460
	for (i = 0; i < 256; i++) {
9435
		intel_crtc->lut_r[i] = i;
9461
		intel_crtc->lut_r[i] = i;
9436
		intel_crtc->lut_g[i] = i;
9462
		intel_crtc->lut_g[i] = i;
9437
		intel_crtc->lut_b[i] = i;
9463
		intel_crtc->lut_b[i] = i;
9438
	}
9464
	}
9439
 
9465
 
9440
	/* Swap pipes & planes for FBC on pre-965 */
9466
	/* Swap pipes & planes for FBC on pre-965 */
9441
	intel_crtc->pipe = pipe;
9467
	intel_crtc->pipe = pipe;
9442
	intel_crtc->plane = pipe;
9468
	intel_crtc->plane = pipe;
9443
	if (IS_MOBILE(dev) && IS_GEN3(dev)) {
9469
	if (IS_MOBILE(dev) && IS_GEN3(dev)) {
9444
		DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
9470
		DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
9445
		intel_crtc->plane = !pipe;
9471
		intel_crtc->plane = !pipe;
9446
	}
9472
	}
9447
 
9473
 
9448
	BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
9474
	BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
9449
	       dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
9475
	       dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
9450
	dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
9476
	dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
9451
	dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
9477
	dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
9452
 
9478
 
9453
	drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
9479
	drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
9454
}
9480
}
9455
 
9481
 
9456
int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
9482
int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
9457
				struct drm_file *file)
9483
				struct drm_file *file)
9458
{
9484
{
9459
	struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
9485
	struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
9460
	struct drm_mode_object *drmmode_obj;
9486
	struct drm_mode_object *drmmode_obj;
9461
	struct intel_crtc *crtc;
9487
	struct intel_crtc *crtc;
9462
 
9488
 
9463
	if (!drm_core_check_feature(dev, DRIVER_MODESET))
9489
	if (!drm_core_check_feature(dev, DRIVER_MODESET))
9464
		return -ENODEV;
9490
		return -ENODEV;
9465
 
9491
 
9466
	drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
9492
	drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
9467
			DRM_MODE_OBJECT_CRTC);
9493
			DRM_MODE_OBJECT_CRTC);
9468
 
9494
 
9469
	if (!drmmode_obj) {
9495
	if (!drmmode_obj) {
9470
		DRM_ERROR("no such CRTC id\n");
9496
		DRM_ERROR("no such CRTC id\n");
9471
		return -EINVAL;
9497
		return -EINVAL;
9472
	}
9498
	}
9473
 
9499
 
9474
	crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
9500
	crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
9475
	pipe_from_crtc_id->pipe = crtc->pipe;
9501
	pipe_from_crtc_id->pipe = crtc->pipe;
9476
 
9502
 
9477
	return 0;
9503
	return 0;
9478
}
9504
}
9479
 
9505
 
9480
static int intel_encoder_clones(struct intel_encoder *encoder)
9506
static int intel_encoder_clones(struct intel_encoder *encoder)
9481
{
9507
{
9482
	struct drm_device *dev = encoder->base.dev;
9508
	struct drm_device *dev = encoder->base.dev;
9483
	struct intel_encoder *source_encoder;
9509
	struct intel_encoder *source_encoder;
9484
	int index_mask = 0;
9510
	int index_mask = 0;
9485
	int entry = 0;
9511
	int entry = 0;
9486
 
9512
 
9487
	list_for_each_entry(source_encoder,
9513
	list_for_each_entry(source_encoder,
9488
			    &dev->mode_config.encoder_list, base.head) {
9514
			    &dev->mode_config.encoder_list, base.head) {
9489
 
9515
 
9490
		if (encoder == source_encoder)
9516
		if (encoder == source_encoder)
9491
			index_mask |= (1 << entry);
9517
			index_mask |= (1 << entry);
9492
 
9518
 
9493
		/* Intel hw has only one MUX where enocoders could be cloned. */
9519
		/* Intel hw has only one MUX where enocoders could be cloned. */
9494
		if (encoder->cloneable && source_encoder->cloneable)
9520
		if (encoder->cloneable && source_encoder->cloneable)
9495
			index_mask |= (1 << entry);
9521
			index_mask |= (1 << entry);
9496
 
9522
 
9497
		entry++;
9523
		entry++;
9498
	}
9524
	}
9499
 
9525
 
9500
	return index_mask;
9526
	return index_mask;
9501
}
9527
}
9502
 
9528
 
9503
static bool has_edp_a(struct drm_device *dev)
9529
static bool has_edp_a(struct drm_device *dev)
9504
{
9530
{
9505
	struct drm_i915_private *dev_priv = dev->dev_private;
9531
	struct drm_i915_private *dev_priv = dev->dev_private;
9506
 
9532
 
9507
	if (!IS_MOBILE(dev))
9533
	if (!IS_MOBILE(dev))
9508
		return false;
9534
		return false;
9509
 
9535
 
9510
	if ((I915_READ(DP_A) & DP_DETECTED) == 0)
9536
	if ((I915_READ(DP_A) & DP_DETECTED) == 0)
9511
		return false;
9537
		return false;
9512
 
9538
 
9513
	if (IS_GEN5(dev) &&
9539
	if (IS_GEN5(dev) &&
9514
	    (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
9540
	    (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
9515
		return false;
9541
		return false;
9516
 
9542
 
9517
	return true;
9543
	return true;
9518
}
9544
}
9519
 
9545
 
9520
static void intel_setup_outputs(struct drm_device *dev)
9546
static void intel_setup_outputs(struct drm_device *dev)
9521
{
9547
{
9522
	struct drm_i915_private *dev_priv = dev->dev_private;
9548
	struct drm_i915_private *dev_priv = dev->dev_private;
9523
	struct intel_encoder *encoder;
9549
	struct intel_encoder *encoder;
9524
	bool dpd_is_edp = false;
9550
	bool dpd_is_edp = false;
9525
 
9551
 
9526
	intel_lvds_init(dev);
9552
	intel_lvds_init(dev);
9527
 
9553
 
9528
	if (!IS_ULT(dev))
9554
	if (!IS_ULT(dev))
9529
	intel_crt_init(dev);
9555
	intel_crt_init(dev);
9530
 
9556
 
9531
	if (HAS_DDI(dev)) {
9557
	if (HAS_DDI(dev)) {
9532
		int found;
9558
		int found;
9533
 
9559
 
9534
		/* Haswell uses DDI functions to detect digital outputs */
9560
		/* Haswell uses DDI functions to detect digital outputs */
9535
		found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
9561
		found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
9536
		/* DDI A only supports eDP */
9562
		/* DDI A only supports eDP */
9537
		if (found)
9563
		if (found)
9538
			intel_ddi_init(dev, PORT_A);
9564
			intel_ddi_init(dev, PORT_A);
9539
 
9565
 
9540
		/* DDI B, C and D detection is indicated by the SFUSE_STRAP
9566
		/* DDI B, C and D detection is indicated by the SFUSE_STRAP
9541
		 * register */
9567
		 * register */
9542
		found = I915_READ(SFUSE_STRAP);
9568
		found = I915_READ(SFUSE_STRAP);
9543
 
9569
 
9544
		if (found & SFUSE_STRAP_DDIB_DETECTED)
9570
		if (found & SFUSE_STRAP_DDIB_DETECTED)
9545
			intel_ddi_init(dev, PORT_B);
9571
			intel_ddi_init(dev, PORT_B);
9546
		if (found & SFUSE_STRAP_DDIC_DETECTED)
9572
		if (found & SFUSE_STRAP_DDIC_DETECTED)
9547
			intel_ddi_init(dev, PORT_C);
9573
			intel_ddi_init(dev, PORT_C);
9548
		if (found & SFUSE_STRAP_DDID_DETECTED)
9574
		if (found & SFUSE_STRAP_DDID_DETECTED)
9549
			intel_ddi_init(dev, PORT_D);
9575
			intel_ddi_init(dev, PORT_D);
9550
	} else if (HAS_PCH_SPLIT(dev)) {
9576
	} else if (HAS_PCH_SPLIT(dev)) {
9551
		int found;
9577
		int found;
9552
		dpd_is_edp = intel_dpd_is_edp(dev);
9578
		dpd_is_edp = intel_dpd_is_edp(dev);
9553
 
9579
 
9554
		if (has_edp_a(dev))
9580
		if (has_edp_a(dev))
9555
			intel_dp_init(dev, DP_A, PORT_A);
9581
			intel_dp_init(dev, DP_A, PORT_A);
9556
 
9582
 
9557
		if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
9583
		if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
9558
			/* PCH SDVOB multiplex with HDMIB */
9584
			/* PCH SDVOB multiplex with HDMIB */
9559
			found = intel_sdvo_init(dev, PCH_SDVOB, true);
9585
			found = intel_sdvo_init(dev, PCH_SDVOB, true);
9560
			if (!found)
9586
			if (!found)
9561
				intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
9587
				intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
9562
			if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
9588
			if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
9563
				intel_dp_init(dev, PCH_DP_B, PORT_B);
9589
				intel_dp_init(dev, PCH_DP_B, PORT_B);
9564
		}
9590
		}
9565
 
9591
 
9566
		if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
9592
		if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
9567
			intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
9593
			intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
9568
 
9594
 
9569
		if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
9595
		if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
9570
			intel_hdmi_init(dev, PCH_HDMID, PORT_D);
9596
			intel_hdmi_init(dev, PCH_HDMID, PORT_D);
9571
 
9597
 
9572
		if (I915_READ(PCH_DP_C) & DP_DETECTED)
9598
		if (I915_READ(PCH_DP_C) & DP_DETECTED)
9573
			intel_dp_init(dev, PCH_DP_C, PORT_C);
9599
			intel_dp_init(dev, PCH_DP_C, PORT_C);
9574
 
9600
 
9575
		if (I915_READ(PCH_DP_D) & DP_DETECTED)
9601
		if (I915_READ(PCH_DP_D) & DP_DETECTED)
9576
			intel_dp_init(dev, PCH_DP_D, PORT_D);
9602
			intel_dp_init(dev, PCH_DP_D, PORT_D);
9577
	} else if (IS_VALLEYVIEW(dev)) {
9603
	} else if (IS_VALLEYVIEW(dev)) {
9578
		/* Check for built-in panel first. Shares lanes with HDMI on SDVOC */
9604
		/* Check for built-in panel first. Shares lanes with HDMI on SDVOC */
9579
		if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
9605
		if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
9580
			intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
9606
			intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
9581
					PORT_C);
9607
					PORT_C);
9582
		if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
9608
		if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
9583
				intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C,
9609
				intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C,
9584
					      PORT_C);
9610
					      PORT_C);
9585
		}
9611
		}
9586
 
9612
 
9587
		if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
9613
		if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
9588
			intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
9614
			intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
9589
					PORT_B);
9615
					PORT_B);
9590
			if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
9616
			if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
9591
				intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
9617
				intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
9592
		}
9618
		}
9593
	} else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
9619
	} else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
9594
		bool found = false;
9620
		bool found = false;
9595
 
9621
 
9596
		if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
9622
		if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
9597
			DRM_DEBUG_KMS("probing SDVOB\n");
9623
			DRM_DEBUG_KMS("probing SDVOB\n");
9598
			found = intel_sdvo_init(dev, GEN3_SDVOB, true);
9624
			found = intel_sdvo_init(dev, GEN3_SDVOB, true);
9599
			if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
9625
			if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
9600
				DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
9626
				DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
9601
				intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
9627
				intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
9602
			}
9628
			}
9603
 
9629
 
9604
			if (!found && SUPPORTS_INTEGRATED_DP(dev))
9630
			if (!found && SUPPORTS_INTEGRATED_DP(dev))
9605
				intel_dp_init(dev, DP_B, PORT_B);
9631
				intel_dp_init(dev, DP_B, PORT_B);
9606
			}
9632
			}
9607
 
9633
 
9608
		/* Before G4X SDVOC doesn't have its own detect register */
9634
		/* Before G4X SDVOC doesn't have its own detect register */
9609
 
9635
 
9610
		if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
9636
		if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
9611
			DRM_DEBUG_KMS("probing SDVOC\n");
9637
			DRM_DEBUG_KMS("probing SDVOC\n");
9612
			found = intel_sdvo_init(dev, GEN3_SDVOC, false);
9638
			found = intel_sdvo_init(dev, GEN3_SDVOC, false);
9613
		}
9639
		}
9614
 
9640
 
9615
		if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
9641
		if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
9616
 
9642
 
9617
			if (SUPPORTS_INTEGRATED_HDMI(dev)) {
9643
			if (SUPPORTS_INTEGRATED_HDMI(dev)) {
9618
				DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
9644
				DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
9619
				intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
9645
				intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
9620
			}
9646
			}
9621
			if (SUPPORTS_INTEGRATED_DP(dev))
9647
			if (SUPPORTS_INTEGRATED_DP(dev))
9622
				intel_dp_init(dev, DP_C, PORT_C);
9648
				intel_dp_init(dev, DP_C, PORT_C);
9623
			}
9649
			}
9624
 
9650
 
9625
		if (SUPPORTS_INTEGRATED_DP(dev) &&
9651
		if (SUPPORTS_INTEGRATED_DP(dev) &&
9626
		    (I915_READ(DP_D) & DP_DETECTED))
9652
		    (I915_READ(DP_D) & DP_DETECTED))
9627
			intel_dp_init(dev, DP_D, PORT_D);
9653
			intel_dp_init(dev, DP_D, PORT_D);
9628
	} else if (IS_GEN2(dev))
9654
	} else if (IS_GEN2(dev))
9629
		intel_dvo_init(dev);
9655
		intel_dvo_init(dev);
9630
 
9656
 
9631
//   if (SUPPORTS_TV(dev))
9657
//   if (SUPPORTS_TV(dev))
9632
//       intel_tv_init(dev);
9658
//       intel_tv_init(dev);
9633
 
9659
 
9634
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
9660
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
9635
		encoder->base.possible_crtcs = encoder->crtc_mask;
9661
		encoder->base.possible_crtcs = encoder->crtc_mask;
9636
		encoder->base.possible_clones =
9662
		encoder->base.possible_clones =
9637
			intel_encoder_clones(encoder);
9663
			intel_encoder_clones(encoder);
9638
	}
9664
	}
9639
 
9665
 
9640
	intel_init_pch_refclk(dev);
9666
	intel_init_pch_refclk(dev);
9641
 
9667
 
9642
	drm_helper_move_panel_connectors_to_head(dev);
9668
	drm_helper_move_panel_connectors_to_head(dev);
9643
}
9669
}
9644
 
9670
 
9645
 
9671
 
9646
 
9672
 
9647
static const struct drm_framebuffer_funcs intel_fb_funcs = {
9673
static const struct drm_framebuffer_funcs intel_fb_funcs = {
9648
//	.destroy = intel_user_framebuffer_destroy,
9674
//	.destroy = intel_user_framebuffer_destroy,
9649
//	.create_handle = intel_user_framebuffer_create_handle,
9675
//	.create_handle = intel_user_framebuffer_create_handle,
9650
};
9676
};
9651
 
9677
 
9652
int intel_framebuffer_init(struct drm_device *dev,
9678
int intel_framebuffer_init(struct drm_device *dev,
9653
			   struct intel_framebuffer *intel_fb,
9679
			   struct intel_framebuffer *intel_fb,
9654
			   struct drm_mode_fb_cmd2 *mode_cmd,
9680
			   struct drm_mode_fb_cmd2 *mode_cmd,
9655
			   struct drm_i915_gem_object *obj)
9681
			   struct drm_i915_gem_object *obj)
9656
{
9682
{
9657
	int pitch_limit;
9683
	int pitch_limit;
9658
	int ret;
9684
	int ret;
9659
 
9685
 
9660
	if (obj->tiling_mode == I915_TILING_Y) {
9686
	if (obj->tiling_mode == I915_TILING_Y) {
9661
		DRM_DEBUG("hardware does not support tiling Y\n");
9687
		DRM_DEBUG("hardware does not support tiling Y\n");
9662
		return -EINVAL;
9688
		return -EINVAL;
9663
	}
9689
	}
9664
 
9690
 
9665
	if (mode_cmd->pitches[0] & 63) {
9691
	if (mode_cmd->pitches[0] & 63) {
9666
		DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
9692
		DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
9667
			  mode_cmd->pitches[0]);
9693
			  mode_cmd->pitches[0]);
9668
		return -EINVAL;
9694
		return -EINVAL;
9669
	}
9695
	}
9670
 
9696
 
9671
	if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
9697
	if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
9672
		pitch_limit = 32*1024;
9698
		pitch_limit = 32*1024;
9673
	} else if (INTEL_INFO(dev)->gen >= 4) {
9699
	} else if (INTEL_INFO(dev)->gen >= 4) {
9674
		if (obj->tiling_mode)
9700
		if (obj->tiling_mode)
9675
			pitch_limit = 16*1024;
9701
			pitch_limit = 16*1024;
9676
		else
9702
		else
9677
			pitch_limit = 32*1024;
9703
			pitch_limit = 32*1024;
9678
	} else if (INTEL_INFO(dev)->gen >= 3) {
9704
	} else if (INTEL_INFO(dev)->gen >= 3) {
9679
		if (obj->tiling_mode)
9705
		if (obj->tiling_mode)
9680
			pitch_limit = 8*1024;
9706
			pitch_limit = 8*1024;
9681
		else
9707
		else
9682
			pitch_limit = 16*1024;
9708
			pitch_limit = 16*1024;
9683
	} else
9709
	} else
9684
		/* XXX DSPC is limited to 4k tiled */
9710
		/* XXX DSPC is limited to 4k tiled */
9685
		pitch_limit = 8*1024;
9711
		pitch_limit = 8*1024;
9686
 
9712
 
9687
	if (mode_cmd->pitches[0] > pitch_limit) {
9713
	if (mode_cmd->pitches[0] > pitch_limit) {
9688
		DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
9714
		DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
9689
			  obj->tiling_mode ? "tiled" : "linear",
9715
			  obj->tiling_mode ? "tiled" : "linear",
9690
			  mode_cmd->pitches[0], pitch_limit);
9716
			  mode_cmd->pitches[0], pitch_limit);
9691
		return -EINVAL;
9717
		return -EINVAL;
9692
	}
9718
	}
9693
 
9719
 
9694
	if (obj->tiling_mode != I915_TILING_NONE &&
9720
	if (obj->tiling_mode != I915_TILING_NONE &&
9695
	    mode_cmd->pitches[0] != obj->stride) {
9721
	    mode_cmd->pitches[0] != obj->stride) {
9696
		DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
9722
		DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
9697
			  mode_cmd->pitches[0], obj->stride);
9723
			  mode_cmd->pitches[0], obj->stride);
9698
			return -EINVAL;
9724
			return -EINVAL;
9699
	}
9725
	}
9700
 
9726
 
9701
	/* Reject formats not supported by any plane early. */
9727
	/* Reject formats not supported by any plane early. */
9702
	switch (mode_cmd->pixel_format) {
9728
	switch (mode_cmd->pixel_format) {
9703
	case DRM_FORMAT_C8:
9729
	case DRM_FORMAT_C8:
9704
	case DRM_FORMAT_RGB565:
9730
	case DRM_FORMAT_RGB565:
9705
	case DRM_FORMAT_XRGB8888:
9731
	case DRM_FORMAT_XRGB8888:
9706
	case DRM_FORMAT_ARGB8888:
9732
	case DRM_FORMAT_ARGB8888:
9707
		break;
9733
		break;
9708
	case DRM_FORMAT_XRGB1555:
9734
	case DRM_FORMAT_XRGB1555:
9709
	case DRM_FORMAT_ARGB1555:
9735
	case DRM_FORMAT_ARGB1555:
9710
		if (INTEL_INFO(dev)->gen > 3) {
9736
		if (INTEL_INFO(dev)->gen > 3) {
9711
			DRM_DEBUG("unsupported pixel format: %s\n",
9737
			DRM_DEBUG("unsupported pixel format: %s\n",
9712
				  drm_get_format_name(mode_cmd->pixel_format));
9738
				  drm_get_format_name(mode_cmd->pixel_format));
9713
			return -EINVAL;
9739
			return -EINVAL;
9714
		}
9740
		}
9715
		break;
9741
		break;
9716
	case DRM_FORMAT_XBGR8888:
9742
	case DRM_FORMAT_XBGR8888:
9717
	case DRM_FORMAT_ABGR8888:
9743
	case DRM_FORMAT_ABGR8888:
9718
	case DRM_FORMAT_XRGB2101010:
9744
	case DRM_FORMAT_XRGB2101010:
9719
	case DRM_FORMAT_ARGB2101010:
9745
	case DRM_FORMAT_ARGB2101010:
9720
	case DRM_FORMAT_XBGR2101010:
9746
	case DRM_FORMAT_XBGR2101010:
9721
	case DRM_FORMAT_ABGR2101010:
9747
	case DRM_FORMAT_ABGR2101010:
9722
		if (INTEL_INFO(dev)->gen < 4) {
9748
		if (INTEL_INFO(dev)->gen < 4) {
9723
			DRM_DEBUG("unsupported pixel format: %s\n",
9749
			DRM_DEBUG("unsupported pixel format: %s\n",
9724
				  drm_get_format_name(mode_cmd->pixel_format));
9750
				  drm_get_format_name(mode_cmd->pixel_format));
9725
			return -EINVAL;
9751
			return -EINVAL;
9726
		}
9752
		}
9727
		break;
9753
		break;
9728
	case DRM_FORMAT_YUYV:
9754
	case DRM_FORMAT_YUYV:
9729
	case DRM_FORMAT_UYVY:
9755
	case DRM_FORMAT_UYVY:
9730
	case DRM_FORMAT_YVYU:
9756
	case DRM_FORMAT_YVYU:
9731
	case DRM_FORMAT_VYUY:
9757
	case DRM_FORMAT_VYUY:
9732
		if (INTEL_INFO(dev)->gen < 5) {
9758
		if (INTEL_INFO(dev)->gen < 5) {
9733
			DRM_DEBUG("unsupported pixel format: %s\n",
9759
			DRM_DEBUG("unsupported pixel format: %s\n",
9734
				  drm_get_format_name(mode_cmd->pixel_format));
9760
				  drm_get_format_name(mode_cmd->pixel_format));
9735
			return -EINVAL;
9761
			return -EINVAL;
9736
		}
9762
		}
9737
		break;
9763
		break;
9738
	default:
9764
	default:
9739
		DRM_DEBUG("unsupported pixel format: %s\n",
9765
		DRM_DEBUG("unsupported pixel format: %s\n",
9740
			  drm_get_format_name(mode_cmd->pixel_format));
9766
			  drm_get_format_name(mode_cmd->pixel_format));
9741
		return -EINVAL;
9767
		return -EINVAL;
9742
	}
9768
	}
9743
 
9769
 
9744
	/* FIXME need to adjust LINOFF/TILEOFF accordingly. */
9770
	/* FIXME need to adjust LINOFF/TILEOFF accordingly. */
9745
	if (mode_cmd->offsets[0] != 0)
9771
	if (mode_cmd->offsets[0] != 0)
9746
		return -EINVAL;
9772
		return -EINVAL;
9747
 
9773
 
9748
	drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
9774
	drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
9749
	intel_fb->obj = obj;
9775
	intel_fb->obj = obj;
9750
 
9776
 
9751
	ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
9777
	ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
9752
	if (ret) {
9778
	if (ret) {
9753
		DRM_ERROR("framebuffer init failed %d\n", ret);
9779
		DRM_ERROR("framebuffer init failed %d\n", ret);
9754
		return ret;
9780
		return ret;
9755
	}
9781
	}
9756
 
9782
 
9757
	return 0;
9783
	return 0;
9758
}
9784
}
9759
 
9785
 
9760
 
9786
 
9761
static const struct drm_mode_config_funcs intel_mode_funcs = {
9787
static const struct drm_mode_config_funcs intel_mode_funcs = {
9762
	.fb_create = NULL /*intel_user_framebuffer_create*/,
9788
	.fb_create = NULL /*intel_user_framebuffer_create*/,
9763
	.output_poll_changed = intel_fb_output_poll_changed,
9789
	.output_poll_changed = intel_fb_output_poll_changed,
9764
};
9790
};
9765
 
9791
 
9766
/* Set up chip specific display functions */
9792
/* Set up chip specific display functions */
9767
static void intel_init_display(struct drm_device *dev)
9793
static void intel_init_display(struct drm_device *dev)
9768
{
9794
{
9769
	struct drm_i915_private *dev_priv = dev->dev_private;
9795
	struct drm_i915_private *dev_priv = dev->dev_private;
9770
 
9796
 
9771
	if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
9797
	if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
9772
		dev_priv->display.find_dpll = g4x_find_best_dpll;
9798
		dev_priv->display.find_dpll = g4x_find_best_dpll;
9773
	else if (IS_VALLEYVIEW(dev))
9799
	else if (IS_VALLEYVIEW(dev))
9774
		dev_priv->display.find_dpll = vlv_find_best_dpll;
9800
		dev_priv->display.find_dpll = vlv_find_best_dpll;
9775
	else if (IS_PINEVIEW(dev))
9801
	else if (IS_PINEVIEW(dev))
9776
		dev_priv->display.find_dpll = pnv_find_best_dpll;
9802
		dev_priv->display.find_dpll = pnv_find_best_dpll;
9777
	else
9803
	else
9778
		dev_priv->display.find_dpll = i9xx_find_best_dpll;
9804
		dev_priv->display.find_dpll = i9xx_find_best_dpll;
9779
 
9805
 
9780
	if (HAS_DDI(dev)) {
9806
	if (HAS_DDI(dev)) {
9781
		dev_priv->display.get_pipe_config = haswell_get_pipe_config;
9807
		dev_priv->display.get_pipe_config = haswell_get_pipe_config;
9782
		dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
9808
		dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
9783
		dev_priv->display.crtc_enable = haswell_crtc_enable;
9809
		dev_priv->display.crtc_enable = haswell_crtc_enable;
9784
		dev_priv->display.crtc_disable = haswell_crtc_disable;
9810
		dev_priv->display.crtc_disable = haswell_crtc_disable;
9785
		dev_priv->display.off = haswell_crtc_off;
9811
		dev_priv->display.off = haswell_crtc_off;
9786
		dev_priv->display.update_plane = ironlake_update_plane;
9812
		dev_priv->display.update_plane = ironlake_update_plane;
9787
	} else if (HAS_PCH_SPLIT(dev)) {
9813
	} else if (HAS_PCH_SPLIT(dev)) {
9788
		dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
9814
		dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
9789
		dev_priv->display.get_clock = ironlake_crtc_clock_get;
9815
		dev_priv->display.get_clock = ironlake_crtc_clock_get;
9790
		dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
9816
		dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
9791
		dev_priv->display.crtc_enable = ironlake_crtc_enable;
9817
		dev_priv->display.crtc_enable = ironlake_crtc_enable;
9792
		dev_priv->display.crtc_disable = ironlake_crtc_disable;
9818
		dev_priv->display.crtc_disable = ironlake_crtc_disable;
9793
		dev_priv->display.off = ironlake_crtc_off;
9819
		dev_priv->display.off = ironlake_crtc_off;
9794
		dev_priv->display.update_plane = ironlake_update_plane;
9820
		dev_priv->display.update_plane = ironlake_update_plane;
9795
	} else if (IS_VALLEYVIEW(dev)) {
9821
	} else if (IS_VALLEYVIEW(dev)) {
9796
		dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
9822
		dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
9797
		dev_priv->display.get_clock = i9xx_crtc_clock_get;
9823
		dev_priv->display.get_clock = vlv_crtc_clock_get;
9798
		dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
9824
		dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
9799
		dev_priv->display.crtc_enable = valleyview_crtc_enable;
9825
		dev_priv->display.crtc_enable = valleyview_crtc_enable;
9800
		dev_priv->display.crtc_disable = i9xx_crtc_disable;
9826
		dev_priv->display.crtc_disable = i9xx_crtc_disable;
9801
		dev_priv->display.off = i9xx_crtc_off;
9827
		dev_priv->display.off = i9xx_crtc_off;
9802
		dev_priv->display.update_plane = i9xx_update_plane;
9828
		dev_priv->display.update_plane = i9xx_update_plane;
9803
	} else {
9829
	} else {
9804
		dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
9830
		dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
9805
		dev_priv->display.get_clock = i9xx_crtc_clock_get;
9831
		dev_priv->display.get_clock = i9xx_crtc_clock_get;
9806
		dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
9832
		dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
9807
		dev_priv->display.crtc_enable = i9xx_crtc_enable;
9833
		dev_priv->display.crtc_enable = i9xx_crtc_enable;
9808
		dev_priv->display.crtc_disable = i9xx_crtc_disable;
9834
		dev_priv->display.crtc_disable = i9xx_crtc_disable;
9809
		dev_priv->display.off = i9xx_crtc_off;
9835
		dev_priv->display.off = i9xx_crtc_off;
9810
		dev_priv->display.update_plane = i9xx_update_plane;
9836
		dev_priv->display.update_plane = i9xx_update_plane;
9811
	}
9837
	}
9812
 
9838
 
9813
	/* Returns the core display clock speed */
9839
	/* Returns the core display clock speed */
9814
	if (IS_VALLEYVIEW(dev))
9840
	if (IS_VALLEYVIEW(dev))
9815
		dev_priv->display.get_display_clock_speed =
9841
		dev_priv->display.get_display_clock_speed =
9816
			valleyview_get_display_clock_speed;
9842
			valleyview_get_display_clock_speed;
9817
	else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
9843
	else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
9818
		dev_priv->display.get_display_clock_speed =
9844
		dev_priv->display.get_display_clock_speed =
9819
			i945_get_display_clock_speed;
9845
			i945_get_display_clock_speed;
9820
	else if (IS_I915G(dev))
9846
	else if (IS_I915G(dev))
9821
		dev_priv->display.get_display_clock_speed =
9847
		dev_priv->display.get_display_clock_speed =
9822
			i915_get_display_clock_speed;
9848
			i915_get_display_clock_speed;
9823
	else if (IS_I945GM(dev) || IS_845G(dev))
9849
	else if (IS_I945GM(dev) || IS_845G(dev))
9824
		dev_priv->display.get_display_clock_speed =
9850
		dev_priv->display.get_display_clock_speed =
9825
			i9xx_misc_get_display_clock_speed;
9851
			i9xx_misc_get_display_clock_speed;
9826
	else if (IS_PINEVIEW(dev))
9852
	else if (IS_PINEVIEW(dev))
9827
		dev_priv->display.get_display_clock_speed =
9853
		dev_priv->display.get_display_clock_speed =
9828
			pnv_get_display_clock_speed;
9854
			pnv_get_display_clock_speed;
9829
	else if (IS_I915GM(dev))
9855
	else if (IS_I915GM(dev))
9830
		dev_priv->display.get_display_clock_speed =
9856
		dev_priv->display.get_display_clock_speed =
9831
			i915gm_get_display_clock_speed;
9857
			i915gm_get_display_clock_speed;
9832
	else if (IS_I865G(dev))
9858
	else if (IS_I865G(dev))
9833
		dev_priv->display.get_display_clock_speed =
9859
		dev_priv->display.get_display_clock_speed =
9834
			i865_get_display_clock_speed;
9860
			i865_get_display_clock_speed;
9835
	else if (IS_I85X(dev))
9861
	else if (IS_I85X(dev))
9836
		dev_priv->display.get_display_clock_speed =
9862
		dev_priv->display.get_display_clock_speed =
9837
			i855_get_display_clock_speed;
9863
			i855_get_display_clock_speed;
9838
	else /* 852, 830 */
9864
	else /* 852, 830 */
9839
		dev_priv->display.get_display_clock_speed =
9865
		dev_priv->display.get_display_clock_speed =
9840
			i830_get_display_clock_speed;
9866
			i830_get_display_clock_speed;
9841
 
9867
 
9842
	if (HAS_PCH_SPLIT(dev)) {
9868
	if (HAS_PCH_SPLIT(dev)) {
9843
		if (IS_GEN5(dev)) {
9869
		if (IS_GEN5(dev)) {
9844
			dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
9870
			dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
9845
			dev_priv->display.write_eld = ironlake_write_eld;
9871
			dev_priv->display.write_eld = ironlake_write_eld;
9846
		} else if (IS_GEN6(dev)) {
9872
		} else if (IS_GEN6(dev)) {
9847
			dev_priv->display.fdi_link_train = gen6_fdi_link_train;
9873
			dev_priv->display.fdi_link_train = gen6_fdi_link_train;
9848
			dev_priv->display.write_eld = ironlake_write_eld;
9874
			dev_priv->display.write_eld = ironlake_write_eld;
9849
		} else if (IS_IVYBRIDGE(dev)) {
9875
		} else if (IS_IVYBRIDGE(dev)) {
9850
			/* FIXME: detect B0+ stepping and use auto training */
9876
			/* FIXME: detect B0+ stepping and use auto training */
9851
			dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
9877
			dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
9852
			dev_priv->display.write_eld = ironlake_write_eld;
9878
			dev_priv->display.write_eld = ironlake_write_eld;
9853
			dev_priv->display.modeset_global_resources =
9879
			dev_priv->display.modeset_global_resources =
9854
				ivb_modeset_global_resources;
9880
				ivb_modeset_global_resources;
9855
		} else if (IS_HASWELL(dev)) {
9881
		} else if (IS_HASWELL(dev)) {
9856
			dev_priv->display.fdi_link_train = hsw_fdi_link_train;
9882
			dev_priv->display.fdi_link_train = hsw_fdi_link_train;
9857
			dev_priv->display.write_eld = haswell_write_eld;
9883
			dev_priv->display.write_eld = haswell_write_eld;
9858
			dev_priv->display.modeset_global_resources =
9884
			dev_priv->display.modeset_global_resources =
9859
				haswell_modeset_global_resources;
9885
				haswell_modeset_global_resources;
9860
		}
9886
		}
9861
	} else if (IS_G4X(dev)) {
9887
	} else if (IS_G4X(dev)) {
9862
		dev_priv->display.write_eld = g4x_write_eld;
9888
		dev_priv->display.write_eld = g4x_write_eld;
9863
	}
9889
	}
9864
 
9890
 
9865
	/* Default just returns -ENODEV to indicate unsupported */
9891
	/* Default just returns -ENODEV to indicate unsupported */
9866
//	dev_priv->display.queue_flip = intel_default_queue_flip;
9892
//	dev_priv->display.queue_flip = intel_default_queue_flip;
9867
 
9893
 
9868
 
9894
 
9869
 
9895
 
9870
 
9896
 
9871
}
9897
}
9872
 
9898
 
9873
/*
9899
/*
9874
 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
9900
 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
9875
 * resume, or other times.  This quirk makes sure that's the case for
9901
 * resume, or other times.  This quirk makes sure that's the case for
9876
 * affected systems.
9902
 * affected systems.
9877
 */
9903
 */
9878
static void quirk_pipea_force(struct drm_device *dev)
9904
static void quirk_pipea_force(struct drm_device *dev)
9879
{
9905
{
9880
	struct drm_i915_private *dev_priv = dev->dev_private;
9906
	struct drm_i915_private *dev_priv = dev->dev_private;
9881
 
9907
 
9882
	dev_priv->quirks |= QUIRK_PIPEA_FORCE;
9908
	dev_priv->quirks |= QUIRK_PIPEA_FORCE;
9883
	DRM_INFO("applying pipe a force quirk\n");
9909
	DRM_INFO("applying pipe a force quirk\n");
9884
}
9910
}
9885
 
9911
 
9886
/*
9912
/*
9887
 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
9913
 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
9888
 */
9914
 */
9889
static void quirk_ssc_force_disable(struct drm_device *dev)
9915
static void quirk_ssc_force_disable(struct drm_device *dev)
9890
{
9916
{
9891
	struct drm_i915_private *dev_priv = dev->dev_private;
9917
	struct drm_i915_private *dev_priv = dev->dev_private;
9892
	dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
9918
	dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
9893
	DRM_INFO("applying lvds SSC disable quirk\n");
9919
	DRM_INFO("applying lvds SSC disable quirk\n");
9894
}
9920
}
9895
 
9921
 
9896
/*
9922
/*
9897
 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
9923
 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
9898
 * brightness value
9924
 * brightness value
9899
 */
9925
 */
9900
static void quirk_invert_brightness(struct drm_device *dev)
9926
static void quirk_invert_brightness(struct drm_device *dev)
9901
{
9927
{
9902
	struct drm_i915_private *dev_priv = dev->dev_private;
9928
	struct drm_i915_private *dev_priv = dev->dev_private;
9903
	dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
9929
	dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
9904
	DRM_INFO("applying inverted panel brightness quirk\n");
9930
	DRM_INFO("applying inverted panel brightness quirk\n");
9905
}
9931
}
9906
 
9932
 
9907
/*
9933
/*
9908
 * Some machines (Dell XPS13) suffer broken backlight controls if
9934
 * Some machines (Dell XPS13) suffer broken backlight controls if
9909
 * BLM_PCH_PWM_ENABLE is set.
9935
 * BLM_PCH_PWM_ENABLE is set.
9910
 */
9936
 */
9911
static void quirk_no_pcm_pwm_enable(struct drm_device *dev)
9937
static void quirk_no_pcm_pwm_enable(struct drm_device *dev)
9912
{
9938
{
9913
	struct drm_i915_private *dev_priv = dev->dev_private;
9939
	struct drm_i915_private *dev_priv = dev->dev_private;
9914
	dev_priv->quirks |= QUIRK_NO_PCH_PWM_ENABLE;
9940
	dev_priv->quirks |= QUIRK_NO_PCH_PWM_ENABLE;
9915
	DRM_INFO("applying no-PCH_PWM_ENABLE quirk\n");
9941
	DRM_INFO("applying no-PCH_PWM_ENABLE quirk\n");
9916
}
9942
}
9917
 
9943
 
9918
struct intel_quirk {
9944
struct intel_quirk {
9919
	int device;
9945
	int device;
9920
	int subsystem_vendor;
9946
	int subsystem_vendor;
9921
	int subsystem_device;
9947
	int subsystem_device;
9922
	void (*hook)(struct drm_device *dev);
9948
	void (*hook)(struct drm_device *dev);
9923
};
9949
};
9924
 
9950
 
9925
/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
9951
/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
9926
struct intel_dmi_quirk {
9952
struct intel_dmi_quirk {
9927
	void (*hook)(struct drm_device *dev);
9953
	void (*hook)(struct drm_device *dev);
9928
	const struct dmi_system_id (*dmi_id_list)[];
9954
	const struct dmi_system_id (*dmi_id_list)[];
9929
};
9955
};
9930
 
9956
 
9931
static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
9957
static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
9932
{
9958
{
9933
	DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
9959
	DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
9934
	return 1;
9960
	return 1;
9935
}
9961
}
9936
 
9962
 
9937
static const struct intel_dmi_quirk intel_dmi_quirks[] = {
9963
static const struct intel_dmi_quirk intel_dmi_quirks[] = {
9938
	{
9964
	{
9939
		.dmi_id_list = &(const struct dmi_system_id[]) {
9965
		.dmi_id_list = &(const struct dmi_system_id[]) {
9940
			{
9966
			{
9941
				.callback = intel_dmi_reverse_brightness,
9967
				.callback = intel_dmi_reverse_brightness,
9942
				.ident = "NCR Corporation",
9968
				.ident = "NCR Corporation",
9943
				.matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
9969
				.matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
9944
					    DMI_MATCH(DMI_PRODUCT_NAME, ""),
9970
					    DMI_MATCH(DMI_PRODUCT_NAME, ""),
9945
				},
9971
				},
9946
			},
9972
			},
9947
			{ }  /* terminating entry */
9973
			{ }  /* terminating entry */
9948
		},
9974
		},
9949
		.hook = quirk_invert_brightness,
9975
		.hook = quirk_invert_brightness,
9950
	},
9976
	},
9951
};
9977
};
9952
 
9978
 
9953
static struct intel_quirk intel_quirks[] = {
9979
static struct intel_quirk intel_quirks[] = {
9954
	/* HP Mini needs pipe A force quirk (LP: #322104) */
9980
	/* HP Mini needs pipe A force quirk (LP: #322104) */
9955
	{ 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
9981
	{ 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
9956
 
9982
 
9957
	/* Toshiba Protege R-205, S-209 needs pipe A force quirk */
9983
	/* Toshiba Protege R-205, S-209 needs pipe A force quirk */
9958
	{ 0x2592, 0x1179, 0x0001, quirk_pipea_force },
9984
	{ 0x2592, 0x1179, 0x0001, quirk_pipea_force },
9959
 
9985
 
9960
	/* ThinkPad T60 needs pipe A force quirk (bug #16494) */
9986
	/* ThinkPad T60 needs pipe A force quirk (bug #16494) */
9961
	{ 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
9987
	{ 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
9962
 
9988
 
9963
	/* 830/845 need to leave pipe A & dpll A up */
9989
	/* 830/845 need to leave pipe A & dpll A up */
9964
	{ 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
9990
	{ 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
9965
	{ 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
9991
	{ 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
9966
 
9992
 
9967
	/* Lenovo U160 cannot use SSC on LVDS */
9993
	/* Lenovo U160 cannot use SSC on LVDS */
9968
	{ 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
9994
	{ 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
9969
 
9995
 
9970
	/* Sony Vaio Y cannot use SSC on LVDS */
9996
	/* Sony Vaio Y cannot use SSC on LVDS */
9971
	{ 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
9997
	{ 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
9972
 
9998
 
9973
	/* Acer Aspire 5734Z must invert backlight brightness */
9999
	/* Acer Aspire 5734Z must invert backlight brightness */
9974
	{ 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
10000
	{ 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
9975
 
10001
 
9976
	/* Acer/eMachines G725 */
10002
	/* Acer/eMachines G725 */
9977
	{ 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
10003
	{ 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
9978
 
10004
 
9979
	/* Acer/eMachines e725 */
10005
	/* Acer/eMachines e725 */
9980
	{ 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
10006
	{ 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
9981
 
10007
 
9982
	/* Acer/Packard Bell NCL20 */
10008
	/* Acer/Packard Bell NCL20 */
9983
	{ 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
10009
	{ 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
9984
 
10010
 
9985
	/* Acer Aspire 4736Z */
10011
	/* Acer Aspire 4736Z */
9986
	{ 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
10012
	{ 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
9987
 
10013
 
9988
	/* Dell XPS13 HD Sandy Bridge */
10014
	/* Dell XPS13 HD Sandy Bridge */
9989
	{ 0x0116, 0x1028, 0x052e, quirk_no_pcm_pwm_enable },
10015
	{ 0x0116, 0x1028, 0x052e, quirk_no_pcm_pwm_enable },
9990
	/* Dell XPS13 HD and XPS13 FHD Ivy Bridge */
10016
	/* Dell XPS13 HD and XPS13 FHD Ivy Bridge */
9991
	{ 0x0166, 0x1028, 0x058b, quirk_no_pcm_pwm_enable },
10017
	{ 0x0166, 0x1028, 0x058b, quirk_no_pcm_pwm_enable },
9992
};
10018
};
9993
 
10019
 
9994
static void intel_init_quirks(struct drm_device *dev)
10020
static void intel_init_quirks(struct drm_device *dev)
9995
{
10021
{
9996
	struct pci_dev *d = dev->pdev;
10022
	struct pci_dev *d = dev->pdev;
9997
	int i;
10023
	int i;
9998
 
10024
 
9999
	for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
10025
	for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
10000
		struct intel_quirk *q = &intel_quirks[i];
10026
		struct intel_quirk *q = &intel_quirks[i];
10001
 
10027
 
10002
		if (d->device == q->device &&
10028
		if (d->device == q->device &&
10003
		    (d->subsystem_vendor == q->subsystem_vendor ||
10029
		    (d->subsystem_vendor == q->subsystem_vendor ||
10004
		     q->subsystem_vendor == PCI_ANY_ID) &&
10030
		     q->subsystem_vendor == PCI_ANY_ID) &&
10005
		    (d->subsystem_device == q->subsystem_device ||
10031
		    (d->subsystem_device == q->subsystem_device ||
10006
		     q->subsystem_device == PCI_ANY_ID))
10032
		     q->subsystem_device == PCI_ANY_ID))
10007
			q->hook(dev);
10033
			q->hook(dev);
10008
	}
10034
	}
10009
}
10035
}
10010
 
10036
 
10011
/* Disable the VGA plane that we never use */
10037
/* Disable the VGA plane that we never use */
10012
static void i915_disable_vga(struct drm_device *dev)
10038
static void i915_disable_vga(struct drm_device *dev)
10013
{
10039
{
10014
	struct drm_i915_private *dev_priv = dev->dev_private;
10040
	struct drm_i915_private *dev_priv = dev->dev_private;
10015
	u8 sr1;
10041
	u8 sr1;
10016
	u32 vga_reg = i915_vgacntrl_reg(dev);
10042
	u32 vga_reg = i915_vgacntrl_reg(dev);
10017
 
10043
 
10018
//   vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
10044
//   vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
10019
    out8(SR01, VGA_SR_INDEX);
10045
    out8(SR01, VGA_SR_INDEX);
10020
    sr1 = in8(VGA_SR_DATA);
10046
    sr1 = in8(VGA_SR_DATA);
10021
    out8(sr1 | 1<<5, VGA_SR_DATA);
10047
    out8(sr1 | 1<<5, VGA_SR_DATA);
10022
//   vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
10048
//   vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
10023
	udelay(300);
10049
	udelay(300);
10024
 
10050
 
10025
	I915_WRITE(vga_reg, VGA_DISP_DISABLE);
10051
	I915_WRITE(vga_reg, VGA_DISP_DISABLE);
10026
	POSTING_READ(vga_reg);
10052
	POSTING_READ(vga_reg);
10027
}
10053
}
10028
 
10054
 
10029
void intel_modeset_init_hw(struct drm_device *dev)
10055
void intel_modeset_init_hw(struct drm_device *dev)
10030
{
10056
{
-
 
10057
	struct drm_i915_private *dev_priv = dev->dev_private;
-
 
10058
 
10031
	intel_init_power_well(dev);
10059
	intel_init_power_well(dev);
10032
 
10060
 
10033
	intel_prepare_ddi(dev);
10061
	intel_prepare_ddi(dev);
10034
 
10062
 
10035
	intel_init_clock_gating(dev);
10063
	intel_init_clock_gating(dev);
-
 
10064
 
-
 
10065
	/* Enable the CRI clock source so we can get at the display */
-
 
10066
	if (IS_VALLEYVIEW(dev))
-
 
10067
		I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) |
-
 
10068
			   DPLL_INTEGRATED_CRI_CLK_VLV);
10036
 
10069
 
10037
    mutex_lock(&dev->struct_mutex);
10070
    mutex_lock(&dev->struct_mutex);
10038
    intel_enable_gt_powersave(dev);
10071
    intel_enable_gt_powersave(dev);
10039
    mutex_unlock(&dev->struct_mutex);
10072
    mutex_unlock(&dev->struct_mutex);
10040
}
10073
}
-
 
10074
 
-
 
10075
void intel_modeset_suspend_hw(struct drm_device *dev)
-
 
10076
{
-
 
10077
	intel_suspend_hw(dev);
-
 
10078
}
10041
 
10079
 
10042
void intel_modeset_init(struct drm_device *dev)
10080
void intel_modeset_init(struct drm_device *dev)
10043
{
10081
{
10044
	struct drm_i915_private *dev_priv = dev->dev_private;
10082
	struct drm_i915_private *dev_priv = dev->dev_private;
10045
	int i, j, ret;
10083
	int i, j, ret;
10046
 
10084
 
10047
	drm_mode_config_init(dev);
10085
	drm_mode_config_init(dev);
10048
 
10086
 
10049
	dev->mode_config.min_width = 0;
10087
	dev->mode_config.min_width = 0;
10050
	dev->mode_config.min_height = 0;
10088
	dev->mode_config.min_height = 0;
10051
 
10089
 
10052
	dev->mode_config.preferred_depth = 24;
10090
	dev->mode_config.preferred_depth = 24;
10053
	dev->mode_config.prefer_shadow = 1;
10091
	dev->mode_config.prefer_shadow = 1;
10054
 
10092
 
10055
	dev->mode_config.funcs = &intel_mode_funcs;
10093
	dev->mode_config.funcs = &intel_mode_funcs;
10056
 
10094
 
10057
	intel_init_quirks(dev);
10095
	intel_init_quirks(dev);
10058
 
10096
 
10059
	intel_init_pm(dev);
10097
	intel_init_pm(dev);
10060
 
10098
 
10061
	if (INTEL_INFO(dev)->num_pipes == 0)
10099
	if (INTEL_INFO(dev)->num_pipes == 0)
10062
		return;
10100
		return;
10063
 
10101
 
10064
	intel_init_display(dev);
10102
	intel_init_display(dev);
10065
 
10103
 
10066
	if (IS_GEN2(dev)) {
10104
	if (IS_GEN2(dev)) {
10067
		dev->mode_config.max_width = 2048;
10105
		dev->mode_config.max_width = 2048;
10068
		dev->mode_config.max_height = 2048;
10106
		dev->mode_config.max_height = 2048;
10069
	} else if (IS_GEN3(dev)) {
10107
	} else if (IS_GEN3(dev)) {
10070
		dev->mode_config.max_width = 4096;
10108
		dev->mode_config.max_width = 4096;
10071
		dev->mode_config.max_height = 4096;
10109
		dev->mode_config.max_height = 4096;
10072
	} else {
10110
	} else {
10073
		dev->mode_config.max_width = 8192;
10111
		dev->mode_config.max_width = 8192;
10074
		dev->mode_config.max_height = 8192;
10112
		dev->mode_config.max_height = 8192;
10075
	}
10113
	}
10076
	dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
10114
	dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
10077
 
10115
 
10078
	DRM_DEBUG_KMS("%d display pipe%s available.\n",
10116
	DRM_DEBUG_KMS("%d display pipe%s available.\n",
10079
		      INTEL_INFO(dev)->num_pipes,
10117
		      INTEL_INFO(dev)->num_pipes,
10080
		      INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
10118
		      INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
10081
 
10119
 
10082
	for_each_pipe(i) {
10120
	for_each_pipe(i) {
10083
		intel_crtc_init(dev, i);
10121
		intel_crtc_init(dev, i);
10084
		for (j = 0; j < dev_priv->num_plane; j++) {
10122
		for (j = 0; j < dev_priv->num_plane; j++) {
10085
			ret = intel_plane_init(dev, i, j);
10123
			ret = intel_plane_init(dev, i, j);
10086
		if (ret)
10124
		if (ret)
10087
				DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
10125
				DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
10088
					      pipe_name(i), sprite_name(i, j), ret);
10126
					      pipe_name(i), sprite_name(i, j), ret);
10089
		}
10127
		}
10090
	}
10128
	}
10091
 
10129
 
10092
	intel_cpu_pll_init(dev);
10130
	intel_cpu_pll_init(dev);
10093
	intel_shared_dpll_init(dev);
10131
	intel_shared_dpll_init(dev);
10094
 
10132
 
10095
	/* Just disable it once at startup */
10133
	/* Just disable it once at startup */
10096
	i915_disable_vga(dev);
10134
	i915_disable_vga(dev);
10097
	intel_setup_outputs(dev);
10135
	intel_setup_outputs(dev);
10098
 
10136
 
10099
	/* Just in case the BIOS is doing something questionable. */
10137
	/* Just in case the BIOS is doing something questionable. */
10100
	intel_disable_fbc(dev);
10138
	intel_disable_fbc(dev);
10101
}
10139
}
10102
 
10140
 
10103
static void
10141
static void
10104
intel_connector_break_all_links(struct intel_connector *connector)
10142
intel_connector_break_all_links(struct intel_connector *connector)
10105
{
10143
{
10106
	connector->base.dpms = DRM_MODE_DPMS_OFF;
10144
	connector->base.dpms = DRM_MODE_DPMS_OFF;
10107
	connector->base.encoder = NULL;
10145
	connector->base.encoder = NULL;
10108
	connector->encoder->connectors_active = false;
10146
	connector->encoder->connectors_active = false;
10109
	connector->encoder->base.crtc = NULL;
10147
	connector->encoder->base.crtc = NULL;
10110
}
10148
}
10111
 
10149
 
10112
static void intel_enable_pipe_a(struct drm_device *dev)
10150
static void intel_enable_pipe_a(struct drm_device *dev)
10113
{
10151
{
10114
	struct intel_connector *connector;
10152
	struct intel_connector *connector;
10115
	struct drm_connector *crt = NULL;
10153
	struct drm_connector *crt = NULL;
10116
	struct intel_load_detect_pipe load_detect_temp;
10154
	struct intel_load_detect_pipe load_detect_temp;
10117
 
10155
 
10118
	/* We can't just switch on the pipe A, we need to set things up with a
10156
	/* We can't just switch on the pipe A, we need to set things up with a
10119
	 * proper mode and output configuration. As a gross hack, enable pipe A
10157
	 * proper mode and output configuration. As a gross hack, enable pipe A
10120
	 * by enabling the load detect pipe once. */
10158
	 * by enabling the load detect pipe once. */
10121
	list_for_each_entry(connector,
10159
	list_for_each_entry(connector,
10122
			    &dev->mode_config.connector_list,
10160
			    &dev->mode_config.connector_list,
10123
			    base.head) {
10161
			    base.head) {
10124
		if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
10162
		if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
10125
			crt = &connector->base;
10163
			crt = &connector->base;
10126
			break;
10164
			break;
10127
		}
10165
		}
10128
	}
10166
	}
10129
 
10167
 
10130
	if (!crt)
10168
	if (!crt)
10131
		return;
10169
		return;
10132
 
10170
 
10133
	if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
10171
	if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
10134
		intel_release_load_detect_pipe(crt, &load_detect_temp);
10172
		intel_release_load_detect_pipe(crt, &load_detect_temp);
10135
 
10173
 
10136
 
10174
 
10137
}
10175
}
10138
 
10176
 
10139
static bool
10177
static bool
10140
intel_check_plane_mapping(struct intel_crtc *crtc)
10178
intel_check_plane_mapping(struct intel_crtc *crtc)
10141
{
10179
{
10142
	struct drm_device *dev = crtc->base.dev;
10180
	struct drm_device *dev = crtc->base.dev;
10143
	struct drm_i915_private *dev_priv = dev->dev_private;
10181
	struct drm_i915_private *dev_priv = dev->dev_private;
10144
	u32 reg, val;
10182
	u32 reg, val;
10145
 
10183
 
10146
	if (INTEL_INFO(dev)->num_pipes == 1)
10184
	if (INTEL_INFO(dev)->num_pipes == 1)
10147
		return true;
10185
		return true;
10148
 
10186
 
10149
	reg = DSPCNTR(!crtc->plane);
10187
	reg = DSPCNTR(!crtc->plane);
10150
	val = I915_READ(reg);
10188
	val = I915_READ(reg);
10151
 
10189
 
10152
	if ((val & DISPLAY_PLANE_ENABLE) &&
10190
	if ((val & DISPLAY_PLANE_ENABLE) &&
10153
	    (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
10191
	    (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
10154
		return false;
10192
		return false;
10155
 
10193
 
10156
	return true;
10194
	return true;
10157
}
10195
}
10158
 
10196
 
10159
static void intel_sanitize_crtc(struct intel_crtc *crtc)
10197
static void intel_sanitize_crtc(struct intel_crtc *crtc)
10160
{
10198
{
10161
	struct drm_device *dev = crtc->base.dev;
10199
	struct drm_device *dev = crtc->base.dev;
10162
	struct drm_i915_private *dev_priv = dev->dev_private;
10200
	struct drm_i915_private *dev_priv = dev->dev_private;
10163
	u32 reg;
10201
	u32 reg;
10164
 
10202
 
10165
	/* Clear any frame start delays used for debugging left by the BIOS */
10203
	/* Clear any frame start delays used for debugging left by the BIOS */
10166
	reg = PIPECONF(crtc->config.cpu_transcoder);
10204
	reg = PIPECONF(crtc->config.cpu_transcoder);
10167
	I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
10205
	I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
10168
 
10206
 
10169
	/* We need to sanitize the plane -> pipe mapping first because this will
10207
	/* We need to sanitize the plane -> pipe mapping first because this will
10170
	 * disable the crtc (and hence change the state) if it is wrong. Note
10208
	 * disable the crtc (and hence change the state) if it is wrong. Note
10171
	 * that gen4+ has a fixed plane -> pipe mapping.  */
10209
	 * that gen4+ has a fixed plane -> pipe mapping.  */
10172
	if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
10210
	if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
10173
		struct intel_connector *connector;
10211
		struct intel_connector *connector;
10174
		bool plane;
10212
		bool plane;
10175
 
10213
 
10176
		DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
10214
		DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
10177
			      crtc->base.base.id);
10215
			      crtc->base.base.id);
10178
 
10216
 
10179
		/* Pipe has the wrong plane attached and the plane is active.
10217
		/* Pipe has the wrong plane attached and the plane is active.
10180
		 * Temporarily change the plane mapping and disable everything
10218
		 * Temporarily change the plane mapping and disable everything
10181
		 * ...  */
10219
		 * ...  */
10182
		plane = crtc->plane;
10220
		plane = crtc->plane;
10183
		crtc->plane = !plane;
10221
		crtc->plane = !plane;
10184
		dev_priv->display.crtc_disable(&crtc->base);
10222
		dev_priv->display.crtc_disable(&crtc->base);
10185
		crtc->plane = plane;
10223
		crtc->plane = plane;
10186
 
10224
 
10187
		/* ... and break all links. */
10225
		/* ... and break all links. */
10188
		list_for_each_entry(connector, &dev->mode_config.connector_list,
10226
		list_for_each_entry(connector, &dev->mode_config.connector_list,
10189
				    base.head) {
10227
				    base.head) {
10190
			if (connector->encoder->base.crtc != &crtc->base)
10228
			if (connector->encoder->base.crtc != &crtc->base)
10191
				continue;
10229
				continue;
10192
 
10230
 
10193
			intel_connector_break_all_links(connector);
10231
			intel_connector_break_all_links(connector);
10194
		}
10232
		}
10195
 
10233
 
10196
		WARN_ON(crtc->active);
10234
		WARN_ON(crtc->active);
10197
		crtc->base.enabled = false;
10235
		crtc->base.enabled = false;
10198
	}
10236
	}
10199
 
10237
 
10200
	if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
10238
	if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
10201
	    crtc->pipe == PIPE_A && !crtc->active) {
10239
	    crtc->pipe == PIPE_A && !crtc->active) {
10202
		/* BIOS forgot to enable pipe A, this mostly happens after
10240
		/* BIOS forgot to enable pipe A, this mostly happens after
10203
		 * resume. Force-enable the pipe to fix this, the update_dpms
10241
		 * resume. Force-enable the pipe to fix this, the update_dpms
10204
		 * call below we restore the pipe to the right state, but leave
10242
		 * call below we restore the pipe to the right state, but leave
10205
		 * the required bits on. */
10243
		 * the required bits on. */
10206
		intel_enable_pipe_a(dev);
10244
		intel_enable_pipe_a(dev);
10207
	}
10245
	}
10208
 
10246
 
10209
	/* Adjust the state of the output pipe according to whether we
10247
	/* Adjust the state of the output pipe according to whether we
10210
	 * have active connectors/encoders. */
10248
	 * have active connectors/encoders. */
10211
	intel_crtc_update_dpms(&crtc->base);
10249
	intel_crtc_update_dpms(&crtc->base);
10212
 
10250
 
10213
	if (crtc->active != crtc->base.enabled) {
10251
	if (crtc->active != crtc->base.enabled) {
10214
		struct intel_encoder *encoder;
10252
		struct intel_encoder *encoder;
10215
 
10253
 
10216
		/* This can happen either due to bugs in the get_hw_state
10254
		/* This can happen either due to bugs in the get_hw_state
10217
		 * functions or because the pipe is force-enabled due to the
10255
		 * functions or because the pipe is force-enabled due to the
10218
		 * pipe A quirk. */
10256
		 * pipe A quirk. */
10219
		DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
10257
		DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
10220
			      crtc->base.base.id,
10258
			      crtc->base.base.id,
10221
			      crtc->base.enabled ? "enabled" : "disabled",
10259
			      crtc->base.enabled ? "enabled" : "disabled",
10222
			      crtc->active ? "enabled" : "disabled");
10260
			      crtc->active ? "enabled" : "disabled");
10223
 
10261
 
10224
		crtc->base.enabled = crtc->active;
10262
		crtc->base.enabled = crtc->active;
10225
 
10263
 
10226
		/* Because we only establish the connector -> encoder ->
10264
		/* Because we only establish the connector -> encoder ->
10227
		 * crtc links if something is active, this means the
10265
		 * crtc links if something is active, this means the
10228
		 * crtc is now deactivated. Break the links. connector
10266
		 * crtc is now deactivated. Break the links. connector
10229
		 * -> encoder links are only establish when things are
10267
		 * -> encoder links are only establish when things are
10230
		 *  actually up, hence no need to break them. */
10268
		 *  actually up, hence no need to break them. */
10231
		WARN_ON(crtc->active);
10269
		WARN_ON(crtc->active);
10232
 
10270
 
10233
		for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
10271
		for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
10234
			WARN_ON(encoder->connectors_active);
10272
			WARN_ON(encoder->connectors_active);
10235
			encoder->base.crtc = NULL;
10273
			encoder->base.crtc = NULL;
10236
		}
10274
		}
10237
	}
10275
	}
10238
}
10276
}
10239
 
10277
 
10240
static void intel_sanitize_encoder(struct intel_encoder *encoder)
10278
static void intel_sanitize_encoder(struct intel_encoder *encoder)
10241
{
10279
{
10242
	struct intel_connector *connector;
10280
	struct intel_connector *connector;
10243
	struct drm_device *dev = encoder->base.dev;
10281
	struct drm_device *dev = encoder->base.dev;
10244
 
10282
 
10245
	/* We need to check both for a crtc link (meaning that the
10283
	/* We need to check both for a crtc link (meaning that the
10246
	 * encoder is active and trying to read from a pipe) and the
10284
	 * encoder is active and trying to read from a pipe) and the
10247
	 * pipe itself being active. */
10285
	 * pipe itself being active. */
10248
	bool has_active_crtc = encoder->base.crtc &&
10286
	bool has_active_crtc = encoder->base.crtc &&
10249
		to_intel_crtc(encoder->base.crtc)->active;
10287
		to_intel_crtc(encoder->base.crtc)->active;
10250
 
10288
 
10251
	if (encoder->connectors_active && !has_active_crtc) {
10289
	if (encoder->connectors_active && !has_active_crtc) {
10252
		DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
10290
		DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
10253
			      encoder->base.base.id,
10291
			      encoder->base.base.id,
10254
			      drm_get_encoder_name(&encoder->base));
10292
			      drm_get_encoder_name(&encoder->base));
10255
 
10293
 
10256
		/* Connector is active, but has no active pipe. This is
10294
		/* Connector is active, but has no active pipe. This is
10257
		 * fallout from our resume register restoring. Disable
10295
		 * fallout from our resume register restoring. Disable
10258
		 * the encoder manually again. */
10296
		 * the encoder manually again. */
10259
		if (encoder->base.crtc) {
10297
		if (encoder->base.crtc) {
10260
			DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
10298
			DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
10261
				      encoder->base.base.id,
10299
				      encoder->base.base.id,
10262
				      drm_get_encoder_name(&encoder->base));
10300
				      drm_get_encoder_name(&encoder->base));
10263
			encoder->disable(encoder);
10301
			encoder->disable(encoder);
10264
		}
10302
		}
10265
 
10303
 
10266
		/* Inconsistent output/port/pipe state happens presumably due to
10304
		/* Inconsistent output/port/pipe state happens presumably due to
10267
		 * a bug in one of the get_hw_state functions. Or someplace else
10305
		 * a bug in one of the get_hw_state functions. Or someplace else
10268
		 * in our code, like the register restore mess on resume. Clamp
10306
		 * in our code, like the register restore mess on resume. Clamp
10269
		 * things to off as a safer default. */
10307
		 * things to off as a safer default. */
10270
		list_for_each_entry(connector,
10308
		list_for_each_entry(connector,
10271
				    &dev->mode_config.connector_list,
10309
				    &dev->mode_config.connector_list,
10272
				    base.head) {
10310
				    base.head) {
10273
			if (connector->encoder != encoder)
10311
			if (connector->encoder != encoder)
10274
				continue;
10312
				continue;
10275
 
10313
 
10276
			intel_connector_break_all_links(connector);
10314
			intel_connector_break_all_links(connector);
10277
		}
10315
		}
10278
	}
10316
	}
10279
	/* Enabled encoders without active connectors will be fixed in
10317
	/* Enabled encoders without active connectors will be fixed in
10280
	 * the crtc fixup. */
10318
	 * the crtc fixup. */
10281
}
10319
}
10282
 
10320
 
10283
void i915_redisable_vga(struct drm_device *dev)
10321
void i915_redisable_vga(struct drm_device *dev)
10284
{
10322
{
10285
	struct drm_i915_private *dev_priv = dev->dev_private;
10323
	struct drm_i915_private *dev_priv = dev->dev_private;
10286
	u32 vga_reg = i915_vgacntrl_reg(dev);
10324
	u32 vga_reg = i915_vgacntrl_reg(dev);
10287
 
10325
 
10288
	/* This function can be called both from intel_modeset_setup_hw_state or
10326
	/* This function can be called both from intel_modeset_setup_hw_state or
10289
	 * at a very early point in our resume sequence, where the power well
10327
	 * at a very early point in our resume sequence, where the power well
10290
	 * structures are not yet restored. Since this function is at a very
10328
	 * structures are not yet restored. Since this function is at a very
10291
	 * paranoid "someone might have enabled VGA while we were not looking"
10329
	 * paranoid "someone might have enabled VGA while we were not looking"
10292
	 * level, just check if the power well is enabled instead of trying to
10330
	 * level, just check if the power well is enabled instead of trying to
10293
	 * follow the "don't touch the power well if we don't need it" policy
10331
	 * follow the "don't touch the power well if we don't need it" policy
10294
	 * the rest of the driver uses. */
10332
	 * the rest of the driver uses. */
10295
	if (HAS_POWER_WELL(dev) &&
10333
	if (HAS_POWER_WELL(dev) &&
10296
	    (I915_READ(HSW_PWR_WELL_DRIVER) & HSW_PWR_WELL_STATE_ENABLED) == 0)
10334
	    (I915_READ(HSW_PWR_WELL_DRIVER) & HSW_PWR_WELL_STATE_ENABLED) == 0)
10297
		return;
10335
		return;
10298
 
10336
 
10299
	if (I915_READ(vga_reg) != VGA_DISP_DISABLE) {
10337
	if (I915_READ(vga_reg) != VGA_DISP_DISABLE) {
10300
		DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
10338
		DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
10301
		i915_disable_vga(dev);
10339
		i915_disable_vga(dev);
10302
	}
10340
	}
10303
}
10341
}
10304
 
10342
 
10305
static void intel_modeset_readout_hw_state(struct drm_device *dev)
10343
static void intel_modeset_readout_hw_state(struct drm_device *dev)
10306
{
10344
{
10307
	struct drm_i915_private *dev_priv = dev->dev_private;
10345
	struct drm_i915_private *dev_priv = dev->dev_private;
10308
	enum pipe pipe;
10346
	enum pipe pipe;
10309
	struct intel_crtc *crtc;
10347
	struct intel_crtc *crtc;
10310
	struct intel_encoder *encoder;
10348
	struct intel_encoder *encoder;
10311
	struct intel_connector *connector;
10349
	struct intel_connector *connector;
10312
	int i;
10350
	int i;
10313
 
10351
 
10314
	list_for_each_entry(crtc, &dev->mode_config.crtc_list,
10352
	list_for_each_entry(crtc, &dev->mode_config.crtc_list,
10315
			    base.head) {
10353
			    base.head) {
10316
		memset(&crtc->config, 0, sizeof(crtc->config));
10354
		memset(&crtc->config, 0, sizeof(crtc->config));
10317
 
10355
 
10318
		crtc->active = dev_priv->display.get_pipe_config(crtc,
10356
		crtc->active = dev_priv->display.get_pipe_config(crtc,
10319
								 &crtc->config);
10357
								 &crtc->config);
10320
 
10358
 
10321
		crtc->base.enabled = crtc->active;
10359
		crtc->base.enabled = crtc->active;
10322
 
10360
 
10323
		DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
10361
		DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
10324
			      crtc->base.base.id,
10362
			      crtc->base.base.id,
10325
			      crtc->active ? "enabled" : "disabled");
10363
			      crtc->active ? "enabled" : "disabled");
10326
	}
10364
	}
10327
 
10365
 
10328
	/* FIXME: Smash this into the new shared dpll infrastructure. */
10366
	/* FIXME: Smash this into the new shared dpll infrastructure. */
10329
	if (HAS_DDI(dev))
10367
	if (HAS_DDI(dev))
10330
		intel_ddi_setup_hw_pll_state(dev);
10368
		intel_ddi_setup_hw_pll_state(dev);
10331
 
10369
 
10332
	for (i = 0; i < dev_priv->num_shared_dpll; i++) {
10370
	for (i = 0; i < dev_priv->num_shared_dpll; i++) {
10333
		struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
10371
		struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
10334
 
10372
 
10335
		pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
10373
		pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
10336
		pll->active = 0;
10374
		pll->active = 0;
10337
		list_for_each_entry(crtc, &dev->mode_config.crtc_list,
10375
		list_for_each_entry(crtc, &dev->mode_config.crtc_list,
10338
				    base.head) {
10376
				    base.head) {
10339
			if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
10377
			if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
10340
				pll->active++;
10378
				pll->active++;
10341
		}
10379
		}
10342
		pll->refcount = pll->active;
10380
		pll->refcount = pll->active;
10343
 
10381
 
10344
		DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
10382
		DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
10345
			      pll->name, pll->refcount, pll->on);
10383
			      pll->name, pll->refcount, pll->on);
10346
	}
10384
	}
10347
 
10385
 
10348
	list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10386
	list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10349
			    base.head) {
10387
			    base.head) {
10350
		pipe = 0;
10388
		pipe = 0;
10351
 
10389
 
10352
		if (encoder->get_hw_state(encoder, &pipe)) {
10390
		if (encoder->get_hw_state(encoder, &pipe)) {
10353
			crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
10391
			crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
10354
			encoder->base.crtc = &crtc->base;
10392
			encoder->base.crtc = &crtc->base;
10355
			if (encoder->get_config)
10393
			if (encoder->get_config)
10356
				encoder->get_config(encoder, &crtc->config);
10394
				encoder->get_config(encoder, &crtc->config);
10357
		} else {
10395
		} else {
10358
			encoder->base.crtc = NULL;
10396
			encoder->base.crtc = NULL;
10359
		}
10397
		}
10360
 
10398
 
10361
		encoder->connectors_active = false;
10399
		encoder->connectors_active = false;
10362
		DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe=%i\n",
10400
		DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe=%i\n",
10363
			      encoder->base.base.id,
10401
			      encoder->base.base.id,
10364
			      drm_get_encoder_name(&encoder->base),
10402
			      drm_get_encoder_name(&encoder->base),
10365
			      encoder->base.crtc ? "enabled" : "disabled",
10403
			      encoder->base.crtc ? "enabled" : "disabled",
10366
			      pipe);
10404
			      pipe);
10367
	}
10405
	}
10368
 
10406
 
10369
	list_for_each_entry(crtc, &dev->mode_config.crtc_list,
10407
	list_for_each_entry(crtc, &dev->mode_config.crtc_list,
10370
			    base.head) {
10408
			    base.head) {
10371
		if (!crtc->active)
10409
		if (!crtc->active)
10372
			continue;
10410
			continue;
10373
		if (dev_priv->display.get_clock)
10411
		if (dev_priv->display.get_clock)
10374
			dev_priv->display.get_clock(crtc,
10412
			dev_priv->display.get_clock(crtc,
10375
						    &crtc->config);
10413
						    &crtc->config);
10376
	}
10414
	}
10377
 
10415
 
10378
	list_for_each_entry(connector, &dev->mode_config.connector_list,
10416
	list_for_each_entry(connector, &dev->mode_config.connector_list,
10379
			    base.head) {
10417
			    base.head) {
10380
		if (connector->get_hw_state(connector)) {
10418
		if (connector->get_hw_state(connector)) {
10381
			connector->base.dpms = DRM_MODE_DPMS_ON;
10419
			connector->base.dpms = DRM_MODE_DPMS_ON;
10382
			connector->encoder->connectors_active = true;
10420
			connector->encoder->connectors_active = true;
10383
			connector->base.encoder = &connector->encoder->base;
10421
			connector->base.encoder = &connector->encoder->base;
10384
		} else {
10422
		} else {
10385
			connector->base.dpms = DRM_MODE_DPMS_OFF;
10423
			connector->base.dpms = DRM_MODE_DPMS_OFF;
10386
			connector->base.encoder = NULL;
10424
			connector->base.encoder = NULL;
10387
		}
10425
		}
10388
		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
10426
		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
10389
			      connector->base.base.id,
10427
			      connector->base.base.id,
10390
			      drm_get_connector_name(&connector->base),
10428
			      drm_get_connector_name(&connector->base),
10391
			      connector->base.encoder ? "enabled" : "disabled");
10429
			      connector->base.encoder ? "enabled" : "disabled");
10392
	}
10430
	}
10393
}
10431
}
10394
 
10432
 
10395
/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
10433
/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
10396
 * and i915 state tracking structures. */
10434
 * and i915 state tracking structures. */
10397
void intel_modeset_setup_hw_state(struct drm_device *dev,
10435
void intel_modeset_setup_hw_state(struct drm_device *dev,
10398
				  bool force_restore)
10436
				  bool force_restore)
10399
{
10437
{
10400
	struct drm_i915_private *dev_priv = dev->dev_private;
10438
	struct drm_i915_private *dev_priv = dev->dev_private;
10401
	enum pipe pipe;
10439
	enum pipe pipe;
10402
	struct drm_plane *plane;
10440
	struct drm_plane *plane;
10403
	struct intel_crtc *crtc;
10441
	struct intel_crtc *crtc;
10404
	struct intel_encoder *encoder;
10442
	struct intel_encoder *encoder;
10405
	int i;
10443
	int i;
10406
 
10444
 
10407
	intel_modeset_readout_hw_state(dev);
10445
	intel_modeset_readout_hw_state(dev);
10408
 
10446
 
10409
	/*
10447
	/*
10410
	 * Now that we have the config, copy it to each CRTC struct
10448
	 * Now that we have the config, copy it to each CRTC struct
10411
	 * Note that this could go away if we move to using crtc_config
10449
	 * Note that this could go away if we move to using crtc_config
10412
	 * checking everywhere.
10450
	 * checking everywhere.
10413
	 */
10451
	 */
10414
	list_for_each_entry(crtc, &dev->mode_config.crtc_list,
10452
	list_for_each_entry(crtc, &dev->mode_config.crtc_list,
10415
			    base.head) {
10453
			    base.head) {
10416
		if (crtc->active && i915_fastboot) {
10454
		if (crtc->active && i915_fastboot) {
10417
			intel_crtc_mode_from_pipe_config(crtc, &crtc->config);
10455
			intel_crtc_mode_from_pipe_config(crtc, &crtc->config);
10418
 
10456
 
10419
			DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
10457
			DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
10420
				      crtc->base.base.id);
10458
				      crtc->base.base.id);
10421
			drm_mode_debug_printmodeline(&crtc->base.mode);
10459
			drm_mode_debug_printmodeline(&crtc->base.mode);
10422
		}
10460
		}
10423
	}
10461
	}
10424
 
10462
 
10425
	/* HW state is read out, now we need to sanitize this mess. */
10463
	/* HW state is read out, now we need to sanitize this mess. */
10426
	list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10464
	list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10427
			    base.head) {
10465
			    base.head) {
10428
		intel_sanitize_encoder(encoder);
10466
		intel_sanitize_encoder(encoder);
10429
	}
10467
	}
10430
 
10468
 
10431
	for_each_pipe(pipe) {
10469
	for_each_pipe(pipe) {
10432
		crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
10470
		crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
10433
		intel_sanitize_crtc(crtc);
10471
		intel_sanitize_crtc(crtc);
10434
		intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
10472
		intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
10435
	}
10473
	}
10436
 
10474
 
10437
	for (i = 0; i < dev_priv->num_shared_dpll; i++) {
10475
	for (i = 0; i < dev_priv->num_shared_dpll; i++) {
10438
		struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
10476
		struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
10439
 
10477
 
10440
		if (!pll->on || pll->active)
10478
		if (!pll->on || pll->active)
10441
			continue;
10479
			continue;
10442
 
10480
 
10443
		DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
10481
		DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
10444
 
10482
 
10445
		pll->disable(dev_priv, pll);
10483
		pll->disable(dev_priv, pll);
10446
		pll->on = false;
10484
		pll->on = false;
10447
	}
10485
	}
10448
 
10486
 
10449
	if (force_restore) {
10487
	if (force_restore) {
10450
		/*
10488
		/*
10451
		 * We need to use raw interfaces for restoring state to avoid
10489
		 * We need to use raw interfaces for restoring state to avoid
10452
		 * checking (bogus) intermediate states.
10490
		 * checking (bogus) intermediate states.
10453
		 */
10491
		 */
10454
		for_each_pipe(pipe) {
10492
		for_each_pipe(pipe) {
10455
			struct drm_crtc *crtc =
10493
			struct drm_crtc *crtc =
10456
				dev_priv->pipe_to_crtc_mapping[pipe];
10494
				dev_priv->pipe_to_crtc_mapping[pipe];
10457
 
10495
 
10458
			__intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
10496
			__intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
10459
					 crtc->fb);
10497
					 crtc->fb);
10460
		}
10498
		}
10461
		list_for_each_entry(plane, &dev->mode_config.plane_list, head)
10499
		list_for_each_entry(plane, &dev->mode_config.plane_list, head)
10462
			intel_plane_restore(plane);
10500
			intel_plane_restore(plane);
10463
 
10501
 
10464
		i915_redisable_vga(dev);
10502
		i915_redisable_vga(dev);
10465
	} else {
10503
	} else {
10466
	intel_modeset_update_staged_output_state(dev);
10504
	intel_modeset_update_staged_output_state(dev);
10467
	}
10505
	}
10468
 
10506
 
10469
	intel_modeset_check_state(dev);
10507
	intel_modeset_check_state(dev);
10470
 
10508
 
10471
	drm_mode_config_reset(dev);
10509
	drm_mode_config_reset(dev);
10472
}
10510
}
10473
 
10511
 
10474
void intel_modeset_gem_init(struct drm_device *dev)
10512
void intel_modeset_gem_init(struct drm_device *dev)
10475
{
10513
{
10476
	intel_modeset_init_hw(dev);
10514
	intel_modeset_init_hw(dev);
10477
 
10515
 
10478
//   intel_setup_overlay(dev);
10516
//   intel_setup_overlay(dev);
10479
 
10517
 
10480
	intel_modeset_setup_hw_state(dev, false);
10518
	intel_modeset_setup_hw_state(dev, false);
10481
}
10519
}
10482
 
10520
 
10483
void intel_modeset_cleanup(struct drm_device *dev)
10521
void intel_modeset_cleanup(struct drm_device *dev)
10484
{
10522
{
10485
#if 0
10523
#if 0
10486
	struct drm_i915_private *dev_priv = dev->dev_private;
10524
	struct drm_i915_private *dev_priv = dev->dev_private;
10487
	struct drm_crtc *crtc;
10525
	struct drm_crtc *crtc;
10488
 
10526
 
10489
	/*
10527
	/*
10490
	 * Interrupts and polling as the first thing to avoid creating havoc.
10528
	 * Interrupts and polling as the first thing to avoid creating havoc.
10491
	 * Too much stuff here (turning of rps, connectors, ...) would
10529
	 * Too much stuff here (turning of rps, connectors, ...) would
10492
	 * experience fancy races otherwise.
10530
	 * experience fancy races otherwise.
10493
	 */
10531
	 */
10494
	drm_irq_uninstall(dev);
10532
	drm_irq_uninstall(dev);
10495
	cancel_work_sync(&dev_priv->hotplug_work);
10533
	cancel_work_sync(&dev_priv->hotplug_work);
10496
	/*
10534
	/*
10497
	 * Due to the hpd irq storm handling the hotplug work can re-arm the
10535
	 * Due to the hpd irq storm handling the hotplug work can re-arm the
10498
	 * poll handlers. Hence disable polling after hpd handling is shut down.
10536
	 * poll handlers. Hence disable polling after hpd handling is shut down.
10499
	 */
10537
	 */
10500
//   drm_kms_helper_poll_fini(dev);
10538
//   drm_kms_helper_poll_fini(dev);
10501
 
10539
 
10502
	mutex_lock(&dev->struct_mutex);
10540
	mutex_lock(&dev->struct_mutex);
10503
 
10541
 
10504
//   intel_unregister_dsm_handler();
10542
//   intel_unregister_dsm_handler();
10505
 
10543
 
10506
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
10544
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
10507
		/* Skip inactive CRTCs */
10545
		/* Skip inactive CRTCs */
10508
		if (!crtc->fb)
10546
		if (!crtc->fb)
10509
			continue;
10547
			continue;
10510
 
10548
 
10511
		intel_increase_pllclock(crtc);
10549
		intel_increase_pllclock(crtc);
10512
	}
10550
	}
10513
 
10551
 
10514
	intel_disable_fbc(dev);
10552
	intel_disable_fbc(dev);
10515
 
10553
 
10516
	intel_disable_gt_powersave(dev);
10554
	intel_disable_gt_powersave(dev);
10517
 
10555
 
10518
	ironlake_teardown_rc6(dev);
10556
	ironlake_teardown_rc6(dev);
10519
 
10557
 
10520
	mutex_unlock(&dev->struct_mutex);
10558
	mutex_unlock(&dev->struct_mutex);
10521
 
10559
 
10522
	/* flush any delayed tasks or pending work */
10560
	/* flush any delayed tasks or pending work */
10523
	flush_scheduled_work();
10561
	flush_scheduled_work();
10524
 
10562
 
10525
	/* destroy backlight, if any, before the connectors */
10563
	/* destroy backlight, if any, before the connectors */
10526
	intel_panel_destroy_backlight(dev);
10564
	intel_panel_destroy_backlight(dev);
10527
 
10565
 
10528
	drm_mode_config_cleanup(dev);
10566
	drm_mode_config_cleanup(dev);
10529
#endif
10567
#endif
10530
}
10568
}
10531
 
10569
 
10532
/*
10570
/*
10533
 * Return which encoder is currently attached for connector.
10571
 * Return which encoder is currently attached for connector.
10534
 */
10572
 */
10535
struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
10573
struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
10536
{
10574
{
10537
	return &intel_attached_encoder(connector)->base;
10575
	return &intel_attached_encoder(connector)->base;
10538
}
10576
}
10539
 
10577
 
10540
void intel_connector_attach_encoder(struct intel_connector *connector,
10578
void intel_connector_attach_encoder(struct intel_connector *connector,
10541
				    struct intel_encoder *encoder)
10579
				    struct intel_encoder *encoder)
10542
{
10580
{
10543
	connector->encoder = encoder;
10581
	connector->encoder = encoder;
10544
	drm_mode_connector_attach_encoder(&connector->base,
10582
	drm_mode_connector_attach_encoder(&connector->base,
10545
					  &encoder->base);
10583
					  &encoder->base);
10546
}
10584
}
10547
 
10585
 
10548
/*
10586
/*
10549
 * set vga decode state - true == enable VGA decode
10587
 * set vga decode state - true == enable VGA decode
10550
 */
10588
 */
10551
int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
10589
int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
10552
{
10590
{
10553
	struct drm_i915_private *dev_priv = dev->dev_private;
10591
	struct drm_i915_private *dev_priv = dev->dev_private;
10554
	u16 gmch_ctrl;
10592
	u16 gmch_ctrl;
10555
 
10593
 
10556
	pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
10594
	pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
10557
	if (state)
10595
	if (state)
10558
		gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
10596
		gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
10559
	else
10597
	else
10560
		gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
10598
		gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
10561
	pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
10599
	pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
10562
	return 0;
10600
	return 0;
10563
}
10601
}
10564
 
10602
 
10565
#ifdef CONFIG_DEBUG_FS
10603
#ifdef CONFIG_DEBUG_FS
10566
#include 
10604
#include 
10567
 
10605
 
10568
struct intel_display_error_state {
10606
struct intel_display_error_state {
10569
 
10607
 
10570
	u32 power_well_driver;
10608
	u32 power_well_driver;
10571
 
10609
 
10572
	int num_transcoders;
10610
	int num_transcoders;
10573
 
10611
 
10574
	struct intel_cursor_error_state {
10612
	struct intel_cursor_error_state {
10575
		u32 control;
10613
		u32 control;
10576
		u32 position;
10614
		u32 position;
10577
		u32 base;
10615
		u32 base;
10578
		u32 size;
10616
		u32 size;
10579
	} cursor[I915_MAX_PIPES];
10617
	} cursor[I915_MAX_PIPES];
10580
 
10618
 
10581
	struct intel_pipe_error_state {
10619
	struct intel_pipe_error_state {
10582
		u32 source;
10620
		u32 source;
10583
	} pipe[I915_MAX_PIPES];
10621
	} pipe[I915_MAX_PIPES];
10584
 
10622
 
10585
	struct intel_plane_error_state {
10623
	struct intel_plane_error_state {
10586
		u32 control;
10624
		u32 control;
10587
		u32 stride;
10625
		u32 stride;
10588
		u32 size;
10626
		u32 size;
10589
		u32 pos;
10627
		u32 pos;
10590
		u32 addr;
10628
		u32 addr;
10591
		u32 surface;
10629
		u32 surface;
10592
		u32 tile_offset;
10630
		u32 tile_offset;
10593
	} plane[I915_MAX_PIPES];
10631
	} plane[I915_MAX_PIPES];
10594
 
10632
 
10595
	struct intel_transcoder_error_state {
10633
	struct intel_transcoder_error_state {
10596
		enum transcoder cpu_transcoder;
10634
		enum transcoder cpu_transcoder;
10597
 
10635
 
10598
		u32 conf;
10636
		u32 conf;
10599
 
10637
 
10600
		u32 htotal;
10638
		u32 htotal;
10601
		u32 hblank;
10639
		u32 hblank;
10602
		u32 hsync;
10640
		u32 hsync;
10603
		u32 vtotal;
10641
		u32 vtotal;
10604
		u32 vblank;
10642
		u32 vblank;
10605
		u32 vsync;
10643
		u32 vsync;
10606
	} transcoder[4];
10644
	} transcoder[4];
10607
};
10645
};
10608
 
10646
 
10609
struct intel_display_error_state *
10647
struct intel_display_error_state *
10610
intel_display_capture_error_state(struct drm_device *dev)
10648
intel_display_capture_error_state(struct drm_device *dev)
10611
{
10649
{
10612
	drm_i915_private_t *dev_priv = dev->dev_private;
10650
	drm_i915_private_t *dev_priv = dev->dev_private;
10613
	struct intel_display_error_state *error;
10651
	struct intel_display_error_state *error;
10614
	int transcoders[] = {
10652
	int transcoders[] = {
10615
		TRANSCODER_A,
10653
		TRANSCODER_A,
10616
		TRANSCODER_B,
10654
		TRANSCODER_B,
10617
		TRANSCODER_C,
10655
		TRANSCODER_C,
10618
		TRANSCODER_EDP,
10656
		TRANSCODER_EDP,
10619
	};
10657
	};
10620
	int i;
10658
	int i;
10621
 
10659
 
10622
	if (INTEL_INFO(dev)->num_pipes == 0)
10660
	if (INTEL_INFO(dev)->num_pipes == 0)
10623
		return NULL;
10661
		return NULL;
10624
 
10662
 
10625
	error = kmalloc(sizeof(*error), GFP_ATOMIC);
10663
	error = kmalloc(sizeof(*error), GFP_ATOMIC);
10626
	if (error == NULL)
10664
	if (error == NULL)
10627
		return NULL;
10665
		return NULL;
10628
 
10666
 
10629
	if (HAS_POWER_WELL(dev))
10667
	if (HAS_POWER_WELL(dev))
10630
		error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
10668
		error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
10631
 
10669
 
10632
	for_each_pipe(i) {
10670
	for_each_pipe(i) {
10633
		if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev)) {
10671
		if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev)) {
10634
		error->cursor[i].control = I915_READ(CURCNTR(i));
10672
		error->cursor[i].control = I915_READ(CURCNTR(i));
10635
		error->cursor[i].position = I915_READ(CURPOS(i));
10673
		error->cursor[i].position = I915_READ(CURPOS(i));
10636
		error->cursor[i].base = I915_READ(CURBASE(i));
10674
		error->cursor[i].base = I915_READ(CURBASE(i));
10637
		} else {
10675
		} else {
10638
			error->cursor[i].control = I915_READ(CURCNTR_IVB(i));
10676
			error->cursor[i].control = I915_READ(CURCNTR_IVB(i));
10639
			error->cursor[i].position = I915_READ(CURPOS_IVB(i));
10677
			error->cursor[i].position = I915_READ(CURPOS_IVB(i));
10640
			error->cursor[i].base = I915_READ(CURBASE_IVB(i));
10678
			error->cursor[i].base = I915_READ(CURBASE_IVB(i));
10641
		}
10679
		}
10642
 
10680
 
10643
		error->plane[i].control = I915_READ(DSPCNTR(i));
10681
		error->plane[i].control = I915_READ(DSPCNTR(i));
10644
		error->plane[i].stride = I915_READ(DSPSTRIDE(i));
10682
		error->plane[i].stride = I915_READ(DSPSTRIDE(i));
10645
		if (INTEL_INFO(dev)->gen <= 3) {
10683
		if (INTEL_INFO(dev)->gen <= 3) {
10646
		error->plane[i].size = I915_READ(DSPSIZE(i));
10684
		error->plane[i].size = I915_READ(DSPSIZE(i));
10647
		error->plane[i].pos = I915_READ(DSPPOS(i));
10685
		error->plane[i].pos = I915_READ(DSPPOS(i));
10648
		}
10686
		}
10649
		if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
10687
		if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
10650
		error->plane[i].addr = I915_READ(DSPADDR(i));
10688
		error->plane[i].addr = I915_READ(DSPADDR(i));
10651
		if (INTEL_INFO(dev)->gen >= 4) {
10689
		if (INTEL_INFO(dev)->gen >= 4) {
10652
			error->plane[i].surface = I915_READ(DSPSURF(i));
10690
			error->plane[i].surface = I915_READ(DSPSURF(i));
10653
			error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
10691
			error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
10654
		}
10692
		}
10655
 
10693
 
10656
		error->pipe[i].source = I915_READ(PIPESRC(i));
10694
		error->pipe[i].source = I915_READ(PIPESRC(i));
10657
	}
10695
	}
10658
 
10696
 
10659
	error->num_transcoders = INTEL_INFO(dev)->num_pipes;
10697
	error->num_transcoders = INTEL_INFO(dev)->num_pipes;
10660
	if (HAS_DDI(dev_priv->dev))
10698
	if (HAS_DDI(dev_priv->dev))
10661
		error->num_transcoders++; /* Account for eDP. */
10699
		error->num_transcoders++; /* Account for eDP. */
10662
 
10700
 
10663
	for (i = 0; i < error->num_transcoders; i++) {
10701
	for (i = 0; i < error->num_transcoders; i++) {
10664
		enum transcoder cpu_transcoder = transcoders[i];
10702
		enum transcoder cpu_transcoder = transcoders[i];
10665
 
10703
 
10666
		error->transcoder[i].cpu_transcoder = cpu_transcoder;
10704
		error->transcoder[i].cpu_transcoder = cpu_transcoder;
10667
 
10705
 
10668
		error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
10706
		error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
10669
		error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
10707
		error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
10670
		error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
10708
		error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
10671
		error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
10709
		error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
10672
		error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
10710
		error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
10673
		error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
10711
		error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
10674
		error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
10712
		error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
10675
	}
10713
	}
10676
 
10714
 
10677
	/* In the code above we read the registers without checking if the power
10715
	/* In the code above we read the registers without checking if the power
10678
	 * well was on, so here we have to clear the FPGA_DBG_RM_NOCLAIM bit to
10716
	 * well was on, so here we have to clear the FPGA_DBG_RM_NOCLAIM bit to
10679
	 * prevent the next I915_WRITE from detecting it and printing an error
10717
	 * prevent the next I915_WRITE from detecting it and printing an error
10680
	 * message. */
10718
	 * message. */
10681
	intel_uncore_clear_errors(dev);
10719
	intel_uncore_clear_errors(dev);
10682
 
10720
 
10683
	return error;
10721
	return error;
10684
}
10722
}
10685
 
10723
 
10686
#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
10724
#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
10687
 
10725
 
10688
void
10726
void
10689
intel_display_print_error_state(struct drm_i915_error_state_buf *m,
10727
intel_display_print_error_state(struct drm_i915_error_state_buf *m,
10690
				struct drm_device *dev,
10728
				struct drm_device *dev,
10691
				struct intel_display_error_state *error)
10729
				struct intel_display_error_state *error)
10692
{
10730
{
10693
	int i;
10731
	int i;
10694
 
10732
 
10695
	if (!error)
10733
	if (!error)
10696
		return;
10734
		return;
10697
 
10735
 
10698
	err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
10736
	err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
10699
	if (HAS_POWER_WELL(dev))
10737
	if (HAS_POWER_WELL(dev))
10700
		err_printf(m, "PWR_WELL_CTL2: %08x\n",
10738
		err_printf(m, "PWR_WELL_CTL2: %08x\n",
10701
			   error->power_well_driver);
10739
			   error->power_well_driver);
10702
	for_each_pipe(i) {
10740
	for_each_pipe(i) {
10703
		err_printf(m, "Pipe [%d]:\n", i);
10741
		err_printf(m, "Pipe [%d]:\n", i);
10704
		err_printf(m, "  SRC: %08x\n", error->pipe[i].source);
10742
		err_printf(m, "  SRC: %08x\n", error->pipe[i].source);
10705
 
10743
 
10706
		err_printf(m, "Plane [%d]:\n", i);
10744
		err_printf(m, "Plane [%d]:\n", i);
10707
		err_printf(m, "  CNTR: %08x\n", error->plane[i].control);
10745
		err_printf(m, "  CNTR: %08x\n", error->plane[i].control);
10708
		err_printf(m, "  STRIDE: %08x\n", error->plane[i].stride);
10746
		err_printf(m, "  STRIDE: %08x\n", error->plane[i].stride);
10709
		if (INTEL_INFO(dev)->gen <= 3) {
10747
		if (INTEL_INFO(dev)->gen <= 3) {
10710
			err_printf(m, "  SIZE: %08x\n", error->plane[i].size);
10748
			err_printf(m, "  SIZE: %08x\n", error->plane[i].size);
10711
			err_printf(m, "  POS: %08x\n", error->plane[i].pos);
10749
			err_printf(m, "  POS: %08x\n", error->plane[i].pos);
10712
		}
10750
		}
10713
		if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
10751
		if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
10714
			err_printf(m, "  ADDR: %08x\n", error->plane[i].addr);
10752
			err_printf(m, "  ADDR: %08x\n", error->plane[i].addr);
10715
		if (INTEL_INFO(dev)->gen >= 4) {
10753
		if (INTEL_INFO(dev)->gen >= 4) {
10716
			err_printf(m, "  SURF: %08x\n", error->plane[i].surface);
10754
			err_printf(m, "  SURF: %08x\n", error->plane[i].surface);
10717
			err_printf(m, "  TILEOFF: %08x\n", error->plane[i].tile_offset);
10755
			err_printf(m, "  TILEOFF: %08x\n", error->plane[i].tile_offset);
10718
		}
10756
		}
10719
 
10757
 
10720
		err_printf(m, "Cursor [%d]:\n", i);
10758
		err_printf(m, "Cursor [%d]:\n", i);
10721
		err_printf(m, "  CNTR: %08x\n", error->cursor[i].control);
10759
		err_printf(m, "  CNTR: %08x\n", error->cursor[i].control);
10722
		err_printf(m, "  POS: %08x\n", error->cursor[i].position);
10760
		err_printf(m, "  POS: %08x\n", error->cursor[i].position);
10723
		err_printf(m, "  BASE: %08x\n", error->cursor[i].base);
10761
		err_printf(m, "  BASE: %08x\n", error->cursor[i].base);
10724
	}
10762
	}
10725
 
10763
 
10726
	for (i = 0; i < error->num_transcoders; i++) {
10764
	for (i = 0; i < error->num_transcoders; i++) {
10727
		err_printf(m, "  CPU transcoder: %c\n",
10765
		err_printf(m, "  CPU transcoder: %c\n",
10728
			   transcoder_name(error->transcoder[i].cpu_transcoder));
10766
			   transcoder_name(error->transcoder[i].cpu_transcoder));
10729
		err_printf(m, "  CONF: %08x\n", error->transcoder[i].conf);
10767
		err_printf(m, "  CONF: %08x\n", error->transcoder[i].conf);
10730
		err_printf(m, "  HTOTAL: %08x\n", error->transcoder[i].htotal);
10768
		err_printf(m, "  HTOTAL: %08x\n", error->transcoder[i].htotal);
10731
		err_printf(m, "  HBLANK: %08x\n", error->transcoder[i].hblank);
10769
		err_printf(m, "  HBLANK: %08x\n", error->transcoder[i].hblank);
10732
		err_printf(m, "  HSYNC: %08x\n", error->transcoder[i].hsync);
10770
		err_printf(m, "  HSYNC: %08x\n", error->transcoder[i].hsync);
10733
		err_printf(m, "  VTOTAL: %08x\n", error->transcoder[i].vtotal);
10771
		err_printf(m, "  VTOTAL: %08x\n", error->transcoder[i].vtotal);
10734
		err_printf(m, "  VBLANK: %08x\n", error->transcoder[i].vblank);
10772
		err_printf(m, "  VBLANK: %08x\n", error->transcoder[i].vblank);
10735
		err_printf(m, "  VSYNC: %08x\n", error->transcoder[i].vsync);
10773
		err_printf(m, "  VSYNC: %08x\n", error->transcoder[i].vsync);
10736
	}
10774
	}
10737
}
10775
}
10738
#endif
10776
#endif