blob: bca81eecd31eee8d14a0c3fcae81faffa7a307c7 [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
Daniel Vetter618563e2012-04-01 13:38:50 +020027#include <linux/dmi.h>
Jesse Barnesc1c7af62009-09-10 15:28:03 -070028#include <linux/module.h>
29#include <linux/input.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080030#include <linux/i2c.h>
Shaohua Li7662c8b2009-06-26 11:23:55 +080031#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Jesse Barnes9cce37f2010-08-13 15:11:26 -070033#include <linux/vgaarb.h>
Wu Fengguange0dac652011-09-05 14:25:34 +080034#include <drm/drm_edid.h>
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drmP.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include "intel_drv.h"
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/i915_drm.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080038#include "i915_drv.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070039#include "i915_trace.h"
David Howells760285e2012-10-02 18:01:07 +010040#include <drm/drm_dp_helper.h>
41#include <drm/drm_crtc_helper.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080042#include <linux/dma_remapping.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080043
Akshay Joshi0206e352011-08-16 15:34:10 -040044bool intel_pipe_has_type(struct drm_crtc *crtc, int type);
Daniel Vetter3dec0092010-08-20 21:40:52 +020045static void intel_increase_pllclock(struct drm_crtc *crtc);
Chris Wilson6b383a72010-09-13 13:54:26 +010046static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080047
Jesse Barnesf1f644d2013-06-27 00:39:25 +030048static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
49 struct intel_crtc_config *pipe_config);
50static void ironlake_crtc_clock_get(struct intel_crtc *crtc,
51 struct intel_crtc_config *pipe_config);
52
Jesse Barnes79e53942008-11-07 14:24:08 -080053typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040054 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -080055} intel_range_t;
56
57typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040058 int dot_limit;
59 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -080060} intel_p2_t;
61
62#define INTEL_P2_NUM 2
Ma Lingd4906092009-03-18 20:13:27 +080063typedef struct intel_limit intel_limit_t;
64struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -040065 intel_range_t dot, vco, n, m, m1, m2, p, p1;
66 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +080067};
Jesse Barnes79e53942008-11-07 14:24:08 -080068
Jesse Barnes2377b742010-07-07 14:06:43 -070069/* FDI */
70#define IRONLAKE_FDI_FREQ 2700000 /* in kHz for mode->clock */
71
Daniel Vetterd2acd212012-10-20 20:57:43 +020072int
73intel_pch_rawclk(struct drm_device *dev)
74{
75 struct drm_i915_private *dev_priv = dev->dev_private;
76
77 WARN_ON(!HAS_PCH_SPLIT(dev));
78
79 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
80}
81
Chris Wilson021357a2010-09-07 20:54:59 +010082static inline u32 /* units of 100MHz */
83intel_fdi_link_freq(struct drm_device *dev)
84{
Chris Wilson8b99e682010-10-13 09:59:17 +010085 if (IS_GEN5(dev)) {
86 struct drm_i915_private *dev_priv = dev->dev_private;
87 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
88 } else
89 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +010090}
91
Daniel Vetter5d536e22013-07-06 12:52:06 +020092static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -040093 .dot = { .min = 25000, .max = 350000 },
94 .vco = { .min = 930000, .max = 1400000 },
95 .n = { .min = 3, .max = 16 },
96 .m = { .min = 96, .max = 140 },
97 .m1 = { .min = 18, .max = 26 },
98 .m2 = { .min = 6, .max = 16 },
99 .p = { .min = 4, .max = 128 },
100 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700101 .p2 = { .dot_limit = 165000,
102 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700103};
104
Daniel Vetter5d536e22013-07-06 12:52:06 +0200105static const intel_limit_t intel_limits_i8xx_dvo = {
106 .dot = { .min = 25000, .max = 350000 },
107 .vco = { .min = 930000, .max = 1400000 },
108 .n = { .min = 3, .max = 16 },
109 .m = { .min = 96, .max = 140 },
110 .m1 = { .min = 18, .max = 26 },
111 .m2 = { .min = 6, .max = 16 },
112 .p = { .min = 4, .max = 128 },
113 .p1 = { .min = 2, .max = 33 },
114 .p2 = { .dot_limit = 165000,
115 .p2_slow = 4, .p2_fast = 4 },
116};
117
Keith Packarde4b36692009-06-05 19:22:17 -0700118static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400119 .dot = { .min = 25000, .max = 350000 },
120 .vco = { .min = 930000, .max = 1400000 },
121 .n = { .min = 3, .max = 16 },
122 .m = { .min = 96, .max = 140 },
123 .m1 = { .min = 18, .max = 26 },
124 .m2 = { .min = 6, .max = 16 },
125 .p = { .min = 4, .max = 128 },
126 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700127 .p2 = { .dot_limit = 165000,
128 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700129};
Eric Anholt273e27c2011-03-30 13:01:10 -0700130
Keith Packarde4b36692009-06-05 19:22:17 -0700131static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400132 .dot = { .min = 20000, .max = 400000 },
133 .vco = { .min = 1400000, .max = 2800000 },
134 .n = { .min = 1, .max = 6 },
135 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100136 .m1 = { .min = 8, .max = 18 },
137 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400138 .p = { .min = 5, .max = 80 },
139 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700140 .p2 = { .dot_limit = 200000,
141 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700142};
143
144static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400145 .dot = { .min = 20000, .max = 400000 },
146 .vco = { .min = 1400000, .max = 2800000 },
147 .n = { .min = 1, .max = 6 },
148 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100149 .m1 = { .min = 8, .max = 18 },
150 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400151 .p = { .min = 7, .max = 98 },
152 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700153 .p2 = { .dot_limit = 112000,
154 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700155};
156
Eric Anholt273e27c2011-03-30 13:01:10 -0700157
Keith Packarde4b36692009-06-05 19:22:17 -0700158static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700159 .dot = { .min = 25000, .max = 270000 },
160 .vco = { .min = 1750000, .max = 3500000},
161 .n = { .min = 1, .max = 4 },
162 .m = { .min = 104, .max = 138 },
163 .m1 = { .min = 17, .max = 23 },
164 .m2 = { .min = 5, .max = 11 },
165 .p = { .min = 10, .max = 30 },
166 .p1 = { .min = 1, .max = 3},
167 .p2 = { .dot_limit = 270000,
168 .p2_slow = 10,
169 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800170 },
Keith Packarde4b36692009-06-05 19:22:17 -0700171};
172
173static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700174 .dot = { .min = 22000, .max = 400000 },
175 .vco = { .min = 1750000, .max = 3500000},
176 .n = { .min = 1, .max = 4 },
177 .m = { .min = 104, .max = 138 },
178 .m1 = { .min = 16, .max = 23 },
179 .m2 = { .min = 5, .max = 11 },
180 .p = { .min = 5, .max = 80 },
181 .p1 = { .min = 1, .max = 8},
182 .p2 = { .dot_limit = 165000,
183 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700184};
185
186static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700187 .dot = { .min = 20000, .max = 115000 },
188 .vco = { .min = 1750000, .max = 3500000 },
189 .n = { .min = 1, .max = 3 },
190 .m = { .min = 104, .max = 138 },
191 .m1 = { .min = 17, .max = 23 },
192 .m2 = { .min = 5, .max = 11 },
193 .p = { .min = 28, .max = 112 },
194 .p1 = { .min = 2, .max = 8 },
195 .p2 = { .dot_limit = 0,
196 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800197 },
Keith Packarde4b36692009-06-05 19:22:17 -0700198};
199
200static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700201 .dot = { .min = 80000, .max = 224000 },
202 .vco = { .min = 1750000, .max = 3500000 },
203 .n = { .min = 1, .max = 3 },
204 .m = { .min = 104, .max = 138 },
205 .m1 = { .min = 17, .max = 23 },
206 .m2 = { .min = 5, .max = 11 },
207 .p = { .min = 14, .max = 42 },
208 .p1 = { .min = 2, .max = 6 },
209 .p2 = { .dot_limit = 0,
210 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800211 },
Keith Packarde4b36692009-06-05 19:22:17 -0700212};
213
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500214static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400215 .dot = { .min = 20000, .max = 400000},
216 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700217 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400218 .n = { .min = 3, .max = 6 },
219 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700220 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400221 .m1 = { .min = 0, .max = 0 },
222 .m2 = { .min = 0, .max = 254 },
223 .p = { .min = 5, .max = 80 },
224 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700225 .p2 = { .dot_limit = 200000,
226 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700227};
228
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500229static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400230 .dot = { .min = 20000, .max = 400000 },
231 .vco = { .min = 1700000, .max = 3500000 },
232 .n = { .min = 3, .max = 6 },
233 .m = { .min = 2, .max = 256 },
234 .m1 = { .min = 0, .max = 0 },
235 .m2 = { .min = 0, .max = 254 },
236 .p = { .min = 7, .max = 112 },
237 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700238 .p2 = { .dot_limit = 112000,
239 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700240};
241
Eric Anholt273e27c2011-03-30 13:01:10 -0700242/* Ironlake / Sandybridge
243 *
244 * We calculate clock using (register_value + 2) for N/M1/M2, so here
245 * the range value for them is (actual_value - 2).
246 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800247static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700248 .dot = { .min = 25000, .max = 350000 },
249 .vco = { .min = 1760000, .max = 3510000 },
250 .n = { .min = 1, .max = 5 },
251 .m = { .min = 79, .max = 127 },
252 .m1 = { .min = 12, .max = 22 },
253 .m2 = { .min = 5, .max = 9 },
254 .p = { .min = 5, .max = 80 },
255 .p1 = { .min = 1, .max = 8 },
256 .p2 = { .dot_limit = 225000,
257 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700258};
259
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800260static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700261 .dot = { .min = 25000, .max = 350000 },
262 .vco = { .min = 1760000, .max = 3510000 },
263 .n = { .min = 1, .max = 3 },
264 .m = { .min = 79, .max = 118 },
265 .m1 = { .min = 12, .max = 22 },
266 .m2 = { .min = 5, .max = 9 },
267 .p = { .min = 28, .max = 112 },
268 .p1 = { .min = 2, .max = 8 },
269 .p2 = { .dot_limit = 225000,
270 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800271};
272
273static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700274 .dot = { .min = 25000, .max = 350000 },
275 .vco = { .min = 1760000, .max = 3510000 },
276 .n = { .min = 1, .max = 3 },
277 .m = { .min = 79, .max = 127 },
278 .m1 = { .min = 12, .max = 22 },
279 .m2 = { .min = 5, .max = 9 },
280 .p = { .min = 14, .max = 56 },
281 .p1 = { .min = 2, .max = 8 },
282 .p2 = { .dot_limit = 225000,
283 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800284};
285
Eric Anholt273e27c2011-03-30 13:01:10 -0700286/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800287static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700288 .dot = { .min = 25000, .max = 350000 },
289 .vco = { .min = 1760000, .max = 3510000 },
290 .n = { .min = 1, .max = 2 },
291 .m = { .min = 79, .max = 126 },
292 .m1 = { .min = 12, .max = 22 },
293 .m2 = { .min = 5, .max = 9 },
294 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400295 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700296 .p2 = { .dot_limit = 225000,
297 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800298};
299
300static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700301 .dot = { .min = 25000, .max = 350000 },
302 .vco = { .min = 1760000, .max = 3510000 },
303 .n = { .min = 1, .max = 3 },
304 .m = { .min = 79, .max = 126 },
305 .m1 = { .min = 12, .max = 22 },
306 .m2 = { .min = 5, .max = 9 },
307 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400308 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700309 .p2 = { .dot_limit = 225000,
310 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800311};
312
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700313static const intel_limit_t intel_limits_vlv_dac = {
314 .dot = { .min = 25000, .max = 270000 },
315 .vco = { .min = 4000000, .max = 6000000 },
316 .n = { .min = 1, .max = 7 },
317 .m = { .min = 22, .max = 450 }, /* guess */
318 .m1 = { .min = 2, .max = 3 },
319 .m2 = { .min = 11, .max = 156 },
320 .p = { .min = 10, .max = 30 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200321 .p1 = { .min = 1, .max = 3 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700322 .p2 = { .dot_limit = 270000,
323 .p2_slow = 2, .p2_fast = 20 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700324};
325
326static const intel_limit_t intel_limits_vlv_hdmi = {
Daniel Vetter75e53982013-04-18 21:10:43 +0200327 .dot = { .min = 25000, .max = 270000 },
328 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700329 .n = { .min = 1, .max = 7 },
330 .m = { .min = 60, .max = 300 }, /* guess */
331 .m1 = { .min = 2, .max = 3 },
332 .m2 = { .min = 11, .max = 156 },
333 .p = { .min = 10, .max = 30 },
334 .p1 = { .min = 2, .max = 3 },
335 .p2 = { .dot_limit = 270000,
336 .p2_slow = 2, .p2_fast = 20 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700337};
338
339static const intel_limit_t intel_limits_vlv_dp = {
Vijay Purushothaman74a4dd22012-09-27 19:13:04 +0530340 .dot = { .min = 25000, .max = 270000 },
341 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700342 .n = { .min = 1, .max = 7 },
Vijay Purushothaman74a4dd22012-09-27 19:13:04 +0530343 .m = { .min = 22, .max = 450 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700344 .m1 = { .min = 2, .max = 3 },
345 .m2 = { .min = 11, .max = 156 },
346 .p = { .min = 10, .max = 30 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200347 .p1 = { .min = 1, .max = 3 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700348 .p2 = { .dot_limit = 270000,
349 .p2_slow = 2, .p2_fast = 20 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700350};
351
Chris Wilson1b894b52010-12-14 20:04:54 +0000352static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
353 int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800354{
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800355 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800356 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800357
358 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100359 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000360 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800361 limit = &intel_limits_ironlake_dual_lvds_100m;
362 else
363 limit = &intel_limits_ironlake_dual_lvds;
364 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000365 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800366 limit = &intel_limits_ironlake_single_lvds_100m;
367 else
368 limit = &intel_limits_ironlake_single_lvds;
369 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200370 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800371 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800372
373 return limit;
374}
375
Ma Ling044c7c42009-03-18 20:13:23 +0800376static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
377{
378 struct drm_device *dev = crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800379 const intel_limit_t *limit;
380
381 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100382 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700383 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800384 else
Keith Packarde4b36692009-06-05 19:22:17 -0700385 limit = &intel_limits_g4x_single_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800386 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
387 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700388 limit = &intel_limits_g4x_hdmi;
Ma Ling044c7c42009-03-18 20:13:23 +0800389 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700390 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800391 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700392 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800393
394 return limit;
395}
396
Chris Wilson1b894b52010-12-14 20:04:54 +0000397static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800398{
399 struct drm_device *dev = crtc->dev;
400 const intel_limit_t *limit;
401
Eric Anholtbad720f2009-10-22 16:11:14 -0700402 if (HAS_PCH_SPLIT(dev))
Chris Wilson1b894b52010-12-14 20:04:54 +0000403 limit = intel_ironlake_limit(crtc, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800404 else if (IS_G4X(dev)) {
Ma Ling044c7c42009-03-18 20:13:23 +0800405 limit = intel_g4x_limit(crtc);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500406 } else if (IS_PINEVIEW(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +0800407 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500408 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800409 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500410 limit = &intel_limits_pineview_sdvo;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700411 } else if (IS_VALLEYVIEW(dev)) {
412 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG))
413 limit = &intel_limits_vlv_dac;
414 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
415 limit = &intel_limits_vlv_hdmi;
416 else
417 limit = &intel_limits_vlv_dp;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100418 } else if (!IS_GEN2(dev)) {
419 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
420 limit = &intel_limits_i9xx_lvds;
421 else
422 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800423 } else {
424 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700425 limit = &intel_limits_i8xx_lvds;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200426 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700427 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200428 else
429 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800430 }
431 return limit;
432}
433
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500434/* m1 is reserved as 0 in Pineview, n is a ring counter */
435static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800436{
Shaohua Li21778322009-02-23 15:19:16 +0800437 clock->m = clock->m2 + 2;
438 clock->p = clock->p1 * clock->p2;
439 clock->vco = refclk * clock->m / clock->n;
440 clock->dot = clock->vco / clock->p;
441}
442
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200443static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
444{
445 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
446}
447
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200448static void i9xx_clock(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800449{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200450 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800451 clock->p = clock->p1 * clock->p2;
452 clock->vco = refclk * clock->m / (clock->n + 2);
453 clock->dot = clock->vco / clock->p;
454}
455
Jesse Barnes79e53942008-11-07 14:24:08 -0800456/**
457 * Returns whether any output on the specified pipe is of the specified type
458 */
Chris Wilson4ef69c72010-09-09 15:14:28 +0100459bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
Jesse Barnes79e53942008-11-07 14:24:08 -0800460{
Chris Wilson4ef69c72010-09-09 15:14:28 +0100461 struct drm_device *dev = crtc->dev;
Chris Wilson4ef69c72010-09-09 15:14:28 +0100462 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -0800463
Daniel Vetter6c2b7c122012-07-05 09:50:24 +0200464 for_each_encoder_on_crtc(dev, crtc, encoder)
465 if (encoder->type == type)
Chris Wilson4ef69c72010-09-09 15:14:28 +0100466 return true;
467
468 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -0800469}
470
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800471#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800472/**
473 * Returns whether the given set of divisors are valid for a given refclk with
474 * the given connectors.
475 */
476
Chris Wilson1b894b52010-12-14 20:04:54 +0000477static bool intel_PLL_is_valid(struct drm_device *dev,
478 const intel_limit_t *limit,
479 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800480{
Jesse Barnes79e53942008-11-07 14:24:08 -0800481 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400482 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800483 if (clock->p < limit->p.min || limit->p.max < clock->p)
Akshay Joshi0206e352011-08-16 15:34:10 -0400484 INTELPllInvalid("p out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800485 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400486 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800487 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400488 INTELPllInvalid("m1 out of range\n");
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500489 if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
Akshay Joshi0206e352011-08-16 15:34:10 -0400490 INTELPllInvalid("m1 <= m2\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800491 if (clock->m < limit->m.min || limit->m.max < clock->m)
Akshay Joshi0206e352011-08-16 15:34:10 -0400492 INTELPllInvalid("m out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800493 if (clock->n < limit->n.min || limit->n.max < clock->n)
Akshay Joshi0206e352011-08-16 15:34:10 -0400494 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800495 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400496 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800497 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
498 * connector, etc., rather than just a single range.
499 */
500 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400501 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800502
503 return true;
504}
505
Ma Lingd4906092009-03-18 20:13:27 +0800506static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200507i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800508 int target, int refclk, intel_clock_t *match_clock,
509 intel_clock_t *best_clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800510{
511 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800512 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800513 int err = target;
514
Daniel Vettera210b022012-11-26 17:22:08 +0100515 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800516 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100517 * For LVDS just rely on its current settings for dual-channel.
518 * We haven't figured out how to reliably set up different
519 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800520 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100521 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800522 clock.p2 = limit->p2.p2_fast;
523 else
524 clock.p2 = limit->p2.p2_slow;
525 } else {
526 if (target < limit->p2.dot_limit)
527 clock.p2 = limit->p2.p2_slow;
528 else
529 clock.p2 = limit->p2.p2_fast;
530 }
531
Akshay Joshi0206e352011-08-16 15:34:10 -0400532 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800533
Zhao Yakui42158662009-11-20 11:24:18 +0800534 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
535 clock.m1++) {
536 for (clock.m2 = limit->m2.min;
537 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200538 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800539 break;
540 for (clock.n = limit->n.min;
541 clock.n <= limit->n.max; clock.n++) {
542 for (clock.p1 = limit->p1.min;
543 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800544 int this_err;
545
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200546 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000547 if (!intel_PLL_is_valid(dev, limit,
548 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800549 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800550 if (match_clock &&
551 clock.p != match_clock->p)
552 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800553
554 this_err = abs(clock.dot - target);
555 if (this_err < err) {
556 *best_clock = clock;
557 err = this_err;
558 }
559 }
560 }
561 }
562 }
563
564 return (err != target);
565}
566
Ma Lingd4906092009-03-18 20:13:27 +0800567static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200568pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
569 int target, int refclk, intel_clock_t *match_clock,
570 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200571{
572 struct drm_device *dev = crtc->dev;
573 intel_clock_t clock;
574 int err = target;
575
576 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
577 /*
578 * For LVDS just rely on its current settings for dual-channel.
579 * We haven't figured out how to reliably set up different
580 * single/dual channel state, if we even can.
581 */
582 if (intel_is_dual_link_lvds(dev))
583 clock.p2 = limit->p2.p2_fast;
584 else
585 clock.p2 = limit->p2.p2_slow;
586 } else {
587 if (target < limit->p2.dot_limit)
588 clock.p2 = limit->p2.p2_slow;
589 else
590 clock.p2 = limit->p2.p2_fast;
591 }
592
593 memset(best_clock, 0, sizeof(*best_clock));
594
595 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
596 clock.m1++) {
597 for (clock.m2 = limit->m2.min;
598 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200599 for (clock.n = limit->n.min;
600 clock.n <= limit->n.max; clock.n++) {
601 for (clock.p1 = limit->p1.min;
602 clock.p1 <= limit->p1.max; clock.p1++) {
603 int this_err;
604
605 pineview_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800606 if (!intel_PLL_is_valid(dev, limit,
607 &clock))
608 continue;
609 if (match_clock &&
610 clock.p != match_clock->p)
611 continue;
612
613 this_err = abs(clock.dot - target);
614 if (this_err < err) {
615 *best_clock = clock;
616 err = this_err;
617 }
618 }
619 }
620 }
621 }
622
623 return (err != target);
624}
625
Ma Lingd4906092009-03-18 20:13:27 +0800626static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200627g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
628 int target, int refclk, intel_clock_t *match_clock,
629 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800630{
631 struct drm_device *dev = crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800632 intel_clock_t clock;
633 int max_n;
634 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400635 /* approximately equals target * 0.00585 */
636 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800637 found = false;
638
639 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100640 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800641 clock.p2 = limit->p2.p2_fast;
642 else
643 clock.p2 = limit->p2.p2_slow;
644 } else {
645 if (target < limit->p2.dot_limit)
646 clock.p2 = limit->p2.p2_slow;
647 else
648 clock.p2 = limit->p2.p2_fast;
649 }
650
651 memset(best_clock, 0, sizeof(*best_clock));
652 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200653 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800654 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200655 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800656 for (clock.m1 = limit->m1.max;
657 clock.m1 >= limit->m1.min; clock.m1--) {
658 for (clock.m2 = limit->m2.max;
659 clock.m2 >= limit->m2.min; clock.m2--) {
660 for (clock.p1 = limit->p1.max;
661 clock.p1 >= limit->p1.min; clock.p1--) {
662 int this_err;
663
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200664 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000665 if (!intel_PLL_is_valid(dev, limit,
666 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800667 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000668
669 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800670 if (this_err < err_most) {
671 *best_clock = clock;
672 err_most = this_err;
673 max_n = clock.n;
674 found = true;
675 }
676 }
677 }
678 }
679 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800680 return found;
681}
Ma Lingd4906092009-03-18 20:13:27 +0800682
Zhenyu Wang2c072452009-06-05 15:38:42 +0800683static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200684vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
685 int target, int refclk, intel_clock_t *match_clock,
686 intel_clock_t *best_clock)
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700687{
688 u32 p1, p2, m1, m2, vco, bestn, bestm1, bestm2, bestp1, bestp2;
689 u32 m, n, fastclk;
690 u32 updrate, minupdate, fracbits, p;
691 unsigned long bestppm, ppm, absppm;
692 int dotclk, flag;
693
Alan Coxaf447bd2012-07-25 13:49:18 +0100694 flag = 0;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700695 dotclk = target * 1000;
696 bestppm = 1000000;
697 ppm = absppm = 0;
698 fastclk = dotclk / (2*100);
699 updrate = 0;
700 minupdate = 19200;
701 fracbits = 1;
702 n = p = p1 = p2 = m = m1 = m2 = vco = bestn = 0;
703 bestm1 = bestm2 = bestp1 = bestp2 = 0;
704
705 /* based on hardware requirement, prefer smaller n to precision */
706 for (n = limit->n.min; n <= ((refclk) / minupdate); n++) {
707 updrate = refclk / n;
708 for (p1 = limit->p1.max; p1 > limit->p1.min; p1--) {
709 for (p2 = limit->p2.p2_fast+1; p2 > 0; p2--) {
710 if (p2 > 10)
711 p2 = p2 - 1;
712 p = p1 * p2;
713 /* based on hardware requirement, prefer bigger m1,m2 values */
714 for (m1 = limit->m1.min; m1 <= limit->m1.max; m1++) {
715 m2 = (((2*(fastclk * p * n / m1 )) +
716 refclk) / (2*refclk));
717 m = m1 * m2;
718 vco = updrate * m;
719 if (vco >= limit->vco.min && vco < limit->vco.max) {
720 ppm = 1000000 * ((vco / p) - fastclk) / fastclk;
721 absppm = (ppm > 0) ? ppm : (-ppm);
722 if (absppm < 100 && ((p1 * p2) > (bestp1 * bestp2))) {
723 bestppm = 0;
724 flag = 1;
725 }
726 if (absppm < bestppm - 10) {
727 bestppm = absppm;
728 flag = 1;
729 }
730 if (flag) {
731 bestn = n;
732 bestm1 = m1;
733 bestm2 = m2;
734 bestp1 = p1;
735 bestp2 = p2;
736 flag = 0;
737 }
738 }
739 }
740 }
741 }
742 }
743 best_clock->n = bestn;
744 best_clock->m1 = bestm1;
745 best_clock->m2 = bestm2;
746 best_clock->p1 = bestp1;
747 best_clock->p2 = bestp2;
748
749 return true;
750}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700751
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200752enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
753 enum pipe pipe)
754{
755 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
756 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
757
Daniel Vetter3b117c82013-04-17 20:15:07 +0200758 return intel_crtc->config.cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200759}
760
Paulo Zanonia928d532012-05-04 17:18:15 -0300761static void ironlake_wait_for_vblank(struct drm_device *dev, int pipe)
762{
763 struct drm_i915_private *dev_priv = dev->dev_private;
764 u32 frame, frame_reg = PIPEFRAME(pipe);
765
766 frame = I915_READ(frame_reg);
767
768 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
769 DRM_DEBUG_KMS("vblank wait timed out\n");
770}
771
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700772/**
773 * intel_wait_for_vblank - wait for vblank on a given pipe
774 * @dev: drm device
775 * @pipe: pipe to wait for
776 *
777 * Wait for vblank to occur on a given pipe. Needed for various bits of
778 * mode setting code.
779 */
780void intel_wait_for_vblank(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -0800781{
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700782 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800783 int pipestat_reg = PIPESTAT(pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700784
Paulo Zanonia928d532012-05-04 17:18:15 -0300785 if (INTEL_INFO(dev)->gen >= 5) {
786 ironlake_wait_for_vblank(dev, pipe);
787 return;
788 }
789
Chris Wilson300387c2010-09-05 20:25:43 +0100790 /* Clear existing vblank status. Note this will clear any other
791 * sticky status fields as well.
792 *
793 * This races with i915_driver_irq_handler() with the result
794 * that either function could miss a vblank event. Here it is not
795 * fatal, as we will either wait upon the next vblank interrupt or
796 * timeout. Generally speaking intel_wait_for_vblank() is only
797 * called during modeset at which time the GPU should be idle and
798 * should *not* be performing page flips and thus not waiting on
799 * vblanks...
800 * Currently, the result of us stealing a vblank from the irq
801 * handler is that a single frame will be skipped during swapbuffers.
802 */
803 I915_WRITE(pipestat_reg,
804 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
805
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700806 /* Wait for vblank interrupt bit to set */
Chris Wilson481b6af2010-08-23 17:43:35 +0100807 if (wait_for(I915_READ(pipestat_reg) &
808 PIPE_VBLANK_INTERRUPT_STATUS,
809 50))
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700810 DRM_DEBUG_KMS("vblank wait timed out\n");
811}
812
Keith Packardab7ad7f2010-10-03 00:33:06 -0700813/*
814 * intel_wait_for_pipe_off - wait for pipe to turn off
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700815 * @dev: drm device
816 * @pipe: pipe to wait for
817 *
818 * After disabling a pipe, we can't wait for vblank in the usual way,
819 * spinning on the vblank interrupt status bit, since we won't actually
820 * see an interrupt when the pipe is disabled.
821 *
Keith Packardab7ad7f2010-10-03 00:33:06 -0700822 * On Gen4 and above:
823 * wait for the pipe register state bit to turn off
824 *
825 * Otherwise:
826 * wait for the display line value to settle (it usually
827 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +0100828 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700829 */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100830void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700831{
832 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200833 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
834 pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700835
Keith Packardab7ad7f2010-10-03 00:33:06 -0700836 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200837 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700838
Keith Packardab7ad7f2010-10-03 00:33:06 -0700839 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100840 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
841 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200842 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700843 } else {
Paulo Zanoni837ba002012-05-04 17:18:14 -0300844 u32 last_line, line_mask;
Chris Wilson58e10eb2010-10-03 10:56:11 +0100845 int reg = PIPEDSL(pipe);
Keith Packardab7ad7f2010-10-03 00:33:06 -0700846 unsigned long timeout = jiffies + msecs_to_jiffies(100);
847
Paulo Zanoni837ba002012-05-04 17:18:14 -0300848 if (IS_GEN2(dev))
849 line_mask = DSL_LINEMASK_GEN2;
850 else
851 line_mask = DSL_LINEMASK_GEN3;
852
Keith Packardab7ad7f2010-10-03 00:33:06 -0700853 /* Wait for the display line to settle */
854 do {
Paulo Zanoni837ba002012-05-04 17:18:14 -0300855 last_line = I915_READ(reg) & line_mask;
Keith Packardab7ad7f2010-10-03 00:33:06 -0700856 mdelay(5);
Paulo Zanoni837ba002012-05-04 17:18:14 -0300857 } while (((I915_READ(reg) & line_mask) != last_line) &&
Keith Packardab7ad7f2010-10-03 00:33:06 -0700858 time_after(timeout, jiffies));
859 if (time_after(jiffies, timeout))
Daniel Vetter284637d2012-07-09 09:51:57 +0200860 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700861 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800862}
863
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000864/*
865 * ibx_digital_port_connected - is the specified port connected?
866 * @dev_priv: i915 private structure
867 * @port: the port to test
868 *
869 * Returns true if @port is connected, false otherwise.
870 */
871bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
872 struct intel_digital_port *port)
873{
874 u32 bit;
875
Damien Lespiauc36346e2012-12-13 16:09:03 +0000876 if (HAS_PCH_IBX(dev_priv->dev)) {
877 switch(port->port) {
878 case PORT_B:
879 bit = SDE_PORTB_HOTPLUG;
880 break;
881 case PORT_C:
882 bit = SDE_PORTC_HOTPLUG;
883 break;
884 case PORT_D:
885 bit = SDE_PORTD_HOTPLUG;
886 break;
887 default:
888 return true;
889 }
890 } else {
891 switch(port->port) {
892 case PORT_B:
893 bit = SDE_PORTB_HOTPLUG_CPT;
894 break;
895 case PORT_C:
896 bit = SDE_PORTC_HOTPLUG_CPT;
897 break;
898 case PORT_D:
899 bit = SDE_PORTD_HOTPLUG_CPT;
900 break;
901 default:
902 return true;
903 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000904 }
905
906 return I915_READ(SDEISR) & bit;
907}
908
Jesse Barnesb24e7172011-01-04 15:09:30 -0800909static const char *state_string(bool enabled)
910{
911 return enabled ? "on" : "off";
912}
913
914/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +0200915void assert_pll(struct drm_i915_private *dev_priv,
916 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -0800917{
918 int reg;
919 u32 val;
920 bool cur_state;
921
922 reg = DPLL(pipe);
923 val = I915_READ(reg);
924 cur_state = !!(val & DPLL_VCO_ENABLE);
925 WARN(cur_state != state,
926 "PLL state assertion failure (expected %s, current %s)\n",
927 state_string(state), state_string(cur_state));
928}
Jesse Barnesb24e7172011-01-04 15:09:30 -0800929
Daniel Vetter55607e82013-06-16 21:42:39 +0200930struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +0200931intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -0800932{
Daniel Vettere2b78262013-06-07 23:10:03 +0200933 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
934
Daniel Vettera43f6e02013-06-07 23:10:32 +0200935 if (crtc->config.shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +0200936 return NULL;
937
Daniel Vettera43f6e02013-06-07 23:10:32 +0200938 return &dev_priv->shared_dplls[crtc->config.shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +0200939}
940
Jesse Barnesb24e7172011-01-04 15:09:30 -0800941/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +0200942void assert_shared_dpll(struct drm_i915_private *dev_priv,
943 struct intel_shared_dpll *pll,
944 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -0800945{
Jesse Barnes040484a2011-01-03 12:14:26 -0800946 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +0200947 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -0800948
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -0300949 if (HAS_PCH_LPT(dev_priv->dev)) {
950 DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
951 return;
952 }
953
Chris Wilson92b27b02012-05-20 18:10:50 +0100954 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +0200955 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +0100956 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +0100957
Daniel Vetter53589012013-06-05 13:34:16 +0200958 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Chris Wilson92b27b02012-05-20 18:10:50 +0100959 WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +0200960 "%s assertion failure (expected %s, current %s)\n",
961 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -0800962}
Jesse Barnes040484a2011-01-03 12:14:26 -0800963
964static void assert_fdi_tx(struct drm_i915_private *dev_priv,
965 enum pipe pipe, bool state)
966{
967 int reg;
968 u32 val;
969 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -0200970 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
971 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -0800972
Paulo Zanoniaffa9352012-11-23 15:30:39 -0200973 if (HAS_DDI(dev_priv->dev)) {
974 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -0200975 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -0300976 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -0200977 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -0300978 } else {
979 reg = FDI_TX_CTL(pipe);
980 val = I915_READ(reg);
981 cur_state = !!(val & FDI_TX_ENABLE);
982 }
Jesse Barnes040484a2011-01-03 12:14:26 -0800983 WARN(cur_state != state,
984 "FDI TX state assertion failure (expected %s, current %s)\n",
985 state_string(state), state_string(cur_state));
986}
987#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
988#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
989
990static void assert_fdi_rx(struct drm_i915_private *dev_priv,
991 enum pipe pipe, bool state)
992{
993 int reg;
994 u32 val;
995 bool cur_state;
996
Paulo Zanonid63fa0d2012-11-20 13:27:35 -0200997 reg = FDI_RX_CTL(pipe);
998 val = I915_READ(reg);
999 cur_state = !!(val & FDI_RX_ENABLE);
Jesse Barnes040484a2011-01-03 12:14:26 -08001000 WARN(cur_state != state,
1001 "FDI RX state assertion failure (expected %s, current %s)\n",
1002 state_string(state), state_string(cur_state));
1003}
1004#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1005#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1006
1007static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1008 enum pipe pipe)
1009{
1010 int reg;
1011 u32 val;
1012
1013 /* ILK FDI PLL is always enabled */
1014 if (dev_priv->info->gen == 5)
1015 return;
1016
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001017 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001018 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001019 return;
1020
Jesse Barnes040484a2011-01-03 12:14:26 -08001021 reg = FDI_TX_CTL(pipe);
1022 val = I915_READ(reg);
1023 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1024}
1025
Daniel Vetter55607e82013-06-16 21:42:39 +02001026void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1027 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001028{
1029 int reg;
1030 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001031 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001032
1033 reg = FDI_RX_CTL(pipe);
1034 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001035 cur_state = !!(val & FDI_RX_PLL_ENABLE);
1036 WARN(cur_state != state,
1037 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1038 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001039}
1040
Jesse Barnesea0760c2011-01-04 15:09:32 -08001041static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1042 enum pipe pipe)
1043{
1044 int pp_reg, lvds_reg;
1045 u32 val;
1046 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001047 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001048
1049 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1050 pp_reg = PCH_PP_CONTROL;
1051 lvds_reg = PCH_LVDS;
1052 } else {
1053 pp_reg = PP_CONTROL;
1054 lvds_reg = LVDS;
1055 }
1056
1057 val = I915_READ(pp_reg);
1058 if (!(val & PANEL_POWER_ON) ||
1059 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1060 locked = false;
1061
1062 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1063 panel_pipe = PIPE_B;
1064
1065 WARN(panel_pipe == pipe && locked,
1066 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001067 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001068}
1069
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001070void assert_pipe(struct drm_i915_private *dev_priv,
1071 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001072{
1073 int reg;
1074 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001075 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001076 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1077 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001078
Daniel Vetter8e636782012-01-22 01:36:48 +01001079 /* if we need the pipe A quirk it must be always on */
1080 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1081 state = true;
1082
Paulo Zanonib97186f2013-05-03 12:15:36 -03001083 if (!intel_display_power_enabled(dev_priv->dev,
1084 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001085 cur_state = false;
1086 } else {
1087 reg = PIPECONF(cpu_transcoder);
1088 val = I915_READ(reg);
1089 cur_state = !!(val & PIPECONF_ENABLE);
1090 }
1091
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001092 WARN(cur_state != state,
1093 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001094 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001095}
1096
Chris Wilson931872f2012-01-16 23:01:13 +00001097static void assert_plane(struct drm_i915_private *dev_priv,
1098 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001099{
1100 int reg;
1101 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001102 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001103
1104 reg = DSPCNTR(plane);
1105 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001106 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1107 WARN(cur_state != state,
1108 "plane %c assertion failure (expected %s, current %s)\n",
1109 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001110}
1111
Chris Wilson931872f2012-01-16 23:01:13 +00001112#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1113#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1114
Jesse Barnesb24e7172011-01-04 15:09:30 -08001115static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1116 enum pipe pipe)
1117{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001118 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001119 int reg, i;
1120 u32 val;
1121 int cur_pipe;
1122
Ville Syrjälä653e1022013-06-04 13:49:05 +03001123 /* Primary planes are fixed to pipes on gen4+ */
1124 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001125 reg = DSPCNTR(pipe);
1126 val = I915_READ(reg);
1127 WARN((val & DISPLAY_PLANE_ENABLE),
1128 "plane %c assertion failure, should be disabled but not\n",
1129 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001130 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001131 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001132
Jesse Barnesb24e7172011-01-04 15:09:30 -08001133 /* Need to check both planes against the pipe */
Damien Lespiau08e2a7d2013-07-11 20:10:54 +01001134 for_each_pipe(i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001135 reg = DSPCNTR(i);
1136 val = I915_READ(reg);
1137 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1138 DISPPLANE_SEL_PIPE_SHIFT;
1139 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001140 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1141 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001142 }
1143}
1144
Jesse Barnes19332d72013-03-28 09:55:38 -07001145static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1146 enum pipe pipe)
1147{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001148 struct drm_device *dev = dev_priv->dev;
Jesse Barnes19332d72013-03-28 09:55:38 -07001149 int reg, i;
1150 u32 val;
1151
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001152 if (IS_VALLEYVIEW(dev)) {
1153 for (i = 0; i < dev_priv->num_plane; i++) {
1154 reg = SPCNTR(pipe, i);
1155 val = I915_READ(reg);
1156 WARN((val & SP_ENABLE),
1157 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1158 sprite_name(pipe, i), pipe_name(pipe));
1159 }
1160 } else if (INTEL_INFO(dev)->gen >= 7) {
1161 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001162 val = I915_READ(reg);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001163 WARN((val & SPRITE_ENABLE),
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001164 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001165 plane_name(pipe), pipe_name(pipe));
1166 } else if (INTEL_INFO(dev)->gen >= 5) {
1167 reg = DVSCNTR(pipe);
1168 val = I915_READ(reg);
1169 WARN((val & DVS_ENABLE),
1170 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1171 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001172 }
1173}
1174
Jesse Barnes92f25842011-01-04 15:09:34 -08001175static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1176{
1177 u32 val;
1178 bool enabled;
1179
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001180 if (HAS_PCH_LPT(dev_priv->dev)) {
1181 DRM_DEBUG_DRIVER("LPT does not has PCH refclk, skipping check\n");
1182 return;
1183 }
1184
Jesse Barnes92f25842011-01-04 15:09:34 -08001185 val = I915_READ(PCH_DREF_CONTROL);
1186 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1187 DREF_SUPERSPREAD_SOURCE_MASK));
1188 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1189}
1190
Daniel Vetterab9412b2013-05-03 11:49:46 +02001191static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1192 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001193{
1194 int reg;
1195 u32 val;
1196 bool enabled;
1197
Daniel Vetterab9412b2013-05-03 11:49:46 +02001198 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001199 val = I915_READ(reg);
1200 enabled = !!(val & TRANS_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001201 WARN(enabled,
1202 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1203 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001204}
1205
Keith Packard4e634382011-08-06 10:39:45 -07001206static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1207 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001208{
1209 if ((val & DP_PORT_EN) == 0)
1210 return false;
1211
1212 if (HAS_PCH_CPT(dev_priv->dev)) {
1213 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1214 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1215 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1216 return false;
1217 } else {
1218 if ((val & DP_PIPE_MASK) != (pipe << 30))
1219 return false;
1220 }
1221 return true;
1222}
1223
Keith Packard1519b992011-08-06 10:35:34 -07001224static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1225 enum pipe pipe, u32 val)
1226{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001227 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001228 return false;
1229
1230 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001231 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001232 return false;
1233 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001234 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001235 return false;
1236 }
1237 return true;
1238}
1239
1240static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1241 enum pipe pipe, u32 val)
1242{
1243 if ((val & LVDS_PORT_EN) == 0)
1244 return false;
1245
1246 if (HAS_PCH_CPT(dev_priv->dev)) {
1247 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1248 return false;
1249 } else {
1250 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1251 return false;
1252 }
1253 return true;
1254}
1255
1256static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1257 enum pipe pipe, u32 val)
1258{
1259 if ((val & ADPA_DAC_ENABLE) == 0)
1260 return false;
1261 if (HAS_PCH_CPT(dev_priv->dev)) {
1262 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1263 return false;
1264 } else {
1265 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1266 return false;
1267 }
1268 return true;
1269}
1270
Jesse Barnes291906f2011-02-02 12:28:03 -08001271static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001272 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001273{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001274 u32 val = I915_READ(reg);
Keith Packard4e634382011-08-06 10:39:45 -07001275 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001276 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001277 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001278
Daniel Vetter75c5da22012-09-10 21:58:29 +02001279 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1280 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001281 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001282}
1283
1284static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1285 enum pipe pipe, int reg)
1286{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001287 u32 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001288 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001289 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001290 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001291
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001292 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001293 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001294 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001295}
1296
1297static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1298 enum pipe pipe)
1299{
1300 int reg;
1301 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001302
Keith Packardf0575e92011-07-25 22:12:43 -07001303 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1304 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1305 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001306
1307 reg = PCH_ADPA;
1308 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001309 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001310 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001311 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001312
1313 reg = PCH_LVDS;
1314 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001315 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001316 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001317 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001318
Paulo Zanonie2debe92013-02-18 19:00:27 -03001319 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1320 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1321 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001322}
1323
Daniel Vetter87442f72013-06-06 00:52:17 +02001324static void vlv_enable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001325{
1326 int reg;
1327 u32 val;
1328
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001329 assert_pipe_disabled(dev_priv, pipe);
1330
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001331 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001332 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1333
1334 /* PLL is protected by panel, make sure we can write it */
1335 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
1336 assert_panel_unlocked(dev_priv, pipe);
1337
1338 reg = DPLL(pipe);
1339 val = I915_READ(reg);
1340 val |= DPLL_VCO_ENABLE;
1341
1342 /* We do this three times for luck */
1343 I915_WRITE(reg, val);
1344 POSTING_READ(reg);
1345 udelay(150); /* wait for warmup */
1346 I915_WRITE(reg, val);
1347 POSTING_READ(reg);
1348 udelay(150); /* wait for warmup */
1349 I915_WRITE(reg, val);
1350 POSTING_READ(reg);
1351 udelay(150); /* wait for warmup */
1352}
1353
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001354static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001355{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001356 struct drm_device *dev = crtc->base.dev;
1357 struct drm_i915_private *dev_priv = dev->dev_private;
1358 int reg = DPLL(crtc->pipe);
1359 u32 dpll = crtc->config.dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001360
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001361 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001362
1363 /* No really, not for ILK+ */
1364 BUG_ON(dev_priv->info->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001365
1366 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001367 if (IS_MOBILE(dev) && !IS_I830(dev))
1368 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001369
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001370 I915_WRITE(reg, dpll);
1371
1372 /* Wait for the clocks to stabilize. */
1373 POSTING_READ(reg);
1374 udelay(150);
1375
1376 if (INTEL_INFO(dev)->gen >= 4) {
1377 I915_WRITE(DPLL_MD(crtc->pipe),
1378 crtc->config.dpll_hw_state.dpll_md);
1379 } else {
1380 /* The pixel multiplier can only be updated once the
1381 * DPLL is enabled and the clocks are stable.
1382 *
1383 * So write it again.
1384 */
1385 I915_WRITE(reg, dpll);
1386 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001387
1388 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001389 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001390 POSTING_READ(reg);
1391 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001392 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001393 POSTING_READ(reg);
1394 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001395 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001396 POSTING_READ(reg);
1397 udelay(150); /* wait for warmup */
1398}
1399
1400/**
1401 * intel_disable_pll - disable a PLL
1402 * @dev_priv: i915 private structure
1403 * @pipe: pipe PLL to disable
1404 *
1405 * Disable the PLL for @pipe, making sure the pipe is off first.
1406 *
1407 * Note! This is for pre-ILK only.
1408 */
1409static void intel_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1410{
1411 int reg;
1412 u32 val;
1413
1414 /* Don't disable pipe A or pipe A PLLs if needed */
1415 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1416 return;
1417
1418 /* Make sure the pipe isn't still relying on us */
1419 assert_pipe_disabled(dev_priv, pipe);
1420
1421 reg = DPLL(pipe);
1422 val = I915_READ(reg);
1423 val &= ~DPLL_VCO_ENABLE;
1424 I915_WRITE(reg, val);
1425 POSTING_READ(reg);
1426}
1427
Jesse Barnes89b667f2013-04-18 14:51:36 -07001428void vlv_wait_port_ready(struct drm_i915_private *dev_priv, int port)
1429{
1430 u32 port_mask;
1431
1432 if (!port)
1433 port_mask = DPLL_PORTB_READY_MASK;
1434 else
1435 port_mask = DPLL_PORTC_READY_MASK;
1436
1437 if (wait_for((I915_READ(DPLL(0)) & port_mask) == 0, 1000))
1438 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
1439 'B' + port, I915_READ(DPLL(0)));
1440}
1441
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001442/**
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001443 * ironlake_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001444 * @dev_priv: i915 private structure
1445 * @pipe: pipe PLL to enable
1446 *
1447 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1448 * drives the transcoder clock.
1449 */
Daniel Vettere2b78262013-06-07 23:10:03 +02001450static void ironlake_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001451{
Daniel Vettere2b78262013-06-07 23:10:03 +02001452 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1453 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001454
Chris Wilson48da64a2012-05-13 20:16:12 +01001455 /* PCH PLLs only available on ILK, SNB and IVB */
Jesse Barnes92f25842011-01-04 15:09:34 -08001456 BUG_ON(dev_priv->info->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001457 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001458 return;
1459
1460 if (WARN_ON(pll->refcount == 0))
1461 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001462
Daniel Vetter46edb022013-06-05 13:34:12 +02001463 DRM_DEBUG_KMS("enable %s (active %d, on? %d)for crtc %d\n",
1464 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001465 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001466
Daniel Vettercdbd2312013-06-05 13:34:03 +02001467 if (pll->active++) {
1468 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001469 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001470 return;
1471 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001472 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001473
Daniel Vetter46edb022013-06-05 13:34:12 +02001474 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001475 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001476 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001477}
1478
Daniel Vettere2b78262013-06-07 23:10:03 +02001479static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001480{
Daniel Vettere2b78262013-06-07 23:10:03 +02001481 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1482 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001483
Jesse Barnes92f25842011-01-04 15:09:34 -08001484 /* PCH only available on ILK+ */
1485 BUG_ON(dev_priv->info->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001486 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001487 return;
1488
Chris Wilson48da64a2012-05-13 20:16:12 +01001489 if (WARN_ON(pll->refcount == 0))
1490 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001491
Daniel Vetter46edb022013-06-05 13:34:12 +02001492 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1493 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001494 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001495
Chris Wilson48da64a2012-05-13 20:16:12 +01001496 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001497 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001498 return;
1499 }
1500
Daniel Vettere9d69442013-06-05 13:34:15 +02001501 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001502 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001503 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001504 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001505
Daniel Vetter46edb022013-06-05 13:34:12 +02001506 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001507 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001508 pll->on = false;
Jesse Barnes92f25842011-01-04 15:09:34 -08001509}
1510
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001511static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1512 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001513{
Daniel Vetter23670b322012-11-01 09:15:30 +01001514 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001515 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001516 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001517 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001518
1519 /* PCH only available on ILK+ */
1520 BUG_ON(dev_priv->info->gen < 5);
1521
1522 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001523 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001524 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001525
1526 /* FDI must be feeding us bits for PCH ports */
1527 assert_fdi_tx_enabled(dev_priv, pipe);
1528 assert_fdi_rx_enabled(dev_priv, pipe);
1529
Daniel Vetter23670b322012-11-01 09:15:30 +01001530 if (HAS_PCH_CPT(dev)) {
1531 /* Workaround: Set the timing override bit before enabling the
1532 * pch transcoder. */
1533 reg = TRANS_CHICKEN2(pipe);
1534 val = I915_READ(reg);
1535 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1536 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001537 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001538
Daniel Vetterab9412b2013-05-03 11:49:46 +02001539 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001540 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001541 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001542
1543 if (HAS_PCH_IBX(dev_priv->dev)) {
1544 /*
1545 * make the BPC in transcoder be consistent with
1546 * that in pipeconf reg.
1547 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001548 val &= ~PIPECONF_BPC_MASK;
1549 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001550 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001551
1552 val &= ~TRANS_INTERLACE_MASK;
1553 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001554 if (HAS_PCH_IBX(dev_priv->dev) &&
1555 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1556 val |= TRANS_LEGACY_INTERLACED_ILK;
1557 else
1558 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001559 else
1560 val |= TRANS_PROGRESSIVE;
1561
Jesse Barnes040484a2011-01-03 12:14:26 -08001562 I915_WRITE(reg, val | TRANS_ENABLE);
1563 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001564 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001565}
1566
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001567static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001568 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001569{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001570 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001571
1572 /* PCH only available on ILK+ */
1573 BUG_ON(dev_priv->info->gen < 5);
1574
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001575 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001576 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001577 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001578
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001579 /* Workaround: set timing override bit. */
1580 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001581 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001582 I915_WRITE(_TRANSA_CHICKEN2, val);
1583
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001584 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001585 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001586
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001587 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1588 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001589 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001590 else
1591 val |= TRANS_PROGRESSIVE;
1592
Daniel Vetterab9412b2013-05-03 11:49:46 +02001593 I915_WRITE(LPT_TRANSCONF, val);
1594 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001595 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001596}
1597
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001598static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1599 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001600{
Daniel Vetter23670b322012-11-01 09:15:30 +01001601 struct drm_device *dev = dev_priv->dev;
1602 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001603
1604 /* FDI relies on the transcoder */
1605 assert_fdi_tx_disabled(dev_priv, pipe);
1606 assert_fdi_rx_disabled(dev_priv, pipe);
1607
Jesse Barnes291906f2011-02-02 12:28:03 -08001608 /* Ports must be off as well */
1609 assert_pch_ports_disabled(dev_priv, pipe);
1610
Daniel Vetterab9412b2013-05-03 11:49:46 +02001611 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001612 val = I915_READ(reg);
1613 val &= ~TRANS_ENABLE;
1614 I915_WRITE(reg, val);
1615 /* wait for PCH transcoder off, transcoder state */
1616 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001617 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001618
1619 if (!HAS_PCH_IBX(dev)) {
1620 /* Workaround: Clear the timing override chicken bit again. */
1621 reg = TRANS_CHICKEN2(pipe);
1622 val = I915_READ(reg);
1623 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1624 I915_WRITE(reg, val);
1625 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001626}
1627
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001628static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001629{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001630 u32 val;
1631
Daniel Vetterab9412b2013-05-03 11:49:46 +02001632 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001633 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001634 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001635 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001636 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001637 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001638
1639 /* Workaround: clear timing override bit. */
1640 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001641 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001642 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001643}
1644
1645/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001646 * intel_enable_pipe - enable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001647 * @dev_priv: i915 private structure
1648 * @pipe: pipe to enable
Jesse Barnes040484a2011-01-03 12:14:26 -08001649 * @pch_port: on ILK+, is this pipe driving a PCH port or not
Jesse Barnesb24e7172011-01-04 15:09:30 -08001650 *
1651 * Enable @pipe, making sure that various hardware specific requirements
1652 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1653 *
1654 * @pipe should be %PIPE_A or %PIPE_B.
1655 *
1656 * Will wait until the pipe is actually running (i.e. first vblank) before
1657 * returning.
1658 */
Jesse Barnes040484a2011-01-03 12:14:26 -08001659static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
1660 bool pch_port)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001661{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001662 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1663 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001664 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001665 int reg;
1666 u32 val;
1667
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001668 assert_planes_disabled(dev_priv, pipe);
1669 assert_sprites_disabled(dev_priv, pipe);
1670
Paulo Zanoni681e5812012-12-06 11:12:38 -02001671 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001672 pch_transcoder = TRANSCODER_A;
1673 else
1674 pch_transcoder = pipe;
1675
Jesse Barnesb24e7172011-01-04 15:09:30 -08001676 /*
1677 * A pipe without a PLL won't actually be able to drive bits from
1678 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1679 * need the check.
1680 */
1681 if (!HAS_PCH_SPLIT(dev_priv->dev))
1682 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001683 else {
1684 if (pch_port) {
1685 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001686 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001687 assert_fdi_tx_pll_enabled(dev_priv,
1688 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001689 }
1690 /* FIXME: assert CPU port conditions for SNB+ */
1691 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001692
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001693 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001694 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001695 if (val & PIPECONF_ENABLE)
1696 return;
1697
1698 I915_WRITE(reg, val | PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001699 intel_wait_for_vblank(dev_priv->dev, pipe);
1700}
1701
1702/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001703 * intel_disable_pipe - disable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001704 * @dev_priv: i915 private structure
1705 * @pipe: pipe to disable
1706 *
1707 * Disable @pipe, making sure that various hardware specific requirements
1708 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1709 *
1710 * @pipe should be %PIPE_A or %PIPE_B.
1711 *
1712 * Will wait until the pipe has shut down before returning.
1713 */
1714static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1715 enum pipe pipe)
1716{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001717 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1718 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001719 int reg;
1720 u32 val;
1721
1722 /*
1723 * Make sure planes won't keep trying to pump pixels to us,
1724 * or we might hang the display.
1725 */
1726 assert_planes_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001727 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001728
1729 /* Don't disable pipe A or pipe A PLLs if needed */
1730 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1731 return;
1732
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001733 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001734 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001735 if ((val & PIPECONF_ENABLE) == 0)
1736 return;
1737
1738 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001739 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1740}
1741
Keith Packardd74362c2011-07-28 14:47:14 -07001742/*
1743 * Plane regs are double buffered, going from enabled->disabled needs a
1744 * trigger in order to latch. The display address reg provides this.
1745 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03001746void intel_flush_display_plane(struct drm_i915_private *dev_priv,
Keith Packardd74362c2011-07-28 14:47:14 -07001747 enum plane plane)
1748{
Damien Lespiau14f86142012-10-29 15:24:49 +00001749 if (dev_priv->info->gen >= 4)
1750 I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
1751 else
1752 I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
Keith Packardd74362c2011-07-28 14:47:14 -07001753}
1754
Jesse Barnesb24e7172011-01-04 15:09:30 -08001755/**
1756 * intel_enable_plane - enable a display plane on a given pipe
1757 * @dev_priv: i915 private structure
1758 * @plane: plane to enable
1759 * @pipe: pipe being fed
1760 *
1761 * Enable @plane on @pipe, making sure that @pipe is running first.
1762 */
1763static void intel_enable_plane(struct drm_i915_private *dev_priv,
1764 enum plane plane, enum pipe pipe)
1765{
1766 int reg;
1767 u32 val;
1768
1769 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1770 assert_pipe_enabled(dev_priv, pipe);
1771
1772 reg = DSPCNTR(plane);
1773 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001774 if (val & DISPLAY_PLANE_ENABLE)
1775 return;
1776
1777 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
Keith Packardd74362c2011-07-28 14:47:14 -07001778 intel_flush_display_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001779 intel_wait_for_vblank(dev_priv->dev, pipe);
1780}
1781
Jesse Barnesb24e7172011-01-04 15:09:30 -08001782/**
1783 * intel_disable_plane - disable a display plane
1784 * @dev_priv: i915 private structure
1785 * @plane: plane to disable
1786 * @pipe: pipe consuming the data
1787 *
1788 * Disable @plane; should be an independent operation.
1789 */
1790static void intel_disable_plane(struct drm_i915_private *dev_priv,
1791 enum plane plane, enum pipe pipe)
1792{
1793 int reg;
1794 u32 val;
1795
1796 reg = DSPCNTR(plane);
1797 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001798 if ((val & DISPLAY_PLANE_ENABLE) == 0)
1799 return;
1800
1801 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001802 intel_flush_display_plane(dev_priv, plane);
1803 intel_wait_for_vblank(dev_priv->dev, pipe);
1804}
1805
Chris Wilson693db182013-03-05 14:52:39 +00001806static bool need_vtd_wa(struct drm_device *dev)
1807{
1808#ifdef CONFIG_INTEL_IOMMU
1809 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
1810 return true;
1811#endif
1812 return false;
1813}
1814
Chris Wilson127bd2a2010-07-23 23:32:05 +01001815int
Chris Wilson48b956c2010-09-14 12:50:34 +01001816intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00001817 struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00001818 struct intel_ring_buffer *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001819{
Chris Wilsonce453d82011-02-21 14:43:56 +00001820 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001821 u32 alignment;
1822 int ret;
1823
Chris Wilson05394f32010-11-08 19:18:58 +00001824 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001825 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01001826 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1827 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001828 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01001829 alignment = 4 * 1024;
1830 else
1831 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001832 break;
1833 case I915_TILING_X:
1834 /* pin() will align the object as required by fence */
1835 alignment = 0;
1836 break;
1837 case I915_TILING_Y:
Daniel Vetter8bb6e952013-04-06 23:54:56 +02001838 /* Despite that we check this in framebuffer_init userspace can
1839 * screw us over and change the tiling after the fact. Only
1840 * pinned buffers can't change their tiling. */
1841 DRM_DEBUG_DRIVER("Y tiled not allowed for scan out buffers\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001842 return -EINVAL;
1843 default:
1844 BUG();
1845 }
1846
Chris Wilson693db182013-03-05 14:52:39 +00001847 /* Note that the w/a also requires 64 PTE of padding following the
1848 * bo. We currently fill all unused PTE with the shadow page and so
1849 * we should always have valid PTE following the scanout preventing
1850 * the VT-d warning.
1851 */
1852 if (need_vtd_wa(dev) && alignment < 256 * 1024)
1853 alignment = 256 * 1024;
1854
Chris Wilsonce453d82011-02-21 14:43:56 +00001855 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01001856 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01001857 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00001858 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001859
1860 /* Install a fence for tiled scan-out. Pre-i965 always needs a
1861 * fence, whereas 965+ only requires a fence if using
1862 * framebuffer compression. For simplicity, we always install
1863 * a fence as the cost is not that onerous.
1864 */
Chris Wilson06d98132012-04-17 15:31:24 +01001865 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001866 if (ret)
1867 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01001868
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001869 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001870
Chris Wilsonce453d82011-02-21 14:43:56 +00001871 dev_priv->mm.interruptible = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001872 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01001873
1874err_unpin:
1875 i915_gem_object_unpin(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00001876err_interruptible:
1877 dev_priv->mm.interruptible = true;
Chris Wilson48b956c2010-09-14 12:50:34 +01001878 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001879}
1880
Chris Wilson1690e1e2011-12-14 13:57:08 +01001881void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
1882{
1883 i915_gem_object_unpin_fence(obj);
1884 i915_gem_object_unpin(obj);
1885}
1886
Daniel Vetterc2c75132012-07-05 12:17:30 +02001887/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
1888 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00001889unsigned long intel_gen4_compute_page_offset(int *x, int *y,
1890 unsigned int tiling_mode,
1891 unsigned int cpp,
1892 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02001893{
Chris Wilsonbc752862013-02-21 20:04:31 +00001894 if (tiling_mode != I915_TILING_NONE) {
1895 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02001896
Chris Wilsonbc752862013-02-21 20:04:31 +00001897 tile_rows = *y / 8;
1898 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02001899
Chris Wilsonbc752862013-02-21 20:04:31 +00001900 tiles = *x / (512/cpp);
1901 *x %= 512/cpp;
1902
1903 return tile_rows * pitch * 8 + tiles * 4096;
1904 } else {
1905 unsigned int offset;
1906
1907 offset = *y * pitch + *x * cpp;
1908 *y = 0;
1909 *x = (offset & 4095) / cpp;
1910 return offset & -4096;
1911 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02001912}
1913
Jesse Barnes17638cd2011-06-24 12:19:23 -07001914static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
1915 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07001916{
1917 struct drm_device *dev = crtc->dev;
1918 struct drm_i915_private *dev_priv = dev->dev_private;
1919 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1920 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00001921 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07001922 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02001923 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07001924 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01001925 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07001926
1927 switch (plane) {
1928 case 0:
1929 case 1:
1930 break;
1931 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03001932 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes81255562010-08-02 12:07:50 -07001933 return -EINVAL;
1934 }
1935
1936 intel_fb = to_intel_framebuffer(fb);
1937 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07001938
Chris Wilson5eddb702010-09-11 13:48:45 +01001939 reg = DSPCNTR(plane);
1940 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07001941 /* Mask out pixel format bits in case we change it */
1942 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02001943 switch (fb->pixel_format) {
1944 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07001945 dspcntr |= DISPPLANE_8BPP;
1946 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02001947 case DRM_FORMAT_XRGB1555:
1948 case DRM_FORMAT_ARGB1555:
1949 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07001950 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02001951 case DRM_FORMAT_RGB565:
1952 dspcntr |= DISPPLANE_BGRX565;
1953 break;
1954 case DRM_FORMAT_XRGB8888:
1955 case DRM_FORMAT_ARGB8888:
1956 dspcntr |= DISPPLANE_BGRX888;
1957 break;
1958 case DRM_FORMAT_XBGR8888:
1959 case DRM_FORMAT_ABGR8888:
1960 dspcntr |= DISPPLANE_RGBX888;
1961 break;
1962 case DRM_FORMAT_XRGB2101010:
1963 case DRM_FORMAT_ARGB2101010:
1964 dspcntr |= DISPPLANE_BGRX101010;
1965 break;
1966 case DRM_FORMAT_XBGR2101010:
1967 case DRM_FORMAT_ABGR2101010:
1968 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07001969 break;
1970 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01001971 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07001972 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02001973
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001974 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00001975 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07001976 dspcntr |= DISPPLANE_TILED;
1977 else
1978 dspcntr &= ~DISPPLANE_TILED;
1979 }
1980
Ville Syrjäläde1aa622013-06-07 10:47:01 +03001981 if (IS_G4X(dev))
1982 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
1983
Chris Wilson5eddb702010-09-11 13:48:45 +01001984 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07001985
Daniel Vettere506a0c2012-07-05 12:17:29 +02001986 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes81255562010-08-02 12:07:50 -07001987
Daniel Vetterc2c75132012-07-05 12:17:30 +02001988 if (INTEL_INFO(dev)->gen >= 4) {
1989 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00001990 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
1991 fb->bits_per_pixel / 8,
1992 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02001993 linear_offset -= intel_crtc->dspaddr_offset;
1994 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02001995 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02001996 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02001997
Ben Widawskyf343c5f2013-07-05 14:41:04 -07001998 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
1999 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2000 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002001 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002002 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetterc2c75132012-07-05 12:17:30 +02002003 I915_MODIFY_DISPBASE(DSPSURF(plane),
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002004 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002005 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002006 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002007 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002008 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002009 POSTING_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002010
Jesse Barnes17638cd2011-06-24 12:19:23 -07002011 return 0;
2012}
2013
2014static int ironlake_update_plane(struct drm_crtc *crtc,
2015 struct drm_framebuffer *fb, int x, int y)
2016{
2017 struct drm_device *dev = crtc->dev;
2018 struct drm_i915_private *dev_priv = dev->dev_private;
2019 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2020 struct intel_framebuffer *intel_fb;
2021 struct drm_i915_gem_object *obj;
2022 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002023 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002024 u32 dspcntr;
2025 u32 reg;
2026
2027 switch (plane) {
2028 case 0:
2029 case 1:
Jesse Barnes27f82272011-09-02 12:54:37 -07002030 case 2:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002031 break;
2032 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002033 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes17638cd2011-06-24 12:19:23 -07002034 return -EINVAL;
2035 }
2036
2037 intel_fb = to_intel_framebuffer(fb);
2038 obj = intel_fb->obj;
2039
2040 reg = DSPCNTR(plane);
2041 dspcntr = I915_READ(reg);
2042 /* Mask out pixel format bits in case we change it */
2043 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002044 switch (fb->pixel_format) {
2045 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002046 dspcntr |= DISPPLANE_8BPP;
2047 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002048 case DRM_FORMAT_RGB565:
2049 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002050 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002051 case DRM_FORMAT_XRGB8888:
2052 case DRM_FORMAT_ARGB8888:
2053 dspcntr |= DISPPLANE_BGRX888;
2054 break;
2055 case DRM_FORMAT_XBGR8888:
2056 case DRM_FORMAT_ABGR8888:
2057 dspcntr |= DISPPLANE_RGBX888;
2058 break;
2059 case DRM_FORMAT_XRGB2101010:
2060 case DRM_FORMAT_ARGB2101010:
2061 dspcntr |= DISPPLANE_BGRX101010;
2062 break;
2063 case DRM_FORMAT_XBGR2101010:
2064 case DRM_FORMAT_ABGR2101010:
2065 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002066 break;
2067 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002068 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002069 }
2070
2071 if (obj->tiling_mode != I915_TILING_NONE)
2072 dspcntr |= DISPPLANE_TILED;
2073 else
2074 dspcntr &= ~DISPPLANE_TILED;
2075
2076 /* must disable */
2077 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2078
2079 I915_WRITE(reg, dspcntr);
2080
Daniel Vettere506a0c2012-07-05 12:17:29 +02002081 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002082 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002083 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2084 fb->bits_per_pixel / 8,
2085 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002086 linear_offset -= intel_crtc->dspaddr_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002087
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002088 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2089 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2090 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002091 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002092 I915_MODIFY_DISPBASE(DSPSURF(plane),
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002093 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002094 if (IS_HASWELL(dev)) {
2095 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2096 } else {
2097 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2098 I915_WRITE(DSPLINOFF(plane), linear_offset);
2099 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002100 POSTING_READ(reg);
2101
2102 return 0;
2103}
2104
2105/* Assume fb object is pinned & idle & fenced and just update base pointers */
2106static int
2107intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2108 int x, int y, enum mode_set_atomic state)
2109{
2110 struct drm_device *dev = crtc->dev;
2111 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002112
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002113 if (dev_priv->display.disable_fbc)
2114 dev_priv->display.disable_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02002115 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07002116
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002117 return dev_priv->display.update_plane(crtc, fb, x, y);
Jesse Barnes81255562010-08-02 12:07:50 -07002118}
2119
Ville Syrjälä96a02912013-02-18 19:08:49 +02002120void intel_display_handle_reset(struct drm_device *dev)
2121{
2122 struct drm_i915_private *dev_priv = dev->dev_private;
2123 struct drm_crtc *crtc;
2124
2125 /*
2126 * Flips in the rings have been nuked by the reset,
2127 * so complete all pending flips so that user space
2128 * will get its events and not get stuck.
2129 *
2130 * Also update the base address of all primary
2131 * planes to the the last fb to make sure we're
2132 * showing the correct fb after a reset.
2133 *
2134 * Need to make two loops over the crtcs so that we
2135 * don't try to grab a crtc mutex before the
2136 * pending_flip_queue really got woken up.
2137 */
2138
2139 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2140 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2141 enum plane plane = intel_crtc->plane;
2142
2143 intel_prepare_page_flip(dev, plane);
2144 intel_finish_page_flip_plane(dev, plane);
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
2150 mutex_lock(&crtc->mutex);
2151 if (intel_crtc->active)
2152 dev_priv->display.update_plane(crtc, crtc->fb,
2153 crtc->x, crtc->y);
2154 mutex_unlock(&crtc->mutex);
2155 }
2156}
2157
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002158static int
Chris Wilson14667a42012-04-03 17:58:35 +01002159intel_finish_fb(struct drm_framebuffer *old_fb)
2160{
2161 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2162 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2163 bool was_interruptible = dev_priv->mm.interruptible;
2164 int ret;
2165
Chris Wilson14667a42012-04-03 17:58:35 +01002166 /* Big Hammer, we also need to ensure that any pending
2167 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2168 * current scanout is retired before unpinning the old
2169 * framebuffer.
2170 *
2171 * This should only fail upon a hung GPU, in which case we
2172 * can safely continue.
2173 */
2174 dev_priv->mm.interruptible = false;
2175 ret = i915_gem_object_finish_gpu(obj);
2176 dev_priv->mm.interruptible = was_interruptible;
2177
2178 return ret;
2179}
2180
Ville Syrjälä198598d2012-10-31 17:50:24 +02002181static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
2182{
2183 struct drm_device *dev = crtc->dev;
2184 struct drm_i915_master_private *master_priv;
2185 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2186
2187 if (!dev->primary->master)
2188 return;
2189
2190 master_priv = dev->primary->master->driver_priv;
2191 if (!master_priv->sarea_priv)
2192 return;
2193
2194 switch (intel_crtc->pipe) {
2195 case 0:
2196 master_priv->sarea_priv->pipeA_x = x;
2197 master_priv->sarea_priv->pipeA_y = y;
2198 break;
2199 case 1:
2200 master_priv->sarea_priv->pipeB_x = x;
2201 master_priv->sarea_priv->pipeB_y = y;
2202 break;
2203 default:
2204 break;
2205 }
2206}
2207
Chris Wilson14667a42012-04-03 17:58:35 +01002208static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002209intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002210 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002211{
2212 struct drm_device *dev = crtc->dev;
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002213 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08002214 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter94352cf2012-07-05 22:51:56 +02002215 struct drm_framebuffer *old_fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002216 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002217
2218 /* no fb bound */
Daniel Vetter94352cf2012-07-05 22:51:56 +02002219 if (!fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002220 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002221 return 0;
2222 }
2223
Ben Widawsky7eb552a2013-03-13 14:05:41 -07002224 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002225 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2226 plane_name(intel_crtc->plane),
2227 INTEL_INFO(dev)->num_pipes);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002228 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002229 }
2230
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002231 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01002232 ret = intel_pin_and_fence_fb_obj(dev,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002233 to_intel_framebuffer(fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002234 NULL);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002235 if (ret != 0) {
2236 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002237 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002238 return ret;
2239 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002240
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002241 /* Update pipe size and adjust fitter if needed */
2242 if (i915_fastboot) {
2243 I915_WRITE(PIPESRC(intel_crtc->pipe),
2244 ((crtc->mode.hdisplay - 1) << 16) |
2245 (crtc->mode.vdisplay - 1));
2246 if (!intel_crtc->config.pch_pfit.size &&
2247 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
2248 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2249 I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
2250 I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
2251 I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
2252 }
2253 }
2254
Daniel Vetter94352cf2012-07-05 22:51:56 +02002255 ret = dev_priv->display.update_plane(crtc, fb, x, y);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002256 if (ret) {
Daniel Vetter94352cf2012-07-05 22:51:56 +02002257 intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002258 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002259 DRM_ERROR("failed to update base address\n");
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002260 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002261 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002262
Daniel Vetter94352cf2012-07-05 22:51:56 +02002263 old_fb = crtc->fb;
2264 crtc->fb = fb;
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02002265 crtc->x = x;
2266 crtc->y = y;
Daniel Vetter94352cf2012-07-05 22:51:56 +02002267
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002268 if (old_fb) {
Daniel Vetterd7697ee2013-06-02 17:23:01 +02002269 if (intel_crtc->active && old_fb != fb)
2270 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002271 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002272 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002273
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002274 intel_update_fbc(dev);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002275 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002276
Ville Syrjälä198598d2012-10-31 17:50:24 +02002277 intel_crtc_update_sarea_pos(crtc, x, y);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002278
2279 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002280}
2281
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002282static void intel_fdi_normal_train(struct drm_crtc *crtc)
2283{
2284 struct drm_device *dev = crtc->dev;
2285 struct drm_i915_private *dev_priv = dev->dev_private;
2286 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2287 int pipe = intel_crtc->pipe;
2288 u32 reg, temp;
2289
2290 /* enable normal train */
2291 reg = FDI_TX_CTL(pipe);
2292 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002293 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002294 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2295 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002296 } else {
2297 temp &= ~FDI_LINK_TRAIN_NONE;
2298 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002299 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002300 I915_WRITE(reg, temp);
2301
2302 reg = FDI_RX_CTL(pipe);
2303 temp = I915_READ(reg);
2304 if (HAS_PCH_CPT(dev)) {
2305 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2306 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2307 } else {
2308 temp &= ~FDI_LINK_TRAIN_NONE;
2309 temp |= FDI_LINK_TRAIN_NONE;
2310 }
2311 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2312
2313 /* wait one idle pattern time */
2314 POSTING_READ(reg);
2315 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002316
2317 /* IVB wants error correction enabled */
2318 if (IS_IVYBRIDGE(dev))
2319 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2320 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002321}
2322
Daniel Vetter1e833f42013-02-19 22:31:57 +01002323static bool pipe_has_enabled_pch(struct intel_crtc *intel_crtc)
2324{
2325 return intel_crtc->base.enabled && intel_crtc->config.has_pch_encoder;
2326}
2327
Daniel Vetter01a415f2012-10-27 15:58:40 +02002328static void ivb_modeset_global_resources(struct drm_device *dev)
2329{
2330 struct drm_i915_private *dev_priv = dev->dev_private;
2331 struct intel_crtc *pipe_B_crtc =
2332 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2333 struct intel_crtc *pipe_C_crtc =
2334 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2335 uint32_t temp;
2336
Daniel Vetter1e833f42013-02-19 22:31:57 +01002337 /*
2338 * When everything is off disable fdi C so that we could enable fdi B
2339 * with all lanes. Note that we don't care about enabled pipes without
2340 * an enabled pch encoder.
2341 */
2342 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2343 !pipe_has_enabled_pch(pipe_C_crtc)) {
Daniel Vetter01a415f2012-10-27 15:58:40 +02002344 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2345 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2346
2347 temp = I915_READ(SOUTH_CHICKEN1);
2348 temp &= ~FDI_BC_BIFURCATION_SELECT;
2349 DRM_DEBUG_KMS("disabling fdi C rx\n");
2350 I915_WRITE(SOUTH_CHICKEN1, temp);
2351 }
2352}
2353
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002354/* The FDI link training functions for ILK/Ibexpeak. */
2355static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2356{
2357 struct drm_device *dev = crtc->dev;
2358 struct drm_i915_private *dev_priv = dev->dev_private;
2359 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2360 int pipe = intel_crtc->pipe;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002361 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002362 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002363
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002364 /* FDI needs bits from pipe & plane first */
2365 assert_pipe_enabled(dev_priv, pipe);
2366 assert_plane_enabled(dev_priv, plane);
2367
Adam Jacksone1a44742010-06-25 15:32:14 -04002368 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2369 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002370 reg = FDI_RX_IMR(pipe);
2371 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002372 temp &= ~FDI_RX_SYMBOL_LOCK;
2373 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002374 I915_WRITE(reg, temp);
2375 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002376 udelay(150);
2377
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002378 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002379 reg = FDI_TX_CTL(pipe);
2380 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002381 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2382 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002383 temp &= ~FDI_LINK_TRAIN_NONE;
2384 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002385 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002386
Chris Wilson5eddb702010-09-11 13:48:45 +01002387 reg = FDI_RX_CTL(pipe);
2388 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002389 temp &= ~FDI_LINK_TRAIN_NONE;
2390 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002391 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2392
2393 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002394 udelay(150);
2395
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002396 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002397 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2398 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2399 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002400
Chris Wilson5eddb702010-09-11 13:48:45 +01002401 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002402 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002403 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002404 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2405
2406 if ((temp & FDI_RX_BIT_LOCK)) {
2407 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002408 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002409 break;
2410 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002411 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002412 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002413 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002414
2415 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002416 reg = FDI_TX_CTL(pipe);
2417 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002418 temp &= ~FDI_LINK_TRAIN_NONE;
2419 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002420 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002421
Chris Wilson5eddb702010-09-11 13:48:45 +01002422 reg = FDI_RX_CTL(pipe);
2423 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002424 temp &= ~FDI_LINK_TRAIN_NONE;
2425 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002426 I915_WRITE(reg, temp);
2427
2428 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002429 udelay(150);
2430
Chris Wilson5eddb702010-09-11 13:48:45 +01002431 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002432 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002433 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002434 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2435
2436 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002437 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002438 DRM_DEBUG_KMS("FDI train 2 done.\n");
2439 break;
2440 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002441 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002442 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002443 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002444
2445 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002446
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002447}
2448
Akshay Joshi0206e352011-08-16 15:34:10 -04002449static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002450 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2451 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2452 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2453 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2454};
2455
2456/* The FDI link training functions for SNB/Cougarpoint. */
2457static void gen6_fdi_link_train(struct drm_crtc *crtc)
2458{
2459 struct drm_device *dev = crtc->dev;
2460 struct drm_i915_private *dev_priv = dev->dev_private;
2461 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2462 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05002463 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002464
Adam Jacksone1a44742010-06-25 15:32:14 -04002465 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2466 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002467 reg = FDI_RX_IMR(pipe);
2468 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002469 temp &= ~FDI_RX_SYMBOL_LOCK;
2470 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002471 I915_WRITE(reg, temp);
2472
2473 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002474 udelay(150);
2475
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002476 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002477 reg = FDI_TX_CTL(pipe);
2478 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002479 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2480 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002481 temp &= ~FDI_LINK_TRAIN_NONE;
2482 temp |= FDI_LINK_TRAIN_PATTERN_1;
2483 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2484 /* SNB-B */
2485 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002486 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002487
Daniel Vetterd74cf322012-10-26 10:58:13 +02002488 I915_WRITE(FDI_RX_MISC(pipe),
2489 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2490
Chris Wilson5eddb702010-09-11 13:48:45 +01002491 reg = FDI_RX_CTL(pipe);
2492 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002493 if (HAS_PCH_CPT(dev)) {
2494 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2495 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2496 } else {
2497 temp &= ~FDI_LINK_TRAIN_NONE;
2498 temp |= FDI_LINK_TRAIN_PATTERN_1;
2499 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002500 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2501
2502 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002503 udelay(150);
2504
Akshay Joshi0206e352011-08-16 15:34:10 -04002505 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002506 reg = FDI_TX_CTL(pipe);
2507 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002508 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2509 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002510 I915_WRITE(reg, temp);
2511
2512 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002513 udelay(500);
2514
Sean Paulfa37d392012-03-02 12:53:39 -05002515 for (retry = 0; retry < 5; retry++) {
2516 reg = FDI_RX_IIR(pipe);
2517 temp = I915_READ(reg);
2518 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2519 if (temp & FDI_RX_BIT_LOCK) {
2520 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2521 DRM_DEBUG_KMS("FDI train 1 done.\n");
2522 break;
2523 }
2524 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002525 }
Sean Paulfa37d392012-03-02 12:53:39 -05002526 if (retry < 5)
2527 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002528 }
2529 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002530 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002531
2532 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002533 reg = FDI_TX_CTL(pipe);
2534 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002535 temp &= ~FDI_LINK_TRAIN_NONE;
2536 temp |= FDI_LINK_TRAIN_PATTERN_2;
2537 if (IS_GEN6(dev)) {
2538 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2539 /* SNB-B */
2540 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2541 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002542 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002543
Chris Wilson5eddb702010-09-11 13:48:45 +01002544 reg = FDI_RX_CTL(pipe);
2545 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002546 if (HAS_PCH_CPT(dev)) {
2547 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2548 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2549 } else {
2550 temp &= ~FDI_LINK_TRAIN_NONE;
2551 temp |= FDI_LINK_TRAIN_PATTERN_2;
2552 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002553 I915_WRITE(reg, temp);
2554
2555 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002556 udelay(150);
2557
Akshay Joshi0206e352011-08-16 15:34:10 -04002558 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002559 reg = FDI_TX_CTL(pipe);
2560 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002561 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2562 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002563 I915_WRITE(reg, temp);
2564
2565 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002566 udelay(500);
2567
Sean Paulfa37d392012-03-02 12:53:39 -05002568 for (retry = 0; retry < 5; retry++) {
2569 reg = FDI_RX_IIR(pipe);
2570 temp = I915_READ(reg);
2571 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2572 if (temp & FDI_RX_SYMBOL_LOCK) {
2573 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2574 DRM_DEBUG_KMS("FDI train 2 done.\n");
2575 break;
2576 }
2577 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002578 }
Sean Paulfa37d392012-03-02 12:53:39 -05002579 if (retry < 5)
2580 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002581 }
2582 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002583 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002584
2585 DRM_DEBUG_KMS("FDI train done.\n");
2586}
2587
Jesse Barnes357555c2011-04-28 15:09:55 -07002588/* Manual link training for Ivy Bridge A0 parts */
2589static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2590{
2591 struct drm_device *dev = crtc->dev;
2592 struct drm_i915_private *dev_priv = dev->dev_private;
2593 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2594 int pipe = intel_crtc->pipe;
2595 u32 reg, temp, i;
2596
2597 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2598 for train result */
2599 reg = FDI_RX_IMR(pipe);
2600 temp = I915_READ(reg);
2601 temp &= ~FDI_RX_SYMBOL_LOCK;
2602 temp &= ~FDI_RX_BIT_LOCK;
2603 I915_WRITE(reg, temp);
2604
2605 POSTING_READ(reg);
2606 udelay(150);
2607
Daniel Vetter01a415f2012-10-27 15:58:40 +02002608 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
2609 I915_READ(FDI_RX_IIR(pipe)));
2610
Jesse Barnes357555c2011-04-28 15:09:55 -07002611 /* enable CPU FDI TX and PCH FDI RX */
2612 reg = FDI_TX_CTL(pipe);
2613 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002614 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2615 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Jesse Barnes357555c2011-04-28 15:09:55 -07002616 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2617 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
2618 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2619 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002620 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002621 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2622
Daniel Vetterd74cf322012-10-26 10:58:13 +02002623 I915_WRITE(FDI_RX_MISC(pipe),
2624 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2625
Jesse Barnes357555c2011-04-28 15:09:55 -07002626 reg = FDI_RX_CTL(pipe);
2627 temp = I915_READ(reg);
2628 temp &= ~FDI_LINK_TRAIN_AUTO;
2629 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2630 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002631 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002632 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2633
2634 POSTING_READ(reg);
2635 udelay(150);
2636
Akshay Joshi0206e352011-08-16 15:34:10 -04002637 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002638 reg = FDI_TX_CTL(pipe);
2639 temp = I915_READ(reg);
2640 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2641 temp |= snb_b_fdi_train_param[i];
2642 I915_WRITE(reg, temp);
2643
2644 POSTING_READ(reg);
2645 udelay(500);
2646
2647 reg = FDI_RX_IIR(pipe);
2648 temp = I915_READ(reg);
2649 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2650
2651 if (temp & FDI_RX_BIT_LOCK ||
2652 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2653 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Daniel Vetter01a415f2012-10-27 15:58:40 +02002654 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n", i);
Jesse Barnes357555c2011-04-28 15:09:55 -07002655 break;
2656 }
2657 }
2658 if (i == 4)
2659 DRM_ERROR("FDI train 1 fail!\n");
2660
2661 /* Train 2 */
2662 reg = FDI_TX_CTL(pipe);
2663 temp = I915_READ(reg);
2664 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2665 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2666 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2667 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2668 I915_WRITE(reg, temp);
2669
2670 reg = FDI_RX_CTL(pipe);
2671 temp = I915_READ(reg);
2672 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2673 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2674 I915_WRITE(reg, temp);
2675
2676 POSTING_READ(reg);
2677 udelay(150);
2678
Akshay Joshi0206e352011-08-16 15:34:10 -04002679 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002680 reg = FDI_TX_CTL(pipe);
2681 temp = I915_READ(reg);
2682 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2683 temp |= snb_b_fdi_train_param[i];
2684 I915_WRITE(reg, temp);
2685
2686 POSTING_READ(reg);
2687 udelay(500);
2688
2689 reg = FDI_RX_IIR(pipe);
2690 temp = I915_READ(reg);
2691 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2692
2693 if (temp & FDI_RX_SYMBOL_LOCK) {
2694 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Daniel Vetter01a415f2012-10-27 15:58:40 +02002695 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n", i);
Jesse Barnes357555c2011-04-28 15:09:55 -07002696 break;
2697 }
2698 }
2699 if (i == 4)
2700 DRM_ERROR("FDI train 2 fail!\n");
2701
2702 DRM_DEBUG_KMS("FDI train done.\n");
2703}
2704
Daniel Vetter88cefb62012-08-12 19:27:14 +02002705static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07002706{
Daniel Vetter88cefb62012-08-12 19:27:14 +02002707 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002708 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002709 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002710 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002711
Jesse Barnesc64e3112010-09-10 11:27:03 -07002712
Jesse Barnes0e23b992010-09-10 11:10:00 -07002713 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01002714 reg = FDI_RX_CTL(pipe);
2715 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002716 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
2717 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002718 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01002719 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2720
2721 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002722 udelay(200);
2723
2724 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002725 temp = I915_READ(reg);
2726 I915_WRITE(reg, temp | FDI_PCDCLK);
2727
2728 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002729 udelay(200);
2730
Paulo Zanoni20749732012-11-23 15:30:38 -02002731 /* Enable CPU FDI TX PLL, always on for Ironlake */
2732 reg = FDI_TX_CTL(pipe);
2733 temp = I915_READ(reg);
2734 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2735 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01002736
Paulo Zanoni20749732012-11-23 15:30:38 -02002737 POSTING_READ(reg);
2738 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002739 }
2740}
2741
Daniel Vetter88cefb62012-08-12 19:27:14 +02002742static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
2743{
2744 struct drm_device *dev = intel_crtc->base.dev;
2745 struct drm_i915_private *dev_priv = dev->dev_private;
2746 int pipe = intel_crtc->pipe;
2747 u32 reg, temp;
2748
2749 /* Switch from PCDclk to Rawclk */
2750 reg = FDI_RX_CTL(pipe);
2751 temp = I915_READ(reg);
2752 I915_WRITE(reg, temp & ~FDI_PCDCLK);
2753
2754 /* Disable CPU FDI TX PLL */
2755 reg = FDI_TX_CTL(pipe);
2756 temp = I915_READ(reg);
2757 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2758
2759 POSTING_READ(reg);
2760 udelay(100);
2761
2762 reg = FDI_RX_CTL(pipe);
2763 temp = I915_READ(reg);
2764 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2765
2766 /* Wait for the clocks to turn off. */
2767 POSTING_READ(reg);
2768 udelay(100);
2769}
2770
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002771static void ironlake_fdi_disable(struct drm_crtc *crtc)
2772{
2773 struct drm_device *dev = crtc->dev;
2774 struct drm_i915_private *dev_priv = dev->dev_private;
2775 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2776 int pipe = intel_crtc->pipe;
2777 u32 reg, temp;
2778
2779 /* disable CPU FDI tx and PCH FDI rx */
2780 reg = FDI_TX_CTL(pipe);
2781 temp = I915_READ(reg);
2782 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2783 POSTING_READ(reg);
2784
2785 reg = FDI_RX_CTL(pipe);
2786 temp = I915_READ(reg);
2787 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002788 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002789 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2790
2791 POSTING_READ(reg);
2792 udelay(100);
2793
2794 /* Ironlake workaround, disable clock pointer after downing FDI */
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002795 if (HAS_PCH_IBX(dev)) {
2796 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002797 }
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002798
2799 /* still set train pattern 1 */
2800 reg = FDI_TX_CTL(pipe);
2801 temp = I915_READ(reg);
2802 temp &= ~FDI_LINK_TRAIN_NONE;
2803 temp |= FDI_LINK_TRAIN_PATTERN_1;
2804 I915_WRITE(reg, temp);
2805
2806 reg = FDI_RX_CTL(pipe);
2807 temp = I915_READ(reg);
2808 if (HAS_PCH_CPT(dev)) {
2809 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2810 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2811 } else {
2812 temp &= ~FDI_LINK_TRAIN_NONE;
2813 temp |= FDI_LINK_TRAIN_PATTERN_1;
2814 }
2815 /* BPC in FDI rx is consistent with that in PIPECONF */
2816 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002817 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002818 I915_WRITE(reg, temp);
2819
2820 POSTING_READ(reg);
2821 udelay(100);
2822}
2823
Chris Wilson5bb61642012-09-27 21:25:58 +01002824static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2825{
2826 struct drm_device *dev = crtc->dev;
2827 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä10d83732013-01-29 18:13:34 +02002828 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5bb61642012-09-27 21:25:58 +01002829 unsigned long flags;
2830 bool pending;
2831
Ville Syrjälä10d83732013-01-29 18:13:34 +02002832 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2833 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
Chris Wilson5bb61642012-09-27 21:25:58 +01002834 return false;
2835
2836 spin_lock_irqsave(&dev->event_lock, flags);
2837 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2838 spin_unlock_irqrestore(&dev->event_lock, flags);
2839
2840 return pending;
2841}
2842
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002843static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2844{
Chris Wilson0f911282012-04-17 10:05:38 +01002845 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01002846 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002847
2848 if (crtc->fb == NULL)
2849 return;
2850
Daniel Vetter2c10d572012-12-20 21:24:07 +01002851 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
2852
Chris Wilson5bb61642012-09-27 21:25:58 +01002853 wait_event(dev_priv->pending_flip_queue,
2854 !intel_crtc_has_pending_flip(crtc));
2855
Chris Wilson0f911282012-04-17 10:05:38 +01002856 mutex_lock(&dev->struct_mutex);
2857 intel_finish_fb(crtc->fb);
2858 mutex_unlock(&dev->struct_mutex);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002859}
2860
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002861/* Program iCLKIP clock to the desired frequency */
2862static void lpt_program_iclkip(struct drm_crtc *crtc)
2863{
2864 struct drm_device *dev = crtc->dev;
2865 struct drm_i915_private *dev_priv = dev->dev_private;
2866 u32 divsel, phaseinc, auxdiv, phasedir = 0;
2867 u32 temp;
2868
Daniel Vetter09153002012-12-12 14:06:44 +01002869 mutex_lock(&dev_priv->dpio_lock);
2870
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002871 /* It is necessary to ungate the pixclk gate prior to programming
2872 * the divisors, and gate it back when it is done.
2873 */
2874 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
2875
2876 /* Disable SSCCTL */
2877 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002878 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
2879 SBI_SSCCTL_DISABLE,
2880 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002881
2882 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
2883 if (crtc->mode.clock == 20000) {
2884 auxdiv = 1;
2885 divsel = 0x41;
2886 phaseinc = 0x20;
2887 } else {
2888 /* The iCLK virtual clock root frequency is in MHz,
2889 * but the crtc->mode.clock in in KHz. To get the divisors,
2890 * it is necessary to divide one by another, so we
2891 * convert the virtual clock precision to KHz here for higher
2892 * precision.
2893 */
2894 u32 iclk_virtual_root_freq = 172800 * 1000;
2895 u32 iclk_pi_range = 64;
2896 u32 desired_divisor, msb_divisor_value, pi_value;
2897
2898 desired_divisor = (iclk_virtual_root_freq / crtc->mode.clock);
2899 msb_divisor_value = desired_divisor / iclk_pi_range;
2900 pi_value = desired_divisor % iclk_pi_range;
2901
2902 auxdiv = 0;
2903 divsel = msb_divisor_value - 2;
2904 phaseinc = pi_value;
2905 }
2906
2907 /* This should not happen with any sane values */
2908 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
2909 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
2910 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
2911 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
2912
2913 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
2914 crtc->mode.clock,
2915 auxdiv,
2916 divsel,
2917 phasedir,
2918 phaseinc);
2919
2920 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002921 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002922 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
2923 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
2924 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
2925 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
2926 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
2927 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002928 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002929
2930 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002931 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002932 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
2933 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002934 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002935
2936 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002937 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002938 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002939 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002940
2941 /* Wait for initialization time */
2942 udelay(24);
2943
2944 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01002945
2946 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002947}
2948
Daniel Vetter275f01b22013-05-03 11:49:47 +02002949static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
2950 enum pipe pch_transcoder)
2951{
2952 struct drm_device *dev = crtc->base.dev;
2953 struct drm_i915_private *dev_priv = dev->dev_private;
2954 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
2955
2956 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
2957 I915_READ(HTOTAL(cpu_transcoder)));
2958 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
2959 I915_READ(HBLANK(cpu_transcoder)));
2960 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
2961 I915_READ(HSYNC(cpu_transcoder)));
2962
2963 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
2964 I915_READ(VTOTAL(cpu_transcoder)));
2965 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
2966 I915_READ(VBLANK(cpu_transcoder)));
2967 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
2968 I915_READ(VSYNC(cpu_transcoder)));
2969 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
2970 I915_READ(VSYNCSHIFT(cpu_transcoder)));
2971}
2972
Jesse Barnesf67a5592011-01-05 10:31:48 -08002973/*
2974 * Enable PCH resources required for PCH ports:
2975 * - PCH PLLs
2976 * - FDI training & RX/TX
2977 * - update transcoder timings
2978 * - DP transcoding bits
2979 * - transcoder
2980 */
2981static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08002982{
2983 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002984 struct drm_i915_private *dev_priv = dev->dev_private;
2985 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2986 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01002987 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07002988
Daniel Vetterab9412b2013-05-03 11:49:46 +02002989 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01002990
Daniel Vettercd986ab2012-10-26 10:58:12 +02002991 /* Write the TU size bits before fdi link training, so that error
2992 * detection works. */
2993 I915_WRITE(FDI_RX_TUSIZE1(pipe),
2994 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
2995
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002996 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07002997 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002998
Daniel Vetter572deb32012-10-27 18:46:14 +02002999 /* XXX: pch pll's can be enabled any time before we enable the PCH
3000 * transcoder, and we actually should do this to not upset any PCH
3001 * transcoder that already use the clock when we share it.
3002 *
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003003 * Note that enable_shared_dpll tries to do the right thing, but
3004 * get_shared_dpll unconditionally resets the pll - we need that to have
3005 * the right LVDS enable sequence. */
3006 ironlake_enable_shared_dpll(intel_crtc);
Chris Wilson6f13b7b2012-05-13 09:54:09 +01003007
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003008 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003009 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07003010
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003011 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003012 temp |= TRANS_DPLL_ENABLE(pipe);
3013 sel = TRANS_DPLLB_SEL(pipe);
Daniel Vettera43f6e02013-06-07 23:10:32 +02003014 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003015 temp |= sel;
3016 else
3017 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003018 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003019 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003020
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003021 /* set transcoder timing, panel must allow it */
3022 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02003023 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003024
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003025 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003026
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003027 /* For PCH DP, enable TRANS_DP_CTL */
3028 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07003029 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3030 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003031 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003032 reg = TRANS_DP_CTL(pipe);
3033 temp = I915_READ(reg);
3034 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003035 TRANS_DP_SYNC_MASK |
3036 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003037 temp |= (TRANS_DP_OUTPUT_ENABLE |
3038 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003039 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003040
3041 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003042 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003043 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003044 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003045
3046 switch (intel_trans_dp_port_sel(crtc)) {
3047 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003048 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003049 break;
3050 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003051 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003052 break;
3053 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003054 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003055 break;
3056 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003057 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003058 }
3059
Chris Wilson5eddb702010-09-11 13:48:45 +01003060 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003061 }
3062
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003063 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003064}
3065
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003066static void lpt_pch_enable(struct drm_crtc *crtc)
3067{
3068 struct drm_device *dev = crtc->dev;
3069 struct drm_i915_private *dev_priv = dev->dev_private;
3070 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02003071 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003072
Daniel Vetterab9412b2013-05-03 11:49:46 +02003073 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003074
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003075 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003076
Paulo Zanoni0540e482012-10-31 18:12:40 -02003077 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02003078 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003079
Paulo Zanoni937bb612012-10-31 18:12:47 -02003080 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003081}
3082
Daniel Vettere2b78262013-06-07 23:10:03 +02003083static void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003084{
Daniel Vettere2b78262013-06-07 23:10:03 +02003085 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003086
3087 if (pll == NULL)
3088 return;
3089
3090 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003091 WARN(1, "bad %s refcount\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003092 return;
3093 }
3094
Daniel Vetterf4a091c2013-06-10 17:28:22 +02003095 if (--pll->refcount == 0) {
3096 WARN_ON(pll->on);
3097 WARN_ON(pll->active);
3098 }
3099
Daniel Vettera43f6e02013-06-07 23:10:32 +02003100 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003101}
3102
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003103static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003104{
Daniel Vettere2b78262013-06-07 23:10:03 +02003105 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3106 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3107 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003108
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003109 if (pll) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003110 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3111 crtc->base.base.id, pll->name);
Daniel Vettere2b78262013-06-07 23:10:03 +02003112 intel_put_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003113 }
3114
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003115 if (HAS_PCH_IBX(dev_priv->dev)) {
3116 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02003117 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003118 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003119
Daniel Vetter46edb022013-06-05 13:34:12 +02003120 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3121 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003122
3123 goto found;
3124 }
3125
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003126 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3127 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003128
3129 /* Only want to check enabled timings first */
3130 if (pll->refcount == 0)
3131 continue;
3132
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003133 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3134 sizeof(pll->hw_state)) == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003135 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
Daniel Vettere2b78262013-06-07 23:10:03 +02003136 crtc->base.base.id,
Daniel Vetter46edb022013-06-05 13:34:12 +02003137 pll->name, pll->refcount, pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003138
3139 goto found;
3140 }
3141 }
3142
3143 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003144 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3145 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003146 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003147 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3148 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003149 goto found;
3150 }
3151 }
3152
3153 return NULL;
3154
3155found:
Daniel Vettera43f6e02013-06-07 23:10:32 +02003156 crtc->config.shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02003157 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3158 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02003159
Daniel Vettercdbd2312013-06-05 13:34:03 +02003160 if (pll->active == 0) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02003161 memcpy(&pll->hw_state, &crtc->config.dpll_hw_state,
3162 sizeof(pll->hw_state));
3163
Daniel Vetter46edb022013-06-05 13:34:12 +02003164 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
Daniel Vettercdbd2312013-06-05 13:34:03 +02003165 WARN_ON(pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02003166 assert_shared_dpll_disabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003167
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02003168 pll->mode_set(dev_priv, pll);
Daniel Vettercdbd2312013-06-05 13:34:03 +02003169 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003170 pll->refcount++;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003171
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003172 return pll;
3173}
3174
Daniel Vettera1520312013-05-03 11:49:50 +02003175static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07003176{
3177 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01003178 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003179 u32 temp;
3180
3181 temp = I915_READ(dslreg);
3182 udelay(500);
3183 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07003184 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03003185 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07003186 }
3187}
3188
Jesse Barnesb074cec2013-04-25 12:55:02 -07003189static void ironlake_pfit_enable(struct intel_crtc *crtc)
3190{
3191 struct drm_device *dev = crtc->base.dev;
3192 struct drm_i915_private *dev_priv = dev->dev_private;
3193 int pipe = crtc->pipe;
3194
Jesse Barnes0ef37f32013-05-03 13:26:37 -07003195 if (crtc->config.pch_pfit.size) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07003196 /* Force use of hard-coded filter coefficients
3197 * as some pre-programmed values are broken,
3198 * e.g. x201.
3199 */
3200 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3201 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3202 PF_PIPE_SEL_IVB(pipe));
3203 else
3204 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3205 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3206 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08003207 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003208}
3209
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003210static void intel_enable_planes(struct drm_crtc *crtc)
3211{
3212 struct drm_device *dev = crtc->dev;
3213 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3214 struct intel_plane *intel_plane;
3215
3216 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3217 if (intel_plane->pipe == pipe)
3218 intel_plane_restore(&intel_plane->base);
3219}
3220
3221static void intel_disable_planes(struct drm_crtc *crtc)
3222{
3223 struct drm_device *dev = crtc->dev;
3224 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3225 struct intel_plane *intel_plane;
3226
3227 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3228 if (intel_plane->pipe == pipe)
3229 intel_plane_disable(&intel_plane->base);
3230}
3231
Jesse Barnesf67a5592011-01-05 10:31:48 -08003232static void ironlake_crtc_enable(struct drm_crtc *crtc)
3233{
3234 struct drm_device *dev = crtc->dev;
3235 struct drm_i915_private *dev_priv = dev->dev_private;
3236 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003237 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003238 int pipe = intel_crtc->pipe;
3239 int plane = intel_crtc->plane;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003240
Daniel Vetter08a48462012-07-02 11:43:47 +02003241 WARN_ON(!crtc->enabled);
3242
Jesse Barnesf67a5592011-01-05 10:31:48 -08003243 if (intel_crtc->active)
3244 return;
3245
3246 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003247
3248 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3249 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
3250
Jesse Barnesf67a5592011-01-05 10:31:48 -08003251 intel_update_watermarks(dev);
3252
Daniel Vetterf6736a12013-06-05 13:34:30 +02003253 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02003254 if (encoder->pre_enable)
3255 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003256
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003257 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02003258 /* Note: FDI PLL enabling _must_ be done before we enable the
3259 * cpu pipes, hence this is separate from all the other fdi/pch
3260 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02003261 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02003262 } else {
3263 assert_fdi_tx_disabled(dev_priv, pipe);
3264 assert_fdi_rx_disabled(dev_priv, pipe);
3265 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003266
Jesse Barnesb074cec2013-04-25 12:55:02 -07003267 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003268
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02003269 /*
3270 * On ILK+ LUT must be loaded before the pipe is running but with
3271 * clocks enabled
3272 */
3273 intel_crtc_load_lut(crtc);
3274
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003275 intel_enable_pipe(dev_priv, pipe,
3276 intel_crtc->config.has_pch_encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003277 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003278 intel_enable_planes(crtc);
Ville Syrjälä5c38d482013-06-04 13:49:00 +03003279 intel_crtc_update_cursor(crtc, true);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003280
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003281 if (intel_crtc->config.has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08003282 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003283
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003284 mutex_lock(&dev->struct_mutex);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003285 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003286 mutex_unlock(&dev->struct_mutex);
3287
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003288 for_each_encoder_on_crtc(dev, crtc, encoder)
3289 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02003290
3291 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02003292 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02003293
3294 /*
3295 * There seems to be a race in PCH platform hw (at least on some
3296 * outputs) where an enabled pipe still completes any pageflip right
3297 * away (as if the pipe is off) instead of waiting for vblank. As soon
3298 * as the first vblank happend, everything works as expected. Hence just
3299 * wait for one vblank before returning to avoid strange things
3300 * happening.
3301 */
3302 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003303}
3304
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003305/* IPS only exists on ULT machines and is tied to pipe A. */
3306static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
3307{
Damien Lespiauf5adf942013-06-24 18:29:34 +01003308 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003309}
3310
3311static void hsw_enable_ips(struct intel_crtc *crtc)
3312{
3313 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3314
3315 if (!crtc->config.ips_enabled)
3316 return;
3317
3318 /* We can only enable IPS after we enable a plane and wait for a vblank.
3319 * We guarantee that the plane is enabled by calling intel_enable_ips
3320 * only after intel_enable_plane. And intel_enable_plane already waits
3321 * for a vblank, so all we need to do here is to enable the IPS bit. */
3322 assert_plane_enabled(dev_priv, crtc->plane);
3323 I915_WRITE(IPS_CTL, IPS_ENABLE);
3324}
3325
3326static void hsw_disable_ips(struct intel_crtc *crtc)
3327{
3328 struct drm_device *dev = crtc->base.dev;
3329 struct drm_i915_private *dev_priv = dev->dev_private;
3330
3331 if (!crtc->config.ips_enabled)
3332 return;
3333
3334 assert_plane_enabled(dev_priv, crtc->plane);
3335 I915_WRITE(IPS_CTL, 0);
3336
3337 /* We need to wait for a vblank before we can disable the plane. */
3338 intel_wait_for_vblank(dev, crtc->pipe);
3339}
3340
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003341static void haswell_crtc_enable(struct drm_crtc *crtc)
3342{
3343 struct drm_device *dev = crtc->dev;
3344 struct drm_i915_private *dev_priv = dev->dev_private;
3345 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3346 struct intel_encoder *encoder;
3347 int pipe = intel_crtc->pipe;
3348 int plane = intel_crtc->plane;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003349
3350 WARN_ON(!crtc->enabled);
3351
3352 if (intel_crtc->active)
3353 return;
3354
3355 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003356
3357 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3358 if (intel_crtc->config.has_pch_encoder)
3359 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
3360
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003361 intel_update_watermarks(dev);
3362
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003363 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni04945642012-11-01 21:00:59 -02003364 dev_priv->display.fdi_link_train(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003365
3366 for_each_encoder_on_crtc(dev, crtc, encoder)
3367 if (encoder->pre_enable)
3368 encoder->pre_enable(encoder);
3369
Paulo Zanoni1f544382012-10-24 11:32:00 -02003370 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003371
Jesse Barnesb074cec2013-04-25 12:55:02 -07003372 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003373
3374 /*
3375 * On ILK+ LUT must be loaded before the pipe is running but with
3376 * clocks enabled
3377 */
3378 intel_crtc_load_lut(crtc);
3379
Paulo Zanoni1f544382012-10-24 11:32:00 -02003380 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00003381 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003382
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003383 intel_enable_pipe(dev_priv, pipe,
3384 intel_crtc->config.has_pch_encoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003385 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003386 intel_enable_planes(crtc);
Ville Syrjälä5c38d482013-06-04 13:49:00 +03003387 intel_crtc_update_cursor(crtc, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003388
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003389 hsw_enable_ips(intel_crtc);
3390
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003391 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003392 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003393
3394 mutex_lock(&dev->struct_mutex);
3395 intel_update_fbc(dev);
3396 mutex_unlock(&dev->struct_mutex);
3397
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003398 for_each_encoder_on_crtc(dev, crtc, encoder)
3399 encoder->enable(encoder);
3400
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003401 /*
3402 * There seems to be a race in PCH platform hw (at least on some
3403 * outputs) where an enabled pipe still completes any pageflip right
3404 * away (as if the pipe is off) instead of waiting for vblank. As soon
3405 * as the first vblank happend, everything works as expected. Hence just
3406 * wait for one vblank before returning to avoid strange things
3407 * happening.
3408 */
3409 intel_wait_for_vblank(dev, intel_crtc->pipe);
3410}
3411
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003412static void ironlake_pfit_disable(struct intel_crtc *crtc)
3413{
3414 struct drm_device *dev = crtc->base.dev;
3415 struct drm_i915_private *dev_priv = dev->dev_private;
3416 int pipe = crtc->pipe;
3417
3418 /* To avoid upsetting the power well on haswell only disable the pfit if
3419 * it's in use. The hw state code will make sure we get this right. */
3420 if (crtc->config.pch_pfit.size) {
3421 I915_WRITE(PF_CTL(pipe), 0);
3422 I915_WRITE(PF_WIN_POS(pipe), 0);
3423 I915_WRITE(PF_WIN_SZ(pipe), 0);
3424 }
3425}
3426
Jesse Barnes6be4a602010-09-10 10:26:01 -07003427static void ironlake_crtc_disable(struct drm_crtc *crtc)
3428{
3429 struct drm_device *dev = crtc->dev;
3430 struct drm_i915_private *dev_priv = dev->dev_private;
3431 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003432 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003433 int pipe = intel_crtc->pipe;
3434 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01003435 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003436
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003437
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003438 if (!intel_crtc->active)
3439 return;
3440
Daniel Vetterea9d7582012-07-10 10:42:52 +02003441 for_each_encoder_on_crtc(dev, crtc, encoder)
3442 encoder->disable(encoder);
3443
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003444 intel_crtc_wait_for_pending_flips(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003445 drm_vblank_off(dev, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003446
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07003447 if (dev_priv->fbc.plane == plane)
Chris Wilson973d04f2011-07-08 12:22:37 +01003448 intel_disable_fbc(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003449
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003450 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003451 intel_disable_planes(crtc);
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003452 intel_disable_plane(dev_priv, plane, pipe);
3453
Daniel Vetterd925c592013-06-05 13:34:04 +02003454 if (intel_crtc->config.has_pch_encoder)
3455 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
3456
Jesse Barnesb24e7172011-01-04 15:09:30 -08003457 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003458
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003459 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003460
Daniel Vetterbf49ec82012-09-06 22:15:40 +02003461 for_each_encoder_on_crtc(dev, crtc, encoder)
3462 if (encoder->post_disable)
3463 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003464
Daniel Vetterd925c592013-06-05 13:34:04 +02003465 if (intel_crtc->config.has_pch_encoder) {
3466 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003467
Daniel Vetterd925c592013-06-05 13:34:04 +02003468 ironlake_disable_pch_transcoder(dev_priv, pipe);
3469 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003470
Daniel Vetterd925c592013-06-05 13:34:04 +02003471 if (HAS_PCH_CPT(dev)) {
3472 /* disable TRANS_DP_CTL */
3473 reg = TRANS_DP_CTL(pipe);
3474 temp = I915_READ(reg);
3475 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
3476 TRANS_DP_PORT_SEL_MASK);
3477 temp |= TRANS_DP_PORT_SEL_NONE;
3478 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003479
Daniel Vetterd925c592013-06-05 13:34:04 +02003480 /* disable DPLL_SEL */
3481 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003482 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02003483 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003484 }
Daniel Vetterd925c592013-06-05 13:34:04 +02003485
3486 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003487 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02003488
3489 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003490 }
3491
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003492 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003493 intel_update_watermarks(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003494
3495 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01003496 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003497 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003498}
3499
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003500static void haswell_crtc_disable(struct drm_crtc *crtc)
3501{
3502 struct drm_device *dev = crtc->dev;
3503 struct drm_i915_private *dev_priv = dev->dev_private;
3504 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3505 struct intel_encoder *encoder;
3506 int pipe = intel_crtc->pipe;
3507 int plane = intel_crtc->plane;
Daniel Vetter3b117c82013-04-17 20:15:07 +02003508 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003509
3510 if (!intel_crtc->active)
3511 return;
3512
3513 for_each_encoder_on_crtc(dev, crtc, encoder)
3514 encoder->disable(encoder);
3515
3516 intel_crtc_wait_for_pending_flips(crtc);
3517 drm_vblank_off(dev, pipe);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003518
Rodrigo Vivi891348b2013-05-06 19:37:36 -03003519 /* FBC must be disabled before disabling the plane on HSW. */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07003520 if (dev_priv->fbc.plane == plane)
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003521 intel_disable_fbc(dev);
3522
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003523 hsw_disable_ips(intel_crtc);
3524
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003525 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003526 intel_disable_planes(crtc);
Rodrigo Vivi891348b2013-05-06 19:37:36 -03003527 intel_disable_plane(dev_priv, plane, pipe);
3528
Paulo Zanoni86642812013-04-12 17:57:57 -03003529 if (intel_crtc->config.has_pch_encoder)
3530 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003531 intel_disable_pipe(dev_priv, pipe);
3532
Paulo Zanoniad80a812012-10-24 16:06:19 -02003533 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003534
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003535 ironlake_pfit_disable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003536
Paulo Zanoni1f544382012-10-24 11:32:00 -02003537 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003538
3539 for_each_encoder_on_crtc(dev, crtc, encoder)
3540 if (encoder->post_disable)
3541 encoder->post_disable(encoder);
3542
Daniel Vetter88adfff2013-03-28 10:42:01 +01003543 if (intel_crtc->config.has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02003544 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni86642812013-04-12 17:57:57 -03003545 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02003546 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02003547 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003548
3549 intel_crtc->active = false;
3550 intel_update_watermarks(dev);
3551
3552 mutex_lock(&dev->struct_mutex);
3553 intel_update_fbc(dev);
3554 mutex_unlock(&dev->struct_mutex);
3555}
3556
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003557static void ironlake_crtc_off(struct drm_crtc *crtc)
3558{
3559 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003560 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003561}
3562
Paulo Zanoni6441ab52012-10-05 12:05:58 -03003563static void haswell_crtc_off(struct drm_crtc *crtc)
3564{
3565 intel_ddi_put_crtc_pll(crtc);
3566}
3567
Daniel Vetter02e792f2009-09-15 22:57:34 +02003568static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3569{
Daniel Vetter02e792f2009-09-15 22:57:34 +02003570 if (!enable && intel_crtc->overlay) {
Chris Wilson23f09ce2010-08-12 13:53:37 +01003571 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00003572 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02003573
Chris Wilson23f09ce2010-08-12 13:53:37 +01003574 mutex_lock(&dev->struct_mutex);
Chris Wilsonce453d82011-02-21 14:43:56 +00003575 dev_priv->mm.interruptible = false;
3576 (void) intel_overlay_switch_off(intel_crtc->overlay);
3577 dev_priv->mm.interruptible = true;
Chris Wilson23f09ce2010-08-12 13:53:37 +01003578 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02003579 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02003580
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01003581 /* Let userspace switch the overlay on again. In most cases userspace
3582 * has to recompute where to put it anyway.
3583 */
Daniel Vetter02e792f2009-09-15 22:57:34 +02003584}
3585
Egbert Eich61bc95c2013-03-04 09:24:38 -05003586/**
3587 * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
3588 * cursor plane briefly if not already running after enabling the display
3589 * plane.
3590 * This workaround avoids occasional blank screens when self refresh is
3591 * enabled.
3592 */
3593static void
3594g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
3595{
3596 u32 cntl = I915_READ(CURCNTR(pipe));
3597
3598 if ((cntl & CURSOR_MODE) == 0) {
3599 u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
3600
3601 I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
3602 I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
3603 intel_wait_for_vblank(dev_priv->dev, pipe);
3604 I915_WRITE(CURCNTR(pipe), cntl);
3605 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3606 I915_WRITE(FW_BLC_SELF, fw_bcl_self);
3607 }
3608}
3609
Jesse Barnes2dd24552013-04-25 12:55:01 -07003610static void i9xx_pfit_enable(struct intel_crtc *crtc)
3611{
3612 struct drm_device *dev = crtc->base.dev;
3613 struct drm_i915_private *dev_priv = dev->dev_private;
3614 struct intel_crtc_config *pipe_config = &crtc->config;
3615
Daniel Vetter328d8e82013-05-08 10:36:31 +02003616 if (!crtc->config.gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07003617 return;
3618
Daniel Vetterc0b03412013-05-28 12:05:54 +02003619 /*
3620 * The panel fitter should only be adjusted whilst the pipe is disabled,
3621 * according to register description and PRM.
3622 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07003623 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
3624 assert_pipe_disabled(dev_priv, crtc->pipe);
3625
Jesse Barnesb074cec2013-04-25 12:55:02 -07003626 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
3627 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02003628
3629 /* Border color in case we don't scale up to the full screen. Black by
3630 * default, change to something else for debugging. */
3631 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07003632}
3633
Jesse Barnes89b667f2013-04-18 14:51:36 -07003634static void valleyview_crtc_enable(struct drm_crtc *crtc)
3635{
3636 struct drm_device *dev = crtc->dev;
3637 struct drm_i915_private *dev_priv = dev->dev_private;
3638 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3639 struct intel_encoder *encoder;
3640 int pipe = intel_crtc->pipe;
3641 int plane = intel_crtc->plane;
3642
3643 WARN_ON(!crtc->enabled);
3644
3645 if (intel_crtc->active)
3646 return;
3647
3648 intel_crtc->active = true;
3649 intel_update_watermarks(dev);
3650
3651 mutex_lock(&dev_priv->dpio_lock);
3652
3653 for_each_encoder_on_crtc(dev, crtc, encoder)
3654 if (encoder->pre_pll_enable)
3655 encoder->pre_pll_enable(encoder);
3656
Daniel Vetter87442f72013-06-06 00:52:17 +02003657 vlv_enable_pll(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07003658
3659 for_each_encoder_on_crtc(dev, crtc, encoder)
3660 if (encoder->pre_enable)
3661 encoder->pre_enable(encoder);
3662
3663 /* VLV wants encoder enabling _before_ the pipe is up. */
3664 for_each_encoder_on_crtc(dev, crtc, encoder)
3665 encoder->enable(encoder);
3666
Jesse Barnes2dd24552013-04-25 12:55:01 -07003667 i9xx_pfit_enable(intel_crtc);
3668
Ville Syrjälä63cbb072013-06-04 13:48:59 +03003669 intel_crtc_load_lut(crtc);
3670
Jesse Barnes89b667f2013-04-18 14:51:36 -07003671 intel_enable_pipe(dev_priv, pipe, false);
3672 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003673 intel_enable_planes(crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07003674 intel_crtc_update_cursor(crtc, true);
3675
Ville Syrjäläf440eb12013-06-04 13:49:01 +03003676 intel_update_fbc(dev);
3677
Jesse Barnes89b667f2013-04-18 14:51:36 -07003678 mutex_unlock(&dev_priv->dpio_lock);
3679}
3680
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003681static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003682{
3683 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08003684 struct drm_i915_private *dev_priv = dev->dev_private;
3685 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003686 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08003687 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07003688 int plane = intel_crtc->plane;
Jesse Barnes79e53942008-11-07 14:24:08 -08003689
Daniel Vetter08a48462012-07-02 11:43:47 +02003690 WARN_ON(!crtc->enabled);
3691
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003692 if (intel_crtc->active)
3693 return;
3694
3695 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01003696 intel_update_watermarks(dev);
3697
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02003698 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02003699 if (encoder->pre_enable)
3700 encoder->pre_enable(encoder);
3701
Daniel Vetterf6736a12013-06-05 13:34:30 +02003702 i9xx_enable_pll(intel_crtc);
3703
Jesse Barnes2dd24552013-04-25 12:55:01 -07003704 i9xx_pfit_enable(intel_crtc);
3705
Ville Syrjälä63cbb072013-06-04 13:48:59 +03003706 intel_crtc_load_lut(crtc);
3707
Jesse Barnes040484a2011-01-03 12:14:26 -08003708 intel_enable_pipe(dev_priv, pipe, false);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003709 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003710 intel_enable_planes(crtc);
Ville Syrjälä22e407d2013-06-07 18:52:24 +03003711 /* The fixup needs to happen before cursor is enabled */
Egbert Eich61bc95c2013-03-04 09:24:38 -05003712 if (IS_G4X(dev))
3713 g4x_fixup_plane(dev_priv, pipe);
Ville Syrjälä22e407d2013-06-07 18:52:24 +03003714 intel_crtc_update_cursor(crtc, true);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003715
3716 /* Give the overlay scaler a chance to enable if it's on this pipe */
3717 intel_crtc_dpms_overlay(intel_crtc, true);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003718
Ville Syrjäläf440eb12013-06-04 13:49:01 +03003719 intel_update_fbc(dev);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003720
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003721 for_each_encoder_on_crtc(dev, crtc, encoder)
3722 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003723}
3724
Daniel Vetter87476d62013-04-11 16:29:06 +02003725static void i9xx_pfit_disable(struct intel_crtc *crtc)
3726{
3727 struct drm_device *dev = crtc->base.dev;
3728 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02003729
3730 if (!crtc->config.gmch_pfit.control)
3731 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02003732
3733 assert_pipe_disabled(dev_priv, crtc->pipe);
3734
Daniel Vetter328d8e82013-05-08 10:36:31 +02003735 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
3736 I915_READ(PFIT_CONTROL));
3737 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02003738}
3739
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003740static void i9xx_crtc_disable(struct drm_crtc *crtc)
3741{
3742 struct drm_device *dev = crtc->dev;
3743 struct drm_i915_private *dev_priv = dev->dev_private;
3744 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003745 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003746 int pipe = intel_crtc->pipe;
3747 int plane = intel_crtc->plane;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003748
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003749 if (!intel_crtc->active)
3750 return;
3751
Daniel Vetterea9d7582012-07-10 10:42:52 +02003752 for_each_encoder_on_crtc(dev, crtc, encoder)
3753 encoder->disable(encoder);
3754
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003755 /* Give the overlay scaler a chance to disable if it's on this pipe */
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003756 intel_crtc_wait_for_pending_flips(crtc);
3757 drm_vblank_off(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003758
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07003759 if (dev_priv->fbc.plane == plane)
Chris Wilson973d04f2011-07-08 12:22:37 +01003760 intel_disable_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003761
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003762 intel_crtc_dpms_overlay(intel_crtc, false);
3763 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003764 intel_disable_planes(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003765 intel_disable_plane(dev_priv, plane, pipe);
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003766
Jesse Barnesb24e7172011-01-04 15:09:30 -08003767 intel_disable_pipe(dev_priv, pipe);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02003768
Daniel Vetter87476d62013-04-11 16:29:06 +02003769 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02003770
Jesse Barnes89b667f2013-04-18 14:51:36 -07003771 for_each_encoder_on_crtc(dev, crtc, encoder)
3772 if (encoder->post_disable)
3773 encoder->post_disable(encoder);
3774
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08003775 intel_disable_pll(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003776
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003777 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003778 intel_update_fbc(dev);
3779 intel_update_watermarks(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003780}
3781
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003782static void i9xx_crtc_off(struct drm_crtc *crtc)
3783{
3784}
3785
Daniel Vetter976f8a22012-07-08 22:34:21 +02003786static void intel_crtc_update_sarea(struct drm_crtc *crtc,
3787 bool enabled)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003788{
3789 struct drm_device *dev = crtc->dev;
3790 struct drm_i915_master_private *master_priv;
3791 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3792 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08003793
3794 if (!dev->primary->master)
3795 return;
3796
3797 master_priv = dev->primary->master->driver_priv;
3798 if (!master_priv->sarea_priv)
3799 return;
3800
Jesse Barnes79e53942008-11-07 14:24:08 -08003801 switch (pipe) {
3802 case 0:
3803 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
3804 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
3805 break;
3806 case 1:
3807 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
3808 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
3809 break;
3810 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003811 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08003812 break;
3813 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003814}
3815
Daniel Vetter976f8a22012-07-08 22:34:21 +02003816/**
3817 * Sets the power management mode of the pipe and plane.
3818 */
3819void intel_crtc_update_dpms(struct drm_crtc *crtc)
Chris Wilsoncdd59982010-09-08 16:30:16 +01003820{
Chris Wilsoncdd59982010-09-08 16:30:16 +01003821 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003822 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter976f8a22012-07-08 22:34:21 +02003823 struct intel_encoder *intel_encoder;
3824 bool enable = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01003825
Daniel Vetter976f8a22012-07-08 22:34:21 +02003826 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
3827 enable |= intel_encoder->connectors_active;
3828
3829 if (enable)
3830 dev_priv->display.crtc_enable(crtc);
3831 else
3832 dev_priv->display.crtc_disable(crtc);
3833
3834 intel_crtc_update_sarea(crtc, enable);
3835}
3836
Daniel Vetter976f8a22012-07-08 22:34:21 +02003837static void intel_crtc_disable(struct drm_crtc *crtc)
3838{
3839 struct drm_device *dev = crtc->dev;
3840 struct drm_connector *connector;
3841 struct drm_i915_private *dev_priv = dev->dev_private;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08003842 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter976f8a22012-07-08 22:34:21 +02003843
3844 /* crtc should still be enabled when we disable it. */
3845 WARN_ON(!crtc->enabled);
3846
3847 dev_priv->display.crtc_disable(crtc);
Paulo Zanonic77bf562013-05-03 12:15:40 -03003848 intel_crtc->eld_vld = false;
Daniel Vetter976f8a22012-07-08 22:34:21 +02003849 intel_crtc_update_sarea(crtc, false);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003850 dev_priv->display.off(crtc);
3851
Chris Wilson931872f2012-01-16 23:01:13 +00003852 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
3853 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003854
3855 if (crtc->fb) {
3856 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01003857 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003858 mutex_unlock(&dev->struct_mutex);
Daniel Vetter976f8a22012-07-08 22:34:21 +02003859 crtc->fb = NULL;
3860 }
3861
3862 /* Update computed state. */
3863 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3864 if (!connector->encoder || !connector->encoder->crtc)
3865 continue;
3866
3867 if (connector->encoder->crtc != crtc)
3868 continue;
3869
3870 connector->dpms = DRM_MODE_DPMS_OFF;
3871 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01003872 }
3873}
3874
Daniel Vettera261b242012-07-26 19:21:47 +02003875void intel_modeset_disable(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08003876{
Daniel Vettera261b242012-07-26 19:21:47 +02003877 struct drm_crtc *crtc;
3878
3879 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3880 if (crtc->enabled)
3881 intel_crtc_disable(crtc);
3882 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003883}
3884
Chris Wilsonea5b2132010-08-04 13:50:23 +01003885void intel_encoder_destroy(struct drm_encoder *encoder)
3886{
Chris Wilson4ef69c72010-09-09 15:14:28 +01003887 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01003888
Chris Wilsonea5b2132010-08-04 13:50:23 +01003889 drm_encoder_cleanup(encoder);
3890 kfree(intel_encoder);
3891}
3892
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003893/* Simple dpms helper for encodres with just one connector, no cloning and only
3894 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
3895 * state of the entire output pipe. */
3896void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
3897{
3898 if (mode == DRM_MODE_DPMS_ON) {
3899 encoder->connectors_active = true;
3900
Daniel Vetterb2cabb02012-07-01 22:42:24 +02003901 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003902 } else {
3903 encoder->connectors_active = false;
3904
Daniel Vetterb2cabb02012-07-01 22:42:24 +02003905 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003906 }
3907}
3908
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003909/* Cross check the actual hw state with our own modeset state tracking (and it's
3910 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02003911static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003912{
3913 if (connector->get_hw_state(connector)) {
3914 struct intel_encoder *encoder = connector->encoder;
3915 struct drm_crtc *crtc;
3916 bool encoder_enabled;
3917 enum pipe pipe;
3918
3919 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
3920 connector->base.base.id,
3921 drm_get_connector_name(&connector->base));
3922
3923 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
3924 "wrong connector dpms state\n");
3925 WARN(connector->base.encoder != &encoder->base,
3926 "active connector not linked to encoder\n");
3927 WARN(!encoder->connectors_active,
3928 "encoder->connectors_active not set\n");
3929
3930 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
3931 WARN(!encoder_enabled, "encoder not enabled\n");
3932 if (WARN_ON(!encoder->base.crtc))
3933 return;
3934
3935 crtc = encoder->base.crtc;
3936
3937 WARN(!crtc->enabled, "crtc not enabled\n");
3938 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
3939 WARN(pipe != to_intel_crtc(crtc)->pipe,
3940 "encoder active on the wrong pipe\n");
3941 }
3942}
3943
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003944/* Even simpler default implementation, if there's really no special case to
3945 * consider. */
3946void intel_connector_dpms(struct drm_connector *connector, int mode)
3947{
3948 struct intel_encoder *encoder = intel_attached_encoder(connector);
3949
3950 /* All the simple cases only support two dpms states. */
3951 if (mode != DRM_MODE_DPMS_ON)
3952 mode = DRM_MODE_DPMS_OFF;
3953
3954 if (mode == connector->dpms)
3955 return;
3956
3957 connector->dpms = mode;
3958
3959 /* Only need to change hw state when actually enabled */
3960 if (encoder->base.crtc)
3961 intel_encoder_dpms(encoder, mode);
3962 else
Daniel Vetter8af6cf82012-07-10 09:50:11 +02003963 WARN_ON(encoder->connectors_active != false);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003964
Daniel Vetterb9805142012-08-31 17:37:33 +02003965 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003966}
3967
Daniel Vetterf0947c32012-07-02 13:10:34 +02003968/* Simple connector->get_hw_state implementation for encoders that support only
3969 * one connector and no cloning and hence the encoder state determines the state
3970 * of the connector. */
3971bool intel_connector_get_hw_state(struct intel_connector *connector)
3972{
Daniel Vetter24929352012-07-02 20:28:59 +02003973 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02003974 struct intel_encoder *encoder = connector->encoder;
3975
3976 return encoder->get_hw_state(encoder, &pipe);
3977}
3978
Daniel Vetter1857e1d2013-04-29 19:34:16 +02003979static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
3980 struct intel_crtc_config *pipe_config)
3981{
3982 struct drm_i915_private *dev_priv = dev->dev_private;
3983 struct intel_crtc *pipe_B_crtc =
3984 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
3985
3986 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
3987 pipe_name(pipe), pipe_config->fdi_lanes);
3988 if (pipe_config->fdi_lanes > 4) {
3989 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
3990 pipe_name(pipe), pipe_config->fdi_lanes);
3991 return false;
3992 }
3993
3994 if (IS_HASWELL(dev)) {
3995 if (pipe_config->fdi_lanes > 2) {
3996 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
3997 pipe_config->fdi_lanes);
3998 return false;
3999 } else {
4000 return true;
4001 }
4002 }
4003
4004 if (INTEL_INFO(dev)->num_pipes == 2)
4005 return true;
4006
4007 /* Ivybridge 3 pipe is really complicated */
4008 switch (pipe) {
4009 case PIPE_A:
4010 return true;
4011 case PIPE_B:
4012 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
4013 pipe_config->fdi_lanes > 2) {
4014 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4015 pipe_name(pipe), pipe_config->fdi_lanes);
4016 return false;
4017 }
4018 return true;
4019 case PIPE_C:
Daniel Vetter1e833f42013-02-19 22:31:57 +01004020 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004021 pipe_B_crtc->config.fdi_lanes <= 2) {
4022 if (pipe_config->fdi_lanes > 2) {
4023 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4024 pipe_name(pipe), pipe_config->fdi_lanes);
4025 return false;
4026 }
4027 } else {
4028 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
4029 return false;
4030 }
4031 return true;
4032 default:
4033 BUG();
4034 }
4035}
4036
Daniel Vettere29c22c2013-02-21 00:00:16 +01004037#define RETRY 1
4038static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
4039 struct intel_crtc_config *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02004040{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004041 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004042 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Daniel Vetterff9a6752013-06-01 17:16:21 +02004043 int lane, link_bw, fdi_dotclock;
Daniel Vettere29c22c2013-02-21 00:00:16 +01004044 bool setup_ok, needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004045
Daniel Vettere29c22c2013-02-21 00:00:16 +01004046retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02004047 /* FDI is a binary signal running at ~2.7GHz, encoding
4048 * each output octet as 10 bits. The actual frequency
4049 * is stored as a divider into a 100MHz clock, and the
4050 * mode pixel clock is stored in units of 1KHz.
4051 * Hence the bw of each lane in terms of the mode signal
4052 * is:
4053 */
4054 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
4055
Daniel Vetterff9a6752013-06-01 17:16:21 +02004056 fdi_dotclock = adjusted_mode->clock;
Daniel Vetteref1b4602013-06-01 17:17:04 +02004057 fdi_dotclock /= pipe_config->pixel_multiplier;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004058
Daniel Vetter2bd89a02013-06-01 17:16:19 +02004059 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02004060 pipe_config->pipe_bpp);
4061
4062 pipe_config->fdi_lanes = lane;
4063
Daniel Vetter2bd89a02013-06-01 17:16:19 +02004064 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02004065 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004066
Daniel Vettere29c22c2013-02-21 00:00:16 +01004067 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
4068 intel_crtc->pipe, pipe_config);
4069 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
4070 pipe_config->pipe_bpp -= 2*3;
4071 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
4072 pipe_config->pipe_bpp);
4073 needs_recompute = true;
4074 pipe_config->bw_constrained = true;
4075
4076 goto retry;
4077 }
4078
4079 if (needs_recompute)
4080 return RETRY;
4081
4082 return setup_ok ? 0 : -EINVAL;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004083}
4084
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004085static void hsw_compute_ips_config(struct intel_crtc *crtc,
4086 struct intel_crtc_config *pipe_config)
4087{
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03004088 pipe_config->ips_enabled = i915_enable_ips &&
4089 hsw_crtc_supports_ips(crtc) &&
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004090 pipe_config->pipe_bpp == 24;
4091}
4092
Daniel Vettera43f6e02013-06-07 23:10:32 +02004093static int intel_crtc_compute_config(struct intel_crtc *crtc,
Daniel Vettere29c22c2013-02-21 00:00:16 +01004094 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08004095{
Daniel Vettera43f6e02013-06-07 23:10:32 +02004096 struct drm_device *dev = crtc->base.dev;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01004097 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01004098
Eric Anholtbad720f2009-10-22 16:11:14 -07004099 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08004100 /* FDI link clock is fixed at 2.7G */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01004101 if (pipe_config->requested_mode.clock * 3
4102 > IRONLAKE_FDI_FREQ * 4)
Daniel Vettere29c22c2013-02-21 00:00:16 +01004103 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004104 }
Chris Wilson89749352010-09-12 18:25:19 +01004105
Daniel Vetterf9bef082012-04-15 19:53:19 +02004106 /* All interlaced capable intel hw wants timings in frames. Note though
4107 * that intel_lvds_mode_fixup does some funny tricks with the crtc
4108 * timings, so we need to be careful not to clobber these.*/
Daniel Vetter7ae89232013-03-27 00:44:52 +01004109 if (!pipe_config->timings_set)
Daniel Vetterf9bef082012-04-15 19:53:19 +02004110 drm_mode_set_crtcinfo(adjusted_mode, 0);
Chris Wilson89749352010-09-12 18:25:19 +01004111
Damien Lespiau8693a822013-05-03 18:48:11 +01004112 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
4113 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03004114 */
4115 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
4116 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01004117 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03004118
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004119 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004120 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004121 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004122 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
4123 * for lvds. */
4124 pipe_config->pipe_bpp = 8*3;
4125 }
4126
Damien Lespiauf5adf942013-06-24 18:29:34 +01004127 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02004128 hsw_compute_ips_config(crtc, pipe_config);
4129
4130 /* XXX: PCH clock sharing is done in ->mode_set, so make sure the old
4131 * clock survives for now. */
4132 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
4133 pipe_config->shared_dpll = crtc->config.shared_dpll;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004134
Daniel Vetter877d48d2013-04-19 11:24:43 +02004135 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02004136 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02004137
Daniel Vettere29c22c2013-02-21 00:00:16 +01004138 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08004139}
4140
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07004141static int valleyview_get_display_clock_speed(struct drm_device *dev)
4142{
4143 return 400000; /* FIXME */
4144}
4145
Jesse Barnese70236a2009-09-21 10:42:27 -07004146static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08004147{
Jesse Barnese70236a2009-09-21 10:42:27 -07004148 return 400000;
4149}
Jesse Barnes79e53942008-11-07 14:24:08 -08004150
Jesse Barnese70236a2009-09-21 10:42:27 -07004151static int i915_get_display_clock_speed(struct drm_device *dev)
4152{
4153 return 333000;
4154}
Jesse Barnes79e53942008-11-07 14:24:08 -08004155
Jesse Barnese70236a2009-09-21 10:42:27 -07004156static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
4157{
4158 return 200000;
4159}
Jesse Barnes79e53942008-11-07 14:24:08 -08004160
Jesse Barnese70236a2009-09-21 10:42:27 -07004161static int i915gm_get_display_clock_speed(struct drm_device *dev)
4162{
4163 u16 gcfgc = 0;
4164
4165 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4166
4167 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08004168 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07004169 else {
4170 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4171 case GC_DISPLAY_CLOCK_333_MHZ:
4172 return 333000;
4173 default:
4174 case GC_DISPLAY_CLOCK_190_200_MHZ:
4175 return 190000;
4176 }
4177 }
4178}
Jesse Barnes79e53942008-11-07 14:24:08 -08004179
Jesse Barnese70236a2009-09-21 10:42:27 -07004180static int i865_get_display_clock_speed(struct drm_device *dev)
4181{
4182 return 266000;
4183}
4184
4185static int i855_get_display_clock_speed(struct drm_device *dev)
4186{
4187 u16 hpllcc = 0;
4188 /* Assume that the hardware is in the high speed state. This
4189 * should be the default.
4190 */
4191 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
4192 case GC_CLOCK_133_200:
4193 case GC_CLOCK_100_200:
4194 return 200000;
4195 case GC_CLOCK_166_250:
4196 return 250000;
4197 case GC_CLOCK_100_133:
4198 return 133000;
4199 }
4200
4201 /* Shouldn't happen */
4202 return 0;
4203}
4204
4205static int i830_get_display_clock_speed(struct drm_device *dev)
4206{
4207 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08004208}
4209
Zhenyu Wang2c072452009-06-05 15:38:42 +08004210static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004211intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004212{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004213 while (*num > DATA_LINK_M_N_MASK ||
4214 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08004215 *num >>= 1;
4216 *den >>= 1;
4217 }
4218}
4219
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004220static void compute_m_n(unsigned int m, unsigned int n,
4221 uint32_t *ret_m, uint32_t *ret_n)
4222{
4223 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
4224 *ret_m = div_u64((uint64_t) m * *ret_n, n);
4225 intel_reduce_m_n_ratio(ret_m, ret_n);
4226}
4227
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004228void
4229intel_link_compute_m_n(int bits_per_pixel, int nlanes,
4230 int pixel_clock, int link_clock,
4231 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004232{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004233 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004234
4235 compute_m_n(bits_per_pixel * pixel_clock,
4236 link_clock * nlanes * 8,
4237 &m_n->gmch_m, &m_n->gmch_n);
4238
4239 compute_m_n(pixel_clock, link_clock,
4240 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004241}
4242
Chris Wilsona7615032011-01-12 17:04:08 +00004243static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
4244{
Keith Packard72bbe582011-09-26 16:09:45 -07004245 if (i915_panel_use_ssc >= 0)
4246 return i915_panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004247 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07004248 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00004249}
4250
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004251static int vlv_get_refclk(struct drm_crtc *crtc)
4252{
4253 struct drm_device *dev = crtc->dev;
4254 struct drm_i915_private *dev_priv = dev->dev_private;
4255 int refclk = 27000; /* for DP & HDMI */
4256
4257 return 100000; /* only one validated so far */
4258
4259 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
4260 refclk = 96000;
4261 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
4262 if (intel_panel_use_ssc(dev_priv))
4263 refclk = 100000;
4264 else
4265 refclk = 96000;
4266 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
4267 refclk = 100000;
4268 }
4269
4270 return refclk;
4271}
4272
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004273static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
4274{
4275 struct drm_device *dev = crtc->dev;
4276 struct drm_i915_private *dev_priv = dev->dev_private;
4277 int refclk;
4278
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004279 if (IS_VALLEYVIEW(dev)) {
4280 refclk = vlv_get_refclk(crtc);
4281 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004282 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004283 refclk = dev_priv->vbt.lvds_ssc_freq * 1000;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004284 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
4285 refclk / 1000);
4286 } else if (!IS_GEN2(dev)) {
4287 refclk = 96000;
4288 } else {
4289 refclk = 48000;
4290 }
4291
4292 return refclk;
4293}
4294
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004295static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004296{
Daniel Vetter7df00d72013-05-21 21:54:55 +02004297 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004298}
Daniel Vetterf47709a2013-03-28 10:42:02 +01004299
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004300static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
4301{
4302 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004303}
4304
Daniel Vetterf47709a2013-03-28 10:42:02 +01004305static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Jesse Barnesa7516a02011-12-15 12:30:37 -08004306 intel_clock_t *reduced_clock)
4307{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004308 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004309 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004310 int pipe = crtc->pipe;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004311 u32 fp, fp2 = 0;
4312
4313 if (IS_PINEVIEW(dev)) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004314 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004315 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004316 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004317 } else {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004318 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004319 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004320 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004321 }
4322
4323 I915_WRITE(FP0(pipe), fp);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004324 crtc->config.dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004325
Daniel Vetterf47709a2013-03-28 10:42:02 +01004326 crtc->lowfreq_avail = false;
4327 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Jesse Barnesa7516a02011-12-15 12:30:37 -08004328 reduced_clock && i915_powersave) {
4329 I915_WRITE(FP1(pipe), fp2);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004330 crtc->config.dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004331 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004332 } else {
4333 I915_WRITE(FP1(pipe), fp);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004334 crtc->config.dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004335 }
4336}
4337
Jesse Barnes89b667f2013-04-18 14:51:36 -07004338static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv)
4339{
4340 u32 reg_val;
4341
4342 /*
4343 * PLLB opamp always calibrates to max value of 0x3f, force enable it
4344 * and set it to a reasonable value instead.
4345 */
Jani Nikulaae992582013-05-22 15:36:19 +03004346 reg_val = vlv_dpio_read(dev_priv, DPIO_IREF(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004347 reg_val &= 0xffffff00;
4348 reg_val |= 0x00000030;
Jani Nikulaae992582013-05-22 15:36:19 +03004349 vlv_dpio_write(dev_priv, DPIO_IREF(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004350
Jani Nikulaae992582013-05-22 15:36:19 +03004351 reg_val = vlv_dpio_read(dev_priv, DPIO_CALIBRATION);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004352 reg_val &= 0x8cffffff;
4353 reg_val = 0x8c000000;
Jani Nikulaae992582013-05-22 15:36:19 +03004354 vlv_dpio_write(dev_priv, DPIO_CALIBRATION, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004355
Jani Nikulaae992582013-05-22 15:36:19 +03004356 reg_val = vlv_dpio_read(dev_priv, DPIO_IREF(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004357 reg_val &= 0xffffff00;
Jani Nikulaae992582013-05-22 15:36:19 +03004358 vlv_dpio_write(dev_priv, DPIO_IREF(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004359
Jani Nikulaae992582013-05-22 15:36:19 +03004360 reg_val = vlv_dpio_read(dev_priv, DPIO_CALIBRATION);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004361 reg_val &= 0x00ffffff;
4362 reg_val |= 0xb0000000;
Jani Nikulaae992582013-05-22 15:36:19 +03004363 vlv_dpio_write(dev_priv, DPIO_CALIBRATION, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004364}
4365
Daniel Vetterb5518422013-05-03 11:49:48 +02004366static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
4367 struct intel_link_m_n *m_n)
4368{
4369 struct drm_device *dev = crtc->base.dev;
4370 struct drm_i915_private *dev_priv = dev->dev_private;
4371 int pipe = crtc->pipe;
4372
Daniel Vettere3b95f12013-05-03 11:49:49 +02004373 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4374 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
4375 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
4376 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02004377}
4378
4379static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
4380 struct intel_link_m_n *m_n)
4381{
4382 struct drm_device *dev = crtc->base.dev;
4383 struct drm_i915_private *dev_priv = dev->dev_private;
4384 int pipe = crtc->pipe;
4385 enum transcoder transcoder = crtc->config.cpu_transcoder;
4386
4387 if (INTEL_INFO(dev)->gen >= 5) {
4388 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
4389 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
4390 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
4391 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
4392 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02004393 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4394 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
4395 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
4396 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02004397 }
4398}
4399
Daniel Vetter03afc4a2013-04-02 23:42:31 +02004400static void intel_dp_set_m_n(struct intel_crtc *crtc)
4401{
4402 if (crtc->config.has_pch_encoder)
4403 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4404 else
4405 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4406}
4407
Daniel Vetterf47709a2013-03-28 10:42:02 +01004408static void vlv_update_pll(struct intel_crtc *crtc)
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004409{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004410 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004411 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004412 struct intel_encoder *encoder;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004413 int pipe = crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004414 u32 dpll, mdiv;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004415 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004416 bool is_hdmi;
Daniel Vetter198a037f2013-04-19 11:14:37 +02004417 u32 coreclk, reg_val, dpll_md;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004418
Daniel Vetter09153002012-12-12 14:06:44 +01004419 mutex_lock(&dev_priv->dpio_lock);
4420
Jesse Barnes89b667f2013-04-18 14:51:36 -07004421 is_hdmi = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004422
Daniel Vetterf47709a2013-03-28 10:42:02 +01004423 bestn = crtc->config.dpll.n;
4424 bestm1 = crtc->config.dpll.m1;
4425 bestm2 = crtc->config.dpll.m2;
4426 bestp1 = crtc->config.dpll.p1;
4427 bestp2 = crtc->config.dpll.p2;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004428
Jesse Barnes89b667f2013-04-18 14:51:36 -07004429 /* See eDP HDMI DPIO driver vbios notes doc */
4430
4431 /* PLL B needs special handling */
4432 if (pipe)
4433 vlv_pllb_recal_opamp(dev_priv);
4434
4435 /* Set up Tx target for periodic Rcomp update */
Jani Nikulaae992582013-05-22 15:36:19 +03004436 vlv_dpio_write(dev_priv, DPIO_IREF_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004437
4438 /* Disable target IRef on PLL */
Jani Nikulaae992582013-05-22 15:36:19 +03004439 reg_val = vlv_dpio_read(dev_priv, DPIO_IREF_CTL(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004440 reg_val &= 0x00ffffff;
Jani Nikulaae992582013-05-22 15:36:19 +03004441 vlv_dpio_write(dev_priv, DPIO_IREF_CTL(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004442
4443 /* Disable fast lock */
Jani Nikulaae992582013-05-22 15:36:19 +03004444 vlv_dpio_write(dev_priv, DPIO_FASTCLK_DISABLE, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004445
4446 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004447 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
4448 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
4449 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004450 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07004451
4452 /*
4453 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
4454 * but we don't support that).
4455 * Note: don't use the DAC post divider as it seems unstable.
4456 */
4457 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Jani Nikulaae992582013-05-22 15:36:19 +03004458 vlv_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004459
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004460 mdiv |= DPIO_ENABLE_CALIBRATION;
Jani Nikulaae992582013-05-22 15:36:19 +03004461 vlv_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004462
Jesse Barnes89b667f2013-04-18 14:51:36 -07004463 /* Set HBR and RBR LPF coefficients */
Daniel Vetterff9a6752013-06-01 17:16:21 +02004464 if (crtc->config.port_clock == 162000 ||
Ville Syrjälä99750bd2013-06-14 14:02:52 +03004465 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
Jesse Barnes89b667f2013-04-18 14:51:36 -07004466 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
Ville Syrjälä4abb2c32013-06-14 14:02:53 +03004467 vlv_dpio_write(dev_priv, DPIO_LPF_COEFF(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03004468 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004469 else
Ville Syrjälä4abb2c32013-06-14 14:02:53 +03004470 vlv_dpio_write(dev_priv, DPIO_LPF_COEFF(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004471 0x00d0000f);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004472
Jesse Barnes89b667f2013-04-18 14:51:36 -07004473 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
4474 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
4475 /* Use SSC source */
4476 if (!pipe)
Jani Nikulaae992582013-05-22 15:36:19 +03004477 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004478 0x0df40000);
4479 else
Jani Nikulaae992582013-05-22 15:36:19 +03004480 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004481 0x0df70000);
4482 } else { /* HDMI or VGA */
4483 /* Use bend source */
4484 if (!pipe)
Jani Nikulaae992582013-05-22 15:36:19 +03004485 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004486 0x0df70000);
4487 else
Jani Nikulaae992582013-05-22 15:36:19 +03004488 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004489 0x0df40000);
4490 }
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004491
Jani Nikulaae992582013-05-22 15:36:19 +03004492 coreclk = vlv_dpio_read(dev_priv, DPIO_CORE_CLK(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004493 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
4494 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
4495 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
4496 coreclk |= 0x01000000;
Jani Nikulaae992582013-05-22 15:36:19 +03004497 vlv_dpio_write(dev_priv, DPIO_CORE_CLK(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004498
Jani Nikulaae992582013-05-22 15:36:19 +03004499 vlv_dpio_write(dev_priv, DPIO_PLL_CML(pipe), 0x87871000);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004500
4501 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
4502 if (encoder->pre_pll_enable)
4503 encoder->pre_pll_enable(encoder);
4504
4505 /* Enable DPIO clock input */
4506 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
4507 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
4508 if (pipe)
4509 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004510
4511 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004512 crtc->config.dpll_hw_state.dpll = dpll;
4513
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004514 I915_WRITE(DPLL(pipe), dpll);
4515 POSTING_READ(DPLL(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004516 udelay(150);
4517
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004518 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
4519 DRM_ERROR("DPLL %d failed to lock\n", pipe);
4520
Daniel Vetteref1b4602013-06-01 17:17:04 +02004521 dpll_md = (crtc->config.pixel_multiplier - 1)
4522 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004523 crtc->config.dpll_hw_state.dpll_md = dpll_md;
4524
Daniel Vetter198a037f2013-04-19 11:14:37 +02004525 I915_WRITE(DPLL_MD(pipe), dpll_md);
4526 POSTING_READ(DPLL_MD(pipe));
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004527
Daniel Vetterf47709a2013-03-28 10:42:02 +01004528 if (crtc->config.has_dp_encoder)
4529 intel_dp_set_m_n(crtc);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304530
Daniel Vetter09153002012-12-12 14:06:44 +01004531 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004532}
4533
Daniel Vetterf47709a2013-03-28 10:42:02 +01004534static void i9xx_update_pll(struct intel_crtc *crtc,
4535 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004536 int num_connectors)
4537{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004538 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004539 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004540 u32 dpll;
4541 bool is_sdvo;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004542 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004543
Daniel Vetterf47709a2013-03-28 10:42:02 +01004544 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304545
Daniel Vetterf47709a2013-03-28 10:42:02 +01004546 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
4547 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004548
4549 dpll = DPLL_VGA_MODE_DIS;
4550
Daniel Vetterf47709a2013-03-28 10:42:02 +01004551 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004552 dpll |= DPLLB_MODE_LVDS;
4553 else
4554 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01004555
Daniel Vetteref1b4602013-06-01 17:17:04 +02004556 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Daniel Vetter198a037f2013-04-19 11:14:37 +02004557 dpll |= (crtc->config.pixel_multiplier - 1)
4558 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004559 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02004560
4561 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02004562 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02004563
Daniel Vetterf47709a2013-03-28 10:42:02 +01004564 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
Daniel Vetter4a33e482013-07-06 12:52:05 +02004565 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004566
4567 /* compute bitmask from p1 value */
4568 if (IS_PINEVIEW(dev))
4569 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
4570 else {
4571 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4572 if (IS_G4X(dev) && reduced_clock)
4573 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
4574 }
4575 switch (clock->p2) {
4576 case 5:
4577 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4578 break;
4579 case 7:
4580 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4581 break;
4582 case 10:
4583 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4584 break;
4585 case 14:
4586 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4587 break;
4588 }
4589 if (INTEL_INFO(dev)->gen >= 4)
4590 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
4591
Daniel Vetter09ede542013-04-30 14:01:45 +02004592 if (crtc->config.sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004593 dpll |= PLL_REF_INPUT_TVCLKINBC;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004594 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004595 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4596 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4597 else
4598 dpll |= PLL_REF_INPUT_DREFCLK;
4599
4600 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004601 crtc->config.dpll_hw_state.dpll = dpll;
4602
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004603 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetteref1b4602013-06-01 17:17:04 +02004604 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
4605 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004606 crtc->config.dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004607 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02004608
4609 if (crtc->config.has_dp_encoder)
4610 intel_dp_set_m_n(crtc);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004611}
4612
Daniel Vetterf47709a2013-03-28 10:42:02 +01004613static void i8xx_update_pll(struct intel_crtc *crtc,
Daniel Vetterf47709a2013-03-28 10:42:02 +01004614 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004615 int num_connectors)
4616{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004617 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004618 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004619 u32 dpll;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004620 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004621
Daniel Vetterf47709a2013-03-28 10:42:02 +01004622 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304623
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004624 dpll = DPLL_VGA_MODE_DIS;
4625
Daniel Vetterf47709a2013-03-28 10:42:02 +01004626 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004627 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4628 } else {
4629 if (clock->p1 == 2)
4630 dpll |= PLL_P1_DIVIDE_BY_TWO;
4631 else
4632 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4633 if (clock->p2 == 4)
4634 dpll |= PLL_P2_DIVIDE_BY_4;
4635 }
4636
Daniel Vetter4a33e482013-07-06 12:52:05 +02004637 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
4638 dpll |= DPLL_DVO_2X_MODE;
4639
Daniel Vetterf47709a2013-03-28 10:42:02 +01004640 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004641 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4642 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4643 else
4644 dpll |= PLL_REF_INPUT_DREFCLK;
4645
4646 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004647 crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004648}
4649
Daniel Vetter8a654f32013-06-01 17:16:22 +02004650static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004651{
4652 struct drm_device *dev = intel_crtc->base.dev;
4653 struct drm_i915_private *dev_priv = dev->dev_private;
4654 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02004655 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02004656 struct drm_display_mode *adjusted_mode =
4657 &intel_crtc->config.adjusted_mode;
4658 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004659 uint32_t vsyncshift, crtc_vtotal, crtc_vblank_end;
4660
4661 /* We need to be careful not to changed the adjusted mode, for otherwise
4662 * the hw state checker will get angry at the mismatch. */
4663 crtc_vtotal = adjusted_mode->crtc_vtotal;
4664 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004665
4666 if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4667 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004668 crtc_vtotal -= 1;
4669 crtc_vblank_end -= 1;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004670 vsyncshift = adjusted_mode->crtc_hsync_start
4671 - adjusted_mode->crtc_htotal / 2;
4672 } else {
4673 vsyncshift = 0;
4674 }
4675
4676 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004677 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004678
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004679 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004680 (adjusted_mode->crtc_hdisplay - 1) |
4681 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004682 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004683 (adjusted_mode->crtc_hblank_start - 1) |
4684 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004685 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004686 (adjusted_mode->crtc_hsync_start - 1) |
4687 ((adjusted_mode->crtc_hsync_end - 1) << 16));
4688
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004689 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004690 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004691 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004692 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004693 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004694 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004695 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004696 (adjusted_mode->crtc_vsync_start - 1) |
4697 ((adjusted_mode->crtc_vsync_end - 1) << 16));
4698
Paulo Zanonib5e508d2012-10-24 11:34:43 -02004699 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
4700 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
4701 * documented on the DDI_FUNC_CTL register description, EDP Input Select
4702 * bits. */
4703 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
4704 (pipe == PIPE_B || pipe == PIPE_C))
4705 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
4706
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004707 /* pipesrc controls the size that is scaled from, which should
4708 * always be the user's requested size.
4709 */
4710 I915_WRITE(PIPESRC(pipe),
4711 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
4712}
4713
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02004714static void intel_get_pipe_timings(struct intel_crtc *crtc,
4715 struct intel_crtc_config *pipe_config)
4716{
4717 struct drm_device *dev = crtc->base.dev;
4718 struct drm_i915_private *dev_priv = dev->dev_private;
4719 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
4720 uint32_t tmp;
4721
4722 tmp = I915_READ(HTOTAL(cpu_transcoder));
4723 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
4724 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
4725 tmp = I915_READ(HBLANK(cpu_transcoder));
4726 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
4727 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
4728 tmp = I915_READ(HSYNC(cpu_transcoder));
4729 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
4730 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
4731
4732 tmp = I915_READ(VTOTAL(cpu_transcoder));
4733 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
4734 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
4735 tmp = I915_READ(VBLANK(cpu_transcoder));
4736 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
4737 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
4738 tmp = I915_READ(VSYNC(cpu_transcoder));
4739 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
4740 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
4741
4742 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
4743 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
4744 pipe_config->adjusted_mode.crtc_vtotal += 1;
4745 pipe_config->adjusted_mode.crtc_vblank_end += 1;
4746 }
4747
4748 tmp = I915_READ(PIPESRC(crtc->pipe));
4749 pipe_config->requested_mode.vdisplay = (tmp & 0xffff) + 1;
4750 pipe_config->requested_mode.hdisplay = ((tmp >> 16) & 0xffff) + 1;
4751}
4752
Jesse Barnesbabea612013-06-26 18:57:38 +03004753static void intel_crtc_mode_from_pipe_config(struct intel_crtc *intel_crtc,
4754 struct intel_crtc_config *pipe_config)
4755{
4756 struct drm_crtc *crtc = &intel_crtc->base;
4757
4758 crtc->mode.hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
4759 crtc->mode.htotal = pipe_config->adjusted_mode.crtc_htotal;
4760 crtc->mode.hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
4761 crtc->mode.hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
4762
4763 crtc->mode.vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
4764 crtc->mode.vtotal = pipe_config->adjusted_mode.crtc_vtotal;
4765 crtc->mode.vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
4766 crtc->mode.vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
4767
4768 crtc->mode.flags = pipe_config->adjusted_mode.flags;
4769
4770 crtc->mode.clock = pipe_config->adjusted_mode.clock;
4771 crtc->mode.flags |= pipe_config->adjusted_mode.flags;
4772}
4773
Daniel Vetter84b046f2013-02-19 18:48:54 +01004774static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
4775{
4776 struct drm_device *dev = intel_crtc->base.dev;
4777 struct drm_i915_private *dev_priv = dev->dev_private;
4778 uint32_t pipeconf;
4779
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02004780 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01004781
4782 if (intel_crtc->pipe == 0 && INTEL_INFO(dev)->gen < 4) {
4783 /* Enable pixel doubling when the dot clock is > 90% of the (display)
4784 * core speed.
4785 *
4786 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
4787 * pipe == 0 check?
4788 */
4789 if (intel_crtc->config.requested_mode.clock >
4790 dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
4791 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01004792 }
4793
Daniel Vetterff9ce462013-04-24 14:57:17 +02004794 /* only g4x and later have fancy bpc/dither controls */
4795 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02004796 /* Bspec claims that we can't use dithering for 30bpp pipes. */
4797 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
4798 pipeconf |= PIPECONF_DITHER_EN |
4799 PIPECONF_DITHER_TYPE_SP;
4800
4801 switch (intel_crtc->config.pipe_bpp) {
4802 case 18:
4803 pipeconf |= PIPECONF_6BPC;
4804 break;
4805 case 24:
4806 pipeconf |= PIPECONF_8BPC;
4807 break;
4808 case 30:
4809 pipeconf |= PIPECONF_10BPC;
4810 break;
4811 default:
4812 /* Case prevented by intel_choose_pipe_bpp_dither. */
4813 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01004814 }
4815 }
4816
4817 if (HAS_PIPE_CXSR(dev)) {
4818 if (intel_crtc->lowfreq_avail) {
4819 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
4820 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
4821 } else {
4822 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01004823 }
4824 }
4825
Daniel Vetter84b046f2013-02-19 18:48:54 +01004826 if (!IS_GEN2(dev) &&
4827 intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
4828 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
4829 else
4830 pipeconf |= PIPECONF_PROGRESSIVE;
4831
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02004832 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
4833 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03004834
Daniel Vetter84b046f2013-02-19 18:48:54 +01004835 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
4836 POSTING_READ(PIPECONF(intel_crtc->pipe));
4837}
4838
Eric Anholtf564048e2011-03-30 13:01:02 -07004839static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07004840 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02004841 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08004842{
4843 struct drm_device *dev = crtc->dev;
4844 struct drm_i915_private *dev_priv = dev->dev_private;
4845 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01004846 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08004847 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07004848 int plane = intel_crtc->plane;
Eric Anholtc751ce42010-03-25 11:48:48 -07004849 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07004850 intel_clock_t clock, reduced_clock;
Daniel Vetter84b046f2013-02-19 18:48:54 +01004851 u32 dspcntr;
Daniel Vettera16af7212013-04-30 14:01:44 +02004852 bool ok, has_reduced_clock = false;
4853 bool is_lvds = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01004854 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08004855 const intel_limit_t *limit;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00004856 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08004857
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02004858 for_each_encoder_on_crtc(dev, crtc, encoder) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004859 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004860 case INTEL_OUTPUT_LVDS:
4861 is_lvds = true;
4862 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08004863 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05004864
Eric Anholtc751ce42010-03-25 11:48:48 -07004865 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08004866 }
4867
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004868 refclk = i9xx_get_refclk(crtc, num_connectors);
Jesse Barnes79e53942008-11-07 14:24:08 -08004869
Ma Lingd4906092009-03-18 20:13:27 +08004870 /*
4871 * Returns a set of divisors for the desired target clock with the given
4872 * refclk, or FALSE. The returned values represent the clock equation:
4873 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
4874 */
Chris Wilson1b894b52010-12-14 20:04:54 +00004875 limit = intel_limit(crtc, refclk);
Daniel Vetterff9a6752013-06-01 17:16:21 +02004876 ok = dev_priv->display.find_dpll(limit, crtc,
4877 intel_crtc->config.port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02004878 refclk, NULL, &clock);
4879 if (!ok && !intel_crtc->config.clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004880 DRM_ERROR("Couldn't find PLL settings for mode!\n");
Eric Anholtf564048e2011-03-30 13:01:02 -07004881 return -EINVAL;
4882 }
4883
4884 /* Ensure that the cursor is valid for the new mode before changing... */
4885 intel_crtc_update_cursor(crtc, true);
4886
4887 if (is_lvds && dev_priv->lvds_downclock_avail) {
Sean Paulcec2f352012-01-10 15:09:36 -08004888 /*
4889 * Ensure we match the reduced clock's P to the target clock.
4890 * If the clocks don't match, we can't switch the display clock
4891 * by using the FP0/FP1. In such case we will disable the LVDS
4892 * downclock feature.
4893 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02004894 has_reduced_clock =
4895 dev_priv->display.find_dpll(limit, crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07004896 dev_priv->lvds_downclock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02004897 refclk, &clock,
Eric Anholtf564048e2011-03-30 13:01:02 -07004898 &reduced_clock);
Eric Anholtf564048e2011-03-30 13:01:02 -07004899 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01004900 /* Compat-code for transition, will disappear. */
4901 if (!intel_crtc->config.clock_set) {
4902 intel_crtc->config.dpll.n = clock.n;
4903 intel_crtc->config.dpll.m1 = clock.m1;
4904 intel_crtc->config.dpll.m2 = clock.m2;
4905 intel_crtc->config.dpll.p1 = clock.p1;
4906 intel_crtc->config.dpll.p2 = clock.p2;
4907 }
Eric Anholtf564048e2011-03-30 13:01:02 -07004908
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004909 if (IS_GEN2(dev))
Daniel Vetter8a654f32013-06-01 17:16:22 +02004910 i8xx_update_pll(intel_crtc,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304911 has_reduced_clock ? &reduced_clock : NULL,
4912 num_connectors);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004913 else if (IS_VALLEYVIEW(dev))
Daniel Vetterf47709a2013-03-28 10:42:02 +01004914 vlv_update_pll(intel_crtc);
Eric Anholtf564048e2011-03-30 13:01:02 -07004915 else
Daniel Vetterf47709a2013-03-28 10:42:02 +01004916 i9xx_update_pll(intel_crtc,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004917 has_reduced_clock ? &reduced_clock : NULL,
Jesse Barnes89b667f2013-04-18 14:51:36 -07004918 num_connectors);
Eric Anholtf564048e2011-03-30 13:01:02 -07004919
Eric Anholtf564048e2011-03-30 13:01:02 -07004920 /* Set up the display plane register */
4921 dspcntr = DISPPLANE_GAMMA_ENABLE;
4922
Jesse Barnesda6ecc52013-03-08 10:46:00 -08004923 if (!IS_VALLEYVIEW(dev)) {
4924 if (pipe == 0)
4925 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
4926 else
4927 dspcntr |= DISPPLANE_SEL_PIPE_B;
4928 }
Eric Anholtf564048e2011-03-30 13:01:02 -07004929
Daniel Vetter8a654f32013-06-01 17:16:22 +02004930 intel_set_pipe_timings(intel_crtc);
Eric Anholtf564048e2011-03-30 13:01:02 -07004931
4932 /* pipesrc and dspsize control the size that is scaled from,
4933 * which should always be the user's requested size.
4934 */
Eric Anholt929c77f2011-03-30 13:01:04 -07004935 I915_WRITE(DSPSIZE(plane),
4936 ((mode->vdisplay - 1) << 16) |
4937 (mode->hdisplay - 1));
4938 I915_WRITE(DSPPOS(plane), 0);
Eric Anholtf564048e2011-03-30 13:01:02 -07004939
Daniel Vetter84b046f2013-02-19 18:48:54 +01004940 i9xx_set_pipeconf(intel_crtc);
4941
Eric Anholtf564048e2011-03-30 13:01:02 -07004942 I915_WRITE(DSPCNTR(plane), dspcntr);
4943 POSTING_READ(DSPCNTR(plane));
4944
Daniel Vetter94352cf2012-07-05 22:51:56 +02004945 ret = intel_pipe_set_base(crtc, x, y, fb);
Eric Anholtf564048e2011-03-30 13:01:02 -07004946
4947 intel_update_watermarks(dev);
4948
Eric Anholtf564048e2011-03-30 13:01:02 -07004949 return ret;
4950}
4951
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02004952static void i9xx_get_pfit_config(struct intel_crtc *crtc,
4953 struct intel_crtc_config *pipe_config)
4954{
4955 struct drm_device *dev = crtc->base.dev;
4956 struct drm_i915_private *dev_priv = dev->dev_private;
4957 uint32_t tmp;
4958
4959 tmp = I915_READ(PFIT_CONTROL);
4960
4961 if (INTEL_INFO(dev)->gen < 4) {
4962 if (crtc->pipe != PIPE_B)
4963 return;
4964
4965 /* gen2/3 store dither state in pfit control, needs to match */
4966 pipe_config->gmch_pfit.control = tmp & PANEL_8TO6_DITHER_ENABLE;
4967 } else {
4968 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
4969 return;
4970 }
4971
4972 if (!(tmp & PFIT_ENABLE))
4973 return;
4974
4975 pipe_config->gmch_pfit.control = I915_READ(PFIT_CONTROL);
4976 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
4977 if (INTEL_INFO(dev)->gen < 5)
4978 pipe_config->gmch_pfit.lvds_border_bits =
4979 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
4980}
4981
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01004982static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
4983 struct intel_crtc_config *pipe_config)
4984{
4985 struct drm_device *dev = crtc->base.dev;
4986 struct drm_i915_private *dev_priv = dev->dev_private;
4987 uint32_t tmp;
4988
Daniel Vettere143a212013-07-04 12:01:15 +02004989 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02004990 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02004991
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01004992 tmp = I915_READ(PIPECONF(crtc->pipe));
4993 if (!(tmp & PIPECONF_ENABLE))
4994 return false;
4995
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02004996 intel_get_pipe_timings(crtc, pipe_config);
4997
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02004998 i9xx_get_pfit_config(crtc, pipe_config);
4999
Daniel Vetter6c49f242013-06-06 12:45:25 +02005000 if (INTEL_INFO(dev)->gen >= 4) {
5001 tmp = I915_READ(DPLL_MD(crtc->pipe));
5002 pipe_config->pixel_multiplier =
5003 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
5004 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005005 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02005006 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
5007 tmp = I915_READ(DPLL(crtc->pipe));
5008 pipe_config->pixel_multiplier =
5009 ((tmp & SDVO_MULTIPLIER_MASK)
5010 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
5011 } else {
5012 /* Note that on i915G/GM the pixel multiplier is in the sdvo
5013 * port and will be fixed up in the encoder->get_config
5014 * function. */
5015 pipe_config->pixel_multiplier = 1;
5016 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005017 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
5018 if (!IS_VALLEYVIEW(dev)) {
5019 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
5020 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03005021 } else {
5022 /* Mask out read-only status bits. */
5023 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
5024 DPLL_PORTC_READY_MASK |
5025 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005026 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02005027
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005028 return true;
5029}
5030
Paulo Zanonidde86e22012-12-01 12:04:25 -02005031static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07005032{
5033 struct drm_i915_private *dev_priv = dev->dev_private;
5034 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005035 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005036 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005037 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005038 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005039 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07005040 bool has_ck505 = false;
5041 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005042
5043 /* We need to take the global config into account */
Keith Packard199e5d72011-09-22 12:01:57 -07005044 list_for_each_entry(encoder, &mode_config->encoder_list,
5045 base.head) {
5046 switch (encoder->type) {
5047 case INTEL_OUTPUT_LVDS:
5048 has_panel = true;
5049 has_lvds = true;
5050 break;
5051 case INTEL_OUTPUT_EDP:
5052 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03005053 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07005054 has_cpu_edp = true;
5055 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005056 }
5057 }
5058
Keith Packard99eb6a02011-09-26 14:29:12 -07005059 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005060 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07005061 can_ssc = has_ck505;
5062 } else {
5063 has_ck505 = false;
5064 can_ssc = true;
5065 }
5066
Imre Deak2de69052013-05-08 13:14:04 +03005067 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
5068 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005069
5070 /* Ironlake: try to setup display ref clock before DPLL
5071 * enabling. This is only under driver's control after
5072 * PCH B stepping, previous chipset stepping should be
5073 * ignoring this setting.
5074 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005075 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005076
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005077 /* As we must carefully and slowly disable/enable each source in turn,
5078 * compute the final state we want first and check if we need to
5079 * make any changes at all.
5080 */
5081 final = val;
5082 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07005083 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005084 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07005085 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005086 final |= DREF_NONSPREAD_SOURCE_ENABLE;
5087
5088 final &= ~DREF_SSC_SOURCE_MASK;
5089 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5090 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005091
Keith Packard199e5d72011-09-22 12:01:57 -07005092 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005093 final |= DREF_SSC_SOURCE_ENABLE;
5094
5095 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5096 final |= DREF_SSC1_ENABLE;
5097
5098 if (has_cpu_edp) {
5099 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5100 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
5101 else
5102 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
5103 } else
5104 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5105 } else {
5106 final |= DREF_SSC_SOURCE_DISABLE;
5107 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5108 }
5109
5110 if (final == val)
5111 return;
5112
5113 /* Always enable nonspread source */
5114 val &= ~DREF_NONSPREAD_SOURCE_MASK;
5115
5116 if (has_ck505)
5117 val |= DREF_NONSPREAD_CK505_ENABLE;
5118 else
5119 val |= DREF_NONSPREAD_SOURCE_ENABLE;
5120
5121 if (has_panel) {
5122 val &= ~DREF_SSC_SOURCE_MASK;
5123 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005124
Keith Packard199e5d72011-09-22 12:01:57 -07005125 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07005126 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005127 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005128 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02005129 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005130 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005131
5132 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005133 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005134 POSTING_READ(PCH_DREF_CONTROL);
5135 udelay(200);
5136
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005137 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005138
5139 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07005140 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07005141 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005142 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005143 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005144 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07005145 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005146 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005147 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005148 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005149
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005150 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005151 POSTING_READ(PCH_DREF_CONTROL);
5152 udelay(200);
5153 } else {
5154 DRM_DEBUG_KMS("Disabling SSC entirely\n");
5155
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005156 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07005157
5158 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005159 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005160
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005161 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005162 POSTING_READ(PCH_DREF_CONTROL);
5163 udelay(200);
5164
5165 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005166 val &= ~DREF_SSC_SOURCE_MASK;
5167 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005168
5169 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005170 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005171
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005172 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005173 POSTING_READ(PCH_DREF_CONTROL);
5174 udelay(200);
5175 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005176
5177 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005178}
5179
Paulo Zanonidde86e22012-12-01 12:04:25 -02005180/* Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O. */
5181static void lpt_init_pch_refclk(struct drm_device *dev)
5182{
5183 struct drm_i915_private *dev_priv = dev->dev_private;
5184 struct drm_mode_config *mode_config = &dev->mode_config;
5185 struct intel_encoder *encoder;
5186 bool has_vga = false;
5187 bool is_sdv = false;
5188 u32 tmp;
5189
5190 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5191 switch (encoder->type) {
5192 case INTEL_OUTPUT_ANALOG:
5193 has_vga = true;
5194 break;
5195 }
5196 }
5197
5198 if (!has_vga)
5199 return;
5200
Daniel Vetterc00db242013-01-22 15:33:27 +01005201 mutex_lock(&dev_priv->dpio_lock);
5202
Paulo Zanonidde86e22012-12-01 12:04:25 -02005203 /* XXX: Rip out SDV support once Haswell ships for real. */
5204 if (IS_HASWELL(dev) && (dev->pci_device & 0xFF00) == 0x0C00)
5205 is_sdv = true;
5206
5207 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5208 tmp &= ~SBI_SSCCTL_DISABLE;
5209 tmp |= SBI_SSCCTL_PATHALT;
5210 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5211
5212 udelay(24);
5213
5214 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5215 tmp &= ~SBI_SSCCTL_PATHALT;
5216 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5217
5218 if (!is_sdv) {
5219 tmp = I915_READ(SOUTH_CHICKEN2);
5220 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
5221 I915_WRITE(SOUTH_CHICKEN2, tmp);
5222
5223 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
5224 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
5225 DRM_ERROR("FDI mPHY reset assert timeout\n");
5226
5227 tmp = I915_READ(SOUTH_CHICKEN2);
5228 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
5229 I915_WRITE(SOUTH_CHICKEN2, tmp);
5230
5231 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
5232 FDI_MPHY_IOSFSB_RESET_STATUS) == 0,
5233 100))
5234 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
5235 }
5236
5237 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
5238 tmp &= ~(0xFF << 24);
5239 tmp |= (0x12 << 24);
5240 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
5241
Paulo Zanonidde86e22012-12-01 12:04:25 -02005242 if (is_sdv) {
5243 tmp = intel_sbi_read(dev_priv, 0x800C, SBI_MPHY);
5244 tmp |= 0x7FFF;
5245 intel_sbi_write(dev_priv, 0x800C, tmp, SBI_MPHY);
5246 }
5247
5248 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
5249 tmp |= (1 << 11);
5250 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
5251
5252 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
5253 tmp |= (1 << 11);
5254 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
5255
5256 if (is_sdv) {
5257 tmp = intel_sbi_read(dev_priv, 0x2038, SBI_MPHY);
5258 tmp |= (0x3F << 24) | (0xF << 20) | (0xF << 16);
5259 intel_sbi_write(dev_priv, 0x2038, tmp, SBI_MPHY);
5260
5261 tmp = intel_sbi_read(dev_priv, 0x2138, SBI_MPHY);
5262 tmp |= (0x3F << 24) | (0xF << 20) | (0xF << 16);
5263 intel_sbi_write(dev_priv, 0x2138, tmp, SBI_MPHY);
5264
5265 tmp = intel_sbi_read(dev_priv, 0x203C, SBI_MPHY);
5266 tmp |= (0x3F << 8);
5267 intel_sbi_write(dev_priv, 0x203C, tmp, SBI_MPHY);
5268
5269 tmp = intel_sbi_read(dev_priv, 0x213C, SBI_MPHY);
5270 tmp |= (0x3F << 8);
5271 intel_sbi_write(dev_priv, 0x213C, tmp, SBI_MPHY);
5272 }
5273
5274 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
5275 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5276 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
5277
5278 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
5279 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5280 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
5281
5282 if (!is_sdv) {
5283 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
5284 tmp &= ~(7 << 13);
5285 tmp |= (5 << 13);
5286 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
5287
5288 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
5289 tmp &= ~(7 << 13);
5290 tmp |= (5 << 13);
5291 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
5292 }
5293
5294 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
5295 tmp &= ~0xFF;
5296 tmp |= 0x1C;
5297 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
5298
5299 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
5300 tmp &= ~0xFF;
5301 tmp |= 0x1C;
5302 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
5303
5304 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
5305 tmp &= ~(0xFF << 16);
5306 tmp |= (0x1C << 16);
5307 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
5308
5309 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
5310 tmp &= ~(0xFF << 16);
5311 tmp |= (0x1C << 16);
5312 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
5313
5314 if (!is_sdv) {
5315 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
5316 tmp |= (1 << 27);
5317 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
5318
5319 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
5320 tmp |= (1 << 27);
5321 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
5322
5323 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
5324 tmp &= ~(0xF << 28);
5325 tmp |= (4 << 28);
5326 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
5327
5328 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
5329 tmp &= ~(0xF << 28);
5330 tmp |= (4 << 28);
5331 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
5332 }
5333
5334 /* ULT uses SBI_GEN0, but ULT doesn't have VGA, so we don't care. */
5335 tmp = intel_sbi_read(dev_priv, SBI_DBUFF0, SBI_ICLK);
5336 tmp |= SBI_DBUFF0_ENABLE;
5337 intel_sbi_write(dev_priv, SBI_DBUFF0, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01005338
5339 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005340}
5341
5342/*
5343 * Initialize reference clocks when the driver loads
5344 */
5345void intel_init_pch_refclk(struct drm_device *dev)
5346{
5347 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
5348 ironlake_init_pch_refclk(dev);
5349 else if (HAS_PCH_LPT(dev))
5350 lpt_init_pch_refclk(dev);
5351}
5352
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005353static int ironlake_get_refclk(struct drm_crtc *crtc)
5354{
5355 struct drm_device *dev = crtc->dev;
5356 struct drm_i915_private *dev_priv = dev->dev_private;
5357 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005358 int num_connectors = 0;
5359 bool is_lvds = false;
5360
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02005361 for_each_encoder_on_crtc(dev, crtc, encoder) {
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005362 switch (encoder->type) {
5363 case INTEL_OUTPUT_LVDS:
5364 is_lvds = true;
5365 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005366 }
5367 num_connectors++;
5368 }
5369
5370 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
5371 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005372 dev_priv->vbt.lvds_ssc_freq);
5373 return dev_priv->vbt.lvds_ssc_freq * 1000;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005374 }
5375
5376 return 120000;
5377}
5378
Daniel Vetter6ff93602013-04-19 11:24:36 +02005379static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03005380{
5381 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5382 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5383 int pipe = intel_crtc->pipe;
5384 uint32_t val;
5385
Daniel Vetter78114072013-06-13 00:54:57 +02005386 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03005387
Daniel Vetter965e0c42013-03-27 00:44:57 +01005388 switch (intel_crtc->config.pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03005389 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005390 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005391 break;
5392 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005393 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005394 break;
5395 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005396 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005397 break;
5398 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005399 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005400 break;
5401 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03005402 /* Case prevented by intel_choose_pipe_bpp_dither. */
5403 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03005404 }
5405
Daniel Vetterd8b32242013-04-25 17:54:44 +02005406 if (intel_crtc->config.dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03005407 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5408
Daniel Vetter6ff93602013-04-19 11:24:36 +02005409 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03005410 val |= PIPECONF_INTERLACED_ILK;
5411 else
5412 val |= PIPECONF_PROGRESSIVE;
5413
Daniel Vetter50f3b012013-03-27 00:44:56 +01005414 if (intel_crtc->config.limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005415 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005416
Paulo Zanonic8203562012-09-12 10:06:29 -03005417 I915_WRITE(PIPECONF(pipe), val);
5418 POSTING_READ(PIPECONF(pipe));
5419}
5420
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005421/*
5422 * Set up the pipe CSC unit.
5423 *
5424 * Currently only full range RGB to limited range RGB conversion
5425 * is supported, but eventually this should handle various
5426 * RGB<->YCbCr scenarios as well.
5427 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01005428static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005429{
5430 struct drm_device *dev = crtc->dev;
5431 struct drm_i915_private *dev_priv = dev->dev_private;
5432 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5433 int pipe = intel_crtc->pipe;
5434 uint16_t coeff = 0x7800; /* 1.0 */
5435
5436 /*
5437 * TODO: Check what kind of values actually come out of the pipe
5438 * with these coeff/postoff values and adjust to get the best
5439 * accuracy. Perhaps we even need to take the bpc value into
5440 * consideration.
5441 */
5442
Daniel Vetter50f3b012013-03-27 00:44:56 +01005443 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005444 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
5445
5446 /*
5447 * GY/GU and RY/RU should be the other way around according
5448 * to BSpec, but reality doesn't agree. Just set them up in
5449 * a way that results in the correct picture.
5450 */
5451 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
5452 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
5453
5454 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
5455 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
5456
5457 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
5458 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
5459
5460 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
5461 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
5462 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
5463
5464 if (INTEL_INFO(dev)->gen > 6) {
5465 uint16_t postoff = 0;
5466
Daniel Vetter50f3b012013-03-27 00:44:56 +01005467 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005468 postoff = (16 * (1 << 13) / 255) & 0x1fff;
5469
5470 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
5471 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
5472 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
5473
5474 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
5475 } else {
5476 uint32_t mode = CSC_MODE_YUV_TO_RGB;
5477
Daniel Vetter50f3b012013-03-27 00:44:56 +01005478 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005479 mode |= CSC_BLACK_SCREEN_OFFSET;
5480
5481 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
5482 }
5483}
5484
Daniel Vetter6ff93602013-04-19 11:24:36 +02005485static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005486{
5487 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5488 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02005489 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005490 uint32_t val;
5491
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02005492 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005493
Daniel Vetterd8b32242013-04-25 17:54:44 +02005494 if (intel_crtc->config.dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005495 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5496
Daniel Vetter6ff93602013-04-19 11:24:36 +02005497 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005498 val |= PIPECONF_INTERLACED_ILK;
5499 else
5500 val |= PIPECONF_PROGRESSIVE;
5501
Paulo Zanoni702e7a52012-10-23 18:29:59 -02005502 I915_WRITE(PIPECONF(cpu_transcoder), val);
5503 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02005504
5505 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
5506 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005507}
5508
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005509static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005510 intel_clock_t *clock,
5511 bool *has_reduced_clock,
5512 intel_clock_t *reduced_clock)
5513{
5514 struct drm_device *dev = crtc->dev;
5515 struct drm_i915_private *dev_priv = dev->dev_private;
5516 struct intel_encoder *intel_encoder;
5517 int refclk;
5518 const intel_limit_t *limit;
Daniel Vettera16af7212013-04-30 14:01:44 +02005519 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005520
5521 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5522 switch (intel_encoder->type) {
5523 case INTEL_OUTPUT_LVDS:
5524 is_lvds = true;
5525 break;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005526 }
5527 }
5528
5529 refclk = ironlake_get_refclk(crtc);
5530
5531 /*
5532 * Returns a set of divisors for the desired target clock with the given
5533 * refclk, or FALSE. The returned values represent the clock equation:
5534 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
5535 */
5536 limit = intel_limit(crtc, refclk);
Daniel Vetterff9a6752013-06-01 17:16:21 +02005537 ret = dev_priv->display.find_dpll(limit, crtc,
5538 to_intel_crtc(crtc)->config.port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02005539 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005540 if (!ret)
5541 return false;
5542
5543 if (is_lvds && dev_priv->lvds_downclock_avail) {
5544 /*
5545 * Ensure we match the reduced clock's P to the target clock.
5546 * If the clocks don't match, we can't switch the display clock
5547 * by using the FP0/FP1. In such case we will disable the LVDS
5548 * downclock feature.
5549 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02005550 *has_reduced_clock =
5551 dev_priv->display.find_dpll(limit, crtc,
5552 dev_priv->lvds_downclock,
5553 refclk, clock,
5554 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005555 }
5556
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005557 return true;
5558}
5559
Daniel Vetter01a415f2012-10-27 15:58:40 +02005560static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
5561{
5562 struct drm_i915_private *dev_priv = dev->dev_private;
5563 uint32_t temp;
5564
5565 temp = I915_READ(SOUTH_CHICKEN1);
5566 if (temp & FDI_BC_BIFURCATION_SELECT)
5567 return;
5568
5569 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
5570 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
5571
5572 temp |= FDI_BC_BIFURCATION_SELECT;
5573 DRM_DEBUG_KMS("enabling fdi C rx\n");
5574 I915_WRITE(SOUTH_CHICKEN1, temp);
5575 POSTING_READ(SOUTH_CHICKEN1);
5576}
5577
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005578static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
Daniel Vetter01a415f2012-10-27 15:58:40 +02005579{
5580 struct drm_device *dev = intel_crtc->base.dev;
5581 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005582
5583 switch (intel_crtc->pipe) {
5584 case PIPE_A:
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005585 break;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005586 case PIPE_B:
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005587 if (intel_crtc->config.fdi_lanes > 2)
Daniel Vetter01a415f2012-10-27 15:58:40 +02005588 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
5589 else
5590 cpt_enable_fdi_bc_bifurcation(dev);
5591
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005592 break;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005593 case PIPE_C:
Daniel Vetter01a415f2012-10-27 15:58:40 +02005594 cpt_enable_fdi_bc_bifurcation(dev);
5595
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005596 break;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005597 default:
5598 BUG();
5599 }
5600}
5601
Paulo Zanonid4b19312012-11-29 11:29:32 -02005602int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
5603{
5604 /*
5605 * Account for spread spectrum to avoid
5606 * oversubscribing the link. Max center spread
5607 * is 2.5%; use 5% for safety's sake.
5608 */
5609 u32 bps = target_clock * bpp * 21 / 20;
5610 return bps / (link_bw * 8) + 1;
5611}
5612
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005613static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02005614{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005615 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005616}
5617
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005618static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005619 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02005620 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005621{
5622 struct drm_crtc *crtc = &intel_crtc->base;
5623 struct drm_device *dev = crtc->dev;
5624 struct drm_i915_private *dev_priv = dev->dev_private;
5625 struct intel_encoder *intel_encoder;
5626 uint32_t dpll;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01005627 int factor, num_connectors = 0;
Daniel Vetter09ede542013-04-30 14:01:45 +02005628 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005629
5630 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5631 switch (intel_encoder->type) {
5632 case INTEL_OUTPUT_LVDS:
5633 is_lvds = true;
5634 break;
5635 case INTEL_OUTPUT_SDVO:
5636 case INTEL_OUTPUT_HDMI:
5637 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005638 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005639 }
5640
5641 num_connectors++;
5642 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005643
Chris Wilsonc1858122010-12-03 21:35:48 +00005644 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07005645 factor = 21;
5646 if (is_lvds) {
5647 if ((intel_panel_use_ssc(dev_priv) &&
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005648 dev_priv->vbt.lvds_ssc_freq == 100) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02005649 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07005650 factor = 25;
Daniel Vetter09ede542013-04-30 14:01:45 +02005651 } else if (intel_crtc->config.sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07005652 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00005653
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005654 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02005655 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00005656
Daniel Vetter9a7c7892013-04-04 22:20:34 +02005657 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
5658 *fp2 |= FP_CB_TUNE;
5659
Chris Wilson5eddb702010-09-11 13:48:45 +01005660 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005661
Eric Anholta07d6782011-03-30 13:01:08 -07005662 if (is_lvds)
5663 dpll |= DPLLB_MODE_LVDS;
5664 else
5665 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02005666
Daniel Vetteref1b4602013-06-01 17:17:04 +02005667 dpll |= (intel_crtc->config.pixel_multiplier - 1)
5668 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02005669
5670 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02005671 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter9566e9a2013-04-19 11:14:36 +02005672 if (intel_crtc->config.has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02005673 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08005674
Eric Anholta07d6782011-03-30 13:01:08 -07005675 /* compute bitmask from p1 value */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005676 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005677 /* also FPA1 */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005678 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005679
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005680 switch (intel_crtc->config.dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07005681 case 5:
5682 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5683 break;
5684 case 7:
5685 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5686 break;
5687 case 10:
5688 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5689 break;
5690 case 14:
5691 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5692 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005693 }
5694
Daniel Vetterb4c09f32013-04-30 14:01:42 +02005695 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005696 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08005697 else
5698 dpll |= PLL_REF_INPUT_DREFCLK;
5699
Daniel Vetter959e16d2013-06-05 13:34:21 +02005700 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005701}
5702
Jesse Barnes79e53942008-11-07 14:24:08 -08005703static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
Jesse Barnes79e53942008-11-07 14:24:08 -08005704 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005705 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005706{
5707 struct drm_device *dev = crtc->dev;
5708 struct drm_i915_private *dev_priv = dev->dev_private;
5709 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5710 int pipe = intel_crtc->pipe;
5711 int plane = intel_crtc->plane;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005712 int num_connectors = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005713 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02005714 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03005715 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01005716 bool is_lvds = false;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005717 struct intel_encoder *encoder;
Daniel Vettere2b78262013-06-07 23:10:03 +02005718 struct intel_shared_dpll *pll;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005719 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08005720
5721 for_each_encoder_on_crtc(dev, crtc, encoder) {
5722 switch (encoder->type) {
5723 case INTEL_OUTPUT_LVDS:
5724 is_lvds = true;
5725 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005726 }
5727
5728 num_connectors++;
5729 }
5730
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005731 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
5732 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
5733
Daniel Vetterff9a6752013-06-01 17:16:21 +02005734 ok = ironlake_compute_clocks(crtc, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005735 &has_reduced_clock, &reduced_clock);
Daniel Vetteree9300b2013-06-03 22:40:22 +02005736 if (!ok && !intel_crtc->config.clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08005737 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5738 return -EINVAL;
5739 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01005740 /* Compat-code for transition, will disappear. */
5741 if (!intel_crtc->config.clock_set) {
5742 intel_crtc->config.dpll.n = clock.n;
5743 intel_crtc->config.dpll.m1 = clock.m1;
5744 intel_crtc->config.dpll.m2 = clock.m2;
5745 intel_crtc->config.dpll.p1 = clock.p1;
5746 intel_crtc->config.dpll.p2 = clock.p2;
5747 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005748
5749 /* Ensure that the cursor is valid for the new mode before changing... */
5750 intel_crtc_update_cursor(crtc, true);
5751
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005752 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
Daniel Vetter8b470472013-03-28 10:41:59 +01005753 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005754 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02005755 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005756 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02005757
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005758 dpll = ironlake_compute_dpll(intel_crtc,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02005759 &fp, &reduced_clock,
5760 has_reduced_clock ? &fp2 : NULL);
5761
Daniel Vetter959e16d2013-06-05 13:34:21 +02005762 intel_crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02005763 intel_crtc->config.dpll_hw_state.fp0 = fp;
5764 if (has_reduced_clock)
5765 intel_crtc->config.dpll_hw_state.fp1 = fp2;
5766 else
5767 intel_crtc->config.dpll_hw_state.fp1 = fp;
5768
Daniel Vetterb89a1d32013-06-05 13:34:24 +02005769 pll = intel_get_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005770 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03005771 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
5772 pipe_name(pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07005773 return -EINVAL;
5774 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005775 } else
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005776 intel_put_shared_dpll(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08005777
Daniel Vetter03afc4a2013-04-02 23:42:31 +02005778 if (intel_crtc->config.has_dp_encoder)
5779 intel_dp_set_m_n(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08005780
Daniel Vetterbcd644e2013-06-05 13:34:22 +02005781 if (is_lvds && has_reduced_clock && i915_powersave)
5782 intel_crtc->lowfreq_avail = true;
5783 else
5784 intel_crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02005785
5786 if (intel_crtc->config.has_pch_encoder) {
5787 pll = intel_crtc_to_shared_dpll(intel_crtc);
5788
Jesse Barnes79e53942008-11-07 14:24:08 -08005789 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005790
Daniel Vetter8a654f32013-06-01 17:16:22 +02005791 intel_set_pipe_timings(intel_crtc);
Krzysztof Halasa734b4152010-05-25 18:41:46 +02005792
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01005793 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01005794 intel_cpu_transcoder_set_m_n(intel_crtc,
5795 &intel_crtc->config.fdi_m_n);
5796 }
Chris Wilson5eddb702010-09-11 13:48:45 +01005797
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005798 if (IS_IVYBRIDGE(dev))
5799 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
Zhenyu Wang2c072452009-06-05 15:38:42 +08005800
Daniel Vetter6ff93602013-04-19 11:24:36 +02005801 ironlake_set_pipeconf(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08005802
Paulo Zanonia1f9e772012-09-12 10:06:32 -03005803 /* Set up the display plane register */
5804 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08005805 POSTING_READ(DSPCNTR(plane));
Jesse Barnes79e53942008-11-07 14:24:08 -08005806
Daniel Vetter94352cf2012-07-05 22:51:56 +02005807 ret = intel_pipe_set_base(crtc, x, y, fb);
Shaohua Li7662c8b2009-06-26 11:23:55 +08005808
5809 intel_update_watermarks(dev);
5810
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005811 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08005812}
5813
Daniel Vetter72419202013-04-04 13:28:53 +02005814static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5815 struct intel_crtc_config *pipe_config)
5816{
5817 struct drm_device *dev = crtc->base.dev;
5818 struct drm_i915_private *dev_priv = dev->dev_private;
5819 enum transcoder transcoder = pipe_config->cpu_transcoder;
5820
5821 pipe_config->fdi_m_n.link_m = I915_READ(PIPE_LINK_M1(transcoder));
5822 pipe_config->fdi_m_n.link_n = I915_READ(PIPE_LINK_N1(transcoder));
5823 pipe_config->fdi_m_n.gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
5824 & ~TU_SIZE_MASK;
5825 pipe_config->fdi_m_n.gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
5826 pipe_config->fdi_m_n.tu = ((I915_READ(PIPE_DATA_M1(transcoder))
5827 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
5828}
5829
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005830static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5831 struct intel_crtc_config *pipe_config)
5832{
5833 struct drm_device *dev = crtc->base.dev;
5834 struct drm_i915_private *dev_priv = dev->dev_private;
5835 uint32_t tmp;
5836
5837 tmp = I915_READ(PF_CTL(crtc->pipe));
5838
5839 if (tmp & PF_ENABLE) {
5840 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
5841 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02005842
5843 /* We currently do not free assignements of panel fitters on
5844 * ivb/hsw (since we don't use the higher upscaling modes which
5845 * differentiates them) so just WARN about this case for now. */
5846 if (IS_GEN7(dev)) {
5847 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
5848 PF_PIPE_SEL_IVB(crtc->pipe));
5849 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005850 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005851}
5852
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005853static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5854 struct intel_crtc_config *pipe_config)
5855{
5856 struct drm_device *dev = crtc->base.dev;
5857 struct drm_i915_private *dev_priv = dev->dev_private;
5858 uint32_t tmp;
5859
Daniel Vettere143a212013-07-04 12:01:15 +02005860 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02005861 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02005862
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005863 tmp = I915_READ(PIPECONF(crtc->pipe));
5864 if (!(tmp & PIPECONF_ENABLE))
5865 return false;
5866
Daniel Vetterab9412b2013-05-03 11:49:46 +02005867 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02005868 struct intel_shared_dpll *pll;
5869
Daniel Vetter88adfff2013-03-28 10:42:01 +01005870 pipe_config->has_pch_encoder = true;
5871
Daniel Vetter627eb5a2013-04-29 19:33:42 +02005872 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
5873 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
5874 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02005875
5876 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02005877
Daniel Vetterc0d43d62013-06-07 23:11:08 +02005878 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02005879 pipe_config->shared_dpll =
5880 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02005881 } else {
5882 tmp = I915_READ(PCH_DPLL_SEL);
5883 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
5884 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
5885 else
5886 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
5887 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02005888
5889 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
5890
5891 WARN_ON(!pll->get_hw_state(dev_priv, pll,
5892 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02005893
5894 tmp = pipe_config->dpll_hw_state.dpll;
5895 pipe_config->pixel_multiplier =
5896 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
5897 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter6c49f242013-06-06 12:45:25 +02005898 } else {
5899 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02005900 }
5901
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005902 intel_get_pipe_timings(crtc, pipe_config);
5903
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005904 ironlake_get_pfit_config(crtc, pipe_config);
5905
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005906 return true;
5907}
5908
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005909static void haswell_modeset_global_resources(struct drm_device *dev)
5910{
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005911 bool enable = false;
5912 struct intel_crtc *crtc;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005913
5914 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
Daniel Vettere7a639c2013-05-31 17:49:17 +02005915 if (!crtc->base.enabled)
5916 continue;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005917
Daniel Vettere7a639c2013-05-31 17:49:17 +02005918 if (crtc->pipe != PIPE_A || crtc->config.pch_pfit.size ||
5919 crtc->config.cpu_transcoder != TRANSCODER_EDP)
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005920 enable = true;
5921 }
5922
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005923 intel_set_power_well(dev, enable);
5924}
5925
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005926static int haswell_crtc_mode_set(struct drm_crtc *crtc,
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005927 int x, int y,
5928 struct drm_framebuffer *fb)
5929{
5930 struct drm_device *dev = crtc->dev;
5931 struct drm_i915_private *dev_priv = dev->dev_private;
5932 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005933 int plane = intel_crtc->plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005934 int ret;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005935
Daniel Vetterff9a6752013-06-01 17:16:21 +02005936 if (!intel_ddi_pll_mode_set(crtc))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03005937 return -EINVAL;
5938
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005939 /* Ensure that the cursor is valid for the new mode before changing... */
5940 intel_crtc_update_cursor(crtc, true);
5941
Daniel Vetter03afc4a2013-04-02 23:42:31 +02005942 if (intel_crtc->config.has_dp_encoder)
5943 intel_dp_set_m_n(intel_crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005944
5945 intel_crtc->lowfreq_avail = false;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005946
Daniel Vetter8a654f32013-06-01 17:16:22 +02005947 intel_set_pipe_timings(intel_crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005948
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01005949 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01005950 intel_cpu_transcoder_set_m_n(intel_crtc,
5951 &intel_crtc->config.fdi_m_n);
5952 }
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005953
Daniel Vetter6ff93602013-04-19 11:24:36 +02005954 haswell_set_pipeconf(crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005955
Daniel Vetter50f3b012013-03-27 00:44:56 +01005956 intel_set_pipe_csc(crtc);
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005957
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005958 /* Set up the display plane register */
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005959 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005960 POSTING_READ(DSPCNTR(plane));
5961
5962 ret = intel_pipe_set_base(crtc, x, y, fb);
5963
5964 intel_update_watermarks(dev);
5965
Jesse Barnes79e53942008-11-07 14:24:08 -08005966 return ret;
5967}
5968
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005969static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5970 struct intel_crtc_config *pipe_config)
5971{
5972 struct drm_device *dev = crtc->base.dev;
5973 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005974 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005975 uint32_t tmp;
5976
Daniel Vettere143a212013-07-04 12:01:15 +02005977 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02005978 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
5979
Daniel Vettereccb1402013-05-22 00:50:22 +02005980 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
5981 if (tmp & TRANS_DDI_FUNC_ENABLE) {
5982 enum pipe trans_edp_pipe;
5983 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
5984 default:
5985 WARN(1, "unknown pipe linked to edp transcoder\n");
5986 case TRANS_DDI_EDP_INPUT_A_ONOFF:
5987 case TRANS_DDI_EDP_INPUT_A_ON:
5988 trans_edp_pipe = PIPE_A;
5989 break;
5990 case TRANS_DDI_EDP_INPUT_B_ONOFF:
5991 trans_edp_pipe = PIPE_B;
5992 break;
5993 case TRANS_DDI_EDP_INPUT_C_ONOFF:
5994 trans_edp_pipe = PIPE_C;
5995 break;
5996 }
5997
5998 if (trans_edp_pipe == crtc->pipe)
5999 pipe_config->cpu_transcoder = TRANSCODER_EDP;
6000 }
6001
Paulo Zanonib97186f2013-05-03 12:15:36 -03006002 if (!intel_display_power_enabled(dev,
Daniel Vettereccb1402013-05-22 00:50:22 +02006003 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03006004 return false;
6005
Daniel Vettereccb1402013-05-22 00:50:22 +02006006 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006007 if (!(tmp & PIPECONF_ENABLE))
6008 return false;
6009
Daniel Vetter88adfff2013-03-28 10:42:01 +01006010 /*
Paulo Zanonif196e6b2013-04-18 16:35:41 -03006011 * Haswell has only FDI/PCH transcoder A. It is which is connected to
Daniel Vetter88adfff2013-03-28 10:42:01 +01006012 * DDI E. So just check whether this pipe is wired to DDI E and whether
6013 * the PCH transcoder is on.
6014 */
Daniel Vettereccb1402013-05-22 00:50:22 +02006015 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
Daniel Vetter88adfff2013-03-28 10:42:01 +01006016 if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
Daniel Vetterab9412b2013-05-03 11:49:46 +02006017 I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter88adfff2013-03-28 10:42:01 +01006018 pipe_config->has_pch_encoder = true;
6019
Daniel Vetter627eb5a2013-04-29 19:33:42 +02006020 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
6021 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6022 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02006023
6024 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02006025 }
6026
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006027 intel_get_pipe_timings(crtc, pipe_config);
6028
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006029 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
6030 if (intel_display_power_enabled(dev, pfit_domain))
6031 ironlake_get_pfit_config(crtc, pipe_config);
Daniel Vetter88adfff2013-03-28 10:42:01 +01006032
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006033 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
6034 (I915_READ(IPS_CTL) & IPS_ENABLE);
6035
Daniel Vetter6c49f242013-06-06 12:45:25 +02006036 pipe_config->pixel_multiplier = 1;
6037
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006038 return true;
6039}
6040
Eric Anholtf564048e2011-03-30 13:01:02 -07006041static int intel_crtc_mode_set(struct drm_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07006042 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02006043 struct drm_framebuffer *fb)
Eric Anholtf564048e2011-03-30 13:01:02 -07006044{
6045 struct drm_device *dev = crtc->dev;
6046 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter9256aa12012-10-31 19:26:13 +01006047 struct drm_encoder_helper_funcs *encoder_funcs;
6048 struct intel_encoder *encoder;
Eric Anholt0b701d22011-03-30 13:01:03 -07006049 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01006050 struct drm_display_mode *adjusted_mode =
6051 &intel_crtc->config.adjusted_mode;
6052 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
Eric Anholt0b701d22011-03-30 13:01:03 -07006053 int pipe = intel_crtc->pipe;
Eric Anholtf564048e2011-03-30 13:01:02 -07006054 int ret;
6055
Eric Anholt0b701d22011-03-30 13:01:03 -07006056 drm_vblank_pre_modeset(dev, pipe);
6057
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01006058 ret = dev_priv->display.crtc_mode_set(crtc, x, y, fb);
6059
Jesse Barnes79e53942008-11-07 14:24:08 -08006060 drm_vblank_post_modeset(dev, pipe);
6061
Daniel Vetter9256aa12012-10-31 19:26:13 +01006062 if (ret != 0)
6063 return ret;
6064
6065 for_each_encoder_on_crtc(dev, crtc, encoder) {
6066 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
6067 encoder->base.base.id,
6068 drm_get_encoder_name(&encoder->base),
6069 mode->base.id, mode->name);
Daniel Vetter6cc5f342013-03-27 00:44:53 +01006070 if (encoder->mode_set) {
6071 encoder->mode_set(encoder);
6072 } else {
6073 encoder_funcs = encoder->base.helper_private;
6074 encoder_funcs->mode_set(&encoder->base, mode, adjusted_mode);
6075 }
Daniel Vetter9256aa12012-10-31 19:26:13 +01006076 }
6077
6078 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006079}
6080
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006081static bool intel_eld_uptodate(struct drm_connector *connector,
6082 int reg_eldv, uint32_t bits_eldv,
6083 int reg_elda, uint32_t bits_elda,
6084 int reg_edid)
6085{
6086 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6087 uint8_t *eld = connector->eld;
6088 uint32_t i;
6089
6090 i = I915_READ(reg_eldv);
6091 i &= bits_eldv;
6092
6093 if (!eld[0])
6094 return !i;
6095
6096 if (!i)
6097 return false;
6098
6099 i = I915_READ(reg_elda);
6100 i &= ~bits_elda;
6101 I915_WRITE(reg_elda, i);
6102
6103 for (i = 0; i < eld[2]; i++)
6104 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
6105 return false;
6106
6107 return true;
6108}
6109
Wu Fengguange0dac652011-09-05 14:25:34 +08006110static void g4x_write_eld(struct drm_connector *connector,
6111 struct drm_crtc *crtc)
6112{
6113 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6114 uint8_t *eld = connector->eld;
6115 uint32_t eldv;
6116 uint32_t len;
6117 uint32_t i;
6118
6119 i = I915_READ(G4X_AUD_VID_DID);
6120
6121 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
6122 eldv = G4X_ELDV_DEVCL_DEVBLC;
6123 else
6124 eldv = G4X_ELDV_DEVCTG;
6125
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006126 if (intel_eld_uptodate(connector,
6127 G4X_AUD_CNTL_ST, eldv,
6128 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
6129 G4X_HDMIW_HDMIEDID))
6130 return;
6131
Wu Fengguange0dac652011-09-05 14:25:34 +08006132 i = I915_READ(G4X_AUD_CNTL_ST);
6133 i &= ~(eldv | G4X_ELD_ADDR);
6134 len = (i >> 9) & 0x1f; /* ELD buffer size */
6135 I915_WRITE(G4X_AUD_CNTL_ST, i);
6136
6137 if (!eld[0])
6138 return;
6139
6140 len = min_t(uint8_t, eld[2], len);
6141 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6142 for (i = 0; i < len; i++)
6143 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
6144
6145 i = I915_READ(G4X_AUD_CNTL_ST);
6146 i |= eldv;
6147 I915_WRITE(G4X_AUD_CNTL_ST, i);
6148}
6149
Wang Xingchao83358c852012-08-16 22:43:37 +08006150static void haswell_write_eld(struct drm_connector *connector,
6151 struct drm_crtc *crtc)
6152{
6153 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6154 uint8_t *eld = connector->eld;
6155 struct drm_device *dev = crtc->dev;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08006156 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Wang Xingchao83358c852012-08-16 22:43:37 +08006157 uint32_t eldv;
6158 uint32_t i;
6159 int len;
6160 int pipe = to_intel_crtc(crtc)->pipe;
6161 int tmp;
6162
6163 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
6164 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
6165 int aud_config = HSW_AUD_CFG(pipe);
6166 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
6167
6168
6169 DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
6170
6171 /* Audio output enable */
6172 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
6173 tmp = I915_READ(aud_cntrl_st2);
6174 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
6175 I915_WRITE(aud_cntrl_st2, tmp);
6176
6177 /* Wait for 1 vertical blank */
6178 intel_wait_for_vblank(dev, pipe);
6179
6180 /* Set ELD valid state */
6181 tmp = I915_READ(aud_cntrl_st2);
6182 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%8x\n", tmp);
6183 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
6184 I915_WRITE(aud_cntrl_st2, tmp);
6185 tmp = I915_READ(aud_cntrl_st2);
6186 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%8x\n", tmp);
6187
6188 /* Enable HDMI mode */
6189 tmp = I915_READ(aud_config);
6190 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%8x\n", tmp);
6191 /* clear N_programing_enable and N_value_index */
6192 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
6193 I915_WRITE(aud_config, tmp);
6194
6195 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
6196
6197 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08006198 intel_crtc->eld_vld = true;
Wang Xingchao83358c852012-08-16 22:43:37 +08006199
6200 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6201 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6202 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
6203 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6204 } else
6205 I915_WRITE(aud_config, 0);
6206
6207 if (intel_eld_uptodate(connector,
6208 aud_cntrl_st2, eldv,
6209 aud_cntl_st, IBX_ELD_ADDRESS,
6210 hdmiw_hdmiedid))
6211 return;
6212
6213 i = I915_READ(aud_cntrl_st2);
6214 i &= ~eldv;
6215 I915_WRITE(aud_cntrl_st2, i);
6216
6217 if (!eld[0])
6218 return;
6219
6220 i = I915_READ(aud_cntl_st);
6221 i &= ~IBX_ELD_ADDRESS;
6222 I915_WRITE(aud_cntl_st, i);
6223 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
6224 DRM_DEBUG_DRIVER("port num:%d\n", i);
6225
6226 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6227 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6228 for (i = 0; i < len; i++)
6229 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6230
6231 i = I915_READ(aud_cntrl_st2);
6232 i |= eldv;
6233 I915_WRITE(aud_cntrl_st2, i);
6234
6235}
6236
Wu Fengguange0dac652011-09-05 14:25:34 +08006237static void ironlake_write_eld(struct drm_connector *connector,
6238 struct drm_crtc *crtc)
6239{
6240 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6241 uint8_t *eld = connector->eld;
6242 uint32_t eldv;
6243 uint32_t i;
6244 int len;
6245 int hdmiw_hdmiedid;
Wu Fengguangb6daa022012-01-06 14:41:31 -06006246 int aud_config;
Wu Fengguange0dac652011-09-05 14:25:34 +08006247 int aud_cntl_st;
6248 int aud_cntrl_st2;
Wang Xingchao9b138a82012-08-09 16:52:18 +08006249 int pipe = to_intel_crtc(crtc)->pipe;
Wu Fengguange0dac652011-09-05 14:25:34 +08006250
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08006251 if (HAS_PCH_IBX(connector->dev)) {
Wang Xingchao9b138a82012-08-09 16:52:18 +08006252 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
6253 aud_config = IBX_AUD_CFG(pipe);
6254 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006255 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08006256 } else {
Wang Xingchao9b138a82012-08-09 16:52:18 +08006257 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
6258 aud_config = CPT_AUD_CFG(pipe);
6259 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006260 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08006261 }
6262
Wang Xingchao9b138a82012-08-09 16:52:18 +08006263 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
Wu Fengguange0dac652011-09-05 14:25:34 +08006264
6265 i = I915_READ(aud_cntl_st);
Wang Xingchao9b138a82012-08-09 16:52:18 +08006266 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
Wu Fengguange0dac652011-09-05 14:25:34 +08006267 if (!i) {
6268 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
6269 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006270 eldv = IBX_ELD_VALIDB;
6271 eldv |= IBX_ELD_VALIDB << 4;
6272 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08006273 } else {
Ville Syrjälä2582a852013-04-17 17:48:47 +03006274 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006275 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08006276 }
6277
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006278 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6279 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6280 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
Wu Fengguangb6daa022012-01-06 14:41:31 -06006281 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6282 } else
6283 I915_WRITE(aud_config, 0);
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006284
6285 if (intel_eld_uptodate(connector,
6286 aud_cntrl_st2, eldv,
6287 aud_cntl_st, IBX_ELD_ADDRESS,
6288 hdmiw_hdmiedid))
6289 return;
6290
Wu Fengguange0dac652011-09-05 14:25:34 +08006291 i = I915_READ(aud_cntrl_st2);
6292 i &= ~eldv;
6293 I915_WRITE(aud_cntrl_st2, i);
6294
6295 if (!eld[0])
6296 return;
6297
Wu Fengguange0dac652011-09-05 14:25:34 +08006298 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006299 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08006300 I915_WRITE(aud_cntl_st, i);
6301
6302 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6303 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6304 for (i = 0; i < len; i++)
6305 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6306
6307 i = I915_READ(aud_cntrl_st2);
6308 i |= eldv;
6309 I915_WRITE(aud_cntrl_st2, i);
6310}
6311
6312void intel_write_eld(struct drm_encoder *encoder,
6313 struct drm_display_mode *mode)
6314{
6315 struct drm_crtc *crtc = encoder->crtc;
6316 struct drm_connector *connector;
6317 struct drm_device *dev = encoder->dev;
6318 struct drm_i915_private *dev_priv = dev->dev_private;
6319
6320 connector = drm_select_eld(encoder, mode);
6321 if (!connector)
6322 return;
6323
6324 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6325 connector->base.id,
6326 drm_get_connector_name(connector),
6327 connector->encoder->base.id,
6328 drm_get_encoder_name(connector->encoder));
6329
6330 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
6331
6332 if (dev_priv->display.write_eld)
6333 dev_priv->display.write_eld(connector, crtc);
6334}
6335
Jesse Barnes79e53942008-11-07 14:24:08 -08006336/** Loads the palette/gamma unit for the CRTC with the prepared values */
6337void intel_crtc_load_lut(struct drm_crtc *crtc)
6338{
6339 struct drm_device *dev = crtc->dev;
6340 struct drm_i915_private *dev_priv = dev->dev_private;
6341 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006342 enum pipe pipe = intel_crtc->pipe;
6343 int palreg = PALETTE(pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08006344 int i;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006345 bool reenable_ips = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006346
6347 /* The clocks have to be on to load the palette. */
Alban Browaeysaed3f092012-02-24 17:12:45 +00006348 if (!crtc->enabled || !intel_crtc->active)
Jesse Barnes79e53942008-11-07 14:24:08 -08006349 return;
6350
Ville Syrjälä14420bd2013-06-04 13:49:07 +03006351 if (!HAS_PCH_SPLIT(dev_priv->dev))
6352 assert_pll_enabled(dev_priv, pipe);
6353
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006354 /* use legacy palette for Ironlake */
Eric Anholtbad720f2009-10-22 16:11:14 -07006355 if (HAS_PCH_SPLIT(dev))
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006356 palreg = LGC_PALETTE(pipe);
6357
6358 /* Workaround : Do not read or write the pipe palette/gamma data while
6359 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
6360 */
6361 if (intel_crtc->config.ips_enabled &&
6362 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
6363 GAMMA_MODE_MODE_SPLIT)) {
6364 hsw_disable_ips(intel_crtc);
6365 reenable_ips = true;
6366 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08006367
Jesse Barnes79e53942008-11-07 14:24:08 -08006368 for (i = 0; i < 256; i++) {
6369 I915_WRITE(palreg + 4 * i,
6370 (intel_crtc->lut_r[i] << 16) |
6371 (intel_crtc->lut_g[i] << 8) |
6372 intel_crtc->lut_b[i]);
6373 }
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006374
6375 if (reenable_ips)
6376 hsw_enable_ips(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006377}
6378
Chris Wilson560b85b2010-08-07 11:01:38 +01006379static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
6380{
6381 struct drm_device *dev = crtc->dev;
6382 struct drm_i915_private *dev_priv = dev->dev_private;
6383 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6384 bool visible = base != 0;
6385 u32 cntl;
6386
6387 if (intel_crtc->cursor_visible == visible)
6388 return;
6389
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006390 cntl = I915_READ(_CURACNTR);
Chris Wilson560b85b2010-08-07 11:01:38 +01006391 if (visible) {
6392 /* On these chipsets we can only modify the base whilst
6393 * the cursor is disabled.
6394 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006395 I915_WRITE(_CURABASE, base);
Chris Wilson560b85b2010-08-07 11:01:38 +01006396
6397 cntl &= ~(CURSOR_FORMAT_MASK);
6398 /* XXX width must be 64, stride 256 => 0x00 << 28 */
6399 cntl |= CURSOR_ENABLE |
6400 CURSOR_GAMMA_ENABLE |
6401 CURSOR_FORMAT_ARGB;
6402 } else
6403 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006404 I915_WRITE(_CURACNTR, cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01006405
6406 intel_crtc->cursor_visible = visible;
6407}
6408
6409static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
6410{
6411 struct drm_device *dev = crtc->dev;
6412 struct drm_i915_private *dev_priv = dev->dev_private;
6413 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6414 int pipe = intel_crtc->pipe;
6415 bool visible = base != 0;
6416
6417 if (intel_crtc->cursor_visible != visible) {
Jesse Barnes548f2452011-02-17 10:40:53 -08006418 uint32_t cntl = I915_READ(CURCNTR(pipe));
Chris Wilson560b85b2010-08-07 11:01:38 +01006419 if (base) {
6420 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
6421 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6422 cntl |= pipe << 28; /* Connect to correct pipe */
6423 } else {
6424 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6425 cntl |= CURSOR_MODE_DISABLE;
6426 }
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006427 I915_WRITE(CURCNTR(pipe), cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01006428
6429 intel_crtc->cursor_visible = visible;
6430 }
6431 /* and commit changes on next vblank */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006432 I915_WRITE(CURBASE(pipe), base);
Chris Wilson560b85b2010-08-07 11:01:38 +01006433}
6434
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006435static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
6436{
6437 struct drm_device *dev = crtc->dev;
6438 struct drm_i915_private *dev_priv = dev->dev_private;
6439 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6440 int pipe = intel_crtc->pipe;
6441 bool visible = base != 0;
6442
6443 if (intel_crtc->cursor_visible != visible) {
6444 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
6445 if (base) {
6446 cntl &= ~CURSOR_MODE;
6447 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6448 } else {
6449 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6450 cntl |= CURSOR_MODE_DISABLE;
6451 }
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006452 if (IS_HASWELL(dev))
6453 cntl |= CURSOR_PIPE_CSC_ENABLE;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006454 I915_WRITE(CURCNTR_IVB(pipe), cntl);
6455
6456 intel_crtc->cursor_visible = visible;
6457 }
6458 /* and commit changes on next vblank */
6459 I915_WRITE(CURBASE_IVB(pipe), base);
6460}
6461
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006462/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01006463static void intel_crtc_update_cursor(struct drm_crtc *crtc,
6464 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006465{
6466 struct drm_device *dev = crtc->dev;
6467 struct drm_i915_private *dev_priv = dev->dev_private;
6468 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6469 int pipe = intel_crtc->pipe;
6470 int x = intel_crtc->cursor_x;
6471 int y = intel_crtc->cursor_y;
Chris Wilson560b85b2010-08-07 11:01:38 +01006472 u32 base, pos;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006473 bool visible;
6474
6475 pos = 0;
6476
Chris Wilson6b383a72010-09-13 13:54:26 +01006477 if (on && crtc->enabled && crtc->fb) {
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006478 base = intel_crtc->cursor_addr;
6479 if (x > (int) crtc->fb->width)
6480 base = 0;
6481
6482 if (y > (int) crtc->fb->height)
6483 base = 0;
6484 } else
6485 base = 0;
6486
6487 if (x < 0) {
6488 if (x + intel_crtc->cursor_width < 0)
6489 base = 0;
6490
6491 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
6492 x = -x;
6493 }
6494 pos |= x << CURSOR_X_SHIFT;
6495
6496 if (y < 0) {
6497 if (y + intel_crtc->cursor_height < 0)
6498 base = 0;
6499
6500 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
6501 y = -y;
6502 }
6503 pos |= y << CURSOR_Y_SHIFT;
6504
6505 visible = base != 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01006506 if (!visible && !intel_crtc->cursor_visible)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006507 return;
6508
Eugeni Dodonov0cd83aa2012-04-13 17:08:48 -03006509 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006510 I915_WRITE(CURPOS_IVB(pipe), pos);
6511 ivb_update_cursor(crtc, base);
6512 } else {
6513 I915_WRITE(CURPOS(pipe), pos);
6514 if (IS_845G(dev) || IS_I865G(dev))
6515 i845_update_cursor(crtc, base);
6516 else
6517 i9xx_update_cursor(crtc, base);
6518 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006519}
6520
Jesse Barnes79e53942008-11-07 14:24:08 -08006521static int intel_crtc_cursor_set(struct drm_crtc *crtc,
Chris Wilson05394f32010-11-08 19:18:58 +00006522 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08006523 uint32_t handle,
6524 uint32_t width, uint32_t height)
6525{
6526 struct drm_device *dev = crtc->dev;
6527 struct drm_i915_private *dev_priv = dev->dev_private;
6528 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00006529 struct drm_i915_gem_object *obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006530 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006531 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006532
Jesse Barnes79e53942008-11-07 14:24:08 -08006533 /* if we want to turn off the cursor ignore width and height */
6534 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08006535 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006536 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00006537 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10006538 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006539 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08006540 }
6541
6542 /* Currently we only support 64x64 cursors */
6543 if (width != 64 || height != 64) {
6544 DRM_ERROR("we currently only support 64x64 cursors\n");
6545 return -EINVAL;
6546 }
6547
Chris Wilson05394f32010-11-08 19:18:58 +00006548 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00006549 if (&obj->base == NULL)
Jesse Barnes79e53942008-11-07 14:24:08 -08006550 return -ENOENT;
6551
Chris Wilson05394f32010-11-08 19:18:58 +00006552 if (obj->base.size < width * height * 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006553 DRM_ERROR("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10006554 ret = -ENOMEM;
6555 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08006556 }
6557
Dave Airlie71acb5e2008-12-30 20:31:46 +10006558 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006559 mutex_lock(&dev->struct_mutex);
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05006560 if (!dev_priv->info->cursor_needs_physical) {
Chris Wilson693db182013-03-05 14:52:39 +00006561 unsigned alignment;
6562
Chris Wilsond9e86c02010-11-10 16:40:20 +00006563 if (obj->tiling_mode) {
6564 DRM_ERROR("cursor cannot be tiled\n");
6565 ret = -EINVAL;
6566 goto fail_locked;
6567 }
6568
Chris Wilson693db182013-03-05 14:52:39 +00006569 /* Note that the w/a also requires 2 PTE of padding following
6570 * the bo. We currently fill all unused PTE with the shadow
6571 * page and so we should always have valid PTE following the
6572 * cursor preventing the VT-d warning.
6573 */
6574 alignment = 0;
6575 if (need_vtd_wa(dev))
6576 alignment = 64*1024;
6577
6578 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01006579 if (ret) {
6580 DRM_ERROR("failed to move cursor bo into the GTT\n");
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006581 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006582 }
6583
Chris Wilsond9e86c02010-11-10 16:40:20 +00006584 ret = i915_gem_object_put_fence(obj);
6585 if (ret) {
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006586 DRM_ERROR("failed to release fence for cursor");
Chris Wilsond9e86c02010-11-10 16:40:20 +00006587 goto fail_unpin;
6588 }
6589
Ben Widawskyf343c5f2013-07-05 14:41:04 -07006590 addr = i915_gem_obj_ggtt_offset(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10006591 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01006592 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson05394f32010-11-08 19:18:58 +00006593 ret = i915_gem_attach_phys_object(dev, obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01006594 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
6595 align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10006596 if (ret) {
6597 DRM_ERROR("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006598 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10006599 }
Chris Wilson05394f32010-11-08 19:18:58 +00006600 addr = obj->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006601 }
6602
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006603 if (IS_GEN2(dev))
Jesse Barnes14b60392009-05-20 16:47:08 -04006604 I915_WRITE(CURSIZE, (height << 12) | width);
6605
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006606 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006607 if (intel_crtc->cursor_bo) {
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05006608 if (dev_priv->info->cursor_needs_physical) {
Chris Wilson05394f32010-11-08 19:18:58 +00006609 if (intel_crtc->cursor_bo != obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10006610 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
6611 } else
6612 i915_gem_object_unpin(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00006613 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006614 }
Jesse Barnes80824002009-09-10 15:28:06 -07006615
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006616 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006617
6618 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00006619 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006620 intel_crtc->cursor_width = width;
6621 intel_crtc->cursor_height = height;
6622
Mika Kuoppala40ccc722013-04-23 17:27:08 +03006623 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006624
Jesse Barnes79e53942008-11-07 14:24:08 -08006625 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006626fail_unpin:
Chris Wilson05394f32010-11-08 19:18:58 +00006627 i915_gem_object_unpin(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006628fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10006629 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00006630fail:
Chris Wilson05394f32010-11-08 19:18:58 +00006631 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10006632 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006633}
6634
6635static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
6636{
Jesse Barnes79e53942008-11-07 14:24:08 -08006637 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006638
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006639 intel_crtc->cursor_x = x;
6640 intel_crtc->cursor_y = y;
Jesse Barnes652c3932009-08-17 13:31:43 -07006641
Mika Kuoppala40ccc722013-04-23 17:27:08 +03006642 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08006643
6644 return 0;
6645}
6646
6647/** Sets the color ramps on behalf of RandR */
6648void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
6649 u16 blue, int regno)
6650{
6651 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6652
6653 intel_crtc->lut_r[regno] = red >> 8;
6654 intel_crtc->lut_g[regno] = green >> 8;
6655 intel_crtc->lut_b[regno] = blue >> 8;
6656}
6657
Dave Airlieb8c00ac2009-10-06 13:54:01 +10006658void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
6659 u16 *blue, int regno)
6660{
6661 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6662
6663 *red = intel_crtc->lut_r[regno] << 8;
6664 *green = intel_crtc->lut_g[regno] << 8;
6665 *blue = intel_crtc->lut_b[regno] << 8;
6666}
6667
Jesse Barnes79e53942008-11-07 14:24:08 -08006668static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01006669 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08006670{
James Simmons72034252010-08-03 01:33:19 +01006671 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08006672 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006673
James Simmons72034252010-08-03 01:33:19 +01006674 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006675 intel_crtc->lut_r[i] = red[i] >> 8;
6676 intel_crtc->lut_g[i] = green[i] >> 8;
6677 intel_crtc->lut_b[i] = blue[i] >> 8;
6678 }
6679
6680 intel_crtc_load_lut(crtc);
6681}
6682
Jesse Barnes79e53942008-11-07 14:24:08 -08006683/* VESA 640x480x72Hz mode to set on the pipe */
6684static struct drm_display_mode load_detect_mode = {
6685 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
6686 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
6687};
6688
Chris Wilsond2dff872011-04-19 08:36:26 +01006689static struct drm_framebuffer *
6690intel_framebuffer_create(struct drm_device *dev,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006691 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilsond2dff872011-04-19 08:36:26 +01006692 struct drm_i915_gem_object *obj)
6693{
6694 struct intel_framebuffer *intel_fb;
6695 int ret;
6696
6697 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
6698 if (!intel_fb) {
6699 drm_gem_object_unreference_unlocked(&obj->base);
6700 return ERR_PTR(-ENOMEM);
6701 }
6702
6703 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
6704 if (ret) {
6705 drm_gem_object_unreference_unlocked(&obj->base);
6706 kfree(intel_fb);
6707 return ERR_PTR(ret);
6708 }
6709
6710 return &intel_fb->base;
6711}
6712
6713static u32
6714intel_framebuffer_pitch_for_width(int width, int bpp)
6715{
6716 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
6717 return ALIGN(pitch, 64);
6718}
6719
6720static u32
6721intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
6722{
6723 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
6724 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
6725}
6726
6727static struct drm_framebuffer *
6728intel_framebuffer_create_for_mode(struct drm_device *dev,
6729 struct drm_display_mode *mode,
6730 int depth, int bpp)
6731{
6732 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00006733 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01006734
6735 obj = i915_gem_alloc_object(dev,
6736 intel_framebuffer_size_for_mode(mode, bpp));
6737 if (obj == NULL)
6738 return ERR_PTR(-ENOMEM);
6739
6740 mode_cmd.width = mode->hdisplay;
6741 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006742 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
6743 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00006744 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01006745
6746 return intel_framebuffer_create(dev, &mode_cmd, obj);
6747}
6748
6749static struct drm_framebuffer *
6750mode_fits_in_fbdev(struct drm_device *dev,
6751 struct drm_display_mode *mode)
6752{
6753 struct drm_i915_private *dev_priv = dev->dev_private;
6754 struct drm_i915_gem_object *obj;
6755 struct drm_framebuffer *fb;
6756
6757 if (dev_priv->fbdev == NULL)
6758 return NULL;
6759
6760 obj = dev_priv->fbdev->ifb.obj;
6761 if (obj == NULL)
6762 return NULL;
6763
6764 fb = &dev_priv->fbdev->ifb.base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006765 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
6766 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01006767 return NULL;
6768
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006769 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01006770 return NULL;
6771
6772 return fb;
6773}
6774
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006775bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01006776 struct drm_display_mode *mode,
Chris Wilson8261b192011-04-19 23:18:09 +01006777 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08006778{
6779 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006780 struct intel_encoder *intel_encoder =
6781 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08006782 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01006783 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08006784 struct drm_crtc *crtc = NULL;
6785 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02006786 struct drm_framebuffer *fb;
Jesse Barnes79e53942008-11-07 14:24:08 -08006787 int i = -1;
6788
Chris Wilsond2dff872011-04-19 08:36:26 +01006789 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6790 connector->base.id, drm_get_connector_name(connector),
6791 encoder->base.id, drm_get_encoder_name(encoder));
6792
Jesse Barnes79e53942008-11-07 14:24:08 -08006793 /*
6794 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01006795 *
Jesse Barnes79e53942008-11-07 14:24:08 -08006796 * - if the connector already has an assigned crtc, use it (but make
6797 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01006798 *
Jesse Barnes79e53942008-11-07 14:24:08 -08006799 * - try to find the first unused crtc that can drive this connector,
6800 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08006801 */
6802
6803 /* See if we already have a CRTC for this connector */
6804 if (encoder->crtc) {
6805 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01006806
Daniel Vetter7b240562012-12-12 00:35:33 +01006807 mutex_lock(&crtc->mutex);
6808
Daniel Vetter24218aa2012-08-12 19:27:11 +02006809 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01006810 old->load_detect_temp = false;
6811
6812 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02006813 if (connector->dpms != DRM_MODE_DPMS_ON)
6814 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01006815
Chris Wilson71731882011-04-19 23:10:58 +01006816 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08006817 }
6818
6819 /* Find an unused one (if possible) */
6820 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
6821 i++;
6822 if (!(encoder->possible_crtcs & (1 << i)))
6823 continue;
6824 if (!possible_crtc->enabled) {
6825 crtc = possible_crtc;
6826 break;
6827 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006828 }
6829
6830 /*
6831 * If we didn't find an unused CRTC, don't use any.
6832 */
6833 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01006834 DRM_DEBUG_KMS("no pipe available for load-detect\n");
6835 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006836 }
6837
Daniel Vetter7b240562012-12-12 00:35:33 +01006838 mutex_lock(&crtc->mutex);
Daniel Vetterfc303102012-07-09 10:40:58 +02006839 intel_encoder->new_crtc = to_intel_crtc(crtc);
6840 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006841
6842 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter24218aa2012-08-12 19:27:11 +02006843 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01006844 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01006845 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08006846
Chris Wilson64927112011-04-20 07:25:26 +01006847 if (!mode)
6848 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08006849
Chris Wilsond2dff872011-04-19 08:36:26 +01006850 /* We need a framebuffer large enough to accommodate all accesses
6851 * that the plane may generate whilst we perform load detection.
6852 * We can not rely on the fbcon either being present (we get called
6853 * during its initialisation to detect all boot displays, or it may
6854 * not even exist) or that it is large enough to satisfy the
6855 * requested mode.
6856 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02006857 fb = mode_fits_in_fbdev(dev, mode);
6858 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01006859 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02006860 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
6861 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01006862 } else
6863 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02006864 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01006865 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Daniel Vetter7b240562012-12-12 00:35:33 +01006866 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00006867 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006868 }
Chris Wilsond2dff872011-04-19 08:36:26 +01006869
Chris Wilsonc0c36b942012-12-19 16:08:43 +00006870 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01006871 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01006872 if (old->release_fb)
6873 old->release_fb->funcs->destroy(old->release_fb);
Daniel Vetter7b240562012-12-12 00:35:33 +01006874 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00006875 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006876 }
Chris Wilson71731882011-04-19 23:10:58 +01006877
Jesse Barnes79e53942008-11-07 14:24:08 -08006878 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006879 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01006880 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08006881}
6882
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006883void intel_release_load_detect_pipe(struct drm_connector *connector,
Chris Wilson8261b192011-04-19 23:18:09 +01006884 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08006885{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006886 struct intel_encoder *intel_encoder =
6887 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01006888 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01006889 struct drm_crtc *crtc = encoder->crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -08006890
Chris Wilsond2dff872011-04-19 08:36:26 +01006891 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6892 connector->base.id, drm_get_connector_name(connector),
6893 encoder->base.id, drm_get_encoder_name(encoder));
6894
Chris Wilson8261b192011-04-19 23:18:09 +01006895 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02006896 to_intel_connector(connector)->new_encoder = NULL;
6897 intel_encoder->new_crtc = NULL;
6898 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01006899
Daniel Vetter36206362012-12-10 20:42:17 +01006900 if (old->release_fb) {
6901 drm_framebuffer_unregister_private(old->release_fb);
6902 drm_framebuffer_unreference(old->release_fb);
6903 }
Chris Wilsond2dff872011-04-19 08:36:26 +01006904
Daniel Vetter67c96402013-01-23 16:25:09 +00006905 mutex_unlock(&crtc->mutex);
Chris Wilson0622a532011-04-21 09:32:11 +01006906 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08006907 }
6908
Eric Anholtc751ce42010-03-25 11:48:48 -07006909 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02006910 if (old->dpms_mode != DRM_MODE_DPMS_ON)
6911 connector->funcs->dpms(connector, old->dpms_mode);
Daniel Vetter7b240562012-12-12 00:35:33 +01006912
6913 mutex_unlock(&crtc->mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08006914}
6915
6916/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03006917static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
6918 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006919{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03006920 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08006921 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03006922 int pipe = pipe_config->cpu_transcoder;
Jesse Barnes548f2452011-02-17 10:40:53 -08006923 u32 dpll = I915_READ(DPLL(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006924 u32 fp;
6925 intel_clock_t clock;
6926
6927 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Chris Wilson39adb7a2011-04-22 22:17:21 +01006928 fp = I915_READ(FP0(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006929 else
Chris Wilson39adb7a2011-04-22 22:17:21 +01006930 fp = I915_READ(FP1(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006931
6932 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006933 if (IS_PINEVIEW(dev)) {
6934 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
6935 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08006936 } else {
6937 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
6938 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
6939 }
6940
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006941 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006942 if (IS_PINEVIEW(dev))
6943 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
6944 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08006945 else
6946 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08006947 DPLL_FPA01_P1_POST_DIV_SHIFT);
6948
6949 switch (dpll & DPLL_MODE_MASK) {
6950 case DPLLB_MODE_DAC_SERIAL:
6951 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
6952 5 : 10;
6953 break;
6954 case DPLLB_MODE_LVDS:
6955 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
6956 7 : 14;
6957 break;
6958 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08006959 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08006960 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03006961 pipe_config->adjusted_mode.clock = 0;
6962 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08006963 }
6964
Daniel Vetterac58c3f2013-06-01 17:16:17 +02006965 if (IS_PINEVIEW(dev))
6966 pineview_clock(96000, &clock);
6967 else
6968 i9xx_clock(96000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006969 } else {
6970 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
6971
6972 if (is_lvds) {
6973 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
6974 DPLL_FPA01_P1_POST_DIV_SHIFT);
6975 clock.p2 = 14;
6976
6977 if ((dpll & PLL_REF_INPUT_MASK) ==
6978 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
6979 /* XXX: might not be 66MHz */
Daniel Vetterac58c3f2013-06-01 17:16:17 +02006980 i9xx_clock(66000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006981 } else
Daniel Vetterac58c3f2013-06-01 17:16:17 +02006982 i9xx_clock(48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006983 } else {
6984 if (dpll & PLL_P1_DIVIDE_BY_TWO)
6985 clock.p1 = 2;
6986 else {
6987 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
6988 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
6989 }
6990 if (dpll & PLL_P2_DIVIDE_BY_4)
6991 clock.p2 = 4;
6992 else
6993 clock.p2 = 2;
6994
Daniel Vetterac58c3f2013-06-01 17:16:17 +02006995 i9xx_clock(48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006996 }
6997 }
6998
Jesse Barnesf1f644d2013-06-27 00:39:25 +03006999 pipe_config->adjusted_mode.clock = clock.dot *
7000 pipe_config->pixel_multiplier;
7001}
7002
7003static void ironlake_crtc_clock_get(struct intel_crtc *crtc,
7004 struct intel_crtc_config *pipe_config)
7005{
7006 struct drm_device *dev = crtc->base.dev;
7007 struct drm_i915_private *dev_priv = dev->dev_private;
7008 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7009 int link_freq, repeat;
7010 u64 clock;
7011 u32 link_m, link_n;
7012
7013 repeat = pipe_config->pixel_multiplier;
7014
7015 /*
7016 * The calculation for the data clock is:
7017 * pixel_clock = ((m/n)*(link_clock * nr_lanes * repeat))/bpp
7018 * But we want to avoid losing precison if possible, so:
7019 * pixel_clock = ((m * link_clock * nr_lanes * repeat)/(n*bpp))
7020 *
7021 * and the link clock is simpler:
7022 * link_clock = (m * link_clock * repeat) / n
Jesse Barnes79e53942008-11-07 14:24:08 -08007023 */
7024
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007025 /*
7026 * We need to get the FDI or DP link clock here to derive
7027 * the M/N dividers.
7028 *
7029 * For FDI, we read it from the BIOS or use a fixed 2.7GHz.
7030 * For DP, it's either 1.62GHz or 2.7GHz.
7031 * We do our calculations in 10*MHz since we don't need much precison.
7032 */
7033 if (pipe_config->has_pch_encoder)
7034 link_freq = intel_fdi_link_freq(dev) * 10000;
7035 else
7036 link_freq = pipe_config->port_clock;
7037
7038 link_m = I915_READ(PIPE_LINK_M1(cpu_transcoder));
7039 link_n = I915_READ(PIPE_LINK_N1(cpu_transcoder));
7040
7041 if (!link_m || !link_n)
7042 return;
7043
7044 clock = ((u64)link_m * (u64)link_freq * (u64)repeat);
7045 do_div(clock, link_n);
7046
7047 pipe_config->adjusted_mode.clock = clock;
Jesse Barnes79e53942008-11-07 14:24:08 -08007048}
7049
7050/** Returns the currently programmed mode of the given pipe. */
7051struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
7052 struct drm_crtc *crtc)
7053{
Jesse Barnes548f2452011-02-17 10:40:53 -08007054 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08007055 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02007056 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08007057 struct drm_display_mode *mode;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007058 struct intel_crtc_config pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007059 int htot = I915_READ(HTOTAL(cpu_transcoder));
7060 int hsync = I915_READ(HSYNC(cpu_transcoder));
7061 int vtot = I915_READ(VTOTAL(cpu_transcoder));
7062 int vsync = I915_READ(VSYNC(cpu_transcoder));
Jesse Barnes79e53942008-11-07 14:24:08 -08007063
7064 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
7065 if (!mode)
7066 return NULL;
7067
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007068 /*
7069 * Construct a pipe_config sufficient for getting the clock info
7070 * back out of crtc_clock_get.
7071 *
7072 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
7073 * to use a real value here instead.
7074 */
Daniel Vettere143a212013-07-04 12:01:15 +02007075 pipe_config.cpu_transcoder = (enum transcoder) intel_crtc->pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007076 pipe_config.pixel_multiplier = 1;
7077 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
7078
7079 mode->clock = pipe_config.adjusted_mode.clock;
Jesse Barnes79e53942008-11-07 14:24:08 -08007080 mode->hdisplay = (htot & 0xffff) + 1;
7081 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
7082 mode->hsync_start = (hsync & 0xffff) + 1;
7083 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
7084 mode->vdisplay = (vtot & 0xffff) + 1;
7085 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
7086 mode->vsync_start = (vsync & 0xffff) + 1;
7087 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
7088
7089 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08007090
7091 return mode;
7092}
7093
Daniel Vetter3dec0092010-08-20 21:40:52 +02007094static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07007095{
7096 struct drm_device *dev = crtc->dev;
7097 drm_i915_private_t *dev_priv = dev->dev_private;
7098 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7099 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007100 int dpll_reg = DPLL(pipe);
7101 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07007102
Eric Anholtbad720f2009-10-22 16:11:14 -07007103 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07007104 return;
7105
7106 if (!dev_priv->lvds_downclock_avail)
7107 return;
7108
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007109 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07007110 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08007111 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007112
Sean Paul8ac5a6d2012-02-13 13:14:51 -05007113 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007114
7115 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
7116 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007117 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007118
Jesse Barnes652c3932009-08-17 13:31:43 -07007119 dpll = I915_READ(dpll_reg);
7120 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08007121 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007122 }
Jesse Barnes652c3932009-08-17 13:31:43 -07007123}
7124
7125static void intel_decrease_pllclock(struct drm_crtc *crtc)
7126{
7127 struct drm_device *dev = crtc->dev;
7128 drm_i915_private_t *dev_priv = dev->dev_private;
7129 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07007130
Eric Anholtbad720f2009-10-22 16:11:14 -07007131 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07007132 return;
7133
7134 if (!dev_priv->lvds_downclock_avail)
7135 return;
7136
7137 /*
7138 * Since this is called by a timer, we should never get here in
7139 * the manual case.
7140 */
7141 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01007142 int pipe = intel_crtc->pipe;
7143 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02007144 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01007145
Zhao Yakui44d98a62009-10-09 11:39:40 +08007146 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007147
Sean Paul8ac5a6d2012-02-13 13:14:51 -05007148 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007149
Chris Wilson074b5e12012-05-02 12:07:06 +01007150 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07007151 dpll |= DISPLAY_RATE_SELECT_FPA1;
7152 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007153 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007154 dpll = I915_READ(dpll_reg);
7155 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08007156 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007157 }
7158
7159}
7160
Chris Wilsonf047e392012-07-21 12:31:41 +01007161void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07007162{
Chris Wilsonf047e392012-07-21 12:31:41 +01007163 i915_update_gfx_val(dev->dev_private);
7164}
7165
7166void intel_mark_idle(struct drm_device *dev)
7167{
Chris Wilson725a5b52013-01-08 11:02:57 +00007168 struct drm_crtc *crtc;
7169
7170 if (!i915_powersave)
7171 return;
7172
7173 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
7174 if (!crtc->fb)
7175 continue;
7176
7177 intel_decrease_pllclock(crtc);
7178 }
Chris Wilsonf047e392012-07-21 12:31:41 +01007179}
7180
Chris Wilsonc65355b2013-06-06 16:53:41 -03007181void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
7182 struct intel_ring_buffer *ring)
Chris Wilsonf047e392012-07-21 12:31:41 +01007183{
7184 struct drm_device *dev = obj->base.dev;
Jesse Barnes652c3932009-08-17 13:31:43 -07007185 struct drm_crtc *crtc;
Jesse Barnes652c3932009-08-17 13:31:43 -07007186
7187 if (!i915_powersave)
7188 return;
7189
Jesse Barnes652c3932009-08-17 13:31:43 -07007190 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Jesse Barnes652c3932009-08-17 13:31:43 -07007191 if (!crtc->fb)
7192 continue;
7193
Chris Wilsonc65355b2013-06-06 16:53:41 -03007194 if (to_intel_framebuffer(crtc->fb)->obj != obj)
7195 continue;
7196
7197 intel_increase_pllclock(crtc);
7198 if (ring && intel_fbc_enabled(dev))
7199 ring->fbc_dirty = true;
Jesse Barnes652c3932009-08-17 13:31:43 -07007200 }
Jesse Barnes652c3932009-08-17 13:31:43 -07007201}
7202
Jesse Barnes79e53942008-11-07 14:24:08 -08007203static void intel_crtc_destroy(struct drm_crtc *crtc)
7204{
7205 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02007206 struct drm_device *dev = crtc->dev;
7207 struct intel_unpin_work *work;
7208 unsigned long flags;
7209
7210 spin_lock_irqsave(&dev->event_lock, flags);
7211 work = intel_crtc->unpin_work;
7212 intel_crtc->unpin_work = NULL;
7213 spin_unlock_irqrestore(&dev->event_lock, flags);
7214
7215 if (work) {
7216 cancel_work_sync(&work->work);
7217 kfree(work);
7218 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007219
Mika Kuoppala40ccc722013-04-23 17:27:08 +03007220 intel_crtc_cursor_set(crtc, NULL, 0, 0, 0);
7221
Jesse Barnes79e53942008-11-07 14:24:08 -08007222 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02007223
Jesse Barnes79e53942008-11-07 14:24:08 -08007224 kfree(intel_crtc);
7225}
7226
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007227static void intel_unpin_work_fn(struct work_struct *__work)
7228{
7229 struct intel_unpin_work *work =
7230 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007231 struct drm_device *dev = work->crtc->dev;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007232
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007233 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01007234 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00007235 drm_gem_object_unreference(&work->pending_flip_obj->base);
7236 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00007237
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007238 intel_update_fbc(dev);
7239 mutex_unlock(&dev->struct_mutex);
7240
7241 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
7242 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
7243
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007244 kfree(work);
7245}
7246
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007247static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01007248 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007249{
7250 drm_i915_private_t *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007251 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7252 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007253 unsigned long flags;
7254
7255 /* Ignore early vblank irqs */
7256 if (intel_crtc == NULL)
7257 return;
7258
7259 spin_lock_irqsave(&dev->event_lock, flags);
7260 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00007261
7262 /* Ensure we don't miss a work->pending update ... */
7263 smp_rmb();
7264
7265 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007266 spin_unlock_irqrestore(&dev->event_lock, flags);
7267 return;
7268 }
7269
Chris Wilsone7d841c2012-12-03 11:36:30 +00007270 /* and that the unpin work is consistent wrt ->pending. */
7271 smp_rmb();
7272
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007273 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007274
Rob Clark45a066e2012-10-08 14:50:40 -05007275 if (work->event)
7276 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007277
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01007278 drm_vblank_put(dev, intel_crtc->pipe);
7279
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007280 spin_unlock_irqrestore(&dev->event_lock, flags);
7281
Daniel Vetter2c10d572012-12-20 21:24:07 +01007282 wake_up_all(&dev_priv->pending_flip_queue);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007283
7284 queue_work(dev_priv->wq, &work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07007285
7286 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007287}
7288
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007289void intel_finish_page_flip(struct drm_device *dev, int pipe)
7290{
7291 drm_i915_private_t *dev_priv = dev->dev_private;
7292 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
7293
Mario Kleiner49b14a52010-12-09 07:00:07 +01007294 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007295}
7296
7297void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
7298{
7299 drm_i915_private_t *dev_priv = dev->dev_private;
7300 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
7301
Mario Kleiner49b14a52010-12-09 07:00:07 +01007302 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007303}
7304
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007305void intel_prepare_page_flip(struct drm_device *dev, int plane)
7306{
7307 drm_i915_private_t *dev_priv = dev->dev_private;
7308 struct intel_crtc *intel_crtc =
7309 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
7310 unsigned long flags;
7311
Chris Wilsone7d841c2012-12-03 11:36:30 +00007312 /* NB: An MMIO update of the plane base pointer will also
7313 * generate a page-flip completion irq, i.e. every modeset
7314 * is also accompanied by a spurious intel_prepare_page_flip().
7315 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007316 spin_lock_irqsave(&dev->event_lock, flags);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007317 if (intel_crtc->unpin_work)
7318 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007319 spin_unlock_irqrestore(&dev->event_lock, flags);
7320}
7321
Chris Wilsone7d841c2012-12-03 11:36:30 +00007322inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
7323{
7324 /* Ensure that the work item is consistent when activating it ... */
7325 smp_wmb();
7326 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
7327 /* and that it is marked active as soon as the irq could fire. */
7328 smp_wmb();
7329}
7330
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007331static int intel_gen2_queue_flip(struct drm_device *dev,
7332 struct drm_crtc *crtc,
7333 struct drm_framebuffer *fb,
7334 struct drm_i915_gem_object *obj)
7335{
7336 struct drm_i915_private *dev_priv = dev->dev_private;
7337 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007338 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007339 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007340 int ret;
7341
Daniel Vetter6d90c952012-04-26 23:28:05 +02007342 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007343 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007344 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007345
Daniel Vetter6d90c952012-04-26 23:28:05 +02007346 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007347 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007348 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007349
7350 /* Can't queue multiple flips, so wait for the previous
7351 * one to finish before executing the next.
7352 */
7353 if (intel_crtc->plane)
7354 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7355 else
7356 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007357 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7358 intel_ring_emit(ring, MI_NOOP);
7359 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7360 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7361 intel_ring_emit(ring, fb->pitches[0]);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007362 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007363 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00007364
7365 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007366 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007367 return 0;
7368
7369err_unpin:
7370 intel_unpin_fb_obj(obj);
7371err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007372 return ret;
7373}
7374
7375static int intel_gen3_queue_flip(struct drm_device *dev,
7376 struct drm_crtc *crtc,
7377 struct drm_framebuffer *fb,
7378 struct drm_i915_gem_object *obj)
7379{
7380 struct drm_i915_private *dev_priv = dev->dev_private;
7381 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007382 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007383 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007384 int ret;
7385
Daniel Vetter6d90c952012-04-26 23:28:05 +02007386 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007387 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007388 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007389
Daniel Vetter6d90c952012-04-26 23:28:05 +02007390 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007391 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007392 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007393
7394 if (intel_crtc->plane)
7395 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7396 else
7397 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007398 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7399 intel_ring_emit(ring, MI_NOOP);
7400 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
7401 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7402 intel_ring_emit(ring, fb->pitches[0]);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007403 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007404 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007405
Chris Wilsone7d841c2012-12-03 11:36:30 +00007406 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007407 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007408 return 0;
7409
7410err_unpin:
7411 intel_unpin_fb_obj(obj);
7412err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007413 return ret;
7414}
7415
7416static int intel_gen4_queue_flip(struct drm_device *dev,
7417 struct drm_crtc *crtc,
7418 struct drm_framebuffer *fb,
7419 struct drm_i915_gem_object *obj)
7420{
7421 struct drm_i915_private *dev_priv = dev->dev_private;
7422 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7423 uint32_t pf, pipesrc;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007424 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007425 int ret;
7426
Daniel Vetter6d90c952012-04-26 23:28:05 +02007427 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007428 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007429 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007430
Daniel Vetter6d90c952012-04-26 23:28:05 +02007431 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007432 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007433 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007434
7435 /* i965+ uses the linear or tiled offsets from the
7436 * Display Registers (which do not change across a page-flip)
7437 * so we need only reprogram the base address.
7438 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02007439 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7440 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7441 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02007442 intel_ring_emit(ring,
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007443 (i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset) |
Daniel Vetterc2c75132012-07-05 12:17:30 +02007444 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007445
7446 /* XXX Enabling the panel-fitter across page-flip is so far
7447 * untested on non-native modes, so ignore it for now.
7448 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
7449 */
7450 pf = 0;
7451 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007452 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007453
7454 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007455 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007456 return 0;
7457
7458err_unpin:
7459 intel_unpin_fb_obj(obj);
7460err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007461 return ret;
7462}
7463
7464static int intel_gen6_queue_flip(struct drm_device *dev,
7465 struct drm_crtc *crtc,
7466 struct drm_framebuffer *fb,
7467 struct drm_i915_gem_object *obj)
7468{
7469 struct drm_i915_private *dev_priv = dev->dev_private;
7470 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007471 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007472 uint32_t pf, pipesrc;
7473 int ret;
7474
Daniel Vetter6d90c952012-04-26 23:28:05 +02007475 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007476 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007477 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007478
Daniel Vetter6d90c952012-04-26 23:28:05 +02007479 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007480 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007481 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007482
Daniel Vetter6d90c952012-04-26 23:28:05 +02007483 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7484 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7485 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007486 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007487
Chris Wilson99d9acd2012-04-17 20:37:00 +01007488 /* Contrary to the suggestions in the documentation,
7489 * "Enable Panel Fitter" does not seem to be required when page
7490 * flipping with a non-native mode, and worse causes a normal
7491 * modeset to fail.
7492 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
7493 */
7494 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007495 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007496 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007497
7498 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007499 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007500 return 0;
7501
7502err_unpin:
7503 intel_unpin_fb_obj(obj);
7504err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007505 return ret;
7506}
7507
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007508/*
7509 * On gen7 we currently use the blit ring because (in early silicon at least)
7510 * the render ring doesn't give us interrpts for page flip completion, which
7511 * means clients will hang after the first flip is queued. Fortunately the
7512 * blit ring generates interrupts properly, so use it instead.
7513 */
7514static int intel_gen7_queue_flip(struct drm_device *dev,
7515 struct drm_crtc *crtc,
7516 struct drm_framebuffer *fb,
7517 struct drm_i915_gem_object *obj)
7518{
7519 struct drm_i915_private *dev_priv = dev->dev_private;
7520 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7521 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007522 uint32_t plane_bit = 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007523 int ret;
7524
7525 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7526 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007527 goto err;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007528
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007529 switch(intel_crtc->plane) {
7530 case PLANE_A:
7531 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
7532 break;
7533 case PLANE_B:
7534 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
7535 break;
7536 case PLANE_C:
7537 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
7538 break;
7539 default:
7540 WARN_ONCE(1, "unknown plane in flip command\n");
7541 ret = -ENODEV;
Eugeni Dodonovab3951e2012-06-18 19:03:38 -03007542 goto err_unpin;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007543 }
7544
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007545 ret = intel_ring_begin(ring, 4);
7546 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007547 goto err_unpin;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007548
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007549 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007550 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007551 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007552 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +00007553
7554 intel_mark_page_flip_active(intel_crtc);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007555 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007556 return 0;
7557
7558err_unpin:
7559 intel_unpin_fb_obj(obj);
7560err:
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007561 return ret;
7562}
7563
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007564static int intel_default_queue_flip(struct drm_device *dev,
7565 struct drm_crtc *crtc,
7566 struct drm_framebuffer *fb,
7567 struct drm_i915_gem_object *obj)
7568{
7569 return -ENODEV;
7570}
7571
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007572static int intel_crtc_page_flip(struct drm_crtc *crtc,
7573 struct drm_framebuffer *fb,
7574 struct drm_pending_vblank_event *event)
7575{
7576 struct drm_device *dev = crtc->dev;
7577 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02007578 struct drm_framebuffer *old_fb = crtc->fb;
7579 struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007580 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7581 struct intel_unpin_work *work;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007582 unsigned long flags;
Chris Wilson52e68632010-08-08 10:15:59 +01007583 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007584
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03007585 /* Can't change pixel format via MI display flips. */
7586 if (fb->pixel_format != crtc->fb->pixel_format)
7587 return -EINVAL;
7588
7589 /*
7590 * TILEOFF/LINOFF registers can't be changed via MI display flips.
7591 * Note that pitch changes could also affect these register.
7592 */
7593 if (INTEL_INFO(dev)->gen > 3 &&
7594 (fb->offsets[0] != crtc->fb->offsets[0] ||
7595 fb->pitches[0] != crtc->fb->pitches[0]))
7596 return -EINVAL;
7597
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007598 work = kzalloc(sizeof *work, GFP_KERNEL);
7599 if (work == NULL)
7600 return -ENOMEM;
7601
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007602 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007603 work->crtc = crtc;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02007604 work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007605 INIT_WORK(&work->work, intel_unpin_work_fn);
7606
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007607 ret = drm_vblank_get(dev, intel_crtc->pipe);
7608 if (ret)
7609 goto free_work;
7610
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007611 /* We borrow the event spin lock for protecting unpin_work */
7612 spin_lock_irqsave(&dev->event_lock, flags);
7613 if (intel_crtc->unpin_work) {
7614 spin_unlock_irqrestore(&dev->event_lock, flags);
7615 kfree(work);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007616 drm_vblank_put(dev, intel_crtc->pipe);
Chris Wilson468f0b42010-05-27 13:18:13 +01007617
7618 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007619 return -EBUSY;
7620 }
7621 intel_crtc->unpin_work = work;
7622 spin_unlock_irqrestore(&dev->event_lock, flags);
7623
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007624 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
7625 flush_workqueue(dev_priv->wq);
7626
Chris Wilson79158102012-05-23 11:13:58 +01007627 ret = i915_mutex_lock_interruptible(dev);
7628 if (ret)
7629 goto cleanup;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007630
Jesse Barnes75dfca82010-02-10 15:09:44 -08007631 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00007632 drm_gem_object_reference(&work->old_fb_obj->base);
7633 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007634
7635 crtc->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01007636
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007637 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007638
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01007639 work->enable_stall_check = true;
7640
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007641 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +02007642 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007643
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007644 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
7645 if (ret)
7646 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007647
Chris Wilson7782de32011-07-08 12:22:41 +01007648 intel_disable_fbc(dev);
Chris Wilsonc65355b2013-06-06 16:53:41 -03007649 intel_mark_fb_busy(obj, NULL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007650 mutex_unlock(&dev->struct_mutex);
7651
Jesse Barnese5510fa2010-07-01 16:48:37 -07007652 trace_i915_flip_request(intel_crtc->plane, obj);
7653
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007654 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01007655
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007656cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007657 atomic_dec(&intel_crtc->unpin_work_count);
Ville Syrjälä4a35f832013-02-22 16:53:38 +02007658 crtc->fb = old_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00007659 drm_gem_object_unreference(&work->old_fb_obj->base);
7660 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01007661 mutex_unlock(&dev->struct_mutex);
7662
Chris Wilson79158102012-05-23 11:13:58 +01007663cleanup:
Chris Wilson96b099f2010-06-07 14:03:04 +01007664 spin_lock_irqsave(&dev->event_lock, flags);
7665 intel_crtc->unpin_work = NULL;
7666 spin_unlock_irqrestore(&dev->event_lock, flags);
7667
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007668 drm_vblank_put(dev, intel_crtc->pipe);
7669free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01007670 kfree(work);
7671
7672 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007673}
7674
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007675static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007676 .mode_set_base_atomic = intel_pipe_set_base_atomic,
7677 .load_lut = intel_crtc_load_lut,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007678};
7679
Daniel Vetter50f56112012-07-02 09:35:43 +02007680static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
7681 struct drm_crtc *crtc)
7682{
7683 struct drm_device *dev;
7684 struct drm_crtc *tmp;
7685 int crtc_mask = 1;
7686
7687 WARN(!crtc, "checking null crtc?\n");
7688
7689 dev = crtc->dev;
7690
7691 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
7692 if (tmp == crtc)
7693 break;
7694 crtc_mask <<= 1;
7695 }
7696
7697 if (encoder->possible_crtcs & crtc_mask)
7698 return true;
7699 return false;
7700}
7701
Daniel Vetter9a935852012-07-05 22:34:27 +02007702/**
7703 * intel_modeset_update_staged_output_state
7704 *
7705 * Updates the staged output configuration state, e.g. after we've read out the
7706 * current hw state.
7707 */
7708static void intel_modeset_update_staged_output_state(struct drm_device *dev)
7709{
7710 struct intel_encoder *encoder;
7711 struct intel_connector *connector;
7712
7713 list_for_each_entry(connector, &dev->mode_config.connector_list,
7714 base.head) {
7715 connector->new_encoder =
7716 to_intel_encoder(connector->base.encoder);
7717 }
7718
7719 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7720 base.head) {
7721 encoder->new_crtc =
7722 to_intel_crtc(encoder->base.crtc);
7723 }
7724}
7725
7726/**
7727 * intel_modeset_commit_output_state
7728 *
7729 * This function copies the stage display pipe configuration to the real one.
7730 */
7731static void intel_modeset_commit_output_state(struct drm_device *dev)
7732{
7733 struct intel_encoder *encoder;
7734 struct intel_connector *connector;
7735
7736 list_for_each_entry(connector, &dev->mode_config.connector_list,
7737 base.head) {
7738 connector->base.encoder = &connector->new_encoder->base;
7739 }
7740
7741 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7742 base.head) {
7743 encoder->base.crtc = &encoder->new_crtc->base;
7744 }
7745}
7746
Daniel Vetter050f7ae2013-06-02 13:26:23 +02007747static void
7748connected_sink_compute_bpp(struct intel_connector * connector,
7749 struct intel_crtc_config *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007750{
Daniel Vetter050f7ae2013-06-02 13:26:23 +02007751 int bpp = pipe_config->pipe_bpp;
7752
7753 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
7754 connector->base.base.id,
7755 drm_get_connector_name(&connector->base));
7756
7757 /* Don't use an invalid EDID bpc value */
7758 if (connector->base.display_info.bpc &&
7759 connector->base.display_info.bpc * 3 < bpp) {
7760 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
7761 bpp, connector->base.display_info.bpc*3);
7762 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
7763 }
7764
7765 /* Clamp bpp to 8 on screens without EDID 1.4 */
7766 if (connector->base.display_info.bpc == 0 && bpp > 24) {
7767 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
7768 bpp);
7769 pipe_config->pipe_bpp = 24;
7770 }
7771}
7772
7773static int
7774compute_baseline_pipe_bpp(struct intel_crtc *crtc,
7775 struct drm_framebuffer *fb,
7776 struct intel_crtc_config *pipe_config)
7777{
7778 struct drm_device *dev = crtc->base.dev;
7779 struct intel_connector *connector;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007780 int bpp;
7781
Daniel Vetterd42264b2013-03-28 16:38:08 +01007782 switch (fb->pixel_format) {
7783 case DRM_FORMAT_C8:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007784 bpp = 8*3; /* since we go through a colormap */
7785 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01007786 case DRM_FORMAT_XRGB1555:
7787 case DRM_FORMAT_ARGB1555:
7788 /* checked in intel_framebuffer_init already */
7789 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
7790 return -EINVAL;
7791 case DRM_FORMAT_RGB565:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007792 bpp = 6*3; /* min is 18bpp */
7793 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01007794 case DRM_FORMAT_XBGR8888:
7795 case DRM_FORMAT_ABGR8888:
7796 /* checked in intel_framebuffer_init already */
7797 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
7798 return -EINVAL;
7799 case DRM_FORMAT_XRGB8888:
7800 case DRM_FORMAT_ARGB8888:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007801 bpp = 8*3;
7802 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01007803 case DRM_FORMAT_XRGB2101010:
7804 case DRM_FORMAT_ARGB2101010:
7805 case DRM_FORMAT_XBGR2101010:
7806 case DRM_FORMAT_ABGR2101010:
7807 /* checked in intel_framebuffer_init already */
7808 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
Daniel Vetterbaba1332013-03-27 00:45:00 +01007809 return -EINVAL;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007810 bpp = 10*3;
7811 break;
Daniel Vetterbaba1332013-03-27 00:45:00 +01007812 /* TODO: gen4+ supports 16 bpc floating point, too. */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007813 default:
7814 DRM_DEBUG_KMS("unsupported depth\n");
7815 return -EINVAL;
7816 }
7817
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007818 pipe_config->pipe_bpp = bpp;
7819
7820 /* Clamp display bpp to EDID value */
7821 list_for_each_entry(connector, &dev->mode_config.connector_list,
Daniel Vetter050f7ae2013-06-02 13:26:23 +02007822 base.head) {
Daniel Vetter1b829e02013-06-02 13:26:24 +02007823 if (!connector->new_encoder ||
7824 connector->new_encoder->new_crtc != crtc)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007825 continue;
7826
Daniel Vetter050f7ae2013-06-02 13:26:23 +02007827 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007828 }
7829
7830 return bpp;
7831}
7832
Daniel Vetterc0b03412013-05-28 12:05:54 +02007833static void intel_dump_pipe_config(struct intel_crtc *crtc,
7834 struct intel_crtc_config *pipe_config,
7835 const char *context)
7836{
7837 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
7838 context, pipe_name(crtc->pipe));
7839
7840 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
7841 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
7842 pipe_config->pipe_bpp, pipe_config->dither);
7843 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
7844 pipe_config->has_pch_encoder,
7845 pipe_config->fdi_lanes,
7846 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
7847 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
7848 pipe_config->fdi_m_n.tu);
7849 DRM_DEBUG_KMS("requested mode:\n");
7850 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
7851 DRM_DEBUG_KMS("adjusted mode:\n");
7852 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
7853 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
7854 pipe_config->gmch_pfit.control,
7855 pipe_config->gmch_pfit.pgm_ratios,
7856 pipe_config->gmch_pfit.lvds_border_bits);
7857 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x\n",
7858 pipe_config->pch_pfit.pos,
7859 pipe_config->pch_pfit.size);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03007860 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Daniel Vetterc0b03412013-05-28 12:05:54 +02007861}
7862
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02007863static bool check_encoder_cloning(struct drm_crtc *crtc)
7864{
7865 int num_encoders = 0;
7866 bool uncloneable_encoders = false;
7867 struct intel_encoder *encoder;
7868
7869 list_for_each_entry(encoder, &crtc->dev->mode_config.encoder_list,
7870 base.head) {
7871 if (&encoder->new_crtc->base != crtc)
7872 continue;
7873
7874 num_encoders++;
7875 if (!encoder->cloneable)
7876 uncloneable_encoders = true;
7877 }
7878
7879 return !(num_encoders > 1 && uncloneable_encoders);
7880}
7881
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007882static struct intel_crtc_config *
7883intel_modeset_pipe_config(struct drm_crtc *crtc,
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007884 struct drm_framebuffer *fb,
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007885 struct drm_display_mode *mode)
Daniel Vetter7758a112012-07-08 19:40:39 +02007886{
7887 struct drm_device *dev = crtc->dev;
Daniel Vetter7758a112012-07-08 19:40:39 +02007888 struct drm_encoder_helper_funcs *encoder_funcs;
7889 struct intel_encoder *encoder;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007890 struct intel_crtc_config *pipe_config;
Daniel Vettere29c22c2013-02-21 00:00:16 +01007891 int plane_bpp, ret = -EINVAL;
7892 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +02007893
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02007894 if (!check_encoder_cloning(crtc)) {
7895 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
7896 return ERR_PTR(-EINVAL);
7897 }
7898
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007899 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7900 if (!pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +02007901 return ERR_PTR(-ENOMEM);
7902
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007903 drm_mode_copy(&pipe_config->adjusted_mode, mode);
7904 drm_mode_copy(&pipe_config->requested_mode, mode);
Daniel Vettere143a212013-07-04 12:01:15 +02007905 pipe_config->cpu_transcoder =
7906 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007907 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007908
Daniel Vetter050f7ae2013-06-02 13:26:23 +02007909 /* Compute a starting value for pipe_config->pipe_bpp taking the source
7910 * plane pixel format and any sink constraints into account. Returns the
7911 * source plane bpp so that dithering can be selected on mismatches
7912 * after encoders and crtc also have had their say. */
7913 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
7914 fb, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007915 if (plane_bpp < 0)
7916 goto fail;
7917
Daniel Vettere29c22c2013-02-21 00:00:16 +01007918encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +02007919 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +02007920 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +02007921 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +02007922
Daniel Vetter7758a112012-07-08 19:40:39 +02007923 /* Pass our mode to the connectors and the CRTC to give them a chance to
7924 * adjust it according to limitations or connector properties, and also
7925 * a chance to reject the mode entirely.
7926 */
7927 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7928 base.head) {
7929
7930 if (&encoder->new_crtc->base != crtc)
7931 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +01007932
7933 if (encoder->compute_config) {
7934 if (!(encoder->compute_config(encoder, pipe_config))) {
7935 DRM_DEBUG_KMS("Encoder config failure\n");
7936 goto fail;
7937 }
7938
7939 continue;
7940 }
7941
Daniel Vetter7758a112012-07-08 19:40:39 +02007942 encoder_funcs = encoder->base.helper_private;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007943 if (!(encoder_funcs->mode_fixup(&encoder->base,
7944 &pipe_config->requested_mode,
7945 &pipe_config->adjusted_mode))) {
Daniel Vetter7758a112012-07-08 19:40:39 +02007946 DRM_DEBUG_KMS("Encoder fixup failed\n");
7947 goto fail;
7948 }
7949 }
7950
Daniel Vetterff9a6752013-06-01 17:16:21 +02007951 /* Set default port clock if not overwritten by the encoder. Needs to be
7952 * done afterwards in case the encoder adjusts the mode. */
7953 if (!pipe_config->port_clock)
7954 pipe_config->port_clock = pipe_config->adjusted_mode.clock;
7955
Daniel Vettera43f6e02013-06-07 23:10:32 +02007956 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01007957 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +02007958 DRM_DEBUG_KMS("CRTC fixup failed\n");
7959 goto fail;
7960 }
Daniel Vettere29c22c2013-02-21 00:00:16 +01007961
7962 if (ret == RETRY) {
7963 if (WARN(!retry, "loop in pipe configuration computation\n")) {
7964 ret = -EINVAL;
7965 goto fail;
7966 }
7967
7968 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
7969 retry = false;
7970 goto encoder_retry;
7971 }
7972
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007973 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
7974 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
7975 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
7976
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007977 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +02007978fail:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007979 kfree(pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01007980 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +02007981}
7982
Daniel Vettere2e1ed42012-07-08 21:14:38 +02007983/* Computes which crtcs are affected and sets the relevant bits in the mask. For
7984 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
7985static void
7986intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
7987 unsigned *prepare_pipes, unsigned *disable_pipes)
7988{
7989 struct intel_crtc *intel_crtc;
7990 struct drm_device *dev = crtc->dev;
7991 struct intel_encoder *encoder;
7992 struct intel_connector *connector;
7993 struct drm_crtc *tmp_crtc;
7994
7995 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
7996
7997 /* Check which crtcs have changed outputs connected to them, these need
7998 * to be part of the prepare_pipes mask. We don't (yet) support global
7999 * modeset across multiple crtcs, so modeset_pipes will only have one
8000 * bit set at most. */
8001 list_for_each_entry(connector, &dev->mode_config.connector_list,
8002 base.head) {
8003 if (connector->base.encoder == &connector->new_encoder->base)
8004 continue;
8005
8006 if (connector->base.encoder) {
8007 tmp_crtc = connector->base.encoder->crtc;
8008
8009 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
8010 }
8011
8012 if (connector->new_encoder)
8013 *prepare_pipes |=
8014 1 << connector->new_encoder->new_crtc->pipe;
8015 }
8016
8017 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8018 base.head) {
8019 if (encoder->base.crtc == &encoder->new_crtc->base)
8020 continue;
8021
8022 if (encoder->base.crtc) {
8023 tmp_crtc = encoder->base.crtc;
8024
8025 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
8026 }
8027
8028 if (encoder->new_crtc)
8029 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
8030 }
8031
8032 /* Check for any pipes that will be fully disabled ... */
8033 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
8034 base.head) {
8035 bool used = false;
8036
8037 /* Don't try to disable disabled crtcs. */
8038 if (!intel_crtc->base.enabled)
8039 continue;
8040
8041 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8042 base.head) {
8043 if (encoder->new_crtc == intel_crtc)
8044 used = true;
8045 }
8046
8047 if (!used)
8048 *disable_pipes |= 1 << intel_crtc->pipe;
8049 }
8050
8051
8052 /* set_mode is also used to update properties on life display pipes. */
8053 intel_crtc = to_intel_crtc(crtc);
8054 if (crtc->enabled)
8055 *prepare_pipes |= 1 << intel_crtc->pipe;
8056
Daniel Vetterb6c51642013-04-12 18:48:43 +02008057 /*
8058 * For simplicity do a full modeset on any pipe where the output routing
8059 * changed. We could be more clever, but that would require us to be
8060 * more careful with calling the relevant encoder->mode_set functions.
8061 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +02008062 if (*prepare_pipes)
8063 *modeset_pipes = *prepare_pipes;
8064
8065 /* ... and mask these out. */
8066 *modeset_pipes &= ~(*disable_pipes);
8067 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +02008068
8069 /*
8070 * HACK: We don't (yet) fully support global modesets. intel_set_config
8071 * obies this rule, but the modeset restore mode of
8072 * intel_modeset_setup_hw_state does not.
8073 */
8074 *modeset_pipes &= 1 << intel_crtc->pipe;
8075 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +02008076
8077 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
8078 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +02008079}
8080
Daniel Vetterea9d7582012-07-10 10:42:52 +02008081static bool intel_crtc_in_use(struct drm_crtc *crtc)
8082{
8083 struct drm_encoder *encoder;
8084 struct drm_device *dev = crtc->dev;
8085
8086 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
8087 if (encoder->crtc == crtc)
8088 return true;
8089
8090 return false;
8091}
8092
8093static void
8094intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
8095{
8096 struct intel_encoder *intel_encoder;
8097 struct intel_crtc *intel_crtc;
8098 struct drm_connector *connector;
8099
8100 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
8101 base.head) {
8102 if (!intel_encoder->base.crtc)
8103 continue;
8104
8105 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
8106
8107 if (prepare_pipes & (1 << intel_crtc->pipe))
8108 intel_encoder->connectors_active = false;
8109 }
8110
8111 intel_modeset_commit_output_state(dev);
8112
8113 /* Update computed state. */
8114 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
8115 base.head) {
8116 intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
8117 }
8118
8119 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
8120 if (!connector->encoder || !connector->encoder->crtc)
8121 continue;
8122
8123 intel_crtc = to_intel_crtc(connector->encoder->crtc);
8124
8125 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +02008126 struct drm_property *dpms_property =
8127 dev->mode_config.dpms_property;
8128
Daniel Vetterea9d7582012-07-10 10:42:52 +02008129 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -05008130 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +02008131 dpms_property,
8132 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +02008133
8134 intel_encoder = to_intel_encoder(connector->encoder);
8135 intel_encoder->connectors_active = true;
8136 }
8137 }
8138
8139}
8140
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008141static bool intel_fuzzy_clock_check(struct intel_crtc_config *cur,
8142 struct intel_crtc_config *new)
8143{
8144 int clock1, clock2, diff;
8145
8146 clock1 = cur->adjusted_mode.clock;
8147 clock2 = new->adjusted_mode.clock;
8148
8149 if (clock1 == clock2)
8150 return true;
8151
8152 if (!clock1 || !clock2)
8153 return false;
8154
8155 diff = abs(clock1 - clock2);
8156
8157 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
8158 return true;
8159
8160 return false;
8161}
8162
Daniel Vetter25c5b262012-07-08 22:08:04 +02008163#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
8164 list_for_each_entry((intel_crtc), \
8165 &(dev)->mode_config.crtc_list, \
8166 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +02008167 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +02008168
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008169static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008170intel_pipe_config_compare(struct drm_device *dev,
8171 struct intel_crtc_config *current_config,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008172 struct intel_crtc_config *pipe_config)
8173{
Daniel Vetter66e985c2013-06-05 13:34:20 +02008174#define PIPE_CONF_CHECK_X(name) \
8175 if (current_config->name != pipe_config->name) { \
8176 DRM_ERROR("mismatch in " #name " " \
8177 "(expected 0x%08x, found 0x%08x)\n", \
8178 current_config->name, \
8179 pipe_config->name); \
8180 return false; \
8181 }
8182
Daniel Vetter08a24032013-04-19 11:25:34 +02008183#define PIPE_CONF_CHECK_I(name) \
8184 if (current_config->name != pipe_config->name) { \
8185 DRM_ERROR("mismatch in " #name " " \
8186 "(expected %i, found %i)\n", \
8187 current_config->name, \
8188 pipe_config->name); \
8189 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +01008190 }
8191
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008192#define PIPE_CONF_CHECK_FLAGS(name, mask) \
8193 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -07008194 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008195 "(expected %i, found %i)\n", \
8196 current_config->name & (mask), \
8197 pipe_config->name & (mask)); \
8198 return false; \
8199 }
8200
Daniel Vetterbb760062013-06-06 14:55:52 +02008201#define PIPE_CONF_QUIRK(quirk) \
8202 ((current_config->quirks | pipe_config->quirks) & (quirk))
8203
Daniel Vettereccb1402013-05-22 00:50:22 +02008204 PIPE_CONF_CHECK_I(cpu_transcoder);
8205
Daniel Vetter08a24032013-04-19 11:25:34 +02008206 PIPE_CONF_CHECK_I(has_pch_encoder);
8207 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +02008208 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
8209 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
8210 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
8211 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
8212 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +02008213
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008214 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
8215 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
8216 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
8217 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
8218 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
8219 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
8220
8221 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
8222 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
8223 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
8224 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
8225 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
8226 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
8227
Daniel Vetterc93f54c2013-06-27 19:47:19 +02008228 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008229
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008230 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8231 DRM_MODE_FLAG_INTERLACE);
8232
Daniel Vetterbb760062013-06-06 14:55:52 +02008233 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
8234 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8235 DRM_MODE_FLAG_PHSYNC);
8236 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8237 DRM_MODE_FLAG_NHSYNC);
8238 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8239 DRM_MODE_FLAG_PVSYNC);
8240 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8241 DRM_MODE_FLAG_NVSYNC);
8242 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -07008243
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008244 PIPE_CONF_CHECK_I(requested_mode.hdisplay);
8245 PIPE_CONF_CHECK_I(requested_mode.vdisplay);
8246
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008247 PIPE_CONF_CHECK_I(gmch_pfit.control);
8248 /* pfit ratios are autocomputed by the hw on gen4+ */
8249 if (INTEL_INFO(dev)->gen < 4)
8250 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
8251 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
8252 PIPE_CONF_CHECK_I(pch_pfit.pos);
8253 PIPE_CONF_CHECK_I(pch_pfit.size);
8254
Paulo Zanoni42db64e2013-05-31 16:33:22 -03008255 PIPE_CONF_CHECK_I(ips_enabled);
8256
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008257 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008258 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008259 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008260 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
8261 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008262
Daniel Vetter66e985c2013-06-05 13:34:20 +02008263#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +02008264#undef PIPE_CONF_CHECK_I
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008265#undef PIPE_CONF_CHECK_FLAGS
Daniel Vetterbb760062013-06-06 14:55:52 +02008266#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008267
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008268 if (!IS_HASWELL(dev)) {
8269 if (!intel_fuzzy_clock_check(current_config, pipe_config)) {
Jesse Barnes6f024882013-07-01 10:19:09 -07008270 DRM_ERROR("mismatch in clock (expected %d, found %d)\n",
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008271 current_config->adjusted_mode.clock,
8272 pipe_config->adjusted_mode.clock);
8273 return false;
8274 }
8275 }
8276
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008277 return true;
8278}
8279
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02008280static void
8281check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008282{
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008283 struct intel_connector *connector;
8284
8285 list_for_each_entry(connector, &dev->mode_config.connector_list,
8286 base.head) {
8287 /* This also checks the encoder/connector hw state with the
8288 * ->get_hw_state callbacks. */
8289 intel_connector_check_state(connector);
8290
8291 WARN(&connector->new_encoder->base != connector->base.encoder,
8292 "connector's staged encoder doesn't match current encoder\n");
8293 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02008294}
8295
8296static void
8297check_encoder_state(struct drm_device *dev)
8298{
8299 struct intel_encoder *encoder;
8300 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008301
8302 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8303 base.head) {
8304 bool enabled = false;
8305 bool active = false;
8306 enum pipe pipe, tracked_pipe;
8307
8308 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
8309 encoder->base.base.id,
8310 drm_get_encoder_name(&encoder->base));
8311
8312 WARN(&encoder->new_crtc->base != encoder->base.crtc,
8313 "encoder's stage crtc doesn't match current crtc\n");
8314 WARN(encoder->connectors_active && !encoder->base.crtc,
8315 "encoder's active_connectors set, but no crtc\n");
8316
8317 list_for_each_entry(connector, &dev->mode_config.connector_list,
8318 base.head) {
8319 if (connector->base.encoder != &encoder->base)
8320 continue;
8321 enabled = true;
8322 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
8323 active = true;
8324 }
8325 WARN(!!encoder->base.crtc != enabled,
8326 "encoder's enabled state mismatch "
8327 "(expected %i, found %i)\n",
8328 !!encoder->base.crtc, enabled);
8329 WARN(active && !encoder->base.crtc,
8330 "active encoder with no crtc\n");
8331
8332 WARN(encoder->connectors_active != active,
8333 "encoder's computed active state doesn't match tracked active state "
8334 "(expected %i, found %i)\n", active, encoder->connectors_active);
8335
8336 active = encoder->get_hw_state(encoder, &pipe);
8337 WARN(active != encoder->connectors_active,
8338 "encoder's hw state doesn't match sw tracking "
8339 "(expected %i, found %i)\n",
8340 encoder->connectors_active, active);
8341
8342 if (!encoder->base.crtc)
8343 continue;
8344
8345 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
8346 WARN(active && pipe != tracked_pipe,
8347 "active encoder's pipe doesn't match"
8348 "(expected %i, found %i)\n",
8349 tracked_pipe, pipe);
8350
8351 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02008352}
8353
8354static void
8355check_crtc_state(struct drm_device *dev)
8356{
8357 drm_i915_private_t *dev_priv = dev->dev_private;
8358 struct intel_crtc *crtc;
8359 struct intel_encoder *encoder;
8360 struct intel_crtc_config pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008361
8362 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
8363 base.head) {
8364 bool enabled = false;
8365 bool active = false;
8366
Jesse Barnes045ac3b2013-05-14 17:08:26 -07008367 memset(&pipe_config, 0, sizeof(pipe_config));
8368
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008369 DRM_DEBUG_KMS("[CRTC:%d]\n",
8370 crtc->base.base.id);
8371
8372 WARN(crtc->active && !crtc->base.enabled,
8373 "active crtc, but not enabled in sw tracking\n");
8374
8375 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8376 base.head) {
8377 if (encoder->base.crtc != &crtc->base)
8378 continue;
8379 enabled = true;
8380 if (encoder->connectors_active)
8381 active = true;
8382 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008383
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008384 WARN(active != crtc->active,
8385 "crtc's computed active state doesn't match tracked active state "
8386 "(expected %i, found %i)\n", active, crtc->active);
8387 WARN(enabled != crtc->base.enabled,
8388 "crtc's computed enabled state doesn't match tracked enabled state "
8389 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
8390
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008391 active = dev_priv->display.get_pipe_config(crtc,
8392 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +02008393
8394 /* hw state is inconsistent with the pipe A quirk */
8395 if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
8396 active = crtc->active;
8397
Daniel Vetter6c49f242013-06-06 12:45:25 +02008398 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8399 base.head) {
8400 if (encoder->base.crtc != &crtc->base)
8401 continue;
Jesse Barnes510d5f22013-07-01 15:50:17 -07008402 if (encoder->get_config)
Daniel Vetter6c49f242013-06-06 12:45:25 +02008403 encoder->get_config(encoder, &pipe_config);
8404 }
8405
Jesse Barnes510d5f22013-07-01 15:50:17 -07008406 if (dev_priv->display.get_clock)
8407 dev_priv->display.get_clock(crtc, &pipe_config);
8408
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008409 WARN(crtc->active != active,
8410 "crtc active state doesn't match with hw state "
8411 "(expected %i, found %i)\n", crtc->active, active);
8412
Daniel Vetterc0b03412013-05-28 12:05:54 +02008413 if (active &&
8414 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
8415 WARN(1, "pipe state doesn't match!\n");
8416 intel_dump_pipe_config(crtc, &pipe_config,
8417 "[hw state]");
8418 intel_dump_pipe_config(crtc, &crtc->config,
8419 "[sw state]");
8420 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008421 }
8422}
8423
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02008424static void
8425check_shared_dpll_state(struct drm_device *dev)
8426{
8427 drm_i915_private_t *dev_priv = dev->dev_private;
8428 struct intel_crtc *crtc;
8429 struct intel_dpll_hw_state dpll_hw_state;
8430 int i;
Daniel Vetter53589012013-06-05 13:34:16 +02008431
8432 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8433 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
8434 int enabled_crtcs = 0, active_crtcs = 0;
8435 bool active;
8436
8437 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
8438
8439 DRM_DEBUG_KMS("%s\n", pll->name);
8440
8441 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
8442
8443 WARN(pll->active > pll->refcount,
8444 "more active pll users than references: %i vs %i\n",
8445 pll->active, pll->refcount);
8446 WARN(pll->active && !pll->on,
8447 "pll in active use but not on in sw tracking\n");
8448 WARN(pll->on != active,
8449 "pll on state mismatch (expected %i, found %i)\n",
8450 pll->on, active);
8451
8452 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
8453 base.head) {
8454 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
8455 enabled_crtcs++;
8456 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
8457 active_crtcs++;
8458 }
8459 WARN(pll->active != active_crtcs,
8460 "pll active crtcs mismatch (expected %i, found %i)\n",
8461 pll->active, active_crtcs);
8462 WARN(pll->refcount != enabled_crtcs,
8463 "pll enabled crtcs mismatch (expected %i, found %i)\n",
8464 pll->refcount, enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008465
8466 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
8467 sizeof(dpll_hw_state)),
8468 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +02008469 }
Daniel Vettera6778b32012-07-02 09:56:42 +02008470}
8471
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02008472void
8473intel_modeset_check_state(struct drm_device *dev)
8474{
8475 check_connector_state(dev);
8476 check_encoder_state(dev);
8477 check_crtc_state(dev);
8478 check_shared_dpll_state(dev);
8479}
8480
Daniel Vetterf30da182013-04-11 20:22:50 +02008481static int __intel_set_mode(struct drm_crtc *crtc,
8482 struct drm_display_mode *mode,
8483 int x, int y, struct drm_framebuffer *fb)
Daniel Vettera6778b32012-07-02 09:56:42 +02008484{
8485 struct drm_device *dev = crtc->dev;
Daniel Vetterdbf2b54e2012-07-02 11:18:29 +02008486 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008487 struct drm_display_mode *saved_mode, *saved_hwmode;
8488 struct intel_crtc_config *pipe_config = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +02008489 struct intel_crtc *intel_crtc;
8490 unsigned disable_pipes, prepare_pipes, modeset_pipes;
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008491 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +02008492
Tim Gardner3ac18232012-12-07 07:54:26 -07008493 saved_mode = kmalloc(2 * sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008494 if (!saved_mode)
8495 return -ENOMEM;
Tim Gardner3ac18232012-12-07 07:54:26 -07008496 saved_hwmode = saved_mode + 1;
Daniel Vettera6778b32012-07-02 09:56:42 +02008497
Daniel Vettere2e1ed42012-07-08 21:14:38 +02008498 intel_modeset_affected_pipes(crtc, &modeset_pipes,
Daniel Vetter25c5b262012-07-08 22:08:04 +02008499 &prepare_pipes, &disable_pipes);
8500
Tim Gardner3ac18232012-12-07 07:54:26 -07008501 *saved_hwmode = crtc->hwmode;
8502 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02008503
Daniel Vetter25c5b262012-07-08 22:08:04 +02008504 /* Hack: Because we don't (yet) support global modeset on multiple
8505 * crtcs, we don't keep track of the new mode for more than one crtc.
8506 * Hence simply check whether any bit is set in modeset_pipes in all the
8507 * pieces of code that are not yet converted to deal with mutliple crtcs
8508 * changing their mode at the same time. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02008509 if (modeset_pipes) {
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008510 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008511 if (IS_ERR(pipe_config)) {
8512 ret = PTR_ERR(pipe_config);
8513 pipe_config = NULL;
8514
Tim Gardner3ac18232012-12-07 07:54:26 -07008515 goto out;
Daniel Vetter25c5b262012-07-08 22:08:04 +02008516 }
Daniel Vetterc0b03412013-05-28 12:05:54 +02008517 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
8518 "[modeset]");
Daniel Vettera6778b32012-07-02 09:56:42 +02008519 }
8520
Daniel Vetter460da9162013-03-27 00:44:51 +01008521 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
8522 intel_crtc_disable(&intel_crtc->base);
8523
Daniel Vetterea9d7582012-07-10 10:42:52 +02008524 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
8525 if (intel_crtc->base.enabled)
8526 dev_priv->display.crtc_disable(&intel_crtc->base);
8527 }
Daniel Vettera6778b32012-07-02 09:56:42 +02008528
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02008529 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
8530 * to set it here already despite that we pass it down the callchain.
8531 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008532 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +02008533 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008534 /* mode_set/enable/disable functions rely on a correct pipe
8535 * config. */
8536 to_intel_crtc(crtc)->config = *pipe_config;
8537 }
Daniel Vetter7758a112012-07-08 19:40:39 +02008538
Daniel Vetterea9d7582012-07-10 10:42:52 +02008539 /* Only after disabling all output pipelines that will be changed can we
8540 * update the the output configuration. */
8541 intel_modeset_update_state(dev, prepare_pipes);
8542
Daniel Vetter47fab732012-10-26 10:58:18 +02008543 if (dev_priv->display.modeset_global_resources)
8544 dev_priv->display.modeset_global_resources(dev);
8545
Daniel Vettera6778b32012-07-02 09:56:42 +02008546 /* Set up the DPLL and any encoders state that needs to adjust or depend
8547 * on the DPLL.
8548 */
Daniel Vetter25c5b262012-07-08 22:08:04 +02008549 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008550 ret = intel_crtc_mode_set(&intel_crtc->base,
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008551 x, y, fb);
8552 if (ret)
8553 goto done;
Daniel Vettera6778b32012-07-02 09:56:42 +02008554 }
8555
8556 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02008557 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
8558 dev_priv->display.crtc_enable(&intel_crtc->base);
Daniel Vettera6778b32012-07-02 09:56:42 +02008559
Daniel Vetter25c5b262012-07-08 22:08:04 +02008560 if (modeset_pipes) {
8561 /* Store real post-adjustment hardware mode. */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008562 crtc->hwmode = pipe_config->adjusted_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02008563
Daniel Vetter25c5b262012-07-08 22:08:04 +02008564 /* Calculate and store various constants which
8565 * are later needed by vblank and swap-completion
8566 * timestamping. They are derived from true hwmode.
8567 */
8568 drm_calc_timestamping_constants(crtc);
8569 }
Daniel Vettera6778b32012-07-02 09:56:42 +02008570
8571 /* FIXME: add subpixel order */
8572done:
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008573 if (ret && crtc->enabled) {
Tim Gardner3ac18232012-12-07 07:54:26 -07008574 crtc->hwmode = *saved_hwmode;
8575 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02008576 }
8577
Tim Gardner3ac18232012-12-07 07:54:26 -07008578out:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008579 kfree(pipe_config);
Tim Gardner3ac18232012-12-07 07:54:26 -07008580 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +02008581 return ret;
8582}
8583
Daniel Vetterf30da182013-04-11 20:22:50 +02008584int intel_set_mode(struct drm_crtc *crtc,
8585 struct drm_display_mode *mode,
8586 int x, int y, struct drm_framebuffer *fb)
8587{
8588 int ret;
8589
8590 ret = __intel_set_mode(crtc, mode, x, y, fb);
8591
8592 if (ret == 0)
8593 intel_modeset_check_state(crtc->dev);
8594
8595 return ret;
8596}
8597
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008598void intel_crtc_restore_mode(struct drm_crtc *crtc)
8599{
8600 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->fb);
8601}
8602
Daniel Vetter25c5b262012-07-08 22:08:04 +02008603#undef for_each_intel_crtc_masked
8604
Daniel Vetterd9e55602012-07-04 22:16:09 +02008605static void intel_set_config_free(struct intel_set_config *config)
8606{
8607 if (!config)
8608 return;
8609
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008610 kfree(config->save_connector_encoders);
8611 kfree(config->save_encoder_crtcs);
Daniel Vetterd9e55602012-07-04 22:16:09 +02008612 kfree(config);
8613}
8614
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008615static int intel_set_config_save_state(struct drm_device *dev,
8616 struct intel_set_config *config)
8617{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008618 struct drm_encoder *encoder;
8619 struct drm_connector *connector;
8620 int count;
8621
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008622 config->save_encoder_crtcs =
8623 kcalloc(dev->mode_config.num_encoder,
8624 sizeof(struct drm_crtc *), GFP_KERNEL);
8625 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008626 return -ENOMEM;
8627
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008628 config->save_connector_encoders =
8629 kcalloc(dev->mode_config.num_connector,
8630 sizeof(struct drm_encoder *), GFP_KERNEL);
8631 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008632 return -ENOMEM;
8633
8634 /* Copy data. Note that driver private data is not affected.
8635 * Should anything bad happen only the expected state is
8636 * restored, not the drivers personal bookkeeping.
8637 */
8638 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008639 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008640 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008641 }
8642
8643 count = 0;
8644 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008645 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008646 }
8647
8648 return 0;
8649}
8650
8651static void intel_set_config_restore_state(struct drm_device *dev,
8652 struct intel_set_config *config)
8653{
Daniel Vetter9a935852012-07-05 22:34:27 +02008654 struct intel_encoder *encoder;
8655 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008656 int count;
8657
8658 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008659 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
8660 encoder->new_crtc =
8661 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008662 }
8663
8664 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008665 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
8666 connector->new_encoder =
8667 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008668 }
8669}
8670
Imre Deake3de42b2013-05-03 19:44:07 +02008671static bool
8672is_crtc_connector_off(struct drm_crtc *crtc, struct drm_connector *connectors,
8673 int num_connectors)
8674{
8675 int i;
8676
8677 for (i = 0; i < num_connectors; i++)
8678 if (connectors[i].encoder &&
8679 connectors[i].encoder->crtc == crtc &&
8680 connectors[i].dpms != DRM_MODE_DPMS_ON)
8681 return true;
8682
8683 return false;
8684}
8685
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008686static void
8687intel_set_config_compute_mode_changes(struct drm_mode_set *set,
8688 struct intel_set_config *config)
8689{
8690
8691 /* We should be able to check here if the fb has the same properties
8692 * and then just flip_or_move it */
Imre Deake3de42b2013-05-03 19:44:07 +02008693 if (set->connectors != NULL &&
8694 is_crtc_connector_off(set->crtc, *set->connectors,
8695 set->num_connectors)) {
8696 config->mode_changed = true;
8697 } else if (set->crtc->fb != set->fb) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008698 /* If we have no fb then treat it as a full mode set */
8699 if (set->crtc->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +03008700 struct intel_crtc *intel_crtc =
8701 to_intel_crtc(set->crtc);
8702
8703 if (intel_crtc->active && i915_fastboot) {
8704 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
8705 config->fb_changed = true;
8706 } else {
8707 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
8708 config->mode_changed = true;
8709 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008710 } else if (set->fb == NULL) {
8711 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +01008712 } else if (set->fb->pixel_format !=
8713 set->crtc->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008714 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02008715 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008716 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02008717 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008718 }
8719
Daniel Vetter835c5872012-07-10 18:11:08 +02008720 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008721 config->fb_changed = true;
8722
8723 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
8724 DRM_DEBUG_KMS("modes are different, full mode set\n");
8725 drm_mode_debug_printmodeline(&set->crtc->mode);
8726 drm_mode_debug_printmodeline(set->mode);
8727 config->mode_changed = true;
8728 }
8729}
8730
Daniel Vetter2e431052012-07-04 22:42:15 +02008731static int
Daniel Vetter9a935852012-07-05 22:34:27 +02008732intel_modeset_stage_output_state(struct drm_device *dev,
8733 struct drm_mode_set *set,
8734 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +02008735{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008736 struct drm_crtc *new_crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02008737 struct intel_connector *connector;
8738 struct intel_encoder *encoder;
Daniel Vetter2e431052012-07-04 22:42:15 +02008739 int count, ro;
Daniel Vetter50f56112012-07-02 09:35:43 +02008740
Damien Lespiau9abdda72013-02-13 13:29:23 +00008741 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +02008742 * of connectors. For paranoia, double-check this. */
8743 WARN_ON(!set->fb && (set->num_connectors != 0));
8744 WARN_ON(set->fb && (set->num_connectors == 0));
8745
Daniel Vetter50f56112012-07-02 09:35:43 +02008746 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008747 list_for_each_entry(connector, &dev->mode_config.connector_list,
8748 base.head) {
8749 /* Otherwise traverse passed in connector list and get encoders
8750 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +02008751 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02008752 if (set->connectors[ro] == &connector->base) {
8753 connector->new_encoder = connector->encoder;
Daniel Vetter50f56112012-07-02 09:35:43 +02008754 break;
8755 }
8756 }
8757
Daniel Vetter9a935852012-07-05 22:34:27 +02008758 /* If we disable the crtc, disable all its connectors. Also, if
8759 * the connector is on the changing crtc but not on the new
8760 * connector list, disable it. */
8761 if ((!set->fb || ro == set->num_connectors) &&
8762 connector->base.encoder &&
8763 connector->base.encoder->crtc == set->crtc) {
8764 connector->new_encoder = NULL;
8765
8766 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
8767 connector->base.base.id,
8768 drm_get_connector_name(&connector->base));
8769 }
8770
8771
8772 if (&connector->new_encoder->base != connector->base.encoder) {
Daniel Vetter50f56112012-07-02 09:35:43 +02008773 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008774 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02008775 }
Daniel Vetter9a935852012-07-05 22:34:27 +02008776 }
8777 /* connector->new_encoder is now updated for all connectors. */
8778
8779 /* Update crtc of enabled connectors. */
Daniel Vetter50f56112012-07-02 09:35:43 +02008780 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008781 list_for_each_entry(connector, &dev->mode_config.connector_list,
8782 base.head) {
8783 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +02008784 continue;
8785
Daniel Vetter9a935852012-07-05 22:34:27 +02008786 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +02008787
8788 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02008789 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +02008790 new_crtc = set->crtc;
8791 }
8792
8793 /* Make sure the new CRTC will work with the encoder */
Daniel Vetter9a935852012-07-05 22:34:27 +02008794 if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
8795 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008796 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +02008797 }
Daniel Vetter9a935852012-07-05 22:34:27 +02008798 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
8799
8800 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
8801 connector->base.base.id,
8802 drm_get_connector_name(&connector->base),
8803 new_crtc->base.id);
8804 }
8805
8806 /* Check for any encoders that needs to be disabled. */
8807 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8808 base.head) {
8809 list_for_each_entry(connector,
8810 &dev->mode_config.connector_list,
8811 base.head) {
8812 if (connector->new_encoder == encoder) {
8813 WARN_ON(!connector->new_encoder->new_crtc);
8814
8815 goto next_encoder;
8816 }
8817 }
8818 encoder->new_crtc = NULL;
8819next_encoder:
8820 /* Only now check for crtc changes so we don't miss encoders
8821 * that will be disabled. */
8822 if (&encoder->new_crtc->base != encoder->base.crtc) {
Daniel Vetter50f56112012-07-02 09:35:43 +02008823 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008824 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02008825 }
8826 }
Daniel Vetter9a935852012-07-05 22:34:27 +02008827 /* Now we've also updated encoder->new_crtc for all encoders. */
Daniel Vetter50f56112012-07-02 09:35:43 +02008828
Daniel Vetter2e431052012-07-04 22:42:15 +02008829 return 0;
8830}
8831
8832static int intel_crtc_set_config(struct drm_mode_set *set)
8833{
8834 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +02008835 struct drm_mode_set save_set;
8836 struct intel_set_config *config;
8837 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +02008838
Daniel Vetter8d3e3752012-07-05 16:09:09 +02008839 BUG_ON(!set);
8840 BUG_ON(!set->crtc);
8841 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +02008842
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01008843 /* Enforce sane interface api - has been abused by the fb helper. */
8844 BUG_ON(!set->mode && set->fb);
8845 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +02008846
Daniel Vetter2e431052012-07-04 22:42:15 +02008847 if (set->fb) {
8848 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
8849 set->crtc->base.id, set->fb->base.id,
8850 (int)set->num_connectors, set->x, set->y);
8851 } else {
8852 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +02008853 }
8854
8855 dev = set->crtc->dev;
8856
8857 ret = -ENOMEM;
8858 config = kzalloc(sizeof(*config), GFP_KERNEL);
8859 if (!config)
8860 goto out_config;
8861
8862 ret = intel_set_config_save_state(dev, config);
8863 if (ret)
8864 goto out_config;
8865
8866 save_set.crtc = set->crtc;
8867 save_set.mode = &set->crtc->mode;
8868 save_set.x = set->crtc->x;
8869 save_set.y = set->crtc->y;
8870 save_set.fb = set->crtc->fb;
8871
8872 /* Compute whether we need a full modeset, only an fb base update or no
8873 * change at all. In the future we might also check whether only the
8874 * mode changed, e.g. for LVDS where we only change the panel fitter in
8875 * such cases. */
8876 intel_set_config_compute_mode_changes(set, config);
8877
Daniel Vetter9a935852012-07-05 22:34:27 +02008878 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +02008879 if (ret)
8880 goto fail;
8881
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008882 if (config->mode_changed) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008883 ret = intel_set_mode(set->crtc, set->mode,
8884 set->x, set->y, set->fb);
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008885 } else if (config->fb_changed) {
Ville Syrjälä4878cae2013-02-18 19:08:48 +02008886 intel_crtc_wait_for_pending_flips(set->crtc);
8887
Daniel Vetter4f660f42012-07-02 09:47:37 +02008888 ret = intel_pipe_set_base(set->crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +02008889 set->x, set->y, set->fb);
Daniel Vetter50f56112012-07-02 09:35:43 +02008890 }
8891
Chris Wilson2d05eae2013-05-03 17:36:25 +01008892 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +02008893 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
8894 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +02008895fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +01008896 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +02008897
Chris Wilson2d05eae2013-05-03 17:36:25 +01008898 /* Try to restore the config */
8899 if (config->mode_changed &&
8900 intel_set_mode(save_set.crtc, save_set.mode,
8901 save_set.x, save_set.y, save_set.fb))
8902 DRM_ERROR("failed to restore config after modeset failure\n");
8903 }
Daniel Vetter50f56112012-07-02 09:35:43 +02008904
Daniel Vetterd9e55602012-07-04 22:16:09 +02008905out_config:
8906 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +02008907 return ret;
8908}
8909
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008910static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008911 .cursor_set = intel_crtc_cursor_set,
8912 .cursor_move = intel_crtc_cursor_move,
8913 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +02008914 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008915 .destroy = intel_crtc_destroy,
8916 .page_flip = intel_crtc_page_flip,
8917};
8918
Paulo Zanoni79f689a2012-10-05 12:05:52 -03008919static void intel_cpu_pll_init(struct drm_device *dev)
8920{
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008921 if (HAS_DDI(dev))
Paulo Zanoni79f689a2012-10-05 12:05:52 -03008922 intel_ddi_pll_init(dev);
8923}
8924
Daniel Vetter53589012013-06-05 13:34:16 +02008925static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
8926 struct intel_shared_dpll *pll,
8927 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008928{
Daniel Vetter53589012013-06-05 13:34:16 +02008929 uint32_t val;
8930
8931 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +02008932 hw_state->dpll = val;
8933 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
8934 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +02008935
8936 return val & DPLL_VCO_ENABLE;
8937}
8938
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02008939static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
8940 struct intel_shared_dpll *pll)
8941{
8942 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
8943 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
8944}
8945
Daniel Vettere7b903d2013-06-05 13:34:14 +02008946static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
8947 struct intel_shared_dpll *pll)
8948{
Daniel Vettere7b903d2013-06-05 13:34:14 +02008949 /* PCH refclock must be enabled first */
8950 assert_pch_refclk_enabled(dev_priv);
8951
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02008952 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
8953
8954 /* Wait for the clocks to stabilize. */
8955 POSTING_READ(PCH_DPLL(pll->id));
8956 udelay(150);
8957
8958 /* The pixel multiplier can only be updated once the
8959 * DPLL is enabled and the clocks are stable.
8960 *
8961 * So write it again.
8962 */
8963 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
8964 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +02008965 udelay(200);
8966}
8967
8968static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
8969 struct intel_shared_dpll *pll)
8970{
8971 struct drm_device *dev = dev_priv->dev;
8972 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +02008973
8974 /* Make sure no transcoder isn't still depending on us. */
8975 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
8976 if (intel_crtc_to_shared_dpll(crtc) == pll)
8977 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
8978 }
8979
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02008980 I915_WRITE(PCH_DPLL(pll->id), 0);
8981 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +02008982 udelay(200);
8983}
8984
Daniel Vetter46edb022013-06-05 13:34:12 +02008985static char *ibx_pch_dpll_names[] = {
8986 "PCH DPLL A",
8987 "PCH DPLL B",
8988};
8989
Daniel Vetter7c74ade2013-06-05 13:34:11 +02008990static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008991{
Daniel Vettere7b903d2013-06-05 13:34:14 +02008992 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008993 int i;
8994
Daniel Vetter7c74ade2013-06-05 13:34:11 +02008995 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008996
Daniel Vettere72f9fb2013-06-05 13:34:06 +02008997 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +02008998 dev_priv->shared_dplls[i].id = i;
8999 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02009000 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +02009001 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
9002 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +02009003 dev_priv->shared_dplls[i].get_hw_state =
9004 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009005 }
9006}
9007
Daniel Vetter7c74ade2013-06-05 13:34:11 +02009008static void intel_shared_dpll_init(struct drm_device *dev)
9009{
Daniel Vettere7b903d2013-06-05 13:34:14 +02009010 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +02009011
9012 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
9013 ibx_pch_dpll_init(dev);
9014 else
9015 dev_priv->num_shared_dpll = 0;
9016
9017 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
9018 DRM_DEBUG_KMS("%i shared PLLs initialized\n",
9019 dev_priv->num_shared_dpll);
9020}
9021
Hannes Ederb358d0a2008-12-18 21:18:47 +01009022static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -08009023{
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08009024 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08009025 struct intel_crtc *intel_crtc;
9026 int i;
9027
9028 intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
9029 if (intel_crtc == NULL)
9030 return;
9031
9032 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
9033
9034 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -08009035 for (i = 0; i < 256; i++) {
9036 intel_crtc->lut_r[i] = i;
9037 intel_crtc->lut_g[i] = i;
9038 intel_crtc->lut_b[i] = i;
9039 }
9040
Jesse Barnes80824002009-09-10 15:28:06 -07009041 /* Swap pipes & planes for FBC on pre-965 */
9042 intel_crtc->pipe = pipe;
9043 intel_crtc->plane = pipe;
Chris Wilsone2e767a2010-09-13 16:53:12 +01009044 if (IS_MOBILE(dev) && IS_GEN3(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08009045 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +01009046 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07009047 }
9048
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08009049 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
9050 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
9051 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
9052 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
9053
Jesse Barnes79e53942008-11-07 14:24:08 -08009054 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -08009055}
9056
Carl Worth08d7b3d2009-04-29 14:43:54 -07009057int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00009058 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -07009059{
Carl Worth08d7b3d2009-04-29 14:43:54 -07009060 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +02009061 struct drm_mode_object *drmmode_obj;
9062 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009063
Daniel Vetter1cff8f62012-04-24 09:55:08 +02009064 if (!drm_core_check_feature(dev, DRIVER_MODESET))
9065 return -ENODEV;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009066
Daniel Vetterc05422d2009-08-11 16:05:30 +02009067 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
9068 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -07009069
Daniel Vetterc05422d2009-08-11 16:05:30 +02009070 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -07009071 DRM_ERROR("no such CRTC id\n");
9072 return -EINVAL;
9073 }
9074
Daniel Vetterc05422d2009-08-11 16:05:30 +02009075 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
9076 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009077
Daniel Vetterc05422d2009-08-11 16:05:30 +02009078 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009079}
9080
Daniel Vetter66a92782012-07-12 20:08:18 +02009081static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08009082{
Daniel Vetter66a92782012-07-12 20:08:18 +02009083 struct drm_device *dev = encoder->base.dev;
9084 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08009085 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009086 int entry = 0;
9087
Daniel Vetter66a92782012-07-12 20:08:18 +02009088 list_for_each_entry(source_encoder,
9089 &dev->mode_config.encoder_list, base.head) {
9090
9091 if (encoder == source_encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08009092 index_mask |= (1 << entry);
Daniel Vetter66a92782012-07-12 20:08:18 +02009093
9094 /* Intel hw has only one MUX where enocoders could be cloned. */
9095 if (encoder->cloneable && source_encoder->cloneable)
9096 index_mask |= (1 << entry);
9097
Jesse Barnes79e53942008-11-07 14:24:08 -08009098 entry++;
9099 }
Chris Wilson4ef69c72010-09-09 15:14:28 +01009100
Jesse Barnes79e53942008-11-07 14:24:08 -08009101 return index_mask;
9102}
9103
Chris Wilson4d302442010-12-14 19:21:29 +00009104static bool has_edp_a(struct drm_device *dev)
9105{
9106 struct drm_i915_private *dev_priv = dev->dev_private;
9107
9108 if (!IS_MOBILE(dev))
9109 return false;
9110
9111 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
9112 return false;
9113
9114 if (IS_GEN5(dev) &&
9115 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
9116 return false;
9117
9118 return true;
9119}
9120
Jesse Barnes79e53942008-11-07 14:24:08 -08009121static void intel_setup_outputs(struct drm_device *dev)
9122{
Eric Anholt725e30a2009-01-22 13:01:02 -08009123 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +01009124 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009125 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08009126
Daniel Vetterc9093352013-06-06 22:22:47 +02009127 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009128
Paulo Zanonic40c0f52013-04-12 18:16:53 -03009129 if (!IS_ULT(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -02009130 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009131
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009132 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -03009133 int found;
9134
9135 /* Haswell uses DDI functions to detect digital outputs */
9136 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
9137 /* DDI A only supports eDP */
9138 if (found)
9139 intel_ddi_init(dev, PORT_A);
9140
9141 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
9142 * register */
9143 found = I915_READ(SFUSE_STRAP);
9144
9145 if (found & SFUSE_STRAP_DDIB_DETECTED)
9146 intel_ddi_init(dev, PORT_B);
9147 if (found & SFUSE_STRAP_DDIC_DETECTED)
9148 intel_ddi_init(dev, PORT_C);
9149 if (found & SFUSE_STRAP_DDID_DETECTED)
9150 intel_ddi_init(dev, PORT_D);
9151 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009152 int found;
Daniel Vetter270b3042012-10-27 15:52:05 +02009153 dpd_is_edp = intel_dpd_is_edp(dev);
9154
9155 if (has_edp_a(dev))
9156 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009157
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009158 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +08009159 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +01009160 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009161 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -03009162 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08009163 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009164 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009165 }
9166
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009167 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -03009168 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009169
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009170 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -03009171 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009172
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08009173 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009174 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08009175
Daniel Vetter270b3042012-10-27 15:52:05 +02009176 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009177 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -07009178 } else if (IS_VALLEYVIEW(dev)) {
Gajanan Bhat19c03922012-09-27 19:13:07 +05309179 /* Check for built-in panel first. Shares lanes with HDMI on SDVOC */
Ville Syrjälä67cfc202013-01-25 21:44:44 +02009180 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
9181 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +05309182
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009183 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
Paulo Zanonie2debe92013-02-18 19:00:27 -03009184 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
9185 PORT_B);
Ville Syrjälä67cfc202013-01-25 21:44:44 +02009186 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
9187 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Jesse Barnes4a87d652012-06-15 11:55:16 -07009188 }
Zhenyu Wang103a1962009-11-27 11:44:36 +08009189 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +08009190 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -08009191
Paulo Zanonie2debe92013-02-18 19:00:27 -03009192 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009193 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009194 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009195 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
9196 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009197 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009198 }
Ma Ling27185ae2009-08-24 13:50:23 +08009199
Imre Deake7281ea2013-05-08 13:14:08 +03009200 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009201 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -08009202 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -04009203
9204 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -04009205
Paulo Zanonie2debe92013-02-18 19:00:27 -03009206 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009207 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009208 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009209 }
Ma Ling27185ae2009-08-24 13:50:23 +08009210
Paulo Zanonie2debe92013-02-18 19:00:27 -03009211 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +08009212
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009213 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
9214 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009215 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009216 }
Imre Deake7281ea2013-05-08 13:14:08 +03009217 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009218 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -08009219 }
Ma Ling27185ae2009-08-24 13:50:23 +08009220
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009221 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +03009222 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009223 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -07009224 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08009225 intel_dvo_init(dev);
9226
Zhenyu Wang103a1962009-11-27 11:44:36 +08009227 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08009228 intel_tv_init(dev);
9229
Chris Wilson4ef69c72010-09-09 15:14:28 +01009230 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
9231 encoder->base.possible_crtcs = encoder->crtc_mask;
9232 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +02009233 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -08009234 }
Chris Wilson47356eb2011-01-11 17:06:04 +00009235
Paulo Zanonidde86e22012-12-01 12:04:25 -02009236 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +02009237
9238 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009239}
9240
9241static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
9242{
9243 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08009244
9245 drm_framebuffer_cleanup(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00009246 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08009247
9248 kfree(intel_fb);
9249}
9250
9251static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +00009252 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08009253 unsigned int *handle)
9254{
9255 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00009256 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08009257
Chris Wilson05394f32010-11-08 19:18:58 +00009258 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -08009259}
9260
9261static const struct drm_framebuffer_funcs intel_fb_funcs = {
9262 .destroy = intel_user_framebuffer_destroy,
9263 .create_handle = intel_user_framebuffer_create_handle,
9264};
9265
Dave Airlie38651672010-03-30 05:34:13 +00009266int intel_framebuffer_init(struct drm_device *dev,
9267 struct intel_framebuffer *intel_fb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009268 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilson05394f32010-11-08 19:18:58 +00009269 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -08009270{
Chris Wilsona35cdaa2013-06-25 17:26:45 +01009271 int pitch_limit;
Jesse Barnes79e53942008-11-07 14:24:08 -08009272 int ret;
9273
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009274 if (obj->tiling_mode == I915_TILING_Y) {
9275 DRM_DEBUG("hardware does not support tiling Y\n");
Chris Wilson57cd6502010-08-08 12:34:44 +01009276 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009277 }
Chris Wilson57cd6502010-08-08 12:34:44 +01009278
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009279 if (mode_cmd->pitches[0] & 63) {
9280 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
9281 mode_cmd->pitches[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +01009282 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009283 }
Chris Wilson57cd6502010-08-08 12:34:44 +01009284
Chris Wilsona35cdaa2013-06-25 17:26:45 +01009285 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
9286 pitch_limit = 32*1024;
9287 } else if (INTEL_INFO(dev)->gen >= 4) {
9288 if (obj->tiling_mode)
9289 pitch_limit = 16*1024;
9290 else
9291 pitch_limit = 32*1024;
9292 } else if (INTEL_INFO(dev)->gen >= 3) {
9293 if (obj->tiling_mode)
9294 pitch_limit = 8*1024;
9295 else
9296 pitch_limit = 16*1024;
9297 } else
9298 /* XXX DSPC is limited to 4k tiled */
9299 pitch_limit = 8*1024;
9300
9301 if (mode_cmd->pitches[0] > pitch_limit) {
9302 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
9303 obj->tiling_mode ? "tiled" : "linear",
9304 mode_cmd->pitches[0], pitch_limit);
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
9308 if (obj->tiling_mode != I915_TILING_NONE &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009309 mode_cmd->pitches[0] != obj->stride) {
9310 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
9311 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02009312 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009313 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02009314
Ville Syrjälä57779d02012-10-31 17:50:14 +02009315 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009316 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +02009317 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02009318 case DRM_FORMAT_RGB565:
9319 case DRM_FORMAT_XRGB8888:
9320 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02009321 break;
9322 case DRM_FORMAT_XRGB1555:
9323 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009324 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +00009325 DRM_DEBUG("unsupported pixel format: %s\n",
9326 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +02009327 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009328 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02009329 break;
9330 case DRM_FORMAT_XBGR8888:
9331 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +02009332 case DRM_FORMAT_XRGB2101010:
9333 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02009334 case DRM_FORMAT_XBGR2101010:
9335 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009336 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +00009337 DRM_DEBUG("unsupported pixel format: %s\n",
9338 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +02009339 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009340 }
Jesse Barnesb5626742011-06-24 12:19:27 -07009341 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02009342 case DRM_FORMAT_YUYV:
9343 case DRM_FORMAT_UYVY:
9344 case DRM_FORMAT_YVYU:
9345 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009346 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +00009347 DRM_DEBUG("unsupported pixel format: %s\n",
9348 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +02009349 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009350 }
Chris Wilson57cd6502010-08-08 12:34:44 +01009351 break;
9352 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +00009353 DRM_DEBUG("unsupported pixel format: %s\n",
9354 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +01009355 return -EINVAL;
9356 }
9357
Ville Syrjälä90f9a332012-10-31 17:50:19 +02009358 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
9359 if (mode_cmd->offsets[0] != 0)
9360 return -EINVAL;
9361
Daniel Vetterc7d73f62012-12-13 23:38:38 +01009362 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
9363 intel_fb->obj = obj;
9364
Jesse Barnes79e53942008-11-07 14:24:08 -08009365 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
9366 if (ret) {
9367 DRM_ERROR("framebuffer init failed %d\n", ret);
9368 return ret;
9369 }
9370
Jesse Barnes79e53942008-11-07 14:24:08 -08009371 return 0;
9372}
9373
Jesse Barnes79e53942008-11-07 14:24:08 -08009374static struct drm_framebuffer *
9375intel_user_framebuffer_create(struct drm_device *dev,
9376 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009377 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -08009378{
Chris Wilson05394f32010-11-08 19:18:58 +00009379 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08009380
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009381 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
9382 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +00009383 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +01009384 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -08009385
Chris Wilsond2dff872011-04-19 08:36:26 +01009386 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08009387}
9388
Jesse Barnes79e53942008-11-07 14:24:08 -08009389static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -08009390 .fb_create = intel_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +00009391 .output_poll_changed = intel_fb_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -08009392};
9393
Jesse Barnese70236a2009-09-21 10:42:27 -07009394/* Set up chip specific display functions */
9395static void intel_init_display(struct drm_device *dev)
9396{
9397 struct drm_i915_private *dev_priv = dev->dev_private;
9398
Daniel Vetteree9300b2013-06-03 22:40:22 +02009399 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
9400 dev_priv->display.find_dpll = g4x_find_best_dpll;
9401 else if (IS_VALLEYVIEW(dev))
9402 dev_priv->display.find_dpll = vlv_find_best_dpll;
9403 else if (IS_PINEVIEW(dev))
9404 dev_priv->display.find_dpll = pnv_find_best_dpll;
9405 else
9406 dev_priv->display.find_dpll = i9xx_find_best_dpll;
9407
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009408 if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009409 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009410 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02009411 dev_priv->display.crtc_enable = haswell_crtc_enable;
9412 dev_priv->display.crtc_disable = haswell_crtc_disable;
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009413 dev_priv->display.off = haswell_crtc_off;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009414 dev_priv->display.update_plane = ironlake_update_plane;
9415 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009416 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009417 dev_priv->display.get_clock = ironlake_crtc_clock_get;
Eric Anholtf564048e2011-03-30 13:01:02 -07009418 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +02009419 dev_priv->display.crtc_enable = ironlake_crtc_enable;
9420 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009421 dev_priv->display.off = ironlake_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -07009422 dev_priv->display.update_plane = ironlake_update_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -07009423 } else if (IS_VALLEYVIEW(dev)) {
9424 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009425 dev_priv->display.get_clock = i9xx_crtc_clock_get;
Jesse Barnes89b667f2013-04-18 14:51:36 -07009426 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
9427 dev_priv->display.crtc_enable = valleyview_crtc_enable;
9428 dev_priv->display.crtc_disable = i9xx_crtc_disable;
9429 dev_priv->display.off = i9xx_crtc_off;
9430 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07009431 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009432 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009433 dev_priv->display.get_clock = i9xx_crtc_clock_get;
Eric Anholtf564048e2011-03-30 13:01:02 -07009434 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +02009435 dev_priv->display.crtc_enable = i9xx_crtc_enable;
9436 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009437 dev_priv->display.off = i9xx_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -07009438 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07009439 }
Jesse Barnese70236a2009-09-21 10:42:27 -07009440
Jesse Barnese70236a2009-09-21 10:42:27 -07009441 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07009442 if (IS_VALLEYVIEW(dev))
9443 dev_priv->display.get_display_clock_speed =
9444 valleyview_get_display_clock_speed;
9445 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -07009446 dev_priv->display.get_display_clock_speed =
9447 i945_get_display_clock_speed;
9448 else if (IS_I915G(dev))
9449 dev_priv->display.get_display_clock_speed =
9450 i915_get_display_clock_speed;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05009451 else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07009452 dev_priv->display.get_display_clock_speed =
9453 i9xx_misc_get_display_clock_speed;
9454 else if (IS_I915GM(dev))
9455 dev_priv->display.get_display_clock_speed =
9456 i915gm_get_display_clock_speed;
9457 else if (IS_I865G(dev))
9458 dev_priv->display.get_display_clock_speed =
9459 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +02009460 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07009461 dev_priv->display.get_display_clock_speed =
9462 i855_get_display_clock_speed;
9463 else /* 852, 830 */
9464 dev_priv->display.get_display_clock_speed =
9465 i830_get_display_clock_speed;
9466
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08009467 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01009468 if (IS_GEN5(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -07009469 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08009470 dev_priv->display.write_eld = ironlake_write_eld;
Yuanhan Liu13982612010-12-15 15:42:31 +08009471 } else if (IS_GEN6(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -07009472 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08009473 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnes357555c2011-04-28 15:09:55 -07009474 } else if (IS_IVYBRIDGE(dev)) {
9475 /* FIXME: detect B0+ stepping and use auto training */
9476 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08009477 dev_priv->display.write_eld = ironlake_write_eld;
Daniel Vetter01a415f2012-10-27 15:58:40 +02009478 dev_priv->display.modeset_global_resources =
9479 ivb_modeset_global_resources;
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -03009480 } else if (IS_HASWELL(dev)) {
9481 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Wang Xingchao83358c852012-08-16 22:43:37 +08009482 dev_priv->display.write_eld = haswell_write_eld;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02009483 dev_priv->display.modeset_global_resources =
9484 haswell_modeset_global_resources;
Paulo Zanonia0e63c22012-12-06 11:12:39 -02009485 }
Jesse Barnes6067aae2011-04-28 15:04:31 -07009486 } else if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +08009487 dev_priv->display.write_eld = g4x_write_eld;
Jesse Barnese70236a2009-09-21 10:42:27 -07009488 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009489
9490 /* Default just returns -ENODEV to indicate unsupported */
9491 dev_priv->display.queue_flip = intel_default_queue_flip;
9492
9493 switch (INTEL_INFO(dev)->gen) {
9494 case 2:
9495 dev_priv->display.queue_flip = intel_gen2_queue_flip;
9496 break;
9497
9498 case 3:
9499 dev_priv->display.queue_flip = intel_gen3_queue_flip;
9500 break;
9501
9502 case 4:
9503 case 5:
9504 dev_priv->display.queue_flip = intel_gen4_queue_flip;
9505 break;
9506
9507 case 6:
9508 dev_priv->display.queue_flip = intel_gen6_queue_flip;
9509 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009510 case 7:
9511 dev_priv->display.queue_flip = intel_gen7_queue_flip;
9512 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009513 }
Jesse Barnese70236a2009-09-21 10:42:27 -07009514}
9515
Jesse Barnesb690e962010-07-19 13:53:12 -07009516/*
9517 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
9518 * resume, or other times. This quirk makes sure that's the case for
9519 * affected systems.
9520 */
Akshay Joshi0206e352011-08-16 15:34:10 -04009521static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -07009522{
9523 struct drm_i915_private *dev_priv = dev->dev_private;
9524
9525 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02009526 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -07009527}
9528
Keith Packard435793d2011-07-12 14:56:22 -07009529/*
9530 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
9531 */
9532static void quirk_ssc_force_disable(struct drm_device *dev)
9533{
9534 struct drm_i915_private *dev_priv = dev->dev_private;
9535 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02009536 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -07009537}
9538
Carsten Emde4dca20e2012-03-15 15:56:26 +01009539/*
Carsten Emde5a15ab52012-03-15 15:56:27 +01009540 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
9541 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +01009542 */
9543static void quirk_invert_brightness(struct drm_device *dev)
9544{
9545 struct drm_i915_private *dev_priv = dev->dev_private;
9546 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02009547 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -07009548}
9549
9550struct intel_quirk {
9551 int device;
9552 int subsystem_vendor;
9553 int subsystem_device;
9554 void (*hook)(struct drm_device *dev);
9555};
9556
Egbert Eich5f85f1762012-10-14 15:46:38 +02009557/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
9558struct intel_dmi_quirk {
9559 void (*hook)(struct drm_device *dev);
9560 const struct dmi_system_id (*dmi_id_list)[];
9561};
9562
9563static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
9564{
9565 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
9566 return 1;
9567}
9568
9569static const struct intel_dmi_quirk intel_dmi_quirks[] = {
9570 {
9571 .dmi_id_list = &(const struct dmi_system_id[]) {
9572 {
9573 .callback = intel_dmi_reverse_brightness,
9574 .ident = "NCR Corporation",
9575 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
9576 DMI_MATCH(DMI_PRODUCT_NAME, ""),
9577 },
9578 },
9579 { } /* terminating entry */
9580 },
9581 .hook = quirk_invert_brightness,
9582 },
9583};
9584
Ben Widawskyc43b5632012-04-16 14:07:40 -07009585static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -07009586 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -04009587 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -07009588
Jesse Barnesb690e962010-07-19 13:53:12 -07009589 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
9590 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
9591
Jesse Barnesb690e962010-07-19 13:53:12 -07009592 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
9593 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
9594
Daniel Vetterccd0d362012-10-10 23:13:59 +02009595 /* 830/845 need to leave pipe A & dpll A up */
Jesse Barnesb690e962010-07-19 13:53:12 -07009596 { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Daniel Vetterdcdaed62012-08-12 21:19:34 +02009597 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Keith Packard435793d2011-07-12 14:56:22 -07009598
9599 /* Lenovo U160 cannot use SSC on LVDS */
9600 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +02009601
9602 /* Sony Vaio Y cannot use SSC on LVDS */
9603 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +01009604
9605 /* Acer Aspire 5734Z must invert backlight brightness */
9606 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
Jani Nikula1ffff602013-01-22 12:50:34 +02009607
9608 /* Acer/eMachines G725 */
9609 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
Jani Nikula01e3a8f2013-01-22 12:50:35 +02009610
9611 /* Acer/eMachines e725 */
9612 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
Jani Nikula5559eca2013-01-22 12:50:36 +02009613
9614 /* Acer/Packard Bell NCL20 */
9615 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
Daniel Vetterac4199e2013-02-15 18:35:30 +01009616
9617 /* Acer Aspire 4736Z */
9618 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jesse Barnesb690e962010-07-19 13:53:12 -07009619};
9620
9621static void intel_init_quirks(struct drm_device *dev)
9622{
9623 struct pci_dev *d = dev->pdev;
9624 int i;
9625
9626 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
9627 struct intel_quirk *q = &intel_quirks[i];
9628
9629 if (d->device == q->device &&
9630 (d->subsystem_vendor == q->subsystem_vendor ||
9631 q->subsystem_vendor == PCI_ANY_ID) &&
9632 (d->subsystem_device == q->subsystem_device ||
9633 q->subsystem_device == PCI_ANY_ID))
9634 q->hook(dev);
9635 }
Egbert Eich5f85f1762012-10-14 15:46:38 +02009636 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
9637 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
9638 intel_dmi_quirks[i].hook(dev);
9639 }
Jesse Barnesb690e962010-07-19 13:53:12 -07009640}
9641
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009642/* Disable the VGA plane that we never use */
9643static void i915_disable_vga(struct drm_device *dev)
9644{
9645 struct drm_i915_private *dev_priv = dev->dev_private;
9646 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +02009647 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009648
9649 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -07009650 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009651 sr1 = inb(VGA_SR_DATA);
9652 outb(sr1 | 1<<5, VGA_SR_DATA);
9653 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
9654 udelay(300);
9655
9656 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9657 POSTING_READ(vga_reg);
9658}
9659
Daniel Vetterf8175862012-04-10 15:50:11 +02009660void intel_modeset_init_hw(struct drm_device *dev)
9661{
Paulo Zanonifa42e232013-01-25 16:59:11 -02009662 intel_init_power_well(dev);
Eugeni Dodonov0232e922012-07-06 15:42:36 -03009663
Eugeni Dodonova8f78b52012-06-28 15:55:35 -03009664 intel_prepare_ddi(dev);
9665
Daniel Vetterf8175862012-04-10 15:50:11 +02009666 intel_init_clock_gating(dev);
9667
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02009668 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02009669 intel_enable_gt_powersave(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02009670 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf8175862012-04-10 15:50:11 +02009671}
9672
Imre Deak7d708ee2013-04-17 14:04:50 +03009673void intel_modeset_suspend_hw(struct drm_device *dev)
9674{
9675 intel_suspend_hw(dev);
9676}
9677
Jesse Barnes79e53942008-11-07 14:24:08 -08009678void intel_modeset_init(struct drm_device *dev)
9679{
Jesse Barnes652c3932009-08-17 13:31:43 -07009680 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7f1f3852013-04-02 11:22:20 -07009681 int i, j, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08009682
9683 drm_mode_config_init(dev);
9684
9685 dev->mode_config.min_width = 0;
9686 dev->mode_config.min_height = 0;
9687
Dave Airlie019d96c2011-09-29 16:20:42 +01009688 dev->mode_config.preferred_depth = 24;
9689 dev->mode_config.prefer_shadow = 1;
9690
Laurent Pincharte6ecefa2012-05-17 13:27:23 +02009691 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -08009692
Jesse Barnesb690e962010-07-19 13:53:12 -07009693 intel_init_quirks(dev);
9694
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009695 intel_init_pm(dev);
9696
Ben Widawskye3c74752013-04-05 13:12:39 -07009697 if (INTEL_INFO(dev)->num_pipes == 0)
9698 return;
9699
Jesse Barnese70236a2009-09-21 10:42:27 -07009700 intel_init_display(dev);
9701
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009702 if (IS_GEN2(dev)) {
9703 dev->mode_config.max_width = 2048;
9704 dev->mode_config.max_height = 2048;
9705 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -07009706 dev->mode_config.max_width = 4096;
9707 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -08009708 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009709 dev->mode_config.max_width = 8192;
9710 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -08009711 }
Ben Widawsky5d4545a2013-01-17 12:45:15 -08009712 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -08009713
Zhao Yakui28c97732009-10-09 11:39:41 +08009714 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -07009715 INTEL_INFO(dev)->num_pipes,
9716 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -08009717
Damien Lespiau08e2a7d2013-07-11 20:10:54 +01009718 for_each_pipe(i) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009719 intel_crtc_init(dev, i);
Jesse Barnes7f1f3852013-04-02 11:22:20 -07009720 for (j = 0; j < dev_priv->num_plane; j++) {
9721 ret = intel_plane_init(dev, i, j);
9722 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +03009723 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
9724 pipe_name(i), sprite_name(i, j), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -07009725 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009726 }
9727
Paulo Zanoni79f689a2012-10-05 12:05:52 -03009728 intel_cpu_pll_init(dev);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02009729 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009730
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009731 /* Just disable it once at startup */
9732 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009733 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +00009734
9735 /* Just in case the BIOS is doing something questionable. */
9736 intel_disable_fbc(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01009737}
Jesse Barnesd5bb0812011-01-05 12:01:26 -08009738
Daniel Vetter24929352012-07-02 20:28:59 +02009739static void
9740intel_connector_break_all_links(struct intel_connector *connector)
9741{
9742 connector->base.dpms = DRM_MODE_DPMS_OFF;
9743 connector->base.encoder = NULL;
9744 connector->encoder->connectors_active = false;
9745 connector->encoder->base.crtc = NULL;
9746}
9747
Daniel Vetter7fad7982012-07-04 17:51:47 +02009748static void intel_enable_pipe_a(struct drm_device *dev)
9749{
9750 struct intel_connector *connector;
9751 struct drm_connector *crt = NULL;
9752 struct intel_load_detect_pipe load_detect_temp;
9753
9754 /* We can't just switch on the pipe A, we need to set things up with a
9755 * proper mode and output configuration. As a gross hack, enable pipe A
9756 * by enabling the load detect pipe once. */
9757 list_for_each_entry(connector,
9758 &dev->mode_config.connector_list,
9759 base.head) {
9760 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
9761 crt = &connector->base;
9762 break;
9763 }
9764 }
9765
9766 if (!crt)
9767 return;
9768
9769 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
9770 intel_release_load_detect_pipe(crt, &load_detect_temp);
9771
9772
9773}
9774
Daniel Vetterfa555832012-10-10 23:14:00 +02009775static bool
9776intel_check_plane_mapping(struct intel_crtc *crtc)
9777{
Ben Widawsky7eb552a2013-03-13 14:05:41 -07009778 struct drm_device *dev = crtc->base.dev;
9779 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +02009780 u32 reg, val;
9781
Ben Widawsky7eb552a2013-03-13 14:05:41 -07009782 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +02009783 return true;
9784
9785 reg = DSPCNTR(!crtc->plane);
9786 val = I915_READ(reg);
9787
9788 if ((val & DISPLAY_PLANE_ENABLE) &&
9789 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
9790 return false;
9791
9792 return true;
9793}
9794
Daniel Vetter24929352012-07-02 20:28:59 +02009795static void intel_sanitize_crtc(struct intel_crtc *crtc)
9796{
9797 struct drm_device *dev = crtc->base.dev;
9798 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +02009799 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +02009800
Daniel Vetter24929352012-07-02 20:28:59 +02009801 /* Clear any frame start delays used for debugging left by the BIOS */
Daniel Vetter3b117c82013-04-17 20:15:07 +02009802 reg = PIPECONF(crtc->config.cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +02009803 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
9804
9805 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +02009806 * disable the crtc (and hence change the state) if it is wrong. Note
9807 * that gen4+ has a fixed plane -> pipe mapping. */
9808 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +02009809 struct intel_connector *connector;
9810 bool plane;
9811
Daniel Vetter24929352012-07-02 20:28:59 +02009812 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
9813 crtc->base.base.id);
9814
9815 /* Pipe has the wrong plane attached and the plane is active.
9816 * Temporarily change the plane mapping and disable everything
9817 * ... */
9818 plane = crtc->plane;
9819 crtc->plane = !plane;
9820 dev_priv->display.crtc_disable(&crtc->base);
9821 crtc->plane = plane;
9822
9823 /* ... and break all links. */
9824 list_for_each_entry(connector, &dev->mode_config.connector_list,
9825 base.head) {
9826 if (connector->encoder->base.crtc != &crtc->base)
9827 continue;
9828
9829 intel_connector_break_all_links(connector);
9830 }
9831
9832 WARN_ON(crtc->active);
9833 crtc->base.enabled = false;
9834 }
Daniel Vetter24929352012-07-02 20:28:59 +02009835
Daniel Vetter7fad7982012-07-04 17:51:47 +02009836 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
9837 crtc->pipe == PIPE_A && !crtc->active) {
9838 /* BIOS forgot to enable pipe A, this mostly happens after
9839 * resume. Force-enable the pipe to fix this, the update_dpms
9840 * call below we restore the pipe to the right state, but leave
9841 * the required bits on. */
9842 intel_enable_pipe_a(dev);
9843 }
9844
Daniel Vetter24929352012-07-02 20:28:59 +02009845 /* Adjust the state of the output pipe according to whether we
9846 * have active connectors/encoders. */
9847 intel_crtc_update_dpms(&crtc->base);
9848
9849 if (crtc->active != crtc->base.enabled) {
9850 struct intel_encoder *encoder;
9851
9852 /* This can happen either due to bugs in the get_hw_state
9853 * functions or because the pipe is force-enabled due to the
9854 * pipe A quirk. */
9855 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
9856 crtc->base.base.id,
9857 crtc->base.enabled ? "enabled" : "disabled",
9858 crtc->active ? "enabled" : "disabled");
9859
9860 crtc->base.enabled = crtc->active;
9861
9862 /* Because we only establish the connector -> encoder ->
9863 * crtc links if something is active, this means the
9864 * crtc is now deactivated. Break the links. connector
9865 * -> encoder links are only establish when things are
9866 * actually up, hence no need to break them. */
9867 WARN_ON(crtc->active);
9868
9869 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
9870 WARN_ON(encoder->connectors_active);
9871 encoder->base.crtc = NULL;
9872 }
9873 }
9874}
9875
9876static void intel_sanitize_encoder(struct intel_encoder *encoder)
9877{
9878 struct intel_connector *connector;
9879 struct drm_device *dev = encoder->base.dev;
9880
9881 /* We need to check both for a crtc link (meaning that the
9882 * encoder is active and trying to read from a pipe) and the
9883 * pipe itself being active. */
9884 bool has_active_crtc = encoder->base.crtc &&
9885 to_intel_crtc(encoder->base.crtc)->active;
9886
9887 if (encoder->connectors_active && !has_active_crtc) {
9888 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
9889 encoder->base.base.id,
9890 drm_get_encoder_name(&encoder->base));
9891
9892 /* Connector is active, but has no active pipe. This is
9893 * fallout from our resume register restoring. Disable
9894 * the encoder manually again. */
9895 if (encoder->base.crtc) {
9896 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
9897 encoder->base.base.id,
9898 drm_get_encoder_name(&encoder->base));
9899 encoder->disable(encoder);
9900 }
9901
9902 /* Inconsistent output/port/pipe state happens presumably due to
9903 * a bug in one of the get_hw_state functions. Or someplace else
9904 * in our code, like the register restore mess on resume. Clamp
9905 * things to off as a safer default. */
9906 list_for_each_entry(connector,
9907 &dev->mode_config.connector_list,
9908 base.head) {
9909 if (connector->encoder != encoder)
9910 continue;
9911
9912 intel_connector_break_all_links(connector);
9913 }
9914 }
9915 /* Enabled encoders without active connectors will be fixed in
9916 * the crtc fixup. */
9917}
9918
Daniel Vetter44cec742013-01-25 17:53:21 +01009919void i915_redisable_vga(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01009920{
9921 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +02009922 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01009923
9924 if (I915_READ(vga_reg) != VGA_DISP_DISABLE) {
9925 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
Ville Syrjälä209d5212013-01-25 21:44:48 +02009926 i915_disable_vga(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01009927 }
9928}
9929
Daniel Vetter30e984d2013-06-05 13:34:17 +02009930static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +02009931{
9932 struct drm_i915_private *dev_priv = dev->dev_private;
9933 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +02009934 struct intel_crtc *crtc;
9935 struct intel_encoder *encoder;
9936 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +02009937 int i;
Daniel Vetter24929352012-07-02 20:28:59 +02009938
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009939 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9940 base.head) {
Daniel Vetter88adfff2013-03-28 10:42:01 +01009941 memset(&crtc->config, 0, sizeof(crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +02009942
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009943 crtc->active = dev_priv->display.get_pipe_config(crtc,
9944 &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +02009945
9946 crtc->base.enabled = crtc->active;
9947
9948 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
9949 crtc->base.base.id,
9950 crtc->active ? "enabled" : "disabled");
9951 }
9952
Daniel Vetter53589012013-06-05 13:34:16 +02009953 /* FIXME: Smash this into the new shared dpll infrastructure. */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009954 if (HAS_DDI(dev))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009955 intel_ddi_setup_hw_pll_state(dev);
9956
Daniel Vetter53589012013-06-05 13:34:16 +02009957 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
9958 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
9959
9960 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
9961 pll->active = 0;
9962 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9963 base.head) {
9964 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
9965 pll->active++;
9966 }
9967 pll->refcount = pll->active;
9968
9969 DRM_DEBUG_KMS("%s hw state readout: refcount %i\n",
9970 pll->name, pll->refcount);
9971 }
9972
Daniel Vetter24929352012-07-02 20:28:59 +02009973 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9974 base.head) {
9975 pipe = 0;
9976
9977 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -07009978 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
9979 encoder->base.crtc = &crtc->base;
Jesse Barnes510d5f22013-07-01 15:50:17 -07009980 if (encoder->get_config)
Jesse Barnes045ac3b2013-05-14 17:08:26 -07009981 encoder->get_config(encoder, &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +02009982 } else {
9983 encoder->base.crtc = NULL;
9984 }
9985
9986 encoder->connectors_active = false;
9987 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe=%i\n",
9988 encoder->base.base.id,
9989 drm_get_encoder_name(&encoder->base),
9990 encoder->base.crtc ? "enabled" : "disabled",
9991 pipe);
9992 }
9993
Jesse Barnes510d5f22013-07-01 15:50:17 -07009994 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9995 base.head) {
9996 if (!crtc->active)
9997 continue;
9998 if (dev_priv->display.get_clock)
9999 dev_priv->display.get_clock(crtc,
10000 &crtc->config);
10001 }
10002
Daniel Vetter24929352012-07-02 20:28:59 +020010003 list_for_each_entry(connector, &dev->mode_config.connector_list,
10004 base.head) {
10005 if (connector->get_hw_state(connector)) {
10006 connector->base.dpms = DRM_MODE_DPMS_ON;
10007 connector->encoder->connectors_active = true;
10008 connector->base.encoder = &connector->encoder->base;
10009 } else {
10010 connector->base.dpms = DRM_MODE_DPMS_OFF;
10011 connector->base.encoder = NULL;
10012 }
10013 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
10014 connector->base.base.id,
10015 drm_get_connector_name(&connector->base),
10016 connector->base.encoder ? "enabled" : "disabled");
10017 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020010018}
10019
10020/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
10021 * and i915 state tracking structures. */
10022void intel_modeset_setup_hw_state(struct drm_device *dev,
10023 bool force_restore)
10024{
10025 struct drm_i915_private *dev_priv = dev->dev_private;
10026 enum pipe pipe;
10027 struct drm_plane *plane;
10028 struct intel_crtc *crtc;
10029 struct intel_encoder *encoder;
10030
10031 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020010032
Jesse Barnesbabea612013-06-26 18:57:38 +030010033 /*
10034 * Now that we have the config, copy it to each CRTC struct
10035 * Note that this could go away if we move to using crtc_config
10036 * checking everywhere.
10037 */
10038 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
10039 base.head) {
10040 if (crtc->active && i915_fastboot) {
10041 intel_crtc_mode_from_pipe_config(crtc, &crtc->config);
10042
10043 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
10044 crtc->base.base.id);
10045 drm_mode_debug_printmodeline(&crtc->base.mode);
10046 }
10047 }
10048
Daniel Vetter24929352012-07-02 20:28:59 +020010049 /* HW state is read out, now we need to sanitize this mess. */
10050 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10051 base.head) {
10052 intel_sanitize_encoder(encoder);
10053 }
10054
10055 for_each_pipe(pipe) {
10056 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
10057 intel_sanitize_crtc(crtc);
Daniel Vetterc0b03412013-05-28 12:05:54 +020010058 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020010059 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010060
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010061 if (force_restore) {
Daniel Vetterf30da182013-04-11 20:22:50 +020010062 /*
10063 * We need to use raw interfaces for restoring state to avoid
10064 * checking (bogus) intermediate states.
10065 */
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010066 for_each_pipe(pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070010067 struct drm_crtc *crtc =
10068 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020010069
10070 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
10071 crtc->fb);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010072 }
Jesse Barnesb5644d02013-03-26 13:25:27 -070010073 list_for_each_entry(plane, &dev->mode_config.plane_list, head)
10074 intel_plane_restore(plane);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010010075
10076 i915_redisable_vga(dev);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010077 } else {
10078 intel_modeset_update_staged_output_state(dev);
10079 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010080
10081 intel_modeset_check_state(dev);
Daniel Vetter2e938892012-10-11 20:08:24 +020010082
10083 drm_mode_config_reset(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010010084}
10085
10086void intel_modeset_gem_init(struct drm_device *dev)
10087{
Chris Wilson1833b132012-05-09 11:56:28 +010010088 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020010089
10090 intel_setup_overlay(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020010091
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010092 intel_modeset_setup_hw_state(dev, false);
Jesse Barnes79e53942008-11-07 14:24:08 -080010093}
10094
10095void intel_modeset_cleanup(struct drm_device *dev)
10096{
Jesse Barnes652c3932009-08-17 13:31:43 -070010097 struct drm_i915_private *dev_priv = dev->dev_private;
10098 struct drm_crtc *crtc;
10099 struct intel_crtc *intel_crtc;
10100
Daniel Vetterfd0c0642013-04-24 11:13:35 +020010101 /*
10102 * Interrupts and polling as the first thing to avoid creating havoc.
10103 * Too much stuff here (turning of rps, connectors, ...) would
10104 * experience fancy races otherwise.
10105 */
10106 drm_irq_uninstall(dev);
10107 cancel_work_sync(&dev_priv->hotplug_work);
10108 /*
10109 * Due to the hpd irq storm handling the hotplug work can re-arm the
10110 * poll handlers. Hence disable polling after hpd handling is shut down.
10111 */
Keith Packardf87ea762010-10-03 19:36:26 -070010112 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020010113
Jesse Barnes652c3932009-08-17 13:31:43 -070010114 mutex_lock(&dev->struct_mutex);
10115
Jesse Barnes723bfd72010-10-07 16:01:13 -070010116 intel_unregister_dsm_handler();
10117
Jesse Barnes652c3932009-08-17 13:31:43 -070010118 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
10119 /* Skip inactive CRTCs */
10120 if (!crtc->fb)
10121 continue;
10122
10123 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3dec0092010-08-20 21:40:52 +020010124 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070010125 }
10126
Chris Wilson973d04f2011-07-08 12:22:37 +010010127 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070010128
Daniel Vetter8090c6b2012-06-24 16:42:32 +020010129 intel_disable_gt_powersave(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +000010130
Daniel Vetter930ebb42012-06-29 23:32:16 +020010131 ironlake_teardown_rc6(dev);
10132
Kristian Høgsberg69341a52009-11-11 12:19:17 -050010133 mutex_unlock(&dev->struct_mutex);
10134
Chris Wilson1630fe72011-07-08 12:22:42 +010010135 /* flush any delayed tasks or pending work */
10136 flush_scheduled_work();
10137
Jani Nikuladc652f92013-04-12 15:18:38 +030010138 /* destroy backlight, if any, before the connectors */
10139 intel_panel_destroy_backlight(dev);
10140
Jesse Barnes79e53942008-11-07 14:24:08 -080010141 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010010142
10143 intel_cleanup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010144}
10145
Dave Airlie28d52042009-09-21 14:33:58 +100010146/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080010147 * Return which encoder is currently attached for connector.
10148 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010010149struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080010150{
Chris Wilsondf0e9242010-09-09 16:20:55 +010010151 return &intel_attached_encoder(connector)->base;
10152}
Jesse Barnes79e53942008-11-07 14:24:08 -080010153
Chris Wilsondf0e9242010-09-09 16:20:55 +010010154void intel_connector_attach_encoder(struct intel_connector *connector,
10155 struct intel_encoder *encoder)
10156{
10157 connector->encoder = encoder;
10158 drm_mode_connector_attach_encoder(&connector->base,
10159 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080010160}
Dave Airlie28d52042009-09-21 14:33:58 +100010161
10162/*
10163 * set vga decode state - true == enable VGA decode
10164 */
10165int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
10166{
10167 struct drm_i915_private *dev_priv = dev->dev_private;
10168 u16 gmch_ctrl;
10169
10170 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
10171 if (state)
10172 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
10173 else
10174 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
10175 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
10176 return 0;
10177}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010178
10179#ifdef CONFIG_DEBUG_FS
10180#include <linux/seq_file.h>
10181
10182struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010183
10184 u32 power_well_driver;
10185
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010186 struct intel_cursor_error_state {
10187 u32 control;
10188 u32 position;
10189 u32 base;
10190 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010010191 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010192
10193 struct intel_pipe_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010194 enum transcoder cpu_transcoder;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010195 u32 conf;
10196 u32 source;
10197
10198 u32 htotal;
10199 u32 hblank;
10200 u32 hsync;
10201 u32 vtotal;
10202 u32 vblank;
10203 u32 vsync;
Damien Lespiau52331302012-08-15 19:23:25 +010010204 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010205
10206 struct intel_plane_error_state {
10207 u32 control;
10208 u32 stride;
10209 u32 size;
10210 u32 pos;
10211 u32 addr;
10212 u32 surface;
10213 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010010214 } plane[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010215};
10216
10217struct intel_display_error_state *
10218intel_display_capture_error_state(struct drm_device *dev)
10219{
Akshay Joshi0206e352011-08-16 15:34:10 -040010220 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010221 struct intel_display_error_state *error;
Paulo Zanoni702e7a52012-10-23 18:29:59 -020010222 enum transcoder cpu_transcoder;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010223 int i;
10224
10225 error = kmalloc(sizeof(*error), GFP_ATOMIC);
10226 if (error == NULL)
10227 return NULL;
10228
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010229 if (HAS_POWER_WELL(dev))
10230 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
10231
Damien Lespiau52331302012-08-15 19:23:25 +010010232 for_each_pipe(i) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -020010233 cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv, i);
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010234 error->pipe[i].cpu_transcoder = cpu_transcoder;
Paulo Zanoni702e7a52012-10-23 18:29:59 -020010235
Paulo Zanonia18c4c32013-03-06 20:03:12 -030010236 if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev)) {
10237 error->cursor[i].control = I915_READ(CURCNTR(i));
10238 error->cursor[i].position = I915_READ(CURPOS(i));
10239 error->cursor[i].base = I915_READ(CURBASE(i));
10240 } else {
10241 error->cursor[i].control = I915_READ(CURCNTR_IVB(i));
10242 error->cursor[i].position = I915_READ(CURPOS_IVB(i));
10243 error->cursor[i].base = I915_READ(CURBASE_IVB(i));
10244 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010245
10246 error->plane[i].control = I915_READ(DSPCNTR(i));
10247 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030010248 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030010249 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030010250 error->plane[i].pos = I915_READ(DSPPOS(i));
10251 }
Paulo Zanonica291362013-03-06 20:03:14 -030010252 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
10253 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010254 if (INTEL_INFO(dev)->gen >= 4) {
10255 error->plane[i].surface = I915_READ(DSPSURF(i));
10256 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
10257 }
10258
Paulo Zanoni702e7a52012-10-23 18:29:59 -020010259 error->pipe[i].conf = I915_READ(PIPECONF(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010260 error->pipe[i].source = I915_READ(PIPESRC(i));
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010261 error->pipe[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
10262 error->pipe[i].hblank = I915_READ(HBLANK(cpu_transcoder));
10263 error->pipe[i].hsync = I915_READ(HSYNC(cpu_transcoder));
10264 error->pipe[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
10265 error->pipe[i].vblank = I915_READ(VBLANK(cpu_transcoder));
10266 error->pipe[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010267 }
10268
Paulo Zanoni12d217c2013-05-03 12:15:38 -030010269 /* In the code above we read the registers without checking if the power
10270 * well was on, so here we have to clear the FPGA_DBG_RM_NOCLAIM bit to
10271 * prevent the next I915_WRITE from detecting it and printing an error
10272 * message. */
10273 if (HAS_POWER_WELL(dev))
10274 I915_WRITE_NOTRACE(FPGA_DBG, FPGA_DBG_RM_NOCLAIM);
10275
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010276 return error;
10277}
10278
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010279#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
10280
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010281void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010282intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010283 struct drm_device *dev,
10284 struct intel_display_error_state *error)
10285{
10286 int i;
10287
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010288 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010289 if (HAS_POWER_WELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010290 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010291 error->power_well_driver);
Damien Lespiau52331302012-08-15 19:23:25 +010010292 for_each_pipe(i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010293 err_printf(m, "Pipe [%d]:\n", i);
10294 err_printf(m, " CPU transcoder: %c\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010295 transcoder_name(error->pipe[i].cpu_transcoder));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010296 err_printf(m, " CONF: %08x\n", error->pipe[i].conf);
10297 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
10298 err_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
10299 err_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
10300 err_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
10301 err_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
10302 err_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
10303 err_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010304
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010305 err_printf(m, "Plane [%d]:\n", i);
10306 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
10307 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030010308 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010309 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
10310 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030010311 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030010312 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010313 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010314 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010315 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
10316 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010317 }
10318
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010319 err_printf(m, "Cursor [%d]:\n", i);
10320 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
10321 err_printf(m, " POS: %08x\n", error->cursor[i].position);
10322 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010323 }
10324}
10325#endif