blob: e9120843a9d7079a79b07e3422425d272c2095f0 [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
Daniel Vetter618563e2012-04-01 13:38:50 +020027#include <linux/dmi.h>
Jesse Barnesc1c7af62009-09-10 15:28:03 -070028#include <linux/module.h>
29#include <linux/input.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080030#include <linux/i2c.h>
Shaohua Li7662c8b2009-06-26 11:23:55 +080031#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Jesse Barnes9cce37f2010-08-13 15:11:26 -070033#include <linux/vgaarb.h>
Wu Fengguange0dac652011-09-05 14:25:34 +080034#include <drm/drm_edid.h>
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drmP.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include "intel_drv.h"
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/i915_drm.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080038#include "i915_drv.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070039#include "i915_trace.h"
David Howells760285e2012-10-02 18:01:07 +010040#include <drm/drm_dp_helper.h>
41#include <drm/drm_crtc_helper.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080042#include <linux/dma_remapping.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080043
Akshay Joshi0206e352011-08-16 15:34:10 -040044bool intel_pipe_has_type(struct drm_crtc *crtc, int type);
Daniel Vetter3dec0092010-08-20 21:40:52 +020045static void intel_increase_pllclock(struct drm_crtc *crtc);
Chris Wilson6b383a72010-09-13 13:54:26 +010046static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080047
Jesse Barnesf1f644d2013-06-27 00:39:25 +030048static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
49 struct intel_crtc_config *pipe_config);
50static void ironlake_crtc_clock_get(struct intel_crtc *crtc,
51 struct intel_crtc_config *pipe_config);
52
Jesse Barnes79e53942008-11-07 14:24:08 -080053typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040054 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -080055} intel_range_t;
56
57typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040058 int dot_limit;
59 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -080060} intel_p2_t;
61
62#define INTEL_P2_NUM 2
Ma Lingd4906092009-03-18 20:13:27 +080063typedef struct intel_limit intel_limit_t;
64struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -040065 intel_range_t dot, vco, n, m, m1, m2, p, p1;
66 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +080067};
Jesse Barnes79e53942008-11-07 14:24:08 -080068
Jesse Barnes2377b742010-07-07 14:06:43 -070069/* FDI */
70#define IRONLAKE_FDI_FREQ 2700000 /* in kHz for mode->clock */
71
Daniel Vetterd2acd212012-10-20 20:57:43 +020072int
73intel_pch_rawclk(struct drm_device *dev)
74{
75 struct drm_i915_private *dev_priv = dev->dev_private;
76
77 WARN_ON(!HAS_PCH_SPLIT(dev));
78
79 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
80}
81
Chris Wilson021357a2010-09-07 20:54:59 +010082static inline u32 /* units of 100MHz */
83intel_fdi_link_freq(struct drm_device *dev)
84{
Chris Wilson8b99e682010-10-13 09:59:17 +010085 if (IS_GEN5(dev)) {
86 struct drm_i915_private *dev_priv = dev->dev_private;
87 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
88 } else
89 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +010090}
91
Daniel Vetter5d536e22013-07-06 12:52:06 +020092static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -040093 .dot = { .min = 25000, .max = 350000 },
94 .vco = { .min = 930000, .max = 1400000 },
95 .n = { .min = 3, .max = 16 },
96 .m = { .min = 96, .max = 140 },
97 .m1 = { .min = 18, .max = 26 },
98 .m2 = { .min = 6, .max = 16 },
99 .p = { .min = 4, .max = 128 },
100 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700101 .p2 = { .dot_limit = 165000,
102 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700103};
104
Daniel Vetter5d536e22013-07-06 12:52:06 +0200105static const intel_limit_t intel_limits_i8xx_dvo = {
106 .dot = { .min = 25000, .max = 350000 },
107 .vco = { .min = 930000, .max = 1400000 },
108 .n = { .min = 3, .max = 16 },
109 .m = { .min = 96, .max = 140 },
110 .m1 = { .min = 18, .max = 26 },
111 .m2 = { .min = 6, .max = 16 },
112 .p = { .min = 4, .max = 128 },
113 .p1 = { .min = 2, .max = 33 },
114 .p2 = { .dot_limit = 165000,
115 .p2_slow = 4, .p2_fast = 4 },
116};
117
Keith Packarde4b36692009-06-05 19:22:17 -0700118static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400119 .dot = { .min = 25000, .max = 350000 },
120 .vco = { .min = 930000, .max = 1400000 },
121 .n = { .min = 3, .max = 16 },
122 .m = { .min = 96, .max = 140 },
123 .m1 = { .min = 18, .max = 26 },
124 .m2 = { .min = 6, .max = 16 },
125 .p = { .min = 4, .max = 128 },
126 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700127 .p2 = { .dot_limit = 165000,
128 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700129};
Eric Anholt273e27c2011-03-30 13:01:10 -0700130
Keith Packarde4b36692009-06-05 19:22:17 -0700131static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400132 .dot = { .min = 20000, .max = 400000 },
133 .vco = { .min = 1400000, .max = 2800000 },
134 .n = { .min = 1, .max = 6 },
135 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100136 .m1 = { .min = 8, .max = 18 },
137 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400138 .p = { .min = 5, .max = 80 },
139 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700140 .p2 = { .dot_limit = 200000,
141 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700142};
143
144static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400145 .dot = { .min = 20000, .max = 400000 },
146 .vco = { .min = 1400000, .max = 2800000 },
147 .n = { .min = 1, .max = 6 },
148 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100149 .m1 = { .min = 8, .max = 18 },
150 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400151 .p = { .min = 7, .max = 98 },
152 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700153 .p2 = { .dot_limit = 112000,
154 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700155};
156
Eric Anholt273e27c2011-03-30 13:01:10 -0700157
Keith Packarde4b36692009-06-05 19:22:17 -0700158static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700159 .dot = { .min = 25000, .max = 270000 },
160 .vco = { .min = 1750000, .max = 3500000},
161 .n = { .min = 1, .max = 4 },
162 .m = { .min = 104, .max = 138 },
163 .m1 = { .min = 17, .max = 23 },
164 .m2 = { .min = 5, .max = 11 },
165 .p = { .min = 10, .max = 30 },
166 .p1 = { .min = 1, .max = 3},
167 .p2 = { .dot_limit = 270000,
168 .p2_slow = 10,
169 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800170 },
Keith Packarde4b36692009-06-05 19:22:17 -0700171};
172
173static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700174 .dot = { .min = 22000, .max = 400000 },
175 .vco = { .min = 1750000, .max = 3500000},
176 .n = { .min = 1, .max = 4 },
177 .m = { .min = 104, .max = 138 },
178 .m1 = { .min = 16, .max = 23 },
179 .m2 = { .min = 5, .max = 11 },
180 .p = { .min = 5, .max = 80 },
181 .p1 = { .min = 1, .max = 8},
182 .p2 = { .dot_limit = 165000,
183 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700184};
185
186static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700187 .dot = { .min = 20000, .max = 115000 },
188 .vco = { .min = 1750000, .max = 3500000 },
189 .n = { .min = 1, .max = 3 },
190 .m = { .min = 104, .max = 138 },
191 .m1 = { .min = 17, .max = 23 },
192 .m2 = { .min = 5, .max = 11 },
193 .p = { .min = 28, .max = 112 },
194 .p1 = { .min = 2, .max = 8 },
195 .p2 = { .dot_limit = 0,
196 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800197 },
Keith Packarde4b36692009-06-05 19:22:17 -0700198};
199
200static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700201 .dot = { .min = 80000, .max = 224000 },
202 .vco = { .min = 1750000, .max = 3500000 },
203 .n = { .min = 1, .max = 3 },
204 .m = { .min = 104, .max = 138 },
205 .m1 = { .min = 17, .max = 23 },
206 .m2 = { .min = 5, .max = 11 },
207 .p = { .min = 14, .max = 42 },
208 .p1 = { .min = 2, .max = 6 },
209 .p2 = { .dot_limit = 0,
210 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800211 },
Keith Packarde4b36692009-06-05 19:22:17 -0700212};
213
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500214static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400215 .dot = { .min = 20000, .max = 400000},
216 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700217 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400218 .n = { .min = 3, .max = 6 },
219 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700220 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400221 .m1 = { .min = 0, .max = 0 },
222 .m2 = { .min = 0, .max = 254 },
223 .p = { .min = 5, .max = 80 },
224 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700225 .p2 = { .dot_limit = 200000,
226 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700227};
228
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500229static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400230 .dot = { .min = 20000, .max = 400000 },
231 .vco = { .min = 1700000, .max = 3500000 },
232 .n = { .min = 3, .max = 6 },
233 .m = { .min = 2, .max = 256 },
234 .m1 = { .min = 0, .max = 0 },
235 .m2 = { .min = 0, .max = 254 },
236 .p = { .min = 7, .max = 112 },
237 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700238 .p2 = { .dot_limit = 112000,
239 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700240};
241
Eric Anholt273e27c2011-03-30 13:01:10 -0700242/* Ironlake / Sandybridge
243 *
244 * We calculate clock using (register_value + 2) for N/M1/M2, so here
245 * the range value for them is (actual_value - 2).
246 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800247static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700248 .dot = { .min = 25000, .max = 350000 },
249 .vco = { .min = 1760000, .max = 3510000 },
250 .n = { .min = 1, .max = 5 },
251 .m = { .min = 79, .max = 127 },
252 .m1 = { .min = 12, .max = 22 },
253 .m2 = { .min = 5, .max = 9 },
254 .p = { .min = 5, .max = 80 },
255 .p1 = { .min = 1, .max = 8 },
256 .p2 = { .dot_limit = 225000,
257 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700258};
259
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800260static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700261 .dot = { .min = 25000, .max = 350000 },
262 .vco = { .min = 1760000, .max = 3510000 },
263 .n = { .min = 1, .max = 3 },
264 .m = { .min = 79, .max = 118 },
265 .m1 = { .min = 12, .max = 22 },
266 .m2 = { .min = 5, .max = 9 },
267 .p = { .min = 28, .max = 112 },
268 .p1 = { .min = 2, .max = 8 },
269 .p2 = { .dot_limit = 225000,
270 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800271};
272
273static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700274 .dot = { .min = 25000, .max = 350000 },
275 .vco = { .min = 1760000, .max = 3510000 },
276 .n = { .min = 1, .max = 3 },
277 .m = { .min = 79, .max = 127 },
278 .m1 = { .min = 12, .max = 22 },
279 .m2 = { .min = 5, .max = 9 },
280 .p = { .min = 14, .max = 56 },
281 .p1 = { .min = 2, .max = 8 },
282 .p2 = { .dot_limit = 225000,
283 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800284};
285
Eric Anholt273e27c2011-03-30 13:01:10 -0700286/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800287static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700288 .dot = { .min = 25000, .max = 350000 },
289 .vco = { .min = 1760000, .max = 3510000 },
290 .n = { .min = 1, .max = 2 },
291 .m = { .min = 79, .max = 126 },
292 .m1 = { .min = 12, .max = 22 },
293 .m2 = { .min = 5, .max = 9 },
294 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400295 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700296 .p2 = { .dot_limit = 225000,
297 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800298};
299
300static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700301 .dot = { .min = 25000, .max = 350000 },
302 .vco = { .min = 1760000, .max = 3510000 },
303 .n = { .min = 1, .max = 3 },
304 .m = { .min = 79, .max = 126 },
305 .m1 = { .min = 12, .max = 22 },
306 .m2 = { .min = 5, .max = 9 },
307 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400308 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700309 .p2 = { .dot_limit = 225000,
310 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800311};
312
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700313static const intel_limit_t intel_limits_vlv_dac = {
314 .dot = { .min = 25000, .max = 270000 },
315 .vco = { .min = 4000000, .max = 6000000 },
316 .n = { .min = 1, .max = 7 },
317 .m = { .min = 22, .max = 450 }, /* guess */
318 .m1 = { .min = 2, .max = 3 },
319 .m2 = { .min = 11, .max = 156 },
320 .p = { .min = 10, .max = 30 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200321 .p1 = { .min = 1, .max = 3 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700322 .p2 = { .dot_limit = 270000,
323 .p2_slow = 2, .p2_fast = 20 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700324};
325
326static const intel_limit_t intel_limits_vlv_hdmi = {
Daniel Vetter75e53982013-04-18 21:10:43 +0200327 .dot = { .min = 25000, .max = 270000 },
328 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700329 .n = { .min = 1, .max = 7 },
330 .m = { .min = 60, .max = 300 }, /* guess */
331 .m1 = { .min = 2, .max = 3 },
332 .m2 = { .min = 11, .max = 156 },
333 .p = { .min = 10, .max = 30 },
334 .p1 = { .min = 2, .max = 3 },
335 .p2 = { .dot_limit = 270000,
336 .p2_slow = 2, .p2_fast = 20 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700337};
338
339static const intel_limit_t intel_limits_vlv_dp = {
Vijay Purushothaman74a4dd22012-09-27 19:13:04 +0530340 .dot = { .min = 25000, .max = 270000 },
341 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700342 .n = { .min = 1, .max = 7 },
Vijay Purushothaman74a4dd22012-09-27 19:13:04 +0530343 .m = { .min = 22, .max = 450 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700344 .m1 = { .min = 2, .max = 3 },
345 .m2 = { .min = 11, .max = 156 },
346 .p = { .min = 10, .max = 30 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200347 .p1 = { .min = 1, .max = 3 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700348 .p2 = { .dot_limit = 270000,
349 .p2_slow = 2, .p2_fast = 20 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700350};
351
Chris Wilson1b894b52010-12-14 20:04:54 +0000352static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
353 int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800354{
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800355 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800356 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800357
358 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100359 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000360 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800361 limit = &intel_limits_ironlake_dual_lvds_100m;
362 else
363 limit = &intel_limits_ironlake_dual_lvds;
364 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000365 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800366 limit = &intel_limits_ironlake_single_lvds_100m;
367 else
368 limit = &intel_limits_ironlake_single_lvds;
369 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200370 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800371 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800372
373 return limit;
374}
375
Ma Ling044c7c42009-03-18 20:13:23 +0800376static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
377{
378 struct drm_device *dev = crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800379 const intel_limit_t *limit;
380
381 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100382 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700383 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800384 else
Keith Packarde4b36692009-06-05 19:22:17 -0700385 limit = &intel_limits_g4x_single_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800386 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
387 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700388 limit = &intel_limits_g4x_hdmi;
Ma Ling044c7c42009-03-18 20:13:23 +0800389 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700390 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800391 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700392 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800393
394 return limit;
395}
396
Chris Wilson1b894b52010-12-14 20:04:54 +0000397static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800398{
399 struct drm_device *dev = crtc->dev;
400 const intel_limit_t *limit;
401
Eric Anholtbad720f2009-10-22 16:11:14 -0700402 if (HAS_PCH_SPLIT(dev))
Chris Wilson1b894b52010-12-14 20:04:54 +0000403 limit = intel_ironlake_limit(crtc, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800404 else if (IS_G4X(dev)) {
Ma Ling044c7c42009-03-18 20:13:23 +0800405 limit = intel_g4x_limit(crtc);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500406 } else if (IS_PINEVIEW(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +0800407 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500408 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800409 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500410 limit = &intel_limits_pineview_sdvo;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700411 } else if (IS_VALLEYVIEW(dev)) {
412 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG))
413 limit = &intel_limits_vlv_dac;
414 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
415 limit = &intel_limits_vlv_hdmi;
416 else
417 limit = &intel_limits_vlv_dp;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100418 } else if (!IS_GEN2(dev)) {
419 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
420 limit = &intel_limits_i9xx_lvds;
421 else
422 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800423 } else {
424 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700425 limit = &intel_limits_i8xx_lvds;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200426 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700427 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200428 else
429 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800430 }
431 return limit;
432}
433
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500434/* m1 is reserved as 0 in Pineview, n is a ring counter */
435static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800436{
Shaohua Li21778322009-02-23 15:19:16 +0800437 clock->m = clock->m2 + 2;
438 clock->p = clock->p1 * clock->p2;
439 clock->vco = refclk * clock->m / clock->n;
440 clock->dot = clock->vco / clock->p;
441}
442
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200443static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
444{
445 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
446}
447
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200448static void i9xx_clock(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800449{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200450 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800451 clock->p = clock->p1 * clock->p2;
452 clock->vco = refclk * clock->m / (clock->n + 2);
453 clock->dot = clock->vco / clock->p;
454}
455
Jesse Barnes79e53942008-11-07 14:24:08 -0800456/**
457 * Returns whether any output on the specified pipe is of the specified type
458 */
Chris Wilson4ef69c72010-09-09 15:14:28 +0100459bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
Jesse Barnes79e53942008-11-07 14:24:08 -0800460{
Chris Wilson4ef69c72010-09-09 15:14:28 +0100461 struct drm_device *dev = crtc->dev;
Chris Wilson4ef69c72010-09-09 15:14:28 +0100462 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -0800463
Daniel Vetter6c2b7c122012-07-05 09:50:24 +0200464 for_each_encoder_on_crtc(dev, crtc, encoder)
465 if (encoder->type == type)
Chris Wilson4ef69c72010-09-09 15:14:28 +0100466 return true;
467
468 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -0800469}
470
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800471#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800472/**
473 * Returns whether the given set of divisors are valid for a given refclk with
474 * the given connectors.
475 */
476
Chris Wilson1b894b52010-12-14 20:04:54 +0000477static bool intel_PLL_is_valid(struct drm_device *dev,
478 const intel_limit_t *limit,
479 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800480{
Jesse Barnes79e53942008-11-07 14:24:08 -0800481 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400482 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800483 if (clock->p < limit->p.min || limit->p.max < clock->p)
Akshay Joshi0206e352011-08-16 15:34:10 -0400484 INTELPllInvalid("p out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800485 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400486 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800487 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400488 INTELPllInvalid("m1 out of range\n");
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500489 if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
Akshay Joshi0206e352011-08-16 15:34:10 -0400490 INTELPllInvalid("m1 <= m2\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800491 if (clock->m < limit->m.min || limit->m.max < clock->m)
Akshay Joshi0206e352011-08-16 15:34:10 -0400492 INTELPllInvalid("m out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800493 if (clock->n < limit->n.min || limit->n.max < clock->n)
Akshay Joshi0206e352011-08-16 15:34:10 -0400494 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800495 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400496 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800497 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
498 * connector, etc., rather than just a single range.
499 */
500 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400501 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800502
503 return true;
504}
505
Ma Lingd4906092009-03-18 20:13:27 +0800506static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200507i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800508 int target, int refclk, intel_clock_t *match_clock,
509 intel_clock_t *best_clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800510{
511 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800512 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800513 int err = target;
514
Daniel Vettera210b022012-11-26 17:22:08 +0100515 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800516 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100517 * For LVDS just rely on its current settings for dual-channel.
518 * We haven't figured out how to reliably set up different
519 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800520 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100521 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800522 clock.p2 = limit->p2.p2_fast;
523 else
524 clock.p2 = limit->p2.p2_slow;
525 } else {
526 if (target < limit->p2.dot_limit)
527 clock.p2 = limit->p2.p2_slow;
528 else
529 clock.p2 = limit->p2.p2_fast;
530 }
531
Akshay Joshi0206e352011-08-16 15:34:10 -0400532 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800533
Zhao Yakui42158662009-11-20 11:24:18 +0800534 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
535 clock.m1++) {
536 for (clock.m2 = limit->m2.min;
537 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200538 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800539 break;
540 for (clock.n = limit->n.min;
541 clock.n <= limit->n.max; clock.n++) {
542 for (clock.p1 = limit->p1.min;
543 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800544 int this_err;
545
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200546 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000547 if (!intel_PLL_is_valid(dev, limit,
548 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800549 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800550 if (match_clock &&
551 clock.p != match_clock->p)
552 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800553
554 this_err = abs(clock.dot - target);
555 if (this_err < err) {
556 *best_clock = clock;
557 err = this_err;
558 }
559 }
560 }
561 }
562 }
563
564 return (err != target);
565}
566
Ma Lingd4906092009-03-18 20:13:27 +0800567static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200568pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
569 int target, int refclk, intel_clock_t *match_clock,
570 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200571{
572 struct drm_device *dev = crtc->dev;
573 intel_clock_t clock;
574 int err = target;
575
576 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
577 /*
578 * For LVDS just rely on its current settings for dual-channel.
579 * We haven't figured out how to reliably set up different
580 * single/dual channel state, if we even can.
581 */
582 if (intel_is_dual_link_lvds(dev))
583 clock.p2 = limit->p2.p2_fast;
584 else
585 clock.p2 = limit->p2.p2_slow;
586 } else {
587 if (target < limit->p2.dot_limit)
588 clock.p2 = limit->p2.p2_slow;
589 else
590 clock.p2 = limit->p2.p2_fast;
591 }
592
593 memset(best_clock, 0, sizeof(*best_clock));
594
595 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
596 clock.m1++) {
597 for (clock.m2 = limit->m2.min;
598 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200599 for (clock.n = limit->n.min;
600 clock.n <= limit->n.max; clock.n++) {
601 for (clock.p1 = limit->p1.min;
602 clock.p1 <= limit->p1.max; clock.p1++) {
603 int this_err;
604
605 pineview_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800606 if (!intel_PLL_is_valid(dev, limit,
607 &clock))
608 continue;
609 if (match_clock &&
610 clock.p != match_clock->p)
611 continue;
612
613 this_err = abs(clock.dot - target);
614 if (this_err < err) {
615 *best_clock = clock;
616 err = this_err;
617 }
618 }
619 }
620 }
621 }
622
623 return (err != target);
624}
625
Ma Lingd4906092009-03-18 20:13:27 +0800626static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200627g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
628 int target, int refclk, intel_clock_t *match_clock,
629 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800630{
631 struct drm_device *dev = crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800632 intel_clock_t clock;
633 int max_n;
634 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400635 /* approximately equals target * 0.00585 */
636 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800637 found = false;
638
639 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100640 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800641 clock.p2 = limit->p2.p2_fast;
642 else
643 clock.p2 = limit->p2.p2_slow;
644 } else {
645 if (target < limit->p2.dot_limit)
646 clock.p2 = limit->p2.p2_slow;
647 else
648 clock.p2 = limit->p2.p2_fast;
649 }
650
651 memset(best_clock, 0, sizeof(*best_clock));
652 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200653 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800654 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200655 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800656 for (clock.m1 = limit->m1.max;
657 clock.m1 >= limit->m1.min; clock.m1--) {
658 for (clock.m2 = limit->m2.max;
659 clock.m2 >= limit->m2.min; clock.m2--) {
660 for (clock.p1 = limit->p1.max;
661 clock.p1 >= limit->p1.min; clock.p1--) {
662 int this_err;
663
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200664 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000665 if (!intel_PLL_is_valid(dev, limit,
666 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800667 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000668
669 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800670 if (this_err < err_most) {
671 *best_clock = clock;
672 err_most = this_err;
673 max_n = clock.n;
674 found = true;
675 }
676 }
677 }
678 }
679 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800680 return found;
681}
Ma Lingd4906092009-03-18 20:13:27 +0800682
Zhenyu Wang2c072452009-06-05 15:38:42 +0800683static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200684vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
685 int target, int refclk, intel_clock_t *match_clock,
686 intel_clock_t *best_clock)
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700687{
688 u32 p1, p2, m1, m2, vco, bestn, bestm1, bestm2, bestp1, bestp2;
689 u32 m, n, fastclk;
690 u32 updrate, minupdate, fracbits, p;
691 unsigned long bestppm, ppm, absppm;
692 int dotclk, flag;
693
Alan Coxaf447bd2012-07-25 13:49:18 +0100694 flag = 0;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700695 dotclk = target * 1000;
696 bestppm = 1000000;
697 ppm = absppm = 0;
698 fastclk = dotclk / (2*100);
699 updrate = 0;
700 minupdate = 19200;
701 fracbits = 1;
702 n = p = p1 = p2 = m = m1 = m2 = vco = bestn = 0;
703 bestm1 = bestm2 = bestp1 = bestp2 = 0;
704
705 /* based on hardware requirement, prefer smaller n to precision */
706 for (n = limit->n.min; n <= ((refclk) / minupdate); n++) {
707 updrate = refclk / n;
708 for (p1 = limit->p1.max; p1 > limit->p1.min; p1--) {
709 for (p2 = limit->p2.p2_fast+1; p2 > 0; p2--) {
710 if (p2 > 10)
711 p2 = p2 - 1;
712 p = p1 * p2;
713 /* based on hardware requirement, prefer bigger m1,m2 values */
714 for (m1 = limit->m1.min; m1 <= limit->m1.max; m1++) {
715 m2 = (((2*(fastclk * p * n / m1 )) +
716 refclk) / (2*refclk));
717 m = m1 * m2;
718 vco = updrate * m;
719 if (vco >= limit->vco.min && vco < limit->vco.max) {
720 ppm = 1000000 * ((vco / p) - fastclk) / fastclk;
721 absppm = (ppm > 0) ? ppm : (-ppm);
722 if (absppm < 100 && ((p1 * p2) > (bestp1 * bestp2))) {
723 bestppm = 0;
724 flag = 1;
725 }
726 if (absppm < bestppm - 10) {
727 bestppm = absppm;
728 flag = 1;
729 }
730 if (flag) {
731 bestn = n;
732 bestm1 = m1;
733 bestm2 = m2;
734 bestp1 = p1;
735 bestp2 = p2;
736 flag = 0;
737 }
738 }
739 }
740 }
741 }
742 }
743 best_clock->n = bestn;
744 best_clock->m1 = bestm1;
745 best_clock->m2 = bestm2;
746 best_clock->p1 = bestp1;
747 best_clock->p2 = bestp2;
748
749 return true;
750}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700751
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200752enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
753 enum pipe pipe)
754{
755 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
756 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
757
Daniel Vetter3b117c82013-04-17 20:15:07 +0200758 return intel_crtc->config.cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200759}
760
Paulo Zanonia928d532012-05-04 17:18:15 -0300761static void ironlake_wait_for_vblank(struct drm_device *dev, int pipe)
762{
763 struct drm_i915_private *dev_priv = dev->dev_private;
764 u32 frame, frame_reg = PIPEFRAME(pipe);
765
766 frame = I915_READ(frame_reg);
767
768 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
769 DRM_DEBUG_KMS("vblank wait timed out\n");
770}
771
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700772/**
773 * intel_wait_for_vblank - wait for vblank on a given pipe
774 * @dev: drm device
775 * @pipe: pipe to wait for
776 *
777 * Wait for vblank to occur on a given pipe. Needed for various bits of
778 * mode setting code.
779 */
780void intel_wait_for_vblank(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -0800781{
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700782 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800783 int pipestat_reg = PIPESTAT(pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700784
Paulo Zanonia928d532012-05-04 17:18:15 -0300785 if (INTEL_INFO(dev)->gen >= 5) {
786 ironlake_wait_for_vblank(dev, pipe);
787 return;
788 }
789
Chris Wilson300387c2010-09-05 20:25:43 +0100790 /* Clear existing vblank status. Note this will clear any other
791 * sticky status fields as well.
792 *
793 * This races with i915_driver_irq_handler() with the result
794 * that either function could miss a vblank event. Here it is not
795 * fatal, as we will either wait upon the next vblank interrupt or
796 * timeout. Generally speaking intel_wait_for_vblank() is only
797 * called during modeset at which time the GPU should be idle and
798 * should *not* be performing page flips and thus not waiting on
799 * vblanks...
800 * Currently, the result of us stealing a vblank from the irq
801 * handler is that a single frame will be skipped during swapbuffers.
802 */
803 I915_WRITE(pipestat_reg,
804 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
805
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700806 /* Wait for vblank interrupt bit to set */
Chris Wilson481b6af2010-08-23 17:43:35 +0100807 if (wait_for(I915_READ(pipestat_reg) &
808 PIPE_VBLANK_INTERRUPT_STATUS,
809 50))
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700810 DRM_DEBUG_KMS("vblank wait timed out\n");
811}
812
Keith Packardab7ad7f2010-10-03 00:33:06 -0700813/*
814 * intel_wait_for_pipe_off - wait for pipe to turn off
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700815 * @dev: drm device
816 * @pipe: pipe to wait for
817 *
818 * After disabling a pipe, we can't wait for vblank in the usual way,
819 * spinning on the vblank interrupt status bit, since we won't actually
820 * see an interrupt when the pipe is disabled.
821 *
Keith Packardab7ad7f2010-10-03 00:33:06 -0700822 * On Gen4 and above:
823 * wait for the pipe register state bit to turn off
824 *
825 * Otherwise:
826 * wait for the display line value to settle (it usually
827 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +0100828 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700829 */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100830void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700831{
832 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200833 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
834 pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700835
Keith Packardab7ad7f2010-10-03 00:33:06 -0700836 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200837 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700838
Keith Packardab7ad7f2010-10-03 00:33:06 -0700839 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100840 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
841 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200842 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700843 } else {
Paulo Zanoni837ba002012-05-04 17:18:14 -0300844 u32 last_line, line_mask;
Chris Wilson58e10eb2010-10-03 10:56:11 +0100845 int reg = PIPEDSL(pipe);
Keith Packardab7ad7f2010-10-03 00:33:06 -0700846 unsigned long timeout = jiffies + msecs_to_jiffies(100);
847
Paulo Zanoni837ba002012-05-04 17:18:14 -0300848 if (IS_GEN2(dev))
849 line_mask = DSL_LINEMASK_GEN2;
850 else
851 line_mask = DSL_LINEMASK_GEN3;
852
Keith Packardab7ad7f2010-10-03 00:33:06 -0700853 /* Wait for the display line to settle */
854 do {
Paulo Zanoni837ba002012-05-04 17:18:14 -0300855 last_line = I915_READ(reg) & line_mask;
Keith Packardab7ad7f2010-10-03 00:33:06 -0700856 mdelay(5);
Paulo Zanoni837ba002012-05-04 17:18:14 -0300857 } while (((I915_READ(reg) & line_mask) != last_line) &&
Keith Packardab7ad7f2010-10-03 00:33:06 -0700858 time_after(timeout, jiffies));
859 if (time_after(jiffies, timeout))
Daniel Vetter284637d2012-07-09 09:51:57 +0200860 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700861 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800862}
863
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000864/*
865 * ibx_digital_port_connected - is the specified port connected?
866 * @dev_priv: i915 private structure
867 * @port: the port to test
868 *
869 * Returns true if @port is connected, false otherwise.
870 */
871bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
872 struct intel_digital_port *port)
873{
874 u32 bit;
875
Damien Lespiauc36346e2012-12-13 16:09:03 +0000876 if (HAS_PCH_IBX(dev_priv->dev)) {
877 switch(port->port) {
878 case PORT_B:
879 bit = SDE_PORTB_HOTPLUG;
880 break;
881 case PORT_C:
882 bit = SDE_PORTC_HOTPLUG;
883 break;
884 case PORT_D:
885 bit = SDE_PORTD_HOTPLUG;
886 break;
887 default:
888 return true;
889 }
890 } else {
891 switch(port->port) {
892 case PORT_B:
893 bit = SDE_PORTB_HOTPLUG_CPT;
894 break;
895 case PORT_C:
896 bit = SDE_PORTC_HOTPLUG_CPT;
897 break;
898 case PORT_D:
899 bit = SDE_PORTD_HOTPLUG_CPT;
900 break;
901 default:
902 return true;
903 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000904 }
905
906 return I915_READ(SDEISR) & bit;
907}
908
Jesse Barnesb24e7172011-01-04 15:09:30 -0800909static const char *state_string(bool enabled)
910{
911 return enabled ? "on" : "off";
912}
913
914/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +0200915void assert_pll(struct drm_i915_private *dev_priv,
916 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -0800917{
918 int reg;
919 u32 val;
920 bool cur_state;
921
922 reg = DPLL(pipe);
923 val = I915_READ(reg);
924 cur_state = !!(val & DPLL_VCO_ENABLE);
925 WARN(cur_state != state,
926 "PLL state assertion failure (expected %s, current %s)\n",
927 state_string(state), state_string(cur_state));
928}
Jesse Barnesb24e7172011-01-04 15:09:30 -0800929
Daniel Vetter55607e82013-06-16 21:42:39 +0200930struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +0200931intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -0800932{
Daniel Vettere2b78262013-06-07 23:10:03 +0200933 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
934
Daniel Vettera43f6e02013-06-07 23:10:32 +0200935 if (crtc->config.shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +0200936 return NULL;
937
Daniel Vettera43f6e02013-06-07 23:10:32 +0200938 return &dev_priv->shared_dplls[crtc->config.shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +0200939}
940
Jesse Barnesb24e7172011-01-04 15:09:30 -0800941/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +0200942void assert_shared_dpll(struct drm_i915_private *dev_priv,
943 struct intel_shared_dpll *pll,
944 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -0800945{
Jesse Barnes040484a2011-01-03 12:14:26 -0800946 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +0200947 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -0800948
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -0300949 if (HAS_PCH_LPT(dev_priv->dev)) {
950 DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
951 return;
952 }
953
Chris Wilson92b27b02012-05-20 18:10:50 +0100954 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +0200955 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +0100956 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +0100957
Daniel Vetter53589012013-06-05 13:34:16 +0200958 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Chris Wilson92b27b02012-05-20 18:10:50 +0100959 WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +0200960 "%s assertion failure (expected %s, current %s)\n",
961 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -0800962}
Jesse Barnes040484a2011-01-03 12:14:26 -0800963
964static void assert_fdi_tx(struct drm_i915_private *dev_priv,
965 enum pipe pipe, bool state)
966{
967 int reg;
968 u32 val;
969 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -0200970 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
971 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -0800972
Paulo Zanoniaffa9352012-11-23 15:30:39 -0200973 if (HAS_DDI(dev_priv->dev)) {
974 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -0200975 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -0300976 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -0200977 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -0300978 } else {
979 reg = FDI_TX_CTL(pipe);
980 val = I915_READ(reg);
981 cur_state = !!(val & FDI_TX_ENABLE);
982 }
Jesse Barnes040484a2011-01-03 12:14:26 -0800983 WARN(cur_state != state,
984 "FDI TX state assertion failure (expected %s, current %s)\n",
985 state_string(state), state_string(cur_state));
986}
987#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
988#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
989
990static void assert_fdi_rx(struct drm_i915_private *dev_priv,
991 enum pipe pipe, bool state)
992{
993 int reg;
994 u32 val;
995 bool cur_state;
996
Paulo Zanonid63fa0d2012-11-20 13:27:35 -0200997 reg = FDI_RX_CTL(pipe);
998 val = I915_READ(reg);
999 cur_state = !!(val & FDI_RX_ENABLE);
Jesse Barnes040484a2011-01-03 12:14:26 -08001000 WARN(cur_state != state,
1001 "FDI RX state assertion failure (expected %s, current %s)\n",
1002 state_string(state), state_string(cur_state));
1003}
1004#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1005#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1006
1007static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1008 enum pipe pipe)
1009{
1010 int reg;
1011 u32 val;
1012
1013 /* ILK FDI PLL is always enabled */
1014 if (dev_priv->info->gen == 5)
1015 return;
1016
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001017 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001018 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001019 return;
1020
Jesse Barnes040484a2011-01-03 12:14:26 -08001021 reg = FDI_TX_CTL(pipe);
1022 val = I915_READ(reg);
1023 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1024}
1025
Daniel Vetter55607e82013-06-16 21:42:39 +02001026void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1027 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001028{
1029 int reg;
1030 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001031 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001032
1033 reg = FDI_RX_CTL(pipe);
1034 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001035 cur_state = !!(val & FDI_RX_PLL_ENABLE);
1036 WARN(cur_state != state,
1037 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1038 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001039}
1040
Jesse Barnesea0760c2011-01-04 15:09:32 -08001041static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1042 enum pipe pipe)
1043{
1044 int pp_reg, lvds_reg;
1045 u32 val;
1046 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001047 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001048
1049 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1050 pp_reg = PCH_PP_CONTROL;
1051 lvds_reg = PCH_LVDS;
1052 } else {
1053 pp_reg = PP_CONTROL;
1054 lvds_reg = LVDS;
1055 }
1056
1057 val = I915_READ(pp_reg);
1058 if (!(val & PANEL_POWER_ON) ||
1059 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1060 locked = false;
1061
1062 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1063 panel_pipe = PIPE_B;
1064
1065 WARN(panel_pipe == pipe && locked,
1066 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001067 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001068}
1069
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001070void assert_pipe(struct drm_i915_private *dev_priv,
1071 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001072{
1073 int reg;
1074 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001075 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001076 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1077 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001078
Daniel Vetter8e636782012-01-22 01:36:48 +01001079 /* if we need the pipe A quirk it must be always on */
1080 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1081 state = true;
1082
Paulo Zanonib97186f2013-05-03 12:15:36 -03001083 if (!intel_display_power_enabled(dev_priv->dev,
1084 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001085 cur_state = false;
1086 } else {
1087 reg = PIPECONF(cpu_transcoder);
1088 val = I915_READ(reg);
1089 cur_state = !!(val & PIPECONF_ENABLE);
1090 }
1091
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001092 WARN(cur_state != state,
1093 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001094 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001095}
1096
Chris Wilson931872f2012-01-16 23:01:13 +00001097static void assert_plane(struct drm_i915_private *dev_priv,
1098 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001099{
1100 int reg;
1101 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001102 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001103
1104 reg = DSPCNTR(plane);
1105 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001106 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1107 WARN(cur_state != state,
1108 "plane %c assertion failure (expected %s, current %s)\n",
1109 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001110}
1111
Chris Wilson931872f2012-01-16 23:01:13 +00001112#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1113#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1114
Jesse Barnesb24e7172011-01-04 15:09:30 -08001115static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1116 enum pipe pipe)
1117{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001118 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001119 int reg, i;
1120 u32 val;
1121 int cur_pipe;
1122
Ville Syrjälä653e1022013-06-04 13:49:05 +03001123 /* Primary planes are fixed to pipes on gen4+ */
1124 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001125 reg = DSPCNTR(pipe);
1126 val = I915_READ(reg);
1127 WARN((val & DISPLAY_PLANE_ENABLE),
1128 "plane %c assertion failure, should be disabled but not\n",
1129 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001130 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001131 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001132
Jesse Barnesb24e7172011-01-04 15:09:30 -08001133 /* Need to check both planes against the pipe */
Damien Lespiau08e2a7d2013-07-11 20:10:54 +01001134 for_each_pipe(i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001135 reg = DSPCNTR(i);
1136 val = I915_READ(reg);
1137 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1138 DISPPLANE_SEL_PIPE_SHIFT;
1139 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001140 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1141 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001142 }
1143}
1144
Jesse Barnes19332d72013-03-28 09:55:38 -07001145static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1146 enum pipe pipe)
1147{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001148 struct drm_device *dev = dev_priv->dev;
Jesse Barnes19332d72013-03-28 09:55:38 -07001149 int reg, i;
1150 u32 val;
1151
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001152 if (IS_VALLEYVIEW(dev)) {
1153 for (i = 0; i < dev_priv->num_plane; i++) {
1154 reg = SPCNTR(pipe, i);
1155 val = I915_READ(reg);
1156 WARN((val & SP_ENABLE),
1157 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1158 sprite_name(pipe, i), pipe_name(pipe));
1159 }
1160 } else if (INTEL_INFO(dev)->gen >= 7) {
1161 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001162 val = I915_READ(reg);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001163 WARN((val & SPRITE_ENABLE),
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001164 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001165 plane_name(pipe), pipe_name(pipe));
1166 } else if (INTEL_INFO(dev)->gen >= 5) {
1167 reg = DVSCNTR(pipe);
1168 val = I915_READ(reg);
1169 WARN((val & DVS_ENABLE),
1170 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1171 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001172 }
1173}
1174
Jesse Barnes92f25842011-01-04 15:09:34 -08001175static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1176{
1177 u32 val;
1178 bool enabled;
1179
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001180 if (HAS_PCH_LPT(dev_priv->dev)) {
1181 DRM_DEBUG_DRIVER("LPT does not has PCH refclk, skipping check\n");
1182 return;
1183 }
1184
Jesse Barnes92f25842011-01-04 15:09:34 -08001185 val = I915_READ(PCH_DREF_CONTROL);
1186 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1187 DREF_SUPERSPREAD_SOURCE_MASK));
1188 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1189}
1190
Daniel Vetterab9412b2013-05-03 11:49:46 +02001191static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1192 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001193{
1194 int reg;
1195 u32 val;
1196 bool enabled;
1197
Daniel Vetterab9412b2013-05-03 11:49:46 +02001198 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001199 val = I915_READ(reg);
1200 enabled = !!(val & TRANS_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001201 WARN(enabled,
1202 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1203 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001204}
1205
Keith Packard4e634382011-08-06 10:39:45 -07001206static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1207 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001208{
1209 if ((val & DP_PORT_EN) == 0)
1210 return false;
1211
1212 if (HAS_PCH_CPT(dev_priv->dev)) {
1213 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1214 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1215 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1216 return false;
1217 } else {
1218 if ((val & DP_PIPE_MASK) != (pipe << 30))
1219 return false;
1220 }
1221 return true;
1222}
1223
Keith Packard1519b992011-08-06 10:35:34 -07001224static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1225 enum pipe pipe, u32 val)
1226{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001227 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001228 return false;
1229
1230 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001231 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001232 return false;
1233 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001234 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001235 return false;
1236 }
1237 return true;
1238}
1239
1240static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1241 enum pipe pipe, u32 val)
1242{
1243 if ((val & LVDS_PORT_EN) == 0)
1244 return false;
1245
1246 if (HAS_PCH_CPT(dev_priv->dev)) {
1247 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1248 return false;
1249 } else {
1250 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1251 return false;
1252 }
1253 return true;
1254}
1255
1256static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1257 enum pipe pipe, u32 val)
1258{
1259 if ((val & ADPA_DAC_ENABLE) == 0)
1260 return false;
1261 if (HAS_PCH_CPT(dev_priv->dev)) {
1262 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1263 return false;
1264 } else {
1265 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1266 return false;
1267 }
1268 return true;
1269}
1270
Jesse Barnes291906f2011-02-02 12:28:03 -08001271static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001272 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001273{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001274 u32 val = I915_READ(reg);
Keith Packard4e634382011-08-06 10:39:45 -07001275 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001276 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001277 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001278
Daniel Vetter75c5da22012-09-10 21:58:29 +02001279 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1280 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001281 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001282}
1283
1284static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1285 enum pipe pipe, int reg)
1286{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001287 u32 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001288 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001289 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001290 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001291
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001292 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001293 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001294 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001295}
1296
1297static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1298 enum pipe pipe)
1299{
1300 int reg;
1301 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001302
Keith Packardf0575e92011-07-25 22:12:43 -07001303 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1304 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1305 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001306
1307 reg = PCH_ADPA;
1308 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001309 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001310 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001311 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001312
1313 reg = PCH_LVDS;
1314 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001315 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001316 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001317 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001318
Paulo Zanonie2debe92013-02-18 19:00:27 -03001319 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1320 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1321 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001322}
1323
Daniel Vetter426115c2013-07-11 22:13:42 +02001324static void vlv_enable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001325{
Daniel Vetter426115c2013-07-11 22:13:42 +02001326 struct drm_device *dev = crtc->base.dev;
1327 struct drm_i915_private *dev_priv = dev->dev_private;
1328 int reg = DPLL(crtc->pipe);
1329 u32 dpll = crtc->config.dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001330
Daniel Vetter426115c2013-07-11 22:13:42 +02001331 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001332
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001333 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001334 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1335
1336 /* PLL is protected by panel, make sure we can write it */
1337 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001338 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001339
Daniel Vetter426115c2013-07-11 22:13:42 +02001340 I915_WRITE(reg, dpll);
1341 POSTING_READ(reg);
1342 udelay(150);
1343
1344 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1345 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1346
1347 I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
1348 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001349
1350 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001351 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001352 POSTING_READ(reg);
1353 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001354 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001355 POSTING_READ(reg);
1356 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001357 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001358 POSTING_READ(reg);
1359 udelay(150); /* wait for warmup */
1360}
1361
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001362static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001363{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001364 struct drm_device *dev = crtc->base.dev;
1365 struct drm_i915_private *dev_priv = dev->dev_private;
1366 int reg = DPLL(crtc->pipe);
1367 u32 dpll = crtc->config.dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001368
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001369 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001370
1371 /* No really, not for ILK+ */
1372 BUG_ON(dev_priv->info->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001373
1374 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001375 if (IS_MOBILE(dev) && !IS_I830(dev))
1376 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001377
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001378 I915_WRITE(reg, dpll);
1379
1380 /* Wait for the clocks to stabilize. */
1381 POSTING_READ(reg);
1382 udelay(150);
1383
1384 if (INTEL_INFO(dev)->gen >= 4) {
1385 I915_WRITE(DPLL_MD(crtc->pipe),
1386 crtc->config.dpll_hw_state.dpll_md);
1387 } else {
1388 /* The pixel multiplier can only be updated once the
1389 * DPLL is enabled and the clocks are stable.
1390 *
1391 * So write it again.
1392 */
1393 I915_WRITE(reg, dpll);
1394 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001395
1396 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001397 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001398 POSTING_READ(reg);
1399 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001400 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001401 POSTING_READ(reg);
1402 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001403 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001404 POSTING_READ(reg);
1405 udelay(150); /* wait for warmup */
1406}
1407
1408/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001409 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001410 * @dev_priv: i915 private structure
1411 * @pipe: pipe PLL to disable
1412 *
1413 * Disable the PLL for @pipe, making sure the pipe is off first.
1414 *
1415 * Note! This is for pre-ILK only.
1416 */
Daniel Vetter50b44a42013-06-05 13:34:33 +02001417static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001418{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001419 /* Don't disable pipe A or pipe A PLLs if needed */
1420 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1421 return;
1422
1423 /* Make sure the pipe isn't still relying on us */
1424 assert_pipe_disabled(dev_priv, pipe);
1425
Daniel Vetter50b44a42013-06-05 13:34:33 +02001426 I915_WRITE(DPLL(pipe), 0);
1427 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001428}
1429
Jesse Barnes89b667f2013-04-18 14:51:36 -07001430void vlv_wait_port_ready(struct drm_i915_private *dev_priv, int port)
1431{
1432 u32 port_mask;
1433
1434 if (!port)
1435 port_mask = DPLL_PORTB_READY_MASK;
1436 else
1437 port_mask = DPLL_PORTC_READY_MASK;
1438
1439 if (wait_for((I915_READ(DPLL(0)) & port_mask) == 0, 1000))
1440 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
1441 'B' + port, I915_READ(DPLL(0)));
1442}
1443
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001444/**
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001445 * ironlake_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001446 * @dev_priv: i915 private structure
1447 * @pipe: pipe PLL to enable
1448 *
1449 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1450 * drives the transcoder clock.
1451 */
Daniel Vettere2b78262013-06-07 23:10:03 +02001452static void ironlake_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001453{
Daniel Vettere2b78262013-06-07 23:10:03 +02001454 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1455 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001456
Chris Wilson48da64a2012-05-13 20:16:12 +01001457 /* PCH PLLs only available on ILK, SNB and IVB */
Jesse Barnes92f25842011-01-04 15:09:34 -08001458 BUG_ON(dev_priv->info->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001459 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001460 return;
1461
1462 if (WARN_ON(pll->refcount == 0))
1463 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001464
Daniel Vetter46edb022013-06-05 13:34:12 +02001465 DRM_DEBUG_KMS("enable %s (active %d, on? %d)for crtc %d\n",
1466 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001467 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001468
Daniel Vettercdbd2312013-06-05 13:34:03 +02001469 if (pll->active++) {
1470 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001471 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001472 return;
1473 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001474 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001475
Daniel Vetter46edb022013-06-05 13:34:12 +02001476 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001477 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001478 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001479}
1480
Daniel Vettere2b78262013-06-07 23:10:03 +02001481static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001482{
Daniel Vettere2b78262013-06-07 23:10:03 +02001483 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1484 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001485
Jesse Barnes92f25842011-01-04 15:09:34 -08001486 /* PCH only available on ILK+ */
1487 BUG_ON(dev_priv->info->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001488 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001489 return;
1490
Chris Wilson48da64a2012-05-13 20:16:12 +01001491 if (WARN_ON(pll->refcount == 0))
1492 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001493
Daniel Vetter46edb022013-06-05 13:34:12 +02001494 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1495 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001496 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001497
Chris Wilson48da64a2012-05-13 20:16:12 +01001498 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001499 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001500 return;
1501 }
1502
Daniel Vettere9d69442013-06-05 13:34:15 +02001503 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001504 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001505 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001506 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001507
Daniel Vetter46edb022013-06-05 13:34:12 +02001508 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001509 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001510 pll->on = false;
Jesse Barnes92f25842011-01-04 15:09:34 -08001511}
1512
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001513static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1514 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001515{
Daniel Vetter23670b322012-11-01 09:15:30 +01001516 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001517 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001518 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001519 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001520
1521 /* PCH only available on ILK+ */
1522 BUG_ON(dev_priv->info->gen < 5);
1523
1524 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001525 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001526 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001527
1528 /* FDI must be feeding us bits for PCH ports */
1529 assert_fdi_tx_enabled(dev_priv, pipe);
1530 assert_fdi_rx_enabled(dev_priv, pipe);
1531
Daniel Vetter23670b322012-11-01 09:15:30 +01001532 if (HAS_PCH_CPT(dev)) {
1533 /* Workaround: Set the timing override bit before enabling the
1534 * pch transcoder. */
1535 reg = TRANS_CHICKEN2(pipe);
1536 val = I915_READ(reg);
1537 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1538 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001539 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001540
Daniel Vetterab9412b2013-05-03 11:49:46 +02001541 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001542 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001543 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001544
1545 if (HAS_PCH_IBX(dev_priv->dev)) {
1546 /*
1547 * make the BPC in transcoder be consistent with
1548 * that in pipeconf reg.
1549 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001550 val &= ~PIPECONF_BPC_MASK;
1551 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001552 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001553
1554 val &= ~TRANS_INTERLACE_MASK;
1555 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001556 if (HAS_PCH_IBX(dev_priv->dev) &&
1557 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1558 val |= TRANS_LEGACY_INTERLACED_ILK;
1559 else
1560 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001561 else
1562 val |= TRANS_PROGRESSIVE;
1563
Jesse Barnes040484a2011-01-03 12:14:26 -08001564 I915_WRITE(reg, val | TRANS_ENABLE);
1565 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001566 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001567}
1568
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001569static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001570 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001571{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001572 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001573
1574 /* PCH only available on ILK+ */
1575 BUG_ON(dev_priv->info->gen < 5);
1576
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001577 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001578 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001579 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001580
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001581 /* Workaround: set timing override bit. */
1582 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001583 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001584 I915_WRITE(_TRANSA_CHICKEN2, val);
1585
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001586 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001587 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001588
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001589 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1590 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001591 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001592 else
1593 val |= TRANS_PROGRESSIVE;
1594
Daniel Vetterab9412b2013-05-03 11:49:46 +02001595 I915_WRITE(LPT_TRANSCONF, val);
1596 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001597 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001598}
1599
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001600static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1601 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001602{
Daniel Vetter23670b322012-11-01 09:15:30 +01001603 struct drm_device *dev = dev_priv->dev;
1604 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001605
1606 /* FDI relies on the transcoder */
1607 assert_fdi_tx_disabled(dev_priv, pipe);
1608 assert_fdi_rx_disabled(dev_priv, pipe);
1609
Jesse Barnes291906f2011-02-02 12:28:03 -08001610 /* Ports must be off as well */
1611 assert_pch_ports_disabled(dev_priv, pipe);
1612
Daniel Vetterab9412b2013-05-03 11:49:46 +02001613 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001614 val = I915_READ(reg);
1615 val &= ~TRANS_ENABLE;
1616 I915_WRITE(reg, val);
1617 /* wait for PCH transcoder off, transcoder state */
1618 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001619 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001620
1621 if (!HAS_PCH_IBX(dev)) {
1622 /* Workaround: Clear the timing override chicken bit again. */
1623 reg = TRANS_CHICKEN2(pipe);
1624 val = I915_READ(reg);
1625 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1626 I915_WRITE(reg, val);
1627 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001628}
1629
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001630static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001631{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001632 u32 val;
1633
Daniel Vetterab9412b2013-05-03 11:49:46 +02001634 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001635 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001636 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001637 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001638 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001639 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001640
1641 /* Workaround: clear timing override bit. */
1642 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001643 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001644 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001645}
1646
1647/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001648 * intel_enable_pipe - enable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001649 * @dev_priv: i915 private structure
1650 * @pipe: pipe to enable
Jesse Barnes040484a2011-01-03 12:14:26 -08001651 * @pch_port: on ILK+, is this pipe driving a PCH port or not
Jesse Barnesb24e7172011-01-04 15:09:30 -08001652 *
1653 * Enable @pipe, making sure that various hardware specific requirements
1654 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1655 *
1656 * @pipe should be %PIPE_A or %PIPE_B.
1657 *
1658 * Will wait until the pipe is actually running (i.e. first vblank) before
1659 * returning.
1660 */
Jesse Barnes040484a2011-01-03 12:14:26 -08001661static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
1662 bool pch_port)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001663{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001664 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1665 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001666 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001667 int reg;
1668 u32 val;
1669
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001670 assert_planes_disabled(dev_priv, pipe);
1671 assert_sprites_disabled(dev_priv, pipe);
1672
Paulo Zanoni681e5812012-12-06 11:12:38 -02001673 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001674 pch_transcoder = TRANSCODER_A;
1675 else
1676 pch_transcoder = pipe;
1677
Jesse Barnesb24e7172011-01-04 15:09:30 -08001678 /*
1679 * A pipe without a PLL won't actually be able to drive bits from
1680 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1681 * need the check.
1682 */
1683 if (!HAS_PCH_SPLIT(dev_priv->dev))
1684 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001685 else {
1686 if (pch_port) {
1687 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001688 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001689 assert_fdi_tx_pll_enabled(dev_priv,
1690 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001691 }
1692 /* FIXME: assert CPU port conditions for SNB+ */
1693 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001694
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001695 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001696 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001697 if (val & PIPECONF_ENABLE)
1698 return;
1699
1700 I915_WRITE(reg, val | PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001701 intel_wait_for_vblank(dev_priv->dev, pipe);
1702}
1703
1704/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001705 * intel_disable_pipe - disable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001706 * @dev_priv: i915 private structure
1707 * @pipe: pipe to disable
1708 *
1709 * Disable @pipe, making sure that various hardware specific requirements
1710 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1711 *
1712 * @pipe should be %PIPE_A or %PIPE_B.
1713 *
1714 * Will wait until the pipe has shut down before returning.
1715 */
1716static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1717 enum pipe pipe)
1718{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001719 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1720 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001721 int reg;
1722 u32 val;
1723
1724 /*
1725 * Make sure planes won't keep trying to pump pixels to us,
1726 * or we might hang the display.
1727 */
1728 assert_planes_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001729 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001730
1731 /* Don't disable pipe A or pipe A PLLs if needed */
1732 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1733 return;
1734
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001735 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001736 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001737 if ((val & PIPECONF_ENABLE) == 0)
1738 return;
1739
1740 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001741 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1742}
1743
Keith Packardd74362c2011-07-28 14:47:14 -07001744/*
1745 * Plane regs are double buffered, going from enabled->disabled needs a
1746 * trigger in order to latch. The display address reg provides this.
1747 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03001748void intel_flush_display_plane(struct drm_i915_private *dev_priv,
Keith Packardd74362c2011-07-28 14:47:14 -07001749 enum plane plane)
1750{
Damien Lespiau14f86142012-10-29 15:24:49 +00001751 if (dev_priv->info->gen >= 4)
1752 I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
1753 else
1754 I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
Keith Packardd74362c2011-07-28 14:47:14 -07001755}
1756
Jesse Barnesb24e7172011-01-04 15:09:30 -08001757/**
1758 * intel_enable_plane - enable a display plane on a given pipe
1759 * @dev_priv: i915 private structure
1760 * @plane: plane to enable
1761 * @pipe: pipe being fed
1762 *
1763 * Enable @plane on @pipe, making sure that @pipe is running first.
1764 */
1765static void intel_enable_plane(struct drm_i915_private *dev_priv,
1766 enum plane plane, enum pipe pipe)
1767{
1768 int reg;
1769 u32 val;
1770
1771 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1772 assert_pipe_enabled(dev_priv, pipe);
1773
1774 reg = DSPCNTR(plane);
1775 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001776 if (val & DISPLAY_PLANE_ENABLE)
1777 return;
1778
1779 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
Keith Packardd74362c2011-07-28 14:47:14 -07001780 intel_flush_display_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001781 intel_wait_for_vblank(dev_priv->dev, pipe);
1782}
1783
Jesse Barnesb24e7172011-01-04 15:09:30 -08001784/**
1785 * intel_disable_plane - disable a display plane
1786 * @dev_priv: i915 private structure
1787 * @plane: plane to disable
1788 * @pipe: pipe consuming the data
1789 *
1790 * Disable @plane; should be an independent operation.
1791 */
1792static void intel_disable_plane(struct drm_i915_private *dev_priv,
1793 enum plane plane, enum pipe pipe)
1794{
1795 int reg;
1796 u32 val;
1797
1798 reg = DSPCNTR(plane);
1799 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001800 if ((val & DISPLAY_PLANE_ENABLE) == 0)
1801 return;
1802
1803 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001804 intel_flush_display_plane(dev_priv, plane);
1805 intel_wait_for_vblank(dev_priv->dev, pipe);
1806}
1807
Chris Wilson693db182013-03-05 14:52:39 +00001808static bool need_vtd_wa(struct drm_device *dev)
1809{
1810#ifdef CONFIG_INTEL_IOMMU
1811 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
1812 return true;
1813#endif
1814 return false;
1815}
1816
Chris Wilson127bd2a2010-07-23 23:32:05 +01001817int
Chris Wilson48b956c2010-09-14 12:50:34 +01001818intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00001819 struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00001820 struct intel_ring_buffer *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001821{
Chris Wilsonce453d82011-02-21 14:43:56 +00001822 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001823 u32 alignment;
1824 int ret;
1825
Chris Wilson05394f32010-11-08 19:18:58 +00001826 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001827 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01001828 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1829 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001830 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01001831 alignment = 4 * 1024;
1832 else
1833 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001834 break;
1835 case I915_TILING_X:
1836 /* pin() will align the object as required by fence */
1837 alignment = 0;
1838 break;
1839 case I915_TILING_Y:
Daniel Vetter8bb6e952013-04-06 23:54:56 +02001840 /* Despite that we check this in framebuffer_init userspace can
1841 * screw us over and change the tiling after the fact. Only
1842 * pinned buffers can't change their tiling. */
1843 DRM_DEBUG_DRIVER("Y tiled not allowed for scan out buffers\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001844 return -EINVAL;
1845 default:
1846 BUG();
1847 }
1848
Chris Wilson693db182013-03-05 14:52:39 +00001849 /* Note that the w/a also requires 64 PTE of padding following the
1850 * bo. We currently fill all unused PTE with the shadow page and so
1851 * we should always have valid PTE following the scanout preventing
1852 * the VT-d warning.
1853 */
1854 if (need_vtd_wa(dev) && alignment < 256 * 1024)
1855 alignment = 256 * 1024;
1856
Chris Wilsonce453d82011-02-21 14:43:56 +00001857 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01001858 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01001859 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00001860 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001861
1862 /* Install a fence for tiled scan-out. Pre-i965 always needs a
1863 * fence, whereas 965+ only requires a fence if using
1864 * framebuffer compression. For simplicity, we always install
1865 * a fence as the cost is not that onerous.
1866 */
Chris Wilson06d98132012-04-17 15:31:24 +01001867 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001868 if (ret)
1869 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01001870
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001871 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001872
Chris Wilsonce453d82011-02-21 14:43:56 +00001873 dev_priv->mm.interruptible = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001874 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01001875
1876err_unpin:
1877 i915_gem_object_unpin(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00001878err_interruptible:
1879 dev_priv->mm.interruptible = true;
Chris Wilson48b956c2010-09-14 12:50:34 +01001880 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001881}
1882
Chris Wilson1690e1e2011-12-14 13:57:08 +01001883void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
1884{
1885 i915_gem_object_unpin_fence(obj);
1886 i915_gem_object_unpin(obj);
1887}
1888
Daniel Vetterc2c75132012-07-05 12:17:30 +02001889/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
1890 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00001891unsigned long intel_gen4_compute_page_offset(int *x, int *y,
1892 unsigned int tiling_mode,
1893 unsigned int cpp,
1894 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02001895{
Chris Wilsonbc752862013-02-21 20:04:31 +00001896 if (tiling_mode != I915_TILING_NONE) {
1897 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02001898
Chris Wilsonbc752862013-02-21 20:04:31 +00001899 tile_rows = *y / 8;
1900 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02001901
Chris Wilsonbc752862013-02-21 20:04:31 +00001902 tiles = *x / (512/cpp);
1903 *x %= 512/cpp;
1904
1905 return tile_rows * pitch * 8 + tiles * 4096;
1906 } else {
1907 unsigned int offset;
1908
1909 offset = *y * pitch + *x * cpp;
1910 *y = 0;
1911 *x = (offset & 4095) / cpp;
1912 return offset & -4096;
1913 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02001914}
1915
Jesse Barnes17638cd2011-06-24 12:19:23 -07001916static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
1917 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07001918{
1919 struct drm_device *dev = crtc->dev;
1920 struct drm_i915_private *dev_priv = dev->dev_private;
1921 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1922 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00001923 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07001924 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02001925 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07001926 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01001927 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07001928
1929 switch (plane) {
1930 case 0:
1931 case 1:
1932 break;
1933 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03001934 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes81255562010-08-02 12:07:50 -07001935 return -EINVAL;
1936 }
1937
1938 intel_fb = to_intel_framebuffer(fb);
1939 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07001940
Chris Wilson5eddb702010-09-11 13:48:45 +01001941 reg = DSPCNTR(plane);
1942 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07001943 /* Mask out pixel format bits in case we change it */
1944 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02001945 switch (fb->pixel_format) {
1946 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07001947 dspcntr |= DISPPLANE_8BPP;
1948 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02001949 case DRM_FORMAT_XRGB1555:
1950 case DRM_FORMAT_ARGB1555:
1951 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07001952 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02001953 case DRM_FORMAT_RGB565:
1954 dspcntr |= DISPPLANE_BGRX565;
1955 break;
1956 case DRM_FORMAT_XRGB8888:
1957 case DRM_FORMAT_ARGB8888:
1958 dspcntr |= DISPPLANE_BGRX888;
1959 break;
1960 case DRM_FORMAT_XBGR8888:
1961 case DRM_FORMAT_ABGR8888:
1962 dspcntr |= DISPPLANE_RGBX888;
1963 break;
1964 case DRM_FORMAT_XRGB2101010:
1965 case DRM_FORMAT_ARGB2101010:
1966 dspcntr |= DISPPLANE_BGRX101010;
1967 break;
1968 case DRM_FORMAT_XBGR2101010:
1969 case DRM_FORMAT_ABGR2101010:
1970 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07001971 break;
1972 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01001973 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07001974 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02001975
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001976 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00001977 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07001978 dspcntr |= DISPPLANE_TILED;
1979 else
1980 dspcntr &= ~DISPPLANE_TILED;
1981 }
1982
Ville Syrjäläde1aa622013-06-07 10:47:01 +03001983 if (IS_G4X(dev))
1984 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
1985
Chris Wilson5eddb702010-09-11 13:48:45 +01001986 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07001987
Daniel Vettere506a0c2012-07-05 12:17:29 +02001988 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes81255562010-08-02 12:07:50 -07001989
Daniel Vetterc2c75132012-07-05 12:17:30 +02001990 if (INTEL_INFO(dev)->gen >= 4) {
1991 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00001992 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
1993 fb->bits_per_pixel / 8,
1994 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02001995 linear_offset -= intel_crtc->dspaddr_offset;
1996 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02001997 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02001998 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02001999
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002000 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2001 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2002 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002003 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002004 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetterc2c75132012-07-05 12:17:30 +02002005 I915_MODIFY_DISPBASE(DSPSURF(plane),
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002006 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002007 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002008 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002009 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002010 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002011 POSTING_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002012
Jesse Barnes17638cd2011-06-24 12:19:23 -07002013 return 0;
2014}
2015
2016static int ironlake_update_plane(struct drm_crtc *crtc,
2017 struct drm_framebuffer *fb, int x, int y)
2018{
2019 struct drm_device *dev = crtc->dev;
2020 struct drm_i915_private *dev_priv = dev->dev_private;
2021 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2022 struct intel_framebuffer *intel_fb;
2023 struct drm_i915_gem_object *obj;
2024 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002025 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002026 u32 dspcntr;
2027 u32 reg;
2028
2029 switch (plane) {
2030 case 0:
2031 case 1:
Jesse Barnes27f82272011-09-02 12:54:37 -07002032 case 2:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002033 break;
2034 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002035 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes17638cd2011-06-24 12:19:23 -07002036 return -EINVAL;
2037 }
2038
2039 intel_fb = to_intel_framebuffer(fb);
2040 obj = intel_fb->obj;
2041
2042 reg = DSPCNTR(plane);
2043 dspcntr = I915_READ(reg);
2044 /* Mask out pixel format bits in case we change it */
2045 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002046 switch (fb->pixel_format) {
2047 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002048 dspcntr |= DISPPLANE_8BPP;
2049 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002050 case DRM_FORMAT_RGB565:
2051 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002052 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002053 case DRM_FORMAT_XRGB8888:
2054 case DRM_FORMAT_ARGB8888:
2055 dspcntr |= DISPPLANE_BGRX888;
2056 break;
2057 case DRM_FORMAT_XBGR8888:
2058 case DRM_FORMAT_ABGR8888:
2059 dspcntr |= DISPPLANE_RGBX888;
2060 break;
2061 case DRM_FORMAT_XRGB2101010:
2062 case DRM_FORMAT_ARGB2101010:
2063 dspcntr |= DISPPLANE_BGRX101010;
2064 break;
2065 case DRM_FORMAT_XBGR2101010:
2066 case DRM_FORMAT_ABGR2101010:
2067 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002068 break;
2069 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002070 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002071 }
2072
2073 if (obj->tiling_mode != I915_TILING_NONE)
2074 dspcntr |= DISPPLANE_TILED;
2075 else
2076 dspcntr &= ~DISPPLANE_TILED;
2077
2078 /* must disable */
2079 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2080
2081 I915_WRITE(reg, dspcntr);
2082
Daniel Vettere506a0c2012-07-05 12:17:29 +02002083 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002084 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002085 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2086 fb->bits_per_pixel / 8,
2087 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002088 linear_offset -= intel_crtc->dspaddr_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002089
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002090 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2091 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2092 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002093 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002094 I915_MODIFY_DISPBASE(DSPSURF(plane),
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002095 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002096 if (IS_HASWELL(dev)) {
2097 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2098 } else {
2099 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2100 I915_WRITE(DSPLINOFF(plane), linear_offset);
2101 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002102 POSTING_READ(reg);
2103
2104 return 0;
2105}
2106
2107/* Assume fb object is pinned & idle & fenced and just update base pointers */
2108static int
2109intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2110 int x, int y, enum mode_set_atomic state)
2111{
2112 struct drm_device *dev = crtc->dev;
2113 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002114
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002115 if (dev_priv->display.disable_fbc)
2116 dev_priv->display.disable_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02002117 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07002118
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002119 return dev_priv->display.update_plane(crtc, fb, x, y);
Jesse Barnes81255562010-08-02 12:07:50 -07002120}
2121
Ville Syrjälä96a02912013-02-18 19:08:49 +02002122void intel_display_handle_reset(struct drm_device *dev)
2123{
2124 struct drm_i915_private *dev_priv = dev->dev_private;
2125 struct drm_crtc *crtc;
2126
2127 /*
2128 * Flips in the rings have been nuked by the reset,
2129 * so complete all pending flips so that user space
2130 * will get its events and not get stuck.
2131 *
2132 * Also update the base address of all primary
2133 * planes to the the last fb to make sure we're
2134 * showing the correct fb after a reset.
2135 *
2136 * Need to make two loops over the crtcs so that we
2137 * don't try to grab a crtc mutex before the
2138 * pending_flip_queue really got woken up.
2139 */
2140
2141 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2142 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2143 enum plane plane = intel_crtc->plane;
2144
2145 intel_prepare_page_flip(dev, plane);
2146 intel_finish_page_flip_plane(dev, plane);
2147 }
2148
2149 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2150 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2151
2152 mutex_lock(&crtc->mutex);
2153 if (intel_crtc->active)
2154 dev_priv->display.update_plane(crtc, crtc->fb,
2155 crtc->x, crtc->y);
2156 mutex_unlock(&crtc->mutex);
2157 }
2158}
2159
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002160static int
Chris Wilson14667a42012-04-03 17:58:35 +01002161intel_finish_fb(struct drm_framebuffer *old_fb)
2162{
2163 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2164 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2165 bool was_interruptible = dev_priv->mm.interruptible;
2166 int ret;
2167
Chris Wilson14667a42012-04-03 17:58:35 +01002168 /* Big Hammer, we also need to ensure that any pending
2169 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2170 * current scanout is retired before unpinning the old
2171 * framebuffer.
2172 *
2173 * This should only fail upon a hung GPU, in which case we
2174 * can safely continue.
2175 */
2176 dev_priv->mm.interruptible = false;
2177 ret = i915_gem_object_finish_gpu(obj);
2178 dev_priv->mm.interruptible = was_interruptible;
2179
2180 return ret;
2181}
2182
Ville Syrjälä198598d2012-10-31 17:50:24 +02002183static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
2184{
2185 struct drm_device *dev = crtc->dev;
2186 struct drm_i915_master_private *master_priv;
2187 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2188
2189 if (!dev->primary->master)
2190 return;
2191
2192 master_priv = dev->primary->master->driver_priv;
2193 if (!master_priv->sarea_priv)
2194 return;
2195
2196 switch (intel_crtc->pipe) {
2197 case 0:
2198 master_priv->sarea_priv->pipeA_x = x;
2199 master_priv->sarea_priv->pipeA_y = y;
2200 break;
2201 case 1:
2202 master_priv->sarea_priv->pipeB_x = x;
2203 master_priv->sarea_priv->pipeB_y = y;
2204 break;
2205 default:
2206 break;
2207 }
2208}
2209
Chris Wilson14667a42012-04-03 17:58:35 +01002210static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002211intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002212 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002213{
2214 struct drm_device *dev = crtc->dev;
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002215 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08002216 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter94352cf2012-07-05 22:51:56 +02002217 struct drm_framebuffer *old_fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002218 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002219
2220 /* no fb bound */
Daniel Vetter94352cf2012-07-05 22:51:56 +02002221 if (!fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002222 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002223 return 0;
2224 }
2225
Ben Widawsky7eb552a2013-03-13 14:05:41 -07002226 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002227 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2228 plane_name(intel_crtc->plane),
2229 INTEL_INFO(dev)->num_pipes);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002230 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002231 }
2232
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002233 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01002234 ret = intel_pin_and_fence_fb_obj(dev,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002235 to_intel_framebuffer(fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002236 NULL);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002237 if (ret != 0) {
2238 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002239 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002240 return ret;
2241 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002242
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002243 /* Update pipe size and adjust fitter if needed */
2244 if (i915_fastboot) {
2245 I915_WRITE(PIPESRC(intel_crtc->pipe),
2246 ((crtc->mode.hdisplay - 1) << 16) |
2247 (crtc->mode.vdisplay - 1));
2248 if (!intel_crtc->config.pch_pfit.size &&
2249 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
2250 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2251 I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
2252 I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
2253 I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
2254 }
2255 }
2256
Daniel Vetter94352cf2012-07-05 22:51:56 +02002257 ret = dev_priv->display.update_plane(crtc, fb, x, y);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002258 if (ret) {
Daniel Vetter94352cf2012-07-05 22:51:56 +02002259 intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002260 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002261 DRM_ERROR("failed to update base address\n");
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002262 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002263 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002264
Daniel Vetter94352cf2012-07-05 22:51:56 +02002265 old_fb = crtc->fb;
2266 crtc->fb = fb;
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02002267 crtc->x = x;
2268 crtc->y = y;
Daniel Vetter94352cf2012-07-05 22:51:56 +02002269
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002270 if (old_fb) {
Daniel Vetterd7697ee2013-06-02 17:23:01 +02002271 if (intel_crtc->active && old_fb != fb)
2272 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002273 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002274 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002275
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002276 intel_update_fbc(dev);
Rodrigo Vivi49065572013-07-11 18:45:05 -03002277 intel_edp_psr_update(dev);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002278 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002279
Ville Syrjälä198598d2012-10-31 17:50:24 +02002280 intel_crtc_update_sarea_pos(crtc, x, y);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002281
2282 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002283}
2284
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002285static void intel_fdi_normal_train(struct drm_crtc *crtc)
2286{
2287 struct drm_device *dev = crtc->dev;
2288 struct drm_i915_private *dev_priv = dev->dev_private;
2289 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2290 int pipe = intel_crtc->pipe;
2291 u32 reg, temp;
2292
2293 /* enable normal train */
2294 reg = FDI_TX_CTL(pipe);
2295 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002296 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002297 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2298 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002299 } else {
2300 temp &= ~FDI_LINK_TRAIN_NONE;
2301 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002302 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002303 I915_WRITE(reg, temp);
2304
2305 reg = FDI_RX_CTL(pipe);
2306 temp = I915_READ(reg);
2307 if (HAS_PCH_CPT(dev)) {
2308 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2309 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2310 } else {
2311 temp &= ~FDI_LINK_TRAIN_NONE;
2312 temp |= FDI_LINK_TRAIN_NONE;
2313 }
2314 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2315
2316 /* wait one idle pattern time */
2317 POSTING_READ(reg);
2318 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002319
2320 /* IVB wants error correction enabled */
2321 if (IS_IVYBRIDGE(dev))
2322 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2323 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002324}
2325
Daniel Vetter1e833f42013-02-19 22:31:57 +01002326static bool pipe_has_enabled_pch(struct intel_crtc *intel_crtc)
2327{
2328 return intel_crtc->base.enabled && intel_crtc->config.has_pch_encoder;
2329}
2330
Daniel Vetter01a415f2012-10-27 15:58:40 +02002331static void ivb_modeset_global_resources(struct drm_device *dev)
2332{
2333 struct drm_i915_private *dev_priv = dev->dev_private;
2334 struct intel_crtc *pipe_B_crtc =
2335 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2336 struct intel_crtc *pipe_C_crtc =
2337 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2338 uint32_t temp;
2339
Daniel Vetter1e833f42013-02-19 22:31:57 +01002340 /*
2341 * When everything is off disable fdi C so that we could enable fdi B
2342 * with all lanes. Note that we don't care about enabled pipes without
2343 * an enabled pch encoder.
2344 */
2345 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2346 !pipe_has_enabled_pch(pipe_C_crtc)) {
Daniel Vetter01a415f2012-10-27 15:58:40 +02002347 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2348 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2349
2350 temp = I915_READ(SOUTH_CHICKEN1);
2351 temp &= ~FDI_BC_BIFURCATION_SELECT;
2352 DRM_DEBUG_KMS("disabling fdi C rx\n");
2353 I915_WRITE(SOUTH_CHICKEN1, temp);
2354 }
2355}
2356
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002357/* The FDI link training functions for ILK/Ibexpeak. */
2358static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2359{
2360 struct drm_device *dev = crtc->dev;
2361 struct drm_i915_private *dev_priv = dev->dev_private;
2362 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2363 int pipe = intel_crtc->pipe;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002364 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002365 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002366
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002367 /* FDI needs bits from pipe & plane first */
2368 assert_pipe_enabled(dev_priv, pipe);
2369 assert_plane_enabled(dev_priv, plane);
2370
Adam Jacksone1a44742010-06-25 15:32:14 -04002371 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2372 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002373 reg = FDI_RX_IMR(pipe);
2374 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002375 temp &= ~FDI_RX_SYMBOL_LOCK;
2376 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002377 I915_WRITE(reg, temp);
2378 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002379 udelay(150);
2380
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002381 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002382 reg = FDI_TX_CTL(pipe);
2383 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002384 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2385 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002386 temp &= ~FDI_LINK_TRAIN_NONE;
2387 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002388 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002389
Chris Wilson5eddb702010-09-11 13:48:45 +01002390 reg = FDI_RX_CTL(pipe);
2391 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002392 temp &= ~FDI_LINK_TRAIN_NONE;
2393 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002394 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2395
2396 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002397 udelay(150);
2398
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002399 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002400 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2401 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2402 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002403
Chris Wilson5eddb702010-09-11 13:48:45 +01002404 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002405 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002406 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002407 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2408
2409 if ((temp & FDI_RX_BIT_LOCK)) {
2410 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002411 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002412 break;
2413 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002414 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002415 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002416 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002417
2418 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002419 reg = FDI_TX_CTL(pipe);
2420 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002421 temp &= ~FDI_LINK_TRAIN_NONE;
2422 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002423 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002424
Chris Wilson5eddb702010-09-11 13:48:45 +01002425 reg = FDI_RX_CTL(pipe);
2426 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002427 temp &= ~FDI_LINK_TRAIN_NONE;
2428 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002429 I915_WRITE(reg, temp);
2430
2431 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002432 udelay(150);
2433
Chris Wilson5eddb702010-09-11 13:48:45 +01002434 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002435 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002436 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002437 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2438
2439 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002440 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002441 DRM_DEBUG_KMS("FDI train 2 done.\n");
2442 break;
2443 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002444 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002445 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002446 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002447
2448 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002449
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002450}
2451
Akshay Joshi0206e352011-08-16 15:34:10 -04002452static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002453 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2454 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2455 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2456 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2457};
2458
2459/* The FDI link training functions for SNB/Cougarpoint. */
2460static void gen6_fdi_link_train(struct drm_crtc *crtc)
2461{
2462 struct drm_device *dev = crtc->dev;
2463 struct drm_i915_private *dev_priv = dev->dev_private;
2464 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2465 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05002466 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002467
Adam Jacksone1a44742010-06-25 15:32:14 -04002468 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2469 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002470 reg = FDI_RX_IMR(pipe);
2471 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002472 temp &= ~FDI_RX_SYMBOL_LOCK;
2473 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002474 I915_WRITE(reg, temp);
2475
2476 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002477 udelay(150);
2478
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002479 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002480 reg = FDI_TX_CTL(pipe);
2481 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002482 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2483 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002484 temp &= ~FDI_LINK_TRAIN_NONE;
2485 temp |= FDI_LINK_TRAIN_PATTERN_1;
2486 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2487 /* SNB-B */
2488 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002489 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002490
Daniel Vetterd74cf322012-10-26 10:58:13 +02002491 I915_WRITE(FDI_RX_MISC(pipe),
2492 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2493
Chris Wilson5eddb702010-09-11 13:48:45 +01002494 reg = FDI_RX_CTL(pipe);
2495 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002496 if (HAS_PCH_CPT(dev)) {
2497 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2498 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2499 } else {
2500 temp &= ~FDI_LINK_TRAIN_NONE;
2501 temp |= FDI_LINK_TRAIN_PATTERN_1;
2502 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002503 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2504
2505 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002506 udelay(150);
2507
Akshay Joshi0206e352011-08-16 15:34:10 -04002508 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002509 reg = FDI_TX_CTL(pipe);
2510 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002511 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2512 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002513 I915_WRITE(reg, temp);
2514
2515 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002516 udelay(500);
2517
Sean Paulfa37d392012-03-02 12:53:39 -05002518 for (retry = 0; retry < 5; retry++) {
2519 reg = FDI_RX_IIR(pipe);
2520 temp = I915_READ(reg);
2521 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2522 if (temp & FDI_RX_BIT_LOCK) {
2523 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2524 DRM_DEBUG_KMS("FDI train 1 done.\n");
2525 break;
2526 }
2527 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002528 }
Sean Paulfa37d392012-03-02 12:53:39 -05002529 if (retry < 5)
2530 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002531 }
2532 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002533 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002534
2535 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002536 reg = FDI_TX_CTL(pipe);
2537 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002538 temp &= ~FDI_LINK_TRAIN_NONE;
2539 temp |= FDI_LINK_TRAIN_PATTERN_2;
2540 if (IS_GEN6(dev)) {
2541 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2542 /* SNB-B */
2543 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2544 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002545 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002546
Chris Wilson5eddb702010-09-11 13:48:45 +01002547 reg = FDI_RX_CTL(pipe);
2548 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002549 if (HAS_PCH_CPT(dev)) {
2550 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2551 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2552 } else {
2553 temp &= ~FDI_LINK_TRAIN_NONE;
2554 temp |= FDI_LINK_TRAIN_PATTERN_2;
2555 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002556 I915_WRITE(reg, temp);
2557
2558 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002559 udelay(150);
2560
Akshay Joshi0206e352011-08-16 15:34:10 -04002561 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002562 reg = FDI_TX_CTL(pipe);
2563 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002564 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2565 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002566 I915_WRITE(reg, temp);
2567
2568 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002569 udelay(500);
2570
Sean Paulfa37d392012-03-02 12:53:39 -05002571 for (retry = 0; retry < 5; retry++) {
2572 reg = FDI_RX_IIR(pipe);
2573 temp = I915_READ(reg);
2574 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2575 if (temp & FDI_RX_SYMBOL_LOCK) {
2576 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2577 DRM_DEBUG_KMS("FDI train 2 done.\n");
2578 break;
2579 }
2580 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002581 }
Sean Paulfa37d392012-03-02 12:53:39 -05002582 if (retry < 5)
2583 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002584 }
2585 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002586 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002587
2588 DRM_DEBUG_KMS("FDI train done.\n");
2589}
2590
Jesse Barnes357555c2011-04-28 15:09:55 -07002591/* Manual link training for Ivy Bridge A0 parts */
2592static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2593{
2594 struct drm_device *dev = crtc->dev;
2595 struct drm_i915_private *dev_priv = dev->dev_private;
2596 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2597 int pipe = intel_crtc->pipe;
2598 u32 reg, temp, i;
2599
2600 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2601 for train result */
2602 reg = FDI_RX_IMR(pipe);
2603 temp = I915_READ(reg);
2604 temp &= ~FDI_RX_SYMBOL_LOCK;
2605 temp &= ~FDI_RX_BIT_LOCK;
2606 I915_WRITE(reg, temp);
2607
2608 POSTING_READ(reg);
2609 udelay(150);
2610
Daniel Vetter01a415f2012-10-27 15:58:40 +02002611 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
2612 I915_READ(FDI_RX_IIR(pipe)));
2613
Jesse Barnes357555c2011-04-28 15:09:55 -07002614 /* enable CPU FDI TX and PCH FDI RX */
2615 reg = FDI_TX_CTL(pipe);
2616 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002617 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2618 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Jesse Barnes357555c2011-04-28 15:09:55 -07002619 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2620 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
2621 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2622 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002623 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002624 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2625
Daniel Vetterd74cf322012-10-26 10:58:13 +02002626 I915_WRITE(FDI_RX_MISC(pipe),
2627 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2628
Jesse Barnes357555c2011-04-28 15:09:55 -07002629 reg = FDI_RX_CTL(pipe);
2630 temp = I915_READ(reg);
2631 temp &= ~FDI_LINK_TRAIN_AUTO;
2632 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2633 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002634 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002635 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2636
2637 POSTING_READ(reg);
2638 udelay(150);
2639
Akshay Joshi0206e352011-08-16 15:34:10 -04002640 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002641 reg = FDI_TX_CTL(pipe);
2642 temp = I915_READ(reg);
2643 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2644 temp |= snb_b_fdi_train_param[i];
2645 I915_WRITE(reg, temp);
2646
2647 POSTING_READ(reg);
2648 udelay(500);
2649
2650 reg = FDI_RX_IIR(pipe);
2651 temp = I915_READ(reg);
2652 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2653
2654 if (temp & FDI_RX_BIT_LOCK ||
2655 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2656 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Daniel Vetter01a415f2012-10-27 15:58:40 +02002657 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n", i);
Jesse Barnes357555c2011-04-28 15:09:55 -07002658 break;
2659 }
2660 }
2661 if (i == 4)
2662 DRM_ERROR("FDI train 1 fail!\n");
2663
2664 /* Train 2 */
2665 reg = FDI_TX_CTL(pipe);
2666 temp = I915_READ(reg);
2667 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2668 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2669 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2670 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2671 I915_WRITE(reg, temp);
2672
2673 reg = FDI_RX_CTL(pipe);
2674 temp = I915_READ(reg);
2675 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2676 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2677 I915_WRITE(reg, temp);
2678
2679 POSTING_READ(reg);
2680 udelay(150);
2681
Akshay Joshi0206e352011-08-16 15:34:10 -04002682 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002683 reg = FDI_TX_CTL(pipe);
2684 temp = I915_READ(reg);
2685 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2686 temp |= snb_b_fdi_train_param[i];
2687 I915_WRITE(reg, temp);
2688
2689 POSTING_READ(reg);
2690 udelay(500);
2691
2692 reg = FDI_RX_IIR(pipe);
2693 temp = I915_READ(reg);
2694 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2695
2696 if (temp & FDI_RX_SYMBOL_LOCK) {
2697 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Daniel Vetter01a415f2012-10-27 15:58:40 +02002698 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n", i);
Jesse Barnes357555c2011-04-28 15:09:55 -07002699 break;
2700 }
2701 }
2702 if (i == 4)
2703 DRM_ERROR("FDI train 2 fail!\n");
2704
2705 DRM_DEBUG_KMS("FDI train done.\n");
2706}
2707
Daniel Vetter88cefb62012-08-12 19:27:14 +02002708static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07002709{
Daniel Vetter88cefb62012-08-12 19:27:14 +02002710 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002711 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002712 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002713 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002714
Jesse Barnesc64e3112010-09-10 11:27:03 -07002715
Jesse Barnes0e23b992010-09-10 11:10:00 -07002716 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01002717 reg = FDI_RX_CTL(pipe);
2718 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002719 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
2720 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002721 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01002722 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2723
2724 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002725 udelay(200);
2726
2727 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002728 temp = I915_READ(reg);
2729 I915_WRITE(reg, temp | FDI_PCDCLK);
2730
2731 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002732 udelay(200);
2733
Paulo Zanoni20749732012-11-23 15:30:38 -02002734 /* Enable CPU FDI TX PLL, always on for Ironlake */
2735 reg = FDI_TX_CTL(pipe);
2736 temp = I915_READ(reg);
2737 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2738 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01002739
Paulo Zanoni20749732012-11-23 15:30:38 -02002740 POSTING_READ(reg);
2741 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002742 }
2743}
2744
Daniel Vetter88cefb62012-08-12 19:27:14 +02002745static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
2746{
2747 struct drm_device *dev = intel_crtc->base.dev;
2748 struct drm_i915_private *dev_priv = dev->dev_private;
2749 int pipe = intel_crtc->pipe;
2750 u32 reg, temp;
2751
2752 /* Switch from PCDclk to Rawclk */
2753 reg = FDI_RX_CTL(pipe);
2754 temp = I915_READ(reg);
2755 I915_WRITE(reg, temp & ~FDI_PCDCLK);
2756
2757 /* Disable CPU FDI TX PLL */
2758 reg = FDI_TX_CTL(pipe);
2759 temp = I915_READ(reg);
2760 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2761
2762 POSTING_READ(reg);
2763 udelay(100);
2764
2765 reg = FDI_RX_CTL(pipe);
2766 temp = I915_READ(reg);
2767 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2768
2769 /* Wait for the clocks to turn off. */
2770 POSTING_READ(reg);
2771 udelay(100);
2772}
2773
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002774static void ironlake_fdi_disable(struct drm_crtc *crtc)
2775{
2776 struct drm_device *dev = crtc->dev;
2777 struct drm_i915_private *dev_priv = dev->dev_private;
2778 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2779 int pipe = intel_crtc->pipe;
2780 u32 reg, temp;
2781
2782 /* disable CPU FDI tx and PCH FDI rx */
2783 reg = FDI_TX_CTL(pipe);
2784 temp = I915_READ(reg);
2785 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2786 POSTING_READ(reg);
2787
2788 reg = FDI_RX_CTL(pipe);
2789 temp = I915_READ(reg);
2790 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002791 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002792 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2793
2794 POSTING_READ(reg);
2795 udelay(100);
2796
2797 /* Ironlake workaround, disable clock pointer after downing FDI */
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002798 if (HAS_PCH_IBX(dev)) {
2799 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002800 }
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002801
2802 /* still set train pattern 1 */
2803 reg = FDI_TX_CTL(pipe);
2804 temp = I915_READ(reg);
2805 temp &= ~FDI_LINK_TRAIN_NONE;
2806 temp |= FDI_LINK_TRAIN_PATTERN_1;
2807 I915_WRITE(reg, temp);
2808
2809 reg = FDI_RX_CTL(pipe);
2810 temp = I915_READ(reg);
2811 if (HAS_PCH_CPT(dev)) {
2812 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2813 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2814 } else {
2815 temp &= ~FDI_LINK_TRAIN_NONE;
2816 temp |= FDI_LINK_TRAIN_PATTERN_1;
2817 }
2818 /* BPC in FDI rx is consistent with that in PIPECONF */
2819 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002820 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002821 I915_WRITE(reg, temp);
2822
2823 POSTING_READ(reg);
2824 udelay(100);
2825}
2826
Chris Wilson5bb61642012-09-27 21:25:58 +01002827static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2828{
2829 struct drm_device *dev = crtc->dev;
2830 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä10d83732013-01-29 18:13:34 +02002831 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5bb61642012-09-27 21:25:58 +01002832 unsigned long flags;
2833 bool pending;
2834
Ville Syrjälä10d83732013-01-29 18:13:34 +02002835 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2836 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
Chris Wilson5bb61642012-09-27 21:25:58 +01002837 return false;
2838
2839 spin_lock_irqsave(&dev->event_lock, flags);
2840 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2841 spin_unlock_irqrestore(&dev->event_lock, flags);
2842
2843 return pending;
2844}
2845
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002846static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2847{
Chris Wilson0f911282012-04-17 10:05:38 +01002848 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01002849 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002850
2851 if (crtc->fb == NULL)
2852 return;
2853
Daniel Vetter2c10d572012-12-20 21:24:07 +01002854 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
2855
Chris Wilson5bb61642012-09-27 21:25:58 +01002856 wait_event(dev_priv->pending_flip_queue,
2857 !intel_crtc_has_pending_flip(crtc));
2858
Chris Wilson0f911282012-04-17 10:05:38 +01002859 mutex_lock(&dev->struct_mutex);
2860 intel_finish_fb(crtc->fb);
2861 mutex_unlock(&dev->struct_mutex);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002862}
2863
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002864/* Program iCLKIP clock to the desired frequency */
2865static void lpt_program_iclkip(struct drm_crtc *crtc)
2866{
2867 struct drm_device *dev = crtc->dev;
2868 struct drm_i915_private *dev_priv = dev->dev_private;
2869 u32 divsel, phaseinc, auxdiv, phasedir = 0;
2870 u32 temp;
2871
Daniel Vetter09153002012-12-12 14:06:44 +01002872 mutex_lock(&dev_priv->dpio_lock);
2873
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002874 /* It is necessary to ungate the pixclk gate prior to programming
2875 * the divisors, and gate it back when it is done.
2876 */
2877 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
2878
2879 /* Disable SSCCTL */
2880 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002881 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
2882 SBI_SSCCTL_DISABLE,
2883 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002884
2885 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
2886 if (crtc->mode.clock == 20000) {
2887 auxdiv = 1;
2888 divsel = 0x41;
2889 phaseinc = 0x20;
2890 } else {
2891 /* The iCLK virtual clock root frequency is in MHz,
2892 * but the crtc->mode.clock in in KHz. To get the divisors,
2893 * it is necessary to divide one by another, so we
2894 * convert the virtual clock precision to KHz here for higher
2895 * precision.
2896 */
2897 u32 iclk_virtual_root_freq = 172800 * 1000;
2898 u32 iclk_pi_range = 64;
2899 u32 desired_divisor, msb_divisor_value, pi_value;
2900
2901 desired_divisor = (iclk_virtual_root_freq / crtc->mode.clock);
2902 msb_divisor_value = desired_divisor / iclk_pi_range;
2903 pi_value = desired_divisor % iclk_pi_range;
2904
2905 auxdiv = 0;
2906 divsel = msb_divisor_value - 2;
2907 phaseinc = pi_value;
2908 }
2909
2910 /* This should not happen with any sane values */
2911 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
2912 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
2913 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
2914 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
2915
2916 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
2917 crtc->mode.clock,
2918 auxdiv,
2919 divsel,
2920 phasedir,
2921 phaseinc);
2922
2923 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002924 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002925 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
2926 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
2927 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
2928 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
2929 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
2930 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002931 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002932
2933 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002934 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002935 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
2936 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002937 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002938
2939 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002940 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002941 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002942 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002943
2944 /* Wait for initialization time */
2945 udelay(24);
2946
2947 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01002948
2949 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002950}
2951
Daniel Vetter275f01b22013-05-03 11:49:47 +02002952static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
2953 enum pipe pch_transcoder)
2954{
2955 struct drm_device *dev = crtc->base.dev;
2956 struct drm_i915_private *dev_priv = dev->dev_private;
2957 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
2958
2959 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
2960 I915_READ(HTOTAL(cpu_transcoder)));
2961 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
2962 I915_READ(HBLANK(cpu_transcoder)));
2963 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
2964 I915_READ(HSYNC(cpu_transcoder)));
2965
2966 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
2967 I915_READ(VTOTAL(cpu_transcoder)));
2968 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
2969 I915_READ(VBLANK(cpu_transcoder)));
2970 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
2971 I915_READ(VSYNC(cpu_transcoder)));
2972 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
2973 I915_READ(VSYNCSHIFT(cpu_transcoder)));
2974}
2975
Jesse Barnesf67a5592011-01-05 10:31:48 -08002976/*
2977 * Enable PCH resources required for PCH ports:
2978 * - PCH PLLs
2979 * - FDI training & RX/TX
2980 * - update transcoder timings
2981 * - DP transcoding bits
2982 * - transcoder
2983 */
2984static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08002985{
2986 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002987 struct drm_i915_private *dev_priv = dev->dev_private;
2988 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2989 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01002990 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07002991
Daniel Vetterab9412b2013-05-03 11:49:46 +02002992 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01002993
Daniel Vettercd986ab2012-10-26 10:58:12 +02002994 /* Write the TU size bits before fdi link training, so that error
2995 * detection works. */
2996 I915_WRITE(FDI_RX_TUSIZE1(pipe),
2997 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
2998
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002999 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07003000 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003001
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003002 /* We need to program the right clock selection before writing the pixel
3003 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003004 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003005 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07003006
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003007 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003008 temp |= TRANS_DPLL_ENABLE(pipe);
3009 sel = TRANS_DPLLB_SEL(pipe);
Daniel Vettera43f6e02013-06-07 23:10:32 +02003010 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003011 temp |= sel;
3012 else
3013 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003014 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003015 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003016
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003017 /* XXX: pch pll's can be enabled any time before we enable the PCH
3018 * transcoder, and we actually should do this to not upset any PCH
3019 * transcoder that already use the clock when we share it.
3020 *
3021 * Note that enable_shared_dpll tries to do the right thing, but
3022 * get_shared_dpll unconditionally resets the pll - we need that to have
3023 * the right LVDS enable sequence. */
3024 ironlake_enable_shared_dpll(intel_crtc);
3025
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003026 /* set transcoder timing, panel must allow it */
3027 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02003028 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003029
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003030 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003031
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003032 /* For PCH DP, enable TRANS_DP_CTL */
3033 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07003034 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3035 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003036 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003037 reg = TRANS_DP_CTL(pipe);
3038 temp = I915_READ(reg);
3039 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003040 TRANS_DP_SYNC_MASK |
3041 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003042 temp |= (TRANS_DP_OUTPUT_ENABLE |
3043 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003044 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003045
3046 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003047 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003048 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003049 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003050
3051 switch (intel_trans_dp_port_sel(crtc)) {
3052 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003053 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003054 break;
3055 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003056 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003057 break;
3058 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003059 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003060 break;
3061 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003062 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003063 }
3064
Chris Wilson5eddb702010-09-11 13:48:45 +01003065 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003066 }
3067
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003068 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003069}
3070
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003071static void lpt_pch_enable(struct drm_crtc *crtc)
3072{
3073 struct drm_device *dev = crtc->dev;
3074 struct drm_i915_private *dev_priv = dev->dev_private;
3075 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02003076 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003077
Daniel Vetterab9412b2013-05-03 11:49:46 +02003078 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003079
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003080 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003081
Paulo Zanoni0540e482012-10-31 18:12:40 -02003082 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02003083 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003084
Paulo Zanoni937bb612012-10-31 18:12:47 -02003085 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003086}
3087
Daniel Vettere2b78262013-06-07 23:10:03 +02003088static void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003089{
Daniel Vettere2b78262013-06-07 23:10:03 +02003090 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003091
3092 if (pll == NULL)
3093 return;
3094
3095 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003096 WARN(1, "bad %s refcount\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003097 return;
3098 }
3099
Daniel Vetterf4a091c2013-06-10 17:28:22 +02003100 if (--pll->refcount == 0) {
3101 WARN_ON(pll->on);
3102 WARN_ON(pll->active);
3103 }
3104
Daniel Vettera43f6e02013-06-07 23:10:32 +02003105 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003106}
3107
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003108static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003109{
Daniel Vettere2b78262013-06-07 23:10:03 +02003110 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3111 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3112 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003113
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003114 if (pll) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003115 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3116 crtc->base.base.id, pll->name);
Daniel Vettere2b78262013-06-07 23:10:03 +02003117 intel_put_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003118 }
3119
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003120 if (HAS_PCH_IBX(dev_priv->dev)) {
3121 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02003122 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003123 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003124
Daniel Vetter46edb022013-06-05 13:34:12 +02003125 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3126 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003127
3128 goto found;
3129 }
3130
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003131 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3132 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003133
3134 /* Only want to check enabled timings first */
3135 if (pll->refcount == 0)
3136 continue;
3137
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003138 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3139 sizeof(pll->hw_state)) == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003140 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
Daniel Vettere2b78262013-06-07 23:10:03 +02003141 crtc->base.base.id,
Daniel Vetter46edb022013-06-05 13:34:12 +02003142 pll->name, pll->refcount, pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003143
3144 goto found;
3145 }
3146 }
3147
3148 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003149 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3150 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003151 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003152 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3153 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003154 goto found;
3155 }
3156 }
3157
3158 return NULL;
3159
3160found:
Daniel Vettera43f6e02013-06-07 23:10:32 +02003161 crtc->config.shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02003162 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3163 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02003164
Daniel Vettercdbd2312013-06-05 13:34:03 +02003165 if (pll->active == 0) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02003166 memcpy(&pll->hw_state, &crtc->config.dpll_hw_state,
3167 sizeof(pll->hw_state));
3168
Daniel Vetter46edb022013-06-05 13:34:12 +02003169 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
Daniel Vettercdbd2312013-06-05 13:34:03 +02003170 WARN_ON(pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02003171 assert_shared_dpll_disabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003172
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02003173 pll->mode_set(dev_priv, pll);
Daniel Vettercdbd2312013-06-05 13:34:03 +02003174 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003175 pll->refcount++;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003176
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003177 return pll;
3178}
3179
Daniel Vettera1520312013-05-03 11:49:50 +02003180static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07003181{
3182 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01003183 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003184 u32 temp;
3185
3186 temp = I915_READ(dslreg);
3187 udelay(500);
3188 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07003189 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03003190 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07003191 }
3192}
3193
Jesse Barnesb074cec2013-04-25 12:55:02 -07003194static void ironlake_pfit_enable(struct intel_crtc *crtc)
3195{
3196 struct drm_device *dev = crtc->base.dev;
3197 struct drm_i915_private *dev_priv = dev->dev_private;
3198 int pipe = crtc->pipe;
3199
Jesse Barnes0ef37f32013-05-03 13:26:37 -07003200 if (crtc->config.pch_pfit.size) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07003201 /* Force use of hard-coded filter coefficients
3202 * as some pre-programmed values are broken,
3203 * e.g. x201.
3204 */
3205 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3206 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3207 PF_PIPE_SEL_IVB(pipe));
3208 else
3209 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3210 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3211 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08003212 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003213}
3214
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003215static void intel_enable_planes(struct drm_crtc *crtc)
3216{
3217 struct drm_device *dev = crtc->dev;
3218 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3219 struct intel_plane *intel_plane;
3220
3221 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3222 if (intel_plane->pipe == pipe)
3223 intel_plane_restore(&intel_plane->base);
3224}
3225
3226static void intel_disable_planes(struct drm_crtc *crtc)
3227{
3228 struct drm_device *dev = crtc->dev;
3229 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3230 struct intel_plane *intel_plane;
3231
3232 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3233 if (intel_plane->pipe == pipe)
3234 intel_plane_disable(&intel_plane->base);
3235}
3236
Jesse Barnesf67a5592011-01-05 10:31:48 -08003237static void ironlake_crtc_enable(struct drm_crtc *crtc)
3238{
3239 struct drm_device *dev = crtc->dev;
3240 struct drm_i915_private *dev_priv = dev->dev_private;
3241 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003242 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003243 int pipe = intel_crtc->pipe;
3244 int plane = intel_crtc->plane;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003245
Daniel Vetter08a48462012-07-02 11:43:47 +02003246 WARN_ON(!crtc->enabled);
3247
Jesse Barnesf67a5592011-01-05 10:31:48 -08003248 if (intel_crtc->active)
3249 return;
3250
3251 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003252
3253 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3254 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
3255
Jesse Barnesf67a5592011-01-05 10:31:48 -08003256 intel_update_watermarks(dev);
3257
Daniel Vetterf6736a12013-06-05 13:34:30 +02003258 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02003259 if (encoder->pre_enable)
3260 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003261
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003262 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02003263 /* Note: FDI PLL enabling _must_ be done before we enable the
3264 * cpu pipes, hence this is separate from all the other fdi/pch
3265 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02003266 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02003267 } else {
3268 assert_fdi_tx_disabled(dev_priv, pipe);
3269 assert_fdi_rx_disabled(dev_priv, pipe);
3270 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003271
Jesse Barnesb074cec2013-04-25 12:55:02 -07003272 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003273
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02003274 /*
3275 * On ILK+ LUT must be loaded before the pipe is running but with
3276 * clocks enabled
3277 */
3278 intel_crtc_load_lut(crtc);
3279
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003280 intel_enable_pipe(dev_priv, pipe,
3281 intel_crtc->config.has_pch_encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003282 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003283 intel_enable_planes(crtc);
Ville Syrjälä5c38d482013-06-04 13:49:00 +03003284 intel_crtc_update_cursor(crtc, true);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003285
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003286 if (intel_crtc->config.has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08003287 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003288
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003289 mutex_lock(&dev->struct_mutex);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003290 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003291 mutex_unlock(&dev->struct_mutex);
3292
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003293 for_each_encoder_on_crtc(dev, crtc, encoder)
3294 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02003295
3296 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02003297 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02003298
3299 /*
3300 * There seems to be a race in PCH platform hw (at least on some
3301 * outputs) where an enabled pipe still completes any pageflip right
3302 * away (as if the pipe is off) instead of waiting for vblank. As soon
3303 * as the first vblank happend, everything works as expected. Hence just
3304 * wait for one vblank before returning to avoid strange things
3305 * happening.
3306 */
3307 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003308}
3309
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003310/* IPS only exists on ULT machines and is tied to pipe A. */
3311static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
3312{
Damien Lespiauf5adf942013-06-24 18:29:34 +01003313 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003314}
3315
3316static void hsw_enable_ips(struct intel_crtc *crtc)
3317{
3318 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3319
3320 if (!crtc->config.ips_enabled)
3321 return;
3322
3323 /* We can only enable IPS after we enable a plane and wait for a vblank.
3324 * We guarantee that the plane is enabled by calling intel_enable_ips
3325 * only after intel_enable_plane. And intel_enable_plane already waits
3326 * for a vblank, so all we need to do here is to enable the IPS bit. */
3327 assert_plane_enabled(dev_priv, crtc->plane);
3328 I915_WRITE(IPS_CTL, IPS_ENABLE);
3329}
3330
3331static void hsw_disable_ips(struct intel_crtc *crtc)
3332{
3333 struct drm_device *dev = crtc->base.dev;
3334 struct drm_i915_private *dev_priv = dev->dev_private;
3335
3336 if (!crtc->config.ips_enabled)
3337 return;
3338
3339 assert_plane_enabled(dev_priv, crtc->plane);
3340 I915_WRITE(IPS_CTL, 0);
3341
3342 /* We need to wait for a vblank before we can disable the plane. */
3343 intel_wait_for_vblank(dev, crtc->pipe);
3344}
3345
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003346static void haswell_crtc_enable(struct drm_crtc *crtc)
3347{
3348 struct drm_device *dev = crtc->dev;
3349 struct drm_i915_private *dev_priv = dev->dev_private;
3350 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3351 struct intel_encoder *encoder;
3352 int pipe = intel_crtc->pipe;
3353 int plane = intel_crtc->plane;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003354
3355 WARN_ON(!crtc->enabled);
3356
3357 if (intel_crtc->active)
3358 return;
3359
3360 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003361
3362 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3363 if (intel_crtc->config.has_pch_encoder)
3364 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
3365
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003366 intel_update_watermarks(dev);
3367
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003368 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni04945642012-11-01 21:00:59 -02003369 dev_priv->display.fdi_link_train(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003370
3371 for_each_encoder_on_crtc(dev, crtc, encoder)
3372 if (encoder->pre_enable)
3373 encoder->pre_enable(encoder);
3374
Paulo Zanoni1f544382012-10-24 11:32:00 -02003375 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003376
Jesse Barnesb074cec2013-04-25 12:55:02 -07003377 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003378
3379 /*
3380 * On ILK+ LUT must be loaded before the pipe is running but with
3381 * clocks enabled
3382 */
3383 intel_crtc_load_lut(crtc);
3384
Paulo Zanoni1f544382012-10-24 11:32:00 -02003385 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00003386 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003387
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003388 intel_enable_pipe(dev_priv, pipe,
3389 intel_crtc->config.has_pch_encoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003390 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003391 intel_enable_planes(crtc);
Ville Syrjälä5c38d482013-06-04 13:49:00 +03003392 intel_crtc_update_cursor(crtc, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003393
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003394 hsw_enable_ips(intel_crtc);
3395
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003396 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003397 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003398
3399 mutex_lock(&dev->struct_mutex);
3400 intel_update_fbc(dev);
3401 mutex_unlock(&dev->struct_mutex);
3402
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003403 for_each_encoder_on_crtc(dev, crtc, encoder)
3404 encoder->enable(encoder);
3405
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003406 /*
3407 * There seems to be a race in PCH platform hw (at least on some
3408 * outputs) where an enabled pipe still completes any pageflip right
3409 * away (as if the pipe is off) instead of waiting for vblank. As soon
3410 * as the first vblank happend, everything works as expected. Hence just
3411 * wait for one vblank before returning to avoid strange things
3412 * happening.
3413 */
3414 intel_wait_for_vblank(dev, intel_crtc->pipe);
3415}
3416
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003417static void ironlake_pfit_disable(struct intel_crtc *crtc)
3418{
3419 struct drm_device *dev = crtc->base.dev;
3420 struct drm_i915_private *dev_priv = dev->dev_private;
3421 int pipe = crtc->pipe;
3422
3423 /* To avoid upsetting the power well on haswell only disable the pfit if
3424 * it's in use. The hw state code will make sure we get this right. */
3425 if (crtc->config.pch_pfit.size) {
3426 I915_WRITE(PF_CTL(pipe), 0);
3427 I915_WRITE(PF_WIN_POS(pipe), 0);
3428 I915_WRITE(PF_WIN_SZ(pipe), 0);
3429 }
3430}
3431
Jesse Barnes6be4a602010-09-10 10:26:01 -07003432static void ironlake_crtc_disable(struct drm_crtc *crtc)
3433{
3434 struct drm_device *dev = crtc->dev;
3435 struct drm_i915_private *dev_priv = dev->dev_private;
3436 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003437 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003438 int pipe = intel_crtc->pipe;
3439 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01003440 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003441
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003442
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003443 if (!intel_crtc->active)
3444 return;
3445
Daniel Vetterea9d7582012-07-10 10:42:52 +02003446 for_each_encoder_on_crtc(dev, crtc, encoder)
3447 encoder->disable(encoder);
3448
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003449 intel_crtc_wait_for_pending_flips(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003450 drm_vblank_off(dev, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003451
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07003452 if (dev_priv->fbc.plane == plane)
Chris Wilson973d04f2011-07-08 12:22:37 +01003453 intel_disable_fbc(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003454
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003455 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003456 intel_disable_planes(crtc);
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003457 intel_disable_plane(dev_priv, plane, pipe);
3458
Daniel Vetterd925c592013-06-05 13:34:04 +02003459 if (intel_crtc->config.has_pch_encoder)
3460 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
3461
Jesse Barnesb24e7172011-01-04 15:09:30 -08003462 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003463
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003464 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003465
Daniel Vetterbf49ec82012-09-06 22:15:40 +02003466 for_each_encoder_on_crtc(dev, crtc, encoder)
3467 if (encoder->post_disable)
3468 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003469
Daniel Vetterd925c592013-06-05 13:34:04 +02003470 if (intel_crtc->config.has_pch_encoder) {
3471 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003472
Daniel Vetterd925c592013-06-05 13:34:04 +02003473 ironlake_disable_pch_transcoder(dev_priv, pipe);
3474 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003475
Daniel Vetterd925c592013-06-05 13:34:04 +02003476 if (HAS_PCH_CPT(dev)) {
3477 /* disable TRANS_DP_CTL */
3478 reg = TRANS_DP_CTL(pipe);
3479 temp = I915_READ(reg);
3480 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
3481 TRANS_DP_PORT_SEL_MASK);
3482 temp |= TRANS_DP_PORT_SEL_NONE;
3483 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003484
Daniel Vetterd925c592013-06-05 13:34:04 +02003485 /* disable DPLL_SEL */
3486 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003487 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02003488 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003489 }
Daniel Vetterd925c592013-06-05 13:34:04 +02003490
3491 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003492 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02003493
3494 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003495 }
3496
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003497 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003498 intel_update_watermarks(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003499
3500 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01003501 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003502 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003503}
3504
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003505static void haswell_crtc_disable(struct drm_crtc *crtc)
3506{
3507 struct drm_device *dev = crtc->dev;
3508 struct drm_i915_private *dev_priv = dev->dev_private;
3509 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3510 struct intel_encoder *encoder;
3511 int pipe = intel_crtc->pipe;
3512 int plane = intel_crtc->plane;
Daniel Vetter3b117c82013-04-17 20:15:07 +02003513 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003514
3515 if (!intel_crtc->active)
3516 return;
3517
3518 for_each_encoder_on_crtc(dev, crtc, encoder)
3519 encoder->disable(encoder);
3520
3521 intel_crtc_wait_for_pending_flips(crtc);
3522 drm_vblank_off(dev, pipe);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003523
Rodrigo Vivi891348b2013-05-06 19:37:36 -03003524 /* FBC must be disabled before disabling the plane on HSW. */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07003525 if (dev_priv->fbc.plane == plane)
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003526 intel_disable_fbc(dev);
3527
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003528 hsw_disable_ips(intel_crtc);
3529
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003530 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003531 intel_disable_planes(crtc);
Rodrigo Vivi891348b2013-05-06 19:37:36 -03003532 intel_disable_plane(dev_priv, plane, pipe);
3533
Paulo Zanoni86642812013-04-12 17:57:57 -03003534 if (intel_crtc->config.has_pch_encoder)
3535 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003536 intel_disable_pipe(dev_priv, pipe);
3537
Paulo Zanoniad80a812012-10-24 16:06:19 -02003538 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003539
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003540 ironlake_pfit_disable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003541
Paulo Zanoni1f544382012-10-24 11:32:00 -02003542 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003543
3544 for_each_encoder_on_crtc(dev, crtc, encoder)
3545 if (encoder->post_disable)
3546 encoder->post_disable(encoder);
3547
Daniel Vetter88adfff2013-03-28 10:42:01 +01003548 if (intel_crtc->config.has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02003549 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni86642812013-04-12 17:57:57 -03003550 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02003551 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02003552 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003553
3554 intel_crtc->active = false;
3555 intel_update_watermarks(dev);
3556
3557 mutex_lock(&dev->struct_mutex);
3558 intel_update_fbc(dev);
3559 mutex_unlock(&dev->struct_mutex);
3560}
3561
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003562static void ironlake_crtc_off(struct drm_crtc *crtc)
3563{
3564 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003565 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003566}
3567
Paulo Zanoni6441ab52012-10-05 12:05:58 -03003568static void haswell_crtc_off(struct drm_crtc *crtc)
3569{
3570 intel_ddi_put_crtc_pll(crtc);
3571}
3572
Daniel Vetter02e792f2009-09-15 22:57:34 +02003573static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3574{
Daniel Vetter02e792f2009-09-15 22:57:34 +02003575 if (!enable && intel_crtc->overlay) {
Chris Wilson23f09ce2010-08-12 13:53:37 +01003576 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00003577 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02003578
Chris Wilson23f09ce2010-08-12 13:53:37 +01003579 mutex_lock(&dev->struct_mutex);
Chris Wilsonce453d82011-02-21 14:43:56 +00003580 dev_priv->mm.interruptible = false;
3581 (void) intel_overlay_switch_off(intel_crtc->overlay);
3582 dev_priv->mm.interruptible = true;
Chris Wilson23f09ce2010-08-12 13:53:37 +01003583 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02003584 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02003585
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01003586 /* Let userspace switch the overlay on again. In most cases userspace
3587 * has to recompute where to put it anyway.
3588 */
Daniel Vetter02e792f2009-09-15 22:57:34 +02003589}
3590
Egbert Eich61bc95c2013-03-04 09:24:38 -05003591/**
3592 * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
3593 * cursor plane briefly if not already running after enabling the display
3594 * plane.
3595 * This workaround avoids occasional blank screens when self refresh is
3596 * enabled.
3597 */
3598static void
3599g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
3600{
3601 u32 cntl = I915_READ(CURCNTR(pipe));
3602
3603 if ((cntl & CURSOR_MODE) == 0) {
3604 u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
3605
3606 I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
3607 I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
3608 intel_wait_for_vblank(dev_priv->dev, pipe);
3609 I915_WRITE(CURCNTR(pipe), cntl);
3610 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3611 I915_WRITE(FW_BLC_SELF, fw_bcl_self);
3612 }
3613}
3614
Jesse Barnes2dd24552013-04-25 12:55:01 -07003615static void i9xx_pfit_enable(struct intel_crtc *crtc)
3616{
3617 struct drm_device *dev = crtc->base.dev;
3618 struct drm_i915_private *dev_priv = dev->dev_private;
3619 struct intel_crtc_config *pipe_config = &crtc->config;
3620
Daniel Vetter328d8e82013-05-08 10:36:31 +02003621 if (!crtc->config.gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07003622 return;
3623
Daniel Vetterc0b03412013-05-28 12:05:54 +02003624 /*
3625 * The panel fitter should only be adjusted whilst the pipe is disabled,
3626 * according to register description and PRM.
3627 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07003628 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
3629 assert_pipe_disabled(dev_priv, crtc->pipe);
3630
Jesse Barnesb074cec2013-04-25 12:55:02 -07003631 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
3632 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02003633
3634 /* Border color in case we don't scale up to the full screen. Black by
3635 * default, change to something else for debugging. */
3636 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07003637}
3638
Jesse Barnes89b667f2013-04-18 14:51:36 -07003639static void valleyview_crtc_enable(struct drm_crtc *crtc)
3640{
3641 struct drm_device *dev = crtc->dev;
3642 struct drm_i915_private *dev_priv = dev->dev_private;
3643 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3644 struct intel_encoder *encoder;
3645 int pipe = intel_crtc->pipe;
3646 int plane = intel_crtc->plane;
3647
3648 WARN_ON(!crtc->enabled);
3649
3650 if (intel_crtc->active)
3651 return;
3652
3653 intel_crtc->active = true;
3654 intel_update_watermarks(dev);
3655
3656 mutex_lock(&dev_priv->dpio_lock);
3657
3658 for_each_encoder_on_crtc(dev, crtc, encoder)
3659 if (encoder->pre_pll_enable)
3660 encoder->pre_pll_enable(encoder);
3661
Daniel Vetter426115c2013-07-11 22:13:42 +02003662 vlv_enable_pll(intel_crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07003663
3664 for_each_encoder_on_crtc(dev, crtc, encoder)
3665 if (encoder->pre_enable)
3666 encoder->pre_enable(encoder);
3667
3668 /* VLV wants encoder enabling _before_ the pipe is up. */
3669 for_each_encoder_on_crtc(dev, crtc, encoder)
3670 encoder->enable(encoder);
3671
Jesse Barnes2dd24552013-04-25 12:55:01 -07003672 i9xx_pfit_enable(intel_crtc);
3673
Ville Syrjälä63cbb072013-06-04 13:48:59 +03003674 intel_crtc_load_lut(crtc);
3675
Jesse Barnes89b667f2013-04-18 14:51:36 -07003676 intel_enable_pipe(dev_priv, pipe, false);
3677 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003678 intel_enable_planes(crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07003679 intel_crtc_update_cursor(crtc, true);
3680
Ville Syrjäläf440eb12013-06-04 13:49:01 +03003681 intel_update_fbc(dev);
3682
Jesse Barnes89b667f2013-04-18 14:51:36 -07003683 mutex_unlock(&dev_priv->dpio_lock);
3684}
3685
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003686static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003687{
3688 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08003689 struct drm_i915_private *dev_priv = dev->dev_private;
3690 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003691 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08003692 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07003693 int plane = intel_crtc->plane;
Jesse Barnes79e53942008-11-07 14:24:08 -08003694
Daniel Vetter08a48462012-07-02 11:43:47 +02003695 WARN_ON(!crtc->enabled);
3696
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003697 if (intel_crtc->active)
3698 return;
3699
3700 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01003701 intel_update_watermarks(dev);
3702
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02003703 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02003704 if (encoder->pre_enable)
3705 encoder->pre_enable(encoder);
3706
Daniel Vetterf6736a12013-06-05 13:34:30 +02003707 i9xx_enable_pll(intel_crtc);
3708
Jesse Barnes2dd24552013-04-25 12:55:01 -07003709 i9xx_pfit_enable(intel_crtc);
3710
Ville Syrjälä63cbb072013-06-04 13:48:59 +03003711 intel_crtc_load_lut(crtc);
3712
Jesse Barnes040484a2011-01-03 12:14:26 -08003713 intel_enable_pipe(dev_priv, pipe, false);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003714 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003715 intel_enable_planes(crtc);
Ville Syrjälä22e407d2013-06-07 18:52:24 +03003716 /* The fixup needs to happen before cursor is enabled */
Egbert Eich61bc95c2013-03-04 09:24:38 -05003717 if (IS_G4X(dev))
3718 g4x_fixup_plane(dev_priv, pipe);
Ville Syrjälä22e407d2013-06-07 18:52:24 +03003719 intel_crtc_update_cursor(crtc, true);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003720
3721 /* Give the overlay scaler a chance to enable if it's on this pipe */
3722 intel_crtc_dpms_overlay(intel_crtc, true);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003723
Ville Syrjäläf440eb12013-06-04 13:49:01 +03003724 intel_update_fbc(dev);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003725
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003726 for_each_encoder_on_crtc(dev, crtc, encoder)
3727 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003728}
3729
Daniel Vetter87476d62013-04-11 16:29:06 +02003730static void i9xx_pfit_disable(struct intel_crtc *crtc)
3731{
3732 struct drm_device *dev = crtc->base.dev;
3733 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02003734
3735 if (!crtc->config.gmch_pfit.control)
3736 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02003737
3738 assert_pipe_disabled(dev_priv, crtc->pipe);
3739
Daniel Vetter328d8e82013-05-08 10:36:31 +02003740 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
3741 I915_READ(PFIT_CONTROL));
3742 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02003743}
3744
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003745static void i9xx_crtc_disable(struct drm_crtc *crtc)
3746{
3747 struct drm_device *dev = crtc->dev;
3748 struct drm_i915_private *dev_priv = dev->dev_private;
3749 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003750 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003751 int pipe = intel_crtc->pipe;
3752 int plane = intel_crtc->plane;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003753
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003754 if (!intel_crtc->active)
3755 return;
3756
Daniel Vetterea9d7582012-07-10 10:42:52 +02003757 for_each_encoder_on_crtc(dev, crtc, encoder)
3758 encoder->disable(encoder);
3759
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003760 /* Give the overlay scaler a chance to disable if it's on this pipe */
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003761 intel_crtc_wait_for_pending_flips(crtc);
3762 drm_vblank_off(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003763
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07003764 if (dev_priv->fbc.plane == plane)
Chris Wilson973d04f2011-07-08 12:22:37 +01003765 intel_disable_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003766
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003767 intel_crtc_dpms_overlay(intel_crtc, false);
3768 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003769 intel_disable_planes(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003770 intel_disable_plane(dev_priv, plane, pipe);
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003771
Jesse Barnesb24e7172011-01-04 15:09:30 -08003772 intel_disable_pipe(dev_priv, pipe);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02003773
Daniel Vetter87476d62013-04-11 16:29:06 +02003774 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02003775
Jesse Barnes89b667f2013-04-18 14:51:36 -07003776 for_each_encoder_on_crtc(dev, crtc, encoder)
3777 if (encoder->post_disable)
3778 encoder->post_disable(encoder);
3779
Daniel Vetter50b44a42013-06-05 13:34:33 +02003780 i9xx_disable_pll(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003781
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003782 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003783 intel_update_fbc(dev);
3784 intel_update_watermarks(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003785}
3786
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003787static void i9xx_crtc_off(struct drm_crtc *crtc)
3788{
3789}
3790
Daniel Vetter976f8a22012-07-08 22:34:21 +02003791static void intel_crtc_update_sarea(struct drm_crtc *crtc,
3792 bool enabled)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003793{
3794 struct drm_device *dev = crtc->dev;
3795 struct drm_i915_master_private *master_priv;
3796 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3797 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08003798
3799 if (!dev->primary->master)
3800 return;
3801
3802 master_priv = dev->primary->master->driver_priv;
3803 if (!master_priv->sarea_priv)
3804 return;
3805
Jesse Barnes79e53942008-11-07 14:24:08 -08003806 switch (pipe) {
3807 case 0:
3808 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
3809 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
3810 break;
3811 case 1:
3812 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
3813 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
3814 break;
3815 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003816 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08003817 break;
3818 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003819}
3820
Daniel Vetter976f8a22012-07-08 22:34:21 +02003821/**
3822 * Sets the power management mode of the pipe and plane.
3823 */
3824void intel_crtc_update_dpms(struct drm_crtc *crtc)
Chris Wilsoncdd59982010-09-08 16:30:16 +01003825{
Chris Wilsoncdd59982010-09-08 16:30:16 +01003826 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003827 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter976f8a22012-07-08 22:34:21 +02003828 struct intel_encoder *intel_encoder;
3829 bool enable = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01003830
Daniel Vetter976f8a22012-07-08 22:34:21 +02003831 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
3832 enable |= intel_encoder->connectors_active;
3833
3834 if (enable)
3835 dev_priv->display.crtc_enable(crtc);
3836 else
3837 dev_priv->display.crtc_disable(crtc);
3838
3839 intel_crtc_update_sarea(crtc, enable);
3840}
3841
Daniel Vetter976f8a22012-07-08 22:34:21 +02003842static void intel_crtc_disable(struct drm_crtc *crtc)
3843{
3844 struct drm_device *dev = crtc->dev;
3845 struct drm_connector *connector;
3846 struct drm_i915_private *dev_priv = dev->dev_private;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08003847 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter976f8a22012-07-08 22:34:21 +02003848
3849 /* crtc should still be enabled when we disable it. */
3850 WARN_ON(!crtc->enabled);
3851
3852 dev_priv->display.crtc_disable(crtc);
Paulo Zanonic77bf562013-05-03 12:15:40 -03003853 intel_crtc->eld_vld = false;
Daniel Vetter976f8a22012-07-08 22:34:21 +02003854 intel_crtc_update_sarea(crtc, false);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003855 dev_priv->display.off(crtc);
3856
Chris Wilson931872f2012-01-16 23:01:13 +00003857 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
3858 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003859
3860 if (crtc->fb) {
3861 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01003862 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003863 mutex_unlock(&dev->struct_mutex);
Daniel Vetter976f8a22012-07-08 22:34:21 +02003864 crtc->fb = NULL;
3865 }
3866
3867 /* Update computed state. */
3868 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3869 if (!connector->encoder || !connector->encoder->crtc)
3870 continue;
3871
3872 if (connector->encoder->crtc != crtc)
3873 continue;
3874
3875 connector->dpms = DRM_MODE_DPMS_OFF;
3876 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01003877 }
3878}
3879
Daniel Vettera261b242012-07-26 19:21:47 +02003880void intel_modeset_disable(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08003881{
Daniel Vettera261b242012-07-26 19:21:47 +02003882 struct drm_crtc *crtc;
3883
3884 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3885 if (crtc->enabled)
3886 intel_crtc_disable(crtc);
3887 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003888}
3889
Chris Wilsonea5b2132010-08-04 13:50:23 +01003890void intel_encoder_destroy(struct drm_encoder *encoder)
3891{
Chris Wilson4ef69c72010-09-09 15:14:28 +01003892 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01003893
Chris Wilsonea5b2132010-08-04 13:50:23 +01003894 drm_encoder_cleanup(encoder);
3895 kfree(intel_encoder);
3896}
3897
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003898/* Simple dpms helper for encodres with just one connector, no cloning and only
3899 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
3900 * state of the entire output pipe. */
3901void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
3902{
3903 if (mode == DRM_MODE_DPMS_ON) {
3904 encoder->connectors_active = true;
3905
Daniel Vetterb2cabb02012-07-01 22:42:24 +02003906 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003907 } else {
3908 encoder->connectors_active = false;
3909
Daniel Vetterb2cabb02012-07-01 22:42:24 +02003910 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003911 }
3912}
3913
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003914/* Cross check the actual hw state with our own modeset state tracking (and it's
3915 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02003916static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003917{
3918 if (connector->get_hw_state(connector)) {
3919 struct intel_encoder *encoder = connector->encoder;
3920 struct drm_crtc *crtc;
3921 bool encoder_enabled;
3922 enum pipe pipe;
3923
3924 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
3925 connector->base.base.id,
3926 drm_get_connector_name(&connector->base));
3927
3928 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
3929 "wrong connector dpms state\n");
3930 WARN(connector->base.encoder != &encoder->base,
3931 "active connector not linked to encoder\n");
3932 WARN(!encoder->connectors_active,
3933 "encoder->connectors_active not set\n");
3934
3935 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
3936 WARN(!encoder_enabled, "encoder not enabled\n");
3937 if (WARN_ON(!encoder->base.crtc))
3938 return;
3939
3940 crtc = encoder->base.crtc;
3941
3942 WARN(!crtc->enabled, "crtc not enabled\n");
3943 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
3944 WARN(pipe != to_intel_crtc(crtc)->pipe,
3945 "encoder active on the wrong pipe\n");
3946 }
3947}
3948
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003949/* Even simpler default implementation, if there's really no special case to
3950 * consider. */
3951void intel_connector_dpms(struct drm_connector *connector, int mode)
3952{
3953 struct intel_encoder *encoder = intel_attached_encoder(connector);
3954
3955 /* All the simple cases only support two dpms states. */
3956 if (mode != DRM_MODE_DPMS_ON)
3957 mode = DRM_MODE_DPMS_OFF;
3958
3959 if (mode == connector->dpms)
3960 return;
3961
3962 connector->dpms = mode;
3963
3964 /* Only need to change hw state when actually enabled */
3965 if (encoder->base.crtc)
3966 intel_encoder_dpms(encoder, mode);
3967 else
Daniel Vetter8af6cf82012-07-10 09:50:11 +02003968 WARN_ON(encoder->connectors_active != false);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003969
Daniel Vetterb9805142012-08-31 17:37:33 +02003970 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003971}
3972
Daniel Vetterf0947c32012-07-02 13:10:34 +02003973/* Simple connector->get_hw_state implementation for encoders that support only
3974 * one connector and no cloning and hence the encoder state determines the state
3975 * of the connector. */
3976bool intel_connector_get_hw_state(struct intel_connector *connector)
3977{
Daniel Vetter24929352012-07-02 20:28:59 +02003978 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02003979 struct intel_encoder *encoder = connector->encoder;
3980
3981 return encoder->get_hw_state(encoder, &pipe);
3982}
3983
Daniel Vetter1857e1d2013-04-29 19:34:16 +02003984static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
3985 struct intel_crtc_config *pipe_config)
3986{
3987 struct drm_i915_private *dev_priv = dev->dev_private;
3988 struct intel_crtc *pipe_B_crtc =
3989 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
3990
3991 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
3992 pipe_name(pipe), pipe_config->fdi_lanes);
3993 if (pipe_config->fdi_lanes > 4) {
3994 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
3995 pipe_name(pipe), pipe_config->fdi_lanes);
3996 return false;
3997 }
3998
3999 if (IS_HASWELL(dev)) {
4000 if (pipe_config->fdi_lanes > 2) {
4001 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
4002 pipe_config->fdi_lanes);
4003 return false;
4004 } else {
4005 return true;
4006 }
4007 }
4008
4009 if (INTEL_INFO(dev)->num_pipes == 2)
4010 return true;
4011
4012 /* Ivybridge 3 pipe is really complicated */
4013 switch (pipe) {
4014 case PIPE_A:
4015 return true;
4016 case PIPE_B:
4017 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
4018 pipe_config->fdi_lanes > 2) {
4019 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4020 pipe_name(pipe), pipe_config->fdi_lanes);
4021 return false;
4022 }
4023 return true;
4024 case PIPE_C:
Daniel Vetter1e833f42013-02-19 22:31:57 +01004025 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004026 pipe_B_crtc->config.fdi_lanes <= 2) {
4027 if (pipe_config->fdi_lanes > 2) {
4028 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4029 pipe_name(pipe), pipe_config->fdi_lanes);
4030 return false;
4031 }
4032 } else {
4033 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
4034 return false;
4035 }
4036 return true;
4037 default:
4038 BUG();
4039 }
4040}
4041
Daniel Vettere29c22c2013-02-21 00:00:16 +01004042#define RETRY 1
4043static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
4044 struct intel_crtc_config *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02004045{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004046 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004047 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Daniel Vetterff9a6752013-06-01 17:16:21 +02004048 int lane, link_bw, fdi_dotclock;
Daniel Vettere29c22c2013-02-21 00:00:16 +01004049 bool setup_ok, needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004050
Daniel Vettere29c22c2013-02-21 00:00:16 +01004051retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02004052 /* FDI is a binary signal running at ~2.7GHz, encoding
4053 * each output octet as 10 bits. The actual frequency
4054 * is stored as a divider into a 100MHz clock, and the
4055 * mode pixel clock is stored in units of 1KHz.
4056 * Hence the bw of each lane in terms of the mode signal
4057 * is:
4058 */
4059 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
4060
Daniel Vetterff9a6752013-06-01 17:16:21 +02004061 fdi_dotclock = adjusted_mode->clock;
Daniel Vetteref1b4602013-06-01 17:17:04 +02004062 fdi_dotclock /= pipe_config->pixel_multiplier;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004063
Daniel Vetter2bd89a02013-06-01 17:16:19 +02004064 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02004065 pipe_config->pipe_bpp);
4066
4067 pipe_config->fdi_lanes = lane;
4068
Daniel Vetter2bd89a02013-06-01 17:16:19 +02004069 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02004070 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004071
Daniel Vettere29c22c2013-02-21 00:00:16 +01004072 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
4073 intel_crtc->pipe, pipe_config);
4074 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
4075 pipe_config->pipe_bpp -= 2*3;
4076 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
4077 pipe_config->pipe_bpp);
4078 needs_recompute = true;
4079 pipe_config->bw_constrained = true;
4080
4081 goto retry;
4082 }
4083
4084 if (needs_recompute)
4085 return RETRY;
4086
4087 return setup_ok ? 0 : -EINVAL;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004088}
4089
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004090static void hsw_compute_ips_config(struct intel_crtc *crtc,
4091 struct intel_crtc_config *pipe_config)
4092{
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03004093 pipe_config->ips_enabled = i915_enable_ips &&
4094 hsw_crtc_supports_ips(crtc) &&
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004095 pipe_config->pipe_bpp == 24;
4096}
4097
Daniel Vettera43f6e02013-06-07 23:10:32 +02004098static int intel_crtc_compute_config(struct intel_crtc *crtc,
Daniel Vettere29c22c2013-02-21 00:00:16 +01004099 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08004100{
Daniel Vettera43f6e02013-06-07 23:10:32 +02004101 struct drm_device *dev = crtc->base.dev;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01004102 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01004103
Eric Anholtbad720f2009-10-22 16:11:14 -07004104 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08004105 /* FDI link clock is fixed at 2.7G */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01004106 if (pipe_config->requested_mode.clock * 3
4107 > IRONLAKE_FDI_FREQ * 4)
Daniel Vettere29c22c2013-02-21 00:00:16 +01004108 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004109 }
Chris Wilson89749352010-09-12 18:25:19 +01004110
Daniel Vetterf9bef082012-04-15 19:53:19 +02004111 /* All interlaced capable intel hw wants timings in frames. Note though
Daniel Vetterefea6e82013-07-21 21:36:59 +02004112 * that intel_lvds_compute_config does some funny tricks with the crtc
Daniel Vetterf9bef082012-04-15 19:53:19 +02004113 * timings, so we need to be careful not to clobber these.*/
Daniel Vetter7ae89232013-03-27 00:44:52 +01004114 if (!pipe_config->timings_set)
Daniel Vetterf9bef082012-04-15 19:53:19 +02004115 drm_mode_set_crtcinfo(adjusted_mode, 0);
Chris Wilson89749352010-09-12 18:25:19 +01004116
Damien Lespiau8693a822013-05-03 18:48:11 +01004117 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
4118 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03004119 */
4120 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
4121 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01004122 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03004123
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004124 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004125 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004126 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004127 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
4128 * for lvds. */
4129 pipe_config->pipe_bpp = 8*3;
4130 }
4131
Damien Lespiauf5adf942013-06-24 18:29:34 +01004132 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02004133 hsw_compute_ips_config(crtc, pipe_config);
4134
4135 /* XXX: PCH clock sharing is done in ->mode_set, so make sure the old
4136 * clock survives for now. */
4137 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
4138 pipe_config->shared_dpll = crtc->config.shared_dpll;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004139
Daniel Vetter877d48d2013-04-19 11:24:43 +02004140 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02004141 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02004142
Daniel Vettere29c22c2013-02-21 00:00:16 +01004143 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08004144}
4145
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07004146static int valleyview_get_display_clock_speed(struct drm_device *dev)
4147{
4148 return 400000; /* FIXME */
4149}
4150
Jesse Barnese70236a2009-09-21 10:42:27 -07004151static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08004152{
Jesse Barnese70236a2009-09-21 10:42:27 -07004153 return 400000;
4154}
Jesse Barnes79e53942008-11-07 14:24:08 -08004155
Jesse Barnese70236a2009-09-21 10:42:27 -07004156static int i915_get_display_clock_speed(struct drm_device *dev)
4157{
4158 return 333000;
4159}
Jesse Barnes79e53942008-11-07 14:24:08 -08004160
Jesse Barnese70236a2009-09-21 10:42:27 -07004161static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
4162{
4163 return 200000;
4164}
Jesse Barnes79e53942008-11-07 14:24:08 -08004165
Daniel Vetter257a7ff2013-07-26 08:35:42 +02004166static int pnv_get_display_clock_speed(struct drm_device *dev)
4167{
4168 u16 gcfgc = 0;
4169
4170 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4171
4172 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4173 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
4174 return 267000;
4175 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
4176 return 333000;
4177 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
4178 return 444000;
4179 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
4180 return 200000;
4181 default:
4182 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
4183 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
4184 return 133000;
4185 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
4186 return 167000;
4187 }
4188}
4189
Jesse Barnese70236a2009-09-21 10:42:27 -07004190static int i915gm_get_display_clock_speed(struct drm_device *dev)
4191{
4192 u16 gcfgc = 0;
4193
4194 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4195
4196 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08004197 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07004198 else {
4199 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4200 case GC_DISPLAY_CLOCK_333_MHZ:
4201 return 333000;
4202 default:
4203 case GC_DISPLAY_CLOCK_190_200_MHZ:
4204 return 190000;
4205 }
4206 }
4207}
Jesse Barnes79e53942008-11-07 14:24:08 -08004208
Jesse Barnese70236a2009-09-21 10:42:27 -07004209static int i865_get_display_clock_speed(struct drm_device *dev)
4210{
4211 return 266000;
4212}
4213
4214static int i855_get_display_clock_speed(struct drm_device *dev)
4215{
4216 u16 hpllcc = 0;
4217 /* Assume that the hardware is in the high speed state. This
4218 * should be the default.
4219 */
4220 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
4221 case GC_CLOCK_133_200:
4222 case GC_CLOCK_100_200:
4223 return 200000;
4224 case GC_CLOCK_166_250:
4225 return 250000;
4226 case GC_CLOCK_100_133:
4227 return 133000;
4228 }
4229
4230 /* Shouldn't happen */
4231 return 0;
4232}
4233
4234static int i830_get_display_clock_speed(struct drm_device *dev)
4235{
4236 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08004237}
4238
Zhenyu Wang2c072452009-06-05 15:38:42 +08004239static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004240intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004241{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004242 while (*num > DATA_LINK_M_N_MASK ||
4243 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08004244 *num >>= 1;
4245 *den >>= 1;
4246 }
4247}
4248
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004249static void compute_m_n(unsigned int m, unsigned int n,
4250 uint32_t *ret_m, uint32_t *ret_n)
4251{
4252 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
4253 *ret_m = div_u64((uint64_t) m * *ret_n, n);
4254 intel_reduce_m_n_ratio(ret_m, ret_n);
4255}
4256
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004257void
4258intel_link_compute_m_n(int bits_per_pixel, int nlanes,
4259 int pixel_clock, int link_clock,
4260 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004261{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004262 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004263
4264 compute_m_n(bits_per_pixel * pixel_clock,
4265 link_clock * nlanes * 8,
4266 &m_n->gmch_m, &m_n->gmch_n);
4267
4268 compute_m_n(pixel_clock, link_clock,
4269 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004270}
4271
Chris Wilsona7615032011-01-12 17:04:08 +00004272static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
4273{
Keith Packard72bbe582011-09-26 16:09:45 -07004274 if (i915_panel_use_ssc >= 0)
4275 return i915_panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004276 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07004277 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00004278}
4279
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004280static int vlv_get_refclk(struct drm_crtc *crtc)
4281{
4282 struct drm_device *dev = crtc->dev;
4283 struct drm_i915_private *dev_priv = dev->dev_private;
4284 int refclk = 27000; /* for DP & HDMI */
4285
4286 return 100000; /* only one validated so far */
4287
4288 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
4289 refclk = 96000;
4290 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
4291 if (intel_panel_use_ssc(dev_priv))
4292 refclk = 100000;
4293 else
4294 refclk = 96000;
4295 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
4296 refclk = 100000;
4297 }
4298
4299 return refclk;
4300}
4301
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004302static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
4303{
4304 struct drm_device *dev = crtc->dev;
4305 struct drm_i915_private *dev_priv = dev->dev_private;
4306 int refclk;
4307
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004308 if (IS_VALLEYVIEW(dev)) {
4309 refclk = vlv_get_refclk(crtc);
4310 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004311 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004312 refclk = dev_priv->vbt.lvds_ssc_freq * 1000;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004313 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
4314 refclk / 1000);
4315 } else if (!IS_GEN2(dev)) {
4316 refclk = 96000;
4317 } else {
4318 refclk = 48000;
4319 }
4320
4321 return refclk;
4322}
4323
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004324static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004325{
Daniel Vetter7df00d72013-05-21 21:54:55 +02004326 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004327}
Daniel Vetterf47709a2013-03-28 10:42:02 +01004328
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004329static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
4330{
4331 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004332}
4333
Daniel Vetterf47709a2013-03-28 10:42:02 +01004334static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Jesse Barnesa7516a02011-12-15 12:30:37 -08004335 intel_clock_t *reduced_clock)
4336{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004337 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004338 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004339 int pipe = crtc->pipe;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004340 u32 fp, fp2 = 0;
4341
4342 if (IS_PINEVIEW(dev)) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004343 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004344 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004345 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004346 } else {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004347 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004348 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004349 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004350 }
4351
4352 I915_WRITE(FP0(pipe), fp);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004353 crtc->config.dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004354
Daniel Vetterf47709a2013-03-28 10:42:02 +01004355 crtc->lowfreq_avail = false;
4356 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Jesse Barnesa7516a02011-12-15 12:30:37 -08004357 reduced_clock && i915_powersave) {
4358 I915_WRITE(FP1(pipe), fp2);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004359 crtc->config.dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004360 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004361 } else {
4362 I915_WRITE(FP1(pipe), fp);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004363 crtc->config.dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004364 }
4365}
4366
Jesse Barnes89b667f2013-04-18 14:51:36 -07004367static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv)
4368{
4369 u32 reg_val;
4370
4371 /*
4372 * PLLB opamp always calibrates to max value of 0x3f, force enable it
4373 * and set it to a reasonable value instead.
4374 */
Jani Nikulaae992582013-05-22 15:36:19 +03004375 reg_val = vlv_dpio_read(dev_priv, DPIO_IREF(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004376 reg_val &= 0xffffff00;
4377 reg_val |= 0x00000030;
Jani Nikulaae992582013-05-22 15:36:19 +03004378 vlv_dpio_write(dev_priv, DPIO_IREF(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004379
Jani Nikulaae992582013-05-22 15:36:19 +03004380 reg_val = vlv_dpio_read(dev_priv, DPIO_CALIBRATION);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004381 reg_val &= 0x8cffffff;
4382 reg_val = 0x8c000000;
Jani Nikulaae992582013-05-22 15:36:19 +03004383 vlv_dpio_write(dev_priv, DPIO_CALIBRATION, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004384
Jani Nikulaae992582013-05-22 15:36:19 +03004385 reg_val = vlv_dpio_read(dev_priv, DPIO_IREF(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004386 reg_val &= 0xffffff00;
Jani Nikulaae992582013-05-22 15:36:19 +03004387 vlv_dpio_write(dev_priv, DPIO_IREF(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004388
Jani Nikulaae992582013-05-22 15:36:19 +03004389 reg_val = vlv_dpio_read(dev_priv, DPIO_CALIBRATION);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004390 reg_val &= 0x00ffffff;
4391 reg_val |= 0xb0000000;
Jani Nikulaae992582013-05-22 15:36:19 +03004392 vlv_dpio_write(dev_priv, DPIO_CALIBRATION, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004393}
4394
Daniel Vetterb5518422013-05-03 11:49:48 +02004395static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
4396 struct intel_link_m_n *m_n)
4397{
4398 struct drm_device *dev = crtc->base.dev;
4399 struct drm_i915_private *dev_priv = dev->dev_private;
4400 int pipe = crtc->pipe;
4401
Daniel Vettere3b95f12013-05-03 11:49:49 +02004402 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4403 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
4404 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
4405 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02004406}
4407
4408static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
4409 struct intel_link_m_n *m_n)
4410{
4411 struct drm_device *dev = crtc->base.dev;
4412 struct drm_i915_private *dev_priv = dev->dev_private;
4413 int pipe = crtc->pipe;
4414 enum transcoder transcoder = crtc->config.cpu_transcoder;
4415
4416 if (INTEL_INFO(dev)->gen >= 5) {
4417 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
4418 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
4419 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
4420 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
4421 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02004422 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4423 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
4424 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
4425 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02004426 }
4427}
4428
Daniel Vetter03afc4a2013-04-02 23:42:31 +02004429static void intel_dp_set_m_n(struct intel_crtc *crtc)
4430{
4431 if (crtc->config.has_pch_encoder)
4432 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4433 else
4434 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4435}
4436
Daniel Vetterf47709a2013-03-28 10:42:02 +01004437static void vlv_update_pll(struct intel_crtc *crtc)
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004438{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004439 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004440 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004441 int pipe = crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004442 u32 dpll, mdiv;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004443 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004444 bool is_hdmi;
Daniel Vetter198a037f2013-04-19 11:14:37 +02004445 u32 coreclk, reg_val, dpll_md;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004446
Daniel Vetter09153002012-12-12 14:06:44 +01004447 mutex_lock(&dev_priv->dpio_lock);
4448
Jesse Barnes89b667f2013-04-18 14:51:36 -07004449 is_hdmi = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004450
Daniel Vetterf47709a2013-03-28 10:42:02 +01004451 bestn = crtc->config.dpll.n;
4452 bestm1 = crtc->config.dpll.m1;
4453 bestm2 = crtc->config.dpll.m2;
4454 bestp1 = crtc->config.dpll.p1;
4455 bestp2 = crtc->config.dpll.p2;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004456
Jesse Barnes89b667f2013-04-18 14:51:36 -07004457 /* See eDP HDMI DPIO driver vbios notes doc */
4458
4459 /* PLL B needs special handling */
4460 if (pipe)
4461 vlv_pllb_recal_opamp(dev_priv);
4462
4463 /* Set up Tx target for periodic Rcomp update */
Jani Nikulaae992582013-05-22 15:36:19 +03004464 vlv_dpio_write(dev_priv, DPIO_IREF_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004465
4466 /* Disable target IRef on PLL */
Jani Nikulaae992582013-05-22 15:36:19 +03004467 reg_val = vlv_dpio_read(dev_priv, DPIO_IREF_CTL(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004468 reg_val &= 0x00ffffff;
Jani Nikulaae992582013-05-22 15:36:19 +03004469 vlv_dpio_write(dev_priv, DPIO_IREF_CTL(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004470
4471 /* Disable fast lock */
Jani Nikulaae992582013-05-22 15:36:19 +03004472 vlv_dpio_write(dev_priv, DPIO_FASTCLK_DISABLE, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004473
4474 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004475 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
4476 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
4477 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004478 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07004479
4480 /*
4481 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
4482 * but we don't support that).
4483 * Note: don't use the DAC post divider as it seems unstable.
4484 */
4485 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Jani Nikulaae992582013-05-22 15:36:19 +03004486 vlv_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004487
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004488 mdiv |= DPIO_ENABLE_CALIBRATION;
Jani Nikulaae992582013-05-22 15:36:19 +03004489 vlv_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004490
Jesse Barnes89b667f2013-04-18 14:51:36 -07004491 /* Set HBR and RBR LPF coefficients */
Daniel Vetterff9a6752013-06-01 17:16:21 +02004492 if (crtc->config.port_clock == 162000 ||
Ville Syrjälä99750bd2013-06-14 14:02:52 +03004493 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
Jesse Barnes89b667f2013-04-18 14:51:36 -07004494 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
Ville Syrjälä4abb2c32013-06-14 14:02:53 +03004495 vlv_dpio_write(dev_priv, DPIO_LPF_COEFF(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03004496 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004497 else
Ville Syrjälä4abb2c32013-06-14 14:02:53 +03004498 vlv_dpio_write(dev_priv, DPIO_LPF_COEFF(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004499 0x00d0000f);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004500
Jesse Barnes89b667f2013-04-18 14:51:36 -07004501 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
4502 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
4503 /* Use SSC source */
4504 if (!pipe)
Jani Nikulaae992582013-05-22 15:36:19 +03004505 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004506 0x0df40000);
4507 else
Jani Nikulaae992582013-05-22 15:36:19 +03004508 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004509 0x0df70000);
4510 } else { /* HDMI or VGA */
4511 /* Use bend source */
4512 if (!pipe)
Jani Nikulaae992582013-05-22 15:36:19 +03004513 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004514 0x0df70000);
4515 else
Jani Nikulaae992582013-05-22 15:36:19 +03004516 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004517 0x0df40000);
4518 }
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004519
Jani Nikulaae992582013-05-22 15:36:19 +03004520 coreclk = vlv_dpio_read(dev_priv, DPIO_CORE_CLK(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004521 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
4522 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
4523 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
4524 coreclk |= 0x01000000;
Jani Nikulaae992582013-05-22 15:36:19 +03004525 vlv_dpio_write(dev_priv, DPIO_CORE_CLK(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004526
Jani Nikulaae992582013-05-22 15:36:19 +03004527 vlv_dpio_write(dev_priv, DPIO_PLL_CML(pipe), 0x87871000);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004528
Jesse Barnes89b667f2013-04-18 14:51:36 -07004529 /* Enable DPIO clock input */
4530 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
4531 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
4532 if (pipe)
4533 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004534
4535 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004536 crtc->config.dpll_hw_state.dpll = dpll;
4537
Daniel Vetteref1b4602013-06-01 17:17:04 +02004538 dpll_md = (crtc->config.pixel_multiplier - 1)
4539 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004540 crtc->config.dpll_hw_state.dpll_md = dpll_md;
4541
Daniel Vetterf47709a2013-03-28 10:42:02 +01004542 if (crtc->config.has_dp_encoder)
4543 intel_dp_set_m_n(crtc);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304544
Daniel Vetter09153002012-12-12 14:06:44 +01004545 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004546}
4547
Daniel Vetterf47709a2013-03-28 10:42:02 +01004548static void i9xx_update_pll(struct intel_crtc *crtc,
4549 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004550 int num_connectors)
4551{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004552 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004553 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004554 u32 dpll;
4555 bool is_sdvo;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004556 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004557
Daniel Vetterf47709a2013-03-28 10:42:02 +01004558 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304559
Daniel Vetterf47709a2013-03-28 10:42:02 +01004560 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
4561 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004562
4563 dpll = DPLL_VGA_MODE_DIS;
4564
Daniel Vetterf47709a2013-03-28 10:42:02 +01004565 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004566 dpll |= DPLLB_MODE_LVDS;
4567 else
4568 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01004569
Daniel Vetteref1b4602013-06-01 17:17:04 +02004570 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Daniel Vetter198a037f2013-04-19 11:14:37 +02004571 dpll |= (crtc->config.pixel_multiplier - 1)
4572 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004573 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02004574
4575 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02004576 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02004577
Daniel Vetterf47709a2013-03-28 10:42:02 +01004578 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
Daniel Vetter4a33e482013-07-06 12:52:05 +02004579 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004580
4581 /* compute bitmask from p1 value */
4582 if (IS_PINEVIEW(dev))
4583 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
4584 else {
4585 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4586 if (IS_G4X(dev) && reduced_clock)
4587 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
4588 }
4589 switch (clock->p2) {
4590 case 5:
4591 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4592 break;
4593 case 7:
4594 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4595 break;
4596 case 10:
4597 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4598 break;
4599 case 14:
4600 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4601 break;
4602 }
4603 if (INTEL_INFO(dev)->gen >= 4)
4604 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
4605
Daniel Vetter09ede542013-04-30 14:01:45 +02004606 if (crtc->config.sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004607 dpll |= PLL_REF_INPUT_TVCLKINBC;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004608 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004609 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4610 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4611 else
4612 dpll |= PLL_REF_INPUT_DREFCLK;
4613
4614 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004615 crtc->config.dpll_hw_state.dpll = dpll;
4616
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004617 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetteref1b4602013-06-01 17:17:04 +02004618 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
4619 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004620 crtc->config.dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004621 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02004622
4623 if (crtc->config.has_dp_encoder)
4624 intel_dp_set_m_n(crtc);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004625}
4626
Daniel Vetterf47709a2013-03-28 10:42:02 +01004627static void i8xx_update_pll(struct intel_crtc *crtc,
Daniel Vetterf47709a2013-03-28 10:42:02 +01004628 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004629 int num_connectors)
4630{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004631 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004632 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004633 u32 dpll;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004634 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004635
Daniel Vetterf47709a2013-03-28 10:42:02 +01004636 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304637
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004638 dpll = DPLL_VGA_MODE_DIS;
4639
Daniel Vetterf47709a2013-03-28 10:42:02 +01004640 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004641 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4642 } else {
4643 if (clock->p1 == 2)
4644 dpll |= PLL_P1_DIVIDE_BY_TWO;
4645 else
4646 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4647 if (clock->p2 == 4)
4648 dpll |= PLL_P2_DIVIDE_BY_4;
4649 }
4650
Daniel Vetter4a33e482013-07-06 12:52:05 +02004651 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
4652 dpll |= DPLL_DVO_2X_MODE;
4653
Daniel Vetterf47709a2013-03-28 10:42:02 +01004654 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004655 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4656 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4657 else
4658 dpll |= PLL_REF_INPUT_DREFCLK;
4659
4660 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004661 crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004662}
4663
Daniel Vetter8a654f32013-06-01 17:16:22 +02004664static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004665{
4666 struct drm_device *dev = intel_crtc->base.dev;
4667 struct drm_i915_private *dev_priv = dev->dev_private;
4668 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02004669 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02004670 struct drm_display_mode *adjusted_mode =
4671 &intel_crtc->config.adjusted_mode;
4672 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004673 uint32_t vsyncshift, crtc_vtotal, crtc_vblank_end;
4674
4675 /* We need to be careful not to changed the adjusted mode, for otherwise
4676 * the hw state checker will get angry at the mismatch. */
4677 crtc_vtotal = adjusted_mode->crtc_vtotal;
4678 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004679
4680 if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4681 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004682 crtc_vtotal -= 1;
4683 crtc_vblank_end -= 1;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004684 vsyncshift = adjusted_mode->crtc_hsync_start
4685 - adjusted_mode->crtc_htotal / 2;
4686 } else {
4687 vsyncshift = 0;
4688 }
4689
4690 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004691 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004692
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004693 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004694 (adjusted_mode->crtc_hdisplay - 1) |
4695 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004696 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004697 (adjusted_mode->crtc_hblank_start - 1) |
4698 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004699 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004700 (adjusted_mode->crtc_hsync_start - 1) |
4701 ((adjusted_mode->crtc_hsync_end - 1) << 16));
4702
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004703 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004704 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004705 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004706 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004707 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004708 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004709 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004710 (adjusted_mode->crtc_vsync_start - 1) |
4711 ((adjusted_mode->crtc_vsync_end - 1) << 16));
4712
Paulo Zanonib5e508d2012-10-24 11:34:43 -02004713 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
4714 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
4715 * documented on the DDI_FUNC_CTL register description, EDP Input Select
4716 * bits. */
4717 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
4718 (pipe == PIPE_B || pipe == PIPE_C))
4719 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
4720
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004721 /* pipesrc controls the size that is scaled from, which should
4722 * always be the user's requested size.
4723 */
4724 I915_WRITE(PIPESRC(pipe),
4725 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
4726}
4727
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02004728static void intel_get_pipe_timings(struct intel_crtc *crtc,
4729 struct intel_crtc_config *pipe_config)
4730{
4731 struct drm_device *dev = crtc->base.dev;
4732 struct drm_i915_private *dev_priv = dev->dev_private;
4733 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
4734 uint32_t tmp;
4735
4736 tmp = I915_READ(HTOTAL(cpu_transcoder));
4737 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
4738 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
4739 tmp = I915_READ(HBLANK(cpu_transcoder));
4740 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
4741 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
4742 tmp = I915_READ(HSYNC(cpu_transcoder));
4743 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
4744 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
4745
4746 tmp = I915_READ(VTOTAL(cpu_transcoder));
4747 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
4748 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
4749 tmp = I915_READ(VBLANK(cpu_transcoder));
4750 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
4751 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
4752 tmp = I915_READ(VSYNC(cpu_transcoder));
4753 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
4754 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
4755
4756 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
4757 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
4758 pipe_config->adjusted_mode.crtc_vtotal += 1;
4759 pipe_config->adjusted_mode.crtc_vblank_end += 1;
4760 }
4761
4762 tmp = I915_READ(PIPESRC(crtc->pipe));
4763 pipe_config->requested_mode.vdisplay = (tmp & 0xffff) + 1;
4764 pipe_config->requested_mode.hdisplay = ((tmp >> 16) & 0xffff) + 1;
4765}
4766
Jesse Barnesbabea612013-06-26 18:57:38 +03004767static void intel_crtc_mode_from_pipe_config(struct intel_crtc *intel_crtc,
4768 struct intel_crtc_config *pipe_config)
4769{
4770 struct drm_crtc *crtc = &intel_crtc->base;
4771
4772 crtc->mode.hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
4773 crtc->mode.htotal = pipe_config->adjusted_mode.crtc_htotal;
4774 crtc->mode.hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
4775 crtc->mode.hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
4776
4777 crtc->mode.vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
4778 crtc->mode.vtotal = pipe_config->adjusted_mode.crtc_vtotal;
4779 crtc->mode.vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
4780 crtc->mode.vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
4781
4782 crtc->mode.flags = pipe_config->adjusted_mode.flags;
4783
4784 crtc->mode.clock = pipe_config->adjusted_mode.clock;
4785 crtc->mode.flags |= pipe_config->adjusted_mode.flags;
4786}
4787
Daniel Vetter84b046f2013-02-19 18:48:54 +01004788static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
4789{
4790 struct drm_device *dev = intel_crtc->base.dev;
4791 struct drm_i915_private *dev_priv = dev->dev_private;
4792 uint32_t pipeconf;
4793
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02004794 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01004795
4796 if (intel_crtc->pipe == 0 && INTEL_INFO(dev)->gen < 4) {
4797 /* Enable pixel doubling when the dot clock is > 90% of the (display)
4798 * core speed.
4799 *
4800 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
4801 * pipe == 0 check?
4802 */
4803 if (intel_crtc->config.requested_mode.clock >
4804 dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
4805 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01004806 }
4807
Daniel Vetterff9ce462013-04-24 14:57:17 +02004808 /* only g4x and later have fancy bpc/dither controls */
4809 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02004810 /* Bspec claims that we can't use dithering for 30bpp pipes. */
4811 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
4812 pipeconf |= PIPECONF_DITHER_EN |
4813 PIPECONF_DITHER_TYPE_SP;
4814
4815 switch (intel_crtc->config.pipe_bpp) {
4816 case 18:
4817 pipeconf |= PIPECONF_6BPC;
4818 break;
4819 case 24:
4820 pipeconf |= PIPECONF_8BPC;
4821 break;
4822 case 30:
4823 pipeconf |= PIPECONF_10BPC;
4824 break;
4825 default:
4826 /* Case prevented by intel_choose_pipe_bpp_dither. */
4827 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01004828 }
4829 }
4830
4831 if (HAS_PIPE_CXSR(dev)) {
4832 if (intel_crtc->lowfreq_avail) {
4833 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
4834 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
4835 } else {
4836 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01004837 }
4838 }
4839
Daniel Vetter84b046f2013-02-19 18:48:54 +01004840 if (!IS_GEN2(dev) &&
4841 intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
4842 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
4843 else
4844 pipeconf |= PIPECONF_PROGRESSIVE;
4845
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02004846 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
4847 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03004848
Daniel Vetter84b046f2013-02-19 18:48:54 +01004849 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
4850 POSTING_READ(PIPECONF(intel_crtc->pipe));
4851}
4852
Eric Anholtf564048e2011-03-30 13:01:02 -07004853static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07004854 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02004855 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08004856{
4857 struct drm_device *dev = crtc->dev;
4858 struct drm_i915_private *dev_priv = dev->dev_private;
4859 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01004860 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08004861 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07004862 int plane = intel_crtc->plane;
Eric Anholtc751ce42010-03-25 11:48:48 -07004863 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07004864 intel_clock_t clock, reduced_clock;
Daniel Vetter84b046f2013-02-19 18:48:54 +01004865 u32 dspcntr;
Daniel Vettera16af7212013-04-30 14:01:44 +02004866 bool ok, has_reduced_clock = false;
4867 bool is_lvds = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01004868 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08004869 const intel_limit_t *limit;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00004870 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08004871
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02004872 for_each_encoder_on_crtc(dev, crtc, encoder) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004873 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004874 case INTEL_OUTPUT_LVDS:
4875 is_lvds = true;
4876 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08004877 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05004878
Eric Anholtc751ce42010-03-25 11:48:48 -07004879 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08004880 }
4881
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004882 refclk = i9xx_get_refclk(crtc, num_connectors);
Jesse Barnes79e53942008-11-07 14:24:08 -08004883
Ma Lingd4906092009-03-18 20:13:27 +08004884 /*
4885 * Returns a set of divisors for the desired target clock with the given
4886 * refclk, or FALSE. The returned values represent the clock equation:
4887 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
4888 */
Chris Wilson1b894b52010-12-14 20:04:54 +00004889 limit = intel_limit(crtc, refclk);
Daniel Vetterff9a6752013-06-01 17:16:21 +02004890 ok = dev_priv->display.find_dpll(limit, crtc,
4891 intel_crtc->config.port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02004892 refclk, NULL, &clock);
4893 if (!ok && !intel_crtc->config.clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004894 DRM_ERROR("Couldn't find PLL settings for mode!\n");
Eric Anholtf564048e2011-03-30 13:01:02 -07004895 return -EINVAL;
4896 }
4897
4898 /* Ensure that the cursor is valid for the new mode before changing... */
4899 intel_crtc_update_cursor(crtc, true);
4900
4901 if (is_lvds && dev_priv->lvds_downclock_avail) {
Sean Paulcec2f352012-01-10 15:09:36 -08004902 /*
4903 * Ensure we match the reduced clock's P to the target clock.
4904 * If the clocks don't match, we can't switch the display clock
4905 * by using the FP0/FP1. In such case we will disable the LVDS
4906 * downclock feature.
4907 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02004908 has_reduced_clock =
4909 dev_priv->display.find_dpll(limit, crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07004910 dev_priv->lvds_downclock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02004911 refclk, &clock,
Eric Anholtf564048e2011-03-30 13:01:02 -07004912 &reduced_clock);
Eric Anholtf564048e2011-03-30 13:01:02 -07004913 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01004914 /* Compat-code for transition, will disappear. */
4915 if (!intel_crtc->config.clock_set) {
4916 intel_crtc->config.dpll.n = clock.n;
4917 intel_crtc->config.dpll.m1 = clock.m1;
4918 intel_crtc->config.dpll.m2 = clock.m2;
4919 intel_crtc->config.dpll.p1 = clock.p1;
4920 intel_crtc->config.dpll.p2 = clock.p2;
4921 }
Eric Anholtf564048e2011-03-30 13:01:02 -07004922
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004923 if (IS_GEN2(dev))
Daniel Vetter8a654f32013-06-01 17:16:22 +02004924 i8xx_update_pll(intel_crtc,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304925 has_reduced_clock ? &reduced_clock : NULL,
4926 num_connectors);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004927 else if (IS_VALLEYVIEW(dev))
Daniel Vetterf47709a2013-03-28 10:42:02 +01004928 vlv_update_pll(intel_crtc);
Eric Anholtf564048e2011-03-30 13:01:02 -07004929 else
Daniel Vetterf47709a2013-03-28 10:42:02 +01004930 i9xx_update_pll(intel_crtc,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004931 has_reduced_clock ? &reduced_clock : NULL,
Jesse Barnes89b667f2013-04-18 14:51:36 -07004932 num_connectors);
Eric Anholtf564048e2011-03-30 13:01:02 -07004933
Eric Anholtf564048e2011-03-30 13:01:02 -07004934 /* Set up the display plane register */
4935 dspcntr = DISPPLANE_GAMMA_ENABLE;
4936
Jesse Barnesda6ecc52013-03-08 10:46:00 -08004937 if (!IS_VALLEYVIEW(dev)) {
4938 if (pipe == 0)
4939 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
4940 else
4941 dspcntr |= DISPPLANE_SEL_PIPE_B;
4942 }
Eric Anholtf564048e2011-03-30 13:01:02 -07004943
Daniel Vetter8a654f32013-06-01 17:16:22 +02004944 intel_set_pipe_timings(intel_crtc);
Eric Anholtf564048e2011-03-30 13:01:02 -07004945
4946 /* pipesrc and dspsize control the size that is scaled from,
4947 * which should always be the user's requested size.
4948 */
Eric Anholt929c77f2011-03-30 13:01:04 -07004949 I915_WRITE(DSPSIZE(plane),
4950 ((mode->vdisplay - 1) << 16) |
4951 (mode->hdisplay - 1));
4952 I915_WRITE(DSPPOS(plane), 0);
Eric Anholtf564048e2011-03-30 13:01:02 -07004953
Daniel Vetter84b046f2013-02-19 18:48:54 +01004954 i9xx_set_pipeconf(intel_crtc);
4955
Eric Anholtf564048e2011-03-30 13:01:02 -07004956 I915_WRITE(DSPCNTR(plane), dspcntr);
4957 POSTING_READ(DSPCNTR(plane));
4958
Daniel Vetter94352cf2012-07-05 22:51:56 +02004959 ret = intel_pipe_set_base(crtc, x, y, fb);
Eric Anholtf564048e2011-03-30 13:01:02 -07004960
4961 intel_update_watermarks(dev);
4962
Eric Anholtf564048e2011-03-30 13:01:02 -07004963 return ret;
4964}
4965
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02004966static void i9xx_get_pfit_config(struct intel_crtc *crtc,
4967 struct intel_crtc_config *pipe_config)
4968{
4969 struct drm_device *dev = crtc->base.dev;
4970 struct drm_i915_private *dev_priv = dev->dev_private;
4971 uint32_t tmp;
4972
4973 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02004974 if (!(tmp & PFIT_ENABLE))
4975 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02004976
Daniel Vetter06922822013-07-11 13:35:40 +02004977 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02004978 if (INTEL_INFO(dev)->gen < 4) {
4979 if (crtc->pipe != PIPE_B)
4980 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02004981 } else {
4982 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
4983 return;
4984 }
4985
Daniel Vetter06922822013-07-11 13:35:40 +02004986 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02004987 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
4988 if (INTEL_INFO(dev)->gen < 5)
4989 pipe_config->gmch_pfit.lvds_border_bits =
4990 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
4991}
4992
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01004993static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
4994 struct intel_crtc_config *pipe_config)
4995{
4996 struct drm_device *dev = crtc->base.dev;
4997 struct drm_i915_private *dev_priv = dev->dev_private;
4998 uint32_t tmp;
4999
Daniel Vettere143a212013-07-04 12:01:15 +02005000 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02005001 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02005002
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005003 tmp = I915_READ(PIPECONF(crtc->pipe));
5004 if (!(tmp & PIPECONF_ENABLE))
5005 return false;
5006
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005007 intel_get_pipe_timings(crtc, pipe_config);
5008
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005009 i9xx_get_pfit_config(crtc, pipe_config);
5010
Daniel Vetter6c49f242013-06-06 12:45:25 +02005011 if (INTEL_INFO(dev)->gen >= 4) {
5012 tmp = I915_READ(DPLL_MD(crtc->pipe));
5013 pipe_config->pixel_multiplier =
5014 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
5015 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005016 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02005017 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
5018 tmp = I915_READ(DPLL(crtc->pipe));
5019 pipe_config->pixel_multiplier =
5020 ((tmp & SDVO_MULTIPLIER_MASK)
5021 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
5022 } else {
5023 /* Note that on i915G/GM the pixel multiplier is in the sdvo
5024 * port and will be fixed up in the encoder->get_config
5025 * function. */
5026 pipe_config->pixel_multiplier = 1;
5027 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005028 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
5029 if (!IS_VALLEYVIEW(dev)) {
5030 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
5031 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03005032 } else {
5033 /* Mask out read-only status bits. */
5034 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
5035 DPLL_PORTC_READY_MASK |
5036 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005037 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02005038
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005039 return true;
5040}
5041
Paulo Zanonidde86e22012-12-01 12:04:25 -02005042static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07005043{
5044 struct drm_i915_private *dev_priv = dev->dev_private;
5045 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005046 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005047 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005048 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005049 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005050 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07005051 bool has_ck505 = false;
5052 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005053
5054 /* We need to take the global config into account */
Keith Packard199e5d72011-09-22 12:01:57 -07005055 list_for_each_entry(encoder, &mode_config->encoder_list,
5056 base.head) {
5057 switch (encoder->type) {
5058 case INTEL_OUTPUT_LVDS:
5059 has_panel = true;
5060 has_lvds = true;
5061 break;
5062 case INTEL_OUTPUT_EDP:
5063 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03005064 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07005065 has_cpu_edp = true;
5066 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005067 }
5068 }
5069
Keith Packard99eb6a02011-09-26 14:29:12 -07005070 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005071 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07005072 can_ssc = has_ck505;
5073 } else {
5074 has_ck505 = false;
5075 can_ssc = true;
5076 }
5077
Imre Deak2de69052013-05-08 13:14:04 +03005078 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
5079 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005080
5081 /* Ironlake: try to setup display ref clock before DPLL
5082 * enabling. This is only under driver's control after
5083 * PCH B stepping, previous chipset stepping should be
5084 * ignoring this setting.
5085 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005086 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005087
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005088 /* As we must carefully and slowly disable/enable each source in turn,
5089 * compute the final state we want first and check if we need to
5090 * make any changes at all.
5091 */
5092 final = val;
5093 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07005094 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005095 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07005096 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005097 final |= DREF_NONSPREAD_SOURCE_ENABLE;
5098
5099 final &= ~DREF_SSC_SOURCE_MASK;
5100 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5101 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005102
Keith Packard199e5d72011-09-22 12:01:57 -07005103 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005104 final |= DREF_SSC_SOURCE_ENABLE;
5105
5106 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5107 final |= DREF_SSC1_ENABLE;
5108
5109 if (has_cpu_edp) {
5110 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5111 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
5112 else
5113 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
5114 } else
5115 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5116 } else {
5117 final |= DREF_SSC_SOURCE_DISABLE;
5118 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5119 }
5120
5121 if (final == val)
5122 return;
5123
5124 /* Always enable nonspread source */
5125 val &= ~DREF_NONSPREAD_SOURCE_MASK;
5126
5127 if (has_ck505)
5128 val |= DREF_NONSPREAD_CK505_ENABLE;
5129 else
5130 val |= DREF_NONSPREAD_SOURCE_ENABLE;
5131
5132 if (has_panel) {
5133 val &= ~DREF_SSC_SOURCE_MASK;
5134 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005135
Keith Packard199e5d72011-09-22 12:01:57 -07005136 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07005137 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005138 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005139 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02005140 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005141 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005142
5143 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005144 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005145 POSTING_READ(PCH_DREF_CONTROL);
5146 udelay(200);
5147
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005148 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005149
5150 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07005151 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07005152 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005153 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005154 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005155 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07005156 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005157 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005158 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005159 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005160
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005161 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005162 POSTING_READ(PCH_DREF_CONTROL);
5163 udelay(200);
5164 } else {
5165 DRM_DEBUG_KMS("Disabling SSC entirely\n");
5166
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005167 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07005168
5169 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005170 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005171
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005172 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005173 POSTING_READ(PCH_DREF_CONTROL);
5174 udelay(200);
5175
5176 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005177 val &= ~DREF_SSC_SOURCE_MASK;
5178 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005179
5180 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005181 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005182
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005183 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005184 POSTING_READ(PCH_DREF_CONTROL);
5185 udelay(200);
5186 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005187
5188 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005189}
5190
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005191static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02005192{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005193 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02005194
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005195 tmp = I915_READ(SOUTH_CHICKEN2);
5196 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
5197 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005198
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005199 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
5200 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
5201 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02005202
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005203 tmp = I915_READ(SOUTH_CHICKEN2);
5204 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
5205 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005206
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005207 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
5208 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
5209 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005210}
5211
5212/* WaMPhyProgramming:hsw */
5213static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
5214{
5215 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02005216
5217 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
5218 tmp &= ~(0xFF << 24);
5219 tmp |= (0x12 << 24);
5220 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
5221
Paulo Zanonidde86e22012-12-01 12:04:25 -02005222 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
5223 tmp |= (1 << 11);
5224 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
5225
5226 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
5227 tmp |= (1 << 11);
5228 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
5229
Paulo Zanonidde86e22012-12-01 12:04:25 -02005230 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
5231 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5232 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
5233
5234 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
5235 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5236 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
5237
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005238 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
5239 tmp &= ~(7 << 13);
5240 tmp |= (5 << 13);
5241 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005242
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005243 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
5244 tmp &= ~(7 << 13);
5245 tmp |= (5 << 13);
5246 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005247
5248 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
5249 tmp &= ~0xFF;
5250 tmp |= 0x1C;
5251 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
5252
5253 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
5254 tmp &= ~0xFF;
5255 tmp |= 0x1C;
5256 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
5257
5258 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
5259 tmp &= ~(0xFF << 16);
5260 tmp |= (0x1C << 16);
5261 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
5262
5263 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
5264 tmp &= ~(0xFF << 16);
5265 tmp |= (0x1C << 16);
5266 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
5267
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005268 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
5269 tmp |= (1 << 27);
5270 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005271
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005272 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
5273 tmp |= (1 << 27);
5274 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005275
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005276 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
5277 tmp &= ~(0xF << 28);
5278 tmp |= (4 << 28);
5279 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005280
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03005281 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
5282 tmp &= ~(0xF << 28);
5283 tmp |= (4 << 28);
5284 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005285}
5286
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005287/* Implements 3 different sequences from BSpec chapter "Display iCLK
5288 * Programming" based on the parameters passed:
5289 * - Sequence to enable CLKOUT_DP
5290 * - Sequence to enable CLKOUT_DP without spread
5291 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
5292 */
5293static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
5294 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005295{
5296 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005297 uint32_t reg, tmp;
5298
5299 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
5300 with_spread = true;
5301 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
5302 with_fdi, "LP PCH doesn't have FDI\n"))
5303 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005304
5305 mutex_lock(&dev_priv->dpio_lock);
5306
5307 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5308 tmp &= ~SBI_SSCCTL_DISABLE;
5309 tmp |= SBI_SSCCTL_PATHALT;
5310 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5311
5312 udelay(24);
5313
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005314 if (with_spread) {
5315 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5316 tmp &= ~SBI_SSCCTL_PATHALT;
5317 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03005318
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005319 if (with_fdi) {
5320 lpt_reset_fdi_mphy(dev_priv);
5321 lpt_program_fdi_mphy(dev_priv);
5322 }
5323 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02005324
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03005325 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
5326 SBI_GEN0 : SBI_DBUFF0;
5327 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
5328 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
5329 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01005330
5331 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005332}
5333
Paulo Zanoni47701c32013-07-23 11:19:25 -03005334/* Sequence to disable CLKOUT_DP */
5335static void lpt_disable_clkout_dp(struct drm_device *dev)
5336{
5337 struct drm_i915_private *dev_priv = dev->dev_private;
5338 uint32_t reg, tmp;
5339
5340 mutex_lock(&dev_priv->dpio_lock);
5341
5342 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
5343 SBI_GEN0 : SBI_DBUFF0;
5344 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
5345 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
5346 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
5347
5348 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5349 if (!(tmp & SBI_SSCCTL_DISABLE)) {
5350 if (!(tmp & SBI_SSCCTL_PATHALT)) {
5351 tmp |= SBI_SSCCTL_PATHALT;
5352 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5353 udelay(32);
5354 }
5355 tmp |= SBI_SSCCTL_DISABLE;
5356 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5357 }
5358
5359 mutex_unlock(&dev_priv->dpio_lock);
5360}
5361
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03005362static void lpt_init_pch_refclk(struct drm_device *dev)
5363{
5364 struct drm_mode_config *mode_config = &dev->mode_config;
5365 struct intel_encoder *encoder;
5366 bool has_vga = false;
5367
5368 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5369 switch (encoder->type) {
5370 case INTEL_OUTPUT_ANALOG:
5371 has_vga = true;
5372 break;
5373 }
5374 }
5375
Paulo Zanoni47701c32013-07-23 11:19:25 -03005376 if (has_vga)
5377 lpt_enable_clkout_dp(dev, true, true);
5378 else
5379 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03005380}
5381
Paulo Zanonidde86e22012-12-01 12:04:25 -02005382/*
5383 * Initialize reference clocks when the driver loads
5384 */
5385void intel_init_pch_refclk(struct drm_device *dev)
5386{
5387 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
5388 ironlake_init_pch_refclk(dev);
5389 else if (HAS_PCH_LPT(dev))
5390 lpt_init_pch_refclk(dev);
5391}
5392
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005393static int ironlake_get_refclk(struct drm_crtc *crtc)
5394{
5395 struct drm_device *dev = crtc->dev;
5396 struct drm_i915_private *dev_priv = dev->dev_private;
5397 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005398 int num_connectors = 0;
5399 bool is_lvds = false;
5400
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02005401 for_each_encoder_on_crtc(dev, crtc, encoder) {
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005402 switch (encoder->type) {
5403 case INTEL_OUTPUT_LVDS:
5404 is_lvds = true;
5405 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005406 }
5407 num_connectors++;
5408 }
5409
5410 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
5411 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005412 dev_priv->vbt.lvds_ssc_freq);
5413 return dev_priv->vbt.lvds_ssc_freq * 1000;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005414 }
5415
5416 return 120000;
5417}
5418
Daniel Vetter6ff93602013-04-19 11:24:36 +02005419static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03005420{
5421 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5422 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5423 int pipe = intel_crtc->pipe;
5424 uint32_t val;
5425
Daniel Vetter78114072013-06-13 00:54:57 +02005426 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03005427
Daniel Vetter965e0c42013-03-27 00:44:57 +01005428 switch (intel_crtc->config.pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03005429 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005430 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005431 break;
5432 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005433 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005434 break;
5435 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005436 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005437 break;
5438 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005439 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005440 break;
5441 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03005442 /* Case prevented by intel_choose_pipe_bpp_dither. */
5443 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03005444 }
5445
Daniel Vetterd8b32242013-04-25 17:54:44 +02005446 if (intel_crtc->config.dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03005447 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5448
Daniel Vetter6ff93602013-04-19 11:24:36 +02005449 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03005450 val |= PIPECONF_INTERLACED_ILK;
5451 else
5452 val |= PIPECONF_PROGRESSIVE;
5453
Daniel Vetter50f3b012013-03-27 00:44:56 +01005454 if (intel_crtc->config.limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005455 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005456
Paulo Zanonic8203562012-09-12 10:06:29 -03005457 I915_WRITE(PIPECONF(pipe), val);
5458 POSTING_READ(PIPECONF(pipe));
5459}
5460
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005461/*
5462 * Set up the pipe CSC unit.
5463 *
5464 * Currently only full range RGB to limited range RGB conversion
5465 * is supported, but eventually this should handle various
5466 * RGB<->YCbCr scenarios as well.
5467 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01005468static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005469{
5470 struct drm_device *dev = crtc->dev;
5471 struct drm_i915_private *dev_priv = dev->dev_private;
5472 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5473 int pipe = intel_crtc->pipe;
5474 uint16_t coeff = 0x7800; /* 1.0 */
5475
5476 /*
5477 * TODO: Check what kind of values actually come out of the pipe
5478 * with these coeff/postoff values and adjust to get the best
5479 * accuracy. Perhaps we even need to take the bpc value into
5480 * consideration.
5481 */
5482
Daniel Vetter50f3b012013-03-27 00:44:56 +01005483 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005484 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
5485
5486 /*
5487 * GY/GU and RY/RU should be the other way around according
5488 * to BSpec, but reality doesn't agree. Just set them up in
5489 * a way that results in the correct picture.
5490 */
5491 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
5492 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
5493
5494 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
5495 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
5496
5497 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
5498 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
5499
5500 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
5501 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
5502 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
5503
5504 if (INTEL_INFO(dev)->gen > 6) {
5505 uint16_t postoff = 0;
5506
Daniel Vetter50f3b012013-03-27 00:44:56 +01005507 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005508 postoff = (16 * (1 << 13) / 255) & 0x1fff;
5509
5510 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
5511 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
5512 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
5513
5514 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
5515 } else {
5516 uint32_t mode = CSC_MODE_YUV_TO_RGB;
5517
Daniel Vetter50f3b012013-03-27 00:44:56 +01005518 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005519 mode |= CSC_BLACK_SCREEN_OFFSET;
5520
5521 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
5522 }
5523}
5524
Daniel Vetter6ff93602013-04-19 11:24:36 +02005525static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005526{
5527 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5528 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02005529 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005530 uint32_t val;
5531
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02005532 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005533
Daniel Vetterd8b32242013-04-25 17:54:44 +02005534 if (intel_crtc->config.dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005535 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5536
Daniel Vetter6ff93602013-04-19 11:24:36 +02005537 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005538 val |= PIPECONF_INTERLACED_ILK;
5539 else
5540 val |= PIPECONF_PROGRESSIVE;
5541
Paulo Zanoni702e7a52012-10-23 18:29:59 -02005542 I915_WRITE(PIPECONF(cpu_transcoder), val);
5543 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02005544
5545 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
5546 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005547}
5548
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005549static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005550 intel_clock_t *clock,
5551 bool *has_reduced_clock,
5552 intel_clock_t *reduced_clock)
5553{
5554 struct drm_device *dev = crtc->dev;
5555 struct drm_i915_private *dev_priv = dev->dev_private;
5556 struct intel_encoder *intel_encoder;
5557 int refclk;
5558 const intel_limit_t *limit;
Daniel Vettera16af7212013-04-30 14:01:44 +02005559 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005560
5561 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5562 switch (intel_encoder->type) {
5563 case INTEL_OUTPUT_LVDS:
5564 is_lvds = true;
5565 break;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005566 }
5567 }
5568
5569 refclk = ironlake_get_refclk(crtc);
5570
5571 /*
5572 * Returns a set of divisors for the desired target clock with the given
5573 * refclk, or FALSE. The returned values represent the clock equation:
5574 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
5575 */
5576 limit = intel_limit(crtc, refclk);
Daniel Vetterff9a6752013-06-01 17:16:21 +02005577 ret = dev_priv->display.find_dpll(limit, crtc,
5578 to_intel_crtc(crtc)->config.port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02005579 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005580 if (!ret)
5581 return false;
5582
5583 if (is_lvds && dev_priv->lvds_downclock_avail) {
5584 /*
5585 * Ensure we match the reduced clock's P to the target clock.
5586 * If the clocks don't match, we can't switch the display clock
5587 * by using the FP0/FP1. In such case we will disable the LVDS
5588 * downclock feature.
5589 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02005590 *has_reduced_clock =
5591 dev_priv->display.find_dpll(limit, crtc,
5592 dev_priv->lvds_downclock,
5593 refclk, clock,
5594 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005595 }
5596
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005597 return true;
5598}
5599
Daniel Vetter01a415f2012-10-27 15:58:40 +02005600static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
5601{
5602 struct drm_i915_private *dev_priv = dev->dev_private;
5603 uint32_t temp;
5604
5605 temp = I915_READ(SOUTH_CHICKEN1);
5606 if (temp & FDI_BC_BIFURCATION_SELECT)
5607 return;
5608
5609 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
5610 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
5611
5612 temp |= FDI_BC_BIFURCATION_SELECT;
5613 DRM_DEBUG_KMS("enabling fdi C rx\n");
5614 I915_WRITE(SOUTH_CHICKEN1, temp);
5615 POSTING_READ(SOUTH_CHICKEN1);
5616}
5617
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005618static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
Daniel Vetter01a415f2012-10-27 15:58:40 +02005619{
5620 struct drm_device *dev = intel_crtc->base.dev;
5621 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005622
5623 switch (intel_crtc->pipe) {
5624 case PIPE_A:
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005625 break;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005626 case PIPE_B:
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005627 if (intel_crtc->config.fdi_lanes > 2)
Daniel Vetter01a415f2012-10-27 15:58:40 +02005628 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
5629 else
5630 cpt_enable_fdi_bc_bifurcation(dev);
5631
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005632 break;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005633 case PIPE_C:
Daniel Vetter01a415f2012-10-27 15:58:40 +02005634 cpt_enable_fdi_bc_bifurcation(dev);
5635
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005636 break;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005637 default:
5638 BUG();
5639 }
5640}
5641
Paulo Zanonid4b19312012-11-29 11:29:32 -02005642int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
5643{
5644 /*
5645 * Account for spread spectrum to avoid
5646 * oversubscribing the link. Max center spread
5647 * is 2.5%; use 5% for safety's sake.
5648 */
5649 u32 bps = target_clock * bpp * 21 / 20;
5650 return bps / (link_bw * 8) + 1;
5651}
5652
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005653static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02005654{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005655 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005656}
5657
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005658static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005659 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02005660 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005661{
5662 struct drm_crtc *crtc = &intel_crtc->base;
5663 struct drm_device *dev = crtc->dev;
5664 struct drm_i915_private *dev_priv = dev->dev_private;
5665 struct intel_encoder *intel_encoder;
5666 uint32_t dpll;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01005667 int factor, num_connectors = 0;
Daniel Vetter09ede542013-04-30 14:01:45 +02005668 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005669
5670 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5671 switch (intel_encoder->type) {
5672 case INTEL_OUTPUT_LVDS:
5673 is_lvds = true;
5674 break;
5675 case INTEL_OUTPUT_SDVO:
5676 case INTEL_OUTPUT_HDMI:
5677 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005678 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005679 }
5680
5681 num_connectors++;
5682 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005683
Chris Wilsonc1858122010-12-03 21:35:48 +00005684 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07005685 factor = 21;
5686 if (is_lvds) {
5687 if ((intel_panel_use_ssc(dev_priv) &&
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005688 dev_priv->vbt.lvds_ssc_freq == 100) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02005689 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07005690 factor = 25;
Daniel Vetter09ede542013-04-30 14:01:45 +02005691 } else if (intel_crtc->config.sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07005692 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00005693
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005694 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02005695 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00005696
Daniel Vetter9a7c7892013-04-04 22:20:34 +02005697 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
5698 *fp2 |= FP_CB_TUNE;
5699
Chris Wilson5eddb702010-09-11 13:48:45 +01005700 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005701
Eric Anholta07d6782011-03-30 13:01:08 -07005702 if (is_lvds)
5703 dpll |= DPLLB_MODE_LVDS;
5704 else
5705 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02005706
Daniel Vetteref1b4602013-06-01 17:17:04 +02005707 dpll |= (intel_crtc->config.pixel_multiplier - 1)
5708 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02005709
5710 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02005711 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter9566e9a2013-04-19 11:14:36 +02005712 if (intel_crtc->config.has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02005713 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08005714
Eric Anholta07d6782011-03-30 13:01:08 -07005715 /* compute bitmask from p1 value */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005716 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005717 /* also FPA1 */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005718 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005719
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005720 switch (intel_crtc->config.dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07005721 case 5:
5722 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5723 break;
5724 case 7:
5725 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5726 break;
5727 case 10:
5728 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5729 break;
5730 case 14:
5731 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5732 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005733 }
5734
Daniel Vetterb4c09f32013-04-30 14:01:42 +02005735 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005736 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08005737 else
5738 dpll |= PLL_REF_INPUT_DREFCLK;
5739
Daniel Vetter959e16d2013-06-05 13:34:21 +02005740 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005741}
5742
Jesse Barnes79e53942008-11-07 14:24:08 -08005743static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
Jesse Barnes79e53942008-11-07 14:24:08 -08005744 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005745 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005746{
5747 struct drm_device *dev = crtc->dev;
5748 struct drm_i915_private *dev_priv = dev->dev_private;
5749 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5750 int pipe = intel_crtc->pipe;
5751 int plane = intel_crtc->plane;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005752 int num_connectors = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005753 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02005754 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03005755 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01005756 bool is_lvds = false;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005757 struct intel_encoder *encoder;
Daniel Vettere2b78262013-06-07 23:10:03 +02005758 struct intel_shared_dpll *pll;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005759 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08005760
5761 for_each_encoder_on_crtc(dev, crtc, encoder) {
5762 switch (encoder->type) {
5763 case INTEL_OUTPUT_LVDS:
5764 is_lvds = true;
5765 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005766 }
5767
5768 num_connectors++;
5769 }
5770
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005771 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
5772 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
5773
Daniel Vetterff9a6752013-06-01 17:16:21 +02005774 ok = ironlake_compute_clocks(crtc, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005775 &has_reduced_clock, &reduced_clock);
Daniel Vetteree9300b2013-06-03 22:40:22 +02005776 if (!ok && !intel_crtc->config.clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08005777 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5778 return -EINVAL;
5779 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01005780 /* Compat-code for transition, will disappear. */
5781 if (!intel_crtc->config.clock_set) {
5782 intel_crtc->config.dpll.n = clock.n;
5783 intel_crtc->config.dpll.m1 = clock.m1;
5784 intel_crtc->config.dpll.m2 = clock.m2;
5785 intel_crtc->config.dpll.p1 = clock.p1;
5786 intel_crtc->config.dpll.p2 = clock.p2;
5787 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005788
5789 /* Ensure that the cursor is valid for the new mode before changing... */
5790 intel_crtc_update_cursor(crtc, true);
5791
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005792 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
Daniel Vetter8b470472013-03-28 10:41:59 +01005793 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005794 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02005795 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005796 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02005797
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005798 dpll = ironlake_compute_dpll(intel_crtc,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02005799 &fp, &reduced_clock,
5800 has_reduced_clock ? &fp2 : NULL);
5801
Daniel Vetter959e16d2013-06-05 13:34:21 +02005802 intel_crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02005803 intel_crtc->config.dpll_hw_state.fp0 = fp;
5804 if (has_reduced_clock)
5805 intel_crtc->config.dpll_hw_state.fp1 = fp2;
5806 else
5807 intel_crtc->config.dpll_hw_state.fp1 = fp;
5808
Daniel Vetterb89a1d32013-06-05 13:34:24 +02005809 pll = intel_get_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005810 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03005811 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
5812 pipe_name(pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07005813 return -EINVAL;
5814 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005815 } else
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005816 intel_put_shared_dpll(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08005817
Daniel Vetter03afc4a2013-04-02 23:42:31 +02005818 if (intel_crtc->config.has_dp_encoder)
5819 intel_dp_set_m_n(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08005820
Daniel Vetterbcd644e2013-06-05 13:34:22 +02005821 if (is_lvds && has_reduced_clock && i915_powersave)
5822 intel_crtc->lowfreq_avail = true;
5823 else
5824 intel_crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02005825
5826 if (intel_crtc->config.has_pch_encoder) {
5827 pll = intel_crtc_to_shared_dpll(intel_crtc);
5828
Jesse Barnes79e53942008-11-07 14:24:08 -08005829 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005830
Daniel Vetter8a654f32013-06-01 17:16:22 +02005831 intel_set_pipe_timings(intel_crtc);
Krzysztof Halasa734b4152010-05-25 18:41:46 +02005832
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01005833 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01005834 intel_cpu_transcoder_set_m_n(intel_crtc,
5835 &intel_crtc->config.fdi_m_n);
5836 }
Chris Wilson5eddb702010-09-11 13:48:45 +01005837
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005838 if (IS_IVYBRIDGE(dev))
5839 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
Zhenyu Wang2c072452009-06-05 15:38:42 +08005840
Daniel Vetter6ff93602013-04-19 11:24:36 +02005841 ironlake_set_pipeconf(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08005842
Paulo Zanonia1f9e772012-09-12 10:06:32 -03005843 /* Set up the display plane register */
5844 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08005845 POSTING_READ(DSPCNTR(plane));
Jesse Barnes79e53942008-11-07 14:24:08 -08005846
Daniel Vetter94352cf2012-07-05 22:51:56 +02005847 ret = intel_pipe_set_base(crtc, x, y, fb);
Shaohua Li7662c8b2009-06-26 11:23:55 +08005848
5849 intel_update_watermarks(dev);
5850
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005851 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08005852}
5853
Daniel Vetter72419202013-04-04 13:28:53 +02005854static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5855 struct intel_crtc_config *pipe_config)
5856{
5857 struct drm_device *dev = crtc->base.dev;
5858 struct drm_i915_private *dev_priv = dev->dev_private;
5859 enum transcoder transcoder = pipe_config->cpu_transcoder;
5860
5861 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));
5863 pipe_config->fdi_m_n.gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
5864 & ~TU_SIZE_MASK;
5865 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))
5867 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
5868}
5869
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005870static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5871 struct intel_crtc_config *pipe_config)
5872{
5873 struct drm_device *dev = crtc->base.dev;
5874 struct drm_i915_private *dev_priv = dev->dev_private;
5875 uint32_t tmp;
5876
5877 tmp = I915_READ(PF_CTL(crtc->pipe));
5878
5879 if (tmp & PF_ENABLE) {
5880 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
5881 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02005882
5883 /* We currently do not free assignements of panel fitters on
5884 * ivb/hsw (since we don't use the higher upscaling modes which
5885 * differentiates them) so just WARN about this case for now. */
5886 if (IS_GEN7(dev)) {
5887 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
5888 PF_PIPE_SEL_IVB(crtc->pipe));
5889 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005890 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005891}
5892
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005893static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5894 struct intel_crtc_config *pipe_config)
5895{
5896 struct drm_device *dev = crtc->base.dev;
5897 struct drm_i915_private *dev_priv = dev->dev_private;
5898 uint32_t tmp;
5899
Daniel Vettere143a212013-07-04 12:01:15 +02005900 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02005901 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02005902
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005903 tmp = I915_READ(PIPECONF(crtc->pipe));
5904 if (!(tmp & PIPECONF_ENABLE))
5905 return false;
5906
Daniel Vetterab9412b2013-05-03 11:49:46 +02005907 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02005908 struct intel_shared_dpll *pll;
5909
Daniel Vetter88adfff2013-03-28 10:42:01 +01005910 pipe_config->has_pch_encoder = true;
5911
Daniel Vetter627eb5a2013-04-29 19:33:42 +02005912 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
5913 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
5914 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02005915
5916 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02005917
Daniel Vetterc0d43d62013-06-07 23:11:08 +02005918 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02005919 pipe_config->shared_dpll =
5920 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02005921 } else {
5922 tmp = I915_READ(PCH_DPLL_SEL);
5923 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
5924 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
5925 else
5926 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
5927 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02005928
5929 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
5930
5931 WARN_ON(!pll->get_hw_state(dev_priv, pll,
5932 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02005933
5934 tmp = pipe_config->dpll_hw_state.dpll;
5935 pipe_config->pixel_multiplier =
5936 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
5937 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter6c49f242013-06-06 12:45:25 +02005938 } else {
5939 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02005940 }
5941
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005942 intel_get_pipe_timings(crtc, pipe_config);
5943
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005944 ironlake_get_pfit_config(crtc, pipe_config);
5945
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005946 return true;
5947}
5948
Paulo Zanonibe256dc2013-07-23 11:19:26 -03005949static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
5950{
5951 struct drm_device *dev = dev_priv->dev;
5952 struct intel_ddi_plls *plls = &dev_priv->ddi_plls;
5953 struct intel_crtc *crtc;
5954 unsigned long irqflags;
5955 uint32_t val, pch_hpd_mask;
5956
5957 pch_hpd_mask = SDE_PORTB_HOTPLUG_CPT | SDE_PORTC_HOTPLUG_CPT;
5958 if (!(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE))
5959 pch_hpd_mask |= SDE_PORTD_HOTPLUG_CPT | SDE_CRT_HOTPLUG_CPT;
5960
5961 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
5962 WARN(crtc->base.enabled, "CRTC for pipe %c enabled\n",
5963 pipe_name(crtc->pipe));
5964
5965 WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
5966 WARN(plls->spll_refcount, "SPLL enabled\n");
5967 WARN(plls->wrpll1_refcount, "WRPLL1 enabled\n");
5968 WARN(plls->wrpll2_refcount, "WRPLL2 enabled\n");
5969 WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
5970 WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
5971 "CPU PWM1 enabled\n");
5972 WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
5973 "CPU PWM2 enabled\n");
5974 WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
5975 "PCH PWM1 enabled\n");
5976 WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
5977 "Utility pin enabled\n");
5978 WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
5979
5980 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
5981 val = I915_READ(DEIMR);
5982 WARN((val & ~DE_PCH_EVENT_IVB) != val,
5983 "Unexpected DEIMR bits enabled: 0x%x\n", val);
5984 val = I915_READ(SDEIMR);
5985 WARN((val & ~pch_hpd_mask) != val,
5986 "Unexpected SDEIMR bits enabled: 0x%x\n", val);
5987 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
5988}
5989
5990/*
5991 * This function implements pieces of two sequences from BSpec:
5992 * - Sequence for display software to disable LCPLL
5993 * - Sequence for display software to allow package C8+
5994 * The steps implemented here are just the steps that actually touch the LCPLL
5995 * register. Callers should take care of disabling all the display engine
5996 * functions, doing the mode unset, fixing interrupts, etc.
5997 */
5998void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
5999 bool switch_to_fclk, bool allow_power_down)
6000{
6001 uint32_t val;
6002
6003 assert_can_disable_lcpll(dev_priv);
6004
6005 val = I915_READ(LCPLL_CTL);
6006
6007 if (switch_to_fclk) {
6008 val |= LCPLL_CD_SOURCE_FCLK;
6009 I915_WRITE(LCPLL_CTL, val);
6010
6011 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
6012 LCPLL_CD_SOURCE_FCLK_DONE, 1))
6013 DRM_ERROR("Switching to FCLK failed\n");
6014
6015 val = I915_READ(LCPLL_CTL);
6016 }
6017
6018 val |= LCPLL_PLL_DISABLE;
6019 I915_WRITE(LCPLL_CTL, val);
6020 POSTING_READ(LCPLL_CTL);
6021
6022 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
6023 DRM_ERROR("LCPLL still locked\n");
6024
6025 val = I915_READ(D_COMP);
6026 val |= D_COMP_COMP_DISABLE;
6027 I915_WRITE(D_COMP, val);
6028 POSTING_READ(D_COMP);
6029 ndelay(100);
6030
6031 if (wait_for((I915_READ(D_COMP) & D_COMP_RCOMP_IN_PROGRESS) == 0, 1))
6032 DRM_ERROR("D_COMP RCOMP still in progress\n");
6033
6034 if (allow_power_down) {
6035 val = I915_READ(LCPLL_CTL);
6036 val |= LCPLL_POWER_DOWN_ALLOW;
6037 I915_WRITE(LCPLL_CTL, val);
6038 POSTING_READ(LCPLL_CTL);
6039 }
6040}
6041
6042/*
6043 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
6044 * source.
6045 */
6046void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
6047{
6048 uint32_t val;
6049
6050 val = I915_READ(LCPLL_CTL);
6051
6052 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
6053 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
6054 return;
6055
6056 if (val & LCPLL_POWER_DOWN_ALLOW) {
6057 val &= ~LCPLL_POWER_DOWN_ALLOW;
6058 I915_WRITE(LCPLL_CTL, val);
6059 }
6060
6061 val = I915_READ(D_COMP);
6062 val |= D_COMP_COMP_FORCE;
6063 val &= ~D_COMP_COMP_DISABLE;
6064 I915_WRITE(D_COMP, val);
6065 I915_READ(D_COMP);
6066
6067 val = I915_READ(LCPLL_CTL);
6068 val &= ~LCPLL_PLL_DISABLE;
6069 I915_WRITE(LCPLL_CTL, val);
6070
6071 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
6072 DRM_ERROR("LCPLL not locked yet\n");
6073
6074 if (val & LCPLL_CD_SOURCE_FCLK) {
6075 val = I915_READ(LCPLL_CTL);
6076 val &= ~LCPLL_CD_SOURCE_FCLK;
6077 I915_WRITE(LCPLL_CTL, val);
6078
6079 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
6080 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
6081 DRM_ERROR("Switching back to LCPLL failed\n");
6082 }
6083}
6084
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02006085static void haswell_modeset_global_resources(struct drm_device *dev)
6086{
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02006087 bool enable = false;
6088 struct intel_crtc *crtc;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02006089
6090 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
Daniel Vettere7a639c2013-05-31 17:49:17 +02006091 if (!crtc->base.enabled)
6092 continue;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02006093
Daniel Vettere7a639c2013-05-31 17:49:17 +02006094 if (crtc->pipe != PIPE_A || crtc->config.pch_pfit.size ||
6095 crtc->config.cpu_transcoder != TRANSCODER_EDP)
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02006096 enable = true;
6097 }
6098
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02006099 intel_set_power_well(dev, enable);
6100}
6101
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03006102static int haswell_crtc_mode_set(struct drm_crtc *crtc,
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03006103 int x, int y,
6104 struct drm_framebuffer *fb)
6105{
6106 struct drm_device *dev = crtc->dev;
6107 struct drm_i915_private *dev_priv = dev->dev_private;
6108 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03006109 int plane = intel_crtc->plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03006110 int ret;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03006111
Daniel Vetterff9a6752013-06-01 17:16:21 +02006112 if (!intel_ddi_pll_mode_set(crtc))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03006113 return -EINVAL;
6114
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03006115 /* Ensure that the cursor is valid for the new mode before changing... */
6116 intel_crtc_update_cursor(crtc, true);
6117
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006118 if (intel_crtc->config.has_dp_encoder)
6119 intel_dp_set_m_n(intel_crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03006120
6121 intel_crtc->lowfreq_avail = false;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03006122
Daniel Vetter8a654f32013-06-01 17:16:22 +02006123 intel_set_pipe_timings(intel_crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03006124
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01006125 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01006126 intel_cpu_transcoder_set_m_n(intel_crtc,
6127 &intel_crtc->config.fdi_m_n);
6128 }
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03006129
Daniel Vetter6ff93602013-04-19 11:24:36 +02006130 haswell_set_pipeconf(crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03006131
Daniel Vetter50f3b012013-03-27 00:44:56 +01006132 intel_set_pipe_csc(crtc);
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006133
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03006134 /* Set up the display plane register */
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006135 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03006136 POSTING_READ(DSPCNTR(plane));
6137
6138 ret = intel_pipe_set_base(crtc, x, y, fb);
6139
6140 intel_update_watermarks(dev);
6141
Jesse Barnes79e53942008-11-07 14:24:08 -08006142 return ret;
6143}
6144
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006145static bool haswell_get_pipe_config(struct intel_crtc *crtc,
6146 struct intel_crtc_config *pipe_config)
6147{
6148 struct drm_device *dev = crtc->base.dev;
6149 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006150 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006151 uint32_t tmp;
6152
Daniel Vettere143a212013-07-04 12:01:15 +02006153 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006154 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
6155
Daniel Vettereccb1402013-05-22 00:50:22 +02006156 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
6157 if (tmp & TRANS_DDI_FUNC_ENABLE) {
6158 enum pipe trans_edp_pipe;
6159 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
6160 default:
6161 WARN(1, "unknown pipe linked to edp transcoder\n");
6162 case TRANS_DDI_EDP_INPUT_A_ONOFF:
6163 case TRANS_DDI_EDP_INPUT_A_ON:
6164 trans_edp_pipe = PIPE_A;
6165 break;
6166 case TRANS_DDI_EDP_INPUT_B_ONOFF:
6167 trans_edp_pipe = PIPE_B;
6168 break;
6169 case TRANS_DDI_EDP_INPUT_C_ONOFF:
6170 trans_edp_pipe = PIPE_C;
6171 break;
6172 }
6173
6174 if (trans_edp_pipe == crtc->pipe)
6175 pipe_config->cpu_transcoder = TRANSCODER_EDP;
6176 }
6177
Paulo Zanonib97186f2013-05-03 12:15:36 -03006178 if (!intel_display_power_enabled(dev,
Daniel Vettereccb1402013-05-22 00:50:22 +02006179 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03006180 return false;
6181
Daniel Vettereccb1402013-05-22 00:50:22 +02006182 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006183 if (!(tmp & PIPECONF_ENABLE))
6184 return false;
6185
Daniel Vetter88adfff2013-03-28 10:42:01 +01006186 /*
Paulo Zanonif196e6b2013-04-18 16:35:41 -03006187 * Haswell has only FDI/PCH transcoder A. It is which is connected to
Daniel Vetter88adfff2013-03-28 10:42:01 +01006188 * DDI E. So just check whether this pipe is wired to DDI E and whether
6189 * the PCH transcoder is on.
6190 */
Daniel Vettereccb1402013-05-22 00:50:22 +02006191 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
Daniel Vetter88adfff2013-03-28 10:42:01 +01006192 if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
Daniel Vetterab9412b2013-05-03 11:49:46 +02006193 I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter88adfff2013-03-28 10:42:01 +01006194 pipe_config->has_pch_encoder = true;
6195
Daniel Vetter627eb5a2013-04-29 19:33:42 +02006196 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
6197 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6198 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02006199
6200 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02006201 }
6202
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006203 intel_get_pipe_timings(crtc, pipe_config);
6204
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006205 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
6206 if (intel_display_power_enabled(dev, pfit_domain))
6207 ironlake_get_pfit_config(crtc, pipe_config);
Daniel Vetter88adfff2013-03-28 10:42:01 +01006208
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006209 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
6210 (I915_READ(IPS_CTL) & IPS_ENABLE);
6211
Daniel Vetter6c49f242013-06-06 12:45:25 +02006212 pipe_config->pixel_multiplier = 1;
6213
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006214 return true;
6215}
6216
Eric Anholtf564048e2011-03-30 13:01:02 -07006217static int intel_crtc_mode_set(struct drm_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07006218 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02006219 struct drm_framebuffer *fb)
Eric Anholtf564048e2011-03-30 13:01:02 -07006220{
6221 struct drm_device *dev = crtc->dev;
6222 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter9256aa12012-10-31 19:26:13 +01006223 struct drm_encoder_helper_funcs *encoder_funcs;
6224 struct intel_encoder *encoder;
Eric Anholt0b701d22011-03-30 13:01:03 -07006225 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01006226 struct drm_display_mode *adjusted_mode =
6227 &intel_crtc->config.adjusted_mode;
6228 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
Eric Anholt0b701d22011-03-30 13:01:03 -07006229 int pipe = intel_crtc->pipe;
Eric Anholtf564048e2011-03-30 13:01:02 -07006230 int ret;
6231
Eric Anholt0b701d22011-03-30 13:01:03 -07006232 drm_vblank_pre_modeset(dev, pipe);
6233
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01006234 ret = dev_priv->display.crtc_mode_set(crtc, x, y, fb);
6235
Jesse Barnes79e53942008-11-07 14:24:08 -08006236 drm_vblank_post_modeset(dev, pipe);
6237
Daniel Vetter9256aa12012-10-31 19:26:13 +01006238 if (ret != 0)
6239 return ret;
6240
6241 for_each_encoder_on_crtc(dev, crtc, encoder) {
6242 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
6243 encoder->base.base.id,
6244 drm_get_encoder_name(&encoder->base),
6245 mode->base.id, mode->name);
Daniel Vetter6cc5f342013-03-27 00:44:53 +01006246 if (encoder->mode_set) {
6247 encoder->mode_set(encoder);
6248 } else {
6249 encoder_funcs = encoder->base.helper_private;
6250 encoder_funcs->mode_set(&encoder->base, mode, adjusted_mode);
6251 }
Daniel Vetter9256aa12012-10-31 19:26:13 +01006252 }
6253
6254 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006255}
6256
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006257static bool intel_eld_uptodate(struct drm_connector *connector,
6258 int reg_eldv, uint32_t bits_eldv,
6259 int reg_elda, uint32_t bits_elda,
6260 int reg_edid)
6261{
6262 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6263 uint8_t *eld = connector->eld;
6264 uint32_t i;
6265
6266 i = I915_READ(reg_eldv);
6267 i &= bits_eldv;
6268
6269 if (!eld[0])
6270 return !i;
6271
6272 if (!i)
6273 return false;
6274
6275 i = I915_READ(reg_elda);
6276 i &= ~bits_elda;
6277 I915_WRITE(reg_elda, i);
6278
6279 for (i = 0; i < eld[2]; i++)
6280 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
6281 return false;
6282
6283 return true;
6284}
6285
Wu Fengguange0dac652011-09-05 14:25:34 +08006286static void g4x_write_eld(struct drm_connector *connector,
6287 struct drm_crtc *crtc)
6288{
6289 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6290 uint8_t *eld = connector->eld;
6291 uint32_t eldv;
6292 uint32_t len;
6293 uint32_t i;
6294
6295 i = I915_READ(G4X_AUD_VID_DID);
6296
6297 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
6298 eldv = G4X_ELDV_DEVCL_DEVBLC;
6299 else
6300 eldv = G4X_ELDV_DEVCTG;
6301
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006302 if (intel_eld_uptodate(connector,
6303 G4X_AUD_CNTL_ST, eldv,
6304 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
6305 G4X_HDMIW_HDMIEDID))
6306 return;
6307
Wu Fengguange0dac652011-09-05 14:25:34 +08006308 i = I915_READ(G4X_AUD_CNTL_ST);
6309 i &= ~(eldv | G4X_ELD_ADDR);
6310 len = (i >> 9) & 0x1f; /* ELD buffer size */
6311 I915_WRITE(G4X_AUD_CNTL_ST, i);
6312
6313 if (!eld[0])
6314 return;
6315
6316 len = min_t(uint8_t, eld[2], len);
6317 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6318 for (i = 0; i < len; i++)
6319 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
6320
6321 i = I915_READ(G4X_AUD_CNTL_ST);
6322 i |= eldv;
6323 I915_WRITE(G4X_AUD_CNTL_ST, i);
6324}
6325
Wang Xingchao83358c852012-08-16 22:43:37 +08006326static void haswell_write_eld(struct drm_connector *connector,
6327 struct drm_crtc *crtc)
6328{
6329 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6330 uint8_t *eld = connector->eld;
6331 struct drm_device *dev = crtc->dev;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08006332 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Wang Xingchao83358c852012-08-16 22:43:37 +08006333 uint32_t eldv;
6334 uint32_t i;
6335 int len;
6336 int pipe = to_intel_crtc(crtc)->pipe;
6337 int tmp;
6338
6339 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
6340 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
6341 int aud_config = HSW_AUD_CFG(pipe);
6342 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
6343
6344
6345 DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
6346
6347 /* Audio output enable */
6348 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
6349 tmp = I915_READ(aud_cntrl_st2);
6350 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
6351 I915_WRITE(aud_cntrl_st2, tmp);
6352
6353 /* Wait for 1 vertical blank */
6354 intel_wait_for_vblank(dev, pipe);
6355
6356 /* Set ELD valid state */
6357 tmp = I915_READ(aud_cntrl_st2);
6358 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%8x\n", tmp);
6359 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
6360 I915_WRITE(aud_cntrl_st2, tmp);
6361 tmp = I915_READ(aud_cntrl_st2);
6362 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%8x\n", tmp);
6363
6364 /* Enable HDMI mode */
6365 tmp = I915_READ(aud_config);
6366 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%8x\n", tmp);
6367 /* clear N_programing_enable and N_value_index */
6368 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
6369 I915_WRITE(aud_config, tmp);
6370
6371 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
6372
6373 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08006374 intel_crtc->eld_vld = true;
Wang Xingchao83358c852012-08-16 22:43:37 +08006375
6376 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6377 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6378 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
6379 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6380 } else
6381 I915_WRITE(aud_config, 0);
6382
6383 if (intel_eld_uptodate(connector,
6384 aud_cntrl_st2, eldv,
6385 aud_cntl_st, IBX_ELD_ADDRESS,
6386 hdmiw_hdmiedid))
6387 return;
6388
6389 i = I915_READ(aud_cntrl_st2);
6390 i &= ~eldv;
6391 I915_WRITE(aud_cntrl_st2, i);
6392
6393 if (!eld[0])
6394 return;
6395
6396 i = I915_READ(aud_cntl_st);
6397 i &= ~IBX_ELD_ADDRESS;
6398 I915_WRITE(aud_cntl_st, i);
6399 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
6400 DRM_DEBUG_DRIVER("port num:%d\n", i);
6401
6402 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6403 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6404 for (i = 0; i < len; i++)
6405 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6406
6407 i = I915_READ(aud_cntrl_st2);
6408 i |= eldv;
6409 I915_WRITE(aud_cntrl_st2, i);
6410
6411}
6412
Wu Fengguange0dac652011-09-05 14:25:34 +08006413static void ironlake_write_eld(struct drm_connector *connector,
6414 struct drm_crtc *crtc)
6415{
6416 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6417 uint8_t *eld = connector->eld;
6418 uint32_t eldv;
6419 uint32_t i;
6420 int len;
6421 int hdmiw_hdmiedid;
Wu Fengguangb6daa022012-01-06 14:41:31 -06006422 int aud_config;
Wu Fengguange0dac652011-09-05 14:25:34 +08006423 int aud_cntl_st;
6424 int aud_cntrl_st2;
Wang Xingchao9b138a82012-08-09 16:52:18 +08006425 int pipe = to_intel_crtc(crtc)->pipe;
Wu Fengguange0dac652011-09-05 14:25:34 +08006426
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08006427 if (HAS_PCH_IBX(connector->dev)) {
Wang Xingchao9b138a82012-08-09 16:52:18 +08006428 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
6429 aud_config = IBX_AUD_CFG(pipe);
6430 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006431 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08006432 } else {
Wang Xingchao9b138a82012-08-09 16:52:18 +08006433 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
6434 aud_config = CPT_AUD_CFG(pipe);
6435 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006436 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08006437 }
6438
Wang Xingchao9b138a82012-08-09 16:52:18 +08006439 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
Wu Fengguange0dac652011-09-05 14:25:34 +08006440
6441 i = I915_READ(aud_cntl_st);
Wang Xingchao9b138a82012-08-09 16:52:18 +08006442 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
Wu Fengguange0dac652011-09-05 14:25:34 +08006443 if (!i) {
6444 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
6445 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006446 eldv = IBX_ELD_VALIDB;
6447 eldv |= IBX_ELD_VALIDB << 4;
6448 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08006449 } else {
Ville Syrjälä2582a852013-04-17 17:48:47 +03006450 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006451 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08006452 }
6453
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006454 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6455 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6456 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
Wu Fengguangb6daa022012-01-06 14:41:31 -06006457 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6458 } else
6459 I915_WRITE(aud_config, 0);
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006460
6461 if (intel_eld_uptodate(connector,
6462 aud_cntrl_st2, eldv,
6463 aud_cntl_st, IBX_ELD_ADDRESS,
6464 hdmiw_hdmiedid))
6465 return;
6466
Wu Fengguange0dac652011-09-05 14:25:34 +08006467 i = I915_READ(aud_cntrl_st2);
6468 i &= ~eldv;
6469 I915_WRITE(aud_cntrl_st2, i);
6470
6471 if (!eld[0])
6472 return;
6473
Wu Fengguange0dac652011-09-05 14:25:34 +08006474 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006475 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08006476 I915_WRITE(aud_cntl_st, i);
6477
6478 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6479 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6480 for (i = 0; i < len; i++)
6481 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6482
6483 i = I915_READ(aud_cntrl_st2);
6484 i |= eldv;
6485 I915_WRITE(aud_cntrl_st2, i);
6486}
6487
6488void intel_write_eld(struct drm_encoder *encoder,
6489 struct drm_display_mode *mode)
6490{
6491 struct drm_crtc *crtc = encoder->crtc;
6492 struct drm_connector *connector;
6493 struct drm_device *dev = encoder->dev;
6494 struct drm_i915_private *dev_priv = dev->dev_private;
6495
6496 connector = drm_select_eld(encoder, mode);
6497 if (!connector)
6498 return;
6499
6500 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6501 connector->base.id,
6502 drm_get_connector_name(connector),
6503 connector->encoder->base.id,
6504 drm_get_encoder_name(connector->encoder));
6505
6506 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
6507
6508 if (dev_priv->display.write_eld)
6509 dev_priv->display.write_eld(connector, crtc);
6510}
6511
Jesse Barnes79e53942008-11-07 14:24:08 -08006512/** Loads the palette/gamma unit for the CRTC with the prepared values */
6513void intel_crtc_load_lut(struct drm_crtc *crtc)
6514{
6515 struct drm_device *dev = crtc->dev;
6516 struct drm_i915_private *dev_priv = dev->dev_private;
6517 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006518 enum pipe pipe = intel_crtc->pipe;
6519 int palreg = PALETTE(pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08006520 int i;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006521 bool reenable_ips = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006522
6523 /* The clocks have to be on to load the palette. */
Alban Browaeysaed3f092012-02-24 17:12:45 +00006524 if (!crtc->enabled || !intel_crtc->active)
Jesse Barnes79e53942008-11-07 14:24:08 -08006525 return;
6526
Ville Syrjälä14420bd2013-06-04 13:49:07 +03006527 if (!HAS_PCH_SPLIT(dev_priv->dev))
6528 assert_pll_enabled(dev_priv, pipe);
6529
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006530 /* use legacy palette for Ironlake */
Eric Anholtbad720f2009-10-22 16:11:14 -07006531 if (HAS_PCH_SPLIT(dev))
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006532 palreg = LGC_PALETTE(pipe);
6533
6534 /* Workaround : Do not read or write the pipe palette/gamma data while
6535 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
6536 */
6537 if (intel_crtc->config.ips_enabled &&
6538 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
6539 GAMMA_MODE_MODE_SPLIT)) {
6540 hsw_disable_ips(intel_crtc);
6541 reenable_ips = true;
6542 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08006543
Jesse Barnes79e53942008-11-07 14:24:08 -08006544 for (i = 0; i < 256; i++) {
6545 I915_WRITE(palreg + 4 * i,
6546 (intel_crtc->lut_r[i] << 16) |
6547 (intel_crtc->lut_g[i] << 8) |
6548 intel_crtc->lut_b[i]);
6549 }
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006550
6551 if (reenable_ips)
6552 hsw_enable_ips(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006553}
6554
Chris Wilson560b85b2010-08-07 11:01:38 +01006555static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
6556{
6557 struct drm_device *dev = crtc->dev;
6558 struct drm_i915_private *dev_priv = dev->dev_private;
6559 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6560 bool visible = base != 0;
6561 u32 cntl;
6562
6563 if (intel_crtc->cursor_visible == visible)
6564 return;
6565
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006566 cntl = I915_READ(_CURACNTR);
Chris Wilson560b85b2010-08-07 11:01:38 +01006567 if (visible) {
6568 /* On these chipsets we can only modify the base whilst
6569 * the cursor is disabled.
6570 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006571 I915_WRITE(_CURABASE, base);
Chris Wilson560b85b2010-08-07 11:01:38 +01006572
6573 cntl &= ~(CURSOR_FORMAT_MASK);
6574 /* XXX width must be 64, stride 256 => 0x00 << 28 */
6575 cntl |= CURSOR_ENABLE |
6576 CURSOR_GAMMA_ENABLE |
6577 CURSOR_FORMAT_ARGB;
6578 } else
6579 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006580 I915_WRITE(_CURACNTR, cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01006581
6582 intel_crtc->cursor_visible = visible;
6583}
6584
6585static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
6586{
6587 struct drm_device *dev = crtc->dev;
6588 struct drm_i915_private *dev_priv = dev->dev_private;
6589 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6590 int pipe = intel_crtc->pipe;
6591 bool visible = base != 0;
6592
6593 if (intel_crtc->cursor_visible != visible) {
Jesse Barnes548f2452011-02-17 10:40:53 -08006594 uint32_t cntl = I915_READ(CURCNTR(pipe));
Chris Wilson560b85b2010-08-07 11:01:38 +01006595 if (base) {
6596 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
6597 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6598 cntl |= pipe << 28; /* Connect to correct pipe */
6599 } else {
6600 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6601 cntl |= CURSOR_MODE_DISABLE;
6602 }
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006603 I915_WRITE(CURCNTR(pipe), cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01006604
6605 intel_crtc->cursor_visible = visible;
6606 }
6607 /* and commit changes on next vblank */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006608 I915_WRITE(CURBASE(pipe), base);
Chris Wilson560b85b2010-08-07 11:01:38 +01006609}
6610
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006611static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
6612{
6613 struct drm_device *dev = crtc->dev;
6614 struct drm_i915_private *dev_priv = dev->dev_private;
6615 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6616 int pipe = intel_crtc->pipe;
6617 bool visible = base != 0;
6618
6619 if (intel_crtc->cursor_visible != visible) {
6620 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
6621 if (base) {
6622 cntl &= ~CURSOR_MODE;
6623 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6624 } else {
6625 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6626 cntl |= CURSOR_MODE_DISABLE;
6627 }
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006628 if (IS_HASWELL(dev))
6629 cntl |= CURSOR_PIPE_CSC_ENABLE;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006630 I915_WRITE(CURCNTR_IVB(pipe), cntl);
6631
6632 intel_crtc->cursor_visible = visible;
6633 }
6634 /* and commit changes on next vblank */
6635 I915_WRITE(CURBASE_IVB(pipe), base);
6636}
6637
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006638/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01006639static void intel_crtc_update_cursor(struct drm_crtc *crtc,
6640 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006641{
6642 struct drm_device *dev = crtc->dev;
6643 struct drm_i915_private *dev_priv = dev->dev_private;
6644 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6645 int pipe = intel_crtc->pipe;
6646 int x = intel_crtc->cursor_x;
6647 int y = intel_crtc->cursor_y;
Chris Wilson560b85b2010-08-07 11:01:38 +01006648 u32 base, pos;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006649 bool visible;
6650
6651 pos = 0;
6652
Chris Wilson6b383a72010-09-13 13:54:26 +01006653 if (on && crtc->enabled && crtc->fb) {
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006654 base = intel_crtc->cursor_addr;
6655 if (x > (int) crtc->fb->width)
6656 base = 0;
6657
6658 if (y > (int) crtc->fb->height)
6659 base = 0;
6660 } else
6661 base = 0;
6662
6663 if (x < 0) {
6664 if (x + intel_crtc->cursor_width < 0)
6665 base = 0;
6666
6667 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
6668 x = -x;
6669 }
6670 pos |= x << CURSOR_X_SHIFT;
6671
6672 if (y < 0) {
6673 if (y + intel_crtc->cursor_height < 0)
6674 base = 0;
6675
6676 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
6677 y = -y;
6678 }
6679 pos |= y << CURSOR_Y_SHIFT;
6680
6681 visible = base != 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01006682 if (!visible && !intel_crtc->cursor_visible)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006683 return;
6684
Eugeni Dodonov0cd83aa2012-04-13 17:08:48 -03006685 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006686 I915_WRITE(CURPOS_IVB(pipe), pos);
6687 ivb_update_cursor(crtc, base);
6688 } else {
6689 I915_WRITE(CURPOS(pipe), pos);
6690 if (IS_845G(dev) || IS_I865G(dev))
6691 i845_update_cursor(crtc, base);
6692 else
6693 i9xx_update_cursor(crtc, base);
6694 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006695}
6696
Jesse Barnes79e53942008-11-07 14:24:08 -08006697static int intel_crtc_cursor_set(struct drm_crtc *crtc,
Chris Wilson05394f32010-11-08 19:18:58 +00006698 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08006699 uint32_t handle,
6700 uint32_t width, uint32_t height)
6701{
6702 struct drm_device *dev = crtc->dev;
6703 struct drm_i915_private *dev_priv = dev->dev_private;
6704 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00006705 struct drm_i915_gem_object *obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006706 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006707 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006708
Jesse Barnes79e53942008-11-07 14:24:08 -08006709 /* if we want to turn off the cursor ignore width and height */
6710 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08006711 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006712 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00006713 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10006714 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006715 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08006716 }
6717
6718 /* Currently we only support 64x64 cursors */
6719 if (width != 64 || height != 64) {
6720 DRM_ERROR("we currently only support 64x64 cursors\n");
6721 return -EINVAL;
6722 }
6723
Chris Wilson05394f32010-11-08 19:18:58 +00006724 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00006725 if (&obj->base == NULL)
Jesse Barnes79e53942008-11-07 14:24:08 -08006726 return -ENOENT;
6727
Chris Wilson05394f32010-11-08 19:18:58 +00006728 if (obj->base.size < width * height * 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006729 DRM_ERROR("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10006730 ret = -ENOMEM;
6731 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08006732 }
6733
Dave Airlie71acb5e2008-12-30 20:31:46 +10006734 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006735 mutex_lock(&dev->struct_mutex);
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05006736 if (!dev_priv->info->cursor_needs_physical) {
Chris Wilson693db182013-03-05 14:52:39 +00006737 unsigned alignment;
6738
Chris Wilsond9e86c02010-11-10 16:40:20 +00006739 if (obj->tiling_mode) {
6740 DRM_ERROR("cursor cannot be tiled\n");
6741 ret = -EINVAL;
6742 goto fail_locked;
6743 }
6744
Chris Wilson693db182013-03-05 14:52:39 +00006745 /* Note that the w/a also requires 2 PTE of padding following
6746 * the bo. We currently fill all unused PTE with the shadow
6747 * page and so we should always have valid PTE following the
6748 * cursor preventing the VT-d warning.
6749 */
6750 alignment = 0;
6751 if (need_vtd_wa(dev))
6752 alignment = 64*1024;
6753
6754 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01006755 if (ret) {
6756 DRM_ERROR("failed to move cursor bo into the GTT\n");
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006757 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006758 }
6759
Chris Wilsond9e86c02010-11-10 16:40:20 +00006760 ret = i915_gem_object_put_fence(obj);
6761 if (ret) {
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006762 DRM_ERROR("failed to release fence for cursor");
Chris Wilsond9e86c02010-11-10 16:40:20 +00006763 goto fail_unpin;
6764 }
6765
Ben Widawskyf343c5f2013-07-05 14:41:04 -07006766 addr = i915_gem_obj_ggtt_offset(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10006767 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01006768 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson05394f32010-11-08 19:18:58 +00006769 ret = i915_gem_attach_phys_object(dev, obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01006770 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
6771 align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10006772 if (ret) {
6773 DRM_ERROR("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006774 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10006775 }
Chris Wilson05394f32010-11-08 19:18:58 +00006776 addr = obj->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006777 }
6778
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006779 if (IS_GEN2(dev))
Jesse Barnes14b60392009-05-20 16:47:08 -04006780 I915_WRITE(CURSIZE, (height << 12) | width);
6781
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006782 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006783 if (intel_crtc->cursor_bo) {
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05006784 if (dev_priv->info->cursor_needs_physical) {
Chris Wilson05394f32010-11-08 19:18:58 +00006785 if (intel_crtc->cursor_bo != obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10006786 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
6787 } else
6788 i915_gem_object_unpin(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00006789 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006790 }
Jesse Barnes80824002009-09-10 15:28:06 -07006791
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006792 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006793
6794 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00006795 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006796 intel_crtc->cursor_width = width;
6797 intel_crtc->cursor_height = height;
6798
Mika Kuoppala40ccc722013-04-23 17:27:08 +03006799 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006800
Jesse Barnes79e53942008-11-07 14:24:08 -08006801 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006802fail_unpin:
Chris Wilson05394f32010-11-08 19:18:58 +00006803 i915_gem_object_unpin(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006804fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10006805 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00006806fail:
Chris Wilson05394f32010-11-08 19:18:58 +00006807 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10006808 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006809}
6810
6811static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
6812{
Jesse Barnes79e53942008-11-07 14:24:08 -08006813 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006814
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006815 intel_crtc->cursor_x = x;
6816 intel_crtc->cursor_y = y;
Jesse Barnes652c3932009-08-17 13:31:43 -07006817
Mika Kuoppala40ccc722013-04-23 17:27:08 +03006818 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08006819
6820 return 0;
6821}
6822
6823/** Sets the color ramps on behalf of RandR */
6824void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
6825 u16 blue, int regno)
6826{
6827 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6828
6829 intel_crtc->lut_r[regno] = red >> 8;
6830 intel_crtc->lut_g[regno] = green >> 8;
6831 intel_crtc->lut_b[regno] = blue >> 8;
6832}
6833
Dave Airlieb8c00ac2009-10-06 13:54:01 +10006834void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
6835 u16 *blue, int regno)
6836{
6837 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6838
6839 *red = intel_crtc->lut_r[regno] << 8;
6840 *green = intel_crtc->lut_g[regno] << 8;
6841 *blue = intel_crtc->lut_b[regno] << 8;
6842}
6843
Jesse Barnes79e53942008-11-07 14:24:08 -08006844static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01006845 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08006846{
James Simmons72034252010-08-03 01:33:19 +01006847 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08006848 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006849
James Simmons72034252010-08-03 01:33:19 +01006850 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006851 intel_crtc->lut_r[i] = red[i] >> 8;
6852 intel_crtc->lut_g[i] = green[i] >> 8;
6853 intel_crtc->lut_b[i] = blue[i] >> 8;
6854 }
6855
6856 intel_crtc_load_lut(crtc);
6857}
6858
Jesse Barnes79e53942008-11-07 14:24:08 -08006859/* VESA 640x480x72Hz mode to set on the pipe */
6860static struct drm_display_mode load_detect_mode = {
6861 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
6862 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
6863};
6864
Chris Wilsond2dff872011-04-19 08:36:26 +01006865static struct drm_framebuffer *
6866intel_framebuffer_create(struct drm_device *dev,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006867 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilsond2dff872011-04-19 08:36:26 +01006868 struct drm_i915_gem_object *obj)
6869{
6870 struct intel_framebuffer *intel_fb;
6871 int ret;
6872
6873 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
6874 if (!intel_fb) {
6875 drm_gem_object_unreference_unlocked(&obj->base);
6876 return ERR_PTR(-ENOMEM);
6877 }
6878
6879 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
6880 if (ret) {
6881 drm_gem_object_unreference_unlocked(&obj->base);
6882 kfree(intel_fb);
6883 return ERR_PTR(ret);
6884 }
6885
6886 return &intel_fb->base;
6887}
6888
6889static u32
6890intel_framebuffer_pitch_for_width(int width, int bpp)
6891{
6892 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
6893 return ALIGN(pitch, 64);
6894}
6895
6896static u32
6897intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
6898{
6899 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
6900 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
6901}
6902
6903static struct drm_framebuffer *
6904intel_framebuffer_create_for_mode(struct drm_device *dev,
6905 struct drm_display_mode *mode,
6906 int depth, int bpp)
6907{
6908 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00006909 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01006910
6911 obj = i915_gem_alloc_object(dev,
6912 intel_framebuffer_size_for_mode(mode, bpp));
6913 if (obj == NULL)
6914 return ERR_PTR(-ENOMEM);
6915
6916 mode_cmd.width = mode->hdisplay;
6917 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006918 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
6919 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00006920 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01006921
6922 return intel_framebuffer_create(dev, &mode_cmd, obj);
6923}
6924
6925static struct drm_framebuffer *
6926mode_fits_in_fbdev(struct drm_device *dev,
6927 struct drm_display_mode *mode)
6928{
6929 struct drm_i915_private *dev_priv = dev->dev_private;
6930 struct drm_i915_gem_object *obj;
6931 struct drm_framebuffer *fb;
6932
6933 if (dev_priv->fbdev == NULL)
6934 return NULL;
6935
6936 obj = dev_priv->fbdev->ifb.obj;
6937 if (obj == NULL)
6938 return NULL;
6939
6940 fb = &dev_priv->fbdev->ifb.base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006941 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
6942 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01006943 return NULL;
6944
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006945 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01006946 return NULL;
6947
6948 return fb;
6949}
6950
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006951bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01006952 struct drm_display_mode *mode,
Chris Wilson8261b192011-04-19 23:18:09 +01006953 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08006954{
6955 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006956 struct intel_encoder *intel_encoder =
6957 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08006958 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01006959 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08006960 struct drm_crtc *crtc = NULL;
6961 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02006962 struct drm_framebuffer *fb;
Jesse Barnes79e53942008-11-07 14:24:08 -08006963 int i = -1;
6964
Chris Wilsond2dff872011-04-19 08:36:26 +01006965 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6966 connector->base.id, drm_get_connector_name(connector),
6967 encoder->base.id, drm_get_encoder_name(encoder));
6968
Jesse Barnes79e53942008-11-07 14:24:08 -08006969 /*
6970 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01006971 *
Jesse Barnes79e53942008-11-07 14:24:08 -08006972 * - if the connector already has an assigned crtc, use it (but make
6973 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01006974 *
Jesse Barnes79e53942008-11-07 14:24:08 -08006975 * - try to find the first unused crtc that can drive this connector,
6976 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08006977 */
6978
6979 /* See if we already have a CRTC for this connector */
6980 if (encoder->crtc) {
6981 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01006982
Daniel Vetter7b240562012-12-12 00:35:33 +01006983 mutex_lock(&crtc->mutex);
6984
Daniel Vetter24218aa2012-08-12 19:27:11 +02006985 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01006986 old->load_detect_temp = false;
6987
6988 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02006989 if (connector->dpms != DRM_MODE_DPMS_ON)
6990 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01006991
Chris Wilson71731882011-04-19 23:10:58 +01006992 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08006993 }
6994
6995 /* Find an unused one (if possible) */
6996 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
6997 i++;
6998 if (!(encoder->possible_crtcs & (1 << i)))
6999 continue;
7000 if (!possible_crtc->enabled) {
7001 crtc = possible_crtc;
7002 break;
7003 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007004 }
7005
7006 /*
7007 * If we didn't find an unused CRTC, don't use any.
7008 */
7009 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01007010 DRM_DEBUG_KMS("no pipe available for load-detect\n");
7011 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007012 }
7013
Daniel Vetter7b240562012-12-12 00:35:33 +01007014 mutex_lock(&crtc->mutex);
Daniel Vetterfc303102012-07-09 10:40:58 +02007015 intel_encoder->new_crtc = to_intel_crtc(crtc);
7016 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08007017
7018 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter24218aa2012-08-12 19:27:11 +02007019 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01007020 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01007021 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08007022
Chris Wilson64927112011-04-20 07:25:26 +01007023 if (!mode)
7024 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08007025
Chris Wilsond2dff872011-04-19 08:36:26 +01007026 /* We need a framebuffer large enough to accommodate all accesses
7027 * that the plane may generate whilst we perform load detection.
7028 * We can not rely on the fbcon either being present (we get called
7029 * during its initialisation to detect all boot displays, or it may
7030 * not even exist) or that it is large enough to satisfy the
7031 * requested mode.
7032 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02007033 fb = mode_fits_in_fbdev(dev, mode);
7034 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01007035 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02007036 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
7037 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01007038 } else
7039 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02007040 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01007041 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Daniel Vetter7b240562012-12-12 00:35:33 +01007042 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00007043 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007044 }
Chris Wilsond2dff872011-04-19 08:36:26 +01007045
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007046 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01007047 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01007048 if (old->release_fb)
7049 old->release_fb->funcs->destroy(old->release_fb);
Daniel Vetter7b240562012-12-12 00:35:33 +01007050 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00007051 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007052 }
Chris Wilson71731882011-04-19 23:10:58 +01007053
Jesse Barnes79e53942008-11-07 14:24:08 -08007054 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007055 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01007056 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08007057}
7058
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007059void intel_release_load_detect_pipe(struct drm_connector *connector,
Chris Wilson8261b192011-04-19 23:18:09 +01007060 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08007061{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02007062 struct intel_encoder *intel_encoder =
7063 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01007064 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01007065 struct drm_crtc *crtc = encoder->crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -08007066
Chris Wilsond2dff872011-04-19 08:36:26 +01007067 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7068 connector->base.id, drm_get_connector_name(connector),
7069 encoder->base.id, drm_get_encoder_name(encoder));
7070
Chris Wilson8261b192011-04-19 23:18:09 +01007071 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02007072 to_intel_connector(connector)->new_encoder = NULL;
7073 intel_encoder->new_crtc = NULL;
7074 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01007075
Daniel Vetter36206362012-12-10 20:42:17 +01007076 if (old->release_fb) {
7077 drm_framebuffer_unregister_private(old->release_fb);
7078 drm_framebuffer_unreference(old->release_fb);
7079 }
Chris Wilsond2dff872011-04-19 08:36:26 +01007080
Daniel Vetter67c96402013-01-23 16:25:09 +00007081 mutex_unlock(&crtc->mutex);
Chris Wilson0622a532011-04-21 09:32:11 +01007082 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08007083 }
7084
Eric Anholtc751ce42010-03-25 11:48:48 -07007085 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02007086 if (old->dpms_mode != DRM_MODE_DPMS_ON)
7087 connector->funcs->dpms(connector, old->dpms_mode);
Daniel Vetter7b240562012-12-12 00:35:33 +01007088
7089 mutex_unlock(&crtc->mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08007090}
7091
7092/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007093static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
7094 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08007095{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007096 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007097 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007098 int pipe = pipe_config->cpu_transcoder;
Jesse Barnes548f2452011-02-17 10:40:53 -08007099 u32 dpll = I915_READ(DPLL(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08007100 u32 fp;
7101 intel_clock_t clock;
7102
7103 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Chris Wilson39adb7a2011-04-22 22:17:21 +01007104 fp = I915_READ(FP0(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08007105 else
Chris Wilson39adb7a2011-04-22 22:17:21 +01007106 fp = I915_READ(FP1(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08007107
7108 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05007109 if (IS_PINEVIEW(dev)) {
7110 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
7111 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08007112 } else {
7113 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
7114 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
7115 }
7116
Chris Wilsona6c45cf2010-09-17 00:32:17 +01007117 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05007118 if (IS_PINEVIEW(dev))
7119 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
7120 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08007121 else
7122 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08007123 DPLL_FPA01_P1_POST_DIV_SHIFT);
7124
7125 switch (dpll & DPLL_MODE_MASK) {
7126 case DPLLB_MODE_DAC_SERIAL:
7127 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
7128 5 : 10;
7129 break;
7130 case DPLLB_MODE_LVDS:
7131 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
7132 7 : 14;
7133 break;
7134 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08007135 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08007136 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007137 pipe_config->adjusted_mode.clock = 0;
7138 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08007139 }
7140
Daniel Vetterac58c3f2013-06-01 17:16:17 +02007141 if (IS_PINEVIEW(dev))
7142 pineview_clock(96000, &clock);
7143 else
7144 i9xx_clock(96000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08007145 } else {
7146 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
7147
7148 if (is_lvds) {
7149 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
7150 DPLL_FPA01_P1_POST_DIV_SHIFT);
7151 clock.p2 = 14;
7152
7153 if ((dpll & PLL_REF_INPUT_MASK) ==
7154 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
7155 /* XXX: might not be 66MHz */
Daniel Vetterac58c3f2013-06-01 17:16:17 +02007156 i9xx_clock(66000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08007157 } else
Daniel Vetterac58c3f2013-06-01 17:16:17 +02007158 i9xx_clock(48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08007159 } else {
7160 if (dpll & PLL_P1_DIVIDE_BY_TWO)
7161 clock.p1 = 2;
7162 else {
7163 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
7164 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
7165 }
7166 if (dpll & PLL_P2_DIVIDE_BY_4)
7167 clock.p2 = 4;
7168 else
7169 clock.p2 = 2;
7170
Daniel Vetterac58c3f2013-06-01 17:16:17 +02007171 i9xx_clock(48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08007172 }
7173 }
7174
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007175 pipe_config->adjusted_mode.clock = clock.dot *
7176 pipe_config->pixel_multiplier;
7177}
7178
7179static void ironlake_crtc_clock_get(struct intel_crtc *crtc,
7180 struct intel_crtc_config *pipe_config)
7181{
7182 struct drm_device *dev = crtc->base.dev;
7183 struct drm_i915_private *dev_priv = dev->dev_private;
7184 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7185 int link_freq, repeat;
7186 u64 clock;
7187 u32 link_m, link_n;
7188
7189 repeat = pipe_config->pixel_multiplier;
7190
7191 /*
7192 * The calculation for the data clock is:
7193 * pixel_clock = ((m/n)*(link_clock * nr_lanes * repeat))/bpp
7194 * But we want to avoid losing precison if possible, so:
7195 * pixel_clock = ((m * link_clock * nr_lanes * repeat)/(n*bpp))
7196 *
7197 * and the link clock is simpler:
7198 * link_clock = (m * link_clock * repeat) / n
Jesse Barnes79e53942008-11-07 14:24:08 -08007199 */
7200
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007201 /*
7202 * We need to get the FDI or DP link clock here to derive
7203 * the M/N dividers.
7204 *
7205 * For FDI, we read it from the BIOS or use a fixed 2.7GHz.
7206 * For DP, it's either 1.62GHz or 2.7GHz.
7207 * We do our calculations in 10*MHz since we don't need much precison.
7208 */
7209 if (pipe_config->has_pch_encoder)
7210 link_freq = intel_fdi_link_freq(dev) * 10000;
7211 else
7212 link_freq = pipe_config->port_clock;
7213
7214 link_m = I915_READ(PIPE_LINK_M1(cpu_transcoder));
7215 link_n = I915_READ(PIPE_LINK_N1(cpu_transcoder));
7216
7217 if (!link_m || !link_n)
7218 return;
7219
7220 clock = ((u64)link_m * (u64)link_freq * (u64)repeat);
7221 do_div(clock, link_n);
7222
7223 pipe_config->adjusted_mode.clock = clock;
Jesse Barnes79e53942008-11-07 14:24:08 -08007224}
7225
7226/** Returns the currently programmed mode of the given pipe. */
7227struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
7228 struct drm_crtc *crtc)
7229{
Jesse Barnes548f2452011-02-17 10:40:53 -08007230 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08007231 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02007232 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08007233 struct drm_display_mode *mode;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007234 struct intel_crtc_config pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007235 int htot = I915_READ(HTOTAL(cpu_transcoder));
7236 int hsync = I915_READ(HSYNC(cpu_transcoder));
7237 int vtot = I915_READ(VTOTAL(cpu_transcoder));
7238 int vsync = I915_READ(VSYNC(cpu_transcoder));
Jesse Barnes79e53942008-11-07 14:24:08 -08007239
7240 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
7241 if (!mode)
7242 return NULL;
7243
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007244 /*
7245 * Construct a pipe_config sufficient for getting the clock info
7246 * back out of crtc_clock_get.
7247 *
7248 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
7249 * to use a real value here instead.
7250 */
Daniel Vettere143a212013-07-04 12:01:15 +02007251 pipe_config.cpu_transcoder = (enum transcoder) intel_crtc->pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007252 pipe_config.pixel_multiplier = 1;
7253 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
7254
7255 mode->clock = pipe_config.adjusted_mode.clock;
Jesse Barnes79e53942008-11-07 14:24:08 -08007256 mode->hdisplay = (htot & 0xffff) + 1;
7257 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
7258 mode->hsync_start = (hsync & 0xffff) + 1;
7259 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
7260 mode->vdisplay = (vtot & 0xffff) + 1;
7261 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
7262 mode->vsync_start = (vsync & 0xffff) + 1;
7263 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
7264
7265 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08007266
7267 return mode;
7268}
7269
Daniel Vetter3dec0092010-08-20 21:40:52 +02007270static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07007271{
7272 struct drm_device *dev = crtc->dev;
7273 drm_i915_private_t *dev_priv = dev->dev_private;
7274 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7275 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007276 int dpll_reg = DPLL(pipe);
7277 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07007278
Eric Anholtbad720f2009-10-22 16:11:14 -07007279 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07007280 return;
7281
7282 if (!dev_priv->lvds_downclock_avail)
7283 return;
7284
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007285 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07007286 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08007287 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007288
Sean Paul8ac5a6d2012-02-13 13:14:51 -05007289 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007290
7291 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
7292 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007293 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007294
Jesse Barnes652c3932009-08-17 13:31:43 -07007295 dpll = I915_READ(dpll_reg);
7296 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08007297 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007298 }
Jesse Barnes652c3932009-08-17 13:31:43 -07007299}
7300
7301static void intel_decrease_pllclock(struct drm_crtc *crtc)
7302{
7303 struct drm_device *dev = crtc->dev;
7304 drm_i915_private_t *dev_priv = dev->dev_private;
7305 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07007306
Eric Anholtbad720f2009-10-22 16:11:14 -07007307 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07007308 return;
7309
7310 if (!dev_priv->lvds_downclock_avail)
7311 return;
7312
7313 /*
7314 * Since this is called by a timer, we should never get here in
7315 * the manual case.
7316 */
7317 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01007318 int pipe = intel_crtc->pipe;
7319 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02007320 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01007321
Zhao Yakui44d98a62009-10-09 11:39:40 +08007322 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007323
Sean Paul8ac5a6d2012-02-13 13:14:51 -05007324 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007325
Chris Wilson074b5e12012-05-02 12:07:06 +01007326 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07007327 dpll |= DISPLAY_RATE_SELECT_FPA1;
7328 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007329 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007330 dpll = I915_READ(dpll_reg);
7331 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08007332 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007333 }
7334
7335}
7336
Chris Wilsonf047e392012-07-21 12:31:41 +01007337void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07007338{
Chris Wilsonf047e392012-07-21 12:31:41 +01007339 i915_update_gfx_val(dev->dev_private);
7340}
7341
7342void intel_mark_idle(struct drm_device *dev)
7343{
Chris Wilson725a5b52013-01-08 11:02:57 +00007344 struct drm_crtc *crtc;
7345
7346 if (!i915_powersave)
7347 return;
7348
7349 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
7350 if (!crtc->fb)
7351 continue;
7352
7353 intel_decrease_pllclock(crtc);
7354 }
Chris Wilsonf047e392012-07-21 12:31:41 +01007355}
7356
Chris Wilsonc65355b2013-06-06 16:53:41 -03007357void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
7358 struct intel_ring_buffer *ring)
Chris Wilsonf047e392012-07-21 12:31:41 +01007359{
7360 struct drm_device *dev = obj->base.dev;
Jesse Barnes652c3932009-08-17 13:31:43 -07007361 struct drm_crtc *crtc;
Jesse Barnes652c3932009-08-17 13:31:43 -07007362
7363 if (!i915_powersave)
7364 return;
7365
Jesse Barnes652c3932009-08-17 13:31:43 -07007366 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Jesse Barnes652c3932009-08-17 13:31:43 -07007367 if (!crtc->fb)
7368 continue;
7369
Chris Wilsonc65355b2013-06-06 16:53:41 -03007370 if (to_intel_framebuffer(crtc->fb)->obj != obj)
7371 continue;
7372
7373 intel_increase_pllclock(crtc);
7374 if (ring && intel_fbc_enabled(dev))
7375 ring->fbc_dirty = true;
Jesse Barnes652c3932009-08-17 13:31:43 -07007376 }
Jesse Barnes652c3932009-08-17 13:31:43 -07007377}
7378
Jesse Barnes79e53942008-11-07 14:24:08 -08007379static void intel_crtc_destroy(struct drm_crtc *crtc)
7380{
7381 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02007382 struct drm_device *dev = crtc->dev;
7383 struct intel_unpin_work *work;
7384 unsigned long flags;
7385
7386 spin_lock_irqsave(&dev->event_lock, flags);
7387 work = intel_crtc->unpin_work;
7388 intel_crtc->unpin_work = NULL;
7389 spin_unlock_irqrestore(&dev->event_lock, flags);
7390
7391 if (work) {
7392 cancel_work_sync(&work->work);
7393 kfree(work);
7394 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007395
Mika Kuoppala40ccc722013-04-23 17:27:08 +03007396 intel_crtc_cursor_set(crtc, NULL, 0, 0, 0);
7397
Jesse Barnes79e53942008-11-07 14:24:08 -08007398 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02007399
Jesse Barnes79e53942008-11-07 14:24:08 -08007400 kfree(intel_crtc);
7401}
7402
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007403static void intel_unpin_work_fn(struct work_struct *__work)
7404{
7405 struct intel_unpin_work *work =
7406 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007407 struct drm_device *dev = work->crtc->dev;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007408
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007409 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01007410 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00007411 drm_gem_object_unreference(&work->pending_flip_obj->base);
7412 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00007413
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007414 intel_update_fbc(dev);
7415 mutex_unlock(&dev->struct_mutex);
7416
7417 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
7418 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
7419
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007420 kfree(work);
7421}
7422
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007423static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01007424 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007425{
7426 drm_i915_private_t *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007427 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7428 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007429 unsigned long flags;
7430
7431 /* Ignore early vblank irqs */
7432 if (intel_crtc == NULL)
7433 return;
7434
7435 spin_lock_irqsave(&dev->event_lock, flags);
7436 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00007437
7438 /* Ensure we don't miss a work->pending update ... */
7439 smp_rmb();
7440
7441 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007442 spin_unlock_irqrestore(&dev->event_lock, flags);
7443 return;
7444 }
7445
Chris Wilsone7d841c2012-12-03 11:36:30 +00007446 /* and that the unpin work is consistent wrt ->pending. */
7447 smp_rmb();
7448
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007449 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007450
Rob Clark45a066e2012-10-08 14:50:40 -05007451 if (work->event)
7452 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007453
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01007454 drm_vblank_put(dev, intel_crtc->pipe);
7455
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007456 spin_unlock_irqrestore(&dev->event_lock, flags);
7457
Daniel Vetter2c10d572012-12-20 21:24:07 +01007458 wake_up_all(&dev_priv->pending_flip_queue);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007459
7460 queue_work(dev_priv->wq, &work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07007461
7462 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007463}
7464
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007465void intel_finish_page_flip(struct drm_device *dev, int pipe)
7466{
7467 drm_i915_private_t *dev_priv = dev->dev_private;
7468 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
7469
Mario Kleiner49b14a52010-12-09 07:00:07 +01007470 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007471}
7472
7473void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
7474{
7475 drm_i915_private_t *dev_priv = dev->dev_private;
7476 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
7477
Mario Kleiner49b14a52010-12-09 07:00:07 +01007478 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007479}
7480
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007481void intel_prepare_page_flip(struct drm_device *dev, int plane)
7482{
7483 drm_i915_private_t *dev_priv = dev->dev_private;
7484 struct intel_crtc *intel_crtc =
7485 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
7486 unsigned long flags;
7487
Chris Wilsone7d841c2012-12-03 11:36:30 +00007488 /* NB: An MMIO update of the plane base pointer will also
7489 * generate a page-flip completion irq, i.e. every modeset
7490 * is also accompanied by a spurious intel_prepare_page_flip().
7491 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007492 spin_lock_irqsave(&dev->event_lock, flags);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007493 if (intel_crtc->unpin_work)
7494 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007495 spin_unlock_irqrestore(&dev->event_lock, flags);
7496}
7497
Chris Wilsone7d841c2012-12-03 11:36:30 +00007498inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
7499{
7500 /* Ensure that the work item is consistent when activating it ... */
7501 smp_wmb();
7502 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
7503 /* and that it is marked active as soon as the irq could fire. */
7504 smp_wmb();
7505}
7506
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007507static int intel_gen2_queue_flip(struct drm_device *dev,
7508 struct drm_crtc *crtc,
7509 struct drm_framebuffer *fb,
7510 struct drm_i915_gem_object *obj)
7511{
7512 struct drm_i915_private *dev_priv = dev->dev_private;
7513 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007514 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007515 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007516 int ret;
7517
Daniel Vetter6d90c952012-04-26 23:28:05 +02007518 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007519 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007520 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007521
Daniel Vetter6d90c952012-04-26 23:28:05 +02007522 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007523 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007524 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007525
7526 /* Can't queue multiple flips, so wait for the previous
7527 * one to finish before executing the next.
7528 */
7529 if (intel_crtc->plane)
7530 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7531 else
7532 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007533 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7534 intel_ring_emit(ring, MI_NOOP);
7535 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7536 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7537 intel_ring_emit(ring, fb->pitches[0]);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007538 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007539 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00007540
7541 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007542 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007543 return 0;
7544
7545err_unpin:
7546 intel_unpin_fb_obj(obj);
7547err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007548 return ret;
7549}
7550
7551static int intel_gen3_queue_flip(struct drm_device *dev,
7552 struct drm_crtc *crtc,
7553 struct drm_framebuffer *fb,
7554 struct drm_i915_gem_object *obj)
7555{
7556 struct drm_i915_private *dev_priv = dev->dev_private;
7557 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007558 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007559 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007560 int ret;
7561
Daniel Vetter6d90c952012-04-26 23:28:05 +02007562 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007563 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007564 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007565
Daniel Vetter6d90c952012-04-26 23:28:05 +02007566 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007567 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007568 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007569
7570 if (intel_crtc->plane)
7571 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7572 else
7573 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007574 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7575 intel_ring_emit(ring, MI_NOOP);
7576 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
7577 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7578 intel_ring_emit(ring, fb->pitches[0]);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007579 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007580 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007581
Chris Wilsone7d841c2012-12-03 11:36:30 +00007582 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007583 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007584 return 0;
7585
7586err_unpin:
7587 intel_unpin_fb_obj(obj);
7588err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007589 return ret;
7590}
7591
7592static int intel_gen4_queue_flip(struct drm_device *dev,
7593 struct drm_crtc *crtc,
7594 struct drm_framebuffer *fb,
7595 struct drm_i915_gem_object *obj)
7596{
7597 struct drm_i915_private *dev_priv = dev->dev_private;
7598 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7599 uint32_t pf, pipesrc;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007600 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007601 int ret;
7602
Daniel Vetter6d90c952012-04-26 23:28:05 +02007603 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007604 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007605 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007606
Daniel Vetter6d90c952012-04-26 23:28:05 +02007607 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007608 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007609 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007610
7611 /* i965+ uses the linear or tiled offsets from the
7612 * Display Registers (which do not change across a page-flip)
7613 * so we need only reprogram the base address.
7614 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02007615 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7616 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7617 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02007618 intel_ring_emit(ring,
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007619 (i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset) |
Daniel Vetterc2c75132012-07-05 12:17:30 +02007620 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007621
7622 /* XXX Enabling the panel-fitter across page-flip is so far
7623 * untested on non-native modes, so ignore it for now.
7624 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
7625 */
7626 pf = 0;
7627 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007628 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007629
7630 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007631 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007632 return 0;
7633
7634err_unpin:
7635 intel_unpin_fb_obj(obj);
7636err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007637 return ret;
7638}
7639
7640static int intel_gen6_queue_flip(struct drm_device *dev,
7641 struct drm_crtc *crtc,
7642 struct drm_framebuffer *fb,
7643 struct drm_i915_gem_object *obj)
7644{
7645 struct drm_i915_private *dev_priv = dev->dev_private;
7646 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007647 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007648 uint32_t pf, pipesrc;
7649 int ret;
7650
Daniel Vetter6d90c952012-04-26 23:28:05 +02007651 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007652 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007653 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007654
Daniel Vetter6d90c952012-04-26 23:28:05 +02007655 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007656 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007657 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007658
Daniel Vetter6d90c952012-04-26 23:28:05 +02007659 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7660 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7661 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007662 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007663
Chris Wilson99d9acd2012-04-17 20:37:00 +01007664 /* Contrary to the suggestions in the documentation,
7665 * "Enable Panel Fitter" does not seem to be required when page
7666 * flipping with a non-native mode, and worse causes a normal
7667 * modeset to fail.
7668 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
7669 */
7670 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007671 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007672 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007673
7674 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007675 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007676 return 0;
7677
7678err_unpin:
7679 intel_unpin_fb_obj(obj);
7680err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007681 return ret;
7682}
7683
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007684/*
7685 * On gen7 we currently use the blit ring because (in early silicon at least)
7686 * the render ring doesn't give us interrpts for page flip completion, which
7687 * means clients will hang after the first flip is queued. Fortunately the
7688 * blit ring generates interrupts properly, so use it instead.
7689 */
7690static int intel_gen7_queue_flip(struct drm_device *dev,
7691 struct drm_crtc *crtc,
7692 struct drm_framebuffer *fb,
7693 struct drm_i915_gem_object *obj)
7694{
7695 struct drm_i915_private *dev_priv = dev->dev_private;
7696 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7697 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007698 uint32_t plane_bit = 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007699 int ret;
7700
7701 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7702 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007703 goto err;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007704
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007705 switch(intel_crtc->plane) {
7706 case PLANE_A:
7707 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
7708 break;
7709 case PLANE_B:
7710 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
7711 break;
7712 case PLANE_C:
7713 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
7714 break;
7715 default:
7716 WARN_ONCE(1, "unknown plane in flip command\n");
7717 ret = -ENODEV;
Eugeni Dodonovab3951e2012-06-18 19:03:38 -03007718 goto err_unpin;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007719 }
7720
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007721 ret = intel_ring_begin(ring, 4);
7722 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007723 goto err_unpin;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007724
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007725 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007726 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007727 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007728 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +00007729
7730 intel_mark_page_flip_active(intel_crtc);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007731 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007732 return 0;
7733
7734err_unpin:
7735 intel_unpin_fb_obj(obj);
7736err:
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007737 return ret;
7738}
7739
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007740static int intel_default_queue_flip(struct drm_device *dev,
7741 struct drm_crtc *crtc,
7742 struct drm_framebuffer *fb,
7743 struct drm_i915_gem_object *obj)
7744{
7745 return -ENODEV;
7746}
7747
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007748static int intel_crtc_page_flip(struct drm_crtc *crtc,
7749 struct drm_framebuffer *fb,
7750 struct drm_pending_vblank_event *event)
7751{
7752 struct drm_device *dev = crtc->dev;
7753 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02007754 struct drm_framebuffer *old_fb = crtc->fb;
7755 struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007756 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7757 struct intel_unpin_work *work;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007758 unsigned long flags;
Chris Wilson52e68632010-08-08 10:15:59 +01007759 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007760
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03007761 /* Can't change pixel format via MI display flips. */
7762 if (fb->pixel_format != crtc->fb->pixel_format)
7763 return -EINVAL;
7764
7765 /*
7766 * TILEOFF/LINOFF registers can't be changed via MI display flips.
7767 * Note that pitch changes could also affect these register.
7768 */
7769 if (INTEL_INFO(dev)->gen > 3 &&
7770 (fb->offsets[0] != crtc->fb->offsets[0] ||
7771 fb->pitches[0] != crtc->fb->pitches[0]))
7772 return -EINVAL;
7773
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007774 work = kzalloc(sizeof *work, GFP_KERNEL);
7775 if (work == NULL)
7776 return -ENOMEM;
7777
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007778 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007779 work->crtc = crtc;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02007780 work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007781 INIT_WORK(&work->work, intel_unpin_work_fn);
7782
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007783 ret = drm_vblank_get(dev, intel_crtc->pipe);
7784 if (ret)
7785 goto free_work;
7786
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007787 /* We borrow the event spin lock for protecting unpin_work */
7788 spin_lock_irqsave(&dev->event_lock, flags);
7789 if (intel_crtc->unpin_work) {
7790 spin_unlock_irqrestore(&dev->event_lock, flags);
7791 kfree(work);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007792 drm_vblank_put(dev, intel_crtc->pipe);
Chris Wilson468f0b42010-05-27 13:18:13 +01007793
7794 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007795 return -EBUSY;
7796 }
7797 intel_crtc->unpin_work = work;
7798 spin_unlock_irqrestore(&dev->event_lock, flags);
7799
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007800 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
7801 flush_workqueue(dev_priv->wq);
7802
Chris Wilson79158102012-05-23 11:13:58 +01007803 ret = i915_mutex_lock_interruptible(dev);
7804 if (ret)
7805 goto cleanup;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007806
Jesse Barnes75dfca82010-02-10 15:09:44 -08007807 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00007808 drm_gem_object_reference(&work->old_fb_obj->base);
7809 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007810
7811 crtc->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01007812
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007813 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007814
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01007815 work->enable_stall_check = true;
7816
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007817 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +02007818 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007819
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007820 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
7821 if (ret)
7822 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007823
Chris Wilson7782de32011-07-08 12:22:41 +01007824 intel_disable_fbc(dev);
Chris Wilsonc65355b2013-06-06 16:53:41 -03007825 intel_mark_fb_busy(obj, NULL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007826 mutex_unlock(&dev->struct_mutex);
7827
Jesse Barnese5510fa2010-07-01 16:48:37 -07007828 trace_i915_flip_request(intel_crtc->plane, obj);
7829
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007830 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01007831
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007832cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007833 atomic_dec(&intel_crtc->unpin_work_count);
Ville Syrjälä4a35f832013-02-22 16:53:38 +02007834 crtc->fb = old_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00007835 drm_gem_object_unreference(&work->old_fb_obj->base);
7836 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01007837 mutex_unlock(&dev->struct_mutex);
7838
Chris Wilson79158102012-05-23 11:13:58 +01007839cleanup:
Chris Wilson96b099f2010-06-07 14:03:04 +01007840 spin_lock_irqsave(&dev->event_lock, flags);
7841 intel_crtc->unpin_work = NULL;
7842 spin_unlock_irqrestore(&dev->event_lock, flags);
7843
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007844 drm_vblank_put(dev, intel_crtc->pipe);
7845free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01007846 kfree(work);
7847
7848 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007849}
7850
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007851static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007852 .mode_set_base_atomic = intel_pipe_set_base_atomic,
7853 .load_lut = intel_crtc_load_lut,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007854};
7855
Daniel Vetter50f56112012-07-02 09:35:43 +02007856static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
7857 struct drm_crtc *crtc)
7858{
7859 struct drm_device *dev;
7860 struct drm_crtc *tmp;
7861 int crtc_mask = 1;
7862
7863 WARN(!crtc, "checking null crtc?\n");
7864
7865 dev = crtc->dev;
7866
7867 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
7868 if (tmp == crtc)
7869 break;
7870 crtc_mask <<= 1;
7871 }
7872
7873 if (encoder->possible_crtcs & crtc_mask)
7874 return true;
7875 return false;
7876}
7877
Daniel Vetter9a935852012-07-05 22:34:27 +02007878/**
7879 * intel_modeset_update_staged_output_state
7880 *
7881 * Updates the staged output configuration state, e.g. after we've read out the
7882 * current hw state.
7883 */
7884static void intel_modeset_update_staged_output_state(struct drm_device *dev)
7885{
7886 struct intel_encoder *encoder;
7887 struct intel_connector *connector;
7888
7889 list_for_each_entry(connector, &dev->mode_config.connector_list,
7890 base.head) {
7891 connector->new_encoder =
7892 to_intel_encoder(connector->base.encoder);
7893 }
7894
7895 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7896 base.head) {
7897 encoder->new_crtc =
7898 to_intel_crtc(encoder->base.crtc);
7899 }
7900}
7901
7902/**
7903 * intel_modeset_commit_output_state
7904 *
7905 * This function copies the stage display pipe configuration to the real one.
7906 */
7907static void intel_modeset_commit_output_state(struct drm_device *dev)
7908{
7909 struct intel_encoder *encoder;
7910 struct intel_connector *connector;
7911
7912 list_for_each_entry(connector, &dev->mode_config.connector_list,
7913 base.head) {
7914 connector->base.encoder = &connector->new_encoder->base;
7915 }
7916
7917 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7918 base.head) {
7919 encoder->base.crtc = &encoder->new_crtc->base;
7920 }
7921}
7922
Daniel Vetter050f7ae2013-06-02 13:26:23 +02007923static void
7924connected_sink_compute_bpp(struct intel_connector * connector,
7925 struct intel_crtc_config *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007926{
Daniel Vetter050f7ae2013-06-02 13:26:23 +02007927 int bpp = pipe_config->pipe_bpp;
7928
7929 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
7930 connector->base.base.id,
7931 drm_get_connector_name(&connector->base));
7932
7933 /* Don't use an invalid EDID bpc value */
7934 if (connector->base.display_info.bpc &&
7935 connector->base.display_info.bpc * 3 < bpp) {
7936 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
7937 bpp, connector->base.display_info.bpc*3);
7938 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
7939 }
7940
7941 /* Clamp bpp to 8 on screens without EDID 1.4 */
7942 if (connector->base.display_info.bpc == 0 && bpp > 24) {
7943 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
7944 bpp);
7945 pipe_config->pipe_bpp = 24;
7946 }
7947}
7948
7949static int
7950compute_baseline_pipe_bpp(struct intel_crtc *crtc,
7951 struct drm_framebuffer *fb,
7952 struct intel_crtc_config *pipe_config)
7953{
7954 struct drm_device *dev = crtc->base.dev;
7955 struct intel_connector *connector;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007956 int bpp;
7957
Daniel Vetterd42264b2013-03-28 16:38:08 +01007958 switch (fb->pixel_format) {
7959 case DRM_FORMAT_C8:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007960 bpp = 8*3; /* since we go through a colormap */
7961 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01007962 case DRM_FORMAT_XRGB1555:
7963 case DRM_FORMAT_ARGB1555:
7964 /* checked in intel_framebuffer_init already */
7965 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
7966 return -EINVAL;
7967 case DRM_FORMAT_RGB565:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007968 bpp = 6*3; /* min is 18bpp */
7969 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01007970 case DRM_FORMAT_XBGR8888:
7971 case DRM_FORMAT_ABGR8888:
7972 /* checked in intel_framebuffer_init already */
7973 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
7974 return -EINVAL;
7975 case DRM_FORMAT_XRGB8888:
7976 case DRM_FORMAT_ARGB8888:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007977 bpp = 8*3;
7978 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01007979 case DRM_FORMAT_XRGB2101010:
7980 case DRM_FORMAT_ARGB2101010:
7981 case DRM_FORMAT_XBGR2101010:
7982 case DRM_FORMAT_ABGR2101010:
7983 /* checked in intel_framebuffer_init already */
7984 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
Daniel Vetterbaba1332013-03-27 00:45:00 +01007985 return -EINVAL;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007986 bpp = 10*3;
7987 break;
Daniel Vetterbaba1332013-03-27 00:45:00 +01007988 /* TODO: gen4+ supports 16 bpc floating point, too. */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007989 default:
7990 DRM_DEBUG_KMS("unsupported depth\n");
7991 return -EINVAL;
7992 }
7993
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007994 pipe_config->pipe_bpp = bpp;
7995
7996 /* Clamp display bpp to EDID value */
7997 list_for_each_entry(connector, &dev->mode_config.connector_list,
Daniel Vetter050f7ae2013-06-02 13:26:23 +02007998 base.head) {
Daniel Vetter1b829e02013-06-02 13:26:24 +02007999 if (!connector->new_encoder ||
8000 connector->new_encoder->new_crtc != crtc)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008001 continue;
8002
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008003 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008004 }
8005
8006 return bpp;
8007}
8008
Daniel Vetterc0b03412013-05-28 12:05:54 +02008009static void intel_dump_pipe_config(struct intel_crtc *crtc,
8010 struct intel_crtc_config *pipe_config,
8011 const char *context)
8012{
8013 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
8014 context, pipe_name(crtc->pipe));
8015
8016 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
8017 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
8018 pipe_config->pipe_bpp, pipe_config->dither);
8019 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
8020 pipe_config->has_pch_encoder,
8021 pipe_config->fdi_lanes,
8022 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
8023 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
8024 pipe_config->fdi_m_n.tu);
8025 DRM_DEBUG_KMS("requested mode:\n");
8026 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
8027 DRM_DEBUG_KMS("adjusted mode:\n");
8028 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
8029 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
8030 pipe_config->gmch_pfit.control,
8031 pipe_config->gmch_pfit.pgm_ratios,
8032 pipe_config->gmch_pfit.lvds_border_bits);
8033 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x\n",
8034 pipe_config->pch_pfit.pos,
8035 pipe_config->pch_pfit.size);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03008036 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Daniel Vetterc0b03412013-05-28 12:05:54 +02008037}
8038
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02008039static bool check_encoder_cloning(struct drm_crtc *crtc)
8040{
8041 int num_encoders = 0;
8042 bool uncloneable_encoders = false;
8043 struct intel_encoder *encoder;
8044
8045 list_for_each_entry(encoder, &crtc->dev->mode_config.encoder_list,
8046 base.head) {
8047 if (&encoder->new_crtc->base != crtc)
8048 continue;
8049
8050 num_encoders++;
8051 if (!encoder->cloneable)
8052 uncloneable_encoders = true;
8053 }
8054
8055 return !(num_encoders > 1 && uncloneable_encoders);
8056}
8057
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008058static struct intel_crtc_config *
8059intel_modeset_pipe_config(struct drm_crtc *crtc,
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008060 struct drm_framebuffer *fb,
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008061 struct drm_display_mode *mode)
Daniel Vetter7758a112012-07-08 19:40:39 +02008062{
8063 struct drm_device *dev = crtc->dev;
Daniel Vetter7758a112012-07-08 19:40:39 +02008064 struct intel_encoder *encoder;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008065 struct intel_crtc_config *pipe_config;
Daniel Vettere29c22c2013-02-21 00:00:16 +01008066 int plane_bpp, ret = -EINVAL;
8067 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +02008068
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02008069 if (!check_encoder_cloning(crtc)) {
8070 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
8071 return ERR_PTR(-EINVAL);
8072 }
8073
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008074 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
8075 if (!pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +02008076 return ERR_PTR(-ENOMEM);
8077
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008078 drm_mode_copy(&pipe_config->adjusted_mode, mode);
8079 drm_mode_copy(&pipe_config->requested_mode, mode);
Daniel Vettere143a212013-07-04 12:01:15 +02008080 pipe_config->cpu_transcoder =
8081 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008082 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008083
Daniel Vetter050f7ae2013-06-02 13:26:23 +02008084 /* Compute a starting value for pipe_config->pipe_bpp taking the source
8085 * plane pixel format and any sink constraints into account. Returns the
8086 * source plane bpp so that dithering can be selected on mismatches
8087 * after encoders and crtc also have had their say. */
8088 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
8089 fb, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008090 if (plane_bpp < 0)
8091 goto fail;
8092
Daniel Vettere29c22c2013-02-21 00:00:16 +01008093encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +02008094 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +02008095 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +02008096 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +02008097
Daniel Vetter7758a112012-07-08 19:40:39 +02008098 /* Pass our mode to the connectors and the CRTC to give them a chance to
8099 * adjust it according to limitations or connector properties, and also
8100 * a chance to reject the mode entirely.
8101 */
8102 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8103 base.head) {
8104
8105 if (&encoder->new_crtc->base != crtc)
8106 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +01008107
Daniel Vetterefea6e82013-07-21 21:36:59 +02008108 if (!(encoder->compute_config(encoder, pipe_config))) {
8109 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +02008110 goto fail;
8111 }
8112 }
8113
Daniel Vetterff9a6752013-06-01 17:16:21 +02008114 /* Set default port clock if not overwritten by the encoder. Needs to be
8115 * done afterwards in case the encoder adjusts the mode. */
8116 if (!pipe_config->port_clock)
8117 pipe_config->port_clock = pipe_config->adjusted_mode.clock;
8118
Daniel Vettera43f6e02013-06-07 23:10:32 +02008119 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01008120 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +02008121 DRM_DEBUG_KMS("CRTC fixup failed\n");
8122 goto fail;
8123 }
Daniel Vettere29c22c2013-02-21 00:00:16 +01008124
8125 if (ret == RETRY) {
8126 if (WARN(!retry, "loop in pipe configuration computation\n")) {
8127 ret = -EINVAL;
8128 goto fail;
8129 }
8130
8131 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
8132 retry = false;
8133 goto encoder_retry;
8134 }
8135
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008136 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
8137 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
8138 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
8139
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008140 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +02008141fail:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008142 kfree(pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01008143 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +02008144}
8145
Daniel Vettere2e1ed42012-07-08 21:14:38 +02008146/* Computes which crtcs are affected and sets the relevant bits in the mask. For
8147 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
8148static void
8149intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
8150 unsigned *prepare_pipes, unsigned *disable_pipes)
8151{
8152 struct intel_crtc *intel_crtc;
8153 struct drm_device *dev = crtc->dev;
8154 struct intel_encoder *encoder;
8155 struct intel_connector *connector;
8156 struct drm_crtc *tmp_crtc;
8157
8158 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
8159
8160 /* Check which crtcs have changed outputs connected to them, these need
8161 * to be part of the prepare_pipes mask. We don't (yet) support global
8162 * modeset across multiple crtcs, so modeset_pipes will only have one
8163 * bit set at most. */
8164 list_for_each_entry(connector, &dev->mode_config.connector_list,
8165 base.head) {
8166 if (connector->base.encoder == &connector->new_encoder->base)
8167 continue;
8168
8169 if (connector->base.encoder) {
8170 tmp_crtc = connector->base.encoder->crtc;
8171
8172 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
8173 }
8174
8175 if (connector->new_encoder)
8176 *prepare_pipes |=
8177 1 << connector->new_encoder->new_crtc->pipe;
8178 }
8179
8180 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8181 base.head) {
8182 if (encoder->base.crtc == &encoder->new_crtc->base)
8183 continue;
8184
8185 if (encoder->base.crtc) {
8186 tmp_crtc = encoder->base.crtc;
8187
8188 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
8189 }
8190
8191 if (encoder->new_crtc)
8192 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
8193 }
8194
8195 /* Check for any pipes that will be fully disabled ... */
8196 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
8197 base.head) {
8198 bool used = false;
8199
8200 /* Don't try to disable disabled crtcs. */
8201 if (!intel_crtc->base.enabled)
8202 continue;
8203
8204 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8205 base.head) {
8206 if (encoder->new_crtc == intel_crtc)
8207 used = true;
8208 }
8209
8210 if (!used)
8211 *disable_pipes |= 1 << intel_crtc->pipe;
8212 }
8213
8214
8215 /* set_mode is also used to update properties on life display pipes. */
8216 intel_crtc = to_intel_crtc(crtc);
8217 if (crtc->enabled)
8218 *prepare_pipes |= 1 << intel_crtc->pipe;
8219
Daniel Vetterb6c51642013-04-12 18:48:43 +02008220 /*
8221 * For simplicity do a full modeset on any pipe where the output routing
8222 * changed. We could be more clever, but that would require us to be
8223 * more careful with calling the relevant encoder->mode_set functions.
8224 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +02008225 if (*prepare_pipes)
8226 *modeset_pipes = *prepare_pipes;
8227
8228 /* ... and mask these out. */
8229 *modeset_pipes &= ~(*disable_pipes);
8230 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +02008231
8232 /*
8233 * HACK: We don't (yet) fully support global modesets. intel_set_config
8234 * obies this rule, but the modeset restore mode of
8235 * intel_modeset_setup_hw_state does not.
8236 */
8237 *modeset_pipes &= 1 << intel_crtc->pipe;
8238 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +02008239
8240 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
8241 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +02008242}
8243
Daniel Vetterea9d7582012-07-10 10:42:52 +02008244static bool intel_crtc_in_use(struct drm_crtc *crtc)
8245{
8246 struct drm_encoder *encoder;
8247 struct drm_device *dev = crtc->dev;
8248
8249 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
8250 if (encoder->crtc == crtc)
8251 return true;
8252
8253 return false;
8254}
8255
8256static void
8257intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
8258{
8259 struct intel_encoder *intel_encoder;
8260 struct intel_crtc *intel_crtc;
8261 struct drm_connector *connector;
8262
8263 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
8264 base.head) {
8265 if (!intel_encoder->base.crtc)
8266 continue;
8267
8268 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
8269
8270 if (prepare_pipes & (1 << intel_crtc->pipe))
8271 intel_encoder->connectors_active = false;
8272 }
8273
8274 intel_modeset_commit_output_state(dev);
8275
8276 /* Update computed state. */
8277 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
8278 base.head) {
8279 intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
8280 }
8281
8282 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
8283 if (!connector->encoder || !connector->encoder->crtc)
8284 continue;
8285
8286 intel_crtc = to_intel_crtc(connector->encoder->crtc);
8287
8288 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +02008289 struct drm_property *dpms_property =
8290 dev->mode_config.dpms_property;
8291
Daniel Vetterea9d7582012-07-10 10:42:52 +02008292 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -05008293 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +02008294 dpms_property,
8295 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +02008296
8297 intel_encoder = to_intel_encoder(connector->encoder);
8298 intel_encoder->connectors_active = true;
8299 }
8300 }
8301
8302}
8303
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008304static bool intel_fuzzy_clock_check(struct intel_crtc_config *cur,
8305 struct intel_crtc_config *new)
8306{
8307 int clock1, clock2, diff;
8308
8309 clock1 = cur->adjusted_mode.clock;
8310 clock2 = new->adjusted_mode.clock;
8311
8312 if (clock1 == clock2)
8313 return true;
8314
8315 if (!clock1 || !clock2)
8316 return false;
8317
8318 diff = abs(clock1 - clock2);
8319
8320 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
8321 return true;
8322
8323 return false;
8324}
8325
Daniel Vetter25c5b262012-07-08 22:08:04 +02008326#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
8327 list_for_each_entry((intel_crtc), \
8328 &(dev)->mode_config.crtc_list, \
8329 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +02008330 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +02008331
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008332static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008333intel_pipe_config_compare(struct drm_device *dev,
8334 struct intel_crtc_config *current_config,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008335 struct intel_crtc_config *pipe_config)
8336{
Daniel Vetter66e985c2013-06-05 13:34:20 +02008337#define PIPE_CONF_CHECK_X(name) \
8338 if (current_config->name != pipe_config->name) { \
8339 DRM_ERROR("mismatch in " #name " " \
8340 "(expected 0x%08x, found 0x%08x)\n", \
8341 current_config->name, \
8342 pipe_config->name); \
8343 return false; \
8344 }
8345
Daniel Vetter08a24032013-04-19 11:25:34 +02008346#define PIPE_CONF_CHECK_I(name) \
8347 if (current_config->name != pipe_config->name) { \
8348 DRM_ERROR("mismatch in " #name " " \
8349 "(expected %i, found %i)\n", \
8350 current_config->name, \
8351 pipe_config->name); \
8352 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +01008353 }
8354
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008355#define PIPE_CONF_CHECK_FLAGS(name, mask) \
8356 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -07008357 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008358 "(expected %i, found %i)\n", \
8359 current_config->name & (mask), \
8360 pipe_config->name & (mask)); \
8361 return false; \
8362 }
8363
Daniel Vetterbb760062013-06-06 14:55:52 +02008364#define PIPE_CONF_QUIRK(quirk) \
8365 ((current_config->quirks | pipe_config->quirks) & (quirk))
8366
Daniel Vettereccb1402013-05-22 00:50:22 +02008367 PIPE_CONF_CHECK_I(cpu_transcoder);
8368
Daniel Vetter08a24032013-04-19 11:25:34 +02008369 PIPE_CONF_CHECK_I(has_pch_encoder);
8370 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +02008371 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
8372 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
8373 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
8374 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
8375 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +02008376
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008377 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
8378 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
8379 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
8380 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
8381 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
8382 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
8383
8384 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
8385 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
8386 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
8387 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
8388 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
8389 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
8390
Daniel Vetterc93f54c2013-06-27 19:47:19 +02008391 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008392
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008393 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8394 DRM_MODE_FLAG_INTERLACE);
8395
Daniel Vetterbb760062013-06-06 14:55:52 +02008396 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
8397 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8398 DRM_MODE_FLAG_PHSYNC);
8399 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8400 DRM_MODE_FLAG_NHSYNC);
8401 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8402 DRM_MODE_FLAG_PVSYNC);
8403 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8404 DRM_MODE_FLAG_NVSYNC);
8405 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -07008406
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008407 PIPE_CONF_CHECK_I(requested_mode.hdisplay);
8408 PIPE_CONF_CHECK_I(requested_mode.vdisplay);
8409
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008410 PIPE_CONF_CHECK_I(gmch_pfit.control);
8411 /* pfit ratios are autocomputed by the hw on gen4+ */
8412 if (INTEL_INFO(dev)->gen < 4)
8413 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
8414 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
8415 PIPE_CONF_CHECK_I(pch_pfit.pos);
8416 PIPE_CONF_CHECK_I(pch_pfit.size);
8417
Paulo Zanoni42db64e2013-05-31 16:33:22 -03008418 PIPE_CONF_CHECK_I(ips_enabled);
8419
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008420 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008421 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008422 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008423 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
8424 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008425
Daniel Vetter66e985c2013-06-05 13:34:20 +02008426#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +02008427#undef PIPE_CONF_CHECK_I
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008428#undef PIPE_CONF_CHECK_FLAGS
Daniel Vetterbb760062013-06-06 14:55:52 +02008429#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008430
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008431 if (!IS_HASWELL(dev)) {
8432 if (!intel_fuzzy_clock_check(current_config, pipe_config)) {
Jesse Barnes6f024882013-07-01 10:19:09 -07008433 DRM_ERROR("mismatch in clock (expected %d, found %d)\n",
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008434 current_config->adjusted_mode.clock,
8435 pipe_config->adjusted_mode.clock);
8436 return false;
8437 }
8438 }
8439
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008440 return true;
8441}
8442
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02008443static void
8444check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008445{
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008446 struct intel_connector *connector;
8447
8448 list_for_each_entry(connector, &dev->mode_config.connector_list,
8449 base.head) {
8450 /* This also checks the encoder/connector hw state with the
8451 * ->get_hw_state callbacks. */
8452 intel_connector_check_state(connector);
8453
8454 WARN(&connector->new_encoder->base != connector->base.encoder,
8455 "connector's staged encoder doesn't match current encoder\n");
8456 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02008457}
8458
8459static void
8460check_encoder_state(struct drm_device *dev)
8461{
8462 struct intel_encoder *encoder;
8463 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008464
8465 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8466 base.head) {
8467 bool enabled = false;
8468 bool active = false;
8469 enum pipe pipe, tracked_pipe;
8470
8471 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
8472 encoder->base.base.id,
8473 drm_get_encoder_name(&encoder->base));
8474
8475 WARN(&encoder->new_crtc->base != encoder->base.crtc,
8476 "encoder's stage crtc doesn't match current crtc\n");
8477 WARN(encoder->connectors_active && !encoder->base.crtc,
8478 "encoder's active_connectors set, but no crtc\n");
8479
8480 list_for_each_entry(connector, &dev->mode_config.connector_list,
8481 base.head) {
8482 if (connector->base.encoder != &encoder->base)
8483 continue;
8484 enabled = true;
8485 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
8486 active = true;
8487 }
8488 WARN(!!encoder->base.crtc != enabled,
8489 "encoder's enabled state mismatch "
8490 "(expected %i, found %i)\n",
8491 !!encoder->base.crtc, enabled);
8492 WARN(active && !encoder->base.crtc,
8493 "active encoder with no crtc\n");
8494
8495 WARN(encoder->connectors_active != active,
8496 "encoder's computed active state doesn't match tracked active state "
8497 "(expected %i, found %i)\n", active, encoder->connectors_active);
8498
8499 active = encoder->get_hw_state(encoder, &pipe);
8500 WARN(active != encoder->connectors_active,
8501 "encoder's hw state doesn't match sw tracking "
8502 "(expected %i, found %i)\n",
8503 encoder->connectors_active, active);
8504
8505 if (!encoder->base.crtc)
8506 continue;
8507
8508 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
8509 WARN(active && pipe != tracked_pipe,
8510 "active encoder's pipe doesn't match"
8511 "(expected %i, found %i)\n",
8512 tracked_pipe, pipe);
8513
8514 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02008515}
8516
8517static void
8518check_crtc_state(struct drm_device *dev)
8519{
8520 drm_i915_private_t *dev_priv = dev->dev_private;
8521 struct intel_crtc *crtc;
8522 struct intel_encoder *encoder;
8523 struct intel_crtc_config pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008524
8525 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
8526 base.head) {
8527 bool enabled = false;
8528 bool active = false;
8529
Jesse Barnes045ac3b2013-05-14 17:08:26 -07008530 memset(&pipe_config, 0, sizeof(pipe_config));
8531
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008532 DRM_DEBUG_KMS("[CRTC:%d]\n",
8533 crtc->base.base.id);
8534
8535 WARN(crtc->active && !crtc->base.enabled,
8536 "active crtc, but not enabled in sw tracking\n");
8537
8538 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8539 base.head) {
8540 if (encoder->base.crtc != &crtc->base)
8541 continue;
8542 enabled = true;
8543 if (encoder->connectors_active)
8544 active = true;
8545 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008546
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008547 WARN(active != crtc->active,
8548 "crtc's computed active state doesn't match tracked active state "
8549 "(expected %i, found %i)\n", active, crtc->active);
8550 WARN(enabled != crtc->base.enabled,
8551 "crtc's computed enabled state doesn't match tracked enabled state "
8552 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
8553
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008554 active = dev_priv->display.get_pipe_config(crtc,
8555 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +02008556
8557 /* hw state is inconsistent with the pipe A quirk */
8558 if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
8559 active = crtc->active;
8560
Daniel Vetter6c49f242013-06-06 12:45:25 +02008561 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8562 base.head) {
8563 if (encoder->base.crtc != &crtc->base)
8564 continue;
Jesse Barnes510d5f22013-07-01 15:50:17 -07008565 if (encoder->get_config)
Daniel Vetter6c49f242013-06-06 12:45:25 +02008566 encoder->get_config(encoder, &pipe_config);
8567 }
8568
Jesse Barnes510d5f22013-07-01 15:50:17 -07008569 if (dev_priv->display.get_clock)
8570 dev_priv->display.get_clock(crtc, &pipe_config);
8571
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008572 WARN(crtc->active != active,
8573 "crtc active state doesn't match with hw state "
8574 "(expected %i, found %i)\n", crtc->active, active);
8575
Daniel Vetterc0b03412013-05-28 12:05:54 +02008576 if (active &&
8577 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
8578 WARN(1, "pipe state doesn't match!\n");
8579 intel_dump_pipe_config(crtc, &pipe_config,
8580 "[hw state]");
8581 intel_dump_pipe_config(crtc, &crtc->config,
8582 "[sw state]");
8583 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008584 }
8585}
8586
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02008587static void
8588check_shared_dpll_state(struct drm_device *dev)
8589{
8590 drm_i915_private_t *dev_priv = dev->dev_private;
8591 struct intel_crtc *crtc;
8592 struct intel_dpll_hw_state dpll_hw_state;
8593 int i;
Daniel Vetter53589012013-06-05 13:34:16 +02008594
8595 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8596 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
8597 int enabled_crtcs = 0, active_crtcs = 0;
8598 bool active;
8599
8600 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
8601
8602 DRM_DEBUG_KMS("%s\n", pll->name);
8603
8604 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
8605
8606 WARN(pll->active > pll->refcount,
8607 "more active pll users than references: %i vs %i\n",
8608 pll->active, pll->refcount);
8609 WARN(pll->active && !pll->on,
8610 "pll in active use but not on in sw tracking\n");
Daniel Vetter35c95372013-07-17 06:55:04 +02008611 WARN(pll->on && !pll->active,
8612 "pll in on but not on in use in sw tracking\n");
Daniel Vetter53589012013-06-05 13:34:16 +02008613 WARN(pll->on != active,
8614 "pll on state mismatch (expected %i, found %i)\n",
8615 pll->on, active);
8616
8617 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
8618 base.head) {
8619 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
8620 enabled_crtcs++;
8621 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
8622 active_crtcs++;
8623 }
8624 WARN(pll->active != active_crtcs,
8625 "pll active crtcs mismatch (expected %i, found %i)\n",
8626 pll->active, active_crtcs);
8627 WARN(pll->refcount != enabled_crtcs,
8628 "pll enabled crtcs mismatch (expected %i, found %i)\n",
8629 pll->refcount, enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008630
8631 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
8632 sizeof(dpll_hw_state)),
8633 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +02008634 }
Daniel Vettera6778b32012-07-02 09:56:42 +02008635}
8636
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02008637void
8638intel_modeset_check_state(struct drm_device *dev)
8639{
8640 check_connector_state(dev);
8641 check_encoder_state(dev);
8642 check_crtc_state(dev);
8643 check_shared_dpll_state(dev);
8644}
8645
Daniel Vetterf30da182013-04-11 20:22:50 +02008646static int __intel_set_mode(struct drm_crtc *crtc,
8647 struct drm_display_mode *mode,
8648 int x, int y, struct drm_framebuffer *fb)
Daniel Vettera6778b32012-07-02 09:56:42 +02008649{
8650 struct drm_device *dev = crtc->dev;
Daniel Vetterdbf2b54e2012-07-02 11:18:29 +02008651 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008652 struct drm_display_mode *saved_mode, *saved_hwmode;
8653 struct intel_crtc_config *pipe_config = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +02008654 struct intel_crtc *intel_crtc;
8655 unsigned disable_pipes, prepare_pipes, modeset_pipes;
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008656 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +02008657
Tim Gardner3ac18232012-12-07 07:54:26 -07008658 saved_mode = kmalloc(2 * sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008659 if (!saved_mode)
8660 return -ENOMEM;
Tim Gardner3ac18232012-12-07 07:54:26 -07008661 saved_hwmode = saved_mode + 1;
Daniel Vettera6778b32012-07-02 09:56:42 +02008662
Daniel Vettere2e1ed42012-07-08 21:14:38 +02008663 intel_modeset_affected_pipes(crtc, &modeset_pipes,
Daniel Vetter25c5b262012-07-08 22:08:04 +02008664 &prepare_pipes, &disable_pipes);
8665
Tim Gardner3ac18232012-12-07 07:54:26 -07008666 *saved_hwmode = crtc->hwmode;
8667 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02008668
Daniel Vetter25c5b262012-07-08 22:08:04 +02008669 /* Hack: Because we don't (yet) support global modeset on multiple
8670 * crtcs, we don't keep track of the new mode for more than one crtc.
8671 * Hence simply check whether any bit is set in modeset_pipes in all the
8672 * pieces of code that are not yet converted to deal with mutliple crtcs
8673 * changing their mode at the same time. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02008674 if (modeset_pipes) {
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008675 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008676 if (IS_ERR(pipe_config)) {
8677 ret = PTR_ERR(pipe_config);
8678 pipe_config = NULL;
8679
Tim Gardner3ac18232012-12-07 07:54:26 -07008680 goto out;
Daniel Vetter25c5b262012-07-08 22:08:04 +02008681 }
Daniel Vetterc0b03412013-05-28 12:05:54 +02008682 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
8683 "[modeset]");
Daniel Vettera6778b32012-07-02 09:56:42 +02008684 }
8685
Daniel Vetter460da9162013-03-27 00:44:51 +01008686 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
8687 intel_crtc_disable(&intel_crtc->base);
8688
Daniel Vetterea9d7582012-07-10 10:42:52 +02008689 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
8690 if (intel_crtc->base.enabled)
8691 dev_priv->display.crtc_disable(&intel_crtc->base);
8692 }
Daniel Vettera6778b32012-07-02 09:56:42 +02008693
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02008694 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
8695 * to set it here already despite that we pass it down the callchain.
8696 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008697 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +02008698 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008699 /* mode_set/enable/disable functions rely on a correct pipe
8700 * config. */
8701 to_intel_crtc(crtc)->config = *pipe_config;
8702 }
Daniel Vetter7758a112012-07-08 19:40:39 +02008703
Daniel Vetterea9d7582012-07-10 10:42:52 +02008704 /* Only after disabling all output pipelines that will be changed can we
8705 * update the the output configuration. */
8706 intel_modeset_update_state(dev, prepare_pipes);
8707
Daniel Vetter47fab732012-10-26 10:58:18 +02008708 if (dev_priv->display.modeset_global_resources)
8709 dev_priv->display.modeset_global_resources(dev);
8710
Daniel Vettera6778b32012-07-02 09:56:42 +02008711 /* Set up the DPLL and any encoders state that needs to adjust or depend
8712 * on the DPLL.
8713 */
Daniel Vetter25c5b262012-07-08 22:08:04 +02008714 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008715 ret = intel_crtc_mode_set(&intel_crtc->base,
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008716 x, y, fb);
8717 if (ret)
8718 goto done;
Daniel Vettera6778b32012-07-02 09:56:42 +02008719 }
8720
8721 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02008722 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
8723 dev_priv->display.crtc_enable(&intel_crtc->base);
Daniel Vettera6778b32012-07-02 09:56:42 +02008724
Daniel Vetter25c5b262012-07-08 22:08:04 +02008725 if (modeset_pipes) {
8726 /* Store real post-adjustment hardware mode. */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008727 crtc->hwmode = pipe_config->adjusted_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02008728
Daniel Vetter25c5b262012-07-08 22:08:04 +02008729 /* Calculate and store various constants which
8730 * are later needed by vblank and swap-completion
8731 * timestamping. They are derived from true hwmode.
8732 */
8733 drm_calc_timestamping_constants(crtc);
8734 }
Daniel Vettera6778b32012-07-02 09:56:42 +02008735
8736 /* FIXME: add subpixel order */
8737done:
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008738 if (ret && crtc->enabled) {
Tim Gardner3ac18232012-12-07 07:54:26 -07008739 crtc->hwmode = *saved_hwmode;
8740 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02008741 }
8742
Tim Gardner3ac18232012-12-07 07:54:26 -07008743out:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008744 kfree(pipe_config);
Tim Gardner3ac18232012-12-07 07:54:26 -07008745 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +02008746 return ret;
8747}
8748
Daniel Vetterf30da182013-04-11 20:22:50 +02008749int intel_set_mode(struct drm_crtc *crtc,
8750 struct drm_display_mode *mode,
8751 int x, int y, struct drm_framebuffer *fb)
8752{
8753 int ret;
8754
8755 ret = __intel_set_mode(crtc, mode, x, y, fb);
8756
8757 if (ret == 0)
8758 intel_modeset_check_state(crtc->dev);
8759
8760 return ret;
8761}
8762
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008763void intel_crtc_restore_mode(struct drm_crtc *crtc)
8764{
8765 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->fb);
8766}
8767
Daniel Vetter25c5b262012-07-08 22:08:04 +02008768#undef for_each_intel_crtc_masked
8769
Daniel Vetterd9e55602012-07-04 22:16:09 +02008770static void intel_set_config_free(struct intel_set_config *config)
8771{
8772 if (!config)
8773 return;
8774
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008775 kfree(config->save_connector_encoders);
8776 kfree(config->save_encoder_crtcs);
Daniel Vetterd9e55602012-07-04 22:16:09 +02008777 kfree(config);
8778}
8779
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008780static int intel_set_config_save_state(struct drm_device *dev,
8781 struct intel_set_config *config)
8782{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008783 struct drm_encoder *encoder;
8784 struct drm_connector *connector;
8785 int count;
8786
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008787 config->save_encoder_crtcs =
8788 kcalloc(dev->mode_config.num_encoder,
8789 sizeof(struct drm_crtc *), GFP_KERNEL);
8790 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008791 return -ENOMEM;
8792
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008793 config->save_connector_encoders =
8794 kcalloc(dev->mode_config.num_connector,
8795 sizeof(struct drm_encoder *), GFP_KERNEL);
8796 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008797 return -ENOMEM;
8798
8799 /* Copy data. Note that driver private data is not affected.
8800 * Should anything bad happen only the expected state is
8801 * restored, not the drivers personal bookkeeping.
8802 */
8803 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008804 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008805 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008806 }
8807
8808 count = 0;
8809 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008810 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008811 }
8812
8813 return 0;
8814}
8815
8816static void intel_set_config_restore_state(struct drm_device *dev,
8817 struct intel_set_config *config)
8818{
Daniel Vetter9a935852012-07-05 22:34:27 +02008819 struct intel_encoder *encoder;
8820 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008821 int count;
8822
8823 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008824 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
8825 encoder->new_crtc =
8826 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008827 }
8828
8829 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008830 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
8831 connector->new_encoder =
8832 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008833 }
8834}
8835
Imre Deake3de42b2013-05-03 19:44:07 +02008836static bool
Chris Wilson2e57f472013-07-17 12:14:40 +01008837is_crtc_connector_off(struct drm_mode_set *set)
Imre Deake3de42b2013-05-03 19:44:07 +02008838{
8839 int i;
8840
Chris Wilson2e57f472013-07-17 12:14:40 +01008841 if (set->num_connectors == 0)
8842 return false;
8843
8844 if (WARN_ON(set->connectors == NULL))
8845 return false;
8846
8847 for (i = 0; i < set->num_connectors; i++)
8848 if (set->connectors[i]->encoder &&
8849 set->connectors[i]->encoder->crtc == set->crtc &&
8850 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
Imre Deake3de42b2013-05-03 19:44:07 +02008851 return true;
8852
8853 return false;
8854}
8855
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008856static void
8857intel_set_config_compute_mode_changes(struct drm_mode_set *set,
8858 struct intel_set_config *config)
8859{
8860
8861 /* We should be able to check here if the fb has the same properties
8862 * and then just flip_or_move it */
Chris Wilson2e57f472013-07-17 12:14:40 +01008863 if (is_crtc_connector_off(set)) {
8864 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02008865 } else if (set->crtc->fb != set->fb) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008866 /* If we have no fb then treat it as a full mode set */
8867 if (set->crtc->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +03008868 struct intel_crtc *intel_crtc =
8869 to_intel_crtc(set->crtc);
8870
8871 if (intel_crtc->active && i915_fastboot) {
8872 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
8873 config->fb_changed = true;
8874 } else {
8875 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
8876 config->mode_changed = true;
8877 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008878 } else if (set->fb == NULL) {
8879 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +01008880 } else if (set->fb->pixel_format !=
8881 set->crtc->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008882 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02008883 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008884 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02008885 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008886 }
8887
Daniel Vetter835c5872012-07-10 18:11:08 +02008888 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008889 config->fb_changed = true;
8890
8891 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
8892 DRM_DEBUG_KMS("modes are different, full mode set\n");
8893 drm_mode_debug_printmodeline(&set->crtc->mode);
8894 drm_mode_debug_printmodeline(set->mode);
8895 config->mode_changed = true;
8896 }
8897}
8898
Daniel Vetter2e431052012-07-04 22:42:15 +02008899static int
Daniel Vetter9a935852012-07-05 22:34:27 +02008900intel_modeset_stage_output_state(struct drm_device *dev,
8901 struct drm_mode_set *set,
8902 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +02008903{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008904 struct drm_crtc *new_crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02008905 struct intel_connector *connector;
8906 struct intel_encoder *encoder;
Daniel Vetter2e431052012-07-04 22:42:15 +02008907 int count, ro;
Daniel Vetter50f56112012-07-02 09:35:43 +02008908
Damien Lespiau9abdda72013-02-13 13:29:23 +00008909 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +02008910 * of connectors. For paranoia, double-check this. */
8911 WARN_ON(!set->fb && (set->num_connectors != 0));
8912 WARN_ON(set->fb && (set->num_connectors == 0));
8913
Daniel Vetter50f56112012-07-02 09:35:43 +02008914 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008915 list_for_each_entry(connector, &dev->mode_config.connector_list,
8916 base.head) {
8917 /* Otherwise traverse passed in connector list and get encoders
8918 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +02008919 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02008920 if (set->connectors[ro] == &connector->base) {
8921 connector->new_encoder = connector->encoder;
Daniel Vetter50f56112012-07-02 09:35:43 +02008922 break;
8923 }
8924 }
8925
Daniel Vetter9a935852012-07-05 22:34:27 +02008926 /* If we disable the crtc, disable all its connectors. Also, if
8927 * the connector is on the changing crtc but not on the new
8928 * connector list, disable it. */
8929 if ((!set->fb || ro == set->num_connectors) &&
8930 connector->base.encoder &&
8931 connector->base.encoder->crtc == set->crtc) {
8932 connector->new_encoder = NULL;
8933
8934 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
8935 connector->base.base.id,
8936 drm_get_connector_name(&connector->base));
8937 }
8938
8939
8940 if (&connector->new_encoder->base != connector->base.encoder) {
Daniel Vetter50f56112012-07-02 09:35:43 +02008941 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008942 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02008943 }
Daniel Vetter9a935852012-07-05 22:34:27 +02008944 }
8945 /* connector->new_encoder is now updated for all connectors. */
8946
8947 /* Update crtc of enabled connectors. */
Daniel Vetter50f56112012-07-02 09:35:43 +02008948 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008949 list_for_each_entry(connector, &dev->mode_config.connector_list,
8950 base.head) {
8951 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +02008952 continue;
8953
Daniel Vetter9a935852012-07-05 22:34:27 +02008954 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +02008955
8956 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02008957 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +02008958 new_crtc = set->crtc;
8959 }
8960
8961 /* Make sure the new CRTC will work with the encoder */
Daniel Vetter9a935852012-07-05 22:34:27 +02008962 if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
8963 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008964 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +02008965 }
Daniel Vetter9a935852012-07-05 22:34:27 +02008966 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
8967
8968 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
8969 connector->base.base.id,
8970 drm_get_connector_name(&connector->base),
8971 new_crtc->base.id);
8972 }
8973
8974 /* Check for any encoders that needs to be disabled. */
8975 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8976 base.head) {
8977 list_for_each_entry(connector,
8978 &dev->mode_config.connector_list,
8979 base.head) {
8980 if (connector->new_encoder == encoder) {
8981 WARN_ON(!connector->new_encoder->new_crtc);
8982
8983 goto next_encoder;
8984 }
8985 }
8986 encoder->new_crtc = NULL;
8987next_encoder:
8988 /* Only now check for crtc changes so we don't miss encoders
8989 * that will be disabled. */
8990 if (&encoder->new_crtc->base != encoder->base.crtc) {
Daniel Vetter50f56112012-07-02 09:35:43 +02008991 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008992 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02008993 }
8994 }
Daniel Vetter9a935852012-07-05 22:34:27 +02008995 /* Now we've also updated encoder->new_crtc for all encoders. */
Daniel Vetter50f56112012-07-02 09:35:43 +02008996
Daniel Vetter2e431052012-07-04 22:42:15 +02008997 return 0;
8998}
8999
9000static int intel_crtc_set_config(struct drm_mode_set *set)
9001{
9002 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +02009003 struct drm_mode_set save_set;
9004 struct intel_set_config *config;
9005 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +02009006
Daniel Vetter8d3e3752012-07-05 16:09:09 +02009007 BUG_ON(!set);
9008 BUG_ON(!set->crtc);
9009 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +02009010
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01009011 /* Enforce sane interface api - has been abused by the fb helper. */
9012 BUG_ON(!set->mode && set->fb);
9013 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +02009014
Daniel Vetter2e431052012-07-04 22:42:15 +02009015 if (set->fb) {
9016 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
9017 set->crtc->base.id, set->fb->base.id,
9018 (int)set->num_connectors, set->x, set->y);
9019 } else {
9020 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +02009021 }
9022
9023 dev = set->crtc->dev;
9024
9025 ret = -ENOMEM;
9026 config = kzalloc(sizeof(*config), GFP_KERNEL);
9027 if (!config)
9028 goto out_config;
9029
9030 ret = intel_set_config_save_state(dev, config);
9031 if (ret)
9032 goto out_config;
9033
9034 save_set.crtc = set->crtc;
9035 save_set.mode = &set->crtc->mode;
9036 save_set.x = set->crtc->x;
9037 save_set.y = set->crtc->y;
9038 save_set.fb = set->crtc->fb;
9039
9040 /* Compute whether we need a full modeset, only an fb base update or no
9041 * change at all. In the future we might also check whether only the
9042 * mode changed, e.g. for LVDS where we only change the panel fitter in
9043 * such cases. */
9044 intel_set_config_compute_mode_changes(set, config);
9045
Daniel Vetter9a935852012-07-05 22:34:27 +02009046 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +02009047 if (ret)
9048 goto fail;
9049
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009050 if (config->mode_changed) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009051 ret = intel_set_mode(set->crtc, set->mode,
9052 set->x, set->y, set->fb);
Daniel Vetter5e2b5842012-07-04 22:41:29 +02009053 } else if (config->fb_changed) {
Ville Syrjälä4878cae2013-02-18 19:08:48 +02009054 intel_crtc_wait_for_pending_flips(set->crtc);
9055
Daniel Vetter4f660f42012-07-02 09:47:37 +02009056 ret = intel_pipe_set_base(set->crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +02009057 set->x, set->y, set->fb);
Daniel Vetter50f56112012-07-02 09:35:43 +02009058 }
9059
Chris Wilson2d05eae2013-05-03 17:36:25 +01009060 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +02009061 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
9062 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +02009063fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +01009064 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +02009065
Chris Wilson2d05eae2013-05-03 17:36:25 +01009066 /* Try to restore the config */
9067 if (config->mode_changed &&
9068 intel_set_mode(save_set.crtc, save_set.mode,
9069 save_set.x, save_set.y, save_set.fb))
9070 DRM_ERROR("failed to restore config after modeset failure\n");
9071 }
Daniel Vetter50f56112012-07-02 09:35:43 +02009072
Daniel Vetterd9e55602012-07-04 22:16:09 +02009073out_config:
9074 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +02009075 return ret;
9076}
9077
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009078static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009079 .cursor_set = intel_crtc_cursor_set,
9080 .cursor_move = intel_crtc_cursor_move,
9081 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +02009082 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009083 .destroy = intel_crtc_destroy,
9084 .page_flip = intel_crtc_page_flip,
9085};
9086
Paulo Zanoni79f689a2012-10-05 12:05:52 -03009087static void intel_cpu_pll_init(struct drm_device *dev)
9088{
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009089 if (HAS_DDI(dev))
Paulo Zanoni79f689a2012-10-05 12:05:52 -03009090 intel_ddi_pll_init(dev);
9091}
9092
Daniel Vetter53589012013-06-05 13:34:16 +02009093static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
9094 struct intel_shared_dpll *pll,
9095 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009096{
Daniel Vetter53589012013-06-05 13:34:16 +02009097 uint32_t val;
9098
9099 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +02009100 hw_state->dpll = val;
9101 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
9102 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +02009103
9104 return val & DPLL_VCO_ENABLE;
9105}
9106
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02009107static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
9108 struct intel_shared_dpll *pll)
9109{
9110 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
9111 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
9112}
9113
Daniel Vettere7b903d2013-06-05 13:34:14 +02009114static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
9115 struct intel_shared_dpll *pll)
9116{
Daniel Vettere7b903d2013-06-05 13:34:14 +02009117 /* PCH refclock must be enabled first */
9118 assert_pch_refclk_enabled(dev_priv);
9119
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02009120 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
9121
9122 /* Wait for the clocks to stabilize. */
9123 POSTING_READ(PCH_DPLL(pll->id));
9124 udelay(150);
9125
9126 /* The pixel multiplier can only be updated once the
9127 * DPLL is enabled and the clocks are stable.
9128 *
9129 * So write it again.
9130 */
9131 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
9132 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +02009133 udelay(200);
9134}
9135
9136static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
9137 struct intel_shared_dpll *pll)
9138{
9139 struct drm_device *dev = dev_priv->dev;
9140 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +02009141
9142 /* Make sure no transcoder isn't still depending on us. */
9143 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
9144 if (intel_crtc_to_shared_dpll(crtc) == pll)
9145 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
9146 }
9147
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02009148 I915_WRITE(PCH_DPLL(pll->id), 0);
9149 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +02009150 udelay(200);
9151}
9152
Daniel Vetter46edb022013-06-05 13:34:12 +02009153static char *ibx_pch_dpll_names[] = {
9154 "PCH DPLL A",
9155 "PCH DPLL B",
9156};
9157
Daniel Vetter7c74ade2013-06-05 13:34:11 +02009158static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009159{
Daniel Vettere7b903d2013-06-05 13:34:14 +02009160 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009161 int i;
9162
Daniel Vetter7c74ade2013-06-05 13:34:11 +02009163 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009164
Daniel Vettere72f9fb2013-06-05 13:34:06 +02009165 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +02009166 dev_priv->shared_dplls[i].id = i;
9167 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02009168 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +02009169 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
9170 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +02009171 dev_priv->shared_dplls[i].get_hw_state =
9172 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009173 }
9174}
9175
Daniel Vetter7c74ade2013-06-05 13:34:11 +02009176static void intel_shared_dpll_init(struct drm_device *dev)
9177{
Daniel Vettere7b903d2013-06-05 13:34:14 +02009178 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +02009179
9180 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
9181 ibx_pch_dpll_init(dev);
9182 else
9183 dev_priv->num_shared_dpll = 0;
9184
9185 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
9186 DRM_DEBUG_KMS("%i shared PLLs initialized\n",
9187 dev_priv->num_shared_dpll);
9188}
9189
Hannes Ederb358d0a2008-12-18 21:18:47 +01009190static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -08009191{
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08009192 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08009193 struct intel_crtc *intel_crtc;
9194 int i;
9195
9196 intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
9197 if (intel_crtc == NULL)
9198 return;
9199
9200 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
9201
9202 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -08009203 for (i = 0; i < 256; i++) {
9204 intel_crtc->lut_r[i] = i;
9205 intel_crtc->lut_g[i] = i;
9206 intel_crtc->lut_b[i] = i;
9207 }
9208
Jesse Barnes80824002009-09-10 15:28:06 -07009209 /* Swap pipes & planes for FBC on pre-965 */
9210 intel_crtc->pipe = pipe;
9211 intel_crtc->plane = pipe;
Chris Wilsone2e767a2010-09-13 16:53:12 +01009212 if (IS_MOBILE(dev) && IS_GEN3(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08009213 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +01009214 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07009215 }
9216
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08009217 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
9218 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
9219 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
9220 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
9221
Jesse Barnes79e53942008-11-07 14:24:08 -08009222 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -08009223}
9224
Carl Worth08d7b3d2009-04-29 14:43:54 -07009225int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00009226 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -07009227{
Carl Worth08d7b3d2009-04-29 14:43:54 -07009228 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +02009229 struct drm_mode_object *drmmode_obj;
9230 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009231
Daniel Vetter1cff8f62012-04-24 09:55:08 +02009232 if (!drm_core_check_feature(dev, DRIVER_MODESET))
9233 return -ENODEV;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009234
Daniel Vetterc05422d2009-08-11 16:05:30 +02009235 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
9236 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -07009237
Daniel Vetterc05422d2009-08-11 16:05:30 +02009238 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -07009239 DRM_ERROR("no such CRTC id\n");
9240 return -EINVAL;
9241 }
9242
Daniel Vetterc05422d2009-08-11 16:05:30 +02009243 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
9244 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009245
Daniel Vetterc05422d2009-08-11 16:05:30 +02009246 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009247}
9248
Daniel Vetter66a92782012-07-12 20:08:18 +02009249static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08009250{
Daniel Vetter66a92782012-07-12 20:08:18 +02009251 struct drm_device *dev = encoder->base.dev;
9252 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08009253 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009254 int entry = 0;
9255
Daniel Vetter66a92782012-07-12 20:08:18 +02009256 list_for_each_entry(source_encoder,
9257 &dev->mode_config.encoder_list, base.head) {
9258
9259 if (encoder == source_encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08009260 index_mask |= (1 << entry);
Daniel Vetter66a92782012-07-12 20:08:18 +02009261
9262 /* Intel hw has only one MUX where enocoders could be cloned. */
9263 if (encoder->cloneable && source_encoder->cloneable)
9264 index_mask |= (1 << entry);
9265
Jesse Barnes79e53942008-11-07 14:24:08 -08009266 entry++;
9267 }
Chris Wilson4ef69c72010-09-09 15:14:28 +01009268
Jesse Barnes79e53942008-11-07 14:24:08 -08009269 return index_mask;
9270}
9271
Chris Wilson4d302442010-12-14 19:21:29 +00009272static bool has_edp_a(struct drm_device *dev)
9273{
9274 struct drm_i915_private *dev_priv = dev->dev_private;
9275
9276 if (!IS_MOBILE(dev))
9277 return false;
9278
9279 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
9280 return false;
9281
9282 if (IS_GEN5(dev) &&
9283 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
9284 return false;
9285
9286 return true;
9287}
9288
Jesse Barnes79e53942008-11-07 14:24:08 -08009289static void intel_setup_outputs(struct drm_device *dev)
9290{
Eric Anholt725e30a2009-01-22 13:01:02 -08009291 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +01009292 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009293 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08009294
Daniel Vetterc9093352013-06-06 22:22:47 +02009295 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009296
Paulo Zanonic40c0f52013-04-12 18:16:53 -03009297 if (!IS_ULT(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -02009298 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009299
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009300 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -03009301 int found;
9302
9303 /* Haswell uses DDI functions to detect digital outputs */
9304 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
9305 /* DDI A only supports eDP */
9306 if (found)
9307 intel_ddi_init(dev, PORT_A);
9308
9309 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
9310 * register */
9311 found = I915_READ(SFUSE_STRAP);
9312
9313 if (found & SFUSE_STRAP_DDIB_DETECTED)
9314 intel_ddi_init(dev, PORT_B);
9315 if (found & SFUSE_STRAP_DDIC_DETECTED)
9316 intel_ddi_init(dev, PORT_C);
9317 if (found & SFUSE_STRAP_DDID_DETECTED)
9318 intel_ddi_init(dev, PORT_D);
9319 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009320 int found;
Daniel Vetter270b3042012-10-27 15:52:05 +02009321 dpd_is_edp = intel_dpd_is_edp(dev);
9322
9323 if (has_edp_a(dev))
9324 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009325
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009326 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +08009327 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +01009328 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009329 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -03009330 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08009331 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009332 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009333 }
9334
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009335 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -03009336 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009337
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009338 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -03009339 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009340
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08009341 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009342 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08009343
Daniel Vetter270b3042012-10-27 15:52:05 +02009344 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009345 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -07009346 } else if (IS_VALLEYVIEW(dev)) {
Gajanan Bhat19c03922012-09-27 19:13:07 +05309347 /* Check for built-in panel first. Shares lanes with HDMI on SDVOC */
Ville Syrjälä67cfc202013-01-25 21:44:44 +02009348 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
9349 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +05309350
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009351 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
Paulo Zanonie2debe92013-02-18 19:00:27 -03009352 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
9353 PORT_B);
Ville Syrjälä67cfc202013-01-25 21:44:44 +02009354 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
9355 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Jesse Barnes4a87d652012-06-15 11:55:16 -07009356 }
Zhenyu Wang103a1962009-11-27 11:44:36 +08009357 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +08009358 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -08009359
Paulo Zanonie2debe92013-02-18 19:00:27 -03009360 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009361 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009362 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009363 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
9364 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009365 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009366 }
Ma Ling27185ae2009-08-24 13:50:23 +08009367
Imre Deake7281ea2013-05-08 13:14:08 +03009368 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009369 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -08009370 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -04009371
9372 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -04009373
Paulo Zanonie2debe92013-02-18 19:00:27 -03009374 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009375 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009376 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009377 }
Ma Ling27185ae2009-08-24 13:50:23 +08009378
Paulo Zanonie2debe92013-02-18 19:00:27 -03009379 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +08009380
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009381 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
9382 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009383 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009384 }
Imre Deake7281ea2013-05-08 13:14:08 +03009385 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009386 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -08009387 }
Ma Ling27185ae2009-08-24 13:50:23 +08009388
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009389 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +03009390 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009391 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -07009392 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08009393 intel_dvo_init(dev);
9394
Zhenyu Wang103a1962009-11-27 11:44:36 +08009395 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08009396 intel_tv_init(dev);
9397
Chris Wilson4ef69c72010-09-09 15:14:28 +01009398 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
9399 encoder->base.possible_crtcs = encoder->crtc_mask;
9400 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +02009401 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -08009402 }
Chris Wilson47356eb2011-01-11 17:06:04 +00009403
Paulo Zanonidde86e22012-12-01 12:04:25 -02009404 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +02009405
9406 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009407}
9408
9409static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
9410{
9411 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08009412
9413 drm_framebuffer_cleanup(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00009414 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08009415
9416 kfree(intel_fb);
9417}
9418
9419static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +00009420 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08009421 unsigned int *handle)
9422{
9423 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00009424 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08009425
Chris Wilson05394f32010-11-08 19:18:58 +00009426 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -08009427}
9428
9429static const struct drm_framebuffer_funcs intel_fb_funcs = {
9430 .destroy = intel_user_framebuffer_destroy,
9431 .create_handle = intel_user_framebuffer_create_handle,
9432};
9433
Dave Airlie38651672010-03-30 05:34:13 +00009434int intel_framebuffer_init(struct drm_device *dev,
9435 struct intel_framebuffer *intel_fb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009436 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilson05394f32010-11-08 19:18:58 +00009437 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -08009438{
Chris Wilsona35cdaa2013-06-25 17:26:45 +01009439 int pitch_limit;
Jesse Barnes79e53942008-11-07 14:24:08 -08009440 int ret;
9441
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009442 if (obj->tiling_mode == I915_TILING_Y) {
9443 DRM_DEBUG("hardware does not support tiling Y\n");
Chris Wilson57cd6502010-08-08 12:34:44 +01009444 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009445 }
Chris Wilson57cd6502010-08-08 12:34:44 +01009446
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009447 if (mode_cmd->pitches[0] & 63) {
9448 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
9449 mode_cmd->pitches[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +01009450 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009451 }
Chris Wilson57cd6502010-08-08 12:34:44 +01009452
Chris Wilsona35cdaa2013-06-25 17:26:45 +01009453 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
9454 pitch_limit = 32*1024;
9455 } else if (INTEL_INFO(dev)->gen >= 4) {
9456 if (obj->tiling_mode)
9457 pitch_limit = 16*1024;
9458 else
9459 pitch_limit = 32*1024;
9460 } else if (INTEL_INFO(dev)->gen >= 3) {
9461 if (obj->tiling_mode)
9462 pitch_limit = 8*1024;
9463 else
9464 pitch_limit = 16*1024;
9465 } else
9466 /* XXX DSPC is limited to 4k tiled */
9467 pitch_limit = 8*1024;
9468
9469 if (mode_cmd->pitches[0] > pitch_limit) {
9470 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
9471 obj->tiling_mode ? "tiled" : "linear",
9472 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02009473 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009474 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02009475
9476 if (obj->tiling_mode != I915_TILING_NONE &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009477 mode_cmd->pitches[0] != obj->stride) {
9478 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
9479 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02009480 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009481 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02009482
Ville Syrjälä57779d02012-10-31 17:50:14 +02009483 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009484 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +02009485 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02009486 case DRM_FORMAT_RGB565:
9487 case DRM_FORMAT_XRGB8888:
9488 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02009489 break;
9490 case DRM_FORMAT_XRGB1555:
9491 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009492 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +00009493 DRM_DEBUG("unsupported pixel format: %s\n",
9494 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +02009495 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009496 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02009497 break;
9498 case DRM_FORMAT_XBGR8888:
9499 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +02009500 case DRM_FORMAT_XRGB2101010:
9501 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02009502 case DRM_FORMAT_XBGR2101010:
9503 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009504 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +00009505 DRM_DEBUG("unsupported pixel format: %s\n",
9506 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +02009507 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009508 }
Jesse Barnesb5626742011-06-24 12:19:27 -07009509 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02009510 case DRM_FORMAT_YUYV:
9511 case DRM_FORMAT_UYVY:
9512 case DRM_FORMAT_YVYU:
9513 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009514 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +00009515 DRM_DEBUG("unsupported pixel format: %s\n",
9516 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +02009517 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009518 }
Chris Wilson57cd6502010-08-08 12:34:44 +01009519 break;
9520 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +00009521 DRM_DEBUG("unsupported pixel format: %s\n",
9522 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +01009523 return -EINVAL;
9524 }
9525
Ville Syrjälä90f9a332012-10-31 17:50:19 +02009526 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
9527 if (mode_cmd->offsets[0] != 0)
9528 return -EINVAL;
9529
Daniel Vetterc7d73f62012-12-13 23:38:38 +01009530 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
9531 intel_fb->obj = obj;
9532
Jesse Barnes79e53942008-11-07 14:24:08 -08009533 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
9534 if (ret) {
9535 DRM_ERROR("framebuffer init failed %d\n", ret);
9536 return ret;
9537 }
9538
Jesse Barnes79e53942008-11-07 14:24:08 -08009539 return 0;
9540}
9541
Jesse Barnes79e53942008-11-07 14:24:08 -08009542static struct drm_framebuffer *
9543intel_user_framebuffer_create(struct drm_device *dev,
9544 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009545 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -08009546{
Chris Wilson05394f32010-11-08 19:18:58 +00009547 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08009548
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009549 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
9550 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +00009551 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +01009552 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -08009553
Chris Wilsond2dff872011-04-19 08:36:26 +01009554 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08009555}
9556
Jesse Barnes79e53942008-11-07 14:24:08 -08009557static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -08009558 .fb_create = intel_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +00009559 .output_poll_changed = intel_fb_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -08009560};
9561
Jesse Barnese70236a2009-09-21 10:42:27 -07009562/* Set up chip specific display functions */
9563static void intel_init_display(struct drm_device *dev)
9564{
9565 struct drm_i915_private *dev_priv = dev->dev_private;
9566
Daniel Vetteree9300b2013-06-03 22:40:22 +02009567 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
9568 dev_priv->display.find_dpll = g4x_find_best_dpll;
9569 else if (IS_VALLEYVIEW(dev))
9570 dev_priv->display.find_dpll = vlv_find_best_dpll;
9571 else if (IS_PINEVIEW(dev))
9572 dev_priv->display.find_dpll = pnv_find_best_dpll;
9573 else
9574 dev_priv->display.find_dpll = i9xx_find_best_dpll;
9575
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009576 if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009577 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009578 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02009579 dev_priv->display.crtc_enable = haswell_crtc_enable;
9580 dev_priv->display.crtc_disable = haswell_crtc_disable;
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009581 dev_priv->display.off = haswell_crtc_off;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009582 dev_priv->display.update_plane = ironlake_update_plane;
9583 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009584 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009585 dev_priv->display.get_clock = ironlake_crtc_clock_get;
Eric Anholtf564048e2011-03-30 13:01:02 -07009586 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +02009587 dev_priv->display.crtc_enable = ironlake_crtc_enable;
9588 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009589 dev_priv->display.off = ironlake_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -07009590 dev_priv->display.update_plane = ironlake_update_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -07009591 } else if (IS_VALLEYVIEW(dev)) {
9592 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009593 dev_priv->display.get_clock = i9xx_crtc_clock_get;
Jesse Barnes89b667f2013-04-18 14:51:36 -07009594 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
9595 dev_priv->display.crtc_enable = valleyview_crtc_enable;
9596 dev_priv->display.crtc_disable = i9xx_crtc_disable;
9597 dev_priv->display.off = i9xx_crtc_off;
9598 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07009599 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009600 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009601 dev_priv->display.get_clock = i9xx_crtc_clock_get;
Eric Anholtf564048e2011-03-30 13:01:02 -07009602 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +02009603 dev_priv->display.crtc_enable = i9xx_crtc_enable;
9604 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009605 dev_priv->display.off = i9xx_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -07009606 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07009607 }
Jesse Barnese70236a2009-09-21 10:42:27 -07009608
Jesse Barnese70236a2009-09-21 10:42:27 -07009609 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07009610 if (IS_VALLEYVIEW(dev))
9611 dev_priv->display.get_display_clock_speed =
9612 valleyview_get_display_clock_speed;
9613 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -07009614 dev_priv->display.get_display_clock_speed =
9615 i945_get_display_clock_speed;
9616 else if (IS_I915G(dev))
9617 dev_priv->display.get_display_clock_speed =
9618 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02009619 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07009620 dev_priv->display.get_display_clock_speed =
9621 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02009622 else if (IS_PINEVIEW(dev))
9623 dev_priv->display.get_display_clock_speed =
9624 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -07009625 else if (IS_I915GM(dev))
9626 dev_priv->display.get_display_clock_speed =
9627 i915gm_get_display_clock_speed;
9628 else if (IS_I865G(dev))
9629 dev_priv->display.get_display_clock_speed =
9630 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +02009631 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07009632 dev_priv->display.get_display_clock_speed =
9633 i855_get_display_clock_speed;
9634 else /* 852, 830 */
9635 dev_priv->display.get_display_clock_speed =
9636 i830_get_display_clock_speed;
9637
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08009638 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01009639 if (IS_GEN5(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -07009640 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08009641 dev_priv->display.write_eld = ironlake_write_eld;
Yuanhan Liu13982612010-12-15 15:42:31 +08009642 } else if (IS_GEN6(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -07009643 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08009644 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnes357555c2011-04-28 15:09:55 -07009645 } else if (IS_IVYBRIDGE(dev)) {
9646 /* FIXME: detect B0+ stepping and use auto training */
9647 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08009648 dev_priv->display.write_eld = ironlake_write_eld;
Daniel Vetter01a415f2012-10-27 15:58:40 +02009649 dev_priv->display.modeset_global_resources =
9650 ivb_modeset_global_resources;
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -03009651 } else if (IS_HASWELL(dev)) {
9652 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Wang Xingchao83358c852012-08-16 22:43:37 +08009653 dev_priv->display.write_eld = haswell_write_eld;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02009654 dev_priv->display.modeset_global_resources =
9655 haswell_modeset_global_resources;
Paulo Zanonia0e63c22012-12-06 11:12:39 -02009656 }
Jesse Barnes6067aae2011-04-28 15:04:31 -07009657 } else if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +08009658 dev_priv->display.write_eld = g4x_write_eld;
Jesse Barnese70236a2009-09-21 10:42:27 -07009659 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009660
9661 /* Default just returns -ENODEV to indicate unsupported */
9662 dev_priv->display.queue_flip = intel_default_queue_flip;
9663
9664 switch (INTEL_INFO(dev)->gen) {
9665 case 2:
9666 dev_priv->display.queue_flip = intel_gen2_queue_flip;
9667 break;
9668
9669 case 3:
9670 dev_priv->display.queue_flip = intel_gen3_queue_flip;
9671 break;
9672
9673 case 4:
9674 case 5:
9675 dev_priv->display.queue_flip = intel_gen4_queue_flip;
9676 break;
9677
9678 case 6:
9679 dev_priv->display.queue_flip = intel_gen6_queue_flip;
9680 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009681 case 7:
9682 dev_priv->display.queue_flip = intel_gen7_queue_flip;
9683 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009684 }
Jesse Barnese70236a2009-09-21 10:42:27 -07009685}
9686
Jesse Barnesb690e962010-07-19 13:53:12 -07009687/*
9688 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
9689 * resume, or other times. This quirk makes sure that's the case for
9690 * affected systems.
9691 */
Akshay Joshi0206e352011-08-16 15:34:10 -04009692static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -07009693{
9694 struct drm_i915_private *dev_priv = dev->dev_private;
9695
9696 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02009697 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -07009698}
9699
Keith Packard435793d2011-07-12 14:56:22 -07009700/*
9701 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
9702 */
9703static void quirk_ssc_force_disable(struct drm_device *dev)
9704{
9705 struct drm_i915_private *dev_priv = dev->dev_private;
9706 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02009707 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -07009708}
9709
Carsten Emde4dca20e2012-03-15 15:56:26 +01009710/*
Carsten Emde5a15ab52012-03-15 15:56:27 +01009711 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
9712 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +01009713 */
9714static void quirk_invert_brightness(struct drm_device *dev)
9715{
9716 struct drm_i915_private *dev_priv = dev->dev_private;
9717 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02009718 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -07009719}
9720
Kamal Mostafae85843b2013-07-19 15:02:01 -07009721/*
9722 * Some machines (Dell XPS13) suffer broken backlight controls if
9723 * BLM_PCH_PWM_ENABLE is set.
9724 */
9725static void quirk_no_pcm_pwm_enable(struct drm_device *dev)
9726{
9727 struct drm_i915_private *dev_priv = dev->dev_private;
9728 dev_priv->quirks |= QUIRK_NO_PCH_PWM_ENABLE;
9729 DRM_INFO("applying no-PCH_PWM_ENABLE quirk\n");
9730}
9731
Jesse Barnesb690e962010-07-19 13:53:12 -07009732struct intel_quirk {
9733 int device;
9734 int subsystem_vendor;
9735 int subsystem_device;
9736 void (*hook)(struct drm_device *dev);
9737};
9738
Egbert Eich5f85f1762012-10-14 15:46:38 +02009739/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
9740struct intel_dmi_quirk {
9741 void (*hook)(struct drm_device *dev);
9742 const struct dmi_system_id (*dmi_id_list)[];
9743};
9744
9745static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
9746{
9747 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
9748 return 1;
9749}
9750
9751static const struct intel_dmi_quirk intel_dmi_quirks[] = {
9752 {
9753 .dmi_id_list = &(const struct dmi_system_id[]) {
9754 {
9755 .callback = intel_dmi_reverse_brightness,
9756 .ident = "NCR Corporation",
9757 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
9758 DMI_MATCH(DMI_PRODUCT_NAME, ""),
9759 },
9760 },
9761 { } /* terminating entry */
9762 },
9763 .hook = quirk_invert_brightness,
9764 },
9765};
9766
Ben Widawskyc43b5632012-04-16 14:07:40 -07009767static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -07009768 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -04009769 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -07009770
Jesse Barnesb690e962010-07-19 13:53:12 -07009771 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
9772 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
9773
Jesse Barnesb690e962010-07-19 13:53:12 -07009774 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
9775 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
9776
Daniel Vetterccd0d362012-10-10 23:13:59 +02009777 /* 830/845 need to leave pipe A & dpll A up */
Jesse Barnesb690e962010-07-19 13:53:12 -07009778 { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Daniel Vetterdcdaed62012-08-12 21:19:34 +02009779 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Keith Packard435793d2011-07-12 14:56:22 -07009780
9781 /* Lenovo U160 cannot use SSC on LVDS */
9782 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +02009783
9784 /* Sony Vaio Y cannot use SSC on LVDS */
9785 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +01009786
9787 /* Acer Aspire 5734Z must invert backlight brightness */
9788 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
Jani Nikula1ffff602013-01-22 12:50:34 +02009789
9790 /* Acer/eMachines G725 */
9791 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
Jani Nikula01e3a8f2013-01-22 12:50:35 +02009792
9793 /* Acer/eMachines e725 */
9794 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
Jani Nikula5559eca2013-01-22 12:50:36 +02009795
9796 /* Acer/Packard Bell NCL20 */
9797 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
Daniel Vetterac4199e2013-02-15 18:35:30 +01009798
9799 /* Acer Aspire 4736Z */
9800 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Kamal Mostafae85843b2013-07-19 15:02:01 -07009801
9802 /* Dell XPS13 HD Sandy Bridge */
9803 { 0x0116, 0x1028, 0x052e, quirk_no_pcm_pwm_enable },
9804 /* Dell XPS13 HD and XPS13 FHD Ivy Bridge */
9805 { 0x0166, 0x1028, 0x058b, quirk_no_pcm_pwm_enable },
Jesse Barnesb690e962010-07-19 13:53:12 -07009806};
9807
9808static void intel_init_quirks(struct drm_device *dev)
9809{
9810 struct pci_dev *d = dev->pdev;
9811 int i;
9812
9813 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
9814 struct intel_quirk *q = &intel_quirks[i];
9815
9816 if (d->device == q->device &&
9817 (d->subsystem_vendor == q->subsystem_vendor ||
9818 q->subsystem_vendor == PCI_ANY_ID) &&
9819 (d->subsystem_device == q->subsystem_device ||
9820 q->subsystem_device == PCI_ANY_ID))
9821 q->hook(dev);
9822 }
Egbert Eich5f85f1762012-10-14 15:46:38 +02009823 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
9824 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
9825 intel_dmi_quirks[i].hook(dev);
9826 }
Jesse Barnesb690e962010-07-19 13:53:12 -07009827}
9828
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009829/* Disable the VGA plane that we never use */
9830static void i915_disable_vga(struct drm_device *dev)
9831{
9832 struct drm_i915_private *dev_priv = dev->dev_private;
9833 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +02009834 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009835
9836 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -07009837 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009838 sr1 = inb(VGA_SR_DATA);
9839 outb(sr1 | 1<<5, VGA_SR_DATA);
9840 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
9841 udelay(300);
9842
9843 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9844 POSTING_READ(vga_reg);
9845}
9846
Daniel Vetterf8175862012-04-10 15:50:11 +02009847void intel_modeset_init_hw(struct drm_device *dev)
9848{
Paulo Zanonifa42e232013-01-25 16:59:11 -02009849 intel_init_power_well(dev);
Eugeni Dodonov0232e922012-07-06 15:42:36 -03009850
Eugeni Dodonova8f78b52012-06-28 15:55:35 -03009851 intel_prepare_ddi(dev);
9852
Daniel Vetterf8175862012-04-10 15:50:11 +02009853 intel_init_clock_gating(dev);
9854
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02009855 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02009856 intel_enable_gt_powersave(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02009857 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf8175862012-04-10 15:50:11 +02009858}
9859
Imre Deak7d708ee2013-04-17 14:04:50 +03009860void intel_modeset_suspend_hw(struct drm_device *dev)
9861{
9862 intel_suspend_hw(dev);
9863}
9864
Jesse Barnes79e53942008-11-07 14:24:08 -08009865void intel_modeset_init(struct drm_device *dev)
9866{
Jesse Barnes652c3932009-08-17 13:31:43 -07009867 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7f1f3852013-04-02 11:22:20 -07009868 int i, j, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08009869
9870 drm_mode_config_init(dev);
9871
9872 dev->mode_config.min_width = 0;
9873 dev->mode_config.min_height = 0;
9874
Dave Airlie019d96c2011-09-29 16:20:42 +01009875 dev->mode_config.preferred_depth = 24;
9876 dev->mode_config.prefer_shadow = 1;
9877
Laurent Pincharte6ecefa2012-05-17 13:27:23 +02009878 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -08009879
Jesse Barnesb690e962010-07-19 13:53:12 -07009880 intel_init_quirks(dev);
9881
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009882 intel_init_pm(dev);
9883
Ben Widawskye3c74752013-04-05 13:12:39 -07009884 if (INTEL_INFO(dev)->num_pipes == 0)
9885 return;
9886
Jesse Barnese70236a2009-09-21 10:42:27 -07009887 intel_init_display(dev);
9888
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009889 if (IS_GEN2(dev)) {
9890 dev->mode_config.max_width = 2048;
9891 dev->mode_config.max_height = 2048;
9892 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -07009893 dev->mode_config.max_width = 4096;
9894 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -08009895 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009896 dev->mode_config.max_width = 8192;
9897 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -08009898 }
Ben Widawsky5d4545a2013-01-17 12:45:15 -08009899 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -08009900
Zhao Yakui28c97732009-10-09 11:39:41 +08009901 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -07009902 INTEL_INFO(dev)->num_pipes,
9903 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -08009904
Damien Lespiau08e2a7d2013-07-11 20:10:54 +01009905 for_each_pipe(i) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009906 intel_crtc_init(dev, i);
Jesse Barnes7f1f3852013-04-02 11:22:20 -07009907 for (j = 0; j < dev_priv->num_plane; j++) {
9908 ret = intel_plane_init(dev, i, j);
9909 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +03009910 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
9911 pipe_name(i), sprite_name(i, j), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -07009912 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009913 }
9914
Paulo Zanoni79f689a2012-10-05 12:05:52 -03009915 intel_cpu_pll_init(dev);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02009916 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009917
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009918 /* Just disable it once at startup */
9919 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009920 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +00009921
9922 /* Just in case the BIOS is doing something questionable. */
9923 intel_disable_fbc(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01009924}
Jesse Barnesd5bb0812011-01-05 12:01:26 -08009925
Daniel Vetter24929352012-07-02 20:28:59 +02009926static void
9927intel_connector_break_all_links(struct intel_connector *connector)
9928{
9929 connector->base.dpms = DRM_MODE_DPMS_OFF;
9930 connector->base.encoder = NULL;
9931 connector->encoder->connectors_active = false;
9932 connector->encoder->base.crtc = NULL;
9933}
9934
Daniel Vetter7fad7982012-07-04 17:51:47 +02009935static void intel_enable_pipe_a(struct drm_device *dev)
9936{
9937 struct intel_connector *connector;
9938 struct drm_connector *crt = NULL;
9939 struct intel_load_detect_pipe load_detect_temp;
9940
9941 /* We can't just switch on the pipe A, we need to set things up with a
9942 * proper mode and output configuration. As a gross hack, enable pipe A
9943 * by enabling the load detect pipe once. */
9944 list_for_each_entry(connector,
9945 &dev->mode_config.connector_list,
9946 base.head) {
9947 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
9948 crt = &connector->base;
9949 break;
9950 }
9951 }
9952
9953 if (!crt)
9954 return;
9955
9956 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
9957 intel_release_load_detect_pipe(crt, &load_detect_temp);
9958
9959
9960}
9961
Daniel Vetterfa555832012-10-10 23:14:00 +02009962static bool
9963intel_check_plane_mapping(struct intel_crtc *crtc)
9964{
Ben Widawsky7eb552a2013-03-13 14:05:41 -07009965 struct drm_device *dev = crtc->base.dev;
9966 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +02009967 u32 reg, val;
9968
Ben Widawsky7eb552a2013-03-13 14:05:41 -07009969 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +02009970 return true;
9971
9972 reg = DSPCNTR(!crtc->plane);
9973 val = I915_READ(reg);
9974
9975 if ((val & DISPLAY_PLANE_ENABLE) &&
9976 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
9977 return false;
9978
9979 return true;
9980}
9981
Daniel Vetter24929352012-07-02 20:28:59 +02009982static void intel_sanitize_crtc(struct intel_crtc *crtc)
9983{
9984 struct drm_device *dev = crtc->base.dev;
9985 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +02009986 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +02009987
Daniel Vetter24929352012-07-02 20:28:59 +02009988 /* Clear any frame start delays used for debugging left by the BIOS */
Daniel Vetter3b117c82013-04-17 20:15:07 +02009989 reg = PIPECONF(crtc->config.cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +02009990 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
9991
9992 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +02009993 * disable the crtc (and hence change the state) if it is wrong. Note
9994 * that gen4+ has a fixed plane -> pipe mapping. */
9995 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +02009996 struct intel_connector *connector;
9997 bool plane;
9998
Daniel Vetter24929352012-07-02 20:28:59 +02009999 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
10000 crtc->base.base.id);
10001
10002 /* Pipe has the wrong plane attached and the plane is active.
10003 * Temporarily change the plane mapping and disable everything
10004 * ... */
10005 plane = crtc->plane;
10006 crtc->plane = !plane;
10007 dev_priv->display.crtc_disable(&crtc->base);
10008 crtc->plane = plane;
10009
10010 /* ... and break all links. */
10011 list_for_each_entry(connector, &dev->mode_config.connector_list,
10012 base.head) {
10013 if (connector->encoder->base.crtc != &crtc->base)
10014 continue;
10015
10016 intel_connector_break_all_links(connector);
10017 }
10018
10019 WARN_ON(crtc->active);
10020 crtc->base.enabled = false;
10021 }
Daniel Vetter24929352012-07-02 20:28:59 +020010022
Daniel Vetter7fad7982012-07-04 17:51:47 +020010023 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
10024 crtc->pipe == PIPE_A && !crtc->active) {
10025 /* BIOS forgot to enable pipe A, this mostly happens after
10026 * resume. Force-enable the pipe to fix this, the update_dpms
10027 * call below we restore the pipe to the right state, but leave
10028 * the required bits on. */
10029 intel_enable_pipe_a(dev);
10030 }
10031
Daniel Vetter24929352012-07-02 20:28:59 +020010032 /* Adjust the state of the output pipe according to whether we
10033 * have active connectors/encoders. */
10034 intel_crtc_update_dpms(&crtc->base);
10035
10036 if (crtc->active != crtc->base.enabled) {
10037 struct intel_encoder *encoder;
10038
10039 /* This can happen either due to bugs in the get_hw_state
10040 * functions or because the pipe is force-enabled due to the
10041 * pipe A quirk. */
10042 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
10043 crtc->base.base.id,
10044 crtc->base.enabled ? "enabled" : "disabled",
10045 crtc->active ? "enabled" : "disabled");
10046
10047 crtc->base.enabled = crtc->active;
10048
10049 /* Because we only establish the connector -> encoder ->
10050 * crtc links if something is active, this means the
10051 * crtc is now deactivated. Break the links. connector
10052 * -> encoder links are only establish when things are
10053 * actually up, hence no need to break them. */
10054 WARN_ON(crtc->active);
10055
10056 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
10057 WARN_ON(encoder->connectors_active);
10058 encoder->base.crtc = NULL;
10059 }
10060 }
10061}
10062
10063static void intel_sanitize_encoder(struct intel_encoder *encoder)
10064{
10065 struct intel_connector *connector;
10066 struct drm_device *dev = encoder->base.dev;
10067
10068 /* We need to check both for a crtc link (meaning that the
10069 * encoder is active and trying to read from a pipe) and the
10070 * pipe itself being active. */
10071 bool has_active_crtc = encoder->base.crtc &&
10072 to_intel_crtc(encoder->base.crtc)->active;
10073
10074 if (encoder->connectors_active && !has_active_crtc) {
10075 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
10076 encoder->base.base.id,
10077 drm_get_encoder_name(&encoder->base));
10078
10079 /* Connector is active, but has no active pipe. This is
10080 * fallout from our resume register restoring. Disable
10081 * the encoder manually again. */
10082 if (encoder->base.crtc) {
10083 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
10084 encoder->base.base.id,
10085 drm_get_encoder_name(&encoder->base));
10086 encoder->disable(encoder);
10087 }
10088
10089 /* Inconsistent output/port/pipe state happens presumably due to
10090 * a bug in one of the get_hw_state functions. Or someplace else
10091 * in our code, like the register restore mess on resume. Clamp
10092 * things to off as a safer default. */
10093 list_for_each_entry(connector,
10094 &dev->mode_config.connector_list,
10095 base.head) {
10096 if (connector->encoder != encoder)
10097 continue;
10098
10099 intel_connector_break_all_links(connector);
10100 }
10101 }
10102 /* Enabled encoders without active connectors will be fixed in
10103 * the crtc fixup. */
10104}
10105
Daniel Vetter44cec742013-01-25 17:53:21 +010010106void i915_redisable_vga(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010010107{
10108 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020010109 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010010110
10111 if (I915_READ(vga_reg) != VGA_DISP_DISABLE) {
10112 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
Ville Syrjälä209d5212013-01-25 21:44:48 +020010113 i915_disable_vga(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010010114 }
10115}
10116
Daniel Vetter30e984d2013-06-05 13:34:17 +020010117static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020010118{
10119 struct drm_i915_private *dev_priv = dev->dev_private;
10120 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020010121 struct intel_crtc *crtc;
10122 struct intel_encoder *encoder;
10123 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020010124 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020010125
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010126 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
10127 base.head) {
Daniel Vetter88adfff2013-03-28 10:42:01 +010010128 memset(&crtc->config, 0, sizeof(crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020010129
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010130 crtc->active = dev_priv->display.get_pipe_config(crtc,
10131 &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020010132
10133 crtc->base.enabled = crtc->active;
10134
10135 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
10136 crtc->base.base.id,
10137 crtc->active ? "enabled" : "disabled");
10138 }
10139
Daniel Vetter53589012013-06-05 13:34:16 +020010140 /* FIXME: Smash this into the new shared dpll infrastructure. */
Paulo Zanoniaffa9352012-11-23 15:30:39 -020010141 if (HAS_DDI(dev))
Paulo Zanoni6441ab52012-10-05 12:05:58 -030010142 intel_ddi_setup_hw_pll_state(dev);
10143
Daniel Vetter53589012013-06-05 13:34:16 +020010144 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
10145 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
10146
10147 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
10148 pll->active = 0;
10149 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
10150 base.head) {
10151 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
10152 pll->active++;
10153 }
10154 pll->refcount = pll->active;
10155
Daniel Vetter35c95372013-07-17 06:55:04 +020010156 DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
10157 pll->name, pll->refcount, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020010158 }
10159
Daniel Vetter24929352012-07-02 20:28:59 +020010160 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10161 base.head) {
10162 pipe = 0;
10163
10164 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070010165 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
10166 encoder->base.crtc = &crtc->base;
Jesse Barnes510d5f22013-07-01 15:50:17 -070010167 if (encoder->get_config)
Jesse Barnes045ac3b2013-05-14 17:08:26 -070010168 encoder->get_config(encoder, &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020010169 } else {
10170 encoder->base.crtc = NULL;
10171 }
10172
10173 encoder->connectors_active = false;
10174 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe=%i\n",
10175 encoder->base.base.id,
10176 drm_get_encoder_name(&encoder->base),
10177 encoder->base.crtc ? "enabled" : "disabled",
10178 pipe);
10179 }
10180
Jesse Barnes510d5f22013-07-01 15:50:17 -070010181 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
10182 base.head) {
10183 if (!crtc->active)
10184 continue;
10185 if (dev_priv->display.get_clock)
10186 dev_priv->display.get_clock(crtc,
10187 &crtc->config);
10188 }
10189
Daniel Vetter24929352012-07-02 20:28:59 +020010190 list_for_each_entry(connector, &dev->mode_config.connector_list,
10191 base.head) {
10192 if (connector->get_hw_state(connector)) {
10193 connector->base.dpms = DRM_MODE_DPMS_ON;
10194 connector->encoder->connectors_active = true;
10195 connector->base.encoder = &connector->encoder->base;
10196 } else {
10197 connector->base.dpms = DRM_MODE_DPMS_OFF;
10198 connector->base.encoder = NULL;
10199 }
10200 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
10201 connector->base.base.id,
10202 drm_get_connector_name(&connector->base),
10203 connector->base.encoder ? "enabled" : "disabled");
10204 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020010205}
10206
10207/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
10208 * and i915 state tracking structures. */
10209void intel_modeset_setup_hw_state(struct drm_device *dev,
10210 bool force_restore)
10211{
10212 struct drm_i915_private *dev_priv = dev->dev_private;
10213 enum pipe pipe;
10214 struct drm_plane *plane;
10215 struct intel_crtc *crtc;
10216 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020010217 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020010218
10219 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020010220
Jesse Barnesbabea612013-06-26 18:57:38 +030010221 /*
10222 * Now that we have the config, copy it to each CRTC struct
10223 * Note that this could go away if we move to using crtc_config
10224 * checking everywhere.
10225 */
10226 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
10227 base.head) {
10228 if (crtc->active && i915_fastboot) {
10229 intel_crtc_mode_from_pipe_config(crtc, &crtc->config);
10230
10231 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
10232 crtc->base.base.id);
10233 drm_mode_debug_printmodeline(&crtc->base.mode);
10234 }
10235 }
10236
Daniel Vetter24929352012-07-02 20:28:59 +020010237 /* HW state is read out, now we need to sanitize this mess. */
10238 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10239 base.head) {
10240 intel_sanitize_encoder(encoder);
10241 }
10242
10243 for_each_pipe(pipe) {
10244 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
10245 intel_sanitize_crtc(crtc);
Daniel Vetterc0b03412013-05-28 12:05:54 +020010246 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020010247 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010248
Daniel Vetter35c95372013-07-17 06:55:04 +020010249 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
10250 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
10251
10252 if (!pll->on || pll->active)
10253 continue;
10254
10255 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
10256
10257 pll->disable(dev_priv, pll);
10258 pll->on = false;
10259 }
10260
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010261 if (force_restore) {
Daniel Vetterf30da182013-04-11 20:22:50 +020010262 /*
10263 * We need to use raw interfaces for restoring state to avoid
10264 * checking (bogus) intermediate states.
10265 */
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010266 for_each_pipe(pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070010267 struct drm_crtc *crtc =
10268 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020010269
10270 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
10271 crtc->fb);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010272 }
Jesse Barnesb5644d02013-03-26 13:25:27 -070010273 list_for_each_entry(plane, &dev->mode_config.plane_list, head)
10274 intel_plane_restore(plane);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010010275
10276 i915_redisable_vga(dev);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010277 } else {
10278 intel_modeset_update_staged_output_state(dev);
10279 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010280
10281 intel_modeset_check_state(dev);
Daniel Vetter2e938892012-10-11 20:08:24 +020010282
10283 drm_mode_config_reset(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010010284}
10285
10286void intel_modeset_gem_init(struct drm_device *dev)
10287{
Chris Wilson1833b132012-05-09 11:56:28 +010010288 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020010289
10290 intel_setup_overlay(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020010291
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010292 intel_modeset_setup_hw_state(dev, false);
Jesse Barnes79e53942008-11-07 14:24:08 -080010293}
10294
10295void intel_modeset_cleanup(struct drm_device *dev)
10296{
Jesse Barnes652c3932009-08-17 13:31:43 -070010297 struct drm_i915_private *dev_priv = dev->dev_private;
10298 struct drm_crtc *crtc;
10299 struct intel_crtc *intel_crtc;
10300
Daniel Vetterfd0c0642013-04-24 11:13:35 +020010301 /*
10302 * Interrupts and polling as the first thing to avoid creating havoc.
10303 * Too much stuff here (turning of rps, connectors, ...) would
10304 * experience fancy races otherwise.
10305 */
10306 drm_irq_uninstall(dev);
10307 cancel_work_sync(&dev_priv->hotplug_work);
10308 /*
10309 * Due to the hpd irq storm handling the hotplug work can re-arm the
10310 * poll handlers. Hence disable polling after hpd handling is shut down.
10311 */
Keith Packardf87ea762010-10-03 19:36:26 -070010312 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020010313
Jesse Barnes652c3932009-08-17 13:31:43 -070010314 mutex_lock(&dev->struct_mutex);
10315
Jesse Barnes723bfd72010-10-07 16:01:13 -070010316 intel_unregister_dsm_handler();
10317
Jesse Barnes652c3932009-08-17 13:31:43 -070010318 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
10319 /* Skip inactive CRTCs */
10320 if (!crtc->fb)
10321 continue;
10322
10323 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3dec0092010-08-20 21:40:52 +020010324 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070010325 }
10326
Chris Wilson973d04f2011-07-08 12:22:37 +010010327 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070010328
Daniel Vetter8090c6b2012-06-24 16:42:32 +020010329 intel_disable_gt_powersave(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +000010330
Daniel Vetter930ebb42012-06-29 23:32:16 +020010331 ironlake_teardown_rc6(dev);
10332
Kristian Høgsberg69341a52009-11-11 12:19:17 -050010333 mutex_unlock(&dev->struct_mutex);
10334
Chris Wilson1630fe72011-07-08 12:22:42 +010010335 /* flush any delayed tasks or pending work */
10336 flush_scheduled_work();
10337
Jani Nikuladc652f92013-04-12 15:18:38 +030010338 /* destroy backlight, if any, before the connectors */
10339 intel_panel_destroy_backlight(dev);
10340
Jesse Barnes79e53942008-11-07 14:24:08 -080010341 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010010342
10343 intel_cleanup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010344}
10345
Dave Airlie28d52042009-09-21 14:33:58 +100010346/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080010347 * Return which encoder is currently attached for connector.
10348 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010010349struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080010350{
Chris Wilsondf0e9242010-09-09 16:20:55 +010010351 return &intel_attached_encoder(connector)->base;
10352}
Jesse Barnes79e53942008-11-07 14:24:08 -080010353
Chris Wilsondf0e9242010-09-09 16:20:55 +010010354void intel_connector_attach_encoder(struct intel_connector *connector,
10355 struct intel_encoder *encoder)
10356{
10357 connector->encoder = encoder;
10358 drm_mode_connector_attach_encoder(&connector->base,
10359 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080010360}
Dave Airlie28d52042009-09-21 14:33:58 +100010361
10362/*
10363 * set vga decode state - true == enable VGA decode
10364 */
10365int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
10366{
10367 struct drm_i915_private *dev_priv = dev->dev_private;
10368 u16 gmch_ctrl;
10369
10370 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
10371 if (state)
10372 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
10373 else
10374 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
10375 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
10376 return 0;
10377}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010378
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010379struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010380
10381 u32 power_well_driver;
10382
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010383 struct intel_cursor_error_state {
10384 u32 control;
10385 u32 position;
10386 u32 base;
10387 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010010388 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010389
10390 struct intel_pipe_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010391 enum transcoder cpu_transcoder;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010392 u32 conf;
10393 u32 source;
10394
10395 u32 htotal;
10396 u32 hblank;
10397 u32 hsync;
10398 u32 vtotal;
10399 u32 vblank;
10400 u32 vsync;
Damien Lespiau52331302012-08-15 19:23:25 +010010401 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010402
10403 struct intel_plane_error_state {
10404 u32 control;
10405 u32 stride;
10406 u32 size;
10407 u32 pos;
10408 u32 addr;
10409 u32 surface;
10410 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010010411 } plane[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010412};
10413
10414struct intel_display_error_state *
10415intel_display_capture_error_state(struct drm_device *dev)
10416{
Akshay Joshi0206e352011-08-16 15:34:10 -040010417 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010418 struct intel_display_error_state *error;
Paulo Zanoni702e7a52012-10-23 18:29:59 -020010419 enum transcoder cpu_transcoder;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010420 int i;
10421
10422 error = kmalloc(sizeof(*error), GFP_ATOMIC);
10423 if (error == NULL)
10424 return NULL;
10425
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010426 if (HAS_POWER_WELL(dev))
10427 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
10428
Damien Lespiau52331302012-08-15 19:23:25 +010010429 for_each_pipe(i) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -020010430 cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv, i);
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010431 error->pipe[i].cpu_transcoder = cpu_transcoder;
Paulo Zanoni702e7a52012-10-23 18:29:59 -020010432
Paulo Zanonia18c4c32013-03-06 20:03:12 -030010433 if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev)) {
10434 error->cursor[i].control = I915_READ(CURCNTR(i));
10435 error->cursor[i].position = I915_READ(CURPOS(i));
10436 error->cursor[i].base = I915_READ(CURBASE(i));
10437 } else {
10438 error->cursor[i].control = I915_READ(CURCNTR_IVB(i));
10439 error->cursor[i].position = I915_READ(CURPOS_IVB(i));
10440 error->cursor[i].base = I915_READ(CURBASE_IVB(i));
10441 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010442
10443 error->plane[i].control = I915_READ(DSPCNTR(i));
10444 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030010445 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030010446 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030010447 error->plane[i].pos = I915_READ(DSPPOS(i));
10448 }
Paulo Zanonica291362013-03-06 20:03:14 -030010449 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
10450 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010451 if (INTEL_INFO(dev)->gen >= 4) {
10452 error->plane[i].surface = I915_READ(DSPSURF(i));
10453 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
10454 }
10455
Paulo Zanoni702e7a52012-10-23 18:29:59 -020010456 error->pipe[i].conf = I915_READ(PIPECONF(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010457 error->pipe[i].source = I915_READ(PIPESRC(i));
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010458 error->pipe[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
10459 error->pipe[i].hblank = I915_READ(HBLANK(cpu_transcoder));
10460 error->pipe[i].hsync = I915_READ(HSYNC(cpu_transcoder));
10461 error->pipe[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
10462 error->pipe[i].vblank = I915_READ(VBLANK(cpu_transcoder));
10463 error->pipe[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010464 }
10465
Paulo Zanoni12d217c2013-05-03 12:15:38 -030010466 /* In the code above we read the registers without checking if the power
10467 * well was on, so here we have to clear the FPGA_DBG_RM_NOCLAIM bit to
10468 * prevent the next I915_WRITE from detecting it and printing an error
10469 * message. */
Chris Wilson907b28c2013-07-19 20:36:52 +010010470 intel_uncore_clear_errors(dev);
Paulo Zanoni12d217c2013-05-03 12:15:38 -030010471
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010472 return error;
10473}
10474
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010475#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
10476
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010477void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010478intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010479 struct drm_device *dev,
10480 struct intel_display_error_state *error)
10481{
10482 int i;
10483
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010484 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010485 if (HAS_POWER_WELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010486 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010487 error->power_well_driver);
Damien Lespiau52331302012-08-15 19:23:25 +010010488 for_each_pipe(i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010489 err_printf(m, "Pipe [%d]:\n", i);
10490 err_printf(m, " CPU transcoder: %c\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010491 transcoder_name(error->pipe[i].cpu_transcoder));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010492 err_printf(m, " CONF: %08x\n", error->pipe[i].conf);
10493 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
10494 err_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
10495 err_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
10496 err_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
10497 err_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
10498 err_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
10499 err_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010500
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010501 err_printf(m, "Plane [%d]:\n", i);
10502 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
10503 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030010504 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010505 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
10506 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030010507 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030010508 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010509 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010510 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010511 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
10512 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010513 }
10514
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010515 err_printf(m, "Cursor [%d]:\n", i);
10516 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
10517 err_printf(m, " POS: %08x\n", error->cursor[i].position);
10518 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010519 }
10520}