blob: 41efd3519e35808bfc9700ce8f69383758961783 [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
Keith Packarde4b36692009-06-05 19:22:17 -070092static const intel_limit_t intel_limits_i8xx_dvo = {
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
105static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400106 .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 = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700114 .p2 = { .dot_limit = 165000,
115 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700116};
Eric Anholt273e27c2011-03-30 13:01:10 -0700117
Keith Packarde4b36692009-06-05 19:22:17 -0700118static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400119 .dot = { .min = 20000, .max = 400000 },
120 .vco = { .min = 1400000, .max = 2800000 },
121 .n = { .min = 1, .max = 6 },
122 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100123 .m1 = { .min = 8, .max = 18 },
124 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400125 .p = { .min = 5, .max = 80 },
126 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700127 .p2 = { .dot_limit = 200000,
128 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700129};
130
131static const intel_limit_t intel_limits_i9xx_lvds = {
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 Jakobsson53a7d2d2013-02-13 22:20:21 +0100136 .m1 = { .min = 8, .max = 18 },
137 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400138 .p = { .min = 7, .max = 98 },
139 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700140 .p2 = { .dot_limit = 112000,
141 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700142};
143
Eric Anholt273e27c2011-03-30 13:01:10 -0700144
Keith Packarde4b36692009-06-05 19:22:17 -0700145static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700146 .dot = { .min = 25000, .max = 270000 },
147 .vco = { .min = 1750000, .max = 3500000},
148 .n = { .min = 1, .max = 4 },
149 .m = { .min = 104, .max = 138 },
150 .m1 = { .min = 17, .max = 23 },
151 .m2 = { .min = 5, .max = 11 },
152 .p = { .min = 10, .max = 30 },
153 .p1 = { .min = 1, .max = 3},
154 .p2 = { .dot_limit = 270000,
155 .p2_slow = 10,
156 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800157 },
Keith Packarde4b36692009-06-05 19:22:17 -0700158};
159
160static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700161 .dot = { .min = 22000, .max = 400000 },
162 .vco = { .min = 1750000, .max = 3500000},
163 .n = { .min = 1, .max = 4 },
164 .m = { .min = 104, .max = 138 },
165 .m1 = { .min = 16, .max = 23 },
166 .m2 = { .min = 5, .max = 11 },
167 .p = { .min = 5, .max = 80 },
168 .p1 = { .min = 1, .max = 8},
169 .p2 = { .dot_limit = 165000,
170 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700171};
172
173static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700174 .dot = { .min = 20000, .max = 115000 },
175 .vco = { .min = 1750000, .max = 3500000 },
176 .n = { .min = 1, .max = 3 },
177 .m = { .min = 104, .max = 138 },
178 .m1 = { .min = 17, .max = 23 },
179 .m2 = { .min = 5, .max = 11 },
180 .p = { .min = 28, .max = 112 },
181 .p1 = { .min = 2, .max = 8 },
182 .p2 = { .dot_limit = 0,
183 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800184 },
Keith Packarde4b36692009-06-05 19:22:17 -0700185};
186
187static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700188 .dot = { .min = 80000, .max = 224000 },
189 .vco = { .min = 1750000, .max = 3500000 },
190 .n = { .min = 1, .max = 3 },
191 .m = { .min = 104, .max = 138 },
192 .m1 = { .min = 17, .max = 23 },
193 .m2 = { .min = 5, .max = 11 },
194 .p = { .min = 14, .max = 42 },
195 .p1 = { .min = 2, .max = 6 },
196 .p2 = { .dot_limit = 0,
197 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800198 },
Keith Packarde4b36692009-06-05 19:22:17 -0700199};
200
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500201static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400202 .dot = { .min = 20000, .max = 400000},
203 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700204 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400205 .n = { .min = 3, .max = 6 },
206 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700207 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400208 .m1 = { .min = 0, .max = 0 },
209 .m2 = { .min = 0, .max = 254 },
210 .p = { .min = 5, .max = 80 },
211 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700212 .p2 = { .dot_limit = 200000,
213 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700214};
215
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500216static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400217 .dot = { .min = 20000, .max = 400000 },
218 .vco = { .min = 1700000, .max = 3500000 },
219 .n = { .min = 3, .max = 6 },
220 .m = { .min = 2, .max = 256 },
221 .m1 = { .min = 0, .max = 0 },
222 .m2 = { .min = 0, .max = 254 },
223 .p = { .min = 7, .max = 112 },
224 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700225 .p2 = { .dot_limit = 112000,
226 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700227};
228
Eric Anholt273e27c2011-03-30 13:01:10 -0700229/* Ironlake / Sandybridge
230 *
231 * We calculate clock using (register_value + 2) for N/M1/M2, so here
232 * the range value for them is (actual_value - 2).
233 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800234static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700235 .dot = { .min = 25000, .max = 350000 },
236 .vco = { .min = 1760000, .max = 3510000 },
237 .n = { .min = 1, .max = 5 },
238 .m = { .min = 79, .max = 127 },
239 .m1 = { .min = 12, .max = 22 },
240 .m2 = { .min = 5, .max = 9 },
241 .p = { .min = 5, .max = 80 },
242 .p1 = { .min = 1, .max = 8 },
243 .p2 = { .dot_limit = 225000,
244 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700245};
246
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800247static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700248 .dot = { .min = 25000, .max = 350000 },
249 .vco = { .min = 1760000, .max = 3510000 },
250 .n = { .min = 1, .max = 3 },
251 .m = { .min = 79, .max = 118 },
252 .m1 = { .min = 12, .max = 22 },
253 .m2 = { .min = 5, .max = 9 },
254 .p = { .min = 28, .max = 112 },
255 .p1 = { .min = 2, .max = 8 },
256 .p2 = { .dot_limit = 225000,
257 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800258};
259
260static const intel_limit_t intel_limits_ironlake_dual_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 = 127 },
265 .m1 = { .min = 12, .max = 22 },
266 .m2 = { .min = 5, .max = 9 },
267 .p = { .min = 14, .max = 56 },
268 .p1 = { .min = 2, .max = 8 },
269 .p2 = { .dot_limit = 225000,
270 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800271};
272
Eric Anholt273e27c2011-03-30 13:01:10 -0700273/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800274static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700275 .dot = { .min = 25000, .max = 350000 },
276 .vco = { .min = 1760000, .max = 3510000 },
277 .n = { .min = 1, .max = 2 },
278 .m = { .min = 79, .max = 126 },
279 .m1 = { .min = 12, .max = 22 },
280 .m2 = { .min = 5, .max = 9 },
281 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400282 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700283 .p2 = { .dot_limit = 225000,
284 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800285};
286
287static const intel_limit_t intel_limits_ironlake_dual_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 = 3 },
291 .m = { .min = 79, .max = 126 },
292 .m1 = { .min = 12, .max = 22 },
293 .m2 = { .min = 5, .max = 9 },
294 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400295 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700296 .p2 = { .dot_limit = 225000,
297 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800298};
299
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700300static const intel_limit_t intel_limits_vlv_dac = {
301 .dot = { .min = 25000, .max = 270000 },
302 .vco = { .min = 4000000, .max = 6000000 },
303 .n = { .min = 1, .max = 7 },
304 .m = { .min = 22, .max = 450 }, /* guess */
305 .m1 = { .min = 2, .max = 3 },
306 .m2 = { .min = 11, .max = 156 },
307 .p = { .min = 10, .max = 30 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200308 .p1 = { .min = 1, .max = 3 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700309 .p2 = { .dot_limit = 270000,
310 .p2_slow = 2, .p2_fast = 20 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700311};
312
313static const intel_limit_t intel_limits_vlv_hdmi = {
Daniel Vetter75e53982013-04-18 21:10:43 +0200314 .dot = { .min = 25000, .max = 270000 },
315 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700316 .n = { .min = 1, .max = 7 },
317 .m = { .min = 60, .max = 300 }, /* guess */
318 .m1 = { .min = 2, .max = 3 },
319 .m2 = { .min = 11, .max = 156 },
320 .p = { .min = 10, .max = 30 },
321 .p1 = { .min = 2, .max = 3 },
322 .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_dp = {
Vijay Purushothaman74a4dd22012-09-27 19:13:04 +0530327 .dot = { .min = 25000, .max = 270000 },
328 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700329 .n = { .min = 1, .max = 7 },
Vijay Purushothaman74a4dd22012-09-27 19:13:04 +0530330 .m = { .min = 22, .max = 450 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700331 .m1 = { .min = 2, .max = 3 },
332 .m2 = { .min = 11, .max = 156 },
333 .p = { .min = 10, .max = 30 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200334 .p1 = { .min = 1, .max = 3 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700335 .p2 = { .dot_limit = 270000,
336 .p2_slow = 2, .p2_fast = 20 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700337};
338
Chris Wilson1b894b52010-12-14 20:04:54 +0000339static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
340 int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800341{
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800342 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800343 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800344
345 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100346 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000347 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800348 limit = &intel_limits_ironlake_dual_lvds_100m;
349 else
350 limit = &intel_limits_ironlake_dual_lvds;
351 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000352 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800353 limit = &intel_limits_ironlake_single_lvds_100m;
354 else
355 limit = &intel_limits_ironlake_single_lvds;
356 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200357 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800358 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800359
360 return limit;
361}
362
Ma Ling044c7c42009-03-18 20:13:23 +0800363static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
364{
365 struct drm_device *dev = crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800366 const intel_limit_t *limit;
367
368 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100369 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700370 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800371 else
Keith Packarde4b36692009-06-05 19:22:17 -0700372 limit = &intel_limits_g4x_single_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800373 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
374 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700375 limit = &intel_limits_g4x_hdmi;
Ma Ling044c7c42009-03-18 20:13:23 +0800376 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700377 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800378 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700379 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800380
381 return limit;
382}
383
Chris Wilson1b894b52010-12-14 20:04:54 +0000384static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800385{
386 struct drm_device *dev = crtc->dev;
387 const intel_limit_t *limit;
388
Eric Anholtbad720f2009-10-22 16:11:14 -0700389 if (HAS_PCH_SPLIT(dev))
Chris Wilson1b894b52010-12-14 20:04:54 +0000390 limit = intel_ironlake_limit(crtc, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800391 else if (IS_G4X(dev)) {
Ma Ling044c7c42009-03-18 20:13:23 +0800392 limit = intel_g4x_limit(crtc);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500393 } else if (IS_PINEVIEW(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +0800394 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500395 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800396 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500397 limit = &intel_limits_pineview_sdvo;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700398 } else if (IS_VALLEYVIEW(dev)) {
399 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG))
400 limit = &intel_limits_vlv_dac;
401 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
402 limit = &intel_limits_vlv_hdmi;
403 else
404 limit = &intel_limits_vlv_dp;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100405 } else if (!IS_GEN2(dev)) {
406 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
407 limit = &intel_limits_i9xx_lvds;
408 else
409 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800410 } else {
411 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700412 limit = &intel_limits_i8xx_lvds;
Jesse Barnes79e53942008-11-07 14:24:08 -0800413 else
Keith Packarde4b36692009-06-05 19:22:17 -0700414 limit = &intel_limits_i8xx_dvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800415 }
416 return limit;
417}
418
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500419/* m1 is reserved as 0 in Pineview, n is a ring counter */
420static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800421{
Shaohua Li21778322009-02-23 15:19:16 +0800422 clock->m = clock->m2 + 2;
423 clock->p = clock->p1 * clock->p2;
424 clock->vco = refclk * clock->m / clock->n;
425 clock->dot = clock->vco / clock->p;
426}
427
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200428static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
429{
430 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
431}
432
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200433static void i9xx_clock(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800434{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200435 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800436 clock->p = clock->p1 * clock->p2;
437 clock->vco = refclk * clock->m / (clock->n + 2);
438 clock->dot = clock->vco / clock->p;
439}
440
Jesse Barnes79e53942008-11-07 14:24:08 -0800441/**
442 * Returns whether any output on the specified pipe is of the specified type
443 */
Chris Wilson4ef69c72010-09-09 15:14:28 +0100444bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
Jesse Barnes79e53942008-11-07 14:24:08 -0800445{
Chris Wilson4ef69c72010-09-09 15:14:28 +0100446 struct drm_device *dev = crtc->dev;
Chris Wilson4ef69c72010-09-09 15:14:28 +0100447 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -0800448
Daniel Vetter6c2b7c122012-07-05 09:50:24 +0200449 for_each_encoder_on_crtc(dev, crtc, encoder)
450 if (encoder->type == type)
Chris Wilson4ef69c72010-09-09 15:14:28 +0100451 return true;
452
453 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -0800454}
455
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800456#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800457/**
458 * Returns whether the given set of divisors are valid for a given refclk with
459 * the given connectors.
460 */
461
Chris Wilson1b894b52010-12-14 20:04:54 +0000462static bool intel_PLL_is_valid(struct drm_device *dev,
463 const intel_limit_t *limit,
464 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800465{
Jesse Barnes79e53942008-11-07 14:24:08 -0800466 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400467 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800468 if (clock->p < limit->p.min || limit->p.max < clock->p)
Akshay Joshi0206e352011-08-16 15:34:10 -0400469 INTELPllInvalid("p out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800470 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400471 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800472 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400473 INTELPllInvalid("m1 out of range\n");
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500474 if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
Akshay Joshi0206e352011-08-16 15:34:10 -0400475 INTELPllInvalid("m1 <= m2\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800476 if (clock->m < limit->m.min || limit->m.max < clock->m)
Akshay Joshi0206e352011-08-16 15:34:10 -0400477 INTELPllInvalid("m out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800478 if (clock->n < limit->n.min || limit->n.max < clock->n)
Akshay Joshi0206e352011-08-16 15:34:10 -0400479 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800480 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400481 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800482 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
483 * connector, etc., rather than just a single range.
484 */
485 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400486 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800487
488 return true;
489}
490
Ma Lingd4906092009-03-18 20:13:27 +0800491static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200492i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800493 int target, int refclk, intel_clock_t *match_clock,
494 intel_clock_t *best_clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800495{
496 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800497 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800498 int err = target;
499
Daniel Vettera210b022012-11-26 17:22:08 +0100500 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800501 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100502 * For LVDS just rely on its current settings for dual-channel.
503 * We haven't figured out how to reliably set up different
504 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800505 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100506 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800507 clock.p2 = limit->p2.p2_fast;
508 else
509 clock.p2 = limit->p2.p2_slow;
510 } else {
511 if (target < limit->p2.dot_limit)
512 clock.p2 = limit->p2.p2_slow;
513 else
514 clock.p2 = limit->p2.p2_fast;
515 }
516
Akshay Joshi0206e352011-08-16 15:34:10 -0400517 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800518
Zhao Yakui42158662009-11-20 11:24:18 +0800519 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
520 clock.m1++) {
521 for (clock.m2 = limit->m2.min;
522 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200523 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800524 break;
525 for (clock.n = limit->n.min;
526 clock.n <= limit->n.max; clock.n++) {
527 for (clock.p1 = limit->p1.min;
528 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800529 int this_err;
530
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200531 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000532 if (!intel_PLL_is_valid(dev, limit,
533 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800534 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800535 if (match_clock &&
536 clock.p != match_clock->p)
537 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800538
539 this_err = abs(clock.dot - target);
540 if (this_err < err) {
541 *best_clock = clock;
542 err = this_err;
543 }
544 }
545 }
546 }
547 }
548
549 return (err != target);
550}
551
Ma Lingd4906092009-03-18 20:13:27 +0800552static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200553pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
554 int target, int refclk, intel_clock_t *match_clock,
555 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200556{
557 struct drm_device *dev = crtc->dev;
558 intel_clock_t clock;
559 int err = target;
560
561 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
562 /*
563 * For LVDS just rely on its current settings for dual-channel.
564 * We haven't figured out how to reliably set up different
565 * single/dual channel state, if we even can.
566 */
567 if (intel_is_dual_link_lvds(dev))
568 clock.p2 = limit->p2.p2_fast;
569 else
570 clock.p2 = limit->p2.p2_slow;
571 } else {
572 if (target < limit->p2.dot_limit)
573 clock.p2 = limit->p2.p2_slow;
574 else
575 clock.p2 = limit->p2.p2_fast;
576 }
577
578 memset(best_clock, 0, sizeof(*best_clock));
579
580 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
581 clock.m1++) {
582 for (clock.m2 = limit->m2.min;
583 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200584 for (clock.n = limit->n.min;
585 clock.n <= limit->n.max; clock.n++) {
586 for (clock.p1 = limit->p1.min;
587 clock.p1 <= limit->p1.max; clock.p1++) {
588 int this_err;
589
590 pineview_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800591 if (!intel_PLL_is_valid(dev, limit,
592 &clock))
593 continue;
594 if (match_clock &&
595 clock.p != match_clock->p)
596 continue;
597
598 this_err = abs(clock.dot - target);
599 if (this_err < err) {
600 *best_clock = clock;
601 err = this_err;
602 }
603 }
604 }
605 }
606 }
607
608 return (err != target);
609}
610
Ma Lingd4906092009-03-18 20:13:27 +0800611static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200612g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
613 int target, int refclk, intel_clock_t *match_clock,
614 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800615{
616 struct drm_device *dev = crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800617 intel_clock_t clock;
618 int max_n;
619 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400620 /* approximately equals target * 0.00585 */
621 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800622 found = false;
623
624 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100625 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800626 clock.p2 = limit->p2.p2_fast;
627 else
628 clock.p2 = limit->p2.p2_slow;
629 } else {
630 if (target < limit->p2.dot_limit)
631 clock.p2 = limit->p2.p2_slow;
632 else
633 clock.p2 = limit->p2.p2_fast;
634 }
635
636 memset(best_clock, 0, sizeof(*best_clock));
637 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200638 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800639 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200640 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800641 for (clock.m1 = limit->m1.max;
642 clock.m1 >= limit->m1.min; clock.m1--) {
643 for (clock.m2 = limit->m2.max;
644 clock.m2 >= limit->m2.min; clock.m2--) {
645 for (clock.p1 = limit->p1.max;
646 clock.p1 >= limit->p1.min; clock.p1--) {
647 int this_err;
648
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200649 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000650 if (!intel_PLL_is_valid(dev, limit,
651 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800652 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000653
654 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800655 if (this_err < err_most) {
656 *best_clock = clock;
657 err_most = this_err;
658 max_n = clock.n;
659 found = true;
660 }
661 }
662 }
663 }
664 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800665 return found;
666}
Ma Lingd4906092009-03-18 20:13:27 +0800667
Zhenyu Wang2c072452009-06-05 15:38:42 +0800668static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200669vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
670 int target, int refclk, intel_clock_t *match_clock,
671 intel_clock_t *best_clock)
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700672{
673 u32 p1, p2, m1, m2, vco, bestn, bestm1, bestm2, bestp1, bestp2;
674 u32 m, n, fastclk;
675 u32 updrate, minupdate, fracbits, p;
676 unsigned long bestppm, ppm, absppm;
677 int dotclk, flag;
678
Alan Coxaf447bd2012-07-25 13:49:18 +0100679 flag = 0;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700680 dotclk = target * 1000;
681 bestppm = 1000000;
682 ppm = absppm = 0;
683 fastclk = dotclk / (2*100);
684 updrate = 0;
685 minupdate = 19200;
686 fracbits = 1;
687 n = p = p1 = p2 = m = m1 = m2 = vco = bestn = 0;
688 bestm1 = bestm2 = bestp1 = bestp2 = 0;
689
690 /* based on hardware requirement, prefer smaller n to precision */
691 for (n = limit->n.min; n <= ((refclk) / minupdate); n++) {
692 updrate = refclk / n;
693 for (p1 = limit->p1.max; p1 > limit->p1.min; p1--) {
694 for (p2 = limit->p2.p2_fast+1; p2 > 0; p2--) {
695 if (p2 > 10)
696 p2 = p2 - 1;
697 p = p1 * p2;
698 /* based on hardware requirement, prefer bigger m1,m2 values */
699 for (m1 = limit->m1.min; m1 <= limit->m1.max; m1++) {
700 m2 = (((2*(fastclk * p * n / m1 )) +
701 refclk) / (2*refclk));
702 m = m1 * m2;
703 vco = updrate * m;
704 if (vco >= limit->vco.min && vco < limit->vco.max) {
705 ppm = 1000000 * ((vco / p) - fastclk) / fastclk;
706 absppm = (ppm > 0) ? ppm : (-ppm);
707 if (absppm < 100 && ((p1 * p2) > (bestp1 * bestp2))) {
708 bestppm = 0;
709 flag = 1;
710 }
711 if (absppm < bestppm - 10) {
712 bestppm = absppm;
713 flag = 1;
714 }
715 if (flag) {
716 bestn = n;
717 bestm1 = m1;
718 bestm2 = m2;
719 bestp1 = p1;
720 bestp2 = p2;
721 flag = 0;
722 }
723 }
724 }
725 }
726 }
727 }
728 best_clock->n = bestn;
729 best_clock->m1 = bestm1;
730 best_clock->m2 = bestm2;
731 best_clock->p1 = bestp1;
732 best_clock->p2 = bestp2;
733
734 return true;
735}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700736
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200737enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
738 enum pipe pipe)
739{
740 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
741 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
742
Daniel Vetter3b117c82013-04-17 20:15:07 +0200743 return intel_crtc->config.cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200744}
745
Paulo Zanonia928d532012-05-04 17:18:15 -0300746static void ironlake_wait_for_vblank(struct drm_device *dev, int pipe)
747{
748 struct drm_i915_private *dev_priv = dev->dev_private;
749 u32 frame, frame_reg = PIPEFRAME(pipe);
750
751 frame = I915_READ(frame_reg);
752
753 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
754 DRM_DEBUG_KMS("vblank wait timed out\n");
755}
756
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700757/**
758 * intel_wait_for_vblank - wait for vblank on a given pipe
759 * @dev: drm device
760 * @pipe: pipe to wait for
761 *
762 * Wait for vblank to occur on a given pipe. Needed for various bits of
763 * mode setting code.
764 */
765void intel_wait_for_vblank(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -0800766{
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700767 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800768 int pipestat_reg = PIPESTAT(pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700769
Paulo Zanonia928d532012-05-04 17:18:15 -0300770 if (INTEL_INFO(dev)->gen >= 5) {
771 ironlake_wait_for_vblank(dev, pipe);
772 return;
773 }
774
Chris Wilson300387c2010-09-05 20:25:43 +0100775 /* Clear existing vblank status. Note this will clear any other
776 * sticky status fields as well.
777 *
778 * This races with i915_driver_irq_handler() with the result
779 * that either function could miss a vblank event. Here it is not
780 * fatal, as we will either wait upon the next vblank interrupt or
781 * timeout. Generally speaking intel_wait_for_vblank() is only
782 * called during modeset at which time the GPU should be idle and
783 * should *not* be performing page flips and thus not waiting on
784 * vblanks...
785 * Currently, the result of us stealing a vblank from the irq
786 * handler is that a single frame will be skipped during swapbuffers.
787 */
788 I915_WRITE(pipestat_reg,
789 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
790
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700791 /* Wait for vblank interrupt bit to set */
Chris Wilson481b6af2010-08-23 17:43:35 +0100792 if (wait_for(I915_READ(pipestat_reg) &
793 PIPE_VBLANK_INTERRUPT_STATUS,
794 50))
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700795 DRM_DEBUG_KMS("vblank wait timed out\n");
796}
797
Keith Packardab7ad7f2010-10-03 00:33:06 -0700798/*
799 * intel_wait_for_pipe_off - wait for pipe to turn off
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700800 * @dev: drm device
801 * @pipe: pipe to wait for
802 *
803 * After disabling a pipe, we can't wait for vblank in the usual way,
804 * spinning on the vblank interrupt status bit, since we won't actually
805 * see an interrupt when the pipe is disabled.
806 *
Keith Packardab7ad7f2010-10-03 00:33:06 -0700807 * On Gen4 and above:
808 * wait for the pipe register state bit to turn off
809 *
810 * Otherwise:
811 * wait for the display line value to settle (it usually
812 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +0100813 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700814 */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100815void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700816{
817 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200818 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
819 pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700820
Keith Packardab7ad7f2010-10-03 00:33:06 -0700821 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200822 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700823
Keith Packardab7ad7f2010-10-03 00:33:06 -0700824 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100825 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
826 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200827 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700828 } else {
Paulo Zanoni837ba002012-05-04 17:18:14 -0300829 u32 last_line, line_mask;
Chris Wilson58e10eb2010-10-03 10:56:11 +0100830 int reg = PIPEDSL(pipe);
Keith Packardab7ad7f2010-10-03 00:33:06 -0700831 unsigned long timeout = jiffies + msecs_to_jiffies(100);
832
Paulo Zanoni837ba002012-05-04 17:18:14 -0300833 if (IS_GEN2(dev))
834 line_mask = DSL_LINEMASK_GEN2;
835 else
836 line_mask = DSL_LINEMASK_GEN3;
837
Keith Packardab7ad7f2010-10-03 00:33:06 -0700838 /* Wait for the display line to settle */
839 do {
Paulo Zanoni837ba002012-05-04 17:18:14 -0300840 last_line = I915_READ(reg) & line_mask;
Keith Packardab7ad7f2010-10-03 00:33:06 -0700841 mdelay(5);
Paulo Zanoni837ba002012-05-04 17:18:14 -0300842 } while (((I915_READ(reg) & line_mask) != last_line) &&
Keith Packardab7ad7f2010-10-03 00:33:06 -0700843 time_after(timeout, jiffies));
844 if (time_after(jiffies, timeout))
Daniel Vetter284637d2012-07-09 09:51:57 +0200845 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700846 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800847}
848
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000849/*
850 * ibx_digital_port_connected - is the specified port connected?
851 * @dev_priv: i915 private structure
852 * @port: the port to test
853 *
854 * Returns true if @port is connected, false otherwise.
855 */
856bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
857 struct intel_digital_port *port)
858{
859 u32 bit;
860
Damien Lespiauc36346e2012-12-13 16:09:03 +0000861 if (HAS_PCH_IBX(dev_priv->dev)) {
862 switch(port->port) {
863 case PORT_B:
864 bit = SDE_PORTB_HOTPLUG;
865 break;
866 case PORT_C:
867 bit = SDE_PORTC_HOTPLUG;
868 break;
869 case PORT_D:
870 bit = SDE_PORTD_HOTPLUG;
871 break;
872 default:
873 return true;
874 }
875 } else {
876 switch(port->port) {
877 case PORT_B:
878 bit = SDE_PORTB_HOTPLUG_CPT;
879 break;
880 case PORT_C:
881 bit = SDE_PORTC_HOTPLUG_CPT;
882 break;
883 case PORT_D:
884 bit = SDE_PORTD_HOTPLUG_CPT;
885 break;
886 default:
887 return true;
888 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000889 }
890
891 return I915_READ(SDEISR) & bit;
892}
893
Jesse Barnesb24e7172011-01-04 15:09:30 -0800894static const char *state_string(bool enabled)
895{
896 return enabled ? "on" : "off";
897}
898
899/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +0200900void assert_pll(struct drm_i915_private *dev_priv,
901 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -0800902{
903 int reg;
904 u32 val;
905 bool cur_state;
906
907 reg = DPLL(pipe);
908 val = I915_READ(reg);
909 cur_state = !!(val & DPLL_VCO_ENABLE);
910 WARN(cur_state != state,
911 "PLL state assertion failure (expected %s, current %s)\n",
912 state_string(state), state_string(cur_state));
913}
Jesse Barnesb24e7172011-01-04 15:09:30 -0800914
Daniel Vetter55607e82013-06-16 21:42:39 +0200915struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +0200916intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -0800917{
Daniel Vettere2b78262013-06-07 23:10:03 +0200918 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
919
Daniel Vettera43f6e02013-06-07 23:10:32 +0200920 if (crtc->config.shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +0200921 return NULL;
922
Daniel Vettera43f6e02013-06-07 23:10:32 +0200923 return &dev_priv->shared_dplls[crtc->config.shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +0200924}
925
Jesse Barnesb24e7172011-01-04 15:09:30 -0800926/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +0200927void assert_shared_dpll(struct drm_i915_private *dev_priv,
928 struct intel_shared_dpll *pll,
929 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -0800930{
Jesse Barnes040484a2011-01-03 12:14:26 -0800931 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +0200932 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -0800933
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -0300934 if (HAS_PCH_LPT(dev_priv->dev)) {
935 DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
936 return;
937 }
938
Chris Wilson92b27b02012-05-20 18:10:50 +0100939 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +0200940 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +0100941 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +0100942
Daniel Vetter53589012013-06-05 13:34:16 +0200943 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Chris Wilson92b27b02012-05-20 18:10:50 +0100944 WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +0200945 "%s assertion failure (expected %s, current %s)\n",
946 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -0800947}
Jesse Barnes040484a2011-01-03 12:14:26 -0800948
949static void assert_fdi_tx(struct drm_i915_private *dev_priv,
950 enum pipe pipe, bool state)
951{
952 int reg;
953 u32 val;
954 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -0200955 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
956 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -0800957
Paulo Zanoniaffa9352012-11-23 15:30:39 -0200958 if (HAS_DDI(dev_priv->dev)) {
959 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -0200960 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -0300961 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -0200962 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -0300963 } else {
964 reg = FDI_TX_CTL(pipe);
965 val = I915_READ(reg);
966 cur_state = !!(val & FDI_TX_ENABLE);
967 }
Jesse Barnes040484a2011-01-03 12:14:26 -0800968 WARN(cur_state != state,
969 "FDI TX state assertion failure (expected %s, current %s)\n",
970 state_string(state), state_string(cur_state));
971}
972#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
973#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
974
975static void assert_fdi_rx(struct drm_i915_private *dev_priv,
976 enum pipe pipe, bool state)
977{
978 int reg;
979 u32 val;
980 bool cur_state;
981
Paulo Zanonid63fa0d2012-11-20 13:27:35 -0200982 reg = FDI_RX_CTL(pipe);
983 val = I915_READ(reg);
984 cur_state = !!(val & FDI_RX_ENABLE);
Jesse Barnes040484a2011-01-03 12:14:26 -0800985 WARN(cur_state != state,
986 "FDI RX state assertion failure (expected %s, current %s)\n",
987 state_string(state), state_string(cur_state));
988}
989#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
990#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
991
992static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
993 enum pipe pipe)
994{
995 int reg;
996 u32 val;
997
998 /* ILK FDI PLL is always enabled */
999 if (dev_priv->info->gen == 5)
1000 return;
1001
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001002 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001003 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001004 return;
1005
Jesse Barnes040484a2011-01-03 12:14:26 -08001006 reg = FDI_TX_CTL(pipe);
1007 val = I915_READ(reg);
1008 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1009}
1010
Daniel Vetter55607e82013-06-16 21:42:39 +02001011void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1012 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001013{
1014 int reg;
1015 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001016 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001017
1018 reg = FDI_RX_CTL(pipe);
1019 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001020 cur_state = !!(val & FDI_RX_PLL_ENABLE);
1021 WARN(cur_state != state,
1022 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1023 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001024}
1025
Jesse Barnesea0760c2011-01-04 15:09:32 -08001026static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1027 enum pipe pipe)
1028{
1029 int pp_reg, lvds_reg;
1030 u32 val;
1031 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001032 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001033
1034 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1035 pp_reg = PCH_PP_CONTROL;
1036 lvds_reg = PCH_LVDS;
1037 } else {
1038 pp_reg = PP_CONTROL;
1039 lvds_reg = LVDS;
1040 }
1041
1042 val = I915_READ(pp_reg);
1043 if (!(val & PANEL_POWER_ON) ||
1044 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1045 locked = false;
1046
1047 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1048 panel_pipe = PIPE_B;
1049
1050 WARN(panel_pipe == pipe && locked,
1051 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001052 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001053}
1054
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001055void assert_pipe(struct drm_i915_private *dev_priv,
1056 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001057{
1058 int reg;
1059 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001060 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001061 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1062 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001063
Daniel Vetter8e636782012-01-22 01:36:48 +01001064 /* if we need the pipe A quirk it must be always on */
1065 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1066 state = true;
1067
Paulo Zanonib97186f2013-05-03 12:15:36 -03001068 if (!intel_display_power_enabled(dev_priv->dev,
1069 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001070 cur_state = false;
1071 } else {
1072 reg = PIPECONF(cpu_transcoder);
1073 val = I915_READ(reg);
1074 cur_state = !!(val & PIPECONF_ENABLE);
1075 }
1076
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001077 WARN(cur_state != state,
1078 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001079 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001080}
1081
Chris Wilson931872f2012-01-16 23:01:13 +00001082static void assert_plane(struct drm_i915_private *dev_priv,
1083 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001084{
1085 int reg;
1086 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001087 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001088
1089 reg = DSPCNTR(plane);
1090 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001091 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1092 WARN(cur_state != state,
1093 "plane %c assertion failure (expected %s, current %s)\n",
1094 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001095}
1096
Chris Wilson931872f2012-01-16 23:01:13 +00001097#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1098#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1099
Jesse Barnesb24e7172011-01-04 15:09:30 -08001100static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1101 enum pipe pipe)
1102{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001103 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001104 int reg, i;
1105 u32 val;
1106 int cur_pipe;
1107
Ville Syrjälä653e1022013-06-04 13:49:05 +03001108 /* Primary planes are fixed to pipes on gen4+ */
1109 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001110 reg = DSPCNTR(pipe);
1111 val = I915_READ(reg);
1112 WARN((val & DISPLAY_PLANE_ENABLE),
1113 "plane %c assertion failure, should be disabled but not\n",
1114 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001115 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001116 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001117
Jesse Barnesb24e7172011-01-04 15:09:30 -08001118 /* Need to check both planes against the pipe */
Ville Syrjälä653e1022013-06-04 13:49:05 +03001119 for (i = 0; i < INTEL_INFO(dev)->num_pipes; i++) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001120 reg = DSPCNTR(i);
1121 val = I915_READ(reg);
1122 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1123 DISPPLANE_SEL_PIPE_SHIFT;
1124 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001125 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1126 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001127 }
1128}
1129
Jesse Barnes19332d72013-03-28 09:55:38 -07001130static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1131 enum pipe pipe)
1132{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001133 struct drm_device *dev = dev_priv->dev;
Jesse Barnes19332d72013-03-28 09:55:38 -07001134 int reg, i;
1135 u32 val;
1136
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001137 if (IS_VALLEYVIEW(dev)) {
1138 for (i = 0; i < dev_priv->num_plane; i++) {
1139 reg = SPCNTR(pipe, i);
1140 val = I915_READ(reg);
1141 WARN((val & SP_ENABLE),
1142 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1143 sprite_name(pipe, i), pipe_name(pipe));
1144 }
1145 } else if (INTEL_INFO(dev)->gen >= 7) {
1146 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001147 val = I915_READ(reg);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001148 WARN((val & SPRITE_ENABLE),
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001149 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001150 plane_name(pipe), pipe_name(pipe));
1151 } else if (INTEL_INFO(dev)->gen >= 5) {
1152 reg = DVSCNTR(pipe);
1153 val = I915_READ(reg);
1154 WARN((val & DVS_ENABLE),
1155 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1156 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001157 }
1158}
1159
Jesse Barnes92f25842011-01-04 15:09:34 -08001160static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1161{
1162 u32 val;
1163 bool enabled;
1164
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001165 if (HAS_PCH_LPT(dev_priv->dev)) {
1166 DRM_DEBUG_DRIVER("LPT does not has PCH refclk, skipping check\n");
1167 return;
1168 }
1169
Jesse Barnes92f25842011-01-04 15:09:34 -08001170 val = I915_READ(PCH_DREF_CONTROL);
1171 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1172 DREF_SUPERSPREAD_SOURCE_MASK));
1173 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1174}
1175
Daniel Vetterab9412b2013-05-03 11:49:46 +02001176static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1177 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001178{
1179 int reg;
1180 u32 val;
1181 bool enabled;
1182
Daniel Vetterab9412b2013-05-03 11:49:46 +02001183 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001184 val = I915_READ(reg);
1185 enabled = !!(val & TRANS_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001186 WARN(enabled,
1187 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1188 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001189}
1190
Keith Packard4e634382011-08-06 10:39:45 -07001191static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1192 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001193{
1194 if ((val & DP_PORT_EN) == 0)
1195 return false;
1196
1197 if (HAS_PCH_CPT(dev_priv->dev)) {
1198 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1199 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1200 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1201 return false;
1202 } else {
1203 if ((val & DP_PIPE_MASK) != (pipe << 30))
1204 return false;
1205 }
1206 return true;
1207}
1208
Keith Packard1519b992011-08-06 10:35:34 -07001209static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1210 enum pipe pipe, u32 val)
1211{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001212 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001213 return false;
1214
1215 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001216 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001217 return false;
1218 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001219 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001220 return false;
1221 }
1222 return true;
1223}
1224
1225static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1226 enum pipe pipe, u32 val)
1227{
1228 if ((val & LVDS_PORT_EN) == 0)
1229 return false;
1230
1231 if (HAS_PCH_CPT(dev_priv->dev)) {
1232 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1233 return false;
1234 } else {
1235 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1236 return false;
1237 }
1238 return true;
1239}
1240
1241static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1242 enum pipe pipe, u32 val)
1243{
1244 if ((val & ADPA_DAC_ENABLE) == 0)
1245 return false;
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 & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1251 return false;
1252 }
1253 return true;
1254}
1255
Jesse Barnes291906f2011-02-02 12:28:03 -08001256static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001257 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001258{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001259 u32 val = I915_READ(reg);
Keith Packard4e634382011-08-06 10:39:45 -07001260 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001261 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001262 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001263
Daniel Vetter75c5da22012-09-10 21:58:29 +02001264 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1265 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001266 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001267}
1268
1269static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1270 enum pipe pipe, int reg)
1271{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001272 u32 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001273 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001274 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001275 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001276
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001277 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001278 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001279 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001280}
1281
1282static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1283 enum pipe pipe)
1284{
1285 int reg;
1286 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001287
Keith Packardf0575e92011-07-25 22:12:43 -07001288 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1289 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1290 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001291
1292 reg = PCH_ADPA;
1293 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001294 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001295 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001296 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001297
1298 reg = PCH_LVDS;
1299 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001300 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001301 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001302 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001303
Paulo Zanonie2debe92013-02-18 19:00:27 -03001304 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1305 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1306 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001307}
1308
Daniel Vetter87442f72013-06-06 00:52:17 +02001309static void vlv_enable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001310{
1311 int reg;
1312 u32 val;
1313
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001314 assert_pipe_disabled(dev_priv, pipe);
1315
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001316 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001317 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1318
1319 /* PLL is protected by panel, make sure we can write it */
1320 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
1321 assert_panel_unlocked(dev_priv, pipe);
1322
1323 reg = DPLL(pipe);
1324 val = I915_READ(reg);
1325 val |= DPLL_VCO_ENABLE;
1326
1327 /* We do this three times for luck */
1328 I915_WRITE(reg, val);
1329 POSTING_READ(reg);
1330 udelay(150); /* wait for warmup */
1331 I915_WRITE(reg, val);
1332 POSTING_READ(reg);
1333 udelay(150); /* wait for warmup */
1334 I915_WRITE(reg, val);
1335 POSTING_READ(reg);
1336 udelay(150); /* wait for warmup */
1337}
1338
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001339static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001340{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001341 struct drm_device *dev = crtc->base.dev;
1342 struct drm_i915_private *dev_priv = dev->dev_private;
1343 int reg = DPLL(crtc->pipe);
1344 u32 dpll = crtc->config.dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001345
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001346 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001347
1348 /* No really, not for ILK+ */
1349 BUG_ON(dev_priv->info->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001350
1351 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001352 if (IS_MOBILE(dev) && !IS_I830(dev))
1353 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001354
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001355 I915_WRITE(reg, dpll);
1356
1357 /* Wait for the clocks to stabilize. */
1358 POSTING_READ(reg);
1359 udelay(150);
1360
1361 if (INTEL_INFO(dev)->gen >= 4) {
1362 I915_WRITE(DPLL_MD(crtc->pipe),
1363 crtc->config.dpll_hw_state.dpll_md);
1364 } else {
1365 /* The pixel multiplier can only be updated once the
1366 * DPLL is enabled and the clocks are stable.
1367 *
1368 * So write it again.
1369 */
1370 I915_WRITE(reg, dpll);
1371 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001372
1373 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001374 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001375 POSTING_READ(reg);
1376 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001377 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001378 POSTING_READ(reg);
1379 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001380 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001381 POSTING_READ(reg);
1382 udelay(150); /* wait for warmup */
1383}
1384
1385/**
1386 * intel_disable_pll - disable a PLL
1387 * @dev_priv: i915 private structure
1388 * @pipe: pipe PLL to disable
1389 *
1390 * Disable the PLL for @pipe, making sure the pipe is off first.
1391 *
1392 * Note! This is for pre-ILK only.
1393 */
1394static void intel_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1395{
1396 int reg;
1397 u32 val;
1398
1399 /* Don't disable pipe A or pipe A PLLs if needed */
1400 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1401 return;
1402
1403 /* Make sure the pipe isn't still relying on us */
1404 assert_pipe_disabled(dev_priv, pipe);
1405
1406 reg = DPLL(pipe);
1407 val = I915_READ(reg);
1408 val &= ~DPLL_VCO_ENABLE;
1409 I915_WRITE(reg, val);
1410 POSTING_READ(reg);
1411}
1412
Jesse Barnes89b667f2013-04-18 14:51:36 -07001413void vlv_wait_port_ready(struct drm_i915_private *dev_priv, int port)
1414{
1415 u32 port_mask;
1416
1417 if (!port)
1418 port_mask = DPLL_PORTB_READY_MASK;
1419 else
1420 port_mask = DPLL_PORTC_READY_MASK;
1421
1422 if (wait_for((I915_READ(DPLL(0)) & port_mask) == 0, 1000))
1423 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
1424 'B' + port, I915_READ(DPLL(0)));
1425}
1426
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001427/**
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001428 * ironlake_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001429 * @dev_priv: i915 private structure
1430 * @pipe: pipe PLL to enable
1431 *
1432 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1433 * drives the transcoder clock.
1434 */
Daniel Vettere2b78262013-06-07 23:10:03 +02001435static void ironlake_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001436{
Daniel Vettere2b78262013-06-07 23:10:03 +02001437 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1438 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001439
Chris Wilson48da64a2012-05-13 20:16:12 +01001440 /* PCH PLLs only available on ILK, SNB and IVB */
Jesse Barnes92f25842011-01-04 15:09:34 -08001441 BUG_ON(dev_priv->info->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001442 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001443 return;
1444
1445 if (WARN_ON(pll->refcount == 0))
1446 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001447
Daniel Vetter46edb022013-06-05 13:34:12 +02001448 DRM_DEBUG_KMS("enable %s (active %d, on? %d)for crtc %d\n",
1449 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001450 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001451
Daniel Vettercdbd2312013-06-05 13:34:03 +02001452 if (pll->active++) {
1453 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001454 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001455 return;
1456 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001457 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001458
Daniel Vetter46edb022013-06-05 13:34:12 +02001459 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001460 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001461 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001462}
1463
Daniel Vettere2b78262013-06-07 23:10:03 +02001464static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001465{
Daniel Vettere2b78262013-06-07 23:10:03 +02001466 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1467 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001468
Jesse Barnes92f25842011-01-04 15:09:34 -08001469 /* PCH only available on ILK+ */
1470 BUG_ON(dev_priv->info->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001471 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001472 return;
1473
Chris Wilson48da64a2012-05-13 20:16:12 +01001474 if (WARN_ON(pll->refcount == 0))
1475 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001476
Daniel Vetter46edb022013-06-05 13:34:12 +02001477 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1478 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001479 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001480
Chris Wilson48da64a2012-05-13 20:16:12 +01001481 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001482 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001483 return;
1484 }
1485
Daniel Vettere9d69442013-06-05 13:34:15 +02001486 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001487 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001488 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001489 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001490
Daniel Vetter46edb022013-06-05 13:34:12 +02001491 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001492 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001493 pll->on = false;
Jesse Barnes92f25842011-01-04 15:09:34 -08001494}
1495
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001496static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1497 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001498{
Daniel Vetter23670b322012-11-01 09:15:30 +01001499 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001500 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001501 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001502 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001503
1504 /* PCH only available on ILK+ */
1505 BUG_ON(dev_priv->info->gen < 5);
1506
1507 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001508 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001509 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001510
1511 /* FDI must be feeding us bits for PCH ports */
1512 assert_fdi_tx_enabled(dev_priv, pipe);
1513 assert_fdi_rx_enabled(dev_priv, pipe);
1514
Daniel Vetter23670b322012-11-01 09:15:30 +01001515 if (HAS_PCH_CPT(dev)) {
1516 /* Workaround: Set the timing override bit before enabling the
1517 * pch transcoder. */
1518 reg = TRANS_CHICKEN2(pipe);
1519 val = I915_READ(reg);
1520 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1521 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001522 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001523
Daniel Vetterab9412b2013-05-03 11:49:46 +02001524 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001525 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001526 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001527
1528 if (HAS_PCH_IBX(dev_priv->dev)) {
1529 /*
1530 * make the BPC in transcoder be consistent with
1531 * that in pipeconf reg.
1532 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001533 val &= ~PIPECONF_BPC_MASK;
1534 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001535 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001536
1537 val &= ~TRANS_INTERLACE_MASK;
1538 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001539 if (HAS_PCH_IBX(dev_priv->dev) &&
1540 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1541 val |= TRANS_LEGACY_INTERLACED_ILK;
1542 else
1543 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001544 else
1545 val |= TRANS_PROGRESSIVE;
1546
Jesse Barnes040484a2011-01-03 12:14:26 -08001547 I915_WRITE(reg, val | TRANS_ENABLE);
1548 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001549 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001550}
1551
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001552static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001553 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001554{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001555 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001556
1557 /* PCH only available on ILK+ */
1558 BUG_ON(dev_priv->info->gen < 5);
1559
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001560 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001561 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001562 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001563
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001564 /* Workaround: set timing override bit. */
1565 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001566 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001567 I915_WRITE(_TRANSA_CHICKEN2, val);
1568
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001569 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001570 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001571
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001572 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1573 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001574 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001575 else
1576 val |= TRANS_PROGRESSIVE;
1577
Daniel Vetterab9412b2013-05-03 11:49:46 +02001578 I915_WRITE(LPT_TRANSCONF, val);
1579 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001580 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001581}
1582
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001583static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1584 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001585{
Daniel Vetter23670b322012-11-01 09:15:30 +01001586 struct drm_device *dev = dev_priv->dev;
1587 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001588
1589 /* FDI relies on the transcoder */
1590 assert_fdi_tx_disabled(dev_priv, pipe);
1591 assert_fdi_rx_disabled(dev_priv, pipe);
1592
Jesse Barnes291906f2011-02-02 12:28:03 -08001593 /* Ports must be off as well */
1594 assert_pch_ports_disabled(dev_priv, pipe);
1595
Daniel Vetterab9412b2013-05-03 11:49:46 +02001596 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001597 val = I915_READ(reg);
1598 val &= ~TRANS_ENABLE;
1599 I915_WRITE(reg, val);
1600 /* wait for PCH transcoder off, transcoder state */
1601 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001602 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001603
1604 if (!HAS_PCH_IBX(dev)) {
1605 /* Workaround: Clear the timing override chicken bit again. */
1606 reg = TRANS_CHICKEN2(pipe);
1607 val = I915_READ(reg);
1608 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1609 I915_WRITE(reg, val);
1610 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001611}
1612
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001613static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001614{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001615 u32 val;
1616
Daniel Vetterab9412b2013-05-03 11:49:46 +02001617 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001618 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001619 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001620 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001621 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001622 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001623
1624 /* Workaround: clear timing override bit. */
1625 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001626 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001627 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001628}
1629
1630/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001631 * intel_enable_pipe - enable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001632 * @dev_priv: i915 private structure
1633 * @pipe: pipe to enable
Jesse Barnes040484a2011-01-03 12:14:26 -08001634 * @pch_port: on ILK+, is this pipe driving a PCH port or not
Jesse Barnesb24e7172011-01-04 15:09:30 -08001635 *
1636 * Enable @pipe, making sure that various hardware specific requirements
1637 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1638 *
1639 * @pipe should be %PIPE_A or %PIPE_B.
1640 *
1641 * Will wait until the pipe is actually running (i.e. first vblank) before
1642 * returning.
1643 */
Jesse Barnes040484a2011-01-03 12:14:26 -08001644static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
1645 bool pch_port)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001646{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001647 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1648 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001649 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001650 int reg;
1651 u32 val;
1652
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001653 assert_planes_disabled(dev_priv, pipe);
1654 assert_sprites_disabled(dev_priv, pipe);
1655
Paulo Zanoni681e5812012-12-06 11:12:38 -02001656 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001657 pch_transcoder = TRANSCODER_A;
1658 else
1659 pch_transcoder = pipe;
1660
Jesse Barnesb24e7172011-01-04 15:09:30 -08001661 /*
1662 * A pipe without a PLL won't actually be able to drive bits from
1663 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1664 * need the check.
1665 */
1666 if (!HAS_PCH_SPLIT(dev_priv->dev))
1667 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001668 else {
1669 if (pch_port) {
1670 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001671 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001672 assert_fdi_tx_pll_enabled(dev_priv,
1673 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001674 }
1675 /* FIXME: assert CPU port conditions for SNB+ */
1676 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001677
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001678 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001679 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001680 if (val & PIPECONF_ENABLE)
1681 return;
1682
1683 I915_WRITE(reg, val | PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001684 intel_wait_for_vblank(dev_priv->dev, pipe);
1685}
1686
1687/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001688 * intel_disable_pipe - disable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001689 * @dev_priv: i915 private structure
1690 * @pipe: pipe to disable
1691 *
1692 * Disable @pipe, making sure that various hardware specific requirements
1693 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1694 *
1695 * @pipe should be %PIPE_A or %PIPE_B.
1696 *
1697 * Will wait until the pipe has shut down before returning.
1698 */
1699static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1700 enum pipe pipe)
1701{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001702 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1703 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001704 int reg;
1705 u32 val;
1706
1707 /*
1708 * Make sure planes won't keep trying to pump pixels to us,
1709 * or we might hang the display.
1710 */
1711 assert_planes_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001712 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001713
1714 /* Don't disable pipe A or pipe A PLLs if needed */
1715 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1716 return;
1717
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001718 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001719 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001720 if ((val & PIPECONF_ENABLE) == 0)
1721 return;
1722
1723 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001724 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1725}
1726
Keith Packardd74362c2011-07-28 14:47:14 -07001727/*
1728 * Plane regs are double buffered, going from enabled->disabled needs a
1729 * trigger in order to latch. The display address reg provides this.
1730 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03001731void intel_flush_display_plane(struct drm_i915_private *dev_priv,
Keith Packardd74362c2011-07-28 14:47:14 -07001732 enum plane plane)
1733{
Damien Lespiau14f86142012-10-29 15:24:49 +00001734 if (dev_priv->info->gen >= 4)
1735 I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
1736 else
1737 I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
Keith Packardd74362c2011-07-28 14:47:14 -07001738}
1739
Jesse Barnesb24e7172011-01-04 15:09:30 -08001740/**
1741 * intel_enable_plane - enable a display plane on a given pipe
1742 * @dev_priv: i915 private structure
1743 * @plane: plane to enable
1744 * @pipe: pipe being fed
1745 *
1746 * Enable @plane on @pipe, making sure that @pipe is running first.
1747 */
1748static void intel_enable_plane(struct drm_i915_private *dev_priv,
1749 enum plane plane, enum pipe pipe)
1750{
1751 int reg;
1752 u32 val;
1753
1754 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1755 assert_pipe_enabled(dev_priv, pipe);
1756
1757 reg = DSPCNTR(plane);
1758 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001759 if (val & DISPLAY_PLANE_ENABLE)
1760 return;
1761
1762 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
Keith Packardd74362c2011-07-28 14:47:14 -07001763 intel_flush_display_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001764 intel_wait_for_vblank(dev_priv->dev, pipe);
1765}
1766
Jesse Barnesb24e7172011-01-04 15:09:30 -08001767/**
1768 * intel_disable_plane - disable a display plane
1769 * @dev_priv: i915 private structure
1770 * @plane: plane to disable
1771 * @pipe: pipe consuming the data
1772 *
1773 * Disable @plane; should be an independent operation.
1774 */
1775static void intel_disable_plane(struct drm_i915_private *dev_priv,
1776 enum plane plane, enum pipe pipe)
1777{
1778 int reg;
1779 u32 val;
1780
1781 reg = DSPCNTR(plane);
1782 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001783 if ((val & DISPLAY_PLANE_ENABLE) == 0)
1784 return;
1785
1786 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001787 intel_flush_display_plane(dev_priv, plane);
1788 intel_wait_for_vblank(dev_priv->dev, pipe);
1789}
1790
Chris Wilson693db182013-03-05 14:52:39 +00001791static bool need_vtd_wa(struct drm_device *dev)
1792{
1793#ifdef CONFIG_INTEL_IOMMU
1794 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
1795 return true;
1796#endif
1797 return false;
1798}
1799
Chris Wilson127bd2a2010-07-23 23:32:05 +01001800int
Chris Wilson48b956c2010-09-14 12:50:34 +01001801intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00001802 struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00001803 struct intel_ring_buffer *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001804{
Chris Wilsonce453d82011-02-21 14:43:56 +00001805 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001806 u32 alignment;
1807 int ret;
1808
Chris Wilson05394f32010-11-08 19:18:58 +00001809 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001810 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01001811 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1812 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001813 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01001814 alignment = 4 * 1024;
1815 else
1816 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001817 break;
1818 case I915_TILING_X:
1819 /* pin() will align the object as required by fence */
1820 alignment = 0;
1821 break;
1822 case I915_TILING_Y:
Daniel Vetter8bb6e952013-04-06 23:54:56 +02001823 /* Despite that we check this in framebuffer_init userspace can
1824 * screw us over and change the tiling after the fact. Only
1825 * pinned buffers can't change their tiling. */
1826 DRM_DEBUG_DRIVER("Y tiled not allowed for scan out buffers\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001827 return -EINVAL;
1828 default:
1829 BUG();
1830 }
1831
Chris Wilson693db182013-03-05 14:52:39 +00001832 /* Note that the w/a also requires 64 PTE of padding following the
1833 * bo. We currently fill all unused PTE with the shadow page and so
1834 * we should always have valid PTE following the scanout preventing
1835 * the VT-d warning.
1836 */
1837 if (need_vtd_wa(dev) && alignment < 256 * 1024)
1838 alignment = 256 * 1024;
1839
Chris Wilsonce453d82011-02-21 14:43:56 +00001840 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01001841 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01001842 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00001843 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001844
1845 /* Install a fence for tiled scan-out. Pre-i965 always needs a
1846 * fence, whereas 965+ only requires a fence if using
1847 * framebuffer compression. For simplicity, we always install
1848 * a fence as the cost is not that onerous.
1849 */
Chris Wilson06d98132012-04-17 15:31:24 +01001850 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001851 if (ret)
1852 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01001853
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001854 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001855
Chris Wilsonce453d82011-02-21 14:43:56 +00001856 dev_priv->mm.interruptible = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001857 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01001858
1859err_unpin:
1860 i915_gem_object_unpin(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00001861err_interruptible:
1862 dev_priv->mm.interruptible = true;
Chris Wilson48b956c2010-09-14 12:50:34 +01001863 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001864}
1865
Chris Wilson1690e1e2011-12-14 13:57:08 +01001866void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
1867{
1868 i915_gem_object_unpin_fence(obj);
1869 i915_gem_object_unpin(obj);
1870}
1871
Daniel Vetterc2c75132012-07-05 12:17:30 +02001872/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
1873 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00001874unsigned long intel_gen4_compute_page_offset(int *x, int *y,
1875 unsigned int tiling_mode,
1876 unsigned int cpp,
1877 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02001878{
Chris Wilsonbc752862013-02-21 20:04:31 +00001879 if (tiling_mode != I915_TILING_NONE) {
1880 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02001881
Chris Wilsonbc752862013-02-21 20:04:31 +00001882 tile_rows = *y / 8;
1883 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02001884
Chris Wilsonbc752862013-02-21 20:04:31 +00001885 tiles = *x / (512/cpp);
1886 *x %= 512/cpp;
1887
1888 return tile_rows * pitch * 8 + tiles * 4096;
1889 } else {
1890 unsigned int offset;
1891
1892 offset = *y * pitch + *x * cpp;
1893 *y = 0;
1894 *x = (offset & 4095) / cpp;
1895 return offset & -4096;
1896 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02001897}
1898
Jesse Barnes17638cd2011-06-24 12:19:23 -07001899static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
1900 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07001901{
1902 struct drm_device *dev = crtc->dev;
1903 struct drm_i915_private *dev_priv = dev->dev_private;
1904 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1905 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00001906 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07001907 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02001908 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07001909 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01001910 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07001911
1912 switch (plane) {
1913 case 0:
1914 case 1:
1915 break;
1916 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03001917 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes81255562010-08-02 12:07:50 -07001918 return -EINVAL;
1919 }
1920
1921 intel_fb = to_intel_framebuffer(fb);
1922 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07001923
Chris Wilson5eddb702010-09-11 13:48:45 +01001924 reg = DSPCNTR(plane);
1925 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07001926 /* Mask out pixel format bits in case we change it */
1927 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02001928 switch (fb->pixel_format) {
1929 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07001930 dspcntr |= DISPPLANE_8BPP;
1931 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02001932 case DRM_FORMAT_XRGB1555:
1933 case DRM_FORMAT_ARGB1555:
1934 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07001935 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02001936 case DRM_FORMAT_RGB565:
1937 dspcntr |= DISPPLANE_BGRX565;
1938 break;
1939 case DRM_FORMAT_XRGB8888:
1940 case DRM_FORMAT_ARGB8888:
1941 dspcntr |= DISPPLANE_BGRX888;
1942 break;
1943 case DRM_FORMAT_XBGR8888:
1944 case DRM_FORMAT_ABGR8888:
1945 dspcntr |= DISPPLANE_RGBX888;
1946 break;
1947 case DRM_FORMAT_XRGB2101010:
1948 case DRM_FORMAT_ARGB2101010:
1949 dspcntr |= DISPPLANE_BGRX101010;
1950 break;
1951 case DRM_FORMAT_XBGR2101010:
1952 case DRM_FORMAT_ABGR2101010:
1953 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07001954 break;
1955 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01001956 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07001957 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02001958
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001959 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00001960 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07001961 dspcntr |= DISPPLANE_TILED;
1962 else
1963 dspcntr &= ~DISPPLANE_TILED;
1964 }
1965
Ville Syrjäläde1aa622013-06-07 10:47:01 +03001966 if (IS_G4X(dev))
1967 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
1968
Chris Wilson5eddb702010-09-11 13:48:45 +01001969 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07001970
Daniel Vettere506a0c2012-07-05 12:17:29 +02001971 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes81255562010-08-02 12:07:50 -07001972
Daniel Vetterc2c75132012-07-05 12:17:30 +02001973 if (INTEL_INFO(dev)->gen >= 4) {
1974 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00001975 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
1976 fb->bits_per_pixel / 8,
1977 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02001978 linear_offset -= intel_crtc->dspaddr_offset;
1979 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02001980 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02001981 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02001982
Ben Widawskyf343c5f2013-07-05 14:41:04 -07001983 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
1984 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
1985 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001986 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001987 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetterc2c75132012-07-05 12:17:30 +02001988 I915_MODIFY_DISPBASE(DSPSURF(plane),
Ben Widawskyf343c5f2013-07-05 14:41:04 -07001989 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01001990 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02001991 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01001992 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07001993 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01001994 POSTING_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07001995
Jesse Barnes17638cd2011-06-24 12:19:23 -07001996 return 0;
1997}
1998
1999static int ironlake_update_plane(struct drm_crtc *crtc,
2000 struct drm_framebuffer *fb, int x, int y)
2001{
2002 struct drm_device *dev = crtc->dev;
2003 struct drm_i915_private *dev_priv = dev->dev_private;
2004 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2005 struct intel_framebuffer *intel_fb;
2006 struct drm_i915_gem_object *obj;
2007 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002008 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002009 u32 dspcntr;
2010 u32 reg;
2011
2012 switch (plane) {
2013 case 0:
2014 case 1:
Jesse Barnes27f82272011-09-02 12:54:37 -07002015 case 2:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002016 break;
2017 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002018 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes17638cd2011-06-24 12:19:23 -07002019 return -EINVAL;
2020 }
2021
2022 intel_fb = to_intel_framebuffer(fb);
2023 obj = intel_fb->obj;
2024
2025 reg = DSPCNTR(plane);
2026 dspcntr = I915_READ(reg);
2027 /* Mask out pixel format bits in case we change it */
2028 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002029 switch (fb->pixel_format) {
2030 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002031 dspcntr |= DISPPLANE_8BPP;
2032 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002033 case DRM_FORMAT_RGB565:
2034 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002035 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002036 case DRM_FORMAT_XRGB8888:
2037 case DRM_FORMAT_ARGB8888:
2038 dspcntr |= DISPPLANE_BGRX888;
2039 break;
2040 case DRM_FORMAT_XBGR8888:
2041 case DRM_FORMAT_ABGR8888:
2042 dspcntr |= DISPPLANE_RGBX888;
2043 break;
2044 case DRM_FORMAT_XRGB2101010:
2045 case DRM_FORMAT_ARGB2101010:
2046 dspcntr |= DISPPLANE_BGRX101010;
2047 break;
2048 case DRM_FORMAT_XBGR2101010:
2049 case DRM_FORMAT_ABGR2101010:
2050 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002051 break;
2052 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002053 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002054 }
2055
2056 if (obj->tiling_mode != I915_TILING_NONE)
2057 dspcntr |= DISPPLANE_TILED;
2058 else
2059 dspcntr &= ~DISPPLANE_TILED;
2060
2061 /* must disable */
2062 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2063
2064 I915_WRITE(reg, dspcntr);
2065
Daniel Vettere506a0c2012-07-05 12:17:29 +02002066 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002067 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002068 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2069 fb->bits_per_pixel / 8,
2070 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002071 linear_offset -= intel_crtc->dspaddr_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002072
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002073 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2074 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2075 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002076 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002077 I915_MODIFY_DISPBASE(DSPSURF(plane),
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002078 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002079 if (IS_HASWELL(dev)) {
2080 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2081 } else {
2082 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2083 I915_WRITE(DSPLINOFF(plane), linear_offset);
2084 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002085 POSTING_READ(reg);
2086
2087 return 0;
2088}
2089
2090/* Assume fb object is pinned & idle & fenced and just update base pointers */
2091static int
2092intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2093 int x, int y, enum mode_set_atomic state)
2094{
2095 struct drm_device *dev = crtc->dev;
2096 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002097
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002098 if (dev_priv->display.disable_fbc)
2099 dev_priv->display.disable_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02002100 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07002101
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002102 return dev_priv->display.update_plane(crtc, fb, x, y);
Jesse Barnes81255562010-08-02 12:07:50 -07002103}
2104
Ville Syrjälä96a02912013-02-18 19:08:49 +02002105void intel_display_handle_reset(struct drm_device *dev)
2106{
2107 struct drm_i915_private *dev_priv = dev->dev_private;
2108 struct drm_crtc *crtc;
2109
2110 /*
2111 * Flips in the rings have been nuked by the reset,
2112 * so complete all pending flips so that user space
2113 * will get its events and not get stuck.
2114 *
2115 * Also update the base address of all primary
2116 * planes to the the last fb to make sure we're
2117 * showing the correct fb after a reset.
2118 *
2119 * Need to make two loops over the crtcs so that we
2120 * don't try to grab a crtc mutex before the
2121 * pending_flip_queue really got woken up.
2122 */
2123
2124 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2125 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2126 enum plane plane = intel_crtc->plane;
2127
2128 intel_prepare_page_flip(dev, plane);
2129 intel_finish_page_flip_plane(dev, plane);
2130 }
2131
2132 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2133 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2134
2135 mutex_lock(&crtc->mutex);
2136 if (intel_crtc->active)
2137 dev_priv->display.update_plane(crtc, crtc->fb,
2138 crtc->x, crtc->y);
2139 mutex_unlock(&crtc->mutex);
2140 }
2141}
2142
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002143static int
Chris Wilson14667a42012-04-03 17:58:35 +01002144intel_finish_fb(struct drm_framebuffer *old_fb)
2145{
2146 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2147 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2148 bool was_interruptible = dev_priv->mm.interruptible;
2149 int ret;
2150
Chris Wilson14667a42012-04-03 17:58:35 +01002151 /* Big Hammer, we also need to ensure that any pending
2152 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2153 * current scanout is retired before unpinning the old
2154 * framebuffer.
2155 *
2156 * This should only fail upon a hung GPU, in which case we
2157 * can safely continue.
2158 */
2159 dev_priv->mm.interruptible = false;
2160 ret = i915_gem_object_finish_gpu(obj);
2161 dev_priv->mm.interruptible = was_interruptible;
2162
2163 return ret;
2164}
2165
Ville Syrjälä198598d2012-10-31 17:50:24 +02002166static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
2167{
2168 struct drm_device *dev = crtc->dev;
2169 struct drm_i915_master_private *master_priv;
2170 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2171
2172 if (!dev->primary->master)
2173 return;
2174
2175 master_priv = dev->primary->master->driver_priv;
2176 if (!master_priv->sarea_priv)
2177 return;
2178
2179 switch (intel_crtc->pipe) {
2180 case 0:
2181 master_priv->sarea_priv->pipeA_x = x;
2182 master_priv->sarea_priv->pipeA_y = y;
2183 break;
2184 case 1:
2185 master_priv->sarea_priv->pipeB_x = x;
2186 master_priv->sarea_priv->pipeB_y = y;
2187 break;
2188 default:
2189 break;
2190 }
2191}
2192
Chris Wilson14667a42012-04-03 17:58:35 +01002193static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002194intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002195 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002196{
2197 struct drm_device *dev = crtc->dev;
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002198 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08002199 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter94352cf2012-07-05 22:51:56 +02002200 struct drm_framebuffer *old_fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002201 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002202
2203 /* no fb bound */
Daniel Vetter94352cf2012-07-05 22:51:56 +02002204 if (!fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002205 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002206 return 0;
2207 }
2208
Ben Widawsky7eb552a2013-03-13 14:05:41 -07002209 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002210 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2211 plane_name(intel_crtc->plane),
2212 INTEL_INFO(dev)->num_pipes);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002213 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002214 }
2215
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002216 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01002217 ret = intel_pin_and_fence_fb_obj(dev,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002218 to_intel_framebuffer(fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002219 NULL);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002220 if (ret != 0) {
2221 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002222 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002223 return ret;
2224 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002225
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002226 /* Update pipe size and adjust fitter if needed */
2227 if (i915_fastboot) {
2228 I915_WRITE(PIPESRC(intel_crtc->pipe),
2229 ((crtc->mode.hdisplay - 1) << 16) |
2230 (crtc->mode.vdisplay - 1));
2231 if (!intel_crtc->config.pch_pfit.size &&
2232 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
2233 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2234 I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
2235 I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
2236 I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
2237 }
2238 }
2239
Daniel Vetter94352cf2012-07-05 22:51:56 +02002240 ret = dev_priv->display.update_plane(crtc, fb, x, y);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002241 if (ret) {
Daniel Vetter94352cf2012-07-05 22:51:56 +02002242 intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002243 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002244 DRM_ERROR("failed to update base address\n");
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002245 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002246 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002247
Daniel Vetter94352cf2012-07-05 22:51:56 +02002248 old_fb = crtc->fb;
2249 crtc->fb = fb;
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02002250 crtc->x = x;
2251 crtc->y = y;
Daniel Vetter94352cf2012-07-05 22:51:56 +02002252
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002253 if (old_fb) {
Daniel Vetterd7697ee2013-06-02 17:23:01 +02002254 if (intel_crtc->active && old_fb != fb)
2255 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002256 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002257 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002258
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002259 intel_update_fbc(dev);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002260 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002261
Ville Syrjälä198598d2012-10-31 17:50:24 +02002262 intel_crtc_update_sarea_pos(crtc, x, y);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002263
2264 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002265}
2266
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002267static void intel_fdi_normal_train(struct drm_crtc *crtc)
2268{
2269 struct drm_device *dev = crtc->dev;
2270 struct drm_i915_private *dev_priv = dev->dev_private;
2271 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2272 int pipe = intel_crtc->pipe;
2273 u32 reg, temp;
2274
2275 /* enable normal train */
2276 reg = FDI_TX_CTL(pipe);
2277 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002278 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002279 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2280 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002281 } else {
2282 temp &= ~FDI_LINK_TRAIN_NONE;
2283 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002284 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002285 I915_WRITE(reg, temp);
2286
2287 reg = FDI_RX_CTL(pipe);
2288 temp = I915_READ(reg);
2289 if (HAS_PCH_CPT(dev)) {
2290 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2291 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2292 } else {
2293 temp &= ~FDI_LINK_TRAIN_NONE;
2294 temp |= FDI_LINK_TRAIN_NONE;
2295 }
2296 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2297
2298 /* wait one idle pattern time */
2299 POSTING_READ(reg);
2300 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002301
2302 /* IVB wants error correction enabled */
2303 if (IS_IVYBRIDGE(dev))
2304 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2305 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002306}
2307
Daniel Vetter1e833f42013-02-19 22:31:57 +01002308static bool pipe_has_enabled_pch(struct intel_crtc *intel_crtc)
2309{
2310 return intel_crtc->base.enabled && intel_crtc->config.has_pch_encoder;
2311}
2312
Daniel Vetter01a415f2012-10-27 15:58:40 +02002313static void ivb_modeset_global_resources(struct drm_device *dev)
2314{
2315 struct drm_i915_private *dev_priv = dev->dev_private;
2316 struct intel_crtc *pipe_B_crtc =
2317 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2318 struct intel_crtc *pipe_C_crtc =
2319 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2320 uint32_t temp;
2321
Daniel Vetter1e833f42013-02-19 22:31:57 +01002322 /*
2323 * When everything is off disable fdi C so that we could enable fdi B
2324 * with all lanes. Note that we don't care about enabled pipes without
2325 * an enabled pch encoder.
2326 */
2327 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2328 !pipe_has_enabled_pch(pipe_C_crtc)) {
Daniel Vetter01a415f2012-10-27 15:58:40 +02002329 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2330 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2331
2332 temp = I915_READ(SOUTH_CHICKEN1);
2333 temp &= ~FDI_BC_BIFURCATION_SELECT;
2334 DRM_DEBUG_KMS("disabling fdi C rx\n");
2335 I915_WRITE(SOUTH_CHICKEN1, temp);
2336 }
2337}
2338
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002339/* The FDI link training functions for ILK/Ibexpeak. */
2340static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2341{
2342 struct drm_device *dev = crtc->dev;
2343 struct drm_i915_private *dev_priv = dev->dev_private;
2344 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2345 int pipe = intel_crtc->pipe;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002346 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002347 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002348
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002349 /* FDI needs bits from pipe & plane first */
2350 assert_pipe_enabled(dev_priv, pipe);
2351 assert_plane_enabled(dev_priv, plane);
2352
Adam Jacksone1a44742010-06-25 15:32:14 -04002353 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2354 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002355 reg = FDI_RX_IMR(pipe);
2356 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002357 temp &= ~FDI_RX_SYMBOL_LOCK;
2358 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002359 I915_WRITE(reg, temp);
2360 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002361 udelay(150);
2362
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002363 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002364 reg = FDI_TX_CTL(pipe);
2365 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002366 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2367 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002368 temp &= ~FDI_LINK_TRAIN_NONE;
2369 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002370 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002371
Chris Wilson5eddb702010-09-11 13:48:45 +01002372 reg = FDI_RX_CTL(pipe);
2373 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002374 temp &= ~FDI_LINK_TRAIN_NONE;
2375 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002376 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2377
2378 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002379 udelay(150);
2380
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002381 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002382 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2383 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2384 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002385
Chris Wilson5eddb702010-09-11 13:48:45 +01002386 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002387 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002388 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002389 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2390
2391 if ((temp & FDI_RX_BIT_LOCK)) {
2392 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002393 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002394 break;
2395 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002396 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002397 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002398 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002399
2400 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002401 reg = FDI_TX_CTL(pipe);
2402 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002403 temp &= ~FDI_LINK_TRAIN_NONE;
2404 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002405 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002406
Chris Wilson5eddb702010-09-11 13:48:45 +01002407 reg = FDI_RX_CTL(pipe);
2408 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002409 temp &= ~FDI_LINK_TRAIN_NONE;
2410 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002411 I915_WRITE(reg, temp);
2412
2413 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002414 udelay(150);
2415
Chris Wilson5eddb702010-09-11 13:48:45 +01002416 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002417 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002418 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002419 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2420
2421 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002422 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002423 DRM_DEBUG_KMS("FDI train 2 done.\n");
2424 break;
2425 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002426 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002427 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002428 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002429
2430 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002431
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002432}
2433
Akshay Joshi0206e352011-08-16 15:34:10 -04002434static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002435 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2436 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2437 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2438 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2439};
2440
2441/* The FDI link training functions for SNB/Cougarpoint. */
2442static void gen6_fdi_link_train(struct drm_crtc *crtc)
2443{
2444 struct drm_device *dev = crtc->dev;
2445 struct drm_i915_private *dev_priv = dev->dev_private;
2446 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2447 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05002448 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002449
Adam Jacksone1a44742010-06-25 15:32:14 -04002450 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2451 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002452 reg = FDI_RX_IMR(pipe);
2453 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002454 temp &= ~FDI_RX_SYMBOL_LOCK;
2455 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002456 I915_WRITE(reg, temp);
2457
2458 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002459 udelay(150);
2460
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002461 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002462 reg = FDI_TX_CTL(pipe);
2463 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002464 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2465 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002466 temp &= ~FDI_LINK_TRAIN_NONE;
2467 temp |= FDI_LINK_TRAIN_PATTERN_1;
2468 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2469 /* SNB-B */
2470 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002471 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002472
Daniel Vetterd74cf322012-10-26 10:58:13 +02002473 I915_WRITE(FDI_RX_MISC(pipe),
2474 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2475
Chris Wilson5eddb702010-09-11 13:48:45 +01002476 reg = FDI_RX_CTL(pipe);
2477 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002478 if (HAS_PCH_CPT(dev)) {
2479 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2480 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2481 } else {
2482 temp &= ~FDI_LINK_TRAIN_NONE;
2483 temp |= FDI_LINK_TRAIN_PATTERN_1;
2484 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002485 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2486
2487 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002488 udelay(150);
2489
Akshay Joshi0206e352011-08-16 15:34:10 -04002490 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002491 reg = FDI_TX_CTL(pipe);
2492 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002493 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2494 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002495 I915_WRITE(reg, temp);
2496
2497 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002498 udelay(500);
2499
Sean Paulfa37d392012-03-02 12:53:39 -05002500 for (retry = 0; retry < 5; retry++) {
2501 reg = FDI_RX_IIR(pipe);
2502 temp = I915_READ(reg);
2503 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2504 if (temp & FDI_RX_BIT_LOCK) {
2505 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2506 DRM_DEBUG_KMS("FDI train 1 done.\n");
2507 break;
2508 }
2509 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002510 }
Sean Paulfa37d392012-03-02 12:53:39 -05002511 if (retry < 5)
2512 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002513 }
2514 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002515 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002516
2517 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002518 reg = FDI_TX_CTL(pipe);
2519 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002520 temp &= ~FDI_LINK_TRAIN_NONE;
2521 temp |= FDI_LINK_TRAIN_PATTERN_2;
2522 if (IS_GEN6(dev)) {
2523 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2524 /* SNB-B */
2525 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2526 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002527 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002528
Chris Wilson5eddb702010-09-11 13:48:45 +01002529 reg = FDI_RX_CTL(pipe);
2530 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002531 if (HAS_PCH_CPT(dev)) {
2532 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2533 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2534 } else {
2535 temp &= ~FDI_LINK_TRAIN_NONE;
2536 temp |= FDI_LINK_TRAIN_PATTERN_2;
2537 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002538 I915_WRITE(reg, temp);
2539
2540 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002541 udelay(150);
2542
Akshay Joshi0206e352011-08-16 15:34:10 -04002543 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002544 reg = FDI_TX_CTL(pipe);
2545 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002546 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2547 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002548 I915_WRITE(reg, temp);
2549
2550 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002551 udelay(500);
2552
Sean Paulfa37d392012-03-02 12:53:39 -05002553 for (retry = 0; retry < 5; retry++) {
2554 reg = FDI_RX_IIR(pipe);
2555 temp = I915_READ(reg);
2556 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2557 if (temp & FDI_RX_SYMBOL_LOCK) {
2558 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2559 DRM_DEBUG_KMS("FDI train 2 done.\n");
2560 break;
2561 }
2562 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002563 }
Sean Paulfa37d392012-03-02 12:53:39 -05002564 if (retry < 5)
2565 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002566 }
2567 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002568 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002569
2570 DRM_DEBUG_KMS("FDI train done.\n");
2571}
2572
Jesse Barnes357555c2011-04-28 15:09:55 -07002573/* Manual link training for Ivy Bridge A0 parts */
2574static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2575{
2576 struct drm_device *dev = crtc->dev;
2577 struct drm_i915_private *dev_priv = dev->dev_private;
2578 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2579 int pipe = intel_crtc->pipe;
2580 u32 reg, temp, i;
2581
2582 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2583 for train result */
2584 reg = FDI_RX_IMR(pipe);
2585 temp = I915_READ(reg);
2586 temp &= ~FDI_RX_SYMBOL_LOCK;
2587 temp &= ~FDI_RX_BIT_LOCK;
2588 I915_WRITE(reg, temp);
2589
2590 POSTING_READ(reg);
2591 udelay(150);
2592
Daniel Vetter01a415f2012-10-27 15:58:40 +02002593 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
2594 I915_READ(FDI_RX_IIR(pipe)));
2595
Jesse Barnes357555c2011-04-28 15:09:55 -07002596 /* enable CPU FDI TX and PCH FDI RX */
2597 reg = FDI_TX_CTL(pipe);
2598 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002599 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2600 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Jesse Barnes357555c2011-04-28 15:09:55 -07002601 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2602 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
2603 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2604 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002605 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002606 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2607
Daniel Vetterd74cf322012-10-26 10:58:13 +02002608 I915_WRITE(FDI_RX_MISC(pipe),
2609 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2610
Jesse Barnes357555c2011-04-28 15:09:55 -07002611 reg = FDI_RX_CTL(pipe);
2612 temp = I915_READ(reg);
2613 temp &= ~FDI_LINK_TRAIN_AUTO;
2614 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2615 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002616 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002617 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2618
2619 POSTING_READ(reg);
2620 udelay(150);
2621
Akshay Joshi0206e352011-08-16 15:34:10 -04002622 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002623 reg = FDI_TX_CTL(pipe);
2624 temp = I915_READ(reg);
2625 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2626 temp |= snb_b_fdi_train_param[i];
2627 I915_WRITE(reg, temp);
2628
2629 POSTING_READ(reg);
2630 udelay(500);
2631
2632 reg = FDI_RX_IIR(pipe);
2633 temp = I915_READ(reg);
2634 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2635
2636 if (temp & FDI_RX_BIT_LOCK ||
2637 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2638 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Daniel Vetter01a415f2012-10-27 15:58:40 +02002639 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n", i);
Jesse Barnes357555c2011-04-28 15:09:55 -07002640 break;
2641 }
2642 }
2643 if (i == 4)
2644 DRM_ERROR("FDI train 1 fail!\n");
2645
2646 /* Train 2 */
2647 reg = FDI_TX_CTL(pipe);
2648 temp = I915_READ(reg);
2649 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2650 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2651 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2652 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2653 I915_WRITE(reg, temp);
2654
2655 reg = FDI_RX_CTL(pipe);
2656 temp = I915_READ(reg);
2657 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2658 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2659 I915_WRITE(reg, temp);
2660
2661 POSTING_READ(reg);
2662 udelay(150);
2663
Akshay Joshi0206e352011-08-16 15:34:10 -04002664 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002665 reg = FDI_TX_CTL(pipe);
2666 temp = I915_READ(reg);
2667 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2668 temp |= snb_b_fdi_train_param[i];
2669 I915_WRITE(reg, temp);
2670
2671 POSTING_READ(reg);
2672 udelay(500);
2673
2674 reg = FDI_RX_IIR(pipe);
2675 temp = I915_READ(reg);
2676 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2677
2678 if (temp & FDI_RX_SYMBOL_LOCK) {
2679 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Daniel Vetter01a415f2012-10-27 15:58:40 +02002680 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n", i);
Jesse Barnes357555c2011-04-28 15:09:55 -07002681 break;
2682 }
2683 }
2684 if (i == 4)
2685 DRM_ERROR("FDI train 2 fail!\n");
2686
2687 DRM_DEBUG_KMS("FDI train done.\n");
2688}
2689
Daniel Vetter88cefb62012-08-12 19:27:14 +02002690static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07002691{
Daniel Vetter88cefb62012-08-12 19:27:14 +02002692 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002693 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002694 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002695 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002696
Jesse Barnesc64e3112010-09-10 11:27:03 -07002697
Jesse Barnes0e23b992010-09-10 11:10:00 -07002698 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01002699 reg = FDI_RX_CTL(pipe);
2700 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002701 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
2702 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002703 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01002704 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2705
2706 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002707 udelay(200);
2708
2709 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002710 temp = I915_READ(reg);
2711 I915_WRITE(reg, temp | FDI_PCDCLK);
2712
2713 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002714 udelay(200);
2715
Paulo Zanoni20749732012-11-23 15:30:38 -02002716 /* Enable CPU FDI TX PLL, always on for Ironlake */
2717 reg = FDI_TX_CTL(pipe);
2718 temp = I915_READ(reg);
2719 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2720 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01002721
Paulo Zanoni20749732012-11-23 15:30:38 -02002722 POSTING_READ(reg);
2723 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002724 }
2725}
2726
Daniel Vetter88cefb62012-08-12 19:27:14 +02002727static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
2728{
2729 struct drm_device *dev = intel_crtc->base.dev;
2730 struct drm_i915_private *dev_priv = dev->dev_private;
2731 int pipe = intel_crtc->pipe;
2732 u32 reg, temp;
2733
2734 /* Switch from PCDclk to Rawclk */
2735 reg = FDI_RX_CTL(pipe);
2736 temp = I915_READ(reg);
2737 I915_WRITE(reg, temp & ~FDI_PCDCLK);
2738
2739 /* Disable CPU FDI TX PLL */
2740 reg = FDI_TX_CTL(pipe);
2741 temp = I915_READ(reg);
2742 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2743
2744 POSTING_READ(reg);
2745 udelay(100);
2746
2747 reg = FDI_RX_CTL(pipe);
2748 temp = I915_READ(reg);
2749 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2750
2751 /* Wait for the clocks to turn off. */
2752 POSTING_READ(reg);
2753 udelay(100);
2754}
2755
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002756static void ironlake_fdi_disable(struct drm_crtc *crtc)
2757{
2758 struct drm_device *dev = crtc->dev;
2759 struct drm_i915_private *dev_priv = dev->dev_private;
2760 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2761 int pipe = intel_crtc->pipe;
2762 u32 reg, temp;
2763
2764 /* disable CPU FDI tx and PCH FDI rx */
2765 reg = FDI_TX_CTL(pipe);
2766 temp = I915_READ(reg);
2767 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2768 POSTING_READ(reg);
2769
2770 reg = FDI_RX_CTL(pipe);
2771 temp = I915_READ(reg);
2772 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002773 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002774 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2775
2776 POSTING_READ(reg);
2777 udelay(100);
2778
2779 /* Ironlake workaround, disable clock pointer after downing FDI */
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002780 if (HAS_PCH_IBX(dev)) {
2781 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002782 }
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002783
2784 /* still set train pattern 1 */
2785 reg = FDI_TX_CTL(pipe);
2786 temp = I915_READ(reg);
2787 temp &= ~FDI_LINK_TRAIN_NONE;
2788 temp |= FDI_LINK_TRAIN_PATTERN_1;
2789 I915_WRITE(reg, temp);
2790
2791 reg = FDI_RX_CTL(pipe);
2792 temp = I915_READ(reg);
2793 if (HAS_PCH_CPT(dev)) {
2794 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2795 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2796 } else {
2797 temp &= ~FDI_LINK_TRAIN_NONE;
2798 temp |= FDI_LINK_TRAIN_PATTERN_1;
2799 }
2800 /* BPC in FDI rx is consistent with that in PIPECONF */
2801 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002802 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002803 I915_WRITE(reg, temp);
2804
2805 POSTING_READ(reg);
2806 udelay(100);
2807}
2808
Chris Wilson5bb61642012-09-27 21:25:58 +01002809static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2810{
2811 struct drm_device *dev = crtc->dev;
2812 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä10d83732013-01-29 18:13:34 +02002813 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5bb61642012-09-27 21:25:58 +01002814 unsigned long flags;
2815 bool pending;
2816
Ville Syrjälä10d83732013-01-29 18:13:34 +02002817 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2818 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
Chris Wilson5bb61642012-09-27 21:25:58 +01002819 return false;
2820
2821 spin_lock_irqsave(&dev->event_lock, flags);
2822 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2823 spin_unlock_irqrestore(&dev->event_lock, flags);
2824
2825 return pending;
2826}
2827
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002828static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2829{
Chris Wilson0f911282012-04-17 10:05:38 +01002830 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01002831 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002832
2833 if (crtc->fb == NULL)
2834 return;
2835
Daniel Vetter2c10d572012-12-20 21:24:07 +01002836 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
2837
Chris Wilson5bb61642012-09-27 21:25:58 +01002838 wait_event(dev_priv->pending_flip_queue,
2839 !intel_crtc_has_pending_flip(crtc));
2840
Chris Wilson0f911282012-04-17 10:05:38 +01002841 mutex_lock(&dev->struct_mutex);
2842 intel_finish_fb(crtc->fb);
2843 mutex_unlock(&dev->struct_mutex);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002844}
2845
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002846/* Program iCLKIP clock to the desired frequency */
2847static void lpt_program_iclkip(struct drm_crtc *crtc)
2848{
2849 struct drm_device *dev = crtc->dev;
2850 struct drm_i915_private *dev_priv = dev->dev_private;
2851 u32 divsel, phaseinc, auxdiv, phasedir = 0;
2852 u32 temp;
2853
Daniel Vetter09153002012-12-12 14:06:44 +01002854 mutex_lock(&dev_priv->dpio_lock);
2855
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002856 /* It is necessary to ungate the pixclk gate prior to programming
2857 * the divisors, and gate it back when it is done.
2858 */
2859 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
2860
2861 /* Disable SSCCTL */
2862 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002863 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
2864 SBI_SSCCTL_DISABLE,
2865 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002866
2867 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
2868 if (crtc->mode.clock == 20000) {
2869 auxdiv = 1;
2870 divsel = 0x41;
2871 phaseinc = 0x20;
2872 } else {
2873 /* The iCLK virtual clock root frequency is in MHz,
2874 * but the crtc->mode.clock in in KHz. To get the divisors,
2875 * it is necessary to divide one by another, so we
2876 * convert the virtual clock precision to KHz here for higher
2877 * precision.
2878 */
2879 u32 iclk_virtual_root_freq = 172800 * 1000;
2880 u32 iclk_pi_range = 64;
2881 u32 desired_divisor, msb_divisor_value, pi_value;
2882
2883 desired_divisor = (iclk_virtual_root_freq / crtc->mode.clock);
2884 msb_divisor_value = desired_divisor / iclk_pi_range;
2885 pi_value = desired_divisor % iclk_pi_range;
2886
2887 auxdiv = 0;
2888 divsel = msb_divisor_value - 2;
2889 phaseinc = pi_value;
2890 }
2891
2892 /* This should not happen with any sane values */
2893 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
2894 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
2895 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
2896 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
2897
2898 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
2899 crtc->mode.clock,
2900 auxdiv,
2901 divsel,
2902 phasedir,
2903 phaseinc);
2904
2905 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002906 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002907 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
2908 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
2909 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
2910 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
2911 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
2912 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002913 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002914
2915 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002916 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002917 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
2918 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002919 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002920
2921 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002922 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002923 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002924 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002925
2926 /* Wait for initialization time */
2927 udelay(24);
2928
2929 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01002930
2931 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002932}
2933
Daniel Vetter275f01b22013-05-03 11:49:47 +02002934static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
2935 enum pipe pch_transcoder)
2936{
2937 struct drm_device *dev = crtc->base.dev;
2938 struct drm_i915_private *dev_priv = dev->dev_private;
2939 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
2940
2941 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
2942 I915_READ(HTOTAL(cpu_transcoder)));
2943 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
2944 I915_READ(HBLANK(cpu_transcoder)));
2945 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
2946 I915_READ(HSYNC(cpu_transcoder)));
2947
2948 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
2949 I915_READ(VTOTAL(cpu_transcoder)));
2950 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
2951 I915_READ(VBLANK(cpu_transcoder)));
2952 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
2953 I915_READ(VSYNC(cpu_transcoder)));
2954 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
2955 I915_READ(VSYNCSHIFT(cpu_transcoder)));
2956}
2957
Jesse Barnesf67a5592011-01-05 10:31:48 -08002958/*
2959 * Enable PCH resources required for PCH ports:
2960 * - PCH PLLs
2961 * - FDI training & RX/TX
2962 * - update transcoder timings
2963 * - DP transcoding bits
2964 * - transcoder
2965 */
2966static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08002967{
2968 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002969 struct drm_i915_private *dev_priv = dev->dev_private;
2970 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2971 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01002972 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07002973
Daniel Vetterab9412b2013-05-03 11:49:46 +02002974 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01002975
Daniel Vettercd986ab2012-10-26 10:58:12 +02002976 /* Write the TU size bits before fdi link training, so that error
2977 * detection works. */
2978 I915_WRITE(FDI_RX_TUSIZE1(pipe),
2979 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
2980
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002981 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07002982 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002983
Daniel Vetter572deb32012-10-27 18:46:14 +02002984 /* XXX: pch pll's can be enabled any time before we enable the PCH
2985 * transcoder, and we actually should do this to not upset any PCH
2986 * transcoder that already use the clock when we share it.
2987 *
Daniel Vettere72f9fb2013-06-05 13:34:06 +02002988 * Note that enable_shared_dpll tries to do the right thing, but
2989 * get_shared_dpll unconditionally resets the pll - we need that to have
2990 * the right LVDS enable sequence. */
2991 ironlake_enable_shared_dpll(intel_crtc);
Chris Wilson6f13b7b2012-05-13 09:54:09 +01002992
Paulo Zanoni303b81e2012-10-31 18:12:23 -02002993 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01002994 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07002995
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002996 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02002997 temp |= TRANS_DPLL_ENABLE(pipe);
2998 sel = TRANS_DPLLB_SEL(pipe);
Daniel Vettera43f6e02013-06-07 23:10:32 +02002999 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003000 temp |= sel;
3001 else
3002 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003003 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003004 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003005
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003006 /* set transcoder timing, panel must allow it */
3007 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02003008 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003009
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003010 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003011
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003012 /* For PCH DP, enable TRANS_DP_CTL */
3013 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07003014 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3015 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003016 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003017 reg = TRANS_DP_CTL(pipe);
3018 temp = I915_READ(reg);
3019 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003020 TRANS_DP_SYNC_MASK |
3021 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003022 temp |= (TRANS_DP_OUTPUT_ENABLE |
3023 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003024 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003025
3026 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003027 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003028 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003029 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003030
3031 switch (intel_trans_dp_port_sel(crtc)) {
3032 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003033 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003034 break;
3035 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003036 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003037 break;
3038 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003039 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003040 break;
3041 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003042 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003043 }
3044
Chris Wilson5eddb702010-09-11 13:48:45 +01003045 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003046 }
3047
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003048 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003049}
3050
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003051static void lpt_pch_enable(struct drm_crtc *crtc)
3052{
3053 struct drm_device *dev = crtc->dev;
3054 struct drm_i915_private *dev_priv = dev->dev_private;
3055 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02003056 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003057
Daniel Vetterab9412b2013-05-03 11:49:46 +02003058 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003059
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003060 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003061
Paulo Zanoni0540e482012-10-31 18:12:40 -02003062 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02003063 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003064
Paulo Zanoni937bb612012-10-31 18:12:47 -02003065 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003066}
3067
Daniel Vettere2b78262013-06-07 23:10:03 +02003068static void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003069{
Daniel Vettere2b78262013-06-07 23:10:03 +02003070 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003071
3072 if (pll == NULL)
3073 return;
3074
3075 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003076 WARN(1, "bad %s refcount\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003077 return;
3078 }
3079
Daniel Vetterf4a091c2013-06-10 17:28:22 +02003080 if (--pll->refcount == 0) {
3081 WARN_ON(pll->on);
3082 WARN_ON(pll->active);
3083 }
3084
Daniel Vettera43f6e02013-06-07 23:10:32 +02003085 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003086}
3087
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003088static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003089{
Daniel Vettere2b78262013-06-07 23:10:03 +02003090 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3091 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3092 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003093
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003094 if (pll) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003095 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3096 crtc->base.base.id, pll->name);
Daniel Vettere2b78262013-06-07 23:10:03 +02003097 intel_put_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003098 }
3099
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003100 if (HAS_PCH_IBX(dev_priv->dev)) {
3101 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02003102 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003103 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003104
Daniel Vetter46edb022013-06-05 13:34:12 +02003105 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3106 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003107
3108 goto found;
3109 }
3110
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003111 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3112 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003113
3114 /* Only want to check enabled timings first */
3115 if (pll->refcount == 0)
3116 continue;
3117
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003118 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3119 sizeof(pll->hw_state)) == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003120 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
Daniel Vettere2b78262013-06-07 23:10:03 +02003121 crtc->base.base.id,
Daniel Vetter46edb022013-06-05 13:34:12 +02003122 pll->name, pll->refcount, pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003123
3124 goto found;
3125 }
3126 }
3127
3128 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003129 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3130 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003131 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003132 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3133 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003134 goto found;
3135 }
3136 }
3137
3138 return NULL;
3139
3140found:
Daniel Vettera43f6e02013-06-07 23:10:32 +02003141 crtc->config.shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02003142 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3143 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02003144
Daniel Vettercdbd2312013-06-05 13:34:03 +02003145 if (pll->active == 0) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02003146 memcpy(&pll->hw_state, &crtc->config.dpll_hw_state,
3147 sizeof(pll->hw_state));
3148
Daniel Vetter46edb022013-06-05 13:34:12 +02003149 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
Daniel Vettercdbd2312013-06-05 13:34:03 +02003150 WARN_ON(pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02003151 assert_shared_dpll_disabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003152
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02003153 pll->mode_set(dev_priv, pll);
Daniel Vettercdbd2312013-06-05 13:34:03 +02003154 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003155 pll->refcount++;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003156
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003157 return pll;
3158}
3159
Daniel Vettera1520312013-05-03 11:49:50 +02003160static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07003161{
3162 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01003163 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003164 u32 temp;
3165
3166 temp = I915_READ(dslreg);
3167 udelay(500);
3168 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07003169 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03003170 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07003171 }
3172}
3173
Jesse Barnesb074cec2013-04-25 12:55:02 -07003174static void ironlake_pfit_enable(struct intel_crtc *crtc)
3175{
3176 struct drm_device *dev = crtc->base.dev;
3177 struct drm_i915_private *dev_priv = dev->dev_private;
3178 int pipe = crtc->pipe;
3179
Jesse Barnes0ef37f32013-05-03 13:26:37 -07003180 if (crtc->config.pch_pfit.size) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07003181 /* Force use of hard-coded filter coefficients
3182 * as some pre-programmed values are broken,
3183 * e.g. x201.
3184 */
3185 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3186 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3187 PF_PIPE_SEL_IVB(pipe));
3188 else
3189 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3190 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3191 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08003192 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003193}
3194
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003195static void intel_enable_planes(struct drm_crtc *crtc)
3196{
3197 struct drm_device *dev = crtc->dev;
3198 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3199 struct intel_plane *intel_plane;
3200
3201 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3202 if (intel_plane->pipe == pipe)
3203 intel_plane_restore(&intel_plane->base);
3204}
3205
3206static void intel_disable_planes(struct drm_crtc *crtc)
3207{
3208 struct drm_device *dev = crtc->dev;
3209 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3210 struct intel_plane *intel_plane;
3211
3212 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3213 if (intel_plane->pipe == pipe)
3214 intel_plane_disable(&intel_plane->base);
3215}
3216
Jesse Barnesf67a5592011-01-05 10:31:48 -08003217static void ironlake_crtc_enable(struct drm_crtc *crtc)
3218{
3219 struct drm_device *dev = crtc->dev;
3220 struct drm_i915_private *dev_priv = dev->dev_private;
3221 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003222 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003223 int pipe = intel_crtc->pipe;
3224 int plane = intel_crtc->plane;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003225
Daniel Vetter08a48462012-07-02 11:43:47 +02003226 WARN_ON(!crtc->enabled);
3227
Jesse Barnesf67a5592011-01-05 10:31:48 -08003228 if (intel_crtc->active)
3229 return;
3230
3231 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003232
3233 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3234 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
3235
Jesse Barnesf67a5592011-01-05 10:31:48 -08003236 intel_update_watermarks(dev);
3237
Daniel Vetterf6736a12013-06-05 13:34:30 +02003238 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02003239 if (encoder->pre_enable)
3240 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003241
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003242 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02003243 /* Note: FDI PLL enabling _must_ be done before we enable the
3244 * cpu pipes, hence this is separate from all the other fdi/pch
3245 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02003246 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02003247 } else {
3248 assert_fdi_tx_disabled(dev_priv, pipe);
3249 assert_fdi_rx_disabled(dev_priv, pipe);
3250 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003251
Jesse Barnesb074cec2013-04-25 12:55:02 -07003252 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003253
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02003254 /*
3255 * On ILK+ LUT must be loaded before the pipe is running but with
3256 * clocks enabled
3257 */
3258 intel_crtc_load_lut(crtc);
3259
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003260 intel_enable_pipe(dev_priv, pipe,
3261 intel_crtc->config.has_pch_encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003262 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003263 intel_enable_planes(crtc);
Ville Syrjälä5c38d482013-06-04 13:49:00 +03003264 intel_crtc_update_cursor(crtc, true);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003265
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003266 if (intel_crtc->config.has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08003267 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003268
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003269 mutex_lock(&dev->struct_mutex);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003270 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003271 mutex_unlock(&dev->struct_mutex);
3272
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003273 for_each_encoder_on_crtc(dev, crtc, encoder)
3274 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02003275
3276 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02003277 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02003278
3279 /*
3280 * There seems to be a race in PCH platform hw (at least on some
3281 * outputs) where an enabled pipe still completes any pageflip right
3282 * away (as if the pipe is off) instead of waiting for vblank. As soon
3283 * as the first vblank happend, everything works as expected. Hence just
3284 * wait for one vblank before returning to avoid strange things
3285 * happening.
3286 */
3287 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003288}
3289
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003290/* IPS only exists on ULT machines and is tied to pipe A. */
3291static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
3292{
Damien Lespiauf5adf942013-06-24 18:29:34 +01003293 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003294}
3295
3296static void hsw_enable_ips(struct intel_crtc *crtc)
3297{
3298 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3299
3300 if (!crtc->config.ips_enabled)
3301 return;
3302
3303 /* We can only enable IPS after we enable a plane and wait for a vblank.
3304 * We guarantee that the plane is enabled by calling intel_enable_ips
3305 * only after intel_enable_plane. And intel_enable_plane already waits
3306 * for a vblank, so all we need to do here is to enable the IPS bit. */
3307 assert_plane_enabled(dev_priv, crtc->plane);
3308 I915_WRITE(IPS_CTL, IPS_ENABLE);
3309}
3310
3311static void hsw_disable_ips(struct intel_crtc *crtc)
3312{
3313 struct drm_device *dev = crtc->base.dev;
3314 struct drm_i915_private *dev_priv = dev->dev_private;
3315
3316 if (!crtc->config.ips_enabled)
3317 return;
3318
3319 assert_plane_enabled(dev_priv, crtc->plane);
3320 I915_WRITE(IPS_CTL, 0);
3321
3322 /* We need to wait for a vblank before we can disable the plane. */
3323 intel_wait_for_vblank(dev, crtc->pipe);
3324}
3325
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003326static void haswell_crtc_enable(struct drm_crtc *crtc)
3327{
3328 struct drm_device *dev = crtc->dev;
3329 struct drm_i915_private *dev_priv = dev->dev_private;
3330 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3331 struct intel_encoder *encoder;
3332 int pipe = intel_crtc->pipe;
3333 int plane = intel_crtc->plane;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003334
3335 WARN_ON(!crtc->enabled);
3336
3337 if (intel_crtc->active)
3338 return;
3339
3340 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003341
3342 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3343 if (intel_crtc->config.has_pch_encoder)
3344 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
3345
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003346 intel_update_watermarks(dev);
3347
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003348 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni04945642012-11-01 21:00:59 -02003349 dev_priv->display.fdi_link_train(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003350
3351 for_each_encoder_on_crtc(dev, crtc, encoder)
3352 if (encoder->pre_enable)
3353 encoder->pre_enable(encoder);
3354
Paulo Zanoni1f544382012-10-24 11:32:00 -02003355 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003356
Jesse Barnesb074cec2013-04-25 12:55:02 -07003357 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003358
3359 /*
3360 * On ILK+ LUT must be loaded before the pipe is running but with
3361 * clocks enabled
3362 */
3363 intel_crtc_load_lut(crtc);
3364
Paulo Zanoni1f544382012-10-24 11:32:00 -02003365 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00003366 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003367
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003368 intel_enable_pipe(dev_priv, pipe,
3369 intel_crtc->config.has_pch_encoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003370 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003371 intel_enable_planes(crtc);
Ville Syrjälä5c38d482013-06-04 13:49:00 +03003372 intel_crtc_update_cursor(crtc, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003373
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003374 hsw_enable_ips(intel_crtc);
3375
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003376 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003377 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003378
3379 mutex_lock(&dev->struct_mutex);
3380 intel_update_fbc(dev);
3381 mutex_unlock(&dev->struct_mutex);
3382
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003383 for_each_encoder_on_crtc(dev, crtc, encoder)
3384 encoder->enable(encoder);
3385
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003386 /*
3387 * There seems to be a race in PCH platform hw (at least on some
3388 * outputs) where an enabled pipe still completes any pageflip right
3389 * away (as if the pipe is off) instead of waiting for vblank. As soon
3390 * as the first vblank happend, everything works as expected. Hence just
3391 * wait for one vblank before returning to avoid strange things
3392 * happening.
3393 */
3394 intel_wait_for_vblank(dev, intel_crtc->pipe);
3395}
3396
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003397static void ironlake_pfit_disable(struct intel_crtc *crtc)
3398{
3399 struct drm_device *dev = crtc->base.dev;
3400 struct drm_i915_private *dev_priv = dev->dev_private;
3401 int pipe = crtc->pipe;
3402
3403 /* To avoid upsetting the power well on haswell only disable the pfit if
3404 * it's in use. The hw state code will make sure we get this right. */
3405 if (crtc->config.pch_pfit.size) {
3406 I915_WRITE(PF_CTL(pipe), 0);
3407 I915_WRITE(PF_WIN_POS(pipe), 0);
3408 I915_WRITE(PF_WIN_SZ(pipe), 0);
3409 }
3410}
3411
Jesse Barnes6be4a602010-09-10 10:26:01 -07003412static void ironlake_crtc_disable(struct drm_crtc *crtc)
3413{
3414 struct drm_device *dev = crtc->dev;
3415 struct drm_i915_private *dev_priv = dev->dev_private;
3416 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003417 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003418 int pipe = intel_crtc->pipe;
3419 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01003420 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003421
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003422
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003423 if (!intel_crtc->active)
3424 return;
3425
Daniel Vetterea9d7582012-07-10 10:42:52 +02003426 for_each_encoder_on_crtc(dev, crtc, encoder)
3427 encoder->disable(encoder);
3428
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003429 intel_crtc_wait_for_pending_flips(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003430 drm_vblank_off(dev, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003431
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07003432 if (dev_priv->fbc.plane == plane)
Chris Wilson973d04f2011-07-08 12:22:37 +01003433 intel_disable_fbc(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003434
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003435 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003436 intel_disable_planes(crtc);
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003437 intel_disable_plane(dev_priv, plane, pipe);
3438
Daniel Vetterd925c592013-06-05 13:34:04 +02003439 if (intel_crtc->config.has_pch_encoder)
3440 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
3441
Jesse Barnesb24e7172011-01-04 15:09:30 -08003442 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003443
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003444 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003445
Daniel Vetterbf49ec82012-09-06 22:15:40 +02003446 for_each_encoder_on_crtc(dev, crtc, encoder)
3447 if (encoder->post_disable)
3448 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003449
Daniel Vetterd925c592013-06-05 13:34:04 +02003450 if (intel_crtc->config.has_pch_encoder) {
3451 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003452
Daniel Vetterd925c592013-06-05 13:34:04 +02003453 ironlake_disable_pch_transcoder(dev_priv, pipe);
3454 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003455
Daniel Vetterd925c592013-06-05 13:34:04 +02003456 if (HAS_PCH_CPT(dev)) {
3457 /* disable TRANS_DP_CTL */
3458 reg = TRANS_DP_CTL(pipe);
3459 temp = I915_READ(reg);
3460 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
3461 TRANS_DP_PORT_SEL_MASK);
3462 temp |= TRANS_DP_PORT_SEL_NONE;
3463 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003464
Daniel Vetterd925c592013-06-05 13:34:04 +02003465 /* disable DPLL_SEL */
3466 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003467 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02003468 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003469 }
Daniel Vetterd925c592013-06-05 13:34:04 +02003470
3471 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003472 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02003473
3474 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003475 }
3476
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003477 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003478 intel_update_watermarks(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003479
3480 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01003481 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003482 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003483}
3484
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003485static void haswell_crtc_disable(struct drm_crtc *crtc)
3486{
3487 struct drm_device *dev = crtc->dev;
3488 struct drm_i915_private *dev_priv = dev->dev_private;
3489 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3490 struct intel_encoder *encoder;
3491 int pipe = intel_crtc->pipe;
3492 int plane = intel_crtc->plane;
Daniel Vetter3b117c82013-04-17 20:15:07 +02003493 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003494
3495 if (!intel_crtc->active)
3496 return;
3497
3498 for_each_encoder_on_crtc(dev, crtc, encoder)
3499 encoder->disable(encoder);
3500
3501 intel_crtc_wait_for_pending_flips(crtc);
3502 drm_vblank_off(dev, pipe);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003503
Rodrigo Vivi891348b2013-05-06 19:37:36 -03003504 /* FBC must be disabled before disabling the plane on HSW. */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07003505 if (dev_priv->fbc.plane == plane)
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003506 intel_disable_fbc(dev);
3507
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003508 hsw_disable_ips(intel_crtc);
3509
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003510 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003511 intel_disable_planes(crtc);
Rodrigo Vivi891348b2013-05-06 19:37:36 -03003512 intel_disable_plane(dev_priv, plane, pipe);
3513
Paulo Zanoni86642812013-04-12 17:57:57 -03003514 if (intel_crtc->config.has_pch_encoder)
3515 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003516 intel_disable_pipe(dev_priv, pipe);
3517
Paulo Zanoniad80a812012-10-24 16:06:19 -02003518 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003519
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003520 ironlake_pfit_disable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003521
Paulo Zanoni1f544382012-10-24 11:32:00 -02003522 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003523
3524 for_each_encoder_on_crtc(dev, crtc, encoder)
3525 if (encoder->post_disable)
3526 encoder->post_disable(encoder);
3527
Daniel Vetter88adfff2013-03-28 10:42:01 +01003528 if (intel_crtc->config.has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02003529 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni86642812013-04-12 17:57:57 -03003530 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02003531 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02003532 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003533
3534 intel_crtc->active = false;
3535 intel_update_watermarks(dev);
3536
3537 mutex_lock(&dev->struct_mutex);
3538 intel_update_fbc(dev);
3539 mutex_unlock(&dev->struct_mutex);
3540}
3541
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003542static void ironlake_crtc_off(struct drm_crtc *crtc)
3543{
3544 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003545 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003546}
3547
Paulo Zanoni6441ab52012-10-05 12:05:58 -03003548static void haswell_crtc_off(struct drm_crtc *crtc)
3549{
3550 intel_ddi_put_crtc_pll(crtc);
3551}
3552
Daniel Vetter02e792f2009-09-15 22:57:34 +02003553static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3554{
Daniel Vetter02e792f2009-09-15 22:57:34 +02003555 if (!enable && intel_crtc->overlay) {
Chris Wilson23f09ce2010-08-12 13:53:37 +01003556 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00003557 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02003558
Chris Wilson23f09ce2010-08-12 13:53:37 +01003559 mutex_lock(&dev->struct_mutex);
Chris Wilsonce453d82011-02-21 14:43:56 +00003560 dev_priv->mm.interruptible = false;
3561 (void) intel_overlay_switch_off(intel_crtc->overlay);
3562 dev_priv->mm.interruptible = true;
Chris Wilson23f09ce2010-08-12 13:53:37 +01003563 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02003564 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02003565
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01003566 /* Let userspace switch the overlay on again. In most cases userspace
3567 * has to recompute where to put it anyway.
3568 */
Daniel Vetter02e792f2009-09-15 22:57:34 +02003569}
3570
Egbert Eich61bc95c2013-03-04 09:24:38 -05003571/**
3572 * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
3573 * cursor plane briefly if not already running after enabling the display
3574 * plane.
3575 * This workaround avoids occasional blank screens when self refresh is
3576 * enabled.
3577 */
3578static void
3579g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
3580{
3581 u32 cntl = I915_READ(CURCNTR(pipe));
3582
3583 if ((cntl & CURSOR_MODE) == 0) {
3584 u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
3585
3586 I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
3587 I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
3588 intel_wait_for_vblank(dev_priv->dev, pipe);
3589 I915_WRITE(CURCNTR(pipe), cntl);
3590 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3591 I915_WRITE(FW_BLC_SELF, fw_bcl_self);
3592 }
3593}
3594
Jesse Barnes2dd24552013-04-25 12:55:01 -07003595static void i9xx_pfit_enable(struct intel_crtc *crtc)
3596{
3597 struct drm_device *dev = crtc->base.dev;
3598 struct drm_i915_private *dev_priv = dev->dev_private;
3599 struct intel_crtc_config *pipe_config = &crtc->config;
3600
Daniel Vetter328d8e82013-05-08 10:36:31 +02003601 if (!crtc->config.gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07003602 return;
3603
Daniel Vetterc0b03412013-05-28 12:05:54 +02003604 /*
3605 * The panel fitter should only be adjusted whilst the pipe is disabled,
3606 * according to register description and PRM.
3607 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07003608 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
3609 assert_pipe_disabled(dev_priv, crtc->pipe);
3610
Jesse Barnesb074cec2013-04-25 12:55:02 -07003611 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
3612 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02003613
3614 /* Border color in case we don't scale up to the full screen. Black by
3615 * default, change to something else for debugging. */
3616 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07003617}
3618
Jesse Barnes89b667f2013-04-18 14:51:36 -07003619static void valleyview_crtc_enable(struct drm_crtc *crtc)
3620{
3621 struct drm_device *dev = crtc->dev;
3622 struct drm_i915_private *dev_priv = dev->dev_private;
3623 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3624 struct intel_encoder *encoder;
3625 int pipe = intel_crtc->pipe;
3626 int plane = intel_crtc->plane;
3627
3628 WARN_ON(!crtc->enabled);
3629
3630 if (intel_crtc->active)
3631 return;
3632
3633 intel_crtc->active = true;
3634 intel_update_watermarks(dev);
3635
3636 mutex_lock(&dev_priv->dpio_lock);
3637
3638 for_each_encoder_on_crtc(dev, crtc, encoder)
3639 if (encoder->pre_pll_enable)
3640 encoder->pre_pll_enable(encoder);
3641
Daniel Vetter87442f72013-06-06 00:52:17 +02003642 vlv_enable_pll(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07003643
3644 for_each_encoder_on_crtc(dev, crtc, encoder)
3645 if (encoder->pre_enable)
3646 encoder->pre_enable(encoder);
3647
3648 /* VLV wants encoder enabling _before_ the pipe is up. */
3649 for_each_encoder_on_crtc(dev, crtc, encoder)
3650 encoder->enable(encoder);
3651
Jesse Barnes2dd24552013-04-25 12:55:01 -07003652 i9xx_pfit_enable(intel_crtc);
3653
Ville Syrjälä63cbb072013-06-04 13:48:59 +03003654 intel_crtc_load_lut(crtc);
3655
Jesse Barnes89b667f2013-04-18 14:51:36 -07003656 intel_enable_pipe(dev_priv, pipe, false);
3657 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003658 intel_enable_planes(crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07003659 intel_crtc_update_cursor(crtc, true);
3660
Ville Syrjäläf440eb12013-06-04 13:49:01 +03003661 intel_update_fbc(dev);
3662
Jesse Barnes89b667f2013-04-18 14:51:36 -07003663 mutex_unlock(&dev_priv->dpio_lock);
3664}
3665
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003666static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003667{
3668 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08003669 struct drm_i915_private *dev_priv = dev->dev_private;
3670 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003671 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08003672 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07003673 int plane = intel_crtc->plane;
Jesse Barnes79e53942008-11-07 14:24:08 -08003674
Daniel Vetter08a48462012-07-02 11:43:47 +02003675 WARN_ON(!crtc->enabled);
3676
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003677 if (intel_crtc->active)
3678 return;
3679
3680 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01003681 intel_update_watermarks(dev);
3682
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02003683 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02003684 if (encoder->pre_enable)
3685 encoder->pre_enable(encoder);
3686
Daniel Vetterf6736a12013-06-05 13:34:30 +02003687 i9xx_enable_pll(intel_crtc);
3688
Jesse Barnes2dd24552013-04-25 12:55:01 -07003689 i9xx_pfit_enable(intel_crtc);
3690
Ville Syrjälä63cbb072013-06-04 13:48:59 +03003691 intel_crtc_load_lut(crtc);
3692
Jesse Barnes040484a2011-01-03 12:14:26 -08003693 intel_enable_pipe(dev_priv, pipe, false);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003694 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003695 intel_enable_planes(crtc);
Ville Syrjälä22e407d2013-06-07 18:52:24 +03003696 /* The fixup needs to happen before cursor is enabled */
Egbert Eich61bc95c2013-03-04 09:24:38 -05003697 if (IS_G4X(dev))
3698 g4x_fixup_plane(dev_priv, pipe);
Ville Syrjälä22e407d2013-06-07 18:52:24 +03003699 intel_crtc_update_cursor(crtc, true);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003700
3701 /* Give the overlay scaler a chance to enable if it's on this pipe */
3702 intel_crtc_dpms_overlay(intel_crtc, true);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003703
Ville Syrjäläf440eb12013-06-04 13:49:01 +03003704 intel_update_fbc(dev);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003705
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003706 for_each_encoder_on_crtc(dev, crtc, encoder)
3707 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003708}
3709
Daniel Vetter87476d62013-04-11 16:29:06 +02003710static void i9xx_pfit_disable(struct intel_crtc *crtc)
3711{
3712 struct drm_device *dev = crtc->base.dev;
3713 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02003714
3715 if (!crtc->config.gmch_pfit.control)
3716 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02003717
3718 assert_pipe_disabled(dev_priv, crtc->pipe);
3719
Daniel Vetter328d8e82013-05-08 10:36:31 +02003720 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
3721 I915_READ(PFIT_CONTROL));
3722 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02003723}
3724
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003725static void i9xx_crtc_disable(struct drm_crtc *crtc)
3726{
3727 struct drm_device *dev = crtc->dev;
3728 struct drm_i915_private *dev_priv = dev->dev_private;
3729 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003730 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003731 int pipe = intel_crtc->pipe;
3732 int plane = intel_crtc->plane;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003733
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003734 if (!intel_crtc->active)
3735 return;
3736
Daniel Vetterea9d7582012-07-10 10:42:52 +02003737 for_each_encoder_on_crtc(dev, crtc, encoder)
3738 encoder->disable(encoder);
3739
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003740 /* Give the overlay scaler a chance to disable if it's on this pipe */
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003741 intel_crtc_wait_for_pending_flips(crtc);
3742 drm_vblank_off(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003743
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07003744 if (dev_priv->fbc.plane == plane)
Chris Wilson973d04f2011-07-08 12:22:37 +01003745 intel_disable_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003746
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003747 intel_crtc_dpms_overlay(intel_crtc, false);
3748 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003749 intel_disable_planes(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003750 intel_disable_plane(dev_priv, plane, pipe);
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003751
Jesse Barnesb24e7172011-01-04 15:09:30 -08003752 intel_disable_pipe(dev_priv, pipe);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02003753
Daniel Vetter87476d62013-04-11 16:29:06 +02003754 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02003755
Jesse Barnes89b667f2013-04-18 14:51:36 -07003756 for_each_encoder_on_crtc(dev, crtc, encoder)
3757 if (encoder->post_disable)
3758 encoder->post_disable(encoder);
3759
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08003760 intel_disable_pll(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003761
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003762 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003763 intel_update_fbc(dev);
3764 intel_update_watermarks(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003765}
3766
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003767static void i9xx_crtc_off(struct drm_crtc *crtc)
3768{
3769}
3770
Daniel Vetter976f8a22012-07-08 22:34:21 +02003771static void intel_crtc_update_sarea(struct drm_crtc *crtc,
3772 bool enabled)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003773{
3774 struct drm_device *dev = crtc->dev;
3775 struct drm_i915_master_private *master_priv;
3776 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3777 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08003778
3779 if (!dev->primary->master)
3780 return;
3781
3782 master_priv = dev->primary->master->driver_priv;
3783 if (!master_priv->sarea_priv)
3784 return;
3785
Jesse Barnes79e53942008-11-07 14:24:08 -08003786 switch (pipe) {
3787 case 0:
3788 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
3789 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
3790 break;
3791 case 1:
3792 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
3793 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
3794 break;
3795 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003796 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08003797 break;
3798 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003799}
3800
Daniel Vetter976f8a22012-07-08 22:34:21 +02003801/**
3802 * Sets the power management mode of the pipe and plane.
3803 */
3804void intel_crtc_update_dpms(struct drm_crtc *crtc)
Chris Wilsoncdd59982010-09-08 16:30:16 +01003805{
Chris Wilsoncdd59982010-09-08 16:30:16 +01003806 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003807 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter976f8a22012-07-08 22:34:21 +02003808 struct intel_encoder *intel_encoder;
3809 bool enable = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01003810
Daniel Vetter976f8a22012-07-08 22:34:21 +02003811 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
3812 enable |= intel_encoder->connectors_active;
3813
3814 if (enable)
3815 dev_priv->display.crtc_enable(crtc);
3816 else
3817 dev_priv->display.crtc_disable(crtc);
3818
3819 intel_crtc_update_sarea(crtc, enable);
3820}
3821
Daniel Vetter976f8a22012-07-08 22:34:21 +02003822static void intel_crtc_disable(struct drm_crtc *crtc)
3823{
3824 struct drm_device *dev = crtc->dev;
3825 struct drm_connector *connector;
3826 struct drm_i915_private *dev_priv = dev->dev_private;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08003827 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter976f8a22012-07-08 22:34:21 +02003828
3829 /* crtc should still be enabled when we disable it. */
3830 WARN_ON(!crtc->enabled);
3831
3832 dev_priv->display.crtc_disable(crtc);
Paulo Zanonic77bf562013-05-03 12:15:40 -03003833 intel_crtc->eld_vld = false;
Daniel Vetter976f8a22012-07-08 22:34:21 +02003834 intel_crtc_update_sarea(crtc, false);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003835 dev_priv->display.off(crtc);
3836
Chris Wilson931872f2012-01-16 23:01:13 +00003837 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
3838 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003839
3840 if (crtc->fb) {
3841 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01003842 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003843 mutex_unlock(&dev->struct_mutex);
Daniel Vetter976f8a22012-07-08 22:34:21 +02003844 crtc->fb = NULL;
3845 }
3846
3847 /* Update computed state. */
3848 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3849 if (!connector->encoder || !connector->encoder->crtc)
3850 continue;
3851
3852 if (connector->encoder->crtc != crtc)
3853 continue;
3854
3855 connector->dpms = DRM_MODE_DPMS_OFF;
3856 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01003857 }
3858}
3859
Daniel Vettera261b242012-07-26 19:21:47 +02003860void intel_modeset_disable(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08003861{
Daniel Vettera261b242012-07-26 19:21:47 +02003862 struct drm_crtc *crtc;
3863
3864 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3865 if (crtc->enabled)
3866 intel_crtc_disable(crtc);
3867 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003868}
3869
Chris Wilsonea5b2132010-08-04 13:50:23 +01003870void intel_encoder_destroy(struct drm_encoder *encoder)
3871{
Chris Wilson4ef69c72010-09-09 15:14:28 +01003872 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01003873
Chris Wilsonea5b2132010-08-04 13:50:23 +01003874 drm_encoder_cleanup(encoder);
3875 kfree(intel_encoder);
3876}
3877
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003878/* Simple dpms helper for encodres with just one connector, no cloning and only
3879 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
3880 * state of the entire output pipe. */
3881void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
3882{
3883 if (mode == DRM_MODE_DPMS_ON) {
3884 encoder->connectors_active = true;
3885
Daniel Vetterb2cabb02012-07-01 22:42:24 +02003886 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003887 } else {
3888 encoder->connectors_active = false;
3889
Daniel Vetterb2cabb02012-07-01 22:42:24 +02003890 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003891 }
3892}
3893
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003894/* Cross check the actual hw state with our own modeset state tracking (and it's
3895 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02003896static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003897{
3898 if (connector->get_hw_state(connector)) {
3899 struct intel_encoder *encoder = connector->encoder;
3900 struct drm_crtc *crtc;
3901 bool encoder_enabled;
3902 enum pipe pipe;
3903
3904 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
3905 connector->base.base.id,
3906 drm_get_connector_name(&connector->base));
3907
3908 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
3909 "wrong connector dpms state\n");
3910 WARN(connector->base.encoder != &encoder->base,
3911 "active connector not linked to encoder\n");
3912 WARN(!encoder->connectors_active,
3913 "encoder->connectors_active not set\n");
3914
3915 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
3916 WARN(!encoder_enabled, "encoder not enabled\n");
3917 if (WARN_ON(!encoder->base.crtc))
3918 return;
3919
3920 crtc = encoder->base.crtc;
3921
3922 WARN(!crtc->enabled, "crtc not enabled\n");
3923 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
3924 WARN(pipe != to_intel_crtc(crtc)->pipe,
3925 "encoder active on the wrong pipe\n");
3926 }
3927}
3928
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003929/* Even simpler default implementation, if there's really no special case to
3930 * consider. */
3931void intel_connector_dpms(struct drm_connector *connector, int mode)
3932{
3933 struct intel_encoder *encoder = intel_attached_encoder(connector);
3934
3935 /* All the simple cases only support two dpms states. */
3936 if (mode != DRM_MODE_DPMS_ON)
3937 mode = DRM_MODE_DPMS_OFF;
3938
3939 if (mode == connector->dpms)
3940 return;
3941
3942 connector->dpms = mode;
3943
3944 /* Only need to change hw state when actually enabled */
3945 if (encoder->base.crtc)
3946 intel_encoder_dpms(encoder, mode);
3947 else
Daniel Vetter8af6cf82012-07-10 09:50:11 +02003948 WARN_ON(encoder->connectors_active != false);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003949
Daniel Vetterb9805142012-08-31 17:37:33 +02003950 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003951}
3952
Daniel Vetterf0947c32012-07-02 13:10:34 +02003953/* Simple connector->get_hw_state implementation for encoders that support only
3954 * one connector and no cloning and hence the encoder state determines the state
3955 * of the connector. */
3956bool intel_connector_get_hw_state(struct intel_connector *connector)
3957{
Daniel Vetter24929352012-07-02 20:28:59 +02003958 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02003959 struct intel_encoder *encoder = connector->encoder;
3960
3961 return encoder->get_hw_state(encoder, &pipe);
3962}
3963
Daniel Vetter1857e1d2013-04-29 19:34:16 +02003964static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
3965 struct intel_crtc_config *pipe_config)
3966{
3967 struct drm_i915_private *dev_priv = dev->dev_private;
3968 struct intel_crtc *pipe_B_crtc =
3969 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
3970
3971 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
3972 pipe_name(pipe), pipe_config->fdi_lanes);
3973 if (pipe_config->fdi_lanes > 4) {
3974 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
3975 pipe_name(pipe), pipe_config->fdi_lanes);
3976 return false;
3977 }
3978
3979 if (IS_HASWELL(dev)) {
3980 if (pipe_config->fdi_lanes > 2) {
3981 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
3982 pipe_config->fdi_lanes);
3983 return false;
3984 } else {
3985 return true;
3986 }
3987 }
3988
3989 if (INTEL_INFO(dev)->num_pipes == 2)
3990 return true;
3991
3992 /* Ivybridge 3 pipe is really complicated */
3993 switch (pipe) {
3994 case PIPE_A:
3995 return true;
3996 case PIPE_B:
3997 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
3998 pipe_config->fdi_lanes > 2) {
3999 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4000 pipe_name(pipe), pipe_config->fdi_lanes);
4001 return false;
4002 }
4003 return true;
4004 case PIPE_C:
Daniel Vetter1e833f42013-02-19 22:31:57 +01004005 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004006 pipe_B_crtc->config.fdi_lanes <= 2) {
4007 if (pipe_config->fdi_lanes > 2) {
4008 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4009 pipe_name(pipe), pipe_config->fdi_lanes);
4010 return false;
4011 }
4012 } else {
4013 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
4014 return false;
4015 }
4016 return true;
4017 default:
4018 BUG();
4019 }
4020}
4021
Daniel Vettere29c22c2013-02-21 00:00:16 +01004022#define RETRY 1
4023static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
4024 struct intel_crtc_config *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02004025{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004026 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004027 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Daniel Vetterff9a6752013-06-01 17:16:21 +02004028 int lane, link_bw, fdi_dotclock;
Daniel Vettere29c22c2013-02-21 00:00:16 +01004029 bool setup_ok, needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004030
Daniel Vettere29c22c2013-02-21 00:00:16 +01004031retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02004032 /* FDI is a binary signal running at ~2.7GHz, encoding
4033 * each output octet as 10 bits. The actual frequency
4034 * is stored as a divider into a 100MHz clock, and the
4035 * mode pixel clock is stored in units of 1KHz.
4036 * Hence the bw of each lane in terms of the mode signal
4037 * is:
4038 */
4039 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
4040
Daniel Vetterff9a6752013-06-01 17:16:21 +02004041 fdi_dotclock = adjusted_mode->clock;
Daniel Vetteref1b4602013-06-01 17:17:04 +02004042 fdi_dotclock /= pipe_config->pixel_multiplier;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004043
Daniel Vetter2bd89a02013-06-01 17:16:19 +02004044 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02004045 pipe_config->pipe_bpp);
4046
4047 pipe_config->fdi_lanes = lane;
4048
Daniel Vetter2bd89a02013-06-01 17:16:19 +02004049 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02004050 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004051
Daniel Vettere29c22c2013-02-21 00:00:16 +01004052 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
4053 intel_crtc->pipe, pipe_config);
4054 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
4055 pipe_config->pipe_bpp -= 2*3;
4056 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
4057 pipe_config->pipe_bpp);
4058 needs_recompute = true;
4059 pipe_config->bw_constrained = true;
4060
4061 goto retry;
4062 }
4063
4064 if (needs_recompute)
4065 return RETRY;
4066
4067 return setup_ok ? 0 : -EINVAL;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004068}
4069
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004070static void hsw_compute_ips_config(struct intel_crtc *crtc,
4071 struct intel_crtc_config *pipe_config)
4072{
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03004073 pipe_config->ips_enabled = i915_enable_ips &&
4074 hsw_crtc_supports_ips(crtc) &&
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004075 pipe_config->pipe_bpp == 24;
4076}
4077
Daniel Vettera43f6e02013-06-07 23:10:32 +02004078static int intel_crtc_compute_config(struct intel_crtc *crtc,
Daniel Vettere29c22c2013-02-21 00:00:16 +01004079 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08004080{
Daniel Vettera43f6e02013-06-07 23:10:32 +02004081 struct drm_device *dev = crtc->base.dev;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01004082 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01004083
Eric Anholtbad720f2009-10-22 16:11:14 -07004084 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08004085 /* FDI link clock is fixed at 2.7G */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01004086 if (pipe_config->requested_mode.clock * 3
4087 > IRONLAKE_FDI_FREQ * 4)
Daniel Vettere29c22c2013-02-21 00:00:16 +01004088 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004089 }
Chris Wilson89749352010-09-12 18:25:19 +01004090
Daniel Vetterf9bef082012-04-15 19:53:19 +02004091 /* All interlaced capable intel hw wants timings in frames. Note though
4092 * that intel_lvds_mode_fixup does some funny tricks with the crtc
4093 * timings, so we need to be careful not to clobber these.*/
Daniel Vetter7ae89232013-03-27 00:44:52 +01004094 if (!pipe_config->timings_set)
Daniel Vetterf9bef082012-04-15 19:53:19 +02004095 drm_mode_set_crtcinfo(adjusted_mode, 0);
Chris Wilson89749352010-09-12 18:25:19 +01004096
Damien Lespiau8693a822013-05-03 18:48:11 +01004097 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
4098 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03004099 */
4100 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
4101 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01004102 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03004103
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004104 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004105 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004106 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004107 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
4108 * for lvds. */
4109 pipe_config->pipe_bpp = 8*3;
4110 }
4111
Damien Lespiauf5adf942013-06-24 18:29:34 +01004112 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02004113 hsw_compute_ips_config(crtc, pipe_config);
4114
4115 /* XXX: PCH clock sharing is done in ->mode_set, so make sure the old
4116 * clock survives for now. */
4117 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
4118 pipe_config->shared_dpll = crtc->config.shared_dpll;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004119
Daniel Vetter877d48d2013-04-19 11:24:43 +02004120 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02004121 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02004122
Daniel Vettere29c22c2013-02-21 00:00:16 +01004123 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08004124}
4125
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07004126static int valleyview_get_display_clock_speed(struct drm_device *dev)
4127{
4128 return 400000; /* FIXME */
4129}
4130
Jesse Barnese70236a2009-09-21 10:42:27 -07004131static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08004132{
Jesse Barnese70236a2009-09-21 10:42:27 -07004133 return 400000;
4134}
Jesse Barnes79e53942008-11-07 14:24:08 -08004135
Jesse Barnese70236a2009-09-21 10:42:27 -07004136static int i915_get_display_clock_speed(struct drm_device *dev)
4137{
4138 return 333000;
4139}
Jesse Barnes79e53942008-11-07 14:24:08 -08004140
Jesse Barnese70236a2009-09-21 10:42:27 -07004141static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
4142{
4143 return 200000;
4144}
Jesse Barnes79e53942008-11-07 14:24:08 -08004145
Jesse Barnese70236a2009-09-21 10:42:27 -07004146static int i915gm_get_display_clock_speed(struct drm_device *dev)
4147{
4148 u16 gcfgc = 0;
4149
4150 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4151
4152 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08004153 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07004154 else {
4155 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4156 case GC_DISPLAY_CLOCK_333_MHZ:
4157 return 333000;
4158 default:
4159 case GC_DISPLAY_CLOCK_190_200_MHZ:
4160 return 190000;
4161 }
4162 }
4163}
Jesse Barnes79e53942008-11-07 14:24:08 -08004164
Jesse Barnese70236a2009-09-21 10:42:27 -07004165static int i865_get_display_clock_speed(struct drm_device *dev)
4166{
4167 return 266000;
4168}
4169
4170static int i855_get_display_clock_speed(struct drm_device *dev)
4171{
4172 u16 hpllcc = 0;
4173 /* Assume that the hardware is in the high speed state. This
4174 * should be the default.
4175 */
4176 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
4177 case GC_CLOCK_133_200:
4178 case GC_CLOCK_100_200:
4179 return 200000;
4180 case GC_CLOCK_166_250:
4181 return 250000;
4182 case GC_CLOCK_100_133:
4183 return 133000;
4184 }
4185
4186 /* Shouldn't happen */
4187 return 0;
4188}
4189
4190static int i830_get_display_clock_speed(struct drm_device *dev)
4191{
4192 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08004193}
4194
Zhenyu Wang2c072452009-06-05 15:38:42 +08004195static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004196intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004197{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004198 while (*num > DATA_LINK_M_N_MASK ||
4199 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08004200 *num >>= 1;
4201 *den >>= 1;
4202 }
4203}
4204
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004205static void compute_m_n(unsigned int m, unsigned int n,
4206 uint32_t *ret_m, uint32_t *ret_n)
4207{
4208 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
4209 *ret_m = div_u64((uint64_t) m * *ret_n, n);
4210 intel_reduce_m_n_ratio(ret_m, ret_n);
4211}
4212
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004213void
4214intel_link_compute_m_n(int bits_per_pixel, int nlanes,
4215 int pixel_clock, int link_clock,
4216 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004217{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004218 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004219
4220 compute_m_n(bits_per_pixel * pixel_clock,
4221 link_clock * nlanes * 8,
4222 &m_n->gmch_m, &m_n->gmch_n);
4223
4224 compute_m_n(pixel_clock, link_clock,
4225 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004226}
4227
Chris Wilsona7615032011-01-12 17:04:08 +00004228static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
4229{
Keith Packard72bbe582011-09-26 16:09:45 -07004230 if (i915_panel_use_ssc >= 0)
4231 return i915_panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004232 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07004233 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00004234}
4235
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004236static int vlv_get_refclk(struct drm_crtc *crtc)
4237{
4238 struct drm_device *dev = crtc->dev;
4239 struct drm_i915_private *dev_priv = dev->dev_private;
4240 int refclk = 27000; /* for DP & HDMI */
4241
4242 return 100000; /* only one validated so far */
4243
4244 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
4245 refclk = 96000;
4246 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
4247 if (intel_panel_use_ssc(dev_priv))
4248 refclk = 100000;
4249 else
4250 refclk = 96000;
4251 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
4252 refclk = 100000;
4253 }
4254
4255 return refclk;
4256}
4257
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004258static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
4259{
4260 struct drm_device *dev = crtc->dev;
4261 struct drm_i915_private *dev_priv = dev->dev_private;
4262 int refclk;
4263
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004264 if (IS_VALLEYVIEW(dev)) {
4265 refclk = vlv_get_refclk(crtc);
4266 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004267 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004268 refclk = dev_priv->vbt.lvds_ssc_freq * 1000;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004269 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
4270 refclk / 1000);
4271 } else if (!IS_GEN2(dev)) {
4272 refclk = 96000;
4273 } else {
4274 refclk = 48000;
4275 }
4276
4277 return refclk;
4278}
4279
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004280static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004281{
Daniel Vetter7df00d72013-05-21 21:54:55 +02004282 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004283}
Daniel Vetterf47709a2013-03-28 10:42:02 +01004284
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004285static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
4286{
4287 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004288}
4289
Daniel Vetterf47709a2013-03-28 10:42:02 +01004290static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Jesse Barnesa7516a02011-12-15 12:30:37 -08004291 intel_clock_t *reduced_clock)
4292{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004293 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004294 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004295 int pipe = crtc->pipe;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004296 u32 fp, fp2 = 0;
4297
4298 if (IS_PINEVIEW(dev)) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004299 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004300 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004301 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004302 } else {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004303 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004304 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004305 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004306 }
4307
4308 I915_WRITE(FP0(pipe), fp);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004309 crtc->config.dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004310
Daniel Vetterf47709a2013-03-28 10:42:02 +01004311 crtc->lowfreq_avail = false;
4312 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Jesse Barnesa7516a02011-12-15 12:30:37 -08004313 reduced_clock && i915_powersave) {
4314 I915_WRITE(FP1(pipe), fp2);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004315 crtc->config.dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004316 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004317 } else {
4318 I915_WRITE(FP1(pipe), fp);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004319 crtc->config.dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004320 }
4321}
4322
Jesse Barnes89b667f2013-04-18 14:51:36 -07004323static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv)
4324{
4325 u32 reg_val;
4326
4327 /*
4328 * PLLB opamp always calibrates to max value of 0x3f, force enable it
4329 * and set it to a reasonable value instead.
4330 */
Jani Nikulaae992582013-05-22 15:36:19 +03004331 reg_val = vlv_dpio_read(dev_priv, DPIO_IREF(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004332 reg_val &= 0xffffff00;
4333 reg_val |= 0x00000030;
Jani Nikulaae992582013-05-22 15:36:19 +03004334 vlv_dpio_write(dev_priv, DPIO_IREF(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004335
Jani Nikulaae992582013-05-22 15:36:19 +03004336 reg_val = vlv_dpio_read(dev_priv, DPIO_CALIBRATION);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004337 reg_val &= 0x8cffffff;
4338 reg_val = 0x8c000000;
Jani Nikulaae992582013-05-22 15:36:19 +03004339 vlv_dpio_write(dev_priv, DPIO_CALIBRATION, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004340
Jani Nikulaae992582013-05-22 15:36:19 +03004341 reg_val = vlv_dpio_read(dev_priv, DPIO_IREF(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004342 reg_val &= 0xffffff00;
Jani Nikulaae992582013-05-22 15:36:19 +03004343 vlv_dpio_write(dev_priv, DPIO_IREF(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004344
Jani Nikulaae992582013-05-22 15:36:19 +03004345 reg_val = vlv_dpio_read(dev_priv, DPIO_CALIBRATION);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004346 reg_val &= 0x00ffffff;
4347 reg_val |= 0xb0000000;
Jani Nikulaae992582013-05-22 15:36:19 +03004348 vlv_dpio_write(dev_priv, DPIO_CALIBRATION, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004349}
4350
Daniel Vetterb5518422013-05-03 11:49:48 +02004351static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
4352 struct intel_link_m_n *m_n)
4353{
4354 struct drm_device *dev = crtc->base.dev;
4355 struct drm_i915_private *dev_priv = dev->dev_private;
4356 int pipe = crtc->pipe;
4357
Daniel Vettere3b95f12013-05-03 11:49:49 +02004358 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4359 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
4360 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
4361 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02004362}
4363
4364static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
4365 struct intel_link_m_n *m_n)
4366{
4367 struct drm_device *dev = crtc->base.dev;
4368 struct drm_i915_private *dev_priv = dev->dev_private;
4369 int pipe = crtc->pipe;
4370 enum transcoder transcoder = crtc->config.cpu_transcoder;
4371
4372 if (INTEL_INFO(dev)->gen >= 5) {
4373 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
4374 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
4375 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
4376 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
4377 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02004378 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4379 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
4380 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
4381 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02004382 }
4383}
4384
Daniel Vetter03afc4a2013-04-02 23:42:31 +02004385static void intel_dp_set_m_n(struct intel_crtc *crtc)
4386{
4387 if (crtc->config.has_pch_encoder)
4388 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4389 else
4390 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4391}
4392
Daniel Vetterf47709a2013-03-28 10:42:02 +01004393static void vlv_update_pll(struct intel_crtc *crtc)
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004394{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004395 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004396 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004397 struct intel_encoder *encoder;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004398 int pipe = crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004399 u32 dpll, mdiv;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004400 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004401 bool is_hdmi;
Daniel Vetter198a037f2013-04-19 11:14:37 +02004402 u32 coreclk, reg_val, dpll_md;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004403
Daniel Vetter09153002012-12-12 14:06:44 +01004404 mutex_lock(&dev_priv->dpio_lock);
4405
Jesse Barnes89b667f2013-04-18 14:51:36 -07004406 is_hdmi = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004407
Daniel Vetterf47709a2013-03-28 10:42:02 +01004408 bestn = crtc->config.dpll.n;
4409 bestm1 = crtc->config.dpll.m1;
4410 bestm2 = crtc->config.dpll.m2;
4411 bestp1 = crtc->config.dpll.p1;
4412 bestp2 = crtc->config.dpll.p2;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004413
Jesse Barnes89b667f2013-04-18 14:51:36 -07004414 /* See eDP HDMI DPIO driver vbios notes doc */
4415
4416 /* PLL B needs special handling */
4417 if (pipe)
4418 vlv_pllb_recal_opamp(dev_priv);
4419
4420 /* Set up Tx target for periodic Rcomp update */
Jani Nikulaae992582013-05-22 15:36:19 +03004421 vlv_dpio_write(dev_priv, DPIO_IREF_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004422
4423 /* Disable target IRef on PLL */
Jani Nikulaae992582013-05-22 15:36:19 +03004424 reg_val = vlv_dpio_read(dev_priv, DPIO_IREF_CTL(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004425 reg_val &= 0x00ffffff;
Jani Nikulaae992582013-05-22 15:36:19 +03004426 vlv_dpio_write(dev_priv, DPIO_IREF_CTL(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004427
4428 /* Disable fast lock */
Jani Nikulaae992582013-05-22 15:36:19 +03004429 vlv_dpio_write(dev_priv, DPIO_FASTCLK_DISABLE, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004430
4431 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004432 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
4433 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
4434 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004435 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07004436
4437 /*
4438 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
4439 * but we don't support that).
4440 * Note: don't use the DAC post divider as it seems unstable.
4441 */
4442 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Jani Nikulaae992582013-05-22 15:36:19 +03004443 vlv_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004444
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004445 mdiv |= DPIO_ENABLE_CALIBRATION;
Jani Nikulaae992582013-05-22 15:36:19 +03004446 vlv_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004447
Jesse Barnes89b667f2013-04-18 14:51:36 -07004448 /* Set HBR and RBR LPF coefficients */
Daniel Vetterff9a6752013-06-01 17:16:21 +02004449 if (crtc->config.port_clock == 162000 ||
Ville Syrjälä99750bd2013-06-14 14:02:52 +03004450 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
Jesse Barnes89b667f2013-04-18 14:51:36 -07004451 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
Ville Syrjälä4abb2c32013-06-14 14:02:53 +03004452 vlv_dpio_write(dev_priv, DPIO_LPF_COEFF(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004453 0x005f0021);
4454 else
Ville Syrjälä4abb2c32013-06-14 14:02:53 +03004455 vlv_dpio_write(dev_priv, DPIO_LPF_COEFF(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004456 0x00d0000f);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004457
Jesse Barnes89b667f2013-04-18 14:51:36 -07004458 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
4459 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
4460 /* Use SSC source */
4461 if (!pipe)
Jani Nikulaae992582013-05-22 15:36:19 +03004462 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004463 0x0df40000);
4464 else
Jani Nikulaae992582013-05-22 15:36:19 +03004465 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004466 0x0df70000);
4467 } else { /* HDMI or VGA */
4468 /* Use bend source */
4469 if (!pipe)
Jani Nikulaae992582013-05-22 15:36:19 +03004470 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004471 0x0df70000);
4472 else
Jani Nikulaae992582013-05-22 15:36:19 +03004473 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004474 0x0df40000);
4475 }
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004476
Jani Nikulaae992582013-05-22 15:36:19 +03004477 coreclk = vlv_dpio_read(dev_priv, DPIO_CORE_CLK(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004478 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
4479 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
4480 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
4481 coreclk |= 0x01000000;
Jani Nikulaae992582013-05-22 15:36:19 +03004482 vlv_dpio_write(dev_priv, DPIO_CORE_CLK(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004483
Jani Nikulaae992582013-05-22 15:36:19 +03004484 vlv_dpio_write(dev_priv, DPIO_PLL_CML(pipe), 0x87871000);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004485
4486 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
4487 if (encoder->pre_pll_enable)
4488 encoder->pre_pll_enable(encoder);
4489
4490 /* Enable DPIO clock input */
4491 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
4492 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
4493 if (pipe)
4494 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004495
4496 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004497 crtc->config.dpll_hw_state.dpll = dpll;
4498
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004499 I915_WRITE(DPLL(pipe), dpll);
4500 POSTING_READ(DPLL(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004501 udelay(150);
4502
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004503 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
4504 DRM_ERROR("DPLL %d failed to lock\n", pipe);
4505
Daniel Vetteref1b4602013-06-01 17:17:04 +02004506 dpll_md = (crtc->config.pixel_multiplier - 1)
4507 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004508 crtc->config.dpll_hw_state.dpll_md = dpll_md;
4509
Daniel Vetter198a037f2013-04-19 11:14:37 +02004510 I915_WRITE(DPLL_MD(pipe), dpll_md);
4511 POSTING_READ(DPLL_MD(pipe));
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004512
Daniel Vetterf47709a2013-03-28 10:42:02 +01004513 if (crtc->config.has_dp_encoder)
4514 intel_dp_set_m_n(crtc);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304515
Daniel Vetter09153002012-12-12 14:06:44 +01004516 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004517}
4518
Daniel Vetterf47709a2013-03-28 10:42:02 +01004519static void i9xx_update_pll(struct intel_crtc *crtc,
4520 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004521 int num_connectors)
4522{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004523 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004524 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004525 u32 dpll;
4526 bool is_sdvo;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004527 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004528
Daniel Vetterf47709a2013-03-28 10:42:02 +01004529 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304530
Daniel Vetterf47709a2013-03-28 10:42:02 +01004531 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
4532 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004533
4534 dpll = DPLL_VGA_MODE_DIS;
4535
Daniel Vetterf47709a2013-03-28 10:42:02 +01004536 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004537 dpll |= DPLLB_MODE_LVDS;
4538 else
4539 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01004540
Daniel Vetteref1b4602013-06-01 17:17:04 +02004541 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Daniel Vetter198a037f2013-04-19 11:14:37 +02004542 dpll |= (crtc->config.pixel_multiplier - 1)
4543 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004544 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02004545
4546 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02004547 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02004548
Daniel Vetterf47709a2013-03-28 10:42:02 +01004549 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
Daniel Vetter4a33e482013-07-06 12:52:05 +02004550 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004551
4552 /* compute bitmask from p1 value */
4553 if (IS_PINEVIEW(dev))
4554 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
4555 else {
4556 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4557 if (IS_G4X(dev) && reduced_clock)
4558 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
4559 }
4560 switch (clock->p2) {
4561 case 5:
4562 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4563 break;
4564 case 7:
4565 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4566 break;
4567 case 10:
4568 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4569 break;
4570 case 14:
4571 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4572 break;
4573 }
4574 if (INTEL_INFO(dev)->gen >= 4)
4575 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
4576
Daniel Vetter09ede542013-04-30 14:01:45 +02004577 if (crtc->config.sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004578 dpll |= PLL_REF_INPUT_TVCLKINBC;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004579 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004580 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4581 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4582 else
4583 dpll |= PLL_REF_INPUT_DREFCLK;
4584
4585 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004586 crtc->config.dpll_hw_state.dpll = dpll;
4587
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004588 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetteref1b4602013-06-01 17:17:04 +02004589 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
4590 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004591 crtc->config.dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004592 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02004593
4594 if (crtc->config.has_dp_encoder)
4595 intel_dp_set_m_n(crtc);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004596}
4597
Daniel Vetterf47709a2013-03-28 10:42:02 +01004598static void i8xx_update_pll(struct intel_crtc *crtc,
Daniel Vetterf47709a2013-03-28 10:42:02 +01004599 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004600 int num_connectors)
4601{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004602 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004603 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004604 u32 dpll;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004605 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004606
Daniel Vetterf47709a2013-03-28 10:42:02 +01004607 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304608
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004609 dpll = DPLL_VGA_MODE_DIS;
4610
Daniel Vetterf47709a2013-03-28 10:42:02 +01004611 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004612 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4613 } else {
4614 if (clock->p1 == 2)
4615 dpll |= PLL_P1_DIVIDE_BY_TWO;
4616 else
4617 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4618 if (clock->p2 == 4)
4619 dpll |= PLL_P2_DIVIDE_BY_4;
4620 }
4621
Daniel Vetter4a33e482013-07-06 12:52:05 +02004622 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
4623 dpll |= DPLL_DVO_2X_MODE;
4624
Daniel Vetterf47709a2013-03-28 10:42:02 +01004625 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004626 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4627 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4628 else
4629 dpll |= PLL_REF_INPUT_DREFCLK;
4630
4631 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004632 crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004633}
4634
Daniel Vetter8a654f32013-06-01 17:16:22 +02004635static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004636{
4637 struct drm_device *dev = intel_crtc->base.dev;
4638 struct drm_i915_private *dev_priv = dev->dev_private;
4639 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02004640 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02004641 struct drm_display_mode *adjusted_mode =
4642 &intel_crtc->config.adjusted_mode;
4643 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004644 uint32_t vsyncshift, crtc_vtotal, crtc_vblank_end;
4645
4646 /* We need to be careful not to changed the adjusted mode, for otherwise
4647 * the hw state checker will get angry at the mismatch. */
4648 crtc_vtotal = adjusted_mode->crtc_vtotal;
4649 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004650
4651 if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4652 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004653 crtc_vtotal -= 1;
4654 crtc_vblank_end -= 1;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004655 vsyncshift = adjusted_mode->crtc_hsync_start
4656 - adjusted_mode->crtc_htotal / 2;
4657 } else {
4658 vsyncshift = 0;
4659 }
4660
4661 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004662 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004663
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004664 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004665 (adjusted_mode->crtc_hdisplay - 1) |
4666 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004667 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004668 (adjusted_mode->crtc_hblank_start - 1) |
4669 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004670 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004671 (adjusted_mode->crtc_hsync_start - 1) |
4672 ((adjusted_mode->crtc_hsync_end - 1) << 16));
4673
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004674 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004675 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004676 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004677 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004678 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004679 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004680 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004681 (adjusted_mode->crtc_vsync_start - 1) |
4682 ((adjusted_mode->crtc_vsync_end - 1) << 16));
4683
Paulo Zanonib5e508d2012-10-24 11:34:43 -02004684 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
4685 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
4686 * documented on the DDI_FUNC_CTL register description, EDP Input Select
4687 * bits. */
4688 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
4689 (pipe == PIPE_B || pipe == PIPE_C))
4690 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
4691
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004692 /* pipesrc controls the size that is scaled from, which should
4693 * always be the user's requested size.
4694 */
4695 I915_WRITE(PIPESRC(pipe),
4696 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
4697}
4698
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02004699static void intel_get_pipe_timings(struct intel_crtc *crtc,
4700 struct intel_crtc_config *pipe_config)
4701{
4702 struct drm_device *dev = crtc->base.dev;
4703 struct drm_i915_private *dev_priv = dev->dev_private;
4704 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
4705 uint32_t tmp;
4706
4707 tmp = I915_READ(HTOTAL(cpu_transcoder));
4708 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
4709 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
4710 tmp = I915_READ(HBLANK(cpu_transcoder));
4711 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
4712 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
4713 tmp = I915_READ(HSYNC(cpu_transcoder));
4714 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
4715 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
4716
4717 tmp = I915_READ(VTOTAL(cpu_transcoder));
4718 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
4719 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
4720 tmp = I915_READ(VBLANK(cpu_transcoder));
4721 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
4722 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
4723 tmp = I915_READ(VSYNC(cpu_transcoder));
4724 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
4725 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
4726
4727 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
4728 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
4729 pipe_config->adjusted_mode.crtc_vtotal += 1;
4730 pipe_config->adjusted_mode.crtc_vblank_end += 1;
4731 }
4732
4733 tmp = I915_READ(PIPESRC(crtc->pipe));
4734 pipe_config->requested_mode.vdisplay = (tmp & 0xffff) + 1;
4735 pipe_config->requested_mode.hdisplay = ((tmp >> 16) & 0xffff) + 1;
4736}
4737
Jesse Barnesbabea612013-06-26 18:57:38 +03004738static void intel_crtc_mode_from_pipe_config(struct intel_crtc *intel_crtc,
4739 struct intel_crtc_config *pipe_config)
4740{
4741 struct drm_crtc *crtc = &intel_crtc->base;
4742
4743 crtc->mode.hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
4744 crtc->mode.htotal = pipe_config->adjusted_mode.crtc_htotal;
4745 crtc->mode.hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
4746 crtc->mode.hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
4747
4748 crtc->mode.vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
4749 crtc->mode.vtotal = pipe_config->adjusted_mode.crtc_vtotal;
4750 crtc->mode.vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
4751 crtc->mode.vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
4752
4753 crtc->mode.flags = pipe_config->adjusted_mode.flags;
4754
4755 crtc->mode.clock = pipe_config->adjusted_mode.clock;
4756 crtc->mode.flags |= pipe_config->adjusted_mode.flags;
4757}
4758
Daniel Vetter84b046f2013-02-19 18:48:54 +01004759static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
4760{
4761 struct drm_device *dev = intel_crtc->base.dev;
4762 struct drm_i915_private *dev_priv = dev->dev_private;
4763 uint32_t pipeconf;
4764
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02004765 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01004766
4767 if (intel_crtc->pipe == 0 && INTEL_INFO(dev)->gen < 4) {
4768 /* Enable pixel doubling when the dot clock is > 90% of the (display)
4769 * core speed.
4770 *
4771 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
4772 * pipe == 0 check?
4773 */
4774 if (intel_crtc->config.requested_mode.clock >
4775 dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
4776 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01004777 }
4778
Daniel Vetterff9ce462013-04-24 14:57:17 +02004779 /* only g4x and later have fancy bpc/dither controls */
4780 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02004781 /* Bspec claims that we can't use dithering for 30bpp pipes. */
4782 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
4783 pipeconf |= PIPECONF_DITHER_EN |
4784 PIPECONF_DITHER_TYPE_SP;
4785
4786 switch (intel_crtc->config.pipe_bpp) {
4787 case 18:
4788 pipeconf |= PIPECONF_6BPC;
4789 break;
4790 case 24:
4791 pipeconf |= PIPECONF_8BPC;
4792 break;
4793 case 30:
4794 pipeconf |= PIPECONF_10BPC;
4795 break;
4796 default:
4797 /* Case prevented by intel_choose_pipe_bpp_dither. */
4798 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01004799 }
4800 }
4801
4802 if (HAS_PIPE_CXSR(dev)) {
4803 if (intel_crtc->lowfreq_avail) {
4804 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
4805 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
4806 } else {
4807 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01004808 }
4809 }
4810
Daniel Vetter84b046f2013-02-19 18:48:54 +01004811 if (!IS_GEN2(dev) &&
4812 intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
4813 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
4814 else
4815 pipeconf |= PIPECONF_PROGRESSIVE;
4816
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02004817 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
4818 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03004819
Daniel Vetter84b046f2013-02-19 18:48:54 +01004820 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
4821 POSTING_READ(PIPECONF(intel_crtc->pipe));
4822}
4823
Eric Anholtf564048e2011-03-30 13:01:02 -07004824static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07004825 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02004826 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08004827{
4828 struct drm_device *dev = crtc->dev;
4829 struct drm_i915_private *dev_priv = dev->dev_private;
4830 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01004831 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08004832 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07004833 int plane = intel_crtc->plane;
Eric Anholtc751ce42010-03-25 11:48:48 -07004834 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07004835 intel_clock_t clock, reduced_clock;
Daniel Vetter84b046f2013-02-19 18:48:54 +01004836 u32 dspcntr;
Daniel Vettera16af7212013-04-30 14:01:44 +02004837 bool ok, has_reduced_clock = false;
4838 bool is_lvds = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01004839 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08004840 const intel_limit_t *limit;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00004841 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08004842
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02004843 for_each_encoder_on_crtc(dev, crtc, encoder) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004844 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004845 case INTEL_OUTPUT_LVDS:
4846 is_lvds = true;
4847 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08004848 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05004849
Eric Anholtc751ce42010-03-25 11:48:48 -07004850 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08004851 }
4852
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004853 refclk = i9xx_get_refclk(crtc, num_connectors);
Jesse Barnes79e53942008-11-07 14:24:08 -08004854
Ma Lingd4906092009-03-18 20:13:27 +08004855 /*
4856 * Returns a set of divisors for the desired target clock with the given
4857 * refclk, or FALSE. The returned values represent the clock equation:
4858 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
4859 */
Chris Wilson1b894b52010-12-14 20:04:54 +00004860 limit = intel_limit(crtc, refclk);
Daniel Vetterff9a6752013-06-01 17:16:21 +02004861 ok = dev_priv->display.find_dpll(limit, crtc,
4862 intel_crtc->config.port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02004863 refclk, NULL, &clock);
4864 if (!ok && !intel_crtc->config.clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004865 DRM_ERROR("Couldn't find PLL settings for mode!\n");
Eric Anholtf564048e2011-03-30 13:01:02 -07004866 return -EINVAL;
4867 }
4868
4869 /* Ensure that the cursor is valid for the new mode before changing... */
4870 intel_crtc_update_cursor(crtc, true);
4871
4872 if (is_lvds && dev_priv->lvds_downclock_avail) {
Sean Paulcec2f352012-01-10 15:09:36 -08004873 /*
4874 * Ensure we match the reduced clock's P to the target clock.
4875 * If the clocks don't match, we can't switch the display clock
4876 * by using the FP0/FP1. In such case we will disable the LVDS
4877 * downclock feature.
4878 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02004879 has_reduced_clock =
4880 dev_priv->display.find_dpll(limit, crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07004881 dev_priv->lvds_downclock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02004882 refclk, &clock,
Eric Anholtf564048e2011-03-30 13:01:02 -07004883 &reduced_clock);
Eric Anholtf564048e2011-03-30 13:01:02 -07004884 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01004885 /* Compat-code for transition, will disappear. */
4886 if (!intel_crtc->config.clock_set) {
4887 intel_crtc->config.dpll.n = clock.n;
4888 intel_crtc->config.dpll.m1 = clock.m1;
4889 intel_crtc->config.dpll.m2 = clock.m2;
4890 intel_crtc->config.dpll.p1 = clock.p1;
4891 intel_crtc->config.dpll.p2 = clock.p2;
4892 }
Eric Anholtf564048e2011-03-30 13:01:02 -07004893
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004894 if (IS_GEN2(dev))
Daniel Vetter8a654f32013-06-01 17:16:22 +02004895 i8xx_update_pll(intel_crtc,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304896 has_reduced_clock ? &reduced_clock : NULL,
4897 num_connectors);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004898 else if (IS_VALLEYVIEW(dev))
Daniel Vetterf47709a2013-03-28 10:42:02 +01004899 vlv_update_pll(intel_crtc);
Eric Anholtf564048e2011-03-30 13:01:02 -07004900 else
Daniel Vetterf47709a2013-03-28 10:42:02 +01004901 i9xx_update_pll(intel_crtc,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004902 has_reduced_clock ? &reduced_clock : NULL,
Jesse Barnes89b667f2013-04-18 14:51:36 -07004903 num_connectors);
Eric Anholtf564048e2011-03-30 13:01:02 -07004904
Eric Anholtf564048e2011-03-30 13:01:02 -07004905 /* Set up the display plane register */
4906 dspcntr = DISPPLANE_GAMMA_ENABLE;
4907
Jesse Barnesda6ecc52013-03-08 10:46:00 -08004908 if (!IS_VALLEYVIEW(dev)) {
4909 if (pipe == 0)
4910 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
4911 else
4912 dspcntr |= DISPPLANE_SEL_PIPE_B;
4913 }
Eric Anholtf564048e2011-03-30 13:01:02 -07004914
Daniel Vetter8a654f32013-06-01 17:16:22 +02004915 intel_set_pipe_timings(intel_crtc);
Eric Anholtf564048e2011-03-30 13:01:02 -07004916
4917 /* pipesrc and dspsize control the size that is scaled from,
4918 * which should always be the user's requested size.
4919 */
Eric Anholt929c77f2011-03-30 13:01:04 -07004920 I915_WRITE(DSPSIZE(plane),
4921 ((mode->vdisplay - 1) << 16) |
4922 (mode->hdisplay - 1));
4923 I915_WRITE(DSPPOS(plane), 0);
Eric Anholtf564048e2011-03-30 13:01:02 -07004924
Daniel Vetter84b046f2013-02-19 18:48:54 +01004925 i9xx_set_pipeconf(intel_crtc);
4926
Eric Anholtf564048e2011-03-30 13:01:02 -07004927 I915_WRITE(DSPCNTR(plane), dspcntr);
4928 POSTING_READ(DSPCNTR(plane));
4929
Daniel Vetter94352cf2012-07-05 22:51:56 +02004930 ret = intel_pipe_set_base(crtc, x, y, fb);
Eric Anholtf564048e2011-03-30 13:01:02 -07004931
4932 intel_update_watermarks(dev);
4933
Eric Anholtf564048e2011-03-30 13:01:02 -07004934 return ret;
4935}
4936
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02004937static void i9xx_get_pfit_config(struct intel_crtc *crtc,
4938 struct intel_crtc_config *pipe_config)
4939{
4940 struct drm_device *dev = crtc->base.dev;
4941 struct drm_i915_private *dev_priv = dev->dev_private;
4942 uint32_t tmp;
4943
4944 tmp = I915_READ(PFIT_CONTROL);
4945
4946 if (INTEL_INFO(dev)->gen < 4) {
4947 if (crtc->pipe != PIPE_B)
4948 return;
4949
4950 /* gen2/3 store dither state in pfit control, needs to match */
4951 pipe_config->gmch_pfit.control = tmp & PANEL_8TO6_DITHER_ENABLE;
4952 } else {
4953 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
4954 return;
4955 }
4956
4957 if (!(tmp & PFIT_ENABLE))
4958 return;
4959
4960 pipe_config->gmch_pfit.control = I915_READ(PFIT_CONTROL);
4961 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
4962 if (INTEL_INFO(dev)->gen < 5)
4963 pipe_config->gmch_pfit.lvds_border_bits =
4964 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
4965}
4966
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01004967static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
4968 struct intel_crtc_config *pipe_config)
4969{
4970 struct drm_device *dev = crtc->base.dev;
4971 struct drm_i915_private *dev_priv = dev->dev_private;
4972 uint32_t tmp;
4973
Daniel Vettere143a212013-07-04 12:01:15 +02004974 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02004975 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02004976
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01004977 tmp = I915_READ(PIPECONF(crtc->pipe));
4978 if (!(tmp & PIPECONF_ENABLE))
4979 return false;
4980
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02004981 intel_get_pipe_timings(crtc, pipe_config);
4982
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02004983 i9xx_get_pfit_config(crtc, pipe_config);
4984
Daniel Vetter6c49f242013-06-06 12:45:25 +02004985 if (INTEL_INFO(dev)->gen >= 4) {
4986 tmp = I915_READ(DPLL_MD(crtc->pipe));
4987 pipe_config->pixel_multiplier =
4988 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
4989 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004990 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02004991 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
4992 tmp = I915_READ(DPLL(crtc->pipe));
4993 pipe_config->pixel_multiplier =
4994 ((tmp & SDVO_MULTIPLIER_MASK)
4995 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
4996 } else {
4997 /* Note that on i915G/GM the pixel multiplier is in the sdvo
4998 * port and will be fixed up in the encoder->get_config
4999 * function. */
5000 pipe_config->pixel_multiplier = 1;
5001 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005002 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
5003 if (!IS_VALLEYVIEW(dev)) {
5004 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
5005 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03005006 } else {
5007 /* Mask out read-only status bits. */
5008 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
5009 DPLL_PORTC_READY_MASK |
5010 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005011 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02005012
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005013 return true;
5014}
5015
Paulo Zanonidde86e22012-12-01 12:04:25 -02005016static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07005017{
5018 struct drm_i915_private *dev_priv = dev->dev_private;
5019 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005020 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005021 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005022 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005023 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005024 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07005025 bool has_ck505 = false;
5026 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005027
5028 /* We need to take the global config into account */
Keith Packard199e5d72011-09-22 12:01:57 -07005029 list_for_each_entry(encoder, &mode_config->encoder_list,
5030 base.head) {
5031 switch (encoder->type) {
5032 case INTEL_OUTPUT_LVDS:
5033 has_panel = true;
5034 has_lvds = true;
5035 break;
5036 case INTEL_OUTPUT_EDP:
5037 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03005038 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07005039 has_cpu_edp = true;
5040 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005041 }
5042 }
5043
Keith Packard99eb6a02011-09-26 14:29:12 -07005044 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005045 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07005046 can_ssc = has_ck505;
5047 } else {
5048 has_ck505 = false;
5049 can_ssc = true;
5050 }
5051
Imre Deak2de69052013-05-08 13:14:04 +03005052 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
5053 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005054
5055 /* Ironlake: try to setup display ref clock before DPLL
5056 * enabling. This is only under driver's control after
5057 * PCH B stepping, previous chipset stepping should be
5058 * ignoring this setting.
5059 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005060 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005061
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005062 /* As we must carefully and slowly disable/enable each source in turn,
5063 * compute the final state we want first and check if we need to
5064 * make any changes at all.
5065 */
5066 final = val;
5067 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07005068 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005069 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07005070 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005071 final |= DREF_NONSPREAD_SOURCE_ENABLE;
5072
5073 final &= ~DREF_SSC_SOURCE_MASK;
5074 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5075 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005076
Keith Packard199e5d72011-09-22 12:01:57 -07005077 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005078 final |= DREF_SSC_SOURCE_ENABLE;
5079
5080 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5081 final |= DREF_SSC1_ENABLE;
5082
5083 if (has_cpu_edp) {
5084 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5085 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
5086 else
5087 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
5088 } else
5089 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5090 } else {
5091 final |= DREF_SSC_SOURCE_DISABLE;
5092 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5093 }
5094
5095 if (final == val)
5096 return;
5097
5098 /* Always enable nonspread source */
5099 val &= ~DREF_NONSPREAD_SOURCE_MASK;
5100
5101 if (has_ck505)
5102 val |= DREF_NONSPREAD_CK505_ENABLE;
5103 else
5104 val |= DREF_NONSPREAD_SOURCE_ENABLE;
5105
5106 if (has_panel) {
5107 val &= ~DREF_SSC_SOURCE_MASK;
5108 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005109
Keith Packard199e5d72011-09-22 12:01:57 -07005110 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07005111 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005112 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005113 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02005114 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005115 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005116
5117 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005118 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005119 POSTING_READ(PCH_DREF_CONTROL);
5120 udelay(200);
5121
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005122 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005123
5124 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07005125 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07005126 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005127 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005128 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005129 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07005130 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005131 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005132 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005133 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005134
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005135 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005136 POSTING_READ(PCH_DREF_CONTROL);
5137 udelay(200);
5138 } else {
5139 DRM_DEBUG_KMS("Disabling SSC entirely\n");
5140
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005141 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07005142
5143 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005144 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005145
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005146 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005147 POSTING_READ(PCH_DREF_CONTROL);
5148 udelay(200);
5149
5150 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005151 val &= ~DREF_SSC_SOURCE_MASK;
5152 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005153
5154 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005155 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005156
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005157 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005158 POSTING_READ(PCH_DREF_CONTROL);
5159 udelay(200);
5160 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005161
5162 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005163}
5164
Paulo Zanonidde86e22012-12-01 12:04:25 -02005165/* Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O. */
5166static void lpt_init_pch_refclk(struct drm_device *dev)
5167{
5168 struct drm_i915_private *dev_priv = dev->dev_private;
5169 struct drm_mode_config *mode_config = &dev->mode_config;
5170 struct intel_encoder *encoder;
5171 bool has_vga = false;
5172 bool is_sdv = false;
5173 u32 tmp;
5174
5175 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5176 switch (encoder->type) {
5177 case INTEL_OUTPUT_ANALOG:
5178 has_vga = true;
5179 break;
5180 }
5181 }
5182
5183 if (!has_vga)
5184 return;
5185
Daniel Vetterc00db242013-01-22 15:33:27 +01005186 mutex_lock(&dev_priv->dpio_lock);
5187
Paulo Zanonidde86e22012-12-01 12:04:25 -02005188 /* XXX: Rip out SDV support once Haswell ships for real. */
5189 if (IS_HASWELL(dev) && (dev->pci_device & 0xFF00) == 0x0C00)
5190 is_sdv = true;
5191
5192 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5193 tmp &= ~SBI_SSCCTL_DISABLE;
5194 tmp |= SBI_SSCCTL_PATHALT;
5195 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5196
5197 udelay(24);
5198
5199 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5200 tmp &= ~SBI_SSCCTL_PATHALT;
5201 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5202
5203 if (!is_sdv) {
5204 tmp = I915_READ(SOUTH_CHICKEN2);
5205 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
5206 I915_WRITE(SOUTH_CHICKEN2, tmp);
5207
5208 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
5209 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
5210 DRM_ERROR("FDI mPHY reset assert timeout\n");
5211
5212 tmp = I915_READ(SOUTH_CHICKEN2);
5213 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
5214 I915_WRITE(SOUTH_CHICKEN2, tmp);
5215
5216 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
5217 FDI_MPHY_IOSFSB_RESET_STATUS) == 0,
5218 100))
5219 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
5220 }
5221
5222 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
5223 tmp &= ~(0xFF << 24);
5224 tmp |= (0x12 << 24);
5225 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
5226
Paulo Zanonidde86e22012-12-01 12:04:25 -02005227 if (is_sdv) {
5228 tmp = intel_sbi_read(dev_priv, 0x800C, SBI_MPHY);
5229 tmp |= 0x7FFF;
5230 intel_sbi_write(dev_priv, 0x800C, tmp, SBI_MPHY);
5231 }
5232
5233 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
5234 tmp |= (1 << 11);
5235 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
5236
5237 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
5238 tmp |= (1 << 11);
5239 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
5240
5241 if (is_sdv) {
5242 tmp = intel_sbi_read(dev_priv, 0x2038, SBI_MPHY);
5243 tmp |= (0x3F << 24) | (0xF << 20) | (0xF << 16);
5244 intel_sbi_write(dev_priv, 0x2038, tmp, SBI_MPHY);
5245
5246 tmp = intel_sbi_read(dev_priv, 0x2138, SBI_MPHY);
5247 tmp |= (0x3F << 24) | (0xF << 20) | (0xF << 16);
5248 intel_sbi_write(dev_priv, 0x2138, tmp, SBI_MPHY);
5249
5250 tmp = intel_sbi_read(dev_priv, 0x203C, SBI_MPHY);
5251 tmp |= (0x3F << 8);
5252 intel_sbi_write(dev_priv, 0x203C, tmp, SBI_MPHY);
5253
5254 tmp = intel_sbi_read(dev_priv, 0x213C, SBI_MPHY);
5255 tmp |= (0x3F << 8);
5256 intel_sbi_write(dev_priv, 0x213C, tmp, SBI_MPHY);
5257 }
5258
5259 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
5260 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5261 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
5262
5263 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
5264 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5265 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
5266
5267 if (!is_sdv) {
5268 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
5269 tmp &= ~(7 << 13);
5270 tmp |= (5 << 13);
5271 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
5272
5273 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
5274 tmp &= ~(7 << 13);
5275 tmp |= (5 << 13);
5276 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
5277 }
5278
5279 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
5280 tmp &= ~0xFF;
5281 tmp |= 0x1C;
5282 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
5283
5284 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
5285 tmp &= ~0xFF;
5286 tmp |= 0x1C;
5287 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
5288
5289 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
5290 tmp &= ~(0xFF << 16);
5291 tmp |= (0x1C << 16);
5292 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
5293
5294 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
5295 tmp &= ~(0xFF << 16);
5296 tmp |= (0x1C << 16);
5297 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
5298
5299 if (!is_sdv) {
5300 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
5301 tmp |= (1 << 27);
5302 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
5303
5304 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
5305 tmp |= (1 << 27);
5306 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
5307
5308 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
5309 tmp &= ~(0xF << 28);
5310 tmp |= (4 << 28);
5311 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
5312
5313 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
5314 tmp &= ~(0xF << 28);
5315 tmp |= (4 << 28);
5316 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
5317 }
5318
5319 /* ULT uses SBI_GEN0, but ULT doesn't have VGA, so we don't care. */
5320 tmp = intel_sbi_read(dev_priv, SBI_DBUFF0, SBI_ICLK);
5321 tmp |= SBI_DBUFF0_ENABLE;
5322 intel_sbi_write(dev_priv, SBI_DBUFF0, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01005323
5324 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005325}
5326
5327/*
5328 * Initialize reference clocks when the driver loads
5329 */
5330void intel_init_pch_refclk(struct drm_device *dev)
5331{
5332 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
5333 ironlake_init_pch_refclk(dev);
5334 else if (HAS_PCH_LPT(dev))
5335 lpt_init_pch_refclk(dev);
5336}
5337
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005338static int ironlake_get_refclk(struct drm_crtc *crtc)
5339{
5340 struct drm_device *dev = crtc->dev;
5341 struct drm_i915_private *dev_priv = dev->dev_private;
5342 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005343 int num_connectors = 0;
5344 bool is_lvds = false;
5345
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02005346 for_each_encoder_on_crtc(dev, crtc, encoder) {
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005347 switch (encoder->type) {
5348 case INTEL_OUTPUT_LVDS:
5349 is_lvds = true;
5350 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005351 }
5352 num_connectors++;
5353 }
5354
5355 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
5356 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005357 dev_priv->vbt.lvds_ssc_freq);
5358 return dev_priv->vbt.lvds_ssc_freq * 1000;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005359 }
5360
5361 return 120000;
5362}
5363
Daniel Vetter6ff93602013-04-19 11:24:36 +02005364static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03005365{
5366 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5367 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5368 int pipe = intel_crtc->pipe;
5369 uint32_t val;
5370
Daniel Vetter78114072013-06-13 00:54:57 +02005371 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03005372
Daniel Vetter965e0c42013-03-27 00:44:57 +01005373 switch (intel_crtc->config.pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03005374 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005375 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005376 break;
5377 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005378 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005379 break;
5380 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005381 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005382 break;
5383 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005384 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005385 break;
5386 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03005387 /* Case prevented by intel_choose_pipe_bpp_dither. */
5388 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03005389 }
5390
Daniel Vetterd8b32242013-04-25 17:54:44 +02005391 if (intel_crtc->config.dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03005392 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5393
Daniel Vetter6ff93602013-04-19 11:24:36 +02005394 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03005395 val |= PIPECONF_INTERLACED_ILK;
5396 else
5397 val |= PIPECONF_PROGRESSIVE;
5398
Daniel Vetter50f3b012013-03-27 00:44:56 +01005399 if (intel_crtc->config.limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005400 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005401
Paulo Zanonic8203562012-09-12 10:06:29 -03005402 I915_WRITE(PIPECONF(pipe), val);
5403 POSTING_READ(PIPECONF(pipe));
5404}
5405
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005406/*
5407 * Set up the pipe CSC unit.
5408 *
5409 * Currently only full range RGB to limited range RGB conversion
5410 * is supported, but eventually this should handle various
5411 * RGB<->YCbCr scenarios as well.
5412 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01005413static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005414{
5415 struct drm_device *dev = crtc->dev;
5416 struct drm_i915_private *dev_priv = dev->dev_private;
5417 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5418 int pipe = intel_crtc->pipe;
5419 uint16_t coeff = 0x7800; /* 1.0 */
5420
5421 /*
5422 * TODO: Check what kind of values actually come out of the pipe
5423 * with these coeff/postoff values and adjust to get the best
5424 * accuracy. Perhaps we even need to take the bpc value into
5425 * consideration.
5426 */
5427
Daniel Vetter50f3b012013-03-27 00:44:56 +01005428 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005429 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
5430
5431 /*
5432 * GY/GU and RY/RU should be the other way around according
5433 * to BSpec, but reality doesn't agree. Just set them up in
5434 * a way that results in the correct picture.
5435 */
5436 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
5437 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
5438
5439 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
5440 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
5441
5442 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
5443 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
5444
5445 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
5446 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
5447 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
5448
5449 if (INTEL_INFO(dev)->gen > 6) {
5450 uint16_t postoff = 0;
5451
Daniel Vetter50f3b012013-03-27 00:44:56 +01005452 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005453 postoff = (16 * (1 << 13) / 255) & 0x1fff;
5454
5455 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
5456 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
5457 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
5458
5459 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
5460 } else {
5461 uint32_t mode = CSC_MODE_YUV_TO_RGB;
5462
Daniel Vetter50f3b012013-03-27 00:44:56 +01005463 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005464 mode |= CSC_BLACK_SCREEN_OFFSET;
5465
5466 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
5467 }
5468}
5469
Daniel Vetter6ff93602013-04-19 11:24:36 +02005470static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005471{
5472 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5473 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02005474 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005475 uint32_t val;
5476
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02005477 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005478
Daniel Vetterd8b32242013-04-25 17:54:44 +02005479 if (intel_crtc->config.dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005480 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5481
Daniel Vetter6ff93602013-04-19 11:24:36 +02005482 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005483 val |= PIPECONF_INTERLACED_ILK;
5484 else
5485 val |= PIPECONF_PROGRESSIVE;
5486
Paulo Zanoni702e7a52012-10-23 18:29:59 -02005487 I915_WRITE(PIPECONF(cpu_transcoder), val);
5488 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02005489
5490 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
5491 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005492}
5493
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005494static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005495 intel_clock_t *clock,
5496 bool *has_reduced_clock,
5497 intel_clock_t *reduced_clock)
5498{
5499 struct drm_device *dev = crtc->dev;
5500 struct drm_i915_private *dev_priv = dev->dev_private;
5501 struct intel_encoder *intel_encoder;
5502 int refclk;
5503 const intel_limit_t *limit;
Daniel Vettera16af7212013-04-30 14:01:44 +02005504 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005505
5506 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5507 switch (intel_encoder->type) {
5508 case INTEL_OUTPUT_LVDS:
5509 is_lvds = true;
5510 break;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005511 }
5512 }
5513
5514 refclk = ironlake_get_refclk(crtc);
5515
5516 /*
5517 * Returns a set of divisors for the desired target clock with the given
5518 * refclk, or FALSE. The returned values represent the clock equation:
5519 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
5520 */
5521 limit = intel_limit(crtc, refclk);
Daniel Vetterff9a6752013-06-01 17:16:21 +02005522 ret = dev_priv->display.find_dpll(limit, crtc,
5523 to_intel_crtc(crtc)->config.port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02005524 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005525 if (!ret)
5526 return false;
5527
5528 if (is_lvds && dev_priv->lvds_downclock_avail) {
5529 /*
5530 * Ensure we match the reduced clock's P to the target clock.
5531 * If the clocks don't match, we can't switch the display clock
5532 * by using the FP0/FP1. In such case we will disable the LVDS
5533 * downclock feature.
5534 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02005535 *has_reduced_clock =
5536 dev_priv->display.find_dpll(limit, crtc,
5537 dev_priv->lvds_downclock,
5538 refclk, clock,
5539 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005540 }
5541
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005542 return true;
5543}
5544
Daniel Vetter01a415f2012-10-27 15:58:40 +02005545static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
5546{
5547 struct drm_i915_private *dev_priv = dev->dev_private;
5548 uint32_t temp;
5549
5550 temp = I915_READ(SOUTH_CHICKEN1);
5551 if (temp & FDI_BC_BIFURCATION_SELECT)
5552 return;
5553
5554 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
5555 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
5556
5557 temp |= FDI_BC_BIFURCATION_SELECT;
5558 DRM_DEBUG_KMS("enabling fdi C rx\n");
5559 I915_WRITE(SOUTH_CHICKEN1, temp);
5560 POSTING_READ(SOUTH_CHICKEN1);
5561}
5562
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005563static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
Daniel Vetter01a415f2012-10-27 15:58:40 +02005564{
5565 struct drm_device *dev = intel_crtc->base.dev;
5566 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005567
5568 switch (intel_crtc->pipe) {
5569 case PIPE_A:
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005570 break;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005571 case PIPE_B:
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005572 if (intel_crtc->config.fdi_lanes > 2)
Daniel Vetter01a415f2012-10-27 15:58:40 +02005573 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
5574 else
5575 cpt_enable_fdi_bc_bifurcation(dev);
5576
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005577 break;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005578 case PIPE_C:
Daniel Vetter01a415f2012-10-27 15:58:40 +02005579 cpt_enable_fdi_bc_bifurcation(dev);
5580
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005581 break;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005582 default:
5583 BUG();
5584 }
5585}
5586
Paulo Zanonid4b19312012-11-29 11:29:32 -02005587int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
5588{
5589 /*
5590 * Account for spread spectrum to avoid
5591 * oversubscribing the link. Max center spread
5592 * is 2.5%; use 5% for safety's sake.
5593 */
5594 u32 bps = target_clock * bpp * 21 / 20;
5595 return bps / (link_bw * 8) + 1;
5596}
5597
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005598static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02005599{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005600 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005601}
5602
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005603static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005604 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02005605 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005606{
5607 struct drm_crtc *crtc = &intel_crtc->base;
5608 struct drm_device *dev = crtc->dev;
5609 struct drm_i915_private *dev_priv = dev->dev_private;
5610 struct intel_encoder *intel_encoder;
5611 uint32_t dpll;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01005612 int factor, num_connectors = 0;
Daniel Vetter09ede542013-04-30 14:01:45 +02005613 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005614
5615 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5616 switch (intel_encoder->type) {
5617 case INTEL_OUTPUT_LVDS:
5618 is_lvds = true;
5619 break;
5620 case INTEL_OUTPUT_SDVO:
5621 case INTEL_OUTPUT_HDMI:
5622 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005623 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005624 }
5625
5626 num_connectors++;
5627 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005628
Chris Wilsonc1858122010-12-03 21:35:48 +00005629 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07005630 factor = 21;
5631 if (is_lvds) {
5632 if ((intel_panel_use_ssc(dev_priv) &&
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005633 dev_priv->vbt.lvds_ssc_freq == 100) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02005634 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07005635 factor = 25;
Daniel Vetter09ede542013-04-30 14:01:45 +02005636 } else if (intel_crtc->config.sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07005637 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00005638
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005639 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02005640 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00005641
Daniel Vetter9a7c7892013-04-04 22:20:34 +02005642 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
5643 *fp2 |= FP_CB_TUNE;
5644
Chris Wilson5eddb702010-09-11 13:48:45 +01005645 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005646
Eric Anholta07d6782011-03-30 13:01:08 -07005647 if (is_lvds)
5648 dpll |= DPLLB_MODE_LVDS;
5649 else
5650 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02005651
Daniel Vetteref1b4602013-06-01 17:17:04 +02005652 dpll |= (intel_crtc->config.pixel_multiplier - 1)
5653 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02005654
5655 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02005656 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter9566e9a2013-04-19 11:14:36 +02005657 if (intel_crtc->config.has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02005658 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08005659
Eric Anholta07d6782011-03-30 13:01:08 -07005660 /* compute bitmask from p1 value */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005661 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005662 /* also FPA1 */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005663 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005664
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005665 switch (intel_crtc->config.dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07005666 case 5:
5667 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5668 break;
5669 case 7:
5670 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5671 break;
5672 case 10:
5673 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5674 break;
5675 case 14:
5676 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5677 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005678 }
5679
Daniel Vetterb4c09f32013-04-30 14:01:42 +02005680 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005681 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08005682 else
5683 dpll |= PLL_REF_INPUT_DREFCLK;
5684
Daniel Vetter959e16d2013-06-05 13:34:21 +02005685 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005686}
5687
Jesse Barnes79e53942008-11-07 14:24:08 -08005688static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
Jesse Barnes79e53942008-11-07 14:24:08 -08005689 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005690 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005691{
5692 struct drm_device *dev = crtc->dev;
5693 struct drm_i915_private *dev_priv = dev->dev_private;
5694 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5695 int pipe = intel_crtc->pipe;
5696 int plane = intel_crtc->plane;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005697 int num_connectors = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005698 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02005699 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03005700 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01005701 bool is_lvds = false;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005702 struct intel_encoder *encoder;
Daniel Vettere2b78262013-06-07 23:10:03 +02005703 struct intel_shared_dpll *pll;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005704 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08005705
5706 for_each_encoder_on_crtc(dev, crtc, encoder) {
5707 switch (encoder->type) {
5708 case INTEL_OUTPUT_LVDS:
5709 is_lvds = true;
5710 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005711 }
5712
5713 num_connectors++;
5714 }
5715
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005716 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
5717 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
5718
Daniel Vetterff9a6752013-06-01 17:16:21 +02005719 ok = ironlake_compute_clocks(crtc, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005720 &has_reduced_clock, &reduced_clock);
Daniel Vetteree9300b2013-06-03 22:40:22 +02005721 if (!ok && !intel_crtc->config.clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08005722 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5723 return -EINVAL;
5724 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01005725 /* Compat-code for transition, will disappear. */
5726 if (!intel_crtc->config.clock_set) {
5727 intel_crtc->config.dpll.n = clock.n;
5728 intel_crtc->config.dpll.m1 = clock.m1;
5729 intel_crtc->config.dpll.m2 = clock.m2;
5730 intel_crtc->config.dpll.p1 = clock.p1;
5731 intel_crtc->config.dpll.p2 = clock.p2;
5732 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005733
5734 /* Ensure that the cursor is valid for the new mode before changing... */
5735 intel_crtc_update_cursor(crtc, true);
5736
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005737 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
Daniel Vetter8b470472013-03-28 10:41:59 +01005738 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005739 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02005740 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005741 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02005742
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005743 dpll = ironlake_compute_dpll(intel_crtc,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02005744 &fp, &reduced_clock,
5745 has_reduced_clock ? &fp2 : NULL);
5746
Daniel Vetter959e16d2013-06-05 13:34:21 +02005747 intel_crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02005748 intel_crtc->config.dpll_hw_state.fp0 = fp;
5749 if (has_reduced_clock)
5750 intel_crtc->config.dpll_hw_state.fp1 = fp2;
5751 else
5752 intel_crtc->config.dpll_hw_state.fp1 = fp;
5753
Daniel Vetterb89a1d32013-06-05 13:34:24 +02005754 pll = intel_get_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005755 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03005756 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
5757 pipe_name(pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07005758 return -EINVAL;
5759 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005760 } else
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005761 intel_put_shared_dpll(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08005762
Daniel Vetter03afc4a2013-04-02 23:42:31 +02005763 if (intel_crtc->config.has_dp_encoder)
5764 intel_dp_set_m_n(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08005765
Daniel Vetterbcd644e2013-06-05 13:34:22 +02005766 if (is_lvds && has_reduced_clock && i915_powersave)
5767 intel_crtc->lowfreq_avail = true;
5768 else
5769 intel_crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02005770
5771 if (intel_crtc->config.has_pch_encoder) {
5772 pll = intel_crtc_to_shared_dpll(intel_crtc);
5773
Jesse Barnes79e53942008-11-07 14:24:08 -08005774 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005775
Daniel Vetter8a654f32013-06-01 17:16:22 +02005776 intel_set_pipe_timings(intel_crtc);
Krzysztof Halasa734b4152010-05-25 18:41:46 +02005777
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01005778 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01005779 intel_cpu_transcoder_set_m_n(intel_crtc,
5780 &intel_crtc->config.fdi_m_n);
5781 }
Chris Wilson5eddb702010-09-11 13:48:45 +01005782
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005783 if (IS_IVYBRIDGE(dev))
5784 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
Zhenyu Wang2c072452009-06-05 15:38:42 +08005785
Daniel Vetter6ff93602013-04-19 11:24:36 +02005786 ironlake_set_pipeconf(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08005787
Paulo Zanonia1f9e772012-09-12 10:06:32 -03005788 /* Set up the display plane register */
5789 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08005790 POSTING_READ(DSPCNTR(plane));
Jesse Barnes79e53942008-11-07 14:24:08 -08005791
Daniel Vetter94352cf2012-07-05 22:51:56 +02005792 ret = intel_pipe_set_base(crtc, x, y, fb);
Shaohua Li7662c8b2009-06-26 11:23:55 +08005793
5794 intel_update_watermarks(dev);
5795
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005796 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08005797}
5798
Daniel Vetter72419202013-04-04 13:28:53 +02005799static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5800 struct intel_crtc_config *pipe_config)
5801{
5802 struct drm_device *dev = crtc->base.dev;
5803 struct drm_i915_private *dev_priv = dev->dev_private;
5804 enum transcoder transcoder = pipe_config->cpu_transcoder;
5805
5806 pipe_config->fdi_m_n.link_m = I915_READ(PIPE_LINK_M1(transcoder));
5807 pipe_config->fdi_m_n.link_n = I915_READ(PIPE_LINK_N1(transcoder));
5808 pipe_config->fdi_m_n.gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
5809 & ~TU_SIZE_MASK;
5810 pipe_config->fdi_m_n.gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
5811 pipe_config->fdi_m_n.tu = ((I915_READ(PIPE_DATA_M1(transcoder))
5812 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
5813}
5814
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005815static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5816 struct intel_crtc_config *pipe_config)
5817{
5818 struct drm_device *dev = crtc->base.dev;
5819 struct drm_i915_private *dev_priv = dev->dev_private;
5820 uint32_t tmp;
5821
5822 tmp = I915_READ(PF_CTL(crtc->pipe));
5823
5824 if (tmp & PF_ENABLE) {
5825 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
5826 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02005827
5828 /* We currently do not free assignements of panel fitters on
5829 * ivb/hsw (since we don't use the higher upscaling modes which
5830 * differentiates them) so just WARN about this case for now. */
5831 if (IS_GEN7(dev)) {
5832 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
5833 PF_PIPE_SEL_IVB(crtc->pipe));
5834 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005835 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005836}
5837
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005838static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5839 struct intel_crtc_config *pipe_config)
5840{
5841 struct drm_device *dev = crtc->base.dev;
5842 struct drm_i915_private *dev_priv = dev->dev_private;
5843 uint32_t tmp;
5844
Daniel Vettere143a212013-07-04 12:01:15 +02005845 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02005846 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02005847
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005848 tmp = I915_READ(PIPECONF(crtc->pipe));
5849 if (!(tmp & PIPECONF_ENABLE))
5850 return false;
5851
Daniel Vetterab9412b2013-05-03 11:49:46 +02005852 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02005853 struct intel_shared_dpll *pll;
5854
Daniel Vetter88adfff2013-03-28 10:42:01 +01005855 pipe_config->has_pch_encoder = true;
5856
Daniel Vetter627eb5a2013-04-29 19:33:42 +02005857 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
5858 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
5859 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02005860
5861 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02005862
Daniel Vetterc0d43d62013-06-07 23:11:08 +02005863 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02005864 pipe_config->shared_dpll =
5865 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02005866 } else {
5867 tmp = I915_READ(PCH_DPLL_SEL);
5868 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
5869 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
5870 else
5871 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
5872 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02005873
5874 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
5875
5876 WARN_ON(!pll->get_hw_state(dev_priv, pll,
5877 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02005878
5879 tmp = pipe_config->dpll_hw_state.dpll;
5880 pipe_config->pixel_multiplier =
5881 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
5882 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter6c49f242013-06-06 12:45:25 +02005883 } else {
5884 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02005885 }
5886
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005887 intel_get_pipe_timings(crtc, pipe_config);
5888
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005889 ironlake_get_pfit_config(crtc, pipe_config);
5890
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005891 return true;
5892}
5893
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005894static void haswell_modeset_global_resources(struct drm_device *dev)
5895{
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005896 bool enable = false;
5897 struct intel_crtc *crtc;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005898
5899 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
Daniel Vettere7a639c2013-05-31 17:49:17 +02005900 if (!crtc->base.enabled)
5901 continue;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005902
Daniel Vettere7a639c2013-05-31 17:49:17 +02005903 if (crtc->pipe != PIPE_A || crtc->config.pch_pfit.size ||
5904 crtc->config.cpu_transcoder != TRANSCODER_EDP)
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005905 enable = true;
5906 }
5907
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005908 intel_set_power_well(dev, enable);
5909}
5910
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005911static int haswell_crtc_mode_set(struct drm_crtc *crtc,
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005912 int x, int y,
5913 struct drm_framebuffer *fb)
5914{
5915 struct drm_device *dev = crtc->dev;
5916 struct drm_i915_private *dev_priv = dev->dev_private;
5917 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005918 int plane = intel_crtc->plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005919 int ret;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005920
Daniel Vetterff9a6752013-06-01 17:16:21 +02005921 if (!intel_ddi_pll_mode_set(crtc))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03005922 return -EINVAL;
5923
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005924 /* Ensure that the cursor is valid for the new mode before changing... */
5925 intel_crtc_update_cursor(crtc, true);
5926
Daniel Vetter03afc4a2013-04-02 23:42:31 +02005927 if (intel_crtc->config.has_dp_encoder)
5928 intel_dp_set_m_n(intel_crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005929
5930 intel_crtc->lowfreq_avail = false;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005931
Daniel Vetter8a654f32013-06-01 17:16:22 +02005932 intel_set_pipe_timings(intel_crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005933
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01005934 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01005935 intel_cpu_transcoder_set_m_n(intel_crtc,
5936 &intel_crtc->config.fdi_m_n);
5937 }
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005938
Daniel Vetter6ff93602013-04-19 11:24:36 +02005939 haswell_set_pipeconf(crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005940
Daniel Vetter50f3b012013-03-27 00:44:56 +01005941 intel_set_pipe_csc(crtc);
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005942
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005943 /* Set up the display plane register */
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005944 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005945 POSTING_READ(DSPCNTR(plane));
5946
5947 ret = intel_pipe_set_base(crtc, x, y, fb);
5948
5949 intel_update_watermarks(dev);
5950
Jesse Barnes79e53942008-11-07 14:24:08 -08005951 return ret;
5952}
5953
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005954static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5955 struct intel_crtc_config *pipe_config)
5956{
5957 struct drm_device *dev = crtc->base.dev;
5958 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005959 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005960 uint32_t tmp;
5961
Daniel Vettere143a212013-07-04 12:01:15 +02005962 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02005963 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
5964
Daniel Vettereccb1402013-05-22 00:50:22 +02005965 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
5966 if (tmp & TRANS_DDI_FUNC_ENABLE) {
5967 enum pipe trans_edp_pipe;
5968 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
5969 default:
5970 WARN(1, "unknown pipe linked to edp transcoder\n");
5971 case TRANS_DDI_EDP_INPUT_A_ONOFF:
5972 case TRANS_DDI_EDP_INPUT_A_ON:
5973 trans_edp_pipe = PIPE_A;
5974 break;
5975 case TRANS_DDI_EDP_INPUT_B_ONOFF:
5976 trans_edp_pipe = PIPE_B;
5977 break;
5978 case TRANS_DDI_EDP_INPUT_C_ONOFF:
5979 trans_edp_pipe = PIPE_C;
5980 break;
5981 }
5982
5983 if (trans_edp_pipe == crtc->pipe)
5984 pipe_config->cpu_transcoder = TRANSCODER_EDP;
5985 }
5986
Paulo Zanonib97186f2013-05-03 12:15:36 -03005987 if (!intel_display_power_enabled(dev,
Daniel Vettereccb1402013-05-22 00:50:22 +02005988 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03005989 return false;
5990
Daniel Vettereccb1402013-05-22 00:50:22 +02005991 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005992 if (!(tmp & PIPECONF_ENABLE))
5993 return false;
5994
Daniel Vetter88adfff2013-03-28 10:42:01 +01005995 /*
Paulo Zanonif196e6b2013-04-18 16:35:41 -03005996 * Haswell has only FDI/PCH transcoder A. It is which is connected to
Daniel Vetter88adfff2013-03-28 10:42:01 +01005997 * DDI E. So just check whether this pipe is wired to DDI E and whether
5998 * the PCH transcoder is on.
5999 */
Daniel Vettereccb1402013-05-22 00:50:22 +02006000 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
Daniel Vetter88adfff2013-03-28 10:42:01 +01006001 if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
Daniel Vetterab9412b2013-05-03 11:49:46 +02006002 I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter88adfff2013-03-28 10:42:01 +01006003 pipe_config->has_pch_encoder = true;
6004
Daniel Vetter627eb5a2013-04-29 19:33:42 +02006005 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
6006 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6007 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02006008
6009 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02006010 }
6011
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006012 intel_get_pipe_timings(crtc, pipe_config);
6013
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006014 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
6015 if (intel_display_power_enabled(dev, pfit_domain))
6016 ironlake_get_pfit_config(crtc, pipe_config);
Daniel Vetter88adfff2013-03-28 10:42:01 +01006017
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006018 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
6019 (I915_READ(IPS_CTL) & IPS_ENABLE);
6020
Daniel Vetter6c49f242013-06-06 12:45:25 +02006021 pipe_config->pixel_multiplier = 1;
6022
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006023 return true;
6024}
6025
Eric Anholtf564048e2011-03-30 13:01:02 -07006026static int intel_crtc_mode_set(struct drm_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07006027 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02006028 struct drm_framebuffer *fb)
Eric Anholtf564048e2011-03-30 13:01:02 -07006029{
6030 struct drm_device *dev = crtc->dev;
6031 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter9256aa12012-10-31 19:26:13 +01006032 struct drm_encoder_helper_funcs *encoder_funcs;
6033 struct intel_encoder *encoder;
Eric Anholt0b701d22011-03-30 13:01:03 -07006034 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01006035 struct drm_display_mode *adjusted_mode =
6036 &intel_crtc->config.adjusted_mode;
6037 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
Eric Anholt0b701d22011-03-30 13:01:03 -07006038 int pipe = intel_crtc->pipe;
Eric Anholtf564048e2011-03-30 13:01:02 -07006039 int ret;
6040
Eric Anholt0b701d22011-03-30 13:01:03 -07006041 drm_vblank_pre_modeset(dev, pipe);
6042
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01006043 ret = dev_priv->display.crtc_mode_set(crtc, x, y, fb);
6044
Jesse Barnes79e53942008-11-07 14:24:08 -08006045 drm_vblank_post_modeset(dev, pipe);
6046
Daniel Vetter9256aa12012-10-31 19:26:13 +01006047 if (ret != 0)
6048 return ret;
6049
6050 for_each_encoder_on_crtc(dev, crtc, encoder) {
6051 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
6052 encoder->base.base.id,
6053 drm_get_encoder_name(&encoder->base),
6054 mode->base.id, mode->name);
Daniel Vetter6cc5f342013-03-27 00:44:53 +01006055 if (encoder->mode_set) {
6056 encoder->mode_set(encoder);
6057 } else {
6058 encoder_funcs = encoder->base.helper_private;
6059 encoder_funcs->mode_set(&encoder->base, mode, adjusted_mode);
6060 }
Daniel Vetter9256aa12012-10-31 19:26:13 +01006061 }
6062
6063 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006064}
6065
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006066static bool intel_eld_uptodate(struct drm_connector *connector,
6067 int reg_eldv, uint32_t bits_eldv,
6068 int reg_elda, uint32_t bits_elda,
6069 int reg_edid)
6070{
6071 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6072 uint8_t *eld = connector->eld;
6073 uint32_t i;
6074
6075 i = I915_READ(reg_eldv);
6076 i &= bits_eldv;
6077
6078 if (!eld[0])
6079 return !i;
6080
6081 if (!i)
6082 return false;
6083
6084 i = I915_READ(reg_elda);
6085 i &= ~bits_elda;
6086 I915_WRITE(reg_elda, i);
6087
6088 for (i = 0; i < eld[2]; i++)
6089 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
6090 return false;
6091
6092 return true;
6093}
6094
Wu Fengguange0dac652011-09-05 14:25:34 +08006095static void g4x_write_eld(struct drm_connector *connector,
6096 struct drm_crtc *crtc)
6097{
6098 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6099 uint8_t *eld = connector->eld;
6100 uint32_t eldv;
6101 uint32_t len;
6102 uint32_t i;
6103
6104 i = I915_READ(G4X_AUD_VID_DID);
6105
6106 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
6107 eldv = G4X_ELDV_DEVCL_DEVBLC;
6108 else
6109 eldv = G4X_ELDV_DEVCTG;
6110
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006111 if (intel_eld_uptodate(connector,
6112 G4X_AUD_CNTL_ST, eldv,
6113 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
6114 G4X_HDMIW_HDMIEDID))
6115 return;
6116
Wu Fengguange0dac652011-09-05 14:25:34 +08006117 i = I915_READ(G4X_AUD_CNTL_ST);
6118 i &= ~(eldv | G4X_ELD_ADDR);
6119 len = (i >> 9) & 0x1f; /* ELD buffer size */
6120 I915_WRITE(G4X_AUD_CNTL_ST, i);
6121
6122 if (!eld[0])
6123 return;
6124
6125 len = min_t(uint8_t, eld[2], len);
6126 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6127 for (i = 0; i < len; i++)
6128 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
6129
6130 i = I915_READ(G4X_AUD_CNTL_ST);
6131 i |= eldv;
6132 I915_WRITE(G4X_AUD_CNTL_ST, i);
6133}
6134
Wang Xingchao83358c852012-08-16 22:43:37 +08006135static void haswell_write_eld(struct drm_connector *connector,
6136 struct drm_crtc *crtc)
6137{
6138 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6139 uint8_t *eld = connector->eld;
6140 struct drm_device *dev = crtc->dev;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08006141 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Wang Xingchao83358c852012-08-16 22:43:37 +08006142 uint32_t eldv;
6143 uint32_t i;
6144 int len;
6145 int pipe = to_intel_crtc(crtc)->pipe;
6146 int tmp;
6147
6148 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
6149 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
6150 int aud_config = HSW_AUD_CFG(pipe);
6151 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
6152
6153
6154 DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
6155
6156 /* Audio output enable */
6157 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
6158 tmp = I915_READ(aud_cntrl_st2);
6159 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
6160 I915_WRITE(aud_cntrl_st2, tmp);
6161
6162 /* Wait for 1 vertical blank */
6163 intel_wait_for_vblank(dev, pipe);
6164
6165 /* Set ELD valid state */
6166 tmp = I915_READ(aud_cntrl_st2);
6167 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%8x\n", tmp);
6168 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
6169 I915_WRITE(aud_cntrl_st2, tmp);
6170 tmp = I915_READ(aud_cntrl_st2);
6171 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%8x\n", tmp);
6172
6173 /* Enable HDMI mode */
6174 tmp = I915_READ(aud_config);
6175 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%8x\n", tmp);
6176 /* clear N_programing_enable and N_value_index */
6177 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
6178 I915_WRITE(aud_config, tmp);
6179
6180 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
6181
6182 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08006183 intel_crtc->eld_vld = true;
Wang Xingchao83358c852012-08-16 22:43:37 +08006184
6185 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6186 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6187 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
6188 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6189 } else
6190 I915_WRITE(aud_config, 0);
6191
6192 if (intel_eld_uptodate(connector,
6193 aud_cntrl_st2, eldv,
6194 aud_cntl_st, IBX_ELD_ADDRESS,
6195 hdmiw_hdmiedid))
6196 return;
6197
6198 i = I915_READ(aud_cntrl_st2);
6199 i &= ~eldv;
6200 I915_WRITE(aud_cntrl_st2, i);
6201
6202 if (!eld[0])
6203 return;
6204
6205 i = I915_READ(aud_cntl_st);
6206 i &= ~IBX_ELD_ADDRESS;
6207 I915_WRITE(aud_cntl_st, i);
6208 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
6209 DRM_DEBUG_DRIVER("port num:%d\n", i);
6210
6211 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6212 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6213 for (i = 0; i < len; i++)
6214 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6215
6216 i = I915_READ(aud_cntrl_st2);
6217 i |= eldv;
6218 I915_WRITE(aud_cntrl_st2, i);
6219
6220}
6221
Wu Fengguange0dac652011-09-05 14:25:34 +08006222static void ironlake_write_eld(struct drm_connector *connector,
6223 struct drm_crtc *crtc)
6224{
6225 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6226 uint8_t *eld = connector->eld;
6227 uint32_t eldv;
6228 uint32_t i;
6229 int len;
6230 int hdmiw_hdmiedid;
Wu Fengguangb6daa022012-01-06 14:41:31 -06006231 int aud_config;
Wu Fengguange0dac652011-09-05 14:25:34 +08006232 int aud_cntl_st;
6233 int aud_cntrl_st2;
Wang Xingchao9b138a82012-08-09 16:52:18 +08006234 int pipe = to_intel_crtc(crtc)->pipe;
Wu Fengguange0dac652011-09-05 14:25:34 +08006235
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08006236 if (HAS_PCH_IBX(connector->dev)) {
Wang Xingchao9b138a82012-08-09 16:52:18 +08006237 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
6238 aud_config = IBX_AUD_CFG(pipe);
6239 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006240 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08006241 } else {
Wang Xingchao9b138a82012-08-09 16:52:18 +08006242 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
6243 aud_config = CPT_AUD_CFG(pipe);
6244 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006245 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08006246 }
6247
Wang Xingchao9b138a82012-08-09 16:52:18 +08006248 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
Wu Fengguange0dac652011-09-05 14:25:34 +08006249
6250 i = I915_READ(aud_cntl_st);
Wang Xingchao9b138a82012-08-09 16:52:18 +08006251 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
Wu Fengguange0dac652011-09-05 14:25:34 +08006252 if (!i) {
6253 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
6254 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006255 eldv = IBX_ELD_VALIDB;
6256 eldv |= IBX_ELD_VALIDB << 4;
6257 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08006258 } else {
Ville Syrjälä2582a852013-04-17 17:48:47 +03006259 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006260 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08006261 }
6262
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006263 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6264 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6265 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
Wu Fengguangb6daa022012-01-06 14:41:31 -06006266 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6267 } else
6268 I915_WRITE(aud_config, 0);
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006269
6270 if (intel_eld_uptodate(connector,
6271 aud_cntrl_st2, eldv,
6272 aud_cntl_st, IBX_ELD_ADDRESS,
6273 hdmiw_hdmiedid))
6274 return;
6275
Wu Fengguange0dac652011-09-05 14:25:34 +08006276 i = I915_READ(aud_cntrl_st2);
6277 i &= ~eldv;
6278 I915_WRITE(aud_cntrl_st2, i);
6279
6280 if (!eld[0])
6281 return;
6282
Wu Fengguange0dac652011-09-05 14:25:34 +08006283 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006284 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08006285 I915_WRITE(aud_cntl_st, i);
6286
6287 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6288 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6289 for (i = 0; i < len; i++)
6290 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6291
6292 i = I915_READ(aud_cntrl_st2);
6293 i |= eldv;
6294 I915_WRITE(aud_cntrl_st2, i);
6295}
6296
6297void intel_write_eld(struct drm_encoder *encoder,
6298 struct drm_display_mode *mode)
6299{
6300 struct drm_crtc *crtc = encoder->crtc;
6301 struct drm_connector *connector;
6302 struct drm_device *dev = encoder->dev;
6303 struct drm_i915_private *dev_priv = dev->dev_private;
6304
6305 connector = drm_select_eld(encoder, mode);
6306 if (!connector)
6307 return;
6308
6309 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6310 connector->base.id,
6311 drm_get_connector_name(connector),
6312 connector->encoder->base.id,
6313 drm_get_encoder_name(connector->encoder));
6314
6315 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
6316
6317 if (dev_priv->display.write_eld)
6318 dev_priv->display.write_eld(connector, crtc);
6319}
6320
Jesse Barnes79e53942008-11-07 14:24:08 -08006321/** Loads the palette/gamma unit for the CRTC with the prepared values */
6322void intel_crtc_load_lut(struct drm_crtc *crtc)
6323{
6324 struct drm_device *dev = crtc->dev;
6325 struct drm_i915_private *dev_priv = dev->dev_private;
6326 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006327 enum pipe pipe = intel_crtc->pipe;
6328 int palreg = PALETTE(pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08006329 int i;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006330 bool reenable_ips = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006331
6332 /* The clocks have to be on to load the palette. */
Alban Browaeysaed3f092012-02-24 17:12:45 +00006333 if (!crtc->enabled || !intel_crtc->active)
Jesse Barnes79e53942008-11-07 14:24:08 -08006334 return;
6335
Ville Syrjälä14420bd2013-06-04 13:49:07 +03006336 if (!HAS_PCH_SPLIT(dev_priv->dev))
6337 assert_pll_enabled(dev_priv, pipe);
6338
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006339 /* use legacy palette for Ironlake */
Eric Anholtbad720f2009-10-22 16:11:14 -07006340 if (HAS_PCH_SPLIT(dev))
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006341 palreg = LGC_PALETTE(pipe);
6342
6343 /* Workaround : Do not read or write the pipe palette/gamma data while
6344 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
6345 */
6346 if (intel_crtc->config.ips_enabled &&
6347 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
6348 GAMMA_MODE_MODE_SPLIT)) {
6349 hsw_disable_ips(intel_crtc);
6350 reenable_ips = true;
6351 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08006352
Jesse Barnes79e53942008-11-07 14:24:08 -08006353 for (i = 0; i < 256; i++) {
6354 I915_WRITE(palreg + 4 * i,
6355 (intel_crtc->lut_r[i] << 16) |
6356 (intel_crtc->lut_g[i] << 8) |
6357 intel_crtc->lut_b[i]);
6358 }
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006359
6360 if (reenable_ips)
6361 hsw_enable_ips(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006362}
6363
Chris Wilson560b85b2010-08-07 11:01:38 +01006364static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
6365{
6366 struct drm_device *dev = crtc->dev;
6367 struct drm_i915_private *dev_priv = dev->dev_private;
6368 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6369 bool visible = base != 0;
6370 u32 cntl;
6371
6372 if (intel_crtc->cursor_visible == visible)
6373 return;
6374
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006375 cntl = I915_READ(_CURACNTR);
Chris Wilson560b85b2010-08-07 11:01:38 +01006376 if (visible) {
6377 /* On these chipsets we can only modify the base whilst
6378 * the cursor is disabled.
6379 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006380 I915_WRITE(_CURABASE, base);
Chris Wilson560b85b2010-08-07 11:01:38 +01006381
6382 cntl &= ~(CURSOR_FORMAT_MASK);
6383 /* XXX width must be 64, stride 256 => 0x00 << 28 */
6384 cntl |= CURSOR_ENABLE |
6385 CURSOR_GAMMA_ENABLE |
6386 CURSOR_FORMAT_ARGB;
6387 } else
6388 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006389 I915_WRITE(_CURACNTR, cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01006390
6391 intel_crtc->cursor_visible = visible;
6392}
6393
6394static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
6395{
6396 struct drm_device *dev = crtc->dev;
6397 struct drm_i915_private *dev_priv = dev->dev_private;
6398 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6399 int pipe = intel_crtc->pipe;
6400 bool visible = base != 0;
6401
6402 if (intel_crtc->cursor_visible != visible) {
Jesse Barnes548f2452011-02-17 10:40:53 -08006403 uint32_t cntl = I915_READ(CURCNTR(pipe));
Chris Wilson560b85b2010-08-07 11:01:38 +01006404 if (base) {
6405 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
6406 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6407 cntl |= pipe << 28; /* Connect to correct pipe */
6408 } else {
6409 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6410 cntl |= CURSOR_MODE_DISABLE;
6411 }
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006412 I915_WRITE(CURCNTR(pipe), cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01006413
6414 intel_crtc->cursor_visible = visible;
6415 }
6416 /* and commit changes on next vblank */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006417 I915_WRITE(CURBASE(pipe), base);
Chris Wilson560b85b2010-08-07 11:01:38 +01006418}
6419
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006420static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
6421{
6422 struct drm_device *dev = crtc->dev;
6423 struct drm_i915_private *dev_priv = dev->dev_private;
6424 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6425 int pipe = intel_crtc->pipe;
6426 bool visible = base != 0;
6427
6428 if (intel_crtc->cursor_visible != visible) {
6429 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
6430 if (base) {
6431 cntl &= ~CURSOR_MODE;
6432 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6433 } else {
6434 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6435 cntl |= CURSOR_MODE_DISABLE;
6436 }
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006437 if (IS_HASWELL(dev))
6438 cntl |= CURSOR_PIPE_CSC_ENABLE;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006439 I915_WRITE(CURCNTR_IVB(pipe), cntl);
6440
6441 intel_crtc->cursor_visible = visible;
6442 }
6443 /* and commit changes on next vblank */
6444 I915_WRITE(CURBASE_IVB(pipe), base);
6445}
6446
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006447/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01006448static void intel_crtc_update_cursor(struct drm_crtc *crtc,
6449 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006450{
6451 struct drm_device *dev = crtc->dev;
6452 struct drm_i915_private *dev_priv = dev->dev_private;
6453 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6454 int pipe = intel_crtc->pipe;
6455 int x = intel_crtc->cursor_x;
6456 int y = intel_crtc->cursor_y;
Chris Wilson560b85b2010-08-07 11:01:38 +01006457 u32 base, pos;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006458 bool visible;
6459
6460 pos = 0;
6461
Chris Wilson6b383a72010-09-13 13:54:26 +01006462 if (on && crtc->enabled && crtc->fb) {
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006463 base = intel_crtc->cursor_addr;
6464 if (x > (int) crtc->fb->width)
6465 base = 0;
6466
6467 if (y > (int) crtc->fb->height)
6468 base = 0;
6469 } else
6470 base = 0;
6471
6472 if (x < 0) {
6473 if (x + intel_crtc->cursor_width < 0)
6474 base = 0;
6475
6476 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
6477 x = -x;
6478 }
6479 pos |= x << CURSOR_X_SHIFT;
6480
6481 if (y < 0) {
6482 if (y + intel_crtc->cursor_height < 0)
6483 base = 0;
6484
6485 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
6486 y = -y;
6487 }
6488 pos |= y << CURSOR_Y_SHIFT;
6489
6490 visible = base != 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01006491 if (!visible && !intel_crtc->cursor_visible)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006492 return;
6493
Eugeni Dodonov0cd83aa2012-04-13 17:08:48 -03006494 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006495 I915_WRITE(CURPOS_IVB(pipe), pos);
6496 ivb_update_cursor(crtc, base);
6497 } else {
6498 I915_WRITE(CURPOS(pipe), pos);
6499 if (IS_845G(dev) || IS_I865G(dev))
6500 i845_update_cursor(crtc, base);
6501 else
6502 i9xx_update_cursor(crtc, base);
6503 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006504}
6505
Jesse Barnes79e53942008-11-07 14:24:08 -08006506static int intel_crtc_cursor_set(struct drm_crtc *crtc,
Chris Wilson05394f32010-11-08 19:18:58 +00006507 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08006508 uint32_t handle,
6509 uint32_t width, uint32_t height)
6510{
6511 struct drm_device *dev = crtc->dev;
6512 struct drm_i915_private *dev_priv = dev->dev_private;
6513 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00006514 struct drm_i915_gem_object *obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006515 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006516 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006517
Jesse Barnes79e53942008-11-07 14:24:08 -08006518 /* if we want to turn off the cursor ignore width and height */
6519 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08006520 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006521 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00006522 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10006523 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006524 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08006525 }
6526
6527 /* Currently we only support 64x64 cursors */
6528 if (width != 64 || height != 64) {
6529 DRM_ERROR("we currently only support 64x64 cursors\n");
6530 return -EINVAL;
6531 }
6532
Chris Wilson05394f32010-11-08 19:18:58 +00006533 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00006534 if (&obj->base == NULL)
Jesse Barnes79e53942008-11-07 14:24:08 -08006535 return -ENOENT;
6536
Chris Wilson05394f32010-11-08 19:18:58 +00006537 if (obj->base.size < width * height * 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006538 DRM_ERROR("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10006539 ret = -ENOMEM;
6540 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08006541 }
6542
Dave Airlie71acb5e2008-12-30 20:31:46 +10006543 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006544 mutex_lock(&dev->struct_mutex);
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05006545 if (!dev_priv->info->cursor_needs_physical) {
Chris Wilson693db182013-03-05 14:52:39 +00006546 unsigned alignment;
6547
Chris Wilsond9e86c02010-11-10 16:40:20 +00006548 if (obj->tiling_mode) {
6549 DRM_ERROR("cursor cannot be tiled\n");
6550 ret = -EINVAL;
6551 goto fail_locked;
6552 }
6553
Chris Wilson693db182013-03-05 14:52:39 +00006554 /* Note that the w/a also requires 2 PTE of padding following
6555 * the bo. We currently fill all unused PTE with the shadow
6556 * page and so we should always have valid PTE following the
6557 * cursor preventing the VT-d warning.
6558 */
6559 alignment = 0;
6560 if (need_vtd_wa(dev))
6561 alignment = 64*1024;
6562
6563 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01006564 if (ret) {
6565 DRM_ERROR("failed to move cursor bo into the GTT\n");
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006566 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006567 }
6568
Chris Wilsond9e86c02010-11-10 16:40:20 +00006569 ret = i915_gem_object_put_fence(obj);
6570 if (ret) {
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006571 DRM_ERROR("failed to release fence for cursor");
Chris Wilsond9e86c02010-11-10 16:40:20 +00006572 goto fail_unpin;
6573 }
6574
Ben Widawskyf343c5f2013-07-05 14:41:04 -07006575 addr = i915_gem_obj_ggtt_offset(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10006576 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01006577 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson05394f32010-11-08 19:18:58 +00006578 ret = i915_gem_attach_phys_object(dev, obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01006579 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
6580 align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10006581 if (ret) {
6582 DRM_ERROR("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006583 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10006584 }
Chris Wilson05394f32010-11-08 19:18:58 +00006585 addr = obj->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006586 }
6587
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006588 if (IS_GEN2(dev))
Jesse Barnes14b60392009-05-20 16:47:08 -04006589 I915_WRITE(CURSIZE, (height << 12) | width);
6590
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006591 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006592 if (intel_crtc->cursor_bo) {
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05006593 if (dev_priv->info->cursor_needs_physical) {
Chris Wilson05394f32010-11-08 19:18:58 +00006594 if (intel_crtc->cursor_bo != obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10006595 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
6596 } else
6597 i915_gem_object_unpin(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00006598 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006599 }
Jesse Barnes80824002009-09-10 15:28:06 -07006600
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006601 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006602
6603 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00006604 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006605 intel_crtc->cursor_width = width;
6606 intel_crtc->cursor_height = height;
6607
Mika Kuoppala40ccc722013-04-23 17:27:08 +03006608 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006609
Jesse Barnes79e53942008-11-07 14:24:08 -08006610 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006611fail_unpin:
Chris Wilson05394f32010-11-08 19:18:58 +00006612 i915_gem_object_unpin(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006613fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10006614 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00006615fail:
Chris Wilson05394f32010-11-08 19:18:58 +00006616 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10006617 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006618}
6619
6620static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
6621{
Jesse Barnes79e53942008-11-07 14:24:08 -08006622 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006623
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006624 intel_crtc->cursor_x = x;
6625 intel_crtc->cursor_y = y;
Jesse Barnes652c3932009-08-17 13:31:43 -07006626
Mika Kuoppala40ccc722013-04-23 17:27:08 +03006627 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08006628
6629 return 0;
6630}
6631
6632/** Sets the color ramps on behalf of RandR */
6633void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
6634 u16 blue, int regno)
6635{
6636 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6637
6638 intel_crtc->lut_r[regno] = red >> 8;
6639 intel_crtc->lut_g[regno] = green >> 8;
6640 intel_crtc->lut_b[regno] = blue >> 8;
6641}
6642
Dave Airlieb8c00ac2009-10-06 13:54:01 +10006643void intel_crtc_fb_gamma_get(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 *red = intel_crtc->lut_r[regno] << 8;
6649 *green = intel_crtc->lut_g[regno] << 8;
6650 *blue = intel_crtc->lut_b[regno] << 8;
6651}
6652
Jesse Barnes79e53942008-11-07 14:24:08 -08006653static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01006654 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08006655{
James Simmons72034252010-08-03 01:33:19 +01006656 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08006657 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006658
James Simmons72034252010-08-03 01:33:19 +01006659 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006660 intel_crtc->lut_r[i] = red[i] >> 8;
6661 intel_crtc->lut_g[i] = green[i] >> 8;
6662 intel_crtc->lut_b[i] = blue[i] >> 8;
6663 }
6664
6665 intel_crtc_load_lut(crtc);
6666}
6667
Jesse Barnes79e53942008-11-07 14:24:08 -08006668/* VESA 640x480x72Hz mode to set on the pipe */
6669static struct drm_display_mode load_detect_mode = {
6670 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
6671 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
6672};
6673
Chris Wilsond2dff872011-04-19 08:36:26 +01006674static struct drm_framebuffer *
6675intel_framebuffer_create(struct drm_device *dev,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006676 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilsond2dff872011-04-19 08:36:26 +01006677 struct drm_i915_gem_object *obj)
6678{
6679 struct intel_framebuffer *intel_fb;
6680 int ret;
6681
6682 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
6683 if (!intel_fb) {
6684 drm_gem_object_unreference_unlocked(&obj->base);
6685 return ERR_PTR(-ENOMEM);
6686 }
6687
6688 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
6689 if (ret) {
6690 drm_gem_object_unreference_unlocked(&obj->base);
6691 kfree(intel_fb);
6692 return ERR_PTR(ret);
6693 }
6694
6695 return &intel_fb->base;
6696}
6697
6698static u32
6699intel_framebuffer_pitch_for_width(int width, int bpp)
6700{
6701 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
6702 return ALIGN(pitch, 64);
6703}
6704
6705static u32
6706intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
6707{
6708 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
6709 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
6710}
6711
6712static struct drm_framebuffer *
6713intel_framebuffer_create_for_mode(struct drm_device *dev,
6714 struct drm_display_mode *mode,
6715 int depth, int bpp)
6716{
6717 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00006718 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01006719
6720 obj = i915_gem_alloc_object(dev,
6721 intel_framebuffer_size_for_mode(mode, bpp));
6722 if (obj == NULL)
6723 return ERR_PTR(-ENOMEM);
6724
6725 mode_cmd.width = mode->hdisplay;
6726 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006727 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
6728 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00006729 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01006730
6731 return intel_framebuffer_create(dev, &mode_cmd, obj);
6732}
6733
6734static struct drm_framebuffer *
6735mode_fits_in_fbdev(struct drm_device *dev,
6736 struct drm_display_mode *mode)
6737{
6738 struct drm_i915_private *dev_priv = dev->dev_private;
6739 struct drm_i915_gem_object *obj;
6740 struct drm_framebuffer *fb;
6741
6742 if (dev_priv->fbdev == NULL)
6743 return NULL;
6744
6745 obj = dev_priv->fbdev->ifb.obj;
6746 if (obj == NULL)
6747 return NULL;
6748
6749 fb = &dev_priv->fbdev->ifb.base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006750 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
6751 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01006752 return NULL;
6753
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006754 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01006755 return NULL;
6756
6757 return fb;
6758}
6759
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006760bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01006761 struct drm_display_mode *mode,
Chris Wilson8261b192011-04-19 23:18:09 +01006762 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08006763{
6764 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006765 struct intel_encoder *intel_encoder =
6766 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08006767 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01006768 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08006769 struct drm_crtc *crtc = NULL;
6770 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02006771 struct drm_framebuffer *fb;
Jesse Barnes79e53942008-11-07 14:24:08 -08006772 int i = -1;
6773
Chris Wilsond2dff872011-04-19 08:36:26 +01006774 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6775 connector->base.id, drm_get_connector_name(connector),
6776 encoder->base.id, drm_get_encoder_name(encoder));
6777
Jesse Barnes79e53942008-11-07 14:24:08 -08006778 /*
6779 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01006780 *
Jesse Barnes79e53942008-11-07 14:24:08 -08006781 * - if the connector already has an assigned crtc, use it (but make
6782 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01006783 *
Jesse Barnes79e53942008-11-07 14:24:08 -08006784 * - try to find the first unused crtc that can drive this connector,
6785 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08006786 */
6787
6788 /* See if we already have a CRTC for this connector */
6789 if (encoder->crtc) {
6790 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01006791
Daniel Vetter7b240562012-12-12 00:35:33 +01006792 mutex_lock(&crtc->mutex);
6793
Daniel Vetter24218aa2012-08-12 19:27:11 +02006794 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01006795 old->load_detect_temp = false;
6796
6797 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02006798 if (connector->dpms != DRM_MODE_DPMS_ON)
6799 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01006800
Chris Wilson71731882011-04-19 23:10:58 +01006801 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08006802 }
6803
6804 /* Find an unused one (if possible) */
6805 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
6806 i++;
6807 if (!(encoder->possible_crtcs & (1 << i)))
6808 continue;
6809 if (!possible_crtc->enabled) {
6810 crtc = possible_crtc;
6811 break;
6812 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006813 }
6814
6815 /*
6816 * If we didn't find an unused CRTC, don't use any.
6817 */
6818 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01006819 DRM_DEBUG_KMS("no pipe available for load-detect\n");
6820 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006821 }
6822
Daniel Vetter7b240562012-12-12 00:35:33 +01006823 mutex_lock(&crtc->mutex);
Daniel Vetterfc303102012-07-09 10:40:58 +02006824 intel_encoder->new_crtc = to_intel_crtc(crtc);
6825 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006826
6827 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter24218aa2012-08-12 19:27:11 +02006828 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01006829 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01006830 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08006831
Chris Wilson64927112011-04-20 07:25:26 +01006832 if (!mode)
6833 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08006834
Chris Wilsond2dff872011-04-19 08:36:26 +01006835 /* We need a framebuffer large enough to accommodate all accesses
6836 * that the plane may generate whilst we perform load detection.
6837 * We can not rely on the fbcon either being present (we get called
6838 * during its initialisation to detect all boot displays, or it may
6839 * not even exist) or that it is large enough to satisfy the
6840 * requested mode.
6841 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02006842 fb = mode_fits_in_fbdev(dev, mode);
6843 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01006844 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02006845 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
6846 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01006847 } else
6848 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02006849 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01006850 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Daniel Vetter7b240562012-12-12 00:35:33 +01006851 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00006852 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006853 }
Chris Wilsond2dff872011-04-19 08:36:26 +01006854
Chris Wilsonc0c36b942012-12-19 16:08:43 +00006855 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01006856 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01006857 if (old->release_fb)
6858 old->release_fb->funcs->destroy(old->release_fb);
Daniel Vetter7b240562012-12-12 00:35:33 +01006859 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00006860 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006861 }
Chris Wilson71731882011-04-19 23:10:58 +01006862
Jesse Barnes79e53942008-11-07 14:24:08 -08006863 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006864 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01006865 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08006866}
6867
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006868void intel_release_load_detect_pipe(struct drm_connector *connector,
Chris Wilson8261b192011-04-19 23:18:09 +01006869 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08006870{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006871 struct intel_encoder *intel_encoder =
6872 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01006873 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01006874 struct drm_crtc *crtc = encoder->crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -08006875
Chris Wilsond2dff872011-04-19 08:36:26 +01006876 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6877 connector->base.id, drm_get_connector_name(connector),
6878 encoder->base.id, drm_get_encoder_name(encoder));
6879
Chris Wilson8261b192011-04-19 23:18:09 +01006880 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02006881 to_intel_connector(connector)->new_encoder = NULL;
6882 intel_encoder->new_crtc = NULL;
6883 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01006884
Daniel Vetter36206362012-12-10 20:42:17 +01006885 if (old->release_fb) {
6886 drm_framebuffer_unregister_private(old->release_fb);
6887 drm_framebuffer_unreference(old->release_fb);
6888 }
Chris Wilsond2dff872011-04-19 08:36:26 +01006889
Daniel Vetter67c96402013-01-23 16:25:09 +00006890 mutex_unlock(&crtc->mutex);
Chris Wilson0622a532011-04-21 09:32:11 +01006891 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08006892 }
6893
Eric Anholtc751ce42010-03-25 11:48:48 -07006894 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02006895 if (old->dpms_mode != DRM_MODE_DPMS_ON)
6896 connector->funcs->dpms(connector, old->dpms_mode);
Daniel Vetter7b240562012-12-12 00:35:33 +01006897
6898 mutex_unlock(&crtc->mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08006899}
6900
6901/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03006902static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
6903 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006904{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03006905 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08006906 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03006907 int pipe = pipe_config->cpu_transcoder;
Jesse Barnes548f2452011-02-17 10:40:53 -08006908 u32 dpll = I915_READ(DPLL(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006909 u32 fp;
6910 intel_clock_t clock;
6911
6912 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Chris Wilson39adb7a2011-04-22 22:17:21 +01006913 fp = I915_READ(FP0(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006914 else
Chris Wilson39adb7a2011-04-22 22:17:21 +01006915 fp = I915_READ(FP1(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006916
6917 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006918 if (IS_PINEVIEW(dev)) {
6919 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
6920 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08006921 } else {
6922 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
6923 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
6924 }
6925
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006926 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006927 if (IS_PINEVIEW(dev))
6928 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
6929 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08006930 else
6931 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08006932 DPLL_FPA01_P1_POST_DIV_SHIFT);
6933
6934 switch (dpll & DPLL_MODE_MASK) {
6935 case DPLLB_MODE_DAC_SERIAL:
6936 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
6937 5 : 10;
6938 break;
6939 case DPLLB_MODE_LVDS:
6940 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
6941 7 : 14;
6942 break;
6943 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08006944 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08006945 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03006946 pipe_config->adjusted_mode.clock = 0;
6947 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08006948 }
6949
Daniel Vetterac58c3f2013-06-01 17:16:17 +02006950 if (IS_PINEVIEW(dev))
6951 pineview_clock(96000, &clock);
6952 else
6953 i9xx_clock(96000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006954 } else {
6955 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
6956
6957 if (is_lvds) {
6958 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
6959 DPLL_FPA01_P1_POST_DIV_SHIFT);
6960 clock.p2 = 14;
6961
6962 if ((dpll & PLL_REF_INPUT_MASK) ==
6963 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
6964 /* XXX: might not be 66MHz */
Daniel Vetterac58c3f2013-06-01 17:16:17 +02006965 i9xx_clock(66000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006966 } else
Daniel Vetterac58c3f2013-06-01 17:16:17 +02006967 i9xx_clock(48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006968 } else {
6969 if (dpll & PLL_P1_DIVIDE_BY_TWO)
6970 clock.p1 = 2;
6971 else {
6972 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
6973 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
6974 }
6975 if (dpll & PLL_P2_DIVIDE_BY_4)
6976 clock.p2 = 4;
6977 else
6978 clock.p2 = 2;
6979
Daniel Vetterac58c3f2013-06-01 17:16:17 +02006980 i9xx_clock(48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006981 }
6982 }
6983
Jesse Barnesf1f644d2013-06-27 00:39:25 +03006984 pipe_config->adjusted_mode.clock = clock.dot *
6985 pipe_config->pixel_multiplier;
6986}
6987
6988static void ironlake_crtc_clock_get(struct intel_crtc *crtc,
6989 struct intel_crtc_config *pipe_config)
6990{
6991 struct drm_device *dev = crtc->base.dev;
6992 struct drm_i915_private *dev_priv = dev->dev_private;
6993 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
6994 int link_freq, repeat;
6995 u64 clock;
6996 u32 link_m, link_n;
6997
6998 repeat = pipe_config->pixel_multiplier;
6999
7000 /*
7001 * The calculation for the data clock is:
7002 * pixel_clock = ((m/n)*(link_clock * nr_lanes * repeat))/bpp
7003 * But we want to avoid losing precison if possible, so:
7004 * pixel_clock = ((m * link_clock * nr_lanes * repeat)/(n*bpp))
7005 *
7006 * and the link clock is simpler:
7007 * link_clock = (m * link_clock * repeat) / n
Jesse Barnes79e53942008-11-07 14:24:08 -08007008 */
7009
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007010 /*
7011 * We need to get the FDI or DP link clock here to derive
7012 * the M/N dividers.
7013 *
7014 * For FDI, we read it from the BIOS or use a fixed 2.7GHz.
7015 * For DP, it's either 1.62GHz or 2.7GHz.
7016 * We do our calculations in 10*MHz since we don't need much precison.
7017 */
7018 if (pipe_config->has_pch_encoder)
7019 link_freq = intel_fdi_link_freq(dev) * 10000;
7020 else
7021 link_freq = pipe_config->port_clock;
7022
7023 link_m = I915_READ(PIPE_LINK_M1(cpu_transcoder));
7024 link_n = I915_READ(PIPE_LINK_N1(cpu_transcoder));
7025
7026 if (!link_m || !link_n)
7027 return;
7028
7029 clock = ((u64)link_m * (u64)link_freq * (u64)repeat);
7030 do_div(clock, link_n);
7031
7032 pipe_config->adjusted_mode.clock = clock;
Jesse Barnes79e53942008-11-07 14:24:08 -08007033}
7034
7035/** Returns the currently programmed mode of the given pipe. */
7036struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
7037 struct drm_crtc *crtc)
7038{
Jesse Barnes548f2452011-02-17 10:40:53 -08007039 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08007040 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02007041 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08007042 struct drm_display_mode *mode;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007043 struct intel_crtc_config pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007044 int htot = I915_READ(HTOTAL(cpu_transcoder));
7045 int hsync = I915_READ(HSYNC(cpu_transcoder));
7046 int vtot = I915_READ(VTOTAL(cpu_transcoder));
7047 int vsync = I915_READ(VSYNC(cpu_transcoder));
Jesse Barnes79e53942008-11-07 14:24:08 -08007048
7049 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
7050 if (!mode)
7051 return NULL;
7052
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007053 /*
7054 * Construct a pipe_config sufficient for getting the clock info
7055 * back out of crtc_clock_get.
7056 *
7057 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
7058 * to use a real value here instead.
7059 */
Daniel Vettere143a212013-07-04 12:01:15 +02007060 pipe_config.cpu_transcoder = (enum transcoder) intel_crtc->pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007061 pipe_config.pixel_multiplier = 1;
7062 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
7063
7064 mode->clock = pipe_config.adjusted_mode.clock;
Jesse Barnes79e53942008-11-07 14:24:08 -08007065 mode->hdisplay = (htot & 0xffff) + 1;
7066 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
7067 mode->hsync_start = (hsync & 0xffff) + 1;
7068 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
7069 mode->vdisplay = (vtot & 0xffff) + 1;
7070 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
7071 mode->vsync_start = (vsync & 0xffff) + 1;
7072 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
7073
7074 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08007075
7076 return mode;
7077}
7078
Daniel Vetter3dec0092010-08-20 21:40:52 +02007079static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07007080{
7081 struct drm_device *dev = crtc->dev;
7082 drm_i915_private_t *dev_priv = dev->dev_private;
7083 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7084 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007085 int dpll_reg = DPLL(pipe);
7086 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07007087
Eric Anholtbad720f2009-10-22 16:11:14 -07007088 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07007089 return;
7090
7091 if (!dev_priv->lvds_downclock_avail)
7092 return;
7093
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007094 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07007095 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08007096 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007097
Sean Paul8ac5a6d2012-02-13 13:14:51 -05007098 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007099
7100 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
7101 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007102 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007103
Jesse Barnes652c3932009-08-17 13:31:43 -07007104 dpll = I915_READ(dpll_reg);
7105 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08007106 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007107 }
Jesse Barnes652c3932009-08-17 13:31:43 -07007108}
7109
7110static void intel_decrease_pllclock(struct drm_crtc *crtc)
7111{
7112 struct drm_device *dev = crtc->dev;
7113 drm_i915_private_t *dev_priv = dev->dev_private;
7114 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07007115
Eric Anholtbad720f2009-10-22 16:11:14 -07007116 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07007117 return;
7118
7119 if (!dev_priv->lvds_downclock_avail)
7120 return;
7121
7122 /*
7123 * Since this is called by a timer, we should never get here in
7124 * the manual case.
7125 */
7126 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01007127 int pipe = intel_crtc->pipe;
7128 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02007129 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01007130
Zhao Yakui44d98a62009-10-09 11:39:40 +08007131 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007132
Sean Paul8ac5a6d2012-02-13 13:14:51 -05007133 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007134
Chris Wilson074b5e12012-05-02 12:07:06 +01007135 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07007136 dpll |= DISPLAY_RATE_SELECT_FPA1;
7137 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007138 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007139 dpll = I915_READ(dpll_reg);
7140 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08007141 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007142 }
7143
7144}
7145
Chris Wilsonf047e392012-07-21 12:31:41 +01007146void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07007147{
Chris Wilsonf047e392012-07-21 12:31:41 +01007148 i915_update_gfx_val(dev->dev_private);
7149}
7150
7151void intel_mark_idle(struct drm_device *dev)
7152{
Chris Wilson725a5b52013-01-08 11:02:57 +00007153 struct drm_crtc *crtc;
7154
7155 if (!i915_powersave)
7156 return;
7157
7158 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
7159 if (!crtc->fb)
7160 continue;
7161
7162 intel_decrease_pllclock(crtc);
7163 }
Chris Wilsonf047e392012-07-21 12:31:41 +01007164}
7165
Chris Wilsonc65355b2013-06-06 16:53:41 -03007166void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
7167 struct intel_ring_buffer *ring)
Chris Wilsonf047e392012-07-21 12:31:41 +01007168{
7169 struct drm_device *dev = obj->base.dev;
Jesse Barnes652c3932009-08-17 13:31:43 -07007170 struct drm_crtc *crtc;
Jesse Barnes652c3932009-08-17 13:31:43 -07007171
7172 if (!i915_powersave)
7173 return;
7174
Jesse Barnes652c3932009-08-17 13:31:43 -07007175 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Jesse Barnes652c3932009-08-17 13:31:43 -07007176 if (!crtc->fb)
7177 continue;
7178
Chris Wilsonc65355b2013-06-06 16:53:41 -03007179 if (to_intel_framebuffer(crtc->fb)->obj != obj)
7180 continue;
7181
7182 intel_increase_pllclock(crtc);
7183 if (ring && intel_fbc_enabled(dev))
7184 ring->fbc_dirty = true;
Jesse Barnes652c3932009-08-17 13:31:43 -07007185 }
Jesse Barnes652c3932009-08-17 13:31:43 -07007186}
7187
Jesse Barnes79e53942008-11-07 14:24:08 -08007188static void intel_crtc_destroy(struct drm_crtc *crtc)
7189{
7190 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02007191 struct drm_device *dev = crtc->dev;
7192 struct intel_unpin_work *work;
7193 unsigned long flags;
7194
7195 spin_lock_irqsave(&dev->event_lock, flags);
7196 work = intel_crtc->unpin_work;
7197 intel_crtc->unpin_work = NULL;
7198 spin_unlock_irqrestore(&dev->event_lock, flags);
7199
7200 if (work) {
7201 cancel_work_sync(&work->work);
7202 kfree(work);
7203 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007204
Mika Kuoppala40ccc722013-04-23 17:27:08 +03007205 intel_crtc_cursor_set(crtc, NULL, 0, 0, 0);
7206
Jesse Barnes79e53942008-11-07 14:24:08 -08007207 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02007208
Jesse Barnes79e53942008-11-07 14:24:08 -08007209 kfree(intel_crtc);
7210}
7211
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007212static void intel_unpin_work_fn(struct work_struct *__work)
7213{
7214 struct intel_unpin_work *work =
7215 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007216 struct drm_device *dev = work->crtc->dev;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007217
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007218 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01007219 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00007220 drm_gem_object_unreference(&work->pending_flip_obj->base);
7221 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00007222
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007223 intel_update_fbc(dev);
7224 mutex_unlock(&dev->struct_mutex);
7225
7226 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
7227 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
7228
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007229 kfree(work);
7230}
7231
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007232static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01007233 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007234{
7235 drm_i915_private_t *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007236 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7237 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007238 unsigned long flags;
7239
7240 /* Ignore early vblank irqs */
7241 if (intel_crtc == NULL)
7242 return;
7243
7244 spin_lock_irqsave(&dev->event_lock, flags);
7245 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00007246
7247 /* Ensure we don't miss a work->pending update ... */
7248 smp_rmb();
7249
7250 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007251 spin_unlock_irqrestore(&dev->event_lock, flags);
7252 return;
7253 }
7254
Chris Wilsone7d841c2012-12-03 11:36:30 +00007255 /* and that the unpin work is consistent wrt ->pending. */
7256 smp_rmb();
7257
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007258 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007259
Rob Clark45a066e2012-10-08 14:50:40 -05007260 if (work->event)
7261 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007262
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01007263 drm_vblank_put(dev, intel_crtc->pipe);
7264
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007265 spin_unlock_irqrestore(&dev->event_lock, flags);
7266
Daniel Vetter2c10d572012-12-20 21:24:07 +01007267 wake_up_all(&dev_priv->pending_flip_queue);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007268
7269 queue_work(dev_priv->wq, &work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07007270
7271 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007272}
7273
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007274void intel_finish_page_flip(struct drm_device *dev, int pipe)
7275{
7276 drm_i915_private_t *dev_priv = dev->dev_private;
7277 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
7278
Mario Kleiner49b14a52010-12-09 07:00:07 +01007279 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007280}
7281
7282void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
7283{
7284 drm_i915_private_t *dev_priv = dev->dev_private;
7285 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
7286
Mario Kleiner49b14a52010-12-09 07:00:07 +01007287 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007288}
7289
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007290void intel_prepare_page_flip(struct drm_device *dev, int plane)
7291{
7292 drm_i915_private_t *dev_priv = dev->dev_private;
7293 struct intel_crtc *intel_crtc =
7294 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
7295 unsigned long flags;
7296
Chris Wilsone7d841c2012-12-03 11:36:30 +00007297 /* NB: An MMIO update of the plane base pointer will also
7298 * generate a page-flip completion irq, i.e. every modeset
7299 * is also accompanied by a spurious intel_prepare_page_flip().
7300 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007301 spin_lock_irqsave(&dev->event_lock, flags);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007302 if (intel_crtc->unpin_work)
7303 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007304 spin_unlock_irqrestore(&dev->event_lock, flags);
7305}
7306
Chris Wilsone7d841c2012-12-03 11:36:30 +00007307inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
7308{
7309 /* Ensure that the work item is consistent when activating it ... */
7310 smp_wmb();
7311 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
7312 /* and that it is marked active as soon as the irq could fire. */
7313 smp_wmb();
7314}
7315
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007316static int intel_gen2_queue_flip(struct drm_device *dev,
7317 struct drm_crtc *crtc,
7318 struct drm_framebuffer *fb,
7319 struct drm_i915_gem_object *obj)
7320{
7321 struct drm_i915_private *dev_priv = dev->dev_private;
7322 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007323 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007324 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007325 int ret;
7326
Daniel Vetter6d90c952012-04-26 23:28:05 +02007327 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007328 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007329 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007330
Daniel Vetter6d90c952012-04-26 23:28:05 +02007331 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007332 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007333 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007334
7335 /* Can't queue multiple flips, so wait for the previous
7336 * one to finish before executing the next.
7337 */
7338 if (intel_crtc->plane)
7339 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7340 else
7341 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007342 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7343 intel_ring_emit(ring, MI_NOOP);
7344 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7345 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7346 intel_ring_emit(ring, fb->pitches[0]);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007347 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007348 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00007349
7350 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007351 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007352 return 0;
7353
7354err_unpin:
7355 intel_unpin_fb_obj(obj);
7356err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007357 return ret;
7358}
7359
7360static int intel_gen3_queue_flip(struct drm_device *dev,
7361 struct drm_crtc *crtc,
7362 struct drm_framebuffer *fb,
7363 struct drm_i915_gem_object *obj)
7364{
7365 struct drm_i915_private *dev_priv = dev->dev_private;
7366 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007367 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007368 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007369 int ret;
7370
Daniel Vetter6d90c952012-04-26 23:28:05 +02007371 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007372 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007373 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007374
Daniel Vetter6d90c952012-04-26 23:28:05 +02007375 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007376 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007377 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007378
7379 if (intel_crtc->plane)
7380 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7381 else
7382 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007383 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7384 intel_ring_emit(ring, MI_NOOP);
7385 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
7386 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7387 intel_ring_emit(ring, fb->pitches[0]);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007388 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007389 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007390
Chris Wilsone7d841c2012-12-03 11:36:30 +00007391 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007392 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007393 return 0;
7394
7395err_unpin:
7396 intel_unpin_fb_obj(obj);
7397err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007398 return ret;
7399}
7400
7401static int intel_gen4_queue_flip(struct drm_device *dev,
7402 struct drm_crtc *crtc,
7403 struct drm_framebuffer *fb,
7404 struct drm_i915_gem_object *obj)
7405{
7406 struct drm_i915_private *dev_priv = dev->dev_private;
7407 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7408 uint32_t pf, pipesrc;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007409 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007410 int ret;
7411
Daniel Vetter6d90c952012-04-26 23:28:05 +02007412 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007413 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007414 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007415
Daniel Vetter6d90c952012-04-26 23:28:05 +02007416 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007417 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007418 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007419
7420 /* i965+ uses the linear or tiled offsets from the
7421 * Display Registers (which do not change across a page-flip)
7422 * so we need only reprogram the base address.
7423 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02007424 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7425 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7426 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02007427 intel_ring_emit(ring,
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007428 (i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset) |
Daniel Vetterc2c75132012-07-05 12:17:30 +02007429 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007430
7431 /* XXX Enabling the panel-fitter across page-flip is so far
7432 * untested on non-native modes, so ignore it for now.
7433 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
7434 */
7435 pf = 0;
7436 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007437 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007438
7439 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007440 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007441 return 0;
7442
7443err_unpin:
7444 intel_unpin_fb_obj(obj);
7445err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007446 return ret;
7447}
7448
7449static int intel_gen6_queue_flip(struct drm_device *dev,
7450 struct drm_crtc *crtc,
7451 struct drm_framebuffer *fb,
7452 struct drm_i915_gem_object *obj)
7453{
7454 struct drm_i915_private *dev_priv = dev->dev_private;
7455 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007456 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007457 uint32_t pf, pipesrc;
7458 int ret;
7459
Daniel Vetter6d90c952012-04-26 23:28:05 +02007460 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007461 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007462 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007463
Daniel Vetter6d90c952012-04-26 23:28:05 +02007464 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007465 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007466 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007467
Daniel Vetter6d90c952012-04-26 23:28:05 +02007468 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7469 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7470 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007471 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007472
Chris Wilson99d9acd2012-04-17 20:37:00 +01007473 /* Contrary to the suggestions in the documentation,
7474 * "Enable Panel Fitter" does not seem to be required when page
7475 * flipping with a non-native mode, and worse causes a normal
7476 * modeset to fail.
7477 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
7478 */
7479 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007480 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007481 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007482
7483 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007484 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007485 return 0;
7486
7487err_unpin:
7488 intel_unpin_fb_obj(obj);
7489err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007490 return ret;
7491}
7492
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007493/*
7494 * On gen7 we currently use the blit ring because (in early silicon at least)
7495 * the render ring doesn't give us interrpts for page flip completion, which
7496 * means clients will hang after the first flip is queued. Fortunately the
7497 * blit ring generates interrupts properly, so use it instead.
7498 */
7499static int intel_gen7_queue_flip(struct drm_device *dev,
7500 struct drm_crtc *crtc,
7501 struct drm_framebuffer *fb,
7502 struct drm_i915_gem_object *obj)
7503{
7504 struct drm_i915_private *dev_priv = dev->dev_private;
7505 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7506 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007507 uint32_t plane_bit = 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007508 int ret;
7509
7510 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7511 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007512 goto err;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007513
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007514 switch(intel_crtc->plane) {
7515 case PLANE_A:
7516 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
7517 break;
7518 case PLANE_B:
7519 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
7520 break;
7521 case PLANE_C:
7522 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
7523 break;
7524 default:
7525 WARN_ONCE(1, "unknown plane in flip command\n");
7526 ret = -ENODEV;
Eugeni Dodonovab3951e2012-06-18 19:03:38 -03007527 goto err_unpin;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007528 }
7529
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007530 ret = intel_ring_begin(ring, 4);
7531 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007532 goto err_unpin;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007533
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007534 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007535 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ben Widawskyf343c5f2013-07-05 14:41:04 -07007536 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007537 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +00007538
7539 intel_mark_page_flip_active(intel_crtc);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007540 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007541 return 0;
7542
7543err_unpin:
7544 intel_unpin_fb_obj(obj);
7545err:
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007546 return ret;
7547}
7548
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007549static int intel_default_queue_flip(struct drm_device *dev,
7550 struct drm_crtc *crtc,
7551 struct drm_framebuffer *fb,
7552 struct drm_i915_gem_object *obj)
7553{
7554 return -ENODEV;
7555}
7556
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007557static int intel_crtc_page_flip(struct drm_crtc *crtc,
7558 struct drm_framebuffer *fb,
7559 struct drm_pending_vblank_event *event)
7560{
7561 struct drm_device *dev = crtc->dev;
7562 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02007563 struct drm_framebuffer *old_fb = crtc->fb;
7564 struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007565 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7566 struct intel_unpin_work *work;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007567 unsigned long flags;
Chris Wilson52e68632010-08-08 10:15:59 +01007568 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007569
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03007570 /* Can't change pixel format via MI display flips. */
7571 if (fb->pixel_format != crtc->fb->pixel_format)
7572 return -EINVAL;
7573
7574 /*
7575 * TILEOFF/LINOFF registers can't be changed via MI display flips.
7576 * Note that pitch changes could also affect these register.
7577 */
7578 if (INTEL_INFO(dev)->gen > 3 &&
7579 (fb->offsets[0] != crtc->fb->offsets[0] ||
7580 fb->pitches[0] != crtc->fb->pitches[0]))
7581 return -EINVAL;
7582
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007583 work = kzalloc(sizeof *work, GFP_KERNEL);
7584 if (work == NULL)
7585 return -ENOMEM;
7586
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007587 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007588 work->crtc = crtc;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02007589 work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007590 INIT_WORK(&work->work, intel_unpin_work_fn);
7591
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007592 ret = drm_vblank_get(dev, intel_crtc->pipe);
7593 if (ret)
7594 goto free_work;
7595
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007596 /* We borrow the event spin lock for protecting unpin_work */
7597 spin_lock_irqsave(&dev->event_lock, flags);
7598 if (intel_crtc->unpin_work) {
7599 spin_unlock_irqrestore(&dev->event_lock, flags);
7600 kfree(work);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007601 drm_vblank_put(dev, intel_crtc->pipe);
Chris Wilson468f0b42010-05-27 13:18:13 +01007602
7603 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007604 return -EBUSY;
7605 }
7606 intel_crtc->unpin_work = work;
7607 spin_unlock_irqrestore(&dev->event_lock, flags);
7608
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007609 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
7610 flush_workqueue(dev_priv->wq);
7611
Chris Wilson79158102012-05-23 11:13:58 +01007612 ret = i915_mutex_lock_interruptible(dev);
7613 if (ret)
7614 goto cleanup;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007615
Jesse Barnes75dfca82010-02-10 15:09:44 -08007616 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00007617 drm_gem_object_reference(&work->old_fb_obj->base);
7618 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007619
7620 crtc->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01007621
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007622 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007623
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01007624 work->enable_stall_check = true;
7625
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007626 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +02007627 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007628
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007629 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
7630 if (ret)
7631 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007632
Chris Wilson7782de32011-07-08 12:22:41 +01007633 intel_disable_fbc(dev);
Chris Wilsonc65355b2013-06-06 16:53:41 -03007634 intel_mark_fb_busy(obj, NULL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007635 mutex_unlock(&dev->struct_mutex);
7636
Jesse Barnese5510fa2010-07-01 16:48:37 -07007637 trace_i915_flip_request(intel_crtc->plane, obj);
7638
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007639 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01007640
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007641cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007642 atomic_dec(&intel_crtc->unpin_work_count);
Ville Syrjälä4a35f832013-02-22 16:53:38 +02007643 crtc->fb = old_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00007644 drm_gem_object_unreference(&work->old_fb_obj->base);
7645 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01007646 mutex_unlock(&dev->struct_mutex);
7647
Chris Wilson79158102012-05-23 11:13:58 +01007648cleanup:
Chris Wilson96b099f2010-06-07 14:03:04 +01007649 spin_lock_irqsave(&dev->event_lock, flags);
7650 intel_crtc->unpin_work = NULL;
7651 spin_unlock_irqrestore(&dev->event_lock, flags);
7652
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007653 drm_vblank_put(dev, intel_crtc->pipe);
7654free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01007655 kfree(work);
7656
7657 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007658}
7659
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007660static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007661 .mode_set_base_atomic = intel_pipe_set_base_atomic,
7662 .load_lut = intel_crtc_load_lut,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007663};
7664
Daniel Vetter50f56112012-07-02 09:35:43 +02007665static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
7666 struct drm_crtc *crtc)
7667{
7668 struct drm_device *dev;
7669 struct drm_crtc *tmp;
7670 int crtc_mask = 1;
7671
7672 WARN(!crtc, "checking null crtc?\n");
7673
7674 dev = crtc->dev;
7675
7676 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
7677 if (tmp == crtc)
7678 break;
7679 crtc_mask <<= 1;
7680 }
7681
7682 if (encoder->possible_crtcs & crtc_mask)
7683 return true;
7684 return false;
7685}
7686
Daniel Vetter9a935852012-07-05 22:34:27 +02007687/**
7688 * intel_modeset_update_staged_output_state
7689 *
7690 * Updates the staged output configuration state, e.g. after we've read out the
7691 * current hw state.
7692 */
7693static void intel_modeset_update_staged_output_state(struct drm_device *dev)
7694{
7695 struct intel_encoder *encoder;
7696 struct intel_connector *connector;
7697
7698 list_for_each_entry(connector, &dev->mode_config.connector_list,
7699 base.head) {
7700 connector->new_encoder =
7701 to_intel_encoder(connector->base.encoder);
7702 }
7703
7704 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7705 base.head) {
7706 encoder->new_crtc =
7707 to_intel_crtc(encoder->base.crtc);
7708 }
7709}
7710
7711/**
7712 * intel_modeset_commit_output_state
7713 *
7714 * This function copies the stage display pipe configuration to the real one.
7715 */
7716static void intel_modeset_commit_output_state(struct drm_device *dev)
7717{
7718 struct intel_encoder *encoder;
7719 struct intel_connector *connector;
7720
7721 list_for_each_entry(connector, &dev->mode_config.connector_list,
7722 base.head) {
7723 connector->base.encoder = &connector->new_encoder->base;
7724 }
7725
7726 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7727 base.head) {
7728 encoder->base.crtc = &encoder->new_crtc->base;
7729 }
7730}
7731
Daniel Vetter050f7ae2013-06-02 13:26:23 +02007732static void
7733connected_sink_compute_bpp(struct intel_connector * connector,
7734 struct intel_crtc_config *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007735{
Daniel Vetter050f7ae2013-06-02 13:26:23 +02007736 int bpp = pipe_config->pipe_bpp;
7737
7738 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
7739 connector->base.base.id,
7740 drm_get_connector_name(&connector->base));
7741
7742 /* Don't use an invalid EDID bpc value */
7743 if (connector->base.display_info.bpc &&
7744 connector->base.display_info.bpc * 3 < bpp) {
7745 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
7746 bpp, connector->base.display_info.bpc*3);
7747 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
7748 }
7749
7750 /* Clamp bpp to 8 on screens without EDID 1.4 */
7751 if (connector->base.display_info.bpc == 0 && bpp > 24) {
7752 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
7753 bpp);
7754 pipe_config->pipe_bpp = 24;
7755 }
7756}
7757
7758static int
7759compute_baseline_pipe_bpp(struct intel_crtc *crtc,
7760 struct drm_framebuffer *fb,
7761 struct intel_crtc_config *pipe_config)
7762{
7763 struct drm_device *dev = crtc->base.dev;
7764 struct intel_connector *connector;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007765 int bpp;
7766
Daniel Vetterd42264b2013-03-28 16:38:08 +01007767 switch (fb->pixel_format) {
7768 case DRM_FORMAT_C8:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007769 bpp = 8*3; /* since we go through a colormap */
7770 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01007771 case DRM_FORMAT_XRGB1555:
7772 case DRM_FORMAT_ARGB1555:
7773 /* checked in intel_framebuffer_init already */
7774 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
7775 return -EINVAL;
7776 case DRM_FORMAT_RGB565:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007777 bpp = 6*3; /* min is 18bpp */
7778 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01007779 case DRM_FORMAT_XBGR8888:
7780 case DRM_FORMAT_ABGR8888:
7781 /* checked in intel_framebuffer_init already */
7782 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
7783 return -EINVAL;
7784 case DRM_FORMAT_XRGB8888:
7785 case DRM_FORMAT_ARGB8888:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007786 bpp = 8*3;
7787 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01007788 case DRM_FORMAT_XRGB2101010:
7789 case DRM_FORMAT_ARGB2101010:
7790 case DRM_FORMAT_XBGR2101010:
7791 case DRM_FORMAT_ABGR2101010:
7792 /* checked in intel_framebuffer_init already */
7793 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
Daniel Vetterbaba1332013-03-27 00:45:00 +01007794 return -EINVAL;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007795 bpp = 10*3;
7796 break;
Daniel Vetterbaba1332013-03-27 00:45:00 +01007797 /* TODO: gen4+ supports 16 bpc floating point, too. */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007798 default:
7799 DRM_DEBUG_KMS("unsupported depth\n");
7800 return -EINVAL;
7801 }
7802
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007803 pipe_config->pipe_bpp = bpp;
7804
7805 /* Clamp display bpp to EDID value */
7806 list_for_each_entry(connector, &dev->mode_config.connector_list,
Daniel Vetter050f7ae2013-06-02 13:26:23 +02007807 base.head) {
Daniel Vetter1b829e02013-06-02 13:26:24 +02007808 if (!connector->new_encoder ||
7809 connector->new_encoder->new_crtc != crtc)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007810 continue;
7811
Daniel Vetter050f7ae2013-06-02 13:26:23 +02007812 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007813 }
7814
7815 return bpp;
7816}
7817
Daniel Vetterc0b03412013-05-28 12:05:54 +02007818static void intel_dump_pipe_config(struct intel_crtc *crtc,
7819 struct intel_crtc_config *pipe_config,
7820 const char *context)
7821{
7822 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
7823 context, pipe_name(crtc->pipe));
7824
7825 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
7826 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
7827 pipe_config->pipe_bpp, pipe_config->dither);
7828 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
7829 pipe_config->has_pch_encoder,
7830 pipe_config->fdi_lanes,
7831 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
7832 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
7833 pipe_config->fdi_m_n.tu);
7834 DRM_DEBUG_KMS("requested mode:\n");
7835 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
7836 DRM_DEBUG_KMS("adjusted mode:\n");
7837 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
7838 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
7839 pipe_config->gmch_pfit.control,
7840 pipe_config->gmch_pfit.pgm_ratios,
7841 pipe_config->gmch_pfit.lvds_border_bits);
7842 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x\n",
7843 pipe_config->pch_pfit.pos,
7844 pipe_config->pch_pfit.size);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03007845 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Daniel Vetterc0b03412013-05-28 12:05:54 +02007846}
7847
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02007848static bool check_encoder_cloning(struct drm_crtc *crtc)
7849{
7850 int num_encoders = 0;
7851 bool uncloneable_encoders = false;
7852 struct intel_encoder *encoder;
7853
7854 list_for_each_entry(encoder, &crtc->dev->mode_config.encoder_list,
7855 base.head) {
7856 if (&encoder->new_crtc->base != crtc)
7857 continue;
7858
7859 num_encoders++;
7860 if (!encoder->cloneable)
7861 uncloneable_encoders = true;
7862 }
7863
7864 return !(num_encoders > 1 && uncloneable_encoders);
7865}
7866
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007867static struct intel_crtc_config *
7868intel_modeset_pipe_config(struct drm_crtc *crtc,
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007869 struct drm_framebuffer *fb,
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007870 struct drm_display_mode *mode)
Daniel Vetter7758a112012-07-08 19:40:39 +02007871{
7872 struct drm_device *dev = crtc->dev;
Daniel Vetter7758a112012-07-08 19:40:39 +02007873 struct drm_encoder_helper_funcs *encoder_funcs;
7874 struct intel_encoder *encoder;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007875 struct intel_crtc_config *pipe_config;
Daniel Vettere29c22c2013-02-21 00:00:16 +01007876 int plane_bpp, ret = -EINVAL;
7877 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +02007878
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02007879 if (!check_encoder_cloning(crtc)) {
7880 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
7881 return ERR_PTR(-EINVAL);
7882 }
7883
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007884 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7885 if (!pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +02007886 return ERR_PTR(-ENOMEM);
7887
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007888 drm_mode_copy(&pipe_config->adjusted_mode, mode);
7889 drm_mode_copy(&pipe_config->requested_mode, mode);
Daniel Vettere143a212013-07-04 12:01:15 +02007890 pipe_config->cpu_transcoder =
7891 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007892 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007893
Daniel Vetter050f7ae2013-06-02 13:26:23 +02007894 /* Compute a starting value for pipe_config->pipe_bpp taking the source
7895 * plane pixel format and any sink constraints into account. Returns the
7896 * source plane bpp so that dithering can be selected on mismatches
7897 * after encoders and crtc also have had their say. */
7898 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
7899 fb, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007900 if (plane_bpp < 0)
7901 goto fail;
7902
Daniel Vettere29c22c2013-02-21 00:00:16 +01007903encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +02007904 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +02007905 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +02007906 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +02007907
Daniel Vetter7758a112012-07-08 19:40:39 +02007908 /* Pass our mode to the connectors and the CRTC to give them a chance to
7909 * adjust it according to limitations or connector properties, and also
7910 * a chance to reject the mode entirely.
7911 */
7912 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7913 base.head) {
7914
7915 if (&encoder->new_crtc->base != crtc)
7916 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +01007917
7918 if (encoder->compute_config) {
7919 if (!(encoder->compute_config(encoder, pipe_config))) {
7920 DRM_DEBUG_KMS("Encoder config failure\n");
7921 goto fail;
7922 }
7923
7924 continue;
7925 }
7926
Daniel Vetter7758a112012-07-08 19:40:39 +02007927 encoder_funcs = encoder->base.helper_private;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007928 if (!(encoder_funcs->mode_fixup(&encoder->base,
7929 &pipe_config->requested_mode,
7930 &pipe_config->adjusted_mode))) {
Daniel Vetter7758a112012-07-08 19:40:39 +02007931 DRM_DEBUG_KMS("Encoder fixup failed\n");
7932 goto fail;
7933 }
7934 }
7935
Daniel Vetterff9a6752013-06-01 17:16:21 +02007936 /* Set default port clock if not overwritten by the encoder. Needs to be
7937 * done afterwards in case the encoder adjusts the mode. */
7938 if (!pipe_config->port_clock)
7939 pipe_config->port_clock = pipe_config->adjusted_mode.clock;
7940
Daniel Vettera43f6e02013-06-07 23:10:32 +02007941 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01007942 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +02007943 DRM_DEBUG_KMS("CRTC fixup failed\n");
7944 goto fail;
7945 }
Daniel Vettere29c22c2013-02-21 00:00:16 +01007946
7947 if (ret == RETRY) {
7948 if (WARN(!retry, "loop in pipe configuration computation\n")) {
7949 ret = -EINVAL;
7950 goto fail;
7951 }
7952
7953 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
7954 retry = false;
7955 goto encoder_retry;
7956 }
7957
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007958 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
7959 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
7960 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
7961
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007962 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +02007963fail:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007964 kfree(pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01007965 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +02007966}
7967
Daniel Vettere2e1ed42012-07-08 21:14:38 +02007968/* Computes which crtcs are affected and sets the relevant bits in the mask. For
7969 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
7970static void
7971intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
7972 unsigned *prepare_pipes, unsigned *disable_pipes)
7973{
7974 struct intel_crtc *intel_crtc;
7975 struct drm_device *dev = crtc->dev;
7976 struct intel_encoder *encoder;
7977 struct intel_connector *connector;
7978 struct drm_crtc *tmp_crtc;
7979
7980 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
7981
7982 /* Check which crtcs have changed outputs connected to them, these need
7983 * to be part of the prepare_pipes mask. We don't (yet) support global
7984 * modeset across multiple crtcs, so modeset_pipes will only have one
7985 * bit set at most. */
7986 list_for_each_entry(connector, &dev->mode_config.connector_list,
7987 base.head) {
7988 if (connector->base.encoder == &connector->new_encoder->base)
7989 continue;
7990
7991 if (connector->base.encoder) {
7992 tmp_crtc = connector->base.encoder->crtc;
7993
7994 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
7995 }
7996
7997 if (connector->new_encoder)
7998 *prepare_pipes |=
7999 1 << connector->new_encoder->new_crtc->pipe;
8000 }
8001
8002 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8003 base.head) {
8004 if (encoder->base.crtc == &encoder->new_crtc->base)
8005 continue;
8006
8007 if (encoder->base.crtc) {
8008 tmp_crtc = encoder->base.crtc;
8009
8010 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
8011 }
8012
8013 if (encoder->new_crtc)
8014 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
8015 }
8016
8017 /* Check for any pipes that will be fully disabled ... */
8018 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
8019 base.head) {
8020 bool used = false;
8021
8022 /* Don't try to disable disabled crtcs. */
8023 if (!intel_crtc->base.enabled)
8024 continue;
8025
8026 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8027 base.head) {
8028 if (encoder->new_crtc == intel_crtc)
8029 used = true;
8030 }
8031
8032 if (!used)
8033 *disable_pipes |= 1 << intel_crtc->pipe;
8034 }
8035
8036
8037 /* set_mode is also used to update properties on life display pipes. */
8038 intel_crtc = to_intel_crtc(crtc);
8039 if (crtc->enabled)
8040 *prepare_pipes |= 1 << intel_crtc->pipe;
8041
Daniel Vetterb6c51642013-04-12 18:48:43 +02008042 /*
8043 * For simplicity do a full modeset on any pipe where the output routing
8044 * changed. We could be more clever, but that would require us to be
8045 * more careful with calling the relevant encoder->mode_set functions.
8046 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +02008047 if (*prepare_pipes)
8048 *modeset_pipes = *prepare_pipes;
8049
8050 /* ... and mask these out. */
8051 *modeset_pipes &= ~(*disable_pipes);
8052 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +02008053
8054 /*
8055 * HACK: We don't (yet) fully support global modesets. intel_set_config
8056 * obies this rule, but the modeset restore mode of
8057 * intel_modeset_setup_hw_state does not.
8058 */
8059 *modeset_pipes &= 1 << intel_crtc->pipe;
8060 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +02008061
8062 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
8063 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +02008064}
8065
Daniel Vetterea9d7582012-07-10 10:42:52 +02008066static bool intel_crtc_in_use(struct drm_crtc *crtc)
8067{
8068 struct drm_encoder *encoder;
8069 struct drm_device *dev = crtc->dev;
8070
8071 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
8072 if (encoder->crtc == crtc)
8073 return true;
8074
8075 return false;
8076}
8077
8078static void
8079intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
8080{
8081 struct intel_encoder *intel_encoder;
8082 struct intel_crtc *intel_crtc;
8083 struct drm_connector *connector;
8084
8085 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
8086 base.head) {
8087 if (!intel_encoder->base.crtc)
8088 continue;
8089
8090 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
8091
8092 if (prepare_pipes & (1 << intel_crtc->pipe))
8093 intel_encoder->connectors_active = false;
8094 }
8095
8096 intel_modeset_commit_output_state(dev);
8097
8098 /* Update computed state. */
8099 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
8100 base.head) {
8101 intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
8102 }
8103
8104 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
8105 if (!connector->encoder || !connector->encoder->crtc)
8106 continue;
8107
8108 intel_crtc = to_intel_crtc(connector->encoder->crtc);
8109
8110 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +02008111 struct drm_property *dpms_property =
8112 dev->mode_config.dpms_property;
8113
Daniel Vetterea9d7582012-07-10 10:42:52 +02008114 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -05008115 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +02008116 dpms_property,
8117 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +02008118
8119 intel_encoder = to_intel_encoder(connector->encoder);
8120 intel_encoder->connectors_active = true;
8121 }
8122 }
8123
8124}
8125
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008126static bool intel_fuzzy_clock_check(struct intel_crtc_config *cur,
8127 struct intel_crtc_config *new)
8128{
8129 int clock1, clock2, diff;
8130
8131 clock1 = cur->adjusted_mode.clock;
8132 clock2 = new->adjusted_mode.clock;
8133
8134 if (clock1 == clock2)
8135 return true;
8136
8137 if (!clock1 || !clock2)
8138 return false;
8139
8140 diff = abs(clock1 - clock2);
8141
8142 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
8143 return true;
8144
8145 return false;
8146}
8147
Daniel Vetter25c5b262012-07-08 22:08:04 +02008148#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
8149 list_for_each_entry((intel_crtc), \
8150 &(dev)->mode_config.crtc_list, \
8151 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +02008152 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +02008153
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008154static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008155intel_pipe_config_compare(struct drm_device *dev,
8156 struct intel_crtc_config *current_config,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008157 struct intel_crtc_config *pipe_config)
8158{
Daniel Vetter66e985c2013-06-05 13:34:20 +02008159#define PIPE_CONF_CHECK_X(name) \
8160 if (current_config->name != pipe_config->name) { \
8161 DRM_ERROR("mismatch in " #name " " \
8162 "(expected 0x%08x, found 0x%08x)\n", \
8163 current_config->name, \
8164 pipe_config->name); \
8165 return false; \
8166 }
8167
Daniel Vetter08a24032013-04-19 11:25:34 +02008168#define PIPE_CONF_CHECK_I(name) \
8169 if (current_config->name != pipe_config->name) { \
8170 DRM_ERROR("mismatch in " #name " " \
8171 "(expected %i, found %i)\n", \
8172 current_config->name, \
8173 pipe_config->name); \
8174 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +01008175 }
8176
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008177#define PIPE_CONF_CHECK_FLAGS(name, mask) \
8178 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -07008179 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008180 "(expected %i, found %i)\n", \
8181 current_config->name & (mask), \
8182 pipe_config->name & (mask)); \
8183 return false; \
8184 }
8185
Daniel Vetterbb760062013-06-06 14:55:52 +02008186#define PIPE_CONF_QUIRK(quirk) \
8187 ((current_config->quirks | pipe_config->quirks) & (quirk))
8188
Daniel Vettereccb1402013-05-22 00:50:22 +02008189 PIPE_CONF_CHECK_I(cpu_transcoder);
8190
Daniel Vetter08a24032013-04-19 11:25:34 +02008191 PIPE_CONF_CHECK_I(has_pch_encoder);
8192 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +02008193 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
8194 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
8195 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
8196 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
8197 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +02008198
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008199 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
8200 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
8201 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
8202 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
8203 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
8204 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
8205
8206 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
8207 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
8208 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
8209 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
8210 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
8211 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
8212
Daniel Vetterc93f54c2013-06-27 19:47:19 +02008213 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008214
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008215 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8216 DRM_MODE_FLAG_INTERLACE);
8217
Daniel Vetterbb760062013-06-06 14:55:52 +02008218 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
8219 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8220 DRM_MODE_FLAG_PHSYNC);
8221 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8222 DRM_MODE_FLAG_NHSYNC);
8223 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8224 DRM_MODE_FLAG_PVSYNC);
8225 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8226 DRM_MODE_FLAG_NVSYNC);
8227 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -07008228
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008229 PIPE_CONF_CHECK_I(requested_mode.hdisplay);
8230 PIPE_CONF_CHECK_I(requested_mode.vdisplay);
8231
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008232 PIPE_CONF_CHECK_I(gmch_pfit.control);
8233 /* pfit ratios are autocomputed by the hw on gen4+ */
8234 if (INTEL_INFO(dev)->gen < 4)
8235 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
8236 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
8237 PIPE_CONF_CHECK_I(pch_pfit.pos);
8238 PIPE_CONF_CHECK_I(pch_pfit.size);
8239
Paulo Zanoni42db64e2013-05-31 16:33:22 -03008240 PIPE_CONF_CHECK_I(ips_enabled);
8241
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008242 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008243 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008244 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008245 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
8246 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008247
Daniel Vetter66e985c2013-06-05 13:34:20 +02008248#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +02008249#undef PIPE_CONF_CHECK_I
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008250#undef PIPE_CONF_CHECK_FLAGS
Daniel Vetterbb760062013-06-06 14:55:52 +02008251#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008252
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008253 if (!IS_HASWELL(dev)) {
8254 if (!intel_fuzzy_clock_check(current_config, pipe_config)) {
Jesse Barnes6f024882013-07-01 10:19:09 -07008255 DRM_ERROR("mismatch in clock (expected %d, found %d)\n",
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008256 current_config->adjusted_mode.clock,
8257 pipe_config->adjusted_mode.clock);
8258 return false;
8259 }
8260 }
8261
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008262 return true;
8263}
8264
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02008265static void
8266check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008267{
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008268 struct intel_connector *connector;
8269
8270 list_for_each_entry(connector, &dev->mode_config.connector_list,
8271 base.head) {
8272 /* This also checks the encoder/connector hw state with the
8273 * ->get_hw_state callbacks. */
8274 intel_connector_check_state(connector);
8275
8276 WARN(&connector->new_encoder->base != connector->base.encoder,
8277 "connector's staged encoder doesn't match current encoder\n");
8278 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02008279}
8280
8281static void
8282check_encoder_state(struct drm_device *dev)
8283{
8284 struct intel_encoder *encoder;
8285 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008286
8287 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8288 base.head) {
8289 bool enabled = false;
8290 bool active = false;
8291 enum pipe pipe, tracked_pipe;
8292
8293 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
8294 encoder->base.base.id,
8295 drm_get_encoder_name(&encoder->base));
8296
8297 WARN(&encoder->new_crtc->base != encoder->base.crtc,
8298 "encoder's stage crtc doesn't match current crtc\n");
8299 WARN(encoder->connectors_active && !encoder->base.crtc,
8300 "encoder's active_connectors set, but no crtc\n");
8301
8302 list_for_each_entry(connector, &dev->mode_config.connector_list,
8303 base.head) {
8304 if (connector->base.encoder != &encoder->base)
8305 continue;
8306 enabled = true;
8307 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
8308 active = true;
8309 }
8310 WARN(!!encoder->base.crtc != enabled,
8311 "encoder's enabled state mismatch "
8312 "(expected %i, found %i)\n",
8313 !!encoder->base.crtc, enabled);
8314 WARN(active && !encoder->base.crtc,
8315 "active encoder with no crtc\n");
8316
8317 WARN(encoder->connectors_active != active,
8318 "encoder's computed active state doesn't match tracked active state "
8319 "(expected %i, found %i)\n", active, encoder->connectors_active);
8320
8321 active = encoder->get_hw_state(encoder, &pipe);
8322 WARN(active != encoder->connectors_active,
8323 "encoder's hw state doesn't match sw tracking "
8324 "(expected %i, found %i)\n",
8325 encoder->connectors_active, active);
8326
8327 if (!encoder->base.crtc)
8328 continue;
8329
8330 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
8331 WARN(active && pipe != tracked_pipe,
8332 "active encoder's pipe doesn't match"
8333 "(expected %i, found %i)\n",
8334 tracked_pipe, pipe);
8335
8336 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02008337}
8338
8339static void
8340check_crtc_state(struct drm_device *dev)
8341{
8342 drm_i915_private_t *dev_priv = dev->dev_private;
8343 struct intel_crtc *crtc;
8344 struct intel_encoder *encoder;
8345 struct intel_crtc_config pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008346
8347 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
8348 base.head) {
8349 bool enabled = false;
8350 bool active = false;
8351
Jesse Barnes045ac3b2013-05-14 17:08:26 -07008352 memset(&pipe_config, 0, sizeof(pipe_config));
8353
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008354 DRM_DEBUG_KMS("[CRTC:%d]\n",
8355 crtc->base.base.id);
8356
8357 WARN(crtc->active && !crtc->base.enabled,
8358 "active crtc, but not enabled in sw tracking\n");
8359
8360 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8361 base.head) {
8362 if (encoder->base.crtc != &crtc->base)
8363 continue;
8364 enabled = true;
8365 if (encoder->connectors_active)
8366 active = true;
8367 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008368
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008369 WARN(active != crtc->active,
8370 "crtc's computed active state doesn't match tracked active state "
8371 "(expected %i, found %i)\n", active, crtc->active);
8372 WARN(enabled != crtc->base.enabled,
8373 "crtc's computed enabled state doesn't match tracked enabled state "
8374 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
8375
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008376 active = dev_priv->display.get_pipe_config(crtc,
8377 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +02008378
8379 /* hw state is inconsistent with the pipe A quirk */
8380 if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
8381 active = crtc->active;
8382
Daniel Vetter6c49f242013-06-06 12:45:25 +02008383 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8384 base.head) {
8385 if (encoder->base.crtc != &crtc->base)
8386 continue;
Jesse Barnes510d5f22013-07-01 15:50:17 -07008387 if (encoder->get_config)
Daniel Vetter6c49f242013-06-06 12:45:25 +02008388 encoder->get_config(encoder, &pipe_config);
8389 }
8390
Jesse Barnes510d5f22013-07-01 15:50:17 -07008391 if (dev_priv->display.get_clock)
8392 dev_priv->display.get_clock(crtc, &pipe_config);
8393
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008394 WARN(crtc->active != active,
8395 "crtc active state doesn't match with hw state "
8396 "(expected %i, found %i)\n", crtc->active, active);
8397
Daniel Vetterc0b03412013-05-28 12:05:54 +02008398 if (active &&
8399 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
8400 WARN(1, "pipe state doesn't match!\n");
8401 intel_dump_pipe_config(crtc, &pipe_config,
8402 "[hw state]");
8403 intel_dump_pipe_config(crtc, &crtc->config,
8404 "[sw state]");
8405 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008406 }
8407}
8408
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02008409static void
8410check_shared_dpll_state(struct drm_device *dev)
8411{
8412 drm_i915_private_t *dev_priv = dev->dev_private;
8413 struct intel_crtc *crtc;
8414 struct intel_dpll_hw_state dpll_hw_state;
8415 int i;
Daniel Vetter53589012013-06-05 13:34:16 +02008416
8417 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8418 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
8419 int enabled_crtcs = 0, active_crtcs = 0;
8420 bool active;
8421
8422 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
8423
8424 DRM_DEBUG_KMS("%s\n", pll->name);
8425
8426 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
8427
8428 WARN(pll->active > pll->refcount,
8429 "more active pll users than references: %i vs %i\n",
8430 pll->active, pll->refcount);
8431 WARN(pll->active && !pll->on,
8432 "pll in active use but not on in sw tracking\n");
8433 WARN(pll->on != active,
8434 "pll on state mismatch (expected %i, found %i)\n",
8435 pll->on, active);
8436
8437 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
8438 base.head) {
8439 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
8440 enabled_crtcs++;
8441 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
8442 active_crtcs++;
8443 }
8444 WARN(pll->active != active_crtcs,
8445 "pll active crtcs mismatch (expected %i, found %i)\n",
8446 pll->active, active_crtcs);
8447 WARN(pll->refcount != enabled_crtcs,
8448 "pll enabled crtcs mismatch (expected %i, found %i)\n",
8449 pll->refcount, enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008450
8451 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
8452 sizeof(dpll_hw_state)),
8453 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +02008454 }
Daniel Vettera6778b32012-07-02 09:56:42 +02008455}
8456
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02008457void
8458intel_modeset_check_state(struct drm_device *dev)
8459{
8460 check_connector_state(dev);
8461 check_encoder_state(dev);
8462 check_crtc_state(dev);
8463 check_shared_dpll_state(dev);
8464}
8465
Daniel Vetterf30da182013-04-11 20:22:50 +02008466static int __intel_set_mode(struct drm_crtc *crtc,
8467 struct drm_display_mode *mode,
8468 int x, int y, struct drm_framebuffer *fb)
Daniel Vettera6778b32012-07-02 09:56:42 +02008469{
8470 struct drm_device *dev = crtc->dev;
Daniel Vetterdbf2b54e2012-07-02 11:18:29 +02008471 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008472 struct drm_display_mode *saved_mode, *saved_hwmode;
8473 struct intel_crtc_config *pipe_config = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +02008474 struct intel_crtc *intel_crtc;
8475 unsigned disable_pipes, prepare_pipes, modeset_pipes;
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008476 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +02008477
Tim Gardner3ac18232012-12-07 07:54:26 -07008478 saved_mode = kmalloc(2 * sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008479 if (!saved_mode)
8480 return -ENOMEM;
Tim Gardner3ac18232012-12-07 07:54:26 -07008481 saved_hwmode = saved_mode + 1;
Daniel Vettera6778b32012-07-02 09:56:42 +02008482
Daniel Vettere2e1ed42012-07-08 21:14:38 +02008483 intel_modeset_affected_pipes(crtc, &modeset_pipes,
Daniel Vetter25c5b262012-07-08 22:08:04 +02008484 &prepare_pipes, &disable_pipes);
8485
Tim Gardner3ac18232012-12-07 07:54:26 -07008486 *saved_hwmode = crtc->hwmode;
8487 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02008488
Daniel Vetter25c5b262012-07-08 22:08:04 +02008489 /* Hack: Because we don't (yet) support global modeset on multiple
8490 * crtcs, we don't keep track of the new mode for more than one crtc.
8491 * Hence simply check whether any bit is set in modeset_pipes in all the
8492 * pieces of code that are not yet converted to deal with mutliple crtcs
8493 * changing their mode at the same time. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02008494 if (modeset_pipes) {
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008495 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008496 if (IS_ERR(pipe_config)) {
8497 ret = PTR_ERR(pipe_config);
8498 pipe_config = NULL;
8499
Tim Gardner3ac18232012-12-07 07:54:26 -07008500 goto out;
Daniel Vetter25c5b262012-07-08 22:08:04 +02008501 }
Daniel Vetterc0b03412013-05-28 12:05:54 +02008502 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
8503 "[modeset]");
Daniel Vettera6778b32012-07-02 09:56:42 +02008504 }
8505
Daniel Vetter460da9162013-03-27 00:44:51 +01008506 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
8507 intel_crtc_disable(&intel_crtc->base);
8508
Daniel Vetterea9d7582012-07-10 10:42:52 +02008509 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
8510 if (intel_crtc->base.enabled)
8511 dev_priv->display.crtc_disable(&intel_crtc->base);
8512 }
Daniel Vettera6778b32012-07-02 09:56:42 +02008513
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02008514 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
8515 * to set it here already despite that we pass it down the callchain.
8516 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008517 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +02008518 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008519 /* mode_set/enable/disable functions rely on a correct pipe
8520 * config. */
8521 to_intel_crtc(crtc)->config = *pipe_config;
8522 }
Daniel Vetter7758a112012-07-08 19:40:39 +02008523
Daniel Vetterea9d7582012-07-10 10:42:52 +02008524 /* Only after disabling all output pipelines that will be changed can we
8525 * update the the output configuration. */
8526 intel_modeset_update_state(dev, prepare_pipes);
8527
Daniel Vetter47fab732012-10-26 10:58:18 +02008528 if (dev_priv->display.modeset_global_resources)
8529 dev_priv->display.modeset_global_resources(dev);
8530
Daniel Vettera6778b32012-07-02 09:56:42 +02008531 /* Set up the DPLL and any encoders state that needs to adjust or depend
8532 * on the DPLL.
8533 */
Daniel Vetter25c5b262012-07-08 22:08:04 +02008534 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008535 ret = intel_crtc_mode_set(&intel_crtc->base,
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008536 x, y, fb);
8537 if (ret)
8538 goto done;
Daniel Vettera6778b32012-07-02 09:56:42 +02008539 }
8540
8541 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02008542 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
8543 dev_priv->display.crtc_enable(&intel_crtc->base);
Daniel Vettera6778b32012-07-02 09:56:42 +02008544
Daniel Vetter25c5b262012-07-08 22:08:04 +02008545 if (modeset_pipes) {
8546 /* Store real post-adjustment hardware mode. */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008547 crtc->hwmode = pipe_config->adjusted_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02008548
Daniel Vetter25c5b262012-07-08 22:08:04 +02008549 /* Calculate and store various constants which
8550 * are later needed by vblank and swap-completion
8551 * timestamping. They are derived from true hwmode.
8552 */
8553 drm_calc_timestamping_constants(crtc);
8554 }
Daniel Vettera6778b32012-07-02 09:56:42 +02008555
8556 /* FIXME: add subpixel order */
8557done:
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008558 if (ret && crtc->enabled) {
Tim Gardner3ac18232012-12-07 07:54:26 -07008559 crtc->hwmode = *saved_hwmode;
8560 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02008561 }
8562
Tim Gardner3ac18232012-12-07 07:54:26 -07008563out:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008564 kfree(pipe_config);
Tim Gardner3ac18232012-12-07 07:54:26 -07008565 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +02008566 return ret;
8567}
8568
Daniel Vetterf30da182013-04-11 20:22:50 +02008569int intel_set_mode(struct drm_crtc *crtc,
8570 struct drm_display_mode *mode,
8571 int x, int y, struct drm_framebuffer *fb)
8572{
8573 int ret;
8574
8575 ret = __intel_set_mode(crtc, mode, x, y, fb);
8576
8577 if (ret == 0)
8578 intel_modeset_check_state(crtc->dev);
8579
8580 return ret;
8581}
8582
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008583void intel_crtc_restore_mode(struct drm_crtc *crtc)
8584{
8585 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->fb);
8586}
8587
Daniel Vetter25c5b262012-07-08 22:08:04 +02008588#undef for_each_intel_crtc_masked
8589
Daniel Vetterd9e55602012-07-04 22:16:09 +02008590static void intel_set_config_free(struct intel_set_config *config)
8591{
8592 if (!config)
8593 return;
8594
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008595 kfree(config->save_connector_encoders);
8596 kfree(config->save_encoder_crtcs);
Daniel Vetterd9e55602012-07-04 22:16:09 +02008597 kfree(config);
8598}
8599
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008600static int intel_set_config_save_state(struct drm_device *dev,
8601 struct intel_set_config *config)
8602{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008603 struct drm_encoder *encoder;
8604 struct drm_connector *connector;
8605 int count;
8606
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008607 config->save_encoder_crtcs =
8608 kcalloc(dev->mode_config.num_encoder,
8609 sizeof(struct drm_crtc *), GFP_KERNEL);
8610 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008611 return -ENOMEM;
8612
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008613 config->save_connector_encoders =
8614 kcalloc(dev->mode_config.num_connector,
8615 sizeof(struct drm_encoder *), GFP_KERNEL);
8616 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008617 return -ENOMEM;
8618
8619 /* Copy data. Note that driver private data is not affected.
8620 * Should anything bad happen only the expected state is
8621 * restored, not the drivers personal bookkeeping.
8622 */
8623 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008624 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008625 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008626 }
8627
8628 count = 0;
8629 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008630 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008631 }
8632
8633 return 0;
8634}
8635
8636static void intel_set_config_restore_state(struct drm_device *dev,
8637 struct intel_set_config *config)
8638{
Daniel Vetter9a935852012-07-05 22:34:27 +02008639 struct intel_encoder *encoder;
8640 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008641 int count;
8642
8643 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008644 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
8645 encoder->new_crtc =
8646 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008647 }
8648
8649 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008650 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
8651 connector->new_encoder =
8652 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008653 }
8654}
8655
Imre Deake3de42b2013-05-03 19:44:07 +02008656static bool
8657is_crtc_connector_off(struct drm_crtc *crtc, struct drm_connector *connectors,
8658 int num_connectors)
8659{
8660 int i;
8661
8662 for (i = 0; i < num_connectors; i++)
8663 if (connectors[i].encoder &&
8664 connectors[i].encoder->crtc == crtc &&
8665 connectors[i].dpms != DRM_MODE_DPMS_ON)
8666 return true;
8667
8668 return false;
8669}
8670
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008671static void
8672intel_set_config_compute_mode_changes(struct drm_mode_set *set,
8673 struct intel_set_config *config)
8674{
8675
8676 /* We should be able to check here if the fb has the same properties
8677 * and then just flip_or_move it */
Imre Deake3de42b2013-05-03 19:44:07 +02008678 if (set->connectors != NULL &&
8679 is_crtc_connector_off(set->crtc, *set->connectors,
8680 set->num_connectors)) {
8681 config->mode_changed = true;
8682 } else if (set->crtc->fb != set->fb) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008683 /* If we have no fb then treat it as a full mode set */
8684 if (set->crtc->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +03008685 struct intel_crtc *intel_crtc =
8686 to_intel_crtc(set->crtc);
8687
8688 if (intel_crtc->active && i915_fastboot) {
8689 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
8690 config->fb_changed = true;
8691 } else {
8692 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
8693 config->mode_changed = true;
8694 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008695 } else if (set->fb == NULL) {
8696 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +01008697 } else if (set->fb->pixel_format !=
8698 set->crtc->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008699 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02008700 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008701 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02008702 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008703 }
8704
Daniel Vetter835c5872012-07-10 18:11:08 +02008705 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008706 config->fb_changed = true;
8707
8708 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
8709 DRM_DEBUG_KMS("modes are different, full mode set\n");
8710 drm_mode_debug_printmodeline(&set->crtc->mode);
8711 drm_mode_debug_printmodeline(set->mode);
8712 config->mode_changed = true;
8713 }
8714}
8715
Daniel Vetter2e431052012-07-04 22:42:15 +02008716static int
Daniel Vetter9a935852012-07-05 22:34:27 +02008717intel_modeset_stage_output_state(struct drm_device *dev,
8718 struct drm_mode_set *set,
8719 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +02008720{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008721 struct drm_crtc *new_crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02008722 struct intel_connector *connector;
8723 struct intel_encoder *encoder;
Daniel Vetter2e431052012-07-04 22:42:15 +02008724 int count, ro;
Daniel Vetter50f56112012-07-02 09:35:43 +02008725
Damien Lespiau9abdda72013-02-13 13:29:23 +00008726 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +02008727 * of connectors. For paranoia, double-check this. */
8728 WARN_ON(!set->fb && (set->num_connectors != 0));
8729 WARN_ON(set->fb && (set->num_connectors == 0));
8730
Daniel Vetter50f56112012-07-02 09:35:43 +02008731 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008732 list_for_each_entry(connector, &dev->mode_config.connector_list,
8733 base.head) {
8734 /* Otherwise traverse passed in connector list and get encoders
8735 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +02008736 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02008737 if (set->connectors[ro] == &connector->base) {
8738 connector->new_encoder = connector->encoder;
Daniel Vetter50f56112012-07-02 09:35:43 +02008739 break;
8740 }
8741 }
8742
Daniel Vetter9a935852012-07-05 22:34:27 +02008743 /* If we disable the crtc, disable all its connectors. Also, if
8744 * the connector is on the changing crtc but not on the new
8745 * connector list, disable it. */
8746 if ((!set->fb || ro == set->num_connectors) &&
8747 connector->base.encoder &&
8748 connector->base.encoder->crtc == set->crtc) {
8749 connector->new_encoder = NULL;
8750
8751 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
8752 connector->base.base.id,
8753 drm_get_connector_name(&connector->base));
8754 }
8755
8756
8757 if (&connector->new_encoder->base != connector->base.encoder) {
Daniel Vetter50f56112012-07-02 09:35:43 +02008758 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008759 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02008760 }
Daniel Vetter9a935852012-07-05 22:34:27 +02008761 }
8762 /* connector->new_encoder is now updated for all connectors. */
8763
8764 /* Update crtc of enabled connectors. */
Daniel Vetter50f56112012-07-02 09:35:43 +02008765 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008766 list_for_each_entry(connector, &dev->mode_config.connector_list,
8767 base.head) {
8768 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +02008769 continue;
8770
Daniel Vetter9a935852012-07-05 22:34:27 +02008771 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +02008772
8773 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02008774 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +02008775 new_crtc = set->crtc;
8776 }
8777
8778 /* Make sure the new CRTC will work with the encoder */
Daniel Vetter9a935852012-07-05 22:34:27 +02008779 if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
8780 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008781 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +02008782 }
Daniel Vetter9a935852012-07-05 22:34:27 +02008783 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
8784
8785 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
8786 connector->base.base.id,
8787 drm_get_connector_name(&connector->base),
8788 new_crtc->base.id);
8789 }
8790
8791 /* Check for any encoders that needs to be disabled. */
8792 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8793 base.head) {
8794 list_for_each_entry(connector,
8795 &dev->mode_config.connector_list,
8796 base.head) {
8797 if (connector->new_encoder == encoder) {
8798 WARN_ON(!connector->new_encoder->new_crtc);
8799
8800 goto next_encoder;
8801 }
8802 }
8803 encoder->new_crtc = NULL;
8804next_encoder:
8805 /* Only now check for crtc changes so we don't miss encoders
8806 * that will be disabled. */
8807 if (&encoder->new_crtc->base != encoder->base.crtc) {
Daniel Vetter50f56112012-07-02 09:35:43 +02008808 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008809 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02008810 }
8811 }
Daniel Vetter9a935852012-07-05 22:34:27 +02008812 /* Now we've also updated encoder->new_crtc for all encoders. */
Daniel Vetter50f56112012-07-02 09:35:43 +02008813
Daniel Vetter2e431052012-07-04 22:42:15 +02008814 return 0;
8815}
8816
8817static int intel_crtc_set_config(struct drm_mode_set *set)
8818{
8819 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +02008820 struct drm_mode_set save_set;
8821 struct intel_set_config *config;
8822 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +02008823
Daniel Vetter8d3e3752012-07-05 16:09:09 +02008824 BUG_ON(!set);
8825 BUG_ON(!set->crtc);
8826 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +02008827
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01008828 /* Enforce sane interface api - has been abused by the fb helper. */
8829 BUG_ON(!set->mode && set->fb);
8830 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +02008831
Daniel Vetter2e431052012-07-04 22:42:15 +02008832 if (set->fb) {
8833 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
8834 set->crtc->base.id, set->fb->base.id,
8835 (int)set->num_connectors, set->x, set->y);
8836 } else {
8837 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +02008838 }
8839
8840 dev = set->crtc->dev;
8841
8842 ret = -ENOMEM;
8843 config = kzalloc(sizeof(*config), GFP_KERNEL);
8844 if (!config)
8845 goto out_config;
8846
8847 ret = intel_set_config_save_state(dev, config);
8848 if (ret)
8849 goto out_config;
8850
8851 save_set.crtc = set->crtc;
8852 save_set.mode = &set->crtc->mode;
8853 save_set.x = set->crtc->x;
8854 save_set.y = set->crtc->y;
8855 save_set.fb = set->crtc->fb;
8856
8857 /* Compute whether we need a full modeset, only an fb base update or no
8858 * change at all. In the future we might also check whether only the
8859 * mode changed, e.g. for LVDS where we only change the panel fitter in
8860 * such cases. */
8861 intel_set_config_compute_mode_changes(set, config);
8862
Daniel Vetter9a935852012-07-05 22:34:27 +02008863 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +02008864 if (ret)
8865 goto fail;
8866
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008867 if (config->mode_changed) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008868 ret = intel_set_mode(set->crtc, set->mode,
8869 set->x, set->y, set->fb);
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008870 } else if (config->fb_changed) {
Ville Syrjälä4878cae2013-02-18 19:08:48 +02008871 intel_crtc_wait_for_pending_flips(set->crtc);
8872
Daniel Vetter4f660f42012-07-02 09:47:37 +02008873 ret = intel_pipe_set_base(set->crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +02008874 set->x, set->y, set->fb);
Daniel Vetter50f56112012-07-02 09:35:43 +02008875 }
8876
Chris Wilson2d05eae2013-05-03 17:36:25 +01008877 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +02008878 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
8879 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +02008880fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +01008881 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +02008882
Chris Wilson2d05eae2013-05-03 17:36:25 +01008883 /* Try to restore the config */
8884 if (config->mode_changed &&
8885 intel_set_mode(save_set.crtc, save_set.mode,
8886 save_set.x, save_set.y, save_set.fb))
8887 DRM_ERROR("failed to restore config after modeset failure\n");
8888 }
Daniel Vetter50f56112012-07-02 09:35:43 +02008889
Daniel Vetterd9e55602012-07-04 22:16:09 +02008890out_config:
8891 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +02008892 return ret;
8893}
8894
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008895static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008896 .cursor_set = intel_crtc_cursor_set,
8897 .cursor_move = intel_crtc_cursor_move,
8898 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +02008899 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008900 .destroy = intel_crtc_destroy,
8901 .page_flip = intel_crtc_page_flip,
8902};
8903
Paulo Zanoni79f689a2012-10-05 12:05:52 -03008904static void intel_cpu_pll_init(struct drm_device *dev)
8905{
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008906 if (HAS_DDI(dev))
Paulo Zanoni79f689a2012-10-05 12:05:52 -03008907 intel_ddi_pll_init(dev);
8908}
8909
Daniel Vetter53589012013-06-05 13:34:16 +02008910static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
8911 struct intel_shared_dpll *pll,
8912 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008913{
Daniel Vetter53589012013-06-05 13:34:16 +02008914 uint32_t val;
8915
8916 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +02008917 hw_state->dpll = val;
8918 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
8919 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +02008920
8921 return val & DPLL_VCO_ENABLE;
8922}
8923
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02008924static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
8925 struct intel_shared_dpll *pll)
8926{
8927 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
8928 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
8929}
8930
Daniel Vettere7b903d2013-06-05 13:34:14 +02008931static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
8932 struct intel_shared_dpll *pll)
8933{
Daniel Vettere7b903d2013-06-05 13:34:14 +02008934 /* PCH refclock must be enabled first */
8935 assert_pch_refclk_enabled(dev_priv);
8936
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02008937 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
8938
8939 /* Wait for the clocks to stabilize. */
8940 POSTING_READ(PCH_DPLL(pll->id));
8941 udelay(150);
8942
8943 /* The pixel multiplier can only be updated once the
8944 * DPLL is enabled and the clocks are stable.
8945 *
8946 * So write it again.
8947 */
8948 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
8949 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +02008950 udelay(200);
8951}
8952
8953static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
8954 struct intel_shared_dpll *pll)
8955{
8956 struct drm_device *dev = dev_priv->dev;
8957 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +02008958
8959 /* Make sure no transcoder isn't still depending on us. */
8960 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
8961 if (intel_crtc_to_shared_dpll(crtc) == pll)
8962 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
8963 }
8964
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02008965 I915_WRITE(PCH_DPLL(pll->id), 0);
8966 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +02008967 udelay(200);
8968}
8969
Daniel Vetter46edb022013-06-05 13:34:12 +02008970static char *ibx_pch_dpll_names[] = {
8971 "PCH DPLL A",
8972 "PCH DPLL B",
8973};
8974
Daniel Vetter7c74ade2013-06-05 13:34:11 +02008975static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008976{
Daniel Vettere7b903d2013-06-05 13:34:14 +02008977 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008978 int i;
8979
Daniel Vetter7c74ade2013-06-05 13:34:11 +02008980 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008981
Daniel Vettere72f9fb2013-06-05 13:34:06 +02008982 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +02008983 dev_priv->shared_dplls[i].id = i;
8984 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02008985 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +02008986 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
8987 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +02008988 dev_priv->shared_dplls[i].get_hw_state =
8989 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008990 }
8991}
8992
Daniel Vetter7c74ade2013-06-05 13:34:11 +02008993static void intel_shared_dpll_init(struct drm_device *dev)
8994{
Daniel Vettere7b903d2013-06-05 13:34:14 +02008995 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +02008996
8997 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8998 ibx_pch_dpll_init(dev);
8999 else
9000 dev_priv->num_shared_dpll = 0;
9001
9002 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
9003 DRM_DEBUG_KMS("%i shared PLLs initialized\n",
9004 dev_priv->num_shared_dpll);
9005}
9006
Hannes Ederb358d0a2008-12-18 21:18:47 +01009007static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -08009008{
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08009009 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08009010 struct intel_crtc *intel_crtc;
9011 int i;
9012
9013 intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
9014 if (intel_crtc == NULL)
9015 return;
9016
9017 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
9018
9019 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -08009020 for (i = 0; i < 256; i++) {
9021 intel_crtc->lut_r[i] = i;
9022 intel_crtc->lut_g[i] = i;
9023 intel_crtc->lut_b[i] = i;
9024 }
9025
Jesse Barnes80824002009-09-10 15:28:06 -07009026 /* Swap pipes & planes for FBC on pre-965 */
9027 intel_crtc->pipe = pipe;
9028 intel_crtc->plane = pipe;
Chris Wilsone2e767a2010-09-13 16:53:12 +01009029 if (IS_MOBILE(dev) && IS_GEN3(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08009030 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +01009031 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07009032 }
9033
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08009034 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
9035 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
9036 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
9037 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
9038
Jesse Barnes79e53942008-11-07 14:24:08 -08009039 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -08009040}
9041
Carl Worth08d7b3d2009-04-29 14:43:54 -07009042int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00009043 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -07009044{
Carl Worth08d7b3d2009-04-29 14:43:54 -07009045 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +02009046 struct drm_mode_object *drmmode_obj;
9047 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009048
Daniel Vetter1cff8f62012-04-24 09:55:08 +02009049 if (!drm_core_check_feature(dev, DRIVER_MODESET))
9050 return -ENODEV;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009051
Daniel Vetterc05422d2009-08-11 16:05:30 +02009052 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
9053 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -07009054
Daniel Vetterc05422d2009-08-11 16:05:30 +02009055 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -07009056 DRM_ERROR("no such CRTC id\n");
9057 return -EINVAL;
9058 }
9059
Daniel Vetterc05422d2009-08-11 16:05:30 +02009060 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
9061 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009062
Daniel Vetterc05422d2009-08-11 16:05:30 +02009063 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009064}
9065
Daniel Vetter66a92782012-07-12 20:08:18 +02009066static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08009067{
Daniel Vetter66a92782012-07-12 20:08:18 +02009068 struct drm_device *dev = encoder->base.dev;
9069 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08009070 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009071 int entry = 0;
9072
Daniel Vetter66a92782012-07-12 20:08:18 +02009073 list_for_each_entry(source_encoder,
9074 &dev->mode_config.encoder_list, base.head) {
9075
9076 if (encoder == source_encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08009077 index_mask |= (1 << entry);
Daniel Vetter66a92782012-07-12 20:08:18 +02009078
9079 /* Intel hw has only one MUX where enocoders could be cloned. */
9080 if (encoder->cloneable && source_encoder->cloneable)
9081 index_mask |= (1 << entry);
9082
Jesse Barnes79e53942008-11-07 14:24:08 -08009083 entry++;
9084 }
Chris Wilson4ef69c72010-09-09 15:14:28 +01009085
Jesse Barnes79e53942008-11-07 14:24:08 -08009086 return index_mask;
9087}
9088
Chris Wilson4d302442010-12-14 19:21:29 +00009089static bool has_edp_a(struct drm_device *dev)
9090{
9091 struct drm_i915_private *dev_priv = dev->dev_private;
9092
9093 if (!IS_MOBILE(dev))
9094 return false;
9095
9096 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
9097 return false;
9098
9099 if (IS_GEN5(dev) &&
9100 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
9101 return false;
9102
9103 return true;
9104}
9105
Jesse Barnes79e53942008-11-07 14:24:08 -08009106static void intel_setup_outputs(struct drm_device *dev)
9107{
Eric Anholt725e30a2009-01-22 13:01:02 -08009108 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +01009109 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009110 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08009111
Daniel Vetterc9093352013-06-06 22:22:47 +02009112 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009113
Paulo Zanonic40c0f52013-04-12 18:16:53 -03009114 if (!IS_ULT(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -02009115 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009116
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009117 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -03009118 int found;
9119
9120 /* Haswell uses DDI functions to detect digital outputs */
9121 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
9122 /* DDI A only supports eDP */
9123 if (found)
9124 intel_ddi_init(dev, PORT_A);
9125
9126 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
9127 * register */
9128 found = I915_READ(SFUSE_STRAP);
9129
9130 if (found & SFUSE_STRAP_DDIB_DETECTED)
9131 intel_ddi_init(dev, PORT_B);
9132 if (found & SFUSE_STRAP_DDIC_DETECTED)
9133 intel_ddi_init(dev, PORT_C);
9134 if (found & SFUSE_STRAP_DDID_DETECTED)
9135 intel_ddi_init(dev, PORT_D);
9136 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009137 int found;
Daniel Vetter270b3042012-10-27 15:52:05 +02009138 dpd_is_edp = intel_dpd_is_edp(dev);
9139
9140 if (has_edp_a(dev))
9141 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009142
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009143 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +08009144 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +01009145 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009146 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -03009147 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08009148 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009149 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009150 }
9151
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009152 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -03009153 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009154
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009155 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -03009156 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009157
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08009158 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009159 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08009160
Daniel Vetter270b3042012-10-27 15:52:05 +02009161 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009162 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -07009163 } else if (IS_VALLEYVIEW(dev)) {
Gajanan Bhat19c03922012-09-27 19:13:07 +05309164 /* Check for built-in panel first. Shares lanes with HDMI on SDVOC */
Ville Syrjälä67cfc202013-01-25 21:44:44 +02009165 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
9166 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +05309167
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009168 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
Paulo Zanonie2debe92013-02-18 19:00:27 -03009169 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
9170 PORT_B);
Ville Syrjälä67cfc202013-01-25 21:44:44 +02009171 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
9172 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Jesse Barnes4a87d652012-06-15 11:55:16 -07009173 }
Zhenyu Wang103a1962009-11-27 11:44:36 +08009174 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +08009175 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -08009176
Paulo Zanonie2debe92013-02-18 19:00:27 -03009177 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009178 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009179 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009180 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
9181 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009182 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009183 }
Ma Ling27185ae2009-08-24 13:50:23 +08009184
Imre Deake7281ea2013-05-08 13:14:08 +03009185 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009186 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -08009187 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -04009188
9189 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -04009190
Paulo Zanonie2debe92013-02-18 19:00:27 -03009191 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009192 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009193 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009194 }
Ma Ling27185ae2009-08-24 13:50:23 +08009195
Paulo Zanonie2debe92013-02-18 19:00:27 -03009196 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +08009197
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009198 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
9199 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009200 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009201 }
Imre Deake7281ea2013-05-08 13:14:08 +03009202 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009203 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -08009204 }
Ma Ling27185ae2009-08-24 13:50:23 +08009205
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009206 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +03009207 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009208 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -07009209 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08009210 intel_dvo_init(dev);
9211
Zhenyu Wang103a1962009-11-27 11:44:36 +08009212 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08009213 intel_tv_init(dev);
9214
Chris Wilson4ef69c72010-09-09 15:14:28 +01009215 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
9216 encoder->base.possible_crtcs = encoder->crtc_mask;
9217 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +02009218 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -08009219 }
Chris Wilson47356eb2011-01-11 17:06:04 +00009220
Paulo Zanonidde86e22012-12-01 12:04:25 -02009221 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +02009222
9223 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009224}
9225
9226static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
9227{
9228 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08009229
9230 drm_framebuffer_cleanup(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00009231 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08009232
9233 kfree(intel_fb);
9234}
9235
9236static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +00009237 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08009238 unsigned int *handle)
9239{
9240 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00009241 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08009242
Chris Wilson05394f32010-11-08 19:18:58 +00009243 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -08009244}
9245
9246static const struct drm_framebuffer_funcs intel_fb_funcs = {
9247 .destroy = intel_user_framebuffer_destroy,
9248 .create_handle = intel_user_framebuffer_create_handle,
9249};
9250
Dave Airlie38651672010-03-30 05:34:13 +00009251int intel_framebuffer_init(struct drm_device *dev,
9252 struct intel_framebuffer *intel_fb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009253 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilson05394f32010-11-08 19:18:58 +00009254 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -08009255{
Chris Wilsona35cdaa2013-06-25 17:26:45 +01009256 int pitch_limit;
Jesse Barnes79e53942008-11-07 14:24:08 -08009257 int ret;
9258
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009259 if (obj->tiling_mode == I915_TILING_Y) {
9260 DRM_DEBUG("hardware does not support tiling Y\n");
Chris Wilson57cd6502010-08-08 12:34:44 +01009261 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009262 }
Chris Wilson57cd6502010-08-08 12:34:44 +01009263
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009264 if (mode_cmd->pitches[0] & 63) {
9265 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
9266 mode_cmd->pitches[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +01009267 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009268 }
Chris Wilson57cd6502010-08-08 12:34:44 +01009269
Chris Wilsona35cdaa2013-06-25 17:26:45 +01009270 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
9271 pitch_limit = 32*1024;
9272 } else if (INTEL_INFO(dev)->gen >= 4) {
9273 if (obj->tiling_mode)
9274 pitch_limit = 16*1024;
9275 else
9276 pitch_limit = 32*1024;
9277 } else if (INTEL_INFO(dev)->gen >= 3) {
9278 if (obj->tiling_mode)
9279 pitch_limit = 8*1024;
9280 else
9281 pitch_limit = 16*1024;
9282 } else
9283 /* XXX DSPC is limited to 4k tiled */
9284 pitch_limit = 8*1024;
9285
9286 if (mode_cmd->pitches[0] > pitch_limit) {
9287 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
9288 obj->tiling_mode ? "tiled" : "linear",
9289 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02009290 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009291 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02009292
9293 if (obj->tiling_mode != I915_TILING_NONE &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009294 mode_cmd->pitches[0] != obj->stride) {
9295 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
9296 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02009297 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009298 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02009299
Ville Syrjälä57779d02012-10-31 17:50:14 +02009300 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009301 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +02009302 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02009303 case DRM_FORMAT_RGB565:
9304 case DRM_FORMAT_XRGB8888:
9305 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02009306 break;
9307 case DRM_FORMAT_XRGB1555:
9308 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009309 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +00009310 DRM_DEBUG("unsupported pixel format: %s\n",
9311 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +02009312 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009313 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02009314 break;
9315 case DRM_FORMAT_XBGR8888:
9316 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +02009317 case DRM_FORMAT_XRGB2101010:
9318 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02009319 case DRM_FORMAT_XBGR2101010:
9320 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009321 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +00009322 DRM_DEBUG("unsupported pixel format: %s\n",
9323 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +02009324 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009325 }
Jesse Barnesb5626742011-06-24 12:19:27 -07009326 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02009327 case DRM_FORMAT_YUYV:
9328 case DRM_FORMAT_UYVY:
9329 case DRM_FORMAT_YVYU:
9330 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009331 if (INTEL_INFO(dev)->gen < 5) {
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 }
Chris Wilson57cd6502010-08-08 12:34:44 +01009336 break;
9337 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +00009338 DRM_DEBUG("unsupported pixel format: %s\n",
9339 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +01009340 return -EINVAL;
9341 }
9342
Ville Syrjälä90f9a332012-10-31 17:50:19 +02009343 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
9344 if (mode_cmd->offsets[0] != 0)
9345 return -EINVAL;
9346
Daniel Vetterc7d73f62012-12-13 23:38:38 +01009347 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
9348 intel_fb->obj = obj;
9349
Jesse Barnes79e53942008-11-07 14:24:08 -08009350 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
9351 if (ret) {
9352 DRM_ERROR("framebuffer init failed %d\n", ret);
9353 return ret;
9354 }
9355
Jesse Barnes79e53942008-11-07 14:24:08 -08009356 return 0;
9357}
9358
Jesse Barnes79e53942008-11-07 14:24:08 -08009359static struct drm_framebuffer *
9360intel_user_framebuffer_create(struct drm_device *dev,
9361 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009362 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -08009363{
Chris Wilson05394f32010-11-08 19:18:58 +00009364 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08009365
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009366 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
9367 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +00009368 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +01009369 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -08009370
Chris Wilsond2dff872011-04-19 08:36:26 +01009371 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08009372}
9373
Jesse Barnes79e53942008-11-07 14:24:08 -08009374static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -08009375 .fb_create = intel_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +00009376 .output_poll_changed = intel_fb_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -08009377};
9378
Jesse Barnese70236a2009-09-21 10:42:27 -07009379/* Set up chip specific display functions */
9380static void intel_init_display(struct drm_device *dev)
9381{
9382 struct drm_i915_private *dev_priv = dev->dev_private;
9383
Daniel Vetteree9300b2013-06-03 22:40:22 +02009384 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
9385 dev_priv->display.find_dpll = g4x_find_best_dpll;
9386 else if (IS_VALLEYVIEW(dev))
9387 dev_priv->display.find_dpll = vlv_find_best_dpll;
9388 else if (IS_PINEVIEW(dev))
9389 dev_priv->display.find_dpll = pnv_find_best_dpll;
9390 else
9391 dev_priv->display.find_dpll = i9xx_find_best_dpll;
9392
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009393 if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009394 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009395 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02009396 dev_priv->display.crtc_enable = haswell_crtc_enable;
9397 dev_priv->display.crtc_disable = haswell_crtc_disable;
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009398 dev_priv->display.off = haswell_crtc_off;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009399 dev_priv->display.update_plane = ironlake_update_plane;
9400 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009401 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009402 dev_priv->display.get_clock = ironlake_crtc_clock_get;
Eric Anholtf564048e2011-03-30 13:01:02 -07009403 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +02009404 dev_priv->display.crtc_enable = ironlake_crtc_enable;
9405 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009406 dev_priv->display.off = ironlake_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -07009407 dev_priv->display.update_plane = ironlake_update_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -07009408 } else if (IS_VALLEYVIEW(dev)) {
9409 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009410 dev_priv->display.get_clock = i9xx_crtc_clock_get;
Jesse Barnes89b667f2013-04-18 14:51:36 -07009411 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
9412 dev_priv->display.crtc_enable = valleyview_crtc_enable;
9413 dev_priv->display.crtc_disable = i9xx_crtc_disable;
9414 dev_priv->display.off = i9xx_crtc_off;
9415 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07009416 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009417 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009418 dev_priv->display.get_clock = i9xx_crtc_clock_get;
Eric Anholtf564048e2011-03-30 13:01:02 -07009419 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +02009420 dev_priv->display.crtc_enable = i9xx_crtc_enable;
9421 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009422 dev_priv->display.off = i9xx_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -07009423 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07009424 }
Jesse Barnese70236a2009-09-21 10:42:27 -07009425
Jesse Barnese70236a2009-09-21 10:42:27 -07009426 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07009427 if (IS_VALLEYVIEW(dev))
9428 dev_priv->display.get_display_clock_speed =
9429 valleyview_get_display_clock_speed;
9430 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -07009431 dev_priv->display.get_display_clock_speed =
9432 i945_get_display_clock_speed;
9433 else if (IS_I915G(dev))
9434 dev_priv->display.get_display_clock_speed =
9435 i915_get_display_clock_speed;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05009436 else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07009437 dev_priv->display.get_display_clock_speed =
9438 i9xx_misc_get_display_clock_speed;
9439 else if (IS_I915GM(dev))
9440 dev_priv->display.get_display_clock_speed =
9441 i915gm_get_display_clock_speed;
9442 else if (IS_I865G(dev))
9443 dev_priv->display.get_display_clock_speed =
9444 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +02009445 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07009446 dev_priv->display.get_display_clock_speed =
9447 i855_get_display_clock_speed;
9448 else /* 852, 830 */
9449 dev_priv->display.get_display_clock_speed =
9450 i830_get_display_clock_speed;
9451
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08009452 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01009453 if (IS_GEN5(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -07009454 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08009455 dev_priv->display.write_eld = ironlake_write_eld;
Yuanhan Liu13982612010-12-15 15:42:31 +08009456 } else if (IS_GEN6(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -07009457 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08009458 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnes357555c2011-04-28 15:09:55 -07009459 } else if (IS_IVYBRIDGE(dev)) {
9460 /* FIXME: detect B0+ stepping and use auto training */
9461 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08009462 dev_priv->display.write_eld = ironlake_write_eld;
Daniel Vetter01a415f2012-10-27 15:58:40 +02009463 dev_priv->display.modeset_global_resources =
9464 ivb_modeset_global_resources;
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -03009465 } else if (IS_HASWELL(dev)) {
9466 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Wang Xingchao83358c852012-08-16 22:43:37 +08009467 dev_priv->display.write_eld = haswell_write_eld;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02009468 dev_priv->display.modeset_global_resources =
9469 haswell_modeset_global_resources;
Paulo Zanonia0e63c22012-12-06 11:12:39 -02009470 }
Jesse Barnes6067aae2011-04-28 15:04:31 -07009471 } else if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +08009472 dev_priv->display.write_eld = g4x_write_eld;
Jesse Barnese70236a2009-09-21 10:42:27 -07009473 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009474
9475 /* Default just returns -ENODEV to indicate unsupported */
9476 dev_priv->display.queue_flip = intel_default_queue_flip;
9477
9478 switch (INTEL_INFO(dev)->gen) {
9479 case 2:
9480 dev_priv->display.queue_flip = intel_gen2_queue_flip;
9481 break;
9482
9483 case 3:
9484 dev_priv->display.queue_flip = intel_gen3_queue_flip;
9485 break;
9486
9487 case 4:
9488 case 5:
9489 dev_priv->display.queue_flip = intel_gen4_queue_flip;
9490 break;
9491
9492 case 6:
9493 dev_priv->display.queue_flip = intel_gen6_queue_flip;
9494 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009495 case 7:
9496 dev_priv->display.queue_flip = intel_gen7_queue_flip;
9497 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009498 }
Jesse Barnese70236a2009-09-21 10:42:27 -07009499}
9500
Jesse Barnesb690e962010-07-19 13:53:12 -07009501/*
9502 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
9503 * resume, or other times. This quirk makes sure that's the case for
9504 * affected systems.
9505 */
Akshay Joshi0206e352011-08-16 15:34:10 -04009506static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -07009507{
9508 struct drm_i915_private *dev_priv = dev->dev_private;
9509
9510 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02009511 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -07009512}
9513
Keith Packard435793d2011-07-12 14:56:22 -07009514/*
9515 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
9516 */
9517static void quirk_ssc_force_disable(struct drm_device *dev)
9518{
9519 struct drm_i915_private *dev_priv = dev->dev_private;
9520 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02009521 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -07009522}
9523
Carsten Emde4dca20e2012-03-15 15:56:26 +01009524/*
Carsten Emde5a15ab52012-03-15 15:56:27 +01009525 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
9526 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +01009527 */
9528static void quirk_invert_brightness(struct drm_device *dev)
9529{
9530 struct drm_i915_private *dev_priv = dev->dev_private;
9531 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02009532 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -07009533}
9534
9535struct intel_quirk {
9536 int device;
9537 int subsystem_vendor;
9538 int subsystem_device;
9539 void (*hook)(struct drm_device *dev);
9540};
9541
Egbert Eich5f85f1762012-10-14 15:46:38 +02009542/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
9543struct intel_dmi_quirk {
9544 void (*hook)(struct drm_device *dev);
9545 const struct dmi_system_id (*dmi_id_list)[];
9546};
9547
9548static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
9549{
9550 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
9551 return 1;
9552}
9553
9554static const struct intel_dmi_quirk intel_dmi_quirks[] = {
9555 {
9556 .dmi_id_list = &(const struct dmi_system_id[]) {
9557 {
9558 .callback = intel_dmi_reverse_brightness,
9559 .ident = "NCR Corporation",
9560 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
9561 DMI_MATCH(DMI_PRODUCT_NAME, ""),
9562 },
9563 },
9564 { } /* terminating entry */
9565 },
9566 .hook = quirk_invert_brightness,
9567 },
9568};
9569
Ben Widawskyc43b5632012-04-16 14:07:40 -07009570static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -07009571 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -04009572 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -07009573
Jesse Barnesb690e962010-07-19 13:53:12 -07009574 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
9575 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
9576
Jesse Barnesb690e962010-07-19 13:53:12 -07009577 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
9578 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
9579
Daniel Vetterccd0d362012-10-10 23:13:59 +02009580 /* 830/845 need to leave pipe A & dpll A up */
Jesse Barnesb690e962010-07-19 13:53:12 -07009581 { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Daniel Vetterdcdaed62012-08-12 21:19:34 +02009582 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Keith Packard435793d2011-07-12 14:56:22 -07009583
9584 /* Lenovo U160 cannot use SSC on LVDS */
9585 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +02009586
9587 /* Sony Vaio Y cannot use SSC on LVDS */
9588 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +01009589
9590 /* Acer Aspire 5734Z must invert backlight brightness */
9591 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
Jani Nikula1ffff602013-01-22 12:50:34 +02009592
9593 /* Acer/eMachines G725 */
9594 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
Jani Nikula01e3a8f2013-01-22 12:50:35 +02009595
9596 /* Acer/eMachines e725 */
9597 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
Jani Nikula5559eca2013-01-22 12:50:36 +02009598
9599 /* Acer/Packard Bell NCL20 */
9600 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
Daniel Vetterac4199e2013-02-15 18:35:30 +01009601
9602 /* Acer Aspire 4736Z */
9603 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jesse Barnesb690e962010-07-19 13:53:12 -07009604};
9605
9606static void intel_init_quirks(struct drm_device *dev)
9607{
9608 struct pci_dev *d = dev->pdev;
9609 int i;
9610
9611 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
9612 struct intel_quirk *q = &intel_quirks[i];
9613
9614 if (d->device == q->device &&
9615 (d->subsystem_vendor == q->subsystem_vendor ||
9616 q->subsystem_vendor == PCI_ANY_ID) &&
9617 (d->subsystem_device == q->subsystem_device ||
9618 q->subsystem_device == PCI_ANY_ID))
9619 q->hook(dev);
9620 }
Egbert Eich5f85f1762012-10-14 15:46:38 +02009621 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
9622 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
9623 intel_dmi_quirks[i].hook(dev);
9624 }
Jesse Barnesb690e962010-07-19 13:53:12 -07009625}
9626
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009627/* Disable the VGA plane that we never use */
9628static void i915_disable_vga(struct drm_device *dev)
9629{
9630 struct drm_i915_private *dev_priv = dev->dev_private;
9631 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +02009632 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009633
9634 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -07009635 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009636 sr1 = inb(VGA_SR_DATA);
9637 outb(sr1 | 1<<5, VGA_SR_DATA);
9638 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
9639 udelay(300);
9640
9641 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9642 POSTING_READ(vga_reg);
9643}
9644
Daniel Vetterf8175862012-04-10 15:50:11 +02009645void intel_modeset_init_hw(struct drm_device *dev)
9646{
Paulo Zanonifa42e232013-01-25 16:59:11 -02009647 intel_init_power_well(dev);
Eugeni Dodonov0232e922012-07-06 15:42:36 -03009648
Eugeni Dodonova8f78b52012-06-28 15:55:35 -03009649 intel_prepare_ddi(dev);
9650
Daniel Vetterf8175862012-04-10 15:50:11 +02009651 intel_init_clock_gating(dev);
9652
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02009653 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02009654 intel_enable_gt_powersave(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02009655 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf8175862012-04-10 15:50:11 +02009656}
9657
Imre Deak7d708ee2013-04-17 14:04:50 +03009658void intel_modeset_suspend_hw(struct drm_device *dev)
9659{
9660 intel_suspend_hw(dev);
9661}
9662
Jesse Barnes79e53942008-11-07 14:24:08 -08009663void intel_modeset_init(struct drm_device *dev)
9664{
Jesse Barnes652c3932009-08-17 13:31:43 -07009665 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7f1f3852013-04-02 11:22:20 -07009666 int i, j, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08009667
9668 drm_mode_config_init(dev);
9669
9670 dev->mode_config.min_width = 0;
9671 dev->mode_config.min_height = 0;
9672
Dave Airlie019d96c2011-09-29 16:20:42 +01009673 dev->mode_config.preferred_depth = 24;
9674 dev->mode_config.prefer_shadow = 1;
9675
Laurent Pincharte6ecefa2012-05-17 13:27:23 +02009676 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -08009677
Jesse Barnesb690e962010-07-19 13:53:12 -07009678 intel_init_quirks(dev);
9679
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009680 intel_init_pm(dev);
9681
Ben Widawskye3c74752013-04-05 13:12:39 -07009682 if (INTEL_INFO(dev)->num_pipes == 0)
9683 return;
9684
Jesse Barnese70236a2009-09-21 10:42:27 -07009685 intel_init_display(dev);
9686
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009687 if (IS_GEN2(dev)) {
9688 dev->mode_config.max_width = 2048;
9689 dev->mode_config.max_height = 2048;
9690 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -07009691 dev->mode_config.max_width = 4096;
9692 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -08009693 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009694 dev->mode_config.max_width = 8192;
9695 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -08009696 }
Ben Widawsky5d4545a2013-01-17 12:45:15 -08009697 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -08009698
Zhao Yakui28c97732009-10-09 11:39:41 +08009699 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -07009700 INTEL_INFO(dev)->num_pipes,
9701 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -08009702
Ben Widawsky7eb552a2013-03-13 14:05:41 -07009703 for (i = 0; i < INTEL_INFO(dev)->num_pipes; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009704 intel_crtc_init(dev, i);
Jesse Barnes7f1f3852013-04-02 11:22:20 -07009705 for (j = 0; j < dev_priv->num_plane; j++) {
9706 ret = intel_plane_init(dev, i, j);
9707 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +03009708 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
9709 pipe_name(i), sprite_name(i, j), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -07009710 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009711 }
9712
Paulo Zanoni79f689a2012-10-05 12:05:52 -03009713 intel_cpu_pll_init(dev);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02009714 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009715
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009716 /* Just disable it once at startup */
9717 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009718 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +00009719
9720 /* Just in case the BIOS is doing something questionable. */
9721 intel_disable_fbc(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01009722}
Jesse Barnesd5bb0812011-01-05 12:01:26 -08009723
Daniel Vetter24929352012-07-02 20:28:59 +02009724static void
9725intel_connector_break_all_links(struct intel_connector *connector)
9726{
9727 connector->base.dpms = DRM_MODE_DPMS_OFF;
9728 connector->base.encoder = NULL;
9729 connector->encoder->connectors_active = false;
9730 connector->encoder->base.crtc = NULL;
9731}
9732
Daniel Vetter7fad7982012-07-04 17:51:47 +02009733static void intel_enable_pipe_a(struct drm_device *dev)
9734{
9735 struct intel_connector *connector;
9736 struct drm_connector *crt = NULL;
9737 struct intel_load_detect_pipe load_detect_temp;
9738
9739 /* We can't just switch on the pipe A, we need to set things up with a
9740 * proper mode and output configuration. As a gross hack, enable pipe A
9741 * by enabling the load detect pipe once. */
9742 list_for_each_entry(connector,
9743 &dev->mode_config.connector_list,
9744 base.head) {
9745 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
9746 crt = &connector->base;
9747 break;
9748 }
9749 }
9750
9751 if (!crt)
9752 return;
9753
9754 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
9755 intel_release_load_detect_pipe(crt, &load_detect_temp);
9756
9757
9758}
9759
Daniel Vetterfa555832012-10-10 23:14:00 +02009760static bool
9761intel_check_plane_mapping(struct intel_crtc *crtc)
9762{
Ben Widawsky7eb552a2013-03-13 14:05:41 -07009763 struct drm_device *dev = crtc->base.dev;
9764 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +02009765 u32 reg, val;
9766
Ben Widawsky7eb552a2013-03-13 14:05:41 -07009767 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +02009768 return true;
9769
9770 reg = DSPCNTR(!crtc->plane);
9771 val = I915_READ(reg);
9772
9773 if ((val & DISPLAY_PLANE_ENABLE) &&
9774 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
9775 return false;
9776
9777 return true;
9778}
9779
Daniel Vetter24929352012-07-02 20:28:59 +02009780static void intel_sanitize_crtc(struct intel_crtc *crtc)
9781{
9782 struct drm_device *dev = crtc->base.dev;
9783 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +02009784 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +02009785
Daniel Vetter24929352012-07-02 20:28:59 +02009786 /* Clear any frame start delays used for debugging left by the BIOS */
Daniel Vetter3b117c82013-04-17 20:15:07 +02009787 reg = PIPECONF(crtc->config.cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +02009788 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
9789
9790 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +02009791 * disable the crtc (and hence change the state) if it is wrong. Note
9792 * that gen4+ has a fixed plane -> pipe mapping. */
9793 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +02009794 struct intel_connector *connector;
9795 bool plane;
9796
Daniel Vetter24929352012-07-02 20:28:59 +02009797 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
9798 crtc->base.base.id);
9799
9800 /* Pipe has the wrong plane attached and the plane is active.
9801 * Temporarily change the plane mapping and disable everything
9802 * ... */
9803 plane = crtc->plane;
9804 crtc->plane = !plane;
9805 dev_priv->display.crtc_disable(&crtc->base);
9806 crtc->plane = plane;
9807
9808 /* ... and break all links. */
9809 list_for_each_entry(connector, &dev->mode_config.connector_list,
9810 base.head) {
9811 if (connector->encoder->base.crtc != &crtc->base)
9812 continue;
9813
9814 intel_connector_break_all_links(connector);
9815 }
9816
9817 WARN_ON(crtc->active);
9818 crtc->base.enabled = false;
9819 }
Daniel Vetter24929352012-07-02 20:28:59 +02009820
Daniel Vetter7fad7982012-07-04 17:51:47 +02009821 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
9822 crtc->pipe == PIPE_A && !crtc->active) {
9823 /* BIOS forgot to enable pipe A, this mostly happens after
9824 * resume. Force-enable the pipe to fix this, the update_dpms
9825 * call below we restore the pipe to the right state, but leave
9826 * the required bits on. */
9827 intel_enable_pipe_a(dev);
9828 }
9829
Daniel Vetter24929352012-07-02 20:28:59 +02009830 /* Adjust the state of the output pipe according to whether we
9831 * have active connectors/encoders. */
9832 intel_crtc_update_dpms(&crtc->base);
9833
9834 if (crtc->active != crtc->base.enabled) {
9835 struct intel_encoder *encoder;
9836
9837 /* This can happen either due to bugs in the get_hw_state
9838 * functions or because the pipe is force-enabled due to the
9839 * pipe A quirk. */
9840 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
9841 crtc->base.base.id,
9842 crtc->base.enabled ? "enabled" : "disabled",
9843 crtc->active ? "enabled" : "disabled");
9844
9845 crtc->base.enabled = crtc->active;
9846
9847 /* Because we only establish the connector -> encoder ->
9848 * crtc links if something is active, this means the
9849 * crtc is now deactivated. Break the links. connector
9850 * -> encoder links are only establish when things are
9851 * actually up, hence no need to break them. */
9852 WARN_ON(crtc->active);
9853
9854 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
9855 WARN_ON(encoder->connectors_active);
9856 encoder->base.crtc = NULL;
9857 }
9858 }
9859}
9860
9861static void intel_sanitize_encoder(struct intel_encoder *encoder)
9862{
9863 struct intel_connector *connector;
9864 struct drm_device *dev = encoder->base.dev;
9865
9866 /* We need to check both for a crtc link (meaning that the
9867 * encoder is active and trying to read from a pipe) and the
9868 * pipe itself being active. */
9869 bool has_active_crtc = encoder->base.crtc &&
9870 to_intel_crtc(encoder->base.crtc)->active;
9871
9872 if (encoder->connectors_active && !has_active_crtc) {
9873 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
9874 encoder->base.base.id,
9875 drm_get_encoder_name(&encoder->base));
9876
9877 /* Connector is active, but has no active pipe. This is
9878 * fallout from our resume register restoring. Disable
9879 * the encoder manually again. */
9880 if (encoder->base.crtc) {
9881 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
9882 encoder->base.base.id,
9883 drm_get_encoder_name(&encoder->base));
9884 encoder->disable(encoder);
9885 }
9886
9887 /* Inconsistent output/port/pipe state happens presumably due to
9888 * a bug in one of the get_hw_state functions. Or someplace else
9889 * in our code, like the register restore mess on resume. Clamp
9890 * things to off as a safer default. */
9891 list_for_each_entry(connector,
9892 &dev->mode_config.connector_list,
9893 base.head) {
9894 if (connector->encoder != encoder)
9895 continue;
9896
9897 intel_connector_break_all_links(connector);
9898 }
9899 }
9900 /* Enabled encoders without active connectors will be fixed in
9901 * the crtc fixup. */
9902}
9903
Daniel Vetter44cec742013-01-25 17:53:21 +01009904void i915_redisable_vga(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01009905{
9906 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +02009907 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01009908
9909 if (I915_READ(vga_reg) != VGA_DISP_DISABLE) {
9910 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
Ville Syrjälä209d5212013-01-25 21:44:48 +02009911 i915_disable_vga(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01009912 }
9913}
9914
Daniel Vetter30e984d2013-06-05 13:34:17 +02009915static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +02009916{
9917 struct drm_i915_private *dev_priv = dev->dev_private;
9918 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +02009919 struct intel_crtc *crtc;
9920 struct intel_encoder *encoder;
9921 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +02009922 int i;
Daniel Vetter24929352012-07-02 20:28:59 +02009923
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009924 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9925 base.head) {
Daniel Vetter88adfff2013-03-28 10:42:01 +01009926 memset(&crtc->config, 0, sizeof(crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +02009927
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009928 crtc->active = dev_priv->display.get_pipe_config(crtc,
9929 &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +02009930
9931 crtc->base.enabled = crtc->active;
9932
9933 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
9934 crtc->base.base.id,
9935 crtc->active ? "enabled" : "disabled");
9936 }
9937
Daniel Vetter53589012013-06-05 13:34:16 +02009938 /* FIXME: Smash this into the new shared dpll infrastructure. */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009939 if (HAS_DDI(dev))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009940 intel_ddi_setup_hw_pll_state(dev);
9941
Daniel Vetter53589012013-06-05 13:34:16 +02009942 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
9943 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
9944
9945 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
9946 pll->active = 0;
9947 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9948 base.head) {
9949 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
9950 pll->active++;
9951 }
9952 pll->refcount = pll->active;
9953
9954 DRM_DEBUG_KMS("%s hw state readout: refcount %i\n",
9955 pll->name, pll->refcount);
9956 }
9957
Daniel Vetter24929352012-07-02 20:28:59 +02009958 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9959 base.head) {
9960 pipe = 0;
9961
9962 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -07009963 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
9964 encoder->base.crtc = &crtc->base;
Jesse Barnes510d5f22013-07-01 15:50:17 -07009965 if (encoder->get_config)
Jesse Barnes045ac3b2013-05-14 17:08:26 -07009966 encoder->get_config(encoder, &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +02009967 } else {
9968 encoder->base.crtc = NULL;
9969 }
9970
9971 encoder->connectors_active = false;
9972 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe=%i\n",
9973 encoder->base.base.id,
9974 drm_get_encoder_name(&encoder->base),
9975 encoder->base.crtc ? "enabled" : "disabled",
9976 pipe);
9977 }
9978
Jesse Barnes510d5f22013-07-01 15:50:17 -07009979 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9980 base.head) {
9981 if (!crtc->active)
9982 continue;
9983 if (dev_priv->display.get_clock)
9984 dev_priv->display.get_clock(crtc,
9985 &crtc->config);
9986 }
9987
Daniel Vetter24929352012-07-02 20:28:59 +02009988 list_for_each_entry(connector, &dev->mode_config.connector_list,
9989 base.head) {
9990 if (connector->get_hw_state(connector)) {
9991 connector->base.dpms = DRM_MODE_DPMS_ON;
9992 connector->encoder->connectors_active = true;
9993 connector->base.encoder = &connector->encoder->base;
9994 } else {
9995 connector->base.dpms = DRM_MODE_DPMS_OFF;
9996 connector->base.encoder = NULL;
9997 }
9998 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
9999 connector->base.base.id,
10000 drm_get_connector_name(&connector->base),
10001 connector->base.encoder ? "enabled" : "disabled");
10002 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020010003}
10004
10005/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
10006 * and i915 state tracking structures. */
10007void intel_modeset_setup_hw_state(struct drm_device *dev,
10008 bool force_restore)
10009{
10010 struct drm_i915_private *dev_priv = dev->dev_private;
10011 enum pipe pipe;
10012 struct drm_plane *plane;
10013 struct intel_crtc *crtc;
10014 struct intel_encoder *encoder;
10015
10016 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020010017
Jesse Barnesbabea612013-06-26 18:57:38 +030010018 /*
10019 * Now that we have the config, copy it to each CRTC struct
10020 * Note that this could go away if we move to using crtc_config
10021 * checking everywhere.
10022 */
10023 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
10024 base.head) {
10025 if (crtc->active && i915_fastboot) {
10026 intel_crtc_mode_from_pipe_config(crtc, &crtc->config);
10027
10028 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
10029 crtc->base.base.id);
10030 drm_mode_debug_printmodeline(&crtc->base.mode);
10031 }
10032 }
10033
Daniel Vetter24929352012-07-02 20:28:59 +020010034 /* HW state is read out, now we need to sanitize this mess. */
10035 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10036 base.head) {
10037 intel_sanitize_encoder(encoder);
10038 }
10039
10040 for_each_pipe(pipe) {
10041 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
10042 intel_sanitize_crtc(crtc);
Daniel Vetterc0b03412013-05-28 12:05:54 +020010043 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020010044 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010045
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010046 if (force_restore) {
Daniel Vetterf30da182013-04-11 20:22:50 +020010047 /*
10048 * We need to use raw interfaces for restoring state to avoid
10049 * checking (bogus) intermediate states.
10050 */
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010051 for_each_pipe(pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070010052 struct drm_crtc *crtc =
10053 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020010054
10055 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
10056 crtc->fb);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010057 }
Jesse Barnesb5644d02013-03-26 13:25:27 -070010058 list_for_each_entry(plane, &dev->mode_config.plane_list, head)
10059 intel_plane_restore(plane);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010010060
10061 i915_redisable_vga(dev);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010062 } else {
10063 intel_modeset_update_staged_output_state(dev);
10064 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010065
10066 intel_modeset_check_state(dev);
Daniel Vetter2e938892012-10-11 20:08:24 +020010067
10068 drm_mode_config_reset(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010010069}
10070
10071void intel_modeset_gem_init(struct drm_device *dev)
10072{
Chris Wilson1833b132012-05-09 11:56:28 +010010073 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020010074
10075 intel_setup_overlay(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020010076
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010077 intel_modeset_setup_hw_state(dev, false);
Jesse Barnes79e53942008-11-07 14:24:08 -080010078}
10079
10080void intel_modeset_cleanup(struct drm_device *dev)
10081{
Jesse Barnes652c3932009-08-17 13:31:43 -070010082 struct drm_i915_private *dev_priv = dev->dev_private;
10083 struct drm_crtc *crtc;
10084 struct intel_crtc *intel_crtc;
10085
Daniel Vetterfd0c0642013-04-24 11:13:35 +020010086 /*
10087 * Interrupts and polling as the first thing to avoid creating havoc.
10088 * Too much stuff here (turning of rps, connectors, ...) would
10089 * experience fancy races otherwise.
10090 */
10091 drm_irq_uninstall(dev);
10092 cancel_work_sync(&dev_priv->hotplug_work);
10093 /*
10094 * Due to the hpd irq storm handling the hotplug work can re-arm the
10095 * poll handlers. Hence disable polling after hpd handling is shut down.
10096 */
Keith Packardf87ea762010-10-03 19:36:26 -070010097 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020010098
Jesse Barnes652c3932009-08-17 13:31:43 -070010099 mutex_lock(&dev->struct_mutex);
10100
Jesse Barnes723bfd72010-10-07 16:01:13 -070010101 intel_unregister_dsm_handler();
10102
Jesse Barnes652c3932009-08-17 13:31:43 -070010103 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
10104 /* Skip inactive CRTCs */
10105 if (!crtc->fb)
10106 continue;
10107
10108 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3dec0092010-08-20 21:40:52 +020010109 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070010110 }
10111
Chris Wilson973d04f2011-07-08 12:22:37 +010010112 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070010113
Daniel Vetter8090c6b2012-06-24 16:42:32 +020010114 intel_disable_gt_powersave(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +000010115
Daniel Vetter930ebb42012-06-29 23:32:16 +020010116 ironlake_teardown_rc6(dev);
10117
Kristian Høgsberg69341a52009-11-11 12:19:17 -050010118 mutex_unlock(&dev->struct_mutex);
10119
Chris Wilson1630fe72011-07-08 12:22:42 +010010120 /* flush any delayed tasks or pending work */
10121 flush_scheduled_work();
10122
Jani Nikuladc652f92013-04-12 15:18:38 +030010123 /* destroy backlight, if any, before the connectors */
10124 intel_panel_destroy_backlight(dev);
10125
Jesse Barnes79e53942008-11-07 14:24:08 -080010126 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010010127
10128 intel_cleanup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010129}
10130
Dave Airlie28d52042009-09-21 14:33:58 +100010131/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080010132 * Return which encoder is currently attached for connector.
10133 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010010134struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080010135{
Chris Wilsondf0e9242010-09-09 16:20:55 +010010136 return &intel_attached_encoder(connector)->base;
10137}
Jesse Barnes79e53942008-11-07 14:24:08 -080010138
Chris Wilsondf0e9242010-09-09 16:20:55 +010010139void intel_connector_attach_encoder(struct intel_connector *connector,
10140 struct intel_encoder *encoder)
10141{
10142 connector->encoder = encoder;
10143 drm_mode_connector_attach_encoder(&connector->base,
10144 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080010145}
Dave Airlie28d52042009-09-21 14:33:58 +100010146
10147/*
10148 * set vga decode state - true == enable VGA decode
10149 */
10150int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
10151{
10152 struct drm_i915_private *dev_priv = dev->dev_private;
10153 u16 gmch_ctrl;
10154
10155 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
10156 if (state)
10157 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
10158 else
10159 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
10160 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
10161 return 0;
10162}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010163
10164#ifdef CONFIG_DEBUG_FS
10165#include <linux/seq_file.h>
10166
10167struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010168
10169 u32 power_well_driver;
10170
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010171 struct intel_cursor_error_state {
10172 u32 control;
10173 u32 position;
10174 u32 base;
10175 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010010176 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010177
10178 struct intel_pipe_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010179 enum transcoder cpu_transcoder;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010180 u32 conf;
10181 u32 source;
10182
10183 u32 htotal;
10184 u32 hblank;
10185 u32 hsync;
10186 u32 vtotal;
10187 u32 vblank;
10188 u32 vsync;
Damien Lespiau52331302012-08-15 19:23:25 +010010189 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010190
10191 struct intel_plane_error_state {
10192 u32 control;
10193 u32 stride;
10194 u32 size;
10195 u32 pos;
10196 u32 addr;
10197 u32 surface;
10198 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010010199 } plane[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010200};
10201
10202struct intel_display_error_state *
10203intel_display_capture_error_state(struct drm_device *dev)
10204{
Akshay Joshi0206e352011-08-16 15:34:10 -040010205 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010206 struct intel_display_error_state *error;
Paulo Zanoni702e7a52012-10-23 18:29:59 -020010207 enum transcoder cpu_transcoder;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010208 int i;
10209
10210 error = kmalloc(sizeof(*error), GFP_ATOMIC);
10211 if (error == NULL)
10212 return NULL;
10213
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010214 if (HAS_POWER_WELL(dev))
10215 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
10216
Damien Lespiau52331302012-08-15 19:23:25 +010010217 for_each_pipe(i) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -020010218 cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv, i);
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010219 error->pipe[i].cpu_transcoder = cpu_transcoder;
Paulo Zanoni702e7a52012-10-23 18:29:59 -020010220
Paulo Zanonia18c4c32013-03-06 20:03:12 -030010221 if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev)) {
10222 error->cursor[i].control = I915_READ(CURCNTR(i));
10223 error->cursor[i].position = I915_READ(CURPOS(i));
10224 error->cursor[i].base = I915_READ(CURBASE(i));
10225 } else {
10226 error->cursor[i].control = I915_READ(CURCNTR_IVB(i));
10227 error->cursor[i].position = I915_READ(CURPOS_IVB(i));
10228 error->cursor[i].base = I915_READ(CURBASE_IVB(i));
10229 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010230
10231 error->plane[i].control = I915_READ(DSPCNTR(i));
10232 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030010233 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030010234 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030010235 error->plane[i].pos = I915_READ(DSPPOS(i));
10236 }
Paulo Zanonica291362013-03-06 20:03:14 -030010237 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
10238 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010239 if (INTEL_INFO(dev)->gen >= 4) {
10240 error->plane[i].surface = I915_READ(DSPSURF(i));
10241 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
10242 }
10243
Paulo Zanoni702e7a52012-10-23 18:29:59 -020010244 error->pipe[i].conf = I915_READ(PIPECONF(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010245 error->pipe[i].source = I915_READ(PIPESRC(i));
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010246 error->pipe[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
10247 error->pipe[i].hblank = I915_READ(HBLANK(cpu_transcoder));
10248 error->pipe[i].hsync = I915_READ(HSYNC(cpu_transcoder));
10249 error->pipe[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
10250 error->pipe[i].vblank = I915_READ(VBLANK(cpu_transcoder));
10251 error->pipe[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010252 }
10253
Paulo Zanoni12d217c2013-05-03 12:15:38 -030010254 /* In the code above we read the registers without checking if the power
10255 * well was on, so here we have to clear the FPGA_DBG_RM_NOCLAIM bit to
10256 * prevent the next I915_WRITE from detecting it and printing an error
10257 * message. */
10258 if (HAS_POWER_WELL(dev))
10259 I915_WRITE_NOTRACE(FPGA_DBG, FPGA_DBG_RM_NOCLAIM);
10260
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010261 return error;
10262}
10263
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010264#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
10265
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010266void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010267intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010268 struct drm_device *dev,
10269 struct intel_display_error_state *error)
10270{
10271 int i;
10272
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010273 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010274 if (HAS_POWER_WELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010275 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010276 error->power_well_driver);
Damien Lespiau52331302012-08-15 19:23:25 +010010277 for_each_pipe(i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010278 err_printf(m, "Pipe [%d]:\n", i);
10279 err_printf(m, " CPU transcoder: %c\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010280 transcoder_name(error->pipe[i].cpu_transcoder));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010281 err_printf(m, " CONF: %08x\n", error->pipe[i].conf);
10282 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
10283 err_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
10284 err_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
10285 err_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
10286 err_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
10287 err_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
10288 err_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010289
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010290 err_printf(m, "Plane [%d]:\n", i);
10291 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
10292 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030010293 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010294 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
10295 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030010296 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030010297 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010298 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010299 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010300 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
10301 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010302 }
10303
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010304 err_printf(m, "Cursor [%d]:\n", i);
10305 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
10306 err_printf(m, " POS: %08x\n", error->cursor[i].position);
10307 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010308 }
10309}
10310#endif