blob: 3d1c97abee28c19560d54f949fabbe1a5abaab69 [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
Daniel Vetter618563e2012-04-01 13:38:50 +020027#include <linux/dmi.h>
Jesse Barnesc1c7af62009-09-10 15:28:03 -070028#include <linux/module.h>
29#include <linux/input.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080030#include <linux/i2c.h>
Shaohua Li7662c8b2009-06-26 11:23:55 +080031#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Jesse Barnes9cce37f2010-08-13 15:11:26 -070033#include <linux/vgaarb.h>
Wu Fengguange0dac652011-09-05 14:25:34 +080034#include <drm/drm_edid.h>
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drmP.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include "intel_drv.h"
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/i915_drm.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080038#include "i915_drv.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070039#include "i915_trace.h"
David Howells760285e2012-10-02 18:01:07 +010040#include <drm/drm_dp_helper.h>
41#include <drm/drm_crtc_helper.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080042#include <linux/dma_remapping.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080043
Akshay Joshi0206e352011-08-16 15:34:10 -040044bool intel_pipe_has_type(struct drm_crtc *crtc, int type);
Daniel Vetter3dec0092010-08-20 21:40:52 +020045static void intel_increase_pllclock(struct drm_crtc *crtc);
Chris Wilson6b383a72010-09-13 13:54:26 +010046static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080047
Jesse Barnesf1f644d2013-06-27 00:39:25 +030048static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
49 struct intel_crtc_config *pipe_config);
50static void ironlake_crtc_clock_get(struct intel_crtc *crtc,
51 struct intel_crtc_config *pipe_config);
52
Jesse Barnes79e53942008-11-07 14:24:08 -080053typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040054 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -080055} intel_range_t;
56
57typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040058 int dot_limit;
59 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -080060} intel_p2_t;
61
62#define INTEL_P2_NUM 2
Ma Lingd4906092009-03-18 20:13:27 +080063typedef struct intel_limit intel_limit_t;
64struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -040065 intel_range_t dot, vco, n, m, m1, m2, p, p1;
66 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +080067};
Jesse Barnes79e53942008-11-07 14:24:08 -080068
Jesse Barnes2377b742010-07-07 14:06:43 -070069/* FDI */
70#define IRONLAKE_FDI_FREQ 2700000 /* in kHz for mode->clock */
71
Daniel Vetterd2acd212012-10-20 20:57:43 +020072int
73intel_pch_rawclk(struct drm_device *dev)
74{
75 struct drm_i915_private *dev_priv = dev->dev_private;
76
77 WARN_ON(!HAS_PCH_SPLIT(dev));
78
79 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
80}
81
Chris Wilson021357a2010-09-07 20:54:59 +010082static inline u32 /* units of 100MHz */
83intel_fdi_link_freq(struct drm_device *dev)
84{
Chris Wilson8b99e682010-10-13 09:59:17 +010085 if (IS_GEN5(dev)) {
86 struct drm_i915_private *dev_priv = dev->dev_private;
87 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
88 } else
89 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +010090}
91
Daniel Vetter5d536e22013-07-06 12:52:06 +020092static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -040093 .dot = { .min = 25000, .max = 350000 },
94 .vco = { .min = 930000, .max = 1400000 },
95 .n = { .min = 3, .max = 16 },
96 .m = { .min = 96, .max = 140 },
97 .m1 = { .min = 18, .max = 26 },
98 .m2 = { .min = 6, .max = 16 },
99 .p = { .min = 4, .max = 128 },
100 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700101 .p2 = { .dot_limit = 165000,
102 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700103};
104
Daniel Vetter5d536e22013-07-06 12:52:06 +0200105static const intel_limit_t intel_limits_i8xx_dvo = {
106 .dot = { .min = 25000, .max = 350000 },
107 .vco = { .min = 930000, .max = 1400000 },
108 .n = { .min = 3, .max = 16 },
109 .m = { .min = 96, .max = 140 },
110 .m1 = { .min = 18, .max = 26 },
111 .m2 = { .min = 6, .max = 16 },
112 .p = { .min = 4, .max = 128 },
113 .p1 = { .min = 2, .max = 33 },
114 .p2 = { .dot_limit = 165000,
115 .p2_slow = 4, .p2_fast = 4 },
116};
117
Keith Packarde4b36692009-06-05 19:22:17 -0700118static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400119 .dot = { .min = 25000, .max = 350000 },
120 .vco = { .min = 930000, .max = 1400000 },
121 .n = { .min = 3, .max = 16 },
122 .m = { .min = 96, .max = 140 },
123 .m1 = { .min = 18, .max = 26 },
124 .m2 = { .min = 6, .max = 16 },
125 .p = { .min = 4, .max = 128 },
126 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700127 .p2 = { .dot_limit = 165000,
128 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700129};
Eric Anholt273e27c2011-03-30 13:01:10 -0700130
Keith Packarde4b36692009-06-05 19:22:17 -0700131static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400132 .dot = { .min = 20000, .max = 400000 },
133 .vco = { .min = 1400000, .max = 2800000 },
134 .n = { .min = 1, .max = 6 },
135 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100136 .m1 = { .min = 8, .max = 18 },
137 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400138 .p = { .min = 5, .max = 80 },
139 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700140 .p2 = { .dot_limit = 200000,
141 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700142};
143
144static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400145 .dot = { .min = 20000, .max = 400000 },
146 .vco = { .min = 1400000, .max = 2800000 },
147 .n = { .min = 1, .max = 6 },
148 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100149 .m1 = { .min = 8, .max = 18 },
150 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400151 .p = { .min = 7, .max = 98 },
152 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700153 .p2 = { .dot_limit = 112000,
154 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700155};
156
Eric Anholt273e27c2011-03-30 13:01:10 -0700157
Keith Packarde4b36692009-06-05 19:22:17 -0700158static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700159 .dot = { .min = 25000, .max = 270000 },
160 .vco = { .min = 1750000, .max = 3500000},
161 .n = { .min = 1, .max = 4 },
162 .m = { .min = 104, .max = 138 },
163 .m1 = { .min = 17, .max = 23 },
164 .m2 = { .min = 5, .max = 11 },
165 .p = { .min = 10, .max = 30 },
166 .p1 = { .min = 1, .max = 3},
167 .p2 = { .dot_limit = 270000,
168 .p2_slow = 10,
169 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800170 },
Keith Packarde4b36692009-06-05 19:22:17 -0700171};
172
173static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700174 .dot = { .min = 22000, .max = 400000 },
175 .vco = { .min = 1750000, .max = 3500000},
176 .n = { .min = 1, .max = 4 },
177 .m = { .min = 104, .max = 138 },
178 .m1 = { .min = 16, .max = 23 },
179 .m2 = { .min = 5, .max = 11 },
180 .p = { .min = 5, .max = 80 },
181 .p1 = { .min = 1, .max = 8},
182 .p2 = { .dot_limit = 165000,
183 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700184};
185
186static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700187 .dot = { .min = 20000, .max = 115000 },
188 .vco = { .min = 1750000, .max = 3500000 },
189 .n = { .min = 1, .max = 3 },
190 .m = { .min = 104, .max = 138 },
191 .m1 = { .min = 17, .max = 23 },
192 .m2 = { .min = 5, .max = 11 },
193 .p = { .min = 28, .max = 112 },
194 .p1 = { .min = 2, .max = 8 },
195 .p2 = { .dot_limit = 0,
196 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800197 },
Keith Packarde4b36692009-06-05 19:22:17 -0700198};
199
200static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700201 .dot = { .min = 80000, .max = 224000 },
202 .vco = { .min = 1750000, .max = 3500000 },
203 .n = { .min = 1, .max = 3 },
204 .m = { .min = 104, .max = 138 },
205 .m1 = { .min = 17, .max = 23 },
206 .m2 = { .min = 5, .max = 11 },
207 .p = { .min = 14, .max = 42 },
208 .p1 = { .min = 2, .max = 6 },
209 .p2 = { .dot_limit = 0,
210 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800211 },
Keith Packarde4b36692009-06-05 19:22:17 -0700212};
213
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500214static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400215 .dot = { .min = 20000, .max = 400000},
216 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700217 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400218 .n = { .min = 3, .max = 6 },
219 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700220 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400221 .m1 = { .min = 0, .max = 0 },
222 .m2 = { .min = 0, .max = 254 },
223 .p = { .min = 5, .max = 80 },
224 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700225 .p2 = { .dot_limit = 200000,
226 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700227};
228
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500229static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400230 .dot = { .min = 20000, .max = 400000 },
231 .vco = { .min = 1700000, .max = 3500000 },
232 .n = { .min = 3, .max = 6 },
233 .m = { .min = 2, .max = 256 },
234 .m1 = { .min = 0, .max = 0 },
235 .m2 = { .min = 0, .max = 254 },
236 .p = { .min = 7, .max = 112 },
237 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700238 .p2 = { .dot_limit = 112000,
239 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700240};
241
Eric Anholt273e27c2011-03-30 13:01:10 -0700242/* Ironlake / Sandybridge
243 *
244 * We calculate clock using (register_value + 2) for N/M1/M2, so here
245 * the range value for them is (actual_value - 2).
246 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800247static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700248 .dot = { .min = 25000, .max = 350000 },
249 .vco = { .min = 1760000, .max = 3510000 },
250 .n = { .min = 1, .max = 5 },
251 .m = { .min = 79, .max = 127 },
252 .m1 = { .min = 12, .max = 22 },
253 .m2 = { .min = 5, .max = 9 },
254 .p = { .min = 5, .max = 80 },
255 .p1 = { .min = 1, .max = 8 },
256 .p2 = { .dot_limit = 225000,
257 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700258};
259
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800260static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700261 .dot = { .min = 25000, .max = 350000 },
262 .vco = { .min = 1760000, .max = 3510000 },
263 .n = { .min = 1, .max = 3 },
264 .m = { .min = 79, .max = 118 },
265 .m1 = { .min = 12, .max = 22 },
266 .m2 = { .min = 5, .max = 9 },
267 .p = { .min = 28, .max = 112 },
268 .p1 = { .min = 2, .max = 8 },
269 .p2 = { .dot_limit = 225000,
270 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800271};
272
273static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700274 .dot = { .min = 25000, .max = 350000 },
275 .vco = { .min = 1760000, .max = 3510000 },
276 .n = { .min = 1, .max = 3 },
277 .m = { .min = 79, .max = 127 },
278 .m1 = { .min = 12, .max = 22 },
279 .m2 = { .min = 5, .max = 9 },
280 .p = { .min = 14, .max = 56 },
281 .p1 = { .min = 2, .max = 8 },
282 .p2 = { .dot_limit = 225000,
283 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800284};
285
Eric Anholt273e27c2011-03-30 13:01:10 -0700286/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800287static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700288 .dot = { .min = 25000, .max = 350000 },
289 .vco = { .min = 1760000, .max = 3510000 },
290 .n = { .min = 1, .max = 2 },
291 .m = { .min = 79, .max = 126 },
292 .m1 = { .min = 12, .max = 22 },
293 .m2 = { .min = 5, .max = 9 },
294 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400295 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700296 .p2 = { .dot_limit = 225000,
297 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800298};
299
300static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700301 .dot = { .min = 25000, .max = 350000 },
302 .vco = { .min = 1760000, .max = 3510000 },
303 .n = { .min = 1, .max = 3 },
304 .m = { .min = 79, .max = 126 },
305 .m1 = { .min = 12, .max = 22 },
306 .m2 = { .min = 5, .max = 9 },
307 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400308 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700309 .p2 = { .dot_limit = 225000,
310 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800311};
312
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700313static const intel_limit_t intel_limits_vlv_dac = {
314 .dot = { .min = 25000, .max = 270000 },
315 .vco = { .min = 4000000, .max = 6000000 },
316 .n = { .min = 1, .max = 7 },
317 .m = { .min = 22, .max = 450 }, /* guess */
318 .m1 = { .min = 2, .max = 3 },
319 .m2 = { .min = 11, .max = 156 },
320 .p = { .min = 10, .max = 30 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200321 .p1 = { .min = 1, .max = 3 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700322 .p2 = { .dot_limit = 270000,
323 .p2_slow = 2, .p2_fast = 20 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700324};
325
326static const intel_limit_t intel_limits_vlv_hdmi = {
Daniel Vetter75e53982013-04-18 21:10:43 +0200327 .dot = { .min = 25000, .max = 270000 },
328 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700329 .n = { .min = 1, .max = 7 },
330 .m = { .min = 60, .max = 300 }, /* guess */
331 .m1 = { .min = 2, .max = 3 },
332 .m2 = { .min = 11, .max = 156 },
333 .p = { .min = 10, .max = 30 },
334 .p1 = { .min = 2, .max = 3 },
335 .p2 = { .dot_limit = 270000,
336 .p2_slow = 2, .p2_fast = 20 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700337};
338
339static const intel_limit_t intel_limits_vlv_dp = {
Vijay Purushothaman74a4dd22012-09-27 19:13:04 +0530340 .dot = { .min = 25000, .max = 270000 },
341 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700342 .n = { .min = 1, .max = 7 },
Vijay Purushothaman74a4dd22012-09-27 19:13:04 +0530343 .m = { .min = 22, .max = 450 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700344 .m1 = { .min = 2, .max = 3 },
345 .m2 = { .min = 11, .max = 156 },
346 .p = { .min = 10, .max = 30 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200347 .p1 = { .min = 1, .max = 3 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700348 .p2 = { .dot_limit = 270000,
349 .p2_slow = 2, .p2_fast = 20 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700350};
351
Chris Wilson1b894b52010-12-14 20:04:54 +0000352static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
353 int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800354{
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800355 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800356 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800357
358 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100359 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000360 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800361 limit = &intel_limits_ironlake_dual_lvds_100m;
362 else
363 limit = &intel_limits_ironlake_dual_lvds;
364 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000365 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800366 limit = &intel_limits_ironlake_single_lvds_100m;
367 else
368 limit = &intel_limits_ironlake_single_lvds;
369 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200370 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800371 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800372
373 return limit;
374}
375
Ma Ling044c7c42009-03-18 20:13:23 +0800376static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
377{
378 struct drm_device *dev = crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800379 const intel_limit_t *limit;
380
381 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100382 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700383 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800384 else
Keith Packarde4b36692009-06-05 19:22:17 -0700385 limit = &intel_limits_g4x_single_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800386 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
387 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700388 limit = &intel_limits_g4x_hdmi;
Ma Ling044c7c42009-03-18 20:13:23 +0800389 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700390 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800391 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700392 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800393
394 return limit;
395}
396
Chris Wilson1b894b52010-12-14 20:04:54 +0000397static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800398{
399 struct drm_device *dev = crtc->dev;
400 const intel_limit_t *limit;
401
Eric Anholtbad720f2009-10-22 16:11:14 -0700402 if (HAS_PCH_SPLIT(dev))
Chris Wilson1b894b52010-12-14 20:04:54 +0000403 limit = intel_ironlake_limit(crtc, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800404 else if (IS_G4X(dev)) {
Ma Ling044c7c42009-03-18 20:13:23 +0800405 limit = intel_g4x_limit(crtc);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500406 } else if (IS_PINEVIEW(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +0800407 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500408 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800409 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500410 limit = &intel_limits_pineview_sdvo;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700411 } else if (IS_VALLEYVIEW(dev)) {
412 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG))
413 limit = &intel_limits_vlv_dac;
414 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
415 limit = &intel_limits_vlv_hdmi;
416 else
417 limit = &intel_limits_vlv_dp;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100418 } else if (!IS_GEN2(dev)) {
419 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
420 limit = &intel_limits_i9xx_lvds;
421 else
422 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800423 } else {
424 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700425 limit = &intel_limits_i8xx_lvds;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200426 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700427 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200428 else
429 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800430 }
431 return limit;
432}
433
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500434/* m1 is reserved as 0 in Pineview, n is a ring counter */
435static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800436{
Shaohua Li21778322009-02-23 15:19:16 +0800437 clock->m = clock->m2 + 2;
438 clock->p = clock->p1 * clock->p2;
439 clock->vco = refclk * clock->m / clock->n;
440 clock->dot = clock->vco / clock->p;
441}
442
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200443static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
444{
445 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
446}
447
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200448static void i9xx_clock(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800449{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200450 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800451 clock->p = clock->p1 * clock->p2;
452 clock->vco = refclk * clock->m / (clock->n + 2);
453 clock->dot = clock->vco / clock->p;
454}
455
Jesse Barnes79e53942008-11-07 14:24:08 -0800456/**
457 * Returns whether any output on the specified pipe is of the specified type
458 */
Chris Wilson4ef69c72010-09-09 15:14:28 +0100459bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
Jesse Barnes79e53942008-11-07 14:24:08 -0800460{
Chris Wilson4ef69c72010-09-09 15:14:28 +0100461 struct drm_device *dev = crtc->dev;
Chris Wilson4ef69c72010-09-09 15:14:28 +0100462 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -0800463
Daniel Vetter6c2b7c122012-07-05 09:50:24 +0200464 for_each_encoder_on_crtc(dev, crtc, encoder)
465 if (encoder->type == type)
Chris Wilson4ef69c72010-09-09 15:14:28 +0100466 return true;
467
468 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -0800469}
470
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800471#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800472/**
473 * Returns whether the given set of divisors are valid for a given refclk with
474 * the given connectors.
475 */
476
Chris Wilson1b894b52010-12-14 20:04:54 +0000477static bool intel_PLL_is_valid(struct drm_device *dev,
478 const intel_limit_t *limit,
479 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800480{
Jesse Barnes79e53942008-11-07 14:24:08 -0800481 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400482 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800483 if (clock->p < limit->p.min || limit->p.max < clock->p)
Akshay Joshi0206e352011-08-16 15:34:10 -0400484 INTELPllInvalid("p out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800485 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400486 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800487 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400488 INTELPllInvalid("m1 out of range\n");
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500489 if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
Akshay Joshi0206e352011-08-16 15:34:10 -0400490 INTELPllInvalid("m1 <= m2\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800491 if (clock->m < limit->m.min || limit->m.max < clock->m)
Akshay Joshi0206e352011-08-16 15:34:10 -0400492 INTELPllInvalid("m out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800493 if (clock->n < limit->n.min || limit->n.max < clock->n)
Akshay Joshi0206e352011-08-16 15:34:10 -0400494 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800495 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400496 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800497 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
498 * connector, etc., rather than just a single range.
499 */
500 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400501 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800502
503 return true;
504}
505
Ma Lingd4906092009-03-18 20:13:27 +0800506static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200507i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800508 int target, int refclk, intel_clock_t *match_clock,
509 intel_clock_t *best_clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800510{
511 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800512 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800513 int err = target;
514
Daniel Vettera210b022012-11-26 17:22:08 +0100515 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800516 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100517 * For LVDS just rely on its current settings for dual-channel.
518 * We haven't figured out how to reliably set up different
519 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800520 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100521 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800522 clock.p2 = limit->p2.p2_fast;
523 else
524 clock.p2 = limit->p2.p2_slow;
525 } else {
526 if (target < limit->p2.dot_limit)
527 clock.p2 = limit->p2.p2_slow;
528 else
529 clock.p2 = limit->p2.p2_fast;
530 }
531
Akshay Joshi0206e352011-08-16 15:34:10 -0400532 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800533
Zhao Yakui42158662009-11-20 11:24:18 +0800534 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
535 clock.m1++) {
536 for (clock.m2 = limit->m2.min;
537 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200538 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800539 break;
540 for (clock.n = limit->n.min;
541 clock.n <= limit->n.max; clock.n++) {
542 for (clock.p1 = limit->p1.min;
543 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800544 int this_err;
545
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200546 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000547 if (!intel_PLL_is_valid(dev, limit,
548 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800549 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800550 if (match_clock &&
551 clock.p != match_clock->p)
552 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800553
554 this_err = abs(clock.dot - target);
555 if (this_err < err) {
556 *best_clock = clock;
557 err = this_err;
558 }
559 }
560 }
561 }
562 }
563
564 return (err != target);
565}
566
Ma Lingd4906092009-03-18 20:13:27 +0800567static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200568pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
569 int target, int refclk, intel_clock_t *match_clock,
570 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200571{
572 struct drm_device *dev = crtc->dev;
573 intel_clock_t clock;
574 int err = target;
575
576 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
577 /*
578 * For LVDS just rely on its current settings for dual-channel.
579 * We haven't figured out how to reliably set up different
580 * single/dual channel state, if we even can.
581 */
582 if (intel_is_dual_link_lvds(dev))
583 clock.p2 = limit->p2.p2_fast;
584 else
585 clock.p2 = limit->p2.p2_slow;
586 } else {
587 if (target < limit->p2.dot_limit)
588 clock.p2 = limit->p2.p2_slow;
589 else
590 clock.p2 = limit->p2.p2_fast;
591 }
592
593 memset(best_clock, 0, sizeof(*best_clock));
594
595 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
596 clock.m1++) {
597 for (clock.m2 = limit->m2.min;
598 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200599 for (clock.n = limit->n.min;
600 clock.n <= limit->n.max; clock.n++) {
601 for (clock.p1 = limit->p1.min;
602 clock.p1 <= limit->p1.max; clock.p1++) {
603 int this_err;
604
605 pineview_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800606 if (!intel_PLL_is_valid(dev, limit,
607 &clock))
608 continue;
609 if (match_clock &&
610 clock.p != match_clock->p)
611 continue;
612
613 this_err = abs(clock.dot - target);
614 if (this_err < err) {
615 *best_clock = clock;
616 err = this_err;
617 }
618 }
619 }
620 }
621 }
622
623 return (err != target);
624}
625
Ma Lingd4906092009-03-18 20:13:27 +0800626static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200627g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
628 int target, int refclk, intel_clock_t *match_clock,
629 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800630{
631 struct drm_device *dev = crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800632 intel_clock_t clock;
633 int max_n;
634 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400635 /* approximately equals target * 0.00585 */
636 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800637 found = false;
638
639 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100640 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800641 clock.p2 = limit->p2.p2_fast;
642 else
643 clock.p2 = limit->p2.p2_slow;
644 } else {
645 if (target < limit->p2.dot_limit)
646 clock.p2 = limit->p2.p2_slow;
647 else
648 clock.p2 = limit->p2.p2_fast;
649 }
650
651 memset(best_clock, 0, sizeof(*best_clock));
652 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200653 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800654 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200655 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800656 for (clock.m1 = limit->m1.max;
657 clock.m1 >= limit->m1.min; clock.m1--) {
658 for (clock.m2 = limit->m2.max;
659 clock.m2 >= limit->m2.min; clock.m2--) {
660 for (clock.p1 = limit->p1.max;
661 clock.p1 >= limit->p1.min; clock.p1--) {
662 int this_err;
663
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200664 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000665 if (!intel_PLL_is_valid(dev, limit,
666 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800667 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000668
669 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800670 if (this_err < err_most) {
671 *best_clock = clock;
672 err_most = this_err;
673 max_n = clock.n;
674 found = true;
675 }
676 }
677 }
678 }
679 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800680 return found;
681}
Ma Lingd4906092009-03-18 20:13:27 +0800682
Zhenyu Wang2c072452009-06-05 15:38:42 +0800683static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200684vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
685 int target, int refclk, intel_clock_t *match_clock,
686 intel_clock_t *best_clock)
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700687{
688 u32 p1, p2, m1, m2, vco, bestn, bestm1, bestm2, bestp1, bestp2;
689 u32 m, n, fastclk;
690 u32 updrate, minupdate, fracbits, p;
691 unsigned long bestppm, ppm, absppm;
692 int dotclk, flag;
693
Alan Coxaf447bd2012-07-25 13:49:18 +0100694 flag = 0;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700695 dotclk = target * 1000;
696 bestppm = 1000000;
697 ppm = absppm = 0;
698 fastclk = dotclk / (2*100);
699 updrate = 0;
700 minupdate = 19200;
701 fracbits = 1;
702 n = p = p1 = p2 = m = m1 = m2 = vco = bestn = 0;
703 bestm1 = bestm2 = bestp1 = bestp2 = 0;
704
705 /* based on hardware requirement, prefer smaller n to precision */
706 for (n = limit->n.min; n <= ((refclk) / minupdate); n++) {
707 updrate = refclk / n;
708 for (p1 = limit->p1.max; p1 > limit->p1.min; p1--) {
709 for (p2 = limit->p2.p2_fast+1; p2 > 0; p2--) {
710 if (p2 > 10)
711 p2 = p2 - 1;
712 p = p1 * p2;
713 /* based on hardware requirement, prefer bigger m1,m2 values */
714 for (m1 = limit->m1.min; m1 <= limit->m1.max; m1++) {
715 m2 = (((2*(fastclk * p * n / m1 )) +
716 refclk) / (2*refclk));
717 m = m1 * m2;
718 vco = updrate * m;
719 if (vco >= limit->vco.min && vco < limit->vco.max) {
720 ppm = 1000000 * ((vco / p) - fastclk) / fastclk;
721 absppm = (ppm > 0) ? ppm : (-ppm);
722 if (absppm < 100 && ((p1 * p2) > (bestp1 * bestp2))) {
723 bestppm = 0;
724 flag = 1;
725 }
726 if (absppm < bestppm - 10) {
727 bestppm = absppm;
728 flag = 1;
729 }
730 if (flag) {
731 bestn = n;
732 bestm1 = m1;
733 bestm2 = m2;
734 bestp1 = p1;
735 bestp2 = p2;
736 flag = 0;
737 }
738 }
739 }
740 }
741 }
742 }
743 best_clock->n = bestn;
744 best_clock->m1 = bestm1;
745 best_clock->m2 = bestm2;
746 best_clock->p1 = bestp1;
747 best_clock->p2 = bestp2;
748
749 return true;
750}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700751
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200752enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
753 enum pipe pipe)
754{
755 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
756 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
757
Daniel Vetter3b117c82013-04-17 20:15:07 +0200758 return intel_crtc->config.cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200759}
760
Paulo Zanonia928d532012-05-04 17:18:15 -0300761static void ironlake_wait_for_vblank(struct drm_device *dev, int pipe)
762{
763 struct drm_i915_private *dev_priv = dev->dev_private;
764 u32 frame, frame_reg = PIPEFRAME(pipe);
765
766 frame = I915_READ(frame_reg);
767
768 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
769 DRM_DEBUG_KMS("vblank wait timed out\n");
770}
771
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700772/**
773 * intel_wait_for_vblank - wait for vblank on a given pipe
774 * @dev: drm device
775 * @pipe: pipe to wait for
776 *
777 * Wait for vblank to occur on a given pipe. Needed for various bits of
778 * mode setting code.
779 */
780void intel_wait_for_vblank(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -0800781{
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700782 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800783 int pipestat_reg = PIPESTAT(pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700784
Paulo Zanonia928d532012-05-04 17:18:15 -0300785 if (INTEL_INFO(dev)->gen >= 5) {
786 ironlake_wait_for_vblank(dev, pipe);
787 return;
788 }
789
Chris Wilson300387c2010-09-05 20:25:43 +0100790 /* Clear existing vblank status. Note this will clear any other
791 * sticky status fields as well.
792 *
793 * This races with i915_driver_irq_handler() with the result
794 * that either function could miss a vblank event. Here it is not
795 * fatal, as we will either wait upon the next vblank interrupt or
796 * timeout. Generally speaking intel_wait_for_vblank() is only
797 * called during modeset at which time the GPU should be idle and
798 * should *not* be performing page flips and thus not waiting on
799 * vblanks...
800 * Currently, the result of us stealing a vblank from the irq
801 * handler is that a single frame will be skipped during swapbuffers.
802 */
803 I915_WRITE(pipestat_reg,
804 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
805
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700806 /* Wait for vblank interrupt bit to set */
Chris Wilson481b6af2010-08-23 17:43:35 +0100807 if (wait_for(I915_READ(pipestat_reg) &
808 PIPE_VBLANK_INTERRUPT_STATUS,
809 50))
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700810 DRM_DEBUG_KMS("vblank wait timed out\n");
811}
812
Keith Packardab7ad7f2010-10-03 00:33:06 -0700813/*
814 * intel_wait_for_pipe_off - wait for pipe to turn off
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700815 * @dev: drm device
816 * @pipe: pipe to wait for
817 *
818 * After disabling a pipe, we can't wait for vblank in the usual way,
819 * spinning on the vblank interrupt status bit, since we won't actually
820 * see an interrupt when the pipe is disabled.
821 *
Keith Packardab7ad7f2010-10-03 00:33:06 -0700822 * On Gen4 and above:
823 * wait for the pipe register state bit to turn off
824 *
825 * Otherwise:
826 * wait for the display line value to settle (it usually
827 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +0100828 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700829 */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100830void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700831{
832 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200833 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
834 pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700835
Keith Packardab7ad7f2010-10-03 00:33:06 -0700836 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200837 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700838
Keith Packardab7ad7f2010-10-03 00:33:06 -0700839 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100840 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
841 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200842 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700843 } else {
Paulo Zanoni837ba002012-05-04 17:18:14 -0300844 u32 last_line, line_mask;
Chris Wilson58e10eb2010-10-03 10:56:11 +0100845 int reg = PIPEDSL(pipe);
Keith Packardab7ad7f2010-10-03 00:33:06 -0700846 unsigned long timeout = jiffies + msecs_to_jiffies(100);
847
Paulo Zanoni837ba002012-05-04 17:18:14 -0300848 if (IS_GEN2(dev))
849 line_mask = DSL_LINEMASK_GEN2;
850 else
851 line_mask = DSL_LINEMASK_GEN3;
852
Keith Packardab7ad7f2010-10-03 00:33:06 -0700853 /* Wait for the display line to settle */
854 do {
Paulo Zanoni837ba002012-05-04 17:18:14 -0300855 last_line = I915_READ(reg) & line_mask;
Keith Packardab7ad7f2010-10-03 00:33:06 -0700856 mdelay(5);
Paulo Zanoni837ba002012-05-04 17:18:14 -0300857 } while (((I915_READ(reg) & line_mask) != last_line) &&
Keith Packardab7ad7f2010-10-03 00:33:06 -0700858 time_after(timeout, jiffies));
859 if (time_after(jiffies, timeout))
Daniel Vetter284637d2012-07-09 09:51:57 +0200860 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700861 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800862}
863
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000864/*
865 * ibx_digital_port_connected - is the specified port connected?
866 * @dev_priv: i915 private structure
867 * @port: the port to test
868 *
869 * Returns true if @port is connected, false otherwise.
870 */
871bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
872 struct intel_digital_port *port)
873{
874 u32 bit;
875
Damien Lespiauc36346e2012-12-13 16:09:03 +0000876 if (HAS_PCH_IBX(dev_priv->dev)) {
877 switch(port->port) {
878 case PORT_B:
879 bit = SDE_PORTB_HOTPLUG;
880 break;
881 case PORT_C:
882 bit = SDE_PORTC_HOTPLUG;
883 break;
884 case PORT_D:
885 bit = SDE_PORTD_HOTPLUG;
886 break;
887 default:
888 return true;
889 }
890 } else {
891 switch(port->port) {
892 case PORT_B:
893 bit = SDE_PORTB_HOTPLUG_CPT;
894 break;
895 case PORT_C:
896 bit = SDE_PORTC_HOTPLUG_CPT;
897 break;
898 case PORT_D:
899 bit = SDE_PORTD_HOTPLUG_CPT;
900 break;
901 default:
902 return true;
903 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000904 }
905
906 return I915_READ(SDEISR) & bit;
907}
908
Jesse Barnesb24e7172011-01-04 15:09:30 -0800909static const char *state_string(bool enabled)
910{
911 return enabled ? "on" : "off";
912}
913
914/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +0200915void assert_pll(struct drm_i915_private *dev_priv,
916 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -0800917{
918 int reg;
919 u32 val;
920 bool cur_state;
921
922 reg = DPLL(pipe);
923 val = I915_READ(reg);
924 cur_state = !!(val & DPLL_VCO_ENABLE);
925 WARN(cur_state != state,
926 "PLL state assertion failure (expected %s, current %s)\n",
927 state_string(state), state_string(cur_state));
928}
Jesse Barnesb24e7172011-01-04 15:09:30 -0800929
Daniel Vetter55607e82013-06-16 21:42:39 +0200930struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +0200931intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -0800932{
Daniel Vettere2b78262013-06-07 23:10:03 +0200933 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
934
Daniel Vettera43f6e02013-06-07 23:10:32 +0200935 if (crtc->config.shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +0200936 return NULL;
937
Daniel Vettera43f6e02013-06-07 23:10:32 +0200938 return &dev_priv->shared_dplls[crtc->config.shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +0200939}
940
Jesse Barnesb24e7172011-01-04 15:09:30 -0800941/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +0200942void assert_shared_dpll(struct drm_i915_private *dev_priv,
943 struct intel_shared_dpll *pll,
944 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -0800945{
Jesse Barnes040484a2011-01-03 12:14:26 -0800946 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +0200947 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -0800948
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -0300949 if (HAS_PCH_LPT(dev_priv->dev)) {
950 DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
951 return;
952 }
953
Chris Wilson92b27b02012-05-20 18:10:50 +0100954 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +0200955 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +0100956 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +0100957
Daniel Vetter53589012013-06-05 13:34:16 +0200958 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Chris Wilson92b27b02012-05-20 18:10:50 +0100959 WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +0200960 "%s assertion failure (expected %s, current %s)\n",
961 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -0800962}
Jesse Barnes040484a2011-01-03 12:14:26 -0800963
964static void assert_fdi_tx(struct drm_i915_private *dev_priv,
965 enum pipe pipe, bool state)
966{
967 int reg;
968 u32 val;
969 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -0200970 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
971 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -0800972
Paulo Zanoniaffa9352012-11-23 15:30:39 -0200973 if (HAS_DDI(dev_priv->dev)) {
974 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -0200975 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -0300976 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -0200977 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -0300978 } else {
979 reg = FDI_TX_CTL(pipe);
980 val = I915_READ(reg);
981 cur_state = !!(val & FDI_TX_ENABLE);
982 }
Jesse Barnes040484a2011-01-03 12:14:26 -0800983 WARN(cur_state != state,
984 "FDI TX state assertion failure (expected %s, current %s)\n",
985 state_string(state), state_string(cur_state));
986}
987#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
988#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
989
990static void assert_fdi_rx(struct drm_i915_private *dev_priv,
991 enum pipe pipe, bool state)
992{
993 int reg;
994 u32 val;
995 bool cur_state;
996
Paulo Zanonid63fa0d2012-11-20 13:27:35 -0200997 reg = FDI_RX_CTL(pipe);
998 val = I915_READ(reg);
999 cur_state = !!(val & FDI_RX_ENABLE);
Jesse Barnes040484a2011-01-03 12:14:26 -08001000 WARN(cur_state != state,
1001 "FDI RX state assertion failure (expected %s, current %s)\n",
1002 state_string(state), state_string(cur_state));
1003}
1004#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1005#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1006
1007static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1008 enum pipe pipe)
1009{
1010 int reg;
1011 u32 val;
1012
1013 /* ILK FDI PLL is always enabled */
1014 if (dev_priv->info->gen == 5)
1015 return;
1016
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001017 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001018 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001019 return;
1020
Jesse Barnes040484a2011-01-03 12:14:26 -08001021 reg = FDI_TX_CTL(pipe);
1022 val = I915_READ(reg);
1023 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1024}
1025
Daniel Vetter55607e82013-06-16 21:42:39 +02001026void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1027 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001028{
1029 int reg;
1030 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001031 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001032
1033 reg = FDI_RX_CTL(pipe);
1034 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001035 cur_state = !!(val & FDI_RX_PLL_ENABLE);
1036 WARN(cur_state != state,
1037 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1038 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001039}
1040
Jesse Barnesea0760c2011-01-04 15:09:32 -08001041static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1042 enum pipe pipe)
1043{
1044 int pp_reg, lvds_reg;
1045 u32 val;
1046 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001047 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001048
1049 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1050 pp_reg = PCH_PP_CONTROL;
1051 lvds_reg = PCH_LVDS;
1052 } else {
1053 pp_reg = PP_CONTROL;
1054 lvds_reg = LVDS;
1055 }
1056
1057 val = I915_READ(pp_reg);
1058 if (!(val & PANEL_POWER_ON) ||
1059 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1060 locked = false;
1061
1062 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1063 panel_pipe = PIPE_B;
1064
1065 WARN(panel_pipe == pipe && locked,
1066 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001067 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001068}
1069
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001070void assert_pipe(struct drm_i915_private *dev_priv,
1071 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001072{
1073 int reg;
1074 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001075 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001076 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1077 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001078
Daniel Vetter8e636782012-01-22 01:36:48 +01001079 /* if we need the pipe A quirk it must be always on */
1080 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1081 state = true;
1082
Paulo Zanonib97186f2013-05-03 12:15:36 -03001083 if (!intel_display_power_enabled(dev_priv->dev,
1084 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001085 cur_state = false;
1086 } else {
1087 reg = PIPECONF(cpu_transcoder);
1088 val = I915_READ(reg);
1089 cur_state = !!(val & PIPECONF_ENABLE);
1090 }
1091
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001092 WARN(cur_state != state,
1093 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001094 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001095}
1096
Chris Wilson931872f2012-01-16 23:01:13 +00001097static void assert_plane(struct drm_i915_private *dev_priv,
1098 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001099{
1100 int reg;
1101 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001102 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001103
1104 reg = DSPCNTR(plane);
1105 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001106 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1107 WARN(cur_state != state,
1108 "plane %c assertion failure (expected %s, current %s)\n",
1109 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001110}
1111
Chris Wilson931872f2012-01-16 23:01:13 +00001112#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1113#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1114
Jesse Barnesb24e7172011-01-04 15:09:30 -08001115static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1116 enum pipe pipe)
1117{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001118 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001119 int reg, i;
1120 u32 val;
1121 int cur_pipe;
1122
Ville Syrjälä653e1022013-06-04 13:49:05 +03001123 /* Primary planes are fixed to pipes on gen4+ */
1124 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001125 reg = DSPCNTR(pipe);
1126 val = I915_READ(reg);
1127 WARN((val & DISPLAY_PLANE_ENABLE),
1128 "plane %c assertion failure, should be disabled but not\n",
1129 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001130 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001131 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001132
Jesse Barnesb24e7172011-01-04 15:09:30 -08001133 /* Need to check both planes against the pipe */
Damien Lespiau08e2a7d2013-07-11 20:10:54 +01001134 for_each_pipe(i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001135 reg = DSPCNTR(i);
1136 val = I915_READ(reg);
1137 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1138 DISPPLANE_SEL_PIPE_SHIFT;
1139 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001140 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1141 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001142 }
1143}
1144
Jesse Barnes19332d72013-03-28 09:55:38 -07001145static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1146 enum pipe pipe)
1147{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001148 struct drm_device *dev = dev_priv->dev;
Jesse Barnes19332d72013-03-28 09:55:38 -07001149 int reg, i;
1150 u32 val;
1151
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001152 if (IS_VALLEYVIEW(dev)) {
1153 for (i = 0; i < dev_priv->num_plane; i++) {
1154 reg = SPCNTR(pipe, i);
1155 val = I915_READ(reg);
1156 WARN((val & SP_ENABLE),
1157 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1158 sprite_name(pipe, i), pipe_name(pipe));
1159 }
1160 } else if (INTEL_INFO(dev)->gen >= 7) {
1161 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001162 val = I915_READ(reg);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001163 WARN((val & SPRITE_ENABLE),
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001164 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001165 plane_name(pipe), pipe_name(pipe));
1166 } else if (INTEL_INFO(dev)->gen >= 5) {
1167 reg = DVSCNTR(pipe);
1168 val = I915_READ(reg);
1169 WARN((val & DVS_ENABLE),
1170 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1171 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001172 }
1173}
1174
Jesse Barnes92f25842011-01-04 15:09:34 -08001175static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1176{
1177 u32 val;
1178 bool enabled;
1179
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001180 if (HAS_PCH_LPT(dev_priv->dev)) {
1181 DRM_DEBUG_DRIVER("LPT does not has PCH refclk, skipping check\n");
1182 return;
1183 }
1184
Jesse Barnes92f25842011-01-04 15:09:34 -08001185 val = I915_READ(PCH_DREF_CONTROL);
1186 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1187 DREF_SUPERSPREAD_SOURCE_MASK));
1188 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1189}
1190
Daniel Vetterab9412b2013-05-03 11:49:46 +02001191static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1192 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001193{
1194 int reg;
1195 u32 val;
1196 bool enabled;
1197
Daniel Vetterab9412b2013-05-03 11:49:46 +02001198 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001199 val = I915_READ(reg);
1200 enabled = !!(val & TRANS_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001201 WARN(enabled,
1202 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1203 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001204}
1205
Keith Packard4e634382011-08-06 10:39:45 -07001206static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1207 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001208{
1209 if ((val & DP_PORT_EN) == 0)
1210 return false;
1211
1212 if (HAS_PCH_CPT(dev_priv->dev)) {
1213 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1214 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1215 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1216 return false;
1217 } else {
1218 if ((val & DP_PIPE_MASK) != (pipe << 30))
1219 return false;
1220 }
1221 return true;
1222}
1223
Keith Packard1519b992011-08-06 10:35:34 -07001224static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1225 enum pipe pipe, u32 val)
1226{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001227 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001228 return false;
1229
1230 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001231 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001232 return false;
1233 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001234 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001235 return false;
1236 }
1237 return true;
1238}
1239
1240static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1241 enum pipe pipe, u32 val)
1242{
1243 if ((val & LVDS_PORT_EN) == 0)
1244 return false;
1245
1246 if (HAS_PCH_CPT(dev_priv->dev)) {
1247 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1248 return false;
1249 } else {
1250 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1251 return false;
1252 }
1253 return true;
1254}
1255
1256static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1257 enum pipe pipe, u32 val)
1258{
1259 if ((val & ADPA_DAC_ENABLE) == 0)
1260 return false;
1261 if (HAS_PCH_CPT(dev_priv->dev)) {
1262 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1263 return false;
1264 } else {
1265 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1266 return false;
1267 }
1268 return true;
1269}
1270
Jesse Barnes291906f2011-02-02 12:28:03 -08001271static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001272 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001273{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001274 u32 val = I915_READ(reg);
Keith Packard4e634382011-08-06 10:39:45 -07001275 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001276 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001277 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001278
Daniel Vetter75c5da22012-09-10 21:58:29 +02001279 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1280 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001281 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001282}
1283
1284static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1285 enum pipe pipe, int reg)
1286{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001287 u32 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001288 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001289 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001290 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001291
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001292 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001293 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001294 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001295}
1296
1297static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1298 enum pipe pipe)
1299{
1300 int reg;
1301 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001302
Keith Packardf0575e92011-07-25 22:12:43 -07001303 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1304 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1305 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001306
1307 reg = PCH_ADPA;
1308 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001309 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001310 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001311 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001312
1313 reg = PCH_LVDS;
1314 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001315 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001316 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001317 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001318
Paulo Zanonie2debe92013-02-18 19:00:27 -03001319 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1320 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1321 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001322}
1323
Daniel Vetter426115c2013-07-11 22:13:42 +02001324static void vlv_enable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001325{
Daniel Vetter426115c2013-07-11 22:13:42 +02001326 struct drm_device *dev = crtc->base.dev;
1327 struct drm_i915_private *dev_priv = dev->dev_private;
1328 int reg = DPLL(crtc->pipe);
1329 u32 dpll = crtc->config.dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001330
Daniel Vetter426115c2013-07-11 22:13:42 +02001331 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001332
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001333 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001334 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1335
1336 /* PLL is protected by panel, make sure we can write it */
1337 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001338 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001339
Daniel Vetter426115c2013-07-11 22:13:42 +02001340 I915_WRITE(reg, dpll);
1341 POSTING_READ(reg);
1342 udelay(150);
1343
1344 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1345 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1346
1347 I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
1348 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001349
1350 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001351 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001352 POSTING_READ(reg);
1353 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001354 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001355 POSTING_READ(reg);
1356 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001357 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001358 POSTING_READ(reg);
1359 udelay(150); /* wait for warmup */
1360}
1361
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001362static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001363{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001364 struct drm_device *dev = crtc->base.dev;
1365 struct drm_i915_private *dev_priv = dev->dev_private;
1366 int reg = DPLL(crtc->pipe);
1367 u32 dpll = crtc->config.dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001368
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001369 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001370
1371 /* No really, not for ILK+ */
1372 BUG_ON(dev_priv->info->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001373
1374 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001375 if (IS_MOBILE(dev) && !IS_I830(dev))
1376 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001377
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001378 I915_WRITE(reg, dpll);
1379
1380 /* Wait for the clocks to stabilize. */
1381 POSTING_READ(reg);
1382 udelay(150);
1383
1384 if (INTEL_INFO(dev)->gen >= 4) {
1385 I915_WRITE(DPLL_MD(crtc->pipe),
1386 crtc->config.dpll_hw_state.dpll_md);
1387 } else {
1388 /* The pixel multiplier can only be updated once the
1389 * DPLL is enabled and the clocks are stable.
1390 *
1391 * So write it again.
1392 */
1393 I915_WRITE(reg, dpll);
1394 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001395
1396 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001397 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001398 POSTING_READ(reg);
1399 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001400 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001401 POSTING_READ(reg);
1402 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001403 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001404 POSTING_READ(reg);
1405 udelay(150); /* wait for warmup */
1406}
1407
1408/**
1409 * intel_disable_pll - disable a PLL
1410 * @dev_priv: i915 private structure
1411 * @pipe: pipe PLL to disable
1412 *
1413 * Disable the PLL for @pipe, making sure the pipe is off first.
1414 *
1415 * Note! This is for pre-ILK only.
1416 */
1417static void intel_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1418{
1419 int reg;
1420 u32 val;
1421
1422 /* Don't disable pipe A or pipe A PLLs if needed */
1423 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1424 return;
1425
1426 /* Make sure the pipe isn't still relying on us */
1427 assert_pipe_disabled(dev_priv, pipe);
1428
1429 reg = DPLL(pipe);
1430 val = I915_READ(reg);
1431 val &= ~DPLL_VCO_ENABLE;
1432 I915_WRITE(reg, val);
1433 POSTING_READ(reg);
1434}
1435
Jesse Barnes89b667f2013-04-18 14:51:36 -07001436void vlv_wait_port_ready(struct drm_i915_private *dev_priv, int port)
1437{
1438 u32 port_mask;
1439
1440 if (!port)
1441 port_mask = DPLL_PORTB_READY_MASK;
1442 else
1443 port_mask = DPLL_PORTC_READY_MASK;
1444
1445 if (wait_for((I915_READ(DPLL(0)) & port_mask) == 0, 1000))
1446 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
1447 'B' + port, I915_READ(DPLL(0)));
1448}
1449
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001450/**
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001451 * ironlake_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001452 * @dev_priv: i915 private structure
1453 * @pipe: pipe PLL to enable
1454 *
1455 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1456 * drives the transcoder clock.
1457 */
Daniel Vettere2b78262013-06-07 23:10:03 +02001458static void ironlake_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001459{
Daniel Vettere2b78262013-06-07 23:10:03 +02001460 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1461 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001462
Chris Wilson48da64a2012-05-13 20:16:12 +01001463 /* PCH PLLs only available on ILK, SNB and IVB */
Jesse Barnes92f25842011-01-04 15:09:34 -08001464 BUG_ON(dev_priv->info->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001465 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001466 return;
1467
1468 if (WARN_ON(pll->refcount == 0))
1469 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001470
Daniel Vetter46edb022013-06-05 13:34:12 +02001471 DRM_DEBUG_KMS("enable %s (active %d, on? %d)for crtc %d\n",
1472 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001473 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001474
Daniel Vettercdbd2312013-06-05 13:34:03 +02001475 if (pll->active++) {
1476 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001477 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001478 return;
1479 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001480 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001481
Daniel Vetter46edb022013-06-05 13:34:12 +02001482 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001483 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001484 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001485}
1486
Daniel Vettere2b78262013-06-07 23:10:03 +02001487static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001488{
Daniel Vettere2b78262013-06-07 23:10:03 +02001489 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1490 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001491
Jesse Barnes92f25842011-01-04 15:09:34 -08001492 /* PCH only available on ILK+ */
1493 BUG_ON(dev_priv->info->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001494 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001495 return;
1496
Chris Wilson48da64a2012-05-13 20:16:12 +01001497 if (WARN_ON(pll->refcount == 0))
1498 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001499
Daniel Vetter46edb022013-06-05 13:34:12 +02001500 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1501 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001502 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001503
Chris Wilson48da64a2012-05-13 20:16:12 +01001504 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001505 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001506 return;
1507 }
1508
Daniel Vettere9d69442013-06-05 13:34:15 +02001509 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001510 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001511 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001512 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001513
Daniel Vetter46edb022013-06-05 13:34:12 +02001514 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001515 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001516 pll->on = false;
Jesse Barnes92f25842011-01-04 15:09:34 -08001517}
1518
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001519static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1520 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001521{
Daniel Vetter23670b322012-11-01 09:15:30 +01001522 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001523 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001524 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001525 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001526
1527 /* PCH only available on ILK+ */
1528 BUG_ON(dev_priv->info->gen < 5);
1529
1530 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001531 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001532 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001533
1534 /* FDI must be feeding us bits for PCH ports */
1535 assert_fdi_tx_enabled(dev_priv, pipe);
1536 assert_fdi_rx_enabled(dev_priv, pipe);
1537
Daniel Vetter23670b322012-11-01 09:15:30 +01001538 if (HAS_PCH_CPT(dev)) {
1539 /* Workaround: Set the timing override bit before enabling the
1540 * pch transcoder. */
1541 reg = TRANS_CHICKEN2(pipe);
1542 val = I915_READ(reg);
1543 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1544 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001545 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001546
Daniel Vetterab9412b2013-05-03 11:49:46 +02001547 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001548 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001549 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001550
1551 if (HAS_PCH_IBX(dev_priv->dev)) {
1552 /*
1553 * make the BPC in transcoder be consistent with
1554 * that in pipeconf reg.
1555 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001556 val &= ~PIPECONF_BPC_MASK;
1557 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001558 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001559
1560 val &= ~TRANS_INTERLACE_MASK;
1561 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001562 if (HAS_PCH_IBX(dev_priv->dev) &&
1563 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1564 val |= TRANS_LEGACY_INTERLACED_ILK;
1565 else
1566 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001567 else
1568 val |= TRANS_PROGRESSIVE;
1569
Jesse Barnes040484a2011-01-03 12:14:26 -08001570 I915_WRITE(reg, val | TRANS_ENABLE);
1571 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001572 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001573}
1574
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001575static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001576 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001577{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001578 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001579
1580 /* PCH only available on ILK+ */
1581 BUG_ON(dev_priv->info->gen < 5);
1582
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001583 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001584 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001585 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001586
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001587 /* Workaround: set timing override bit. */
1588 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001589 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001590 I915_WRITE(_TRANSA_CHICKEN2, val);
1591
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001592 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001593 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001594
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001595 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1596 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001597 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001598 else
1599 val |= TRANS_PROGRESSIVE;
1600
Daniel Vetterab9412b2013-05-03 11:49:46 +02001601 I915_WRITE(LPT_TRANSCONF, val);
1602 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001603 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001604}
1605
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001606static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1607 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001608{
Daniel Vetter23670b322012-11-01 09:15:30 +01001609 struct drm_device *dev = dev_priv->dev;
1610 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001611
1612 /* FDI relies on the transcoder */
1613 assert_fdi_tx_disabled(dev_priv, pipe);
1614 assert_fdi_rx_disabled(dev_priv, pipe);
1615
Jesse Barnes291906f2011-02-02 12:28:03 -08001616 /* Ports must be off as well */
1617 assert_pch_ports_disabled(dev_priv, pipe);
1618
Daniel Vetterab9412b2013-05-03 11:49:46 +02001619 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001620 val = I915_READ(reg);
1621 val &= ~TRANS_ENABLE;
1622 I915_WRITE(reg, val);
1623 /* wait for PCH transcoder off, transcoder state */
1624 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001625 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001626
1627 if (!HAS_PCH_IBX(dev)) {
1628 /* Workaround: Clear the timing override chicken bit again. */
1629 reg = TRANS_CHICKEN2(pipe);
1630 val = I915_READ(reg);
1631 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1632 I915_WRITE(reg, val);
1633 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001634}
1635
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001636static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001637{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001638 u32 val;
1639
Daniel Vetterab9412b2013-05-03 11:49:46 +02001640 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001641 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001642 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001643 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001644 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001645 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001646
1647 /* Workaround: clear timing override bit. */
1648 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001649 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001650 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001651}
1652
1653/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001654 * intel_enable_pipe - enable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001655 * @dev_priv: i915 private structure
1656 * @pipe: pipe to enable
Jesse Barnes040484a2011-01-03 12:14:26 -08001657 * @pch_port: on ILK+, is this pipe driving a PCH port or not
Jesse Barnesb24e7172011-01-04 15:09:30 -08001658 *
1659 * Enable @pipe, making sure that various hardware specific requirements
1660 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1661 *
1662 * @pipe should be %PIPE_A or %PIPE_B.
1663 *
1664 * Will wait until the pipe is actually running (i.e. first vblank) before
1665 * returning.
1666 */
Jesse Barnes040484a2011-01-03 12:14:26 -08001667static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
1668 bool pch_port)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001669{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001670 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1671 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001672 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001673 int reg;
1674 u32 val;
1675
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001676 assert_planes_disabled(dev_priv, pipe);
1677 assert_sprites_disabled(dev_priv, pipe);
1678
Paulo Zanoni681e5812012-12-06 11:12:38 -02001679 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001680 pch_transcoder = TRANSCODER_A;
1681 else
1682 pch_transcoder = pipe;
1683
Jesse Barnesb24e7172011-01-04 15:09:30 -08001684 /*
1685 * A pipe without a PLL won't actually be able to drive bits from
1686 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1687 * need the check.
1688 */
1689 if (!HAS_PCH_SPLIT(dev_priv->dev))
1690 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001691 else {
1692 if (pch_port) {
1693 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001694 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001695 assert_fdi_tx_pll_enabled(dev_priv,
1696 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001697 }
1698 /* FIXME: assert CPU port conditions for SNB+ */
1699 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001700
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001701 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001702 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001703 if (val & PIPECONF_ENABLE)
1704 return;
1705
1706 I915_WRITE(reg, val | PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001707 intel_wait_for_vblank(dev_priv->dev, pipe);
1708}
1709
1710/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001711 * intel_disable_pipe - disable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001712 * @dev_priv: i915 private structure
1713 * @pipe: pipe to disable
1714 *
1715 * Disable @pipe, making sure that various hardware specific requirements
1716 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1717 *
1718 * @pipe should be %PIPE_A or %PIPE_B.
1719 *
1720 * Will wait until the pipe has shut down before returning.
1721 */
1722static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1723 enum pipe pipe)
1724{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001725 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1726 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001727 int reg;
1728 u32 val;
1729
1730 /*
1731 * Make sure planes won't keep trying to pump pixels to us,
1732 * or we might hang the display.
1733 */
1734 assert_planes_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001735 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001736
1737 /* Don't disable pipe A or pipe A PLLs if needed */
1738 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1739 return;
1740
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001741 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001742 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001743 if ((val & PIPECONF_ENABLE) == 0)
1744 return;
1745
1746 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001747 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1748}
1749
Keith Packardd74362c2011-07-28 14:47:14 -07001750/*
1751 * Plane regs are double buffered, going from enabled->disabled needs a
1752 * trigger in order to latch. The display address reg provides this.
1753 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03001754void intel_flush_display_plane(struct drm_i915_private *dev_priv,
Keith Packardd74362c2011-07-28 14:47:14 -07001755 enum plane plane)
1756{
Damien Lespiau14f86142012-10-29 15:24:49 +00001757 if (dev_priv->info->gen >= 4)
1758 I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
1759 else
1760 I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
Keith Packardd74362c2011-07-28 14:47:14 -07001761}
1762
Jesse Barnesb24e7172011-01-04 15:09:30 -08001763/**
1764 * intel_enable_plane - enable a display plane on a given pipe
1765 * @dev_priv: i915 private structure
1766 * @plane: plane to enable
1767 * @pipe: pipe being fed
1768 *
1769 * Enable @plane on @pipe, making sure that @pipe is running first.
1770 */
1771static void intel_enable_plane(struct drm_i915_private *dev_priv,
1772 enum plane plane, enum pipe pipe)
1773{
1774 int reg;
1775 u32 val;
1776
1777 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1778 assert_pipe_enabled(dev_priv, pipe);
1779
1780 reg = DSPCNTR(plane);
1781 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001782 if (val & DISPLAY_PLANE_ENABLE)
1783 return;
1784
1785 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
Keith Packardd74362c2011-07-28 14:47:14 -07001786 intel_flush_display_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001787 intel_wait_for_vblank(dev_priv->dev, pipe);
1788}
1789
Jesse Barnesb24e7172011-01-04 15:09:30 -08001790/**
1791 * intel_disable_plane - disable a display plane
1792 * @dev_priv: i915 private structure
1793 * @plane: plane to disable
1794 * @pipe: pipe consuming the data
1795 *
1796 * Disable @plane; should be an independent operation.
1797 */
1798static void intel_disable_plane(struct drm_i915_private *dev_priv,
1799 enum plane plane, enum pipe pipe)
1800{
1801 int reg;
1802 u32 val;
1803
1804 reg = DSPCNTR(plane);
1805 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001806 if ((val & DISPLAY_PLANE_ENABLE) == 0)
1807 return;
1808
1809 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001810 intel_flush_display_plane(dev_priv, plane);
1811 intel_wait_for_vblank(dev_priv->dev, pipe);
1812}
1813
Chris Wilson693db182013-03-05 14:52:39 +00001814static bool need_vtd_wa(struct drm_device *dev)
1815{
1816#ifdef CONFIG_INTEL_IOMMU
1817 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
1818 return true;
1819#endif
1820 return false;
1821}
1822
Chris Wilson127bd2a2010-07-23 23:32:05 +01001823int
Chris Wilson48b956c2010-09-14 12:50:34 +01001824intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00001825 struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00001826 struct intel_ring_buffer *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001827{
Chris Wilsonce453d82011-02-21 14:43:56 +00001828 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001829 u32 alignment;
1830 int ret;
1831
Chris Wilson05394f32010-11-08 19:18:58 +00001832 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001833 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01001834 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1835 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001836 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01001837 alignment = 4 * 1024;
1838 else
1839 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001840 break;
1841 case I915_TILING_X:
1842 /* pin() will align the object as required by fence */
1843 alignment = 0;
1844 break;
1845 case I915_TILING_Y:
Daniel Vetter8bb6e952013-04-06 23:54:56 +02001846 /* Despite that we check this in framebuffer_init userspace can
1847 * screw us over and change the tiling after the fact. Only
1848 * pinned buffers can't change their tiling. */
1849 DRM_DEBUG_DRIVER("Y tiled not allowed for scan out buffers\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001850 return -EINVAL;
1851 default:
1852 BUG();
1853 }
1854
Chris Wilson693db182013-03-05 14:52:39 +00001855 /* Note that the w/a also requires 64 PTE of padding following the
1856 * bo. We currently fill all unused PTE with the shadow page and so
1857 * we should always have valid PTE following the scanout preventing
1858 * the VT-d warning.
1859 */
1860 if (need_vtd_wa(dev) && alignment < 256 * 1024)
1861 alignment = 256 * 1024;
1862
Chris Wilsonce453d82011-02-21 14:43:56 +00001863 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01001864 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01001865 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00001866 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001867
1868 /* Install a fence for tiled scan-out. Pre-i965 always needs a
1869 * fence, whereas 965+ only requires a fence if using
1870 * framebuffer compression. For simplicity, we always install
1871 * a fence as the cost is not that onerous.
1872 */
Chris Wilson06d98132012-04-17 15:31:24 +01001873 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001874 if (ret)
1875 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01001876
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001877 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001878
Chris Wilsonce453d82011-02-21 14:43:56 +00001879 dev_priv->mm.interruptible = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001880 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01001881
1882err_unpin:
1883 i915_gem_object_unpin(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00001884err_interruptible:
1885 dev_priv->mm.interruptible = true;
Chris Wilson48b956c2010-09-14 12:50:34 +01001886 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001887}
1888
Chris Wilson1690e1e2011-12-14 13:57:08 +01001889void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
1890{
1891 i915_gem_object_unpin_fence(obj);
1892 i915_gem_object_unpin(obj);
1893}
1894
Daniel Vetterc2c75132012-07-05 12:17:30 +02001895/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
1896 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00001897unsigned long intel_gen4_compute_page_offset(int *x, int *y,
1898 unsigned int tiling_mode,
1899 unsigned int cpp,
1900 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02001901{
Chris Wilsonbc752862013-02-21 20:04:31 +00001902 if (tiling_mode != I915_TILING_NONE) {
1903 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02001904
Chris Wilsonbc752862013-02-21 20:04:31 +00001905 tile_rows = *y / 8;
1906 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02001907
Chris Wilsonbc752862013-02-21 20:04:31 +00001908 tiles = *x / (512/cpp);
1909 *x %= 512/cpp;
1910
1911 return tile_rows * pitch * 8 + tiles * 4096;
1912 } else {
1913 unsigned int offset;
1914
1915 offset = *y * pitch + *x * cpp;
1916 *y = 0;
1917 *x = (offset & 4095) / cpp;
1918 return offset & -4096;
1919 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02001920}
1921
Jesse Barnes17638cd2011-06-24 12:19:23 -07001922static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
1923 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07001924{
1925 struct drm_device *dev = crtc->dev;
1926 struct drm_i915_private *dev_priv = dev->dev_private;
1927 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1928 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00001929 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07001930 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02001931 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07001932 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01001933 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07001934
1935 switch (plane) {
1936 case 0:
1937 case 1:
1938 break;
1939 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03001940 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes81255562010-08-02 12:07:50 -07001941 return -EINVAL;
1942 }
1943
1944 intel_fb = to_intel_framebuffer(fb);
1945 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07001946
Chris Wilson5eddb702010-09-11 13:48:45 +01001947 reg = DSPCNTR(plane);
1948 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07001949 /* Mask out pixel format bits in case we change it */
1950 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02001951 switch (fb->pixel_format) {
1952 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07001953 dspcntr |= DISPPLANE_8BPP;
1954 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02001955 case DRM_FORMAT_XRGB1555:
1956 case DRM_FORMAT_ARGB1555:
1957 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07001958 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02001959 case DRM_FORMAT_RGB565:
1960 dspcntr |= DISPPLANE_BGRX565;
1961 break;
1962 case DRM_FORMAT_XRGB8888:
1963 case DRM_FORMAT_ARGB8888:
1964 dspcntr |= DISPPLANE_BGRX888;
1965 break;
1966 case DRM_FORMAT_XBGR8888:
1967 case DRM_FORMAT_ABGR8888:
1968 dspcntr |= DISPPLANE_RGBX888;
1969 break;
1970 case DRM_FORMAT_XRGB2101010:
1971 case DRM_FORMAT_ARGB2101010:
1972 dspcntr |= DISPPLANE_BGRX101010;
1973 break;
1974 case DRM_FORMAT_XBGR2101010:
1975 case DRM_FORMAT_ABGR2101010:
1976 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07001977 break;
1978 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01001979 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07001980 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02001981
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001982 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00001983 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07001984 dspcntr |= DISPPLANE_TILED;
1985 else
1986 dspcntr &= ~DISPPLANE_TILED;
1987 }
1988
Ville Syrjäläde1aa622013-06-07 10:47:01 +03001989 if (IS_G4X(dev))
1990 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
1991
Chris Wilson5eddb702010-09-11 13:48:45 +01001992 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07001993
Daniel Vettere506a0c2012-07-05 12:17:29 +02001994 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes81255562010-08-02 12:07:50 -07001995
Daniel Vetterc2c75132012-07-05 12:17:30 +02001996 if (INTEL_INFO(dev)->gen >= 4) {
1997 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00001998 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
1999 fb->bits_per_pixel / 8,
2000 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002001 linear_offset -= intel_crtc->dspaddr_offset;
2002 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002003 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002004 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002005
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002006 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2007 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2008 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002009 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002010 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetterc2c75132012-07-05 12:17:30 +02002011 I915_MODIFY_DISPBASE(DSPSURF(plane),
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002012 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002013 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002014 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002015 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002016 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002017 POSTING_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002018
Jesse Barnes17638cd2011-06-24 12:19:23 -07002019 return 0;
2020}
2021
2022static int ironlake_update_plane(struct drm_crtc *crtc,
2023 struct drm_framebuffer *fb, int x, int y)
2024{
2025 struct drm_device *dev = crtc->dev;
2026 struct drm_i915_private *dev_priv = dev->dev_private;
2027 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2028 struct intel_framebuffer *intel_fb;
2029 struct drm_i915_gem_object *obj;
2030 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002031 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002032 u32 dspcntr;
2033 u32 reg;
2034
2035 switch (plane) {
2036 case 0:
2037 case 1:
Jesse Barnes27f82272011-09-02 12:54:37 -07002038 case 2:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002039 break;
2040 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002041 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes17638cd2011-06-24 12:19:23 -07002042 return -EINVAL;
2043 }
2044
2045 intel_fb = to_intel_framebuffer(fb);
2046 obj = intel_fb->obj;
2047
2048 reg = DSPCNTR(plane);
2049 dspcntr = I915_READ(reg);
2050 /* Mask out pixel format bits in case we change it */
2051 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002052 switch (fb->pixel_format) {
2053 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002054 dspcntr |= DISPPLANE_8BPP;
2055 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002056 case DRM_FORMAT_RGB565:
2057 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002058 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002059 case DRM_FORMAT_XRGB8888:
2060 case DRM_FORMAT_ARGB8888:
2061 dspcntr |= DISPPLANE_BGRX888;
2062 break;
2063 case DRM_FORMAT_XBGR8888:
2064 case DRM_FORMAT_ABGR8888:
2065 dspcntr |= DISPPLANE_RGBX888;
2066 break;
2067 case DRM_FORMAT_XRGB2101010:
2068 case DRM_FORMAT_ARGB2101010:
2069 dspcntr |= DISPPLANE_BGRX101010;
2070 break;
2071 case DRM_FORMAT_XBGR2101010:
2072 case DRM_FORMAT_ABGR2101010:
2073 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002074 break;
2075 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002076 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002077 }
2078
2079 if (obj->tiling_mode != I915_TILING_NONE)
2080 dspcntr |= DISPPLANE_TILED;
2081 else
2082 dspcntr &= ~DISPPLANE_TILED;
2083
2084 /* must disable */
2085 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2086
2087 I915_WRITE(reg, dspcntr);
2088
Daniel Vettere506a0c2012-07-05 12:17:29 +02002089 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002090 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002091 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2092 fb->bits_per_pixel / 8,
2093 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002094 linear_offset -= intel_crtc->dspaddr_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002095
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002096 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2097 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2098 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002099 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002100 I915_MODIFY_DISPBASE(DSPSURF(plane),
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002101 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002102 if (IS_HASWELL(dev)) {
2103 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2104 } else {
2105 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2106 I915_WRITE(DSPLINOFF(plane), linear_offset);
2107 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002108 POSTING_READ(reg);
2109
2110 return 0;
2111}
2112
2113/* Assume fb object is pinned & idle & fenced and just update base pointers */
2114static int
2115intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2116 int x, int y, enum mode_set_atomic state)
2117{
2118 struct drm_device *dev = crtc->dev;
2119 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002120
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002121 if (dev_priv->display.disable_fbc)
2122 dev_priv->display.disable_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02002123 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07002124
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002125 return dev_priv->display.update_plane(crtc, fb, x, y);
Jesse Barnes81255562010-08-02 12:07:50 -07002126}
2127
Ville Syrjälä96a02912013-02-18 19:08:49 +02002128void intel_display_handle_reset(struct drm_device *dev)
2129{
2130 struct drm_i915_private *dev_priv = dev->dev_private;
2131 struct drm_crtc *crtc;
2132
2133 /*
2134 * Flips in the rings have been nuked by the reset,
2135 * so complete all pending flips so that user space
2136 * will get its events and not get stuck.
2137 *
2138 * Also update the base address of all primary
2139 * planes to the the last fb to make sure we're
2140 * showing the correct fb after a reset.
2141 *
2142 * Need to make two loops over the crtcs so that we
2143 * don't try to grab a crtc mutex before the
2144 * pending_flip_queue really got woken up.
2145 */
2146
2147 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2148 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2149 enum plane plane = intel_crtc->plane;
2150
2151 intel_prepare_page_flip(dev, plane);
2152 intel_finish_page_flip_plane(dev, plane);
2153 }
2154
2155 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2156 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2157
2158 mutex_lock(&crtc->mutex);
2159 if (intel_crtc->active)
2160 dev_priv->display.update_plane(crtc, crtc->fb,
2161 crtc->x, crtc->y);
2162 mutex_unlock(&crtc->mutex);
2163 }
2164}
2165
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002166static int
Chris Wilson14667a42012-04-03 17:58:35 +01002167intel_finish_fb(struct drm_framebuffer *old_fb)
2168{
2169 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2170 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2171 bool was_interruptible = dev_priv->mm.interruptible;
2172 int ret;
2173
Chris Wilson14667a42012-04-03 17:58:35 +01002174 /* Big Hammer, we also need to ensure that any pending
2175 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2176 * current scanout is retired before unpinning the old
2177 * framebuffer.
2178 *
2179 * This should only fail upon a hung GPU, in which case we
2180 * can safely continue.
2181 */
2182 dev_priv->mm.interruptible = false;
2183 ret = i915_gem_object_finish_gpu(obj);
2184 dev_priv->mm.interruptible = was_interruptible;
2185
2186 return ret;
2187}
2188
Ville Syrjälä198598d2012-10-31 17:50:24 +02002189static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
2190{
2191 struct drm_device *dev = crtc->dev;
2192 struct drm_i915_master_private *master_priv;
2193 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2194
2195 if (!dev->primary->master)
2196 return;
2197
2198 master_priv = dev->primary->master->driver_priv;
2199 if (!master_priv->sarea_priv)
2200 return;
2201
2202 switch (intel_crtc->pipe) {
2203 case 0:
2204 master_priv->sarea_priv->pipeA_x = x;
2205 master_priv->sarea_priv->pipeA_y = y;
2206 break;
2207 case 1:
2208 master_priv->sarea_priv->pipeB_x = x;
2209 master_priv->sarea_priv->pipeB_y = y;
2210 break;
2211 default:
2212 break;
2213 }
2214}
2215
Chris Wilson14667a42012-04-03 17:58:35 +01002216static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002217intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002218 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002219{
2220 struct drm_device *dev = crtc->dev;
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002221 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08002222 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter94352cf2012-07-05 22:51:56 +02002223 struct drm_framebuffer *old_fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002224 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002225
2226 /* no fb bound */
Daniel Vetter94352cf2012-07-05 22:51:56 +02002227 if (!fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002228 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002229 return 0;
2230 }
2231
Ben Widawsky7eb552a2013-03-13 14:05:41 -07002232 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002233 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2234 plane_name(intel_crtc->plane),
2235 INTEL_INFO(dev)->num_pipes);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002236 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002237 }
2238
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002239 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01002240 ret = intel_pin_and_fence_fb_obj(dev,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002241 to_intel_framebuffer(fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002242 NULL);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002243 if (ret != 0) {
2244 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002245 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002246 return ret;
2247 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002248
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002249 /* Update pipe size and adjust fitter if needed */
2250 if (i915_fastboot) {
2251 I915_WRITE(PIPESRC(intel_crtc->pipe),
2252 ((crtc->mode.hdisplay - 1) << 16) |
2253 (crtc->mode.vdisplay - 1));
2254 if (!intel_crtc->config.pch_pfit.size &&
2255 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
2256 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2257 I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
2258 I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
2259 I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
2260 }
2261 }
2262
Daniel Vetter94352cf2012-07-05 22:51:56 +02002263 ret = dev_priv->display.update_plane(crtc, fb, x, y);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002264 if (ret) {
Daniel Vetter94352cf2012-07-05 22:51:56 +02002265 intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002266 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002267 DRM_ERROR("failed to update base address\n");
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002268 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002269 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002270
Daniel Vetter94352cf2012-07-05 22:51:56 +02002271 old_fb = crtc->fb;
2272 crtc->fb = fb;
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02002273 crtc->x = x;
2274 crtc->y = y;
Daniel Vetter94352cf2012-07-05 22:51:56 +02002275
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002276 if (old_fb) {
Daniel Vetterd7697ee2013-06-02 17:23:01 +02002277 if (intel_crtc->active && old_fb != fb)
2278 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002279 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002280 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002281
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002282 intel_update_fbc(dev);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002283 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002284
Ville Syrjälä198598d2012-10-31 17:50:24 +02002285 intel_crtc_update_sarea_pos(crtc, x, y);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002286
2287 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002288}
2289
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002290static void intel_fdi_normal_train(struct drm_crtc *crtc)
2291{
2292 struct drm_device *dev = crtc->dev;
2293 struct drm_i915_private *dev_priv = dev->dev_private;
2294 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2295 int pipe = intel_crtc->pipe;
2296 u32 reg, temp;
2297
2298 /* enable normal train */
2299 reg = FDI_TX_CTL(pipe);
2300 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002301 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002302 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2303 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002304 } else {
2305 temp &= ~FDI_LINK_TRAIN_NONE;
2306 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002307 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002308 I915_WRITE(reg, temp);
2309
2310 reg = FDI_RX_CTL(pipe);
2311 temp = I915_READ(reg);
2312 if (HAS_PCH_CPT(dev)) {
2313 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2314 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2315 } else {
2316 temp &= ~FDI_LINK_TRAIN_NONE;
2317 temp |= FDI_LINK_TRAIN_NONE;
2318 }
2319 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2320
2321 /* wait one idle pattern time */
2322 POSTING_READ(reg);
2323 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002324
2325 /* IVB wants error correction enabled */
2326 if (IS_IVYBRIDGE(dev))
2327 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2328 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002329}
2330
Daniel Vetter1e833f42013-02-19 22:31:57 +01002331static bool pipe_has_enabled_pch(struct intel_crtc *intel_crtc)
2332{
2333 return intel_crtc->base.enabled && intel_crtc->config.has_pch_encoder;
2334}
2335
Daniel Vetter01a415f2012-10-27 15:58:40 +02002336static void ivb_modeset_global_resources(struct drm_device *dev)
2337{
2338 struct drm_i915_private *dev_priv = dev->dev_private;
2339 struct intel_crtc *pipe_B_crtc =
2340 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2341 struct intel_crtc *pipe_C_crtc =
2342 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2343 uint32_t temp;
2344
Daniel Vetter1e833f42013-02-19 22:31:57 +01002345 /*
2346 * When everything is off disable fdi C so that we could enable fdi B
2347 * with all lanes. Note that we don't care about enabled pipes without
2348 * an enabled pch encoder.
2349 */
2350 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2351 !pipe_has_enabled_pch(pipe_C_crtc)) {
Daniel Vetter01a415f2012-10-27 15:58:40 +02002352 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2353 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2354
2355 temp = I915_READ(SOUTH_CHICKEN1);
2356 temp &= ~FDI_BC_BIFURCATION_SELECT;
2357 DRM_DEBUG_KMS("disabling fdi C rx\n");
2358 I915_WRITE(SOUTH_CHICKEN1, temp);
2359 }
2360}
2361
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002362/* The FDI link training functions for ILK/Ibexpeak. */
2363static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2364{
2365 struct drm_device *dev = crtc->dev;
2366 struct drm_i915_private *dev_priv = dev->dev_private;
2367 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2368 int pipe = intel_crtc->pipe;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002369 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002370 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002371
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002372 /* FDI needs bits from pipe & plane first */
2373 assert_pipe_enabled(dev_priv, pipe);
2374 assert_plane_enabled(dev_priv, plane);
2375
Adam Jacksone1a44742010-06-25 15:32:14 -04002376 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2377 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002378 reg = FDI_RX_IMR(pipe);
2379 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002380 temp &= ~FDI_RX_SYMBOL_LOCK;
2381 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002382 I915_WRITE(reg, temp);
2383 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002384 udelay(150);
2385
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002386 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002387 reg = FDI_TX_CTL(pipe);
2388 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002389 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2390 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002391 temp &= ~FDI_LINK_TRAIN_NONE;
2392 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002393 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002394
Chris Wilson5eddb702010-09-11 13:48:45 +01002395 reg = FDI_RX_CTL(pipe);
2396 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002397 temp &= ~FDI_LINK_TRAIN_NONE;
2398 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002399 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2400
2401 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002402 udelay(150);
2403
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002404 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002405 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2406 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2407 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002408
Chris Wilson5eddb702010-09-11 13:48:45 +01002409 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002410 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002411 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002412 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2413
2414 if ((temp & FDI_RX_BIT_LOCK)) {
2415 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002416 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002417 break;
2418 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002419 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002420 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002421 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002422
2423 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002424 reg = FDI_TX_CTL(pipe);
2425 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002426 temp &= ~FDI_LINK_TRAIN_NONE;
2427 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002428 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002429
Chris Wilson5eddb702010-09-11 13:48:45 +01002430 reg = FDI_RX_CTL(pipe);
2431 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002432 temp &= ~FDI_LINK_TRAIN_NONE;
2433 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002434 I915_WRITE(reg, temp);
2435
2436 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002437 udelay(150);
2438
Chris Wilson5eddb702010-09-11 13:48:45 +01002439 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002440 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002441 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002442 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2443
2444 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002445 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002446 DRM_DEBUG_KMS("FDI train 2 done.\n");
2447 break;
2448 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002449 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002450 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002451 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002452
2453 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002454
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002455}
2456
Akshay Joshi0206e352011-08-16 15:34:10 -04002457static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002458 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2459 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2460 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2461 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2462};
2463
2464/* The FDI link training functions for SNB/Cougarpoint. */
2465static void gen6_fdi_link_train(struct drm_crtc *crtc)
2466{
2467 struct drm_device *dev = crtc->dev;
2468 struct drm_i915_private *dev_priv = dev->dev_private;
2469 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2470 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05002471 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002472
Adam Jacksone1a44742010-06-25 15:32:14 -04002473 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2474 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002475 reg = FDI_RX_IMR(pipe);
2476 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002477 temp &= ~FDI_RX_SYMBOL_LOCK;
2478 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002479 I915_WRITE(reg, temp);
2480
2481 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002482 udelay(150);
2483
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002484 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002485 reg = FDI_TX_CTL(pipe);
2486 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002487 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2488 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002489 temp &= ~FDI_LINK_TRAIN_NONE;
2490 temp |= FDI_LINK_TRAIN_PATTERN_1;
2491 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2492 /* SNB-B */
2493 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002494 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002495
Daniel Vetterd74cf322012-10-26 10:58:13 +02002496 I915_WRITE(FDI_RX_MISC(pipe),
2497 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2498
Chris Wilson5eddb702010-09-11 13:48:45 +01002499 reg = FDI_RX_CTL(pipe);
2500 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002501 if (HAS_PCH_CPT(dev)) {
2502 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2503 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2504 } else {
2505 temp &= ~FDI_LINK_TRAIN_NONE;
2506 temp |= FDI_LINK_TRAIN_PATTERN_1;
2507 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002508 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2509
2510 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002511 udelay(150);
2512
Akshay Joshi0206e352011-08-16 15:34:10 -04002513 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002514 reg = FDI_TX_CTL(pipe);
2515 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002516 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2517 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002518 I915_WRITE(reg, temp);
2519
2520 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002521 udelay(500);
2522
Sean Paulfa37d392012-03-02 12:53:39 -05002523 for (retry = 0; retry < 5; retry++) {
2524 reg = FDI_RX_IIR(pipe);
2525 temp = I915_READ(reg);
2526 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2527 if (temp & FDI_RX_BIT_LOCK) {
2528 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2529 DRM_DEBUG_KMS("FDI train 1 done.\n");
2530 break;
2531 }
2532 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002533 }
Sean Paulfa37d392012-03-02 12:53:39 -05002534 if (retry < 5)
2535 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002536 }
2537 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002538 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002539
2540 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002541 reg = FDI_TX_CTL(pipe);
2542 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002543 temp &= ~FDI_LINK_TRAIN_NONE;
2544 temp |= FDI_LINK_TRAIN_PATTERN_2;
2545 if (IS_GEN6(dev)) {
2546 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2547 /* SNB-B */
2548 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2549 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002550 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002551
Chris Wilson5eddb702010-09-11 13:48:45 +01002552 reg = FDI_RX_CTL(pipe);
2553 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002554 if (HAS_PCH_CPT(dev)) {
2555 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2556 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2557 } else {
2558 temp &= ~FDI_LINK_TRAIN_NONE;
2559 temp |= FDI_LINK_TRAIN_PATTERN_2;
2560 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002561 I915_WRITE(reg, temp);
2562
2563 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002564 udelay(150);
2565
Akshay Joshi0206e352011-08-16 15:34:10 -04002566 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002567 reg = FDI_TX_CTL(pipe);
2568 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002569 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2570 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002571 I915_WRITE(reg, temp);
2572
2573 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002574 udelay(500);
2575
Sean Paulfa37d392012-03-02 12:53:39 -05002576 for (retry = 0; retry < 5; retry++) {
2577 reg = FDI_RX_IIR(pipe);
2578 temp = I915_READ(reg);
2579 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2580 if (temp & FDI_RX_SYMBOL_LOCK) {
2581 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2582 DRM_DEBUG_KMS("FDI train 2 done.\n");
2583 break;
2584 }
2585 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002586 }
Sean Paulfa37d392012-03-02 12:53:39 -05002587 if (retry < 5)
2588 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002589 }
2590 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002591 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002592
2593 DRM_DEBUG_KMS("FDI train done.\n");
2594}
2595
Jesse Barnes357555c2011-04-28 15:09:55 -07002596/* Manual link training for Ivy Bridge A0 parts */
2597static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2598{
2599 struct drm_device *dev = crtc->dev;
2600 struct drm_i915_private *dev_priv = dev->dev_private;
2601 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2602 int pipe = intel_crtc->pipe;
2603 u32 reg, temp, i;
2604
2605 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2606 for train result */
2607 reg = FDI_RX_IMR(pipe);
2608 temp = I915_READ(reg);
2609 temp &= ~FDI_RX_SYMBOL_LOCK;
2610 temp &= ~FDI_RX_BIT_LOCK;
2611 I915_WRITE(reg, temp);
2612
2613 POSTING_READ(reg);
2614 udelay(150);
2615
Daniel Vetter01a415f2012-10-27 15:58:40 +02002616 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
2617 I915_READ(FDI_RX_IIR(pipe)));
2618
Jesse Barnes357555c2011-04-28 15:09:55 -07002619 /* enable CPU FDI TX and PCH FDI RX */
2620 reg = FDI_TX_CTL(pipe);
2621 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002622 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2623 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Jesse Barnes357555c2011-04-28 15:09:55 -07002624 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2625 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
2626 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2627 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002628 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002629 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2630
Daniel Vetterd74cf322012-10-26 10:58:13 +02002631 I915_WRITE(FDI_RX_MISC(pipe),
2632 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2633
Jesse Barnes357555c2011-04-28 15:09:55 -07002634 reg = FDI_RX_CTL(pipe);
2635 temp = I915_READ(reg);
2636 temp &= ~FDI_LINK_TRAIN_AUTO;
2637 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2638 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002639 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002640 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2641
2642 POSTING_READ(reg);
2643 udelay(150);
2644
Akshay Joshi0206e352011-08-16 15:34:10 -04002645 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002646 reg = FDI_TX_CTL(pipe);
2647 temp = I915_READ(reg);
2648 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2649 temp |= snb_b_fdi_train_param[i];
2650 I915_WRITE(reg, temp);
2651
2652 POSTING_READ(reg);
2653 udelay(500);
2654
2655 reg = FDI_RX_IIR(pipe);
2656 temp = I915_READ(reg);
2657 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2658
2659 if (temp & FDI_RX_BIT_LOCK ||
2660 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2661 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Daniel Vetter01a415f2012-10-27 15:58:40 +02002662 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n", i);
Jesse Barnes357555c2011-04-28 15:09:55 -07002663 break;
2664 }
2665 }
2666 if (i == 4)
2667 DRM_ERROR("FDI train 1 fail!\n");
2668
2669 /* Train 2 */
2670 reg = FDI_TX_CTL(pipe);
2671 temp = I915_READ(reg);
2672 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2673 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2674 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2675 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2676 I915_WRITE(reg, temp);
2677
2678 reg = FDI_RX_CTL(pipe);
2679 temp = I915_READ(reg);
2680 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2681 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2682 I915_WRITE(reg, temp);
2683
2684 POSTING_READ(reg);
2685 udelay(150);
2686
Akshay Joshi0206e352011-08-16 15:34:10 -04002687 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002688 reg = FDI_TX_CTL(pipe);
2689 temp = I915_READ(reg);
2690 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2691 temp |= snb_b_fdi_train_param[i];
2692 I915_WRITE(reg, temp);
2693
2694 POSTING_READ(reg);
2695 udelay(500);
2696
2697 reg = FDI_RX_IIR(pipe);
2698 temp = I915_READ(reg);
2699 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2700
2701 if (temp & FDI_RX_SYMBOL_LOCK) {
2702 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Daniel Vetter01a415f2012-10-27 15:58:40 +02002703 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n", i);
Jesse Barnes357555c2011-04-28 15:09:55 -07002704 break;
2705 }
2706 }
2707 if (i == 4)
2708 DRM_ERROR("FDI train 2 fail!\n");
2709
2710 DRM_DEBUG_KMS("FDI train done.\n");
2711}
2712
Daniel Vetter88cefb62012-08-12 19:27:14 +02002713static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07002714{
Daniel Vetter88cefb62012-08-12 19:27:14 +02002715 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002716 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002717 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002718 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002719
Jesse Barnesc64e3112010-09-10 11:27:03 -07002720
Jesse Barnes0e23b992010-09-10 11:10:00 -07002721 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01002722 reg = FDI_RX_CTL(pipe);
2723 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002724 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
2725 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002726 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01002727 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2728
2729 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002730 udelay(200);
2731
2732 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002733 temp = I915_READ(reg);
2734 I915_WRITE(reg, temp | FDI_PCDCLK);
2735
2736 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002737 udelay(200);
2738
Paulo Zanoni20749732012-11-23 15:30:38 -02002739 /* Enable CPU FDI TX PLL, always on for Ironlake */
2740 reg = FDI_TX_CTL(pipe);
2741 temp = I915_READ(reg);
2742 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2743 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01002744
Paulo Zanoni20749732012-11-23 15:30:38 -02002745 POSTING_READ(reg);
2746 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002747 }
2748}
2749
Daniel Vetter88cefb62012-08-12 19:27:14 +02002750static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
2751{
2752 struct drm_device *dev = intel_crtc->base.dev;
2753 struct drm_i915_private *dev_priv = dev->dev_private;
2754 int pipe = intel_crtc->pipe;
2755 u32 reg, temp;
2756
2757 /* Switch from PCDclk to Rawclk */
2758 reg = FDI_RX_CTL(pipe);
2759 temp = I915_READ(reg);
2760 I915_WRITE(reg, temp & ~FDI_PCDCLK);
2761
2762 /* Disable CPU FDI TX PLL */
2763 reg = FDI_TX_CTL(pipe);
2764 temp = I915_READ(reg);
2765 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2766
2767 POSTING_READ(reg);
2768 udelay(100);
2769
2770 reg = FDI_RX_CTL(pipe);
2771 temp = I915_READ(reg);
2772 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2773
2774 /* Wait for the clocks to turn off. */
2775 POSTING_READ(reg);
2776 udelay(100);
2777}
2778
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002779static void ironlake_fdi_disable(struct drm_crtc *crtc)
2780{
2781 struct drm_device *dev = crtc->dev;
2782 struct drm_i915_private *dev_priv = dev->dev_private;
2783 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2784 int pipe = intel_crtc->pipe;
2785 u32 reg, temp;
2786
2787 /* disable CPU FDI tx and PCH FDI rx */
2788 reg = FDI_TX_CTL(pipe);
2789 temp = I915_READ(reg);
2790 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2791 POSTING_READ(reg);
2792
2793 reg = FDI_RX_CTL(pipe);
2794 temp = I915_READ(reg);
2795 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002796 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002797 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2798
2799 POSTING_READ(reg);
2800 udelay(100);
2801
2802 /* Ironlake workaround, disable clock pointer after downing FDI */
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002803 if (HAS_PCH_IBX(dev)) {
2804 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002805 }
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002806
2807 /* still set train pattern 1 */
2808 reg = FDI_TX_CTL(pipe);
2809 temp = I915_READ(reg);
2810 temp &= ~FDI_LINK_TRAIN_NONE;
2811 temp |= FDI_LINK_TRAIN_PATTERN_1;
2812 I915_WRITE(reg, temp);
2813
2814 reg = FDI_RX_CTL(pipe);
2815 temp = I915_READ(reg);
2816 if (HAS_PCH_CPT(dev)) {
2817 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2818 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2819 } else {
2820 temp &= ~FDI_LINK_TRAIN_NONE;
2821 temp |= FDI_LINK_TRAIN_PATTERN_1;
2822 }
2823 /* BPC in FDI rx is consistent with that in PIPECONF */
2824 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002825 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002826 I915_WRITE(reg, temp);
2827
2828 POSTING_READ(reg);
2829 udelay(100);
2830}
2831
Chris Wilson5bb61642012-09-27 21:25:58 +01002832static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2833{
2834 struct drm_device *dev = crtc->dev;
2835 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä10d83732013-01-29 18:13:34 +02002836 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5bb61642012-09-27 21:25:58 +01002837 unsigned long flags;
2838 bool pending;
2839
Ville Syrjälä10d83732013-01-29 18:13:34 +02002840 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2841 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
Chris Wilson5bb61642012-09-27 21:25:58 +01002842 return false;
2843
2844 spin_lock_irqsave(&dev->event_lock, flags);
2845 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2846 spin_unlock_irqrestore(&dev->event_lock, flags);
2847
2848 return pending;
2849}
2850
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002851static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2852{
Chris Wilson0f911282012-04-17 10:05:38 +01002853 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01002854 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002855
2856 if (crtc->fb == NULL)
2857 return;
2858
Daniel Vetter2c10d572012-12-20 21:24:07 +01002859 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
2860
Chris Wilson5bb61642012-09-27 21:25:58 +01002861 wait_event(dev_priv->pending_flip_queue,
2862 !intel_crtc_has_pending_flip(crtc));
2863
Chris Wilson0f911282012-04-17 10:05:38 +01002864 mutex_lock(&dev->struct_mutex);
2865 intel_finish_fb(crtc->fb);
2866 mutex_unlock(&dev->struct_mutex);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002867}
2868
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002869/* Program iCLKIP clock to the desired frequency */
2870static void lpt_program_iclkip(struct drm_crtc *crtc)
2871{
2872 struct drm_device *dev = crtc->dev;
2873 struct drm_i915_private *dev_priv = dev->dev_private;
2874 u32 divsel, phaseinc, auxdiv, phasedir = 0;
2875 u32 temp;
2876
Daniel Vetter09153002012-12-12 14:06:44 +01002877 mutex_lock(&dev_priv->dpio_lock);
2878
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002879 /* It is necessary to ungate the pixclk gate prior to programming
2880 * the divisors, and gate it back when it is done.
2881 */
2882 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
2883
2884 /* Disable SSCCTL */
2885 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002886 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
2887 SBI_SSCCTL_DISABLE,
2888 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002889
2890 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
2891 if (crtc->mode.clock == 20000) {
2892 auxdiv = 1;
2893 divsel = 0x41;
2894 phaseinc = 0x20;
2895 } else {
2896 /* The iCLK virtual clock root frequency is in MHz,
2897 * but the crtc->mode.clock in in KHz. To get the divisors,
2898 * it is necessary to divide one by another, so we
2899 * convert the virtual clock precision to KHz here for higher
2900 * precision.
2901 */
2902 u32 iclk_virtual_root_freq = 172800 * 1000;
2903 u32 iclk_pi_range = 64;
2904 u32 desired_divisor, msb_divisor_value, pi_value;
2905
2906 desired_divisor = (iclk_virtual_root_freq / crtc->mode.clock);
2907 msb_divisor_value = desired_divisor / iclk_pi_range;
2908 pi_value = desired_divisor % iclk_pi_range;
2909
2910 auxdiv = 0;
2911 divsel = msb_divisor_value - 2;
2912 phaseinc = pi_value;
2913 }
2914
2915 /* This should not happen with any sane values */
2916 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
2917 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
2918 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
2919 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
2920
2921 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
2922 crtc->mode.clock,
2923 auxdiv,
2924 divsel,
2925 phasedir,
2926 phaseinc);
2927
2928 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002929 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002930 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
2931 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
2932 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
2933 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
2934 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
2935 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002936 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002937
2938 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002939 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002940 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
2941 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002942 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002943
2944 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002945 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002946 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002947 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002948
2949 /* Wait for initialization time */
2950 udelay(24);
2951
2952 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01002953
2954 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002955}
2956
Daniel Vetter275f01b22013-05-03 11:49:47 +02002957static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
2958 enum pipe pch_transcoder)
2959{
2960 struct drm_device *dev = crtc->base.dev;
2961 struct drm_i915_private *dev_priv = dev->dev_private;
2962 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
2963
2964 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
2965 I915_READ(HTOTAL(cpu_transcoder)));
2966 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
2967 I915_READ(HBLANK(cpu_transcoder)));
2968 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
2969 I915_READ(HSYNC(cpu_transcoder)));
2970
2971 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
2972 I915_READ(VTOTAL(cpu_transcoder)));
2973 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
2974 I915_READ(VBLANK(cpu_transcoder)));
2975 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
2976 I915_READ(VSYNC(cpu_transcoder)));
2977 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
2978 I915_READ(VSYNCSHIFT(cpu_transcoder)));
2979}
2980
Jesse Barnesf67a5592011-01-05 10:31:48 -08002981/*
2982 * Enable PCH resources required for PCH ports:
2983 * - PCH PLLs
2984 * - FDI training & RX/TX
2985 * - update transcoder timings
2986 * - DP transcoding bits
2987 * - transcoder
2988 */
2989static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08002990{
2991 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002992 struct drm_i915_private *dev_priv = dev->dev_private;
2993 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2994 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01002995 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07002996
Daniel Vetterab9412b2013-05-03 11:49:46 +02002997 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01002998
Daniel Vettercd986ab2012-10-26 10:58:12 +02002999 /* Write the TU size bits before fdi link training, so that error
3000 * detection works. */
3001 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3002 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3003
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003004 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07003005 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003006
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003007 /* We need to program the right clock selection before writing the pixel
3008 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003009 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003010 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07003011
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003012 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003013 temp |= TRANS_DPLL_ENABLE(pipe);
3014 sel = TRANS_DPLLB_SEL(pipe);
Daniel Vettera43f6e02013-06-07 23:10:32 +02003015 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003016 temp |= sel;
3017 else
3018 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003019 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003020 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003021
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003022 /* XXX: pch pll's can be enabled any time before we enable the PCH
3023 * transcoder, and we actually should do this to not upset any PCH
3024 * transcoder that already use the clock when we share it.
3025 *
3026 * Note that enable_shared_dpll tries to do the right thing, but
3027 * get_shared_dpll unconditionally resets the pll - we need that to have
3028 * the right LVDS enable sequence. */
3029 ironlake_enable_shared_dpll(intel_crtc);
3030
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003031 /* set transcoder timing, panel must allow it */
3032 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02003033 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003034
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003035 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003036
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003037 /* For PCH DP, enable TRANS_DP_CTL */
3038 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07003039 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3040 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003041 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003042 reg = TRANS_DP_CTL(pipe);
3043 temp = I915_READ(reg);
3044 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003045 TRANS_DP_SYNC_MASK |
3046 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003047 temp |= (TRANS_DP_OUTPUT_ENABLE |
3048 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003049 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003050
3051 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003052 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003053 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003054 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003055
3056 switch (intel_trans_dp_port_sel(crtc)) {
3057 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003058 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003059 break;
3060 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003061 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003062 break;
3063 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003064 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003065 break;
3066 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003067 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003068 }
3069
Chris Wilson5eddb702010-09-11 13:48:45 +01003070 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003071 }
3072
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003073 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003074}
3075
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003076static void lpt_pch_enable(struct drm_crtc *crtc)
3077{
3078 struct drm_device *dev = crtc->dev;
3079 struct drm_i915_private *dev_priv = dev->dev_private;
3080 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02003081 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003082
Daniel Vetterab9412b2013-05-03 11:49:46 +02003083 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003084
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003085 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003086
Paulo Zanoni0540e482012-10-31 18:12:40 -02003087 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02003088 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003089
Paulo Zanoni937bb612012-10-31 18:12:47 -02003090 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003091}
3092
Daniel Vettere2b78262013-06-07 23:10:03 +02003093static void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003094{
Daniel Vettere2b78262013-06-07 23:10:03 +02003095 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003096
3097 if (pll == NULL)
3098 return;
3099
3100 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003101 WARN(1, "bad %s refcount\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003102 return;
3103 }
3104
Daniel Vetterf4a091c2013-06-10 17:28:22 +02003105 if (--pll->refcount == 0) {
3106 WARN_ON(pll->on);
3107 WARN_ON(pll->active);
3108 }
3109
Daniel Vettera43f6e02013-06-07 23:10:32 +02003110 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003111}
3112
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003113static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003114{
Daniel Vettere2b78262013-06-07 23:10:03 +02003115 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3116 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3117 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003118
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003119 if (pll) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003120 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3121 crtc->base.base.id, pll->name);
Daniel Vettere2b78262013-06-07 23:10:03 +02003122 intel_put_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003123 }
3124
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003125 if (HAS_PCH_IBX(dev_priv->dev)) {
3126 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02003127 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003128 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003129
Daniel Vetter46edb022013-06-05 13:34:12 +02003130 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3131 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003132
3133 goto found;
3134 }
3135
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003136 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3137 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003138
3139 /* Only want to check enabled timings first */
3140 if (pll->refcount == 0)
3141 continue;
3142
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003143 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3144 sizeof(pll->hw_state)) == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003145 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
Daniel Vettere2b78262013-06-07 23:10:03 +02003146 crtc->base.base.id,
Daniel Vetter46edb022013-06-05 13:34:12 +02003147 pll->name, pll->refcount, pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003148
3149 goto found;
3150 }
3151 }
3152
3153 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003154 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3155 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003156 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003157 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3158 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003159 goto found;
3160 }
3161 }
3162
3163 return NULL;
3164
3165found:
Daniel Vettera43f6e02013-06-07 23:10:32 +02003166 crtc->config.shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02003167 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3168 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02003169
Daniel Vettercdbd2312013-06-05 13:34:03 +02003170 if (pll->active == 0) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02003171 memcpy(&pll->hw_state, &crtc->config.dpll_hw_state,
3172 sizeof(pll->hw_state));
3173
Daniel Vetter46edb022013-06-05 13:34:12 +02003174 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
Daniel Vettercdbd2312013-06-05 13:34:03 +02003175 WARN_ON(pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02003176 assert_shared_dpll_disabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003177
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02003178 pll->mode_set(dev_priv, pll);
Daniel Vettercdbd2312013-06-05 13:34:03 +02003179 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003180 pll->refcount++;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003181
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003182 return pll;
3183}
3184
Daniel Vettera1520312013-05-03 11:49:50 +02003185static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07003186{
3187 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01003188 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003189 u32 temp;
3190
3191 temp = I915_READ(dslreg);
3192 udelay(500);
3193 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07003194 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03003195 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07003196 }
3197}
3198
Jesse Barnesb074cec2013-04-25 12:55:02 -07003199static void ironlake_pfit_enable(struct intel_crtc *crtc)
3200{
3201 struct drm_device *dev = crtc->base.dev;
3202 struct drm_i915_private *dev_priv = dev->dev_private;
3203 int pipe = crtc->pipe;
3204
Jesse Barnes0ef37f32013-05-03 13:26:37 -07003205 if (crtc->config.pch_pfit.size) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07003206 /* Force use of hard-coded filter coefficients
3207 * as some pre-programmed values are broken,
3208 * e.g. x201.
3209 */
3210 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3211 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3212 PF_PIPE_SEL_IVB(pipe));
3213 else
3214 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3215 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3216 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08003217 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003218}
3219
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003220static void intel_enable_planes(struct drm_crtc *crtc)
3221{
3222 struct drm_device *dev = crtc->dev;
3223 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3224 struct intel_plane *intel_plane;
3225
3226 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3227 if (intel_plane->pipe == pipe)
3228 intel_plane_restore(&intel_plane->base);
3229}
3230
3231static void intel_disable_planes(struct drm_crtc *crtc)
3232{
3233 struct drm_device *dev = crtc->dev;
3234 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3235 struct intel_plane *intel_plane;
3236
3237 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3238 if (intel_plane->pipe == pipe)
3239 intel_plane_disable(&intel_plane->base);
3240}
3241
Jesse Barnesf67a5592011-01-05 10:31:48 -08003242static void ironlake_crtc_enable(struct drm_crtc *crtc)
3243{
3244 struct drm_device *dev = crtc->dev;
3245 struct drm_i915_private *dev_priv = dev->dev_private;
3246 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003247 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003248 int pipe = intel_crtc->pipe;
3249 int plane = intel_crtc->plane;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003250
Daniel Vetter08a48462012-07-02 11:43:47 +02003251 WARN_ON(!crtc->enabled);
3252
Jesse Barnesf67a5592011-01-05 10:31:48 -08003253 if (intel_crtc->active)
3254 return;
3255
3256 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003257
3258 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3259 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
3260
Jesse Barnesf67a5592011-01-05 10:31:48 -08003261 intel_update_watermarks(dev);
3262
Daniel Vetterf6736a12013-06-05 13:34:30 +02003263 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02003264 if (encoder->pre_enable)
3265 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003266
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003267 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02003268 /* Note: FDI PLL enabling _must_ be done before we enable the
3269 * cpu pipes, hence this is separate from all the other fdi/pch
3270 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02003271 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02003272 } else {
3273 assert_fdi_tx_disabled(dev_priv, pipe);
3274 assert_fdi_rx_disabled(dev_priv, pipe);
3275 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003276
Jesse Barnesb074cec2013-04-25 12:55:02 -07003277 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003278
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02003279 /*
3280 * On ILK+ LUT must be loaded before the pipe is running but with
3281 * clocks enabled
3282 */
3283 intel_crtc_load_lut(crtc);
3284
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003285 intel_enable_pipe(dev_priv, pipe,
3286 intel_crtc->config.has_pch_encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003287 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003288 intel_enable_planes(crtc);
Ville Syrjälä5c38d482013-06-04 13:49:00 +03003289 intel_crtc_update_cursor(crtc, true);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003290
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003291 if (intel_crtc->config.has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08003292 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003293
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003294 mutex_lock(&dev->struct_mutex);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003295 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003296 mutex_unlock(&dev->struct_mutex);
3297
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003298 for_each_encoder_on_crtc(dev, crtc, encoder)
3299 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02003300
3301 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02003302 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02003303
3304 /*
3305 * There seems to be a race in PCH platform hw (at least on some
3306 * outputs) where an enabled pipe still completes any pageflip right
3307 * away (as if the pipe is off) instead of waiting for vblank. As soon
3308 * as the first vblank happend, everything works as expected. Hence just
3309 * wait for one vblank before returning to avoid strange things
3310 * happening.
3311 */
3312 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003313}
3314
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003315/* IPS only exists on ULT machines and is tied to pipe A. */
3316static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
3317{
Damien Lespiauf5adf942013-06-24 18:29:34 +01003318 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003319}
3320
3321static void hsw_enable_ips(struct intel_crtc *crtc)
3322{
3323 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3324
3325 if (!crtc->config.ips_enabled)
3326 return;
3327
3328 /* We can only enable IPS after we enable a plane and wait for a vblank.
3329 * We guarantee that the plane is enabled by calling intel_enable_ips
3330 * only after intel_enable_plane. And intel_enable_plane already waits
3331 * for a vblank, so all we need to do here is to enable the IPS bit. */
3332 assert_plane_enabled(dev_priv, crtc->plane);
3333 I915_WRITE(IPS_CTL, IPS_ENABLE);
3334}
3335
3336static void hsw_disable_ips(struct intel_crtc *crtc)
3337{
3338 struct drm_device *dev = crtc->base.dev;
3339 struct drm_i915_private *dev_priv = dev->dev_private;
3340
3341 if (!crtc->config.ips_enabled)
3342 return;
3343
3344 assert_plane_enabled(dev_priv, crtc->plane);
3345 I915_WRITE(IPS_CTL, 0);
3346
3347 /* We need to wait for a vblank before we can disable the plane. */
3348 intel_wait_for_vblank(dev, crtc->pipe);
3349}
3350
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003351static void haswell_crtc_enable(struct drm_crtc *crtc)
3352{
3353 struct drm_device *dev = crtc->dev;
3354 struct drm_i915_private *dev_priv = dev->dev_private;
3355 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3356 struct intel_encoder *encoder;
3357 int pipe = intel_crtc->pipe;
3358 int plane = intel_crtc->plane;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003359
3360 WARN_ON(!crtc->enabled);
3361
3362 if (intel_crtc->active)
3363 return;
3364
3365 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003366
3367 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3368 if (intel_crtc->config.has_pch_encoder)
3369 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
3370
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003371 intel_update_watermarks(dev);
3372
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003373 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni04945642012-11-01 21:00:59 -02003374 dev_priv->display.fdi_link_train(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003375
3376 for_each_encoder_on_crtc(dev, crtc, encoder)
3377 if (encoder->pre_enable)
3378 encoder->pre_enable(encoder);
3379
Paulo Zanoni1f544382012-10-24 11:32:00 -02003380 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003381
Jesse Barnesb074cec2013-04-25 12:55:02 -07003382 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003383
3384 /*
3385 * On ILK+ LUT must be loaded before the pipe is running but with
3386 * clocks enabled
3387 */
3388 intel_crtc_load_lut(crtc);
3389
Paulo Zanoni1f544382012-10-24 11:32:00 -02003390 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00003391 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003392
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003393 intel_enable_pipe(dev_priv, pipe,
3394 intel_crtc->config.has_pch_encoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003395 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003396 intel_enable_planes(crtc);
Ville Syrjälä5c38d482013-06-04 13:49:00 +03003397 intel_crtc_update_cursor(crtc, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003398
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003399 hsw_enable_ips(intel_crtc);
3400
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003401 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003402 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003403
3404 mutex_lock(&dev->struct_mutex);
3405 intel_update_fbc(dev);
3406 mutex_unlock(&dev->struct_mutex);
3407
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003408 for_each_encoder_on_crtc(dev, crtc, encoder)
3409 encoder->enable(encoder);
3410
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003411 /*
3412 * There seems to be a race in PCH platform hw (at least on some
3413 * outputs) where an enabled pipe still completes any pageflip right
3414 * away (as if the pipe is off) instead of waiting for vblank. As soon
3415 * as the first vblank happend, everything works as expected. Hence just
3416 * wait for one vblank before returning to avoid strange things
3417 * happening.
3418 */
3419 intel_wait_for_vblank(dev, intel_crtc->pipe);
3420}
3421
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003422static void ironlake_pfit_disable(struct intel_crtc *crtc)
3423{
3424 struct drm_device *dev = crtc->base.dev;
3425 struct drm_i915_private *dev_priv = dev->dev_private;
3426 int pipe = crtc->pipe;
3427
3428 /* To avoid upsetting the power well on haswell only disable the pfit if
3429 * it's in use. The hw state code will make sure we get this right. */
3430 if (crtc->config.pch_pfit.size) {
3431 I915_WRITE(PF_CTL(pipe), 0);
3432 I915_WRITE(PF_WIN_POS(pipe), 0);
3433 I915_WRITE(PF_WIN_SZ(pipe), 0);
3434 }
3435}
3436
Jesse Barnes6be4a602010-09-10 10:26:01 -07003437static void ironlake_crtc_disable(struct drm_crtc *crtc)
3438{
3439 struct drm_device *dev = crtc->dev;
3440 struct drm_i915_private *dev_priv = dev->dev_private;
3441 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003442 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003443 int pipe = intel_crtc->pipe;
3444 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01003445 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003446
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003447
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003448 if (!intel_crtc->active)
3449 return;
3450
Daniel Vetterea9d7582012-07-10 10:42:52 +02003451 for_each_encoder_on_crtc(dev, crtc, encoder)
3452 encoder->disable(encoder);
3453
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003454 intel_crtc_wait_for_pending_flips(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003455 drm_vblank_off(dev, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003456
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07003457 if (dev_priv->fbc.plane == plane)
Chris Wilson973d04f2011-07-08 12:22:37 +01003458 intel_disable_fbc(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003459
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003460 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003461 intel_disable_planes(crtc);
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003462 intel_disable_plane(dev_priv, plane, pipe);
3463
Daniel Vetterd925c592013-06-05 13:34:04 +02003464 if (intel_crtc->config.has_pch_encoder)
3465 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
3466
Jesse Barnesb24e7172011-01-04 15:09:30 -08003467 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003468
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003469 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003470
Daniel Vetterbf49ec82012-09-06 22:15:40 +02003471 for_each_encoder_on_crtc(dev, crtc, encoder)
3472 if (encoder->post_disable)
3473 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003474
Daniel Vetterd925c592013-06-05 13:34:04 +02003475 if (intel_crtc->config.has_pch_encoder) {
3476 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003477
Daniel Vetterd925c592013-06-05 13:34:04 +02003478 ironlake_disable_pch_transcoder(dev_priv, pipe);
3479 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003480
Daniel Vetterd925c592013-06-05 13:34:04 +02003481 if (HAS_PCH_CPT(dev)) {
3482 /* disable TRANS_DP_CTL */
3483 reg = TRANS_DP_CTL(pipe);
3484 temp = I915_READ(reg);
3485 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
3486 TRANS_DP_PORT_SEL_MASK);
3487 temp |= TRANS_DP_PORT_SEL_NONE;
3488 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003489
Daniel Vetterd925c592013-06-05 13:34:04 +02003490 /* disable DPLL_SEL */
3491 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003492 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02003493 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003494 }
Daniel Vetterd925c592013-06-05 13:34:04 +02003495
3496 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003497 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02003498
3499 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003500 }
3501
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003502 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003503 intel_update_watermarks(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003504
3505 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01003506 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003507 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003508}
3509
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003510static void haswell_crtc_disable(struct drm_crtc *crtc)
3511{
3512 struct drm_device *dev = crtc->dev;
3513 struct drm_i915_private *dev_priv = dev->dev_private;
3514 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3515 struct intel_encoder *encoder;
3516 int pipe = intel_crtc->pipe;
3517 int plane = intel_crtc->plane;
Daniel Vetter3b117c82013-04-17 20:15:07 +02003518 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003519
3520 if (!intel_crtc->active)
3521 return;
3522
3523 for_each_encoder_on_crtc(dev, crtc, encoder)
3524 encoder->disable(encoder);
3525
3526 intel_crtc_wait_for_pending_flips(crtc);
3527 drm_vblank_off(dev, pipe);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003528
Rodrigo Vivi891348b2013-05-06 19:37:36 -03003529 /* FBC must be disabled before disabling the plane on HSW. */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07003530 if (dev_priv->fbc.plane == plane)
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003531 intel_disable_fbc(dev);
3532
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003533 hsw_disable_ips(intel_crtc);
3534
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003535 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003536 intel_disable_planes(crtc);
Rodrigo Vivi891348b2013-05-06 19:37:36 -03003537 intel_disable_plane(dev_priv, plane, pipe);
3538
Paulo Zanoni86642812013-04-12 17:57:57 -03003539 if (intel_crtc->config.has_pch_encoder)
3540 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003541 intel_disable_pipe(dev_priv, pipe);
3542
Paulo Zanoniad80a812012-10-24 16:06:19 -02003543 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003544
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003545 ironlake_pfit_disable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003546
Paulo Zanoni1f544382012-10-24 11:32:00 -02003547 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003548
3549 for_each_encoder_on_crtc(dev, crtc, encoder)
3550 if (encoder->post_disable)
3551 encoder->post_disable(encoder);
3552
Daniel Vetter88adfff2013-03-28 10:42:01 +01003553 if (intel_crtc->config.has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02003554 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni86642812013-04-12 17:57:57 -03003555 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02003556 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02003557 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003558
3559 intel_crtc->active = false;
3560 intel_update_watermarks(dev);
3561
3562 mutex_lock(&dev->struct_mutex);
3563 intel_update_fbc(dev);
3564 mutex_unlock(&dev->struct_mutex);
3565}
3566
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003567static void ironlake_crtc_off(struct drm_crtc *crtc)
3568{
3569 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003570 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003571}
3572
Paulo Zanoni6441ab52012-10-05 12:05:58 -03003573static void haswell_crtc_off(struct drm_crtc *crtc)
3574{
3575 intel_ddi_put_crtc_pll(crtc);
3576}
3577
Daniel Vetter02e792f2009-09-15 22:57:34 +02003578static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3579{
Daniel Vetter02e792f2009-09-15 22:57:34 +02003580 if (!enable && intel_crtc->overlay) {
Chris Wilson23f09ce2010-08-12 13:53:37 +01003581 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00003582 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02003583
Chris Wilson23f09ce2010-08-12 13:53:37 +01003584 mutex_lock(&dev->struct_mutex);
Chris Wilsonce453d82011-02-21 14:43:56 +00003585 dev_priv->mm.interruptible = false;
3586 (void) intel_overlay_switch_off(intel_crtc->overlay);
3587 dev_priv->mm.interruptible = true;
Chris Wilson23f09ce2010-08-12 13:53:37 +01003588 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02003589 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02003590
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01003591 /* Let userspace switch the overlay on again. In most cases userspace
3592 * has to recompute where to put it anyway.
3593 */
Daniel Vetter02e792f2009-09-15 22:57:34 +02003594}
3595
Egbert Eich61bc95c2013-03-04 09:24:38 -05003596/**
3597 * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
3598 * cursor plane briefly if not already running after enabling the display
3599 * plane.
3600 * This workaround avoids occasional blank screens when self refresh is
3601 * enabled.
3602 */
3603static void
3604g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
3605{
3606 u32 cntl = I915_READ(CURCNTR(pipe));
3607
3608 if ((cntl & CURSOR_MODE) == 0) {
3609 u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
3610
3611 I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
3612 I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
3613 intel_wait_for_vblank(dev_priv->dev, pipe);
3614 I915_WRITE(CURCNTR(pipe), cntl);
3615 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3616 I915_WRITE(FW_BLC_SELF, fw_bcl_self);
3617 }
3618}
3619
Jesse Barnes2dd24552013-04-25 12:55:01 -07003620static void i9xx_pfit_enable(struct intel_crtc *crtc)
3621{
3622 struct drm_device *dev = crtc->base.dev;
3623 struct drm_i915_private *dev_priv = dev->dev_private;
3624 struct intel_crtc_config *pipe_config = &crtc->config;
3625
Daniel Vetter328d8e82013-05-08 10:36:31 +02003626 if (!crtc->config.gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07003627 return;
3628
Daniel Vetterc0b03412013-05-28 12:05:54 +02003629 /*
3630 * The panel fitter should only be adjusted whilst the pipe is disabled,
3631 * according to register description and PRM.
3632 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07003633 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
3634 assert_pipe_disabled(dev_priv, crtc->pipe);
3635
Jesse Barnesb074cec2013-04-25 12:55:02 -07003636 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
3637 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02003638
3639 /* Border color in case we don't scale up to the full screen. Black by
3640 * default, change to something else for debugging. */
3641 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07003642}
3643
Jesse Barnes89b667f2013-04-18 14:51:36 -07003644static void valleyview_crtc_enable(struct drm_crtc *crtc)
3645{
3646 struct drm_device *dev = crtc->dev;
3647 struct drm_i915_private *dev_priv = dev->dev_private;
3648 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3649 struct intel_encoder *encoder;
3650 int pipe = intel_crtc->pipe;
3651 int plane = intel_crtc->plane;
3652
3653 WARN_ON(!crtc->enabled);
3654
3655 if (intel_crtc->active)
3656 return;
3657
3658 intel_crtc->active = true;
3659 intel_update_watermarks(dev);
3660
3661 mutex_lock(&dev_priv->dpio_lock);
3662
3663 for_each_encoder_on_crtc(dev, crtc, encoder)
3664 if (encoder->pre_pll_enable)
3665 encoder->pre_pll_enable(encoder);
3666
Daniel Vetter426115c2013-07-11 22:13:42 +02003667 vlv_enable_pll(intel_crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07003668
3669 for_each_encoder_on_crtc(dev, crtc, encoder)
3670 if (encoder->pre_enable)
3671 encoder->pre_enable(encoder);
3672
3673 /* VLV wants encoder enabling _before_ the pipe is up. */
3674 for_each_encoder_on_crtc(dev, crtc, encoder)
3675 encoder->enable(encoder);
3676
Jesse Barnes2dd24552013-04-25 12:55:01 -07003677 i9xx_pfit_enable(intel_crtc);
3678
Ville Syrjälä63cbb072013-06-04 13:48:59 +03003679 intel_crtc_load_lut(crtc);
3680
Jesse Barnes89b667f2013-04-18 14:51:36 -07003681 intel_enable_pipe(dev_priv, pipe, false);
3682 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003683 intel_enable_planes(crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07003684 intel_crtc_update_cursor(crtc, true);
3685
Ville Syrjäläf440eb12013-06-04 13:49:01 +03003686 intel_update_fbc(dev);
3687
Jesse Barnes89b667f2013-04-18 14:51:36 -07003688 mutex_unlock(&dev_priv->dpio_lock);
3689}
3690
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003691static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003692{
3693 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08003694 struct drm_i915_private *dev_priv = dev->dev_private;
3695 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003696 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08003697 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07003698 int plane = intel_crtc->plane;
Jesse Barnes79e53942008-11-07 14:24:08 -08003699
Daniel Vetter08a48462012-07-02 11:43:47 +02003700 WARN_ON(!crtc->enabled);
3701
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003702 if (intel_crtc->active)
3703 return;
3704
3705 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01003706 intel_update_watermarks(dev);
3707
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02003708 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02003709 if (encoder->pre_enable)
3710 encoder->pre_enable(encoder);
3711
Daniel Vetterf6736a12013-06-05 13:34:30 +02003712 i9xx_enable_pll(intel_crtc);
3713
Jesse Barnes2dd24552013-04-25 12:55:01 -07003714 i9xx_pfit_enable(intel_crtc);
3715
Ville Syrjälä63cbb072013-06-04 13:48:59 +03003716 intel_crtc_load_lut(crtc);
3717
Jesse Barnes040484a2011-01-03 12:14:26 -08003718 intel_enable_pipe(dev_priv, pipe, false);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003719 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003720 intel_enable_planes(crtc);
Ville Syrjälä22e407d2013-06-07 18:52:24 +03003721 /* The fixup needs to happen before cursor is enabled */
Egbert Eich61bc95c2013-03-04 09:24:38 -05003722 if (IS_G4X(dev))
3723 g4x_fixup_plane(dev_priv, pipe);
Ville Syrjälä22e407d2013-06-07 18:52:24 +03003724 intel_crtc_update_cursor(crtc, true);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003725
3726 /* Give the overlay scaler a chance to enable if it's on this pipe */
3727 intel_crtc_dpms_overlay(intel_crtc, true);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003728
Ville Syrjäläf440eb12013-06-04 13:49:01 +03003729 intel_update_fbc(dev);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003730
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003731 for_each_encoder_on_crtc(dev, crtc, encoder)
3732 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003733}
3734
Daniel Vetter87476d62013-04-11 16:29:06 +02003735static void i9xx_pfit_disable(struct intel_crtc *crtc)
3736{
3737 struct drm_device *dev = crtc->base.dev;
3738 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02003739
3740 if (!crtc->config.gmch_pfit.control)
3741 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02003742
3743 assert_pipe_disabled(dev_priv, crtc->pipe);
3744
Daniel Vetter328d8e82013-05-08 10:36:31 +02003745 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
3746 I915_READ(PFIT_CONTROL));
3747 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02003748}
3749
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003750static void i9xx_crtc_disable(struct drm_crtc *crtc)
3751{
3752 struct drm_device *dev = crtc->dev;
3753 struct drm_i915_private *dev_priv = dev->dev_private;
3754 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003755 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003756 int pipe = intel_crtc->pipe;
3757 int plane = intel_crtc->plane;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003758
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003759 if (!intel_crtc->active)
3760 return;
3761
Daniel Vetterea9d7582012-07-10 10:42:52 +02003762 for_each_encoder_on_crtc(dev, crtc, encoder)
3763 encoder->disable(encoder);
3764
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003765 /* Give the overlay scaler a chance to disable if it's on this pipe */
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003766 intel_crtc_wait_for_pending_flips(crtc);
3767 drm_vblank_off(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003768
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07003769 if (dev_priv->fbc.plane == plane)
Chris Wilson973d04f2011-07-08 12:22:37 +01003770 intel_disable_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003771
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003772 intel_crtc_dpms_overlay(intel_crtc, false);
3773 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003774 intel_disable_planes(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003775 intel_disable_plane(dev_priv, plane, pipe);
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003776
Jesse Barnesb24e7172011-01-04 15:09:30 -08003777 intel_disable_pipe(dev_priv, pipe);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02003778
Daniel Vetter87476d62013-04-11 16:29:06 +02003779 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02003780
Jesse Barnes89b667f2013-04-18 14:51:36 -07003781 for_each_encoder_on_crtc(dev, crtc, encoder)
3782 if (encoder->post_disable)
3783 encoder->post_disable(encoder);
3784
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08003785 intel_disable_pll(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003786
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003787 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003788 intel_update_fbc(dev);
3789 intel_update_watermarks(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003790}
3791
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003792static void i9xx_crtc_off(struct drm_crtc *crtc)
3793{
3794}
3795
Daniel Vetter976f8a22012-07-08 22:34:21 +02003796static void intel_crtc_update_sarea(struct drm_crtc *crtc,
3797 bool enabled)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003798{
3799 struct drm_device *dev = crtc->dev;
3800 struct drm_i915_master_private *master_priv;
3801 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3802 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08003803
3804 if (!dev->primary->master)
3805 return;
3806
3807 master_priv = dev->primary->master->driver_priv;
3808 if (!master_priv->sarea_priv)
3809 return;
3810
Jesse Barnes79e53942008-11-07 14:24:08 -08003811 switch (pipe) {
3812 case 0:
3813 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
3814 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
3815 break;
3816 case 1:
3817 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
3818 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
3819 break;
3820 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003821 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08003822 break;
3823 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003824}
3825
Daniel Vetter976f8a22012-07-08 22:34:21 +02003826/**
3827 * Sets the power management mode of the pipe and plane.
3828 */
3829void intel_crtc_update_dpms(struct drm_crtc *crtc)
Chris Wilsoncdd59982010-09-08 16:30:16 +01003830{
Chris Wilsoncdd59982010-09-08 16:30:16 +01003831 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003832 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter976f8a22012-07-08 22:34:21 +02003833 struct intel_encoder *intel_encoder;
3834 bool enable = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01003835
Daniel Vetter976f8a22012-07-08 22:34:21 +02003836 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
3837 enable |= intel_encoder->connectors_active;
3838
3839 if (enable)
3840 dev_priv->display.crtc_enable(crtc);
3841 else
3842 dev_priv->display.crtc_disable(crtc);
3843
3844 intel_crtc_update_sarea(crtc, enable);
3845}
3846
Daniel Vetter976f8a22012-07-08 22:34:21 +02003847static void intel_crtc_disable(struct drm_crtc *crtc)
3848{
3849 struct drm_device *dev = crtc->dev;
3850 struct drm_connector *connector;
3851 struct drm_i915_private *dev_priv = dev->dev_private;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08003852 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter976f8a22012-07-08 22:34:21 +02003853
3854 /* crtc should still be enabled when we disable it. */
3855 WARN_ON(!crtc->enabled);
3856
3857 dev_priv->display.crtc_disable(crtc);
Paulo Zanonic77bf562013-05-03 12:15:40 -03003858 intel_crtc->eld_vld = false;
Daniel Vetter976f8a22012-07-08 22:34:21 +02003859 intel_crtc_update_sarea(crtc, false);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003860 dev_priv->display.off(crtc);
3861
Chris Wilson931872f2012-01-16 23:01:13 +00003862 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
3863 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003864
3865 if (crtc->fb) {
3866 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01003867 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003868 mutex_unlock(&dev->struct_mutex);
Daniel Vetter976f8a22012-07-08 22:34:21 +02003869 crtc->fb = NULL;
3870 }
3871
3872 /* Update computed state. */
3873 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3874 if (!connector->encoder || !connector->encoder->crtc)
3875 continue;
3876
3877 if (connector->encoder->crtc != crtc)
3878 continue;
3879
3880 connector->dpms = DRM_MODE_DPMS_OFF;
3881 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01003882 }
3883}
3884
Daniel Vettera261b242012-07-26 19:21:47 +02003885void intel_modeset_disable(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08003886{
Daniel Vettera261b242012-07-26 19:21:47 +02003887 struct drm_crtc *crtc;
3888
3889 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3890 if (crtc->enabled)
3891 intel_crtc_disable(crtc);
3892 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003893}
3894
Chris Wilsonea5b2132010-08-04 13:50:23 +01003895void intel_encoder_destroy(struct drm_encoder *encoder)
3896{
Chris Wilson4ef69c72010-09-09 15:14:28 +01003897 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01003898
Chris Wilsonea5b2132010-08-04 13:50:23 +01003899 drm_encoder_cleanup(encoder);
3900 kfree(intel_encoder);
3901}
3902
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003903/* Simple dpms helper for encodres with just one connector, no cloning and only
3904 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
3905 * state of the entire output pipe. */
3906void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
3907{
3908 if (mode == DRM_MODE_DPMS_ON) {
3909 encoder->connectors_active = true;
3910
Daniel Vetterb2cabb02012-07-01 22:42:24 +02003911 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003912 } else {
3913 encoder->connectors_active = false;
3914
Daniel Vetterb2cabb02012-07-01 22:42:24 +02003915 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003916 }
3917}
3918
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003919/* Cross check the actual hw state with our own modeset state tracking (and it's
3920 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02003921static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003922{
3923 if (connector->get_hw_state(connector)) {
3924 struct intel_encoder *encoder = connector->encoder;
3925 struct drm_crtc *crtc;
3926 bool encoder_enabled;
3927 enum pipe pipe;
3928
3929 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
3930 connector->base.base.id,
3931 drm_get_connector_name(&connector->base));
3932
3933 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
3934 "wrong connector dpms state\n");
3935 WARN(connector->base.encoder != &encoder->base,
3936 "active connector not linked to encoder\n");
3937 WARN(!encoder->connectors_active,
3938 "encoder->connectors_active not set\n");
3939
3940 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
3941 WARN(!encoder_enabled, "encoder not enabled\n");
3942 if (WARN_ON(!encoder->base.crtc))
3943 return;
3944
3945 crtc = encoder->base.crtc;
3946
3947 WARN(!crtc->enabled, "crtc not enabled\n");
3948 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
3949 WARN(pipe != to_intel_crtc(crtc)->pipe,
3950 "encoder active on the wrong pipe\n");
3951 }
3952}
3953
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003954/* Even simpler default implementation, if there's really no special case to
3955 * consider. */
3956void intel_connector_dpms(struct drm_connector *connector, int mode)
3957{
3958 struct intel_encoder *encoder = intel_attached_encoder(connector);
3959
3960 /* All the simple cases only support two dpms states. */
3961 if (mode != DRM_MODE_DPMS_ON)
3962 mode = DRM_MODE_DPMS_OFF;
3963
3964 if (mode == connector->dpms)
3965 return;
3966
3967 connector->dpms = mode;
3968
3969 /* Only need to change hw state when actually enabled */
3970 if (encoder->base.crtc)
3971 intel_encoder_dpms(encoder, mode);
3972 else
Daniel Vetter8af6cf82012-07-10 09:50:11 +02003973 WARN_ON(encoder->connectors_active != false);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003974
Daniel Vetterb9805142012-08-31 17:37:33 +02003975 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003976}
3977
Daniel Vetterf0947c32012-07-02 13:10:34 +02003978/* Simple connector->get_hw_state implementation for encoders that support only
3979 * one connector and no cloning and hence the encoder state determines the state
3980 * of the connector. */
3981bool intel_connector_get_hw_state(struct intel_connector *connector)
3982{
Daniel Vetter24929352012-07-02 20:28:59 +02003983 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02003984 struct intel_encoder *encoder = connector->encoder;
3985
3986 return encoder->get_hw_state(encoder, &pipe);
3987}
3988
Daniel Vetter1857e1d2013-04-29 19:34:16 +02003989static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
3990 struct intel_crtc_config *pipe_config)
3991{
3992 struct drm_i915_private *dev_priv = dev->dev_private;
3993 struct intel_crtc *pipe_B_crtc =
3994 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
3995
3996 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
3997 pipe_name(pipe), pipe_config->fdi_lanes);
3998 if (pipe_config->fdi_lanes > 4) {
3999 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
4000 pipe_name(pipe), pipe_config->fdi_lanes);
4001 return false;
4002 }
4003
4004 if (IS_HASWELL(dev)) {
4005 if (pipe_config->fdi_lanes > 2) {
4006 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
4007 pipe_config->fdi_lanes);
4008 return false;
4009 } else {
4010 return true;
4011 }
4012 }
4013
4014 if (INTEL_INFO(dev)->num_pipes == 2)
4015 return true;
4016
4017 /* Ivybridge 3 pipe is really complicated */
4018 switch (pipe) {
4019 case PIPE_A:
4020 return true;
4021 case PIPE_B:
4022 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
4023 pipe_config->fdi_lanes > 2) {
4024 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4025 pipe_name(pipe), pipe_config->fdi_lanes);
4026 return false;
4027 }
4028 return true;
4029 case PIPE_C:
Daniel Vetter1e833f42013-02-19 22:31:57 +01004030 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004031 pipe_B_crtc->config.fdi_lanes <= 2) {
4032 if (pipe_config->fdi_lanes > 2) {
4033 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4034 pipe_name(pipe), pipe_config->fdi_lanes);
4035 return false;
4036 }
4037 } else {
4038 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
4039 return false;
4040 }
4041 return true;
4042 default:
4043 BUG();
4044 }
4045}
4046
Daniel Vettere29c22c2013-02-21 00:00:16 +01004047#define RETRY 1
4048static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
4049 struct intel_crtc_config *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02004050{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004051 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004052 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Daniel Vetterff9a6752013-06-01 17:16:21 +02004053 int lane, link_bw, fdi_dotclock;
Daniel Vettere29c22c2013-02-21 00:00:16 +01004054 bool setup_ok, needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004055
Daniel Vettere29c22c2013-02-21 00:00:16 +01004056retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02004057 /* FDI is a binary signal running at ~2.7GHz, encoding
4058 * each output octet as 10 bits. The actual frequency
4059 * is stored as a divider into a 100MHz clock, and the
4060 * mode pixel clock is stored in units of 1KHz.
4061 * Hence the bw of each lane in terms of the mode signal
4062 * is:
4063 */
4064 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
4065
Daniel Vetterff9a6752013-06-01 17:16:21 +02004066 fdi_dotclock = adjusted_mode->clock;
Daniel Vetteref1b4602013-06-01 17:17:04 +02004067 fdi_dotclock /= pipe_config->pixel_multiplier;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004068
Daniel Vetter2bd89a02013-06-01 17:16:19 +02004069 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02004070 pipe_config->pipe_bpp);
4071
4072 pipe_config->fdi_lanes = lane;
4073
Daniel Vetter2bd89a02013-06-01 17:16:19 +02004074 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02004075 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004076
Daniel Vettere29c22c2013-02-21 00:00:16 +01004077 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
4078 intel_crtc->pipe, pipe_config);
4079 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
4080 pipe_config->pipe_bpp -= 2*3;
4081 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
4082 pipe_config->pipe_bpp);
4083 needs_recompute = true;
4084 pipe_config->bw_constrained = true;
4085
4086 goto retry;
4087 }
4088
4089 if (needs_recompute)
4090 return RETRY;
4091
4092 return setup_ok ? 0 : -EINVAL;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004093}
4094
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004095static void hsw_compute_ips_config(struct intel_crtc *crtc,
4096 struct intel_crtc_config *pipe_config)
4097{
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03004098 pipe_config->ips_enabled = i915_enable_ips &&
4099 hsw_crtc_supports_ips(crtc) &&
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004100 pipe_config->pipe_bpp == 24;
4101}
4102
Daniel Vettera43f6e02013-06-07 23:10:32 +02004103static int intel_crtc_compute_config(struct intel_crtc *crtc,
Daniel Vettere29c22c2013-02-21 00:00:16 +01004104 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08004105{
Daniel Vettera43f6e02013-06-07 23:10:32 +02004106 struct drm_device *dev = crtc->base.dev;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01004107 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01004108
Eric Anholtbad720f2009-10-22 16:11:14 -07004109 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08004110 /* FDI link clock is fixed at 2.7G */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01004111 if (pipe_config->requested_mode.clock * 3
4112 > IRONLAKE_FDI_FREQ * 4)
Daniel Vettere29c22c2013-02-21 00:00:16 +01004113 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004114 }
Chris Wilson89749352010-09-12 18:25:19 +01004115
Daniel Vetterf9bef082012-04-15 19:53:19 +02004116 /* All interlaced capable intel hw wants timings in frames. Note though
4117 * that intel_lvds_mode_fixup does some funny tricks with the crtc
4118 * timings, so we need to be careful not to clobber these.*/
Daniel Vetter7ae89232013-03-27 00:44:52 +01004119 if (!pipe_config->timings_set)
Daniel Vetterf9bef082012-04-15 19:53:19 +02004120 drm_mode_set_crtcinfo(adjusted_mode, 0);
Chris Wilson89749352010-09-12 18:25:19 +01004121
Damien Lespiau8693a822013-05-03 18:48:11 +01004122 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
4123 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03004124 */
4125 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
4126 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01004127 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03004128
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004129 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004130 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004131 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004132 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
4133 * for lvds. */
4134 pipe_config->pipe_bpp = 8*3;
4135 }
4136
Damien Lespiauf5adf942013-06-24 18:29:34 +01004137 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02004138 hsw_compute_ips_config(crtc, pipe_config);
4139
4140 /* XXX: PCH clock sharing is done in ->mode_set, so make sure the old
4141 * clock survives for now. */
4142 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
4143 pipe_config->shared_dpll = crtc->config.shared_dpll;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004144
Daniel Vetter877d48d2013-04-19 11:24:43 +02004145 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02004146 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02004147
Daniel Vettere29c22c2013-02-21 00:00:16 +01004148 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08004149}
4150
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07004151static int valleyview_get_display_clock_speed(struct drm_device *dev)
4152{
4153 return 400000; /* FIXME */
4154}
4155
Jesse Barnese70236a2009-09-21 10:42:27 -07004156static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08004157{
Jesse Barnese70236a2009-09-21 10:42:27 -07004158 return 400000;
4159}
Jesse Barnes79e53942008-11-07 14:24:08 -08004160
Jesse Barnese70236a2009-09-21 10:42:27 -07004161static int i915_get_display_clock_speed(struct drm_device *dev)
4162{
4163 return 333000;
4164}
Jesse Barnes79e53942008-11-07 14:24:08 -08004165
Jesse Barnese70236a2009-09-21 10:42:27 -07004166static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
4167{
4168 return 200000;
4169}
Jesse Barnes79e53942008-11-07 14:24:08 -08004170
Jesse Barnese70236a2009-09-21 10:42:27 -07004171static int i915gm_get_display_clock_speed(struct drm_device *dev)
4172{
4173 u16 gcfgc = 0;
4174
4175 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4176
4177 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08004178 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07004179 else {
4180 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4181 case GC_DISPLAY_CLOCK_333_MHZ:
4182 return 333000;
4183 default:
4184 case GC_DISPLAY_CLOCK_190_200_MHZ:
4185 return 190000;
4186 }
4187 }
4188}
Jesse Barnes79e53942008-11-07 14:24:08 -08004189
Jesse Barnese70236a2009-09-21 10:42:27 -07004190static int i865_get_display_clock_speed(struct drm_device *dev)
4191{
4192 return 266000;
4193}
4194
4195static int i855_get_display_clock_speed(struct drm_device *dev)
4196{
4197 u16 hpllcc = 0;
4198 /* Assume that the hardware is in the high speed state. This
4199 * should be the default.
4200 */
4201 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
4202 case GC_CLOCK_133_200:
4203 case GC_CLOCK_100_200:
4204 return 200000;
4205 case GC_CLOCK_166_250:
4206 return 250000;
4207 case GC_CLOCK_100_133:
4208 return 133000;
4209 }
4210
4211 /* Shouldn't happen */
4212 return 0;
4213}
4214
4215static int i830_get_display_clock_speed(struct drm_device *dev)
4216{
4217 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08004218}
4219
Zhenyu Wang2c072452009-06-05 15:38:42 +08004220static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004221intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004222{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004223 while (*num > DATA_LINK_M_N_MASK ||
4224 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08004225 *num >>= 1;
4226 *den >>= 1;
4227 }
4228}
4229
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004230static void compute_m_n(unsigned int m, unsigned int n,
4231 uint32_t *ret_m, uint32_t *ret_n)
4232{
4233 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
4234 *ret_m = div_u64((uint64_t) m * *ret_n, n);
4235 intel_reduce_m_n_ratio(ret_m, ret_n);
4236}
4237
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004238void
4239intel_link_compute_m_n(int bits_per_pixel, int nlanes,
4240 int pixel_clock, int link_clock,
4241 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004242{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004243 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004244
4245 compute_m_n(bits_per_pixel * pixel_clock,
4246 link_clock * nlanes * 8,
4247 &m_n->gmch_m, &m_n->gmch_n);
4248
4249 compute_m_n(pixel_clock, link_clock,
4250 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004251}
4252
Chris Wilsona7615032011-01-12 17:04:08 +00004253static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
4254{
Keith Packard72bbe582011-09-26 16:09:45 -07004255 if (i915_panel_use_ssc >= 0)
4256 return i915_panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004257 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07004258 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00004259}
4260
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004261static int vlv_get_refclk(struct drm_crtc *crtc)
4262{
4263 struct drm_device *dev = crtc->dev;
4264 struct drm_i915_private *dev_priv = dev->dev_private;
4265 int refclk = 27000; /* for DP & HDMI */
4266
4267 return 100000; /* only one validated so far */
4268
4269 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
4270 refclk = 96000;
4271 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
4272 if (intel_panel_use_ssc(dev_priv))
4273 refclk = 100000;
4274 else
4275 refclk = 96000;
4276 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
4277 refclk = 100000;
4278 }
4279
4280 return refclk;
4281}
4282
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004283static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
4284{
4285 struct drm_device *dev = crtc->dev;
4286 struct drm_i915_private *dev_priv = dev->dev_private;
4287 int refclk;
4288
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004289 if (IS_VALLEYVIEW(dev)) {
4290 refclk = vlv_get_refclk(crtc);
4291 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004292 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004293 refclk = dev_priv->vbt.lvds_ssc_freq * 1000;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004294 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
4295 refclk / 1000);
4296 } else if (!IS_GEN2(dev)) {
4297 refclk = 96000;
4298 } else {
4299 refclk = 48000;
4300 }
4301
4302 return refclk;
4303}
4304
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004305static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004306{
Daniel Vetter7df00d72013-05-21 21:54:55 +02004307 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004308}
Daniel Vetterf47709a2013-03-28 10:42:02 +01004309
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004310static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
4311{
4312 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004313}
4314
Daniel Vetterf47709a2013-03-28 10:42:02 +01004315static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Jesse Barnesa7516a02011-12-15 12:30:37 -08004316 intel_clock_t *reduced_clock)
4317{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004318 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004319 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004320 int pipe = crtc->pipe;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004321 u32 fp, fp2 = 0;
4322
4323 if (IS_PINEVIEW(dev)) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004324 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004325 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004326 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004327 } else {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004328 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004329 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004330 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004331 }
4332
4333 I915_WRITE(FP0(pipe), fp);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004334 crtc->config.dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004335
Daniel Vetterf47709a2013-03-28 10:42:02 +01004336 crtc->lowfreq_avail = false;
4337 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Jesse Barnesa7516a02011-12-15 12:30:37 -08004338 reduced_clock && i915_powersave) {
4339 I915_WRITE(FP1(pipe), fp2);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004340 crtc->config.dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004341 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004342 } else {
4343 I915_WRITE(FP1(pipe), fp);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004344 crtc->config.dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004345 }
4346}
4347
Jesse Barnes89b667f2013-04-18 14:51:36 -07004348static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv)
4349{
4350 u32 reg_val;
4351
4352 /*
4353 * PLLB opamp always calibrates to max value of 0x3f, force enable it
4354 * and set it to a reasonable value instead.
4355 */
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;
4358 reg_val |= 0x00000030;
Jani Nikulaae992582013-05-22 15:36:19 +03004359 vlv_dpio_write(dev_priv, DPIO_IREF(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004360
Jani Nikulaae992582013-05-22 15:36:19 +03004361 reg_val = vlv_dpio_read(dev_priv, DPIO_CALIBRATION);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004362 reg_val &= 0x8cffffff;
4363 reg_val = 0x8c000000;
Jani Nikulaae992582013-05-22 15:36:19 +03004364 vlv_dpio_write(dev_priv, DPIO_CALIBRATION, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004365
Jani Nikulaae992582013-05-22 15:36:19 +03004366 reg_val = vlv_dpio_read(dev_priv, DPIO_IREF(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004367 reg_val &= 0xffffff00;
Jani Nikulaae992582013-05-22 15:36:19 +03004368 vlv_dpio_write(dev_priv, DPIO_IREF(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004369
Jani Nikulaae992582013-05-22 15:36:19 +03004370 reg_val = vlv_dpio_read(dev_priv, DPIO_CALIBRATION);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004371 reg_val &= 0x00ffffff;
4372 reg_val |= 0xb0000000;
Jani Nikulaae992582013-05-22 15:36:19 +03004373 vlv_dpio_write(dev_priv, DPIO_CALIBRATION, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004374}
4375
Daniel Vetterb5518422013-05-03 11:49:48 +02004376static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
4377 struct intel_link_m_n *m_n)
4378{
4379 struct drm_device *dev = crtc->base.dev;
4380 struct drm_i915_private *dev_priv = dev->dev_private;
4381 int pipe = crtc->pipe;
4382
Daniel Vettere3b95f12013-05-03 11:49:49 +02004383 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4384 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
4385 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
4386 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02004387}
4388
4389static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
4390 struct intel_link_m_n *m_n)
4391{
4392 struct drm_device *dev = crtc->base.dev;
4393 struct drm_i915_private *dev_priv = dev->dev_private;
4394 int pipe = crtc->pipe;
4395 enum transcoder transcoder = crtc->config.cpu_transcoder;
4396
4397 if (INTEL_INFO(dev)->gen >= 5) {
4398 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
4399 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
4400 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
4401 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
4402 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02004403 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4404 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
4405 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
4406 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02004407 }
4408}
4409
Daniel Vetter03afc4a2013-04-02 23:42:31 +02004410static void intel_dp_set_m_n(struct intel_crtc *crtc)
4411{
4412 if (crtc->config.has_pch_encoder)
4413 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4414 else
4415 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4416}
4417
Daniel Vetterf47709a2013-03-28 10:42:02 +01004418static void vlv_update_pll(struct intel_crtc *crtc)
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004419{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004420 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004421 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004422 int pipe = crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004423 u32 dpll, mdiv;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004424 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004425 bool is_hdmi;
Daniel Vetter198a037f2013-04-19 11:14:37 +02004426 u32 coreclk, reg_val, dpll_md;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004427
Daniel Vetter09153002012-12-12 14:06:44 +01004428 mutex_lock(&dev_priv->dpio_lock);
4429
Jesse Barnes89b667f2013-04-18 14:51:36 -07004430 is_hdmi = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004431
Daniel Vetterf47709a2013-03-28 10:42:02 +01004432 bestn = crtc->config.dpll.n;
4433 bestm1 = crtc->config.dpll.m1;
4434 bestm2 = crtc->config.dpll.m2;
4435 bestp1 = crtc->config.dpll.p1;
4436 bestp2 = crtc->config.dpll.p2;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004437
Jesse Barnes89b667f2013-04-18 14:51:36 -07004438 /* See eDP HDMI DPIO driver vbios notes doc */
4439
4440 /* PLL B needs special handling */
4441 if (pipe)
4442 vlv_pllb_recal_opamp(dev_priv);
4443
4444 /* Set up Tx target for periodic Rcomp update */
Jani Nikulaae992582013-05-22 15:36:19 +03004445 vlv_dpio_write(dev_priv, DPIO_IREF_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004446
4447 /* Disable target IRef on PLL */
Jani Nikulaae992582013-05-22 15:36:19 +03004448 reg_val = vlv_dpio_read(dev_priv, DPIO_IREF_CTL(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004449 reg_val &= 0x00ffffff;
Jani Nikulaae992582013-05-22 15:36:19 +03004450 vlv_dpio_write(dev_priv, DPIO_IREF_CTL(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004451
4452 /* Disable fast lock */
Jani Nikulaae992582013-05-22 15:36:19 +03004453 vlv_dpio_write(dev_priv, DPIO_FASTCLK_DISABLE, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004454
4455 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004456 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
4457 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
4458 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004459 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07004460
4461 /*
4462 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
4463 * but we don't support that).
4464 * Note: don't use the DAC post divider as it seems unstable.
4465 */
4466 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Jani Nikulaae992582013-05-22 15:36:19 +03004467 vlv_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004468
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004469 mdiv |= DPIO_ENABLE_CALIBRATION;
Jani Nikulaae992582013-05-22 15:36:19 +03004470 vlv_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004471
Jesse Barnes89b667f2013-04-18 14:51:36 -07004472 /* Set HBR and RBR LPF coefficients */
Daniel Vetterff9a6752013-06-01 17:16:21 +02004473 if (crtc->config.port_clock == 162000 ||
Ville Syrjälä99750bd2013-06-14 14:02:52 +03004474 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
Jesse Barnes89b667f2013-04-18 14:51:36 -07004475 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
Ville Syrjälä4abb2c32013-06-14 14:02:53 +03004476 vlv_dpio_write(dev_priv, DPIO_LPF_COEFF(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03004477 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004478 else
Ville Syrjälä4abb2c32013-06-14 14:02:53 +03004479 vlv_dpio_write(dev_priv, DPIO_LPF_COEFF(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004480 0x00d0000f);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004481
Jesse Barnes89b667f2013-04-18 14:51:36 -07004482 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
4483 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
4484 /* Use SSC source */
4485 if (!pipe)
Jani Nikulaae992582013-05-22 15:36:19 +03004486 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004487 0x0df40000);
4488 else
Jani Nikulaae992582013-05-22 15:36:19 +03004489 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004490 0x0df70000);
4491 } else { /* HDMI or VGA */
4492 /* Use bend source */
4493 if (!pipe)
Jani Nikulaae992582013-05-22 15:36:19 +03004494 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004495 0x0df70000);
4496 else
Jani Nikulaae992582013-05-22 15:36:19 +03004497 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004498 0x0df40000);
4499 }
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004500
Jani Nikulaae992582013-05-22 15:36:19 +03004501 coreclk = vlv_dpio_read(dev_priv, DPIO_CORE_CLK(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004502 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
4503 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
4504 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
4505 coreclk |= 0x01000000;
Jani Nikulaae992582013-05-22 15:36:19 +03004506 vlv_dpio_write(dev_priv, DPIO_CORE_CLK(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004507
Jani Nikulaae992582013-05-22 15:36:19 +03004508 vlv_dpio_write(dev_priv, DPIO_PLL_CML(pipe), 0x87871000);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004509
Jesse Barnes89b667f2013-04-18 14:51:36 -07004510 /* Enable DPIO clock input */
4511 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
4512 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
4513 if (pipe)
4514 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004515
4516 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004517 crtc->config.dpll_hw_state.dpll = dpll;
4518
Daniel Vetteref1b4602013-06-01 17:17:04 +02004519 dpll_md = (crtc->config.pixel_multiplier - 1)
4520 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004521 crtc->config.dpll_hw_state.dpll_md = dpll_md;
4522
Daniel Vetterf47709a2013-03-28 10:42:02 +01004523 if (crtc->config.has_dp_encoder)
4524 intel_dp_set_m_n(crtc);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304525
Daniel Vetter09153002012-12-12 14:06:44 +01004526 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004527}
4528
Daniel Vetterf47709a2013-03-28 10:42:02 +01004529static void i9xx_update_pll(struct intel_crtc *crtc,
4530 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004531 int num_connectors)
4532{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004533 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004534 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004535 u32 dpll;
4536 bool is_sdvo;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004537 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004538
Daniel Vetterf47709a2013-03-28 10:42:02 +01004539 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304540
Daniel Vetterf47709a2013-03-28 10:42:02 +01004541 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
4542 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004543
4544 dpll = DPLL_VGA_MODE_DIS;
4545
Daniel Vetterf47709a2013-03-28 10:42:02 +01004546 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004547 dpll |= DPLLB_MODE_LVDS;
4548 else
4549 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01004550
Daniel Vetteref1b4602013-06-01 17:17:04 +02004551 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Daniel Vetter198a037f2013-04-19 11:14:37 +02004552 dpll |= (crtc->config.pixel_multiplier - 1)
4553 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004554 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02004555
4556 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02004557 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02004558
Daniel Vetterf47709a2013-03-28 10:42:02 +01004559 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
Daniel Vetter4a33e482013-07-06 12:52:05 +02004560 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004561
4562 /* compute bitmask from p1 value */
4563 if (IS_PINEVIEW(dev))
4564 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
4565 else {
4566 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4567 if (IS_G4X(dev) && reduced_clock)
4568 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
4569 }
4570 switch (clock->p2) {
4571 case 5:
4572 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4573 break;
4574 case 7:
4575 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4576 break;
4577 case 10:
4578 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4579 break;
4580 case 14:
4581 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4582 break;
4583 }
4584 if (INTEL_INFO(dev)->gen >= 4)
4585 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
4586
Daniel Vetter09ede542013-04-30 14:01:45 +02004587 if (crtc->config.sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004588 dpll |= PLL_REF_INPUT_TVCLKINBC;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004589 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004590 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4591 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4592 else
4593 dpll |= PLL_REF_INPUT_DREFCLK;
4594
4595 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004596 crtc->config.dpll_hw_state.dpll = dpll;
4597
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004598 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetteref1b4602013-06-01 17:17:04 +02004599 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
4600 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004601 crtc->config.dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004602 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02004603
4604 if (crtc->config.has_dp_encoder)
4605 intel_dp_set_m_n(crtc);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004606}
4607
Daniel Vetterf47709a2013-03-28 10:42:02 +01004608static void i8xx_update_pll(struct intel_crtc *crtc,
Daniel Vetterf47709a2013-03-28 10:42:02 +01004609 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004610 int num_connectors)
4611{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004612 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004613 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004614 u32 dpll;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004615 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004616
Daniel Vetterf47709a2013-03-28 10:42:02 +01004617 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304618
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004619 dpll = DPLL_VGA_MODE_DIS;
4620
Daniel Vetterf47709a2013-03-28 10:42:02 +01004621 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004622 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4623 } else {
4624 if (clock->p1 == 2)
4625 dpll |= PLL_P1_DIVIDE_BY_TWO;
4626 else
4627 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4628 if (clock->p2 == 4)
4629 dpll |= PLL_P2_DIVIDE_BY_4;
4630 }
4631
Daniel Vetter4a33e482013-07-06 12:52:05 +02004632 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
4633 dpll |= DPLL_DVO_2X_MODE;
4634
Daniel Vetterf47709a2013-03-28 10:42:02 +01004635 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004636 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4637 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4638 else
4639 dpll |= PLL_REF_INPUT_DREFCLK;
4640
4641 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004642 crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004643}
4644
Daniel Vetter8a654f32013-06-01 17:16:22 +02004645static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004646{
4647 struct drm_device *dev = intel_crtc->base.dev;
4648 struct drm_i915_private *dev_priv = dev->dev_private;
4649 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02004650 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02004651 struct drm_display_mode *adjusted_mode =
4652 &intel_crtc->config.adjusted_mode;
4653 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004654 uint32_t vsyncshift, crtc_vtotal, crtc_vblank_end;
4655
4656 /* We need to be careful not to changed the adjusted mode, for otherwise
4657 * the hw state checker will get angry at the mismatch. */
4658 crtc_vtotal = adjusted_mode->crtc_vtotal;
4659 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004660
4661 if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4662 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004663 crtc_vtotal -= 1;
4664 crtc_vblank_end -= 1;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004665 vsyncshift = adjusted_mode->crtc_hsync_start
4666 - adjusted_mode->crtc_htotal / 2;
4667 } else {
4668 vsyncshift = 0;
4669 }
4670
4671 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004672 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004673
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004674 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004675 (adjusted_mode->crtc_hdisplay - 1) |
4676 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004677 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004678 (adjusted_mode->crtc_hblank_start - 1) |
4679 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004680 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004681 (adjusted_mode->crtc_hsync_start - 1) |
4682 ((adjusted_mode->crtc_hsync_end - 1) << 16));
4683
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004684 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004685 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004686 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004687 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004688 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004689 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004690 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004691 (adjusted_mode->crtc_vsync_start - 1) |
4692 ((adjusted_mode->crtc_vsync_end - 1) << 16));
4693
Paulo Zanonib5e508d2012-10-24 11:34:43 -02004694 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
4695 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
4696 * documented on the DDI_FUNC_CTL register description, EDP Input Select
4697 * bits. */
4698 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
4699 (pipe == PIPE_B || pipe == PIPE_C))
4700 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
4701
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004702 /* pipesrc controls the size that is scaled from, which should
4703 * always be the user's requested size.
4704 */
4705 I915_WRITE(PIPESRC(pipe),
4706 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
4707}
4708
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02004709static void intel_get_pipe_timings(struct intel_crtc *crtc,
4710 struct intel_crtc_config *pipe_config)
4711{
4712 struct drm_device *dev = crtc->base.dev;
4713 struct drm_i915_private *dev_priv = dev->dev_private;
4714 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
4715 uint32_t tmp;
4716
4717 tmp = I915_READ(HTOTAL(cpu_transcoder));
4718 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
4719 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
4720 tmp = I915_READ(HBLANK(cpu_transcoder));
4721 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
4722 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
4723 tmp = I915_READ(HSYNC(cpu_transcoder));
4724 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
4725 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
4726
4727 tmp = I915_READ(VTOTAL(cpu_transcoder));
4728 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
4729 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
4730 tmp = I915_READ(VBLANK(cpu_transcoder));
4731 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
4732 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
4733 tmp = I915_READ(VSYNC(cpu_transcoder));
4734 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
4735 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
4736
4737 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
4738 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
4739 pipe_config->adjusted_mode.crtc_vtotal += 1;
4740 pipe_config->adjusted_mode.crtc_vblank_end += 1;
4741 }
4742
4743 tmp = I915_READ(PIPESRC(crtc->pipe));
4744 pipe_config->requested_mode.vdisplay = (tmp & 0xffff) + 1;
4745 pipe_config->requested_mode.hdisplay = ((tmp >> 16) & 0xffff) + 1;
4746}
4747
Jesse Barnesbabea612013-06-26 18:57:38 +03004748static void intel_crtc_mode_from_pipe_config(struct intel_crtc *intel_crtc,
4749 struct intel_crtc_config *pipe_config)
4750{
4751 struct drm_crtc *crtc = &intel_crtc->base;
4752
4753 crtc->mode.hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
4754 crtc->mode.htotal = pipe_config->adjusted_mode.crtc_htotal;
4755 crtc->mode.hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
4756 crtc->mode.hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
4757
4758 crtc->mode.vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
4759 crtc->mode.vtotal = pipe_config->adjusted_mode.crtc_vtotal;
4760 crtc->mode.vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
4761 crtc->mode.vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
4762
4763 crtc->mode.flags = pipe_config->adjusted_mode.flags;
4764
4765 crtc->mode.clock = pipe_config->adjusted_mode.clock;
4766 crtc->mode.flags |= pipe_config->adjusted_mode.flags;
4767}
4768
Daniel Vetter84b046f2013-02-19 18:48:54 +01004769static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
4770{
4771 struct drm_device *dev = intel_crtc->base.dev;
4772 struct drm_i915_private *dev_priv = dev->dev_private;
4773 uint32_t pipeconf;
4774
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02004775 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01004776
4777 if (intel_crtc->pipe == 0 && INTEL_INFO(dev)->gen < 4) {
4778 /* Enable pixel doubling when the dot clock is > 90% of the (display)
4779 * core speed.
4780 *
4781 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
4782 * pipe == 0 check?
4783 */
4784 if (intel_crtc->config.requested_mode.clock >
4785 dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
4786 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01004787 }
4788
Daniel Vetterff9ce462013-04-24 14:57:17 +02004789 /* only g4x and later have fancy bpc/dither controls */
4790 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02004791 /* Bspec claims that we can't use dithering for 30bpp pipes. */
4792 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
4793 pipeconf |= PIPECONF_DITHER_EN |
4794 PIPECONF_DITHER_TYPE_SP;
4795
4796 switch (intel_crtc->config.pipe_bpp) {
4797 case 18:
4798 pipeconf |= PIPECONF_6BPC;
4799 break;
4800 case 24:
4801 pipeconf |= PIPECONF_8BPC;
4802 break;
4803 case 30:
4804 pipeconf |= PIPECONF_10BPC;
4805 break;
4806 default:
4807 /* Case prevented by intel_choose_pipe_bpp_dither. */
4808 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01004809 }
4810 }
4811
4812 if (HAS_PIPE_CXSR(dev)) {
4813 if (intel_crtc->lowfreq_avail) {
4814 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
4815 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
4816 } else {
4817 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01004818 }
4819 }
4820
Daniel Vetter84b046f2013-02-19 18:48:54 +01004821 if (!IS_GEN2(dev) &&
4822 intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
4823 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
4824 else
4825 pipeconf |= PIPECONF_PROGRESSIVE;
4826
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02004827 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
4828 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03004829
Daniel Vetter84b046f2013-02-19 18:48:54 +01004830 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
4831 POSTING_READ(PIPECONF(intel_crtc->pipe));
4832}
4833
Eric Anholtf564048e2011-03-30 13:01:02 -07004834static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07004835 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02004836 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08004837{
4838 struct drm_device *dev = crtc->dev;
4839 struct drm_i915_private *dev_priv = dev->dev_private;
4840 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01004841 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08004842 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07004843 int plane = intel_crtc->plane;
Eric Anholtc751ce42010-03-25 11:48:48 -07004844 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07004845 intel_clock_t clock, reduced_clock;
Daniel Vetter84b046f2013-02-19 18:48:54 +01004846 u32 dspcntr;
Daniel Vettera16af7212013-04-30 14:01:44 +02004847 bool ok, has_reduced_clock = false;
4848 bool is_lvds = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01004849 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08004850 const intel_limit_t *limit;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00004851 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08004852
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02004853 for_each_encoder_on_crtc(dev, crtc, encoder) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004854 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004855 case INTEL_OUTPUT_LVDS:
4856 is_lvds = true;
4857 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08004858 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05004859
Eric Anholtc751ce42010-03-25 11:48:48 -07004860 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08004861 }
4862
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004863 refclk = i9xx_get_refclk(crtc, num_connectors);
Jesse Barnes79e53942008-11-07 14:24:08 -08004864
Ma Lingd4906092009-03-18 20:13:27 +08004865 /*
4866 * Returns a set of divisors for the desired target clock with the given
4867 * refclk, or FALSE. The returned values represent the clock equation:
4868 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
4869 */
Chris Wilson1b894b52010-12-14 20:04:54 +00004870 limit = intel_limit(crtc, refclk);
Daniel Vetterff9a6752013-06-01 17:16:21 +02004871 ok = dev_priv->display.find_dpll(limit, crtc,
4872 intel_crtc->config.port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02004873 refclk, NULL, &clock);
4874 if (!ok && !intel_crtc->config.clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004875 DRM_ERROR("Couldn't find PLL settings for mode!\n");
Eric Anholtf564048e2011-03-30 13:01:02 -07004876 return -EINVAL;
4877 }
4878
4879 /* Ensure that the cursor is valid for the new mode before changing... */
4880 intel_crtc_update_cursor(crtc, true);
4881
4882 if (is_lvds && dev_priv->lvds_downclock_avail) {
Sean Paulcec2f352012-01-10 15:09:36 -08004883 /*
4884 * Ensure we match the reduced clock's P to the target clock.
4885 * If the clocks don't match, we can't switch the display clock
4886 * by using the FP0/FP1. In such case we will disable the LVDS
4887 * downclock feature.
4888 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02004889 has_reduced_clock =
4890 dev_priv->display.find_dpll(limit, crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07004891 dev_priv->lvds_downclock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02004892 refclk, &clock,
Eric Anholtf564048e2011-03-30 13:01:02 -07004893 &reduced_clock);
Eric Anholtf564048e2011-03-30 13:01:02 -07004894 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01004895 /* Compat-code for transition, will disappear. */
4896 if (!intel_crtc->config.clock_set) {
4897 intel_crtc->config.dpll.n = clock.n;
4898 intel_crtc->config.dpll.m1 = clock.m1;
4899 intel_crtc->config.dpll.m2 = clock.m2;
4900 intel_crtc->config.dpll.p1 = clock.p1;
4901 intel_crtc->config.dpll.p2 = clock.p2;
4902 }
Eric Anholtf564048e2011-03-30 13:01:02 -07004903
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004904 if (IS_GEN2(dev))
Daniel Vetter8a654f32013-06-01 17:16:22 +02004905 i8xx_update_pll(intel_crtc,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304906 has_reduced_clock ? &reduced_clock : NULL,
4907 num_connectors);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004908 else if (IS_VALLEYVIEW(dev))
Daniel Vetterf47709a2013-03-28 10:42:02 +01004909 vlv_update_pll(intel_crtc);
Eric Anholtf564048e2011-03-30 13:01:02 -07004910 else
Daniel Vetterf47709a2013-03-28 10:42:02 +01004911 i9xx_update_pll(intel_crtc,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004912 has_reduced_clock ? &reduced_clock : NULL,
Jesse Barnes89b667f2013-04-18 14:51:36 -07004913 num_connectors);
Eric Anholtf564048e2011-03-30 13:01:02 -07004914
Eric Anholtf564048e2011-03-30 13:01:02 -07004915 /* Set up the display plane register */
4916 dspcntr = DISPPLANE_GAMMA_ENABLE;
4917
Jesse Barnesda6ecc52013-03-08 10:46:00 -08004918 if (!IS_VALLEYVIEW(dev)) {
4919 if (pipe == 0)
4920 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
4921 else
4922 dspcntr |= DISPPLANE_SEL_PIPE_B;
4923 }
Eric Anholtf564048e2011-03-30 13:01:02 -07004924
Daniel Vetter8a654f32013-06-01 17:16:22 +02004925 intel_set_pipe_timings(intel_crtc);
Eric Anholtf564048e2011-03-30 13:01:02 -07004926
4927 /* pipesrc and dspsize control the size that is scaled from,
4928 * which should always be the user's requested size.
4929 */
Eric Anholt929c77f2011-03-30 13:01:04 -07004930 I915_WRITE(DSPSIZE(plane),
4931 ((mode->vdisplay - 1) << 16) |
4932 (mode->hdisplay - 1));
4933 I915_WRITE(DSPPOS(plane), 0);
Eric Anholtf564048e2011-03-30 13:01:02 -07004934
Daniel Vetter84b046f2013-02-19 18:48:54 +01004935 i9xx_set_pipeconf(intel_crtc);
4936
Eric Anholtf564048e2011-03-30 13:01:02 -07004937 I915_WRITE(DSPCNTR(plane), dspcntr);
4938 POSTING_READ(DSPCNTR(plane));
4939
Daniel Vetter94352cf2012-07-05 22:51:56 +02004940 ret = intel_pipe_set_base(crtc, x, y, fb);
Eric Anholtf564048e2011-03-30 13:01:02 -07004941
4942 intel_update_watermarks(dev);
4943
Eric Anholtf564048e2011-03-30 13:01:02 -07004944 return ret;
4945}
4946
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02004947static void i9xx_get_pfit_config(struct intel_crtc *crtc,
4948 struct intel_crtc_config *pipe_config)
4949{
4950 struct drm_device *dev = crtc->base.dev;
4951 struct drm_i915_private *dev_priv = dev->dev_private;
4952 uint32_t tmp;
4953
4954 tmp = I915_READ(PFIT_CONTROL);
4955
4956 if (INTEL_INFO(dev)->gen < 4) {
4957 if (crtc->pipe != PIPE_B)
4958 return;
4959
4960 /* gen2/3 store dither state in pfit control, needs to match */
4961 pipe_config->gmch_pfit.control = tmp & PANEL_8TO6_DITHER_ENABLE;
4962 } else {
4963 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
4964 return;
4965 }
4966
4967 if (!(tmp & PFIT_ENABLE))
4968 return;
4969
4970 pipe_config->gmch_pfit.control = I915_READ(PFIT_CONTROL);
4971 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
4972 if (INTEL_INFO(dev)->gen < 5)
4973 pipe_config->gmch_pfit.lvds_border_bits =
4974 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
4975}
4976
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01004977static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
4978 struct intel_crtc_config *pipe_config)
4979{
4980 struct drm_device *dev = crtc->base.dev;
4981 struct drm_i915_private *dev_priv = dev->dev_private;
4982 uint32_t tmp;
4983
Daniel Vettere143a212013-07-04 12:01:15 +02004984 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02004985 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02004986
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01004987 tmp = I915_READ(PIPECONF(crtc->pipe));
4988 if (!(tmp & PIPECONF_ENABLE))
4989 return false;
4990
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02004991 intel_get_pipe_timings(crtc, pipe_config);
4992
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02004993 i9xx_get_pfit_config(crtc, pipe_config);
4994
Daniel Vetter6c49f242013-06-06 12:45:25 +02004995 if (INTEL_INFO(dev)->gen >= 4) {
4996 tmp = I915_READ(DPLL_MD(crtc->pipe));
4997 pipe_config->pixel_multiplier =
4998 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
4999 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005000 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02005001 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
5002 tmp = I915_READ(DPLL(crtc->pipe));
5003 pipe_config->pixel_multiplier =
5004 ((tmp & SDVO_MULTIPLIER_MASK)
5005 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
5006 } else {
5007 /* Note that on i915G/GM the pixel multiplier is in the sdvo
5008 * port and will be fixed up in the encoder->get_config
5009 * function. */
5010 pipe_config->pixel_multiplier = 1;
5011 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005012 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
5013 if (!IS_VALLEYVIEW(dev)) {
5014 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
5015 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03005016 } else {
5017 /* Mask out read-only status bits. */
5018 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
5019 DPLL_PORTC_READY_MASK |
5020 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005021 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02005022
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005023 return true;
5024}
5025
Paulo Zanonidde86e22012-12-01 12:04:25 -02005026static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07005027{
5028 struct drm_i915_private *dev_priv = dev->dev_private;
5029 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005030 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005031 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005032 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005033 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005034 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07005035 bool has_ck505 = false;
5036 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005037
5038 /* We need to take the global config into account */
Keith Packard199e5d72011-09-22 12:01:57 -07005039 list_for_each_entry(encoder, &mode_config->encoder_list,
5040 base.head) {
5041 switch (encoder->type) {
5042 case INTEL_OUTPUT_LVDS:
5043 has_panel = true;
5044 has_lvds = true;
5045 break;
5046 case INTEL_OUTPUT_EDP:
5047 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03005048 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07005049 has_cpu_edp = true;
5050 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005051 }
5052 }
5053
Keith Packard99eb6a02011-09-26 14:29:12 -07005054 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005055 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07005056 can_ssc = has_ck505;
5057 } else {
5058 has_ck505 = false;
5059 can_ssc = true;
5060 }
5061
Imre Deak2de69052013-05-08 13:14:04 +03005062 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
5063 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005064
5065 /* Ironlake: try to setup display ref clock before DPLL
5066 * enabling. This is only under driver's control after
5067 * PCH B stepping, previous chipset stepping should be
5068 * ignoring this setting.
5069 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005070 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005071
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005072 /* As we must carefully and slowly disable/enable each source in turn,
5073 * compute the final state we want first and check if we need to
5074 * make any changes at all.
5075 */
5076 final = val;
5077 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07005078 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005079 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07005080 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005081 final |= DREF_NONSPREAD_SOURCE_ENABLE;
5082
5083 final &= ~DREF_SSC_SOURCE_MASK;
5084 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5085 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005086
Keith Packard199e5d72011-09-22 12:01:57 -07005087 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005088 final |= DREF_SSC_SOURCE_ENABLE;
5089
5090 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5091 final |= DREF_SSC1_ENABLE;
5092
5093 if (has_cpu_edp) {
5094 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5095 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
5096 else
5097 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
5098 } else
5099 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5100 } else {
5101 final |= DREF_SSC_SOURCE_DISABLE;
5102 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5103 }
5104
5105 if (final == val)
5106 return;
5107
5108 /* Always enable nonspread source */
5109 val &= ~DREF_NONSPREAD_SOURCE_MASK;
5110
5111 if (has_ck505)
5112 val |= DREF_NONSPREAD_CK505_ENABLE;
5113 else
5114 val |= DREF_NONSPREAD_SOURCE_ENABLE;
5115
5116 if (has_panel) {
5117 val &= ~DREF_SSC_SOURCE_MASK;
5118 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005119
Keith Packard199e5d72011-09-22 12:01:57 -07005120 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07005121 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005122 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005123 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02005124 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005125 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005126
5127 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005128 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005129 POSTING_READ(PCH_DREF_CONTROL);
5130 udelay(200);
5131
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005132 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005133
5134 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07005135 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07005136 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005137 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005138 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005139 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07005140 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005141 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005142 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005143 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005144
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005145 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005146 POSTING_READ(PCH_DREF_CONTROL);
5147 udelay(200);
5148 } else {
5149 DRM_DEBUG_KMS("Disabling SSC entirely\n");
5150
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005151 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07005152
5153 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005154 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005155
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005156 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005157 POSTING_READ(PCH_DREF_CONTROL);
5158 udelay(200);
5159
5160 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005161 val &= ~DREF_SSC_SOURCE_MASK;
5162 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005163
5164 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005165 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005166
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005167 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005168 POSTING_READ(PCH_DREF_CONTROL);
5169 udelay(200);
5170 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005171
5172 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005173}
5174
Paulo Zanonidde86e22012-12-01 12:04:25 -02005175/* Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O. */
5176static void lpt_init_pch_refclk(struct drm_device *dev)
5177{
5178 struct drm_i915_private *dev_priv = dev->dev_private;
5179 struct drm_mode_config *mode_config = &dev->mode_config;
5180 struct intel_encoder *encoder;
5181 bool has_vga = false;
5182 bool is_sdv = false;
5183 u32 tmp;
5184
5185 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5186 switch (encoder->type) {
5187 case INTEL_OUTPUT_ANALOG:
5188 has_vga = true;
5189 break;
5190 }
5191 }
5192
5193 if (!has_vga)
5194 return;
5195
Daniel Vetterc00db242013-01-22 15:33:27 +01005196 mutex_lock(&dev_priv->dpio_lock);
5197
Paulo Zanonidde86e22012-12-01 12:04:25 -02005198 /* XXX: Rip out SDV support once Haswell ships for real. */
5199 if (IS_HASWELL(dev) && (dev->pci_device & 0xFF00) == 0x0C00)
5200 is_sdv = true;
5201
5202 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5203 tmp &= ~SBI_SSCCTL_DISABLE;
5204 tmp |= SBI_SSCCTL_PATHALT;
5205 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5206
5207 udelay(24);
5208
5209 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5210 tmp &= ~SBI_SSCCTL_PATHALT;
5211 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5212
5213 if (!is_sdv) {
5214 tmp = I915_READ(SOUTH_CHICKEN2);
5215 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
5216 I915_WRITE(SOUTH_CHICKEN2, tmp);
5217
5218 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
5219 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
5220 DRM_ERROR("FDI mPHY reset assert timeout\n");
5221
5222 tmp = I915_READ(SOUTH_CHICKEN2);
5223 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
5224 I915_WRITE(SOUTH_CHICKEN2, tmp);
5225
5226 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
5227 FDI_MPHY_IOSFSB_RESET_STATUS) == 0,
5228 100))
5229 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
5230 }
5231
5232 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
5233 tmp &= ~(0xFF << 24);
5234 tmp |= (0x12 << 24);
5235 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
5236
Paulo Zanonidde86e22012-12-01 12:04:25 -02005237 if (is_sdv) {
5238 tmp = intel_sbi_read(dev_priv, 0x800C, SBI_MPHY);
5239 tmp |= 0x7FFF;
5240 intel_sbi_write(dev_priv, 0x800C, tmp, SBI_MPHY);
5241 }
5242
5243 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
5244 tmp |= (1 << 11);
5245 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
5246
5247 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
5248 tmp |= (1 << 11);
5249 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
5250
5251 if (is_sdv) {
5252 tmp = intel_sbi_read(dev_priv, 0x2038, SBI_MPHY);
5253 tmp |= (0x3F << 24) | (0xF << 20) | (0xF << 16);
5254 intel_sbi_write(dev_priv, 0x2038, tmp, SBI_MPHY);
5255
5256 tmp = intel_sbi_read(dev_priv, 0x2138, SBI_MPHY);
5257 tmp |= (0x3F << 24) | (0xF << 20) | (0xF << 16);
5258 intel_sbi_write(dev_priv, 0x2138, tmp, SBI_MPHY);
5259
5260 tmp = intel_sbi_read(dev_priv, 0x203C, SBI_MPHY);
5261 tmp |= (0x3F << 8);
5262 intel_sbi_write(dev_priv, 0x203C, tmp, SBI_MPHY);
5263
5264 tmp = intel_sbi_read(dev_priv, 0x213C, SBI_MPHY);
5265 tmp |= (0x3F << 8);
5266 intel_sbi_write(dev_priv, 0x213C, tmp, SBI_MPHY);
5267 }
5268
5269 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
5270 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5271 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
5272
5273 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
5274 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5275 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
5276
5277 if (!is_sdv) {
5278 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
5279 tmp &= ~(7 << 13);
5280 tmp |= (5 << 13);
5281 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
5282
5283 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
5284 tmp &= ~(7 << 13);
5285 tmp |= (5 << 13);
5286 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
5287 }
5288
5289 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
5290 tmp &= ~0xFF;
5291 tmp |= 0x1C;
5292 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
5293
5294 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
5295 tmp &= ~0xFF;
5296 tmp |= 0x1C;
5297 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
5298
5299 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
5300 tmp &= ~(0xFF << 16);
5301 tmp |= (0x1C << 16);
5302 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
5303
5304 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
5305 tmp &= ~(0xFF << 16);
5306 tmp |= (0x1C << 16);
5307 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
5308
5309 if (!is_sdv) {
5310 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
5311 tmp |= (1 << 27);
5312 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
5313
5314 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
5315 tmp |= (1 << 27);
5316 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
5317
5318 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
5319 tmp &= ~(0xF << 28);
5320 tmp |= (4 << 28);
5321 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
5322
5323 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
5324 tmp &= ~(0xF << 28);
5325 tmp |= (4 << 28);
5326 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
5327 }
5328
5329 /* ULT uses SBI_GEN0, but ULT doesn't have VGA, so we don't care. */
5330 tmp = intel_sbi_read(dev_priv, SBI_DBUFF0, SBI_ICLK);
5331 tmp |= SBI_DBUFF0_ENABLE;
5332 intel_sbi_write(dev_priv, SBI_DBUFF0, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01005333
5334 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005335}
5336
5337/*
5338 * Initialize reference clocks when the driver loads
5339 */
5340void intel_init_pch_refclk(struct drm_device *dev)
5341{
5342 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
5343 ironlake_init_pch_refclk(dev);
5344 else if (HAS_PCH_LPT(dev))
5345 lpt_init_pch_refclk(dev);
5346}
5347
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005348static int ironlake_get_refclk(struct drm_crtc *crtc)
5349{
5350 struct drm_device *dev = crtc->dev;
5351 struct drm_i915_private *dev_priv = dev->dev_private;
5352 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005353 int num_connectors = 0;
5354 bool is_lvds = false;
5355
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02005356 for_each_encoder_on_crtc(dev, crtc, encoder) {
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005357 switch (encoder->type) {
5358 case INTEL_OUTPUT_LVDS:
5359 is_lvds = true;
5360 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005361 }
5362 num_connectors++;
5363 }
5364
5365 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
5366 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005367 dev_priv->vbt.lvds_ssc_freq);
5368 return dev_priv->vbt.lvds_ssc_freq * 1000;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005369 }
5370
5371 return 120000;
5372}
5373
Daniel Vetter6ff93602013-04-19 11:24:36 +02005374static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03005375{
5376 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5377 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5378 int pipe = intel_crtc->pipe;
5379 uint32_t val;
5380
Daniel Vetter78114072013-06-13 00:54:57 +02005381 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03005382
Daniel Vetter965e0c42013-03-27 00:44:57 +01005383 switch (intel_crtc->config.pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03005384 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005385 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005386 break;
5387 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005388 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005389 break;
5390 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005391 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005392 break;
5393 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005394 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005395 break;
5396 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03005397 /* Case prevented by intel_choose_pipe_bpp_dither. */
5398 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03005399 }
5400
Daniel Vetterd8b32242013-04-25 17:54:44 +02005401 if (intel_crtc->config.dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03005402 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5403
Daniel Vetter6ff93602013-04-19 11:24:36 +02005404 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03005405 val |= PIPECONF_INTERLACED_ILK;
5406 else
5407 val |= PIPECONF_PROGRESSIVE;
5408
Daniel Vetter50f3b012013-03-27 00:44:56 +01005409 if (intel_crtc->config.limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005410 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005411
Paulo Zanonic8203562012-09-12 10:06:29 -03005412 I915_WRITE(PIPECONF(pipe), val);
5413 POSTING_READ(PIPECONF(pipe));
5414}
5415
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005416/*
5417 * Set up the pipe CSC unit.
5418 *
5419 * Currently only full range RGB to limited range RGB conversion
5420 * is supported, but eventually this should handle various
5421 * RGB<->YCbCr scenarios as well.
5422 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01005423static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005424{
5425 struct drm_device *dev = crtc->dev;
5426 struct drm_i915_private *dev_priv = dev->dev_private;
5427 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5428 int pipe = intel_crtc->pipe;
5429 uint16_t coeff = 0x7800; /* 1.0 */
5430
5431 /*
5432 * TODO: Check what kind of values actually come out of the pipe
5433 * with these coeff/postoff values and adjust to get the best
5434 * accuracy. Perhaps we even need to take the bpc value into
5435 * consideration.
5436 */
5437
Daniel Vetter50f3b012013-03-27 00:44:56 +01005438 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005439 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
5440
5441 /*
5442 * GY/GU and RY/RU should be the other way around according
5443 * to BSpec, but reality doesn't agree. Just set them up in
5444 * a way that results in the correct picture.
5445 */
5446 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
5447 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
5448
5449 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
5450 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
5451
5452 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
5453 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
5454
5455 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
5456 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
5457 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
5458
5459 if (INTEL_INFO(dev)->gen > 6) {
5460 uint16_t postoff = 0;
5461
Daniel Vetter50f3b012013-03-27 00:44:56 +01005462 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005463 postoff = (16 * (1 << 13) / 255) & 0x1fff;
5464
5465 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
5466 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
5467 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
5468
5469 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
5470 } else {
5471 uint32_t mode = CSC_MODE_YUV_TO_RGB;
5472
Daniel Vetter50f3b012013-03-27 00:44:56 +01005473 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005474 mode |= CSC_BLACK_SCREEN_OFFSET;
5475
5476 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
5477 }
5478}
5479
Daniel Vetter6ff93602013-04-19 11:24:36 +02005480static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005481{
5482 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5483 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02005484 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005485 uint32_t val;
5486
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02005487 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005488
Daniel Vetterd8b32242013-04-25 17:54:44 +02005489 if (intel_crtc->config.dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005490 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5491
Daniel Vetter6ff93602013-04-19 11:24:36 +02005492 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005493 val |= PIPECONF_INTERLACED_ILK;
5494 else
5495 val |= PIPECONF_PROGRESSIVE;
5496
Paulo Zanoni702e7a52012-10-23 18:29:59 -02005497 I915_WRITE(PIPECONF(cpu_transcoder), val);
5498 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02005499
5500 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
5501 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005502}
5503
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005504static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005505 intel_clock_t *clock,
5506 bool *has_reduced_clock,
5507 intel_clock_t *reduced_clock)
5508{
5509 struct drm_device *dev = crtc->dev;
5510 struct drm_i915_private *dev_priv = dev->dev_private;
5511 struct intel_encoder *intel_encoder;
5512 int refclk;
5513 const intel_limit_t *limit;
Daniel Vettera16af7212013-04-30 14:01:44 +02005514 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005515
5516 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5517 switch (intel_encoder->type) {
5518 case INTEL_OUTPUT_LVDS:
5519 is_lvds = true;
5520 break;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005521 }
5522 }
5523
5524 refclk = ironlake_get_refclk(crtc);
5525
5526 /*
5527 * Returns a set of divisors for the desired target clock with the given
5528 * refclk, or FALSE. The returned values represent the clock equation:
5529 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
5530 */
5531 limit = intel_limit(crtc, refclk);
Daniel Vetterff9a6752013-06-01 17:16:21 +02005532 ret = dev_priv->display.find_dpll(limit, crtc,
5533 to_intel_crtc(crtc)->config.port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02005534 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005535 if (!ret)
5536 return false;
5537
5538 if (is_lvds && dev_priv->lvds_downclock_avail) {
5539 /*
5540 * Ensure we match the reduced clock's P to the target clock.
5541 * If the clocks don't match, we can't switch the display clock
5542 * by using the FP0/FP1. In such case we will disable the LVDS
5543 * downclock feature.
5544 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02005545 *has_reduced_clock =
5546 dev_priv->display.find_dpll(limit, crtc,
5547 dev_priv->lvds_downclock,
5548 refclk, clock,
5549 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005550 }
5551
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005552 return true;
5553}
5554
Daniel Vetter01a415f2012-10-27 15:58:40 +02005555static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
5556{
5557 struct drm_i915_private *dev_priv = dev->dev_private;
5558 uint32_t temp;
5559
5560 temp = I915_READ(SOUTH_CHICKEN1);
5561 if (temp & FDI_BC_BIFURCATION_SELECT)
5562 return;
5563
5564 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
5565 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
5566
5567 temp |= FDI_BC_BIFURCATION_SELECT;
5568 DRM_DEBUG_KMS("enabling fdi C rx\n");
5569 I915_WRITE(SOUTH_CHICKEN1, temp);
5570 POSTING_READ(SOUTH_CHICKEN1);
5571}
5572
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005573static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
Daniel Vetter01a415f2012-10-27 15:58:40 +02005574{
5575 struct drm_device *dev = intel_crtc->base.dev;
5576 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005577
5578 switch (intel_crtc->pipe) {
5579 case PIPE_A:
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005580 break;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005581 case PIPE_B:
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005582 if (intel_crtc->config.fdi_lanes > 2)
Daniel Vetter01a415f2012-10-27 15:58:40 +02005583 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
5584 else
5585 cpt_enable_fdi_bc_bifurcation(dev);
5586
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005587 break;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005588 case PIPE_C:
Daniel Vetter01a415f2012-10-27 15:58:40 +02005589 cpt_enable_fdi_bc_bifurcation(dev);
5590
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005591 break;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005592 default:
5593 BUG();
5594 }
5595}
5596
Paulo Zanonid4b19312012-11-29 11:29:32 -02005597int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
5598{
5599 /*
5600 * Account for spread spectrum to avoid
5601 * oversubscribing the link. Max center spread
5602 * is 2.5%; use 5% for safety's sake.
5603 */
5604 u32 bps = target_clock * bpp * 21 / 20;
5605 return bps / (link_bw * 8) + 1;
5606}
5607
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005608static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02005609{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005610 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005611}
5612
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005613static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005614 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02005615 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005616{
5617 struct drm_crtc *crtc = &intel_crtc->base;
5618 struct drm_device *dev = crtc->dev;
5619 struct drm_i915_private *dev_priv = dev->dev_private;
5620 struct intel_encoder *intel_encoder;
5621 uint32_t dpll;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01005622 int factor, num_connectors = 0;
Daniel Vetter09ede542013-04-30 14:01:45 +02005623 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005624
5625 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5626 switch (intel_encoder->type) {
5627 case INTEL_OUTPUT_LVDS:
5628 is_lvds = true;
5629 break;
5630 case INTEL_OUTPUT_SDVO:
5631 case INTEL_OUTPUT_HDMI:
5632 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005633 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005634 }
5635
5636 num_connectors++;
5637 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005638
Chris Wilsonc1858122010-12-03 21:35:48 +00005639 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07005640 factor = 21;
5641 if (is_lvds) {
5642 if ((intel_panel_use_ssc(dev_priv) &&
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005643 dev_priv->vbt.lvds_ssc_freq == 100) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02005644 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07005645 factor = 25;
Daniel Vetter09ede542013-04-30 14:01:45 +02005646 } else if (intel_crtc->config.sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07005647 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00005648
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005649 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02005650 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00005651
Daniel Vetter9a7c7892013-04-04 22:20:34 +02005652 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
5653 *fp2 |= FP_CB_TUNE;
5654
Chris Wilson5eddb702010-09-11 13:48:45 +01005655 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005656
Eric Anholta07d6782011-03-30 13:01:08 -07005657 if (is_lvds)
5658 dpll |= DPLLB_MODE_LVDS;
5659 else
5660 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02005661
Daniel Vetteref1b4602013-06-01 17:17:04 +02005662 dpll |= (intel_crtc->config.pixel_multiplier - 1)
5663 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02005664
5665 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02005666 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter9566e9a2013-04-19 11:14:36 +02005667 if (intel_crtc->config.has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02005668 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08005669
Eric Anholta07d6782011-03-30 13:01:08 -07005670 /* compute bitmask from p1 value */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005671 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005672 /* also FPA1 */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005673 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005674
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005675 switch (intel_crtc->config.dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07005676 case 5:
5677 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5678 break;
5679 case 7:
5680 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5681 break;
5682 case 10:
5683 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5684 break;
5685 case 14:
5686 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5687 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005688 }
5689
Daniel Vetterb4c09f32013-04-30 14:01:42 +02005690 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005691 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08005692 else
5693 dpll |= PLL_REF_INPUT_DREFCLK;
5694
Daniel Vetter959e16d2013-06-05 13:34:21 +02005695 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005696}
5697
Jesse Barnes79e53942008-11-07 14:24:08 -08005698static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
Jesse Barnes79e53942008-11-07 14:24:08 -08005699 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005700 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005701{
5702 struct drm_device *dev = crtc->dev;
5703 struct drm_i915_private *dev_priv = dev->dev_private;
5704 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5705 int pipe = intel_crtc->pipe;
5706 int plane = intel_crtc->plane;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005707 int num_connectors = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005708 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02005709 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03005710 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01005711 bool is_lvds = false;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005712 struct intel_encoder *encoder;
Daniel Vettere2b78262013-06-07 23:10:03 +02005713 struct intel_shared_dpll *pll;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005714 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08005715
5716 for_each_encoder_on_crtc(dev, crtc, encoder) {
5717 switch (encoder->type) {
5718 case INTEL_OUTPUT_LVDS:
5719 is_lvds = true;
5720 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005721 }
5722
5723 num_connectors++;
5724 }
5725
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005726 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
5727 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
5728
Daniel Vetterff9a6752013-06-01 17:16:21 +02005729 ok = ironlake_compute_clocks(crtc, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005730 &has_reduced_clock, &reduced_clock);
Daniel Vetteree9300b2013-06-03 22:40:22 +02005731 if (!ok && !intel_crtc->config.clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08005732 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5733 return -EINVAL;
5734 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01005735 /* Compat-code for transition, will disappear. */
5736 if (!intel_crtc->config.clock_set) {
5737 intel_crtc->config.dpll.n = clock.n;
5738 intel_crtc->config.dpll.m1 = clock.m1;
5739 intel_crtc->config.dpll.m2 = clock.m2;
5740 intel_crtc->config.dpll.p1 = clock.p1;
5741 intel_crtc->config.dpll.p2 = clock.p2;
5742 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005743
5744 /* Ensure that the cursor is valid for the new mode before changing... */
5745 intel_crtc_update_cursor(crtc, true);
5746
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005747 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
Daniel Vetter8b470472013-03-28 10:41:59 +01005748 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005749 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02005750 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005751 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02005752
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005753 dpll = ironlake_compute_dpll(intel_crtc,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02005754 &fp, &reduced_clock,
5755 has_reduced_clock ? &fp2 : NULL);
5756
Daniel Vetter959e16d2013-06-05 13:34:21 +02005757 intel_crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02005758 intel_crtc->config.dpll_hw_state.fp0 = fp;
5759 if (has_reduced_clock)
5760 intel_crtc->config.dpll_hw_state.fp1 = fp2;
5761 else
5762 intel_crtc->config.dpll_hw_state.fp1 = fp;
5763
Daniel Vetterb89a1d32013-06-05 13:34:24 +02005764 pll = intel_get_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005765 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03005766 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
5767 pipe_name(pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07005768 return -EINVAL;
5769 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005770 } else
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005771 intel_put_shared_dpll(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08005772
Daniel Vetter03afc4a2013-04-02 23:42:31 +02005773 if (intel_crtc->config.has_dp_encoder)
5774 intel_dp_set_m_n(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08005775
Daniel Vetterbcd644e2013-06-05 13:34:22 +02005776 if (is_lvds && has_reduced_clock && i915_powersave)
5777 intel_crtc->lowfreq_avail = true;
5778 else
5779 intel_crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02005780
5781 if (intel_crtc->config.has_pch_encoder) {
5782 pll = intel_crtc_to_shared_dpll(intel_crtc);
5783
Jesse Barnes79e53942008-11-07 14:24:08 -08005784 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005785
Daniel Vetter8a654f32013-06-01 17:16:22 +02005786 intel_set_pipe_timings(intel_crtc);
Krzysztof Halasa734b4152010-05-25 18:41:46 +02005787
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01005788 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01005789 intel_cpu_transcoder_set_m_n(intel_crtc,
5790 &intel_crtc->config.fdi_m_n);
5791 }
Chris Wilson5eddb702010-09-11 13:48:45 +01005792
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005793 if (IS_IVYBRIDGE(dev))
5794 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
Zhenyu Wang2c072452009-06-05 15:38:42 +08005795
Daniel Vetter6ff93602013-04-19 11:24:36 +02005796 ironlake_set_pipeconf(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08005797
Paulo Zanonia1f9e772012-09-12 10:06:32 -03005798 /* Set up the display plane register */
5799 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08005800 POSTING_READ(DSPCNTR(plane));
Jesse Barnes79e53942008-11-07 14:24:08 -08005801
Daniel Vetter94352cf2012-07-05 22:51:56 +02005802 ret = intel_pipe_set_base(crtc, x, y, fb);
Shaohua Li7662c8b2009-06-26 11:23:55 +08005803
5804 intel_update_watermarks(dev);
5805
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005806 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08005807}
5808
Daniel Vetter72419202013-04-04 13:28:53 +02005809static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5810 struct intel_crtc_config *pipe_config)
5811{
5812 struct drm_device *dev = crtc->base.dev;
5813 struct drm_i915_private *dev_priv = dev->dev_private;
5814 enum transcoder transcoder = pipe_config->cpu_transcoder;
5815
5816 pipe_config->fdi_m_n.link_m = I915_READ(PIPE_LINK_M1(transcoder));
5817 pipe_config->fdi_m_n.link_n = I915_READ(PIPE_LINK_N1(transcoder));
5818 pipe_config->fdi_m_n.gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
5819 & ~TU_SIZE_MASK;
5820 pipe_config->fdi_m_n.gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
5821 pipe_config->fdi_m_n.tu = ((I915_READ(PIPE_DATA_M1(transcoder))
5822 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
5823}
5824
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005825static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5826 struct intel_crtc_config *pipe_config)
5827{
5828 struct drm_device *dev = crtc->base.dev;
5829 struct drm_i915_private *dev_priv = dev->dev_private;
5830 uint32_t tmp;
5831
5832 tmp = I915_READ(PF_CTL(crtc->pipe));
5833
5834 if (tmp & PF_ENABLE) {
5835 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
5836 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02005837
5838 /* We currently do not free assignements of panel fitters on
5839 * ivb/hsw (since we don't use the higher upscaling modes which
5840 * differentiates them) so just WARN about this case for now. */
5841 if (IS_GEN7(dev)) {
5842 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
5843 PF_PIPE_SEL_IVB(crtc->pipe));
5844 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005845 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005846}
5847
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005848static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5849 struct intel_crtc_config *pipe_config)
5850{
5851 struct drm_device *dev = crtc->base.dev;
5852 struct drm_i915_private *dev_priv = dev->dev_private;
5853 uint32_t tmp;
5854
Daniel Vettere143a212013-07-04 12:01:15 +02005855 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02005856 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02005857
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005858 tmp = I915_READ(PIPECONF(crtc->pipe));
5859 if (!(tmp & PIPECONF_ENABLE))
5860 return false;
5861
Daniel Vetterab9412b2013-05-03 11:49:46 +02005862 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02005863 struct intel_shared_dpll *pll;
5864
Daniel Vetter88adfff2013-03-28 10:42:01 +01005865 pipe_config->has_pch_encoder = true;
5866
Daniel Vetter627eb5a2013-04-29 19:33:42 +02005867 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
5868 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
5869 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02005870
5871 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02005872
Daniel Vetterc0d43d62013-06-07 23:11:08 +02005873 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02005874 pipe_config->shared_dpll =
5875 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02005876 } else {
5877 tmp = I915_READ(PCH_DPLL_SEL);
5878 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
5879 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
5880 else
5881 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
5882 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02005883
5884 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
5885
5886 WARN_ON(!pll->get_hw_state(dev_priv, pll,
5887 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02005888
5889 tmp = pipe_config->dpll_hw_state.dpll;
5890 pipe_config->pixel_multiplier =
5891 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
5892 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter6c49f242013-06-06 12:45:25 +02005893 } else {
5894 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02005895 }
5896
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005897 intel_get_pipe_timings(crtc, pipe_config);
5898
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005899 ironlake_get_pfit_config(crtc, pipe_config);
5900
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005901 return true;
5902}
5903
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005904static void haswell_modeset_global_resources(struct drm_device *dev)
5905{
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005906 bool enable = false;
5907 struct intel_crtc *crtc;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005908
5909 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
Daniel Vettere7a639c2013-05-31 17:49:17 +02005910 if (!crtc->base.enabled)
5911 continue;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005912
Daniel Vettere7a639c2013-05-31 17:49:17 +02005913 if (crtc->pipe != PIPE_A || crtc->config.pch_pfit.size ||
5914 crtc->config.cpu_transcoder != TRANSCODER_EDP)
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005915 enable = true;
5916 }
5917
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005918 intel_set_power_well(dev, enable);
5919}
5920
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005921static int haswell_crtc_mode_set(struct drm_crtc *crtc,
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005922 int x, int y,
5923 struct drm_framebuffer *fb)
5924{
5925 struct drm_device *dev = crtc->dev;
5926 struct drm_i915_private *dev_priv = dev->dev_private;
5927 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005928 int plane = intel_crtc->plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005929 int ret;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005930
Daniel Vetterff9a6752013-06-01 17:16:21 +02005931 if (!intel_ddi_pll_mode_set(crtc))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03005932 return -EINVAL;
5933
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005934 /* Ensure that the cursor is valid for the new mode before changing... */
5935 intel_crtc_update_cursor(crtc, true);
5936
Daniel Vetter03afc4a2013-04-02 23:42:31 +02005937 if (intel_crtc->config.has_dp_encoder)
5938 intel_dp_set_m_n(intel_crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005939
5940 intel_crtc->lowfreq_avail = false;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005941
Daniel Vetter8a654f32013-06-01 17:16:22 +02005942 intel_set_pipe_timings(intel_crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005943
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01005944 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01005945 intel_cpu_transcoder_set_m_n(intel_crtc,
5946 &intel_crtc->config.fdi_m_n);
5947 }
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005948
Daniel Vetter6ff93602013-04-19 11:24:36 +02005949 haswell_set_pipeconf(crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005950
Daniel Vetter50f3b012013-03-27 00:44:56 +01005951 intel_set_pipe_csc(crtc);
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005952
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005953 /* Set up the display plane register */
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005954 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005955 POSTING_READ(DSPCNTR(plane));
5956
5957 ret = intel_pipe_set_base(crtc, x, y, fb);
5958
5959 intel_update_watermarks(dev);
5960
Jesse Barnes79e53942008-11-07 14:24:08 -08005961 return ret;
5962}
5963
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005964static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5965 struct intel_crtc_config *pipe_config)
5966{
5967 struct drm_device *dev = crtc->base.dev;
5968 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005969 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005970 uint32_t tmp;
5971
Daniel Vettere143a212013-07-04 12:01:15 +02005972 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02005973 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
5974
Daniel Vettereccb1402013-05-22 00:50:22 +02005975 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
5976 if (tmp & TRANS_DDI_FUNC_ENABLE) {
5977 enum pipe trans_edp_pipe;
5978 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
5979 default:
5980 WARN(1, "unknown pipe linked to edp transcoder\n");
5981 case TRANS_DDI_EDP_INPUT_A_ONOFF:
5982 case TRANS_DDI_EDP_INPUT_A_ON:
5983 trans_edp_pipe = PIPE_A;
5984 break;
5985 case TRANS_DDI_EDP_INPUT_B_ONOFF:
5986 trans_edp_pipe = PIPE_B;
5987 break;
5988 case TRANS_DDI_EDP_INPUT_C_ONOFF:
5989 trans_edp_pipe = PIPE_C;
5990 break;
5991 }
5992
5993 if (trans_edp_pipe == crtc->pipe)
5994 pipe_config->cpu_transcoder = TRANSCODER_EDP;
5995 }
5996
Paulo Zanonib97186f2013-05-03 12:15:36 -03005997 if (!intel_display_power_enabled(dev,
Daniel Vettereccb1402013-05-22 00:50:22 +02005998 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03005999 return false;
6000
Daniel Vettereccb1402013-05-22 00:50:22 +02006001 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006002 if (!(tmp & PIPECONF_ENABLE))
6003 return false;
6004
Daniel Vetter88adfff2013-03-28 10:42:01 +01006005 /*
Paulo Zanonif196e6b2013-04-18 16:35:41 -03006006 * Haswell has only FDI/PCH transcoder A. It is which is connected to
Daniel Vetter88adfff2013-03-28 10:42:01 +01006007 * DDI E. So just check whether this pipe is wired to DDI E and whether
6008 * the PCH transcoder is on.
6009 */
Daniel Vettereccb1402013-05-22 00:50:22 +02006010 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
Daniel Vetter88adfff2013-03-28 10:42:01 +01006011 if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
Daniel Vetterab9412b2013-05-03 11:49:46 +02006012 I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter88adfff2013-03-28 10:42:01 +01006013 pipe_config->has_pch_encoder = true;
6014
Daniel Vetter627eb5a2013-04-29 19:33:42 +02006015 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
6016 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6017 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02006018
6019 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02006020 }
6021
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006022 intel_get_pipe_timings(crtc, pipe_config);
6023
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006024 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
6025 if (intel_display_power_enabled(dev, pfit_domain))
6026 ironlake_get_pfit_config(crtc, pipe_config);
Daniel Vetter88adfff2013-03-28 10:42:01 +01006027
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006028 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
6029 (I915_READ(IPS_CTL) & IPS_ENABLE);
6030
Daniel Vetter6c49f242013-06-06 12:45:25 +02006031 pipe_config->pixel_multiplier = 1;
6032
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006033 return true;
6034}
6035
Eric Anholtf564048e2011-03-30 13:01:02 -07006036static int intel_crtc_mode_set(struct drm_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07006037 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02006038 struct drm_framebuffer *fb)
Eric Anholtf564048e2011-03-30 13:01:02 -07006039{
6040 struct drm_device *dev = crtc->dev;
6041 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter9256aa12012-10-31 19:26:13 +01006042 struct drm_encoder_helper_funcs *encoder_funcs;
6043 struct intel_encoder *encoder;
Eric Anholt0b701d22011-03-30 13:01:03 -07006044 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01006045 struct drm_display_mode *adjusted_mode =
6046 &intel_crtc->config.adjusted_mode;
6047 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
Eric Anholt0b701d22011-03-30 13:01:03 -07006048 int pipe = intel_crtc->pipe;
Eric Anholtf564048e2011-03-30 13:01:02 -07006049 int ret;
6050
Eric Anholt0b701d22011-03-30 13:01:03 -07006051 drm_vblank_pre_modeset(dev, pipe);
6052
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01006053 ret = dev_priv->display.crtc_mode_set(crtc, x, y, fb);
6054
Jesse Barnes79e53942008-11-07 14:24:08 -08006055 drm_vblank_post_modeset(dev, pipe);
6056
Daniel Vetter9256aa12012-10-31 19:26:13 +01006057 if (ret != 0)
6058 return ret;
6059
6060 for_each_encoder_on_crtc(dev, crtc, encoder) {
6061 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
6062 encoder->base.base.id,
6063 drm_get_encoder_name(&encoder->base),
6064 mode->base.id, mode->name);
Daniel Vetter6cc5f342013-03-27 00:44:53 +01006065 if (encoder->mode_set) {
6066 encoder->mode_set(encoder);
6067 } else {
6068 encoder_funcs = encoder->base.helper_private;
6069 encoder_funcs->mode_set(&encoder->base, mode, adjusted_mode);
6070 }
Daniel Vetter9256aa12012-10-31 19:26:13 +01006071 }
6072
6073 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006074}
6075
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006076static bool intel_eld_uptodate(struct drm_connector *connector,
6077 int reg_eldv, uint32_t bits_eldv,
6078 int reg_elda, uint32_t bits_elda,
6079 int reg_edid)
6080{
6081 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6082 uint8_t *eld = connector->eld;
6083 uint32_t i;
6084
6085 i = I915_READ(reg_eldv);
6086 i &= bits_eldv;
6087
6088 if (!eld[0])
6089 return !i;
6090
6091 if (!i)
6092 return false;
6093
6094 i = I915_READ(reg_elda);
6095 i &= ~bits_elda;
6096 I915_WRITE(reg_elda, i);
6097
6098 for (i = 0; i < eld[2]; i++)
6099 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
6100 return false;
6101
6102 return true;
6103}
6104
Wu Fengguange0dac652011-09-05 14:25:34 +08006105static void g4x_write_eld(struct drm_connector *connector,
6106 struct drm_crtc *crtc)
6107{
6108 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6109 uint8_t *eld = connector->eld;
6110 uint32_t eldv;
6111 uint32_t len;
6112 uint32_t i;
6113
6114 i = I915_READ(G4X_AUD_VID_DID);
6115
6116 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
6117 eldv = G4X_ELDV_DEVCL_DEVBLC;
6118 else
6119 eldv = G4X_ELDV_DEVCTG;
6120
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006121 if (intel_eld_uptodate(connector,
6122 G4X_AUD_CNTL_ST, eldv,
6123 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
6124 G4X_HDMIW_HDMIEDID))
6125 return;
6126
Wu Fengguange0dac652011-09-05 14:25:34 +08006127 i = I915_READ(G4X_AUD_CNTL_ST);
6128 i &= ~(eldv | G4X_ELD_ADDR);
6129 len = (i >> 9) & 0x1f; /* ELD buffer size */
6130 I915_WRITE(G4X_AUD_CNTL_ST, i);
6131
6132 if (!eld[0])
6133 return;
6134
6135 len = min_t(uint8_t, eld[2], len);
6136 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6137 for (i = 0; i < len; i++)
6138 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
6139
6140 i = I915_READ(G4X_AUD_CNTL_ST);
6141 i |= eldv;
6142 I915_WRITE(G4X_AUD_CNTL_ST, i);
6143}
6144
Wang Xingchao83358c852012-08-16 22:43:37 +08006145static void haswell_write_eld(struct drm_connector *connector,
6146 struct drm_crtc *crtc)
6147{
6148 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6149 uint8_t *eld = connector->eld;
6150 struct drm_device *dev = crtc->dev;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08006151 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Wang Xingchao83358c852012-08-16 22:43:37 +08006152 uint32_t eldv;
6153 uint32_t i;
6154 int len;
6155 int pipe = to_intel_crtc(crtc)->pipe;
6156 int tmp;
6157
6158 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
6159 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
6160 int aud_config = HSW_AUD_CFG(pipe);
6161 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
6162
6163
6164 DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
6165
6166 /* Audio output enable */
6167 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
6168 tmp = I915_READ(aud_cntrl_st2);
6169 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
6170 I915_WRITE(aud_cntrl_st2, tmp);
6171
6172 /* Wait for 1 vertical blank */
6173 intel_wait_for_vblank(dev, pipe);
6174
6175 /* Set ELD valid state */
6176 tmp = I915_READ(aud_cntrl_st2);
6177 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%8x\n", tmp);
6178 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
6179 I915_WRITE(aud_cntrl_st2, tmp);
6180 tmp = I915_READ(aud_cntrl_st2);
6181 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%8x\n", tmp);
6182
6183 /* Enable HDMI mode */
6184 tmp = I915_READ(aud_config);
6185 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%8x\n", tmp);
6186 /* clear N_programing_enable and N_value_index */
6187 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
6188 I915_WRITE(aud_config, tmp);
6189
6190 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
6191
6192 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08006193 intel_crtc->eld_vld = true;
Wang Xingchao83358c852012-08-16 22:43:37 +08006194
6195 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6196 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6197 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
6198 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6199 } else
6200 I915_WRITE(aud_config, 0);
6201
6202 if (intel_eld_uptodate(connector,
6203 aud_cntrl_st2, eldv,
6204 aud_cntl_st, IBX_ELD_ADDRESS,
6205 hdmiw_hdmiedid))
6206 return;
6207
6208 i = I915_READ(aud_cntrl_st2);
6209 i &= ~eldv;
6210 I915_WRITE(aud_cntrl_st2, i);
6211
6212 if (!eld[0])
6213 return;
6214
6215 i = I915_READ(aud_cntl_st);
6216 i &= ~IBX_ELD_ADDRESS;
6217 I915_WRITE(aud_cntl_st, i);
6218 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
6219 DRM_DEBUG_DRIVER("port num:%d\n", i);
6220
6221 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6222 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6223 for (i = 0; i < len; i++)
6224 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6225
6226 i = I915_READ(aud_cntrl_st2);
6227 i |= eldv;
6228 I915_WRITE(aud_cntrl_st2, i);
6229
6230}
6231
Wu Fengguange0dac652011-09-05 14:25:34 +08006232static void ironlake_write_eld(struct drm_connector *connector,
6233 struct drm_crtc *crtc)
6234{
6235 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6236 uint8_t *eld = connector->eld;
6237 uint32_t eldv;
6238 uint32_t i;
6239 int len;
6240 int hdmiw_hdmiedid;
Wu Fengguangb6daa022012-01-06 14:41:31 -06006241 int aud_config;
Wu Fengguange0dac652011-09-05 14:25:34 +08006242 int aud_cntl_st;
6243 int aud_cntrl_st2;
Wang Xingchao9b138a82012-08-09 16:52:18 +08006244 int pipe = to_intel_crtc(crtc)->pipe;
Wu Fengguange0dac652011-09-05 14:25:34 +08006245
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08006246 if (HAS_PCH_IBX(connector->dev)) {
Wang Xingchao9b138a82012-08-09 16:52:18 +08006247 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
6248 aud_config = IBX_AUD_CFG(pipe);
6249 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006250 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08006251 } else {
Wang Xingchao9b138a82012-08-09 16:52:18 +08006252 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
6253 aud_config = CPT_AUD_CFG(pipe);
6254 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006255 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08006256 }
6257
Wang Xingchao9b138a82012-08-09 16:52:18 +08006258 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
Wu Fengguange0dac652011-09-05 14:25:34 +08006259
6260 i = I915_READ(aud_cntl_st);
Wang Xingchao9b138a82012-08-09 16:52:18 +08006261 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
Wu Fengguange0dac652011-09-05 14:25:34 +08006262 if (!i) {
6263 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
6264 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006265 eldv = IBX_ELD_VALIDB;
6266 eldv |= IBX_ELD_VALIDB << 4;
6267 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08006268 } else {
Ville Syrjälä2582a852013-04-17 17:48:47 +03006269 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006270 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08006271 }
6272
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006273 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6274 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6275 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
Wu Fengguangb6daa022012-01-06 14:41:31 -06006276 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6277 } else
6278 I915_WRITE(aud_config, 0);
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006279
6280 if (intel_eld_uptodate(connector,
6281 aud_cntrl_st2, eldv,
6282 aud_cntl_st, IBX_ELD_ADDRESS,
6283 hdmiw_hdmiedid))
6284 return;
6285
Wu Fengguange0dac652011-09-05 14:25:34 +08006286 i = I915_READ(aud_cntrl_st2);
6287 i &= ~eldv;
6288 I915_WRITE(aud_cntrl_st2, i);
6289
6290 if (!eld[0])
6291 return;
6292
Wu Fengguange0dac652011-09-05 14:25:34 +08006293 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006294 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08006295 I915_WRITE(aud_cntl_st, i);
6296
6297 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6298 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6299 for (i = 0; i < len; i++)
6300 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6301
6302 i = I915_READ(aud_cntrl_st2);
6303 i |= eldv;
6304 I915_WRITE(aud_cntrl_st2, i);
6305}
6306
6307void intel_write_eld(struct drm_encoder *encoder,
6308 struct drm_display_mode *mode)
6309{
6310 struct drm_crtc *crtc = encoder->crtc;
6311 struct drm_connector *connector;
6312 struct drm_device *dev = encoder->dev;
6313 struct drm_i915_private *dev_priv = dev->dev_private;
6314
6315 connector = drm_select_eld(encoder, mode);
6316 if (!connector)
6317 return;
6318
6319 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6320 connector->base.id,
6321 drm_get_connector_name(connector),
6322 connector->encoder->base.id,
6323 drm_get_encoder_name(connector->encoder));
6324
6325 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
6326
6327 if (dev_priv->display.write_eld)
6328 dev_priv->display.write_eld(connector, crtc);
6329}
6330
Jesse Barnes79e53942008-11-07 14:24:08 -08006331/** Loads the palette/gamma unit for the CRTC with the prepared values */
6332void intel_crtc_load_lut(struct drm_crtc *crtc)
6333{
6334 struct drm_device *dev = crtc->dev;
6335 struct drm_i915_private *dev_priv = dev->dev_private;
6336 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006337 enum pipe pipe = intel_crtc->pipe;
6338 int palreg = PALETTE(pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08006339 int i;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006340 bool reenable_ips = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006341
6342 /* The clocks have to be on to load the palette. */
Alban Browaeysaed3f092012-02-24 17:12:45 +00006343 if (!crtc->enabled || !intel_crtc->active)
Jesse Barnes79e53942008-11-07 14:24:08 -08006344 return;
6345
Ville Syrjälä14420bd2013-06-04 13:49:07 +03006346 if (!HAS_PCH_SPLIT(dev_priv->dev))
6347 assert_pll_enabled(dev_priv, pipe);
6348
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006349 /* use legacy palette for Ironlake */
Eric Anholtbad720f2009-10-22 16:11:14 -07006350 if (HAS_PCH_SPLIT(dev))
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006351 palreg = LGC_PALETTE(pipe);
6352
6353 /* Workaround : Do not read or write the pipe palette/gamma data while
6354 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
6355 */
6356 if (intel_crtc->config.ips_enabled &&
6357 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
6358 GAMMA_MODE_MODE_SPLIT)) {
6359 hsw_disable_ips(intel_crtc);
6360 reenable_ips = true;
6361 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08006362
Jesse Barnes79e53942008-11-07 14:24:08 -08006363 for (i = 0; i < 256; i++) {
6364 I915_WRITE(palreg + 4 * i,
6365 (intel_crtc->lut_r[i] << 16) |
6366 (intel_crtc->lut_g[i] << 8) |
6367 intel_crtc->lut_b[i]);
6368 }
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006369
6370 if (reenable_ips)
6371 hsw_enable_ips(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006372}
6373
Chris Wilson560b85b2010-08-07 11:01:38 +01006374static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
6375{
6376 struct drm_device *dev = crtc->dev;
6377 struct drm_i915_private *dev_priv = dev->dev_private;
6378 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6379 bool visible = base != 0;
6380 u32 cntl;
6381
6382 if (intel_crtc->cursor_visible == visible)
6383 return;
6384
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006385 cntl = I915_READ(_CURACNTR);
Chris Wilson560b85b2010-08-07 11:01:38 +01006386 if (visible) {
6387 /* On these chipsets we can only modify the base whilst
6388 * the cursor is disabled.
6389 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006390 I915_WRITE(_CURABASE, base);
Chris Wilson560b85b2010-08-07 11:01:38 +01006391
6392 cntl &= ~(CURSOR_FORMAT_MASK);
6393 /* XXX width must be 64, stride 256 => 0x00 << 28 */
6394 cntl |= CURSOR_ENABLE |
6395 CURSOR_GAMMA_ENABLE |
6396 CURSOR_FORMAT_ARGB;
6397 } else
6398 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006399 I915_WRITE(_CURACNTR, cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01006400
6401 intel_crtc->cursor_visible = visible;
6402}
6403
6404static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
6405{
6406 struct drm_device *dev = crtc->dev;
6407 struct drm_i915_private *dev_priv = dev->dev_private;
6408 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6409 int pipe = intel_crtc->pipe;
6410 bool visible = base != 0;
6411
6412 if (intel_crtc->cursor_visible != visible) {
Jesse Barnes548f2452011-02-17 10:40:53 -08006413 uint32_t cntl = I915_READ(CURCNTR(pipe));
Chris Wilson560b85b2010-08-07 11:01:38 +01006414 if (base) {
6415 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
6416 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6417 cntl |= pipe << 28; /* Connect to correct pipe */
6418 } else {
6419 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6420 cntl |= CURSOR_MODE_DISABLE;
6421 }
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006422 I915_WRITE(CURCNTR(pipe), cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01006423
6424 intel_crtc->cursor_visible = visible;
6425 }
6426 /* and commit changes on next vblank */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006427 I915_WRITE(CURBASE(pipe), base);
Chris Wilson560b85b2010-08-07 11:01:38 +01006428}
6429
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006430static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
6431{
6432 struct drm_device *dev = crtc->dev;
6433 struct drm_i915_private *dev_priv = dev->dev_private;
6434 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6435 int pipe = intel_crtc->pipe;
6436 bool visible = base != 0;
6437
6438 if (intel_crtc->cursor_visible != visible) {
6439 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
6440 if (base) {
6441 cntl &= ~CURSOR_MODE;
6442 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6443 } else {
6444 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6445 cntl |= CURSOR_MODE_DISABLE;
6446 }
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006447 if (IS_HASWELL(dev))
6448 cntl |= CURSOR_PIPE_CSC_ENABLE;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006449 I915_WRITE(CURCNTR_IVB(pipe), cntl);
6450
6451 intel_crtc->cursor_visible = visible;
6452 }
6453 /* and commit changes on next vblank */
6454 I915_WRITE(CURBASE_IVB(pipe), base);
6455}
6456
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006457/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01006458static void intel_crtc_update_cursor(struct drm_crtc *crtc,
6459 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006460{
6461 struct drm_device *dev = crtc->dev;
6462 struct drm_i915_private *dev_priv = dev->dev_private;
6463 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6464 int pipe = intel_crtc->pipe;
6465 int x = intel_crtc->cursor_x;
6466 int y = intel_crtc->cursor_y;
Chris Wilson560b85b2010-08-07 11:01:38 +01006467 u32 base, pos;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006468 bool visible;
6469
6470 pos = 0;
6471
Chris Wilson6b383a72010-09-13 13:54:26 +01006472 if (on && crtc->enabled && crtc->fb) {
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006473 base = intel_crtc->cursor_addr;
6474 if (x > (int) crtc->fb->width)
6475 base = 0;
6476
6477 if (y > (int) crtc->fb->height)
6478 base = 0;
6479 } else
6480 base = 0;
6481
6482 if (x < 0) {
6483 if (x + intel_crtc->cursor_width < 0)
6484 base = 0;
6485
6486 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
6487 x = -x;
6488 }
6489 pos |= x << CURSOR_X_SHIFT;
6490
6491 if (y < 0) {
6492 if (y + intel_crtc->cursor_height < 0)
6493 base = 0;
6494
6495 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
6496 y = -y;
6497 }
6498 pos |= y << CURSOR_Y_SHIFT;
6499
6500 visible = base != 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01006501 if (!visible && !intel_crtc->cursor_visible)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006502 return;
6503
Eugeni Dodonov0cd83aa2012-04-13 17:08:48 -03006504 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006505 I915_WRITE(CURPOS_IVB(pipe), pos);
6506 ivb_update_cursor(crtc, base);
6507 } else {
6508 I915_WRITE(CURPOS(pipe), pos);
6509 if (IS_845G(dev) || IS_I865G(dev))
6510 i845_update_cursor(crtc, base);
6511 else
6512 i9xx_update_cursor(crtc, base);
6513 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006514}
6515
Jesse Barnes79e53942008-11-07 14:24:08 -08006516static int intel_crtc_cursor_set(struct drm_crtc *crtc,
Chris Wilson05394f32010-11-08 19:18:58 +00006517 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08006518 uint32_t handle,
6519 uint32_t width, uint32_t height)
6520{
6521 struct drm_device *dev = crtc->dev;
6522 struct drm_i915_private *dev_priv = dev->dev_private;
6523 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00006524 struct drm_i915_gem_object *obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006525 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006526 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006527
Jesse Barnes79e53942008-11-07 14:24:08 -08006528 /* if we want to turn off the cursor ignore width and height */
6529 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08006530 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006531 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00006532 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10006533 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006534 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08006535 }
6536
6537 /* Currently we only support 64x64 cursors */
6538 if (width != 64 || height != 64) {
6539 DRM_ERROR("we currently only support 64x64 cursors\n");
6540 return -EINVAL;
6541 }
6542
Chris Wilson05394f32010-11-08 19:18:58 +00006543 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00006544 if (&obj->base == NULL)
Jesse Barnes79e53942008-11-07 14:24:08 -08006545 return -ENOENT;
6546
Chris Wilson05394f32010-11-08 19:18:58 +00006547 if (obj->base.size < width * height * 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006548 DRM_ERROR("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10006549 ret = -ENOMEM;
6550 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08006551 }
6552
Dave Airlie71acb5e2008-12-30 20:31:46 +10006553 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006554 mutex_lock(&dev->struct_mutex);
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05006555 if (!dev_priv->info->cursor_needs_physical) {
Chris Wilson693db182013-03-05 14:52:39 +00006556 unsigned alignment;
6557
Chris Wilsond9e86c02010-11-10 16:40:20 +00006558 if (obj->tiling_mode) {
6559 DRM_ERROR("cursor cannot be tiled\n");
6560 ret = -EINVAL;
6561 goto fail_locked;
6562 }
6563
Chris Wilson693db182013-03-05 14:52:39 +00006564 /* Note that the w/a also requires 2 PTE of padding following
6565 * the bo. We currently fill all unused PTE with the shadow
6566 * page and so we should always have valid PTE following the
6567 * cursor preventing the VT-d warning.
6568 */
6569 alignment = 0;
6570 if (need_vtd_wa(dev))
6571 alignment = 64*1024;
6572
6573 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01006574 if (ret) {
6575 DRM_ERROR("failed to move cursor bo into the GTT\n");
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006576 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006577 }
6578
Chris Wilsond9e86c02010-11-10 16:40:20 +00006579 ret = i915_gem_object_put_fence(obj);
6580 if (ret) {
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006581 DRM_ERROR("failed to release fence for cursor");
Chris Wilsond9e86c02010-11-10 16:40:20 +00006582 goto fail_unpin;
6583 }
6584
Ben Widawskyf343c5f2013-07-05 14:41:04 -07006585 addr = i915_gem_obj_ggtt_offset(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10006586 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01006587 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson05394f32010-11-08 19:18:58 +00006588 ret = i915_gem_attach_phys_object(dev, obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01006589 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
6590 align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10006591 if (ret) {
6592 DRM_ERROR("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006593 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10006594 }
Chris Wilson05394f32010-11-08 19:18:58 +00006595 addr = obj->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006596 }
6597
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006598 if (IS_GEN2(dev))
Jesse Barnes14b60392009-05-20 16:47:08 -04006599 I915_WRITE(CURSIZE, (height << 12) | width);
6600
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006601 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006602 if (intel_crtc->cursor_bo) {
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05006603 if (dev_priv->info->cursor_needs_physical) {
Chris Wilson05394f32010-11-08 19:18:58 +00006604 if (intel_crtc->cursor_bo != obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10006605 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
6606 } else
6607 i915_gem_object_unpin(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00006608 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006609 }
Jesse Barnes80824002009-09-10 15:28:06 -07006610
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006611 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006612
6613 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00006614 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006615 intel_crtc->cursor_width = width;
6616 intel_crtc->cursor_height = height;
6617
Mika Kuoppala40ccc722013-04-23 17:27:08 +03006618 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006619
Jesse Barnes79e53942008-11-07 14:24:08 -08006620 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006621fail_unpin:
Chris Wilson05394f32010-11-08 19:18:58 +00006622 i915_gem_object_unpin(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006623fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10006624 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00006625fail:
Chris Wilson05394f32010-11-08 19:18:58 +00006626 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10006627 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006628}
6629
6630static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
6631{
Jesse Barnes79e53942008-11-07 14:24:08 -08006632 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006633
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006634 intel_crtc->cursor_x = x;
6635 intel_crtc->cursor_y = y;
Jesse Barnes652c3932009-08-17 13:31:43 -07006636
Mika Kuoppala40ccc722013-04-23 17:27:08 +03006637 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08006638
6639 return 0;
6640}
6641
6642/** Sets the color ramps on behalf of RandR */
6643void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
6644 u16 blue, int regno)
6645{
6646 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6647
6648 intel_crtc->lut_r[regno] = red >> 8;
6649 intel_crtc->lut_g[regno] = green >> 8;
6650 intel_crtc->lut_b[regno] = blue >> 8;
6651}
6652
Dave Airlieb8c00ac2009-10-06 13:54:01 +10006653void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
6654 u16 *blue, int regno)
6655{
6656 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6657
6658 *red = intel_crtc->lut_r[regno] << 8;
6659 *green = intel_crtc->lut_g[regno] << 8;
6660 *blue = intel_crtc->lut_b[regno] << 8;
6661}
6662
Jesse Barnes79e53942008-11-07 14:24:08 -08006663static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01006664 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08006665{
James Simmons72034252010-08-03 01:33:19 +01006666 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08006667 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006668
James Simmons72034252010-08-03 01:33:19 +01006669 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006670 intel_crtc->lut_r[i] = red[i] >> 8;
6671 intel_crtc->lut_g[i] = green[i] >> 8;
6672 intel_crtc->lut_b[i] = blue[i] >> 8;
6673 }
6674
6675 intel_crtc_load_lut(crtc);
6676}
6677
Jesse Barnes79e53942008-11-07 14:24:08 -08006678/* VESA 640x480x72Hz mode to set on the pipe */
6679static struct drm_display_mode load_detect_mode = {
6680 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
6681 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
6682};
6683
Chris Wilsond2dff872011-04-19 08:36:26 +01006684static struct drm_framebuffer *
6685intel_framebuffer_create(struct drm_device *dev,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006686 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilsond2dff872011-04-19 08:36:26 +01006687 struct drm_i915_gem_object *obj)
6688{
6689 struct intel_framebuffer *intel_fb;
6690 int ret;
6691
6692 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
6693 if (!intel_fb) {
6694 drm_gem_object_unreference_unlocked(&obj->base);
6695 return ERR_PTR(-ENOMEM);
6696 }
6697
6698 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
6699 if (ret) {
6700 drm_gem_object_unreference_unlocked(&obj->base);
6701 kfree(intel_fb);
6702 return ERR_PTR(ret);
6703 }
6704
6705 return &intel_fb->base;
6706}
6707
6708static u32
6709intel_framebuffer_pitch_for_width(int width, int bpp)
6710{
6711 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
6712 return ALIGN(pitch, 64);
6713}
6714
6715static u32
6716intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
6717{
6718 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
6719 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
6720}
6721
6722static struct drm_framebuffer *
6723intel_framebuffer_create_for_mode(struct drm_device *dev,
6724 struct drm_display_mode *mode,
6725 int depth, int bpp)
6726{
6727 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00006728 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01006729
6730 obj = i915_gem_alloc_object(dev,
6731 intel_framebuffer_size_for_mode(mode, bpp));
6732 if (obj == NULL)
6733 return ERR_PTR(-ENOMEM);
6734
6735 mode_cmd.width = mode->hdisplay;
6736 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006737 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
6738 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00006739 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01006740
6741 return intel_framebuffer_create(dev, &mode_cmd, obj);
6742}
6743
6744static struct drm_framebuffer *
6745mode_fits_in_fbdev(struct drm_device *dev,
6746 struct drm_display_mode *mode)
6747{
6748 struct drm_i915_private *dev_priv = dev->dev_private;
6749 struct drm_i915_gem_object *obj;
6750 struct drm_framebuffer *fb;
6751
6752 if (dev_priv->fbdev == NULL)
6753 return NULL;
6754
6755 obj = dev_priv->fbdev->ifb.obj;
6756 if (obj == NULL)
6757 return NULL;
6758
6759 fb = &dev_priv->fbdev->ifb.base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006760 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
6761 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01006762 return NULL;
6763
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006764 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01006765 return NULL;
6766
6767 return fb;
6768}
6769
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006770bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01006771 struct drm_display_mode *mode,
Chris Wilson8261b192011-04-19 23:18:09 +01006772 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08006773{
6774 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006775 struct intel_encoder *intel_encoder =
6776 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08006777 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01006778 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08006779 struct drm_crtc *crtc = NULL;
6780 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02006781 struct drm_framebuffer *fb;
Jesse Barnes79e53942008-11-07 14:24:08 -08006782 int i = -1;
6783
Chris Wilsond2dff872011-04-19 08:36:26 +01006784 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6785 connector->base.id, drm_get_connector_name(connector),
6786 encoder->base.id, drm_get_encoder_name(encoder));
6787
Jesse Barnes79e53942008-11-07 14:24:08 -08006788 /*
6789 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01006790 *
Jesse Barnes79e53942008-11-07 14:24:08 -08006791 * - if the connector already has an assigned crtc, use it (but make
6792 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01006793 *
Jesse Barnes79e53942008-11-07 14:24:08 -08006794 * - try to find the first unused crtc that can drive this connector,
6795 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08006796 */
6797
6798 /* See if we already have a CRTC for this connector */
6799 if (encoder->crtc) {
6800 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01006801
Daniel Vetter7b240562012-12-12 00:35:33 +01006802 mutex_lock(&crtc->mutex);
6803
Daniel Vetter24218aa2012-08-12 19:27:11 +02006804 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01006805 old->load_detect_temp = false;
6806
6807 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02006808 if (connector->dpms != DRM_MODE_DPMS_ON)
6809 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01006810
Chris Wilson71731882011-04-19 23:10:58 +01006811 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08006812 }
6813
6814 /* Find an unused one (if possible) */
6815 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
6816 i++;
6817 if (!(encoder->possible_crtcs & (1 << i)))
6818 continue;
6819 if (!possible_crtc->enabled) {
6820 crtc = possible_crtc;
6821 break;
6822 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006823 }
6824
6825 /*
6826 * If we didn't find an unused CRTC, don't use any.
6827 */
6828 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01006829 DRM_DEBUG_KMS("no pipe available for load-detect\n");
6830 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006831 }
6832
Daniel Vetter7b240562012-12-12 00:35:33 +01006833 mutex_lock(&crtc->mutex);
Daniel Vetterfc303102012-07-09 10:40:58 +02006834 intel_encoder->new_crtc = to_intel_crtc(crtc);
6835 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006836
6837 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter24218aa2012-08-12 19:27:11 +02006838 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01006839 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01006840 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08006841
Chris Wilson64927112011-04-20 07:25:26 +01006842 if (!mode)
6843 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08006844
Chris Wilsond2dff872011-04-19 08:36:26 +01006845 /* We need a framebuffer large enough to accommodate all accesses
6846 * that the plane may generate whilst we perform load detection.
6847 * We can not rely on the fbcon either being present (we get called
6848 * during its initialisation to detect all boot displays, or it may
6849 * not even exist) or that it is large enough to satisfy the
6850 * requested mode.
6851 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02006852 fb = mode_fits_in_fbdev(dev, mode);
6853 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01006854 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02006855 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
6856 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01006857 } else
6858 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02006859 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01006860 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Daniel Vetter7b240562012-12-12 00:35:33 +01006861 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00006862 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006863 }
Chris Wilsond2dff872011-04-19 08:36:26 +01006864
Chris Wilsonc0c36b942012-12-19 16:08:43 +00006865 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01006866 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01006867 if (old->release_fb)
6868 old->release_fb->funcs->destroy(old->release_fb);
Daniel Vetter7b240562012-12-12 00:35:33 +01006869 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00006870 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006871 }
Chris Wilson71731882011-04-19 23:10:58 +01006872
Jesse Barnes79e53942008-11-07 14:24:08 -08006873 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006874 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01006875 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08006876}
6877
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006878void intel_release_load_detect_pipe(struct drm_connector *connector,
Chris Wilson8261b192011-04-19 23:18:09 +01006879 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08006880{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006881 struct intel_encoder *intel_encoder =
6882 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01006883 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01006884 struct drm_crtc *crtc = encoder->crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -08006885
Chris Wilsond2dff872011-04-19 08:36:26 +01006886 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6887 connector->base.id, drm_get_connector_name(connector),
6888 encoder->base.id, drm_get_encoder_name(encoder));
6889
Chris Wilson8261b192011-04-19 23:18:09 +01006890 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02006891 to_intel_connector(connector)->new_encoder = NULL;
6892 intel_encoder->new_crtc = NULL;
6893 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01006894
Daniel Vetter36206362012-12-10 20:42:17 +01006895 if (old->release_fb) {
6896 drm_framebuffer_unregister_private(old->release_fb);
6897 drm_framebuffer_unreference(old->release_fb);
6898 }
Chris Wilsond2dff872011-04-19 08:36:26 +01006899
Daniel Vetter67c96402013-01-23 16:25:09 +00006900 mutex_unlock(&crtc->mutex);
Chris Wilson0622a532011-04-21 09:32:11 +01006901 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08006902 }
6903
Eric Anholtc751ce42010-03-25 11:48:48 -07006904 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02006905 if (old->dpms_mode != DRM_MODE_DPMS_ON)
6906 connector->funcs->dpms(connector, old->dpms_mode);
Daniel Vetter7b240562012-12-12 00:35:33 +01006907
6908 mutex_unlock(&crtc->mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08006909}
6910
6911/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03006912static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
6913 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006914{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03006915 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08006916 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03006917 int pipe = pipe_config->cpu_transcoder;
Jesse Barnes548f2452011-02-17 10:40:53 -08006918 u32 dpll = I915_READ(DPLL(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006919 u32 fp;
6920 intel_clock_t clock;
6921
6922 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Chris Wilson39adb7a2011-04-22 22:17:21 +01006923 fp = I915_READ(FP0(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006924 else
Chris Wilson39adb7a2011-04-22 22:17:21 +01006925 fp = I915_READ(FP1(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006926
6927 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006928 if (IS_PINEVIEW(dev)) {
6929 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
6930 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08006931 } else {
6932 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
6933 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
6934 }
6935
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006936 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006937 if (IS_PINEVIEW(dev))
6938 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
6939 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08006940 else
6941 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08006942 DPLL_FPA01_P1_POST_DIV_SHIFT);
6943
6944 switch (dpll & DPLL_MODE_MASK) {
6945 case DPLLB_MODE_DAC_SERIAL:
6946 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
6947 5 : 10;
6948 break;
6949 case DPLLB_MODE_LVDS:
6950 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
6951 7 : 14;
6952 break;
6953 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08006954 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08006955 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03006956 pipe_config->adjusted_mode.clock = 0;
6957 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08006958 }
6959
Daniel Vetterac58c3f2013-06-01 17:16:17 +02006960 if (IS_PINEVIEW(dev))
6961 pineview_clock(96000, &clock);
6962 else
6963 i9xx_clock(96000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006964 } else {
6965 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
6966
6967 if (is_lvds) {
6968 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
6969 DPLL_FPA01_P1_POST_DIV_SHIFT);
6970 clock.p2 = 14;
6971
6972 if ((dpll & PLL_REF_INPUT_MASK) ==
6973 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
6974 /* XXX: might not be 66MHz */
Daniel Vetterac58c3f2013-06-01 17:16:17 +02006975 i9xx_clock(66000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006976 } else
Daniel Vetterac58c3f2013-06-01 17:16:17 +02006977 i9xx_clock(48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006978 } else {
6979 if (dpll & PLL_P1_DIVIDE_BY_TWO)
6980 clock.p1 = 2;
6981 else {
6982 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
6983 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
6984 }
6985 if (dpll & PLL_P2_DIVIDE_BY_4)
6986 clock.p2 = 4;
6987 else
6988 clock.p2 = 2;
6989
Daniel Vetterac58c3f2013-06-01 17:16:17 +02006990 i9xx_clock(48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006991 }
6992 }
6993
Jesse Barnesf1f644d2013-06-27 00:39:25 +03006994 pipe_config->adjusted_mode.clock = clock.dot *
6995 pipe_config->pixel_multiplier;
6996}
6997
6998static void ironlake_crtc_clock_get(struct intel_crtc *crtc,
6999 struct intel_crtc_config *pipe_config)
7000{
7001 struct drm_device *dev = crtc->base.dev;
7002 struct drm_i915_private *dev_priv = dev->dev_private;
7003 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7004 int link_freq, repeat;
7005 u64 clock;
7006 u32 link_m, link_n;
7007
7008 repeat = pipe_config->pixel_multiplier;
7009
7010 /*
7011 * The calculation for the data clock is:
7012 * pixel_clock = ((m/n)*(link_clock * nr_lanes * repeat))/bpp
7013 * But we want to avoid losing precison if possible, so:
7014 * pixel_clock = ((m * link_clock * nr_lanes * repeat)/(n*bpp))
7015 *
7016 * and the link clock is simpler:
7017 * link_clock = (m * link_clock * repeat) / n
Jesse Barnes79e53942008-11-07 14:24:08 -08007018 */
7019
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007020 /*
7021 * We need to get the FDI or DP link clock here to derive
7022 * the M/N dividers.
7023 *
7024 * For FDI, we read it from the BIOS or use a fixed 2.7GHz.
7025 * For DP, it's either 1.62GHz or 2.7GHz.
7026 * We do our calculations in 10*MHz since we don't need much precison.
7027 */
7028 if (pipe_config->has_pch_encoder)
7029 link_freq = intel_fdi_link_freq(dev) * 10000;
7030 else
7031 link_freq = pipe_config->port_clock;
7032
7033 link_m = I915_READ(PIPE_LINK_M1(cpu_transcoder));
7034 link_n = I915_READ(PIPE_LINK_N1(cpu_transcoder));
7035
7036 if (!link_m || !link_n)
7037 return;
7038
7039 clock = ((u64)link_m * (u64)link_freq * (u64)repeat);
7040 do_div(clock, link_n);
7041
7042 pipe_config->adjusted_mode.clock = clock;
Jesse Barnes79e53942008-11-07 14:24:08 -08007043}
7044
7045/** Returns the currently programmed mode of the given pipe. */
7046struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
7047 struct drm_crtc *crtc)
7048{
Jesse Barnes548f2452011-02-17 10:40:53 -08007049 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08007050 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02007051 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08007052 struct drm_display_mode *mode;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007053 struct intel_crtc_config pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007054 int htot = I915_READ(HTOTAL(cpu_transcoder));
7055 int hsync = I915_READ(HSYNC(cpu_transcoder));
7056 int vtot = I915_READ(VTOTAL(cpu_transcoder));
7057 int vsync = I915_READ(VSYNC(cpu_transcoder));
Jesse Barnes79e53942008-11-07 14:24:08 -08007058
7059 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
7060 if (!mode)
7061 return NULL;
7062
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007063 /*
7064 * Construct a pipe_config sufficient for getting the clock info
7065 * back out of crtc_clock_get.
7066 *
7067 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
7068 * to use a real value here instead.
7069 */
Daniel Vettere143a212013-07-04 12:01:15 +02007070 pipe_config.cpu_transcoder = (enum transcoder) intel_crtc->pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007071 pipe_config.pixel_multiplier = 1;
7072 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
7073
7074 mode->clock = pipe_config.adjusted_mode.clock;
Jesse Barnes79e53942008-11-07 14:24:08 -08007075 mode->hdisplay = (htot & 0xffff) + 1;
7076 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
7077 mode->hsync_start = (hsync & 0xffff) + 1;
7078 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
7079 mode->vdisplay = (vtot & 0xffff) + 1;
7080 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
7081 mode->vsync_start = (vsync & 0xffff) + 1;
7082 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
7083
7084 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08007085
7086 return mode;
7087}
7088
Daniel Vetter3dec0092010-08-20 21:40:52 +02007089static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07007090{
7091 struct drm_device *dev = crtc->dev;
7092 drm_i915_private_t *dev_priv = dev->dev_private;
7093 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7094 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007095 int dpll_reg = DPLL(pipe);
7096 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07007097
Eric Anholtbad720f2009-10-22 16:11:14 -07007098 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07007099 return;
7100
7101 if (!dev_priv->lvds_downclock_avail)
7102 return;
7103
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007104 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07007105 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08007106 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007107
Sean Paul8ac5a6d2012-02-13 13:14:51 -05007108 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007109
7110 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
7111 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007112 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007113
Jesse Barnes652c3932009-08-17 13:31:43 -07007114 dpll = I915_READ(dpll_reg);
7115 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08007116 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007117 }
Jesse Barnes652c3932009-08-17 13:31:43 -07007118}
7119
7120static void intel_decrease_pllclock(struct drm_crtc *crtc)
7121{
7122 struct drm_device *dev = crtc->dev;
7123 drm_i915_private_t *dev_priv = dev->dev_private;
7124 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07007125
Eric Anholtbad720f2009-10-22 16:11:14 -07007126 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07007127 return;
7128
7129 if (!dev_priv->lvds_downclock_avail)
7130 return;
7131
7132 /*
7133 * Since this is called by a timer, we should never get here in
7134 * the manual case.
7135 */
7136 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01007137 int pipe = intel_crtc->pipe;
7138 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02007139 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01007140
Zhao Yakui44d98a62009-10-09 11:39:40 +08007141 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007142
Sean Paul8ac5a6d2012-02-13 13:14:51 -05007143 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007144
Chris Wilson074b5e12012-05-02 12:07:06 +01007145 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07007146 dpll |= DISPLAY_RATE_SELECT_FPA1;
7147 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007148 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007149 dpll = I915_READ(dpll_reg);
7150 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08007151 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007152 }
7153
7154}
7155
Chris Wilsonf047e392012-07-21 12:31:41 +01007156void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07007157{
Chris Wilsonf047e392012-07-21 12:31:41 +01007158 i915_update_gfx_val(dev->dev_private);
7159}
7160
7161void intel_mark_idle(struct drm_device *dev)
7162{
Chris Wilson725a5b52013-01-08 11:02:57 +00007163 struct drm_crtc *crtc;
7164
7165 if (!i915_powersave)
7166 return;
7167
7168 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
7169 if (!crtc->fb)
7170 continue;
7171
7172 intel_decrease_pllclock(crtc);
7173 }
Chris Wilsonf047e392012-07-21 12:31:41 +01007174}
7175
Chris Wilsonc65355b2013-06-06 16:53:41 -03007176void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
7177 struct intel_ring_buffer *ring)
Chris Wilsonf047e392012-07-21 12:31:41 +01007178{
7179 struct drm_device *dev = obj->base.dev;
Jesse Barnes652c3932009-08-17 13:31:43 -07007180 struct drm_crtc *crtc;
Jesse Barnes652c3932009-08-17 13:31:43 -07007181
7182 if (!i915_powersave)
7183 return;
7184
Jesse Barnes652c3932009-08-17 13:31:43 -07007185 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Jesse Barnes652c3932009-08-17 13:31:43 -07007186 if (!crtc->fb)
7187 continue;
7188
Chris Wilsonc65355b2013-06-06 16:53:41 -03007189 if (to_intel_framebuffer(crtc->fb)->obj != obj)
7190 continue;
7191
7192 intel_increase_pllclock(crtc);
7193 if (ring && intel_fbc_enabled(dev))
7194 ring->fbc_dirty = true;
Jesse Barnes652c3932009-08-17 13:31:43 -07007195 }
Jesse Barnes652c3932009-08-17 13:31:43 -07007196}
7197
Jesse Barnes79e53942008-11-07 14:24:08 -08007198static void intel_crtc_destroy(struct drm_crtc *crtc)
7199{
7200 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02007201 struct drm_device *dev = crtc->dev;
7202 struct intel_unpin_work *work;
7203 unsigned long flags;
7204
7205 spin_lock_irqsave(&dev->event_lock, flags);
7206 work = intel_crtc->unpin_work;
7207 intel_crtc->unpin_work = NULL;
7208 spin_unlock_irqrestore(&dev->event_lock, flags);
7209
7210 if (work) {
7211 cancel_work_sync(&work->work);
7212 kfree(work);
7213 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007214
Mika Kuoppala40ccc722013-04-23 17:27:08 +03007215 intel_crtc_cursor_set(crtc, NULL, 0, 0, 0);
7216
Jesse Barnes79e53942008-11-07 14:24:08 -08007217 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02007218
Jesse Barnes79e53942008-11-07 14:24:08 -08007219 kfree(intel_crtc);
7220}
7221
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007222static void intel_unpin_work_fn(struct work_struct *__work)
7223{
7224 struct intel_unpin_work *work =
7225 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007226 struct drm_device *dev = work->crtc->dev;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007227
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007228 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01007229 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00007230 drm_gem_object_unreference(&work->pending_flip_obj->base);
7231 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00007232
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007233 intel_update_fbc(dev);
7234 mutex_unlock(&dev->struct_mutex);
7235
7236 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
7237 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
7238
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007239 kfree(work);
7240}
7241
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007242static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01007243 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007244{
7245 drm_i915_private_t *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007246 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7247 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007248 unsigned long flags;
7249
7250 /* Ignore early vblank irqs */
7251 if (intel_crtc == NULL)
7252 return;
7253
7254 spin_lock_irqsave(&dev->event_lock, flags);
7255 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00007256
7257 /* Ensure we don't miss a work->pending update ... */
7258 smp_rmb();
7259
7260 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007261 spin_unlock_irqrestore(&dev->event_lock, flags);
7262 return;
7263 }
7264
Chris Wilsone7d841c2012-12-03 11:36:30 +00007265 /* and that the unpin work is consistent wrt ->pending. */
7266 smp_rmb();
7267
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007268 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007269
Rob Clark45a066e2012-10-08 14:50:40 -05007270 if (work->event)
7271 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007272
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01007273 drm_vblank_put(dev, intel_crtc->pipe);
7274
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007275 spin_unlock_irqrestore(&dev->event_lock, flags);
7276
Daniel Vetter2c10d572012-12-20 21:24:07 +01007277 wake_up_all(&dev_priv->pending_flip_queue);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007278
7279 queue_work(dev_priv->wq, &work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07007280
7281 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007282}
7283
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007284void intel_finish_page_flip(struct drm_device *dev, int pipe)
7285{
7286 drm_i915_private_t *dev_priv = dev->dev_private;
7287 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
7288
Mario Kleiner49b14a52010-12-09 07:00:07 +01007289 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007290}
7291
7292void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
7293{
7294 drm_i915_private_t *dev_priv = dev->dev_private;
7295 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
7296
Mario Kleiner49b14a52010-12-09 07:00:07 +01007297 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007298}
7299
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007300void intel_prepare_page_flip(struct drm_device *dev, int plane)
7301{
7302 drm_i915_private_t *dev_priv = dev->dev_private;
7303 struct intel_crtc *intel_crtc =
7304 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
7305 unsigned long flags;
7306
Chris Wilsone7d841c2012-12-03 11:36:30 +00007307 /* NB: An MMIO update of the plane base pointer will also
7308 * generate a page-flip completion irq, i.e. every modeset
7309 * is also accompanied by a spurious intel_prepare_page_flip().
7310 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007311 spin_lock_irqsave(&dev->event_lock, flags);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007312 if (intel_crtc->unpin_work)
7313 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007314 spin_unlock_irqrestore(&dev->event_lock, flags);
7315}
7316
Chris Wilsone7d841c2012-12-03 11:36:30 +00007317inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
7318{
7319 /* Ensure that the work item is consistent when activating it ... */
7320 smp_wmb();
7321 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
7322 /* and that it is marked active as soon as the irq could fire. */
7323 smp_wmb();
7324}
7325
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007326static int intel_gen2_queue_flip(struct drm_device *dev,
7327 struct drm_crtc *crtc,
7328 struct drm_framebuffer *fb,
7329 struct drm_i915_gem_object *obj)
7330{
7331 struct drm_i915_private *dev_priv = dev->dev_private;
7332 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007333 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007334 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007335 int ret;
7336
Daniel Vetter6d90c952012-04-26 23:28:05 +02007337 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007338 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007339 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007340
Daniel Vetter6d90c952012-04-26 23:28:05 +02007341 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007342 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007343 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007344
7345 /* Can't queue multiple flips, so wait for the previous
7346 * one to finish before executing the next.
7347 */
7348 if (intel_crtc->plane)
7349 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7350 else
7351 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007352 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7353 intel_ring_emit(ring, MI_NOOP);
7354 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7355 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7356 intel_ring_emit(ring, fb->pitches[0]);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007357 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007358 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00007359
7360 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007361 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007362 return 0;
7363
7364err_unpin:
7365 intel_unpin_fb_obj(obj);
7366err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007367 return ret;
7368}
7369
7370static int intel_gen3_queue_flip(struct drm_device *dev,
7371 struct drm_crtc *crtc,
7372 struct drm_framebuffer *fb,
7373 struct drm_i915_gem_object *obj)
7374{
7375 struct drm_i915_private *dev_priv = dev->dev_private;
7376 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007377 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007378 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007379 int ret;
7380
Daniel Vetter6d90c952012-04-26 23:28:05 +02007381 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007382 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007383 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007384
Daniel Vetter6d90c952012-04-26 23:28:05 +02007385 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007386 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007387 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007388
7389 if (intel_crtc->plane)
7390 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7391 else
7392 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007393 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7394 intel_ring_emit(ring, MI_NOOP);
7395 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
7396 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7397 intel_ring_emit(ring, fb->pitches[0]);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007398 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007399 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007400
Chris Wilsone7d841c2012-12-03 11:36:30 +00007401 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007402 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007403 return 0;
7404
7405err_unpin:
7406 intel_unpin_fb_obj(obj);
7407err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007408 return ret;
7409}
7410
7411static int intel_gen4_queue_flip(struct drm_device *dev,
7412 struct drm_crtc *crtc,
7413 struct drm_framebuffer *fb,
7414 struct drm_i915_gem_object *obj)
7415{
7416 struct drm_i915_private *dev_priv = dev->dev_private;
7417 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7418 uint32_t pf, pipesrc;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007419 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007420 int ret;
7421
Daniel Vetter6d90c952012-04-26 23:28:05 +02007422 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007423 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007424 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007425
Daniel Vetter6d90c952012-04-26 23:28:05 +02007426 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007427 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007428 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007429
7430 /* i965+ uses the linear or tiled offsets from the
7431 * Display Registers (which do not change across a page-flip)
7432 * so we need only reprogram the base address.
7433 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02007434 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7435 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7436 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02007437 intel_ring_emit(ring,
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007438 (i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset) |
Daniel Vetterc2c75132012-07-05 12:17:30 +02007439 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007440
7441 /* XXX Enabling the panel-fitter across page-flip is so far
7442 * untested on non-native modes, so ignore it for now.
7443 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
7444 */
7445 pf = 0;
7446 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007447 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007448
7449 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007450 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007451 return 0;
7452
7453err_unpin:
7454 intel_unpin_fb_obj(obj);
7455err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007456 return ret;
7457}
7458
7459static int intel_gen6_queue_flip(struct drm_device *dev,
7460 struct drm_crtc *crtc,
7461 struct drm_framebuffer *fb,
7462 struct drm_i915_gem_object *obj)
7463{
7464 struct drm_i915_private *dev_priv = dev->dev_private;
7465 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007466 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007467 uint32_t pf, pipesrc;
7468 int ret;
7469
Daniel Vetter6d90c952012-04-26 23:28:05 +02007470 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007471 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007472 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007473
Daniel Vetter6d90c952012-04-26 23:28:05 +02007474 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007475 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007476 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007477
Daniel Vetter6d90c952012-04-26 23:28:05 +02007478 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7479 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7480 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007481 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007482
Chris Wilson99d9acd2012-04-17 20:37:00 +01007483 /* Contrary to the suggestions in the documentation,
7484 * "Enable Panel Fitter" does not seem to be required when page
7485 * flipping with a non-native mode, and worse causes a normal
7486 * modeset to fail.
7487 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
7488 */
7489 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007490 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007491 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007492
7493 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007494 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007495 return 0;
7496
7497err_unpin:
7498 intel_unpin_fb_obj(obj);
7499err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007500 return ret;
7501}
7502
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007503/*
7504 * On gen7 we currently use the blit ring because (in early silicon at least)
7505 * the render ring doesn't give us interrpts for page flip completion, which
7506 * means clients will hang after the first flip is queued. Fortunately the
7507 * blit ring generates interrupts properly, so use it instead.
7508 */
7509static int intel_gen7_queue_flip(struct drm_device *dev,
7510 struct drm_crtc *crtc,
7511 struct drm_framebuffer *fb,
7512 struct drm_i915_gem_object *obj)
7513{
7514 struct drm_i915_private *dev_priv = dev->dev_private;
7515 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7516 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007517 uint32_t plane_bit = 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007518 int ret;
7519
7520 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7521 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007522 goto err;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007523
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007524 switch(intel_crtc->plane) {
7525 case PLANE_A:
7526 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
7527 break;
7528 case PLANE_B:
7529 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
7530 break;
7531 case PLANE_C:
7532 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
7533 break;
7534 default:
7535 WARN_ONCE(1, "unknown plane in flip command\n");
7536 ret = -ENODEV;
Eugeni Dodonovab3951e2012-06-18 19:03:38 -03007537 goto err_unpin;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007538 }
7539
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007540 ret = intel_ring_begin(ring, 4);
7541 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007542 goto err_unpin;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007543
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007544 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007545 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007546 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007547 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +00007548
7549 intel_mark_page_flip_active(intel_crtc);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007550 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007551 return 0;
7552
7553err_unpin:
7554 intel_unpin_fb_obj(obj);
7555err:
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007556 return ret;
7557}
7558
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007559static int intel_default_queue_flip(struct drm_device *dev,
7560 struct drm_crtc *crtc,
7561 struct drm_framebuffer *fb,
7562 struct drm_i915_gem_object *obj)
7563{
7564 return -ENODEV;
7565}
7566
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007567static int intel_crtc_page_flip(struct drm_crtc *crtc,
7568 struct drm_framebuffer *fb,
7569 struct drm_pending_vblank_event *event)
7570{
7571 struct drm_device *dev = crtc->dev;
7572 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02007573 struct drm_framebuffer *old_fb = crtc->fb;
7574 struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007575 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7576 struct intel_unpin_work *work;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007577 unsigned long flags;
Chris Wilson52e68632010-08-08 10:15:59 +01007578 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007579
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03007580 /* Can't change pixel format via MI display flips. */
7581 if (fb->pixel_format != crtc->fb->pixel_format)
7582 return -EINVAL;
7583
7584 /*
7585 * TILEOFF/LINOFF registers can't be changed via MI display flips.
7586 * Note that pitch changes could also affect these register.
7587 */
7588 if (INTEL_INFO(dev)->gen > 3 &&
7589 (fb->offsets[0] != crtc->fb->offsets[0] ||
7590 fb->pitches[0] != crtc->fb->pitches[0]))
7591 return -EINVAL;
7592
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007593 work = kzalloc(sizeof *work, GFP_KERNEL);
7594 if (work == NULL)
7595 return -ENOMEM;
7596
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007597 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007598 work->crtc = crtc;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02007599 work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007600 INIT_WORK(&work->work, intel_unpin_work_fn);
7601
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007602 ret = drm_vblank_get(dev, intel_crtc->pipe);
7603 if (ret)
7604 goto free_work;
7605
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007606 /* We borrow the event spin lock for protecting unpin_work */
7607 spin_lock_irqsave(&dev->event_lock, flags);
7608 if (intel_crtc->unpin_work) {
7609 spin_unlock_irqrestore(&dev->event_lock, flags);
7610 kfree(work);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007611 drm_vblank_put(dev, intel_crtc->pipe);
Chris Wilson468f0b42010-05-27 13:18:13 +01007612
7613 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007614 return -EBUSY;
7615 }
7616 intel_crtc->unpin_work = work;
7617 spin_unlock_irqrestore(&dev->event_lock, flags);
7618
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007619 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
7620 flush_workqueue(dev_priv->wq);
7621
Chris Wilson79158102012-05-23 11:13:58 +01007622 ret = i915_mutex_lock_interruptible(dev);
7623 if (ret)
7624 goto cleanup;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007625
Jesse Barnes75dfca82010-02-10 15:09:44 -08007626 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00007627 drm_gem_object_reference(&work->old_fb_obj->base);
7628 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007629
7630 crtc->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01007631
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007632 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007633
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01007634 work->enable_stall_check = true;
7635
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007636 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +02007637 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007638
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007639 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
7640 if (ret)
7641 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007642
Chris Wilson7782de32011-07-08 12:22:41 +01007643 intel_disable_fbc(dev);
Chris Wilsonc65355b2013-06-06 16:53:41 -03007644 intel_mark_fb_busy(obj, NULL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007645 mutex_unlock(&dev->struct_mutex);
7646
Jesse Barnese5510fa2010-07-01 16:48:37 -07007647 trace_i915_flip_request(intel_crtc->plane, obj);
7648
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007649 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01007650
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007651cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007652 atomic_dec(&intel_crtc->unpin_work_count);
Ville Syrjälä4a35f832013-02-22 16:53:38 +02007653 crtc->fb = old_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00007654 drm_gem_object_unreference(&work->old_fb_obj->base);
7655 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01007656 mutex_unlock(&dev->struct_mutex);
7657
Chris Wilson79158102012-05-23 11:13:58 +01007658cleanup:
Chris Wilson96b099f2010-06-07 14:03:04 +01007659 spin_lock_irqsave(&dev->event_lock, flags);
7660 intel_crtc->unpin_work = NULL;
7661 spin_unlock_irqrestore(&dev->event_lock, flags);
7662
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007663 drm_vblank_put(dev, intel_crtc->pipe);
7664free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01007665 kfree(work);
7666
7667 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007668}
7669
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007670static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007671 .mode_set_base_atomic = intel_pipe_set_base_atomic,
7672 .load_lut = intel_crtc_load_lut,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007673};
7674
Daniel Vetter50f56112012-07-02 09:35:43 +02007675static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
7676 struct drm_crtc *crtc)
7677{
7678 struct drm_device *dev;
7679 struct drm_crtc *tmp;
7680 int crtc_mask = 1;
7681
7682 WARN(!crtc, "checking null crtc?\n");
7683
7684 dev = crtc->dev;
7685
7686 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
7687 if (tmp == crtc)
7688 break;
7689 crtc_mask <<= 1;
7690 }
7691
7692 if (encoder->possible_crtcs & crtc_mask)
7693 return true;
7694 return false;
7695}
7696
Daniel Vetter9a935852012-07-05 22:34:27 +02007697/**
7698 * intel_modeset_update_staged_output_state
7699 *
7700 * Updates the staged output configuration state, e.g. after we've read out the
7701 * current hw state.
7702 */
7703static void intel_modeset_update_staged_output_state(struct drm_device *dev)
7704{
7705 struct intel_encoder *encoder;
7706 struct intel_connector *connector;
7707
7708 list_for_each_entry(connector, &dev->mode_config.connector_list,
7709 base.head) {
7710 connector->new_encoder =
7711 to_intel_encoder(connector->base.encoder);
7712 }
7713
7714 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7715 base.head) {
7716 encoder->new_crtc =
7717 to_intel_crtc(encoder->base.crtc);
7718 }
7719}
7720
7721/**
7722 * intel_modeset_commit_output_state
7723 *
7724 * This function copies the stage display pipe configuration to the real one.
7725 */
7726static void intel_modeset_commit_output_state(struct drm_device *dev)
7727{
7728 struct intel_encoder *encoder;
7729 struct intel_connector *connector;
7730
7731 list_for_each_entry(connector, &dev->mode_config.connector_list,
7732 base.head) {
7733 connector->base.encoder = &connector->new_encoder->base;
7734 }
7735
7736 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7737 base.head) {
7738 encoder->base.crtc = &encoder->new_crtc->base;
7739 }
7740}
7741
Daniel Vetter050f7ae2013-06-02 13:26:23 +02007742static void
7743connected_sink_compute_bpp(struct intel_connector * connector,
7744 struct intel_crtc_config *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007745{
Daniel Vetter050f7ae2013-06-02 13:26:23 +02007746 int bpp = pipe_config->pipe_bpp;
7747
7748 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
7749 connector->base.base.id,
7750 drm_get_connector_name(&connector->base));
7751
7752 /* Don't use an invalid EDID bpc value */
7753 if (connector->base.display_info.bpc &&
7754 connector->base.display_info.bpc * 3 < bpp) {
7755 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
7756 bpp, connector->base.display_info.bpc*3);
7757 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
7758 }
7759
7760 /* Clamp bpp to 8 on screens without EDID 1.4 */
7761 if (connector->base.display_info.bpc == 0 && bpp > 24) {
7762 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
7763 bpp);
7764 pipe_config->pipe_bpp = 24;
7765 }
7766}
7767
7768static int
7769compute_baseline_pipe_bpp(struct intel_crtc *crtc,
7770 struct drm_framebuffer *fb,
7771 struct intel_crtc_config *pipe_config)
7772{
7773 struct drm_device *dev = crtc->base.dev;
7774 struct intel_connector *connector;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007775 int bpp;
7776
Daniel Vetterd42264b2013-03-28 16:38:08 +01007777 switch (fb->pixel_format) {
7778 case DRM_FORMAT_C8:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007779 bpp = 8*3; /* since we go through a colormap */
7780 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01007781 case DRM_FORMAT_XRGB1555:
7782 case DRM_FORMAT_ARGB1555:
7783 /* checked in intel_framebuffer_init already */
7784 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
7785 return -EINVAL;
7786 case DRM_FORMAT_RGB565:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007787 bpp = 6*3; /* min is 18bpp */
7788 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01007789 case DRM_FORMAT_XBGR8888:
7790 case DRM_FORMAT_ABGR8888:
7791 /* checked in intel_framebuffer_init already */
7792 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
7793 return -EINVAL;
7794 case DRM_FORMAT_XRGB8888:
7795 case DRM_FORMAT_ARGB8888:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007796 bpp = 8*3;
7797 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01007798 case DRM_FORMAT_XRGB2101010:
7799 case DRM_FORMAT_ARGB2101010:
7800 case DRM_FORMAT_XBGR2101010:
7801 case DRM_FORMAT_ABGR2101010:
7802 /* checked in intel_framebuffer_init already */
7803 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
Daniel Vetterbaba1332013-03-27 00:45:00 +01007804 return -EINVAL;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007805 bpp = 10*3;
7806 break;
Daniel Vetterbaba1332013-03-27 00:45:00 +01007807 /* TODO: gen4+ supports 16 bpc floating point, too. */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007808 default:
7809 DRM_DEBUG_KMS("unsupported depth\n");
7810 return -EINVAL;
7811 }
7812
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007813 pipe_config->pipe_bpp = bpp;
7814
7815 /* Clamp display bpp to EDID value */
7816 list_for_each_entry(connector, &dev->mode_config.connector_list,
Daniel Vetter050f7ae2013-06-02 13:26:23 +02007817 base.head) {
Daniel Vetter1b829e02013-06-02 13:26:24 +02007818 if (!connector->new_encoder ||
7819 connector->new_encoder->new_crtc != crtc)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007820 continue;
7821
Daniel Vetter050f7ae2013-06-02 13:26:23 +02007822 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007823 }
7824
7825 return bpp;
7826}
7827
Daniel Vetterc0b03412013-05-28 12:05:54 +02007828static void intel_dump_pipe_config(struct intel_crtc *crtc,
7829 struct intel_crtc_config *pipe_config,
7830 const char *context)
7831{
7832 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
7833 context, pipe_name(crtc->pipe));
7834
7835 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
7836 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
7837 pipe_config->pipe_bpp, pipe_config->dither);
7838 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
7839 pipe_config->has_pch_encoder,
7840 pipe_config->fdi_lanes,
7841 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
7842 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
7843 pipe_config->fdi_m_n.tu);
7844 DRM_DEBUG_KMS("requested mode:\n");
7845 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
7846 DRM_DEBUG_KMS("adjusted mode:\n");
7847 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
7848 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
7849 pipe_config->gmch_pfit.control,
7850 pipe_config->gmch_pfit.pgm_ratios,
7851 pipe_config->gmch_pfit.lvds_border_bits);
7852 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x\n",
7853 pipe_config->pch_pfit.pos,
7854 pipe_config->pch_pfit.size);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03007855 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Daniel Vetterc0b03412013-05-28 12:05:54 +02007856}
7857
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02007858static bool check_encoder_cloning(struct drm_crtc *crtc)
7859{
7860 int num_encoders = 0;
7861 bool uncloneable_encoders = false;
7862 struct intel_encoder *encoder;
7863
7864 list_for_each_entry(encoder, &crtc->dev->mode_config.encoder_list,
7865 base.head) {
7866 if (&encoder->new_crtc->base != crtc)
7867 continue;
7868
7869 num_encoders++;
7870 if (!encoder->cloneable)
7871 uncloneable_encoders = true;
7872 }
7873
7874 return !(num_encoders > 1 && uncloneable_encoders);
7875}
7876
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007877static struct intel_crtc_config *
7878intel_modeset_pipe_config(struct drm_crtc *crtc,
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007879 struct drm_framebuffer *fb,
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007880 struct drm_display_mode *mode)
Daniel Vetter7758a112012-07-08 19:40:39 +02007881{
7882 struct drm_device *dev = crtc->dev;
Daniel Vetter7758a112012-07-08 19:40:39 +02007883 struct drm_encoder_helper_funcs *encoder_funcs;
7884 struct intel_encoder *encoder;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007885 struct intel_crtc_config *pipe_config;
Daniel Vettere29c22c2013-02-21 00:00:16 +01007886 int plane_bpp, ret = -EINVAL;
7887 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +02007888
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02007889 if (!check_encoder_cloning(crtc)) {
7890 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
7891 return ERR_PTR(-EINVAL);
7892 }
7893
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007894 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7895 if (!pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +02007896 return ERR_PTR(-ENOMEM);
7897
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007898 drm_mode_copy(&pipe_config->adjusted_mode, mode);
7899 drm_mode_copy(&pipe_config->requested_mode, mode);
Daniel Vettere143a212013-07-04 12:01:15 +02007900 pipe_config->cpu_transcoder =
7901 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007902 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007903
Daniel Vetter050f7ae2013-06-02 13:26:23 +02007904 /* Compute a starting value for pipe_config->pipe_bpp taking the source
7905 * plane pixel format and any sink constraints into account. Returns the
7906 * source plane bpp so that dithering can be selected on mismatches
7907 * after encoders and crtc also have had their say. */
7908 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
7909 fb, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007910 if (plane_bpp < 0)
7911 goto fail;
7912
Daniel Vettere29c22c2013-02-21 00:00:16 +01007913encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +02007914 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +02007915 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +02007916 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +02007917
Daniel Vetter7758a112012-07-08 19:40:39 +02007918 /* Pass our mode to the connectors and the CRTC to give them a chance to
7919 * adjust it according to limitations or connector properties, and also
7920 * a chance to reject the mode entirely.
7921 */
7922 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7923 base.head) {
7924
7925 if (&encoder->new_crtc->base != crtc)
7926 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +01007927
7928 if (encoder->compute_config) {
7929 if (!(encoder->compute_config(encoder, pipe_config))) {
7930 DRM_DEBUG_KMS("Encoder config failure\n");
7931 goto fail;
7932 }
7933
7934 continue;
7935 }
7936
Daniel Vetter7758a112012-07-08 19:40:39 +02007937 encoder_funcs = encoder->base.helper_private;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007938 if (!(encoder_funcs->mode_fixup(&encoder->base,
7939 &pipe_config->requested_mode,
7940 &pipe_config->adjusted_mode))) {
Daniel Vetter7758a112012-07-08 19:40:39 +02007941 DRM_DEBUG_KMS("Encoder fixup failed\n");
7942 goto fail;
7943 }
7944 }
7945
Daniel Vetterff9a6752013-06-01 17:16:21 +02007946 /* Set default port clock if not overwritten by the encoder. Needs to be
7947 * done afterwards in case the encoder adjusts the mode. */
7948 if (!pipe_config->port_clock)
7949 pipe_config->port_clock = pipe_config->adjusted_mode.clock;
7950
Daniel Vettera43f6e02013-06-07 23:10:32 +02007951 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01007952 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +02007953 DRM_DEBUG_KMS("CRTC fixup failed\n");
7954 goto fail;
7955 }
Daniel Vettere29c22c2013-02-21 00:00:16 +01007956
7957 if (ret == RETRY) {
7958 if (WARN(!retry, "loop in pipe configuration computation\n")) {
7959 ret = -EINVAL;
7960 goto fail;
7961 }
7962
7963 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
7964 retry = false;
7965 goto encoder_retry;
7966 }
7967
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007968 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
7969 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
7970 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
7971
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007972 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +02007973fail:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007974 kfree(pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01007975 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +02007976}
7977
Daniel Vettere2e1ed42012-07-08 21:14:38 +02007978/* Computes which crtcs are affected and sets the relevant bits in the mask. For
7979 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
7980static void
7981intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
7982 unsigned *prepare_pipes, unsigned *disable_pipes)
7983{
7984 struct intel_crtc *intel_crtc;
7985 struct drm_device *dev = crtc->dev;
7986 struct intel_encoder *encoder;
7987 struct intel_connector *connector;
7988 struct drm_crtc *tmp_crtc;
7989
7990 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
7991
7992 /* Check which crtcs have changed outputs connected to them, these need
7993 * to be part of the prepare_pipes mask. We don't (yet) support global
7994 * modeset across multiple crtcs, so modeset_pipes will only have one
7995 * bit set at most. */
7996 list_for_each_entry(connector, &dev->mode_config.connector_list,
7997 base.head) {
7998 if (connector->base.encoder == &connector->new_encoder->base)
7999 continue;
8000
8001 if (connector->base.encoder) {
8002 tmp_crtc = connector->base.encoder->crtc;
8003
8004 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
8005 }
8006
8007 if (connector->new_encoder)
8008 *prepare_pipes |=
8009 1 << connector->new_encoder->new_crtc->pipe;
8010 }
8011
8012 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8013 base.head) {
8014 if (encoder->base.crtc == &encoder->new_crtc->base)
8015 continue;
8016
8017 if (encoder->base.crtc) {
8018 tmp_crtc = encoder->base.crtc;
8019
8020 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
8021 }
8022
8023 if (encoder->new_crtc)
8024 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
8025 }
8026
8027 /* Check for any pipes that will be fully disabled ... */
8028 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
8029 base.head) {
8030 bool used = false;
8031
8032 /* Don't try to disable disabled crtcs. */
8033 if (!intel_crtc->base.enabled)
8034 continue;
8035
8036 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8037 base.head) {
8038 if (encoder->new_crtc == intel_crtc)
8039 used = true;
8040 }
8041
8042 if (!used)
8043 *disable_pipes |= 1 << intel_crtc->pipe;
8044 }
8045
8046
8047 /* set_mode is also used to update properties on life display pipes. */
8048 intel_crtc = to_intel_crtc(crtc);
8049 if (crtc->enabled)
8050 *prepare_pipes |= 1 << intel_crtc->pipe;
8051
Daniel Vetterb6c51642013-04-12 18:48:43 +02008052 /*
8053 * For simplicity do a full modeset on any pipe where the output routing
8054 * changed. We could be more clever, but that would require us to be
8055 * more careful with calling the relevant encoder->mode_set functions.
8056 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +02008057 if (*prepare_pipes)
8058 *modeset_pipes = *prepare_pipes;
8059
8060 /* ... and mask these out. */
8061 *modeset_pipes &= ~(*disable_pipes);
8062 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +02008063
8064 /*
8065 * HACK: We don't (yet) fully support global modesets. intel_set_config
8066 * obies this rule, but the modeset restore mode of
8067 * intel_modeset_setup_hw_state does not.
8068 */
8069 *modeset_pipes &= 1 << intel_crtc->pipe;
8070 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +02008071
8072 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
8073 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +02008074}
8075
Daniel Vetterea9d7582012-07-10 10:42:52 +02008076static bool intel_crtc_in_use(struct drm_crtc *crtc)
8077{
8078 struct drm_encoder *encoder;
8079 struct drm_device *dev = crtc->dev;
8080
8081 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
8082 if (encoder->crtc == crtc)
8083 return true;
8084
8085 return false;
8086}
8087
8088static void
8089intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
8090{
8091 struct intel_encoder *intel_encoder;
8092 struct intel_crtc *intel_crtc;
8093 struct drm_connector *connector;
8094
8095 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
8096 base.head) {
8097 if (!intel_encoder->base.crtc)
8098 continue;
8099
8100 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
8101
8102 if (prepare_pipes & (1 << intel_crtc->pipe))
8103 intel_encoder->connectors_active = false;
8104 }
8105
8106 intel_modeset_commit_output_state(dev);
8107
8108 /* Update computed state. */
8109 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
8110 base.head) {
8111 intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
8112 }
8113
8114 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
8115 if (!connector->encoder || !connector->encoder->crtc)
8116 continue;
8117
8118 intel_crtc = to_intel_crtc(connector->encoder->crtc);
8119
8120 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +02008121 struct drm_property *dpms_property =
8122 dev->mode_config.dpms_property;
8123
Daniel Vetterea9d7582012-07-10 10:42:52 +02008124 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -05008125 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +02008126 dpms_property,
8127 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +02008128
8129 intel_encoder = to_intel_encoder(connector->encoder);
8130 intel_encoder->connectors_active = true;
8131 }
8132 }
8133
8134}
8135
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008136static bool intel_fuzzy_clock_check(struct intel_crtc_config *cur,
8137 struct intel_crtc_config *new)
8138{
8139 int clock1, clock2, diff;
8140
8141 clock1 = cur->adjusted_mode.clock;
8142 clock2 = new->adjusted_mode.clock;
8143
8144 if (clock1 == clock2)
8145 return true;
8146
8147 if (!clock1 || !clock2)
8148 return false;
8149
8150 diff = abs(clock1 - clock2);
8151
8152 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
8153 return true;
8154
8155 return false;
8156}
8157
Daniel Vetter25c5b262012-07-08 22:08:04 +02008158#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
8159 list_for_each_entry((intel_crtc), \
8160 &(dev)->mode_config.crtc_list, \
8161 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +02008162 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +02008163
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008164static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008165intel_pipe_config_compare(struct drm_device *dev,
8166 struct intel_crtc_config *current_config,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008167 struct intel_crtc_config *pipe_config)
8168{
Daniel Vetter66e985c2013-06-05 13:34:20 +02008169#define PIPE_CONF_CHECK_X(name) \
8170 if (current_config->name != pipe_config->name) { \
8171 DRM_ERROR("mismatch in " #name " " \
8172 "(expected 0x%08x, found 0x%08x)\n", \
8173 current_config->name, \
8174 pipe_config->name); \
8175 return false; \
8176 }
8177
Daniel Vetter08a24032013-04-19 11:25:34 +02008178#define PIPE_CONF_CHECK_I(name) \
8179 if (current_config->name != pipe_config->name) { \
8180 DRM_ERROR("mismatch in " #name " " \
8181 "(expected %i, found %i)\n", \
8182 current_config->name, \
8183 pipe_config->name); \
8184 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +01008185 }
8186
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008187#define PIPE_CONF_CHECK_FLAGS(name, mask) \
8188 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -07008189 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008190 "(expected %i, found %i)\n", \
8191 current_config->name & (mask), \
8192 pipe_config->name & (mask)); \
8193 return false; \
8194 }
8195
Daniel Vetterbb760062013-06-06 14:55:52 +02008196#define PIPE_CONF_QUIRK(quirk) \
8197 ((current_config->quirks | pipe_config->quirks) & (quirk))
8198
Daniel Vettereccb1402013-05-22 00:50:22 +02008199 PIPE_CONF_CHECK_I(cpu_transcoder);
8200
Daniel Vetter08a24032013-04-19 11:25:34 +02008201 PIPE_CONF_CHECK_I(has_pch_encoder);
8202 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +02008203 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
8204 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
8205 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
8206 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
8207 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +02008208
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008209 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
8210 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
8211 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
8212 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
8213 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
8214 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
8215
8216 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
8217 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
8218 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
8219 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
8220 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
8221 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
8222
Daniel Vetterc93f54c2013-06-27 19:47:19 +02008223 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008224
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008225 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8226 DRM_MODE_FLAG_INTERLACE);
8227
Daniel Vetterbb760062013-06-06 14:55:52 +02008228 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
8229 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8230 DRM_MODE_FLAG_PHSYNC);
8231 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8232 DRM_MODE_FLAG_NHSYNC);
8233 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8234 DRM_MODE_FLAG_PVSYNC);
8235 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8236 DRM_MODE_FLAG_NVSYNC);
8237 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -07008238
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008239 PIPE_CONF_CHECK_I(requested_mode.hdisplay);
8240 PIPE_CONF_CHECK_I(requested_mode.vdisplay);
8241
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008242 PIPE_CONF_CHECK_I(gmch_pfit.control);
8243 /* pfit ratios are autocomputed by the hw on gen4+ */
8244 if (INTEL_INFO(dev)->gen < 4)
8245 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
8246 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
8247 PIPE_CONF_CHECK_I(pch_pfit.pos);
8248 PIPE_CONF_CHECK_I(pch_pfit.size);
8249
Paulo Zanoni42db64e2013-05-31 16:33:22 -03008250 PIPE_CONF_CHECK_I(ips_enabled);
8251
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008252 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008253 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008254 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008255 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
8256 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008257
Daniel Vetter66e985c2013-06-05 13:34:20 +02008258#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +02008259#undef PIPE_CONF_CHECK_I
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008260#undef PIPE_CONF_CHECK_FLAGS
Daniel Vetterbb760062013-06-06 14:55:52 +02008261#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008262
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008263 if (!IS_HASWELL(dev)) {
8264 if (!intel_fuzzy_clock_check(current_config, pipe_config)) {
Jesse Barnes6f024882013-07-01 10:19:09 -07008265 DRM_ERROR("mismatch in clock (expected %d, found %d)\n",
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008266 current_config->adjusted_mode.clock,
8267 pipe_config->adjusted_mode.clock);
8268 return false;
8269 }
8270 }
8271
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008272 return true;
8273}
8274
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02008275static void
8276check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008277{
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008278 struct intel_connector *connector;
8279
8280 list_for_each_entry(connector, &dev->mode_config.connector_list,
8281 base.head) {
8282 /* This also checks the encoder/connector hw state with the
8283 * ->get_hw_state callbacks. */
8284 intel_connector_check_state(connector);
8285
8286 WARN(&connector->new_encoder->base != connector->base.encoder,
8287 "connector's staged encoder doesn't match current encoder\n");
8288 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02008289}
8290
8291static void
8292check_encoder_state(struct drm_device *dev)
8293{
8294 struct intel_encoder *encoder;
8295 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008296
8297 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8298 base.head) {
8299 bool enabled = false;
8300 bool active = false;
8301 enum pipe pipe, tracked_pipe;
8302
8303 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
8304 encoder->base.base.id,
8305 drm_get_encoder_name(&encoder->base));
8306
8307 WARN(&encoder->new_crtc->base != encoder->base.crtc,
8308 "encoder's stage crtc doesn't match current crtc\n");
8309 WARN(encoder->connectors_active && !encoder->base.crtc,
8310 "encoder's active_connectors set, but no crtc\n");
8311
8312 list_for_each_entry(connector, &dev->mode_config.connector_list,
8313 base.head) {
8314 if (connector->base.encoder != &encoder->base)
8315 continue;
8316 enabled = true;
8317 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
8318 active = true;
8319 }
8320 WARN(!!encoder->base.crtc != enabled,
8321 "encoder's enabled state mismatch "
8322 "(expected %i, found %i)\n",
8323 !!encoder->base.crtc, enabled);
8324 WARN(active && !encoder->base.crtc,
8325 "active encoder with no crtc\n");
8326
8327 WARN(encoder->connectors_active != active,
8328 "encoder's computed active state doesn't match tracked active state "
8329 "(expected %i, found %i)\n", active, encoder->connectors_active);
8330
8331 active = encoder->get_hw_state(encoder, &pipe);
8332 WARN(active != encoder->connectors_active,
8333 "encoder's hw state doesn't match sw tracking "
8334 "(expected %i, found %i)\n",
8335 encoder->connectors_active, active);
8336
8337 if (!encoder->base.crtc)
8338 continue;
8339
8340 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
8341 WARN(active && pipe != tracked_pipe,
8342 "active encoder's pipe doesn't match"
8343 "(expected %i, found %i)\n",
8344 tracked_pipe, pipe);
8345
8346 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02008347}
8348
8349static void
8350check_crtc_state(struct drm_device *dev)
8351{
8352 drm_i915_private_t *dev_priv = dev->dev_private;
8353 struct intel_crtc *crtc;
8354 struct intel_encoder *encoder;
8355 struct intel_crtc_config pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008356
8357 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
8358 base.head) {
8359 bool enabled = false;
8360 bool active = false;
8361
Jesse Barnes045ac3b2013-05-14 17:08:26 -07008362 memset(&pipe_config, 0, sizeof(pipe_config));
8363
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008364 DRM_DEBUG_KMS("[CRTC:%d]\n",
8365 crtc->base.base.id);
8366
8367 WARN(crtc->active && !crtc->base.enabled,
8368 "active crtc, but not enabled in sw tracking\n");
8369
8370 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8371 base.head) {
8372 if (encoder->base.crtc != &crtc->base)
8373 continue;
8374 enabled = true;
8375 if (encoder->connectors_active)
8376 active = true;
8377 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008378
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008379 WARN(active != crtc->active,
8380 "crtc's computed active state doesn't match tracked active state "
8381 "(expected %i, found %i)\n", active, crtc->active);
8382 WARN(enabled != crtc->base.enabled,
8383 "crtc's computed enabled state doesn't match tracked enabled state "
8384 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
8385
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008386 active = dev_priv->display.get_pipe_config(crtc,
8387 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +02008388
8389 /* hw state is inconsistent with the pipe A quirk */
8390 if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
8391 active = crtc->active;
8392
Daniel Vetter6c49f242013-06-06 12:45:25 +02008393 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8394 base.head) {
8395 if (encoder->base.crtc != &crtc->base)
8396 continue;
Jesse Barnes510d5f22013-07-01 15:50:17 -07008397 if (encoder->get_config)
Daniel Vetter6c49f242013-06-06 12:45:25 +02008398 encoder->get_config(encoder, &pipe_config);
8399 }
8400
Jesse Barnes510d5f22013-07-01 15:50:17 -07008401 if (dev_priv->display.get_clock)
8402 dev_priv->display.get_clock(crtc, &pipe_config);
8403
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008404 WARN(crtc->active != active,
8405 "crtc active state doesn't match with hw state "
8406 "(expected %i, found %i)\n", crtc->active, active);
8407
Daniel Vetterc0b03412013-05-28 12:05:54 +02008408 if (active &&
8409 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
8410 WARN(1, "pipe state doesn't match!\n");
8411 intel_dump_pipe_config(crtc, &pipe_config,
8412 "[hw state]");
8413 intel_dump_pipe_config(crtc, &crtc->config,
8414 "[sw state]");
8415 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008416 }
8417}
8418
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02008419static void
8420check_shared_dpll_state(struct drm_device *dev)
8421{
8422 drm_i915_private_t *dev_priv = dev->dev_private;
8423 struct intel_crtc *crtc;
8424 struct intel_dpll_hw_state dpll_hw_state;
8425 int i;
Daniel Vetter53589012013-06-05 13:34:16 +02008426
8427 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8428 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
8429 int enabled_crtcs = 0, active_crtcs = 0;
8430 bool active;
8431
8432 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
8433
8434 DRM_DEBUG_KMS("%s\n", pll->name);
8435
8436 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
8437
8438 WARN(pll->active > pll->refcount,
8439 "more active pll users than references: %i vs %i\n",
8440 pll->active, pll->refcount);
8441 WARN(pll->active && !pll->on,
8442 "pll in active use but not on in sw tracking\n");
8443 WARN(pll->on != active,
8444 "pll on state mismatch (expected %i, found %i)\n",
8445 pll->on, active);
8446
8447 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
8448 base.head) {
8449 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
8450 enabled_crtcs++;
8451 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
8452 active_crtcs++;
8453 }
8454 WARN(pll->active != active_crtcs,
8455 "pll active crtcs mismatch (expected %i, found %i)\n",
8456 pll->active, active_crtcs);
8457 WARN(pll->refcount != enabled_crtcs,
8458 "pll enabled crtcs mismatch (expected %i, found %i)\n",
8459 pll->refcount, enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008460
8461 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
8462 sizeof(dpll_hw_state)),
8463 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +02008464 }
Daniel Vettera6778b32012-07-02 09:56:42 +02008465}
8466
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02008467void
8468intel_modeset_check_state(struct drm_device *dev)
8469{
8470 check_connector_state(dev);
8471 check_encoder_state(dev);
8472 check_crtc_state(dev);
8473 check_shared_dpll_state(dev);
8474}
8475
Daniel Vetterf30da182013-04-11 20:22:50 +02008476static int __intel_set_mode(struct drm_crtc *crtc,
8477 struct drm_display_mode *mode,
8478 int x, int y, struct drm_framebuffer *fb)
Daniel Vettera6778b32012-07-02 09:56:42 +02008479{
8480 struct drm_device *dev = crtc->dev;
Daniel Vetterdbf2b54e2012-07-02 11:18:29 +02008481 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008482 struct drm_display_mode *saved_mode, *saved_hwmode;
8483 struct intel_crtc_config *pipe_config = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +02008484 struct intel_crtc *intel_crtc;
8485 unsigned disable_pipes, prepare_pipes, modeset_pipes;
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008486 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +02008487
Tim Gardner3ac18232012-12-07 07:54:26 -07008488 saved_mode = kmalloc(2 * sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008489 if (!saved_mode)
8490 return -ENOMEM;
Tim Gardner3ac18232012-12-07 07:54:26 -07008491 saved_hwmode = saved_mode + 1;
Daniel Vettera6778b32012-07-02 09:56:42 +02008492
Daniel Vettere2e1ed42012-07-08 21:14:38 +02008493 intel_modeset_affected_pipes(crtc, &modeset_pipes,
Daniel Vetter25c5b262012-07-08 22:08:04 +02008494 &prepare_pipes, &disable_pipes);
8495
Tim Gardner3ac18232012-12-07 07:54:26 -07008496 *saved_hwmode = crtc->hwmode;
8497 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02008498
Daniel Vetter25c5b262012-07-08 22:08:04 +02008499 /* Hack: Because we don't (yet) support global modeset on multiple
8500 * crtcs, we don't keep track of the new mode for more than one crtc.
8501 * Hence simply check whether any bit is set in modeset_pipes in all the
8502 * pieces of code that are not yet converted to deal with mutliple crtcs
8503 * changing their mode at the same time. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02008504 if (modeset_pipes) {
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008505 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008506 if (IS_ERR(pipe_config)) {
8507 ret = PTR_ERR(pipe_config);
8508 pipe_config = NULL;
8509
Tim Gardner3ac18232012-12-07 07:54:26 -07008510 goto out;
Daniel Vetter25c5b262012-07-08 22:08:04 +02008511 }
Daniel Vetterc0b03412013-05-28 12:05:54 +02008512 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
8513 "[modeset]");
Daniel Vettera6778b32012-07-02 09:56:42 +02008514 }
8515
Daniel Vetter460da9162013-03-27 00:44:51 +01008516 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
8517 intel_crtc_disable(&intel_crtc->base);
8518
Daniel Vetterea9d7582012-07-10 10:42:52 +02008519 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
8520 if (intel_crtc->base.enabled)
8521 dev_priv->display.crtc_disable(&intel_crtc->base);
8522 }
Daniel Vettera6778b32012-07-02 09:56:42 +02008523
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02008524 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
8525 * to set it here already despite that we pass it down the callchain.
8526 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008527 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +02008528 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008529 /* mode_set/enable/disable functions rely on a correct pipe
8530 * config. */
8531 to_intel_crtc(crtc)->config = *pipe_config;
8532 }
Daniel Vetter7758a112012-07-08 19:40:39 +02008533
Daniel Vetterea9d7582012-07-10 10:42:52 +02008534 /* Only after disabling all output pipelines that will be changed can we
8535 * update the the output configuration. */
8536 intel_modeset_update_state(dev, prepare_pipes);
8537
Daniel Vetter47fab732012-10-26 10:58:18 +02008538 if (dev_priv->display.modeset_global_resources)
8539 dev_priv->display.modeset_global_resources(dev);
8540
Daniel Vettera6778b32012-07-02 09:56:42 +02008541 /* Set up the DPLL and any encoders state that needs to adjust or depend
8542 * on the DPLL.
8543 */
Daniel Vetter25c5b262012-07-08 22:08:04 +02008544 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008545 ret = intel_crtc_mode_set(&intel_crtc->base,
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008546 x, y, fb);
8547 if (ret)
8548 goto done;
Daniel Vettera6778b32012-07-02 09:56:42 +02008549 }
8550
8551 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02008552 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
8553 dev_priv->display.crtc_enable(&intel_crtc->base);
Daniel Vettera6778b32012-07-02 09:56:42 +02008554
Daniel Vetter25c5b262012-07-08 22:08:04 +02008555 if (modeset_pipes) {
8556 /* Store real post-adjustment hardware mode. */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008557 crtc->hwmode = pipe_config->adjusted_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02008558
Daniel Vetter25c5b262012-07-08 22:08:04 +02008559 /* Calculate and store various constants which
8560 * are later needed by vblank and swap-completion
8561 * timestamping. They are derived from true hwmode.
8562 */
8563 drm_calc_timestamping_constants(crtc);
8564 }
Daniel Vettera6778b32012-07-02 09:56:42 +02008565
8566 /* FIXME: add subpixel order */
8567done:
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008568 if (ret && crtc->enabled) {
Tim Gardner3ac18232012-12-07 07:54:26 -07008569 crtc->hwmode = *saved_hwmode;
8570 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02008571 }
8572
Tim Gardner3ac18232012-12-07 07:54:26 -07008573out:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008574 kfree(pipe_config);
Tim Gardner3ac18232012-12-07 07:54:26 -07008575 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +02008576 return ret;
8577}
8578
Daniel Vetterf30da182013-04-11 20:22:50 +02008579int intel_set_mode(struct drm_crtc *crtc,
8580 struct drm_display_mode *mode,
8581 int x, int y, struct drm_framebuffer *fb)
8582{
8583 int ret;
8584
8585 ret = __intel_set_mode(crtc, mode, x, y, fb);
8586
8587 if (ret == 0)
8588 intel_modeset_check_state(crtc->dev);
8589
8590 return ret;
8591}
8592
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008593void intel_crtc_restore_mode(struct drm_crtc *crtc)
8594{
8595 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->fb);
8596}
8597
Daniel Vetter25c5b262012-07-08 22:08:04 +02008598#undef for_each_intel_crtc_masked
8599
Daniel Vetterd9e55602012-07-04 22:16:09 +02008600static void intel_set_config_free(struct intel_set_config *config)
8601{
8602 if (!config)
8603 return;
8604
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008605 kfree(config->save_connector_encoders);
8606 kfree(config->save_encoder_crtcs);
Daniel Vetterd9e55602012-07-04 22:16:09 +02008607 kfree(config);
8608}
8609
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008610static int intel_set_config_save_state(struct drm_device *dev,
8611 struct intel_set_config *config)
8612{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008613 struct drm_encoder *encoder;
8614 struct drm_connector *connector;
8615 int count;
8616
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008617 config->save_encoder_crtcs =
8618 kcalloc(dev->mode_config.num_encoder,
8619 sizeof(struct drm_crtc *), GFP_KERNEL);
8620 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008621 return -ENOMEM;
8622
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008623 config->save_connector_encoders =
8624 kcalloc(dev->mode_config.num_connector,
8625 sizeof(struct drm_encoder *), GFP_KERNEL);
8626 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008627 return -ENOMEM;
8628
8629 /* Copy data. Note that driver private data is not affected.
8630 * Should anything bad happen only the expected state is
8631 * restored, not the drivers personal bookkeeping.
8632 */
8633 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008634 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008635 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008636 }
8637
8638 count = 0;
8639 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008640 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008641 }
8642
8643 return 0;
8644}
8645
8646static void intel_set_config_restore_state(struct drm_device *dev,
8647 struct intel_set_config *config)
8648{
Daniel Vetter9a935852012-07-05 22:34:27 +02008649 struct intel_encoder *encoder;
8650 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008651 int count;
8652
8653 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008654 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
8655 encoder->new_crtc =
8656 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008657 }
8658
8659 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008660 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
8661 connector->new_encoder =
8662 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008663 }
8664}
8665
Imre Deake3de42b2013-05-03 19:44:07 +02008666static bool
8667is_crtc_connector_off(struct drm_crtc *crtc, struct drm_connector *connectors,
8668 int num_connectors)
8669{
8670 int i;
8671
8672 for (i = 0; i < num_connectors; i++)
8673 if (connectors[i].encoder &&
8674 connectors[i].encoder->crtc == crtc &&
8675 connectors[i].dpms != DRM_MODE_DPMS_ON)
8676 return true;
8677
8678 return false;
8679}
8680
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008681static void
8682intel_set_config_compute_mode_changes(struct drm_mode_set *set,
8683 struct intel_set_config *config)
8684{
8685
8686 /* We should be able to check here if the fb has the same properties
8687 * and then just flip_or_move it */
Imre Deake3de42b2013-05-03 19:44:07 +02008688 if (set->connectors != NULL &&
8689 is_crtc_connector_off(set->crtc, *set->connectors,
8690 set->num_connectors)) {
8691 config->mode_changed = true;
8692 } else if (set->crtc->fb != set->fb) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008693 /* If we have no fb then treat it as a full mode set */
8694 if (set->crtc->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +03008695 struct intel_crtc *intel_crtc =
8696 to_intel_crtc(set->crtc);
8697
8698 if (intel_crtc->active && i915_fastboot) {
8699 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
8700 config->fb_changed = true;
8701 } else {
8702 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
8703 config->mode_changed = true;
8704 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008705 } else if (set->fb == NULL) {
8706 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +01008707 } else if (set->fb->pixel_format !=
8708 set->crtc->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008709 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02008710 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008711 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02008712 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008713 }
8714
Daniel Vetter835c5872012-07-10 18:11:08 +02008715 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008716 config->fb_changed = true;
8717
8718 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
8719 DRM_DEBUG_KMS("modes are different, full mode set\n");
8720 drm_mode_debug_printmodeline(&set->crtc->mode);
8721 drm_mode_debug_printmodeline(set->mode);
8722 config->mode_changed = true;
8723 }
8724}
8725
Daniel Vetter2e431052012-07-04 22:42:15 +02008726static int
Daniel Vetter9a935852012-07-05 22:34:27 +02008727intel_modeset_stage_output_state(struct drm_device *dev,
8728 struct drm_mode_set *set,
8729 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +02008730{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008731 struct drm_crtc *new_crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02008732 struct intel_connector *connector;
8733 struct intel_encoder *encoder;
Daniel Vetter2e431052012-07-04 22:42:15 +02008734 int count, ro;
Daniel Vetter50f56112012-07-02 09:35:43 +02008735
Damien Lespiau9abdda72013-02-13 13:29:23 +00008736 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +02008737 * of connectors. For paranoia, double-check this. */
8738 WARN_ON(!set->fb && (set->num_connectors != 0));
8739 WARN_ON(set->fb && (set->num_connectors == 0));
8740
Daniel Vetter50f56112012-07-02 09:35:43 +02008741 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008742 list_for_each_entry(connector, &dev->mode_config.connector_list,
8743 base.head) {
8744 /* Otherwise traverse passed in connector list and get encoders
8745 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +02008746 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02008747 if (set->connectors[ro] == &connector->base) {
8748 connector->new_encoder = connector->encoder;
Daniel Vetter50f56112012-07-02 09:35:43 +02008749 break;
8750 }
8751 }
8752
Daniel Vetter9a935852012-07-05 22:34:27 +02008753 /* If we disable the crtc, disable all its connectors. Also, if
8754 * the connector is on the changing crtc but not on the new
8755 * connector list, disable it. */
8756 if ((!set->fb || ro == set->num_connectors) &&
8757 connector->base.encoder &&
8758 connector->base.encoder->crtc == set->crtc) {
8759 connector->new_encoder = NULL;
8760
8761 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
8762 connector->base.base.id,
8763 drm_get_connector_name(&connector->base));
8764 }
8765
8766
8767 if (&connector->new_encoder->base != connector->base.encoder) {
Daniel Vetter50f56112012-07-02 09:35:43 +02008768 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008769 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02008770 }
Daniel Vetter9a935852012-07-05 22:34:27 +02008771 }
8772 /* connector->new_encoder is now updated for all connectors. */
8773
8774 /* Update crtc of enabled connectors. */
Daniel Vetter50f56112012-07-02 09:35:43 +02008775 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008776 list_for_each_entry(connector, &dev->mode_config.connector_list,
8777 base.head) {
8778 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +02008779 continue;
8780
Daniel Vetter9a935852012-07-05 22:34:27 +02008781 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +02008782
8783 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02008784 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +02008785 new_crtc = set->crtc;
8786 }
8787
8788 /* Make sure the new CRTC will work with the encoder */
Daniel Vetter9a935852012-07-05 22:34:27 +02008789 if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
8790 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008791 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +02008792 }
Daniel Vetter9a935852012-07-05 22:34:27 +02008793 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
8794
8795 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
8796 connector->base.base.id,
8797 drm_get_connector_name(&connector->base),
8798 new_crtc->base.id);
8799 }
8800
8801 /* Check for any encoders that needs to be disabled. */
8802 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8803 base.head) {
8804 list_for_each_entry(connector,
8805 &dev->mode_config.connector_list,
8806 base.head) {
8807 if (connector->new_encoder == encoder) {
8808 WARN_ON(!connector->new_encoder->new_crtc);
8809
8810 goto next_encoder;
8811 }
8812 }
8813 encoder->new_crtc = NULL;
8814next_encoder:
8815 /* Only now check for crtc changes so we don't miss encoders
8816 * that will be disabled. */
8817 if (&encoder->new_crtc->base != encoder->base.crtc) {
Daniel Vetter50f56112012-07-02 09:35:43 +02008818 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008819 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02008820 }
8821 }
Daniel Vetter9a935852012-07-05 22:34:27 +02008822 /* Now we've also updated encoder->new_crtc for all encoders. */
Daniel Vetter50f56112012-07-02 09:35:43 +02008823
Daniel Vetter2e431052012-07-04 22:42:15 +02008824 return 0;
8825}
8826
8827static int intel_crtc_set_config(struct drm_mode_set *set)
8828{
8829 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +02008830 struct drm_mode_set save_set;
8831 struct intel_set_config *config;
8832 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +02008833
Daniel Vetter8d3e3752012-07-05 16:09:09 +02008834 BUG_ON(!set);
8835 BUG_ON(!set->crtc);
8836 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +02008837
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01008838 /* Enforce sane interface api - has been abused by the fb helper. */
8839 BUG_ON(!set->mode && set->fb);
8840 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +02008841
Daniel Vetter2e431052012-07-04 22:42:15 +02008842 if (set->fb) {
8843 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
8844 set->crtc->base.id, set->fb->base.id,
8845 (int)set->num_connectors, set->x, set->y);
8846 } else {
8847 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +02008848 }
8849
8850 dev = set->crtc->dev;
8851
8852 ret = -ENOMEM;
8853 config = kzalloc(sizeof(*config), GFP_KERNEL);
8854 if (!config)
8855 goto out_config;
8856
8857 ret = intel_set_config_save_state(dev, config);
8858 if (ret)
8859 goto out_config;
8860
8861 save_set.crtc = set->crtc;
8862 save_set.mode = &set->crtc->mode;
8863 save_set.x = set->crtc->x;
8864 save_set.y = set->crtc->y;
8865 save_set.fb = set->crtc->fb;
8866
8867 /* Compute whether we need a full modeset, only an fb base update or no
8868 * change at all. In the future we might also check whether only the
8869 * mode changed, e.g. for LVDS where we only change the panel fitter in
8870 * such cases. */
8871 intel_set_config_compute_mode_changes(set, config);
8872
Daniel Vetter9a935852012-07-05 22:34:27 +02008873 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +02008874 if (ret)
8875 goto fail;
8876
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008877 if (config->mode_changed) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008878 ret = intel_set_mode(set->crtc, set->mode,
8879 set->x, set->y, set->fb);
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008880 } else if (config->fb_changed) {
Ville Syrjälä4878cae2013-02-18 19:08:48 +02008881 intel_crtc_wait_for_pending_flips(set->crtc);
8882
Daniel Vetter4f660f42012-07-02 09:47:37 +02008883 ret = intel_pipe_set_base(set->crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +02008884 set->x, set->y, set->fb);
Daniel Vetter50f56112012-07-02 09:35:43 +02008885 }
8886
Chris Wilson2d05eae2013-05-03 17:36:25 +01008887 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +02008888 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
8889 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +02008890fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +01008891 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +02008892
Chris Wilson2d05eae2013-05-03 17:36:25 +01008893 /* Try to restore the config */
8894 if (config->mode_changed &&
8895 intel_set_mode(save_set.crtc, save_set.mode,
8896 save_set.x, save_set.y, save_set.fb))
8897 DRM_ERROR("failed to restore config after modeset failure\n");
8898 }
Daniel Vetter50f56112012-07-02 09:35:43 +02008899
Daniel Vetterd9e55602012-07-04 22:16:09 +02008900out_config:
8901 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +02008902 return ret;
8903}
8904
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008905static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008906 .cursor_set = intel_crtc_cursor_set,
8907 .cursor_move = intel_crtc_cursor_move,
8908 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +02008909 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008910 .destroy = intel_crtc_destroy,
8911 .page_flip = intel_crtc_page_flip,
8912};
8913
Paulo Zanoni79f689a2012-10-05 12:05:52 -03008914static void intel_cpu_pll_init(struct drm_device *dev)
8915{
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008916 if (HAS_DDI(dev))
Paulo Zanoni79f689a2012-10-05 12:05:52 -03008917 intel_ddi_pll_init(dev);
8918}
8919
Daniel Vetter53589012013-06-05 13:34:16 +02008920static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
8921 struct intel_shared_dpll *pll,
8922 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008923{
Daniel Vetter53589012013-06-05 13:34:16 +02008924 uint32_t val;
8925
8926 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +02008927 hw_state->dpll = val;
8928 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
8929 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +02008930
8931 return val & DPLL_VCO_ENABLE;
8932}
8933
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02008934static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
8935 struct intel_shared_dpll *pll)
8936{
8937 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
8938 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
8939}
8940
Daniel Vettere7b903d2013-06-05 13:34:14 +02008941static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
8942 struct intel_shared_dpll *pll)
8943{
Daniel Vettere7b903d2013-06-05 13:34:14 +02008944 /* PCH refclock must be enabled first */
8945 assert_pch_refclk_enabled(dev_priv);
8946
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02008947 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
8948
8949 /* Wait for the clocks to stabilize. */
8950 POSTING_READ(PCH_DPLL(pll->id));
8951 udelay(150);
8952
8953 /* The pixel multiplier can only be updated once the
8954 * DPLL is enabled and the clocks are stable.
8955 *
8956 * So write it again.
8957 */
8958 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
8959 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +02008960 udelay(200);
8961}
8962
8963static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
8964 struct intel_shared_dpll *pll)
8965{
8966 struct drm_device *dev = dev_priv->dev;
8967 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +02008968
8969 /* Make sure no transcoder isn't still depending on us. */
8970 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
8971 if (intel_crtc_to_shared_dpll(crtc) == pll)
8972 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
8973 }
8974
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02008975 I915_WRITE(PCH_DPLL(pll->id), 0);
8976 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +02008977 udelay(200);
8978}
8979
Daniel Vetter46edb022013-06-05 13:34:12 +02008980static char *ibx_pch_dpll_names[] = {
8981 "PCH DPLL A",
8982 "PCH DPLL B",
8983};
8984
Daniel Vetter7c74ade2013-06-05 13:34:11 +02008985static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008986{
Daniel Vettere7b903d2013-06-05 13:34:14 +02008987 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008988 int i;
8989
Daniel Vetter7c74ade2013-06-05 13:34:11 +02008990 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008991
Daniel Vettere72f9fb2013-06-05 13:34:06 +02008992 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +02008993 dev_priv->shared_dplls[i].id = i;
8994 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02008995 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +02008996 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
8997 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +02008998 dev_priv->shared_dplls[i].get_hw_state =
8999 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009000 }
9001}
9002
Daniel Vetter7c74ade2013-06-05 13:34:11 +02009003static void intel_shared_dpll_init(struct drm_device *dev)
9004{
Daniel Vettere7b903d2013-06-05 13:34:14 +02009005 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +02009006
9007 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
9008 ibx_pch_dpll_init(dev);
9009 else
9010 dev_priv->num_shared_dpll = 0;
9011
9012 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
9013 DRM_DEBUG_KMS("%i shared PLLs initialized\n",
9014 dev_priv->num_shared_dpll);
9015}
9016
Hannes Ederb358d0a2008-12-18 21:18:47 +01009017static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -08009018{
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08009019 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08009020 struct intel_crtc *intel_crtc;
9021 int i;
9022
9023 intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
9024 if (intel_crtc == NULL)
9025 return;
9026
9027 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
9028
9029 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -08009030 for (i = 0; i < 256; i++) {
9031 intel_crtc->lut_r[i] = i;
9032 intel_crtc->lut_g[i] = i;
9033 intel_crtc->lut_b[i] = i;
9034 }
9035
Jesse Barnes80824002009-09-10 15:28:06 -07009036 /* Swap pipes & planes for FBC on pre-965 */
9037 intel_crtc->pipe = pipe;
9038 intel_crtc->plane = pipe;
Chris Wilsone2e767a2010-09-13 16:53:12 +01009039 if (IS_MOBILE(dev) && IS_GEN3(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08009040 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +01009041 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07009042 }
9043
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08009044 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
9045 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
9046 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
9047 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
9048
Jesse Barnes79e53942008-11-07 14:24:08 -08009049 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -08009050}
9051
Carl Worth08d7b3d2009-04-29 14:43:54 -07009052int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00009053 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -07009054{
Carl Worth08d7b3d2009-04-29 14:43:54 -07009055 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +02009056 struct drm_mode_object *drmmode_obj;
9057 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009058
Daniel Vetter1cff8f62012-04-24 09:55:08 +02009059 if (!drm_core_check_feature(dev, DRIVER_MODESET))
9060 return -ENODEV;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009061
Daniel Vetterc05422d2009-08-11 16:05:30 +02009062 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
9063 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -07009064
Daniel Vetterc05422d2009-08-11 16:05:30 +02009065 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -07009066 DRM_ERROR("no such CRTC id\n");
9067 return -EINVAL;
9068 }
9069
Daniel Vetterc05422d2009-08-11 16:05:30 +02009070 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
9071 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009072
Daniel Vetterc05422d2009-08-11 16:05:30 +02009073 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009074}
9075
Daniel Vetter66a92782012-07-12 20:08:18 +02009076static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08009077{
Daniel Vetter66a92782012-07-12 20:08:18 +02009078 struct drm_device *dev = encoder->base.dev;
9079 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08009080 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009081 int entry = 0;
9082
Daniel Vetter66a92782012-07-12 20:08:18 +02009083 list_for_each_entry(source_encoder,
9084 &dev->mode_config.encoder_list, base.head) {
9085
9086 if (encoder == source_encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08009087 index_mask |= (1 << entry);
Daniel Vetter66a92782012-07-12 20:08:18 +02009088
9089 /* Intel hw has only one MUX where enocoders could be cloned. */
9090 if (encoder->cloneable && source_encoder->cloneable)
9091 index_mask |= (1 << entry);
9092
Jesse Barnes79e53942008-11-07 14:24:08 -08009093 entry++;
9094 }
Chris Wilson4ef69c72010-09-09 15:14:28 +01009095
Jesse Barnes79e53942008-11-07 14:24:08 -08009096 return index_mask;
9097}
9098
Chris Wilson4d302442010-12-14 19:21:29 +00009099static bool has_edp_a(struct drm_device *dev)
9100{
9101 struct drm_i915_private *dev_priv = dev->dev_private;
9102
9103 if (!IS_MOBILE(dev))
9104 return false;
9105
9106 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
9107 return false;
9108
9109 if (IS_GEN5(dev) &&
9110 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
9111 return false;
9112
9113 return true;
9114}
9115
Jesse Barnes79e53942008-11-07 14:24:08 -08009116static void intel_setup_outputs(struct drm_device *dev)
9117{
Eric Anholt725e30a2009-01-22 13:01:02 -08009118 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +01009119 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009120 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08009121
Daniel Vetterc9093352013-06-06 22:22:47 +02009122 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009123
Paulo Zanonic40c0f52013-04-12 18:16:53 -03009124 if (!IS_ULT(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -02009125 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009126
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009127 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -03009128 int found;
9129
9130 /* Haswell uses DDI functions to detect digital outputs */
9131 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
9132 /* DDI A only supports eDP */
9133 if (found)
9134 intel_ddi_init(dev, PORT_A);
9135
9136 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
9137 * register */
9138 found = I915_READ(SFUSE_STRAP);
9139
9140 if (found & SFUSE_STRAP_DDIB_DETECTED)
9141 intel_ddi_init(dev, PORT_B);
9142 if (found & SFUSE_STRAP_DDIC_DETECTED)
9143 intel_ddi_init(dev, PORT_C);
9144 if (found & SFUSE_STRAP_DDID_DETECTED)
9145 intel_ddi_init(dev, PORT_D);
9146 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009147 int found;
Daniel Vetter270b3042012-10-27 15:52:05 +02009148 dpd_is_edp = intel_dpd_is_edp(dev);
9149
9150 if (has_edp_a(dev))
9151 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009152
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009153 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +08009154 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +01009155 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009156 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -03009157 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08009158 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009159 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009160 }
9161
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009162 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -03009163 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009164
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009165 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -03009166 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009167
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08009168 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009169 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08009170
Daniel Vetter270b3042012-10-27 15:52:05 +02009171 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009172 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -07009173 } else if (IS_VALLEYVIEW(dev)) {
Gajanan Bhat19c03922012-09-27 19:13:07 +05309174 /* Check for built-in panel first. Shares lanes with HDMI on SDVOC */
Ville Syrjälä67cfc202013-01-25 21:44:44 +02009175 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
9176 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +05309177
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009178 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
Paulo Zanonie2debe92013-02-18 19:00:27 -03009179 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
9180 PORT_B);
Ville Syrjälä67cfc202013-01-25 21:44:44 +02009181 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
9182 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Jesse Barnes4a87d652012-06-15 11:55:16 -07009183 }
Zhenyu Wang103a1962009-11-27 11:44:36 +08009184 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +08009185 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -08009186
Paulo Zanonie2debe92013-02-18 19:00:27 -03009187 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009188 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009189 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009190 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
9191 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009192 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009193 }
Ma Ling27185ae2009-08-24 13:50:23 +08009194
Imre Deake7281ea2013-05-08 13:14:08 +03009195 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009196 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -08009197 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -04009198
9199 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -04009200
Paulo Zanonie2debe92013-02-18 19:00:27 -03009201 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009202 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009203 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009204 }
Ma Ling27185ae2009-08-24 13:50:23 +08009205
Paulo Zanonie2debe92013-02-18 19:00:27 -03009206 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +08009207
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009208 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
9209 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009210 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009211 }
Imre Deake7281ea2013-05-08 13:14:08 +03009212 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009213 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -08009214 }
Ma Ling27185ae2009-08-24 13:50:23 +08009215
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009216 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +03009217 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009218 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -07009219 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08009220 intel_dvo_init(dev);
9221
Zhenyu Wang103a1962009-11-27 11:44:36 +08009222 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08009223 intel_tv_init(dev);
9224
Chris Wilson4ef69c72010-09-09 15:14:28 +01009225 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
9226 encoder->base.possible_crtcs = encoder->crtc_mask;
9227 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +02009228 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -08009229 }
Chris Wilson47356eb2011-01-11 17:06:04 +00009230
Paulo Zanonidde86e22012-12-01 12:04:25 -02009231 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +02009232
9233 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009234}
9235
9236static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
9237{
9238 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08009239
9240 drm_framebuffer_cleanup(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00009241 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08009242
9243 kfree(intel_fb);
9244}
9245
9246static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +00009247 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08009248 unsigned int *handle)
9249{
9250 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00009251 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08009252
Chris Wilson05394f32010-11-08 19:18:58 +00009253 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -08009254}
9255
9256static const struct drm_framebuffer_funcs intel_fb_funcs = {
9257 .destroy = intel_user_framebuffer_destroy,
9258 .create_handle = intel_user_framebuffer_create_handle,
9259};
9260
Dave Airlie38651672010-03-30 05:34:13 +00009261int intel_framebuffer_init(struct drm_device *dev,
9262 struct intel_framebuffer *intel_fb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009263 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilson05394f32010-11-08 19:18:58 +00009264 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -08009265{
Chris Wilsona35cdaa2013-06-25 17:26:45 +01009266 int pitch_limit;
Jesse Barnes79e53942008-11-07 14:24:08 -08009267 int ret;
9268
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009269 if (obj->tiling_mode == I915_TILING_Y) {
9270 DRM_DEBUG("hardware does not support tiling Y\n");
Chris Wilson57cd6502010-08-08 12:34:44 +01009271 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009272 }
Chris Wilson57cd6502010-08-08 12:34:44 +01009273
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009274 if (mode_cmd->pitches[0] & 63) {
9275 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
9276 mode_cmd->pitches[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +01009277 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009278 }
Chris Wilson57cd6502010-08-08 12:34:44 +01009279
Chris Wilsona35cdaa2013-06-25 17:26:45 +01009280 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
9281 pitch_limit = 32*1024;
9282 } else if (INTEL_INFO(dev)->gen >= 4) {
9283 if (obj->tiling_mode)
9284 pitch_limit = 16*1024;
9285 else
9286 pitch_limit = 32*1024;
9287 } else if (INTEL_INFO(dev)->gen >= 3) {
9288 if (obj->tiling_mode)
9289 pitch_limit = 8*1024;
9290 else
9291 pitch_limit = 16*1024;
9292 } else
9293 /* XXX DSPC is limited to 4k tiled */
9294 pitch_limit = 8*1024;
9295
9296 if (mode_cmd->pitches[0] > pitch_limit) {
9297 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
9298 obj->tiling_mode ? "tiled" : "linear",
9299 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02009300 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009301 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02009302
9303 if (obj->tiling_mode != I915_TILING_NONE &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009304 mode_cmd->pitches[0] != obj->stride) {
9305 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
9306 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02009307 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009308 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02009309
Ville Syrjälä57779d02012-10-31 17:50:14 +02009310 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009311 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +02009312 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02009313 case DRM_FORMAT_RGB565:
9314 case DRM_FORMAT_XRGB8888:
9315 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02009316 break;
9317 case DRM_FORMAT_XRGB1555:
9318 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009319 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +00009320 DRM_DEBUG("unsupported pixel format: %s\n",
9321 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +02009322 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009323 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02009324 break;
9325 case DRM_FORMAT_XBGR8888:
9326 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +02009327 case DRM_FORMAT_XRGB2101010:
9328 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02009329 case DRM_FORMAT_XBGR2101010:
9330 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009331 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +00009332 DRM_DEBUG("unsupported pixel format: %s\n",
9333 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +02009334 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009335 }
Jesse Barnesb5626742011-06-24 12:19:27 -07009336 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02009337 case DRM_FORMAT_YUYV:
9338 case DRM_FORMAT_UYVY:
9339 case DRM_FORMAT_YVYU:
9340 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009341 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +00009342 DRM_DEBUG("unsupported pixel format: %s\n",
9343 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +02009344 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009345 }
Chris Wilson57cd6502010-08-08 12:34:44 +01009346 break;
9347 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +00009348 DRM_DEBUG("unsupported pixel format: %s\n",
9349 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +01009350 return -EINVAL;
9351 }
9352
Ville Syrjälä90f9a332012-10-31 17:50:19 +02009353 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
9354 if (mode_cmd->offsets[0] != 0)
9355 return -EINVAL;
9356
Daniel Vetterc7d73f62012-12-13 23:38:38 +01009357 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
9358 intel_fb->obj = obj;
9359
Jesse Barnes79e53942008-11-07 14:24:08 -08009360 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
9361 if (ret) {
9362 DRM_ERROR("framebuffer init failed %d\n", ret);
9363 return ret;
9364 }
9365
Jesse Barnes79e53942008-11-07 14:24:08 -08009366 return 0;
9367}
9368
Jesse Barnes79e53942008-11-07 14:24:08 -08009369static struct drm_framebuffer *
9370intel_user_framebuffer_create(struct drm_device *dev,
9371 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009372 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -08009373{
Chris Wilson05394f32010-11-08 19:18:58 +00009374 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08009375
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009376 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
9377 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +00009378 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +01009379 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -08009380
Chris Wilsond2dff872011-04-19 08:36:26 +01009381 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08009382}
9383
Jesse Barnes79e53942008-11-07 14:24:08 -08009384static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -08009385 .fb_create = intel_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +00009386 .output_poll_changed = intel_fb_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -08009387};
9388
Jesse Barnese70236a2009-09-21 10:42:27 -07009389/* Set up chip specific display functions */
9390static void intel_init_display(struct drm_device *dev)
9391{
9392 struct drm_i915_private *dev_priv = dev->dev_private;
9393
Daniel Vetteree9300b2013-06-03 22:40:22 +02009394 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
9395 dev_priv->display.find_dpll = g4x_find_best_dpll;
9396 else if (IS_VALLEYVIEW(dev))
9397 dev_priv->display.find_dpll = vlv_find_best_dpll;
9398 else if (IS_PINEVIEW(dev))
9399 dev_priv->display.find_dpll = pnv_find_best_dpll;
9400 else
9401 dev_priv->display.find_dpll = i9xx_find_best_dpll;
9402
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009403 if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009404 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009405 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02009406 dev_priv->display.crtc_enable = haswell_crtc_enable;
9407 dev_priv->display.crtc_disable = haswell_crtc_disable;
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009408 dev_priv->display.off = haswell_crtc_off;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009409 dev_priv->display.update_plane = ironlake_update_plane;
9410 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009411 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009412 dev_priv->display.get_clock = ironlake_crtc_clock_get;
Eric Anholtf564048e2011-03-30 13:01:02 -07009413 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +02009414 dev_priv->display.crtc_enable = ironlake_crtc_enable;
9415 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009416 dev_priv->display.off = ironlake_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -07009417 dev_priv->display.update_plane = ironlake_update_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -07009418 } else if (IS_VALLEYVIEW(dev)) {
9419 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009420 dev_priv->display.get_clock = i9xx_crtc_clock_get;
Jesse Barnes89b667f2013-04-18 14:51:36 -07009421 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
9422 dev_priv->display.crtc_enable = valleyview_crtc_enable;
9423 dev_priv->display.crtc_disable = i9xx_crtc_disable;
9424 dev_priv->display.off = i9xx_crtc_off;
9425 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07009426 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009427 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009428 dev_priv->display.get_clock = i9xx_crtc_clock_get;
Eric Anholtf564048e2011-03-30 13:01:02 -07009429 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +02009430 dev_priv->display.crtc_enable = i9xx_crtc_enable;
9431 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009432 dev_priv->display.off = i9xx_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -07009433 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07009434 }
Jesse Barnese70236a2009-09-21 10:42:27 -07009435
Jesse Barnese70236a2009-09-21 10:42:27 -07009436 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07009437 if (IS_VALLEYVIEW(dev))
9438 dev_priv->display.get_display_clock_speed =
9439 valleyview_get_display_clock_speed;
9440 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -07009441 dev_priv->display.get_display_clock_speed =
9442 i945_get_display_clock_speed;
9443 else if (IS_I915G(dev))
9444 dev_priv->display.get_display_clock_speed =
9445 i915_get_display_clock_speed;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05009446 else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07009447 dev_priv->display.get_display_clock_speed =
9448 i9xx_misc_get_display_clock_speed;
9449 else if (IS_I915GM(dev))
9450 dev_priv->display.get_display_clock_speed =
9451 i915gm_get_display_clock_speed;
9452 else if (IS_I865G(dev))
9453 dev_priv->display.get_display_clock_speed =
9454 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +02009455 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07009456 dev_priv->display.get_display_clock_speed =
9457 i855_get_display_clock_speed;
9458 else /* 852, 830 */
9459 dev_priv->display.get_display_clock_speed =
9460 i830_get_display_clock_speed;
9461
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08009462 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01009463 if (IS_GEN5(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -07009464 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08009465 dev_priv->display.write_eld = ironlake_write_eld;
Yuanhan Liu13982612010-12-15 15:42:31 +08009466 } else if (IS_GEN6(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -07009467 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08009468 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnes357555c2011-04-28 15:09:55 -07009469 } else if (IS_IVYBRIDGE(dev)) {
9470 /* FIXME: detect B0+ stepping and use auto training */
9471 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08009472 dev_priv->display.write_eld = ironlake_write_eld;
Daniel Vetter01a415f2012-10-27 15:58:40 +02009473 dev_priv->display.modeset_global_resources =
9474 ivb_modeset_global_resources;
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -03009475 } else if (IS_HASWELL(dev)) {
9476 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Wang Xingchao83358c852012-08-16 22:43:37 +08009477 dev_priv->display.write_eld = haswell_write_eld;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02009478 dev_priv->display.modeset_global_resources =
9479 haswell_modeset_global_resources;
Paulo Zanonia0e63c22012-12-06 11:12:39 -02009480 }
Jesse Barnes6067aae2011-04-28 15:04:31 -07009481 } else if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +08009482 dev_priv->display.write_eld = g4x_write_eld;
Jesse Barnese70236a2009-09-21 10:42:27 -07009483 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009484
9485 /* Default just returns -ENODEV to indicate unsupported */
9486 dev_priv->display.queue_flip = intel_default_queue_flip;
9487
9488 switch (INTEL_INFO(dev)->gen) {
9489 case 2:
9490 dev_priv->display.queue_flip = intel_gen2_queue_flip;
9491 break;
9492
9493 case 3:
9494 dev_priv->display.queue_flip = intel_gen3_queue_flip;
9495 break;
9496
9497 case 4:
9498 case 5:
9499 dev_priv->display.queue_flip = intel_gen4_queue_flip;
9500 break;
9501
9502 case 6:
9503 dev_priv->display.queue_flip = intel_gen6_queue_flip;
9504 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009505 case 7:
9506 dev_priv->display.queue_flip = intel_gen7_queue_flip;
9507 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009508 }
Jesse Barnese70236a2009-09-21 10:42:27 -07009509}
9510
Jesse Barnesb690e962010-07-19 13:53:12 -07009511/*
9512 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
9513 * resume, or other times. This quirk makes sure that's the case for
9514 * affected systems.
9515 */
Akshay Joshi0206e352011-08-16 15:34:10 -04009516static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -07009517{
9518 struct drm_i915_private *dev_priv = dev->dev_private;
9519
9520 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02009521 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -07009522}
9523
Keith Packard435793d2011-07-12 14:56:22 -07009524/*
9525 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
9526 */
9527static void quirk_ssc_force_disable(struct drm_device *dev)
9528{
9529 struct drm_i915_private *dev_priv = dev->dev_private;
9530 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02009531 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -07009532}
9533
Carsten Emde4dca20e2012-03-15 15:56:26 +01009534/*
Carsten Emde5a15ab52012-03-15 15:56:27 +01009535 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
9536 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +01009537 */
9538static void quirk_invert_brightness(struct drm_device *dev)
9539{
9540 struct drm_i915_private *dev_priv = dev->dev_private;
9541 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02009542 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -07009543}
9544
9545struct intel_quirk {
9546 int device;
9547 int subsystem_vendor;
9548 int subsystem_device;
9549 void (*hook)(struct drm_device *dev);
9550};
9551
Egbert Eich5f85f1762012-10-14 15:46:38 +02009552/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
9553struct intel_dmi_quirk {
9554 void (*hook)(struct drm_device *dev);
9555 const struct dmi_system_id (*dmi_id_list)[];
9556};
9557
9558static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
9559{
9560 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
9561 return 1;
9562}
9563
9564static const struct intel_dmi_quirk intel_dmi_quirks[] = {
9565 {
9566 .dmi_id_list = &(const struct dmi_system_id[]) {
9567 {
9568 .callback = intel_dmi_reverse_brightness,
9569 .ident = "NCR Corporation",
9570 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
9571 DMI_MATCH(DMI_PRODUCT_NAME, ""),
9572 },
9573 },
9574 { } /* terminating entry */
9575 },
9576 .hook = quirk_invert_brightness,
9577 },
9578};
9579
Ben Widawskyc43b5632012-04-16 14:07:40 -07009580static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -07009581 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -04009582 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -07009583
Jesse Barnesb690e962010-07-19 13:53:12 -07009584 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
9585 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
9586
Jesse Barnesb690e962010-07-19 13:53:12 -07009587 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
9588 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
9589
Daniel Vetterccd0d362012-10-10 23:13:59 +02009590 /* 830/845 need to leave pipe A & dpll A up */
Jesse Barnesb690e962010-07-19 13:53:12 -07009591 { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Daniel Vetterdcdaed62012-08-12 21:19:34 +02009592 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Keith Packard435793d2011-07-12 14:56:22 -07009593
9594 /* Lenovo U160 cannot use SSC on LVDS */
9595 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +02009596
9597 /* Sony Vaio Y cannot use SSC on LVDS */
9598 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +01009599
9600 /* Acer Aspire 5734Z must invert backlight brightness */
9601 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
Jani Nikula1ffff602013-01-22 12:50:34 +02009602
9603 /* Acer/eMachines G725 */
9604 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
Jani Nikula01e3a8f2013-01-22 12:50:35 +02009605
9606 /* Acer/eMachines e725 */
9607 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
Jani Nikula5559eca2013-01-22 12:50:36 +02009608
9609 /* Acer/Packard Bell NCL20 */
9610 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
Daniel Vetterac4199e2013-02-15 18:35:30 +01009611
9612 /* Acer Aspire 4736Z */
9613 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jesse Barnesb690e962010-07-19 13:53:12 -07009614};
9615
9616static void intel_init_quirks(struct drm_device *dev)
9617{
9618 struct pci_dev *d = dev->pdev;
9619 int i;
9620
9621 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
9622 struct intel_quirk *q = &intel_quirks[i];
9623
9624 if (d->device == q->device &&
9625 (d->subsystem_vendor == q->subsystem_vendor ||
9626 q->subsystem_vendor == PCI_ANY_ID) &&
9627 (d->subsystem_device == q->subsystem_device ||
9628 q->subsystem_device == PCI_ANY_ID))
9629 q->hook(dev);
9630 }
Egbert Eich5f85f1762012-10-14 15:46:38 +02009631 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
9632 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
9633 intel_dmi_quirks[i].hook(dev);
9634 }
Jesse Barnesb690e962010-07-19 13:53:12 -07009635}
9636
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009637/* Disable the VGA plane that we never use */
9638static void i915_disable_vga(struct drm_device *dev)
9639{
9640 struct drm_i915_private *dev_priv = dev->dev_private;
9641 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +02009642 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009643
9644 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -07009645 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009646 sr1 = inb(VGA_SR_DATA);
9647 outb(sr1 | 1<<5, VGA_SR_DATA);
9648 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
9649 udelay(300);
9650
9651 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9652 POSTING_READ(vga_reg);
9653}
9654
Daniel Vetterf8175862012-04-10 15:50:11 +02009655void intel_modeset_init_hw(struct drm_device *dev)
9656{
Paulo Zanonifa42e232013-01-25 16:59:11 -02009657 intel_init_power_well(dev);
Eugeni Dodonov0232e922012-07-06 15:42:36 -03009658
Eugeni Dodonova8f78b52012-06-28 15:55:35 -03009659 intel_prepare_ddi(dev);
9660
Daniel Vetterf8175862012-04-10 15:50:11 +02009661 intel_init_clock_gating(dev);
9662
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02009663 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02009664 intel_enable_gt_powersave(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02009665 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf8175862012-04-10 15:50:11 +02009666}
9667
Imre Deak7d708ee2013-04-17 14:04:50 +03009668void intel_modeset_suspend_hw(struct drm_device *dev)
9669{
9670 intel_suspend_hw(dev);
9671}
9672
Jesse Barnes79e53942008-11-07 14:24:08 -08009673void intel_modeset_init(struct drm_device *dev)
9674{
Jesse Barnes652c3932009-08-17 13:31:43 -07009675 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7f1f3852013-04-02 11:22:20 -07009676 int i, j, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08009677
9678 drm_mode_config_init(dev);
9679
9680 dev->mode_config.min_width = 0;
9681 dev->mode_config.min_height = 0;
9682
Dave Airlie019d96c2011-09-29 16:20:42 +01009683 dev->mode_config.preferred_depth = 24;
9684 dev->mode_config.prefer_shadow = 1;
9685
Laurent Pincharte6ecefa2012-05-17 13:27:23 +02009686 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -08009687
Jesse Barnesb690e962010-07-19 13:53:12 -07009688 intel_init_quirks(dev);
9689
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009690 intel_init_pm(dev);
9691
Ben Widawskye3c74752013-04-05 13:12:39 -07009692 if (INTEL_INFO(dev)->num_pipes == 0)
9693 return;
9694
Jesse Barnese70236a2009-09-21 10:42:27 -07009695 intel_init_display(dev);
9696
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009697 if (IS_GEN2(dev)) {
9698 dev->mode_config.max_width = 2048;
9699 dev->mode_config.max_height = 2048;
9700 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -07009701 dev->mode_config.max_width = 4096;
9702 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -08009703 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009704 dev->mode_config.max_width = 8192;
9705 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -08009706 }
Ben Widawsky5d4545a2013-01-17 12:45:15 -08009707 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -08009708
Zhao Yakui28c97732009-10-09 11:39:41 +08009709 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -07009710 INTEL_INFO(dev)->num_pipes,
9711 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -08009712
Damien Lespiau08e2a7d2013-07-11 20:10:54 +01009713 for_each_pipe(i) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009714 intel_crtc_init(dev, i);
Jesse Barnes7f1f3852013-04-02 11:22:20 -07009715 for (j = 0; j < dev_priv->num_plane; j++) {
9716 ret = intel_plane_init(dev, i, j);
9717 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +03009718 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
9719 pipe_name(i), sprite_name(i, j), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -07009720 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009721 }
9722
Paulo Zanoni79f689a2012-10-05 12:05:52 -03009723 intel_cpu_pll_init(dev);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02009724 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009725
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009726 /* Just disable it once at startup */
9727 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009728 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +00009729
9730 /* Just in case the BIOS is doing something questionable. */
9731 intel_disable_fbc(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01009732}
Jesse Barnesd5bb0812011-01-05 12:01:26 -08009733
Daniel Vetter24929352012-07-02 20:28:59 +02009734static void
9735intel_connector_break_all_links(struct intel_connector *connector)
9736{
9737 connector->base.dpms = DRM_MODE_DPMS_OFF;
9738 connector->base.encoder = NULL;
9739 connector->encoder->connectors_active = false;
9740 connector->encoder->base.crtc = NULL;
9741}
9742
Daniel Vetter7fad7982012-07-04 17:51:47 +02009743static void intel_enable_pipe_a(struct drm_device *dev)
9744{
9745 struct intel_connector *connector;
9746 struct drm_connector *crt = NULL;
9747 struct intel_load_detect_pipe load_detect_temp;
9748
9749 /* We can't just switch on the pipe A, we need to set things up with a
9750 * proper mode and output configuration. As a gross hack, enable pipe A
9751 * by enabling the load detect pipe once. */
9752 list_for_each_entry(connector,
9753 &dev->mode_config.connector_list,
9754 base.head) {
9755 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
9756 crt = &connector->base;
9757 break;
9758 }
9759 }
9760
9761 if (!crt)
9762 return;
9763
9764 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
9765 intel_release_load_detect_pipe(crt, &load_detect_temp);
9766
9767
9768}
9769
Daniel Vetterfa555832012-10-10 23:14:00 +02009770static bool
9771intel_check_plane_mapping(struct intel_crtc *crtc)
9772{
Ben Widawsky7eb552a2013-03-13 14:05:41 -07009773 struct drm_device *dev = crtc->base.dev;
9774 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +02009775 u32 reg, val;
9776
Ben Widawsky7eb552a2013-03-13 14:05:41 -07009777 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +02009778 return true;
9779
9780 reg = DSPCNTR(!crtc->plane);
9781 val = I915_READ(reg);
9782
9783 if ((val & DISPLAY_PLANE_ENABLE) &&
9784 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
9785 return false;
9786
9787 return true;
9788}
9789
Daniel Vetter24929352012-07-02 20:28:59 +02009790static void intel_sanitize_crtc(struct intel_crtc *crtc)
9791{
9792 struct drm_device *dev = crtc->base.dev;
9793 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +02009794 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +02009795
Daniel Vetter24929352012-07-02 20:28:59 +02009796 /* Clear any frame start delays used for debugging left by the BIOS */
Daniel Vetter3b117c82013-04-17 20:15:07 +02009797 reg = PIPECONF(crtc->config.cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +02009798 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
9799
9800 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +02009801 * disable the crtc (and hence change the state) if it is wrong. Note
9802 * that gen4+ has a fixed plane -> pipe mapping. */
9803 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +02009804 struct intel_connector *connector;
9805 bool plane;
9806
Daniel Vetter24929352012-07-02 20:28:59 +02009807 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
9808 crtc->base.base.id);
9809
9810 /* Pipe has the wrong plane attached and the plane is active.
9811 * Temporarily change the plane mapping and disable everything
9812 * ... */
9813 plane = crtc->plane;
9814 crtc->plane = !plane;
9815 dev_priv->display.crtc_disable(&crtc->base);
9816 crtc->plane = plane;
9817
9818 /* ... and break all links. */
9819 list_for_each_entry(connector, &dev->mode_config.connector_list,
9820 base.head) {
9821 if (connector->encoder->base.crtc != &crtc->base)
9822 continue;
9823
9824 intel_connector_break_all_links(connector);
9825 }
9826
9827 WARN_ON(crtc->active);
9828 crtc->base.enabled = false;
9829 }
Daniel Vetter24929352012-07-02 20:28:59 +02009830
Daniel Vetter7fad7982012-07-04 17:51:47 +02009831 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
9832 crtc->pipe == PIPE_A && !crtc->active) {
9833 /* BIOS forgot to enable pipe A, this mostly happens after
9834 * resume. Force-enable the pipe to fix this, the update_dpms
9835 * call below we restore the pipe to the right state, but leave
9836 * the required bits on. */
9837 intel_enable_pipe_a(dev);
9838 }
9839
Daniel Vetter24929352012-07-02 20:28:59 +02009840 /* Adjust the state of the output pipe according to whether we
9841 * have active connectors/encoders. */
9842 intel_crtc_update_dpms(&crtc->base);
9843
9844 if (crtc->active != crtc->base.enabled) {
9845 struct intel_encoder *encoder;
9846
9847 /* This can happen either due to bugs in the get_hw_state
9848 * functions or because the pipe is force-enabled due to the
9849 * pipe A quirk. */
9850 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
9851 crtc->base.base.id,
9852 crtc->base.enabled ? "enabled" : "disabled",
9853 crtc->active ? "enabled" : "disabled");
9854
9855 crtc->base.enabled = crtc->active;
9856
9857 /* Because we only establish the connector -> encoder ->
9858 * crtc links if something is active, this means the
9859 * crtc is now deactivated. Break the links. connector
9860 * -> encoder links are only establish when things are
9861 * actually up, hence no need to break them. */
9862 WARN_ON(crtc->active);
9863
9864 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
9865 WARN_ON(encoder->connectors_active);
9866 encoder->base.crtc = NULL;
9867 }
9868 }
9869}
9870
9871static void intel_sanitize_encoder(struct intel_encoder *encoder)
9872{
9873 struct intel_connector *connector;
9874 struct drm_device *dev = encoder->base.dev;
9875
9876 /* We need to check both for a crtc link (meaning that the
9877 * encoder is active and trying to read from a pipe) and the
9878 * pipe itself being active. */
9879 bool has_active_crtc = encoder->base.crtc &&
9880 to_intel_crtc(encoder->base.crtc)->active;
9881
9882 if (encoder->connectors_active && !has_active_crtc) {
9883 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
9884 encoder->base.base.id,
9885 drm_get_encoder_name(&encoder->base));
9886
9887 /* Connector is active, but has no active pipe. This is
9888 * fallout from our resume register restoring. Disable
9889 * the encoder manually again. */
9890 if (encoder->base.crtc) {
9891 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
9892 encoder->base.base.id,
9893 drm_get_encoder_name(&encoder->base));
9894 encoder->disable(encoder);
9895 }
9896
9897 /* Inconsistent output/port/pipe state happens presumably due to
9898 * a bug in one of the get_hw_state functions. Or someplace else
9899 * in our code, like the register restore mess on resume. Clamp
9900 * things to off as a safer default. */
9901 list_for_each_entry(connector,
9902 &dev->mode_config.connector_list,
9903 base.head) {
9904 if (connector->encoder != encoder)
9905 continue;
9906
9907 intel_connector_break_all_links(connector);
9908 }
9909 }
9910 /* Enabled encoders without active connectors will be fixed in
9911 * the crtc fixup. */
9912}
9913
Daniel Vetter44cec742013-01-25 17:53:21 +01009914void i915_redisable_vga(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01009915{
9916 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +02009917 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01009918
9919 if (I915_READ(vga_reg) != VGA_DISP_DISABLE) {
9920 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
Ville Syrjälä209d5212013-01-25 21:44:48 +02009921 i915_disable_vga(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01009922 }
9923}
9924
Daniel Vetter30e984d2013-06-05 13:34:17 +02009925static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +02009926{
9927 struct drm_i915_private *dev_priv = dev->dev_private;
9928 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +02009929 struct intel_crtc *crtc;
9930 struct intel_encoder *encoder;
9931 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +02009932 int i;
Daniel Vetter24929352012-07-02 20:28:59 +02009933
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009934 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9935 base.head) {
Daniel Vetter88adfff2013-03-28 10:42:01 +01009936 memset(&crtc->config, 0, sizeof(crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +02009937
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009938 crtc->active = dev_priv->display.get_pipe_config(crtc,
9939 &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +02009940
9941 crtc->base.enabled = crtc->active;
9942
9943 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
9944 crtc->base.base.id,
9945 crtc->active ? "enabled" : "disabled");
9946 }
9947
Daniel Vetter53589012013-06-05 13:34:16 +02009948 /* FIXME: Smash this into the new shared dpll infrastructure. */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009949 if (HAS_DDI(dev))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009950 intel_ddi_setup_hw_pll_state(dev);
9951
Daniel Vetter53589012013-06-05 13:34:16 +02009952 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
9953 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
9954
9955 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
9956 pll->active = 0;
9957 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9958 base.head) {
9959 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
9960 pll->active++;
9961 }
9962 pll->refcount = pll->active;
9963
9964 DRM_DEBUG_KMS("%s hw state readout: refcount %i\n",
9965 pll->name, pll->refcount);
9966 }
9967
Daniel Vetter24929352012-07-02 20:28:59 +02009968 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9969 base.head) {
9970 pipe = 0;
9971
9972 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -07009973 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
9974 encoder->base.crtc = &crtc->base;
Jesse Barnes510d5f22013-07-01 15:50:17 -07009975 if (encoder->get_config)
Jesse Barnes045ac3b2013-05-14 17:08:26 -07009976 encoder->get_config(encoder, &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +02009977 } else {
9978 encoder->base.crtc = NULL;
9979 }
9980
9981 encoder->connectors_active = false;
9982 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe=%i\n",
9983 encoder->base.base.id,
9984 drm_get_encoder_name(&encoder->base),
9985 encoder->base.crtc ? "enabled" : "disabled",
9986 pipe);
9987 }
9988
Jesse Barnes510d5f22013-07-01 15:50:17 -07009989 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9990 base.head) {
9991 if (!crtc->active)
9992 continue;
9993 if (dev_priv->display.get_clock)
9994 dev_priv->display.get_clock(crtc,
9995 &crtc->config);
9996 }
9997
Daniel Vetter24929352012-07-02 20:28:59 +02009998 list_for_each_entry(connector, &dev->mode_config.connector_list,
9999 base.head) {
10000 if (connector->get_hw_state(connector)) {
10001 connector->base.dpms = DRM_MODE_DPMS_ON;
10002 connector->encoder->connectors_active = true;
10003 connector->base.encoder = &connector->encoder->base;
10004 } else {
10005 connector->base.dpms = DRM_MODE_DPMS_OFF;
10006 connector->base.encoder = NULL;
10007 }
10008 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
10009 connector->base.base.id,
10010 drm_get_connector_name(&connector->base),
10011 connector->base.encoder ? "enabled" : "disabled");
10012 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020010013}
10014
10015/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
10016 * and i915 state tracking structures. */
10017void intel_modeset_setup_hw_state(struct drm_device *dev,
10018 bool force_restore)
10019{
10020 struct drm_i915_private *dev_priv = dev->dev_private;
10021 enum pipe pipe;
10022 struct drm_plane *plane;
10023 struct intel_crtc *crtc;
10024 struct intel_encoder *encoder;
10025
10026 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020010027
Jesse Barnesbabea612013-06-26 18:57:38 +030010028 /*
10029 * Now that we have the config, copy it to each CRTC struct
10030 * Note that this could go away if we move to using crtc_config
10031 * checking everywhere.
10032 */
10033 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
10034 base.head) {
10035 if (crtc->active && i915_fastboot) {
10036 intel_crtc_mode_from_pipe_config(crtc, &crtc->config);
10037
10038 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
10039 crtc->base.base.id);
10040 drm_mode_debug_printmodeline(&crtc->base.mode);
10041 }
10042 }
10043
Daniel Vetter24929352012-07-02 20:28:59 +020010044 /* HW state is read out, now we need to sanitize this mess. */
10045 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10046 base.head) {
10047 intel_sanitize_encoder(encoder);
10048 }
10049
10050 for_each_pipe(pipe) {
10051 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
10052 intel_sanitize_crtc(crtc);
Daniel Vetterc0b03412013-05-28 12:05:54 +020010053 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020010054 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010055
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010056 if (force_restore) {
Daniel Vetterf30da182013-04-11 20:22:50 +020010057 /*
10058 * We need to use raw interfaces for restoring state to avoid
10059 * checking (bogus) intermediate states.
10060 */
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010061 for_each_pipe(pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070010062 struct drm_crtc *crtc =
10063 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020010064
10065 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
10066 crtc->fb);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010067 }
Jesse Barnesb5644d02013-03-26 13:25:27 -070010068 list_for_each_entry(plane, &dev->mode_config.plane_list, head)
10069 intel_plane_restore(plane);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010010070
10071 i915_redisable_vga(dev);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010072 } else {
10073 intel_modeset_update_staged_output_state(dev);
10074 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010075
10076 intel_modeset_check_state(dev);
Daniel Vetter2e938892012-10-11 20:08:24 +020010077
10078 drm_mode_config_reset(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010010079}
10080
10081void intel_modeset_gem_init(struct drm_device *dev)
10082{
Chris Wilson1833b132012-05-09 11:56:28 +010010083 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020010084
10085 intel_setup_overlay(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020010086
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010087 intel_modeset_setup_hw_state(dev, false);
Jesse Barnes79e53942008-11-07 14:24:08 -080010088}
10089
10090void intel_modeset_cleanup(struct drm_device *dev)
10091{
Jesse Barnes652c3932009-08-17 13:31:43 -070010092 struct drm_i915_private *dev_priv = dev->dev_private;
10093 struct drm_crtc *crtc;
10094 struct intel_crtc *intel_crtc;
10095
Daniel Vetterfd0c0642013-04-24 11:13:35 +020010096 /*
10097 * Interrupts and polling as the first thing to avoid creating havoc.
10098 * Too much stuff here (turning of rps, connectors, ...) would
10099 * experience fancy races otherwise.
10100 */
10101 drm_irq_uninstall(dev);
10102 cancel_work_sync(&dev_priv->hotplug_work);
10103 /*
10104 * Due to the hpd irq storm handling the hotplug work can re-arm the
10105 * poll handlers. Hence disable polling after hpd handling is shut down.
10106 */
Keith Packardf87ea762010-10-03 19:36:26 -070010107 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020010108
Jesse Barnes652c3932009-08-17 13:31:43 -070010109 mutex_lock(&dev->struct_mutex);
10110
Jesse Barnes723bfd72010-10-07 16:01:13 -070010111 intel_unregister_dsm_handler();
10112
Jesse Barnes652c3932009-08-17 13:31:43 -070010113 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
10114 /* Skip inactive CRTCs */
10115 if (!crtc->fb)
10116 continue;
10117
10118 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3dec0092010-08-20 21:40:52 +020010119 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070010120 }
10121
Chris Wilson973d04f2011-07-08 12:22:37 +010010122 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070010123
Daniel Vetter8090c6b2012-06-24 16:42:32 +020010124 intel_disable_gt_powersave(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +000010125
Daniel Vetter930ebb42012-06-29 23:32:16 +020010126 ironlake_teardown_rc6(dev);
10127
Kristian Høgsberg69341a52009-11-11 12:19:17 -050010128 mutex_unlock(&dev->struct_mutex);
10129
Chris Wilson1630fe72011-07-08 12:22:42 +010010130 /* flush any delayed tasks or pending work */
10131 flush_scheduled_work();
10132
Jani Nikuladc652f92013-04-12 15:18:38 +030010133 /* destroy backlight, if any, before the connectors */
10134 intel_panel_destroy_backlight(dev);
10135
Jesse Barnes79e53942008-11-07 14:24:08 -080010136 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010010137
10138 intel_cleanup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010139}
10140
Dave Airlie28d52042009-09-21 14:33:58 +100010141/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080010142 * Return which encoder is currently attached for connector.
10143 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010010144struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080010145{
Chris Wilsondf0e9242010-09-09 16:20:55 +010010146 return &intel_attached_encoder(connector)->base;
10147}
Jesse Barnes79e53942008-11-07 14:24:08 -080010148
Chris Wilsondf0e9242010-09-09 16:20:55 +010010149void intel_connector_attach_encoder(struct intel_connector *connector,
10150 struct intel_encoder *encoder)
10151{
10152 connector->encoder = encoder;
10153 drm_mode_connector_attach_encoder(&connector->base,
10154 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080010155}
Dave Airlie28d52042009-09-21 14:33:58 +100010156
10157/*
10158 * set vga decode state - true == enable VGA decode
10159 */
10160int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
10161{
10162 struct drm_i915_private *dev_priv = dev->dev_private;
10163 u16 gmch_ctrl;
10164
10165 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
10166 if (state)
10167 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
10168 else
10169 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
10170 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
10171 return 0;
10172}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010173
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010174struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010175
10176 u32 power_well_driver;
10177
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010178 struct intel_cursor_error_state {
10179 u32 control;
10180 u32 position;
10181 u32 base;
10182 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010010183 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010184
10185 struct intel_pipe_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010186 enum transcoder cpu_transcoder;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010187 u32 conf;
10188 u32 source;
10189
10190 u32 htotal;
10191 u32 hblank;
10192 u32 hsync;
10193 u32 vtotal;
10194 u32 vblank;
10195 u32 vsync;
Damien Lespiau52331302012-08-15 19:23:25 +010010196 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010197
10198 struct intel_plane_error_state {
10199 u32 control;
10200 u32 stride;
10201 u32 size;
10202 u32 pos;
10203 u32 addr;
10204 u32 surface;
10205 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010010206 } plane[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010207};
10208
10209struct intel_display_error_state *
10210intel_display_capture_error_state(struct drm_device *dev)
10211{
Akshay Joshi0206e352011-08-16 15:34:10 -040010212 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010213 struct intel_display_error_state *error;
Paulo Zanoni702e7a52012-10-23 18:29:59 -020010214 enum transcoder cpu_transcoder;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010215 int i;
10216
10217 error = kmalloc(sizeof(*error), GFP_ATOMIC);
10218 if (error == NULL)
10219 return NULL;
10220
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010221 if (HAS_POWER_WELL(dev))
10222 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
10223
Damien Lespiau52331302012-08-15 19:23:25 +010010224 for_each_pipe(i) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -020010225 cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv, i);
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010226 error->pipe[i].cpu_transcoder = cpu_transcoder;
Paulo Zanoni702e7a52012-10-23 18:29:59 -020010227
Paulo Zanonia18c4c32013-03-06 20:03:12 -030010228 if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev)) {
10229 error->cursor[i].control = I915_READ(CURCNTR(i));
10230 error->cursor[i].position = I915_READ(CURPOS(i));
10231 error->cursor[i].base = I915_READ(CURBASE(i));
10232 } else {
10233 error->cursor[i].control = I915_READ(CURCNTR_IVB(i));
10234 error->cursor[i].position = I915_READ(CURPOS_IVB(i));
10235 error->cursor[i].base = I915_READ(CURBASE_IVB(i));
10236 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010237
10238 error->plane[i].control = I915_READ(DSPCNTR(i));
10239 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030010240 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030010241 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030010242 error->plane[i].pos = I915_READ(DSPPOS(i));
10243 }
Paulo Zanonica291362013-03-06 20:03:14 -030010244 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
10245 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010246 if (INTEL_INFO(dev)->gen >= 4) {
10247 error->plane[i].surface = I915_READ(DSPSURF(i));
10248 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
10249 }
10250
Paulo Zanoni702e7a52012-10-23 18:29:59 -020010251 error->pipe[i].conf = I915_READ(PIPECONF(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010252 error->pipe[i].source = I915_READ(PIPESRC(i));
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010253 error->pipe[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
10254 error->pipe[i].hblank = I915_READ(HBLANK(cpu_transcoder));
10255 error->pipe[i].hsync = I915_READ(HSYNC(cpu_transcoder));
10256 error->pipe[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
10257 error->pipe[i].vblank = I915_READ(VBLANK(cpu_transcoder));
10258 error->pipe[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010259 }
10260
Paulo Zanoni12d217c2013-05-03 12:15:38 -030010261 /* In the code above we read the registers without checking if the power
10262 * well was on, so here we have to clear the FPGA_DBG_RM_NOCLAIM bit to
10263 * prevent the next I915_WRITE from detecting it and printing an error
10264 * message. */
10265 if (HAS_POWER_WELL(dev))
10266 I915_WRITE_NOTRACE(FPGA_DBG, FPGA_DBG_RM_NOCLAIM);
10267
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010268 return error;
10269}
10270
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010271#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
10272
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010273void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010274intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010275 struct drm_device *dev,
10276 struct intel_display_error_state *error)
10277{
10278 int i;
10279
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010280 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010281 if (HAS_POWER_WELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010282 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010283 error->power_well_driver);
Damien Lespiau52331302012-08-15 19:23:25 +010010284 for_each_pipe(i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010285 err_printf(m, "Pipe [%d]:\n", i);
10286 err_printf(m, " CPU transcoder: %c\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010287 transcoder_name(error->pipe[i].cpu_transcoder));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010288 err_printf(m, " CONF: %08x\n", error->pipe[i].conf);
10289 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
10290 err_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
10291 err_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
10292 err_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
10293 err_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
10294 err_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
10295 err_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010296
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010297 err_printf(m, "Plane [%d]:\n", i);
10298 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
10299 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030010300 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010301 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
10302 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030010303 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030010304 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010305 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010306 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010307 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
10308 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010309 }
10310
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010311 err_printf(m, "Cursor [%d]:\n", i);
10312 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
10313 err_printf(m, " POS: %08x\n", error->cursor[i].position);
10314 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010315 }
10316}