blob: abaae78f0c52255b04289dc7a25d42e33343cbae [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 Barnesa0c4da242012-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 Barnesa0c4da242012-06-15 11:55:13 -0700322 .p2 = { .dot_limit = 270000,
323 .p2_slow = 2, .p2_fast = 20 },
Jesse Barnesa0c4da242012-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 Barnesa0c4da242012-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 Barnesa0c4da242012-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 Barnesa0c4da242012-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 Barnesa0c4da242012-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 Barnesa0c4da242012-06-15 11:55:13 -0700348 .p2 = { .dot_limit = 270000,
349 .p2_slow = 2, .p2_fast = 20 },
Jesse Barnesa0c4da242012-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 Barnesa0c4da242012-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 Vetter6c2b7c12012-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 Barnesa0c4da242012-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 Barnesa0c4da242012-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/**
1409 * intel_disable_pll - disable a PLL
1410 * @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 */
1417static void intel_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1418{
1419 int reg;
1420 u32 val;
1421
1422 /* Don't disable pipe A or pipe A PLLs if needed */
1423 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1424 return;
1425
1426 /* Make sure the pipe isn't still relying on us */
1427 assert_pipe_disabled(dev_priv, pipe);
1428
1429 reg = DPLL(pipe);
1430 val = I915_READ(reg);
1431 val &= ~DPLL_VCO_ENABLE;
1432 I915_WRITE(reg, val);
1433 POSTING_READ(reg);
1434}
1435
Jesse Barnes89b667f2013-04-18 14:51:36 -07001436void vlv_wait_port_ready(struct drm_i915_private *dev_priv, int port)
1437{
1438 u32 port_mask;
1439
1440 if (!port)
1441 port_mask = DPLL_PORTB_READY_MASK;
1442 else
1443 port_mask = DPLL_PORTC_READY_MASK;
1444
1445 if (wait_for((I915_READ(DPLL(0)) & port_mask) == 0, 1000))
1446 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
1447 'B' + port, I915_READ(DPLL(0)));
1448}
1449
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001450/**
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001451 * ironlake_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001452 * @dev_priv: i915 private structure
1453 * @pipe: pipe PLL to enable
1454 *
1455 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1456 * drives the transcoder clock.
1457 */
Daniel Vettere2b78262013-06-07 23:10:03 +02001458static void ironlake_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001459{
Daniel Vettere2b78262013-06-07 23:10:03 +02001460 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1461 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001462
Chris Wilson48da64a2012-05-13 20:16:12 +01001463 /* PCH PLLs only available on ILK, SNB and IVB */
Jesse Barnes92f25842011-01-04 15:09:34 -08001464 BUG_ON(dev_priv->info->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001465 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001466 return;
1467
1468 if (WARN_ON(pll->refcount == 0))
1469 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001470
Daniel Vetter46edb022013-06-05 13:34:12 +02001471 DRM_DEBUG_KMS("enable %s (active %d, on? %d)for crtc %d\n",
1472 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001473 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001474
Daniel Vettercdbd2312013-06-05 13:34:03 +02001475 if (pll->active++) {
1476 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001477 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001478 return;
1479 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001480 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001481
Daniel Vetter46edb022013-06-05 13:34:12 +02001482 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001483 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001484 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001485}
1486
Daniel Vettere2b78262013-06-07 23:10:03 +02001487static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001488{
Daniel Vettere2b78262013-06-07 23:10:03 +02001489 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1490 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001491
Jesse Barnes92f25842011-01-04 15:09:34 -08001492 /* PCH only available on ILK+ */
1493 BUG_ON(dev_priv->info->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001494 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001495 return;
1496
Chris Wilson48da64a2012-05-13 20:16:12 +01001497 if (WARN_ON(pll->refcount == 0))
1498 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001499
Daniel Vetter46edb022013-06-05 13:34:12 +02001500 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1501 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001502 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001503
Chris Wilson48da64a2012-05-13 20:16:12 +01001504 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001505 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001506 return;
1507 }
1508
Daniel Vettere9d69442013-06-05 13:34:15 +02001509 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001510 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001511 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001512 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001513
Daniel Vetter46edb022013-06-05 13:34:12 +02001514 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001515 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001516 pll->on = false;
Jesse Barnes92f25842011-01-04 15:09:34 -08001517}
1518
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001519static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1520 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001521{
Daniel Vetter23670b322012-11-01 09:15:30 +01001522 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001523 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001524 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001525 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001526
1527 /* PCH only available on ILK+ */
1528 BUG_ON(dev_priv->info->gen < 5);
1529
1530 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001531 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001532 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001533
1534 /* FDI must be feeding us bits for PCH ports */
1535 assert_fdi_tx_enabled(dev_priv, pipe);
1536 assert_fdi_rx_enabled(dev_priv, pipe);
1537
Daniel Vetter23670b322012-11-01 09:15:30 +01001538 if (HAS_PCH_CPT(dev)) {
1539 /* Workaround: Set the timing override bit before enabling the
1540 * pch transcoder. */
1541 reg = TRANS_CHICKEN2(pipe);
1542 val = I915_READ(reg);
1543 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1544 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001545 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001546
Daniel Vetterab9412b2013-05-03 11:49:46 +02001547 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001548 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001549 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001550
1551 if (HAS_PCH_IBX(dev_priv->dev)) {
1552 /*
1553 * make the BPC in transcoder be consistent with
1554 * that in pipeconf reg.
1555 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001556 val &= ~PIPECONF_BPC_MASK;
1557 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001558 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001559
1560 val &= ~TRANS_INTERLACE_MASK;
1561 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001562 if (HAS_PCH_IBX(dev_priv->dev) &&
1563 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1564 val |= TRANS_LEGACY_INTERLACED_ILK;
1565 else
1566 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001567 else
1568 val |= TRANS_PROGRESSIVE;
1569
Jesse Barnes040484a2011-01-03 12:14:26 -08001570 I915_WRITE(reg, val | TRANS_ENABLE);
1571 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001572 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001573}
1574
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001575static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001576 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001577{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001578 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001579
1580 /* PCH only available on ILK+ */
1581 BUG_ON(dev_priv->info->gen < 5);
1582
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001583 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001584 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001585 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001586
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001587 /* Workaround: set timing override bit. */
1588 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001589 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001590 I915_WRITE(_TRANSA_CHICKEN2, val);
1591
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001592 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001593 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001594
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001595 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1596 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001597 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001598 else
1599 val |= TRANS_PROGRESSIVE;
1600
Daniel Vetterab9412b2013-05-03 11:49:46 +02001601 I915_WRITE(LPT_TRANSCONF, val);
1602 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001603 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001604}
1605
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001606static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1607 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001608{
Daniel Vetter23670b322012-11-01 09:15:30 +01001609 struct drm_device *dev = dev_priv->dev;
1610 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001611
1612 /* FDI relies on the transcoder */
1613 assert_fdi_tx_disabled(dev_priv, pipe);
1614 assert_fdi_rx_disabled(dev_priv, pipe);
1615
Jesse Barnes291906f2011-02-02 12:28:03 -08001616 /* Ports must be off as well */
1617 assert_pch_ports_disabled(dev_priv, pipe);
1618
Daniel Vetterab9412b2013-05-03 11:49:46 +02001619 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001620 val = I915_READ(reg);
1621 val &= ~TRANS_ENABLE;
1622 I915_WRITE(reg, val);
1623 /* wait for PCH transcoder off, transcoder state */
1624 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001625 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001626
1627 if (!HAS_PCH_IBX(dev)) {
1628 /* Workaround: Clear the timing override chicken bit again. */
1629 reg = TRANS_CHICKEN2(pipe);
1630 val = I915_READ(reg);
1631 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1632 I915_WRITE(reg, val);
1633 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001634}
1635
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001636static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001637{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001638 u32 val;
1639
Daniel Vetterab9412b2013-05-03 11:49:46 +02001640 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001641 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001642 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001643 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001644 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001645 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001646
1647 /* Workaround: clear timing override bit. */
1648 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001649 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001650 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001651}
1652
1653/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001654 * intel_enable_pipe - enable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001655 * @dev_priv: i915 private structure
1656 * @pipe: pipe to enable
Jesse Barnes040484a2011-01-03 12:14:26 -08001657 * @pch_port: on ILK+, is this pipe driving a PCH port or not
Jesse Barnesb24e7172011-01-04 15:09:30 -08001658 *
1659 * Enable @pipe, making sure that various hardware specific requirements
1660 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1661 *
1662 * @pipe should be %PIPE_A or %PIPE_B.
1663 *
1664 * Will wait until the pipe is actually running (i.e. first vblank) before
1665 * returning.
1666 */
Jesse Barnes040484a2011-01-03 12:14:26 -08001667static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
1668 bool pch_port)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001669{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001670 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1671 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001672 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001673 int reg;
1674 u32 val;
1675
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001676 assert_planes_disabled(dev_priv, pipe);
1677 assert_sprites_disabled(dev_priv, pipe);
1678
Paulo Zanoni681e5812012-12-06 11:12:38 -02001679 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001680 pch_transcoder = TRANSCODER_A;
1681 else
1682 pch_transcoder = pipe;
1683
Jesse Barnesb24e7172011-01-04 15:09:30 -08001684 /*
1685 * A pipe without a PLL won't actually be able to drive bits from
1686 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1687 * need the check.
1688 */
1689 if (!HAS_PCH_SPLIT(dev_priv->dev))
1690 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001691 else {
1692 if (pch_port) {
1693 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001694 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001695 assert_fdi_tx_pll_enabled(dev_priv,
1696 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001697 }
1698 /* FIXME: assert CPU port conditions for SNB+ */
1699 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001700
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001701 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001702 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001703 if (val & PIPECONF_ENABLE)
1704 return;
1705
1706 I915_WRITE(reg, val | PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001707 intel_wait_for_vblank(dev_priv->dev, pipe);
1708}
1709
1710/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001711 * intel_disable_pipe - disable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001712 * @dev_priv: i915 private structure
1713 * @pipe: pipe to disable
1714 *
1715 * Disable @pipe, making sure that various hardware specific requirements
1716 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1717 *
1718 * @pipe should be %PIPE_A or %PIPE_B.
1719 *
1720 * Will wait until the pipe has shut down before returning.
1721 */
1722static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1723 enum pipe pipe)
1724{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001725 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1726 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001727 int reg;
1728 u32 val;
1729
1730 /*
1731 * Make sure planes won't keep trying to pump pixels to us,
1732 * or we might hang the display.
1733 */
1734 assert_planes_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001735 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001736
1737 /* Don't disable pipe A or pipe A PLLs if needed */
1738 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1739 return;
1740
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001741 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001742 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001743 if ((val & PIPECONF_ENABLE) == 0)
1744 return;
1745
1746 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001747 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1748}
1749
Keith Packardd74362c2011-07-28 14:47:14 -07001750/*
1751 * Plane regs are double buffered, going from enabled->disabled needs a
1752 * trigger in order to latch. The display address reg provides this.
1753 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03001754void intel_flush_display_plane(struct drm_i915_private *dev_priv,
Keith Packardd74362c2011-07-28 14:47:14 -07001755 enum plane plane)
1756{
Damien Lespiau14f86142012-10-29 15:24:49 +00001757 if (dev_priv->info->gen >= 4)
1758 I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
1759 else
1760 I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
Keith Packardd74362c2011-07-28 14:47:14 -07001761}
1762
Jesse Barnesb24e7172011-01-04 15:09:30 -08001763/**
1764 * intel_enable_plane - enable a display plane on a given pipe
1765 * @dev_priv: i915 private structure
1766 * @plane: plane to enable
1767 * @pipe: pipe being fed
1768 *
1769 * Enable @plane on @pipe, making sure that @pipe is running first.
1770 */
1771static void intel_enable_plane(struct drm_i915_private *dev_priv,
1772 enum plane plane, enum pipe pipe)
1773{
1774 int reg;
1775 u32 val;
1776
1777 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1778 assert_pipe_enabled(dev_priv, pipe);
1779
1780 reg = DSPCNTR(plane);
1781 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001782 if (val & DISPLAY_PLANE_ENABLE)
1783 return;
1784
1785 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
Keith Packardd74362c2011-07-28 14:47:14 -07001786 intel_flush_display_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001787 intel_wait_for_vblank(dev_priv->dev, pipe);
1788}
1789
Jesse Barnesb24e7172011-01-04 15:09:30 -08001790/**
1791 * intel_disable_plane - disable a display plane
1792 * @dev_priv: i915 private structure
1793 * @plane: plane to disable
1794 * @pipe: pipe consuming the data
1795 *
1796 * Disable @plane; should be an independent operation.
1797 */
1798static void intel_disable_plane(struct drm_i915_private *dev_priv,
1799 enum plane plane, enum pipe pipe)
1800{
1801 int reg;
1802 u32 val;
1803
1804 reg = DSPCNTR(plane);
1805 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001806 if ((val & DISPLAY_PLANE_ENABLE) == 0)
1807 return;
1808
1809 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001810 intel_flush_display_plane(dev_priv, plane);
1811 intel_wait_for_vblank(dev_priv->dev, pipe);
1812}
1813
Chris Wilson693db182013-03-05 14:52:39 +00001814static bool need_vtd_wa(struct drm_device *dev)
1815{
1816#ifdef CONFIG_INTEL_IOMMU
1817 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
1818 return true;
1819#endif
1820 return false;
1821}
1822
Chris Wilson127bd2a2010-07-23 23:32:05 +01001823int
Chris Wilson48b956c2010-09-14 12:50:34 +01001824intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00001825 struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00001826 struct intel_ring_buffer *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001827{
Chris Wilsonce453d82011-02-21 14:43:56 +00001828 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001829 u32 alignment;
1830 int ret;
1831
Chris Wilson05394f32010-11-08 19:18:58 +00001832 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001833 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01001834 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1835 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001836 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01001837 alignment = 4 * 1024;
1838 else
1839 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001840 break;
1841 case I915_TILING_X:
1842 /* pin() will align the object as required by fence */
1843 alignment = 0;
1844 break;
1845 case I915_TILING_Y:
Daniel Vetter8bb6e952013-04-06 23:54:56 +02001846 /* Despite that we check this in framebuffer_init userspace can
1847 * screw us over and change the tiling after the fact. Only
1848 * pinned buffers can't change their tiling. */
1849 DRM_DEBUG_DRIVER("Y tiled not allowed for scan out buffers\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001850 return -EINVAL;
1851 default:
1852 BUG();
1853 }
1854
Chris Wilson693db182013-03-05 14:52:39 +00001855 /* Note that the w/a also requires 64 PTE of padding following the
1856 * bo. We currently fill all unused PTE with the shadow page and so
1857 * we should always have valid PTE following the scanout preventing
1858 * the VT-d warning.
1859 */
1860 if (need_vtd_wa(dev) && alignment < 256 * 1024)
1861 alignment = 256 * 1024;
1862
Chris Wilsonce453d82011-02-21 14:43:56 +00001863 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01001864 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01001865 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00001866 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001867
1868 /* Install a fence for tiled scan-out. Pre-i965 always needs a
1869 * fence, whereas 965+ only requires a fence if using
1870 * framebuffer compression. For simplicity, we always install
1871 * a fence as the cost is not that onerous.
1872 */
Chris Wilson06d98132012-04-17 15:31:24 +01001873 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001874 if (ret)
1875 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01001876
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001877 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001878
Chris Wilsonce453d82011-02-21 14:43:56 +00001879 dev_priv->mm.interruptible = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001880 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01001881
1882err_unpin:
1883 i915_gem_object_unpin(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00001884err_interruptible:
1885 dev_priv->mm.interruptible = true;
Chris Wilson48b956c2010-09-14 12:50:34 +01001886 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001887}
1888
Chris Wilson1690e1e2011-12-14 13:57:08 +01001889void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
1890{
1891 i915_gem_object_unpin_fence(obj);
1892 i915_gem_object_unpin(obj);
1893}
1894
Daniel Vetterc2c75132012-07-05 12:17:30 +02001895/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
1896 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00001897unsigned long intel_gen4_compute_page_offset(int *x, int *y,
1898 unsigned int tiling_mode,
1899 unsigned int cpp,
1900 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02001901{
Chris Wilsonbc752862013-02-21 20:04:31 +00001902 if (tiling_mode != I915_TILING_NONE) {
1903 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02001904
Chris Wilsonbc752862013-02-21 20:04:31 +00001905 tile_rows = *y / 8;
1906 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02001907
Chris Wilsonbc752862013-02-21 20:04:31 +00001908 tiles = *x / (512/cpp);
1909 *x %= 512/cpp;
1910
1911 return tile_rows * pitch * 8 + tiles * 4096;
1912 } else {
1913 unsigned int offset;
1914
1915 offset = *y * pitch + *x * cpp;
1916 *y = 0;
1917 *x = (offset & 4095) / cpp;
1918 return offset & -4096;
1919 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02001920}
1921
Jesse Barnes17638cd2011-06-24 12:19:23 -07001922static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
1923 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07001924{
1925 struct drm_device *dev = crtc->dev;
1926 struct drm_i915_private *dev_priv = dev->dev_private;
1927 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1928 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00001929 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07001930 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02001931 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07001932 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01001933 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07001934
1935 switch (plane) {
1936 case 0:
1937 case 1:
1938 break;
1939 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03001940 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes81255562010-08-02 12:07:50 -07001941 return -EINVAL;
1942 }
1943
1944 intel_fb = to_intel_framebuffer(fb);
1945 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07001946
Chris Wilson5eddb702010-09-11 13:48:45 +01001947 reg = DSPCNTR(plane);
1948 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07001949 /* Mask out pixel format bits in case we change it */
1950 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02001951 switch (fb->pixel_format) {
1952 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07001953 dspcntr |= DISPPLANE_8BPP;
1954 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02001955 case DRM_FORMAT_XRGB1555:
1956 case DRM_FORMAT_ARGB1555:
1957 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07001958 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02001959 case DRM_FORMAT_RGB565:
1960 dspcntr |= DISPPLANE_BGRX565;
1961 break;
1962 case DRM_FORMAT_XRGB8888:
1963 case DRM_FORMAT_ARGB8888:
1964 dspcntr |= DISPPLANE_BGRX888;
1965 break;
1966 case DRM_FORMAT_XBGR8888:
1967 case DRM_FORMAT_ABGR8888:
1968 dspcntr |= DISPPLANE_RGBX888;
1969 break;
1970 case DRM_FORMAT_XRGB2101010:
1971 case DRM_FORMAT_ARGB2101010:
1972 dspcntr |= DISPPLANE_BGRX101010;
1973 break;
1974 case DRM_FORMAT_XBGR2101010:
1975 case DRM_FORMAT_ABGR2101010:
1976 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07001977 break;
1978 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01001979 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07001980 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02001981
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001982 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00001983 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07001984 dspcntr |= DISPPLANE_TILED;
1985 else
1986 dspcntr &= ~DISPPLANE_TILED;
1987 }
1988
Ville Syrjäläde1aa622013-06-07 10:47:01 +03001989 if (IS_G4X(dev))
1990 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
1991
Chris Wilson5eddb702010-09-11 13:48:45 +01001992 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07001993
Daniel Vettere506a0c2012-07-05 12:17:29 +02001994 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes81255562010-08-02 12:07:50 -07001995
Daniel Vetterc2c75132012-07-05 12:17:30 +02001996 if (INTEL_INFO(dev)->gen >= 4) {
1997 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00001998 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
1999 fb->bits_per_pixel / 8,
2000 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002001 linear_offset -= intel_crtc->dspaddr_offset;
2002 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002003 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002004 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002005
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002006 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2007 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2008 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002009 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002010 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetterc2c75132012-07-05 12:17:30 +02002011 I915_MODIFY_DISPBASE(DSPSURF(plane),
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002012 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002013 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002014 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002015 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002016 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002017 POSTING_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002018
Jesse Barnes17638cd2011-06-24 12:19:23 -07002019 return 0;
2020}
2021
2022static int ironlake_update_plane(struct drm_crtc *crtc,
2023 struct drm_framebuffer *fb, int x, int y)
2024{
2025 struct drm_device *dev = crtc->dev;
2026 struct drm_i915_private *dev_priv = dev->dev_private;
2027 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2028 struct intel_framebuffer *intel_fb;
2029 struct drm_i915_gem_object *obj;
2030 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002031 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002032 u32 dspcntr;
2033 u32 reg;
2034
2035 switch (plane) {
2036 case 0:
2037 case 1:
Jesse Barnes27f82272011-09-02 12:54:37 -07002038 case 2:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002039 break;
2040 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002041 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes17638cd2011-06-24 12:19:23 -07002042 return -EINVAL;
2043 }
2044
2045 intel_fb = to_intel_framebuffer(fb);
2046 obj = intel_fb->obj;
2047
2048 reg = DSPCNTR(plane);
2049 dspcntr = I915_READ(reg);
2050 /* Mask out pixel format bits in case we change it */
2051 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002052 switch (fb->pixel_format) {
2053 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002054 dspcntr |= DISPPLANE_8BPP;
2055 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002056 case DRM_FORMAT_RGB565:
2057 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002058 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002059 case DRM_FORMAT_XRGB8888:
2060 case DRM_FORMAT_ARGB8888:
2061 dspcntr |= DISPPLANE_BGRX888;
2062 break;
2063 case DRM_FORMAT_XBGR8888:
2064 case DRM_FORMAT_ABGR8888:
2065 dspcntr |= DISPPLANE_RGBX888;
2066 break;
2067 case DRM_FORMAT_XRGB2101010:
2068 case DRM_FORMAT_ARGB2101010:
2069 dspcntr |= DISPPLANE_BGRX101010;
2070 break;
2071 case DRM_FORMAT_XBGR2101010:
2072 case DRM_FORMAT_ABGR2101010:
2073 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002074 break;
2075 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002076 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002077 }
2078
2079 if (obj->tiling_mode != I915_TILING_NONE)
2080 dspcntr |= DISPPLANE_TILED;
2081 else
2082 dspcntr &= ~DISPPLANE_TILED;
2083
2084 /* must disable */
2085 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2086
2087 I915_WRITE(reg, dspcntr);
2088
Daniel Vettere506a0c2012-07-05 12:17:29 +02002089 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002090 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002091 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2092 fb->bits_per_pixel / 8,
2093 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002094 linear_offset -= intel_crtc->dspaddr_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002095
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002096 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2097 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2098 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002099 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002100 I915_MODIFY_DISPBASE(DSPSURF(plane),
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002101 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002102 if (IS_HASWELL(dev)) {
2103 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2104 } else {
2105 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2106 I915_WRITE(DSPLINOFF(plane), linear_offset);
2107 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002108 POSTING_READ(reg);
2109
2110 return 0;
2111}
2112
2113/* Assume fb object is pinned & idle & fenced and just update base pointers */
2114static int
2115intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2116 int x, int y, enum mode_set_atomic state)
2117{
2118 struct drm_device *dev = crtc->dev;
2119 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002120
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002121 if (dev_priv->display.disable_fbc)
2122 dev_priv->display.disable_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02002123 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07002124
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002125 return dev_priv->display.update_plane(crtc, fb, x, y);
Jesse Barnes81255562010-08-02 12:07:50 -07002126}
2127
Ville Syrjälä96a02912013-02-18 19:08:49 +02002128void intel_display_handle_reset(struct drm_device *dev)
2129{
2130 struct drm_i915_private *dev_priv = dev->dev_private;
2131 struct drm_crtc *crtc;
2132
2133 /*
2134 * Flips in the rings have been nuked by the reset,
2135 * so complete all pending flips so that user space
2136 * will get its events and not get stuck.
2137 *
2138 * Also update the base address of all primary
2139 * planes to the the last fb to make sure we're
2140 * showing the correct fb after a reset.
2141 *
2142 * Need to make two loops over the crtcs so that we
2143 * don't try to grab a crtc mutex before the
2144 * pending_flip_queue really got woken up.
2145 */
2146
2147 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2148 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2149 enum plane plane = intel_crtc->plane;
2150
2151 intel_prepare_page_flip(dev, plane);
2152 intel_finish_page_flip_plane(dev, plane);
2153 }
2154
2155 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2156 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2157
2158 mutex_lock(&crtc->mutex);
2159 if (intel_crtc->active)
2160 dev_priv->display.update_plane(crtc, crtc->fb,
2161 crtc->x, crtc->y);
2162 mutex_unlock(&crtc->mutex);
2163 }
2164}
2165
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002166static int
Chris Wilson14667a42012-04-03 17:58:35 +01002167intel_finish_fb(struct drm_framebuffer *old_fb)
2168{
2169 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2170 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2171 bool was_interruptible = dev_priv->mm.interruptible;
2172 int ret;
2173
Chris Wilson14667a42012-04-03 17:58:35 +01002174 /* Big Hammer, we also need to ensure that any pending
2175 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2176 * current scanout is retired before unpinning the old
2177 * framebuffer.
2178 *
2179 * This should only fail upon a hung GPU, in which case we
2180 * can safely continue.
2181 */
2182 dev_priv->mm.interruptible = false;
2183 ret = i915_gem_object_finish_gpu(obj);
2184 dev_priv->mm.interruptible = was_interruptible;
2185
2186 return ret;
2187}
2188
Ville Syrjälä198598d2012-10-31 17:50:24 +02002189static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
2190{
2191 struct drm_device *dev = crtc->dev;
2192 struct drm_i915_master_private *master_priv;
2193 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2194
2195 if (!dev->primary->master)
2196 return;
2197
2198 master_priv = dev->primary->master->driver_priv;
2199 if (!master_priv->sarea_priv)
2200 return;
2201
2202 switch (intel_crtc->pipe) {
2203 case 0:
2204 master_priv->sarea_priv->pipeA_x = x;
2205 master_priv->sarea_priv->pipeA_y = y;
2206 break;
2207 case 1:
2208 master_priv->sarea_priv->pipeB_x = x;
2209 master_priv->sarea_priv->pipeB_y = y;
2210 break;
2211 default:
2212 break;
2213 }
2214}
2215
Chris Wilson14667a42012-04-03 17:58:35 +01002216static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002217intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002218 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002219{
2220 struct drm_device *dev = crtc->dev;
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002221 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08002222 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter94352cf2012-07-05 22:51:56 +02002223 struct drm_framebuffer *old_fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002224 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002225
2226 /* no fb bound */
Daniel Vetter94352cf2012-07-05 22:51:56 +02002227 if (!fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002228 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002229 return 0;
2230 }
2231
Ben Widawsky7eb552a2013-03-13 14:05:41 -07002232 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002233 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2234 plane_name(intel_crtc->plane),
2235 INTEL_INFO(dev)->num_pipes);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002236 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002237 }
2238
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002239 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01002240 ret = intel_pin_and_fence_fb_obj(dev,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002241 to_intel_framebuffer(fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002242 NULL);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002243 if (ret != 0) {
2244 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002245 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002246 return ret;
2247 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002248
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002249 /* Update pipe size and adjust fitter if needed */
2250 if (i915_fastboot) {
2251 I915_WRITE(PIPESRC(intel_crtc->pipe),
2252 ((crtc->mode.hdisplay - 1) << 16) |
2253 (crtc->mode.vdisplay - 1));
2254 if (!intel_crtc->config.pch_pfit.size &&
2255 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
2256 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2257 I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
2258 I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
2259 I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
2260 }
2261 }
2262
Daniel Vetter94352cf2012-07-05 22:51:56 +02002263 ret = dev_priv->display.update_plane(crtc, fb, x, y);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002264 if (ret) {
Daniel Vetter94352cf2012-07-05 22:51:56 +02002265 intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002266 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002267 DRM_ERROR("failed to update base address\n");
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002268 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002269 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002270
Daniel Vetter94352cf2012-07-05 22:51:56 +02002271 old_fb = crtc->fb;
2272 crtc->fb = fb;
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02002273 crtc->x = x;
2274 crtc->y = y;
Daniel Vetter94352cf2012-07-05 22:51:56 +02002275
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002276 if (old_fb) {
Daniel Vetterd7697ee2013-06-02 17:23:01 +02002277 if (intel_crtc->active && old_fb != fb)
2278 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002279 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002280 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002281
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002282 intel_update_fbc(dev);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002283 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002284
Ville Syrjälä198598d2012-10-31 17:50:24 +02002285 intel_crtc_update_sarea_pos(crtc, x, y);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002286
2287 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002288}
2289
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002290static void intel_fdi_normal_train(struct drm_crtc *crtc)
2291{
2292 struct drm_device *dev = crtc->dev;
2293 struct drm_i915_private *dev_priv = dev->dev_private;
2294 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2295 int pipe = intel_crtc->pipe;
2296 u32 reg, temp;
2297
2298 /* enable normal train */
2299 reg = FDI_TX_CTL(pipe);
2300 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002301 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002302 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2303 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002304 } else {
2305 temp &= ~FDI_LINK_TRAIN_NONE;
2306 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002307 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002308 I915_WRITE(reg, temp);
2309
2310 reg = FDI_RX_CTL(pipe);
2311 temp = I915_READ(reg);
2312 if (HAS_PCH_CPT(dev)) {
2313 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2314 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2315 } else {
2316 temp &= ~FDI_LINK_TRAIN_NONE;
2317 temp |= FDI_LINK_TRAIN_NONE;
2318 }
2319 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2320
2321 /* wait one idle pattern time */
2322 POSTING_READ(reg);
2323 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002324
2325 /* IVB wants error correction enabled */
2326 if (IS_IVYBRIDGE(dev))
2327 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2328 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002329}
2330
Daniel Vetter1e833f42013-02-19 22:31:57 +01002331static bool pipe_has_enabled_pch(struct intel_crtc *intel_crtc)
2332{
2333 return intel_crtc->base.enabled && intel_crtc->config.has_pch_encoder;
2334}
2335
Daniel Vetter01a415f2012-10-27 15:58:40 +02002336static void ivb_modeset_global_resources(struct drm_device *dev)
2337{
2338 struct drm_i915_private *dev_priv = dev->dev_private;
2339 struct intel_crtc *pipe_B_crtc =
2340 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2341 struct intel_crtc *pipe_C_crtc =
2342 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2343 uint32_t temp;
2344
Daniel Vetter1e833f42013-02-19 22:31:57 +01002345 /*
2346 * When everything is off disable fdi C so that we could enable fdi B
2347 * with all lanes. Note that we don't care about enabled pipes without
2348 * an enabled pch encoder.
2349 */
2350 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2351 !pipe_has_enabled_pch(pipe_C_crtc)) {
Daniel Vetter01a415f2012-10-27 15:58:40 +02002352 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2353 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2354
2355 temp = I915_READ(SOUTH_CHICKEN1);
2356 temp &= ~FDI_BC_BIFURCATION_SELECT;
2357 DRM_DEBUG_KMS("disabling fdi C rx\n");
2358 I915_WRITE(SOUTH_CHICKEN1, temp);
2359 }
2360}
2361
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002362/* The FDI link training functions for ILK/Ibexpeak. */
2363static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2364{
2365 struct drm_device *dev = crtc->dev;
2366 struct drm_i915_private *dev_priv = dev->dev_private;
2367 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2368 int pipe = intel_crtc->pipe;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002369 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002370 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002371
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002372 /* FDI needs bits from pipe & plane first */
2373 assert_pipe_enabled(dev_priv, pipe);
2374 assert_plane_enabled(dev_priv, plane);
2375
Adam Jacksone1a44742010-06-25 15:32:14 -04002376 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2377 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002378 reg = FDI_RX_IMR(pipe);
2379 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002380 temp &= ~FDI_RX_SYMBOL_LOCK;
2381 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002382 I915_WRITE(reg, temp);
2383 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002384 udelay(150);
2385
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002386 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002387 reg = FDI_TX_CTL(pipe);
2388 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002389 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2390 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002391 temp &= ~FDI_LINK_TRAIN_NONE;
2392 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002393 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002394
Chris Wilson5eddb702010-09-11 13:48:45 +01002395 reg = FDI_RX_CTL(pipe);
2396 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002397 temp &= ~FDI_LINK_TRAIN_NONE;
2398 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002399 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2400
2401 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002402 udelay(150);
2403
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002404 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002405 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2406 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2407 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002408
Chris Wilson5eddb702010-09-11 13:48:45 +01002409 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002410 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002411 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002412 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2413
2414 if ((temp & FDI_RX_BIT_LOCK)) {
2415 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002416 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002417 break;
2418 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002419 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002420 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002421 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002422
2423 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002424 reg = FDI_TX_CTL(pipe);
2425 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002426 temp &= ~FDI_LINK_TRAIN_NONE;
2427 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002428 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002429
Chris Wilson5eddb702010-09-11 13:48:45 +01002430 reg = FDI_RX_CTL(pipe);
2431 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002432 temp &= ~FDI_LINK_TRAIN_NONE;
2433 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002434 I915_WRITE(reg, temp);
2435
2436 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002437 udelay(150);
2438
Chris Wilson5eddb702010-09-11 13:48:45 +01002439 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002440 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002441 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002442 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2443
2444 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002445 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002446 DRM_DEBUG_KMS("FDI train 2 done.\n");
2447 break;
2448 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002449 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002450 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002451 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002452
2453 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002454
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002455}
2456
Akshay Joshi0206e352011-08-16 15:34:10 -04002457static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002458 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2459 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2460 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2461 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2462};
2463
2464/* The FDI link training functions for SNB/Cougarpoint. */
2465static void gen6_fdi_link_train(struct drm_crtc *crtc)
2466{
2467 struct drm_device *dev = crtc->dev;
2468 struct drm_i915_private *dev_priv = dev->dev_private;
2469 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2470 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05002471 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002472
Adam Jacksone1a44742010-06-25 15:32:14 -04002473 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2474 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002475 reg = FDI_RX_IMR(pipe);
2476 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002477 temp &= ~FDI_RX_SYMBOL_LOCK;
2478 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002479 I915_WRITE(reg, temp);
2480
2481 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002482 udelay(150);
2483
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002484 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002485 reg = FDI_TX_CTL(pipe);
2486 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002487 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2488 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002489 temp &= ~FDI_LINK_TRAIN_NONE;
2490 temp |= FDI_LINK_TRAIN_PATTERN_1;
2491 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2492 /* SNB-B */
2493 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002494 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002495
Daniel Vetterd74cf322012-10-26 10:58:13 +02002496 I915_WRITE(FDI_RX_MISC(pipe),
2497 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2498
Chris Wilson5eddb702010-09-11 13:48:45 +01002499 reg = FDI_RX_CTL(pipe);
2500 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002501 if (HAS_PCH_CPT(dev)) {
2502 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2503 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2504 } else {
2505 temp &= ~FDI_LINK_TRAIN_NONE;
2506 temp |= FDI_LINK_TRAIN_PATTERN_1;
2507 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002508 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2509
2510 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002511 udelay(150);
2512
Akshay Joshi0206e352011-08-16 15:34:10 -04002513 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002514 reg = FDI_TX_CTL(pipe);
2515 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002516 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2517 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002518 I915_WRITE(reg, temp);
2519
2520 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002521 udelay(500);
2522
Sean Paulfa37d392012-03-02 12:53:39 -05002523 for (retry = 0; retry < 5; retry++) {
2524 reg = FDI_RX_IIR(pipe);
2525 temp = I915_READ(reg);
2526 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2527 if (temp & FDI_RX_BIT_LOCK) {
2528 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2529 DRM_DEBUG_KMS("FDI train 1 done.\n");
2530 break;
2531 }
2532 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002533 }
Sean Paulfa37d392012-03-02 12:53:39 -05002534 if (retry < 5)
2535 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002536 }
2537 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002538 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002539
2540 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002541 reg = FDI_TX_CTL(pipe);
2542 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002543 temp &= ~FDI_LINK_TRAIN_NONE;
2544 temp |= FDI_LINK_TRAIN_PATTERN_2;
2545 if (IS_GEN6(dev)) {
2546 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2547 /* SNB-B */
2548 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2549 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002550 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002551
Chris Wilson5eddb702010-09-11 13:48:45 +01002552 reg = FDI_RX_CTL(pipe);
2553 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002554 if (HAS_PCH_CPT(dev)) {
2555 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2556 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2557 } else {
2558 temp &= ~FDI_LINK_TRAIN_NONE;
2559 temp |= FDI_LINK_TRAIN_PATTERN_2;
2560 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002561 I915_WRITE(reg, temp);
2562
2563 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002564 udelay(150);
2565
Akshay Joshi0206e352011-08-16 15:34:10 -04002566 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002567 reg = FDI_TX_CTL(pipe);
2568 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002569 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2570 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002571 I915_WRITE(reg, temp);
2572
2573 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002574 udelay(500);
2575
Sean Paulfa37d392012-03-02 12:53:39 -05002576 for (retry = 0; retry < 5; retry++) {
2577 reg = FDI_RX_IIR(pipe);
2578 temp = I915_READ(reg);
2579 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2580 if (temp & FDI_RX_SYMBOL_LOCK) {
2581 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2582 DRM_DEBUG_KMS("FDI train 2 done.\n");
2583 break;
2584 }
2585 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002586 }
Sean Paulfa37d392012-03-02 12:53:39 -05002587 if (retry < 5)
2588 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002589 }
2590 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002591 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002592
2593 DRM_DEBUG_KMS("FDI train done.\n");
2594}
2595
Jesse Barnes357555c2011-04-28 15:09:55 -07002596/* Manual link training for Ivy Bridge A0 parts */
2597static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2598{
2599 struct drm_device *dev = crtc->dev;
2600 struct drm_i915_private *dev_priv = dev->dev_private;
2601 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2602 int pipe = intel_crtc->pipe;
2603 u32 reg, temp, i;
2604
2605 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2606 for train result */
2607 reg = FDI_RX_IMR(pipe);
2608 temp = I915_READ(reg);
2609 temp &= ~FDI_RX_SYMBOL_LOCK;
2610 temp &= ~FDI_RX_BIT_LOCK;
2611 I915_WRITE(reg, temp);
2612
2613 POSTING_READ(reg);
2614 udelay(150);
2615
Daniel Vetter01a415f2012-10-27 15:58:40 +02002616 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
2617 I915_READ(FDI_RX_IIR(pipe)));
2618
Jesse Barnes357555c2011-04-28 15:09:55 -07002619 /* enable CPU FDI TX and PCH FDI RX */
2620 reg = FDI_TX_CTL(pipe);
2621 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002622 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2623 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Jesse Barnes357555c2011-04-28 15:09:55 -07002624 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2625 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
2626 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2627 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002628 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002629 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2630
Daniel Vetterd74cf322012-10-26 10:58:13 +02002631 I915_WRITE(FDI_RX_MISC(pipe),
2632 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2633
Jesse Barnes357555c2011-04-28 15:09:55 -07002634 reg = FDI_RX_CTL(pipe);
2635 temp = I915_READ(reg);
2636 temp &= ~FDI_LINK_TRAIN_AUTO;
2637 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2638 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002639 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002640 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2641
2642 POSTING_READ(reg);
2643 udelay(150);
2644
Akshay Joshi0206e352011-08-16 15:34:10 -04002645 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002646 reg = FDI_TX_CTL(pipe);
2647 temp = I915_READ(reg);
2648 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2649 temp |= snb_b_fdi_train_param[i];
2650 I915_WRITE(reg, temp);
2651
2652 POSTING_READ(reg);
2653 udelay(500);
2654
2655 reg = FDI_RX_IIR(pipe);
2656 temp = I915_READ(reg);
2657 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2658
2659 if (temp & FDI_RX_BIT_LOCK ||
2660 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2661 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Daniel Vetter01a415f2012-10-27 15:58:40 +02002662 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n", i);
Jesse Barnes357555c2011-04-28 15:09:55 -07002663 break;
2664 }
2665 }
2666 if (i == 4)
2667 DRM_ERROR("FDI train 1 fail!\n");
2668
2669 /* Train 2 */
2670 reg = FDI_TX_CTL(pipe);
2671 temp = I915_READ(reg);
2672 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2673 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2674 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2675 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2676 I915_WRITE(reg, temp);
2677
2678 reg = FDI_RX_CTL(pipe);
2679 temp = I915_READ(reg);
2680 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2681 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2682 I915_WRITE(reg, temp);
2683
2684 POSTING_READ(reg);
2685 udelay(150);
2686
Akshay Joshi0206e352011-08-16 15:34:10 -04002687 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002688 reg = FDI_TX_CTL(pipe);
2689 temp = I915_READ(reg);
2690 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2691 temp |= snb_b_fdi_train_param[i];
2692 I915_WRITE(reg, temp);
2693
2694 POSTING_READ(reg);
2695 udelay(500);
2696
2697 reg = FDI_RX_IIR(pipe);
2698 temp = I915_READ(reg);
2699 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2700
2701 if (temp & FDI_RX_SYMBOL_LOCK) {
2702 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Daniel Vetter01a415f2012-10-27 15:58:40 +02002703 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n", i);
Jesse Barnes357555c2011-04-28 15:09:55 -07002704 break;
2705 }
2706 }
2707 if (i == 4)
2708 DRM_ERROR("FDI train 2 fail!\n");
2709
2710 DRM_DEBUG_KMS("FDI train done.\n");
2711}
2712
Daniel Vetter88cefb62012-08-12 19:27:14 +02002713static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07002714{
Daniel Vetter88cefb62012-08-12 19:27:14 +02002715 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002716 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002717 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002718 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002719
Jesse Barnesc64e3112010-09-10 11:27:03 -07002720
Jesse Barnes0e23b992010-09-10 11:10:00 -07002721 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01002722 reg = FDI_RX_CTL(pipe);
2723 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002724 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
2725 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002726 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01002727 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2728
2729 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002730 udelay(200);
2731
2732 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002733 temp = I915_READ(reg);
2734 I915_WRITE(reg, temp | FDI_PCDCLK);
2735
2736 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002737 udelay(200);
2738
Paulo Zanoni20749732012-11-23 15:30:38 -02002739 /* Enable CPU FDI TX PLL, always on for Ironlake */
2740 reg = FDI_TX_CTL(pipe);
2741 temp = I915_READ(reg);
2742 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2743 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01002744
Paulo Zanoni20749732012-11-23 15:30:38 -02002745 POSTING_READ(reg);
2746 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002747 }
2748}
2749
Daniel Vetter88cefb62012-08-12 19:27:14 +02002750static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
2751{
2752 struct drm_device *dev = intel_crtc->base.dev;
2753 struct drm_i915_private *dev_priv = dev->dev_private;
2754 int pipe = intel_crtc->pipe;
2755 u32 reg, temp;
2756
2757 /* Switch from PCDclk to Rawclk */
2758 reg = FDI_RX_CTL(pipe);
2759 temp = I915_READ(reg);
2760 I915_WRITE(reg, temp & ~FDI_PCDCLK);
2761
2762 /* Disable CPU FDI TX PLL */
2763 reg = FDI_TX_CTL(pipe);
2764 temp = I915_READ(reg);
2765 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2766
2767 POSTING_READ(reg);
2768 udelay(100);
2769
2770 reg = FDI_RX_CTL(pipe);
2771 temp = I915_READ(reg);
2772 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2773
2774 /* Wait for the clocks to turn off. */
2775 POSTING_READ(reg);
2776 udelay(100);
2777}
2778
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002779static void ironlake_fdi_disable(struct drm_crtc *crtc)
2780{
2781 struct drm_device *dev = crtc->dev;
2782 struct drm_i915_private *dev_priv = dev->dev_private;
2783 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2784 int pipe = intel_crtc->pipe;
2785 u32 reg, temp;
2786
2787 /* disable CPU FDI tx and PCH FDI rx */
2788 reg = FDI_TX_CTL(pipe);
2789 temp = I915_READ(reg);
2790 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2791 POSTING_READ(reg);
2792
2793 reg = FDI_RX_CTL(pipe);
2794 temp = I915_READ(reg);
2795 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002796 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002797 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2798
2799 POSTING_READ(reg);
2800 udelay(100);
2801
2802 /* Ironlake workaround, disable clock pointer after downing FDI */
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002803 if (HAS_PCH_IBX(dev)) {
2804 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002805 }
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002806
2807 /* still set train pattern 1 */
2808 reg = FDI_TX_CTL(pipe);
2809 temp = I915_READ(reg);
2810 temp &= ~FDI_LINK_TRAIN_NONE;
2811 temp |= FDI_LINK_TRAIN_PATTERN_1;
2812 I915_WRITE(reg, temp);
2813
2814 reg = FDI_RX_CTL(pipe);
2815 temp = I915_READ(reg);
2816 if (HAS_PCH_CPT(dev)) {
2817 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2818 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2819 } else {
2820 temp &= ~FDI_LINK_TRAIN_NONE;
2821 temp |= FDI_LINK_TRAIN_PATTERN_1;
2822 }
2823 /* BPC in FDI rx is consistent with that in PIPECONF */
2824 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002825 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002826 I915_WRITE(reg, temp);
2827
2828 POSTING_READ(reg);
2829 udelay(100);
2830}
2831
Chris Wilson5bb61642012-09-27 21:25:58 +01002832static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2833{
2834 struct drm_device *dev = crtc->dev;
2835 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä10d83732013-01-29 18:13:34 +02002836 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5bb61642012-09-27 21:25:58 +01002837 unsigned long flags;
2838 bool pending;
2839
Ville Syrjälä10d83732013-01-29 18:13:34 +02002840 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2841 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
Chris Wilson5bb61642012-09-27 21:25:58 +01002842 return false;
2843
2844 spin_lock_irqsave(&dev->event_lock, flags);
2845 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2846 spin_unlock_irqrestore(&dev->event_lock, flags);
2847
2848 return pending;
2849}
2850
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002851static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2852{
Chris Wilson0f911282012-04-17 10:05:38 +01002853 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01002854 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002855
2856 if (crtc->fb == NULL)
2857 return;
2858
Daniel Vetter2c10d572012-12-20 21:24:07 +01002859 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
2860
Chris Wilson5bb61642012-09-27 21:25:58 +01002861 wait_event(dev_priv->pending_flip_queue,
2862 !intel_crtc_has_pending_flip(crtc));
2863
Chris Wilson0f911282012-04-17 10:05:38 +01002864 mutex_lock(&dev->struct_mutex);
2865 intel_finish_fb(crtc->fb);
2866 mutex_unlock(&dev->struct_mutex);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002867}
2868
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002869/* Program iCLKIP clock to the desired frequency */
2870static void lpt_program_iclkip(struct drm_crtc *crtc)
2871{
2872 struct drm_device *dev = crtc->dev;
2873 struct drm_i915_private *dev_priv = dev->dev_private;
2874 u32 divsel, phaseinc, auxdiv, phasedir = 0;
2875 u32 temp;
2876
Daniel Vetter09153002012-12-12 14:06:44 +01002877 mutex_lock(&dev_priv->dpio_lock);
2878
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002879 /* It is necessary to ungate the pixclk gate prior to programming
2880 * the divisors, and gate it back when it is done.
2881 */
2882 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
2883
2884 /* Disable SSCCTL */
2885 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002886 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
2887 SBI_SSCCTL_DISABLE,
2888 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002889
2890 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
2891 if (crtc->mode.clock == 20000) {
2892 auxdiv = 1;
2893 divsel = 0x41;
2894 phaseinc = 0x20;
2895 } else {
2896 /* The iCLK virtual clock root frequency is in MHz,
2897 * but the crtc->mode.clock in in KHz. To get the divisors,
2898 * it is necessary to divide one by another, so we
2899 * convert the virtual clock precision to KHz here for higher
2900 * precision.
2901 */
2902 u32 iclk_virtual_root_freq = 172800 * 1000;
2903 u32 iclk_pi_range = 64;
2904 u32 desired_divisor, msb_divisor_value, pi_value;
2905
2906 desired_divisor = (iclk_virtual_root_freq / crtc->mode.clock);
2907 msb_divisor_value = desired_divisor / iclk_pi_range;
2908 pi_value = desired_divisor % iclk_pi_range;
2909
2910 auxdiv = 0;
2911 divsel = msb_divisor_value - 2;
2912 phaseinc = pi_value;
2913 }
2914
2915 /* This should not happen with any sane values */
2916 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
2917 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
2918 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
2919 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
2920
2921 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
2922 crtc->mode.clock,
2923 auxdiv,
2924 divsel,
2925 phasedir,
2926 phaseinc);
2927
2928 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002929 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002930 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
2931 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
2932 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
2933 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
2934 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
2935 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002936 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002937
2938 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002939 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002940 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
2941 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002942 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002943
2944 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002945 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002946 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002947 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002948
2949 /* Wait for initialization time */
2950 udelay(24);
2951
2952 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01002953
2954 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002955}
2956
Daniel Vetter275f01b22013-05-03 11:49:47 +02002957static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
2958 enum pipe pch_transcoder)
2959{
2960 struct drm_device *dev = crtc->base.dev;
2961 struct drm_i915_private *dev_priv = dev->dev_private;
2962 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
2963
2964 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
2965 I915_READ(HTOTAL(cpu_transcoder)));
2966 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
2967 I915_READ(HBLANK(cpu_transcoder)));
2968 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
2969 I915_READ(HSYNC(cpu_transcoder)));
2970
2971 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
2972 I915_READ(VTOTAL(cpu_transcoder)));
2973 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
2974 I915_READ(VBLANK(cpu_transcoder)));
2975 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
2976 I915_READ(VSYNC(cpu_transcoder)));
2977 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
2978 I915_READ(VSYNCSHIFT(cpu_transcoder)));
2979}
2980
Jesse Barnesf67a5592011-01-05 10:31:48 -08002981/*
2982 * Enable PCH resources required for PCH ports:
2983 * - PCH PLLs
2984 * - FDI training & RX/TX
2985 * - update transcoder timings
2986 * - DP transcoding bits
2987 * - transcoder
2988 */
2989static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08002990{
2991 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002992 struct drm_i915_private *dev_priv = dev->dev_private;
2993 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2994 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01002995 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07002996
Daniel Vetterab9412b2013-05-03 11:49:46 +02002997 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01002998
Daniel Vettercd986ab2012-10-26 10:58:12 +02002999 /* Write the TU size bits before fdi link training, so that error
3000 * detection works. */
3001 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3002 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3003
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003004 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07003005 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003006
Daniel Vetter572deb32012-10-27 18:46:14 +02003007 /* XXX: pch pll's can be enabled any time before we enable the PCH
3008 * transcoder, and we actually should do this to not upset any PCH
3009 * transcoder that already use the clock when we share it.
3010 *
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003011 * Note that enable_shared_dpll tries to do the right thing, but
3012 * get_shared_dpll unconditionally resets the pll - we need that to have
3013 * the right LVDS enable sequence. */
3014 ironlake_enable_shared_dpll(intel_crtc);
Chris Wilson6f13b7b2012-05-13 09:54:09 +01003015
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003016 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003017 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07003018
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003019 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003020 temp |= TRANS_DPLL_ENABLE(pipe);
3021 sel = TRANS_DPLLB_SEL(pipe);
Daniel Vettera43f6e02013-06-07 23:10:32 +02003022 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003023 temp |= sel;
3024 else
3025 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003026 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003027 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003028
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003029 /* set transcoder timing, panel must allow it */
3030 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02003031 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003032
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003033 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003034
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003035 /* For PCH DP, enable TRANS_DP_CTL */
3036 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07003037 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3038 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003039 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003040 reg = TRANS_DP_CTL(pipe);
3041 temp = I915_READ(reg);
3042 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003043 TRANS_DP_SYNC_MASK |
3044 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003045 temp |= (TRANS_DP_OUTPUT_ENABLE |
3046 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003047 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003048
3049 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003050 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003051 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003052 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003053
3054 switch (intel_trans_dp_port_sel(crtc)) {
3055 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003056 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003057 break;
3058 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003059 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003060 break;
3061 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003062 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003063 break;
3064 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003065 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003066 }
3067
Chris Wilson5eddb702010-09-11 13:48:45 +01003068 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003069 }
3070
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003071 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003072}
3073
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003074static void lpt_pch_enable(struct drm_crtc *crtc)
3075{
3076 struct drm_device *dev = crtc->dev;
3077 struct drm_i915_private *dev_priv = dev->dev_private;
3078 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02003079 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003080
Daniel Vetterab9412b2013-05-03 11:49:46 +02003081 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003082
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003083 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003084
Paulo Zanoni0540e482012-10-31 18:12:40 -02003085 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02003086 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003087
Paulo Zanoni937bb612012-10-31 18:12:47 -02003088 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003089}
3090
Daniel Vettere2b78262013-06-07 23:10:03 +02003091static void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003092{
Daniel Vettere2b78262013-06-07 23:10:03 +02003093 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003094
3095 if (pll == NULL)
3096 return;
3097
3098 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003099 WARN(1, "bad %s refcount\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003100 return;
3101 }
3102
Daniel Vetterf4a091c2013-06-10 17:28:22 +02003103 if (--pll->refcount == 0) {
3104 WARN_ON(pll->on);
3105 WARN_ON(pll->active);
3106 }
3107
Daniel Vettera43f6e02013-06-07 23:10:32 +02003108 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003109}
3110
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003111static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003112{
Daniel Vettere2b78262013-06-07 23:10:03 +02003113 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3114 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3115 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003116
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003117 if (pll) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003118 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3119 crtc->base.base.id, pll->name);
Daniel Vettere2b78262013-06-07 23:10:03 +02003120 intel_put_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003121 }
3122
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003123 if (HAS_PCH_IBX(dev_priv->dev)) {
3124 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02003125 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003126 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003127
Daniel Vetter46edb022013-06-05 13:34:12 +02003128 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3129 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003130
3131 goto found;
3132 }
3133
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003134 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3135 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003136
3137 /* Only want to check enabled timings first */
3138 if (pll->refcount == 0)
3139 continue;
3140
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003141 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3142 sizeof(pll->hw_state)) == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003143 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
Daniel Vettere2b78262013-06-07 23:10:03 +02003144 crtc->base.base.id,
Daniel Vetter46edb022013-06-05 13:34:12 +02003145 pll->name, pll->refcount, pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003146
3147 goto found;
3148 }
3149 }
3150
3151 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003152 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3153 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003154 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003155 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3156 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003157 goto found;
3158 }
3159 }
3160
3161 return NULL;
3162
3163found:
Daniel Vettera43f6e02013-06-07 23:10:32 +02003164 crtc->config.shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02003165 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3166 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02003167
Daniel Vettercdbd2312013-06-05 13:34:03 +02003168 if (pll->active == 0) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02003169 memcpy(&pll->hw_state, &crtc->config.dpll_hw_state,
3170 sizeof(pll->hw_state));
3171
Daniel Vetter46edb022013-06-05 13:34:12 +02003172 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
Daniel Vettercdbd2312013-06-05 13:34:03 +02003173 WARN_ON(pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02003174 assert_shared_dpll_disabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003175
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02003176 pll->mode_set(dev_priv, pll);
Daniel Vettercdbd2312013-06-05 13:34:03 +02003177 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003178 pll->refcount++;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003179
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003180 return pll;
3181}
3182
Daniel Vettera1520312013-05-03 11:49:50 +02003183static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07003184{
3185 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01003186 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003187 u32 temp;
3188
3189 temp = I915_READ(dslreg);
3190 udelay(500);
3191 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07003192 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03003193 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07003194 }
3195}
3196
Jesse Barnesb074cec2013-04-25 12:55:02 -07003197static void ironlake_pfit_enable(struct intel_crtc *crtc)
3198{
3199 struct drm_device *dev = crtc->base.dev;
3200 struct drm_i915_private *dev_priv = dev->dev_private;
3201 int pipe = crtc->pipe;
3202
Jesse Barnes0ef37f32013-05-03 13:26:37 -07003203 if (crtc->config.pch_pfit.size) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07003204 /* Force use of hard-coded filter coefficients
3205 * as some pre-programmed values are broken,
3206 * e.g. x201.
3207 */
3208 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3209 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3210 PF_PIPE_SEL_IVB(pipe));
3211 else
3212 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3213 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3214 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08003215 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003216}
3217
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003218static void intel_enable_planes(struct drm_crtc *crtc)
3219{
3220 struct drm_device *dev = crtc->dev;
3221 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3222 struct intel_plane *intel_plane;
3223
3224 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3225 if (intel_plane->pipe == pipe)
3226 intel_plane_restore(&intel_plane->base);
3227}
3228
3229static void intel_disable_planes(struct drm_crtc *crtc)
3230{
3231 struct drm_device *dev = crtc->dev;
3232 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3233 struct intel_plane *intel_plane;
3234
3235 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3236 if (intel_plane->pipe == pipe)
3237 intel_plane_disable(&intel_plane->base);
3238}
3239
Jesse Barnesf67a5592011-01-05 10:31:48 -08003240static void ironlake_crtc_enable(struct drm_crtc *crtc)
3241{
3242 struct drm_device *dev = crtc->dev;
3243 struct drm_i915_private *dev_priv = dev->dev_private;
3244 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003245 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003246 int pipe = intel_crtc->pipe;
3247 int plane = intel_crtc->plane;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003248
Daniel Vetter08a48462012-07-02 11:43:47 +02003249 WARN_ON(!crtc->enabled);
3250
Jesse Barnesf67a5592011-01-05 10:31:48 -08003251 if (intel_crtc->active)
3252 return;
3253
3254 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003255
3256 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3257 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
3258
Jesse Barnesf67a5592011-01-05 10:31:48 -08003259 intel_update_watermarks(dev);
3260
Daniel Vetterf6736a12013-06-05 13:34:30 +02003261 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02003262 if (encoder->pre_enable)
3263 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003264
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003265 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02003266 /* Note: FDI PLL enabling _must_ be done before we enable the
3267 * cpu pipes, hence this is separate from all the other fdi/pch
3268 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02003269 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02003270 } else {
3271 assert_fdi_tx_disabled(dev_priv, pipe);
3272 assert_fdi_rx_disabled(dev_priv, pipe);
3273 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003274
Jesse Barnesb074cec2013-04-25 12:55:02 -07003275 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003276
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02003277 /*
3278 * On ILK+ LUT must be loaded before the pipe is running but with
3279 * clocks enabled
3280 */
3281 intel_crtc_load_lut(crtc);
3282
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003283 intel_enable_pipe(dev_priv, pipe,
3284 intel_crtc->config.has_pch_encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003285 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003286 intel_enable_planes(crtc);
Ville Syrjälä5c38d482013-06-04 13:49:00 +03003287 intel_crtc_update_cursor(crtc, true);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003288
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003289 if (intel_crtc->config.has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08003290 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003291
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003292 mutex_lock(&dev->struct_mutex);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003293 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003294 mutex_unlock(&dev->struct_mutex);
3295
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003296 for_each_encoder_on_crtc(dev, crtc, encoder)
3297 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02003298
3299 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02003300 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02003301
3302 /*
3303 * There seems to be a race in PCH platform hw (at least on some
3304 * outputs) where an enabled pipe still completes any pageflip right
3305 * away (as if the pipe is off) instead of waiting for vblank. As soon
3306 * as the first vblank happend, everything works as expected. Hence just
3307 * wait for one vblank before returning to avoid strange things
3308 * happening.
3309 */
3310 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003311}
3312
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003313/* IPS only exists on ULT machines and is tied to pipe A. */
3314static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
3315{
Damien Lespiauf5adf942013-06-24 18:29:34 +01003316 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003317}
3318
3319static void hsw_enable_ips(struct intel_crtc *crtc)
3320{
3321 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3322
3323 if (!crtc->config.ips_enabled)
3324 return;
3325
3326 /* We can only enable IPS after we enable a plane and wait for a vblank.
3327 * We guarantee that the plane is enabled by calling intel_enable_ips
3328 * only after intel_enable_plane. And intel_enable_plane already waits
3329 * for a vblank, so all we need to do here is to enable the IPS bit. */
3330 assert_plane_enabled(dev_priv, crtc->plane);
3331 I915_WRITE(IPS_CTL, IPS_ENABLE);
3332}
3333
3334static void hsw_disable_ips(struct intel_crtc *crtc)
3335{
3336 struct drm_device *dev = crtc->base.dev;
3337 struct drm_i915_private *dev_priv = dev->dev_private;
3338
3339 if (!crtc->config.ips_enabled)
3340 return;
3341
3342 assert_plane_enabled(dev_priv, crtc->plane);
3343 I915_WRITE(IPS_CTL, 0);
3344
3345 /* We need to wait for a vblank before we can disable the plane. */
3346 intel_wait_for_vblank(dev, crtc->pipe);
3347}
3348
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003349static void haswell_crtc_enable(struct drm_crtc *crtc)
3350{
3351 struct drm_device *dev = crtc->dev;
3352 struct drm_i915_private *dev_priv = dev->dev_private;
3353 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3354 struct intel_encoder *encoder;
3355 int pipe = intel_crtc->pipe;
3356 int plane = intel_crtc->plane;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003357
3358 WARN_ON(!crtc->enabled);
3359
3360 if (intel_crtc->active)
3361 return;
3362
3363 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003364
3365 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3366 if (intel_crtc->config.has_pch_encoder)
3367 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
3368
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003369 intel_update_watermarks(dev);
3370
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003371 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni04945642012-11-01 21:00:59 -02003372 dev_priv->display.fdi_link_train(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003373
3374 for_each_encoder_on_crtc(dev, crtc, encoder)
3375 if (encoder->pre_enable)
3376 encoder->pre_enable(encoder);
3377
Paulo Zanoni1f544382012-10-24 11:32:00 -02003378 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003379
Jesse Barnesb074cec2013-04-25 12:55:02 -07003380 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003381
3382 /*
3383 * On ILK+ LUT must be loaded before the pipe is running but with
3384 * clocks enabled
3385 */
3386 intel_crtc_load_lut(crtc);
3387
Paulo Zanoni1f544382012-10-24 11:32:00 -02003388 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00003389 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003390
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003391 intel_enable_pipe(dev_priv, pipe,
3392 intel_crtc->config.has_pch_encoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003393 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003394 intel_enable_planes(crtc);
Ville Syrjälä5c38d482013-06-04 13:49:00 +03003395 intel_crtc_update_cursor(crtc, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003396
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003397 hsw_enable_ips(intel_crtc);
3398
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003399 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003400 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003401
3402 mutex_lock(&dev->struct_mutex);
3403 intel_update_fbc(dev);
3404 mutex_unlock(&dev->struct_mutex);
3405
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003406 for_each_encoder_on_crtc(dev, crtc, encoder)
3407 encoder->enable(encoder);
3408
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003409 /*
3410 * There seems to be a race in PCH platform hw (at least on some
3411 * outputs) where an enabled pipe still completes any pageflip right
3412 * away (as if the pipe is off) instead of waiting for vblank. As soon
3413 * as the first vblank happend, everything works as expected. Hence just
3414 * wait for one vblank before returning to avoid strange things
3415 * happening.
3416 */
3417 intel_wait_for_vblank(dev, intel_crtc->pipe);
3418}
3419
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003420static void ironlake_pfit_disable(struct intel_crtc *crtc)
3421{
3422 struct drm_device *dev = crtc->base.dev;
3423 struct drm_i915_private *dev_priv = dev->dev_private;
3424 int pipe = crtc->pipe;
3425
3426 /* To avoid upsetting the power well on haswell only disable the pfit if
3427 * it's in use. The hw state code will make sure we get this right. */
3428 if (crtc->config.pch_pfit.size) {
3429 I915_WRITE(PF_CTL(pipe), 0);
3430 I915_WRITE(PF_WIN_POS(pipe), 0);
3431 I915_WRITE(PF_WIN_SZ(pipe), 0);
3432 }
3433}
3434
Jesse Barnes6be4a602010-09-10 10:26:01 -07003435static void ironlake_crtc_disable(struct drm_crtc *crtc)
3436{
3437 struct drm_device *dev = crtc->dev;
3438 struct drm_i915_private *dev_priv = dev->dev_private;
3439 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003440 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003441 int pipe = intel_crtc->pipe;
3442 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01003443 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003444
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003445
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003446 if (!intel_crtc->active)
3447 return;
3448
Daniel Vetterea9d7582012-07-10 10:42:52 +02003449 for_each_encoder_on_crtc(dev, crtc, encoder)
3450 encoder->disable(encoder);
3451
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003452 intel_crtc_wait_for_pending_flips(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003453 drm_vblank_off(dev, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003454
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07003455 if (dev_priv->fbc.plane == plane)
Chris Wilson973d04f2011-07-08 12:22:37 +01003456 intel_disable_fbc(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003457
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003458 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003459 intel_disable_planes(crtc);
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003460 intel_disable_plane(dev_priv, plane, pipe);
3461
Daniel Vetterd925c592013-06-05 13:34:04 +02003462 if (intel_crtc->config.has_pch_encoder)
3463 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
3464
Jesse Barnesb24e7172011-01-04 15:09:30 -08003465 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003466
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003467 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003468
Daniel Vetterbf49ec8c2012-09-06 22:15:40 +02003469 for_each_encoder_on_crtc(dev, crtc, encoder)
3470 if (encoder->post_disable)
3471 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003472
Daniel Vetterd925c592013-06-05 13:34:04 +02003473 if (intel_crtc->config.has_pch_encoder) {
3474 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003475
Daniel Vetterd925c592013-06-05 13:34:04 +02003476 ironlake_disable_pch_transcoder(dev_priv, pipe);
3477 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003478
Daniel Vetterd925c592013-06-05 13:34:04 +02003479 if (HAS_PCH_CPT(dev)) {
3480 /* disable TRANS_DP_CTL */
3481 reg = TRANS_DP_CTL(pipe);
3482 temp = I915_READ(reg);
3483 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
3484 TRANS_DP_PORT_SEL_MASK);
3485 temp |= TRANS_DP_PORT_SEL_NONE;
3486 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003487
Daniel Vetterd925c592013-06-05 13:34:04 +02003488 /* disable DPLL_SEL */
3489 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003490 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02003491 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003492 }
Daniel Vetterd925c592013-06-05 13:34:04 +02003493
3494 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003495 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02003496
3497 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003498 }
3499
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003500 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003501 intel_update_watermarks(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003502
3503 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01003504 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003505 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003506}
3507
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003508static void haswell_crtc_disable(struct drm_crtc *crtc)
3509{
3510 struct drm_device *dev = crtc->dev;
3511 struct drm_i915_private *dev_priv = dev->dev_private;
3512 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3513 struct intel_encoder *encoder;
3514 int pipe = intel_crtc->pipe;
3515 int plane = intel_crtc->plane;
Daniel Vetter3b117c82013-04-17 20:15:07 +02003516 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003517
3518 if (!intel_crtc->active)
3519 return;
3520
3521 for_each_encoder_on_crtc(dev, crtc, encoder)
3522 encoder->disable(encoder);
3523
3524 intel_crtc_wait_for_pending_flips(crtc);
3525 drm_vblank_off(dev, pipe);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003526
Rodrigo Vivi891348b2013-05-06 19:37:36 -03003527 /* FBC must be disabled before disabling the plane on HSW. */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07003528 if (dev_priv->fbc.plane == plane)
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003529 intel_disable_fbc(dev);
3530
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003531 hsw_disable_ips(intel_crtc);
3532
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003533 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003534 intel_disable_planes(crtc);
Rodrigo Vivi891348b2013-05-06 19:37:36 -03003535 intel_disable_plane(dev_priv, plane, pipe);
3536
Paulo Zanoni86642812013-04-12 17:57:57 -03003537 if (intel_crtc->config.has_pch_encoder)
3538 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003539 intel_disable_pipe(dev_priv, pipe);
3540
Paulo Zanoniad80a812012-10-24 16:06:19 -02003541 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003542
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003543 ironlake_pfit_disable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003544
Paulo Zanoni1f544382012-10-24 11:32:00 -02003545 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003546
3547 for_each_encoder_on_crtc(dev, crtc, encoder)
3548 if (encoder->post_disable)
3549 encoder->post_disable(encoder);
3550
Daniel Vetter88adfff2013-03-28 10:42:01 +01003551 if (intel_crtc->config.has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02003552 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni86642812013-04-12 17:57:57 -03003553 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02003554 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02003555 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003556
3557 intel_crtc->active = false;
3558 intel_update_watermarks(dev);
3559
3560 mutex_lock(&dev->struct_mutex);
3561 intel_update_fbc(dev);
3562 mutex_unlock(&dev->struct_mutex);
3563}
3564
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003565static void ironlake_crtc_off(struct drm_crtc *crtc)
3566{
3567 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003568 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003569}
3570
Paulo Zanoni6441ab52012-10-05 12:05:58 -03003571static void haswell_crtc_off(struct drm_crtc *crtc)
3572{
3573 intel_ddi_put_crtc_pll(crtc);
3574}
3575
Daniel Vetter02e792f2009-09-15 22:57:34 +02003576static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3577{
Daniel Vetter02e792f2009-09-15 22:57:34 +02003578 if (!enable && intel_crtc->overlay) {
Chris Wilson23f09ce2010-08-12 13:53:37 +01003579 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00003580 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02003581
Chris Wilson23f09ce2010-08-12 13:53:37 +01003582 mutex_lock(&dev->struct_mutex);
Chris Wilsonce453d82011-02-21 14:43:56 +00003583 dev_priv->mm.interruptible = false;
3584 (void) intel_overlay_switch_off(intel_crtc->overlay);
3585 dev_priv->mm.interruptible = true;
Chris Wilson23f09ce2010-08-12 13:53:37 +01003586 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02003587 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02003588
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01003589 /* Let userspace switch the overlay on again. In most cases userspace
3590 * has to recompute where to put it anyway.
3591 */
Daniel Vetter02e792f2009-09-15 22:57:34 +02003592}
3593
Egbert Eich61bc95c2013-03-04 09:24:38 -05003594/**
3595 * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
3596 * cursor plane briefly if not already running after enabling the display
3597 * plane.
3598 * This workaround avoids occasional blank screens when self refresh is
3599 * enabled.
3600 */
3601static void
3602g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
3603{
3604 u32 cntl = I915_READ(CURCNTR(pipe));
3605
3606 if ((cntl & CURSOR_MODE) == 0) {
3607 u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
3608
3609 I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
3610 I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
3611 intel_wait_for_vblank(dev_priv->dev, pipe);
3612 I915_WRITE(CURCNTR(pipe), cntl);
3613 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3614 I915_WRITE(FW_BLC_SELF, fw_bcl_self);
3615 }
3616}
3617
Jesse Barnes2dd24552013-04-25 12:55:01 -07003618static void i9xx_pfit_enable(struct intel_crtc *crtc)
3619{
3620 struct drm_device *dev = crtc->base.dev;
3621 struct drm_i915_private *dev_priv = dev->dev_private;
3622 struct intel_crtc_config *pipe_config = &crtc->config;
3623
Daniel Vetter328d8e82013-05-08 10:36:31 +02003624 if (!crtc->config.gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07003625 return;
3626
Daniel Vetterc0b03412013-05-28 12:05:54 +02003627 /*
3628 * The panel fitter should only be adjusted whilst the pipe is disabled,
3629 * according to register description and PRM.
3630 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07003631 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
3632 assert_pipe_disabled(dev_priv, crtc->pipe);
3633
Jesse Barnesb074cec2013-04-25 12:55:02 -07003634 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
3635 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02003636
3637 /* Border color in case we don't scale up to the full screen. Black by
3638 * default, change to something else for debugging. */
3639 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07003640}
3641
Jesse Barnes89b667f2013-04-18 14:51:36 -07003642static void valleyview_crtc_enable(struct drm_crtc *crtc)
3643{
3644 struct drm_device *dev = crtc->dev;
3645 struct drm_i915_private *dev_priv = dev->dev_private;
3646 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3647 struct intel_encoder *encoder;
3648 int pipe = intel_crtc->pipe;
3649 int plane = intel_crtc->plane;
3650
3651 WARN_ON(!crtc->enabled);
3652
3653 if (intel_crtc->active)
3654 return;
3655
3656 intel_crtc->active = true;
3657 intel_update_watermarks(dev);
3658
3659 mutex_lock(&dev_priv->dpio_lock);
3660
3661 for_each_encoder_on_crtc(dev, crtc, encoder)
3662 if (encoder->pre_pll_enable)
3663 encoder->pre_pll_enable(encoder);
3664
Daniel Vetter426115c2013-07-11 22:13:42 +02003665 vlv_enable_pll(intel_crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07003666
3667 for_each_encoder_on_crtc(dev, crtc, encoder)
3668 if (encoder->pre_enable)
3669 encoder->pre_enable(encoder);
3670
3671 /* VLV wants encoder enabling _before_ the pipe is up. */
3672 for_each_encoder_on_crtc(dev, crtc, encoder)
3673 encoder->enable(encoder);
3674
Jesse Barnes2dd24552013-04-25 12:55:01 -07003675 i9xx_pfit_enable(intel_crtc);
3676
Ville Syrjälä63cbb072013-06-04 13:48:59 +03003677 intel_crtc_load_lut(crtc);
3678
Jesse Barnes89b667f2013-04-18 14:51:36 -07003679 intel_enable_pipe(dev_priv, pipe, false);
3680 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003681 intel_enable_planes(crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07003682 intel_crtc_update_cursor(crtc, true);
3683
Ville Syrjäläf440eb12013-06-04 13:49:01 +03003684 intel_update_fbc(dev);
3685
Jesse Barnes89b667f2013-04-18 14:51:36 -07003686 mutex_unlock(&dev_priv->dpio_lock);
3687}
3688
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003689static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003690{
3691 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08003692 struct drm_i915_private *dev_priv = dev->dev_private;
3693 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003694 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08003695 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07003696 int plane = intel_crtc->plane;
Jesse Barnes79e53942008-11-07 14:24:08 -08003697
Daniel Vetter08a48462012-07-02 11:43:47 +02003698 WARN_ON(!crtc->enabled);
3699
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003700 if (intel_crtc->active)
3701 return;
3702
3703 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01003704 intel_update_watermarks(dev);
3705
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02003706 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02003707 if (encoder->pre_enable)
3708 encoder->pre_enable(encoder);
3709
Daniel Vetterf6736a12013-06-05 13:34:30 +02003710 i9xx_enable_pll(intel_crtc);
3711
Jesse Barnes2dd24552013-04-25 12:55:01 -07003712 i9xx_pfit_enable(intel_crtc);
3713
Ville Syrjälä63cbb072013-06-04 13:48:59 +03003714 intel_crtc_load_lut(crtc);
3715
Jesse Barnes040484a2011-01-03 12:14:26 -08003716 intel_enable_pipe(dev_priv, pipe, false);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003717 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003718 intel_enable_planes(crtc);
Ville Syrjälä22e407d2013-06-07 18:52:24 +03003719 /* The fixup needs to happen before cursor is enabled */
Egbert Eich61bc95c2013-03-04 09:24:38 -05003720 if (IS_G4X(dev))
3721 g4x_fixup_plane(dev_priv, pipe);
Ville Syrjälä22e407d2013-06-07 18:52:24 +03003722 intel_crtc_update_cursor(crtc, true);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003723
3724 /* Give the overlay scaler a chance to enable if it's on this pipe */
3725 intel_crtc_dpms_overlay(intel_crtc, true);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003726
Ville Syrjäläf440eb12013-06-04 13:49:01 +03003727 intel_update_fbc(dev);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003728
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003729 for_each_encoder_on_crtc(dev, crtc, encoder)
3730 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003731}
3732
Daniel Vetter87476d62013-04-11 16:29:06 +02003733static void i9xx_pfit_disable(struct intel_crtc *crtc)
3734{
3735 struct drm_device *dev = crtc->base.dev;
3736 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02003737
3738 if (!crtc->config.gmch_pfit.control)
3739 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02003740
3741 assert_pipe_disabled(dev_priv, crtc->pipe);
3742
Daniel Vetter328d8e82013-05-08 10:36:31 +02003743 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
3744 I915_READ(PFIT_CONTROL));
3745 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02003746}
3747
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003748static void i9xx_crtc_disable(struct drm_crtc *crtc)
3749{
3750 struct drm_device *dev = crtc->dev;
3751 struct drm_i915_private *dev_priv = dev->dev_private;
3752 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003753 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003754 int pipe = intel_crtc->pipe;
3755 int plane = intel_crtc->plane;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003756
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003757 if (!intel_crtc->active)
3758 return;
3759
Daniel Vetterea9d7582012-07-10 10:42:52 +02003760 for_each_encoder_on_crtc(dev, crtc, encoder)
3761 encoder->disable(encoder);
3762
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003763 /* Give the overlay scaler a chance to disable if it's on this pipe */
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003764 intel_crtc_wait_for_pending_flips(crtc);
3765 drm_vblank_off(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003766
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07003767 if (dev_priv->fbc.plane == plane)
Chris Wilson973d04f2011-07-08 12:22:37 +01003768 intel_disable_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003769
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003770 intel_crtc_dpms_overlay(intel_crtc, false);
3771 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003772 intel_disable_planes(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003773 intel_disable_plane(dev_priv, plane, pipe);
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003774
Jesse Barnesb24e7172011-01-04 15:09:30 -08003775 intel_disable_pipe(dev_priv, pipe);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02003776
Daniel Vetter87476d62013-04-11 16:29:06 +02003777 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02003778
Jesse Barnes89b667f2013-04-18 14:51:36 -07003779 for_each_encoder_on_crtc(dev, crtc, encoder)
3780 if (encoder->post_disable)
3781 encoder->post_disable(encoder);
3782
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08003783 intel_disable_pll(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003784
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003785 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003786 intel_update_fbc(dev);
3787 intel_update_watermarks(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003788}
3789
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003790static void i9xx_crtc_off(struct drm_crtc *crtc)
3791{
3792}
3793
Daniel Vetter976f8a22012-07-08 22:34:21 +02003794static void intel_crtc_update_sarea(struct drm_crtc *crtc,
3795 bool enabled)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003796{
3797 struct drm_device *dev = crtc->dev;
3798 struct drm_i915_master_private *master_priv;
3799 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3800 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08003801
3802 if (!dev->primary->master)
3803 return;
3804
3805 master_priv = dev->primary->master->driver_priv;
3806 if (!master_priv->sarea_priv)
3807 return;
3808
Jesse Barnes79e53942008-11-07 14:24:08 -08003809 switch (pipe) {
3810 case 0:
3811 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
3812 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
3813 break;
3814 case 1:
3815 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
3816 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
3817 break;
3818 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003819 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08003820 break;
3821 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003822}
3823
Daniel Vetter976f8a22012-07-08 22:34:21 +02003824/**
3825 * Sets the power management mode of the pipe and plane.
3826 */
3827void intel_crtc_update_dpms(struct drm_crtc *crtc)
Chris Wilsoncdd59982010-09-08 16:30:16 +01003828{
Chris Wilsoncdd59982010-09-08 16:30:16 +01003829 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003830 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter976f8a22012-07-08 22:34:21 +02003831 struct intel_encoder *intel_encoder;
3832 bool enable = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01003833
Daniel Vetter976f8a22012-07-08 22:34:21 +02003834 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
3835 enable |= intel_encoder->connectors_active;
3836
3837 if (enable)
3838 dev_priv->display.crtc_enable(crtc);
3839 else
3840 dev_priv->display.crtc_disable(crtc);
3841
3842 intel_crtc_update_sarea(crtc, enable);
3843}
3844
Daniel Vetter976f8a22012-07-08 22:34:21 +02003845static void intel_crtc_disable(struct drm_crtc *crtc)
3846{
3847 struct drm_device *dev = crtc->dev;
3848 struct drm_connector *connector;
3849 struct drm_i915_private *dev_priv = dev->dev_private;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08003850 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter976f8a22012-07-08 22:34:21 +02003851
3852 /* crtc should still be enabled when we disable it. */
3853 WARN_ON(!crtc->enabled);
3854
3855 dev_priv->display.crtc_disable(crtc);
Paulo Zanonic77bf562013-05-03 12:15:40 -03003856 intel_crtc->eld_vld = false;
Daniel Vetter976f8a22012-07-08 22:34:21 +02003857 intel_crtc_update_sarea(crtc, false);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003858 dev_priv->display.off(crtc);
3859
Chris Wilson931872f2012-01-16 23:01:13 +00003860 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
3861 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003862
3863 if (crtc->fb) {
3864 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01003865 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003866 mutex_unlock(&dev->struct_mutex);
Daniel Vetter976f8a22012-07-08 22:34:21 +02003867 crtc->fb = NULL;
3868 }
3869
3870 /* Update computed state. */
3871 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3872 if (!connector->encoder || !connector->encoder->crtc)
3873 continue;
3874
3875 if (connector->encoder->crtc != crtc)
3876 continue;
3877
3878 connector->dpms = DRM_MODE_DPMS_OFF;
3879 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01003880 }
3881}
3882
Daniel Vettera261b242012-07-26 19:21:47 +02003883void intel_modeset_disable(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08003884{
Daniel Vettera261b242012-07-26 19:21:47 +02003885 struct drm_crtc *crtc;
3886
3887 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3888 if (crtc->enabled)
3889 intel_crtc_disable(crtc);
3890 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003891}
3892
Chris Wilsonea5b2132010-08-04 13:50:23 +01003893void intel_encoder_destroy(struct drm_encoder *encoder)
3894{
Chris Wilson4ef69c72010-09-09 15:14:28 +01003895 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01003896
Chris Wilsonea5b2132010-08-04 13:50:23 +01003897 drm_encoder_cleanup(encoder);
3898 kfree(intel_encoder);
3899}
3900
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003901/* Simple dpms helper for encodres with just one connector, no cloning and only
3902 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
3903 * state of the entire output pipe. */
3904void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
3905{
3906 if (mode == DRM_MODE_DPMS_ON) {
3907 encoder->connectors_active = true;
3908
Daniel Vetterb2cabb02012-07-01 22:42:24 +02003909 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003910 } else {
3911 encoder->connectors_active = false;
3912
Daniel Vetterb2cabb02012-07-01 22:42:24 +02003913 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003914 }
3915}
3916
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003917/* Cross check the actual hw state with our own modeset state tracking (and it's
3918 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02003919static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003920{
3921 if (connector->get_hw_state(connector)) {
3922 struct intel_encoder *encoder = connector->encoder;
3923 struct drm_crtc *crtc;
3924 bool encoder_enabled;
3925 enum pipe pipe;
3926
3927 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
3928 connector->base.base.id,
3929 drm_get_connector_name(&connector->base));
3930
3931 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
3932 "wrong connector dpms state\n");
3933 WARN(connector->base.encoder != &encoder->base,
3934 "active connector not linked to encoder\n");
3935 WARN(!encoder->connectors_active,
3936 "encoder->connectors_active not set\n");
3937
3938 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
3939 WARN(!encoder_enabled, "encoder not enabled\n");
3940 if (WARN_ON(!encoder->base.crtc))
3941 return;
3942
3943 crtc = encoder->base.crtc;
3944
3945 WARN(!crtc->enabled, "crtc not enabled\n");
3946 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
3947 WARN(pipe != to_intel_crtc(crtc)->pipe,
3948 "encoder active on the wrong pipe\n");
3949 }
3950}
3951
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003952/* Even simpler default implementation, if there's really no special case to
3953 * consider. */
3954void intel_connector_dpms(struct drm_connector *connector, int mode)
3955{
3956 struct intel_encoder *encoder = intel_attached_encoder(connector);
3957
3958 /* All the simple cases only support two dpms states. */
3959 if (mode != DRM_MODE_DPMS_ON)
3960 mode = DRM_MODE_DPMS_OFF;
3961
3962 if (mode == connector->dpms)
3963 return;
3964
3965 connector->dpms = mode;
3966
3967 /* Only need to change hw state when actually enabled */
3968 if (encoder->base.crtc)
3969 intel_encoder_dpms(encoder, mode);
3970 else
Daniel Vetter8af6cf82012-07-10 09:50:11 +02003971 WARN_ON(encoder->connectors_active != false);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003972
Daniel Vetterb9805142012-08-31 17:37:33 +02003973 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003974}
3975
Daniel Vetterf0947c32012-07-02 13:10:34 +02003976/* Simple connector->get_hw_state implementation for encoders that support only
3977 * one connector and no cloning and hence the encoder state determines the state
3978 * of the connector. */
3979bool intel_connector_get_hw_state(struct intel_connector *connector)
3980{
Daniel Vetter24929352012-07-02 20:28:59 +02003981 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02003982 struct intel_encoder *encoder = connector->encoder;
3983
3984 return encoder->get_hw_state(encoder, &pipe);
3985}
3986
Daniel Vetter1857e1d2013-04-29 19:34:16 +02003987static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
3988 struct intel_crtc_config *pipe_config)
3989{
3990 struct drm_i915_private *dev_priv = dev->dev_private;
3991 struct intel_crtc *pipe_B_crtc =
3992 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
3993
3994 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
3995 pipe_name(pipe), pipe_config->fdi_lanes);
3996 if (pipe_config->fdi_lanes > 4) {
3997 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
3998 pipe_name(pipe), pipe_config->fdi_lanes);
3999 return false;
4000 }
4001
4002 if (IS_HASWELL(dev)) {
4003 if (pipe_config->fdi_lanes > 2) {
4004 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
4005 pipe_config->fdi_lanes);
4006 return false;
4007 } else {
4008 return true;
4009 }
4010 }
4011
4012 if (INTEL_INFO(dev)->num_pipes == 2)
4013 return true;
4014
4015 /* Ivybridge 3 pipe is really complicated */
4016 switch (pipe) {
4017 case PIPE_A:
4018 return true;
4019 case PIPE_B:
4020 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
4021 pipe_config->fdi_lanes > 2) {
4022 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4023 pipe_name(pipe), pipe_config->fdi_lanes);
4024 return false;
4025 }
4026 return true;
4027 case PIPE_C:
Daniel Vetter1e833f42013-02-19 22:31:57 +01004028 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004029 pipe_B_crtc->config.fdi_lanes <= 2) {
4030 if (pipe_config->fdi_lanes > 2) {
4031 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4032 pipe_name(pipe), pipe_config->fdi_lanes);
4033 return false;
4034 }
4035 } else {
4036 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
4037 return false;
4038 }
4039 return true;
4040 default:
4041 BUG();
4042 }
4043}
4044
Daniel Vettere29c22c2013-02-21 00:00:16 +01004045#define RETRY 1
4046static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
4047 struct intel_crtc_config *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02004048{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004049 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004050 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Daniel Vetterff9a6752013-06-01 17:16:21 +02004051 int lane, link_bw, fdi_dotclock;
Daniel Vettere29c22c2013-02-21 00:00:16 +01004052 bool setup_ok, needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004053
Daniel Vettere29c22c2013-02-21 00:00:16 +01004054retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02004055 /* FDI is a binary signal running at ~2.7GHz, encoding
4056 * each output octet as 10 bits. The actual frequency
4057 * is stored as a divider into a 100MHz clock, and the
4058 * mode pixel clock is stored in units of 1KHz.
4059 * Hence the bw of each lane in terms of the mode signal
4060 * is:
4061 */
4062 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
4063
Daniel Vetterff9a6752013-06-01 17:16:21 +02004064 fdi_dotclock = adjusted_mode->clock;
Daniel Vetteref1b4602013-06-01 17:17:04 +02004065 fdi_dotclock /= pipe_config->pixel_multiplier;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004066
Daniel Vetter2bd89a02013-06-01 17:16:19 +02004067 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02004068 pipe_config->pipe_bpp);
4069
4070 pipe_config->fdi_lanes = lane;
4071
Daniel Vetter2bd89a02013-06-01 17:16:19 +02004072 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02004073 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004074
Daniel Vettere29c22c2013-02-21 00:00:16 +01004075 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
4076 intel_crtc->pipe, pipe_config);
4077 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
4078 pipe_config->pipe_bpp -= 2*3;
4079 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
4080 pipe_config->pipe_bpp);
4081 needs_recompute = true;
4082 pipe_config->bw_constrained = true;
4083
4084 goto retry;
4085 }
4086
4087 if (needs_recompute)
4088 return RETRY;
4089
4090 return setup_ok ? 0 : -EINVAL;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004091}
4092
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004093static void hsw_compute_ips_config(struct intel_crtc *crtc,
4094 struct intel_crtc_config *pipe_config)
4095{
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03004096 pipe_config->ips_enabled = i915_enable_ips &&
4097 hsw_crtc_supports_ips(crtc) &&
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004098 pipe_config->pipe_bpp == 24;
4099}
4100
Daniel Vettera43f6e02013-06-07 23:10:32 +02004101static int intel_crtc_compute_config(struct intel_crtc *crtc,
Daniel Vettere29c22c2013-02-21 00:00:16 +01004102 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08004103{
Daniel Vettera43f6e02013-06-07 23:10:32 +02004104 struct drm_device *dev = crtc->base.dev;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01004105 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01004106
Eric Anholtbad720f2009-10-22 16:11:14 -07004107 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08004108 /* FDI link clock is fixed at 2.7G */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01004109 if (pipe_config->requested_mode.clock * 3
4110 > IRONLAKE_FDI_FREQ * 4)
Daniel Vettere29c22c2013-02-21 00:00:16 +01004111 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004112 }
Chris Wilson89749352010-09-12 18:25:19 +01004113
Daniel Vetterf9bef082012-04-15 19:53:19 +02004114 /* All interlaced capable intel hw wants timings in frames. Note though
4115 * that intel_lvds_mode_fixup does some funny tricks with the crtc
4116 * timings, so we need to be careful not to clobber these.*/
Daniel Vetter7ae89232013-03-27 00:44:52 +01004117 if (!pipe_config->timings_set)
Daniel Vetterf9bef082012-04-15 19:53:19 +02004118 drm_mode_set_crtcinfo(adjusted_mode, 0);
Chris Wilson89749352010-09-12 18:25:19 +01004119
Damien Lespiau8693a822013-05-03 18:48:11 +01004120 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
4121 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03004122 */
4123 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
4124 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01004125 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03004126
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004127 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004128 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004129 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004130 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
4131 * for lvds. */
4132 pipe_config->pipe_bpp = 8*3;
4133 }
4134
Damien Lespiauf5adf942013-06-24 18:29:34 +01004135 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02004136 hsw_compute_ips_config(crtc, pipe_config);
4137
4138 /* XXX: PCH clock sharing is done in ->mode_set, so make sure the old
4139 * clock survives for now. */
4140 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
4141 pipe_config->shared_dpll = crtc->config.shared_dpll;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004142
Daniel Vetter877d48d2013-04-19 11:24:43 +02004143 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02004144 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02004145
Daniel Vettere29c22c2013-02-21 00:00:16 +01004146 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08004147}
4148
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07004149static int valleyview_get_display_clock_speed(struct drm_device *dev)
4150{
4151 return 400000; /* FIXME */
4152}
4153
Jesse Barnese70236a2009-09-21 10:42:27 -07004154static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08004155{
Jesse Barnese70236a2009-09-21 10:42:27 -07004156 return 400000;
4157}
Jesse Barnes79e53942008-11-07 14:24:08 -08004158
Jesse Barnese70236a2009-09-21 10:42:27 -07004159static int i915_get_display_clock_speed(struct drm_device *dev)
4160{
4161 return 333000;
4162}
Jesse Barnes79e53942008-11-07 14:24:08 -08004163
Jesse Barnese70236a2009-09-21 10:42:27 -07004164static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
4165{
4166 return 200000;
4167}
Jesse Barnes79e53942008-11-07 14:24:08 -08004168
Jesse Barnese70236a2009-09-21 10:42:27 -07004169static int i915gm_get_display_clock_speed(struct drm_device *dev)
4170{
4171 u16 gcfgc = 0;
4172
4173 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4174
4175 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08004176 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07004177 else {
4178 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4179 case GC_DISPLAY_CLOCK_333_MHZ:
4180 return 333000;
4181 default:
4182 case GC_DISPLAY_CLOCK_190_200_MHZ:
4183 return 190000;
4184 }
4185 }
4186}
Jesse Barnes79e53942008-11-07 14:24:08 -08004187
Jesse Barnese70236a2009-09-21 10:42:27 -07004188static int i865_get_display_clock_speed(struct drm_device *dev)
4189{
4190 return 266000;
4191}
4192
4193static int i855_get_display_clock_speed(struct drm_device *dev)
4194{
4195 u16 hpllcc = 0;
4196 /* Assume that the hardware is in the high speed state. This
4197 * should be the default.
4198 */
4199 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
4200 case GC_CLOCK_133_200:
4201 case GC_CLOCK_100_200:
4202 return 200000;
4203 case GC_CLOCK_166_250:
4204 return 250000;
4205 case GC_CLOCK_100_133:
4206 return 133000;
4207 }
4208
4209 /* Shouldn't happen */
4210 return 0;
4211}
4212
4213static int i830_get_display_clock_speed(struct drm_device *dev)
4214{
4215 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08004216}
4217
Zhenyu Wang2c072452009-06-05 15:38:42 +08004218static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004219intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004220{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004221 while (*num > DATA_LINK_M_N_MASK ||
4222 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08004223 *num >>= 1;
4224 *den >>= 1;
4225 }
4226}
4227
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004228static void compute_m_n(unsigned int m, unsigned int n,
4229 uint32_t *ret_m, uint32_t *ret_n)
4230{
4231 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
4232 *ret_m = div_u64((uint64_t) m * *ret_n, n);
4233 intel_reduce_m_n_ratio(ret_m, ret_n);
4234}
4235
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004236void
4237intel_link_compute_m_n(int bits_per_pixel, int nlanes,
4238 int pixel_clock, int link_clock,
4239 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004240{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004241 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004242
4243 compute_m_n(bits_per_pixel * pixel_clock,
4244 link_clock * nlanes * 8,
4245 &m_n->gmch_m, &m_n->gmch_n);
4246
4247 compute_m_n(pixel_clock, link_clock,
4248 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004249}
4250
Chris Wilsona7615032011-01-12 17:04:08 +00004251static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
4252{
Keith Packard72bbe582011-09-26 16:09:45 -07004253 if (i915_panel_use_ssc >= 0)
4254 return i915_panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004255 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07004256 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00004257}
4258
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004259static int vlv_get_refclk(struct drm_crtc *crtc)
4260{
4261 struct drm_device *dev = crtc->dev;
4262 struct drm_i915_private *dev_priv = dev->dev_private;
4263 int refclk = 27000; /* for DP & HDMI */
4264
4265 return 100000; /* only one validated so far */
4266
4267 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
4268 refclk = 96000;
4269 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
4270 if (intel_panel_use_ssc(dev_priv))
4271 refclk = 100000;
4272 else
4273 refclk = 96000;
4274 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
4275 refclk = 100000;
4276 }
4277
4278 return refclk;
4279}
4280
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004281static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
4282{
4283 struct drm_device *dev = crtc->dev;
4284 struct drm_i915_private *dev_priv = dev->dev_private;
4285 int refclk;
4286
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004287 if (IS_VALLEYVIEW(dev)) {
4288 refclk = vlv_get_refclk(crtc);
4289 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004290 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004291 refclk = dev_priv->vbt.lvds_ssc_freq * 1000;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004292 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
4293 refclk / 1000);
4294 } else if (!IS_GEN2(dev)) {
4295 refclk = 96000;
4296 } else {
4297 refclk = 48000;
4298 }
4299
4300 return refclk;
4301}
4302
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004303static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004304{
Daniel Vetter7df00d72013-05-21 21:54:55 +02004305 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004306}
Daniel Vetterf47709a2013-03-28 10:42:02 +01004307
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004308static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
4309{
4310 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004311}
4312
Daniel Vetterf47709a2013-03-28 10:42:02 +01004313static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Jesse Barnesa7516a02011-12-15 12:30:37 -08004314 intel_clock_t *reduced_clock)
4315{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004316 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004317 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004318 int pipe = crtc->pipe;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004319 u32 fp, fp2 = 0;
4320
4321 if (IS_PINEVIEW(dev)) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004322 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004323 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004324 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004325 } else {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004326 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004327 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004328 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004329 }
4330
4331 I915_WRITE(FP0(pipe), fp);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004332 crtc->config.dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004333
Daniel Vetterf47709a2013-03-28 10:42:02 +01004334 crtc->lowfreq_avail = false;
4335 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Jesse Barnesa7516a02011-12-15 12:30:37 -08004336 reduced_clock && i915_powersave) {
4337 I915_WRITE(FP1(pipe), fp2);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004338 crtc->config.dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004339 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004340 } else {
4341 I915_WRITE(FP1(pipe), fp);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004342 crtc->config.dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004343 }
4344}
4345
Jesse Barnes89b667f2013-04-18 14:51:36 -07004346static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv)
4347{
4348 u32 reg_val;
4349
4350 /*
4351 * PLLB opamp always calibrates to max value of 0x3f, force enable it
4352 * and set it to a reasonable value instead.
4353 */
Jani Nikulaae992582013-05-22 15:36:19 +03004354 reg_val = vlv_dpio_read(dev_priv, DPIO_IREF(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004355 reg_val &= 0xffffff00;
4356 reg_val |= 0x00000030;
Jani Nikulaae992582013-05-22 15:36:19 +03004357 vlv_dpio_write(dev_priv, DPIO_IREF(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004358
Jani Nikulaae992582013-05-22 15:36:19 +03004359 reg_val = vlv_dpio_read(dev_priv, DPIO_CALIBRATION);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004360 reg_val &= 0x8cffffff;
4361 reg_val = 0x8c000000;
Jani Nikulaae992582013-05-22 15:36:19 +03004362 vlv_dpio_write(dev_priv, DPIO_CALIBRATION, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004363
Jani Nikulaae992582013-05-22 15:36:19 +03004364 reg_val = vlv_dpio_read(dev_priv, DPIO_IREF(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004365 reg_val &= 0xffffff00;
Jani Nikulaae992582013-05-22 15:36:19 +03004366 vlv_dpio_write(dev_priv, DPIO_IREF(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004367
Jani Nikulaae992582013-05-22 15:36:19 +03004368 reg_val = vlv_dpio_read(dev_priv, DPIO_CALIBRATION);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004369 reg_val &= 0x00ffffff;
4370 reg_val |= 0xb0000000;
Jani Nikulaae992582013-05-22 15:36:19 +03004371 vlv_dpio_write(dev_priv, DPIO_CALIBRATION, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004372}
4373
Daniel Vetterb5518422013-05-03 11:49:48 +02004374static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
4375 struct intel_link_m_n *m_n)
4376{
4377 struct drm_device *dev = crtc->base.dev;
4378 struct drm_i915_private *dev_priv = dev->dev_private;
4379 int pipe = crtc->pipe;
4380
Daniel Vettere3b95f12013-05-03 11:49:49 +02004381 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4382 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
4383 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
4384 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02004385}
4386
4387static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
4388 struct intel_link_m_n *m_n)
4389{
4390 struct drm_device *dev = crtc->base.dev;
4391 struct drm_i915_private *dev_priv = dev->dev_private;
4392 int pipe = crtc->pipe;
4393 enum transcoder transcoder = crtc->config.cpu_transcoder;
4394
4395 if (INTEL_INFO(dev)->gen >= 5) {
4396 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
4397 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
4398 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
4399 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
4400 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02004401 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4402 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
4403 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
4404 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02004405 }
4406}
4407
Daniel Vetter03afc4a2013-04-02 23:42:31 +02004408static void intel_dp_set_m_n(struct intel_crtc *crtc)
4409{
4410 if (crtc->config.has_pch_encoder)
4411 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4412 else
4413 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4414}
4415
Daniel Vetterf47709a2013-03-28 10:42:02 +01004416static void vlv_update_pll(struct intel_crtc *crtc)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004417{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004418 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004419 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004420 int pipe = crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004421 u32 dpll, mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004422 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004423 bool is_hdmi;
Daniel Vetter198a037f2013-04-19 11:14:37 +02004424 u32 coreclk, reg_val, dpll_md;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004425
Daniel Vetter09153002012-12-12 14:06:44 +01004426 mutex_lock(&dev_priv->dpio_lock);
4427
Jesse Barnes89b667f2013-04-18 14:51:36 -07004428 is_hdmi = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004429
Daniel Vetterf47709a2013-03-28 10:42:02 +01004430 bestn = crtc->config.dpll.n;
4431 bestm1 = crtc->config.dpll.m1;
4432 bestm2 = crtc->config.dpll.m2;
4433 bestp1 = crtc->config.dpll.p1;
4434 bestp2 = crtc->config.dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004435
Jesse Barnes89b667f2013-04-18 14:51:36 -07004436 /* See eDP HDMI DPIO driver vbios notes doc */
4437
4438 /* PLL B needs special handling */
4439 if (pipe)
4440 vlv_pllb_recal_opamp(dev_priv);
4441
4442 /* Set up Tx target for periodic Rcomp update */
Jani Nikulaae992582013-05-22 15:36:19 +03004443 vlv_dpio_write(dev_priv, DPIO_IREF_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004444
4445 /* Disable target IRef on PLL */
Jani Nikulaae992582013-05-22 15:36:19 +03004446 reg_val = vlv_dpio_read(dev_priv, DPIO_IREF_CTL(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004447 reg_val &= 0x00ffffff;
Jani Nikulaae992582013-05-22 15:36:19 +03004448 vlv_dpio_write(dev_priv, DPIO_IREF_CTL(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004449
4450 /* Disable fast lock */
Jani Nikulaae992582013-05-22 15:36:19 +03004451 vlv_dpio_write(dev_priv, DPIO_FASTCLK_DISABLE, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004452
4453 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004454 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
4455 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
4456 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004457 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07004458
4459 /*
4460 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
4461 * but we don't support that).
4462 * Note: don't use the DAC post divider as it seems unstable.
4463 */
4464 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Jani Nikulaae992582013-05-22 15:36:19 +03004465 vlv_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004466
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004467 mdiv |= DPIO_ENABLE_CALIBRATION;
Jani Nikulaae992582013-05-22 15:36:19 +03004468 vlv_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004469
Jesse Barnes89b667f2013-04-18 14:51:36 -07004470 /* Set HBR and RBR LPF coefficients */
Daniel Vetterff9a6752013-06-01 17:16:21 +02004471 if (crtc->config.port_clock == 162000 ||
Ville Syrjälä99750bd2013-06-14 14:02:52 +03004472 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
Jesse Barnes89b667f2013-04-18 14:51:36 -07004473 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
Ville Syrjälä4abb2c32013-06-14 14:02:53 +03004474 vlv_dpio_write(dev_priv, DPIO_LPF_COEFF(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03004475 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004476 else
Ville Syrjälä4abb2c32013-06-14 14:02:53 +03004477 vlv_dpio_write(dev_priv, DPIO_LPF_COEFF(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004478 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004479
Jesse Barnes89b667f2013-04-18 14:51:36 -07004480 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
4481 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
4482 /* Use SSC source */
4483 if (!pipe)
Jani Nikulaae992582013-05-22 15:36:19 +03004484 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004485 0x0df40000);
4486 else
Jani Nikulaae992582013-05-22 15:36:19 +03004487 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004488 0x0df70000);
4489 } else { /* HDMI or VGA */
4490 /* Use bend source */
4491 if (!pipe)
Jani Nikulaae992582013-05-22 15:36:19 +03004492 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004493 0x0df70000);
4494 else
Jani Nikulaae992582013-05-22 15:36:19 +03004495 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004496 0x0df40000);
4497 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004498
Jani Nikulaae992582013-05-22 15:36:19 +03004499 coreclk = vlv_dpio_read(dev_priv, DPIO_CORE_CLK(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004500 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
4501 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
4502 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
4503 coreclk |= 0x01000000;
Jani Nikulaae992582013-05-22 15:36:19 +03004504 vlv_dpio_write(dev_priv, DPIO_CORE_CLK(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004505
Jani Nikulaae992582013-05-22 15:36:19 +03004506 vlv_dpio_write(dev_priv, DPIO_PLL_CML(pipe), 0x87871000);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004507
Jesse Barnes89b667f2013-04-18 14:51:36 -07004508 /* Enable DPIO clock input */
4509 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
4510 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
4511 if (pipe)
4512 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004513
4514 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004515 crtc->config.dpll_hw_state.dpll = dpll;
4516
Daniel Vetteref1b4602013-06-01 17:17:04 +02004517 dpll_md = (crtc->config.pixel_multiplier - 1)
4518 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004519 crtc->config.dpll_hw_state.dpll_md = dpll_md;
4520
Daniel Vetterf47709a2013-03-28 10:42:02 +01004521 if (crtc->config.has_dp_encoder)
4522 intel_dp_set_m_n(crtc);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304523
Daniel Vetter09153002012-12-12 14:06:44 +01004524 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004525}
4526
Daniel Vetterf47709a2013-03-28 10:42:02 +01004527static void i9xx_update_pll(struct intel_crtc *crtc,
4528 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004529 int num_connectors)
4530{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004531 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004532 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004533 u32 dpll;
4534 bool is_sdvo;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004535 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004536
Daniel Vetterf47709a2013-03-28 10:42:02 +01004537 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304538
Daniel Vetterf47709a2013-03-28 10:42:02 +01004539 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
4540 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004541
4542 dpll = DPLL_VGA_MODE_DIS;
4543
Daniel Vetterf47709a2013-03-28 10:42:02 +01004544 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004545 dpll |= DPLLB_MODE_LVDS;
4546 else
4547 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01004548
Daniel Vetteref1b4602013-06-01 17:17:04 +02004549 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Daniel Vetter198a037f2013-04-19 11:14:37 +02004550 dpll |= (crtc->config.pixel_multiplier - 1)
4551 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004552 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02004553
4554 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02004555 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02004556
Daniel Vetterf47709a2013-03-28 10:42:02 +01004557 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
Daniel Vetter4a33e482013-07-06 12:52:05 +02004558 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004559
4560 /* compute bitmask from p1 value */
4561 if (IS_PINEVIEW(dev))
4562 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
4563 else {
4564 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4565 if (IS_G4X(dev) && reduced_clock)
4566 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
4567 }
4568 switch (clock->p2) {
4569 case 5:
4570 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4571 break;
4572 case 7:
4573 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4574 break;
4575 case 10:
4576 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4577 break;
4578 case 14:
4579 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4580 break;
4581 }
4582 if (INTEL_INFO(dev)->gen >= 4)
4583 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
4584
Daniel Vetter09ede542013-04-30 14:01:45 +02004585 if (crtc->config.sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004586 dpll |= PLL_REF_INPUT_TVCLKINBC;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004587 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004588 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4589 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4590 else
4591 dpll |= PLL_REF_INPUT_DREFCLK;
4592
4593 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004594 crtc->config.dpll_hw_state.dpll = dpll;
4595
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004596 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetteref1b4602013-06-01 17:17:04 +02004597 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
4598 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004599 crtc->config.dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004600 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02004601
4602 if (crtc->config.has_dp_encoder)
4603 intel_dp_set_m_n(crtc);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004604}
4605
Daniel Vetterf47709a2013-03-28 10:42:02 +01004606static void i8xx_update_pll(struct intel_crtc *crtc,
Daniel Vetterf47709a2013-03-28 10:42:02 +01004607 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004608 int num_connectors)
4609{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004610 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004611 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004612 u32 dpll;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004613 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004614
Daniel Vetterf47709a2013-03-28 10:42:02 +01004615 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304616
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004617 dpll = DPLL_VGA_MODE_DIS;
4618
Daniel Vetterf47709a2013-03-28 10:42:02 +01004619 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004620 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4621 } else {
4622 if (clock->p1 == 2)
4623 dpll |= PLL_P1_DIVIDE_BY_TWO;
4624 else
4625 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4626 if (clock->p2 == 4)
4627 dpll |= PLL_P2_DIVIDE_BY_4;
4628 }
4629
Daniel Vetter4a33e482013-07-06 12:52:05 +02004630 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
4631 dpll |= DPLL_DVO_2X_MODE;
4632
Daniel Vetterf47709a2013-03-28 10:42:02 +01004633 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004634 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4635 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4636 else
4637 dpll |= PLL_REF_INPUT_DREFCLK;
4638
4639 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004640 crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004641}
4642
Daniel Vetter8a654f32013-06-01 17:16:22 +02004643static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004644{
4645 struct drm_device *dev = intel_crtc->base.dev;
4646 struct drm_i915_private *dev_priv = dev->dev_private;
4647 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02004648 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02004649 struct drm_display_mode *adjusted_mode =
4650 &intel_crtc->config.adjusted_mode;
4651 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004652 uint32_t vsyncshift, crtc_vtotal, crtc_vblank_end;
4653
4654 /* We need to be careful not to changed the adjusted mode, for otherwise
4655 * the hw state checker will get angry at the mismatch. */
4656 crtc_vtotal = adjusted_mode->crtc_vtotal;
4657 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004658
4659 if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4660 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004661 crtc_vtotal -= 1;
4662 crtc_vblank_end -= 1;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004663 vsyncshift = adjusted_mode->crtc_hsync_start
4664 - adjusted_mode->crtc_htotal / 2;
4665 } else {
4666 vsyncshift = 0;
4667 }
4668
4669 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004670 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004671
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004672 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004673 (adjusted_mode->crtc_hdisplay - 1) |
4674 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004675 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004676 (adjusted_mode->crtc_hblank_start - 1) |
4677 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004678 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004679 (adjusted_mode->crtc_hsync_start - 1) |
4680 ((adjusted_mode->crtc_hsync_end - 1) << 16));
4681
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004682 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004683 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004684 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004685 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004686 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004687 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004688 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004689 (adjusted_mode->crtc_vsync_start - 1) |
4690 ((adjusted_mode->crtc_vsync_end - 1) << 16));
4691
Paulo Zanonib5e508d2012-10-24 11:34:43 -02004692 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
4693 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
4694 * documented on the DDI_FUNC_CTL register description, EDP Input Select
4695 * bits. */
4696 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
4697 (pipe == PIPE_B || pipe == PIPE_C))
4698 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
4699
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004700 /* pipesrc controls the size that is scaled from, which should
4701 * always be the user's requested size.
4702 */
4703 I915_WRITE(PIPESRC(pipe),
4704 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
4705}
4706
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02004707static void intel_get_pipe_timings(struct intel_crtc *crtc,
4708 struct intel_crtc_config *pipe_config)
4709{
4710 struct drm_device *dev = crtc->base.dev;
4711 struct drm_i915_private *dev_priv = dev->dev_private;
4712 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
4713 uint32_t tmp;
4714
4715 tmp = I915_READ(HTOTAL(cpu_transcoder));
4716 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
4717 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
4718 tmp = I915_READ(HBLANK(cpu_transcoder));
4719 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
4720 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
4721 tmp = I915_READ(HSYNC(cpu_transcoder));
4722 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
4723 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
4724
4725 tmp = I915_READ(VTOTAL(cpu_transcoder));
4726 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
4727 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
4728 tmp = I915_READ(VBLANK(cpu_transcoder));
4729 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
4730 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
4731 tmp = I915_READ(VSYNC(cpu_transcoder));
4732 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
4733 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
4734
4735 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
4736 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
4737 pipe_config->adjusted_mode.crtc_vtotal += 1;
4738 pipe_config->adjusted_mode.crtc_vblank_end += 1;
4739 }
4740
4741 tmp = I915_READ(PIPESRC(crtc->pipe));
4742 pipe_config->requested_mode.vdisplay = (tmp & 0xffff) + 1;
4743 pipe_config->requested_mode.hdisplay = ((tmp >> 16) & 0xffff) + 1;
4744}
4745
Jesse Barnesbabea612013-06-26 18:57:38 +03004746static void intel_crtc_mode_from_pipe_config(struct intel_crtc *intel_crtc,
4747 struct intel_crtc_config *pipe_config)
4748{
4749 struct drm_crtc *crtc = &intel_crtc->base;
4750
4751 crtc->mode.hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
4752 crtc->mode.htotal = pipe_config->adjusted_mode.crtc_htotal;
4753 crtc->mode.hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
4754 crtc->mode.hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
4755
4756 crtc->mode.vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
4757 crtc->mode.vtotal = pipe_config->adjusted_mode.crtc_vtotal;
4758 crtc->mode.vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
4759 crtc->mode.vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
4760
4761 crtc->mode.flags = pipe_config->adjusted_mode.flags;
4762
4763 crtc->mode.clock = pipe_config->adjusted_mode.clock;
4764 crtc->mode.flags |= pipe_config->adjusted_mode.flags;
4765}
4766
Daniel Vetter84b046f2013-02-19 18:48:54 +01004767static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
4768{
4769 struct drm_device *dev = intel_crtc->base.dev;
4770 struct drm_i915_private *dev_priv = dev->dev_private;
4771 uint32_t pipeconf;
4772
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02004773 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01004774
4775 if (intel_crtc->pipe == 0 && INTEL_INFO(dev)->gen < 4) {
4776 /* Enable pixel doubling when the dot clock is > 90% of the (display)
4777 * core speed.
4778 *
4779 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
4780 * pipe == 0 check?
4781 */
4782 if (intel_crtc->config.requested_mode.clock >
4783 dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
4784 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01004785 }
4786
Daniel Vetterff9ce462013-04-24 14:57:17 +02004787 /* only g4x and later have fancy bpc/dither controls */
4788 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02004789 /* Bspec claims that we can't use dithering for 30bpp pipes. */
4790 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
4791 pipeconf |= PIPECONF_DITHER_EN |
4792 PIPECONF_DITHER_TYPE_SP;
4793
4794 switch (intel_crtc->config.pipe_bpp) {
4795 case 18:
4796 pipeconf |= PIPECONF_6BPC;
4797 break;
4798 case 24:
4799 pipeconf |= PIPECONF_8BPC;
4800 break;
4801 case 30:
4802 pipeconf |= PIPECONF_10BPC;
4803 break;
4804 default:
4805 /* Case prevented by intel_choose_pipe_bpp_dither. */
4806 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01004807 }
4808 }
4809
4810 if (HAS_PIPE_CXSR(dev)) {
4811 if (intel_crtc->lowfreq_avail) {
4812 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
4813 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
4814 } else {
4815 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01004816 }
4817 }
4818
Daniel Vetter84b046f2013-02-19 18:48:54 +01004819 if (!IS_GEN2(dev) &&
4820 intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
4821 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
4822 else
4823 pipeconf |= PIPECONF_PROGRESSIVE;
4824
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02004825 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
4826 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03004827
Daniel Vetter84b046f2013-02-19 18:48:54 +01004828 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
4829 POSTING_READ(PIPECONF(intel_crtc->pipe));
4830}
4831
Eric Anholtf564048e2011-03-30 13:01:02 -07004832static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07004833 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02004834 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08004835{
4836 struct drm_device *dev = crtc->dev;
4837 struct drm_i915_private *dev_priv = dev->dev_private;
4838 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01004839 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08004840 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07004841 int plane = intel_crtc->plane;
Eric Anholtc751ce42010-03-25 11:48:48 -07004842 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07004843 intel_clock_t clock, reduced_clock;
Daniel Vetter84b046f2013-02-19 18:48:54 +01004844 u32 dspcntr;
Daniel Vettera16af722013-04-30 14:01:44 +02004845 bool ok, has_reduced_clock = false;
4846 bool is_lvds = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01004847 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08004848 const intel_limit_t *limit;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00004849 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08004850
Daniel Vetter6c2b7c12012-07-05 09:50:24 +02004851 for_each_encoder_on_crtc(dev, crtc, encoder) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004852 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004853 case INTEL_OUTPUT_LVDS:
4854 is_lvds = true;
4855 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08004856 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05004857
Eric Anholtc751ce42010-03-25 11:48:48 -07004858 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08004859 }
4860
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004861 refclk = i9xx_get_refclk(crtc, num_connectors);
Jesse Barnes79e53942008-11-07 14:24:08 -08004862
Ma Lingd4906092009-03-18 20:13:27 +08004863 /*
4864 * Returns a set of divisors for the desired target clock with the given
4865 * refclk, or FALSE. The returned values represent the clock equation:
4866 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
4867 */
Chris Wilson1b894b52010-12-14 20:04:54 +00004868 limit = intel_limit(crtc, refclk);
Daniel Vetterff9a6752013-06-01 17:16:21 +02004869 ok = dev_priv->display.find_dpll(limit, crtc,
4870 intel_crtc->config.port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02004871 refclk, NULL, &clock);
4872 if (!ok && !intel_crtc->config.clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004873 DRM_ERROR("Couldn't find PLL settings for mode!\n");
Eric Anholtf564048e2011-03-30 13:01:02 -07004874 return -EINVAL;
4875 }
4876
4877 /* Ensure that the cursor is valid for the new mode before changing... */
4878 intel_crtc_update_cursor(crtc, true);
4879
4880 if (is_lvds && dev_priv->lvds_downclock_avail) {
Sean Paulcec2f352012-01-10 15:09:36 -08004881 /*
4882 * Ensure we match the reduced clock's P to the target clock.
4883 * If the clocks don't match, we can't switch the display clock
4884 * by using the FP0/FP1. In such case we will disable the LVDS
4885 * downclock feature.
4886 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02004887 has_reduced_clock =
4888 dev_priv->display.find_dpll(limit, crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07004889 dev_priv->lvds_downclock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02004890 refclk, &clock,
Eric Anholtf564048e2011-03-30 13:01:02 -07004891 &reduced_clock);
Eric Anholtf564048e2011-03-30 13:01:02 -07004892 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01004893 /* Compat-code for transition, will disappear. */
4894 if (!intel_crtc->config.clock_set) {
4895 intel_crtc->config.dpll.n = clock.n;
4896 intel_crtc->config.dpll.m1 = clock.m1;
4897 intel_crtc->config.dpll.m2 = clock.m2;
4898 intel_crtc->config.dpll.p1 = clock.p1;
4899 intel_crtc->config.dpll.p2 = clock.p2;
4900 }
Eric Anholtf564048e2011-03-30 13:01:02 -07004901
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004902 if (IS_GEN2(dev))
Daniel Vetter8a654f32013-06-01 17:16:22 +02004903 i8xx_update_pll(intel_crtc,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304904 has_reduced_clock ? &reduced_clock : NULL,
4905 num_connectors);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004906 else if (IS_VALLEYVIEW(dev))
Daniel Vetterf47709a2013-03-28 10:42:02 +01004907 vlv_update_pll(intel_crtc);
Eric Anholtf564048e2011-03-30 13:01:02 -07004908 else
Daniel Vetterf47709a2013-03-28 10:42:02 +01004909 i9xx_update_pll(intel_crtc,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004910 has_reduced_clock ? &reduced_clock : NULL,
Jesse Barnes89b667f2013-04-18 14:51:36 -07004911 num_connectors);
Eric Anholtf564048e2011-03-30 13:01:02 -07004912
Eric Anholtf564048e2011-03-30 13:01:02 -07004913 /* Set up the display plane register */
4914 dspcntr = DISPPLANE_GAMMA_ENABLE;
4915
Jesse Barnesda6ecc52013-03-08 10:46:00 -08004916 if (!IS_VALLEYVIEW(dev)) {
4917 if (pipe == 0)
4918 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
4919 else
4920 dspcntr |= DISPPLANE_SEL_PIPE_B;
4921 }
Eric Anholtf564048e2011-03-30 13:01:02 -07004922
Daniel Vetter8a654f32013-06-01 17:16:22 +02004923 intel_set_pipe_timings(intel_crtc);
Eric Anholtf564048e2011-03-30 13:01:02 -07004924
4925 /* pipesrc and dspsize control the size that is scaled from,
4926 * which should always be the user's requested size.
4927 */
Eric Anholt929c77f2011-03-30 13:01:04 -07004928 I915_WRITE(DSPSIZE(plane),
4929 ((mode->vdisplay - 1) << 16) |
4930 (mode->hdisplay - 1));
4931 I915_WRITE(DSPPOS(plane), 0);
Eric Anholtf564048e2011-03-30 13:01:02 -07004932
Daniel Vetter84b046f2013-02-19 18:48:54 +01004933 i9xx_set_pipeconf(intel_crtc);
4934
Eric Anholtf564048e2011-03-30 13:01:02 -07004935 I915_WRITE(DSPCNTR(plane), dspcntr);
4936 POSTING_READ(DSPCNTR(plane));
4937
Daniel Vetter94352cf2012-07-05 22:51:56 +02004938 ret = intel_pipe_set_base(crtc, x, y, fb);
Eric Anholtf564048e2011-03-30 13:01:02 -07004939
4940 intel_update_watermarks(dev);
4941
Eric Anholtf564048e2011-03-30 13:01:02 -07004942 return ret;
4943}
4944
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02004945static void i9xx_get_pfit_config(struct intel_crtc *crtc,
4946 struct intel_crtc_config *pipe_config)
4947{
4948 struct drm_device *dev = crtc->base.dev;
4949 struct drm_i915_private *dev_priv = dev->dev_private;
4950 uint32_t tmp;
4951
4952 tmp = I915_READ(PFIT_CONTROL);
4953
4954 if (INTEL_INFO(dev)->gen < 4) {
4955 if (crtc->pipe != PIPE_B)
4956 return;
4957
4958 /* gen2/3 store dither state in pfit control, needs to match */
4959 pipe_config->gmch_pfit.control = tmp & PANEL_8TO6_DITHER_ENABLE;
4960 } else {
4961 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
4962 return;
4963 }
4964
4965 if (!(tmp & PFIT_ENABLE))
4966 return;
4967
4968 pipe_config->gmch_pfit.control = I915_READ(PFIT_CONTROL);
4969 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
4970 if (INTEL_INFO(dev)->gen < 5)
4971 pipe_config->gmch_pfit.lvds_border_bits =
4972 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
4973}
4974
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01004975static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
4976 struct intel_crtc_config *pipe_config)
4977{
4978 struct drm_device *dev = crtc->base.dev;
4979 struct drm_i915_private *dev_priv = dev->dev_private;
4980 uint32_t tmp;
4981
Daniel Vettere143a212013-07-04 12:01:15 +02004982 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02004983 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02004984
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01004985 tmp = I915_READ(PIPECONF(crtc->pipe));
4986 if (!(tmp & PIPECONF_ENABLE))
4987 return false;
4988
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02004989 intel_get_pipe_timings(crtc, pipe_config);
4990
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02004991 i9xx_get_pfit_config(crtc, pipe_config);
4992
Daniel Vetter6c49f242013-06-06 12:45:25 +02004993 if (INTEL_INFO(dev)->gen >= 4) {
4994 tmp = I915_READ(DPLL_MD(crtc->pipe));
4995 pipe_config->pixel_multiplier =
4996 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
4997 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004998 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02004999 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
5000 tmp = I915_READ(DPLL(crtc->pipe));
5001 pipe_config->pixel_multiplier =
5002 ((tmp & SDVO_MULTIPLIER_MASK)
5003 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
5004 } else {
5005 /* Note that on i915G/GM the pixel multiplier is in the sdvo
5006 * port and will be fixed up in the encoder->get_config
5007 * function. */
5008 pipe_config->pixel_multiplier = 1;
5009 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005010 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
5011 if (!IS_VALLEYVIEW(dev)) {
5012 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
5013 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03005014 } else {
5015 /* Mask out read-only status bits. */
5016 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
5017 DPLL_PORTC_READY_MASK |
5018 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005019 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02005020
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005021 return true;
5022}
5023
Paulo Zanonidde86e22012-12-01 12:04:25 -02005024static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07005025{
5026 struct drm_i915_private *dev_priv = dev->dev_private;
5027 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005028 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005029 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005030 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005031 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005032 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07005033 bool has_ck505 = false;
5034 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005035
5036 /* We need to take the global config into account */
Keith Packard199e5d72011-09-22 12:01:57 -07005037 list_for_each_entry(encoder, &mode_config->encoder_list,
5038 base.head) {
5039 switch (encoder->type) {
5040 case INTEL_OUTPUT_LVDS:
5041 has_panel = true;
5042 has_lvds = true;
5043 break;
5044 case INTEL_OUTPUT_EDP:
5045 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03005046 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07005047 has_cpu_edp = true;
5048 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005049 }
5050 }
5051
Keith Packard99eb6a02011-09-26 14:29:12 -07005052 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005053 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07005054 can_ssc = has_ck505;
5055 } else {
5056 has_ck505 = false;
5057 can_ssc = true;
5058 }
5059
Imre Deak2de69052013-05-08 13:14:04 +03005060 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
5061 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005062
5063 /* Ironlake: try to setup display ref clock before DPLL
5064 * enabling. This is only under driver's control after
5065 * PCH B stepping, previous chipset stepping should be
5066 * ignoring this setting.
5067 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005068 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005069
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005070 /* As we must carefully and slowly disable/enable each source in turn,
5071 * compute the final state we want first and check if we need to
5072 * make any changes at all.
5073 */
5074 final = val;
5075 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07005076 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005077 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07005078 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005079 final |= DREF_NONSPREAD_SOURCE_ENABLE;
5080
5081 final &= ~DREF_SSC_SOURCE_MASK;
5082 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5083 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005084
Keith Packard199e5d72011-09-22 12:01:57 -07005085 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005086 final |= DREF_SSC_SOURCE_ENABLE;
5087
5088 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5089 final |= DREF_SSC1_ENABLE;
5090
5091 if (has_cpu_edp) {
5092 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5093 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
5094 else
5095 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
5096 } else
5097 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5098 } else {
5099 final |= DREF_SSC_SOURCE_DISABLE;
5100 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5101 }
5102
5103 if (final == val)
5104 return;
5105
5106 /* Always enable nonspread source */
5107 val &= ~DREF_NONSPREAD_SOURCE_MASK;
5108
5109 if (has_ck505)
5110 val |= DREF_NONSPREAD_CK505_ENABLE;
5111 else
5112 val |= DREF_NONSPREAD_SOURCE_ENABLE;
5113
5114 if (has_panel) {
5115 val &= ~DREF_SSC_SOURCE_MASK;
5116 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005117
Keith Packard199e5d72011-09-22 12:01:57 -07005118 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07005119 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005120 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005121 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02005122 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005123 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005124
5125 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005126 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005127 POSTING_READ(PCH_DREF_CONTROL);
5128 udelay(200);
5129
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005130 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005131
5132 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07005133 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07005134 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005135 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005136 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005137 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07005138 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005139 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005140 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005141 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005142
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005143 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005144 POSTING_READ(PCH_DREF_CONTROL);
5145 udelay(200);
5146 } else {
5147 DRM_DEBUG_KMS("Disabling SSC entirely\n");
5148
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005149 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07005150
5151 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005152 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005153
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005154 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005155 POSTING_READ(PCH_DREF_CONTROL);
5156 udelay(200);
5157
5158 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005159 val &= ~DREF_SSC_SOURCE_MASK;
5160 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005161
5162 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005163 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005164
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005165 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005166 POSTING_READ(PCH_DREF_CONTROL);
5167 udelay(200);
5168 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005169
5170 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005171}
5172
Paulo Zanonidde86e22012-12-01 12:04:25 -02005173/* Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O. */
5174static void lpt_init_pch_refclk(struct drm_device *dev)
5175{
5176 struct drm_i915_private *dev_priv = dev->dev_private;
5177 struct drm_mode_config *mode_config = &dev->mode_config;
5178 struct intel_encoder *encoder;
5179 bool has_vga = false;
5180 bool is_sdv = false;
5181 u32 tmp;
5182
5183 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5184 switch (encoder->type) {
5185 case INTEL_OUTPUT_ANALOG:
5186 has_vga = true;
5187 break;
5188 }
5189 }
5190
5191 if (!has_vga)
5192 return;
5193
Daniel Vetterc00db242013-01-22 15:33:27 +01005194 mutex_lock(&dev_priv->dpio_lock);
5195
Paulo Zanonidde86e22012-12-01 12:04:25 -02005196 /* XXX: Rip out SDV support once Haswell ships for real. */
5197 if (IS_HASWELL(dev) && (dev->pci_device & 0xFF00) == 0x0C00)
5198 is_sdv = true;
5199
5200 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5201 tmp &= ~SBI_SSCCTL_DISABLE;
5202 tmp |= SBI_SSCCTL_PATHALT;
5203 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5204
5205 udelay(24);
5206
5207 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5208 tmp &= ~SBI_SSCCTL_PATHALT;
5209 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5210
5211 if (!is_sdv) {
5212 tmp = I915_READ(SOUTH_CHICKEN2);
5213 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
5214 I915_WRITE(SOUTH_CHICKEN2, tmp);
5215
5216 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
5217 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
5218 DRM_ERROR("FDI mPHY reset assert timeout\n");
5219
5220 tmp = I915_READ(SOUTH_CHICKEN2);
5221 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
5222 I915_WRITE(SOUTH_CHICKEN2, tmp);
5223
5224 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
5225 FDI_MPHY_IOSFSB_RESET_STATUS) == 0,
5226 100))
5227 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
5228 }
5229
5230 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
5231 tmp &= ~(0xFF << 24);
5232 tmp |= (0x12 << 24);
5233 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
5234
Paulo Zanonidde86e22012-12-01 12:04:25 -02005235 if (is_sdv) {
5236 tmp = intel_sbi_read(dev_priv, 0x800C, SBI_MPHY);
5237 tmp |= 0x7FFF;
5238 intel_sbi_write(dev_priv, 0x800C, tmp, SBI_MPHY);
5239 }
5240
5241 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
5242 tmp |= (1 << 11);
5243 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
5244
5245 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
5246 tmp |= (1 << 11);
5247 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
5248
5249 if (is_sdv) {
5250 tmp = intel_sbi_read(dev_priv, 0x2038, SBI_MPHY);
5251 tmp |= (0x3F << 24) | (0xF << 20) | (0xF << 16);
5252 intel_sbi_write(dev_priv, 0x2038, tmp, SBI_MPHY);
5253
5254 tmp = intel_sbi_read(dev_priv, 0x2138, SBI_MPHY);
5255 tmp |= (0x3F << 24) | (0xF << 20) | (0xF << 16);
5256 intel_sbi_write(dev_priv, 0x2138, tmp, SBI_MPHY);
5257
5258 tmp = intel_sbi_read(dev_priv, 0x203C, SBI_MPHY);
5259 tmp |= (0x3F << 8);
5260 intel_sbi_write(dev_priv, 0x203C, tmp, SBI_MPHY);
5261
5262 tmp = intel_sbi_read(dev_priv, 0x213C, SBI_MPHY);
5263 tmp |= (0x3F << 8);
5264 intel_sbi_write(dev_priv, 0x213C, tmp, SBI_MPHY);
5265 }
5266
5267 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
5268 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5269 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
5270
5271 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
5272 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5273 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
5274
5275 if (!is_sdv) {
5276 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
5277 tmp &= ~(7 << 13);
5278 tmp |= (5 << 13);
5279 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
5280
5281 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
5282 tmp &= ~(7 << 13);
5283 tmp |= (5 << 13);
5284 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
5285 }
5286
5287 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
5288 tmp &= ~0xFF;
5289 tmp |= 0x1C;
5290 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
5291
5292 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
5293 tmp &= ~0xFF;
5294 tmp |= 0x1C;
5295 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
5296
5297 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
5298 tmp &= ~(0xFF << 16);
5299 tmp |= (0x1C << 16);
5300 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
5301
5302 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
5303 tmp &= ~(0xFF << 16);
5304 tmp |= (0x1C << 16);
5305 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
5306
5307 if (!is_sdv) {
5308 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
5309 tmp |= (1 << 27);
5310 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
5311
5312 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
5313 tmp |= (1 << 27);
5314 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
5315
5316 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
5317 tmp &= ~(0xF << 28);
5318 tmp |= (4 << 28);
5319 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
5320
5321 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
5322 tmp &= ~(0xF << 28);
5323 tmp |= (4 << 28);
5324 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
5325 }
5326
5327 /* ULT uses SBI_GEN0, but ULT doesn't have VGA, so we don't care. */
5328 tmp = intel_sbi_read(dev_priv, SBI_DBUFF0, SBI_ICLK);
5329 tmp |= SBI_DBUFF0_ENABLE;
5330 intel_sbi_write(dev_priv, SBI_DBUFF0, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01005331
5332 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005333}
5334
5335/*
5336 * Initialize reference clocks when the driver loads
5337 */
5338void intel_init_pch_refclk(struct drm_device *dev)
5339{
5340 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
5341 ironlake_init_pch_refclk(dev);
5342 else if (HAS_PCH_LPT(dev))
5343 lpt_init_pch_refclk(dev);
5344}
5345
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005346static int ironlake_get_refclk(struct drm_crtc *crtc)
5347{
5348 struct drm_device *dev = crtc->dev;
5349 struct drm_i915_private *dev_priv = dev->dev_private;
5350 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005351 int num_connectors = 0;
5352 bool is_lvds = false;
5353
Daniel Vetter6c2b7c12012-07-05 09:50:24 +02005354 for_each_encoder_on_crtc(dev, crtc, encoder) {
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005355 switch (encoder->type) {
5356 case INTEL_OUTPUT_LVDS:
5357 is_lvds = true;
5358 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005359 }
5360 num_connectors++;
5361 }
5362
5363 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
5364 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005365 dev_priv->vbt.lvds_ssc_freq);
5366 return dev_priv->vbt.lvds_ssc_freq * 1000;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005367 }
5368
5369 return 120000;
5370}
5371
Daniel Vetter6ff93602013-04-19 11:24:36 +02005372static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03005373{
5374 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5375 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5376 int pipe = intel_crtc->pipe;
5377 uint32_t val;
5378
Daniel Vetter78114072013-06-13 00:54:57 +02005379 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03005380
Daniel Vetter965e0c42013-03-27 00:44:57 +01005381 switch (intel_crtc->config.pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03005382 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005383 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005384 break;
5385 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005386 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005387 break;
5388 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005389 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005390 break;
5391 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005392 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005393 break;
5394 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03005395 /* Case prevented by intel_choose_pipe_bpp_dither. */
5396 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03005397 }
5398
Daniel Vetterd8b32242013-04-25 17:54:44 +02005399 if (intel_crtc->config.dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03005400 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5401
Daniel Vetter6ff93602013-04-19 11:24:36 +02005402 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03005403 val |= PIPECONF_INTERLACED_ILK;
5404 else
5405 val |= PIPECONF_PROGRESSIVE;
5406
Daniel Vetter50f3b012013-03-27 00:44:56 +01005407 if (intel_crtc->config.limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005408 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005409
Paulo Zanonic8203562012-09-12 10:06:29 -03005410 I915_WRITE(PIPECONF(pipe), val);
5411 POSTING_READ(PIPECONF(pipe));
5412}
5413
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005414/*
5415 * Set up the pipe CSC unit.
5416 *
5417 * Currently only full range RGB to limited range RGB conversion
5418 * is supported, but eventually this should handle various
5419 * RGB<->YCbCr scenarios as well.
5420 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01005421static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005422{
5423 struct drm_device *dev = crtc->dev;
5424 struct drm_i915_private *dev_priv = dev->dev_private;
5425 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5426 int pipe = intel_crtc->pipe;
5427 uint16_t coeff = 0x7800; /* 1.0 */
5428
5429 /*
5430 * TODO: Check what kind of values actually come out of the pipe
5431 * with these coeff/postoff values and adjust to get the best
5432 * accuracy. Perhaps we even need to take the bpc value into
5433 * consideration.
5434 */
5435
Daniel Vetter50f3b012013-03-27 00:44:56 +01005436 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005437 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
5438
5439 /*
5440 * GY/GU and RY/RU should be the other way around according
5441 * to BSpec, but reality doesn't agree. Just set them up in
5442 * a way that results in the correct picture.
5443 */
5444 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
5445 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
5446
5447 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
5448 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
5449
5450 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
5451 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
5452
5453 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
5454 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
5455 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
5456
5457 if (INTEL_INFO(dev)->gen > 6) {
5458 uint16_t postoff = 0;
5459
Daniel Vetter50f3b012013-03-27 00:44:56 +01005460 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005461 postoff = (16 * (1 << 13) / 255) & 0x1fff;
5462
5463 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
5464 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
5465 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
5466
5467 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
5468 } else {
5469 uint32_t mode = CSC_MODE_YUV_TO_RGB;
5470
Daniel Vetter50f3b012013-03-27 00:44:56 +01005471 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005472 mode |= CSC_BLACK_SCREEN_OFFSET;
5473
5474 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
5475 }
5476}
5477
Daniel Vetter6ff93602013-04-19 11:24:36 +02005478static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005479{
5480 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5481 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02005482 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005483 uint32_t val;
5484
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02005485 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005486
Daniel Vetterd8b32242013-04-25 17:54:44 +02005487 if (intel_crtc->config.dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005488 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5489
Daniel Vetter6ff93602013-04-19 11:24:36 +02005490 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005491 val |= PIPECONF_INTERLACED_ILK;
5492 else
5493 val |= PIPECONF_PROGRESSIVE;
5494
Paulo Zanoni702e7a52012-10-23 18:29:59 -02005495 I915_WRITE(PIPECONF(cpu_transcoder), val);
5496 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02005497
5498 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
5499 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005500}
5501
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005502static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005503 intel_clock_t *clock,
5504 bool *has_reduced_clock,
5505 intel_clock_t *reduced_clock)
5506{
5507 struct drm_device *dev = crtc->dev;
5508 struct drm_i915_private *dev_priv = dev->dev_private;
5509 struct intel_encoder *intel_encoder;
5510 int refclk;
5511 const intel_limit_t *limit;
Daniel Vettera16af722013-04-30 14:01:44 +02005512 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005513
5514 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5515 switch (intel_encoder->type) {
5516 case INTEL_OUTPUT_LVDS:
5517 is_lvds = true;
5518 break;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005519 }
5520 }
5521
5522 refclk = ironlake_get_refclk(crtc);
5523
5524 /*
5525 * Returns a set of divisors for the desired target clock with the given
5526 * refclk, or FALSE. The returned values represent the clock equation:
5527 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
5528 */
5529 limit = intel_limit(crtc, refclk);
Daniel Vetterff9a6752013-06-01 17:16:21 +02005530 ret = dev_priv->display.find_dpll(limit, crtc,
5531 to_intel_crtc(crtc)->config.port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02005532 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005533 if (!ret)
5534 return false;
5535
5536 if (is_lvds && dev_priv->lvds_downclock_avail) {
5537 /*
5538 * Ensure we match the reduced clock's P to the target clock.
5539 * If the clocks don't match, we can't switch the display clock
5540 * by using the FP0/FP1. In such case we will disable the LVDS
5541 * downclock feature.
5542 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02005543 *has_reduced_clock =
5544 dev_priv->display.find_dpll(limit, crtc,
5545 dev_priv->lvds_downclock,
5546 refclk, clock,
5547 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005548 }
5549
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005550 return true;
5551}
5552
Daniel Vetter01a415f2012-10-27 15:58:40 +02005553static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
5554{
5555 struct drm_i915_private *dev_priv = dev->dev_private;
5556 uint32_t temp;
5557
5558 temp = I915_READ(SOUTH_CHICKEN1);
5559 if (temp & FDI_BC_BIFURCATION_SELECT)
5560 return;
5561
5562 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
5563 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
5564
5565 temp |= FDI_BC_BIFURCATION_SELECT;
5566 DRM_DEBUG_KMS("enabling fdi C rx\n");
5567 I915_WRITE(SOUTH_CHICKEN1, temp);
5568 POSTING_READ(SOUTH_CHICKEN1);
5569}
5570
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005571static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
Daniel Vetter01a415f2012-10-27 15:58:40 +02005572{
5573 struct drm_device *dev = intel_crtc->base.dev;
5574 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005575
5576 switch (intel_crtc->pipe) {
5577 case PIPE_A:
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005578 break;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005579 case PIPE_B:
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005580 if (intel_crtc->config.fdi_lanes > 2)
Daniel Vetter01a415f2012-10-27 15:58:40 +02005581 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
5582 else
5583 cpt_enable_fdi_bc_bifurcation(dev);
5584
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005585 break;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005586 case PIPE_C:
Daniel Vetter01a415f2012-10-27 15:58:40 +02005587 cpt_enable_fdi_bc_bifurcation(dev);
5588
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005589 break;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005590 default:
5591 BUG();
5592 }
5593}
5594
Paulo Zanonid4b19312012-11-29 11:29:32 -02005595int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
5596{
5597 /*
5598 * Account for spread spectrum to avoid
5599 * oversubscribing the link. Max center spread
5600 * is 2.5%; use 5% for safety's sake.
5601 */
5602 u32 bps = target_clock * bpp * 21 / 20;
5603 return bps / (link_bw * 8) + 1;
5604}
5605
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005606static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02005607{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005608 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005609}
5610
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005611static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005612 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02005613 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005614{
5615 struct drm_crtc *crtc = &intel_crtc->base;
5616 struct drm_device *dev = crtc->dev;
5617 struct drm_i915_private *dev_priv = dev->dev_private;
5618 struct intel_encoder *intel_encoder;
5619 uint32_t dpll;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01005620 int factor, num_connectors = 0;
Daniel Vetter09ede542013-04-30 14:01:45 +02005621 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005622
5623 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5624 switch (intel_encoder->type) {
5625 case INTEL_OUTPUT_LVDS:
5626 is_lvds = true;
5627 break;
5628 case INTEL_OUTPUT_SDVO:
5629 case INTEL_OUTPUT_HDMI:
5630 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005631 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005632 }
5633
5634 num_connectors++;
5635 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005636
Chris Wilsonc1858122010-12-03 21:35:48 +00005637 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07005638 factor = 21;
5639 if (is_lvds) {
5640 if ((intel_panel_use_ssc(dev_priv) &&
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005641 dev_priv->vbt.lvds_ssc_freq == 100) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02005642 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07005643 factor = 25;
Daniel Vetter09ede542013-04-30 14:01:45 +02005644 } else if (intel_crtc->config.sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07005645 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00005646
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005647 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02005648 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00005649
Daniel Vetter9a7c7892013-04-04 22:20:34 +02005650 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
5651 *fp2 |= FP_CB_TUNE;
5652
Chris Wilson5eddb702010-09-11 13:48:45 +01005653 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005654
Eric Anholta07d6782011-03-30 13:01:08 -07005655 if (is_lvds)
5656 dpll |= DPLLB_MODE_LVDS;
5657 else
5658 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02005659
Daniel Vetteref1b4602013-06-01 17:17:04 +02005660 dpll |= (intel_crtc->config.pixel_multiplier - 1)
5661 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02005662
5663 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02005664 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter9566e9a2013-04-19 11:14:36 +02005665 if (intel_crtc->config.has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02005666 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08005667
Eric Anholta07d6782011-03-30 13:01:08 -07005668 /* compute bitmask from p1 value */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005669 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005670 /* also FPA1 */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005671 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005672
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005673 switch (intel_crtc->config.dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07005674 case 5:
5675 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5676 break;
5677 case 7:
5678 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5679 break;
5680 case 10:
5681 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5682 break;
5683 case 14:
5684 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5685 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005686 }
5687
Daniel Vetterb4c09f32013-04-30 14:01:42 +02005688 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005689 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08005690 else
5691 dpll |= PLL_REF_INPUT_DREFCLK;
5692
Daniel Vetter959e16d2013-06-05 13:34:21 +02005693 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005694}
5695
Jesse Barnes79e53942008-11-07 14:24:08 -08005696static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
Jesse Barnes79e53942008-11-07 14:24:08 -08005697 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005698 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005699{
5700 struct drm_device *dev = crtc->dev;
5701 struct drm_i915_private *dev_priv = dev->dev_private;
5702 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5703 int pipe = intel_crtc->pipe;
5704 int plane = intel_crtc->plane;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005705 int num_connectors = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005706 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02005707 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03005708 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01005709 bool is_lvds = false;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005710 struct intel_encoder *encoder;
Daniel Vettere2b78262013-06-07 23:10:03 +02005711 struct intel_shared_dpll *pll;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005712 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08005713
5714 for_each_encoder_on_crtc(dev, crtc, encoder) {
5715 switch (encoder->type) {
5716 case INTEL_OUTPUT_LVDS:
5717 is_lvds = true;
5718 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005719 }
5720
5721 num_connectors++;
5722 }
5723
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005724 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
5725 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
5726
Daniel Vetterff9a6752013-06-01 17:16:21 +02005727 ok = ironlake_compute_clocks(crtc, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005728 &has_reduced_clock, &reduced_clock);
Daniel Vetteree9300b2013-06-03 22:40:22 +02005729 if (!ok && !intel_crtc->config.clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08005730 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5731 return -EINVAL;
5732 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01005733 /* Compat-code for transition, will disappear. */
5734 if (!intel_crtc->config.clock_set) {
5735 intel_crtc->config.dpll.n = clock.n;
5736 intel_crtc->config.dpll.m1 = clock.m1;
5737 intel_crtc->config.dpll.m2 = clock.m2;
5738 intel_crtc->config.dpll.p1 = clock.p1;
5739 intel_crtc->config.dpll.p2 = clock.p2;
5740 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005741
5742 /* Ensure that the cursor is valid for the new mode before changing... */
5743 intel_crtc_update_cursor(crtc, true);
5744
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005745 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
Daniel Vetter8b470472013-03-28 10:41:59 +01005746 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005747 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02005748 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005749 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02005750
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005751 dpll = ironlake_compute_dpll(intel_crtc,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02005752 &fp, &reduced_clock,
5753 has_reduced_clock ? &fp2 : NULL);
5754
Daniel Vetter959e16d2013-06-05 13:34:21 +02005755 intel_crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02005756 intel_crtc->config.dpll_hw_state.fp0 = fp;
5757 if (has_reduced_clock)
5758 intel_crtc->config.dpll_hw_state.fp1 = fp2;
5759 else
5760 intel_crtc->config.dpll_hw_state.fp1 = fp;
5761
Daniel Vetterb89a1d32013-06-05 13:34:24 +02005762 pll = intel_get_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005763 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03005764 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
5765 pipe_name(pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07005766 return -EINVAL;
5767 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005768 } else
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005769 intel_put_shared_dpll(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08005770
Daniel Vetter03afc4a2013-04-02 23:42:31 +02005771 if (intel_crtc->config.has_dp_encoder)
5772 intel_dp_set_m_n(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08005773
Daniel Vetterbcd644e2013-06-05 13:34:22 +02005774 if (is_lvds && has_reduced_clock && i915_powersave)
5775 intel_crtc->lowfreq_avail = true;
5776 else
5777 intel_crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02005778
5779 if (intel_crtc->config.has_pch_encoder) {
5780 pll = intel_crtc_to_shared_dpll(intel_crtc);
5781
Jesse Barnes79e53942008-11-07 14:24:08 -08005782 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005783
Daniel Vetter8a654f32013-06-01 17:16:22 +02005784 intel_set_pipe_timings(intel_crtc);
Krzysztof Halasa734b4152010-05-25 18:41:46 +02005785
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01005786 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01005787 intel_cpu_transcoder_set_m_n(intel_crtc,
5788 &intel_crtc->config.fdi_m_n);
5789 }
Chris Wilson5eddb702010-09-11 13:48:45 +01005790
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005791 if (IS_IVYBRIDGE(dev))
5792 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
Zhenyu Wang2c072452009-06-05 15:38:42 +08005793
Daniel Vetter6ff93602013-04-19 11:24:36 +02005794 ironlake_set_pipeconf(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08005795
Paulo Zanonia1f9e772012-09-12 10:06:32 -03005796 /* Set up the display plane register */
5797 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08005798 POSTING_READ(DSPCNTR(plane));
Jesse Barnes79e53942008-11-07 14:24:08 -08005799
Daniel Vetter94352cf2012-07-05 22:51:56 +02005800 ret = intel_pipe_set_base(crtc, x, y, fb);
Shaohua Li7662c8b2009-06-26 11:23:55 +08005801
5802 intel_update_watermarks(dev);
5803
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005804 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08005805}
5806
Daniel Vetter72419202013-04-04 13:28:53 +02005807static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5808 struct intel_crtc_config *pipe_config)
5809{
5810 struct drm_device *dev = crtc->base.dev;
5811 struct drm_i915_private *dev_priv = dev->dev_private;
5812 enum transcoder transcoder = pipe_config->cpu_transcoder;
5813
5814 pipe_config->fdi_m_n.link_m = I915_READ(PIPE_LINK_M1(transcoder));
5815 pipe_config->fdi_m_n.link_n = I915_READ(PIPE_LINK_N1(transcoder));
5816 pipe_config->fdi_m_n.gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
5817 & ~TU_SIZE_MASK;
5818 pipe_config->fdi_m_n.gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
5819 pipe_config->fdi_m_n.tu = ((I915_READ(PIPE_DATA_M1(transcoder))
5820 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
5821}
5822
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005823static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5824 struct intel_crtc_config *pipe_config)
5825{
5826 struct drm_device *dev = crtc->base.dev;
5827 struct drm_i915_private *dev_priv = dev->dev_private;
5828 uint32_t tmp;
5829
5830 tmp = I915_READ(PF_CTL(crtc->pipe));
5831
5832 if (tmp & PF_ENABLE) {
5833 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
5834 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02005835
5836 /* We currently do not free assignements of panel fitters on
5837 * ivb/hsw (since we don't use the higher upscaling modes which
5838 * differentiates them) so just WARN about this case for now. */
5839 if (IS_GEN7(dev)) {
5840 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
5841 PF_PIPE_SEL_IVB(crtc->pipe));
5842 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005843 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005844}
5845
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005846static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5847 struct intel_crtc_config *pipe_config)
5848{
5849 struct drm_device *dev = crtc->base.dev;
5850 struct drm_i915_private *dev_priv = dev->dev_private;
5851 uint32_t tmp;
5852
Daniel Vettere143a212013-07-04 12:01:15 +02005853 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02005854 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02005855
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005856 tmp = I915_READ(PIPECONF(crtc->pipe));
5857 if (!(tmp & PIPECONF_ENABLE))
5858 return false;
5859
Daniel Vetterab9412b2013-05-03 11:49:46 +02005860 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02005861 struct intel_shared_dpll *pll;
5862
Daniel Vetter88adfff2013-03-28 10:42:01 +01005863 pipe_config->has_pch_encoder = true;
5864
Daniel Vetter627eb5a2013-04-29 19:33:42 +02005865 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
5866 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
5867 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02005868
5869 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02005870
Daniel Vetterc0d43d62013-06-07 23:11:08 +02005871 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02005872 pipe_config->shared_dpll =
5873 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02005874 } else {
5875 tmp = I915_READ(PCH_DPLL_SEL);
5876 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
5877 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
5878 else
5879 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
5880 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02005881
5882 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
5883
5884 WARN_ON(!pll->get_hw_state(dev_priv, pll,
5885 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02005886
5887 tmp = pipe_config->dpll_hw_state.dpll;
5888 pipe_config->pixel_multiplier =
5889 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
5890 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter6c49f242013-06-06 12:45:25 +02005891 } else {
5892 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02005893 }
5894
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005895 intel_get_pipe_timings(crtc, pipe_config);
5896
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005897 ironlake_get_pfit_config(crtc, pipe_config);
5898
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005899 return true;
5900}
5901
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005902static void haswell_modeset_global_resources(struct drm_device *dev)
5903{
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005904 bool enable = false;
5905 struct intel_crtc *crtc;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005906
5907 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
Daniel Vettere7a639c2013-05-31 17:49:17 +02005908 if (!crtc->base.enabled)
5909 continue;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005910
Daniel Vettere7a639c2013-05-31 17:49:17 +02005911 if (crtc->pipe != PIPE_A || crtc->config.pch_pfit.size ||
5912 crtc->config.cpu_transcoder != TRANSCODER_EDP)
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005913 enable = true;
5914 }
5915
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005916 intel_set_power_well(dev, enable);
5917}
5918
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005919static int haswell_crtc_mode_set(struct drm_crtc *crtc,
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005920 int x, int y,
5921 struct drm_framebuffer *fb)
5922{
5923 struct drm_device *dev = crtc->dev;
5924 struct drm_i915_private *dev_priv = dev->dev_private;
5925 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005926 int plane = intel_crtc->plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005927 int ret;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005928
Daniel Vetterff9a6752013-06-01 17:16:21 +02005929 if (!intel_ddi_pll_mode_set(crtc))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03005930 return -EINVAL;
5931
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005932 /* Ensure that the cursor is valid for the new mode before changing... */
5933 intel_crtc_update_cursor(crtc, true);
5934
Daniel Vetter03afc4a2013-04-02 23:42:31 +02005935 if (intel_crtc->config.has_dp_encoder)
5936 intel_dp_set_m_n(intel_crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005937
5938 intel_crtc->lowfreq_avail = false;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005939
Daniel Vetter8a654f32013-06-01 17:16:22 +02005940 intel_set_pipe_timings(intel_crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005941
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01005942 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01005943 intel_cpu_transcoder_set_m_n(intel_crtc,
5944 &intel_crtc->config.fdi_m_n);
5945 }
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005946
Daniel Vetter6ff93602013-04-19 11:24:36 +02005947 haswell_set_pipeconf(crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005948
Daniel Vetter50f3b012013-03-27 00:44:56 +01005949 intel_set_pipe_csc(crtc);
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005950
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005951 /* Set up the display plane register */
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005952 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005953 POSTING_READ(DSPCNTR(plane));
5954
5955 ret = intel_pipe_set_base(crtc, x, y, fb);
5956
5957 intel_update_watermarks(dev);
5958
Jesse Barnes79e53942008-11-07 14:24:08 -08005959 return ret;
5960}
5961
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005962static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5963 struct intel_crtc_config *pipe_config)
5964{
5965 struct drm_device *dev = crtc->base.dev;
5966 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005967 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005968 uint32_t tmp;
5969
Daniel Vettere143a212013-07-04 12:01:15 +02005970 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02005971 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
5972
Daniel Vettereccb1402013-05-22 00:50:22 +02005973 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
5974 if (tmp & TRANS_DDI_FUNC_ENABLE) {
5975 enum pipe trans_edp_pipe;
5976 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
5977 default:
5978 WARN(1, "unknown pipe linked to edp transcoder\n");
5979 case TRANS_DDI_EDP_INPUT_A_ONOFF:
5980 case TRANS_DDI_EDP_INPUT_A_ON:
5981 trans_edp_pipe = PIPE_A;
5982 break;
5983 case TRANS_DDI_EDP_INPUT_B_ONOFF:
5984 trans_edp_pipe = PIPE_B;
5985 break;
5986 case TRANS_DDI_EDP_INPUT_C_ONOFF:
5987 trans_edp_pipe = PIPE_C;
5988 break;
5989 }
5990
5991 if (trans_edp_pipe == crtc->pipe)
5992 pipe_config->cpu_transcoder = TRANSCODER_EDP;
5993 }
5994
Paulo Zanonib97186f2013-05-03 12:15:36 -03005995 if (!intel_display_power_enabled(dev,
Daniel Vettereccb1402013-05-22 00:50:22 +02005996 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03005997 return false;
5998
Daniel Vettereccb1402013-05-22 00:50:22 +02005999 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006000 if (!(tmp & PIPECONF_ENABLE))
6001 return false;
6002
Daniel Vetter88adfff2013-03-28 10:42:01 +01006003 /*
Paulo Zanonif196e6b2013-04-18 16:35:41 -03006004 * Haswell has only FDI/PCH transcoder A. It is which is connected to
Daniel Vetter88adfff2013-03-28 10:42:01 +01006005 * DDI E. So just check whether this pipe is wired to DDI E and whether
6006 * the PCH transcoder is on.
6007 */
Daniel Vettereccb1402013-05-22 00:50:22 +02006008 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
Daniel Vetter88adfff2013-03-28 10:42:01 +01006009 if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
Daniel Vetterab9412b2013-05-03 11:49:46 +02006010 I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter88adfff2013-03-28 10:42:01 +01006011 pipe_config->has_pch_encoder = true;
6012
Daniel Vetter627eb5a2013-04-29 19:33:42 +02006013 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
6014 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6015 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02006016
6017 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02006018 }
6019
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006020 intel_get_pipe_timings(crtc, pipe_config);
6021
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006022 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
6023 if (intel_display_power_enabled(dev, pfit_domain))
6024 ironlake_get_pfit_config(crtc, pipe_config);
Daniel Vetter88adfff2013-03-28 10:42:01 +01006025
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006026 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
6027 (I915_READ(IPS_CTL) & IPS_ENABLE);
6028
Daniel Vetter6c49f242013-06-06 12:45:25 +02006029 pipe_config->pixel_multiplier = 1;
6030
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006031 return true;
6032}
6033
Eric Anholtf564048e2011-03-30 13:01:02 -07006034static int intel_crtc_mode_set(struct drm_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07006035 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02006036 struct drm_framebuffer *fb)
Eric Anholtf564048e2011-03-30 13:01:02 -07006037{
6038 struct drm_device *dev = crtc->dev;
6039 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter9256aa12012-10-31 19:26:13 +01006040 struct drm_encoder_helper_funcs *encoder_funcs;
6041 struct intel_encoder *encoder;
Eric Anholt0b701d22011-03-30 13:01:03 -07006042 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01006043 struct drm_display_mode *adjusted_mode =
6044 &intel_crtc->config.adjusted_mode;
6045 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
Eric Anholt0b701d22011-03-30 13:01:03 -07006046 int pipe = intel_crtc->pipe;
Eric Anholtf564048e2011-03-30 13:01:02 -07006047 int ret;
6048
Eric Anholt0b701d22011-03-30 13:01:03 -07006049 drm_vblank_pre_modeset(dev, pipe);
6050
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01006051 ret = dev_priv->display.crtc_mode_set(crtc, x, y, fb);
6052
Jesse Barnes79e53942008-11-07 14:24:08 -08006053 drm_vblank_post_modeset(dev, pipe);
6054
Daniel Vetter9256aa12012-10-31 19:26:13 +01006055 if (ret != 0)
6056 return ret;
6057
6058 for_each_encoder_on_crtc(dev, crtc, encoder) {
6059 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
6060 encoder->base.base.id,
6061 drm_get_encoder_name(&encoder->base),
6062 mode->base.id, mode->name);
Daniel Vetter6cc5f342013-03-27 00:44:53 +01006063 if (encoder->mode_set) {
6064 encoder->mode_set(encoder);
6065 } else {
6066 encoder_funcs = encoder->base.helper_private;
6067 encoder_funcs->mode_set(&encoder->base, mode, adjusted_mode);
6068 }
Daniel Vetter9256aa12012-10-31 19:26:13 +01006069 }
6070
6071 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006072}
6073
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006074static bool intel_eld_uptodate(struct drm_connector *connector,
6075 int reg_eldv, uint32_t bits_eldv,
6076 int reg_elda, uint32_t bits_elda,
6077 int reg_edid)
6078{
6079 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6080 uint8_t *eld = connector->eld;
6081 uint32_t i;
6082
6083 i = I915_READ(reg_eldv);
6084 i &= bits_eldv;
6085
6086 if (!eld[0])
6087 return !i;
6088
6089 if (!i)
6090 return false;
6091
6092 i = I915_READ(reg_elda);
6093 i &= ~bits_elda;
6094 I915_WRITE(reg_elda, i);
6095
6096 for (i = 0; i < eld[2]; i++)
6097 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
6098 return false;
6099
6100 return true;
6101}
6102
Wu Fengguange0dac652011-09-05 14:25:34 +08006103static void g4x_write_eld(struct drm_connector *connector,
6104 struct drm_crtc *crtc)
6105{
6106 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6107 uint8_t *eld = connector->eld;
6108 uint32_t eldv;
6109 uint32_t len;
6110 uint32_t i;
6111
6112 i = I915_READ(G4X_AUD_VID_DID);
6113
6114 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
6115 eldv = G4X_ELDV_DEVCL_DEVBLC;
6116 else
6117 eldv = G4X_ELDV_DEVCTG;
6118
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006119 if (intel_eld_uptodate(connector,
6120 G4X_AUD_CNTL_ST, eldv,
6121 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
6122 G4X_HDMIW_HDMIEDID))
6123 return;
6124
Wu Fengguange0dac652011-09-05 14:25:34 +08006125 i = I915_READ(G4X_AUD_CNTL_ST);
6126 i &= ~(eldv | G4X_ELD_ADDR);
6127 len = (i >> 9) & 0x1f; /* ELD buffer size */
6128 I915_WRITE(G4X_AUD_CNTL_ST, i);
6129
6130 if (!eld[0])
6131 return;
6132
6133 len = min_t(uint8_t, eld[2], len);
6134 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6135 for (i = 0; i < len; i++)
6136 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
6137
6138 i = I915_READ(G4X_AUD_CNTL_ST);
6139 i |= eldv;
6140 I915_WRITE(G4X_AUD_CNTL_ST, i);
6141}
6142
Wang Xingchao83358c852012-08-16 22:43:37 +08006143static void haswell_write_eld(struct drm_connector *connector,
6144 struct drm_crtc *crtc)
6145{
6146 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6147 uint8_t *eld = connector->eld;
6148 struct drm_device *dev = crtc->dev;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08006149 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Wang Xingchao83358c852012-08-16 22:43:37 +08006150 uint32_t eldv;
6151 uint32_t i;
6152 int len;
6153 int pipe = to_intel_crtc(crtc)->pipe;
6154 int tmp;
6155
6156 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
6157 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
6158 int aud_config = HSW_AUD_CFG(pipe);
6159 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
6160
6161
6162 DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
6163
6164 /* Audio output enable */
6165 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
6166 tmp = I915_READ(aud_cntrl_st2);
6167 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
6168 I915_WRITE(aud_cntrl_st2, tmp);
6169
6170 /* Wait for 1 vertical blank */
6171 intel_wait_for_vblank(dev, pipe);
6172
6173 /* Set ELD valid state */
6174 tmp = I915_READ(aud_cntrl_st2);
6175 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%8x\n", tmp);
6176 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
6177 I915_WRITE(aud_cntrl_st2, tmp);
6178 tmp = I915_READ(aud_cntrl_st2);
6179 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%8x\n", tmp);
6180
6181 /* Enable HDMI mode */
6182 tmp = I915_READ(aud_config);
6183 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%8x\n", tmp);
6184 /* clear N_programing_enable and N_value_index */
6185 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
6186 I915_WRITE(aud_config, tmp);
6187
6188 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
6189
6190 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08006191 intel_crtc->eld_vld = true;
Wang Xingchao83358c852012-08-16 22:43:37 +08006192
6193 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6194 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6195 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
6196 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6197 } else
6198 I915_WRITE(aud_config, 0);
6199
6200 if (intel_eld_uptodate(connector,
6201 aud_cntrl_st2, eldv,
6202 aud_cntl_st, IBX_ELD_ADDRESS,
6203 hdmiw_hdmiedid))
6204 return;
6205
6206 i = I915_READ(aud_cntrl_st2);
6207 i &= ~eldv;
6208 I915_WRITE(aud_cntrl_st2, i);
6209
6210 if (!eld[0])
6211 return;
6212
6213 i = I915_READ(aud_cntl_st);
6214 i &= ~IBX_ELD_ADDRESS;
6215 I915_WRITE(aud_cntl_st, i);
6216 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
6217 DRM_DEBUG_DRIVER("port num:%d\n", i);
6218
6219 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6220 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6221 for (i = 0; i < len; i++)
6222 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6223
6224 i = I915_READ(aud_cntrl_st2);
6225 i |= eldv;
6226 I915_WRITE(aud_cntrl_st2, i);
6227
6228}
6229
Wu Fengguange0dac652011-09-05 14:25:34 +08006230static void ironlake_write_eld(struct drm_connector *connector,
6231 struct drm_crtc *crtc)
6232{
6233 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6234 uint8_t *eld = connector->eld;
6235 uint32_t eldv;
6236 uint32_t i;
6237 int len;
6238 int hdmiw_hdmiedid;
Wu Fengguangb6daa022012-01-06 14:41:31 -06006239 int aud_config;
Wu Fengguange0dac652011-09-05 14:25:34 +08006240 int aud_cntl_st;
6241 int aud_cntrl_st2;
Wang Xingchao9b138a82012-08-09 16:52:18 +08006242 int pipe = to_intel_crtc(crtc)->pipe;
Wu Fengguange0dac652011-09-05 14:25:34 +08006243
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08006244 if (HAS_PCH_IBX(connector->dev)) {
Wang Xingchao9b138a82012-08-09 16:52:18 +08006245 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
6246 aud_config = IBX_AUD_CFG(pipe);
6247 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006248 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08006249 } else {
Wang Xingchao9b138a82012-08-09 16:52:18 +08006250 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
6251 aud_config = CPT_AUD_CFG(pipe);
6252 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006253 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08006254 }
6255
Wang Xingchao9b138a82012-08-09 16:52:18 +08006256 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
Wu Fengguange0dac652011-09-05 14:25:34 +08006257
6258 i = I915_READ(aud_cntl_st);
Wang Xingchao9b138a82012-08-09 16:52:18 +08006259 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
Wu Fengguange0dac652011-09-05 14:25:34 +08006260 if (!i) {
6261 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
6262 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006263 eldv = IBX_ELD_VALIDB;
6264 eldv |= IBX_ELD_VALIDB << 4;
6265 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08006266 } else {
Ville Syrjälä2582a852013-04-17 17:48:47 +03006267 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006268 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08006269 }
6270
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006271 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6272 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6273 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
Wu Fengguangb6daa022012-01-06 14:41:31 -06006274 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6275 } else
6276 I915_WRITE(aud_config, 0);
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006277
6278 if (intel_eld_uptodate(connector,
6279 aud_cntrl_st2, eldv,
6280 aud_cntl_st, IBX_ELD_ADDRESS,
6281 hdmiw_hdmiedid))
6282 return;
6283
Wu Fengguange0dac652011-09-05 14:25:34 +08006284 i = I915_READ(aud_cntrl_st2);
6285 i &= ~eldv;
6286 I915_WRITE(aud_cntrl_st2, i);
6287
6288 if (!eld[0])
6289 return;
6290
Wu Fengguange0dac652011-09-05 14:25:34 +08006291 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006292 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08006293 I915_WRITE(aud_cntl_st, i);
6294
6295 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6296 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6297 for (i = 0; i < len; i++)
6298 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6299
6300 i = I915_READ(aud_cntrl_st2);
6301 i |= eldv;
6302 I915_WRITE(aud_cntrl_st2, i);
6303}
6304
6305void intel_write_eld(struct drm_encoder *encoder,
6306 struct drm_display_mode *mode)
6307{
6308 struct drm_crtc *crtc = encoder->crtc;
6309 struct drm_connector *connector;
6310 struct drm_device *dev = encoder->dev;
6311 struct drm_i915_private *dev_priv = dev->dev_private;
6312
6313 connector = drm_select_eld(encoder, mode);
6314 if (!connector)
6315 return;
6316
6317 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6318 connector->base.id,
6319 drm_get_connector_name(connector),
6320 connector->encoder->base.id,
6321 drm_get_encoder_name(connector->encoder));
6322
6323 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
6324
6325 if (dev_priv->display.write_eld)
6326 dev_priv->display.write_eld(connector, crtc);
6327}
6328
Jesse Barnes79e53942008-11-07 14:24:08 -08006329/** Loads the palette/gamma unit for the CRTC with the prepared values */
6330void intel_crtc_load_lut(struct drm_crtc *crtc)
6331{
6332 struct drm_device *dev = crtc->dev;
6333 struct drm_i915_private *dev_priv = dev->dev_private;
6334 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006335 enum pipe pipe = intel_crtc->pipe;
6336 int palreg = PALETTE(pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08006337 int i;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006338 bool reenable_ips = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006339
6340 /* The clocks have to be on to load the palette. */
Alban Browaeysaed3f092012-02-24 17:12:45 +00006341 if (!crtc->enabled || !intel_crtc->active)
Jesse Barnes79e53942008-11-07 14:24:08 -08006342 return;
6343
Ville Syrjälä14420bd2013-06-04 13:49:07 +03006344 if (!HAS_PCH_SPLIT(dev_priv->dev))
6345 assert_pll_enabled(dev_priv, pipe);
6346
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006347 /* use legacy palette for Ironlake */
Eric Anholtbad720f2009-10-22 16:11:14 -07006348 if (HAS_PCH_SPLIT(dev))
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006349 palreg = LGC_PALETTE(pipe);
6350
6351 /* Workaround : Do not read or write the pipe palette/gamma data while
6352 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
6353 */
6354 if (intel_crtc->config.ips_enabled &&
6355 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
6356 GAMMA_MODE_MODE_SPLIT)) {
6357 hsw_disable_ips(intel_crtc);
6358 reenable_ips = true;
6359 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08006360
Jesse Barnes79e53942008-11-07 14:24:08 -08006361 for (i = 0; i < 256; i++) {
6362 I915_WRITE(palreg + 4 * i,
6363 (intel_crtc->lut_r[i] << 16) |
6364 (intel_crtc->lut_g[i] << 8) |
6365 intel_crtc->lut_b[i]);
6366 }
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006367
6368 if (reenable_ips)
6369 hsw_enable_ips(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006370}
6371
Chris Wilson560b85b2010-08-07 11:01:38 +01006372static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
6373{
6374 struct drm_device *dev = crtc->dev;
6375 struct drm_i915_private *dev_priv = dev->dev_private;
6376 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6377 bool visible = base != 0;
6378 u32 cntl;
6379
6380 if (intel_crtc->cursor_visible == visible)
6381 return;
6382
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006383 cntl = I915_READ(_CURACNTR);
Chris Wilson560b85b2010-08-07 11:01:38 +01006384 if (visible) {
6385 /* On these chipsets we can only modify the base whilst
6386 * the cursor is disabled.
6387 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006388 I915_WRITE(_CURABASE, base);
Chris Wilson560b85b2010-08-07 11:01:38 +01006389
6390 cntl &= ~(CURSOR_FORMAT_MASK);
6391 /* XXX width must be 64, stride 256 => 0x00 << 28 */
6392 cntl |= CURSOR_ENABLE |
6393 CURSOR_GAMMA_ENABLE |
6394 CURSOR_FORMAT_ARGB;
6395 } else
6396 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006397 I915_WRITE(_CURACNTR, cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01006398
6399 intel_crtc->cursor_visible = visible;
6400}
6401
6402static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
6403{
6404 struct drm_device *dev = crtc->dev;
6405 struct drm_i915_private *dev_priv = dev->dev_private;
6406 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6407 int pipe = intel_crtc->pipe;
6408 bool visible = base != 0;
6409
6410 if (intel_crtc->cursor_visible != visible) {
Jesse Barnes548f2452011-02-17 10:40:53 -08006411 uint32_t cntl = I915_READ(CURCNTR(pipe));
Chris Wilson560b85b2010-08-07 11:01:38 +01006412 if (base) {
6413 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
6414 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6415 cntl |= pipe << 28; /* Connect to correct pipe */
6416 } else {
6417 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6418 cntl |= CURSOR_MODE_DISABLE;
6419 }
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006420 I915_WRITE(CURCNTR(pipe), cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01006421
6422 intel_crtc->cursor_visible = visible;
6423 }
6424 /* and commit changes on next vblank */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006425 I915_WRITE(CURBASE(pipe), base);
Chris Wilson560b85b2010-08-07 11:01:38 +01006426}
6427
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006428static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
6429{
6430 struct drm_device *dev = crtc->dev;
6431 struct drm_i915_private *dev_priv = dev->dev_private;
6432 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6433 int pipe = intel_crtc->pipe;
6434 bool visible = base != 0;
6435
6436 if (intel_crtc->cursor_visible != visible) {
6437 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
6438 if (base) {
6439 cntl &= ~CURSOR_MODE;
6440 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6441 } else {
6442 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6443 cntl |= CURSOR_MODE_DISABLE;
6444 }
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006445 if (IS_HASWELL(dev))
6446 cntl |= CURSOR_PIPE_CSC_ENABLE;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006447 I915_WRITE(CURCNTR_IVB(pipe), cntl);
6448
6449 intel_crtc->cursor_visible = visible;
6450 }
6451 /* and commit changes on next vblank */
6452 I915_WRITE(CURBASE_IVB(pipe), base);
6453}
6454
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006455/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01006456static void intel_crtc_update_cursor(struct drm_crtc *crtc,
6457 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006458{
6459 struct drm_device *dev = crtc->dev;
6460 struct drm_i915_private *dev_priv = dev->dev_private;
6461 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6462 int pipe = intel_crtc->pipe;
6463 int x = intel_crtc->cursor_x;
6464 int y = intel_crtc->cursor_y;
Chris Wilson560b85b2010-08-07 11:01:38 +01006465 u32 base, pos;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006466 bool visible;
6467
6468 pos = 0;
6469
Chris Wilson6b383a72010-09-13 13:54:26 +01006470 if (on && crtc->enabled && crtc->fb) {
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006471 base = intel_crtc->cursor_addr;
6472 if (x > (int) crtc->fb->width)
6473 base = 0;
6474
6475 if (y > (int) crtc->fb->height)
6476 base = 0;
6477 } else
6478 base = 0;
6479
6480 if (x < 0) {
6481 if (x + intel_crtc->cursor_width < 0)
6482 base = 0;
6483
6484 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
6485 x = -x;
6486 }
6487 pos |= x << CURSOR_X_SHIFT;
6488
6489 if (y < 0) {
6490 if (y + intel_crtc->cursor_height < 0)
6491 base = 0;
6492
6493 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
6494 y = -y;
6495 }
6496 pos |= y << CURSOR_Y_SHIFT;
6497
6498 visible = base != 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01006499 if (!visible && !intel_crtc->cursor_visible)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006500 return;
6501
Eugeni Dodonov0cd83aa2012-04-13 17:08:48 -03006502 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006503 I915_WRITE(CURPOS_IVB(pipe), pos);
6504 ivb_update_cursor(crtc, base);
6505 } else {
6506 I915_WRITE(CURPOS(pipe), pos);
6507 if (IS_845G(dev) || IS_I865G(dev))
6508 i845_update_cursor(crtc, base);
6509 else
6510 i9xx_update_cursor(crtc, base);
6511 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006512}
6513
Jesse Barnes79e53942008-11-07 14:24:08 -08006514static int intel_crtc_cursor_set(struct drm_crtc *crtc,
Chris Wilson05394f32010-11-08 19:18:58 +00006515 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08006516 uint32_t handle,
6517 uint32_t width, uint32_t height)
6518{
6519 struct drm_device *dev = crtc->dev;
6520 struct drm_i915_private *dev_priv = dev->dev_private;
6521 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00006522 struct drm_i915_gem_object *obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006523 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006524 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006525
Jesse Barnes79e53942008-11-07 14:24:08 -08006526 /* if we want to turn off the cursor ignore width and height */
6527 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08006528 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006529 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00006530 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10006531 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006532 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08006533 }
6534
6535 /* Currently we only support 64x64 cursors */
6536 if (width != 64 || height != 64) {
6537 DRM_ERROR("we currently only support 64x64 cursors\n");
6538 return -EINVAL;
6539 }
6540
Chris Wilson05394f32010-11-08 19:18:58 +00006541 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00006542 if (&obj->base == NULL)
Jesse Barnes79e53942008-11-07 14:24:08 -08006543 return -ENOENT;
6544
Chris Wilson05394f32010-11-08 19:18:58 +00006545 if (obj->base.size < width * height * 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006546 DRM_ERROR("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10006547 ret = -ENOMEM;
6548 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08006549 }
6550
Dave Airlie71acb5e2008-12-30 20:31:46 +10006551 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006552 mutex_lock(&dev->struct_mutex);
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05006553 if (!dev_priv->info->cursor_needs_physical) {
Chris Wilson693db182013-03-05 14:52:39 +00006554 unsigned alignment;
6555
Chris Wilsond9e86c02010-11-10 16:40:20 +00006556 if (obj->tiling_mode) {
6557 DRM_ERROR("cursor cannot be tiled\n");
6558 ret = -EINVAL;
6559 goto fail_locked;
6560 }
6561
Chris Wilson693db182013-03-05 14:52:39 +00006562 /* Note that the w/a also requires 2 PTE of padding following
6563 * the bo. We currently fill all unused PTE with the shadow
6564 * page and so we should always have valid PTE following the
6565 * cursor preventing the VT-d warning.
6566 */
6567 alignment = 0;
6568 if (need_vtd_wa(dev))
6569 alignment = 64*1024;
6570
6571 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01006572 if (ret) {
6573 DRM_ERROR("failed to move cursor bo into the GTT\n");
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006574 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006575 }
6576
Chris Wilsond9e86c02010-11-10 16:40:20 +00006577 ret = i915_gem_object_put_fence(obj);
6578 if (ret) {
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006579 DRM_ERROR("failed to release fence for cursor");
Chris Wilsond9e86c02010-11-10 16:40:20 +00006580 goto fail_unpin;
6581 }
6582
Ben Widawskyf343c5f2013-07-05 14:41:04 -07006583 addr = i915_gem_obj_ggtt_offset(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10006584 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01006585 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson05394f32010-11-08 19:18:58 +00006586 ret = i915_gem_attach_phys_object(dev, obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01006587 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
6588 align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10006589 if (ret) {
6590 DRM_ERROR("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006591 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10006592 }
Chris Wilson05394f32010-11-08 19:18:58 +00006593 addr = obj->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006594 }
6595
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006596 if (IS_GEN2(dev))
Jesse Barnes14b60392009-05-20 16:47:08 -04006597 I915_WRITE(CURSIZE, (height << 12) | width);
6598
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006599 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006600 if (intel_crtc->cursor_bo) {
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05006601 if (dev_priv->info->cursor_needs_physical) {
Chris Wilson05394f32010-11-08 19:18:58 +00006602 if (intel_crtc->cursor_bo != obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10006603 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
6604 } else
6605 i915_gem_object_unpin(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00006606 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006607 }
Jesse Barnes80824002009-09-10 15:28:06 -07006608
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006609 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006610
6611 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00006612 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006613 intel_crtc->cursor_width = width;
6614 intel_crtc->cursor_height = height;
6615
Mika Kuoppala40ccc722013-04-23 17:27:08 +03006616 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006617
Jesse Barnes79e53942008-11-07 14:24:08 -08006618 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006619fail_unpin:
Chris Wilson05394f32010-11-08 19:18:58 +00006620 i915_gem_object_unpin(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006621fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10006622 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00006623fail:
Chris Wilson05394f32010-11-08 19:18:58 +00006624 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10006625 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006626}
6627
6628static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
6629{
Jesse Barnes79e53942008-11-07 14:24:08 -08006630 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006631
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006632 intel_crtc->cursor_x = x;
6633 intel_crtc->cursor_y = y;
Jesse Barnes652c3932009-08-17 13:31:43 -07006634
Mika Kuoppala40ccc722013-04-23 17:27:08 +03006635 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08006636
6637 return 0;
6638}
6639
6640/** Sets the color ramps on behalf of RandR */
6641void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
6642 u16 blue, int regno)
6643{
6644 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6645
6646 intel_crtc->lut_r[regno] = red >> 8;
6647 intel_crtc->lut_g[regno] = green >> 8;
6648 intel_crtc->lut_b[regno] = blue >> 8;
6649}
6650
Dave Airlieb8c00ac2009-10-06 13:54:01 +10006651void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
6652 u16 *blue, int regno)
6653{
6654 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6655
6656 *red = intel_crtc->lut_r[regno] << 8;
6657 *green = intel_crtc->lut_g[regno] << 8;
6658 *blue = intel_crtc->lut_b[regno] << 8;
6659}
6660
Jesse Barnes79e53942008-11-07 14:24:08 -08006661static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01006662 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08006663{
James Simmons72034252010-08-03 01:33:19 +01006664 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08006665 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006666
James Simmons72034252010-08-03 01:33:19 +01006667 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006668 intel_crtc->lut_r[i] = red[i] >> 8;
6669 intel_crtc->lut_g[i] = green[i] >> 8;
6670 intel_crtc->lut_b[i] = blue[i] >> 8;
6671 }
6672
6673 intel_crtc_load_lut(crtc);
6674}
6675
Jesse Barnes79e53942008-11-07 14:24:08 -08006676/* VESA 640x480x72Hz mode to set on the pipe */
6677static struct drm_display_mode load_detect_mode = {
6678 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
6679 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
6680};
6681
Chris Wilsond2dff872011-04-19 08:36:26 +01006682static struct drm_framebuffer *
6683intel_framebuffer_create(struct drm_device *dev,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006684 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilsond2dff872011-04-19 08:36:26 +01006685 struct drm_i915_gem_object *obj)
6686{
6687 struct intel_framebuffer *intel_fb;
6688 int ret;
6689
6690 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
6691 if (!intel_fb) {
6692 drm_gem_object_unreference_unlocked(&obj->base);
6693 return ERR_PTR(-ENOMEM);
6694 }
6695
6696 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
6697 if (ret) {
6698 drm_gem_object_unreference_unlocked(&obj->base);
6699 kfree(intel_fb);
6700 return ERR_PTR(ret);
6701 }
6702
6703 return &intel_fb->base;
6704}
6705
6706static u32
6707intel_framebuffer_pitch_for_width(int width, int bpp)
6708{
6709 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
6710 return ALIGN(pitch, 64);
6711}
6712
6713static u32
6714intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
6715{
6716 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
6717 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
6718}
6719
6720static struct drm_framebuffer *
6721intel_framebuffer_create_for_mode(struct drm_device *dev,
6722 struct drm_display_mode *mode,
6723 int depth, int bpp)
6724{
6725 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00006726 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01006727
6728 obj = i915_gem_alloc_object(dev,
6729 intel_framebuffer_size_for_mode(mode, bpp));
6730 if (obj == NULL)
6731 return ERR_PTR(-ENOMEM);
6732
6733 mode_cmd.width = mode->hdisplay;
6734 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006735 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
6736 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00006737 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01006738
6739 return intel_framebuffer_create(dev, &mode_cmd, obj);
6740}
6741
6742static struct drm_framebuffer *
6743mode_fits_in_fbdev(struct drm_device *dev,
6744 struct drm_display_mode *mode)
6745{
6746 struct drm_i915_private *dev_priv = dev->dev_private;
6747 struct drm_i915_gem_object *obj;
6748 struct drm_framebuffer *fb;
6749
6750 if (dev_priv->fbdev == NULL)
6751 return NULL;
6752
6753 obj = dev_priv->fbdev->ifb.obj;
6754 if (obj == NULL)
6755 return NULL;
6756
6757 fb = &dev_priv->fbdev->ifb.base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006758 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
6759 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01006760 return NULL;
6761
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006762 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01006763 return NULL;
6764
6765 return fb;
6766}
6767
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006768bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01006769 struct drm_display_mode *mode,
Chris Wilson8261b192011-04-19 23:18:09 +01006770 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08006771{
6772 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006773 struct intel_encoder *intel_encoder =
6774 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08006775 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01006776 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08006777 struct drm_crtc *crtc = NULL;
6778 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02006779 struct drm_framebuffer *fb;
Jesse Barnes79e53942008-11-07 14:24:08 -08006780 int i = -1;
6781
Chris Wilsond2dff872011-04-19 08:36:26 +01006782 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6783 connector->base.id, drm_get_connector_name(connector),
6784 encoder->base.id, drm_get_encoder_name(encoder));
6785
Jesse Barnes79e53942008-11-07 14:24:08 -08006786 /*
6787 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01006788 *
Jesse Barnes79e53942008-11-07 14:24:08 -08006789 * - if the connector already has an assigned crtc, use it (but make
6790 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01006791 *
Jesse Barnes79e53942008-11-07 14:24:08 -08006792 * - try to find the first unused crtc that can drive this connector,
6793 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08006794 */
6795
6796 /* See if we already have a CRTC for this connector */
6797 if (encoder->crtc) {
6798 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01006799
Daniel Vetter7b240562012-12-12 00:35:33 +01006800 mutex_lock(&crtc->mutex);
6801
Daniel Vetter24218aa2012-08-12 19:27:11 +02006802 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01006803 old->load_detect_temp = false;
6804
6805 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02006806 if (connector->dpms != DRM_MODE_DPMS_ON)
6807 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01006808
Chris Wilson71731882011-04-19 23:10:58 +01006809 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08006810 }
6811
6812 /* Find an unused one (if possible) */
6813 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
6814 i++;
6815 if (!(encoder->possible_crtcs & (1 << i)))
6816 continue;
6817 if (!possible_crtc->enabled) {
6818 crtc = possible_crtc;
6819 break;
6820 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006821 }
6822
6823 /*
6824 * If we didn't find an unused CRTC, don't use any.
6825 */
6826 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01006827 DRM_DEBUG_KMS("no pipe available for load-detect\n");
6828 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006829 }
6830
Daniel Vetter7b240562012-12-12 00:35:33 +01006831 mutex_lock(&crtc->mutex);
Daniel Vetterfc303102012-07-09 10:40:58 +02006832 intel_encoder->new_crtc = to_intel_crtc(crtc);
6833 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006834
6835 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter24218aa2012-08-12 19:27:11 +02006836 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01006837 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01006838 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08006839
Chris Wilson64927112011-04-20 07:25:26 +01006840 if (!mode)
6841 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08006842
Chris Wilsond2dff872011-04-19 08:36:26 +01006843 /* We need a framebuffer large enough to accommodate all accesses
6844 * that the plane may generate whilst we perform load detection.
6845 * We can not rely on the fbcon either being present (we get called
6846 * during its initialisation to detect all boot displays, or it may
6847 * not even exist) or that it is large enough to satisfy the
6848 * requested mode.
6849 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02006850 fb = mode_fits_in_fbdev(dev, mode);
6851 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01006852 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02006853 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
6854 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01006855 } else
6856 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02006857 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01006858 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Daniel Vetter7b240562012-12-12 00:35:33 +01006859 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00006860 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006861 }
Chris Wilsond2dff872011-04-19 08:36:26 +01006862
Chris Wilsonc0c36b942012-12-19 16:08:43 +00006863 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01006864 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01006865 if (old->release_fb)
6866 old->release_fb->funcs->destroy(old->release_fb);
Daniel Vetter7b240562012-12-12 00:35:33 +01006867 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00006868 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006869 }
Chris Wilson71731882011-04-19 23:10:58 +01006870
Jesse Barnes79e53942008-11-07 14:24:08 -08006871 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006872 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01006873 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08006874}
6875
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006876void intel_release_load_detect_pipe(struct drm_connector *connector,
Chris Wilson8261b192011-04-19 23:18:09 +01006877 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08006878{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006879 struct intel_encoder *intel_encoder =
6880 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01006881 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01006882 struct drm_crtc *crtc = encoder->crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -08006883
Chris Wilsond2dff872011-04-19 08:36:26 +01006884 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6885 connector->base.id, drm_get_connector_name(connector),
6886 encoder->base.id, drm_get_encoder_name(encoder));
6887
Chris Wilson8261b192011-04-19 23:18:09 +01006888 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02006889 to_intel_connector(connector)->new_encoder = NULL;
6890 intel_encoder->new_crtc = NULL;
6891 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01006892
Daniel Vetter36206362012-12-10 20:42:17 +01006893 if (old->release_fb) {
6894 drm_framebuffer_unregister_private(old->release_fb);
6895 drm_framebuffer_unreference(old->release_fb);
6896 }
Chris Wilsond2dff872011-04-19 08:36:26 +01006897
Daniel Vetter67c96402013-01-23 16:25:09 +00006898 mutex_unlock(&crtc->mutex);
Chris Wilson0622a532011-04-21 09:32:11 +01006899 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08006900 }
6901
Eric Anholtc751ce42010-03-25 11:48:48 -07006902 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02006903 if (old->dpms_mode != DRM_MODE_DPMS_ON)
6904 connector->funcs->dpms(connector, old->dpms_mode);
Daniel Vetter7b240562012-12-12 00:35:33 +01006905
6906 mutex_unlock(&crtc->mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08006907}
6908
6909/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03006910static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
6911 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006912{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03006913 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08006914 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03006915 int pipe = pipe_config->cpu_transcoder;
Jesse Barnes548f2452011-02-17 10:40:53 -08006916 u32 dpll = I915_READ(DPLL(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006917 u32 fp;
6918 intel_clock_t clock;
6919
6920 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Chris Wilson39adb7a2011-04-22 22:17:21 +01006921 fp = I915_READ(FP0(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006922 else
Chris Wilson39adb7a2011-04-22 22:17:21 +01006923 fp = I915_READ(FP1(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006924
6925 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006926 if (IS_PINEVIEW(dev)) {
6927 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
6928 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08006929 } else {
6930 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
6931 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
6932 }
6933
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006934 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006935 if (IS_PINEVIEW(dev))
6936 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
6937 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08006938 else
6939 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08006940 DPLL_FPA01_P1_POST_DIV_SHIFT);
6941
6942 switch (dpll & DPLL_MODE_MASK) {
6943 case DPLLB_MODE_DAC_SERIAL:
6944 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
6945 5 : 10;
6946 break;
6947 case DPLLB_MODE_LVDS:
6948 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
6949 7 : 14;
6950 break;
6951 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08006952 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08006953 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03006954 pipe_config->adjusted_mode.clock = 0;
6955 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08006956 }
6957
Daniel Vetterac58c3f2013-06-01 17:16:17 +02006958 if (IS_PINEVIEW(dev))
6959 pineview_clock(96000, &clock);
6960 else
6961 i9xx_clock(96000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006962 } else {
6963 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
6964
6965 if (is_lvds) {
6966 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
6967 DPLL_FPA01_P1_POST_DIV_SHIFT);
6968 clock.p2 = 14;
6969
6970 if ((dpll & PLL_REF_INPUT_MASK) ==
6971 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
6972 /* XXX: might not be 66MHz */
Daniel Vetterac58c3f2013-06-01 17:16:17 +02006973 i9xx_clock(66000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006974 } else
Daniel Vetterac58c3f2013-06-01 17:16:17 +02006975 i9xx_clock(48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006976 } else {
6977 if (dpll & PLL_P1_DIVIDE_BY_TWO)
6978 clock.p1 = 2;
6979 else {
6980 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
6981 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
6982 }
6983 if (dpll & PLL_P2_DIVIDE_BY_4)
6984 clock.p2 = 4;
6985 else
6986 clock.p2 = 2;
6987
Daniel Vetterac58c3f2013-06-01 17:16:17 +02006988 i9xx_clock(48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006989 }
6990 }
6991
Jesse Barnesf1f644d2013-06-27 00:39:25 +03006992 pipe_config->adjusted_mode.clock = clock.dot *
6993 pipe_config->pixel_multiplier;
6994}
6995
6996static void ironlake_crtc_clock_get(struct intel_crtc *crtc,
6997 struct intel_crtc_config *pipe_config)
6998{
6999 struct drm_device *dev = crtc->base.dev;
7000 struct drm_i915_private *dev_priv = dev->dev_private;
7001 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7002 int link_freq, repeat;
7003 u64 clock;
7004 u32 link_m, link_n;
7005
7006 repeat = pipe_config->pixel_multiplier;
7007
7008 /*
7009 * The calculation for the data clock is:
7010 * pixel_clock = ((m/n)*(link_clock * nr_lanes * repeat))/bpp
7011 * But we want to avoid losing precison if possible, so:
7012 * pixel_clock = ((m * link_clock * nr_lanes * repeat)/(n*bpp))
7013 *
7014 * and the link clock is simpler:
7015 * link_clock = (m * link_clock * repeat) / n
Jesse Barnes79e53942008-11-07 14:24:08 -08007016 */
7017
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007018 /*
7019 * We need to get the FDI or DP link clock here to derive
7020 * the M/N dividers.
7021 *
7022 * For FDI, we read it from the BIOS or use a fixed 2.7GHz.
7023 * For DP, it's either 1.62GHz or 2.7GHz.
7024 * We do our calculations in 10*MHz since we don't need much precison.
7025 */
7026 if (pipe_config->has_pch_encoder)
7027 link_freq = intel_fdi_link_freq(dev) * 10000;
7028 else
7029 link_freq = pipe_config->port_clock;
7030
7031 link_m = I915_READ(PIPE_LINK_M1(cpu_transcoder));
7032 link_n = I915_READ(PIPE_LINK_N1(cpu_transcoder));
7033
7034 if (!link_m || !link_n)
7035 return;
7036
7037 clock = ((u64)link_m * (u64)link_freq * (u64)repeat);
7038 do_div(clock, link_n);
7039
7040 pipe_config->adjusted_mode.clock = clock;
Jesse Barnes79e53942008-11-07 14:24:08 -08007041}
7042
7043/** Returns the currently programmed mode of the given pipe. */
7044struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
7045 struct drm_crtc *crtc)
7046{
Jesse Barnes548f2452011-02-17 10:40:53 -08007047 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08007048 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02007049 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08007050 struct drm_display_mode *mode;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007051 struct intel_crtc_config pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007052 int htot = I915_READ(HTOTAL(cpu_transcoder));
7053 int hsync = I915_READ(HSYNC(cpu_transcoder));
7054 int vtot = I915_READ(VTOTAL(cpu_transcoder));
7055 int vsync = I915_READ(VSYNC(cpu_transcoder));
Jesse Barnes79e53942008-11-07 14:24:08 -08007056
7057 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
7058 if (!mode)
7059 return NULL;
7060
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007061 /*
7062 * Construct a pipe_config sufficient for getting the clock info
7063 * back out of crtc_clock_get.
7064 *
7065 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
7066 * to use a real value here instead.
7067 */
Daniel Vettere143a212013-07-04 12:01:15 +02007068 pipe_config.cpu_transcoder = (enum transcoder) intel_crtc->pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007069 pipe_config.pixel_multiplier = 1;
7070 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
7071
7072 mode->clock = pipe_config.adjusted_mode.clock;
Jesse Barnes79e53942008-11-07 14:24:08 -08007073 mode->hdisplay = (htot & 0xffff) + 1;
7074 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
7075 mode->hsync_start = (hsync & 0xffff) + 1;
7076 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
7077 mode->vdisplay = (vtot & 0xffff) + 1;
7078 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
7079 mode->vsync_start = (vsync & 0xffff) + 1;
7080 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
7081
7082 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08007083
7084 return mode;
7085}
7086
Daniel Vetter3dec0092010-08-20 21:40:52 +02007087static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07007088{
7089 struct drm_device *dev = crtc->dev;
7090 drm_i915_private_t *dev_priv = dev->dev_private;
7091 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7092 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007093 int dpll_reg = DPLL(pipe);
7094 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07007095
Eric Anholtbad720f2009-10-22 16:11:14 -07007096 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07007097 return;
7098
7099 if (!dev_priv->lvds_downclock_avail)
7100 return;
7101
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007102 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07007103 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08007104 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007105
Sean Paul8ac5a6d2012-02-13 13:14:51 -05007106 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007107
7108 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
7109 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007110 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007111
Jesse Barnes652c3932009-08-17 13:31:43 -07007112 dpll = I915_READ(dpll_reg);
7113 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08007114 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007115 }
Jesse Barnes652c3932009-08-17 13:31:43 -07007116}
7117
7118static void intel_decrease_pllclock(struct drm_crtc *crtc)
7119{
7120 struct drm_device *dev = crtc->dev;
7121 drm_i915_private_t *dev_priv = dev->dev_private;
7122 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07007123
Eric Anholtbad720f2009-10-22 16:11:14 -07007124 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07007125 return;
7126
7127 if (!dev_priv->lvds_downclock_avail)
7128 return;
7129
7130 /*
7131 * Since this is called by a timer, we should never get here in
7132 * the manual case.
7133 */
7134 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01007135 int pipe = intel_crtc->pipe;
7136 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02007137 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01007138
Zhao Yakui44d98a62009-10-09 11:39:40 +08007139 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007140
Sean Paul8ac5a6d2012-02-13 13:14:51 -05007141 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007142
Chris Wilson074b5e12012-05-02 12:07:06 +01007143 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07007144 dpll |= DISPLAY_RATE_SELECT_FPA1;
7145 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007146 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007147 dpll = I915_READ(dpll_reg);
7148 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08007149 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007150 }
7151
7152}
7153
Chris Wilsonf047e392012-07-21 12:31:41 +01007154void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07007155{
Chris Wilsonf047e392012-07-21 12:31:41 +01007156 i915_update_gfx_val(dev->dev_private);
7157}
7158
7159void intel_mark_idle(struct drm_device *dev)
7160{
Chris Wilson725a5b52013-01-08 11:02:57 +00007161 struct drm_crtc *crtc;
7162
7163 if (!i915_powersave)
7164 return;
7165
7166 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
7167 if (!crtc->fb)
7168 continue;
7169
7170 intel_decrease_pllclock(crtc);
7171 }
Chris Wilsonf047e392012-07-21 12:31:41 +01007172}
7173
Chris Wilsonc65355b2013-06-06 16:53:41 -03007174void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
7175 struct intel_ring_buffer *ring)
Chris Wilsonf047e392012-07-21 12:31:41 +01007176{
7177 struct drm_device *dev = obj->base.dev;
Jesse Barnes652c3932009-08-17 13:31:43 -07007178 struct drm_crtc *crtc;
Jesse Barnes652c3932009-08-17 13:31:43 -07007179
7180 if (!i915_powersave)
7181 return;
7182
Jesse Barnes652c3932009-08-17 13:31:43 -07007183 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Jesse Barnes652c3932009-08-17 13:31:43 -07007184 if (!crtc->fb)
7185 continue;
7186
Chris Wilsonc65355b2013-06-06 16:53:41 -03007187 if (to_intel_framebuffer(crtc->fb)->obj != obj)
7188 continue;
7189
7190 intel_increase_pllclock(crtc);
7191 if (ring && intel_fbc_enabled(dev))
7192 ring->fbc_dirty = true;
Jesse Barnes652c3932009-08-17 13:31:43 -07007193 }
Jesse Barnes652c3932009-08-17 13:31:43 -07007194}
7195
Jesse Barnes79e53942008-11-07 14:24:08 -08007196static void intel_crtc_destroy(struct drm_crtc *crtc)
7197{
7198 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02007199 struct drm_device *dev = crtc->dev;
7200 struct intel_unpin_work *work;
7201 unsigned long flags;
7202
7203 spin_lock_irqsave(&dev->event_lock, flags);
7204 work = intel_crtc->unpin_work;
7205 intel_crtc->unpin_work = NULL;
7206 spin_unlock_irqrestore(&dev->event_lock, flags);
7207
7208 if (work) {
7209 cancel_work_sync(&work->work);
7210 kfree(work);
7211 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007212
Mika Kuoppala40ccc722013-04-23 17:27:08 +03007213 intel_crtc_cursor_set(crtc, NULL, 0, 0, 0);
7214
Jesse Barnes79e53942008-11-07 14:24:08 -08007215 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02007216
Jesse Barnes79e53942008-11-07 14:24:08 -08007217 kfree(intel_crtc);
7218}
7219
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007220static void intel_unpin_work_fn(struct work_struct *__work)
7221{
7222 struct intel_unpin_work *work =
7223 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007224 struct drm_device *dev = work->crtc->dev;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007225
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007226 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01007227 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00007228 drm_gem_object_unreference(&work->pending_flip_obj->base);
7229 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00007230
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007231 intel_update_fbc(dev);
7232 mutex_unlock(&dev->struct_mutex);
7233
7234 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
7235 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
7236
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007237 kfree(work);
7238}
7239
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007240static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01007241 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007242{
7243 drm_i915_private_t *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007244 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7245 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007246 unsigned long flags;
7247
7248 /* Ignore early vblank irqs */
7249 if (intel_crtc == NULL)
7250 return;
7251
7252 spin_lock_irqsave(&dev->event_lock, flags);
7253 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00007254
7255 /* Ensure we don't miss a work->pending update ... */
7256 smp_rmb();
7257
7258 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007259 spin_unlock_irqrestore(&dev->event_lock, flags);
7260 return;
7261 }
7262
Chris Wilsone7d841c2012-12-03 11:36:30 +00007263 /* and that the unpin work is consistent wrt ->pending. */
7264 smp_rmb();
7265
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007266 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007267
Rob Clark45a066e2012-10-08 14:50:40 -05007268 if (work->event)
7269 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007270
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01007271 drm_vblank_put(dev, intel_crtc->pipe);
7272
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007273 spin_unlock_irqrestore(&dev->event_lock, flags);
7274
Daniel Vetter2c10d572012-12-20 21:24:07 +01007275 wake_up_all(&dev_priv->pending_flip_queue);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007276
7277 queue_work(dev_priv->wq, &work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07007278
7279 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007280}
7281
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007282void intel_finish_page_flip(struct drm_device *dev, int pipe)
7283{
7284 drm_i915_private_t *dev_priv = dev->dev_private;
7285 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
7286
Mario Kleiner49b14a52010-12-09 07:00:07 +01007287 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007288}
7289
7290void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
7291{
7292 drm_i915_private_t *dev_priv = dev->dev_private;
7293 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
7294
Mario Kleiner49b14a52010-12-09 07:00:07 +01007295 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007296}
7297
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007298void intel_prepare_page_flip(struct drm_device *dev, int plane)
7299{
7300 drm_i915_private_t *dev_priv = dev->dev_private;
7301 struct intel_crtc *intel_crtc =
7302 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
7303 unsigned long flags;
7304
Chris Wilsone7d841c2012-12-03 11:36:30 +00007305 /* NB: An MMIO update of the plane base pointer will also
7306 * generate a page-flip completion irq, i.e. every modeset
7307 * is also accompanied by a spurious intel_prepare_page_flip().
7308 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007309 spin_lock_irqsave(&dev->event_lock, flags);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007310 if (intel_crtc->unpin_work)
7311 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007312 spin_unlock_irqrestore(&dev->event_lock, flags);
7313}
7314
Chris Wilsone7d841c2012-12-03 11:36:30 +00007315inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
7316{
7317 /* Ensure that the work item is consistent when activating it ... */
7318 smp_wmb();
7319 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
7320 /* and that it is marked active as soon as the irq could fire. */
7321 smp_wmb();
7322}
7323
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007324static int intel_gen2_queue_flip(struct drm_device *dev,
7325 struct drm_crtc *crtc,
7326 struct drm_framebuffer *fb,
7327 struct drm_i915_gem_object *obj)
7328{
7329 struct drm_i915_private *dev_priv = dev->dev_private;
7330 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007331 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007332 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007333 int ret;
7334
Daniel Vetter6d90c952012-04-26 23:28:05 +02007335 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007336 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007337 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007338
Daniel Vetter6d90c952012-04-26 23:28:05 +02007339 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007340 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007341 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007342
7343 /* Can't queue multiple flips, so wait for the previous
7344 * one to finish before executing the next.
7345 */
7346 if (intel_crtc->plane)
7347 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7348 else
7349 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007350 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7351 intel_ring_emit(ring, MI_NOOP);
7352 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7353 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7354 intel_ring_emit(ring, fb->pitches[0]);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007355 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007356 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00007357
7358 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007359 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007360 return 0;
7361
7362err_unpin:
7363 intel_unpin_fb_obj(obj);
7364err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007365 return ret;
7366}
7367
7368static int intel_gen3_queue_flip(struct drm_device *dev,
7369 struct drm_crtc *crtc,
7370 struct drm_framebuffer *fb,
7371 struct drm_i915_gem_object *obj)
7372{
7373 struct drm_i915_private *dev_priv = dev->dev_private;
7374 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007375 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007376 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007377 int ret;
7378
Daniel Vetter6d90c952012-04-26 23:28:05 +02007379 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007380 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007381 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007382
Daniel Vetter6d90c952012-04-26 23:28:05 +02007383 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007384 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007385 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007386
7387 if (intel_crtc->plane)
7388 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7389 else
7390 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007391 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7392 intel_ring_emit(ring, MI_NOOP);
7393 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
7394 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7395 intel_ring_emit(ring, fb->pitches[0]);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007396 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007397 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007398
Chris Wilsone7d841c2012-12-03 11:36:30 +00007399 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007400 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007401 return 0;
7402
7403err_unpin:
7404 intel_unpin_fb_obj(obj);
7405err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007406 return ret;
7407}
7408
7409static int intel_gen4_queue_flip(struct drm_device *dev,
7410 struct drm_crtc *crtc,
7411 struct drm_framebuffer *fb,
7412 struct drm_i915_gem_object *obj)
7413{
7414 struct drm_i915_private *dev_priv = dev->dev_private;
7415 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7416 uint32_t pf, pipesrc;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007417 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007418 int ret;
7419
Daniel Vetter6d90c952012-04-26 23:28:05 +02007420 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007421 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007422 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007423
Daniel Vetter6d90c952012-04-26 23:28:05 +02007424 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007425 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007426 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007427
7428 /* i965+ uses the linear or tiled offsets from the
7429 * Display Registers (which do not change across a page-flip)
7430 * so we need only reprogram the base address.
7431 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02007432 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7433 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7434 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02007435 intel_ring_emit(ring,
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007436 (i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset) |
Daniel Vetterc2c75132012-07-05 12:17:30 +02007437 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007438
7439 /* XXX Enabling the panel-fitter across page-flip is so far
7440 * untested on non-native modes, so ignore it for now.
7441 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
7442 */
7443 pf = 0;
7444 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007445 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007446
7447 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007448 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007449 return 0;
7450
7451err_unpin:
7452 intel_unpin_fb_obj(obj);
7453err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007454 return ret;
7455}
7456
7457static int intel_gen6_queue_flip(struct drm_device *dev,
7458 struct drm_crtc *crtc,
7459 struct drm_framebuffer *fb,
7460 struct drm_i915_gem_object *obj)
7461{
7462 struct drm_i915_private *dev_priv = dev->dev_private;
7463 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007464 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007465 uint32_t pf, pipesrc;
7466 int ret;
7467
Daniel Vetter6d90c952012-04-26 23:28:05 +02007468 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007469 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007470 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007471
Daniel Vetter6d90c952012-04-26 23:28:05 +02007472 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007473 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007474 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007475
Daniel Vetter6d90c952012-04-26 23:28:05 +02007476 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7477 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7478 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007479 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007480
Chris Wilson99d9acd2012-04-17 20:37:00 +01007481 /* Contrary to the suggestions in the documentation,
7482 * "Enable Panel Fitter" does not seem to be required when page
7483 * flipping with a non-native mode, and worse causes a normal
7484 * modeset to fail.
7485 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
7486 */
7487 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007488 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007489 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007490
7491 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007492 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007493 return 0;
7494
7495err_unpin:
7496 intel_unpin_fb_obj(obj);
7497err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007498 return ret;
7499}
7500
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007501/*
7502 * On gen7 we currently use the blit ring because (in early silicon at least)
7503 * the render ring doesn't give us interrpts for page flip completion, which
7504 * means clients will hang after the first flip is queued. Fortunately the
7505 * blit ring generates interrupts properly, so use it instead.
7506 */
7507static int intel_gen7_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);
7514 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007515 uint32_t plane_bit = 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007516 int ret;
7517
7518 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7519 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007520 goto err;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007521
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007522 switch(intel_crtc->plane) {
7523 case PLANE_A:
7524 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
7525 break;
7526 case PLANE_B:
7527 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
7528 break;
7529 case PLANE_C:
7530 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
7531 break;
7532 default:
7533 WARN_ONCE(1, "unknown plane in flip command\n");
7534 ret = -ENODEV;
Eugeni Dodonovab3951e2012-06-18 19:03:38 -03007535 goto err_unpin;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007536 }
7537
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007538 ret = intel_ring_begin(ring, 4);
7539 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007540 goto err_unpin;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007541
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007542 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007543 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007544 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007545 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +00007546
7547 intel_mark_page_flip_active(intel_crtc);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007548 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007549 return 0;
7550
7551err_unpin:
7552 intel_unpin_fb_obj(obj);
7553err:
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007554 return ret;
7555}
7556
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007557static int intel_default_queue_flip(struct drm_device *dev,
7558 struct drm_crtc *crtc,
7559 struct drm_framebuffer *fb,
7560 struct drm_i915_gem_object *obj)
7561{
7562 return -ENODEV;
7563}
7564
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007565static int intel_crtc_page_flip(struct drm_crtc *crtc,
7566 struct drm_framebuffer *fb,
7567 struct drm_pending_vblank_event *event)
7568{
7569 struct drm_device *dev = crtc->dev;
7570 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02007571 struct drm_framebuffer *old_fb = crtc->fb;
7572 struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007573 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7574 struct intel_unpin_work *work;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007575 unsigned long flags;
Chris Wilson52e68632010-08-08 10:15:59 +01007576 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007577
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03007578 /* Can't change pixel format via MI display flips. */
7579 if (fb->pixel_format != crtc->fb->pixel_format)
7580 return -EINVAL;
7581
7582 /*
7583 * TILEOFF/LINOFF registers can't be changed via MI display flips.
7584 * Note that pitch changes could also affect these register.
7585 */
7586 if (INTEL_INFO(dev)->gen > 3 &&
7587 (fb->offsets[0] != crtc->fb->offsets[0] ||
7588 fb->pitches[0] != crtc->fb->pitches[0]))
7589 return -EINVAL;
7590
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007591 work = kzalloc(sizeof *work, GFP_KERNEL);
7592 if (work == NULL)
7593 return -ENOMEM;
7594
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007595 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007596 work->crtc = crtc;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02007597 work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007598 INIT_WORK(&work->work, intel_unpin_work_fn);
7599
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007600 ret = drm_vblank_get(dev, intel_crtc->pipe);
7601 if (ret)
7602 goto free_work;
7603
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007604 /* We borrow the event spin lock for protecting unpin_work */
7605 spin_lock_irqsave(&dev->event_lock, flags);
7606 if (intel_crtc->unpin_work) {
7607 spin_unlock_irqrestore(&dev->event_lock, flags);
7608 kfree(work);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007609 drm_vblank_put(dev, intel_crtc->pipe);
Chris Wilson468f0b42010-05-27 13:18:13 +01007610
7611 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007612 return -EBUSY;
7613 }
7614 intel_crtc->unpin_work = work;
7615 spin_unlock_irqrestore(&dev->event_lock, flags);
7616
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007617 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
7618 flush_workqueue(dev_priv->wq);
7619
Chris Wilson79158102012-05-23 11:13:58 +01007620 ret = i915_mutex_lock_interruptible(dev);
7621 if (ret)
7622 goto cleanup;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007623
Jesse Barnes75dfca82010-02-10 15:09:44 -08007624 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00007625 drm_gem_object_reference(&work->old_fb_obj->base);
7626 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007627
7628 crtc->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01007629
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007630 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007631
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01007632 work->enable_stall_check = true;
7633
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007634 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +02007635 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007636
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007637 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
7638 if (ret)
7639 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007640
Chris Wilson7782de32011-07-08 12:22:41 +01007641 intel_disable_fbc(dev);
Chris Wilsonc65355b2013-06-06 16:53:41 -03007642 intel_mark_fb_busy(obj, NULL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007643 mutex_unlock(&dev->struct_mutex);
7644
Jesse Barnese5510fa2010-07-01 16:48:37 -07007645 trace_i915_flip_request(intel_crtc->plane, obj);
7646
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007647 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01007648
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007649cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007650 atomic_dec(&intel_crtc->unpin_work_count);
Ville Syrjälä4a35f832013-02-22 16:53:38 +02007651 crtc->fb = old_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00007652 drm_gem_object_unreference(&work->old_fb_obj->base);
7653 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01007654 mutex_unlock(&dev->struct_mutex);
7655
Chris Wilson79158102012-05-23 11:13:58 +01007656cleanup:
Chris Wilson96b099f2010-06-07 14:03:04 +01007657 spin_lock_irqsave(&dev->event_lock, flags);
7658 intel_crtc->unpin_work = NULL;
7659 spin_unlock_irqrestore(&dev->event_lock, flags);
7660
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007661 drm_vblank_put(dev, intel_crtc->pipe);
7662free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01007663 kfree(work);
7664
7665 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007666}
7667
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007668static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007669 .mode_set_base_atomic = intel_pipe_set_base_atomic,
7670 .load_lut = intel_crtc_load_lut,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007671};
7672
Daniel Vetter50f56112012-07-02 09:35:43 +02007673static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
7674 struct drm_crtc *crtc)
7675{
7676 struct drm_device *dev;
7677 struct drm_crtc *tmp;
7678 int crtc_mask = 1;
7679
7680 WARN(!crtc, "checking null crtc?\n");
7681
7682 dev = crtc->dev;
7683
7684 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
7685 if (tmp == crtc)
7686 break;
7687 crtc_mask <<= 1;
7688 }
7689
7690 if (encoder->possible_crtcs & crtc_mask)
7691 return true;
7692 return false;
7693}
7694
Daniel Vetter9a935852012-07-05 22:34:27 +02007695/**
7696 * intel_modeset_update_staged_output_state
7697 *
7698 * Updates the staged output configuration state, e.g. after we've read out the
7699 * current hw state.
7700 */
7701static void intel_modeset_update_staged_output_state(struct drm_device *dev)
7702{
7703 struct intel_encoder *encoder;
7704 struct intel_connector *connector;
7705
7706 list_for_each_entry(connector, &dev->mode_config.connector_list,
7707 base.head) {
7708 connector->new_encoder =
7709 to_intel_encoder(connector->base.encoder);
7710 }
7711
7712 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7713 base.head) {
7714 encoder->new_crtc =
7715 to_intel_crtc(encoder->base.crtc);
7716 }
7717}
7718
7719/**
7720 * intel_modeset_commit_output_state
7721 *
7722 * This function copies the stage display pipe configuration to the real one.
7723 */
7724static void intel_modeset_commit_output_state(struct drm_device *dev)
7725{
7726 struct intel_encoder *encoder;
7727 struct intel_connector *connector;
7728
7729 list_for_each_entry(connector, &dev->mode_config.connector_list,
7730 base.head) {
7731 connector->base.encoder = &connector->new_encoder->base;
7732 }
7733
7734 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7735 base.head) {
7736 encoder->base.crtc = &encoder->new_crtc->base;
7737 }
7738}
7739
Daniel Vetter050f7ae2013-06-02 13:26:23 +02007740static void
7741connected_sink_compute_bpp(struct intel_connector * connector,
7742 struct intel_crtc_config *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007743{
Daniel Vetter050f7ae2013-06-02 13:26:23 +02007744 int bpp = pipe_config->pipe_bpp;
7745
7746 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
7747 connector->base.base.id,
7748 drm_get_connector_name(&connector->base));
7749
7750 /* Don't use an invalid EDID bpc value */
7751 if (connector->base.display_info.bpc &&
7752 connector->base.display_info.bpc * 3 < bpp) {
7753 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
7754 bpp, connector->base.display_info.bpc*3);
7755 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
7756 }
7757
7758 /* Clamp bpp to 8 on screens without EDID 1.4 */
7759 if (connector->base.display_info.bpc == 0 && bpp > 24) {
7760 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
7761 bpp);
7762 pipe_config->pipe_bpp = 24;
7763 }
7764}
7765
7766static int
7767compute_baseline_pipe_bpp(struct intel_crtc *crtc,
7768 struct drm_framebuffer *fb,
7769 struct intel_crtc_config *pipe_config)
7770{
7771 struct drm_device *dev = crtc->base.dev;
7772 struct intel_connector *connector;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007773 int bpp;
7774
Daniel Vetterd42264b2013-03-28 16:38:08 +01007775 switch (fb->pixel_format) {
7776 case DRM_FORMAT_C8:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007777 bpp = 8*3; /* since we go through a colormap */
7778 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01007779 case DRM_FORMAT_XRGB1555:
7780 case DRM_FORMAT_ARGB1555:
7781 /* checked in intel_framebuffer_init already */
7782 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
7783 return -EINVAL;
7784 case DRM_FORMAT_RGB565:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007785 bpp = 6*3; /* min is 18bpp */
7786 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01007787 case DRM_FORMAT_XBGR8888:
7788 case DRM_FORMAT_ABGR8888:
7789 /* checked in intel_framebuffer_init already */
7790 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
7791 return -EINVAL;
7792 case DRM_FORMAT_XRGB8888:
7793 case DRM_FORMAT_ARGB8888:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007794 bpp = 8*3;
7795 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01007796 case DRM_FORMAT_XRGB2101010:
7797 case DRM_FORMAT_ARGB2101010:
7798 case DRM_FORMAT_XBGR2101010:
7799 case DRM_FORMAT_ABGR2101010:
7800 /* checked in intel_framebuffer_init already */
7801 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
Daniel Vetterbaba1332013-03-27 00:45:00 +01007802 return -EINVAL;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007803 bpp = 10*3;
7804 break;
Daniel Vetterbaba1332013-03-27 00:45:00 +01007805 /* TODO: gen4+ supports 16 bpc floating point, too. */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007806 default:
7807 DRM_DEBUG_KMS("unsupported depth\n");
7808 return -EINVAL;
7809 }
7810
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007811 pipe_config->pipe_bpp = bpp;
7812
7813 /* Clamp display bpp to EDID value */
7814 list_for_each_entry(connector, &dev->mode_config.connector_list,
Daniel Vetter050f7ae2013-06-02 13:26:23 +02007815 base.head) {
Daniel Vetter1b829e02013-06-02 13:26:24 +02007816 if (!connector->new_encoder ||
7817 connector->new_encoder->new_crtc != crtc)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007818 continue;
7819
Daniel Vetter050f7ae2013-06-02 13:26:23 +02007820 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007821 }
7822
7823 return bpp;
7824}
7825
Daniel Vetterc0b03412013-05-28 12:05:54 +02007826static void intel_dump_pipe_config(struct intel_crtc *crtc,
7827 struct intel_crtc_config *pipe_config,
7828 const char *context)
7829{
7830 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
7831 context, pipe_name(crtc->pipe));
7832
7833 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
7834 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
7835 pipe_config->pipe_bpp, pipe_config->dither);
7836 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
7837 pipe_config->has_pch_encoder,
7838 pipe_config->fdi_lanes,
7839 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
7840 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
7841 pipe_config->fdi_m_n.tu);
7842 DRM_DEBUG_KMS("requested mode:\n");
7843 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
7844 DRM_DEBUG_KMS("adjusted mode:\n");
7845 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
7846 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
7847 pipe_config->gmch_pfit.control,
7848 pipe_config->gmch_pfit.pgm_ratios,
7849 pipe_config->gmch_pfit.lvds_border_bits);
7850 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x\n",
7851 pipe_config->pch_pfit.pos,
7852 pipe_config->pch_pfit.size);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03007853 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Daniel Vetterc0b03412013-05-28 12:05:54 +02007854}
7855
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02007856static bool check_encoder_cloning(struct drm_crtc *crtc)
7857{
7858 int num_encoders = 0;
7859 bool uncloneable_encoders = false;
7860 struct intel_encoder *encoder;
7861
7862 list_for_each_entry(encoder, &crtc->dev->mode_config.encoder_list,
7863 base.head) {
7864 if (&encoder->new_crtc->base != crtc)
7865 continue;
7866
7867 num_encoders++;
7868 if (!encoder->cloneable)
7869 uncloneable_encoders = true;
7870 }
7871
7872 return !(num_encoders > 1 && uncloneable_encoders);
7873}
7874
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007875static struct intel_crtc_config *
7876intel_modeset_pipe_config(struct drm_crtc *crtc,
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007877 struct drm_framebuffer *fb,
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007878 struct drm_display_mode *mode)
Daniel Vetter7758a112012-07-08 19:40:39 +02007879{
7880 struct drm_device *dev = crtc->dev;
Daniel Vetter7758a112012-07-08 19:40:39 +02007881 struct drm_encoder_helper_funcs *encoder_funcs;
7882 struct intel_encoder *encoder;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007883 struct intel_crtc_config *pipe_config;
Daniel Vettere29c22c2013-02-21 00:00:16 +01007884 int plane_bpp, ret = -EINVAL;
7885 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +02007886
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02007887 if (!check_encoder_cloning(crtc)) {
7888 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
7889 return ERR_PTR(-EINVAL);
7890 }
7891
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007892 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7893 if (!pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +02007894 return ERR_PTR(-ENOMEM);
7895
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007896 drm_mode_copy(&pipe_config->adjusted_mode, mode);
7897 drm_mode_copy(&pipe_config->requested_mode, mode);
Daniel Vettere143a212013-07-04 12:01:15 +02007898 pipe_config->cpu_transcoder =
7899 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007900 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007901
Daniel Vetter050f7ae2013-06-02 13:26:23 +02007902 /* Compute a starting value for pipe_config->pipe_bpp taking the source
7903 * plane pixel format and any sink constraints into account. Returns the
7904 * source plane bpp so that dithering can be selected on mismatches
7905 * after encoders and crtc also have had their say. */
7906 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
7907 fb, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007908 if (plane_bpp < 0)
7909 goto fail;
7910
Daniel Vettere29c22c2013-02-21 00:00:16 +01007911encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +02007912 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +02007913 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +02007914 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +02007915
Daniel Vetter7758a112012-07-08 19:40:39 +02007916 /* Pass our mode to the connectors and the CRTC to give them a chance to
7917 * adjust it according to limitations or connector properties, and also
7918 * a chance to reject the mode entirely.
7919 */
7920 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7921 base.head) {
7922
7923 if (&encoder->new_crtc->base != crtc)
7924 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +01007925
7926 if (encoder->compute_config) {
7927 if (!(encoder->compute_config(encoder, pipe_config))) {
7928 DRM_DEBUG_KMS("Encoder config failure\n");
7929 goto fail;
7930 }
7931
7932 continue;
7933 }
7934
Daniel Vetter7758a112012-07-08 19:40:39 +02007935 encoder_funcs = encoder->base.helper_private;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007936 if (!(encoder_funcs->mode_fixup(&encoder->base,
7937 &pipe_config->requested_mode,
7938 &pipe_config->adjusted_mode))) {
Daniel Vetter7758a112012-07-08 19:40:39 +02007939 DRM_DEBUG_KMS("Encoder fixup failed\n");
7940 goto fail;
7941 }
7942 }
7943
Daniel Vetterff9a6752013-06-01 17:16:21 +02007944 /* Set default port clock if not overwritten by the encoder. Needs to be
7945 * done afterwards in case the encoder adjusts the mode. */
7946 if (!pipe_config->port_clock)
7947 pipe_config->port_clock = pipe_config->adjusted_mode.clock;
7948
Daniel Vettera43f6e02013-06-07 23:10:32 +02007949 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01007950 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +02007951 DRM_DEBUG_KMS("CRTC fixup failed\n");
7952 goto fail;
7953 }
Daniel Vettere29c22c2013-02-21 00:00:16 +01007954
7955 if (ret == RETRY) {
7956 if (WARN(!retry, "loop in pipe configuration computation\n")) {
7957 ret = -EINVAL;
7958 goto fail;
7959 }
7960
7961 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
7962 retry = false;
7963 goto encoder_retry;
7964 }
7965
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007966 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
7967 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
7968 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
7969
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007970 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +02007971fail:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007972 kfree(pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01007973 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +02007974}
7975
Daniel Vettere2e1ed42012-07-08 21:14:38 +02007976/* Computes which crtcs are affected and sets the relevant bits in the mask. For
7977 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
7978static void
7979intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
7980 unsigned *prepare_pipes, unsigned *disable_pipes)
7981{
7982 struct intel_crtc *intel_crtc;
7983 struct drm_device *dev = crtc->dev;
7984 struct intel_encoder *encoder;
7985 struct intel_connector *connector;
7986 struct drm_crtc *tmp_crtc;
7987
7988 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
7989
7990 /* Check which crtcs have changed outputs connected to them, these need
7991 * to be part of the prepare_pipes mask. We don't (yet) support global
7992 * modeset across multiple crtcs, so modeset_pipes will only have one
7993 * bit set at most. */
7994 list_for_each_entry(connector, &dev->mode_config.connector_list,
7995 base.head) {
7996 if (connector->base.encoder == &connector->new_encoder->base)
7997 continue;
7998
7999 if (connector->base.encoder) {
8000 tmp_crtc = connector->base.encoder->crtc;
8001
8002 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
8003 }
8004
8005 if (connector->new_encoder)
8006 *prepare_pipes |=
8007 1 << connector->new_encoder->new_crtc->pipe;
8008 }
8009
8010 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8011 base.head) {
8012 if (encoder->base.crtc == &encoder->new_crtc->base)
8013 continue;
8014
8015 if (encoder->base.crtc) {
8016 tmp_crtc = encoder->base.crtc;
8017
8018 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
8019 }
8020
8021 if (encoder->new_crtc)
8022 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
8023 }
8024
8025 /* Check for any pipes that will be fully disabled ... */
8026 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
8027 base.head) {
8028 bool used = false;
8029
8030 /* Don't try to disable disabled crtcs. */
8031 if (!intel_crtc->base.enabled)
8032 continue;
8033
8034 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8035 base.head) {
8036 if (encoder->new_crtc == intel_crtc)
8037 used = true;
8038 }
8039
8040 if (!used)
8041 *disable_pipes |= 1 << intel_crtc->pipe;
8042 }
8043
8044
8045 /* set_mode is also used to update properties on life display pipes. */
8046 intel_crtc = to_intel_crtc(crtc);
8047 if (crtc->enabled)
8048 *prepare_pipes |= 1 << intel_crtc->pipe;
8049
Daniel Vetterb6c51642013-04-12 18:48:43 +02008050 /*
8051 * For simplicity do a full modeset on any pipe where the output routing
8052 * changed. We could be more clever, but that would require us to be
8053 * more careful with calling the relevant encoder->mode_set functions.
8054 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +02008055 if (*prepare_pipes)
8056 *modeset_pipes = *prepare_pipes;
8057
8058 /* ... and mask these out. */
8059 *modeset_pipes &= ~(*disable_pipes);
8060 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +02008061
8062 /*
8063 * HACK: We don't (yet) fully support global modesets. intel_set_config
8064 * obies this rule, but the modeset restore mode of
8065 * intel_modeset_setup_hw_state does not.
8066 */
8067 *modeset_pipes &= 1 << intel_crtc->pipe;
8068 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +02008069
8070 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
8071 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +02008072}
8073
Daniel Vetterea9d7582012-07-10 10:42:52 +02008074static bool intel_crtc_in_use(struct drm_crtc *crtc)
8075{
8076 struct drm_encoder *encoder;
8077 struct drm_device *dev = crtc->dev;
8078
8079 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
8080 if (encoder->crtc == crtc)
8081 return true;
8082
8083 return false;
8084}
8085
8086static void
8087intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
8088{
8089 struct intel_encoder *intel_encoder;
8090 struct intel_crtc *intel_crtc;
8091 struct drm_connector *connector;
8092
8093 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
8094 base.head) {
8095 if (!intel_encoder->base.crtc)
8096 continue;
8097
8098 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
8099
8100 if (prepare_pipes & (1 << intel_crtc->pipe))
8101 intel_encoder->connectors_active = false;
8102 }
8103
8104 intel_modeset_commit_output_state(dev);
8105
8106 /* Update computed state. */
8107 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
8108 base.head) {
8109 intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
8110 }
8111
8112 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
8113 if (!connector->encoder || !connector->encoder->crtc)
8114 continue;
8115
8116 intel_crtc = to_intel_crtc(connector->encoder->crtc);
8117
8118 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +02008119 struct drm_property *dpms_property =
8120 dev->mode_config.dpms_property;
8121
Daniel Vetterea9d7582012-07-10 10:42:52 +02008122 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -05008123 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +02008124 dpms_property,
8125 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +02008126
8127 intel_encoder = to_intel_encoder(connector->encoder);
8128 intel_encoder->connectors_active = true;
8129 }
8130 }
8131
8132}
8133
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008134static bool intel_fuzzy_clock_check(struct intel_crtc_config *cur,
8135 struct intel_crtc_config *new)
8136{
8137 int clock1, clock2, diff;
8138
8139 clock1 = cur->adjusted_mode.clock;
8140 clock2 = new->adjusted_mode.clock;
8141
8142 if (clock1 == clock2)
8143 return true;
8144
8145 if (!clock1 || !clock2)
8146 return false;
8147
8148 diff = abs(clock1 - clock2);
8149
8150 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
8151 return true;
8152
8153 return false;
8154}
8155
Daniel Vetter25c5b262012-07-08 22:08:04 +02008156#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
8157 list_for_each_entry((intel_crtc), \
8158 &(dev)->mode_config.crtc_list, \
8159 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +02008160 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +02008161
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008162static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008163intel_pipe_config_compare(struct drm_device *dev,
8164 struct intel_crtc_config *current_config,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008165 struct intel_crtc_config *pipe_config)
8166{
Daniel Vetter66e985c2013-06-05 13:34:20 +02008167#define PIPE_CONF_CHECK_X(name) \
8168 if (current_config->name != pipe_config->name) { \
8169 DRM_ERROR("mismatch in " #name " " \
8170 "(expected 0x%08x, found 0x%08x)\n", \
8171 current_config->name, \
8172 pipe_config->name); \
8173 return false; \
8174 }
8175
Daniel Vetter08a24032013-04-19 11:25:34 +02008176#define PIPE_CONF_CHECK_I(name) \
8177 if (current_config->name != pipe_config->name) { \
8178 DRM_ERROR("mismatch in " #name " " \
8179 "(expected %i, found %i)\n", \
8180 current_config->name, \
8181 pipe_config->name); \
8182 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +01008183 }
8184
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008185#define PIPE_CONF_CHECK_FLAGS(name, mask) \
8186 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -07008187 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008188 "(expected %i, found %i)\n", \
8189 current_config->name & (mask), \
8190 pipe_config->name & (mask)); \
8191 return false; \
8192 }
8193
Daniel Vetterbb760062013-06-06 14:55:52 +02008194#define PIPE_CONF_QUIRK(quirk) \
8195 ((current_config->quirks | pipe_config->quirks) & (quirk))
8196
Daniel Vettereccb1402013-05-22 00:50:22 +02008197 PIPE_CONF_CHECK_I(cpu_transcoder);
8198
Daniel Vetter08a24032013-04-19 11:25:34 +02008199 PIPE_CONF_CHECK_I(has_pch_encoder);
8200 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +02008201 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
8202 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
8203 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
8204 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
8205 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +02008206
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008207 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
8208 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
8209 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
8210 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
8211 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
8212 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
8213
8214 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
8215 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
8216 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
8217 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
8218 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
8219 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
8220
Daniel Vetterc93f54c2013-06-27 19:47:19 +02008221 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008222
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008223 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8224 DRM_MODE_FLAG_INTERLACE);
8225
Daniel Vetterbb760062013-06-06 14:55:52 +02008226 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
8227 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8228 DRM_MODE_FLAG_PHSYNC);
8229 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8230 DRM_MODE_FLAG_NHSYNC);
8231 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8232 DRM_MODE_FLAG_PVSYNC);
8233 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8234 DRM_MODE_FLAG_NVSYNC);
8235 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -07008236
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008237 PIPE_CONF_CHECK_I(requested_mode.hdisplay);
8238 PIPE_CONF_CHECK_I(requested_mode.vdisplay);
8239
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008240 PIPE_CONF_CHECK_I(gmch_pfit.control);
8241 /* pfit ratios are autocomputed by the hw on gen4+ */
8242 if (INTEL_INFO(dev)->gen < 4)
8243 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
8244 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
8245 PIPE_CONF_CHECK_I(pch_pfit.pos);
8246 PIPE_CONF_CHECK_I(pch_pfit.size);
8247
Paulo Zanoni42db64e2013-05-31 16:33:22 -03008248 PIPE_CONF_CHECK_I(ips_enabled);
8249
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008250 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008251 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008252 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008253 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
8254 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008255
Daniel Vetter66e985c2013-06-05 13:34:20 +02008256#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +02008257#undef PIPE_CONF_CHECK_I
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008258#undef PIPE_CONF_CHECK_FLAGS
Daniel Vetterbb760062013-06-06 14:55:52 +02008259#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008260
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008261 if (!IS_HASWELL(dev)) {
8262 if (!intel_fuzzy_clock_check(current_config, pipe_config)) {
Jesse Barnes6f024882013-07-01 10:19:09 -07008263 DRM_ERROR("mismatch in clock (expected %d, found %d)\n",
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008264 current_config->adjusted_mode.clock,
8265 pipe_config->adjusted_mode.clock);
8266 return false;
8267 }
8268 }
8269
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008270 return true;
8271}
8272
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02008273static void
8274check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008275{
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008276 struct intel_connector *connector;
8277
8278 list_for_each_entry(connector, &dev->mode_config.connector_list,
8279 base.head) {
8280 /* This also checks the encoder/connector hw state with the
8281 * ->get_hw_state callbacks. */
8282 intel_connector_check_state(connector);
8283
8284 WARN(&connector->new_encoder->base != connector->base.encoder,
8285 "connector's staged encoder doesn't match current encoder\n");
8286 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02008287}
8288
8289static void
8290check_encoder_state(struct drm_device *dev)
8291{
8292 struct intel_encoder *encoder;
8293 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008294
8295 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8296 base.head) {
8297 bool enabled = false;
8298 bool active = false;
8299 enum pipe pipe, tracked_pipe;
8300
8301 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
8302 encoder->base.base.id,
8303 drm_get_encoder_name(&encoder->base));
8304
8305 WARN(&encoder->new_crtc->base != encoder->base.crtc,
8306 "encoder's stage crtc doesn't match current crtc\n");
8307 WARN(encoder->connectors_active && !encoder->base.crtc,
8308 "encoder's active_connectors set, but no crtc\n");
8309
8310 list_for_each_entry(connector, &dev->mode_config.connector_list,
8311 base.head) {
8312 if (connector->base.encoder != &encoder->base)
8313 continue;
8314 enabled = true;
8315 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
8316 active = true;
8317 }
8318 WARN(!!encoder->base.crtc != enabled,
8319 "encoder's enabled state mismatch "
8320 "(expected %i, found %i)\n",
8321 !!encoder->base.crtc, enabled);
8322 WARN(active && !encoder->base.crtc,
8323 "active encoder with no crtc\n");
8324
8325 WARN(encoder->connectors_active != active,
8326 "encoder's computed active state doesn't match tracked active state "
8327 "(expected %i, found %i)\n", active, encoder->connectors_active);
8328
8329 active = encoder->get_hw_state(encoder, &pipe);
8330 WARN(active != encoder->connectors_active,
8331 "encoder's hw state doesn't match sw tracking "
8332 "(expected %i, found %i)\n",
8333 encoder->connectors_active, active);
8334
8335 if (!encoder->base.crtc)
8336 continue;
8337
8338 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
8339 WARN(active && pipe != tracked_pipe,
8340 "active encoder's pipe doesn't match"
8341 "(expected %i, found %i)\n",
8342 tracked_pipe, pipe);
8343
8344 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02008345}
8346
8347static void
8348check_crtc_state(struct drm_device *dev)
8349{
8350 drm_i915_private_t *dev_priv = dev->dev_private;
8351 struct intel_crtc *crtc;
8352 struct intel_encoder *encoder;
8353 struct intel_crtc_config pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008354
8355 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
8356 base.head) {
8357 bool enabled = false;
8358 bool active = false;
8359
Jesse Barnes045ac3b2013-05-14 17:08:26 -07008360 memset(&pipe_config, 0, sizeof(pipe_config));
8361
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008362 DRM_DEBUG_KMS("[CRTC:%d]\n",
8363 crtc->base.base.id);
8364
8365 WARN(crtc->active && !crtc->base.enabled,
8366 "active crtc, but not enabled in sw tracking\n");
8367
8368 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8369 base.head) {
8370 if (encoder->base.crtc != &crtc->base)
8371 continue;
8372 enabled = true;
8373 if (encoder->connectors_active)
8374 active = true;
8375 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008376
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008377 WARN(active != crtc->active,
8378 "crtc's computed active state doesn't match tracked active state "
8379 "(expected %i, found %i)\n", active, crtc->active);
8380 WARN(enabled != crtc->base.enabled,
8381 "crtc's computed enabled state doesn't match tracked enabled state "
8382 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
8383
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008384 active = dev_priv->display.get_pipe_config(crtc,
8385 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +02008386
8387 /* hw state is inconsistent with the pipe A quirk */
8388 if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
8389 active = crtc->active;
8390
Daniel Vetter6c49f242013-06-06 12:45:25 +02008391 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8392 base.head) {
8393 if (encoder->base.crtc != &crtc->base)
8394 continue;
Jesse Barnes510d5f22013-07-01 15:50:17 -07008395 if (encoder->get_config)
Daniel Vetter6c49f242013-06-06 12:45:25 +02008396 encoder->get_config(encoder, &pipe_config);
8397 }
8398
Jesse Barnes510d5f22013-07-01 15:50:17 -07008399 if (dev_priv->display.get_clock)
8400 dev_priv->display.get_clock(crtc, &pipe_config);
8401
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008402 WARN(crtc->active != active,
8403 "crtc active state doesn't match with hw state "
8404 "(expected %i, found %i)\n", crtc->active, active);
8405
Daniel Vetterc0b03412013-05-28 12:05:54 +02008406 if (active &&
8407 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
8408 WARN(1, "pipe state doesn't match!\n");
8409 intel_dump_pipe_config(crtc, &pipe_config,
8410 "[hw state]");
8411 intel_dump_pipe_config(crtc, &crtc->config,
8412 "[sw state]");
8413 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008414 }
8415}
8416
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02008417static void
8418check_shared_dpll_state(struct drm_device *dev)
8419{
8420 drm_i915_private_t *dev_priv = dev->dev_private;
8421 struct intel_crtc *crtc;
8422 struct intel_dpll_hw_state dpll_hw_state;
8423 int i;
Daniel Vetter53589012013-06-05 13:34:16 +02008424
8425 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8426 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
8427 int enabled_crtcs = 0, active_crtcs = 0;
8428 bool active;
8429
8430 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
8431
8432 DRM_DEBUG_KMS("%s\n", pll->name);
8433
8434 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
8435
8436 WARN(pll->active > pll->refcount,
8437 "more active pll users than references: %i vs %i\n",
8438 pll->active, pll->refcount);
8439 WARN(pll->active && !pll->on,
8440 "pll in active use but not on in sw tracking\n");
8441 WARN(pll->on != active,
8442 "pll on state mismatch (expected %i, found %i)\n",
8443 pll->on, active);
8444
8445 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
8446 base.head) {
8447 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
8448 enabled_crtcs++;
8449 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
8450 active_crtcs++;
8451 }
8452 WARN(pll->active != active_crtcs,
8453 "pll active crtcs mismatch (expected %i, found %i)\n",
8454 pll->active, active_crtcs);
8455 WARN(pll->refcount != enabled_crtcs,
8456 "pll enabled crtcs mismatch (expected %i, found %i)\n",
8457 pll->refcount, enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008458
8459 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
8460 sizeof(dpll_hw_state)),
8461 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +02008462 }
Daniel Vettera6778b32012-07-02 09:56:42 +02008463}
8464
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02008465void
8466intel_modeset_check_state(struct drm_device *dev)
8467{
8468 check_connector_state(dev);
8469 check_encoder_state(dev);
8470 check_crtc_state(dev);
8471 check_shared_dpll_state(dev);
8472}
8473
Daniel Vetterf30da182013-04-11 20:22:50 +02008474static int __intel_set_mode(struct drm_crtc *crtc,
8475 struct drm_display_mode *mode,
8476 int x, int y, struct drm_framebuffer *fb)
Daniel Vettera6778b32012-07-02 09:56:42 +02008477{
8478 struct drm_device *dev = crtc->dev;
Daniel Vetterdbf2b54e2012-07-02 11:18:29 +02008479 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008480 struct drm_display_mode *saved_mode, *saved_hwmode;
8481 struct intel_crtc_config *pipe_config = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +02008482 struct intel_crtc *intel_crtc;
8483 unsigned disable_pipes, prepare_pipes, modeset_pipes;
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008484 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +02008485
Tim Gardner3ac18232012-12-07 07:54:26 -07008486 saved_mode = kmalloc(2 * sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008487 if (!saved_mode)
8488 return -ENOMEM;
Tim Gardner3ac18232012-12-07 07:54:26 -07008489 saved_hwmode = saved_mode + 1;
Daniel Vettera6778b32012-07-02 09:56:42 +02008490
Daniel Vettere2e1ed42012-07-08 21:14:38 +02008491 intel_modeset_affected_pipes(crtc, &modeset_pipes,
Daniel Vetter25c5b262012-07-08 22:08:04 +02008492 &prepare_pipes, &disable_pipes);
8493
Tim Gardner3ac18232012-12-07 07:54:26 -07008494 *saved_hwmode = crtc->hwmode;
8495 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02008496
Daniel Vetter25c5b262012-07-08 22:08:04 +02008497 /* Hack: Because we don't (yet) support global modeset on multiple
8498 * crtcs, we don't keep track of the new mode for more than one crtc.
8499 * Hence simply check whether any bit is set in modeset_pipes in all the
8500 * pieces of code that are not yet converted to deal with mutliple crtcs
8501 * changing their mode at the same time. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02008502 if (modeset_pipes) {
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008503 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008504 if (IS_ERR(pipe_config)) {
8505 ret = PTR_ERR(pipe_config);
8506 pipe_config = NULL;
8507
Tim Gardner3ac18232012-12-07 07:54:26 -07008508 goto out;
Daniel Vetter25c5b262012-07-08 22:08:04 +02008509 }
Daniel Vetterc0b03412013-05-28 12:05:54 +02008510 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
8511 "[modeset]");
Daniel Vettera6778b32012-07-02 09:56:42 +02008512 }
8513
Daniel Vetter460da9162013-03-27 00:44:51 +01008514 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
8515 intel_crtc_disable(&intel_crtc->base);
8516
Daniel Vetterea9d7582012-07-10 10:42:52 +02008517 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
8518 if (intel_crtc->base.enabled)
8519 dev_priv->display.crtc_disable(&intel_crtc->base);
8520 }
Daniel Vettera6778b32012-07-02 09:56:42 +02008521
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02008522 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
8523 * to set it here already despite that we pass it down the callchain.
8524 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008525 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +02008526 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008527 /* mode_set/enable/disable functions rely on a correct pipe
8528 * config. */
8529 to_intel_crtc(crtc)->config = *pipe_config;
8530 }
Daniel Vetter7758a112012-07-08 19:40:39 +02008531
Daniel Vetterea9d7582012-07-10 10:42:52 +02008532 /* Only after disabling all output pipelines that will be changed can we
8533 * update the the output configuration. */
8534 intel_modeset_update_state(dev, prepare_pipes);
8535
Daniel Vetter47fab732012-10-26 10:58:18 +02008536 if (dev_priv->display.modeset_global_resources)
8537 dev_priv->display.modeset_global_resources(dev);
8538
Daniel Vettera6778b32012-07-02 09:56:42 +02008539 /* Set up the DPLL and any encoders state that needs to adjust or depend
8540 * on the DPLL.
8541 */
Daniel Vetter25c5b262012-07-08 22:08:04 +02008542 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008543 ret = intel_crtc_mode_set(&intel_crtc->base,
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008544 x, y, fb);
8545 if (ret)
8546 goto done;
Daniel Vettera6778b32012-07-02 09:56:42 +02008547 }
8548
8549 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02008550 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
8551 dev_priv->display.crtc_enable(&intel_crtc->base);
Daniel Vettera6778b32012-07-02 09:56:42 +02008552
Daniel Vetter25c5b262012-07-08 22:08:04 +02008553 if (modeset_pipes) {
8554 /* Store real post-adjustment hardware mode. */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008555 crtc->hwmode = pipe_config->adjusted_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02008556
Daniel Vetter25c5b262012-07-08 22:08:04 +02008557 /* Calculate and store various constants which
8558 * are later needed by vblank and swap-completion
8559 * timestamping. They are derived from true hwmode.
8560 */
8561 drm_calc_timestamping_constants(crtc);
8562 }
Daniel Vettera6778b32012-07-02 09:56:42 +02008563
8564 /* FIXME: add subpixel order */
8565done:
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008566 if (ret && crtc->enabled) {
Tim Gardner3ac18232012-12-07 07:54:26 -07008567 crtc->hwmode = *saved_hwmode;
8568 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02008569 }
8570
Tim Gardner3ac18232012-12-07 07:54:26 -07008571out:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008572 kfree(pipe_config);
Tim Gardner3ac18232012-12-07 07:54:26 -07008573 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +02008574 return ret;
8575}
8576
Daniel Vetterf30da182013-04-11 20:22:50 +02008577int intel_set_mode(struct drm_crtc *crtc,
8578 struct drm_display_mode *mode,
8579 int x, int y, struct drm_framebuffer *fb)
8580{
8581 int ret;
8582
8583 ret = __intel_set_mode(crtc, mode, x, y, fb);
8584
8585 if (ret == 0)
8586 intel_modeset_check_state(crtc->dev);
8587
8588 return ret;
8589}
8590
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008591void intel_crtc_restore_mode(struct drm_crtc *crtc)
8592{
8593 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->fb);
8594}
8595
Daniel Vetter25c5b262012-07-08 22:08:04 +02008596#undef for_each_intel_crtc_masked
8597
Daniel Vetterd9e55602012-07-04 22:16:09 +02008598static void intel_set_config_free(struct intel_set_config *config)
8599{
8600 if (!config)
8601 return;
8602
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008603 kfree(config->save_connector_encoders);
8604 kfree(config->save_encoder_crtcs);
Daniel Vetterd9e55602012-07-04 22:16:09 +02008605 kfree(config);
8606}
8607
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008608static int intel_set_config_save_state(struct drm_device *dev,
8609 struct intel_set_config *config)
8610{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008611 struct drm_encoder *encoder;
8612 struct drm_connector *connector;
8613 int count;
8614
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008615 config->save_encoder_crtcs =
8616 kcalloc(dev->mode_config.num_encoder,
8617 sizeof(struct drm_crtc *), GFP_KERNEL);
8618 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008619 return -ENOMEM;
8620
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008621 config->save_connector_encoders =
8622 kcalloc(dev->mode_config.num_connector,
8623 sizeof(struct drm_encoder *), GFP_KERNEL);
8624 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008625 return -ENOMEM;
8626
8627 /* Copy data. Note that driver private data is not affected.
8628 * Should anything bad happen only the expected state is
8629 * restored, not the drivers personal bookkeeping.
8630 */
8631 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008632 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008633 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008634 }
8635
8636 count = 0;
8637 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008638 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008639 }
8640
8641 return 0;
8642}
8643
8644static void intel_set_config_restore_state(struct drm_device *dev,
8645 struct intel_set_config *config)
8646{
Daniel Vetter9a935852012-07-05 22:34:27 +02008647 struct intel_encoder *encoder;
8648 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008649 int count;
8650
8651 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008652 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
8653 encoder->new_crtc =
8654 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008655 }
8656
8657 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008658 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
8659 connector->new_encoder =
8660 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008661 }
8662}
8663
Imre Deake3de42b2013-05-03 19:44:07 +02008664static bool
8665is_crtc_connector_off(struct drm_crtc *crtc, struct drm_connector *connectors,
8666 int num_connectors)
8667{
8668 int i;
8669
8670 for (i = 0; i < num_connectors; i++)
8671 if (connectors[i].encoder &&
8672 connectors[i].encoder->crtc == crtc &&
8673 connectors[i].dpms != DRM_MODE_DPMS_ON)
8674 return true;
8675
8676 return false;
8677}
8678
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008679static void
8680intel_set_config_compute_mode_changes(struct drm_mode_set *set,
8681 struct intel_set_config *config)
8682{
8683
8684 /* We should be able to check here if the fb has the same properties
8685 * and then just flip_or_move it */
Imre Deake3de42b2013-05-03 19:44:07 +02008686 if (set->connectors != NULL &&
8687 is_crtc_connector_off(set->crtc, *set->connectors,
8688 set->num_connectors)) {
8689 config->mode_changed = true;
8690 } else if (set->crtc->fb != set->fb) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008691 /* If we have no fb then treat it as a full mode set */
8692 if (set->crtc->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +03008693 struct intel_crtc *intel_crtc =
8694 to_intel_crtc(set->crtc);
8695
8696 if (intel_crtc->active && i915_fastboot) {
8697 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
8698 config->fb_changed = true;
8699 } else {
8700 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
8701 config->mode_changed = true;
8702 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008703 } else if (set->fb == NULL) {
8704 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +01008705 } else if (set->fb->pixel_format !=
8706 set->crtc->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008707 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02008708 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008709 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02008710 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008711 }
8712
Daniel Vetter835c5872012-07-10 18:11:08 +02008713 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008714 config->fb_changed = true;
8715
8716 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
8717 DRM_DEBUG_KMS("modes are different, full mode set\n");
8718 drm_mode_debug_printmodeline(&set->crtc->mode);
8719 drm_mode_debug_printmodeline(set->mode);
8720 config->mode_changed = true;
8721 }
8722}
8723
Daniel Vetter2e431052012-07-04 22:42:15 +02008724static int
Daniel Vetter9a935852012-07-05 22:34:27 +02008725intel_modeset_stage_output_state(struct drm_device *dev,
8726 struct drm_mode_set *set,
8727 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +02008728{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008729 struct drm_crtc *new_crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02008730 struct intel_connector *connector;
8731 struct intel_encoder *encoder;
Daniel Vetter2e431052012-07-04 22:42:15 +02008732 int count, ro;
Daniel Vetter50f56112012-07-02 09:35:43 +02008733
Damien Lespiau9abdda72013-02-13 13:29:23 +00008734 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +02008735 * of connectors. For paranoia, double-check this. */
8736 WARN_ON(!set->fb && (set->num_connectors != 0));
8737 WARN_ON(set->fb && (set->num_connectors == 0));
8738
Daniel Vetter50f56112012-07-02 09:35:43 +02008739 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008740 list_for_each_entry(connector, &dev->mode_config.connector_list,
8741 base.head) {
8742 /* Otherwise traverse passed in connector list and get encoders
8743 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +02008744 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02008745 if (set->connectors[ro] == &connector->base) {
8746 connector->new_encoder = connector->encoder;
Daniel Vetter50f56112012-07-02 09:35:43 +02008747 break;
8748 }
8749 }
8750
Daniel Vetter9a935852012-07-05 22:34:27 +02008751 /* If we disable the crtc, disable all its connectors. Also, if
8752 * the connector is on the changing crtc but not on the new
8753 * connector list, disable it. */
8754 if ((!set->fb || ro == set->num_connectors) &&
8755 connector->base.encoder &&
8756 connector->base.encoder->crtc == set->crtc) {
8757 connector->new_encoder = NULL;
8758
8759 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
8760 connector->base.base.id,
8761 drm_get_connector_name(&connector->base));
8762 }
8763
8764
8765 if (&connector->new_encoder->base != connector->base.encoder) {
Daniel Vetter50f56112012-07-02 09:35:43 +02008766 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008767 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02008768 }
Daniel Vetter9a935852012-07-05 22:34:27 +02008769 }
8770 /* connector->new_encoder is now updated for all connectors. */
8771
8772 /* Update crtc of enabled connectors. */
Daniel Vetter50f56112012-07-02 09:35:43 +02008773 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008774 list_for_each_entry(connector, &dev->mode_config.connector_list,
8775 base.head) {
8776 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +02008777 continue;
8778
Daniel Vetter9a935852012-07-05 22:34:27 +02008779 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +02008780
8781 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02008782 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +02008783 new_crtc = set->crtc;
8784 }
8785
8786 /* Make sure the new CRTC will work with the encoder */
Daniel Vetter9a935852012-07-05 22:34:27 +02008787 if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
8788 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008789 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +02008790 }
Daniel Vetter9a935852012-07-05 22:34:27 +02008791 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
8792
8793 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
8794 connector->base.base.id,
8795 drm_get_connector_name(&connector->base),
8796 new_crtc->base.id);
8797 }
8798
8799 /* Check for any encoders that needs to be disabled. */
8800 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8801 base.head) {
8802 list_for_each_entry(connector,
8803 &dev->mode_config.connector_list,
8804 base.head) {
8805 if (connector->new_encoder == encoder) {
8806 WARN_ON(!connector->new_encoder->new_crtc);
8807
8808 goto next_encoder;
8809 }
8810 }
8811 encoder->new_crtc = NULL;
8812next_encoder:
8813 /* Only now check for crtc changes so we don't miss encoders
8814 * that will be disabled. */
8815 if (&encoder->new_crtc->base != encoder->base.crtc) {
Daniel Vetter50f56112012-07-02 09:35:43 +02008816 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008817 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02008818 }
8819 }
Daniel Vetter9a935852012-07-05 22:34:27 +02008820 /* Now we've also updated encoder->new_crtc for all encoders. */
Daniel Vetter50f56112012-07-02 09:35:43 +02008821
Daniel Vetter2e431052012-07-04 22:42:15 +02008822 return 0;
8823}
8824
8825static int intel_crtc_set_config(struct drm_mode_set *set)
8826{
8827 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +02008828 struct drm_mode_set save_set;
8829 struct intel_set_config *config;
8830 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +02008831
Daniel Vetter8d3e3752012-07-05 16:09:09 +02008832 BUG_ON(!set);
8833 BUG_ON(!set->crtc);
8834 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +02008835
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01008836 /* Enforce sane interface api - has been abused by the fb helper. */
8837 BUG_ON(!set->mode && set->fb);
8838 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +02008839
Daniel Vetter2e431052012-07-04 22:42:15 +02008840 if (set->fb) {
8841 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
8842 set->crtc->base.id, set->fb->base.id,
8843 (int)set->num_connectors, set->x, set->y);
8844 } else {
8845 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +02008846 }
8847
8848 dev = set->crtc->dev;
8849
8850 ret = -ENOMEM;
8851 config = kzalloc(sizeof(*config), GFP_KERNEL);
8852 if (!config)
8853 goto out_config;
8854
8855 ret = intel_set_config_save_state(dev, config);
8856 if (ret)
8857 goto out_config;
8858
8859 save_set.crtc = set->crtc;
8860 save_set.mode = &set->crtc->mode;
8861 save_set.x = set->crtc->x;
8862 save_set.y = set->crtc->y;
8863 save_set.fb = set->crtc->fb;
8864
8865 /* Compute whether we need a full modeset, only an fb base update or no
8866 * change at all. In the future we might also check whether only the
8867 * mode changed, e.g. for LVDS where we only change the panel fitter in
8868 * such cases. */
8869 intel_set_config_compute_mode_changes(set, config);
8870
Daniel Vetter9a935852012-07-05 22:34:27 +02008871 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +02008872 if (ret)
8873 goto fail;
8874
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008875 if (config->mode_changed) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008876 ret = intel_set_mode(set->crtc, set->mode,
8877 set->x, set->y, set->fb);
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008878 } else if (config->fb_changed) {
Ville Syrjälä4878cae2013-02-18 19:08:48 +02008879 intel_crtc_wait_for_pending_flips(set->crtc);
8880
Daniel Vetter4f660f42012-07-02 09:47:37 +02008881 ret = intel_pipe_set_base(set->crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +02008882 set->x, set->y, set->fb);
Daniel Vetter50f56112012-07-02 09:35:43 +02008883 }
8884
Chris Wilson2d05eae2013-05-03 17:36:25 +01008885 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +02008886 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
8887 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +02008888fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +01008889 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +02008890
Chris Wilson2d05eae2013-05-03 17:36:25 +01008891 /* Try to restore the config */
8892 if (config->mode_changed &&
8893 intel_set_mode(save_set.crtc, save_set.mode,
8894 save_set.x, save_set.y, save_set.fb))
8895 DRM_ERROR("failed to restore config after modeset failure\n");
8896 }
Daniel Vetter50f56112012-07-02 09:35:43 +02008897
Daniel Vetterd9e55602012-07-04 22:16:09 +02008898out_config:
8899 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +02008900 return ret;
8901}
8902
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008903static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008904 .cursor_set = intel_crtc_cursor_set,
8905 .cursor_move = intel_crtc_cursor_move,
8906 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +02008907 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008908 .destroy = intel_crtc_destroy,
8909 .page_flip = intel_crtc_page_flip,
8910};
8911
Paulo Zanoni79f689a2012-10-05 12:05:52 -03008912static void intel_cpu_pll_init(struct drm_device *dev)
8913{
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008914 if (HAS_DDI(dev))
Paulo Zanoni79f689a2012-10-05 12:05:52 -03008915 intel_ddi_pll_init(dev);
8916}
8917
Daniel Vetter53589012013-06-05 13:34:16 +02008918static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
8919 struct intel_shared_dpll *pll,
8920 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008921{
Daniel Vetter53589012013-06-05 13:34:16 +02008922 uint32_t val;
8923
8924 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +02008925 hw_state->dpll = val;
8926 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
8927 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +02008928
8929 return val & DPLL_VCO_ENABLE;
8930}
8931
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02008932static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
8933 struct intel_shared_dpll *pll)
8934{
8935 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
8936 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
8937}
8938
Daniel Vettere7b903d2013-06-05 13:34:14 +02008939static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
8940 struct intel_shared_dpll *pll)
8941{
Daniel Vettere7b903d2013-06-05 13:34:14 +02008942 /* PCH refclock must be enabled first */
8943 assert_pch_refclk_enabled(dev_priv);
8944
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02008945 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
8946
8947 /* Wait for the clocks to stabilize. */
8948 POSTING_READ(PCH_DPLL(pll->id));
8949 udelay(150);
8950
8951 /* The pixel multiplier can only be updated once the
8952 * DPLL is enabled and the clocks are stable.
8953 *
8954 * So write it again.
8955 */
8956 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
8957 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +02008958 udelay(200);
8959}
8960
8961static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
8962 struct intel_shared_dpll *pll)
8963{
8964 struct drm_device *dev = dev_priv->dev;
8965 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +02008966
8967 /* Make sure no transcoder isn't still depending on us. */
8968 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
8969 if (intel_crtc_to_shared_dpll(crtc) == pll)
8970 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
8971 }
8972
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02008973 I915_WRITE(PCH_DPLL(pll->id), 0);
8974 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +02008975 udelay(200);
8976}
8977
Daniel Vetter46edb022013-06-05 13:34:12 +02008978static char *ibx_pch_dpll_names[] = {
8979 "PCH DPLL A",
8980 "PCH DPLL B",
8981};
8982
Daniel Vetter7c74ade2013-06-05 13:34:11 +02008983static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008984{
Daniel Vettere7b903d2013-06-05 13:34:14 +02008985 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008986 int i;
8987
Daniel Vetter7c74ade2013-06-05 13:34:11 +02008988 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008989
Daniel Vettere72f9fb2013-06-05 13:34:06 +02008990 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +02008991 dev_priv->shared_dplls[i].id = i;
8992 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02008993 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +02008994 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
8995 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +02008996 dev_priv->shared_dplls[i].get_hw_state =
8997 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008998 }
8999}
9000
Daniel Vetter7c74ade2013-06-05 13:34:11 +02009001static void intel_shared_dpll_init(struct drm_device *dev)
9002{
Daniel Vettere7b903d2013-06-05 13:34:14 +02009003 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +02009004
9005 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
9006 ibx_pch_dpll_init(dev);
9007 else
9008 dev_priv->num_shared_dpll = 0;
9009
9010 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
9011 DRM_DEBUG_KMS("%i shared PLLs initialized\n",
9012 dev_priv->num_shared_dpll);
9013}
9014
Hannes Ederb358d0a2008-12-18 21:18:47 +01009015static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -08009016{
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08009017 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08009018 struct intel_crtc *intel_crtc;
9019 int i;
9020
9021 intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
9022 if (intel_crtc == NULL)
9023 return;
9024
9025 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
9026
9027 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -08009028 for (i = 0; i < 256; i++) {
9029 intel_crtc->lut_r[i] = i;
9030 intel_crtc->lut_g[i] = i;
9031 intel_crtc->lut_b[i] = i;
9032 }
9033
Jesse Barnes80824002009-09-10 15:28:06 -07009034 /* Swap pipes & planes for FBC on pre-965 */
9035 intel_crtc->pipe = pipe;
9036 intel_crtc->plane = pipe;
Chris Wilsone2e767a2010-09-13 16:53:12 +01009037 if (IS_MOBILE(dev) && IS_GEN3(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08009038 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +01009039 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07009040 }
9041
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08009042 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
9043 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
9044 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
9045 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
9046
Jesse Barnes79e53942008-11-07 14:24:08 -08009047 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -08009048}
9049
Carl Worth08d7b3d2009-04-29 14:43:54 -07009050int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00009051 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -07009052{
Carl Worth08d7b3d2009-04-29 14:43:54 -07009053 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +02009054 struct drm_mode_object *drmmode_obj;
9055 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009056
Daniel Vetter1cff8f62012-04-24 09:55:08 +02009057 if (!drm_core_check_feature(dev, DRIVER_MODESET))
9058 return -ENODEV;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009059
Daniel Vetterc05422d2009-08-11 16:05:30 +02009060 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
9061 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -07009062
Daniel Vetterc05422d2009-08-11 16:05:30 +02009063 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -07009064 DRM_ERROR("no such CRTC id\n");
9065 return -EINVAL;
9066 }
9067
Daniel Vetterc05422d2009-08-11 16:05:30 +02009068 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
9069 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009070
Daniel Vetterc05422d2009-08-11 16:05:30 +02009071 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009072}
9073
Daniel Vetter66a92782012-07-12 20:08:18 +02009074static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08009075{
Daniel Vetter66a92782012-07-12 20:08:18 +02009076 struct drm_device *dev = encoder->base.dev;
9077 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08009078 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009079 int entry = 0;
9080
Daniel Vetter66a92782012-07-12 20:08:18 +02009081 list_for_each_entry(source_encoder,
9082 &dev->mode_config.encoder_list, base.head) {
9083
9084 if (encoder == source_encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08009085 index_mask |= (1 << entry);
Daniel Vetter66a92782012-07-12 20:08:18 +02009086
9087 /* Intel hw has only one MUX where enocoders could be cloned. */
9088 if (encoder->cloneable && source_encoder->cloneable)
9089 index_mask |= (1 << entry);
9090
Jesse Barnes79e53942008-11-07 14:24:08 -08009091 entry++;
9092 }
Chris Wilson4ef69c72010-09-09 15:14:28 +01009093
Jesse Barnes79e53942008-11-07 14:24:08 -08009094 return index_mask;
9095}
9096
Chris Wilson4d302442010-12-14 19:21:29 +00009097static bool has_edp_a(struct drm_device *dev)
9098{
9099 struct drm_i915_private *dev_priv = dev->dev_private;
9100
9101 if (!IS_MOBILE(dev))
9102 return false;
9103
9104 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
9105 return false;
9106
9107 if (IS_GEN5(dev) &&
9108 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
9109 return false;
9110
9111 return true;
9112}
9113
Jesse Barnes79e53942008-11-07 14:24:08 -08009114static void intel_setup_outputs(struct drm_device *dev)
9115{
Eric Anholt725e30a2009-01-22 13:01:02 -08009116 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +01009117 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009118 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08009119
Daniel Vetterc9093352013-06-06 22:22:47 +02009120 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009121
Paulo Zanonic40c0f52013-04-12 18:16:53 -03009122 if (!IS_ULT(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -02009123 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009124
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009125 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -03009126 int found;
9127
9128 /* Haswell uses DDI functions to detect digital outputs */
9129 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
9130 /* DDI A only supports eDP */
9131 if (found)
9132 intel_ddi_init(dev, PORT_A);
9133
9134 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
9135 * register */
9136 found = I915_READ(SFUSE_STRAP);
9137
9138 if (found & SFUSE_STRAP_DDIB_DETECTED)
9139 intel_ddi_init(dev, PORT_B);
9140 if (found & SFUSE_STRAP_DDIC_DETECTED)
9141 intel_ddi_init(dev, PORT_C);
9142 if (found & SFUSE_STRAP_DDID_DETECTED)
9143 intel_ddi_init(dev, PORT_D);
9144 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009145 int found;
Daniel Vetter270b3042012-10-27 15:52:05 +02009146 dpd_is_edp = intel_dpd_is_edp(dev);
9147
9148 if (has_edp_a(dev))
9149 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009150
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009151 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +08009152 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +01009153 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009154 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -03009155 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08009156 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009157 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009158 }
9159
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009160 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -03009161 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009162
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009163 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -03009164 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009165
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08009166 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009167 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08009168
Daniel Vetter270b3042012-10-27 15:52:05 +02009169 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009170 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -07009171 } else if (IS_VALLEYVIEW(dev)) {
Gajanan Bhat19c03922012-09-27 19:13:07 +05309172 /* Check for built-in panel first. Shares lanes with HDMI on SDVOC */
Ville Syrjälä67cfc202013-01-25 21:44:44 +02009173 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
9174 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +05309175
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009176 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
Paulo Zanonie2debe92013-02-18 19:00:27 -03009177 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
9178 PORT_B);
Ville Syrjälä67cfc202013-01-25 21:44:44 +02009179 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
9180 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Jesse Barnes4a87d652012-06-15 11:55:16 -07009181 }
Zhenyu Wang103a1962009-11-27 11:44:36 +08009182 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +08009183 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -08009184
Paulo Zanonie2debe92013-02-18 19:00:27 -03009185 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009186 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009187 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009188 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
9189 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009190 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009191 }
Ma Ling27185ae2009-08-24 13:50:23 +08009192
Imre Deake7281ea2013-05-08 13:14:08 +03009193 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009194 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -08009195 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -04009196
9197 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -04009198
Paulo Zanonie2debe92013-02-18 19:00:27 -03009199 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009200 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009201 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009202 }
Ma Ling27185ae2009-08-24 13:50:23 +08009203
Paulo Zanonie2debe92013-02-18 19:00:27 -03009204 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +08009205
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009206 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
9207 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009208 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009209 }
Imre Deake7281ea2013-05-08 13:14:08 +03009210 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009211 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -08009212 }
Ma Ling27185ae2009-08-24 13:50:23 +08009213
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009214 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +03009215 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009216 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -07009217 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08009218 intel_dvo_init(dev);
9219
Zhenyu Wang103a1962009-11-27 11:44:36 +08009220 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08009221 intel_tv_init(dev);
9222
Chris Wilson4ef69c72010-09-09 15:14:28 +01009223 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
9224 encoder->base.possible_crtcs = encoder->crtc_mask;
9225 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +02009226 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -08009227 }
Chris Wilson47356eb2011-01-11 17:06:04 +00009228
Paulo Zanonidde86e22012-12-01 12:04:25 -02009229 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +02009230
9231 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009232}
9233
9234static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
9235{
9236 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08009237
9238 drm_framebuffer_cleanup(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00009239 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08009240
9241 kfree(intel_fb);
9242}
9243
9244static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +00009245 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08009246 unsigned int *handle)
9247{
9248 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00009249 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08009250
Chris Wilson05394f32010-11-08 19:18:58 +00009251 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -08009252}
9253
9254static const struct drm_framebuffer_funcs intel_fb_funcs = {
9255 .destroy = intel_user_framebuffer_destroy,
9256 .create_handle = intel_user_framebuffer_create_handle,
9257};
9258
Dave Airlie38651672010-03-30 05:34:13 +00009259int intel_framebuffer_init(struct drm_device *dev,
9260 struct intel_framebuffer *intel_fb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009261 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilson05394f32010-11-08 19:18:58 +00009262 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -08009263{
Chris Wilsona35cdaa2013-06-25 17:26:45 +01009264 int pitch_limit;
Jesse Barnes79e53942008-11-07 14:24:08 -08009265 int ret;
9266
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009267 if (obj->tiling_mode == I915_TILING_Y) {
9268 DRM_DEBUG("hardware does not support tiling Y\n");
Chris Wilson57cd6502010-08-08 12:34:44 +01009269 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009270 }
Chris Wilson57cd6502010-08-08 12:34:44 +01009271
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009272 if (mode_cmd->pitches[0] & 63) {
9273 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
9274 mode_cmd->pitches[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +01009275 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009276 }
Chris Wilson57cd6502010-08-08 12:34:44 +01009277
Chris Wilsona35cdaa2013-06-25 17:26:45 +01009278 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
9279 pitch_limit = 32*1024;
9280 } else if (INTEL_INFO(dev)->gen >= 4) {
9281 if (obj->tiling_mode)
9282 pitch_limit = 16*1024;
9283 else
9284 pitch_limit = 32*1024;
9285 } else if (INTEL_INFO(dev)->gen >= 3) {
9286 if (obj->tiling_mode)
9287 pitch_limit = 8*1024;
9288 else
9289 pitch_limit = 16*1024;
9290 } else
9291 /* XXX DSPC is limited to 4k tiled */
9292 pitch_limit = 8*1024;
9293
9294 if (mode_cmd->pitches[0] > pitch_limit) {
9295 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
9296 obj->tiling_mode ? "tiled" : "linear",
9297 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02009298 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009299 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02009300
9301 if (obj->tiling_mode != I915_TILING_NONE &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009302 mode_cmd->pitches[0] != obj->stride) {
9303 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
9304 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02009305 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009306 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02009307
Ville Syrjälä57779d02012-10-31 17:50:14 +02009308 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009309 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +02009310 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02009311 case DRM_FORMAT_RGB565:
9312 case DRM_FORMAT_XRGB8888:
9313 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02009314 break;
9315 case DRM_FORMAT_XRGB1555:
9316 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009317 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +00009318 DRM_DEBUG("unsupported pixel format: %s\n",
9319 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +02009320 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009321 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02009322 break;
9323 case DRM_FORMAT_XBGR8888:
9324 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +02009325 case DRM_FORMAT_XRGB2101010:
9326 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02009327 case DRM_FORMAT_XBGR2101010:
9328 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009329 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +00009330 DRM_DEBUG("unsupported pixel format: %s\n",
9331 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +02009332 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009333 }
Jesse Barnesb5626742011-06-24 12:19:27 -07009334 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02009335 case DRM_FORMAT_YUYV:
9336 case DRM_FORMAT_UYVY:
9337 case DRM_FORMAT_YVYU:
9338 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009339 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +00009340 DRM_DEBUG("unsupported pixel format: %s\n",
9341 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +02009342 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009343 }
Chris Wilson57cd6502010-08-08 12:34:44 +01009344 break;
9345 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +00009346 DRM_DEBUG("unsupported pixel format: %s\n",
9347 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +01009348 return -EINVAL;
9349 }
9350
Ville Syrjälä90f9a332012-10-31 17:50:19 +02009351 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
9352 if (mode_cmd->offsets[0] != 0)
9353 return -EINVAL;
9354
Daniel Vetterc7d73f62012-12-13 23:38:38 +01009355 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
9356 intel_fb->obj = obj;
9357
Jesse Barnes79e53942008-11-07 14:24:08 -08009358 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
9359 if (ret) {
9360 DRM_ERROR("framebuffer init failed %d\n", ret);
9361 return ret;
9362 }
9363
Jesse Barnes79e53942008-11-07 14:24:08 -08009364 return 0;
9365}
9366
Jesse Barnes79e53942008-11-07 14:24:08 -08009367static struct drm_framebuffer *
9368intel_user_framebuffer_create(struct drm_device *dev,
9369 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009370 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -08009371{
Chris Wilson05394f32010-11-08 19:18:58 +00009372 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08009373
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009374 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
9375 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +00009376 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +01009377 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -08009378
Chris Wilsond2dff872011-04-19 08:36:26 +01009379 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08009380}
9381
Jesse Barnes79e53942008-11-07 14:24:08 -08009382static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -08009383 .fb_create = intel_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +00009384 .output_poll_changed = intel_fb_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -08009385};
9386
Jesse Barnese70236a2009-09-21 10:42:27 -07009387/* Set up chip specific display functions */
9388static void intel_init_display(struct drm_device *dev)
9389{
9390 struct drm_i915_private *dev_priv = dev->dev_private;
9391
Daniel Vetteree9300b2013-06-03 22:40:22 +02009392 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
9393 dev_priv->display.find_dpll = g4x_find_best_dpll;
9394 else if (IS_VALLEYVIEW(dev))
9395 dev_priv->display.find_dpll = vlv_find_best_dpll;
9396 else if (IS_PINEVIEW(dev))
9397 dev_priv->display.find_dpll = pnv_find_best_dpll;
9398 else
9399 dev_priv->display.find_dpll = i9xx_find_best_dpll;
9400
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009401 if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009402 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009403 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02009404 dev_priv->display.crtc_enable = haswell_crtc_enable;
9405 dev_priv->display.crtc_disable = haswell_crtc_disable;
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009406 dev_priv->display.off = haswell_crtc_off;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009407 dev_priv->display.update_plane = ironlake_update_plane;
9408 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009409 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009410 dev_priv->display.get_clock = ironlake_crtc_clock_get;
Eric Anholtf564048e2011-03-30 13:01:02 -07009411 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +02009412 dev_priv->display.crtc_enable = ironlake_crtc_enable;
9413 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009414 dev_priv->display.off = ironlake_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -07009415 dev_priv->display.update_plane = ironlake_update_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -07009416 } else if (IS_VALLEYVIEW(dev)) {
9417 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009418 dev_priv->display.get_clock = i9xx_crtc_clock_get;
Jesse Barnes89b667f2013-04-18 14:51:36 -07009419 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
9420 dev_priv->display.crtc_enable = valleyview_crtc_enable;
9421 dev_priv->display.crtc_disable = i9xx_crtc_disable;
9422 dev_priv->display.off = i9xx_crtc_off;
9423 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07009424 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009425 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009426 dev_priv->display.get_clock = i9xx_crtc_clock_get;
Eric Anholtf564048e2011-03-30 13:01:02 -07009427 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +02009428 dev_priv->display.crtc_enable = i9xx_crtc_enable;
9429 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009430 dev_priv->display.off = i9xx_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -07009431 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07009432 }
Jesse Barnese70236a2009-09-21 10:42:27 -07009433
Jesse Barnese70236a2009-09-21 10:42:27 -07009434 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07009435 if (IS_VALLEYVIEW(dev))
9436 dev_priv->display.get_display_clock_speed =
9437 valleyview_get_display_clock_speed;
9438 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -07009439 dev_priv->display.get_display_clock_speed =
9440 i945_get_display_clock_speed;
9441 else if (IS_I915G(dev))
9442 dev_priv->display.get_display_clock_speed =
9443 i915_get_display_clock_speed;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05009444 else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07009445 dev_priv->display.get_display_clock_speed =
9446 i9xx_misc_get_display_clock_speed;
9447 else if (IS_I915GM(dev))
9448 dev_priv->display.get_display_clock_speed =
9449 i915gm_get_display_clock_speed;
9450 else if (IS_I865G(dev))
9451 dev_priv->display.get_display_clock_speed =
9452 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +02009453 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07009454 dev_priv->display.get_display_clock_speed =
9455 i855_get_display_clock_speed;
9456 else /* 852, 830 */
9457 dev_priv->display.get_display_clock_speed =
9458 i830_get_display_clock_speed;
9459
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08009460 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01009461 if (IS_GEN5(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -07009462 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08009463 dev_priv->display.write_eld = ironlake_write_eld;
Yuanhan Liu13982612010-12-15 15:42:31 +08009464 } else if (IS_GEN6(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -07009465 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08009466 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnes357555c2011-04-28 15:09:55 -07009467 } else if (IS_IVYBRIDGE(dev)) {
9468 /* FIXME: detect B0+ stepping and use auto training */
9469 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08009470 dev_priv->display.write_eld = ironlake_write_eld;
Daniel Vetter01a415f2012-10-27 15:58:40 +02009471 dev_priv->display.modeset_global_resources =
9472 ivb_modeset_global_resources;
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -03009473 } else if (IS_HASWELL(dev)) {
9474 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Wang Xingchao83358c852012-08-16 22:43:37 +08009475 dev_priv->display.write_eld = haswell_write_eld;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02009476 dev_priv->display.modeset_global_resources =
9477 haswell_modeset_global_resources;
Paulo Zanonia0e63c22012-12-06 11:12:39 -02009478 }
Jesse Barnes6067aae2011-04-28 15:04:31 -07009479 } else if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +08009480 dev_priv->display.write_eld = g4x_write_eld;
Jesse Barnese70236a2009-09-21 10:42:27 -07009481 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009482
9483 /* Default just returns -ENODEV to indicate unsupported */
9484 dev_priv->display.queue_flip = intel_default_queue_flip;
9485
9486 switch (INTEL_INFO(dev)->gen) {
9487 case 2:
9488 dev_priv->display.queue_flip = intel_gen2_queue_flip;
9489 break;
9490
9491 case 3:
9492 dev_priv->display.queue_flip = intel_gen3_queue_flip;
9493 break;
9494
9495 case 4:
9496 case 5:
9497 dev_priv->display.queue_flip = intel_gen4_queue_flip;
9498 break;
9499
9500 case 6:
9501 dev_priv->display.queue_flip = intel_gen6_queue_flip;
9502 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009503 case 7:
9504 dev_priv->display.queue_flip = intel_gen7_queue_flip;
9505 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009506 }
Jesse Barnese70236a2009-09-21 10:42:27 -07009507}
9508
Jesse Barnesb690e962010-07-19 13:53:12 -07009509/*
9510 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
9511 * resume, or other times. This quirk makes sure that's the case for
9512 * affected systems.
9513 */
Akshay Joshi0206e352011-08-16 15:34:10 -04009514static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -07009515{
9516 struct drm_i915_private *dev_priv = dev->dev_private;
9517
9518 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02009519 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -07009520}
9521
Keith Packard435793d2011-07-12 14:56:22 -07009522/*
9523 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
9524 */
9525static void quirk_ssc_force_disable(struct drm_device *dev)
9526{
9527 struct drm_i915_private *dev_priv = dev->dev_private;
9528 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02009529 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -07009530}
9531
Carsten Emde4dca20e2012-03-15 15:56:26 +01009532/*
Carsten Emde5a15ab52012-03-15 15:56:27 +01009533 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
9534 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +01009535 */
9536static void quirk_invert_brightness(struct drm_device *dev)
9537{
9538 struct drm_i915_private *dev_priv = dev->dev_private;
9539 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02009540 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -07009541}
9542
9543struct intel_quirk {
9544 int device;
9545 int subsystem_vendor;
9546 int subsystem_device;
9547 void (*hook)(struct drm_device *dev);
9548};
9549
Egbert Eich5f85f172012-10-14 15:46:38 +02009550/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
9551struct intel_dmi_quirk {
9552 void (*hook)(struct drm_device *dev);
9553 const struct dmi_system_id (*dmi_id_list)[];
9554};
9555
9556static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
9557{
9558 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
9559 return 1;
9560}
9561
9562static const struct intel_dmi_quirk intel_dmi_quirks[] = {
9563 {
9564 .dmi_id_list = &(const struct dmi_system_id[]) {
9565 {
9566 .callback = intel_dmi_reverse_brightness,
9567 .ident = "NCR Corporation",
9568 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
9569 DMI_MATCH(DMI_PRODUCT_NAME, ""),
9570 },
9571 },
9572 { } /* terminating entry */
9573 },
9574 .hook = quirk_invert_brightness,
9575 },
9576};
9577
Ben Widawskyc43b5632012-04-16 14:07:40 -07009578static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -07009579 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -04009580 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -07009581
Jesse Barnesb690e962010-07-19 13:53:12 -07009582 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
9583 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
9584
Jesse Barnesb690e962010-07-19 13:53:12 -07009585 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
9586 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
9587
Daniel Vetterccd0d362012-10-10 23:13:59 +02009588 /* 830/845 need to leave pipe A & dpll A up */
Jesse Barnesb690e962010-07-19 13:53:12 -07009589 { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Daniel Vetterdcdaed62012-08-12 21:19:34 +02009590 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Keith Packard435793d2011-07-12 14:56:22 -07009591
9592 /* Lenovo U160 cannot use SSC on LVDS */
9593 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +02009594
9595 /* Sony Vaio Y cannot use SSC on LVDS */
9596 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +01009597
9598 /* Acer Aspire 5734Z must invert backlight brightness */
9599 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
Jani Nikula1ffff602013-01-22 12:50:34 +02009600
9601 /* Acer/eMachines G725 */
9602 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
Jani Nikula01e3a8f2013-01-22 12:50:35 +02009603
9604 /* Acer/eMachines e725 */
9605 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
Jani Nikula5559eca2013-01-22 12:50:36 +02009606
9607 /* Acer/Packard Bell NCL20 */
9608 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
Daniel Vetterac4199e2013-02-15 18:35:30 +01009609
9610 /* Acer Aspire 4736Z */
9611 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jesse Barnesb690e962010-07-19 13:53:12 -07009612};
9613
9614static void intel_init_quirks(struct drm_device *dev)
9615{
9616 struct pci_dev *d = dev->pdev;
9617 int i;
9618
9619 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
9620 struct intel_quirk *q = &intel_quirks[i];
9621
9622 if (d->device == q->device &&
9623 (d->subsystem_vendor == q->subsystem_vendor ||
9624 q->subsystem_vendor == PCI_ANY_ID) &&
9625 (d->subsystem_device == q->subsystem_device ||
9626 q->subsystem_device == PCI_ANY_ID))
9627 q->hook(dev);
9628 }
Egbert Eich5f85f172012-10-14 15:46:38 +02009629 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
9630 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
9631 intel_dmi_quirks[i].hook(dev);
9632 }
Jesse Barnesb690e962010-07-19 13:53:12 -07009633}
9634
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009635/* Disable the VGA plane that we never use */
9636static void i915_disable_vga(struct drm_device *dev)
9637{
9638 struct drm_i915_private *dev_priv = dev->dev_private;
9639 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +02009640 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009641
9642 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -07009643 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009644 sr1 = inb(VGA_SR_DATA);
9645 outb(sr1 | 1<<5, VGA_SR_DATA);
9646 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
9647 udelay(300);
9648
9649 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9650 POSTING_READ(vga_reg);
9651}
9652
Daniel Vetterf8175862012-04-10 15:50:11 +02009653void intel_modeset_init_hw(struct drm_device *dev)
9654{
Paulo Zanonifa42e232013-01-25 16:59:11 -02009655 intel_init_power_well(dev);
Eugeni Dodonov0232e922012-07-06 15:42:36 -03009656
Eugeni Dodonova8f78b52012-06-28 15:55:35 -03009657 intel_prepare_ddi(dev);
9658
Daniel Vetterf8175862012-04-10 15:50:11 +02009659 intel_init_clock_gating(dev);
9660
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02009661 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02009662 intel_enable_gt_powersave(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02009663 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf8175862012-04-10 15:50:11 +02009664}
9665
Imre Deak7d708ee2013-04-17 14:04:50 +03009666void intel_modeset_suspend_hw(struct drm_device *dev)
9667{
9668 intel_suspend_hw(dev);
9669}
9670
Jesse Barnes79e53942008-11-07 14:24:08 -08009671void intel_modeset_init(struct drm_device *dev)
9672{
Jesse Barnes652c3932009-08-17 13:31:43 -07009673 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7f1f3852013-04-02 11:22:20 -07009674 int i, j, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08009675
9676 drm_mode_config_init(dev);
9677
9678 dev->mode_config.min_width = 0;
9679 dev->mode_config.min_height = 0;
9680
Dave Airlie019d96c2011-09-29 16:20:42 +01009681 dev->mode_config.preferred_depth = 24;
9682 dev->mode_config.prefer_shadow = 1;
9683
Laurent Pincharte6ecefa2012-05-17 13:27:23 +02009684 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -08009685
Jesse Barnesb690e962010-07-19 13:53:12 -07009686 intel_init_quirks(dev);
9687
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009688 intel_init_pm(dev);
9689
Ben Widawskye3c74752013-04-05 13:12:39 -07009690 if (INTEL_INFO(dev)->num_pipes == 0)
9691 return;
9692
Jesse Barnese70236a2009-09-21 10:42:27 -07009693 intel_init_display(dev);
9694
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009695 if (IS_GEN2(dev)) {
9696 dev->mode_config.max_width = 2048;
9697 dev->mode_config.max_height = 2048;
9698 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -07009699 dev->mode_config.max_width = 4096;
9700 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -08009701 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009702 dev->mode_config.max_width = 8192;
9703 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -08009704 }
Ben Widawsky5d4545a2013-01-17 12:45:15 -08009705 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -08009706
Zhao Yakui28c97732009-10-09 11:39:41 +08009707 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -07009708 INTEL_INFO(dev)->num_pipes,
9709 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -08009710
Damien Lespiau08e2a7d2013-07-11 20:10:54 +01009711 for_each_pipe(i) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009712 intel_crtc_init(dev, i);
Jesse Barnes7f1f3852013-04-02 11:22:20 -07009713 for (j = 0; j < dev_priv->num_plane; j++) {
9714 ret = intel_plane_init(dev, i, j);
9715 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +03009716 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
9717 pipe_name(i), sprite_name(i, j), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -07009718 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009719 }
9720
Paulo Zanoni79f689a2012-10-05 12:05:52 -03009721 intel_cpu_pll_init(dev);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02009722 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009723
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009724 /* Just disable it once at startup */
9725 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009726 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +00009727
9728 /* Just in case the BIOS is doing something questionable. */
9729 intel_disable_fbc(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01009730}
Jesse Barnesd5bb0812011-01-05 12:01:26 -08009731
Daniel Vetter24929352012-07-02 20:28:59 +02009732static void
9733intel_connector_break_all_links(struct intel_connector *connector)
9734{
9735 connector->base.dpms = DRM_MODE_DPMS_OFF;
9736 connector->base.encoder = NULL;
9737 connector->encoder->connectors_active = false;
9738 connector->encoder->base.crtc = NULL;
9739}
9740
Daniel Vetter7fad7982012-07-04 17:51:47 +02009741static void intel_enable_pipe_a(struct drm_device *dev)
9742{
9743 struct intel_connector *connector;
9744 struct drm_connector *crt = NULL;
9745 struct intel_load_detect_pipe load_detect_temp;
9746
9747 /* We can't just switch on the pipe A, we need to set things up with a
9748 * proper mode and output configuration. As a gross hack, enable pipe A
9749 * by enabling the load detect pipe once. */
9750 list_for_each_entry(connector,
9751 &dev->mode_config.connector_list,
9752 base.head) {
9753 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
9754 crt = &connector->base;
9755 break;
9756 }
9757 }
9758
9759 if (!crt)
9760 return;
9761
9762 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
9763 intel_release_load_detect_pipe(crt, &load_detect_temp);
9764
9765
9766}
9767
Daniel Vetterfa555832012-10-10 23:14:00 +02009768static bool
9769intel_check_plane_mapping(struct intel_crtc *crtc)
9770{
Ben Widawsky7eb552a2013-03-13 14:05:41 -07009771 struct drm_device *dev = crtc->base.dev;
9772 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +02009773 u32 reg, val;
9774
Ben Widawsky7eb552a2013-03-13 14:05:41 -07009775 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +02009776 return true;
9777
9778 reg = DSPCNTR(!crtc->plane);
9779 val = I915_READ(reg);
9780
9781 if ((val & DISPLAY_PLANE_ENABLE) &&
9782 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
9783 return false;
9784
9785 return true;
9786}
9787
Daniel Vetter24929352012-07-02 20:28:59 +02009788static void intel_sanitize_crtc(struct intel_crtc *crtc)
9789{
9790 struct drm_device *dev = crtc->base.dev;
9791 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +02009792 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +02009793
Daniel Vetter24929352012-07-02 20:28:59 +02009794 /* Clear any frame start delays used for debugging left by the BIOS */
Daniel Vetter3b117c82013-04-17 20:15:07 +02009795 reg = PIPECONF(crtc->config.cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +02009796 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
9797
9798 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +02009799 * disable the crtc (and hence change the state) if it is wrong. Note
9800 * that gen4+ has a fixed plane -> pipe mapping. */
9801 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +02009802 struct intel_connector *connector;
9803 bool plane;
9804
Daniel Vetter24929352012-07-02 20:28:59 +02009805 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
9806 crtc->base.base.id);
9807
9808 /* Pipe has the wrong plane attached and the plane is active.
9809 * Temporarily change the plane mapping and disable everything
9810 * ... */
9811 plane = crtc->plane;
9812 crtc->plane = !plane;
9813 dev_priv->display.crtc_disable(&crtc->base);
9814 crtc->plane = plane;
9815
9816 /* ... and break all links. */
9817 list_for_each_entry(connector, &dev->mode_config.connector_list,
9818 base.head) {
9819 if (connector->encoder->base.crtc != &crtc->base)
9820 continue;
9821
9822 intel_connector_break_all_links(connector);
9823 }
9824
9825 WARN_ON(crtc->active);
9826 crtc->base.enabled = false;
9827 }
Daniel Vetter24929352012-07-02 20:28:59 +02009828
Daniel Vetter7fad7982012-07-04 17:51:47 +02009829 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
9830 crtc->pipe == PIPE_A && !crtc->active) {
9831 /* BIOS forgot to enable pipe A, this mostly happens after
9832 * resume. Force-enable the pipe to fix this, the update_dpms
9833 * call below we restore the pipe to the right state, but leave
9834 * the required bits on. */
9835 intel_enable_pipe_a(dev);
9836 }
9837
Daniel Vetter24929352012-07-02 20:28:59 +02009838 /* Adjust the state of the output pipe according to whether we
9839 * have active connectors/encoders. */
9840 intel_crtc_update_dpms(&crtc->base);
9841
9842 if (crtc->active != crtc->base.enabled) {
9843 struct intel_encoder *encoder;
9844
9845 /* This can happen either due to bugs in the get_hw_state
9846 * functions or because the pipe is force-enabled due to the
9847 * pipe A quirk. */
9848 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
9849 crtc->base.base.id,
9850 crtc->base.enabled ? "enabled" : "disabled",
9851 crtc->active ? "enabled" : "disabled");
9852
9853 crtc->base.enabled = crtc->active;
9854
9855 /* Because we only establish the connector -> encoder ->
9856 * crtc links if something is active, this means the
9857 * crtc is now deactivated. Break the links. connector
9858 * -> encoder links are only establish when things are
9859 * actually up, hence no need to break them. */
9860 WARN_ON(crtc->active);
9861
9862 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
9863 WARN_ON(encoder->connectors_active);
9864 encoder->base.crtc = NULL;
9865 }
9866 }
9867}
9868
9869static void intel_sanitize_encoder(struct intel_encoder *encoder)
9870{
9871 struct intel_connector *connector;
9872 struct drm_device *dev = encoder->base.dev;
9873
9874 /* We need to check both for a crtc link (meaning that the
9875 * encoder is active and trying to read from a pipe) and the
9876 * pipe itself being active. */
9877 bool has_active_crtc = encoder->base.crtc &&
9878 to_intel_crtc(encoder->base.crtc)->active;
9879
9880 if (encoder->connectors_active && !has_active_crtc) {
9881 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
9882 encoder->base.base.id,
9883 drm_get_encoder_name(&encoder->base));
9884
9885 /* Connector is active, but has no active pipe. This is
9886 * fallout from our resume register restoring. Disable
9887 * the encoder manually again. */
9888 if (encoder->base.crtc) {
9889 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
9890 encoder->base.base.id,
9891 drm_get_encoder_name(&encoder->base));
9892 encoder->disable(encoder);
9893 }
9894
9895 /* Inconsistent output/port/pipe state happens presumably due to
9896 * a bug in one of the get_hw_state functions. Or someplace else
9897 * in our code, like the register restore mess on resume. Clamp
9898 * things to off as a safer default. */
9899 list_for_each_entry(connector,
9900 &dev->mode_config.connector_list,
9901 base.head) {
9902 if (connector->encoder != encoder)
9903 continue;
9904
9905 intel_connector_break_all_links(connector);
9906 }
9907 }
9908 /* Enabled encoders without active connectors will be fixed in
9909 * the crtc fixup. */
9910}
9911
Daniel Vetter44cec742013-01-25 17:53:21 +01009912void i915_redisable_vga(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01009913{
9914 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +02009915 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01009916
9917 if (I915_READ(vga_reg) != VGA_DISP_DISABLE) {
9918 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
Ville Syrjälä209d5212013-01-25 21:44:48 +02009919 i915_disable_vga(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01009920 }
9921}
9922
Daniel Vetter30e984d2013-06-05 13:34:17 +02009923static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +02009924{
9925 struct drm_i915_private *dev_priv = dev->dev_private;
9926 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +02009927 struct intel_crtc *crtc;
9928 struct intel_encoder *encoder;
9929 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +02009930 int i;
Daniel Vetter24929352012-07-02 20:28:59 +02009931
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009932 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9933 base.head) {
Daniel Vetter88adfff2013-03-28 10:42:01 +01009934 memset(&crtc->config, 0, sizeof(crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +02009935
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009936 crtc->active = dev_priv->display.get_pipe_config(crtc,
9937 &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +02009938
9939 crtc->base.enabled = crtc->active;
9940
9941 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
9942 crtc->base.base.id,
9943 crtc->active ? "enabled" : "disabled");
9944 }
9945
Daniel Vetter53589012013-06-05 13:34:16 +02009946 /* FIXME: Smash this into the new shared dpll infrastructure. */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009947 if (HAS_DDI(dev))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009948 intel_ddi_setup_hw_pll_state(dev);
9949
Daniel Vetter53589012013-06-05 13:34:16 +02009950 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
9951 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
9952
9953 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
9954 pll->active = 0;
9955 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9956 base.head) {
9957 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
9958 pll->active++;
9959 }
9960 pll->refcount = pll->active;
9961
9962 DRM_DEBUG_KMS("%s hw state readout: refcount %i\n",
9963 pll->name, pll->refcount);
9964 }
9965
Daniel Vetter24929352012-07-02 20:28:59 +02009966 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9967 base.head) {
9968 pipe = 0;
9969
9970 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -07009971 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
9972 encoder->base.crtc = &crtc->base;
Jesse Barnes510d5f22013-07-01 15:50:17 -07009973 if (encoder->get_config)
Jesse Barnes045ac3b2013-05-14 17:08:26 -07009974 encoder->get_config(encoder, &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +02009975 } else {
9976 encoder->base.crtc = NULL;
9977 }
9978
9979 encoder->connectors_active = false;
9980 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe=%i\n",
9981 encoder->base.base.id,
9982 drm_get_encoder_name(&encoder->base),
9983 encoder->base.crtc ? "enabled" : "disabled",
9984 pipe);
9985 }
9986
Jesse Barnes510d5f22013-07-01 15:50:17 -07009987 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9988 base.head) {
9989 if (!crtc->active)
9990 continue;
9991 if (dev_priv->display.get_clock)
9992 dev_priv->display.get_clock(crtc,
9993 &crtc->config);
9994 }
9995
Daniel Vetter24929352012-07-02 20:28:59 +02009996 list_for_each_entry(connector, &dev->mode_config.connector_list,
9997 base.head) {
9998 if (connector->get_hw_state(connector)) {
9999 connector->base.dpms = DRM_MODE_DPMS_ON;
10000 connector->encoder->connectors_active = true;
10001 connector->base.encoder = &connector->encoder->base;
10002 } else {
10003 connector->base.dpms = DRM_MODE_DPMS_OFF;
10004 connector->base.encoder = NULL;
10005 }
10006 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
10007 connector->base.base.id,
10008 drm_get_connector_name(&connector->base),
10009 connector->base.encoder ? "enabled" : "disabled");
10010 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020010011}
10012
10013/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
10014 * and i915 state tracking structures. */
10015void intel_modeset_setup_hw_state(struct drm_device *dev,
10016 bool force_restore)
10017{
10018 struct drm_i915_private *dev_priv = dev->dev_private;
10019 enum pipe pipe;
10020 struct drm_plane *plane;
10021 struct intel_crtc *crtc;
10022 struct intel_encoder *encoder;
10023
10024 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020010025
Jesse Barnesbabea612013-06-26 18:57:38 +030010026 /*
10027 * Now that we have the config, copy it to each CRTC struct
10028 * Note that this could go away if we move to using crtc_config
10029 * checking everywhere.
10030 */
10031 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
10032 base.head) {
10033 if (crtc->active && i915_fastboot) {
10034 intel_crtc_mode_from_pipe_config(crtc, &crtc->config);
10035
10036 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
10037 crtc->base.base.id);
10038 drm_mode_debug_printmodeline(&crtc->base.mode);
10039 }
10040 }
10041
Daniel Vetter24929352012-07-02 20:28:59 +020010042 /* HW state is read out, now we need to sanitize this mess. */
10043 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10044 base.head) {
10045 intel_sanitize_encoder(encoder);
10046 }
10047
10048 for_each_pipe(pipe) {
10049 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
10050 intel_sanitize_crtc(crtc);
Daniel Vetterc0b03412013-05-28 12:05:54 +020010051 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020010052 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010053
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010054 if (force_restore) {
Daniel Vetterf30da182013-04-11 20:22:50 +020010055 /*
10056 * We need to use raw interfaces for restoring state to avoid
10057 * checking (bogus) intermediate states.
10058 */
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010059 for_each_pipe(pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070010060 struct drm_crtc *crtc =
10061 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020010062
10063 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
10064 crtc->fb);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010065 }
Jesse Barnesb5644d02013-03-26 13:25:27 -070010066 list_for_each_entry(plane, &dev->mode_config.plane_list, head)
10067 intel_plane_restore(plane);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010010068
10069 i915_redisable_vga(dev);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010070 } else {
10071 intel_modeset_update_staged_output_state(dev);
10072 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010073
10074 intel_modeset_check_state(dev);
Daniel Vetter2e938892012-10-11 20:08:24 +020010075
10076 drm_mode_config_reset(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010010077}
10078
10079void intel_modeset_gem_init(struct drm_device *dev)
10080{
Chris Wilson1833b132012-05-09 11:56:28 +010010081 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020010082
10083 intel_setup_overlay(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020010084
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010085 intel_modeset_setup_hw_state(dev, false);
Jesse Barnes79e53942008-11-07 14:24:08 -080010086}
10087
10088void intel_modeset_cleanup(struct drm_device *dev)
10089{
Jesse Barnes652c3932009-08-17 13:31:43 -070010090 struct drm_i915_private *dev_priv = dev->dev_private;
10091 struct drm_crtc *crtc;
10092 struct intel_crtc *intel_crtc;
10093
Daniel Vetterfd0c0642013-04-24 11:13:35 +020010094 /*
10095 * Interrupts and polling as the first thing to avoid creating havoc.
10096 * Too much stuff here (turning of rps, connectors, ...) would
10097 * experience fancy races otherwise.
10098 */
10099 drm_irq_uninstall(dev);
10100 cancel_work_sync(&dev_priv->hotplug_work);
10101 /*
10102 * Due to the hpd irq storm handling the hotplug work can re-arm the
10103 * poll handlers. Hence disable polling after hpd handling is shut down.
10104 */
Keith Packardf87ea762010-10-03 19:36:26 -070010105 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020010106
Jesse Barnes652c3932009-08-17 13:31:43 -070010107 mutex_lock(&dev->struct_mutex);
10108
Jesse Barnes723bfd72010-10-07 16:01:13 -070010109 intel_unregister_dsm_handler();
10110
Jesse Barnes652c3932009-08-17 13:31:43 -070010111 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
10112 /* Skip inactive CRTCs */
10113 if (!crtc->fb)
10114 continue;
10115
10116 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3dec0092010-08-20 21:40:52 +020010117 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070010118 }
10119
Chris Wilson973d04f2011-07-08 12:22:37 +010010120 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070010121
Daniel Vetter8090c6b2012-06-24 16:42:32 +020010122 intel_disable_gt_powersave(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +000010123
Daniel Vetter930ebb42012-06-29 23:32:16 +020010124 ironlake_teardown_rc6(dev);
10125
Kristian Høgsberg69341a52009-11-11 12:19:17 -050010126 mutex_unlock(&dev->struct_mutex);
10127
Chris Wilson1630fe72011-07-08 12:22:42 +010010128 /* flush any delayed tasks or pending work */
10129 flush_scheduled_work();
10130
Jani Nikuladc652f92013-04-12 15:18:38 +030010131 /* destroy backlight, if any, before the connectors */
10132 intel_panel_destroy_backlight(dev);
10133
Jesse Barnes79e53942008-11-07 14:24:08 -080010134 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010010135
10136 intel_cleanup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010137}
10138
Dave Airlie28d52042009-09-21 14:33:58 +100010139/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080010140 * Return which encoder is currently attached for connector.
10141 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010010142struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080010143{
Chris Wilsondf0e9242010-09-09 16:20:55 +010010144 return &intel_attached_encoder(connector)->base;
10145}
Jesse Barnes79e53942008-11-07 14:24:08 -080010146
Chris Wilsondf0e9242010-09-09 16:20:55 +010010147void intel_connector_attach_encoder(struct intel_connector *connector,
10148 struct intel_encoder *encoder)
10149{
10150 connector->encoder = encoder;
10151 drm_mode_connector_attach_encoder(&connector->base,
10152 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080010153}
Dave Airlie28d52042009-09-21 14:33:58 +100010154
10155/*
10156 * set vga decode state - true == enable VGA decode
10157 */
10158int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
10159{
10160 struct drm_i915_private *dev_priv = dev->dev_private;
10161 u16 gmch_ctrl;
10162
10163 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
10164 if (state)
10165 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
10166 else
10167 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
10168 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
10169 return 0;
10170}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010171
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010172struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010173
10174 u32 power_well_driver;
10175
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010176 struct intel_cursor_error_state {
10177 u32 control;
10178 u32 position;
10179 u32 base;
10180 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010010181 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010182
10183 struct intel_pipe_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010184 enum transcoder cpu_transcoder;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010185 u32 conf;
10186 u32 source;
10187
10188 u32 htotal;
10189 u32 hblank;
10190 u32 hsync;
10191 u32 vtotal;
10192 u32 vblank;
10193 u32 vsync;
Damien Lespiau52331302012-08-15 19:23:25 +010010194 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010195
10196 struct intel_plane_error_state {
10197 u32 control;
10198 u32 stride;
10199 u32 size;
10200 u32 pos;
10201 u32 addr;
10202 u32 surface;
10203 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010010204 } plane[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010205};
10206
10207struct intel_display_error_state *
10208intel_display_capture_error_state(struct drm_device *dev)
10209{
Akshay Joshi0206e352011-08-16 15:34:10 -040010210 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010211 struct intel_display_error_state *error;
Paulo Zanoni702e7a52012-10-23 18:29:59 -020010212 enum transcoder cpu_transcoder;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010213 int i;
10214
10215 error = kmalloc(sizeof(*error), GFP_ATOMIC);
10216 if (error == NULL)
10217 return NULL;
10218
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010219 if (HAS_POWER_WELL(dev))
10220 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
10221
Damien Lespiau52331302012-08-15 19:23:25 +010010222 for_each_pipe(i) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -020010223 cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv, i);
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010224 error->pipe[i].cpu_transcoder = cpu_transcoder;
Paulo Zanoni702e7a52012-10-23 18:29:59 -020010225
Paulo Zanonia18c4c32013-03-06 20:03:12 -030010226 if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev)) {
10227 error->cursor[i].control = I915_READ(CURCNTR(i));
10228 error->cursor[i].position = I915_READ(CURPOS(i));
10229 error->cursor[i].base = I915_READ(CURBASE(i));
10230 } else {
10231 error->cursor[i].control = I915_READ(CURCNTR_IVB(i));
10232 error->cursor[i].position = I915_READ(CURPOS_IVB(i));
10233 error->cursor[i].base = I915_READ(CURBASE_IVB(i));
10234 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010235
10236 error->plane[i].control = I915_READ(DSPCNTR(i));
10237 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030010238 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030010239 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030010240 error->plane[i].pos = I915_READ(DSPPOS(i));
10241 }
Paulo Zanonica291362013-03-06 20:03:14 -030010242 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
10243 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010244 if (INTEL_INFO(dev)->gen >= 4) {
10245 error->plane[i].surface = I915_READ(DSPSURF(i));
10246 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
10247 }
10248
Paulo Zanoni702e7a52012-10-23 18:29:59 -020010249 error->pipe[i].conf = I915_READ(PIPECONF(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010250 error->pipe[i].source = I915_READ(PIPESRC(i));
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010251 error->pipe[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
10252 error->pipe[i].hblank = I915_READ(HBLANK(cpu_transcoder));
10253 error->pipe[i].hsync = I915_READ(HSYNC(cpu_transcoder));
10254 error->pipe[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
10255 error->pipe[i].vblank = I915_READ(VBLANK(cpu_transcoder));
10256 error->pipe[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010257 }
10258
Paulo Zanoni12d217c2013-05-03 12:15:38 -030010259 /* In the code above we read the registers without checking if the power
10260 * well was on, so here we have to clear the FPGA_DBG_RM_NOCLAIM bit to
10261 * prevent the next I915_WRITE from detecting it and printing an error
10262 * message. */
10263 if (HAS_POWER_WELL(dev))
10264 I915_WRITE_NOTRACE(FPGA_DBG, FPGA_DBG_RM_NOCLAIM);
10265
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010266 return error;
10267}
10268
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010269#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
10270
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010271void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010272intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010273 struct drm_device *dev,
10274 struct intel_display_error_state *error)
10275{
10276 int i;
10277
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010278 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010279 if (HAS_POWER_WELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010280 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010281 error->power_well_driver);
Damien Lespiau52331302012-08-15 19:23:25 +010010282 for_each_pipe(i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010283 err_printf(m, "Pipe [%d]:\n", i);
10284 err_printf(m, " CPU transcoder: %c\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010285 transcoder_name(error->pipe[i].cpu_transcoder));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010286 err_printf(m, " CONF: %08x\n", error->pipe[i].conf);
10287 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
10288 err_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
10289 err_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
10290 err_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
10291 err_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
10292 err_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
10293 err_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010294
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010295 err_printf(m, "Plane [%d]:\n", i);
10296 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
10297 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030010298 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010299 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
10300 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030010301 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030010302 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010303 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010304 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010305 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
10306 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010307 }
10308
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010309 err_printf(m, "Cursor [%d]:\n", i);
10310 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
10311 err_printf(m, " POS: %08x\n", error->cursor[i].position);
10312 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010313 }
10314}