blob: 9c05e57fb7ae911413c643e790be99e859d9c774 [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
1983 DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
1984 obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001985 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001986 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetterc2c75132012-07-05 12:17:30 +02001987 I915_MODIFY_DISPBASE(DSPSURF(plane),
1988 obj->gtt_offset + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01001989 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02001990 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01001991 } else
Daniel Vettere506a0c2012-07-05 12:17:29 +02001992 I915_WRITE(DSPADDR(plane), obj->gtt_offset + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01001993 POSTING_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07001994
Jesse Barnes17638cd2011-06-24 12:19:23 -07001995 return 0;
1996}
1997
1998static int ironlake_update_plane(struct drm_crtc *crtc,
1999 struct drm_framebuffer *fb, int x, int y)
2000{
2001 struct drm_device *dev = crtc->dev;
2002 struct drm_i915_private *dev_priv = dev->dev_private;
2003 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2004 struct intel_framebuffer *intel_fb;
2005 struct drm_i915_gem_object *obj;
2006 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002007 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002008 u32 dspcntr;
2009 u32 reg;
2010
2011 switch (plane) {
2012 case 0:
2013 case 1:
Jesse Barnes27f82272011-09-02 12:54:37 -07002014 case 2:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002015 break;
2016 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002017 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes17638cd2011-06-24 12:19:23 -07002018 return -EINVAL;
2019 }
2020
2021 intel_fb = to_intel_framebuffer(fb);
2022 obj = intel_fb->obj;
2023
2024 reg = DSPCNTR(plane);
2025 dspcntr = I915_READ(reg);
2026 /* Mask out pixel format bits in case we change it */
2027 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002028 switch (fb->pixel_format) {
2029 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002030 dspcntr |= DISPPLANE_8BPP;
2031 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002032 case DRM_FORMAT_RGB565:
2033 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002034 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002035 case DRM_FORMAT_XRGB8888:
2036 case DRM_FORMAT_ARGB8888:
2037 dspcntr |= DISPPLANE_BGRX888;
2038 break;
2039 case DRM_FORMAT_XBGR8888:
2040 case DRM_FORMAT_ABGR8888:
2041 dspcntr |= DISPPLANE_RGBX888;
2042 break;
2043 case DRM_FORMAT_XRGB2101010:
2044 case DRM_FORMAT_ARGB2101010:
2045 dspcntr |= DISPPLANE_BGRX101010;
2046 break;
2047 case DRM_FORMAT_XBGR2101010:
2048 case DRM_FORMAT_ABGR2101010:
2049 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002050 break;
2051 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002052 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002053 }
2054
2055 if (obj->tiling_mode != I915_TILING_NONE)
2056 dspcntr |= DISPPLANE_TILED;
2057 else
2058 dspcntr &= ~DISPPLANE_TILED;
2059
2060 /* must disable */
2061 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2062
2063 I915_WRITE(reg, dspcntr);
2064
Daniel Vettere506a0c2012-07-05 12:17:29 +02002065 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002066 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002067 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2068 fb->bits_per_pixel / 8,
2069 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002070 linear_offset -= intel_crtc->dspaddr_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002071
Daniel Vettere506a0c2012-07-05 12:17:29 +02002072 DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
2073 obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002074 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002075 I915_MODIFY_DISPBASE(DSPSURF(plane),
2076 obj->gtt_offset + intel_crtc->dspaddr_offset);
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002077 if (IS_HASWELL(dev)) {
2078 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2079 } else {
2080 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2081 I915_WRITE(DSPLINOFF(plane), linear_offset);
2082 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002083 POSTING_READ(reg);
2084
2085 return 0;
2086}
2087
2088/* Assume fb object is pinned & idle & fenced and just update base pointers */
2089static int
2090intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2091 int x, int y, enum mode_set_atomic state)
2092{
2093 struct drm_device *dev = crtc->dev;
2094 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002095
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002096 if (dev_priv->display.disable_fbc)
2097 dev_priv->display.disable_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02002098 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07002099
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002100 return dev_priv->display.update_plane(crtc, fb, x, y);
Jesse Barnes81255562010-08-02 12:07:50 -07002101}
2102
Ville Syrjälä96a02912013-02-18 19:08:49 +02002103void intel_display_handle_reset(struct drm_device *dev)
2104{
2105 struct drm_i915_private *dev_priv = dev->dev_private;
2106 struct drm_crtc *crtc;
2107
2108 /*
2109 * Flips in the rings have been nuked by the reset,
2110 * so complete all pending flips so that user space
2111 * will get its events and not get stuck.
2112 *
2113 * Also update the base address of all primary
2114 * planes to the the last fb to make sure we're
2115 * showing the correct fb after a reset.
2116 *
2117 * Need to make two loops over the crtcs so that we
2118 * don't try to grab a crtc mutex before the
2119 * pending_flip_queue really got woken up.
2120 */
2121
2122 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2123 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2124 enum plane plane = intel_crtc->plane;
2125
2126 intel_prepare_page_flip(dev, plane);
2127 intel_finish_page_flip_plane(dev, plane);
2128 }
2129
2130 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2131 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2132
2133 mutex_lock(&crtc->mutex);
2134 if (intel_crtc->active)
2135 dev_priv->display.update_plane(crtc, crtc->fb,
2136 crtc->x, crtc->y);
2137 mutex_unlock(&crtc->mutex);
2138 }
2139}
2140
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002141static int
Chris Wilson14667a42012-04-03 17:58:35 +01002142intel_finish_fb(struct drm_framebuffer *old_fb)
2143{
2144 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2145 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2146 bool was_interruptible = dev_priv->mm.interruptible;
2147 int ret;
2148
Chris Wilson14667a42012-04-03 17:58:35 +01002149 /* Big Hammer, we also need to ensure that any pending
2150 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2151 * current scanout is retired before unpinning the old
2152 * framebuffer.
2153 *
2154 * This should only fail upon a hung GPU, in which case we
2155 * can safely continue.
2156 */
2157 dev_priv->mm.interruptible = false;
2158 ret = i915_gem_object_finish_gpu(obj);
2159 dev_priv->mm.interruptible = was_interruptible;
2160
2161 return ret;
2162}
2163
Ville Syrjälä198598d2012-10-31 17:50:24 +02002164static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
2165{
2166 struct drm_device *dev = crtc->dev;
2167 struct drm_i915_master_private *master_priv;
2168 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2169
2170 if (!dev->primary->master)
2171 return;
2172
2173 master_priv = dev->primary->master->driver_priv;
2174 if (!master_priv->sarea_priv)
2175 return;
2176
2177 switch (intel_crtc->pipe) {
2178 case 0:
2179 master_priv->sarea_priv->pipeA_x = x;
2180 master_priv->sarea_priv->pipeA_y = y;
2181 break;
2182 case 1:
2183 master_priv->sarea_priv->pipeB_x = x;
2184 master_priv->sarea_priv->pipeB_y = y;
2185 break;
2186 default:
2187 break;
2188 }
2189}
2190
Chris Wilson14667a42012-04-03 17:58:35 +01002191static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002192intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002193 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002194{
2195 struct drm_device *dev = crtc->dev;
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002196 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08002197 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter94352cf2012-07-05 22:51:56 +02002198 struct drm_framebuffer *old_fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002199 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002200
2201 /* no fb bound */
Daniel Vetter94352cf2012-07-05 22:51:56 +02002202 if (!fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002203 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002204 return 0;
2205 }
2206
Ben Widawsky7eb552a2013-03-13 14:05:41 -07002207 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002208 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2209 plane_name(intel_crtc->plane),
2210 INTEL_INFO(dev)->num_pipes);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002211 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002212 }
2213
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002214 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01002215 ret = intel_pin_and_fence_fb_obj(dev,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002216 to_intel_framebuffer(fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002217 NULL);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002218 if (ret != 0) {
2219 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002220 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002221 return ret;
2222 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002223
Daniel Vetter94352cf2012-07-05 22:51:56 +02002224 ret = dev_priv->display.update_plane(crtc, fb, x, y);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002225 if (ret) {
Daniel Vetter94352cf2012-07-05 22:51:56 +02002226 intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002227 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002228 DRM_ERROR("failed to update base address\n");
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002229 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002230 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002231
Daniel Vetter94352cf2012-07-05 22:51:56 +02002232 old_fb = crtc->fb;
2233 crtc->fb = fb;
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02002234 crtc->x = x;
2235 crtc->y = y;
Daniel Vetter94352cf2012-07-05 22:51:56 +02002236
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002237 if (old_fb) {
Daniel Vetterd7697ee2013-06-02 17:23:01 +02002238 if (intel_crtc->active && old_fb != fb)
2239 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002240 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002241 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002242
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002243 intel_update_fbc(dev);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002244 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002245
Ville Syrjälä198598d2012-10-31 17:50:24 +02002246 intel_crtc_update_sarea_pos(crtc, x, y);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002247
2248 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002249}
2250
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002251static void intel_fdi_normal_train(struct drm_crtc *crtc)
2252{
2253 struct drm_device *dev = crtc->dev;
2254 struct drm_i915_private *dev_priv = dev->dev_private;
2255 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2256 int pipe = intel_crtc->pipe;
2257 u32 reg, temp;
2258
2259 /* enable normal train */
2260 reg = FDI_TX_CTL(pipe);
2261 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002262 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002263 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2264 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002265 } else {
2266 temp &= ~FDI_LINK_TRAIN_NONE;
2267 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002268 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002269 I915_WRITE(reg, temp);
2270
2271 reg = FDI_RX_CTL(pipe);
2272 temp = I915_READ(reg);
2273 if (HAS_PCH_CPT(dev)) {
2274 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2275 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2276 } else {
2277 temp &= ~FDI_LINK_TRAIN_NONE;
2278 temp |= FDI_LINK_TRAIN_NONE;
2279 }
2280 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2281
2282 /* wait one idle pattern time */
2283 POSTING_READ(reg);
2284 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002285
2286 /* IVB wants error correction enabled */
2287 if (IS_IVYBRIDGE(dev))
2288 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2289 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002290}
2291
Daniel Vetter1e833f42013-02-19 22:31:57 +01002292static bool pipe_has_enabled_pch(struct intel_crtc *intel_crtc)
2293{
2294 return intel_crtc->base.enabled && intel_crtc->config.has_pch_encoder;
2295}
2296
Daniel Vetter01a415f2012-10-27 15:58:40 +02002297static void ivb_modeset_global_resources(struct drm_device *dev)
2298{
2299 struct drm_i915_private *dev_priv = dev->dev_private;
2300 struct intel_crtc *pipe_B_crtc =
2301 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2302 struct intel_crtc *pipe_C_crtc =
2303 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2304 uint32_t temp;
2305
Daniel Vetter1e833f42013-02-19 22:31:57 +01002306 /*
2307 * When everything is off disable fdi C so that we could enable fdi B
2308 * with all lanes. Note that we don't care about enabled pipes without
2309 * an enabled pch encoder.
2310 */
2311 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2312 !pipe_has_enabled_pch(pipe_C_crtc)) {
Daniel Vetter01a415f2012-10-27 15:58:40 +02002313 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2314 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2315
2316 temp = I915_READ(SOUTH_CHICKEN1);
2317 temp &= ~FDI_BC_BIFURCATION_SELECT;
2318 DRM_DEBUG_KMS("disabling fdi C rx\n");
2319 I915_WRITE(SOUTH_CHICKEN1, temp);
2320 }
2321}
2322
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002323/* The FDI link training functions for ILK/Ibexpeak. */
2324static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2325{
2326 struct drm_device *dev = crtc->dev;
2327 struct drm_i915_private *dev_priv = dev->dev_private;
2328 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2329 int pipe = intel_crtc->pipe;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002330 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002331 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002332
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002333 /* FDI needs bits from pipe & plane first */
2334 assert_pipe_enabled(dev_priv, pipe);
2335 assert_plane_enabled(dev_priv, plane);
2336
Adam Jacksone1a44742010-06-25 15:32:14 -04002337 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2338 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002339 reg = FDI_RX_IMR(pipe);
2340 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002341 temp &= ~FDI_RX_SYMBOL_LOCK;
2342 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002343 I915_WRITE(reg, temp);
2344 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002345 udelay(150);
2346
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002347 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002348 reg = FDI_TX_CTL(pipe);
2349 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002350 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2351 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002352 temp &= ~FDI_LINK_TRAIN_NONE;
2353 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002354 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002355
Chris Wilson5eddb702010-09-11 13:48:45 +01002356 reg = FDI_RX_CTL(pipe);
2357 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002358 temp &= ~FDI_LINK_TRAIN_NONE;
2359 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002360 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2361
2362 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002363 udelay(150);
2364
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002365 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002366 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2367 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2368 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002369
Chris Wilson5eddb702010-09-11 13:48:45 +01002370 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002371 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002372 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002373 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2374
2375 if ((temp & FDI_RX_BIT_LOCK)) {
2376 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002377 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002378 break;
2379 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002380 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002381 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002382 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002383
2384 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002385 reg = FDI_TX_CTL(pipe);
2386 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002387 temp &= ~FDI_LINK_TRAIN_NONE;
2388 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002389 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002390
Chris Wilson5eddb702010-09-11 13:48:45 +01002391 reg = FDI_RX_CTL(pipe);
2392 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002393 temp &= ~FDI_LINK_TRAIN_NONE;
2394 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002395 I915_WRITE(reg, temp);
2396
2397 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002398 udelay(150);
2399
Chris Wilson5eddb702010-09-11 13:48:45 +01002400 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002401 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002402 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002403 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2404
2405 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002406 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002407 DRM_DEBUG_KMS("FDI train 2 done.\n");
2408 break;
2409 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002410 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002411 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002412 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002413
2414 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002415
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002416}
2417
Akshay Joshi0206e352011-08-16 15:34:10 -04002418static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002419 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2420 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2421 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2422 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2423};
2424
2425/* The FDI link training functions for SNB/Cougarpoint. */
2426static void gen6_fdi_link_train(struct drm_crtc *crtc)
2427{
2428 struct drm_device *dev = crtc->dev;
2429 struct drm_i915_private *dev_priv = dev->dev_private;
2430 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2431 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05002432 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002433
Adam Jacksone1a44742010-06-25 15:32:14 -04002434 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2435 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002436 reg = FDI_RX_IMR(pipe);
2437 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002438 temp &= ~FDI_RX_SYMBOL_LOCK;
2439 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002440 I915_WRITE(reg, temp);
2441
2442 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002443 udelay(150);
2444
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002445 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002446 reg = FDI_TX_CTL(pipe);
2447 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002448 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2449 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002450 temp &= ~FDI_LINK_TRAIN_NONE;
2451 temp |= FDI_LINK_TRAIN_PATTERN_1;
2452 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2453 /* SNB-B */
2454 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002455 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002456
Daniel Vetterd74cf322012-10-26 10:58:13 +02002457 I915_WRITE(FDI_RX_MISC(pipe),
2458 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2459
Chris Wilson5eddb702010-09-11 13:48:45 +01002460 reg = FDI_RX_CTL(pipe);
2461 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002462 if (HAS_PCH_CPT(dev)) {
2463 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2464 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2465 } else {
2466 temp &= ~FDI_LINK_TRAIN_NONE;
2467 temp |= FDI_LINK_TRAIN_PATTERN_1;
2468 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002469 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2470
2471 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002472 udelay(150);
2473
Akshay Joshi0206e352011-08-16 15:34:10 -04002474 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002475 reg = FDI_TX_CTL(pipe);
2476 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002477 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2478 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002479 I915_WRITE(reg, temp);
2480
2481 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002482 udelay(500);
2483
Sean Paulfa37d392012-03-02 12:53:39 -05002484 for (retry = 0; retry < 5; retry++) {
2485 reg = FDI_RX_IIR(pipe);
2486 temp = I915_READ(reg);
2487 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2488 if (temp & FDI_RX_BIT_LOCK) {
2489 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2490 DRM_DEBUG_KMS("FDI train 1 done.\n");
2491 break;
2492 }
2493 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002494 }
Sean Paulfa37d392012-03-02 12:53:39 -05002495 if (retry < 5)
2496 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002497 }
2498 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002499 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002500
2501 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002502 reg = FDI_TX_CTL(pipe);
2503 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002504 temp &= ~FDI_LINK_TRAIN_NONE;
2505 temp |= FDI_LINK_TRAIN_PATTERN_2;
2506 if (IS_GEN6(dev)) {
2507 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2508 /* SNB-B */
2509 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2510 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002511 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002512
Chris Wilson5eddb702010-09-11 13:48:45 +01002513 reg = FDI_RX_CTL(pipe);
2514 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002515 if (HAS_PCH_CPT(dev)) {
2516 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2517 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2518 } else {
2519 temp &= ~FDI_LINK_TRAIN_NONE;
2520 temp |= FDI_LINK_TRAIN_PATTERN_2;
2521 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002522 I915_WRITE(reg, temp);
2523
2524 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002525 udelay(150);
2526
Akshay Joshi0206e352011-08-16 15:34:10 -04002527 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002528 reg = FDI_TX_CTL(pipe);
2529 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002530 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2531 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002532 I915_WRITE(reg, temp);
2533
2534 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002535 udelay(500);
2536
Sean Paulfa37d392012-03-02 12:53:39 -05002537 for (retry = 0; retry < 5; retry++) {
2538 reg = FDI_RX_IIR(pipe);
2539 temp = I915_READ(reg);
2540 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2541 if (temp & FDI_RX_SYMBOL_LOCK) {
2542 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2543 DRM_DEBUG_KMS("FDI train 2 done.\n");
2544 break;
2545 }
2546 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002547 }
Sean Paulfa37d392012-03-02 12:53:39 -05002548 if (retry < 5)
2549 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002550 }
2551 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002552 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002553
2554 DRM_DEBUG_KMS("FDI train done.\n");
2555}
2556
Jesse Barnes357555c2011-04-28 15:09:55 -07002557/* Manual link training for Ivy Bridge A0 parts */
2558static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2559{
2560 struct drm_device *dev = crtc->dev;
2561 struct drm_i915_private *dev_priv = dev->dev_private;
2562 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2563 int pipe = intel_crtc->pipe;
2564 u32 reg, temp, i;
2565
2566 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2567 for train result */
2568 reg = FDI_RX_IMR(pipe);
2569 temp = I915_READ(reg);
2570 temp &= ~FDI_RX_SYMBOL_LOCK;
2571 temp &= ~FDI_RX_BIT_LOCK;
2572 I915_WRITE(reg, temp);
2573
2574 POSTING_READ(reg);
2575 udelay(150);
2576
Daniel Vetter01a415f2012-10-27 15:58:40 +02002577 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
2578 I915_READ(FDI_RX_IIR(pipe)));
2579
Jesse Barnes357555c2011-04-28 15:09:55 -07002580 /* enable CPU FDI TX and PCH FDI RX */
2581 reg = FDI_TX_CTL(pipe);
2582 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002583 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2584 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Jesse Barnes357555c2011-04-28 15:09:55 -07002585 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2586 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
2587 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2588 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002589 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002590 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2591
Daniel Vetterd74cf322012-10-26 10:58:13 +02002592 I915_WRITE(FDI_RX_MISC(pipe),
2593 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2594
Jesse Barnes357555c2011-04-28 15:09:55 -07002595 reg = FDI_RX_CTL(pipe);
2596 temp = I915_READ(reg);
2597 temp &= ~FDI_LINK_TRAIN_AUTO;
2598 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2599 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002600 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002601 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2602
2603 POSTING_READ(reg);
2604 udelay(150);
2605
Akshay Joshi0206e352011-08-16 15:34:10 -04002606 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002607 reg = FDI_TX_CTL(pipe);
2608 temp = I915_READ(reg);
2609 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2610 temp |= snb_b_fdi_train_param[i];
2611 I915_WRITE(reg, temp);
2612
2613 POSTING_READ(reg);
2614 udelay(500);
2615
2616 reg = FDI_RX_IIR(pipe);
2617 temp = I915_READ(reg);
2618 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2619
2620 if (temp & FDI_RX_BIT_LOCK ||
2621 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2622 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Daniel Vetter01a415f2012-10-27 15:58:40 +02002623 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n", i);
Jesse Barnes357555c2011-04-28 15:09:55 -07002624 break;
2625 }
2626 }
2627 if (i == 4)
2628 DRM_ERROR("FDI train 1 fail!\n");
2629
2630 /* Train 2 */
2631 reg = FDI_TX_CTL(pipe);
2632 temp = I915_READ(reg);
2633 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2634 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2635 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2636 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2637 I915_WRITE(reg, temp);
2638
2639 reg = FDI_RX_CTL(pipe);
2640 temp = I915_READ(reg);
2641 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2642 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2643 I915_WRITE(reg, temp);
2644
2645 POSTING_READ(reg);
2646 udelay(150);
2647
Akshay Joshi0206e352011-08-16 15:34:10 -04002648 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002649 reg = FDI_TX_CTL(pipe);
2650 temp = I915_READ(reg);
2651 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2652 temp |= snb_b_fdi_train_param[i];
2653 I915_WRITE(reg, temp);
2654
2655 POSTING_READ(reg);
2656 udelay(500);
2657
2658 reg = FDI_RX_IIR(pipe);
2659 temp = I915_READ(reg);
2660 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2661
2662 if (temp & FDI_RX_SYMBOL_LOCK) {
2663 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Daniel Vetter01a415f2012-10-27 15:58:40 +02002664 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n", i);
Jesse Barnes357555c2011-04-28 15:09:55 -07002665 break;
2666 }
2667 }
2668 if (i == 4)
2669 DRM_ERROR("FDI train 2 fail!\n");
2670
2671 DRM_DEBUG_KMS("FDI train done.\n");
2672}
2673
Daniel Vetter88cefb62012-08-12 19:27:14 +02002674static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07002675{
Daniel Vetter88cefb62012-08-12 19:27:14 +02002676 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002677 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002678 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002679 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002680
Jesse Barnesc64e3112010-09-10 11:27:03 -07002681
Jesse Barnes0e23b992010-09-10 11:10:00 -07002682 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01002683 reg = FDI_RX_CTL(pipe);
2684 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002685 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
2686 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002687 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01002688 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2689
2690 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002691 udelay(200);
2692
2693 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002694 temp = I915_READ(reg);
2695 I915_WRITE(reg, temp | FDI_PCDCLK);
2696
2697 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002698 udelay(200);
2699
Paulo Zanoni20749732012-11-23 15:30:38 -02002700 /* Enable CPU FDI TX PLL, always on for Ironlake */
2701 reg = FDI_TX_CTL(pipe);
2702 temp = I915_READ(reg);
2703 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2704 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01002705
Paulo Zanoni20749732012-11-23 15:30:38 -02002706 POSTING_READ(reg);
2707 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002708 }
2709}
2710
Daniel Vetter88cefb62012-08-12 19:27:14 +02002711static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
2712{
2713 struct drm_device *dev = intel_crtc->base.dev;
2714 struct drm_i915_private *dev_priv = dev->dev_private;
2715 int pipe = intel_crtc->pipe;
2716 u32 reg, temp;
2717
2718 /* Switch from PCDclk to Rawclk */
2719 reg = FDI_RX_CTL(pipe);
2720 temp = I915_READ(reg);
2721 I915_WRITE(reg, temp & ~FDI_PCDCLK);
2722
2723 /* Disable CPU FDI TX PLL */
2724 reg = FDI_TX_CTL(pipe);
2725 temp = I915_READ(reg);
2726 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2727
2728 POSTING_READ(reg);
2729 udelay(100);
2730
2731 reg = FDI_RX_CTL(pipe);
2732 temp = I915_READ(reg);
2733 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2734
2735 /* Wait for the clocks to turn off. */
2736 POSTING_READ(reg);
2737 udelay(100);
2738}
2739
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002740static void ironlake_fdi_disable(struct drm_crtc *crtc)
2741{
2742 struct drm_device *dev = crtc->dev;
2743 struct drm_i915_private *dev_priv = dev->dev_private;
2744 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2745 int pipe = intel_crtc->pipe;
2746 u32 reg, temp;
2747
2748 /* disable CPU FDI tx and PCH FDI rx */
2749 reg = FDI_TX_CTL(pipe);
2750 temp = I915_READ(reg);
2751 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2752 POSTING_READ(reg);
2753
2754 reg = FDI_RX_CTL(pipe);
2755 temp = I915_READ(reg);
2756 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002757 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002758 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2759
2760 POSTING_READ(reg);
2761 udelay(100);
2762
2763 /* Ironlake workaround, disable clock pointer after downing FDI */
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002764 if (HAS_PCH_IBX(dev)) {
2765 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002766 }
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002767
2768 /* still set train pattern 1 */
2769 reg = FDI_TX_CTL(pipe);
2770 temp = I915_READ(reg);
2771 temp &= ~FDI_LINK_TRAIN_NONE;
2772 temp |= FDI_LINK_TRAIN_PATTERN_1;
2773 I915_WRITE(reg, temp);
2774
2775 reg = FDI_RX_CTL(pipe);
2776 temp = I915_READ(reg);
2777 if (HAS_PCH_CPT(dev)) {
2778 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2779 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2780 } else {
2781 temp &= ~FDI_LINK_TRAIN_NONE;
2782 temp |= FDI_LINK_TRAIN_PATTERN_1;
2783 }
2784 /* BPC in FDI rx is consistent with that in PIPECONF */
2785 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002786 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002787 I915_WRITE(reg, temp);
2788
2789 POSTING_READ(reg);
2790 udelay(100);
2791}
2792
Chris Wilson5bb61642012-09-27 21:25:58 +01002793static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2794{
2795 struct drm_device *dev = crtc->dev;
2796 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä10d83732013-01-29 18:13:34 +02002797 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5bb61642012-09-27 21:25:58 +01002798 unsigned long flags;
2799 bool pending;
2800
Ville Syrjälä10d83732013-01-29 18:13:34 +02002801 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2802 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
Chris Wilson5bb61642012-09-27 21:25:58 +01002803 return false;
2804
2805 spin_lock_irqsave(&dev->event_lock, flags);
2806 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2807 spin_unlock_irqrestore(&dev->event_lock, flags);
2808
2809 return pending;
2810}
2811
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002812static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2813{
Chris Wilson0f911282012-04-17 10:05:38 +01002814 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01002815 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002816
2817 if (crtc->fb == NULL)
2818 return;
2819
Daniel Vetter2c10d572012-12-20 21:24:07 +01002820 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
2821
Chris Wilson5bb61642012-09-27 21:25:58 +01002822 wait_event(dev_priv->pending_flip_queue,
2823 !intel_crtc_has_pending_flip(crtc));
2824
Chris Wilson0f911282012-04-17 10:05:38 +01002825 mutex_lock(&dev->struct_mutex);
2826 intel_finish_fb(crtc->fb);
2827 mutex_unlock(&dev->struct_mutex);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002828}
2829
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002830/* Program iCLKIP clock to the desired frequency */
2831static void lpt_program_iclkip(struct drm_crtc *crtc)
2832{
2833 struct drm_device *dev = crtc->dev;
2834 struct drm_i915_private *dev_priv = dev->dev_private;
2835 u32 divsel, phaseinc, auxdiv, phasedir = 0;
2836 u32 temp;
2837
Daniel Vetter09153002012-12-12 14:06:44 +01002838 mutex_lock(&dev_priv->dpio_lock);
2839
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002840 /* It is necessary to ungate the pixclk gate prior to programming
2841 * the divisors, and gate it back when it is done.
2842 */
2843 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
2844
2845 /* Disable SSCCTL */
2846 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002847 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
2848 SBI_SSCCTL_DISABLE,
2849 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002850
2851 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
2852 if (crtc->mode.clock == 20000) {
2853 auxdiv = 1;
2854 divsel = 0x41;
2855 phaseinc = 0x20;
2856 } else {
2857 /* The iCLK virtual clock root frequency is in MHz,
2858 * but the crtc->mode.clock in in KHz. To get the divisors,
2859 * it is necessary to divide one by another, so we
2860 * convert the virtual clock precision to KHz here for higher
2861 * precision.
2862 */
2863 u32 iclk_virtual_root_freq = 172800 * 1000;
2864 u32 iclk_pi_range = 64;
2865 u32 desired_divisor, msb_divisor_value, pi_value;
2866
2867 desired_divisor = (iclk_virtual_root_freq / crtc->mode.clock);
2868 msb_divisor_value = desired_divisor / iclk_pi_range;
2869 pi_value = desired_divisor % iclk_pi_range;
2870
2871 auxdiv = 0;
2872 divsel = msb_divisor_value - 2;
2873 phaseinc = pi_value;
2874 }
2875
2876 /* This should not happen with any sane values */
2877 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
2878 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
2879 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
2880 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
2881
2882 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
2883 crtc->mode.clock,
2884 auxdiv,
2885 divsel,
2886 phasedir,
2887 phaseinc);
2888
2889 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002890 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002891 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
2892 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
2893 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
2894 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
2895 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
2896 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002897 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002898
2899 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002900 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002901 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
2902 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002903 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002904
2905 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002906 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002907 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002908 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002909
2910 /* Wait for initialization time */
2911 udelay(24);
2912
2913 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01002914
2915 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002916}
2917
Daniel Vetter275f01b22013-05-03 11:49:47 +02002918static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
2919 enum pipe pch_transcoder)
2920{
2921 struct drm_device *dev = crtc->base.dev;
2922 struct drm_i915_private *dev_priv = dev->dev_private;
2923 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
2924
2925 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
2926 I915_READ(HTOTAL(cpu_transcoder)));
2927 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
2928 I915_READ(HBLANK(cpu_transcoder)));
2929 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
2930 I915_READ(HSYNC(cpu_transcoder)));
2931
2932 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
2933 I915_READ(VTOTAL(cpu_transcoder)));
2934 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
2935 I915_READ(VBLANK(cpu_transcoder)));
2936 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
2937 I915_READ(VSYNC(cpu_transcoder)));
2938 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
2939 I915_READ(VSYNCSHIFT(cpu_transcoder)));
2940}
2941
Jesse Barnesf67a5592011-01-05 10:31:48 -08002942/*
2943 * Enable PCH resources required for PCH ports:
2944 * - PCH PLLs
2945 * - FDI training & RX/TX
2946 * - update transcoder timings
2947 * - DP transcoding bits
2948 * - transcoder
2949 */
2950static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08002951{
2952 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002953 struct drm_i915_private *dev_priv = dev->dev_private;
2954 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2955 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01002956 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07002957
Daniel Vetterab9412b2013-05-03 11:49:46 +02002958 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01002959
Daniel Vettercd986ab2012-10-26 10:58:12 +02002960 /* Write the TU size bits before fdi link training, so that error
2961 * detection works. */
2962 I915_WRITE(FDI_RX_TUSIZE1(pipe),
2963 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
2964
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002965 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07002966 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002967
Daniel Vetter572deb32012-10-27 18:46:14 +02002968 /* XXX: pch pll's can be enabled any time before we enable the PCH
2969 * transcoder, and we actually should do this to not upset any PCH
2970 * transcoder that already use the clock when we share it.
2971 *
Daniel Vettere72f9fb2013-06-05 13:34:06 +02002972 * Note that enable_shared_dpll tries to do the right thing, but
2973 * get_shared_dpll unconditionally resets the pll - we need that to have
2974 * the right LVDS enable sequence. */
2975 ironlake_enable_shared_dpll(intel_crtc);
Chris Wilson6f13b7b2012-05-13 09:54:09 +01002976
Paulo Zanoni303b81e2012-10-31 18:12:23 -02002977 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01002978 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07002979
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002980 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02002981 temp |= TRANS_DPLL_ENABLE(pipe);
2982 sel = TRANS_DPLLB_SEL(pipe);
Daniel Vettera43f6e02013-06-07 23:10:32 +02002983 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01002984 temp |= sel;
2985 else
2986 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002987 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002988 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002989
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08002990 /* set transcoder timing, panel must allow it */
2991 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02002992 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002993
Paulo Zanoni303b81e2012-10-31 18:12:23 -02002994 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002995
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002996 /* For PCH DP, enable TRANS_DP_CTL */
2997 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07002998 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
2999 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003000 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003001 reg = TRANS_DP_CTL(pipe);
3002 temp = I915_READ(reg);
3003 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003004 TRANS_DP_SYNC_MASK |
3005 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003006 temp |= (TRANS_DP_OUTPUT_ENABLE |
3007 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003008 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003009
3010 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003011 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003012 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003013 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003014
3015 switch (intel_trans_dp_port_sel(crtc)) {
3016 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003017 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003018 break;
3019 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003020 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003021 break;
3022 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003023 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003024 break;
3025 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003026 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003027 }
3028
Chris Wilson5eddb702010-09-11 13:48:45 +01003029 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003030 }
3031
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003032 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003033}
3034
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003035static void lpt_pch_enable(struct drm_crtc *crtc)
3036{
3037 struct drm_device *dev = crtc->dev;
3038 struct drm_i915_private *dev_priv = dev->dev_private;
3039 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02003040 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003041
Daniel Vetterab9412b2013-05-03 11:49:46 +02003042 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003043
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003044 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003045
Paulo Zanoni0540e482012-10-31 18:12:40 -02003046 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02003047 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003048
Paulo Zanoni937bb612012-10-31 18:12:47 -02003049 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003050}
3051
Daniel Vettere2b78262013-06-07 23:10:03 +02003052static void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003053{
Daniel Vettere2b78262013-06-07 23:10:03 +02003054 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003055
3056 if (pll == NULL)
3057 return;
3058
3059 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003060 WARN(1, "bad %s refcount\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003061 return;
3062 }
3063
Daniel Vetterf4a091c2013-06-10 17:28:22 +02003064 if (--pll->refcount == 0) {
3065 WARN_ON(pll->on);
3066 WARN_ON(pll->active);
3067 }
3068
Daniel Vettera43f6e02013-06-07 23:10:32 +02003069 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003070}
3071
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003072static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003073{
Daniel Vettere2b78262013-06-07 23:10:03 +02003074 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3075 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3076 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003077
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003078 if (pll) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003079 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3080 crtc->base.base.id, pll->name);
Daniel Vettere2b78262013-06-07 23:10:03 +02003081 intel_put_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003082 }
3083
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003084 if (HAS_PCH_IBX(dev_priv->dev)) {
3085 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vettere2b78262013-06-07 23:10:03 +02003086 i = crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003087 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003088
Daniel Vetter46edb022013-06-05 13:34:12 +02003089 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3090 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003091
3092 goto found;
3093 }
3094
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003095 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3096 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003097
3098 /* Only want to check enabled timings first */
3099 if (pll->refcount == 0)
3100 continue;
3101
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003102 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3103 sizeof(pll->hw_state)) == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003104 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
Daniel Vettere2b78262013-06-07 23:10:03 +02003105 crtc->base.base.id,
Daniel Vetter46edb022013-06-05 13:34:12 +02003106 pll->name, pll->refcount, pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003107
3108 goto found;
3109 }
3110 }
3111
3112 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003113 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3114 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003115 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003116 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3117 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003118 goto found;
3119 }
3120 }
3121
3122 return NULL;
3123
3124found:
Daniel Vettera43f6e02013-06-07 23:10:32 +02003125 crtc->config.shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02003126 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3127 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02003128
Daniel Vettercdbd2312013-06-05 13:34:03 +02003129 if (pll->active == 0) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02003130 memcpy(&pll->hw_state, &crtc->config.dpll_hw_state,
3131 sizeof(pll->hw_state));
3132
Daniel Vetter46edb022013-06-05 13:34:12 +02003133 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
Daniel Vettercdbd2312013-06-05 13:34:03 +02003134 WARN_ON(pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02003135 assert_shared_dpll_disabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003136
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02003137 pll->mode_set(dev_priv, pll);
Daniel Vettercdbd2312013-06-05 13:34:03 +02003138 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003139 pll->refcount++;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003140
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003141 return pll;
3142}
3143
Daniel Vettera1520312013-05-03 11:49:50 +02003144static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07003145{
3146 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01003147 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003148 u32 temp;
3149
3150 temp = I915_READ(dslreg);
3151 udelay(500);
3152 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07003153 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03003154 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07003155 }
3156}
3157
Jesse Barnesb074cec2013-04-25 12:55:02 -07003158static void ironlake_pfit_enable(struct intel_crtc *crtc)
3159{
3160 struct drm_device *dev = crtc->base.dev;
3161 struct drm_i915_private *dev_priv = dev->dev_private;
3162 int pipe = crtc->pipe;
3163
Jesse Barnes0ef37f32013-05-03 13:26:37 -07003164 if (crtc->config.pch_pfit.size) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07003165 /* Force use of hard-coded filter coefficients
3166 * as some pre-programmed values are broken,
3167 * e.g. x201.
3168 */
3169 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3170 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3171 PF_PIPE_SEL_IVB(pipe));
3172 else
3173 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3174 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3175 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08003176 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003177}
3178
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003179static void intel_enable_planes(struct drm_crtc *crtc)
3180{
3181 struct drm_device *dev = crtc->dev;
3182 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3183 struct intel_plane *intel_plane;
3184
3185 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3186 if (intel_plane->pipe == pipe)
3187 intel_plane_restore(&intel_plane->base);
3188}
3189
3190static void intel_disable_planes(struct drm_crtc *crtc)
3191{
3192 struct drm_device *dev = crtc->dev;
3193 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3194 struct intel_plane *intel_plane;
3195
3196 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3197 if (intel_plane->pipe == pipe)
3198 intel_plane_disable(&intel_plane->base);
3199}
3200
Jesse Barnesf67a5592011-01-05 10:31:48 -08003201static void ironlake_crtc_enable(struct drm_crtc *crtc)
3202{
3203 struct drm_device *dev = crtc->dev;
3204 struct drm_i915_private *dev_priv = dev->dev_private;
3205 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003206 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003207 int pipe = intel_crtc->pipe;
3208 int plane = intel_crtc->plane;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003209
Daniel Vetter08a48462012-07-02 11:43:47 +02003210 WARN_ON(!crtc->enabled);
3211
Jesse Barnesf67a5592011-01-05 10:31:48 -08003212 if (intel_crtc->active)
3213 return;
3214
3215 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003216
3217 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3218 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
3219
Jesse Barnesf67a5592011-01-05 10:31:48 -08003220 intel_update_watermarks(dev);
3221
Daniel Vetterf6736a12013-06-05 13:34:30 +02003222 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02003223 if (encoder->pre_enable)
3224 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003225
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003226 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02003227 /* Note: FDI PLL enabling _must_ be done before we enable the
3228 * cpu pipes, hence this is separate from all the other fdi/pch
3229 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02003230 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02003231 } else {
3232 assert_fdi_tx_disabled(dev_priv, pipe);
3233 assert_fdi_rx_disabled(dev_priv, pipe);
3234 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003235
Jesse Barnesb074cec2013-04-25 12:55:02 -07003236 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003237
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02003238 /*
3239 * On ILK+ LUT must be loaded before the pipe is running but with
3240 * clocks enabled
3241 */
3242 intel_crtc_load_lut(crtc);
3243
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003244 intel_enable_pipe(dev_priv, pipe,
3245 intel_crtc->config.has_pch_encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003246 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003247 intel_enable_planes(crtc);
Ville Syrjälä5c38d482013-06-04 13:49:00 +03003248 intel_crtc_update_cursor(crtc, true);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003249
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003250 if (intel_crtc->config.has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08003251 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003252
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003253 mutex_lock(&dev->struct_mutex);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003254 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003255 mutex_unlock(&dev->struct_mutex);
3256
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003257 for_each_encoder_on_crtc(dev, crtc, encoder)
3258 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02003259
3260 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02003261 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02003262
3263 /*
3264 * There seems to be a race in PCH platform hw (at least on some
3265 * outputs) where an enabled pipe still completes any pageflip right
3266 * away (as if the pipe is off) instead of waiting for vblank. As soon
3267 * as the first vblank happend, everything works as expected. Hence just
3268 * wait for one vblank before returning to avoid strange things
3269 * happening.
3270 */
3271 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003272}
3273
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003274/* IPS only exists on ULT machines and is tied to pipe A. */
3275static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
3276{
Damien Lespiauf5adf942013-06-24 18:29:34 +01003277 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003278}
3279
3280static void hsw_enable_ips(struct intel_crtc *crtc)
3281{
3282 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3283
3284 if (!crtc->config.ips_enabled)
3285 return;
3286
3287 /* We can only enable IPS after we enable a plane and wait for a vblank.
3288 * We guarantee that the plane is enabled by calling intel_enable_ips
3289 * only after intel_enable_plane. And intel_enable_plane already waits
3290 * for a vblank, so all we need to do here is to enable the IPS bit. */
3291 assert_plane_enabled(dev_priv, crtc->plane);
3292 I915_WRITE(IPS_CTL, IPS_ENABLE);
3293}
3294
3295static void hsw_disable_ips(struct intel_crtc *crtc)
3296{
3297 struct drm_device *dev = crtc->base.dev;
3298 struct drm_i915_private *dev_priv = dev->dev_private;
3299
3300 if (!crtc->config.ips_enabled)
3301 return;
3302
3303 assert_plane_enabled(dev_priv, crtc->plane);
3304 I915_WRITE(IPS_CTL, 0);
3305
3306 /* We need to wait for a vblank before we can disable the plane. */
3307 intel_wait_for_vblank(dev, crtc->pipe);
3308}
3309
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003310static void haswell_crtc_enable(struct drm_crtc *crtc)
3311{
3312 struct drm_device *dev = crtc->dev;
3313 struct drm_i915_private *dev_priv = dev->dev_private;
3314 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3315 struct intel_encoder *encoder;
3316 int pipe = intel_crtc->pipe;
3317 int plane = intel_crtc->plane;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003318
3319 WARN_ON(!crtc->enabled);
3320
3321 if (intel_crtc->active)
3322 return;
3323
3324 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003325
3326 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3327 if (intel_crtc->config.has_pch_encoder)
3328 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
3329
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003330 intel_update_watermarks(dev);
3331
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003332 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni04945642012-11-01 21:00:59 -02003333 dev_priv->display.fdi_link_train(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003334
3335 for_each_encoder_on_crtc(dev, crtc, encoder)
3336 if (encoder->pre_enable)
3337 encoder->pre_enable(encoder);
3338
Paulo Zanoni1f544382012-10-24 11:32:00 -02003339 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003340
Jesse Barnesb074cec2013-04-25 12:55:02 -07003341 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003342
3343 /*
3344 * On ILK+ LUT must be loaded before the pipe is running but with
3345 * clocks enabled
3346 */
3347 intel_crtc_load_lut(crtc);
3348
Paulo Zanoni1f544382012-10-24 11:32:00 -02003349 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00003350 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003351
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003352 intel_enable_pipe(dev_priv, pipe,
3353 intel_crtc->config.has_pch_encoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003354 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003355 intel_enable_planes(crtc);
Ville Syrjälä5c38d482013-06-04 13:49:00 +03003356 intel_crtc_update_cursor(crtc, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003357
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003358 hsw_enable_ips(intel_crtc);
3359
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003360 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003361 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003362
3363 mutex_lock(&dev->struct_mutex);
3364 intel_update_fbc(dev);
3365 mutex_unlock(&dev->struct_mutex);
3366
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003367 for_each_encoder_on_crtc(dev, crtc, encoder)
3368 encoder->enable(encoder);
3369
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003370 /*
3371 * There seems to be a race in PCH platform hw (at least on some
3372 * outputs) where an enabled pipe still completes any pageflip right
3373 * away (as if the pipe is off) instead of waiting for vblank. As soon
3374 * as the first vblank happend, everything works as expected. Hence just
3375 * wait for one vblank before returning to avoid strange things
3376 * happening.
3377 */
3378 intel_wait_for_vblank(dev, intel_crtc->pipe);
3379}
3380
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003381static void ironlake_pfit_disable(struct intel_crtc *crtc)
3382{
3383 struct drm_device *dev = crtc->base.dev;
3384 struct drm_i915_private *dev_priv = dev->dev_private;
3385 int pipe = crtc->pipe;
3386
3387 /* To avoid upsetting the power well on haswell only disable the pfit if
3388 * it's in use. The hw state code will make sure we get this right. */
3389 if (crtc->config.pch_pfit.size) {
3390 I915_WRITE(PF_CTL(pipe), 0);
3391 I915_WRITE(PF_WIN_POS(pipe), 0);
3392 I915_WRITE(PF_WIN_SZ(pipe), 0);
3393 }
3394}
3395
Jesse Barnes6be4a602010-09-10 10:26:01 -07003396static void ironlake_crtc_disable(struct drm_crtc *crtc)
3397{
3398 struct drm_device *dev = crtc->dev;
3399 struct drm_i915_private *dev_priv = dev->dev_private;
3400 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003401 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003402 int pipe = intel_crtc->pipe;
3403 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01003404 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003405
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003406
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003407 if (!intel_crtc->active)
3408 return;
3409
Daniel Vetterea9d7582012-07-10 10:42:52 +02003410 for_each_encoder_on_crtc(dev, crtc, encoder)
3411 encoder->disable(encoder);
3412
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003413 intel_crtc_wait_for_pending_flips(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003414 drm_vblank_off(dev, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003415
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07003416 if (dev_priv->fbc.plane == plane)
Chris Wilson973d04f2011-07-08 12:22:37 +01003417 intel_disable_fbc(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003418
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003419 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003420 intel_disable_planes(crtc);
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003421 intel_disable_plane(dev_priv, plane, pipe);
3422
Daniel Vetterd925c592013-06-05 13:34:04 +02003423 if (intel_crtc->config.has_pch_encoder)
3424 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
3425
Jesse Barnesb24e7172011-01-04 15:09:30 -08003426 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003427
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003428 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003429
Daniel Vetterbf49ec82012-09-06 22:15:40 +02003430 for_each_encoder_on_crtc(dev, crtc, encoder)
3431 if (encoder->post_disable)
3432 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003433
Daniel Vetterd925c592013-06-05 13:34:04 +02003434 if (intel_crtc->config.has_pch_encoder) {
3435 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003436
Daniel Vetterd925c592013-06-05 13:34:04 +02003437 ironlake_disable_pch_transcoder(dev_priv, pipe);
3438 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003439
Daniel Vetterd925c592013-06-05 13:34:04 +02003440 if (HAS_PCH_CPT(dev)) {
3441 /* disable TRANS_DP_CTL */
3442 reg = TRANS_DP_CTL(pipe);
3443 temp = I915_READ(reg);
3444 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
3445 TRANS_DP_PORT_SEL_MASK);
3446 temp |= TRANS_DP_PORT_SEL_NONE;
3447 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003448
Daniel Vetterd925c592013-06-05 13:34:04 +02003449 /* disable DPLL_SEL */
3450 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003451 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02003452 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003453 }
Daniel Vetterd925c592013-06-05 13:34:04 +02003454
3455 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003456 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02003457
3458 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003459 }
3460
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003461 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003462 intel_update_watermarks(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003463
3464 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01003465 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003466 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003467}
3468
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003469static void haswell_crtc_disable(struct drm_crtc *crtc)
3470{
3471 struct drm_device *dev = crtc->dev;
3472 struct drm_i915_private *dev_priv = dev->dev_private;
3473 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3474 struct intel_encoder *encoder;
3475 int pipe = intel_crtc->pipe;
3476 int plane = intel_crtc->plane;
Daniel Vetter3b117c82013-04-17 20:15:07 +02003477 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003478
3479 if (!intel_crtc->active)
3480 return;
3481
3482 for_each_encoder_on_crtc(dev, crtc, encoder)
3483 encoder->disable(encoder);
3484
3485 intel_crtc_wait_for_pending_flips(crtc);
3486 drm_vblank_off(dev, pipe);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003487
Rodrigo Vivi891348b2013-05-06 19:37:36 -03003488 /* FBC must be disabled before disabling the plane on HSW. */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07003489 if (dev_priv->fbc.plane == plane)
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003490 intel_disable_fbc(dev);
3491
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003492 hsw_disable_ips(intel_crtc);
3493
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003494 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003495 intel_disable_planes(crtc);
Rodrigo Vivi891348b2013-05-06 19:37:36 -03003496 intel_disable_plane(dev_priv, plane, pipe);
3497
Paulo Zanoni86642812013-04-12 17:57:57 -03003498 if (intel_crtc->config.has_pch_encoder)
3499 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003500 intel_disable_pipe(dev_priv, pipe);
3501
Paulo Zanoniad80a812012-10-24 16:06:19 -02003502 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003503
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003504 ironlake_pfit_disable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003505
Paulo Zanoni1f544382012-10-24 11:32:00 -02003506 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003507
3508 for_each_encoder_on_crtc(dev, crtc, encoder)
3509 if (encoder->post_disable)
3510 encoder->post_disable(encoder);
3511
Daniel Vetter88adfff2013-03-28 10:42:01 +01003512 if (intel_crtc->config.has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02003513 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni86642812013-04-12 17:57:57 -03003514 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02003515 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02003516 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003517
3518 intel_crtc->active = false;
3519 intel_update_watermarks(dev);
3520
3521 mutex_lock(&dev->struct_mutex);
3522 intel_update_fbc(dev);
3523 mutex_unlock(&dev->struct_mutex);
3524}
3525
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003526static void ironlake_crtc_off(struct drm_crtc *crtc)
3527{
3528 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003529 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003530}
3531
Paulo Zanoni6441ab52012-10-05 12:05:58 -03003532static void haswell_crtc_off(struct drm_crtc *crtc)
3533{
3534 intel_ddi_put_crtc_pll(crtc);
3535}
3536
Daniel Vetter02e792f2009-09-15 22:57:34 +02003537static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3538{
Daniel Vetter02e792f2009-09-15 22:57:34 +02003539 if (!enable && intel_crtc->overlay) {
Chris Wilson23f09ce2010-08-12 13:53:37 +01003540 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00003541 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02003542
Chris Wilson23f09ce2010-08-12 13:53:37 +01003543 mutex_lock(&dev->struct_mutex);
Chris Wilsonce453d82011-02-21 14:43:56 +00003544 dev_priv->mm.interruptible = false;
3545 (void) intel_overlay_switch_off(intel_crtc->overlay);
3546 dev_priv->mm.interruptible = true;
Chris Wilson23f09ce2010-08-12 13:53:37 +01003547 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02003548 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02003549
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01003550 /* Let userspace switch the overlay on again. In most cases userspace
3551 * has to recompute where to put it anyway.
3552 */
Daniel Vetter02e792f2009-09-15 22:57:34 +02003553}
3554
Egbert Eich61bc95c2013-03-04 09:24:38 -05003555/**
3556 * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
3557 * cursor plane briefly if not already running after enabling the display
3558 * plane.
3559 * This workaround avoids occasional blank screens when self refresh is
3560 * enabled.
3561 */
3562static void
3563g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
3564{
3565 u32 cntl = I915_READ(CURCNTR(pipe));
3566
3567 if ((cntl & CURSOR_MODE) == 0) {
3568 u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
3569
3570 I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
3571 I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
3572 intel_wait_for_vblank(dev_priv->dev, pipe);
3573 I915_WRITE(CURCNTR(pipe), cntl);
3574 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3575 I915_WRITE(FW_BLC_SELF, fw_bcl_self);
3576 }
3577}
3578
Jesse Barnes2dd24552013-04-25 12:55:01 -07003579static void i9xx_pfit_enable(struct intel_crtc *crtc)
3580{
3581 struct drm_device *dev = crtc->base.dev;
3582 struct drm_i915_private *dev_priv = dev->dev_private;
3583 struct intel_crtc_config *pipe_config = &crtc->config;
3584
Daniel Vetter328d8e82013-05-08 10:36:31 +02003585 if (!crtc->config.gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07003586 return;
3587
Daniel Vetterc0b03412013-05-28 12:05:54 +02003588 /*
3589 * The panel fitter should only be adjusted whilst the pipe is disabled,
3590 * according to register description and PRM.
3591 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07003592 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
3593 assert_pipe_disabled(dev_priv, crtc->pipe);
3594
Jesse Barnesb074cec2013-04-25 12:55:02 -07003595 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
3596 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02003597
3598 /* Border color in case we don't scale up to the full screen. Black by
3599 * default, change to something else for debugging. */
3600 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07003601}
3602
Jesse Barnes89b667f2013-04-18 14:51:36 -07003603static void valleyview_crtc_enable(struct drm_crtc *crtc)
3604{
3605 struct drm_device *dev = crtc->dev;
3606 struct drm_i915_private *dev_priv = dev->dev_private;
3607 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3608 struct intel_encoder *encoder;
3609 int pipe = intel_crtc->pipe;
3610 int plane = intel_crtc->plane;
3611
3612 WARN_ON(!crtc->enabled);
3613
3614 if (intel_crtc->active)
3615 return;
3616
3617 intel_crtc->active = true;
3618 intel_update_watermarks(dev);
3619
3620 mutex_lock(&dev_priv->dpio_lock);
3621
3622 for_each_encoder_on_crtc(dev, crtc, encoder)
3623 if (encoder->pre_pll_enable)
3624 encoder->pre_pll_enable(encoder);
3625
Daniel Vetter87442f72013-06-06 00:52:17 +02003626 vlv_enable_pll(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07003627
3628 for_each_encoder_on_crtc(dev, crtc, encoder)
3629 if (encoder->pre_enable)
3630 encoder->pre_enable(encoder);
3631
3632 /* VLV wants encoder enabling _before_ the pipe is up. */
3633 for_each_encoder_on_crtc(dev, crtc, encoder)
3634 encoder->enable(encoder);
3635
Jesse Barnes2dd24552013-04-25 12:55:01 -07003636 i9xx_pfit_enable(intel_crtc);
3637
Ville Syrjälä63cbb072013-06-04 13:48:59 +03003638 intel_crtc_load_lut(crtc);
3639
Jesse Barnes89b667f2013-04-18 14:51:36 -07003640 intel_enable_pipe(dev_priv, pipe, false);
3641 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003642 intel_enable_planes(crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07003643 intel_crtc_update_cursor(crtc, true);
3644
Ville Syrjäläf440eb12013-06-04 13:49:01 +03003645 intel_update_fbc(dev);
3646
Jesse Barnes89b667f2013-04-18 14:51:36 -07003647 mutex_unlock(&dev_priv->dpio_lock);
3648}
3649
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003650static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003651{
3652 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08003653 struct drm_i915_private *dev_priv = dev->dev_private;
3654 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003655 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08003656 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07003657 int plane = intel_crtc->plane;
Jesse Barnes79e53942008-11-07 14:24:08 -08003658
Daniel Vetter08a48462012-07-02 11:43:47 +02003659 WARN_ON(!crtc->enabled);
3660
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003661 if (intel_crtc->active)
3662 return;
3663
3664 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01003665 intel_update_watermarks(dev);
3666
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02003667 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02003668 if (encoder->pre_enable)
3669 encoder->pre_enable(encoder);
3670
Daniel Vetterf6736a12013-06-05 13:34:30 +02003671 i9xx_enable_pll(intel_crtc);
3672
Jesse Barnes2dd24552013-04-25 12:55:01 -07003673 i9xx_pfit_enable(intel_crtc);
3674
Ville Syrjälä63cbb072013-06-04 13:48:59 +03003675 intel_crtc_load_lut(crtc);
3676
Jesse Barnes040484a2011-01-03 12:14:26 -08003677 intel_enable_pipe(dev_priv, pipe, false);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003678 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003679 intel_enable_planes(crtc);
Ville Syrjälä22e407d2013-06-07 18:52:24 +03003680 /* The fixup needs to happen before cursor is enabled */
Egbert Eich61bc95c2013-03-04 09:24:38 -05003681 if (IS_G4X(dev))
3682 g4x_fixup_plane(dev_priv, pipe);
Ville Syrjälä22e407d2013-06-07 18:52:24 +03003683 intel_crtc_update_cursor(crtc, true);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003684
3685 /* Give the overlay scaler a chance to enable if it's on this pipe */
3686 intel_crtc_dpms_overlay(intel_crtc, true);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003687
Ville Syrjäläf440eb12013-06-04 13:49:01 +03003688 intel_update_fbc(dev);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003689
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003690 for_each_encoder_on_crtc(dev, crtc, encoder)
3691 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003692}
3693
Daniel Vetter87476d62013-04-11 16:29:06 +02003694static void i9xx_pfit_disable(struct intel_crtc *crtc)
3695{
3696 struct drm_device *dev = crtc->base.dev;
3697 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02003698
3699 if (!crtc->config.gmch_pfit.control)
3700 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02003701
3702 assert_pipe_disabled(dev_priv, crtc->pipe);
3703
Daniel Vetter328d8e82013-05-08 10:36:31 +02003704 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
3705 I915_READ(PFIT_CONTROL));
3706 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02003707}
3708
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003709static void i9xx_crtc_disable(struct drm_crtc *crtc)
3710{
3711 struct drm_device *dev = crtc->dev;
3712 struct drm_i915_private *dev_priv = dev->dev_private;
3713 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003714 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003715 int pipe = intel_crtc->pipe;
3716 int plane = intel_crtc->plane;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003717
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003718 if (!intel_crtc->active)
3719 return;
3720
Daniel Vetterea9d7582012-07-10 10:42:52 +02003721 for_each_encoder_on_crtc(dev, crtc, encoder)
3722 encoder->disable(encoder);
3723
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003724 /* Give the overlay scaler a chance to disable if it's on this pipe */
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003725 intel_crtc_wait_for_pending_flips(crtc);
3726 drm_vblank_off(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003727
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -07003728 if (dev_priv->fbc.plane == plane)
Chris Wilson973d04f2011-07-08 12:22:37 +01003729 intel_disable_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003730
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003731 intel_crtc_dpms_overlay(intel_crtc, false);
3732 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003733 intel_disable_planes(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003734 intel_disable_plane(dev_priv, plane, pipe);
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003735
Jesse Barnesb24e7172011-01-04 15:09:30 -08003736 intel_disable_pipe(dev_priv, pipe);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02003737
Daniel Vetter87476d62013-04-11 16:29:06 +02003738 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02003739
Jesse Barnes89b667f2013-04-18 14:51:36 -07003740 for_each_encoder_on_crtc(dev, crtc, encoder)
3741 if (encoder->post_disable)
3742 encoder->post_disable(encoder);
3743
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08003744 intel_disable_pll(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003745
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003746 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003747 intel_update_fbc(dev);
3748 intel_update_watermarks(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003749}
3750
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003751static void i9xx_crtc_off(struct drm_crtc *crtc)
3752{
3753}
3754
Daniel Vetter976f8a22012-07-08 22:34:21 +02003755static void intel_crtc_update_sarea(struct drm_crtc *crtc,
3756 bool enabled)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003757{
3758 struct drm_device *dev = crtc->dev;
3759 struct drm_i915_master_private *master_priv;
3760 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3761 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08003762
3763 if (!dev->primary->master)
3764 return;
3765
3766 master_priv = dev->primary->master->driver_priv;
3767 if (!master_priv->sarea_priv)
3768 return;
3769
Jesse Barnes79e53942008-11-07 14:24:08 -08003770 switch (pipe) {
3771 case 0:
3772 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
3773 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
3774 break;
3775 case 1:
3776 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
3777 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
3778 break;
3779 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003780 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08003781 break;
3782 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003783}
3784
Daniel Vetter976f8a22012-07-08 22:34:21 +02003785/**
3786 * Sets the power management mode of the pipe and plane.
3787 */
3788void intel_crtc_update_dpms(struct drm_crtc *crtc)
Chris Wilsoncdd59982010-09-08 16:30:16 +01003789{
Chris Wilsoncdd59982010-09-08 16:30:16 +01003790 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003791 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter976f8a22012-07-08 22:34:21 +02003792 struct intel_encoder *intel_encoder;
3793 bool enable = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01003794
Daniel Vetter976f8a22012-07-08 22:34:21 +02003795 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
3796 enable |= intel_encoder->connectors_active;
3797
3798 if (enable)
3799 dev_priv->display.crtc_enable(crtc);
3800 else
3801 dev_priv->display.crtc_disable(crtc);
3802
3803 intel_crtc_update_sarea(crtc, enable);
3804}
3805
Daniel Vetter976f8a22012-07-08 22:34:21 +02003806static void intel_crtc_disable(struct drm_crtc *crtc)
3807{
3808 struct drm_device *dev = crtc->dev;
3809 struct drm_connector *connector;
3810 struct drm_i915_private *dev_priv = dev->dev_private;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08003811 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter976f8a22012-07-08 22:34:21 +02003812
3813 /* crtc should still be enabled when we disable it. */
3814 WARN_ON(!crtc->enabled);
3815
3816 dev_priv->display.crtc_disable(crtc);
Paulo Zanonic77bf562013-05-03 12:15:40 -03003817 intel_crtc->eld_vld = false;
Daniel Vetter976f8a22012-07-08 22:34:21 +02003818 intel_crtc_update_sarea(crtc, false);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003819 dev_priv->display.off(crtc);
3820
Chris Wilson931872f2012-01-16 23:01:13 +00003821 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
3822 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003823
3824 if (crtc->fb) {
3825 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01003826 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003827 mutex_unlock(&dev->struct_mutex);
Daniel Vetter976f8a22012-07-08 22:34:21 +02003828 crtc->fb = NULL;
3829 }
3830
3831 /* Update computed state. */
3832 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3833 if (!connector->encoder || !connector->encoder->crtc)
3834 continue;
3835
3836 if (connector->encoder->crtc != crtc)
3837 continue;
3838
3839 connector->dpms = DRM_MODE_DPMS_OFF;
3840 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01003841 }
3842}
3843
Daniel Vettera261b242012-07-26 19:21:47 +02003844void intel_modeset_disable(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08003845{
Daniel Vettera261b242012-07-26 19:21:47 +02003846 struct drm_crtc *crtc;
3847
3848 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3849 if (crtc->enabled)
3850 intel_crtc_disable(crtc);
3851 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003852}
3853
Chris Wilsonea5b2132010-08-04 13:50:23 +01003854void intel_encoder_destroy(struct drm_encoder *encoder)
3855{
Chris Wilson4ef69c72010-09-09 15:14:28 +01003856 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01003857
Chris Wilsonea5b2132010-08-04 13:50:23 +01003858 drm_encoder_cleanup(encoder);
3859 kfree(intel_encoder);
3860}
3861
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003862/* Simple dpms helper for encodres with just one connector, no cloning and only
3863 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
3864 * state of the entire output pipe. */
3865void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
3866{
3867 if (mode == DRM_MODE_DPMS_ON) {
3868 encoder->connectors_active = true;
3869
Daniel Vetterb2cabb02012-07-01 22:42:24 +02003870 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003871 } else {
3872 encoder->connectors_active = false;
3873
Daniel Vetterb2cabb02012-07-01 22:42:24 +02003874 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003875 }
3876}
3877
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003878/* Cross check the actual hw state with our own modeset state tracking (and it's
3879 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02003880static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003881{
3882 if (connector->get_hw_state(connector)) {
3883 struct intel_encoder *encoder = connector->encoder;
3884 struct drm_crtc *crtc;
3885 bool encoder_enabled;
3886 enum pipe pipe;
3887
3888 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
3889 connector->base.base.id,
3890 drm_get_connector_name(&connector->base));
3891
3892 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
3893 "wrong connector dpms state\n");
3894 WARN(connector->base.encoder != &encoder->base,
3895 "active connector not linked to encoder\n");
3896 WARN(!encoder->connectors_active,
3897 "encoder->connectors_active not set\n");
3898
3899 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
3900 WARN(!encoder_enabled, "encoder not enabled\n");
3901 if (WARN_ON(!encoder->base.crtc))
3902 return;
3903
3904 crtc = encoder->base.crtc;
3905
3906 WARN(!crtc->enabled, "crtc not enabled\n");
3907 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
3908 WARN(pipe != to_intel_crtc(crtc)->pipe,
3909 "encoder active on the wrong pipe\n");
3910 }
3911}
3912
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003913/* Even simpler default implementation, if there's really no special case to
3914 * consider. */
3915void intel_connector_dpms(struct drm_connector *connector, int mode)
3916{
3917 struct intel_encoder *encoder = intel_attached_encoder(connector);
3918
3919 /* All the simple cases only support two dpms states. */
3920 if (mode != DRM_MODE_DPMS_ON)
3921 mode = DRM_MODE_DPMS_OFF;
3922
3923 if (mode == connector->dpms)
3924 return;
3925
3926 connector->dpms = mode;
3927
3928 /* Only need to change hw state when actually enabled */
3929 if (encoder->base.crtc)
3930 intel_encoder_dpms(encoder, mode);
3931 else
Daniel Vetter8af6cf82012-07-10 09:50:11 +02003932 WARN_ON(encoder->connectors_active != false);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003933
Daniel Vetterb9805142012-08-31 17:37:33 +02003934 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003935}
3936
Daniel Vetterf0947c32012-07-02 13:10:34 +02003937/* Simple connector->get_hw_state implementation for encoders that support only
3938 * one connector and no cloning and hence the encoder state determines the state
3939 * of the connector. */
3940bool intel_connector_get_hw_state(struct intel_connector *connector)
3941{
Daniel Vetter24929352012-07-02 20:28:59 +02003942 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02003943 struct intel_encoder *encoder = connector->encoder;
3944
3945 return encoder->get_hw_state(encoder, &pipe);
3946}
3947
Daniel Vetter1857e1d2013-04-29 19:34:16 +02003948static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
3949 struct intel_crtc_config *pipe_config)
3950{
3951 struct drm_i915_private *dev_priv = dev->dev_private;
3952 struct intel_crtc *pipe_B_crtc =
3953 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
3954
3955 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
3956 pipe_name(pipe), pipe_config->fdi_lanes);
3957 if (pipe_config->fdi_lanes > 4) {
3958 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
3959 pipe_name(pipe), pipe_config->fdi_lanes);
3960 return false;
3961 }
3962
3963 if (IS_HASWELL(dev)) {
3964 if (pipe_config->fdi_lanes > 2) {
3965 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
3966 pipe_config->fdi_lanes);
3967 return false;
3968 } else {
3969 return true;
3970 }
3971 }
3972
3973 if (INTEL_INFO(dev)->num_pipes == 2)
3974 return true;
3975
3976 /* Ivybridge 3 pipe is really complicated */
3977 switch (pipe) {
3978 case PIPE_A:
3979 return true;
3980 case PIPE_B:
3981 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
3982 pipe_config->fdi_lanes > 2) {
3983 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
3984 pipe_name(pipe), pipe_config->fdi_lanes);
3985 return false;
3986 }
3987 return true;
3988 case PIPE_C:
Daniel Vetter1e833f42013-02-19 22:31:57 +01003989 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
Daniel Vetter1857e1d2013-04-29 19:34:16 +02003990 pipe_B_crtc->config.fdi_lanes <= 2) {
3991 if (pipe_config->fdi_lanes > 2) {
3992 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
3993 pipe_name(pipe), pipe_config->fdi_lanes);
3994 return false;
3995 }
3996 } else {
3997 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
3998 return false;
3999 }
4000 return true;
4001 default:
4002 BUG();
4003 }
4004}
4005
Daniel Vettere29c22c2013-02-21 00:00:16 +01004006#define RETRY 1
4007static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
4008 struct intel_crtc_config *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02004009{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004010 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004011 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Daniel Vetterff9a6752013-06-01 17:16:21 +02004012 int lane, link_bw, fdi_dotclock;
Daniel Vettere29c22c2013-02-21 00:00:16 +01004013 bool setup_ok, needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004014
Daniel Vettere29c22c2013-02-21 00:00:16 +01004015retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02004016 /* FDI is a binary signal running at ~2.7GHz, encoding
4017 * each output octet as 10 bits. The actual frequency
4018 * is stored as a divider into a 100MHz clock, and the
4019 * mode pixel clock is stored in units of 1KHz.
4020 * Hence the bw of each lane in terms of the mode signal
4021 * is:
4022 */
4023 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
4024
Daniel Vetterff9a6752013-06-01 17:16:21 +02004025 fdi_dotclock = adjusted_mode->clock;
Daniel Vetteref1b4602013-06-01 17:17:04 +02004026 fdi_dotclock /= pipe_config->pixel_multiplier;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004027
Daniel Vetter2bd89a02013-06-01 17:16:19 +02004028 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02004029 pipe_config->pipe_bpp);
4030
4031 pipe_config->fdi_lanes = lane;
4032
Daniel Vetter2bd89a02013-06-01 17:16:19 +02004033 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02004034 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004035
Daniel Vettere29c22c2013-02-21 00:00:16 +01004036 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
4037 intel_crtc->pipe, pipe_config);
4038 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
4039 pipe_config->pipe_bpp -= 2*3;
4040 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
4041 pipe_config->pipe_bpp);
4042 needs_recompute = true;
4043 pipe_config->bw_constrained = true;
4044
4045 goto retry;
4046 }
4047
4048 if (needs_recompute)
4049 return RETRY;
4050
4051 return setup_ok ? 0 : -EINVAL;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004052}
4053
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004054static void hsw_compute_ips_config(struct intel_crtc *crtc,
4055 struct intel_crtc_config *pipe_config)
4056{
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03004057 pipe_config->ips_enabled = i915_enable_ips &&
4058 hsw_crtc_supports_ips(crtc) &&
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004059 pipe_config->pipe_bpp == 24;
4060}
4061
Daniel Vettera43f6e02013-06-07 23:10:32 +02004062static int intel_crtc_compute_config(struct intel_crtc *crtc,
Daniel Vettere29c22c2013-02-21 00:00:16 +01004063 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08004064{
Daniel Vettera43f6e02013-06-07 23:10:32 +02004065 struct drm_device *dev = crtc->base.dev;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01004066 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01004067
Eric Anholtbad720f2009-10-22 16:11:14 -07004068 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08004069 /* FDI link clock is fixed at 2.7G */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01004070 if (pipe_config->requested_mode.clock * 3
4071 > IRONLAKE_FDI_FREQ * 4)
Daniel Vettere29c22c2013-02-21 00:00:16 +01004072 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004073 }
Chris Wilson89749352010-09-12 18:25:19 +01004074
Daniel Vetterf9bef082012-04-15 19:53:19 +02004075 /* All interlaced capable intel hw wants timings in frames. Note though
4076 * that intel_lvds_mode_fixup does some funny tricks with the crtc
4077 * timings, so we need to be careful not to clobber these.*/
Daniel Vetter7ae89232013-03-27 00:44:52 +01004078 if (!pipe_config->timings_set)
Daniel Vetterf9bef082012-04-15 19:53:19 +02004079 drm_mode_set_crtcinfo(adjusted_mode, 0);
Chris Wilson89749352010-09-12 18:25:19 +01004080
Damien Lespiau8693a822013-05-03 18:48:11 +01004081 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
4082 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03004083 */
4084 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
4085 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01004086 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03004087
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004088 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004089 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004090 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004091 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
4092 * for lvds. */
4093 pipe_config->pipe_bpp = 8*3;
4094 }
4095
Damien Lespiauf5adf942013-06-24 18:29:34 +01004096 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02004097 hsw_compute_ips_config(crtc, pipe_config);
4098
4099 /* XXX: PCH clock sharing is done in ->mode_set, so make sure the old
4100 * clock survives for now. */
4101 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
4102 pipe_config->shared_dpll = crtc->config.shared_dpll;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004103
Daniel Vetter877d48d2013-04-19 11:24:43 +02004104 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02004105 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02004106
Daniel Vettere29c22c2013-02-21 00:00:16 +01004107 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08004108}
4109
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07004110static int valleyview_get_display_clock_speed(struct drm_device *dev)
4111{
4112 return 400000; /* FIXME */
4113}
4114
Jesse Barnese70236a2009-09-21 10:42:27 -07004115static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08004116{
Jesse Barnese70236a2009-09-21 10:42:27 -07004117 return 400000;
4118}
Jesse Barnes79e53942008-11-07 14:24:08 -08004119
Jesse Barnese70236a2009-09-21 10:42:27 -07004120static int i915_get_display_clock_speed(struct drm_device *dev)
4121{
4122 return 333000;
4123}
Jesse Barnes79e53942008-11-07 14:24:08 -08004124
Jesse Barnese70236a2009-09-21 10:42:27 -07004125static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
4126{
4127 return 200000;
4128}
Jesse Barnes79e53942008-11-07 14:24:08 -08004129
Jesse Barnese70236a2009-09-21 10:42:27 -07004130static int i915gm_get_display_clock_speed(struct drm_device *dev)
4131{
4132 u16 gcfgc = 0;
4133
4134 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4135
4136 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08004137 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07004138 else {
4139 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4140 case GC_DISPLAY_CLOCK_333_MHZ:
4141 return 333000;
4142 default:
4143 case GC_DISPLAY_CLOCK_190_200_MHZ:
4144 return 190000;
4145 }
4146 }
4147}
Jesse Barnes79e53942008-11-07 14:24:08 -08004148
Jesse Barnese70236a2009-09-21 10:42:27 -07004149static int i865_get_display_clock_speed(struct drm_device *dev)
4150{
4151 return 266000;
4152}
4153
4154static int i855_get_display_clock_speed(struct drm_device *dev)
4155{
4156 u16 hpllcc = 0;
4157 /* Assume that the hardware is in the high speed state. This
4158 * should be the default.
4159 */
4160 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
4161 case GC_CLOCK_133_200:
4162 case GC_CLOCK_100_200:
4163 return 200000;
4164 case GC_CLOCK_166_250:
4165 return 250000;
4166 case GC_CLOCK_100_133:
4167 return 133000;
4168 }
4169
4170 /* Shouldn't happen */
4171 return 0;
4172}
4173
4174static int i830_get_display_clock_speed(struct drm_device *dev)
4175{
4176 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08004177}
4178
Zhenyu Wang2c072452009-06-05 15:38:42 +08004179static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004180intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004181{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004182 while (*num > DATA_LINK_M_N_MASK ||
4183 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08004184 *num >>= 1;
4185 *den >>= 1;
4186 }
4187}
4188
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004189static void compute_m_n(unsigned int m, unsigned int n,
4190 uint32_t *ret_m, uint32_t *ret_n)
4191{
4192 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
4193 *ret_m = div_u64((uint64_t) m * *ret_n, n);
4194 intel_reduce_m_n_ratio(ret_m, ret_n);
4195}
4196
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004197void
4198intel_link_compute_m_n(int bits_per_pixel, int nlanes,
4199 int pixel_clock, int link_clock,
4200 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004201{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004202 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004203
4204 compute_m_n(bits_per_pixel * pixel_clock,
4205 link_clock * nlanes * 8,
4206 &m_n->gmch_m, &m_n->gmch_n);
4207
4208 compute_m_n(pixel_clock, link_clock,
4209 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004210}
4211
Chris Wilsona7615032011-01-12 17:04:08 +00004212static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
4213{
Keith Packard72bbe582011-09-26 16:09:45 -07004214 if (i915_panel_use_ssc >= 0)
4215 return i915_panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004216 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07004217 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00004218}
4219
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004220static int vlv_get_refclk(struct drm_crtc *crtc)
4221{
4222 struct drm_device *dev = crtc->dev;
4223 struct drm_i915_private *dev_priv = dev->dev_private;
4224 int refclk = 27000; /* for DP & HDMI */
4225
4226 return 100000; /* only one validated so far */
4227
4228 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
4229 refclk = 96000;
4230 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
4231 if (intel_panel_use_ssc(dev_priv))
4232 refclk = 100000;
4233 else
4234 refclk = 96000;
4235 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
4236 refclk = 100000;
4237 }
4238
4239 return refclk;
4240}
4241
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004242static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
4243{
4244 struct drm_device *dev = crtc->dev;
4245 struct drm_i915_private *dev_priv = dev->dev_private;
4246 int refclk;
4247
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004248 if (IS_VALLEYVIEW(dev)) {
4249 refclk = vlv_get_refclk(crtc);
4250 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004251 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004252 refclk = dev_priv->vbt.lvds_ssc_freq * 1000;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004253 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
4254 refclk / 1000);
4255 } else if (!IS_GEN2(dev)) {
4256 refclk = 96000;
4257 } else {
4258 refclk = 48000;
4259 }
4260
4261 return refclk;
4262}
4263
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004264static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004265{
Daniel Vetter7df00d72013-05-21 21:54:55 +02004266 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004267}
Daniel Vetterf47709a2013-03-28 10:42:02 +01004268
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004269static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
4270{
4271 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004272}
4273
Daniel Vetterf47709a2013-03-28 10:42:02 +01004274static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Jesse Barnesa7516a02011-12-15 12:30:37 -08004275 intel_clock_t *reduced_clock)
4276{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004277 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004278 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004279 int pipe = crtc->pipe;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004280 u32 fp, fp2 = 0;
4281
4282 if (IS_PINEVIEW(dev)) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004283 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004284 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004285 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004286 } else {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004287 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004288 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004289 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004290 }
4291
4292 I915_WRITE(FP0(pipe), fp);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004293 crtc->config.dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004294
Daniel Vetterf47709a2013-03-28 10:42:02 +01004295 crtc->lowfreq_avail = false;
4296 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Jesse Barnesa7516a02011-12-15 12:30:37 -08004297 reduced_clock && i915_powersave) {
4298 I915_WRITE(FP1(pipe), fp2);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004299 crtc->config.dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004300 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004301 } else {
4302 I915_WRITE(FP1(pipe), fp);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004303 crtc->config.dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004304 }
4305}
4306
Jesse Barnes89b667f2013-04-18 14:51:36 -07004307static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv)
4308{
4309 u32 reg_val;
4310
4311 /*
4312 * PLLB opamp always calibrates to max value of 0x3f, force enable it
4313 * and set it to a reasonable value instead.
4314 */
Jani Nikulaae992582013-05-22 15:36:19 +03004315 reg_val = vlv_dpio_read(dev_priv, DPIO_IREF(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004316 reg_val &= 0xffffff00;
4317 reg_val |= 0x00000030;
Jani Nikulaae992582013-05-22 15:36:19 +03004318 vlv_dpio_write(dev_priv, DPIO_IREF(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004319
Jani Nikulaae992582013-05-22 15:36:19 +03004320 reg_val = vlv_dpio_read(dev_priv, DPIO_CALIBRATION);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004321 reg_val &= 0x8cffffff;
4322 reg_val = 0x8c000000;
Jani Nikulaae992582013-05-22 15:36:19 +03004323 vlv_dpio_write(dev_priv, DPIO_CALIBRATION, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004324
Jani Nikulaae992582013-05-22 15:36:19 +03004325 reg_val = vlv_dpio_read(dev_priv, DPIO_IREF(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004326 reg_val &= 0xffffff00;
Jani Nikulaae992582013-05-22 15:36:19 +03004327 vlv_dpio_write(dev_priv, DPIO_IREF(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004328
Jani Nikulaae992582013-05-22 15:36:19 +03004329 reg_val = vlv_dpio_read(dev_priv, DPIO_CALIBRATION);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004330 reg_val &= 0x00ffffff;
4331 reg_val |= 0xb0000000;
Jani Nikulaae992582013-05-22 15:36:19 +03004332 vlv_dpio_write(dev_priv, DPIO_CALIBRATION, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004333}
4334
Daniel Vetterb5518422013-05-03 11:49:48 +02004335static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
4336 struct intel_link_m_n *m_n)
4337{
4338 struct drm_device *dev = crtc->base.dev;
4339 struct drm_i915_private *dev_priv = dev->dev_private;
4340 int pipe = crtc->pipe;
4341
Daniel Vettere3b95f12013-05-03 11:49:49 +02004342 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4343 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
4344 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
4345 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02004346}
4347
4348static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
4349 struct intel_link_m_n *m_n)
4350{
4351 struct drm_device *dev = crtc->base.dev;
4352 struct drm_i915_private *dev_priv = dev->dev_private;
4353 int pipe = crtc->pipe;
4354 enum transcoder transcoder = crtc->config.cpu_transcoder;
4355
4356 if (INTEL_INFO(dev)->gen >= 5) {
4357 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
4358 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
4359 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
4360 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
4361 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02004362 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4363 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
4364 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
4365 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02004366 }
4367}
4368
Daniel Vetter03afc4a2013-04-02 23:42:31 +02004369static void intel_dp_set_m_n(struct intel_crtc *crtc)
4370{
4371 if (crtc->config.has_pch_encoder)
4372 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4373 else
4374 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4375}
4376
Daniel Vetterf47709a2013-03-28 10:42:02 +01004377static void vlv_update_pll(struct intel_crtc *crtc)
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004378{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004379 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004380 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004381 struct intel_encoder *encoder;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004382 int pipe = crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004383 u32 dpll, mdiv;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004384 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004385 bool is_hdmi;
Daniel Vetter198a037f2013-04-19 11:14:37 +02004386 u32 coreclk, reg_val, dpll_md;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004387
Daniel Vetter09153002012-12-12 14:06:44 +01004388 mutex_lock(&dev_priv->dpio_lock);
4389
Jesse Barnes89b667f2013-04-18 14:51:36 -07004390 is_hdmi = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004391
Daniel Vetterf47709a2013-03-28 10:42:02 +01004392 bestn = crtc->config.dpll.n;
4393 bestm1 = crtc->config.dpll.m1;
4394 bestm2 = crtc->config.dpll.m2;
4395 bestp1 = crtc->config.dpll.p1;
4396 bestp2 = crtc->config.dpll.p2;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004397
Jesse Barnes89b667f2013-04-18 14:51:36 -07004398 /* See eDP HDMI DPIO driver vbios notes doc */
4399
4400 /* PLL B needs special handling */
4401 if (pipe)
4402 vlv_pllb_recal_opamp(dev_priv);
4403
4404 /* Set up Tx target for periodic Rcomp update */
Jani Nikulaae992582013-05-22 15:36:19 +03004405 vlv_dpio_write(dev_priv, DPIO_IREF_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004406
4407 /* Disable target IRef on PLL */
Jani Nikulaae992582013-05-22 15:36:19 +03004408 reg_val = vlv_dpio_read(dev_priv, DPIO_IREF_CTL(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004409 reg_val &= 0x00ffffff;
Jani Nikulaae992582013-05-22 15:36:19 +03004410 vlv_dpio_write(dev_priv, DPIO_IREF_CTL(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004411
4412 /* Disable fast lock */
Jani Nikulaae992582013-05-22 15:36:19 +03004413 vlv_dpio_write(dev_priv, DPIO_FASTCLK_DISABLE, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004414
4415 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004416 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
4417 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
4418 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004419 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07004420
4421 /*
4422 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
4423 * but we don't support that).
4424 * Note: don't use the DAC post divider as it seems unstable.
4425 */
4426 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Jani Nikulaae992582013-05-22 15:36:19 +03004427 vlv_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004428
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004429 mdiv |= DPIO_ENABLE_CALIBRATION;
Jani Nikulaae992582013-05-22 15:36:19 +03004430 vlv_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004431
Jesse Barnes89b667f2013-04-18 14:51:36 -07004432 /* Set HBR and RBR LPF coefficients */
Daniel Vetterff9a6752013-06-01 17:16:21 +02004433 if (crtc->config.port_clock == 162000 ||
Ville Syrjälä99750bd2013-06-14 14:02:52 +03004434 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
Jesse Barnes89b667f2013-04-18 14:51:36 -07004435 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
Ville Syrjälä4abb2c32013-06-14 14:02:53 +03004436 vlv_dpio_write(dev_priv, DPIO_LPF_COEFF(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004437 0x005f0021);
4438 else
Ville Syrjälä4abb2c32013-06-14 14:02:53 +03004439 vlv_dpio_write(dev_priv, DPIO_LPF_COEFF(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004440 0x00d0000f);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004441
Jesse Barnes89b667f2013-04-18 14:51:36 -07004442 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
4443 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
4444 /* Use SSC source */
4445 if (!pipe)
Jani Nikulaae992582013-05-22 15:36:19 +03004446 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004447 0x0df40000);
4448 else
Jani Nikulaae992582013-05-22 15:36:19 +03004449 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004450 0x0df70000);
4451 } else { /* HDMI or VGA */
4452 /* Use bend source */
4453 if (!pipe)
Jani Nikulaae992582013-05-22 15:36:19 +03004454 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004455 0x0df70000);
4456 else
Jani Nikulaae992582013-05-22 15:36:19 +03004457 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004458 0x0df40000);
4459 }
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004460
Jani Nikulaae992582013-05-22 15:36:19 +03004461 coreclk = vlv_dpio_read(dev_priv, DPIO_CORE_CLK(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004462 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
4463 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
4464 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
4465 coreclk |= 0x01000000;
Jani Nikulaae992582013-05-22 15:36:19 +03004466 vlv_dpio_write(dev_priv, DPIO_CORE_CLK(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004467
Jani Nikulaae992582013-05-22 15:36:19 +03004468 vlv_dpio_write(dev_priv, DPIO_PLL_CML(pipe), 0x87871000);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004469
4470 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
4471 if (encoder->pre_pll_enable)
4472 encoder->pre_pll_enable(encoder);
4473
4474 /* Enable DPIO clock input */
4475 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
4476 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
4477 if (pipe)
4478 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004479
4480 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004481 crtc->config.dpll_hw_state.dpll = dpll;
4482
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004483 I915_WRITE(DPLL(pipe), dpll);
4484 POSTING_READ(DPLL(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004485 udelay(150);
4486
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004487 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
4488 DRM_ERROR("DPLL %d failed to lock\n", pipe);
4489
Daniel Vetteref1b4602013-06-01 17:17:04 +02004490 dpll_md = (crtc->config.pixel_multiplier - 1)
4491 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004492 crtc->config.dpll_hw_state.dpll_md = dpll_md;
4493
Daniel Vetter198a037f2013-04-19 11:14:37 +02004494 I915_WRITE(DPLL_MD(pipe), dpll_md);
4495 POSTING_READ(DPLL_MD(pipe));
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004496
Daniel Vetterf47709a2013-03-28 10:42:02 +01004497 if (crtc->config.has_dp_encoder)
4498 intel_dp_set_m_n(crtc);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304499
Daniel Vetter09153002012-12-12 14:06:44 +01004500 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004501}
4502
Daniel Vetterf47709a2013-03-28 10:42:02 +01004503static void i9xx_update_pll(struct intel_crtc *crtc,
4504 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004505 int num_connectors)
4506{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004507 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004508 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004509 u32 dpll;
4510 bool is_sdvo;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004511 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004512
Daniel Vetterf47709a2013-03-28 10:42:02 +01004513 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304514
Daniel Vetterf47709a2013-03-28 10:42:02 +01004515 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
4516 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004517
4518 dpll = DPLL_VGA_MODE_DIS;
4519
Daniel Vetterf47709a2013-03-28 10:42:02 +01004520 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004521 dpll |= DPLLB_MODE_LVDS;
4522 else
4523 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01004524
Daniel Vetteref1b4602013-06-01 17:17:04 +02004525 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Daniel Vetter198a037f2013-04-19 11:14:37 +02004526 dpll |= (crtc->config.pixel_multiplier - 1)
4527 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004528 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02004529
4530 if (is_sdvo)
4531 dpll |= DPLL_DVO_HIGH_SPEED;
4532
Daniel Vetterf47709a2013-03-28 10:42:02 +01004533 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004534 dpll |= DPLL_DVO_HIGH_SPEED;
4535
4536 /* compute bitmask from p1 value */
4537 if (IS_PINEVIEW(dev))
4538 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
4539 else {
4540 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4541 if (IS_G4X(dev) && reduced_clock)
4542 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
4543 }
4544 switch (clock->p2) {
4545 case 5:
4546 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4547 break;
4548 case 7:
4549 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4550 break;
4551 case 10:
4552 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4553 break;
4554 case 14:
4555 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4556 break;
4557 }
4558 if (INTEL_INFO(dev)->gen >= 4)
4559 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
4560
Daniel Vetter09ede542013-04-30 14:01:45 +02004561 if (crtc->config.sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004562 dpll |= PLL_REF_INPUT_TVCLKINBC;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004563 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004564 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4565 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4566 else
4567 dpll |= PLL_REF_INPUT_DREFCLK;
4568
4569 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004570 crtc->config.dpll_hw_state.dpll = dpll;
4571
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004572 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetteref1b4602013-06-01 17:17:04 +02004573 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
4574 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004575 crtc->config.dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004576 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02004577
4578 if (crtc->config.has_dp_encoder)
4579 intel_dp_set_m_n(crtc);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004580}
4581
Daniel Vetterf47709a2013-03-28 10:42:02 +01004582static void i8xx_update_pll(struct intel_crtc *crtc,
Daniel Vetterf47709a2013-03-28 10:42:02 +01004583 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004584 int num_connectors)
4585{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004586 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004587 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004588 u32 dpll;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004589 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004590
Daniel Vetterf47709a2013-03-28 10:42:02 +01004591 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304592
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004593 dpll = DPLL_VGA_MODE_DIS;
4594
Daniel Vetterf47709a2013-03-28 10:42:02 +01004595 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004596 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4597 } else {
4598 if (clock->p1 == 2)
4599 dpll |= PLL_P1_DIVIDE_BY_TWO;
4600 else
4601 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4602 if (clock->p2 == 4)
4603 dpll |= PLL_P2_DIVIDE_BY_4;
4604 }
4605
Daniel Vetterf47709a2013-03-28 10:42:02 +01004606 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004607 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4608 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4609 else
4610 dpll |= PLL_REF_INPUT_DREFCLK;
4611
4612 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004613 crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004614}
4615
Daniel Vetter8a654f32013-06-01 17:16:22 +02004616static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004617{
4618 struct drm_device *dev = intel_crtc->base.dev;
4619 struct drm_i915_private *dev_priv = dev->dev_private;
4620 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02004621 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02004622 struct drm_display_mode *adjusted_mode =
4623 &intel_crtc->config.adjusted_mode;
4624 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004625 uint32_t vsyncshift, crtc_vtotal, crtc_vblank_end;
4626
4627 /* We need to be careful not to changed the adjusted mode, for otherwise
4628 * the hw state checker will get angry at the mismatch. */
4629 crtc_vtotal = adjusted_mode->crtc_vtotal;
4630 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004631
4632 if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4633 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004634 crtc_vtotal -= 1;
4635 crtc_vblank_end -= 1;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004636 vsyncshift = adjusted_mode->crtc_hsync_start
4637 - adjusted_mode->crtc_htotal / 2;
4638 } else {
4639 vsyncshift = 0;
4640 }
4641
4642 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004643 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004644
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004645 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004646 (adjusted_mode->crtc_hdisplay - 1) |
4647 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004648 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004649 (adjusted_mode->crtc_hblank_start - 1) |
4650 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004651 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004652 (adjusted_mode->crtc_hsync_start - 1) |
4653 ((adjusted_mode->crtc_hsync_end - 1) << 16));
4654
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004655 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004656 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004657 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004658 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004659 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004660 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004661 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004662 (adjusted_mode->crtc_vsync_start - 1) |
4663 ((adjusted_mode->crtc_vsync_end - 1) << 16));
4664
Paulo Zanonib5e508d2012-10-24 11:34:43 -02004665 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
4666 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
4667 * documented on the DDI_FUNC_CTL register description, EDP Input Select
4668 * bits. */
4669 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
4670 (pipe == PIPE_B || pipe == PIPE_C))
4671 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
4672
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004673 /* pipesrc controls the size that is scaled from, which should
4674 * always be the user's requested size.
4675 */
4676 I915_WRITE(PIPESRC(pipe),
4677 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
4678}
4679
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02004680static void intel_get_pipe_timings(struct intel_crtc *crtc,
4681 struct intel_crtc_config *pipe_config)
4682{
4683 struct drm_device *dev = crtc->base.dev;
4684 struct drm_i915_private *dev_priv = dev->dev_private;
4685 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
4686 uint32_t tmp;
4687
4688 tmp = I915_READ(HTOTAL(cpu_transcoder));
4689 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
4690 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
4691 tmp = I915_READ(HBLANK(cpu_transcoder));
4692 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
4693 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
4694 tmp = I915_READ(HSYNC(cpu_transcoder));
4695 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
4696 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
4697
4698 tmp = I915_READ(VTOTAL(cpu_transcoder));
4699 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
4700 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
4701 tmp = I915_READ(VBLANK(cpu_transcoder));
4702 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
4703 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
4704 tmp = I915_READ(VSYNC(cpu_transcoder));
4705 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
4706 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
4707
4708 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
4709 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
4710 pipe_config->adjusted_mode.crtc_vtotal += 1;
4711 pipe_config->adjusted_mode.crtc_vblank_end += 1;
4712 }
4713
4714 tmp = I915_READ(PIPESRC(crtc->pipe));
4715 pipe_config->requested_mode.vdisplay = (tmp & 0xffff) + 1;
4716 pipe_config->requested_mode.hdisplay = ((tmp >> 16) & 0xffff) + 1;
4717}
4718
Daniel Vetter84b046f2013-02-19 18:48:54 +01004719static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
4720{
4721 struct drm_device *dev = intel_crtc->base.dev;
4722 struct drm_i915_private *dev_priv = dev->dev_private;
4723 uint32_t pipeconf;
4724
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02004725 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01004726
4727 if (intel_crtc->pipe == 0 && INTEL_INFO(dev)->gen < 4) {
4728 /* Enable pixel doubling when the dot clock is > 90% of the (display)
4729 * core speed.
4730 *
4731 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
4732 * pipe == 0 check?
4733 */
4734 if (intel_crtc->config.requested_mode.clock >
4735 dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
4736 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01004737 }
4738
Daniel Vetterff9ce462013-04-24 14:57:17 +02004739 /* only g4x and later have fancy bpc/dither controls */
4740 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02004741 /* Bspec claims that we can't use dithering for 30bpp pipes. */
4742 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
4743 pipeconf |= PIPECONF_DITHER_EN |
4744 PIPECONF_DITHER_TYPE_SP;
4745
4746 switch (intel_crtc->config.pipe_bpp) {
4747 case 18:
4748 pipeconf |= PIPECONF_6BPC;
4749 break;
4750 case 24:
4751 pipeconf |= PIPECONF_8BPC;
4752 break;
4753 case 30:
4754 pipeconf |= PIPECONF_10BPC;
4755 break;
4756 default:
4757 /* Case prevented by intel_choose_pipe_bpp_dither. */
4758 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01004759 }
4760 }
4761
4762 if (HAS_PIPE_CXSR(dev)) {
4763 if (intel_crtc->lowfreq_avail) {
4764 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
4765 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
4766 } else {
4767 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01004768 }
4769 }
4770
Daniel Vetter84b046f2013-02-19 18:48:54 +01004771 if (!IS_GEN2(dev) &&
4772 intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
4773 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
4774 else
4775 pipeconf |= PIPECONF_PROGRESSIVE;
4776
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02004777 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
4778 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03004779
Daniel Vetter84b046f2013-02-19 18:48:54 +01004780 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
4781 POSTING_READ(PIPECONF(intel_crtc->pipe));
4782}
4783
Eric Anholtf564048e2011-03-30 13:01:02 -07004784static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07004785 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02004786 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08004787{
4788 struct drm_device *dev = crtc->dev;
4789 struct drm_i915_private *dev_priv = dev->dev_private;
4790 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01004791 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08004792 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07004793 int plane = intel_crtc->plane;
Eric Anholtc751ce42010-03-25 11:48:48 -07004794 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07004795 intel_clock_t clock, reduced_clock;
Daniel Vetter84b046f2013-02-19 18:48:54 +01004796 u32 dspcntr;
Daniel Vettera16af7212013-04-30 14:01:44 +02004797 bool ok, has_reduced_clock = false;
4798 bool is_lvds = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01004799 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08004800 const intel_limit_t *limit;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00004801 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08004802
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02004803 for_each_encoder_on_crtc(dev, crtc, encoder) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004804 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004805 case INTEL_OUTPUT_LVDS:
4806 is_lvds = true;
4807 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08004808 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05004809
Eric Anholtc751ce42010-03-25 11:48:48 -07004810 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08004811 }
4812
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004813 refclk = i9xx_get_refclk(crtc, num_connectors);
Jesse Barnes79e53942008-11-07 14:24:08 -08004814
Ma Lingd4906092009-03-18 20:13:27 +08004815 /*
4816 * Returns a set of divisors for the desired target clock with the given
4817 * refclk, or FALSE. The returned values represent the clock equation:
4818 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
4819 */
Chris Wilson1b894b52010-12-14 20:04:54 +00004820 limit = intel_limit(crtc, refclk);
Daniel Vetterff9a6752013-06-01 17:16:21 +02004821 ok = dev_priv->display.find_dpll(limit, crtc,
4822 intel_crtc->config.port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02004823 refclk, NULL, &clock);
4824 if (!ok && !intel_crtc->config.clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004825 DRM_ERROR("Couldn't find PLL settings for mode!\n");
Eric Anholtf564048e2011-03-30 13:01:02 -07004826 return -EINVAL;
4827 }
4828
4829 /* Ensure that the cursor is valid for the new mode before changing... */
4830 intel_crtc_update_cursor(crtc, true);
4831
4832 if (is_lvds && dev_priv->lvds_downclock_avail) {
Sean Paulcec2f352012-01-10 15:09:36 -08004833 /*
4834 * Ensure we match the reduced clock's P to the target clock.
4835 * If the clocks don't match, we can't switch the display clock
4836 * by using the FP0/FP1. In such case we will disable the LVDS
4837 * downclock feature.
4838 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02004839 has_reduced_clock =
4840 dev_priv->display.find_dpll(limit, crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07004841 dev_priv->lvds_downclock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02004842 refclk, &clock,
Eric Anholtf564048e2011-03-30 13:01:02 -07004843 &reduced_clock);
Eric Anholtf564048e2011-03-30 13:01:02 -07004844 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01004845 /* Compat-code for transition, will disappear. */
4846 if (!intel_crtc->config.clock_set) {
4847 intel_crtc->config.dpll.n = clock.n;
4848 intel_crtc->config.dpll.m1 = clock.m1;
4849 intel_crtc->config.dpll.m2 = clock.m2;
4850 intel_crtc->config.dpll.p1 = clock.p1;
4851 intel_crtc->config.dpll.p2 = clock.p2;
4852 }
Eric Anholtf564048e2011-03-30 13:01:02 -07004853
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004854 if (IS_GEN2(dev))
Daniel Vetter8a654f32013-06-01 17:16:22 +02004855 i8xx_update_pll(intel_crtc,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304856 has_reduced_clock ? &reduced_clock : NULL,
4857 num_connectors);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004858 else if (IS_VALLEYVIEW(dev))
Daniel Vetterf47709a2013-03-28 10:42:02 +01004859 vlv_update_pll(intel_crtc);
Eric Anholtf564048e2011-03-30 13:01:02 -07004860 else
Daniel Vetterf47709a2013-03-28 10:42:02 +01004861 i9xx_update_pll(intel_crtc,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004862 has_reduced_clock ? &reduced_clock : NULL,
Jesse Barnes89b667f2013-04-18 14:51:36 -07004863 num_connectors);
Eric Anholtf564048e2011-03-30 13:01:02 -07004864
Eric Anholtf564048e2011-03-30 13:01:02 -07004865 /* Set up the display plane register */
4866 dspcntr = DISPPLANE_GAMMA_ENABLE;
4867
Jesse Barnesda6ecc52013-03-08 10:46:00 -08004868 if (!IS_VALLEYVIEW(dev)) {
4869 if (pipe == 0)
4870 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
4871 else
4872 dspcntr |= DISPPLANE_SEL_PIPE_B;
4873 }
Eric Anholtf564048e2011-03-30 13:01:02 -07004874
Daniel Vetter8a654f32013-06-01 17:16:22 +02004875 intel_set_pipe_timings(intel_crtc);
Eric Anholtf564048e2011-03-30 13:01:02 -07004876
4877 /* pipesrc and dspsize control the size that is scaled from,
4878 * which should always be the user's requested size.
4879 */
Eric Anholt929c77f2011-03-30 13:01:04 -07004880 I915_WRITE(DSPSIZE(plane),
4881 ((mode->vdisplay - 1) << 16) |
4882 (mode->hdisplay - 1));
4883 I915_WRITE(DSPPOS(plane), 0);
Eric Anholtf564048e2011-03-30 13:01:02 -07004884
Daniel Vetter84b046f2013-02-19 18:48:54 +01004885 i9xx_set_pipeconf(intel_crtc);
4886
Eric Anholtf564048e2011-03-30 13:01:02 -07004887 I915_WRITE(DSPCNTR(plane), dspcntr);
4888 POSTING_READ(DSPCNTR(plane));
4889
Daniel Vetter94352cf2012-07-05 22:51:56 +02004890 ret = intel_pipe_set_base(crtc, x, y, fb);
Eric Anholtf564048e2011-03-30 13:01:02 -07004891
4892 intel_update_watermarks(dev);
4893
Eric Anholtf564048e2011-03-30 13:01:02 -07004894 return ret;
4895}
4896
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02004897static void i9xx_get_pfit_config(struct intel_crtc *crtc,
4898 struct intel_crtc_config *pipe_config)
4899{
4900 struct drm_device *dev = crtc->base.dev;
4901 struct drm_i915_private *dev_priv = dev->dev_private;
4902 uint32_t tmp;
4903
4904 tmp = I915_READ(PFIT_CONTROL);
4905
4906 if (INTEL_INFO(dev)->gen < 4) {
4907 if (crtc->pipe != PIPE_B)
4908 return;
4909
4910 /* gen2/3 store dither state in pfit control, needs to match */
4911 pipe_config->gmch_pfit.control = tmp & PANEL_8TO6_DITHER_ENABLE;
4912 } else {
4913 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
4914 return;
4915 }
4916
4917 if (!(tmp & PFIT_ENABLE))
4918 return;
4919
4920 pipe_config->gmch_pfit.control = I915_READ(PFIT_CONTROL);
4921 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
4922 if (INTEL_INFO(dev)->gen < 5)
4923 pipe_config->gmch_pfit.lvds_border_bits =
4924 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
4925}
4926
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01004927static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
4928 struct intel_crtc_config *pipe_config)
4929{
4930 struct drm_device *dev = crtc->base.dev;
4931 struct drm_i915_private *dev_priv = dev->dev_private;
4932 uint32_t tmp;
4933
Daniel Vettereccb1402013-05-22 00:50:22 +02004934 pipe_config->cpu_transcoder = crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02004935 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02004936
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01004937 tmp = I915_READ(PIPECONF(crtc->pipe));
4938 if (!(tmp & PIPECONF_ENABLE))
4939 return false;
4940
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02004941 intel_get_pipe_timings(crtc, pipe_config);
4942
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02004943 i9xx_get_pfit_config(crtc, pipe_config);
4944
Daniel Vetter6c49f242013-06-06 12:45:25 +02004945 if (INTEL_INFO(dev)->gen >= 4) {
4946 tmp = I915_READ(DPLL_MD(crtc->pipe));
4947 pipe_config->pixel_multiplier =
4948 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
4949 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004950 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02004951 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
4952 tmp = I915_READ(DPLL(crtc->pipe));
4953 pipe_config->pixel_multiplier =
4954 ((tmp & SDVO_MULTIPLIER_MASK)
4955 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
4956 } else {
4957 /* Note that on i915G/GM the pixel multiplier is in the sdvo
4958 * port and will be fixed up in the encoder->get_config
4959 * function. */
4960 pipe_config->pixel_multiplier = 1;
4961 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004962 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
4963 if (!IS_VALLEYVIEW(dev)) {
4964 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
4965 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03004966 } else {
4967 /* Mask out read-only status bits. */
4968 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
4969 DPLL_PORTC_READY_MASK |
4970 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02004971 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02004972
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01004973 return true;
4974}
4975
Paulo Zanonidde86e22012-12-01 12:04:25 -02004976static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07004977{
4978 struct drm_i915_private *dev_priv = dev->dev_private;
4979 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004980 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07004981 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004982 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07004983 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07004984 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07004985 bool has_ck505 = false;
4986 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004987
4988 /* We need to take the global config into account */
Keith Packard199e5d72011-09-22 12:01:57 -07004989 list_for_each_entry(encoder, &mode_config->encoder_list,
4990 base.head) {
4991 switch (encoder->type) {
4992 case INTEL_OUTPUT_LVDS:
4993 has_panel = true;
4994 has_lvds = true;
4995 break;
4996 case INTEL_OUTPUT_EDP:
4997 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03004998 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07004999 has_cpu_edp = true;
5000 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005001 }
5002 }
5003
Keith Packard99eb6a02011-09-26 14:29:12 -07005004 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005005 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07005006 can_ssc = has_ck505;
5007 } else {
5008 has_ck505 = false;
5009 can_ssc = true;
5010 }
5011
Imre Deak2de69052013-05-08 13:14:04 +03005012 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
5013 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005014
5015 /* Ironlake: try to setup display ref clock before DPLL
5016 * enabling. This is only under driver's control after
5017 * PCH B stepping, previous chipset stepping should be
5018 * ignoring this setting.
5019 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005020 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005021
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005022 /* As we must carefully and slowly disable/enable each source in turn,
5023 * compute the final state we want first and check if we need to
5024 * make any changes at all.
5025 */
5026 final = val;
5027 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07005028 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005029 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07005030 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005031 final |= DREF_NONSPREAD_SOURCE_ENABLE;
5032
5033 final &= ~DREF_SSC_SOURCE_MASK;
5034 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5035 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005036
Keith Packard199e5d72011-09-22 12:01:57 -07005037 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005038 final |= DREF_SSC_SOURCE_ENABLE;
5039
5040 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5041 final |= DREF_SSC1_ENABLE;
5042
5043 if (has_cpu_edp) {
5044 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5045 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
5046 else
5047 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
5048 } else
5049 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5050 } else {
5051 final |= DREF_SSC_SOURCE_DISABLE;
5052 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5053 }
5054
5055 if (final == val)
5056 return;
5057
5058 /* Always enable nonspread source */
5059 val &= ~DREF_NONSPREAD_SOURCE_MASK;
5060
5061 if (has_ck505)
5062 val |= DREF_NONSPREAD_CK505_ENABLE;
5063 else
5064 val |= DREF_NONSPREAD_SOURCE_ENABLE;
5065
5066 if (has_panel) {
5067 val &= ~DREF_SSC_SOURCE_MASK;
5068 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005069
Keith Packard199e5d72011-09-22 12:01:57 -07005070 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07005071 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005072 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005073 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02005074 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005075 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005076
5077 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005078 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005079 POSTING_READ(PCH_DREF_CONTROL);
5080 udelay(200);
5081
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005082 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005083
5084 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07005085 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07005086 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005087 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005088 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005089 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07005090 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005091 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005092 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005093 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005094
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005095 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005096 POSTING_READ(PCH_DREF_CONTROL);
5097 udelay(200);
5098 } else {
5099 DRM_DEBUG_KMS("Disabling SSC entirely\n");
5100
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005101 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07005102
5103 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005104 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005105
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005106 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005107 POSTING_READ(PCH_DREF_CONTROL);
5108 udelay(200);
5109
5110 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005111 val &= ~DREF_SSC_SOURCE_MASK;
5112 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005113
5114 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005115 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005116
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005117 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005118 POSTING_READ(PCH_DREF_CONTROL);
5119 udelay(200);
5120 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005121
5122 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005123}
5124
Paulo Zanonidde86e22012-12-01 12:04:25 -02005125/* Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O. */
5126static void lpt_init_pch_refclk(struct drm_device *dev)
5127{
5128 struct drm_i915_private *dev_priv = dev->dev_private;
5129 struct drm_mode_config *mode_config = &dev->mode_config;
5130 struct intel_encoder *encoder;
5131 bool has_vga = false;
5132 bool is_sdv = false;
5133 u32 tmp;
5134
5135 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5136 switch (encoder->type) {
5137 case INTEL_OUTPUT_ANALOG:
5138 has_vga = true;
5139 break;
5140 }
5141 }
5142
5143 if (!has_vga)
5144 return;
5145
Daniel Vetterc00db242013-01-22 15:33:27 +01005146 mutex_lock(&dev_priv->dpio_lock);
5147
Paulo Zanonidde86e22012-12-01 12:04:25 -02005148 /* XXX: Rip out SDV support once Haswell ships for real. */
5149 if (IS_HASWELL(dev) && (dev->pci_device & 0xFF00) == 0x0C00)
5150 is_sdv = true;
5151
5152 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5153 tmp &= ~SBI_SSCCTL_DISABLE;
5154 tmp |= SBI_SSCCTL_PATHALT;
5155 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5156
5157 udelay(24);
5158
5159 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5160 tmp &= ~SBI_SSCCTL_PATHALT;
5161 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5162
5163 if (!is_sdv) {
5164 tmp = I915_READ(SOUTH_CHICKEN2);
5165 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
5166 I915_WRITE(SOUTH_CHICKEN2, tmp);
5167
5168 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
5169 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
5170 DRM_ERROR("FDI mPHY reset assert timeout\n");
5171
5172 tmp = I915_READ(SOUTH_CHICKEN2);
5173 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
5174 I915_WRITE(SOUTH_CHICKEN2, tmp);
5175
5176 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
5177 FDI_MPHY_IOSFSB_RESET_STATUS) == 0,
5178 100))
5179 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
5180 }
5181
5182 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
5183 tmp &= ~(0xFF << 24);
5184 tmp |= (0x12 << 24);
5185 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
5186
Paulo Zanonidde86e22012-12-01 12:04:25 -02005187 if (is_sdv) {
5188 tmp = intel_sbi_read(dev_priv, 0x800C, SBI_MPHY);
5189 tmp |= 0x7FFF;
5190 intel_sbi_write(dev_priv, 0x800C, tmp, SBI_MPHY);
5191 }
5192
5193 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
5194 tmp |= (1 << 11);
5195 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
5196
5197 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
5198 tmp |= (1 << 11);
5199 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
5200
5201 if (is_sdv) {
5202 tmp = intel_sbi_read(dev_priv, 0x2038, SBI_MPHY);
5203 tmp |= (0x3F << 24) | (0xF << 20) | (0xF << 16);
5204 intel_sbi_write(dev_priv, 0x2038, tmp, SBI_MPHY);
5205
5206 tmp = intel_sbi_read(dev_priv, 0x2138, SBI_MPHY);
5207 tmp |= (0x3F << 24) | (0xF << 20) | (0xF << 16);
5208 intel_sbi_write(dev_priv, 0x2138, tmp, SBI_MPHY);
5209
5210 tmp = intel_sbi_read(dev_priv, 0x203C, SBI_MPHY);
5211 tmp |= (0x3F << 8);
5212 intel_sbi_write(dev_priv, 0x203C, tmp, SBI_MPHY);
5213
5214 tmp = intel_sbi_read(dev_priv, 0x213C, SBI_MPHY);
5215 tmp |= (0x3F << 8);
5216 intel_sbi_write(dev_priv, 0x213C, tmp, SBI_MPHY);
5217 }
5218
5219 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
5220 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5221 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
5222
5223 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
5224 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5225 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
5226
5227 if (!is_sdv) {
5228 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
5229 tmp &= ~(7 << 13);
5230 tmp |= (5 << 13);
5231 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
5232
5233 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
5234 tmp &= ~(7 << 13);
5235 tmp |= (5 << 13);
5236 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
5237 }
5238
5239 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
5240 tmp &= ~0xFF;
5241 tmp |= 0x1C;
5242 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
5243
5244 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
5245 tmp &= ~0xFF;
5246 tmp |= 0x1C;
5247 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
5248
5249 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
5250 tmp &= ~(0xFF << 16);
5251 tmp |= (0x1C << 16);
5252 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
5253
5254 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
5255 tmp &= ~(0xFF << 16);
5256 tmp |= (0x1C << 16);
5257 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
5258
5259 if (!is_sdv) {
5260 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
5261 tmp |= (1 << 27);
5262 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
5263
5264 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
5265 tmp |= (1 << 27);
5266 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
5267
5268 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
5269 tmp &= ~(0xF << 28);
5270 tmp |= (4 << 28);
5271 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
5272
5273 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
5274 tmp &= ~(0xF << 28);
5275 tmp |= (4 << 28);
5276 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
5277 }
5278
5279 /* ULT uses SBI_GEN0, but ULT doesn't have VGA, so we don't care. */
5280 tmp = intel_sbi_read(dev_priv, SBI_DBUFF0, SBI_ICLK);
5281 tmp |= SBI_DBUFF0_ENABLE;
5282 intel_sbi_write(dev_priv, SBI_DBUFF0, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01005283
5284 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005285}
5286
5287/*
5288 * Initialize reference clocks when the driver loads
5289 */
5290void intel_init_pch_refclk(struct drm_device *dev)
5291{
5292 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
5293 ironlake_init_pch_refclk(dev);
5294 else if (HAS_PCH_LPT(dev))
5295 lpt_init_pch_refclk(dev);
5296}
5297
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005298static int ironlake_get_refclk(struct drm_crtc *crtc)
5299{
5300 struct drm_device *dev = crtc->dev;
5301 struct drm_i915_private *dev_priv = dev->dev_private;
5302 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005303 int num_connectors = 0;
5304 bool is_lvds = false;
5305
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02005306 for_each_encoder_on_crtc(dev, crtc, encoder) {
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005307 switch (encoder->type) {
5308 case INTEL_OUTPUT_LVDS:
5309 is_lvds = true;
5310 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005311 }
5312 num_connectors++;
5313 }
5314
5315 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
5316 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005317 dev_priv->vbt.lvds_ssc_freq);
5318 return dev_priv->vbt.lvds_ssc_freq * 1000;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005319 }
5320
5321 return 120000;
5322}
5323
Daniel Vetter6ff93602013-04-19 11:24:36 +02005324static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03005325{
5326 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5327 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5328 int pipe = intel_crtc->pipe;
5329 uint32_t val;
5330
Daniel Vetter78114072013-06-13 00:54:57 +02005331 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03005332
Daniel Vetter965e0c42013-03-27 00:44:57 +01005333 switch (intel_crtc->config.pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03005334 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005335 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005336 break;
5337 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005338 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005339 break;
5340 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005341 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005342 break;
5343 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005344 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005345 break;
5346 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03005347 /* Case prevented by intel_choose_pipe_bpp_dither. */
5348 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03005349 }
5350
Daniel Vetterd8b32242013-04-25 17:54:44 +02005351 if (intel_crtc->config.dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03005352 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5353
Daniel Vetter6ff93602013-04-19 11:24:36 +02005354 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03005355 val |= PIPECONF_INTERLACED_ILK;
5356 else
5357 val |= PIPECONF_PROGRESSIVE;
5358
Daniel Vetter50f3b012013-03-27 00:44:56 +01005359 if (intel_crtc->config.limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005360 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005361
Paulo Zanonic8203562012-09-12 10:06:29 -03005362 I915_WRITE(PIPECONF(pipe), val);
5363 POSTING_READ(PIPECONF(pipe));
5364}
5365
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005366/*
5367 * Set up the pipe CSC unit.
5368 *
5369 * Currently only full range RGB to limited range RGB conversion
5370 * is supported, but eventually this should handle various
5371 * RGB<->YCbCr scenarios as well.
5372 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01005373static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005374{
5375 struct drm_device *dev = crtc->dev;
5376 struct drm_i915_private *dev_priv = dev->dev_private;
5377 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5378 int pipe = intel_crtc->pipe;
5379 uint16_t coeff = 0x7800; /* 1.0 */
5380
5381 /*
5382 * TODO: Check what kind of values actually come out of the pipe
5383 * with these coeff/postoff values and adjust to get the best
5384 * accuracy. Perhaps we even need to take the bpc value into
5385 * consideration.
5386 */
5387
Daniel Vetter50f3b012013-03-27 00:44:56 +01005388 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005389 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
5390
5391 /*
5392 * GY/GU and RY/RU should be the other way around according
5393 * to BSpec, but reality doesn't agree. Just set them up in
5394 * a way that results in the correct picture.
5395 */
5396 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
5397 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
5398
5399 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
5400 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
5401
5402 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
5403 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
5404
5405 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
5406 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
5407 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
5408
5409 if (INTEL_INFO(dev)->gen > 6) {
5410 uint16_t postoff = 0;
5411
Daniel Vetter50f3b012013-03-27 00:44:56 +01005412 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005413 postoff = (16 * (1 << 13) / 255) & 0x1fff;
5414
5415 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
5416 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
5417 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
5418
5419 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
5420 } else {
5421 uint32_t mode = CSC_MODE_YUV_TO_RGB;
5422
Daniel Vetter50f3b012013-03-27 00:44:56 +01005423 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005424 mode |= CSC_BLACK_SCREEN_OFFSET;
5425
5426 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
5427 }
5428}
5429
Daniel Vetter6ff93602013-04-19 11:24:36 +02005430static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005431{
5432 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5433 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02005434 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005435 uint32_t val;
5436
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02005437 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005438
Daniel Vetterd8b32242013-04-25 17:54:44 +02005439 if (intel_crtc->config.dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005440 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5441
Daniel Vetter6ff93602013-04-19 11:24:36 +02005442 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005443 val |= PIPECONF_INTERLACED_ILK;
5444 else
5445 val |= PIPECONF_PROGRESSIVE;
5446
Paulo Zanoni702e7a52012-10-23 18:29:59 -02005447 I915_WRITE(PIPECONF(cpu_transcoder), val);
5448 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02005449
5450 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
5451 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005452}
5453
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005454static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005455 intel_clock_t *clock,
5456 bool *has_reduced_clock,
5457 intel_clock_t *reduced_clock)
5458{
5459 struct drm_device *dev = crtc->dev;
5460 struct drm_i915_private *dev_priv = dev->dev_private;
5461 struct intel_encoder *intel_encoder;
5462 int refclk;
5463 const intel_limit_t *limit;
Daniel Vettera16af7212013-04-30 14:01:44 +02005464 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005465
5466 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5467 switch (intel_encoder->type) {
5468 case INTEL_OUTPUT_LVDS:
5469 is_lvds = true;
5470 break;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005471 }
5472 }
5473
5474 refclk = ironlake_get_refclk(crtc);
5475
5476 /*
5477 * Returns a set of divisors for the desired target clock with the given
5478 * refclk, or FALSE. The returned values represent the clock equation:
5479 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
5480 */
5481 limit = intel_limit(crtc, refclk);
Daniel Vetterff9a6752013-06-01 17:16:21 +02005482 ret = dev_priv->display.find_dpll(limit, crtc,
5483 to_intel_crtc(crtc)->config.port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02005484 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005485 if (!ret)
5486 return false;
5487
5488 if (is_lvds && dev_priv->lvds_downclock_avail) {
5489 /*
5490 * Ensure we match the reduced clock's P to the target clock.
5491 * If the clocks don't match, we can't switch the display clock
5492 * by using the FP0/FP1. In such case we will disable the LVDS
5493 * downclock feature.
5494 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02005495 *has_reduced_clock =
5496 dev_priv->display.find_dpll(limit, crtc,
5497 dev_priv->lvds_downclock,
5498 refclk, clock,
5499 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005500 }
5501
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005502 return true;
5503}
5504
Daniel Vetter01a415f2012-10-27 15:58:40 +02005505static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
5506{
5507 struct drm_i915_private *dev_priv = dev->dev_private;
5508 uint32_t temp;
5509
5510 temp = I915_READ(SOUTH_CHICKEN1);
5511 if (temp & FDI_BC_BIFURCATION_SELECT)
5512 return;
5513
5514 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
5515 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
5516
5517 temp |= FDI_BC_BIFURCATION_SELECT;
5518 DRM_DEBUG_KMS("enabling fdi C rx\n");
5519 I915_WRITE(SOUTH_CHICKEN1, temp);
5520 POSTING_READ(SOUTH_CHICKEN1);
5521}
5522
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005523static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
Daniel Vetter01a415f2012-10-27 15:58:40 +02005524{
5525 struct drm_device *dev = intel_crtc->base.dev;
5526 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005527
5528 switch (intel_crtc->pipe) {
5529 case PIPE_A:
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005530 break;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005531 case PIPE_B:
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005532 if (intel_crtc->config.fdi_lanes > 2)
Daniel Vetter01a415f2012-10-27 15:58:40 +02005533 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
5534 else
5535 cpt_enable_fdi_bc_bifurcation(dev);
5536
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005537 break;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005538 case PIPE_C:
Daniel Vetter01a415f2012-10-27 15:58:40 +02005539 cpt_enable_fdi_bc_bifurcation(dev);
5540
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005541 break;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005542 default:
5543 BUG();
5544 }
5545}
5546
Paulo Zanonid4b19312012-11-29 11:29:32 -02005547int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
5548{
5549 /*
5550 * Account for spread spectrum to avoid
5551 * oversubscribing the link. Max center spread
5552 * is 2.5%; use 5% for safety's sake.
5553 */
5554 u32 bps = target_clock * bpp * 21 / 20;
5555 return bps / (link_bw * 8) + 1;
5556}
5557
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005558static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02005559{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005560 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005561}
5562
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005563static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005564 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02005565 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005566{
5567 struct drm_crtc *crtc = &intel_crtc->base;
5568 struct drm_device *dev = crtc->dev;
5569 struct drm_i915_private *dev_priv = dev->dev_private;
5570 struct intel_encoder *intel_encoder;
5571 uint32_t dpll;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01005572 int factor, num_connectors = 0;
Daniel Vetter09ede542013-04-30 14:01:45 +02005573 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005574
5575 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5576 switch (intel_encoder->type) {
5577 case INTEL_OUTPUT_LVDS:
5578 is_lvds = true;
5579 break;
5580 case INTEL_OUTPUT_SDVO:
5581 case INTEL_OUTPUT_HDMI:
5582 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005583 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005584 }
5585
5586 num_connectors++;
5587 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005588
Chris Wilsonc1858122010-12-03 21:35:48 +00005589 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07005590 factor = 21;
5591 if (is_lvds) {
5592 if ((intel_panel_use_ssc(dev_priv) &&
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005593 dev_priv->vbt.lvds_ssc_freq == 100) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02005594 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07005595 factor = 25;
Daniel Vetter09ede542013-04-30 14:01:45 +02005596 } else if (intel_crtc->config.sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07005597 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00005598
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005599 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02005600 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00005601
Daniel Vetter9a7c7892013-04-04 22:20:34 +02005602 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
5603 *fp2 |= FP_CB_TUNE;
5604
Chris Wilson5eddb702010-09-11 13:48:45 +01005605 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005606
Eric Anholta07d6782011-03-30 13:01:08 -07005607 if (is_lvds)
5608 dpll |= DPLLB_MODE_LVDS;
5609 else
5610 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02005611
Daniel Vetteref1b4602013-06-01 17:17:04 +02005612 dpll |= (intel_crtc->config.pixel_multiplier - 1)
5613 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02005614
5615 if (is_sdvo)
5616 dpll |= DPLL_DVO_HIGH_SPEED;
Daniel Vetter9566e9a2013-04-19 11:14:36 +02005617 if (intel_crtc->config.has_dp_encoder)
Eric Anholta07d6782011-03-30 13:01:08 -07005618 dpll |= DPLL_DVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08005619
Eric Anholta07d6782011-03-30 13:01:08 -07005620 /* compute bitmask from p1 value */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005621 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005622 /* also FPA1 */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005623 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005624
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005625 switch (intel_crtc->config.dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07005626 case 5:
5627 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5628 break;
5629 case 7:
5630 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5631 break;
5632 case 10:
5633 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5634 break;
5635 case 14:
5636 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5637 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005638 }
5639
Daniel Vetterb4c09f32013-04-30 14:01:42 +02005640 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005641 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08005642 else
5643 dpll |= PLL_REF_INPUT_DREFCLK;
5644
Daniel Vetter959e16d2013-06-05 13:34:21 +02005645 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005646}
5647
Jesse Barnes79e53942008-11-07 14:24:08 -08005648static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
Jesse Barnes79e53942008-11-07 14:24:08 -08005649 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005650 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005651{
5652 struct drm_device *dev = crtc->dev;
5653 struct drm_i915_private *dev_priv = dev->dev_private;
5654 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5655 int pipe = intel_crtc->pipe;
5656 int plane = intel_crtc->plane;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005657 int num_connectors = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005658 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02005659 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03005660 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01005661 bool is_lvds = false;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005662 struct intel_encoder *encoder;
Daniel Vettere2b78262013-06-07 23:10:03 +02005663 struct intel_shared_dpll *pll;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005664 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08005665
5666 for_each_encoder_on_crtc(dev, crtc, encoder) {
5667 switch (encoder->type) {
5668 case INTEL_OUTPUT_LVDS:
5669 is_lvds = true;
5670 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005671 }
5672
5673 num_connectors++;
5674 }
5675
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005676 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
5677 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
5678
Daniel Vetterff9a6752013-06-01 17:16:21 +02005679 ok = ironlake_compute_clocks(crtc, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005680 &has_reduced_clock, &reduced_clock);
Daniel Vetteree9300b2013-06-03 22:40:22 +02005681 if (!ok && !intel_crtc->config.clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08005682 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5683 return -EINVAL;
5684 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01005685 /* Compat-code for transition, will disappear. */
5686 if (!intel_crtc->config.clock_set) {
5687 intel_crtc->config.dpll.n = clock.n;
5688 intel_crtc->config.dpll.m1 = clock.m1;
5689 intel_crtc->config.dpll.m2 = clock.m2;
5690 intel_crtc->config.dpll.p1 = clock.p1;
5691 intel_crtc->config.dpll.p2 = clock.p2;
5692 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005693
5694 /* Ensure that the cursor is valid for the new mode before changing... */
5695 intel_crtc_update_cursor(crtc, true);
5696
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005697 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
Daniel Vetter8b470472013-03-28 10:41:59 +01005698 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005699 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02005700 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005701 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02005702
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005703 dpll = ironlake_compute_dpll(intel_crtc,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02005704 &fp, &reduced_clock,
5705 has_reduced_clock ? &fp2 : NULL);
5706
Daniel Vetter959e16d2013-06-05 13:34:21 +02005707 intel_crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02005708 intel_crtc->config.dpll_hw_state.fp0 = fp;
5709 if (has_reduced_clock)
5710 intel_crtc->config.dpll_hw_state.fp1 = fp2;
5711 else
5712 intel_crtc->config.dpll_hw_state.fp1 = fp;
5713
Daniel Vetterb89a1d32013-06-05 13:34:24 +02005714 pll = intel_get_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005715 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03005716 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
5717 pipe_name(pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07005718 return -EINVAL;
5719 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005720 } else
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005721 intel_put_shared_dpll(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08005722
Daniel Vetter03afc4a2013-04-02 23:42:31 +02005723 if (intel_crtc->config.has_dp_encoder)
5724 intel_dp_set_m_n(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08005725
Daniel Vetterbcd644e2013-06-05 13:34:22 +02005726 if (is_lvds && has_reduced_clock && i915_powersave)
5727 intel_crtc->lowfreq_avail = true;
5728 else
5729 intel_crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02005730
5731 if (intel_crtc->config.has_pch_encoder) {
5732 pll = intel_crtc_to_shared_dpll(intel_crtc);
5733
Jesse Barnes79e53942008-11-07 14:24:08 -08005734 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005735
Daniel Vetter8a654f32013-06-01 17:16:22 +02005736 intel_set_pipe_timings(intel_crtc);
Krzysztof Halasa734b4152010-05-25 18:41:46 +02005737
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01005738 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01005739 intel_cpu_transcoder_set_m_n(intel_crtc,
5740 &intel_crtc->config.fdi_m_n);
5741 }
Chris Wilson5eddb702010-09-11 13:48:45 +01005742
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005743 if (IS_IVYBRIDGE(dev))
5744 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
Zhenyu Wang2c072452009-06-05 15:38:42 +08005745
Daniel Vetter6ff93602013-04-19 11:24:36 +02005746 ironlake_set_pipeconf(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08005747
Paulo Zanonia1f9e772012-09-12 10:06:32 -03005748 /* Set up the display plane register */
5749 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08005750 POSTING_READ(DSPCNTR(plane));
Jesse Barnes79e53942008-11-07 14:24:08 -08005751
Daniel Vetter94352cf2012-07-05 22:51:56 +02005752 ret = intel_pipe_set_base(crtc, x, y, fb);
Shaohua Li7662c8b2009-06-26 11:23:55 +08005753
5754 intel_update_watermarks(dev);
5755
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005756 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08005757}
5758
Daniel Vetter72419202013-04-04 13:28:53 +02005759static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5760 struct intel_crtc_config *pipe_config)
5761{
5762 struct drm_device *dev = crtc->base.dev;
5763 struct drm_i915_private *dev_priv = dev->dev_private;
5764 enum transcoder transcoder = pipe_config->cpu_transcoder;
5765
5766 pipe_config->fdi_m_n.link_m = I915_READ(PIPE_LINK_M1(transcoder));
5767 pipe_config->fdi_m_n.link_n = I915_READ(PIPE_LINK_N1(transcoder));
5768 pipe_config->fdi_m_n.gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
5769 & ~TU_SIZE_MASK;
5770 pipe_config->fdi_m_n.gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
5771 pipe_config->fdi_m_n.tu = ((I915_READ(PIPE_DATA_M1(transcoder))
5772 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
5773}
5774
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005775static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5776 struct intel_crtc_config *pipe_config)
5777{
5778 struct drm_device *dev = crtc->base.dev;
5779 struct drm_i915_private *dev_priv = dev->dev_private;
5780 uint32_t tmp;
5781
5782 tmp = I915_READ(PF_CTL(crtc->pipe));
5783
5784 if (tmp & PF_ENABLE) {
5785 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
5786 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02005787
5788 /* We currently do not free assignements of panel fitters on
5789 * ivb/hsw (since we don't use the higher upscaling modes which
5790 * differentiates them) so just WARN about this case for now. */
5791 if (IS_GEN7(dev)) {
5792 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
5793 PF_PIPE_SEL_IVB(crtc->pipe));
5794 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005795 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005796}
5797
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005798static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5799 struct intel_crtc_config *pipe_config)
5800{
5801 struct drm_device *dev = crtc->base.dev;
5802 struct drm_i915_private *dev_priv = dev->dev_private;
5803 uint32_t tmp;
5804
Daniel Vettereccb1402013-05-22 00:50:22 +02005805 pipe_config->cpu_transcoder = crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02005806 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02005807
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005808 tmp = I915_READ(PIPECONF(crtc->pipe));
5809 if (!(tmp & PIPECONF_ENABLE))
5810 return false;
5811
Daniel Vetterab9412b2013-05-03 11:49:46 +02005812 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02005813 struct intel_shared_dpll *pll;
5814
Daniel Vetter88adfff2013-03-28 10:42:01 +01005815 pipe_config->has_pch_encoder = true;
5816
Daniel Vetter627eb5a2013-04-29 19:33:42 +02005817 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
5818 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
5819 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02005820
5821 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02005822
Daniel Vetterc0d43d62013-06-07 23:11:08 +02005823 if (HAS_PCH_IBX(dev_priv->dev)) {
5824 pipe_config->shared_dpll = crtc->pipe;
5825 } else {
5826 tmp = I915_READ(PCH_DPLL_SEL);
5827 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
5828 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
5829 else
5830 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
5831 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02005832
5833 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
5834
5835 WARN_ON(!pll->get_hw_state(dev_priv, pll,
5836 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02005837
5838 tmp = pipe_config->dpll_hw_state.dpll;
5839 pipe_config->pixel_multiplier =
5840 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
5841 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter6c49f242013-06-06 12:45:25 +02005842 } else {
5843 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02005844 }
5845
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005846 intel_get_pipe_timings(crtc, pipe_config);
5847
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005848 ironlake_get_pfit_config(crtc, pipe_config);
5849
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005850 return true;
5851}
5852
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005853static void haswell_modeset_global_resources(struct drm_device *dev)
5854{
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005855 bool enable = false;
5856 struct intel_crtc *crtc;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005857
5858 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
Daniel Vettere7a639c2013-05-31 17:49:17 +02005859 if (!crtc->base.enabled)
5860 continue;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005861
Daniel Vettere7a639c2013-05-31 17:49:17 +02005862 if (crtc->pipe != PIPE_A || crtc->config.pch_pfit.size ||
5863 crtc->config.cpu_transcoder != TRANSCODER_EDP)
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005864 enable = true;
5865 }
5866
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005867 intel_set_power_well(dev, enable);
5868}
5869
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005870static int haswell_crtc_mode_set(struct drm_crtc *crtc,
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005871 int x, int y,
5872 struct drm_framebuffer *fb)
5873{
5874 struct drm_device *dev = crtc->dev;
5875 struct drm_i915_private *dev_priv = dev->dev_private;
5876 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005877 int plane = intel_crtc->plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005878 int ret;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005879
Daniel Vetterff9a6752013-06-01 17:16:21 +02005880 if (!intel_ddi_pll_mode_set(crtc))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03005881 return -EINVAL;
5882
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005883 /* Ensure that the cursor is valid for the new mode before changing... */
5884 intel_crtc_update_cursor(crtc, true);
5885
Daniel Vetter03afc4a2013-04-02 23:42:31 +02005886 if (intel_crtc->config.has_dp_encoder)
5887 intel_dp_set_m_n(intel_crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005888
5889 intel_crtc->lowfreq_avail = false;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005890
Daniel Vetter8a654f32013-06-01 17:16:22 +02005891 intel_set_pipe_timings(intel_crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005892
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01005893 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01005894 intel_cpu_transcoder_set_m_n(intel_crtc,
5895 &intel_crtc->config.fdi_m_n);
5896 }
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005897
Daniel Vetter6ff93602013-04-19 11:24:36 +02005898 haswell_set_pipeconf(crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005899
Daniel Vetter50f3b012013-03-27 00:44:56 +01005900 intel_set_pipe_csc(crtc);
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005901
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005902 /* Set up the display plane register */
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005903 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005904 POSTING_READ(DSPCNTR(plane));
5905
5906 ret = intel_pipe_set_base(crtc, x, y, fb);
5907
5908 intel_update_watermarks(dev);
5909
Jesse Barnes79e53942008-11-07 14:24:08 -08005910 return ret;
5911}
5912
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005913static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5914 struct intel_crtc_config *pipe_config)
5915{
5916 struct drm_device *dev = crtc->base.dev;
5917 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005918 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005919 uint32_t tmp;
5920
Daniel Vettereccb1402013-05-22 00:50:22 +02005921 pipe_config->cpu_transcoder = crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02005922 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
5923
Daniel Vettereccb1402013-05-22 00:50:22 +02005924 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
5925 if (tmp & TRANS_DDI_FUNC_ENABLE) {
5926 enum pipe trans_edp_pipe;
5927 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
5928 default:
5929 WARN(1, "unknown pipe linked to edp transcoder\n");
5930 case TRANS_DDI_EDP_INPUT_A_ONOFF:
5931 case TRANS_DDI_EDP_INPUT_A_ON:
5932 trans_edp_pipe = PIPE_A;
5933 break;
5934 case TRANS_DDI_EDP_INPUT_B_ONOFF:
5935 trans_edp_pipe = PIPE_B;
5936 break;
5937 case TRANS_DDI_EDP_INPUT_C_ONOFF:
5938 trans_edp_pipe = PIPE_C;
5939 break;
5940 }
5941
5942 if (trans_edp_pipe == crtc->pipe)
5943 pipe_config->cpu_transcoder = TRANSCODER_EDP;
5944 }
5945
Paulo Zanonib97186f2013-05-03 12:15:36 -03005946 if (!intel_display_power_enabled(dev,
Daniel Vettereccb1402013-05-22 00:50:22 +02005947 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03005948 return false;
5949
Daniel Vettereccb1402013-05-22 00:50:22 +02005950 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005951 if (!(tmp & PIPECONF_ENABLE))
5952 return false;
5953
Daniel Vetter88adfff2013-03-28 10:42:01 +01005954 /*
Paulo Zanonif196e6b2013-04-18 16:35:41 -03005955 * Haswell has only FDI/PCH transcoder A. It is which is connected to
Daniel Vetter88adfff2013-03-28 10:42:01 +01005956 * DDI E. So just check whether this pipe is wired to DDI E and whether
5957 * the PCH transcoder is on.
5958 */
Daniel Vettereccb1402013-05-22 00:50:22 +02005959 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
Daniel Vetter88adfff2013-03-28 10:42:01 +01005960 if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
Daniel Vetterab9412b2013-05-03 11:49:46 +02005961 I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter88adfff2013-03-28 10:42:01 +01005962 pipe_config->has_pch_encoder = true;
5963
Daniel Vetter627eb5a2013-04-29 19:33:42 +02005964 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
5965 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
5966 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02005967
5968 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02005969 }
5970
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005971 intel_get_pipe_timings(crtc, pipe_config);
5972
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005973 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
5974 if (intel_display_power_enabled(dev, pfit_domain))
5975 ironlake_get_pfit_config(crtc, pipe_config);
Daniel Vetter88adfff2013-03-28 10:42:01 +01005976
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005977 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
5978 (I915_READ(IPS_CTL) & IPS_ENABLE);
5979
Daniel Vetter6c49f242013-06-06 12:45:25 +02005980 pipe_config->pixel_multiplier = 1;
5981
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005982 return true;
5983}
5984
Eric Anholtf564048e2011-03-30 13:01:02 -07005985static int intel_crtc_mode_set(struct drm_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07005986 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005987 struct drm_framebuffer *fb)
Eric Anholtf564048e2011-03-30 13:01:02 -07005988{
5989 struct drm_device *dev = crtc->dev;
5990 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter9256aa12012-10-31 19:26:13 +01005991 struct drm_encoder_helper_funcs *encoder_funcs;
5992 struct intel_encoder *encoder;
Eric Anholt0b701d22011-03-30 13:01:03 -07005993 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01005994 struct drm_display_mode *adjusted_mode =
5995 &intel_crtc->config.adjusted_mode;
5996 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
Eric Anholt0b701d22011-03-30 13:01:03 -07005997 int pipe = intel_crtc->pipe;
Eric Anholtf564048e2011-03-30 13:01:02 -07005998 int ret;
5999
Eric Anholt0b701d22011-03-30 13:01:03 -07006000 drm_vblank_pre_modeset(dev, pipe);
6001
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01006002 ret = dev_priv->display.crtc_mode_set(crtc, x, y, fb);
6003
Jesse Barnes79e53942008-11-07 14:24:08 -08006004 drm_vblank_post_modeset(dev, pipe);
6005
Daniel Vetter9256aa12012-10-31 19:26:13 +01006006 if (ret != 0)
6007 return ret;
6008
6009 for_each_encoder_on_crtc(dev, crtc, encoder) {
6010 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
6011 encoder->base.base.id,
6012 drm_get_encoder_name(&encoder->base),
6013 mode->base.id, mode->name);
Daniel Vetter6cc5f342013-03-27 00:44:53 +01006014 if (encoder->mode_set) {
6015 encoder->mode_set(encoder);
6016 } else {
6017 encoder_funcs = encoder->base.helper_private;
6018 encoder_funcs->mode_set(&encoder->base, mode, adjusted_mode);
6019 }
Daniel Vetter9256aa12012-10-31 19:26:13 +01006020 }
6021
6022 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006023}
6024
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006025static bool intel_eld_uptodate(struct drm_connector *connector,
6026 int reg_eldv, uint32_t bits_eldv,
6027 int reg_elda, uint32_t bits_elda,
6028 int reg_edid)
6029{
6030 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6031 uint8_t *eld = connector->eld;
6032 uint32_t i;
6033
6034 i = I915_READ(reg_eldv);
6035 i &= bits_eldv;
6036
6037 if (!eld[0])
6038 return !i;
6039
6040 if (!i)
6041 return false;
6042
6043 i = I915_READ(reg_elda);
6044 i &= ~bits_elda;
6045 I915_WRITE(reg_elda, i);
6046
6047 for (i = 0; i < eld[2]; i++)
6048 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
6049 return false;
6050
6051 return true;
6052}
6053
Wu Fengguange0dac652011-09-05 14:25:34 +08006054static void g4x_write_eld(struct drm_connector *connector,
6055 struct drm_crtc *crtc)
6056{
6057 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6058 uint8_t *eld = connector->eld;
6059 uint32_t eldv;
6060 uint32_t len;
6061 uint32_t i;
6062
6063 i = I915_READ(G4X_AUD_VID_DID);
6064
6065 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
6066 eldv = G4X_ELDV_DEVCL_DEVBLC;
6067 else
6068 eldv = G4X_ELDV_DEVCTG;
6069
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006070 if (intel_eld_uptodate(connector,
6071 G4X_AUD_CNTL_ST, eldv,
6072 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
6073 G4X_HDMIW_HDMIEDID))
6074 return;
6075
Wu Fengguange0dac652011-09-05 14:25:34 +08006076 i = I915_READ(G4X_AUD_CNTL_ST);
6077 i &= ~(eldv | G4X_ELD_ADDR);
6078 len = (i >> 9) & 0x1f; /* ELD buffer size */
6079 I915_WRITE(G4X_AUD_CNTL_ST, i);
6080
6081 if (!eld[0])
6082 return;
6083
6084 len = min_t(uint8_t, eld[2], len);
6085 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6086 for (i = 0; i < len; i++)
6087 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
6088
6089 i = I915_READ(G4X_AUD_CNTL_ST);
6090 i |= eldv;
6091 I915_WRITE(G4X_AUD_CNTL_ST, i);
6092}
6093
Wang Xingchao83358c852012-08-16 22:43:37 +08006094static void haswell_write_eld(struct drm_connector *connector,
6095 struct drm_crtc *crtc)
6096{
6097 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6098 uint8_t *eld = connector->eld;
6099 struct drm_device *dev = crtc->dev;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08006100 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Wang Xingchao83358c852012-08-16 22:43:37 +08006101 uint32_t eldv;
6102 uint32_t i;
6103 int len;
6104 int pipe = to_intel_crtc(crtc)->pipe;
6105 int tmp;
6106
6107 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
6108 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
6109 int aud_config = HSW_AUD_CFG(pipe);
6110 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
6111
6112
6113 DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
6114
6115 /* Audio output enable */
6116 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
6117 tmp = I915_READ(aud_cntrl_st2);
6118 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
6119 I915_WRITE(aud_cntrl_st2, tmp);
6120
6121 /* Wait for 1 vertical blank */
6122 intel_wait_for_vblank(dev, pipe);
6123
6124 /* Set ELD valid state */
6125 tmp = I915_READ(aud_cntrl_st2);
6126 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%8x\n", tmp);
6127 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
6128 I915_WRITE(aud_cntrl_st2, tmp);
6129 tmp = I915_READ(aud_cntrl_st2);
6130 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%8x\n", tmp);
6131
6132 /* Enable HDMI mode */
6133 tmp = I915_READ(aud_config);
6134 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%8x\n", tmp);
6135 /* clear N_programing_enable and N_value_index */
6136 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
6137 I915_WRITE(aud_config, tmp);
6138
6139 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
6140
6141 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08006142 intel_crtc->eld_vld = true;
Wang Xingchao83358c852012-08-16 22:43:37 +08006143
6144 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6145 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6146 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
6147 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6148 } else
6149 I915_WRITE(aud_config, 0);
6150
6151 if (intel_eld_uptodate(connector,
6152 aud_cntrl_st2, eldv,
6153 aud_cntl_st, IBX_ELD_ADDRESS,
6154 hdmiw_hdmiedid))
6155 return;
6156
6157 i = I915_READ(aud_cntrl_st2);
6158 i &= ~eldv;
6159 I915_WRITE(aud_cntrl_st2, i);
6160
6161 if (!eld[0])
6162 return;
6163
6164 i = I915_READ(aud_cntl_st);
6165 i &= ~IBX_ELD_ADDRESS;
6166 I915_WRITE(aud_cntl_st, i);
6167 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
6168 DRM_DEBUG_DRIVER("port num:%d\n", i);
6169
6170 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6171 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6172 for (i = 0; i < len; i++)
6173 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6174
6175 i = I915_READ(aud_cntrl_st2);
6176 i |= eldv;
6177 I915_WRITE(aud_cntrl_st2, i);
6178
6179}
6180
Wu Fengguange0dac652011-09-05 14:25:34 +08006181static void ironlake_write_eld(struct drm_connector *connector,
6182 struct drm_crtc *crtc)
6183{
6184 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6185 uint8_t *eld = connector->eld;
6186 uint32_t eldv;
6187 uint32_t i;
6188 int len;
6189 int hdmiw_hdmiedid;
Wu Fengguangb6daa022012-01-06 14:41:31 -06006190 int aud_config;
Wu Fengguange0dac652011-09-05 14:25:34 +08006191 int aud_cntl_st;
6192 int aud_cntrl_st2;
Wang Xingchao9b138a82012-08-09 16:52:18 +08006193 int pipe = to_intel_crtc(crtc)->pipe;
Wu Fengguange0dac652011-09-05 14:25:34 +08006194
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08006195 if (HAS_PCH_IBX(connector->dev)) {
Wang Xingchao9b138a82012-08-09 16:52:18 +08006196 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
6197 aud_config = IBX_AUD_CFG(pipe);
6198 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006199 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08006200 } else {
Wang Xingchao9b138a82012-08-09 16:52:18 +08006201 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
6202 aud_config = CPT_AUD_CFG(pipe);
6203 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006204 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08006205 }
6206
Wang Xingchao9b138a82012-08-09 16:52:18 +08006207 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
Wu Fengguange0dac652011-09-05 14:25:34 +08006208
6209 i = I915_READ(aud_cntl_st);
Wang Xingchao9b138a82012-08-09 16:52:18 +08006210 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
Wu Fengguange0dac652011-09-05 14:25:34 +08006211 if (!i) {
6212 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
6213 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006214 eldv = IBX_ELD_VALIDB;
6215 eldv |= IBX_ELD_VALIDB << 4;
6216 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08006217 } else {
Ville Syrjälä2582a852013-04-17 17:48:47 +03006218 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006219 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08006220 }
6221
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006222 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6223 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6224 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
Wu Fengguangb6daa022012-01-06 14:41:31 -06006225 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6226 } else
6227 I915_WRITE(aud_config, 0);
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006228
6229 if (intel_eld_uptodate(connector,
6230 aud_cntrl_st2, eldv,
6231 aud_cntl_st, IBX_ELD_ADDRESS,
6232 hdmiw_hdmiedid))
6233 return;
6234
Wu Fengguange0dac652011-09-05 14:25:34 +08006235 i = I915_READ(aud_cntrl_st2);
6236 i &= ~eldv;
6237 I915_WRITE(aud_cntrl_st2, i);
6238
6239 if (!eld[0])
6240 return;
6241
Wu Fengguange0dac652011-09-05 14:25:34 +08006242 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006243 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08006244 I915_WRITE(aud_cntl_st, i);
6245
6246 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6247 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6248 for (i = 0; i < len; i++)
6249 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6250
6251 i = I915_READ(aud_cntrl_st2);
6252 i |= eldv;
6253 I915_WRITE(aud_cntrl_st2, i);
6254}
6255
6256void intel_write_eld(struct drm_encoder *encoder,
6257 struct drm_display_mode *mode)
6258{
6259 struct drm_crtc *crtc = encoder->crtc;
6260 struct drm_connector *connector;
6261 struct drm_device *dev = encoder->dev;
6262 struct drm_i915_private *dev_priv = dev->dev_private;
6263
6264 connector = drm_select_eld(encoder, mode);
6265 if (!connector)
6266 return;
6267
6268 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6269 connector->base.id,
6270 drm_get_connector_name(connector),
6271 connector->encoder->base.id,
6272 drm_get_encoder_name(connector->encoder));
6273
6274 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
6275
6276 if (dev_priv->display.write_eld)
6277 dev_priv->display.write_eld(connector, crtc);
6278}
6279
Jesse Barnes79e53942008-11-07 14:24:08 -08006280/** Loads the palette/gamma unit for the CRTC with the prepared values */
6281void intel_crtc_load_lut(struct drm_crtc *crtc)
6282{
6283 struct drm_device *dev = crtc->dev;
6284 struct drm_i915_private *dev_priv = dev->dev_private;
6285 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006286 enum pipe pipe = intel_crtc->pipe;
6287 int palreg = PALETTE(pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08006288 int i;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006289 bool reenable_ips = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006290
6291 /* The clocks have to be on to load the palette. */
Alban Browaeysaed3f092012-02-24 17:12:45 +00006292 if (!crtc->enabled || !intel_crtc->active)
Jesse Barnes79e53942008-11-07 14:24:08 -08006293 return;
6294
Ville Syrjälä14420bd2013-06-04 13:49:07 +03006295 if (!HAS_PCH_SPLIT(dev_priv->dev))
6296 assert_pll_enabled(dev_priv, pipe);
6297
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006298 /* use legacy palette for Ironlake */
Eric Anholtbad720f2009-10-22 16:11:14 -07006299 if (HAS_PCH_SPLIT(dev))
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006300 palreg = LGC_PALETTE(pipe);
6301
6302 /* Workaround : Do not read or write the pipe palette/gamma data while
6303 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
6304 */
6305 if (intel_crtc->config.ips_enabled &&
6306 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
6307 GAMMA_MODE_MODE_SPLIT)) {
6308 hsw_disable_ips(intel_crtc);
6309 reenable_ips = true;
6310 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08006311
Jesse Barnes79e53942008-11-07 14:24:08 -08006312 for (i = 0; i < 256; i++) {
6313 I915_WRITE(palreg + 4 * i,
6314 (intel_crtc->lut_r[i] << 16) |
6315 (intel_crtc->lut_g[i] << 8) |
6316 intel_crtc->lut_b[i]);
6317 }
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006318
6319 if (reenable_ips)
6320 hsw_enable_ips(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006321}
6322
Chris Wilson560b85b2010-08-07 11:01:38 +01006323static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
6324{
6325 struct drm_device *dev = crtc->dev;
6326 struct drm_i915_private *dev_priv = dev->dev_private;
6327 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6328 bool visible = base != 0;
6329 u32 cntl;
6330
6331 if (intel_crtc->cursor_visible == visible)
6332 return;
6333
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006334 cntl = I915_READ(_CURACNTR);
Chris Wilson560b85b2010-08-07 11:01:38 +01006335 if (visible) {
6336 /* On these chipsets we can only modify the base whilst
6337 * the cursor is disabled.
6338 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006339 I915_WRITE(_CURABASE, base);
Chris Wilson560b85b2010-08-07 11:01:38 +01006340
6341 cntl &= ~(CURSOR_FORMAT_MASK);
6342 /* XXX width must be 64, stride 256 => 0x00 << 28 */
6343 cntl |= CURSOR_ENABLE |
6344 CURSOR_GAMMA_ENABLE |
6345 CURSOR_FORMAT_ARGB;
6346 } else
6347 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006348 I915_WRITE(_CURACNTR, cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01006349
6350 intel_crtc->cursor_visible = visible;
6351}
6352
6353static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
6354{
6355 struct drm_device *dev = crtc->dev;
6356 struct drm_i915_private *dev_priv = dev->dev_private;
6357 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6358 int pipe = intel_crtc->pipe;
6359 bool visible = base != 0;
6360
6361 if (intel_crtc->cursor_visible != visible) {
Jesse Barnes548f2452011-02-17 10:40:53 -08006362 uint32_t cntl = I915_READ(CURCNTR(pipe));
Chris Wilson560b85b2010-08-07 11:01:38 +01006363 if (base) {
6364 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
6365 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6366 cntl |= pipe << 28; /* Connect to correct pipe */
6367 } else {
6368 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6369 cntl |= CURSOR_MODE_DISABLE;
6370 }
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006371 I915_WRITE(CURCNTR(pipe), cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01006372
6373 intel_crtc->cursor_visible = visible;
6374 }
6375 /* and commit changes on next vblank */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006376 I915_WRITE(CURBASE(pipe), base);
Chris Wilson560b85b2010-08-07 11:01:38 +01006377}
6378
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006379static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
6380{
6381 struct drm_device *dev = crtc->dev;
6382 struct drm_i915_private *dev_priv = dev->dev_private;
6383 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6384 int pipe = intel_crtc->pipe;
6385 bool visible = base != 0;
6386
6387 if (intel_crtc->cursor_visible != visible) {
6388 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
6389 if (base) {
6390 cntl &= ~CURSOR_MODE;
6391 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6392 } else {
6393 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6394 cntl |= CURSOR_MODE_DISABLE;
6395 }
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006396 if (IS_HASWELL(dev))
6397 cntl |= CURSOR_PIPE_CSC_ENABLE;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006398 I915_WRITE(CURCNTR_IVB(pipe), cntl);
6399
6400 intel_crtc->cursor_visible = visible;
6401 }
6402 /* and commit changes on next vblank */
6403 I915_WRITE(CURBASE_IVB(pipe), base);
6404}
6405
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006406/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01006407static void intel_crtc_update_cursor(struct drm_crtc *crtc,
6408 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006409{
6410 struct drm_device *dev = crtc->dev;
6411 struct drm_i915_private *dev_priv = dev->dev_private;
6412 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6413 int pipe = intel_crtc->pipe;
6414 int x = intel_crtc->cursor_x;
6415 int y = intel_crtc->cursor_y;
Chris Wilson560b85b2010-08-07 11:01:38 +01006416 u32 base, pos;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006417 bool visible;
6418
6419 pos = 0;
6420
Chris Wilson6b383a72010-09-13 13:54:26 +01006421 if (on && crtc->enabled && crtc->fb) {
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006422 base = intel_crtc->cursor_addr;
6423 if (x > (int) crtc->fb->width)
6424 base = 0;
6425
6426 if (y > (int) crtc->fb->height)
6427 base = 0;
6428 } else
6429 base = 0;
6430
6431 if (x < 0) {
6432 if (x + intel_crtc->cursor_width < 0)
6433 base = 0;
6434
6435 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
6436 x = -x;
6437 }
6438 pos |= x << CURSOR_X_SHIFT;
6439
6440 if (y < 0) {
6441 if (y + intel_crtc->cursor_height < 0)
6442 base = 0;
6443
6444 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
6445 y = -y;
6446 }
6447 pos |= y << CURSOR_Y_SHIFT;
6448
6449 visible = base != 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01006450 if (!visible && !intel_crtc->cursor_visible)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006451 return;
6452
Eugeni Dodonov0cd83aa2012-04-13 17:08:48 -03006453 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006454 I915_WRITE(CURPOS_IVB(pipe), pos);
6455 ivb_update_cursor(crtc, base);
6456 } else {
6457 I915_WRITE(CURPOS(pipe), pos);
6458 if (IS_845G(dev) || IS_I865G(dev))
6459 i845_update_cursor(crtc, base);
6460 else
6461 i9xx_update_cursor(crtc, base);
6462 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006463}
6464
Jesse Barnes79e53942008-11-07 14:24:08 -08006465static int intel_crtc_cursor_set(struct drm_crtc *crtc,
Chris Wilson05394f32010-11-08 19:18:58 +00006466 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08006467 uint32_t handle,
6468 uint32_t width, uint32_t height)
6469{
6470 struct drm_device *dev = crtc->dev;
6471 struct drm_i915_private *dev_priv = dev->dev_private;
6472 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00006473 struct drm_i915_gem_object *obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006474 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006475 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006476
Jesse Barnes79e53942008-11-07 14:24:08 -08006477 /* if we want to turn off the cursor ignore width and height */
6478 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08006479 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006480 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00006481 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10006482 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006483 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08006484 }
6485
6486 /* Currently we only support 64x64 cursors */
6487 if (width != 64 || height != 64) {
6488 DRM_ERROR("we currently only support 64x64 cursors\n");
6489 return -EINVAL;
6490 }
6491
Chris Wilson05394f32010-11-08 19:18:58 +00006492 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00006493 if (&obj->base == NULL)
Jesse Barnes79e53942008-11-07 14:24:08 -08006494 return -ENOENT;
6495
Chris Wilson05394f32010-11-08 19:18:58 +00006496 if (obj->base.size < width * height * 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006497 DRM_ERROR("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10006498 ret = -ENOMEM;
6499 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08006500 }
6501
Dave Airlie71acb5e2008-12-30 20:31:46 +10006502 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006503 mutex_lock(&dev->struct_mutex);
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05006504 if (!dev_priv->info->cursor_needs_physical) {
Chris Wilson693db182013-03-05 14:52:39 +00006505 unsigned alignment;
6506
Chris Wilsond9e86c02010-11-10 16:40:20 +00006507 if (obj->tiling_mode) {
6508 DRM_ERROR("cursor cannot be tiled\n");
6509 ret = -EINVAL;
6510 goto fail_locked;
6511 }
6512
Chris Wilson693db182013-03-05 14:52:39 +00006513 /* Note that the w/a also requires 2 PTE of padding following
6514 * the bo. We currently fill all unused PTE with the shadow
6515 * page and so we should always have valid PTE following the
6516 * cursor preventing the VT-d warning.
6517 */
6518 alignment = 0;
6519 if (need_vtd_wa(dev))
6520 alignment = 64*1024;
6521
6522 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01006523 if (ret) {
6524 DRM_ERROR("failed to move cursor bo into the GTT\n");
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006525 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006526 }
6527
Chris Wilsond9e86c02010-11-10 16:40:20 +00006528 ret = i915_gem_object_put_fence(obj);
6529 if (ret) {
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006530 DRM_ERROR("failed to release fence for cursor");
Chris Wilsond9e86c02010-11-10 16:40:20 +00006531 goto fail_unpin;
6532 }
6533
Chris Wilson05394f32010-11-08 19:18:58 +00006534 addr = obj->gtt_offset;
Dave Airlie71acb5e2008-12-30 20:31:46 +10006535 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01006536 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson05394f32010-11-08 19:18:58 +00006537 ret = i915_gem_attach_phys_object(dev, obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01006538 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
6539 align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10006540 if (ret) {
6541 DRM_ERROR("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006542 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10006543 }
Chris Wilson05394f32010-11-08 19:18:58 +00006544 addr = obj->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006545 }
6546
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006547 if (IS_GEN2(dev))
Jesse Barnes14b60392009-05-20 16:47:08 -04006548 I915_WRITE(CURSIZE, (height << 12) | width);
6549
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006550 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006551 if (intel_crtc->cursor_bo) {
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05006552 if (dev_priv->info->cursor_needs_physical) {
Chris Wilson05394f32010-11-08 19:18:58 +00006553 if (intel_crtc->cursor_bo != obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10006554 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
6555 } else
6556 i915_gem_object_unpin(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00006557 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006558 }
Jesse Barnes80824002009-09-10 15:28:06 -07006559
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006560 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006561
6562 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00006563 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006564 intel_crtc->cursor_width = width;
6565 intel_crtc->cursor_height = height;
6566
Mika Kuoppala40ccc722013-04-23 17:27:08 +03006567 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006568
Jesse Barnes79e53942008-11-07 14:24:08 -08006569 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006570fail_unpin:
Chris Wilson05394f32010-11-08 19:18:58 +00006571 i915_gem_object_unpin(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006572fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10006573 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00006574fail:
Chris Wilson05394f32010-11-08 19:18:58 +00006575 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10006576 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006577}
6578
6579static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
6580{
Jesse Barnes79e53942008-11-07 14:24:08 -08006581 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006582
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006583 intel_crtc->cursor_x = x;
6584 intel_crtc->cursor_y = y;
Jesse Barnes652c3932009-08-17 13:31:43 -07006585
Mika Kuoppala40ccc722013-04-23 17:27:08 +03006586 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08006587
6588 return 0;
6589}
6590
6591/** Sets the color ramps on behalf of RandR */
6592void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
6593 u16 blue, int regno)
6594{
6595 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6596
6597 intel_crtc->lut_r[regno] = red >> 8;
6598 intel_crtc->lut_g[regno] = green >> 8;
6599 intel_crtc->lut_b[regno] = blue >> 8;
6600}
6601
Dave Airlieb8c00ac2009-10-06 13:54:01 +10006602void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
6603 u16 *blue, int regno)
6604{
6605 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6606
6607 *red = intel_crtc->lut_r[regno] << 8;
6608 *green = intel_crtc->lut_g[regno] << 8;
6609 *blue = intel_crtc->lut_b[regno] << 8;
6610}
6611
Jesse Barnes79e53942008-11-07 14:24:08 -08006612static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01006613 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08006614{
James Simmons72034252010-08-03 01:33:19 +01006615 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08006616 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006617
James Simmons72034252010-08-03 01:33:19 +01006618 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006619 intel_crtc->lut_r[i] = red[i] >> 8;
6620 intel_crtc->lut_g[i] = green[i] >> 8;
6621 intel_crtc->lut_b[i] = blue[i] >> 8;
6622 }
6623
6624 intel_crtc_load_lut(crtc);
6625}
6626
Jesse Barnes79e53942008-11-07 14:24:08 -08006627/* VESA 640x480x72Hz mode to set on the pipe */
6628static struct drm_display_mode load_detect_mode = {
6629 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
6630 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
6631};
6632
Chris Wilsond2dff872011-04-19 08:36:26 +01006633static struct drm_framebuffer *
6634intel_framebuffer_create(struct drm_device *dev,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006635 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilsond2dff872011-04-19 08:36:26 +01006636 struct drm_i915_gem_object *obj)
6637{
6638 struct intel_framebuffer *intel_fb;
6639 int ret;
6640
6641 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
6642 if (!intel_fb) {
6643 drm_gem_object_unreference_unlocked(&obj->base);
6644 return ERR_PTR(-ENOMEM);
6645 }
6646
6647 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
6648 if (ret) {
6649 drm_gem_object_unreference_unlocked(&obj->base);
6650 kfree(intel_fb);
6651 return ERR_PTR(ret);
6652 }
6653
6654 return &intel_fb->base;
6655}
6656
6657static u32
6658intel_framebuffer_pitch_for_width(int width, int bpp)
6659{
6660 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
6661 return ALIGN(pitch, 64);
6662}
6663
6664static u32
6665intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
6666{
6667 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
6668 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
6669}
6670
6671static struct drm_framebuffer *
6672intel_framebuffer_create_for_mode(struct drm_device *dev,
6673 struct drm_display_mode *mode,
6674 int depth, int bpp)
6675{
6676 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00006677 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01006678
6679 obj = i915_gem_alloc_object(dev,
6680 intel_framebuffer_size_for_mode(mode, bpp));
6681 if (obj == NULL)
6682 return ERR_PTR(-ENOMEM);
6683
6684 mode_cmd.width = mode->hdisplay;
6685 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006686 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
6687 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00006688 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01006689
6690 return intel_framebuffer_create(dev, &mode_cmd, obj);
6691}
6692
6693static struct drm_framebuffer *
6694mode_fits_in_fbdev(struct drm_device *dev,
6695 struct drm_display_mode *mode)
6696{
6697 struct drm_i915_private *dev_priv = dev->dev_private;
6698 struct drm_i915_gem_object *obj;
6699 struct drm_framebuffer *fb;
6700
6701 if (dev_priv->fbdev == NULL)
6702 return NULL;
6703
6704 obj = dev_priv->fbdev->ifb.obj;
6705 if (obj == NULL)
6706 return NULL;
6707
6708 fb = &dev_priv->fbdev->ifb.base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006709 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
6710 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01006711 return NULL;
6712
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006713 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01006714 return NULL;
6715
6716 return fb;
6717}
6718
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006719bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01006720 struct drm_display_mode *mode,
Chris Wilson8261b192011-04-19 23:18:09 +01006721 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08006722{
6723 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006724 struct intel_encoder *intel_encoder =
6725 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08006726 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01006727 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08006728 struct drm_crtc *crtc = NULL;
6729 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02006730 struct drm_framebuffer *fb;
Jesse Barnes79e53942008-11-07 14:24:08 -08006731 int i = -1;
6732
Chris Wilsond2dff872011-04-19 08:36:26 +01006733 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6734 connector->base.id, drm_get_connector_name(connector),
6735 encoder->base.id, drm_get_encoder_name(encoder));
6736
Jesse Barnes79e53942008-11-07 14:24:08 -08006737 /*
6738 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01006739 *
Jesse Barnes79e53942008-11-07 14:24:08 -08006740 * - if the connector already has an assigned crtc, use it (but make
6741 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01006742 *
Jesse Barnes79e53942008-11-07 14:24:08 -08006743 * - try to find the first unused crtc that can drive this connector,
6744 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08006745 */
6746
6747 /* See if we already have a CRTC for this connector */
6748 if (encoder->crtc) {
6749 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01006750
Daniel Vetter7b240562012-12-12 00:35:33 +01006751 mutex_lock(&crtc->mutex);
6752
Daniel Vetter24218aa2012-08-12 19:27:11 +02006753 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01006754 old->load_detect_temp = false;
6755
6756 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02006757 if (connector->dpms != DRM_MODE_DPMS_ON)
6758 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01006759
Chris Wilson71731882011-04-19 23:10:58 +01006760 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08006761 }
6762
6763 /* Find an unused one (if possible) */
6764 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
6765 i++;
6766 if (!(encoder->possible_crtcs & (1 << i)))
6767 continue;
6768 if (!possible_crtc->enabled) {
6769 crtc = possible_crtc;
6770 break;
6771 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006772 }
6773
6774 /*
6775 * If we didn't find an unused CRTC, don't use any.
6776 */
6777 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01006778 DRM_DEBUG_KMS("no pipe available for load-detect\n");
6779 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006780 }
6781
Daniel Vetter7b240562012-12-12 00:35:33 +01006782 mutex_lock(&crtc->mutex);
Daniel Vetterfc303102012-07-09 10:40:58 +02006783 intel_encoder->new_crtc = to_intel_crtc(crtc);
6784 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006785
6786 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter24218aa2012-08-12 19:27:11 +02006787 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01006788 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01006789 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08006790
Chris Wilson64927112011-04-20 07:25:26 +01006791 if (!mode)
6792 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08006793
Chris Wilsond2dff872011-04-19 08:36:26 +01006794 /* We need a framebuffer large enough to accommodate all accesses
6795 * that the plane may generate whilst we perform load detection.
6796 * We can not rely on the fbcon either being present (we get called
6797 * during its initialisation to detect all boot displays, or it may
6798 * not even exist) or that it is large enough to satisfy the
6799 * requested mode.
6800 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02006801 fb = mode_fits_in_fbdev(dev, mode);
6802 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01006803 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02006804 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
6805 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01006806 } else
6807 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02006808 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01006809 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Daniel Vetter7b240562012-12-12 00:35:33 +01006810 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00006811 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006812 }
Chris Wilsond2dff872011-04-19 08:36:26 +01006813
Chris Wilsonc0c36b942012-12-19 16:08:43 +00006814 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01006815 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01006816 if (old->release_fb)
6817 old->release_fb->funcs->destroy(old->release_fb);
Daniel Vetter7b240562012-12-12 00:35:33 +01006818 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00006819 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006820 }
Chris Wilson71731882011-04-19 23:10:58 +01006821
Jesse Barnes79e53942008-11-07 14:24:08 -08006822 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006823 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01006824 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08006825}
6826
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006827void intel_release_load_detect_pipe(struct drm_connector *connector,
Chris Wilson8261b192011-04-19 23:18:09 +01006828 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08006829{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006830 struct intel_encoder *intel_encoder =
6831 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01006832 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01006833 struct drm_crtc *crtc = encoder->crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -08006834
Chris Wilsond2dff872011-04-19 08:36:26 +01006835 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6836 connector->base.id, drm_get_connector_name(connector),
6837 encoder->base.id, drm_get_encoder_name(encoder));
6838
Chris Wilson8261b192011-04-19 23:18:09 +01006839 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02006840 to_intel_connector(connector)->new_encoder = NULL;
6841 intel_encoder->new_crtc = NULL;
6842 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01006843
Daniel Vetter36206362012-12-10 20:42:17 +01006844 if (old->release_fb) {
6845 drm_framebuffer_unregister_private(old->release_fb);
6846 drm_framebuffer_unreference(old->release_fb);
6847 }
Chris Wilsond2dff872011-04-19 08:36:26 +01006848
Daniel Vetter67c96402013-01-23 16:25:09 +00006849 mutex_unlock(&crtc->mutex);
Chris Wilson0622a532011-04-21 09:32:11 +01006850 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08006851 }
6852
Eric Anholtc751ce42010-03-25 11:48:48 -07006853 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02006854 if (old->dpms_mode != DRM_MODE_DPMS_ON)
6855 connector->funcs->dpms(connector, old->dpms_mode);
Daniel Vetter7b240562012-12-12 00:35:33 +01006856
6857 mutex_unlock(&crtc->mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08006858}
6859
6860/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03006861static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
6862 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006863{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03006864 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08006865 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03006866 int pipe = pipe_config->cpu_transcoder;
Jesse Barnes548f2452011-02-17 10:40:53 -08006867 u32 dpll = I915_READ(DPLL(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006868 u32 fp;
6869 intel_clock_t clock;
6870
6871 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Chris Wilson39adb7a2011-04-22 22:17:21 +01006872 fp = I915_READ(FP0(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006873 else
Chris Wilson39adb7a2011-04-22 22:17:21 +01006874 fp = I915_READ(FP1(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006875
6876 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006877 if (IS_PINEVIEW(dev)) {
6878 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
6879 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08006880 } else {
6881 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
6882 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
6883 }
6884
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006885 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006886 if (IS_PINEVIEW(dev))
6887 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
6888 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08006889 else
6890 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08006891 DPLL_FPA01_P1_POST_DIV_SHIFT);
6892
6893 switch (dpll & DPLL_MODE_MASK) {
6894 case DPLLB_MODE_DAC_SERIAL:
6895 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
6896 5 : 10;
6897 break;
6898 case DPLLB_MODE_LVDS:
6899 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
6900 7 : 14;
6901 break;
6902 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08006903 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08006904 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03006905 pipe_config->adjusted_mode.clock = 0;
6906 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08006907 }
6908
Daniel Vetterac58c3f2013-06-01 17:16:17 +02006909 if (IS_PINEVIEW(dev))
6910 pineview_clock(96000, &clock);
6911 else
6912 i9xx_clock(96000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006913 } else {
6914 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
6915
6916 if (is_lvds) {
6917 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
6918 DPLL_FPA01_P1_POST_DIV_SHIFT);
6919 clock.p2 = 14;
6920
6921 if ((dpll & PLL_REF_INPUT_MASK) ==
6922 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
6923 /* XXX: might not be 66MHz */
Daniel Vetterac58c3f2013-06-01 17:16:17 +02006924 i9xx_clock(66000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006925 } else
Daniel Vetterac58c3f2013-06-01 17:16:17 +02006926 i9xx_clock(48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006927 } else {
6928 if (dpll & PLL_P1_DIVIDE_BY_TWO)
6929 clock.p1 = 2;
6930 else {
6931 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
6932 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
6933 }
6934 if (dpll & PLL_P2_DIVIDE_BY_4)
6935 clock.p2 = 4;
6936 else
6937 clock.p2 = 2;
6938
Daniel Vetterac58c3f2013-06-01 17:16:17 +02006939 i9xx_clock(48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006940 }
6941 }
6942
Jesse Barnesf1f644d2013-06-27 00:39:25 +03006943 pipe_config->adjusted_mode.clock = clock.dot *
6944 pipe_config->pixel_multiplier;
6945}
6946
6947static void ironlake_crtc_clock_get(struct intel_crtc *crtc,
6948 struct intel_crtc_config *pipe_config)
6949{
6950 struct drm_device *dev = crtc->base.dev;
6951 struct drm_i915_private *dev_priv = dev->dev_private;
6952 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
6953 int link_freq, repeat;
6954 u64 clock;
6955 u32 link_m, link_n;
6956
6957 repeat = pipe_config->pixel_multiplier;
6958
6959 /*
6960 * The calculation for the data clock is:
6961 * pixel_clock = ((m/n)*(link_clock * nr_lanes * repeat))/bpp
6962 * But we want to avoid losing precison if possible, so:
6963 * pixel_clock = ((m * link_clock * nr_lanes * repeat)/(n*bpp))
6964 *
6965 * and the link clock is simpler:
6966 * link_clock = (m * link_clock * repeat) / n
Jesse Barnes79e53942008-11-07 14:24:08 -08006967 */
6968
Jesse Barnesf1f644d2013-06-27 00:39:25 +03006969 /*
6970 * We need to get the FDI or DP link clock here to derive
6971 * the M/N dividers.
6972 *
6973 * For FDI, we read it from the BIOS or use a fixed 2.7GHz.
6974 * For DP, it's either 1.62GHz or 2.7GHz.
6975 * We do our calculations in 10*MHz since we don't need much precison.
6976 */
6977 if (pipe_config->has_pch_encoder)
6978 link_freq = intel_fdi_link_freq(dev) * 10000;
6979 else
6980 link_freq = pipe_config->port_clock;
6981
6982 link_m = I915_READ(PIPE_LINK_M1(cpu_transcoder));
6983 link_n = I915_READ(PIPE_LINK_N1(cpu_transcoder));
6984
6985 if (!link_m || !link_n)
6986 return;
6987
6988 clock = ((u64)link_m * (u64)link_freq * (u64)repeat);
6989 do_div(clock, link_n);
6990
6991 pipe_config->adjusted_mode.clock = clock;
Jesse Barnes79e53942008-11-07 14:24:08 -08006992}
6993
6994/** Returns the currently programmed mode of the given pipe. */
6995struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
6996 struct drm_crtc *crtc)
6997{
Jesse Barnes548f2452011-02-17 10:40:53 -08006998 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08006999 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02007000 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08007001 struct drm_display_mode *mode;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007002 struct intel_crtc_config pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007003 int htot = I915_READ(HTOTAL(cpu_transcoder));
7004 int hsync = I915_READ(HSYNC(cpu_transcoder));
7005 int vtot = I915_READ(VTOTAL(cpu_transcoder));
7006 int vsync = I915_READ(VSYNC(cpu_transcoder));
Jesse Barnes79e53942008-11-07 14:24:08 -08007007
7008 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
7009 if (!mode)
7010 return NULL;
7011
Jesse Barnesf1f644d2013-06-27 00:39:25 +03007012 /*
7013 * Construct a pipe_config sufficient for getting the clock info
7014 * back out of crtc_clock_get.
7015 *
7016 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
7017 * to use a real value here instead.
7018 */
7019 pipe_config.cpu_transcoder = intel_crtc->pipe;
7020 pipe_config.pixel_multiplier = 1;
7021 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
7022
7023 mode->clock = pipe_config.adjusted_mode.clock;
Jesse Barnes79e53942008-11-07 14:24:08 -08007024 mode->hdisplay = (htot & 0xffff) + 1;
7025 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
7026 mode->hsync_start = (hsync & 0xffff) + 1;
7027 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
7028 mode->vdisplay = (vtot & 0xffff) + 1;
7029 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
7030 mode->vsync_start = (vsync & 0xffff) + 1;
7031 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
7032
7033 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08007034
7035 return mode;
7036}
7037
Daniel Vetter3dec0092010-08-20 21:40:52 +02007038static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07007039{
7040 struct drm_device *dev = crtc->dev;
7041 drm_i915_private_t *dev_priv = dev->dev_private;
7042 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7043 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007044 int dpll_reg = DPLL(pipe);
7045 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07007046
Eric Anholtbad720f2009-10-22 16:11:14 -07007047 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07007048 return;
7049
7050 if (!dev_priv->lvds_downclock_avail)
7051 return;
7052
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007053 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07007054 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08007055 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007056
Sean Paul8ac5a6d2012-02-13 13:14:51 -05007057 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007058
7059 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
7060 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007061 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007062
Jesse Barnes652c3932009-08-17 13:31:43 -07007063 dpll = I915_READ(dpll_reg);
7064 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08007065 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007066 }
Jesse Barnes652c3932009-08-17 13:31:43 -07007067}
7068
7069static void intel_decrease_pllclock(struct drm_crtc *crtc)
7070{
7071 struct drm_device *dev = crtc->dev;
7072 drm_i915_private_t *dev_priv = dev->dev_private;
7073 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07007074
Eric Anholtbad720f2009-10-22 16:11:14 -07007075 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07007076 return;
7077
7078 if (!dev_priv->lvds_downclock_avail)
7079 return;
7080
7081 /*
7082 * Since this is called by a timer, we should never get here in
7083 * the manual case.
7084 */
7085 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01007086 int pipe = intel_crtc->pipe;
7087 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02007088 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01007089
Zhao Yakui44d98a62009-10-09 11:39:40 +08007090 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007091
Sean Paul8ac5a6d2012-02-13 13:14:51 -05007092 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007093
Chris Wilson074b5e12012-05-02 12:07:06 +01007094 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07007095 dpll |= DISPLAY_RATE_SELECT_FPA1;
7096 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007097 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007098 dpll = I915_READ(dpll_reg);
7099 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08007100 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007101 }
7102
7103}
7104
Chris Wilsonf047e392012-07-21 12:31:41 +01007105void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07007106{
Chris Wilsonf047e392012-07-21 12:31:41 +01007107 i915_update_gfx_val(dev->dev_private);
7108}
7109
7110void intel_mark_idle(struct drm_device *dev)
7111{
Chris Wilson725a5b52013-01-08 11:02:57 +00007112 struct drm_crtc *crtc;
7113
7114 if (!i915_powersave)
7115 return;
7116
7117 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
7118 if (!crtc->fb)
7119 continue;
7120
7121 intel_decrease_pllclock(crtc);
7122 }
Chris Wilsonf047e392012-07-21 12:31:41 +01007123}
7124
Chris Wilsonc65355b2013-06-06 16:53:41 -03007125void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
7126 struct intel_ring_buffer *ring)
Chris Wilsonf047e392012-07-21 12:31:41 +01007127{
7128 struct drm_device *dev = obj->base.dev;
Jesse Barnes652c3932009-08-17 13:31:43 -07007129 struct drm_crtc *crtc;
Jesse Barnes652c3932009-08-17 13:31:43 -07007130
7131 if (!i915_powersave)
7132 return;
7133
Jesse Barnes652c3932009-08-17 13:31:43 -07007134 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Jesse Barnes652c3932009-08-17 13:31:43 -07007135 if (!crtc->fb)
7136 continue;
7137
Chris Wilsonc65355b2013-06-06 16:53:41 -03007138 if (to_intel_framebuffer(crtc->fb)->obj != obj)
7139 continue;
7140
7141 intel_increase_pllclock(crtc);
7142 if (ring && intel_fbc_enabled(dev))
7143 ring->fbc_dirty = true;
Jesse Barnes652c3932009-08-17 13:31:43 -07007144 }
Jesse Barnes652c3932009-08-17 13:31:43 -07007145}
7146
Jesse Barnes79e53942008-11-07 14:24:08 -08007147static void intel_crtc_destroy(struct drm_crtc *crtc)
7148{
7149 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02007150 struct drm_device *dev = crtc->dev;
7151 struct intel_unpin_work *work;
7152 unsigned long flags;
7153
7154 spin_lock_irqsave(&dev->event_lock, flags);
7155 work = intel_crtc->unpin_work;
7156 intel_crtc->unpin_work = NULL;
7157 spin_unlock_irqrestore(&dev->event_lock, flags);
7158
7159 if (work) {
7160 cancel_work_sync(&work->work);
7161 kfree(work);
7162 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007163
Mika Kuoppala40ccc722013-04-23 17:27:08 +03007164 intel_crtc_cursor_set(crtc, NULL, 0, 0, 0);
7165
Jesse Barnes79e53942008-11-07 14:24:08 -08007166 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02007167
Jesse Barnes79e53942008-11-07 14:24:08 -08007168 kfree(intel_crtc);
7169}
7170
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007171static void intel_unpin_work_fn(struct work_struct *__work)
7172{
7173 struct intel_unpin_work *work =
7174 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007175 struct drm_device *dev = work->crtc->dev;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007176
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007177 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01007178 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00007179 drm_gem_object_unreference(&work->pending_flip_obj->base);
7180 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00007181
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007182 intel_update_fbc(dev);
7183 mutex_unlock(&dev->struct_mutex);
7184
7185 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
7186 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
7187
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007188 kfree(work);
7189}
7190
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007191static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01007192 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007193{
7194 drm_i915_private_t *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007195 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7196 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007197 unsigned long flags;
7198
7199 /* Ignore early vblank irqs */
7200 if (intel_crtc == NULL)
7201 return;
7202
7203 spin_lock_irqsave(&dev->event_lock, flags);
7204 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00007205
7206 /* Ensure we don't miss a work->pending update ... */
7207 smp_rmb();
7208
7209 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007210 spin_unlock_irqrestore(&dev->event_lock, flags);
7211 return;
7212 }
7213
Chris Wilsone7d841c2012-12-03 11:36:30 +00007214 /* and that the unpin work is consistent wrt ->pending. */
7215 smp_rmb();
7216
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007217 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007218
Rob Clark45a066e2012-10-08 14:50:40 -05007219 if (work->event)
7220 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007221
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01007222 drm_vblank_put(dev, intel_crtc->pipe);
7223
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007224 spin_unlock_irqrestore(&dev->event_lock, flags);
7225
Daniel Vetter2c10d572012-12-20 21:24:07 +01007226 wake_up_all(&dev_priv->pending_flip_queue);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007227
7228 queue_work(dev_priv->wq, &work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07007229
7230 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007231}
7232
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007233void intel_finish_page_flip(struct drm_device *dev, int pipe)
7234{
7235 drm_i915_private_t *dev_priv = dev->dev_private;
7236 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
7237
Mario Kleiner49b14a52010-12-09 07:00:07 +01007238 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007239}
7240
7241void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
7242{
7243 drm_i915_private_t *dev_priv = dev->dev_private;
7244 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
7245
Mario Kleiner49b14a52010-12-09 07:00:07 +01007246 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007247}
7248
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007249void intel_prepare_page_flip(struct drm_device *dev, int plane)
7250{
7251 drm_i915_private_t *dev_priv = dev->dev_private;
7252 struct intel_crtc *intel_crtc =
7253 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
7254 unsigned long flags;
7255
Chris Wilsone7d841c2012-12-03 11:36:30 +00007256 /* NB: An MMIO update of the plane base pointer will also
7257 * generate a page-flip completion irq, i.e. every modeset
7258 * is also accompanied by a spurious intel_prepare_page_flip().
7259 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007260 spin_lock_irqsave(&dev->event_lock, flags);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007261 if (intel_crtc->unpin_work)
7262 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007263 spin_unlock_irqrestore(&dev->event_lock, flags);
7264}
7265
Chris Wilsone7d841c2012-12-03 11:36:30 +00007266inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
7267{
7268 /* Ensure that the work item is consistent when activating it ... */
7269 smp_wmb();
7270 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
7271 /* and that it is marked active as soon as the irq could fire. */
7272 smp_wmb();
7273}
7274
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007275static int intel_gen2_queue_flip(struct drm_device *dev,
7276 struct drm_crtc *crtc,
7277 struct drm_framebuffer *fb,
7278 struct drm_i915_gem_object *obj)
7279{
7280 struct drm_i915_private *dev_priv = dev->dev_private;
7281 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007282 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007283 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007284 int ret;
7285
Daniel Vetter6d90c952012-04-26 23:28:05 +02007286 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007287 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007288 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007289
Daniel Vetter6d90c952012-04-26 23:28:05 +02007290 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007291 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007292 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007293
7294 /* Can't queue multiple flips, so wait for the previous
7295 * one to finish before executing the next.
7296 */
7297 if (intel_crtc->plane)
7298 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7299 else
7300 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007301 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7302 intel_ring_emit(ring, MI_NOOP);
7303 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7304 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7305 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vettere506a0c2012-07-05 12:17:29 +02007306 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007307 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00007308
7309 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007310 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007311 return 0;
7312
7313err_unpin:
7314 intel_unpin_fb_obj(obj);
7315err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007316 return ret;
7317}
7318
7319static int intel_gen3_queue_flip(struct drm_device *dev,
7320 struct drm_crtc *crtc,
7321 struct drm_framebuffer *fb,
7322 struct drm_i915_gem_object *obj)
7323{
7324 struct drm_i915_private *dev_priv = dev->dev_private;
7325 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007326 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007327 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007328 int ret;
7329
Daniel Vetter6d90c952012-04-26 23:28:05 +02007330 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007331 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007332 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007333
Daniel Vetter6d90c952012-04-26 23:28:05 +02007334 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007335 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007336 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007337
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_I915 |
7345 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7346 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vettere506a0c2012-07-05 12:17:29 +02007347 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007348 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007349
Chris Wilsone7d841c2012-12-03 11:36:30 +00007350 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_gen4_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);
7367 uint32_t pf, pipesrc;
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, 4);
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 /* i965+ uses the linear or tiled offsets from the
7380 * Display Registers (which do not change across a page-flip)
7381 * so we need only reprogram the base address.
7382 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02007383 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7384 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7385 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02007386 intel_ring_emit(ring,
7387 (obj->gtt_offset + intel_crtc->dspaddr_offset) |
7388 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007389
7390 /* XXX Enabling the panel-fitter across page-flip is so far
7391 * untested on non-native modes, so ignore it for now.
7392 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
7393 */
7394 pf = 0;
7395 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007396 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007397
7398 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007399 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007400 return 0;
7401
7402err_unpin:
7403 intel_unpin_fb_obj(obj);
7404err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007405 return ret;
7406}
7407
7408static int intel_gen6_queue_flip(struct drm_device *dev,
7409 struct drm_crtc *crtc,
7410 struct drm_framebuffer *fb,
7411 struct drm_i915_gem_object *obj)
7412{
7413 struct drm_i915_private *dev_priv = dev->dev_private;
7414 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007415 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007416 uint32_t pf, pipesrc;
7417 int ret;
7418
Daniel Vetter6d90c952012-04-26 23:28:05 +02007419 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007420 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007421 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007422
Daniel Vetter6d90c952012-04-26 23:28:05 +02007423 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007424 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007425 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007426
Daniel Vetter6d90c952012-04-26 23:28:05 +02007427 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7428 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7429 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Daniel Vetterc2c75132012-07-05 12:17:30 +02007430 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007431
Chris Wilson99d9acd2012-04-17 20:37:00 +01007432 /* Contrary to the suggestions in the documentation,
7433 * "Enable Panel Fitter" does not seem to be required when page
7434 * flipping with a non-native mode, and worse causes a normal
7435 * modeset to fail.
7436 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
7437 */
7438 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007439 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007440 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007441
7442 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007443 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007444 return 0;
7445
7446err_unpin:
7447 intel_unpin_fb_obj(obj);
7448err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007449 return ret;
7450}
7451
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007452/*
7453 * On gen7 we currently use the blit ring because (in early silicon at least)
7454 * the render ring doesn't give us interrpts for page flip completion, which
7455 * means clients will hang after the first flip is queued. Fortunately the
7456 * blit ring generates interrupts properly, so use it instead.
7457 */
7458static int intel_gen7_queue_flip(struct drm_device *dev,
7459 struct drm_crtc *crtc,
7460 struct drm_framebuffer *fb,
7461 struct drm_i915_gem_object *obj)
7462{
7463 struct drm_i915_private *dev_priv = dev->dev_private;
7464 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7465 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007466 uint32_t plane_bit = 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007467 int ret;
7468
7469 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7470 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007471 goto err;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007472
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007473 switch(intel_crtc->plane) {
7474 case PLANE_A:
7475 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
7476 break;
7477 case PLANE_B:
7478 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
7479 break;
7480 case PLANE_C:
7481 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
7482 break;
7483 default:
7484 WARN_ONCE(1, "unknown plane in flip command\n");
7485 ret = -ENODEV;
Eugeni Dodonovab3951e2012-06-18 19:03:38 -03007486 goto err_unpin;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007487 }
7488
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007489 ret = intel_ring_begin(ring, 4);
7490 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007491 goto err_unpin;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007492
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007493 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007494 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Daniel Vetterc2c75132012-07-05 12:17:30 +02007495 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007496 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +00007497
7498 intel_mark_page_flip_active(intel_crtc);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007499 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007500 return 0;
7501
7502err_unpin:
7503 intel_unpin_fb_obj(obj);
7504err:
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007505 return ret;
7506}
7507
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007508static int intel_default_queue_flip(struct drm_device *dev,
7509 struct drm_crtc *crtc,
7510 struct drm_framebuffer *fb,
7511 struct drm_i915_gem_object *obj)
7512{
7513 return -ENODEV;
7514}
7515
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007516static int intel_crtc_page_flip(struct drm_crtc *crtc,
7517 struct drm_framebuffer *fb,
7518 struct drm_pending_vblank_event *event)
7519{
7520 struct drm_device *dev = crtc->dev;
7521 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02007522 struct drm_framebuffer *old_fb = crtc->fb;
7523 struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007524 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7525 struct intel_unpin_work *work;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007526 unsigned long flags;
Chris Wilson52e68632010-08-08 10:15:59 +01007527 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007528
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03007529 /* Can't change pixel format via MI display flips. */
7530 if (fb->pixel_format != crtc->fb->pixel_format)
7531 return -EINVAL;
7532
7533 /*
7534 * TILEOFF/LINOFF registers can't be changed via MI display flips.
7535 * Note that pitch changes could also affect these register.
7536 */
7537 if (INTEL_INFO(dev)->gen > 3 &&
7538 (fb->offsets[0] != crtc->fb->offsets[0] ||
7539 fb->pitches[0] != crtc->fb->pitches[0]))
7540 return -EINVAL;
7541
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007542 work = kzalloc(sizeof *work, GFP_KERNEL);
7543 if (work == NULL)
7544 return -ENOMEM;
7545
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007546 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007547 work->crtc = crtc;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02007548 work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007549 INIT_WORK(&work->work, intel_unpin_work_fn);
7550
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007551 ret = drm_vblank_get(dev, intel_crtc->pipe);
7552 if (ret)
7553 goto free_work;
7554
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007555 /* We borrow the event spin lock for protecting unpin_work */
7556 spin_lock_irqsave(&dev->event_lock, flags);
7557 if (intel_crtc->unpin_work) {
7558 spin_unlock_irqrestore(&dev->event_lock, flags);
7559 kfree(work);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007560 drm_vblank_put(dev, intel_crtc->pipe);
Chris Wilson468f0b42010-05-27 13:18:13 +01007561
7562 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007563 return -EBUSY;
7564 }
7565 intel_crtc->unpin_work = work;
7566 spin_unlock_irqrestore(&dev->event_lock, flags);
7567
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007568 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
7569 flush_workqueue(dev_priv->wq);
7570
Chris Wilson79158102012-05-23 11:13:58 +01007571 ret = i915_mutex_lock_interruptible(dev);
7572 if (ret)
7573 goto cleanup;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007574
Jesse Barnes75dfca82010-02-10 15:09:44 -08007575 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00007576 drm_gem_object_reference(&work->old_fb_obj->base);
7577 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007578
7579 crtc->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01007580
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007581 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007582
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01007583 work->enable_stall_check = true;
7584
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007585 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +02007586 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007587
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007588 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
7589 if (ret)
7590 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007591
Chris Wilson7782de32011-07-08 12:22:41 +01007592 intel_disable_fbc(dev);
Chris Wilsonc65355b2013-06-06 16:53:41 -03007593 intel_mark_fb_busy(obj, NULL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007594 mutex_unlock(&dev->struct_mutex);
7595
Jesse Barnese5510fa2010-07-01 16:48:37 -07007596 trace_i915_flip_request(intel_crtc->plane, obj);
7597
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007598 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01007599
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007600cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007601 atomic_dec(&intel_crtc->unpin_work_count);
Ville Syrjälä4a35f832013-02-22 16:53:38 +02007602 crtc->fb = old_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00007603 drm_gem_object_unreference(&work->old_fb_obj->base);
7604 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01007605 mutex_unlock(&dev->struct_mutex);
7606
Chris Wilson79158102012-05-23 11:13:58 +01007607cleanup:
Chris Wilson96b099f2010-06-07 14:03:04 +01007608 spin_lock_irqsave(&dev->event_lock, flags);
7609 intel_crtc->unpin_work = NULL;
7610 spin_unlock_irqrestore(&dev->event_lock, flags);
7611
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007612 drm_vblank_put(dev, intel_crtc->pipe);
7613free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01007614 kfree(work);
7615
7616 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007617}
7618
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007619static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007620 .mode_set_base_atomic = intel_pipe_set_base_atomic,
7621 .load_lut = intel_crtc_load_lut,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007622};
7623
Daniel Vetter50f56112012-07-02 09:35:43 +02007624static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
7625 struct drm_crtc *crtc)
7626{
7627 struct drm_device *dev;
7628 struct drm_crtc *tmp;
7629 int crtc_mask = 1;
7630
7631 WARN(!crtc, "checking null crtc?\n");
7632
7633 dev = crtc->dev;
7634
7635 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
7636 if (tmp == crtc)
7637 break;
7638 crtc_mask <<= 1;
7639 }
7640
7641 if (encoder->possible_crtcs & crtc_mask)
7642 return true;
7643 return false;
7644}
7645
Daniel Vetter9a935852012-07-05 22:34:27 +02007646/**
7647 * intel_modeset_update_staged_output_state
7648 *
7649 * Updates the staged output configuration state, e.g. after we've read out the
7650 * current hw state.
7651 */
7652static void intel_modeset_update_staged_output_state(struct drm_device *dev)
7653{
7654 struct intel_encoder *encoder;
7655 struct intel_connector *connector;
7656
7657 list_for_each_entry(connector, &dev->mode_config.connector_list,
7658 base.head) {
7659 connector->new_encoder =
7660 to_intel_encoder(connector->base.encoder);
7661 }
7662
7663 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7664 base.head) {
7665 encoder->new_crtc =
7666 to_intel_crtc(encoder->base.crtc);
7667 }
7668}
7669
7670/**
7671 * intel_modeset_commit_output_state
7672 *
7673 * This function copies the stage display pipe configuration to the real one.
7674 */
7675static void intel_modeset_commit_output_state(struct drm_device *dev)
7676{
7677 struct intel_encoder *encoder;
7678 struct intel_connector *connector;
7679
7680 list_for_each_entry(connector, &dev->mode_config.connector_list,
7681 base.head) {
7682 connector->base.encoder = &connector->new_encoder->base;
7683 }
7684
7685 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7686 base.head) {
7687 encoder->base.crtc = &encoder->new_crtc->base;
7688 }
7689}
7690
Daniel Vetter050f7ae2013-06-02 13:26:23 +02007691static void
7692connected_sink_compute_bpp(struct intel_connector * connector,
7693 struct intel_crtc_config *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007694{
Daniel Vetter050f7ae2013-06-02 13:26:23 +02007695 int bpp = pipe_config->pipe_bpp;
7696
7697 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
7698 connector->base.base.id,
7699 drm_get_connector_name(&connector->base));
7700
7701 /* Don't use an invalid EDID bpc value */
7702 if (connector->base.display_info.bpc &&
7703 connector->base.display_info.bpc * 3 < bpp) {
7704 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
7705 bpp, connector->base.display_info.bpc*3);
7706 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
7707 }
7708
7709 /* Clamp bpp to 8 on screens without EDID 1.4 */
7710 if (connector->base.display_info.bpc == 0 && bpp > 24) {
7711 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
7712 bpp);
7713 pipe_config->pipe_bpp = 24;
7714 }
7715}
7716
7717static int
7718compute_baseline_pipe_bpp(struct intel_crtc *crtc,
7719 struct drm_framebuffer *fb,
7720 struct intel_crtc_config *pipe_config)
7721{
7722 struct drm_device *dev = crtc->base.dev;
7723 struct intel_connector *connector;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007724 int bpp;
7725
Daniel Vetterd42264b2013-03-28 16:38:08 +01007726 switch (fb->pixel_format) {
7727 case DRM_FORMAT_C8:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007728 bpp = 8*3; /* since we go through a colormap */
7729 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01007730 case DRM_FORMAT_XRGB1555:
7731 case DRM_FORMAT_ARGB1555:
7732 /* checked in intel_framebuffer_init already */
7733 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
7734 return -EINVAL;
7735 case DRM_FORMAT_RGB565:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007736 bpp = 6*3; /* min is 18bpp */
7737 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01007738 case DRM_FORMAT_XBGR8888:
7739 case DRM_FORMAT_ABGR8888:
7740 /* checked in intel_framebuffer_init already */
7741 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
7742 return -EINVAL;
7743 case DRM_FORMAT_XRGB8888:
7744 case DRM_FORMAT_ARGB8888:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007745 bpp = 8*3;
7746 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01007747 case DRM_FORMAT_XRGB2101010:
7748 case DRM_FORMAT_ARGB2101010:
7749 case DRM_FORMAT_XBGR2101010:
7750 case DRM_FORMAT_ABGR2101010:
7751 /* checked in intel_framebuffer_init already */
7752 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
Daniel Vetterbaba1332013-03-27 00:45:00 +01007753 return -EINVAL;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007754 bpp = 10*3;
7755 break;
Daniel Vetterbaba1332013-03-27 00:45:00 +01007756 /* TODO: gen4+ supports 16 bpc floating point, too. */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007757 default:
7758 DRM_DEBUG_KMS("unsupported depth\n");
7759 return -EINVAL;
7760 }
7761
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007762 pipe_config->pipe_bpp = bpp;
7763
7764 /* Clamp display bpp to EDID value */
7765 list_for_each_entry(connector, &dev->mode_config.connector_list,
Daniel Vetter050f7ae2013-06-02 13:26:23 +02007766 base.head) {
Daniel Vetter1b829e02013-06-02 13:26:24 +02007767 if (!connector->new_encoder ||
7768 connector->new_encoder->new_crtc != crtc)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007769 continue;
7770
Daniel Vetter050f7ae2013-06-02 13:26:23 +02007771 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007772 }
7773
7774 return bpp;
7775}
7776
Daniel Vetterc0b03412013-05-28 12:05:54 +02007777static void intel_dump_pipe_config(struct intel_crtc *crtc,
7778 struct intel_crtc_config *pipe_config,
7779 const char *context)
7780{
7781 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
7782 context, pipe_name(crtc->pipe));
7783
7784 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
7785 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
7786 pipe_config->pipe_bpp, pipe_config->dither);
7787 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
7788 pipe_config->has_pch_encoder,
7789 pipe_config->fdi_lanes,
7790 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
7791 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
7792 pipe_config->fdi_m_n.tu);
7793 DRM_DEBUG_KMS("requested mode:\n");
7794 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
7795 DRM_DEBUG_KMS("adjusted mode:\n");
7796 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
7797 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
7798 pipe_config->gmch_pfit.control,
7799 pipe_config->gmch_pfit.pgm_ratios,
7800 pipe_config->gmch_pfit.lvds_border_bits);
7801 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x\n",
7802 pipe_config->pch_pfit.pos,
7803 pipe_config->pch_pfit.size);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03007804 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Daniel Vetterc0b03412013-05-28 12:05:54 +02007805}
7806
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02007807static bool check_encoder_cloning(struct drm_crtc *crtc)
7808{
7809 int num_encoders = 0;
7810 bool uncloneable_encoders = false;
7811 struct intel_encoder *encoder;
7812
7813 list_for_each_entry(encoder, &crtc->dev->mode_config.encoder_list,
7814 base.head) {
7815 if (&encoder->new_crtc->base != crtc)
7816 continue;
7817
7818 num_encoders++;
7819 if (!encoder->cloneable)
7820 uncloneable_encoders = true;
7821 }
7822
7823 return !(num_encoders > 1 && uncloneable_encoders);
7824}
7825
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007826static struct intel_crtc_config *
7827intel_modeset_pipe_config(struct drm_crtc *crtc,
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007828 struct drm_framebuffer *fb,
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007829 struct drm_display_mode *mode)
Daniel Vetter7758a112012-07-08 19:40:39 +02007830{
7831 struct drm_device *dev = crtc->dev;
Daniel Vetter7758a112012-07-08 19:40:39 +02007832 struct drm_encoder_helper_funcs *encoder_funcs;
7833 struct intel_encoder *encoder;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007834 struct intel_crtc_config *pipe_config;
Daniel Vettere29c22c2013-02-21 00:00:16 +01007835 int plane_bpp, ret = -EINVAL;
7836 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +02007837
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02007838 if (!check_encoder_cloning(crtc)) {
7839 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
7840 return ERR_PTR(-EINVAL);
7841 }
7842
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007843 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7844 if (!pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +02007845 return ERR_PTR(-ENOMEM);
7846
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007847 drm_mode_copy(&pipe_config->adjusted_mode, mode);
7848 drm_mode_copy(&pipe_config->requested_mode, mode);
Daniel Vettereccb1402013-05-22 00:50:22 +02007849 pipe_config->cpu_transcoder = to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007850 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007851
Daniel Vetter050f7ae2013-06-02 13:26:23 +02007852 /* Compute a starting value for pipe_config->pipe_bpp taking the source
7853 * plane pixel format and any sink constraints into account. Returns the
7854 * source plane bpp so that dithering can be selected on mismatches
7855 * after encoders and crtc also have had their say. */
7856 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
7857 fb, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007858 if (plane_bpp < 0)
7859 goto fail;
7860
Daniel Vettere29c22c2013-02-21 00:00:16 +01007861encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +02007862 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +02007863 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +02007864 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +02007865
Daniel Vetter7758a112012-07-08 19:40:39 +02007866 /* Pass our mode to the connectors and the CRTC to give them a chance to
7867 * adjust it according to limitations or connector properties, and also
7868 * a chance to reject the mode entirely.
7869 */
7870 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7871 base.head) {
7872
7873 if (&encoder->new_crtc->base != crtc)
7874 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +01007875
7876 if (encoder->compute_config) {
7877 if (!(encoder->compute_config(encoder, pipe_config))) {
7878 DRM_DEBUG_KMS("Encoder config failure\n");
7879 goto fail;
7880 }
7881
7882 continue;
7883 }
7884
Daniel Vetter7758a112012-07-08 19:40:39 +02007885 encoder_funcs = encoder->base.helper_private;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007886 if (!(encoder_funcs->mode_fixup(&encoder->base,
7887 &pipe_config->requested_mode,
7888 &pipe_config->adjusted_mode))) {
Daniel Vetter7758a112012-07-08 19:40:39 +02007889 DRM_DEBUG_KMS("Encoder fixup failed\n");
7890 goto fail;
7891 }
7892 }
7893
Daniel Vetterff9a6752013-06-01 17:16:21 +02007894 /* Set default port clock if not overwritten by the encoder. Needs to be
7895 * done afterwards in case the encoder adjusts the mode. */
7896 if (!pipe_config->port_clock)
7897 pipe_config->port_clock = pipe_config->adjusted_mode.clock;
7898
Daniel Vettera43f6e02013-06-07 23:10:32 +02007899 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01007900 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +02007901 DRM_DEBUG_KMS("CRTC fixup failed\n");
7902 goto fail;
7903 }
Daniel Vettere29c22c2013-02-21 00:00:16 +01007904
7905 if (ret == RETRY) {
7906 if (WARN(!retry, "loop in pipe configuration computation\n")) {
7907 ret = -EINVAL;
7908 goto fail;
7909 }
7910
7911 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
7912 retry = false;
7913 goto encoder_retry;
7914 }
7915
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007916 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
7917 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
7918 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
7919
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007920 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +02007921fail:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007922 kfree(pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01007923 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +02007924}
7925
Daniel Vettere2e1ed42012-07-08 21:14:38 +02007926/* Computes which crtcs are affected and sets the relevant bits in the mask. For
7927 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
7928static void
7929intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
7930 unsigned *prepare_pipes, unsigned *disable_pipes)
7931{
7932 struct intel_crtc *intel_crtc;
7933 struct drm_device *dev = crtc->dev;
7934 struct intel_encoder *encoder;
7935 struct intel_connector *connector;
7936 struct drm_crtc *tmp_crtc;
7937
7938 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
7939
7940 /* Check which crtcs have changed outputs connected to them, these need
7941 * to be part of the prepare_pipes mask. We don't (yet) support global
7942 * modeset across multiple crtcs, so modeset_pipes will only have one
7943 * bit set at most. */
7944 list_for_each_entry(connector, &dev->mode_config.connector_list,
7945 base.head) {
7946 if (connector->base.encoder == &connector->new_encoder->base)
7947 continue;
7948
7949 if (connector->base.encoder) {
7950 tmp_crtc = connector->base.encoder->crtc;
7951
7952 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
7953 }
7954
7955 if (connector->new_encoder)
7956 *prepare_pipes |=
7957 1 << connector->new_encoder->new_crtc->pipe;
7958 }
7959
7960 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7961 base.head) {
7962 if (encoder->base.crtc == &encoder->new_crtc->base)
7963 continue;
7964
7965 if (encoder->base.crtc) {
7966 tmp_crtc = encoder->base.crtc;
7967
7968 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
7969 }
7970
7971 if (encoder->new_crtc)
7972 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
7973 }
7974
7975 /* Check for any pipes that will be fully disabled ... */
7976 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
7977 base.head) {
7978 bool used = false;
7979
7980 /* Don't try to disable disabled crtcs. */
7981 if (!intel_crtc->base.enabled)
7982 continue;
7983
7984 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7985 base.head) {
7986 if (encoder->new_crtc == intel_crtc)
7987 used = true;
7988 }
7989
7990 if (!used)
7991 *disable_pipes |= 1 << intel_crtc->pipe;
7992 }
7993
7994
7995 /* set_mode is also used to update properties on life display pipes. */
7996 intel_crtc = to_intel_crtc(crtc);
7997 if (crtc->enabled)
7998 *prepare_pipes |= 1 << intel_crtc->pipe;
7999
Daniel Vetterb6c51642013-04-12 18:48:43 +02008000 /*
8001 * For simplicity do a full modeset on any pipe where the output routing
8002 * changed. We could be more clever, but that would require us to be
8003 * more careful with calling the relevant encoder->mode_set functions.
8004 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +02008005 if (*prepare_pipes)
8006 *modeset_pipes = *prepare_pipes;
8007
8008 /* ... and mask these out. */
8009 *modeset_pipes &= ~(*disable_pipes);
8010 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +02008011
8012 /*
8013 * HACK: We don't (yet) fully support global modesets. intel_set_config
8014 * obies this rule, but the modeset restore mode of
8015 * intel_modeset_setup_hw_state does not.
8016 */
8017 *modeset_pipes &= 1 << intel_crtc->pipe;
8018 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +02008019
8020 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
8021 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +02008022}
8023
Daniel Vetterea9d7582012-07-10 10:42:52 +02008024static bool intel_crtc_in_use(struct drm_crtc *crtc)
8025{
8026 struct drm_encoder *encoder;
8027 struct drm_device *dev = crtc->dev;
8028
8029 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
8030 if (encoder->crtc == crtc)
8031 return true;
8032
8033 return false;
8034}
8035
8036static void
8037intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
8038{
8039 struct intel_encoder *intel_encoder;
8040 struct intel_crtc *intel_crtc;
8041 struct drm_connector *connector;
8042
8043 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
8044 base.head) {
8045 if (!intel_encoder->base.crtc)
8046 continue;
8047
8048 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
8049
8050 if (prepare_pipes & (1 << intel_crtc->pipe))
8051 intel_encoder->connectors_active = false;
8052 }
8053
8054 intel_modeset_commit_output_state(dev);
8055
8056 /* Update computed state. */
8057 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
8058 base.head) {
8059 intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
8060 }
8061
8062 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
8063 if (!connector->encoder || !connector->encoder->crtc)
8064 continue;
8065
8066 intel_crtc = to_intel_crtc(connector->encoder->crtc);
8067
8068 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +02008069 struct drm_property *dpms_property =
8070 dev->mode_config.dpms_property;
8071
Daniel Vetterea9d7582012-07-10 10:42:52 +02008072 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -05008073 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +02008074 dpms_property,
8075 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +02008076
8077 intel_encoder = to_intel_encoder(connector->encoder);
8078 intel_encoder->connectors_active = true;
8079 }
8080 }
8081
8082}
8083
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008084static bool intel_fuzzy_clock_check(struct intel_crtc_config *cur,
8085 struct intel_crtc_config *new)
8086{
8087 int clock1, clock2, diff;
8088
8089 clock1 = cur->adjusted_mode.clock;
8090 clock2 = new->adjusted_mode.clock;
8091
8092 if (clock1 == clock2)
8093 return true;
8094
8095 if (!clock1 || !clock2)
8096 return false;
8097
8098 diff = abs(clock1 - clock2);
8099
8100 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
8101 return true;
8102
8103 return false;
8104}
8105
Daniel Vetter25c5b262012-07-08 22:08:04 +02008106#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
8107 list_for_each_entry((intel_crtc), \
8108 &(dev)->mode_config.crtc_list, \
8109 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +02008110 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +02008111
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008112static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008113intel_pipe_config_compare(struct drm_device *dev,
8114 struct intel_crtc_config *current_config,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008115 struct intel_crtc_config *pipe_config)
8116{
Daniel Vetter66e985c2013-06-05 13:34:20 +02008117#define PIPE_CONF_CHECK_X(name) \
8118 if (current_config->name != pipe_config->name) { \
8119 DRM_ERROR("mismatch in " #name " " \
8120 "(expected 0x%08x, found 0x%08x)\n", \
8121 current_config->name, \
8122 pipe_config->name); \
8123 return false; \
8124 }
8125
Daniel Vetter08a24032013-04-19 11:25:34 +02008126#define PIPE_CONF_CHECK_I(name) \
8127 if (current_config->name != pipe_config->name) { \
8128 DRM_ERROR("mismatch in " #name " " \
8129 "(expected %i, found %i)\n", \
8130 current_config->name, \
8131 pipe_config->name); \
8132 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +01008133 }
8134
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008135#define PIPE_CONF_CHECK_FLAGS(name, mask) \
8136 if ((current_config->name ^ pipe_config->name) & (mask)) { \
8137 DRM_ERROR("mismatch in " #name " " \
8138 "(expected %i, found %i)\n", \
8139 current_config->name & (mask), \
8140 pipe_config->name & (mask)); \
8141 return false; \
8142 }
8143
Daniel Vetterbb760062013-06-06 14:55:52 +02008144#define PIPE_CONF_QUIRK(quirk) \
8145 ((current_config->quirks | pipe_config->quirks) & (quirk))
8146
Daniel Vettereccb1402013-05-22 00:50:22 +02008147 PIPE_CONF_CHECK_I(cpu_transcoder);
8148
Daniel Vetter08a24032013-04-19 11:25:34 +02008149 PIPE_CONF_CHECK_I(has_pch_encoder);
8150 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +02008151 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
8152 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
8153 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
8154 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
8155 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +02008156
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008157 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
8158 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
8159 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
8160 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
8161 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
8162 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
8163
8164 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
8165 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
8166 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
8167 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
8168 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
8169 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
8170
Daniel Vetterc93f54c2013-06-27 19:47:19 +02008171 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008172
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008173 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8174 DRM_MODE_FLAG_INTERLACE);
8175
Daniel Vetterbb760062013-06-06 14:55:52 +02008176 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
8177 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8178 DRM_MODE_FLAG_PHSYNC);
8179 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8180 DRM_MODE_FLAG_NHSYNC);
8181 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8182 DRM_MODE_FLAG_PVSYNC);
8183 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8184 DRM_MODE_FLAG_NVSYNC);
8185 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -07008186
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008187 PIPE_CONF_CHECK_I(requested_mode.hdisplay);
8188 PIPE_CONF_CHECK_I(requested_mode.vdisplay);
8189
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008190 PIPE_CONF_CHECK_I(gmch_pfit.control);
8191 /* pfit ratios are autocomputed by the hw on gen4+ */
8192 if (INTEL_INFO(dev)->gen < 4)
8193 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
8194 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
8195 PIPE_CONF_CHECK_I(pch_pfit.pos);
8196 PIPE_CONF_CHECK_I(pch_pfit.size);
8197
Paulo Zanoni42db64e2013-05-31 16:33:22 -03008198 PIPE_CONF_CHECK_I(ips_enabled);
8199
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008200 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008201 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008202 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008203 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
8204 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008205
Daniel Vetter66e985c2013-06-05 13:34:20 +02008206#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +02008207#undef PIPE_CONF_CHECK_I
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008208#undef PIPE_CONF_CHECK_FLAGS
Daniel Vetterbb760062013-06-06 14:55:52 +02008209#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008210
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008211 if (!IS_HASWELL(dev)) {
8212 if (!intel_fuzzy_clock_check(current_config, pipe_config)) {
8213 DRM_ERROR("mismatch in clock (expected %d, found %d\n",
8214 current_config->adjusted_mode.clock,
8215 pipe_config->adjusted_mode.clock);
8216 return false;
8217 }
8218 }
8219
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008220 return true;
8221}
8222
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02008223static void
8224check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008225{
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008226 struct intel_connector *connector;
8227
8228 list_for_each_entry(connector, &dev->mode_config.connector_list,
8229 base.head) {
8230 /* This also checks the encoder/connector hw state with the
8231 * ->get_hw_state callbacks. */
8232 intel_connector_check_state(connector);
8233
8234 WARN(&connector->new_encoder->base != connector->base.encoder,
8235 "connector's staged encoder doesn't match current encoder\n");
8236 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02008237}
8238
8239static void
8240check_encoder_state(struct drm_device *dev)
8241{
8242 struct intel_encoder *encoder;
8243 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008244
8245 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8246 base.head) {
8247 bool enabled = false;
8248 bool active = false;
8249 enum pipe pipe, tracked_pipe;
8250
8251 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
8252 encoder->base.base.id,
8253 drm_get_encoder_name(&encoder->base));
8254
8255 WARN(&encoder->new_crtc->base != encoder->base.crtc,
8256 "encoder's stage crtc doesn't match current crtc\n");
8257 WARN(encoder->connectors_active && !encoder->base.crtc,
8258 "encoder's active_connectors set, but no crtc\n");
8259
8260 list_for_each_entry(connector, &dev->mode_config.connector_list,
8261 base.head) {
8262 if (connector->base.encoder != &encoder->base)
8263 continue;
8264 enabled = true;
8265 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
8266 active = true;
8267 }
8268 WARN(!!encoder->base.crtc != enabled,
8269 "encoder's enabled state mismatch "
8270 "(expected %i, found %i)\n",
8271 !!encoder->base.crtc, enabled);
8272 WARN(active && !encoder->base.crtc,
8273 "active encoder with no crtc\n");
8274
8275 WARN(encoder->connectors_active != active,
8276 "encoder's computed active state doesn't match tracked active state "
8277 "(expected %i, found %i)\n", active, encoder->connectors_active);
8278
8279 active = encoder->get_hw_state(encoder, &pipe);
8280 WARN(active != encoder->connectors_active,
8281 "encoder's hw state doesn't match sw tracking "
8282 "(expected %i, found %i)\n",
8283 encoder->connectors_active, active);
8284
8285 if (!encoder->base.crtc)
8286 continue;
8287
8288 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
8289 WARN(active && pipe != tracked_pipe,
8290 "active encoder's pipe doesn't match"
8291 "(expected %i, found %i)\n",
8292 tracked_pipe, pipe);
8293
8294 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02008295}
8296
8297static void
8298check_crtc_state(struct drm_device *dev)
8299{
8300 drm_i915_private_t *dev_priv = dev->dev_private;
8301 struct intel_crtc *crtc;
8302 struct intel_encoder *encoder;
8303 struct intel_crtc_config pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008304
8305 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
8306 base.head) {
8307 bool enabled = false;
8308 bool active = false;
8309
Jesse Barnes045ac3b2013-05-14 17:08:26 -07008310 memset(&pipe_config, 0, sizeof(pipe_config));
8311
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008312 DRM_DEBUG_KMS("[CRTC:%d]\n",
8313 crtc->base.base.id);
8314
8315 WARN(crtc->active && !crtc->base.enabled,
8316 "active crtc, but not enabled in sw tracking\n");
8317
8318 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8319 base.head) {
8320 if (encoder->base.crtc != &crtc->base)
8321 continue;
8322 enabled = true;
8323 if (encoder->connectors_active)
8324 active = true;
8325 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008326
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008327 WARN(active != crtc->active,
8328 "crtc's computed active state doesn't match tracked active state "
8329 "(expected %i, found %i)\n", active, crtc->active);
8330 WARN(enabled != crtc->base.enabled,
8331 "crtc's computed enabled state doesn't match tracked enabled state "
8332 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
8333
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008334 active = dev_priv->display.get_pipe_config(crtc,
8335 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +02008336
8337 /* hw state is inconsistent with the pipe A quirk */
8338 if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
8339 active = crtc->active;
8340
Daniel Vetter6c49f242013-06-06 12:45:25 +02008341 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8342 base.head) {
8343 if (encoder->base.crtc != &crtc->base)
8344 continue;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008345 if (encoder->get_config &&
8346 dev_priv->display.get_clock) {
Daniel Vetter6c49f242013-06-06 12:45:25 +02008347 encoder->get_config(encoder, &pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008348 dev_priv->display.get_clock(crtc,
8349 &pipe_config);
8350 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008351 }
8352
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008353 WARN(crtc->active != active,
8354 "crtc active state doesn't match with hw state "
8355 "(expected %i, found %i)\n", crtc->active, active);
8356
Daniel Vetterc0b03412013-05-28 12:05:54 +02008357 if (active &&
8358 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
8359 WARN(1, "pipe state doesn't match!\n");
8360 intel_dump_pipe_config(crtc, &pipe_config,
8361 "[hw state]");
8362 intel_dump_pipe_config(crtc, &crtc->config,
8363 "[sw state]");
8364 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008365 }
8366}
8367
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02008368static void
8369check_shared_dpll_state(struct drm_device *dev)
8370{
8371 drm_i915_private_t *dev_priv = dev->dev_private;
8372 struct intel_crtc *crtc;
8373 struct intel_dpll_hw_state dpll_hw_state;
8374 int i;
Daniel Vetter53589012013-06-05 13:34:16 +02008375
8376 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8377 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
8378 int enabled_crtcs = 0, active_crtcs = 0;
8379 bool active;
8380
8381 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
8382
8383 DRM_DEBUG_KMS("%s\n", pll->name);
8384
8385 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
8386
8387 WARN(pll->active > pll->refcount,
8388 "more active pll users than references: %i vs %i\n",
8389 pll->active, pll->refcount);
8390 WARN(pll->active && !pll->on,
8391 "pll in active use but not on in sw tracking\n");
8392 WARN(pll->on != active,
8393 "pll on state mismatch (expected %i, found %i)\n",
8394 pll->on, active);
8395
8396 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
8397 base.head) {
8398 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
8399 enabled_crtcs++;
8400 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
8401 active_crtcs++;
8402 }
8403 WARN(pll->active != active_crtcs,
8404 "pll active crtcs mismatch (expected %i, found %i)\n",
8405 pll->active, active_crtcs);
8406 WARN(pll->refcount != enabled_crtcs,
8407 "pll enabled crtcs mismatch (expected %i, found %i)\n",
8408 pll->refcount, enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008409
8410 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
8411 sizeof(dpll_hw_state)),
8412 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +02008413 }
Daniel Vettera6778b32012-07-02 09:56:42 +02008414}
8415
Daniel Vetter91d1b4b2013-06-05 13:34:18 +02008416void
8417intel_modeset_check_state(struct drm_device *dev)
8418{
8419 check_connector_state(dev);
8420 check_encoder_state(dev);
8421 check_crtc_state(dev);
8422 check_shared_dpll_state(dev);
8423}
8424
Daniel Vetterf30da182013-04-11 20:22:50 +02008425static int __intel_set_mode(struct drm_crtc *crtc,
8426 struct drm_display_mode *mode,
8427 int x, int y, struct drm_framebuffer *fb)
Daniel Vettera6778b32012-07-02 09:56:42 +02008428{
8429 struct drm_device *dev = crtc->dev;
Daniel Vetterdbf2b54e2012-07-02 11:18:29 +02008430 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008431 struct drm_display_mode *saved_mode, *saved_hwmode;
8432 struct intel_crtc_config *pipe_config = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +02008433 struct intel_crtc *intel_crtc;
8434 unsigned disable_pipes, prepare_pipes, modeset_pipes;
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008435 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +02008436
Tim Gardner3ac18232012-12-07 07:54:26 -07008437 saved_mode = kmalloc(2 * sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008438 if (!saved_mode)
8439 return -ENOMEM;
Tim Gardner3ac18232012-12-07 07:54:26 -07008440 saved_hwmode = saved_mode + 1;
Daniel Vettera6778b32012-07-02 09:56:42 +02008441
Daniel Vettere2e1ed42012-07-08 21:14:38 +02008442 intel_modeset_affected_pipes(crtc, &modeset_pipes,
Daniel Vetter25c5b262012-07-08 22:08:04 +02008443 &prepare_pipes, &disable_pipes);
8444
Tim Gardner3ac18232012-12-07 07:54:26 -07008445 *saved_hwmode = crtc->hwmode;
8446 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02008447
Daniel Vetter25c5b262012-07-08 22:08:04 +02008448 /* Hack: Because we don't (yet) support global modeset on multiple
8449 * crtcs, we don't keep track of the new mode for more than one crtc.
8450 * Hence simply check whether any bit is set in modeset_pipes in all the
8451 * pieces of code that are not yet converted to deal with mutliple crtcs
8452 * changing their mode at the same time. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02008453 if (modeset_pipes) {
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008454 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008455 if (IS_ERR(pipe_config)) {
8456 ret = PTR_ERR(pipe_config);
8457 pipe_config = NULL;
8458
Tim Gardner3ac18232012-12-07 07:54:26 -07008459 goto out;
Daniel Vetter25c5b262012-07-08 22:08:04 +02008460 }
Daniel Vetterc0b03412013-05-28 12:05:54 +02008461 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
8462 "[modeset]");
Daniel Vettera6778b32012-07-02 09:56:42 +02008463 }
8464
Daniel Vetter460da9162013-03-27 00:44:51 +01008465 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
8466 intel_crtc_disable(&intel_crtc->base);
8467
Daniel Vetterea9d7582012-07-10 10:42:52 +02008468 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
8469 if (intel_crtc->base.enabled)
8470 dev_priv->display.crtc_disable(&intel_crtc->base);
8471 }
Daniel Vettera6778b32012-07-02 09:56:42 +02008472
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02008473 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
8474 * to set it here already despite that we pass it down the callchain.
8475 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008476 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +02008477 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008478 /* mode_set/enable/disable functions rely on a correct pipe
8479 * config. */
8480 to_intel_crtc(crtc)->config = *pipe_config;
8481 }
Daniel Vetter7758a112012-07-08 19:40:39 +02008482
Daniel Vetterea9d7582012-07-10 10:42:52 +02008483 /* Only after disabling all output pipelines that will be changed can we
8484 * update the the output configuration. */
8485 intel_modeset_update_state(dev, prepare_pipes);
8486
Daniel Vetter47fab732012-10-26 10:58:18 +02008487 if (dev_priv->display.modeset_global_resources)
8488 dev_priv->display.modeset_global_resources(dev);
8489
Daniel Vettera6778b32012-07-02 09:56:42 +02008490 /* Set up the DPLL and any encoders state that needs to adjust or depend
8491 * on the DPLL.
8492 */
Daniel Vetter25c5b262012-07-08 22:08:04 +02008493 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008494 ret = intel_crtc_mode_set(&intel_crtc->base,
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008495 x, y, fb);
8496 if (ret)
8497 goto done;
Daniel Vettera6778b32012-07-02 09:56:42 +02008498 }
8499
8500 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02008501 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
8502 dev_priv->display.crtc_enable(&intel_crtc->base);
Daniel Vettera6778b32012-07-02 09:56:42 +02008503
Daniel Vetter25c5b262012-07-08 22:08:04 +02008504 if (modeset_pipes) {
8505 /* Store real post-adjustment hardware mode. */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008506 crtc->hwmode = pipe_config->adjusted_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02008507
Daniel Vetter25c5b262012-07-08 22:08:04 +02008508 /* Calculate and store various constants which
8509 * are later needed by vblank and swap-completion
8510 * timestamping. They are derived from true hwmode.
8511 */
8512 drm_calc_timestamping_constants(crtc);
8513 }
Daniel Vettera6778b32012-07-02 09:56:42 +02008514
8515 /* FIXME: add subpixel order */
8516done:
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008517 if (ret && crtc->enabled) {
Tim Gardner3ac18232012-12-07 07:54:26 -07008518 crtc->hwmode = *saved_hwmode;
8519 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02008520 }
8521
Tim Gardner3ac18232012-12-07 07:54:26 -07008522out:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008523 kfree(pipe_config);
Tim Gardner3ac18232012-12-07 07:54:26 -07008524 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +02008525 return ret;
8526}
8527
Daniel Vetterf30da182013-04-11 20:22:50 +02008528int intel_set_mode(struct drm_crtc *crtc,
8529 struct drm_display_mode *mode,
8530 int x, int y, struct drm_framebuffer *fb)
8531{
8532 int ret;
8533
8534 ret = __intel_set_mode(crtc, mode, x, y, fb);
8535
8536 if (ret == 0)
8537 intel_modeset_check_state(crtc->dev);
8538
8539 return ret;
8540}
8541
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008542void intel_crtc_restore_mode(struct drm_crtc *crtc)
8543{
8544 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->fb);
8545}
8546
Daniel Vetter25c5b262012-07-08 22:08:04 +02008547#undef for_each_intel_crtc_masked
8548
Daniel Vetterd9e55602012-07-04 22:16:09 +02008549static void intel_set_config_free(struct intel_set_config *config)
8550{
8551 if (!config)
8552 return;
8553
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008554 kfree(config->save_connector_encoders);
8555 kfree(config->save_encoder_crtcs);
Daniel Vetterd9e55602012-07-04 22:16:09 +02008556 kfree(config);
8557}
8558
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008559static int intel_set_config_save_state(struct drm_device *dev,
8560 struct intel_set_config *config)
8561{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008562 struct drm_encoder *encoder;
8563 struct drm_connector *connector;
8564 int count;
8565
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008566 config->save_encoder_crtcs =
8567 kcalloc(dev->mode_config.num_encoder,
8568 sizeof(struct drm_crtc *), GFP_KERNEL);
8569 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008570 return -ENOMEM;
8571
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008572 config->save_connector_encoders =
8573 kcalloc(dev->mode_config.num_connector,
8574 sizeof(struct drm_encoder *), GFP_KERNEL);
8575 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008576 return -ENOMEM;
8577
8578 /* Copy data. Note that driver private data is not affected.
8579 * Should anything bad happen only the expected state is
8580 * restored, not the drivers personal bookkeeping.
8581 */
8582 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008583 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008584 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008585 }
8586
8587 count = 0;
8588 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008589 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008590 }
8591
8592 return 0;
8593}
8594
8595static void intel_set_config_restore_state(struct drm_device *dev,
8596 struct intel_set_config *config)
8597{
Daniel Vetter9a935852012-07-05 22:34:27 +02008598 struct intel_encoder *encoder;
8599 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008600 int count;
8601
8602 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008603 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
8604 encoder->new_crtc =
8605 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008606 }
8607
8608 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008609 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
8610 connector->new_encoder =
8611 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008612 }
8613}
8614
Imre Deake3de42b2013-05-03 19:44:07 +02008615static bool
8616is_crtc_connector_off(struct drm_crtc *crtc, struct drm_connector *connectors,
8617 int num_connectors)
8618{
8619 int i;
8620
8621 for (i = 0; i < num_connectors; i++)
8622 if (connectors[i].encoder &&
8623 connectors[i].encoder->crtc == crtc &&
8624 connectors[i].dpms != DRM_MODE_DPMS_ON)
8625 return true;
8626
8627 return false;
8628}
8629
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008630static void
8631intel_set_config_compute_mode_changes(struct drm_mode_set *set,
8632 struct intel_set_config *config)
8633{
8634
8635 /* We should be able to check here if the fb has the same properties
8636 * and then just flip_or_move it */
Imre Deake3de42b2013-05-03 19:44:07 +02008637 if (set->connectors != NULL &&
8638 is_crtc_connector_off(set->crtc, *set->connectors,
8639 set->num_connectors)) {
8640 config->mode_changed = true;
8641 } else if (set->crtc->fb != set->fb) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008642 /* If we have no fb then treat it as a full mode set */
8643 if (set->crtc->fb == NULL) {
8644 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
8645 config->mode_changed = true;
8646 } else if (set->fb == NULL) {
8647 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +01008648 } else if (set->fb->pixel_format !=
8649 set->crtc->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008650 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02008651 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008652 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +02008653 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008654 }
8655
Daniel Vetter835c5872012-07-10 18:11:08 +02008656 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008657 config->fb_changed = true;
8658
8659 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
8660 DRM_DEBUG_KMS("modes are different, full mode set\n");
8661 drm_mode_debug_printmodeline(&set->crtc->mode);
8662 drm_mode_debug_printmodeline(set->mode);
8663 config->mode_changed = true;
8664 }
8665}
8666
Daniel Vetter2e431052012-07-04 22:42:15 +02008667static int
Daniel Vetter9a935852012-07-05 22:34:27 +02008668intel_modeset_stage_output_state(struct drm_device *dev,
8669 struct drm_mode_set *set,
8670 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +02008671{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008672 struct drm_crtc *new_crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02008673 struct intel_connector *connector;
8674 struct intel_encoder *encoder;
Daniel Vetter2e431052012-07-04 22:42:15 +02008675 int count, ro;
Daniel Vetter50f56112012-07-02 09:35:43 +02008676
Damien Lespiau9abdda72013-02-13 13:29:23 +00008677 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +02008678 * of connectors. For paranoia, double-check this. */
8679 WARN_ON(!set->fb && (set->num_connectors != 0));
8680 WARN_ON(set->fb && (set->num_connectors == 0));
8681
Daniel Vetter50f56112012-07-02 09:35:43 +02008682 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008683 list_for_each_entry(connector, &dev->mode_config.connector_list,
8684 base.head) {
8685 /* Otherwise traverse passed in connector list and get encoders
8686 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +02008687 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02008688 if (set->connectors[ro] == &connector->base) {
8689 connector->new_encoder = connector->encoder;
Daniel Vetter50f56112012-07-02 09:35:43 +02008690 break;
8691 }
8692 }
8693
Daniel Vetter9a935852012-07-05 22:34:27 +02008694 /* If we disable the crtc, disable all its connectors. Also, if
8695 * the connector is on the changing crtc but not on the new
8696 * connector list, disable it. */
8697 if ((!set->fb || ro == set->num_connectors) &&
8698 connector->base.encoder &&
8699 connector->base.encoder->crtc == set->crtc) {
8700 connector->new_encoder = NULL;
8701
8702 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
8703 connector->base.base.id,
8704 drm_get_connector_name(&connector->base));
8705 }
8706
8707
8708 if (&connector->new_encoder->base != connector->base.encoder) {
Daniel Vetter50f56112012-07-02 09:35:43 +02008709 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008710 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02008711 }
Daniel Vetter9a935852012-07-05 22:34:27 +02008712 }
8713 /* connector->new_encoder is now updated for all connectors. */
8714
8715 /* Update crtc of enabled connectors. */
Daniel Vetter50f56112012-07-02 09:35:43 +02008716 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008717 list_for_each_entry(connector, &dev->mode_config.connector_list,
8718 base.head) {
8719 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +02008720 continue;
8721
Daniel Vetter9a935852012-07-05 22:34:27 +02008722 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +02008723
8724 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02008725 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +02008726 new_crtc = set->crtc;
8727 }
8728
8729 /* Make sure the new CRTC will work with the encoder */
Daniel Vetter9a935852012-07-05 22:34:27 +02008730 if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
8731 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008732 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +02008733 }
Daniel Vetter9a935852012-07-05 22:34:27 +02008734 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
8735
8736 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
8737 connector->base.base.id,
8738 drm_get_connector_name(&connector->base),
8739 new_crtc->base.id);
8740 }
8741
8742 /* Check for any encoders that needs to be disabled. */
8743 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8744 base.head) {
8745 list_for_each_entry(connector,
8746 &dev->mode_config.connector_list,
8747 base.head) {
8748 if (connector->new_encoder == encoder) {
8749 WARN_ON(!connector->new_encoder->new_crtc);
8750
8751 goto next_encoder;
8752 }
8753 }
8754 encoder->new_crtc = NULL;
8755next_encoder:
8756 /* Only now check for crtc changes so we don't miss encoders
8757 * that will be disabled. */
8758 if (&encoder->new_crtc->base != encoder->base.crtc) {
Daniel Vetter50f56112012-07-02 09:35:43 +02008759 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008760 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02008761 }
8762 }
Daniel Vetter9a935852012-07-05 22:34:27 +02008763 /* Now we've also updated encoder->new_crtc for all encoders. */
Daniel Vetter50f56112012-07-02 09:35:43 +02008764
Daniel Vetter2e431052012-07-04 22:42:15 +02008765 return 0;
8766}
8767
8768static int intel_crtc_set_config(struct drm_mode_set *set)
8769{
8770 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +02008771 struct drm_mode_set save_set;
8772 struct intel_set_config *config;
8773 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +02008774
Daniel Vetter8d3e3752012-07-05 16:09:09 +02008775 BUG_ON(!set);
8776 BUG_ON(!set->crtc);
8777 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +02008778
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01008779 /* Enforce sane interface api - has been abused by the fb helper. */
8780 BUG_ON(!set->mode && set->fb);
8781 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +02008782
Daniel Vetter2e431052012-07-04 22:42:15 +02008783 if (set->fb) {
8784 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
8785 set->crtc->base.id, set->fb->base.id,
8786 (int)set->num_connectors, set->x, set->y);
8787 } else {
8788 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +02008789 }
8790
8791 dev = set->crtc->dev;
8792
8793 ret = -ENOMEM;
8794 config = kzalloc(sizeof(*config), GFP_KERNEL);
8795 if (!config)
8796 goto out_config;
8797
8798 ret = intel_set_config_save_state(dev, config);
8799 if (ret)
8800 goto out_config;
8801
8802 save_set.crtc = set->crtc;
8803 save_set.mode = &set->crtc->mode;
8804 save_set.x = set->crtc->x;
8805 save_set.y = set->crtc->y;
8806 save_set.fb = set->crtc->fb;
8807
8808 /* Compute whether we need a full modeset, only an fb base update or no
8809 * change at all. In the future we might also check whether only the
8810 * mode changed, e.g. for LVDS where we only change the panel fitter in
8811 * such cases. */
8812 intel_set_config_compute_mode_changes(set, config);
8813
Daniel Vetter9a935852012-07-05 22:34:27 +02008814 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +02008815 if (ret)
8816 goto fail;
8817
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008818 if (config->mode_changed) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008819 ret = intel_set_mode(set->crtc, set->mode,
8820 set->x, set->y, set->fb);
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008821 } else if (config->fb_changed) {
Ville Syrjälä4878cae2013-02-18 19:08:48 +02008822 intel_crtc_wait_for_pending_flips(set->crtc);
8823
Daniel Vetter4f660f42012-07-02 09:47:37 +02008824 ret = intel_pipe_set_base(set->crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +02008825 set->x, set->y, set->fb);
Daniel Vetter50f56112012-07-02 09:35:43 +02008826 }
8827
Chris Wilson2d05eae2013-05-03 17:36:25 +01008828 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +02008829 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
8830 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +02008831fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +01008832 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +02008833
Chris Wilson2d05eae2013-05-03 17:36:25 +01008834 /* Try to restore the config */
8835 if (config->mode_changed &&
8836 intel_set_mode(save_set.crtc, save_set.mode,
8837 save_set.x, save_set.y, save_set.fb))
8838 DRM_ERROR("failed to restore config after modeset failure\n");
8839 }
Daniel Vetter50f56112012-07-02 09:35:43 +02008840
Daniel Vetterd9e55602012-07-04 22:16:09 +02008841out_config:
8842 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +02008843 return ret;
8844}
8845
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008846static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008847 .cursor_set = intel_crtc_cursor_set,
8848 .cursor_move = intel_crtc_cursor_move,
8849 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +02008850 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008851 .destroy = intel_crtc_destroy,
8852 .page_flip = intel_crtc_page_flip,
8853};
8854
Paulo Zanoni79f689a2012-10-05 12:05:52 -03008855static void intel_cpu_pll_init(struct drm_device *dev)
8856{
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008857 if (HAS_DDI(dev))
Paulo Zanoni79f689a2012-10-05 12:05:52 -03008858 intel_ddi_pll_init(dev);
8859}
8860
Daniel Vetter53589012013-06-05 13:34:16 +02008861static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
8862 struct intel_shared_dpll *pll,
8863 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008864{
Daniel Vetter53589012013-06-05 13:34:16 +02008865 uint32_t val;
8866
8867 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +02008868 hw_state->dpll = val;
8869 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
8870 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +02008871
8872 return val & DPLL_VCO_ENABLE;
8873}
8874
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02008875static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
8876 struct intel_shared_dpll *pll)
8877{
8878 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
8879 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
8880}
8881
Daniel Vettere7b903d2013-06-05 13:34:14 +02008882static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
8883 struct intel_shared_dpll *pll)
8884{
Daniel Vettere7b903d2013-06-05 13:34:14 +02008885 /* PCH refclock must be enabled first */
8886 assert_pch_refclk_enabled(dev_priv);
8887
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02008888 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
8889
8890 /* Wait for the clocks to stabilize. */
8891 POSTING_READ(PCH_DPLL(pll->id));
8892 udelay(150);
8893
8894 /* The pixel multiplier can only be updated once the
8895 * DPLL is enabled and the clocks are stable.
8896 *
8897 * So write it again.
8898 */
8899 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
8900 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +02008901 udelay(200);
8902}
8903
8904static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
8905 struct intel_shared_dpll *pll)
8906{
8907 struct drm_device *dev = dev_priv->dev;
8908 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +02008909
8910 /* Make sure no transcoder isn't still depending on us. */
8911 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
8912 if (intel_crtc_to_shared_dpll(crtc) == pll)
8913 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
8914 }
8915
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02008916 I915_WRITE(PCH_DPLL(pll->id), 0);
8917 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +02008918 udelay(200);
8919}
8920
Daniel Vetter46edb022013-06-05 13:34:12 +02008921static char *ibx_pch_dpll_names[] = {
8922 "PCH DPLL A",
8923 "PCH DPLL B",
8924};
8925
Daniel Vetter7c74ade2013-06-05 13:34:11 +02008926static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008927{
Daniel Vettere7b903d2013-06-05 13:34:14 +02008928 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008929 int i;
8930
Daniel Vetter7c74ade2013-06-05 13:34:11 +02008931 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008932
Daniel Vettere72f9fb2013-06-05 13:34:06 +02008933 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +02008934 dev_priv->shared_dplls[i].id = i;
8935 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +02008936 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +02008937 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
8938 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +02008939 dev_priv->shared_dplls[i].get_hw_state =
8940 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008941 }
8942}
8943
Daniel Vetter7c74ade2013-06-05 13:34:11 +02008944static void intel_shared_dpll_init(struct drm_device *dev)
8945{
Daniel Vettere7b903d2013-06-05 13:34:14 +02008946 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +02008947
8948 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8949 ibx_pch_dpll_init(dev);
8950 else
8951 dev_priv->num_shared_dpll = 0;
8952
8953 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
8954 DRM_DEBUG_KMS("%i shared PLLs initialized\n",
8955 dev_priv->num_shared_dpll);
8956}
8957
Hannes Ederb358d0a2008-12-18 21:18:47 +01008958static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -08008959{
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08008960 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08008961 struct intel_crtc *intel_crtc;
8962 int i;
8963
8964 intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
8965 if (intel_crtc == NULL)
8966 return;
8967
8968 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
8969
8970 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -08008971 for (i = 0; i < 256; i++) {
8972 intel_crtc->lut_r[i] = i;
8973 intel_crtc->lut_g[i] = i;
8974 intel_crtc->lut_b[i] = i;
8975 }
8976
Jesse Barnes80824002009-09-10 15:28:06 -07008977 /* Swap pipes & planes for FBC on pre-965 */
8978 intel_crtc->pipe = pipe;
8979 intel_crtc->plane = pipe;
Chris Wilsone2e767a2010-09-13 16:53:12 +01008980 if (IS_MOBILE(dev) && IS_GEN3(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08008981 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +01008982 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07008983 }
8984
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08008985 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
8986 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
8987 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
8988 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
8989
Jesse Barnes79e53942008-11-07 14:24:08 -08008990 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -08008991}
8992
Carl Worth08d7b3d2009-04-29 14:43:54 -07008993int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00008994 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -07008995{
Carl Worth08d7b3d2009-04-29 14:43:54 -07008996 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +02008997 struct drm_mode_object *drmmode_obj;
8998 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -07008999
Daniel Vetter1cff8f62012-04-24 09:55:08 +02009000 if (!drm_core_check_feature(dev, DRIVER_MODESET))
9001 return -ENODEV;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009002
Daniel Vetterc05422d2009-08-11 16:05:30 +02009003 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
9004 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -07009005
Daniel Vetterc05422d2009-08-11 16:05:30 +02009006 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -07009007 DRM_ERROR("no such CRTC id\n");
9008 return -EINVAL;
9009 }
9010
Daniel Vetterc05422d2009-08-11 16:05:30 +02009011 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
9012 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009013
Daniel Vetterc05422d2009-08-11 16:05:30 +02009014 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -07009015}
9016
Daniel Vetter66a92782012-07-12 20:08:18 +02009017static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08009018{
Daniel Vetter66a92782012-07-12 20:08:18 +02009019 struct drm_device *dev = encoder->base.dev;
9020 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08009021 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009022 int entry = 0;
9023
Daniel Vetter66a92782012-07-12 20:08:18 +02009024 list_for_each_entry(source_encoder,
9025 &dev->mode_config.encoder_list, base.head) {
9026
9027 if (encoder == source_encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08009028 index_mask |= (1 << entry);
Daniel Vetter66a92782012-07-12 20:08:18 +02009029
9030 /* Intel hw has only one MUX where enocoders could be cloned. */
9031 if (encoder->cloneable && source_encoder->cloneable)
9032 index_mask |= (1 << entry);
9033
Jesse Barnes79e53942008-11-07 14:24:08 -08009034 entry++;
9035 }
Chris Wilson4ef69c72010-09-09 15:14:28 +01009036
Jesse Barnes79e53942008-11-07 14:24:08 -08009037 return index_mask;
9038}
9039
Chris Wilson4d302442010-12-14 19:21:29 +00009040static bool has_edp_a(struct drm_device *dev)
9041{
9042 struct drm_i915_private *dev_priv = dev->dev_private;
9043
9044 if (!IS_MOBILE(dev))
9045 return false;
9046
9047 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
9048 return false;
9049
9050 if (IS_GEN5(dev) &&
9051 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
9052 return false;
9053
9054 return true;
9055}
9056
Jesse Barnes79e53942008-11-07 14:24:08 -08009057static void intel_setup_outputs(struct drm_device *dev)
9058{
Eric Anholt725e30a2009-01-22 13:01:02 -08009059 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +01009060 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009061 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08009062
Daniel Vetterc9093352013-06-06 22:22:47 +02009063 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009064
Paulo Zanonic40c0f52013-04-12 18:16:53 -03009065 if (!IS_ULT(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -02009066 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009067
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009068 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -03009069 int found;
9070
9071 /* Haswell uses DDI functions to detect digital outputs */
9072 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
9073 /* DDI A only supports eDP */
9074 if (found)
9075 intel_ddi_init(dev, PORT_A);
9076
9077 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
9078 * register */
9079 found = I915_READ(SFUSE_STRAP);
9080
9081 if (found & SFUSE_STRAP_DDIB_DETECTED)
9082 intel_ddi_init(dev, PORT_B);
9083 if (found & SFUSE_STRAP_DDIC_DETECTED)
9084 intel_ddi_init(dev, PORT_C);
9085 if (found & SFUSE_STRAP_DDID_DETECTED)
9086 intel_ddi_init(dev, PORT_D);
9087 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009088 int found;
Daniel Vetter270b3042012-10-27 15:52:05 +02009089 dpd_is_edp = intel_dpd_is_edp(dev);
9090
9091 if (has_edp_a(dev))
9092 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04009093
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009094 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +08009095 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +01009096 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009097 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -03009098 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08009099 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009100 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009101 }
9102
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009103 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -03009104 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009105
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009106 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -03009107 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08009108
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08009109 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009110 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08009111
Daniel Vetter270b3042012-10-27 15:52:05 +02009112 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009113 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -07009114 } else if (IS_VALLEYVIEW(dev)) {
Gajanan Bhat19c03922012-09-27 19:13:07 +05309115 /* Check for built-in panel first. Shares lanes with HDMI on SDVOC */
Ville Syrjälä67cfc202013-01-25 21:44:44 +02009116 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
9117 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +05309118
Paulo Zanonidc0fa712013-02-19 16:21:46 -03009119 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
Paulo Zanonie2debe92013-02-18 19:00:27 -03009120 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
9121 PORT_B);
Ville Syrjälä67cfc202013-01-25 21:44:44 +02009122 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
9123 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Jesse Barnes4a87d652012-06-15 11:55:16 -07009124 }
Zhenyu Wang103a1962009-11-27 11:44:36 +08009125 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +08009126 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -08009127
Paulo Zanonie2debe92013-02-18 19:00:27 -03009128 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009129 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009130 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009131 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
9132 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009133 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009134 }
Ma Ling27185ae2009-08-24 13:50:23 +08009135
Imre Deake7281ea2013-05-08 13:14:08 +03009136 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009137 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -08009138 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -04009139
9140 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -04009141
Paulo Zanonie2debe92013-02-18 19:00:27 -03009142 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009143 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009144 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009145 }
Ma Ling27185ae2009-08-24 13:50:23 +08009146
Paulo Zanonie2debe92013-02-18 19:00:27 -03009147 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +08009148
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009149 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
9150 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03009151 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009152 }
Imre Deake7281ea2013-05-08 13:14:08 +03009153 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009154 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -08009155 }
Ma Ling27185ae2009-08-24 13:50:23 +08009156
Jesse Barnesb01f2c32009-12-11 11:07:17 -08009157 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +03009158 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03009159 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -07009160 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08009161 intel_dvo_init(dev);
9162
Zhenyu Wang103a1962009-11-27 11:44:36 +08009163 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08009164 intel_tv_init(dev);
9165
Chris Wilson4ef69c72010-09-09 15:14:28 +01009166 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
9167 encoder->base.possible_crtcs = encoder->crtc_mask;
9168 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +02009169 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -08009170 }
Chris Wilson47356eb2011-01-11 17:06:04 +00009171
Paulo Zanonidde86e22012-12-01 12:04:25 -02009172 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +02009173
9174 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009175}
9176
9177static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
9178{
9179 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08009180
9181 drm_framebuffer_cleanup(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00009182 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08009183
9184 kfree(intel_fb);
9185}
9186
9187static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +00009188 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08009189 unsigned int *handle)
9190{
9191 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00009192 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08009193
Chris Wilson05394f32010-11-08 19:18:58 +00009194 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -08009195}
9196
9197static const struct drm_framebuffer_funcs intel_fb_funcs = {
9198 .destroy = intel_user_framebuffer_destroy,
9199 .create_handle = intel_user_framebuffer_create_handle,
9200};
9201
Dave Airlie38651672010-03-30 05:34:13 +00009202int intel_framebuffer_init(struct drm_device *dev,
9203 struct intel_framebuffer *intel_fb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009204 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilson05394f32010-11-08 19:18:58 +00009205 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -08009206{
Chris Wilsona35cdaa2013-06-25 17:26:45 +01009207 int pitch_limit;
Jesse Barnes79e53942008-11-07 14:24:08 -08009208 int ret;
9209
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009210 if (obj->tiling_mode == I915_TILING_Y) {
9211 DRM_DEBUG("hardware does not support tiling Y\n");
Chris Wilson57cd6502010-08-08 12:34:44 +01009212 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009213 }
Chris Wilson57cd6502010-08-08 12:34:44 +01009214
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009215 if (mode_cmd->pitches[0] & 63) {
9216 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
9217 mode_cmd->pitches[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +01009218 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009219 }
Chris Wilson57cd6502010-08-08 12:34:44 +01009220
Chris Wilsona35cdaa2013-06-25 17:26:45 +01009221 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
9222 pitch_limit = 32*1024;
9223 } else if (INTEL_INFO(dev)->gen >= 4) {
9224 if (obj->tiling_mode)
9225 pitch_limit = 16*1024;
9226 else
9227 pitch_limit = 32*1024;
9228 } else if (INTEL_INFO(dev)->gen >= 3) {
9229 if (obj->tiling_mode)
9230 pitch_limit = 8*1024;
9231 else
9232 pitch_limit = 16*1024;
9233 } else
9234 /* XXX DSPC is limited to 4k tiled */
9235 pitch_limit = 8*1024;
9236
9237 if (mode_cmd->pitches[0] > pitch_limit) {
9238 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
9239 obj->tiling_mode ? "tiled" : "linear",
9240 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02009241 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009242 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02009243
9244 if (obj->tiling_mode != I915_TILING_NONE &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009245 mode_cmd->pitches[0] != obj->stride) {
9246 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
9247 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02009248 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009249 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02009250
Ville Syrjälä57779d02012-10-31 17:50:14 +02009251 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009252 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +02009253 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02009254 case DRM_FORMAT_RGB565:
9255 case DRM_FORMAT_XRGB8888:
9256 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02009257 break;
9258 case DRM_FORMAT_XRGB1555:
9259 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009260 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +00009261 DRM_DEBUG("unsupported pixel format: %s\n",
9262 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +02009263 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009264 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02009265 break;
9266 case DRM_FORMAT_XBGR8888:
9267 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +02009268 case DRM_FORMAT_XRGB2101010:
9269 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02009270 case DRM_FORMAT_XBGR2101010:
9271 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009272 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +00009273 DRM_DEBUG("unsupported pixel format: %s\n",
9274 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +02009275 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009276 }
Jesse Barnesb5626742011-06-24 12:19:27 -07009277 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02009278 case DRM_FORMAT_YUYV:
9279 case DRM_FORMAT_UYVY:
9280 case DRM_FORMAT_YVYU:
9281 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009282 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +00009283 DRM_DEBUG("unsupported pixel format: %s\n",
9284 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +02009285 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009286 }
Chris Wilson57cd6502010-08-08 12:34:44 +01009287 break;
9288 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +00009289 DRM_DEBUG("unsupported pixel format: %s\n",
9290 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +01009291 return -EINVAL;
9292 }
9293
Ville Syrjälä90f9a332012-10-31 17:50:19 +02009294 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
9295 if (mode_cmd->offsets[0] != 0)
9296 return -EINVAL;
9297
Daniel Vetterc7d73f62012-12-13 23:38:38 +01009298 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
9299 intel_fb->obj = obj;
9300
Jesse Barnes79e53942008-11-07 14:24:08 -08009301 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
9302 if (ret) {
9303 DRM_ERROR("framebuffer init failed %d\n", ret);
9304 return ret;
9305 }
9306
Jesse Barnes79e53942008-11-07 14:24:08 -08009307 return 0;
9308}
9309
Jesse Barnes79e53942008-11-07 14:24:08 -08009310static struct drm_framebuffer *
9311intel_user_framebuffer_create(struct drm_device *dev,
9312 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009313 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -08009314{
Chris Wilson05394f32010-11-08 19:18:58 +00009315 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08009316
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009317 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
9318 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +00009319 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +01009320 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -08009321
Chris Wilsond2dff872011-04-19 08:36:26 +01009322 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08009323}
9324
Jesse Barnes79e53942008-11-07 14:24:08 -08009325static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -08009326 .fb_create = intel_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +00009327 .output_poll_changed = intel_fb_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -08009328};
9329
Jesse Barnese70236a2009-09-21 10:42:27 -07009330/* Set up chip specific display functions */
9331static void intel_init_display(struct drm_device *dev)
9332{
9333 struct drm_i915_private *dev_priv = dev->dev_private;
9334
Daniel Vetteree9300b2013-06-03 22:40:22 +02009335 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
9336 dev_priv->display.find_dpll = g4x_find_best_dpll;
9337 else if (IS_VALLEYVIEW(dev))
9338 dev_priv->display.find_dpll = vlv_find_best_dpll;
9339 else if (IS_PINEVIEW(dev))
9340 dev_priv->display.find_dpll = pnv_find_best_dpll;
9341 else
9342 dev_priv->display.find_dpll = i9xx_find_best_dpll;
9343
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009344 if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009345 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009346 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02009347 dev_priv->display.crtc_enable = haswell_crtc_enable;
9348 dev_priv->display.crtc_disable = haswell_crtc_disable;
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009349 dev_priv->display.off = haswell_crtc_off;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009350 dev_priv->display.update_plane = ironlake_update_plane;
9351 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009352 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009353 dev_priv->display.get_clock = ironlake_crtc_clock_get;
Eric Anholtf564048e2011-03-30 13:01:02 -07009354 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +02009355 dev_priv->display.crtc_enable = ironlake_crtc_enable;
9356 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009357 dev_priv->display.off = ironlake_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -07009358 dev_priv->display.update_plane = ironlake_update_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -07009359 } else if (IS_VALLEYVIEW(dev)) {
9360 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009361 dev_priv->display.get_clock = i9xx_crtc_clock_get;
Jesse Barnes89b667f2013-04-18 14:51:36 -07009362 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
9363 dev_priv->display.crtc_enable = valleyview_crtc_enable;
9364 dev_priv->display.crtc_disable = i9xx_crtc_disable;
9365 dev_priv->display.off = i9xx_crtc_off;
9366 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07009367 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009368 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009369 dev_priv->display.get_clock = i9xx_crtc_clock_get;
Eric Anholtf564048e2011-03-30 13:01:02 -07009370 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +02009371 dev_priv->display.crtc_enable = i9xx_crtc_enable;
9372 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009373 dev_priv->display.off = i9xx_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -07009374 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07009375 }
Jesse Barnese70236a2009-09-21 10:42:27 -07009376
Jesse Barnese70236a2009-09-21 10:42:27 -07009377 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07009378 if (IS_VALLEYVIEW(dev))
9379 dev_priv->display.get_display_clock_speed =
9380 valleyview_get_display_clock_speed;
9381 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -07009382 dev_priv->display.get_display_clock_speed =
9383 i945_get_display_clock_speed;
9384 else if (IS_I915G(dev))
9385 dev_priv->display.get_display_clock_speed =
9386 i915_get_display_clock_speed;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05009387 else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07009388 dev_priv->display.get_display_clock_speed =
9389 i9xx_misc_get_display_clock_speed;
9390 else if (IS_I915GM(dev))
9391 dev_priv->display.get_display_clock_speed =
9392 i915gm_get_display_clock_speed;
9393 else if (IS_I865G(dev))
9394 dev_priv->display.get_display_clock_speed =
9395 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +02009396 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07009397 dev_priv->display.get_display_clock_speed =
9398 i855_get_display_clock_speed;
9399 else /* 852, 830 */
9400 dev_priv->display.get_display_clock_speed =
9401 i830_get_display_clock_speed;
9402
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08009403 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01009404 if (IS_GEN5(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -07009405 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08009406 dev_priv->display.write_eld = ironlake_write_eld;
Yuanhan Liu13982612010-12-15 15:42:31 +08009407 } else if (IS_GEN6(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -07009408 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08009409 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnes357555c2011-04-28 15:09:55 -07009410 } else if (IS_IVYBRIDGE(dev)) {
9411 /* FIXME: detect B0+ stepping and use auto training */
9412 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08009413 dev_priv->display.write_eld = ironlake_write_eld;
Daniel Vetter01a415f2012-10-27 15:58:40 +02009414 dev_priv->display.modeset_global_resources =
9415 ivb_modeset_global_resources;
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -03009416 } else if (IS_HASWELL(dev)) {
9417 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Wang Xingchao83358c852012-08-16 22:43:37 +08009418 dev_priv->display.write_eld = haswell_write_eld;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02009419 dev_priv->display.modeset_global_resources =
9420 haswell_modeset_global_resources;
Paulo Zanonia0e63c22012-12-06 11:12:39 -02009421 }
Jesse Barnes6067aae2011-04-28 15:04:31 -07009422 } else if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +08009423 dev_priv->display.write_eld = g4x_write_eld;
Jesse Barnese70236a2009-09-21 10:42:27 -07009424 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009425
9426 /* Default just returns -ENODEV to indicate unsupported */
9427 dev_priv->display.queue_flip = intel_default_queue_flip;
9428
9429 switch (INTEL_INFO(dev)->gen) {
9430 case 2:
9431 dev_priv->display.queue_flip = intel_gen2_queue_flip;
9432 break;
9433
9434 case 3:
9435 dev_priv->display.queue_flip = intel_gen3_queue_flip;
9436 break;
9437
9438 case 4:
9439 case 5:
9440 dev_priv->display.queue_flip = intel_gen4_queue_flip;
9441 break;
9442
9443 case 6:
9444 dev_priv->display.queue_flip = intel_gen6_queue_flip;
9445 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009446 case 7:
9447 dev_priv->display.queue_flip = intel_gen7_queue_flip;
9448 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009449 }
Jesse Barnese70236a2009-09-21 10:42:27 -07009450}
9451
Jesse Barnesb690e962010-07-19 13:53:12 -07009452/*
9453 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
9454 * resume, or other times. This quirk makes sure that's the case for
9455 * affected systems.
9456 */
Akshay Joshi0206e352011-08-16 15:34:10 -04009457static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -07009458{
9459 struct drm_i915_private *dev_priv = dev->dev_private;
9460
9461 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02009462 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -07009463}
9464
Keith Packard435793d2011-07-12 14:56:22 -07009465/*
9466 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
9467 */
9468static void quirk_ssc_force_disable(struct drm_device *dev)
9469{
9470 struct drm_i915_private *dev_priv = dev->dev_private;
9471 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02009472 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -07009473}
9474
Carsten Emde4dca20e2012-03-15 15:56:26 +01009475/*
Carsten Emde5a15ab52012-03-15 15:56:27 +01009476 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
9477 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +01009478 */
9479static void quirk_invert_brightness(struct drm_device *dev)
9480{
9481 struct drm_i915_private *dev_priv = dev->dev_private;
9482 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02009483 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -07009484}
9485
9486struct intel_quirk {
9487 int device;
9488 int subsystem_vendor;
9489 int subsystem_device;
9490 void (*hook)(struct drm_device *dev);
9491};
9492
Egbert Eich5f85f1762012-10-14 15:46:38 +02009493/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
9494struct intel_dmi_quirk {
9495 void (*hook)(struct drm_device *dev);
9496 const struct dmi_system_id (*dmi_id_list)[];
9497};
9498
9499static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
9500{
9501 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
9502 return 1;
9503}
9504
9505static const struct intel_dmi_quirk intel_dmi_quirks[] = {
9506 {
9507 .dmi_id_list = &(const struct dmi_system_id[]) {
9508 {
9509 .callback = intel_dmi_reverse_brightness,
9510 .ident = "NCR Corporation",
9511 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
9512 DMI_MATCH(DMI_PRODUCT_NAME, ""),
9513 },
9514 },
9515 { } /* terminating entry */
9516 },
9517 .hook = quirk_invert_brightness,
9518 },
9519};
9520
Ben Widawskyc43b5632012-04-16 14:07:40 -07009521static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -07009522 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -04009523 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -07009524
Jesse Barnesb690e962010-07-19 13:53:12 -07009525 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
9526 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
9527
Jesse Barnesb690e962010-07-19 13:53:12 -07009528 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
9529 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
9530
Daniel Vetterccd0d362012-10-10 23:13:59 +02009531 /* 830/845 need to leave pipe A & dpll A up */
Jesse Barnesb690e962010-07-19 13:53:12 -07009532 { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Daniel Vetterdcdaed62012-08-12 21:19:34 +02009533 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Keith Packard435793d2011-07-12 14:56:22 -07009534
9535 /* Lenovo U160 cannot use SSC on LVDS */
9536 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +02009537
9538 /* Sony Vaio Y cannot use SSC on LVDS */
9539 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +01009540
9541 /* Acer Aspire 5734Z must invert backlight brightness */
9542 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
Jani Nikula1ffff602013-01-22 12:50:34 +02009543
9544 /* Acer/eMachines G725 */
9545 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
Jani Nikula01e3a8f2013-01-22 12:50:35 +02009546
9547 /* Acer/eMachines e725 */
9548 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
Jani Nikula5559eca2013-01-22 12:50:36 +02009549
9550 /* Acer/Packard Bell NCL20 */
9551 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
Daniel Vetterac4199e2013-02-15 18:35:30 +01009552
9553 /* Acer Aspire 4736Z */
9554 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jesse Barnesb690e962010-07-19 13:53:12 -07009555};
9556
9557static void intel_init_quirks(struct drm_device *dev)
9558{
9559 struct pci_dev *d = dev->pdev;
9560 int i;
9561
9562 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
9563 struct intel_quirk *q = &intel_quirks[i];
9564
9565 if (d->device == q->device &&
9566 (d->subsystem_vendor == q->subsystem_vendor ||
9567 q->subsystem_vendor == PCI_ANY_ID) &&
9568 (d->subsystem_device == q->subsystem_device ||
9569 q->subsystem_device == PCI_ANY_ID))
9570 q->hook(dev);
9571 }
Egbert Eich5f85f1762012-10-14 15:46:38 +02009572 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
9573 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
9574 intel_dmi_quirks[i].hook(dev);
9575 }
Jesse Barnesb690e962010-07-19 13:53:12 -07009576}
9577
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009578/* Disable the VGA plane that we never use */
9579static void i915_disable_vga(struct drm_device *dev)
9580{
9581 struct drm_i915_private *dev_priv = dev->dev_private;
9582 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +02009583 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009584
9585 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -07009586 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009587 sr1 = inb(VGA_SR_DATA);
9588 outb(sr1 | 1<<5, VGA_SR_DATA);
9589 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
9590 udelay(300);
9591
9592 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9593 POSTING_READ(vga_reg);
9594}
9595
Daniel Vetterf8175862012-04-10 15:50:11 +02009596void intel_modeset_init_hw(struct drm_device *dev)
9597{
Paulo Zanonifa42e232013-01-25 16:59:11 -02009598 intel_init_power_well(dev);
Eugeni Dodonov0232e922012-07-06 15:42:36 -03009599
Eugeni Dodonova8f78b52012-06-28 15:55:35 -03009600 intel_prepare_ddi(dev);
9601
Daniel Vetterf8175862012-04-10 15:50:11 +02009602 intel_init_clock_gating(dev);
9603
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02009604 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02009605 intel_enable_gt_powersave(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02009606 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf8175862012-04-10 15:50:11 +02009607}
9608
Imre Deak7d708ee2013-04-17 14:04:50 +03009609void intel_modeset_suspend_hw(struct drm_device *dev)
9610{
9611 intel_suspend_hw(dev);
9612}
9613
Jesse Barnes79e53942008-11-07 14:24:08 -08009614void intel_modeset_init(struct drm_device *dev)
9615{
Jesse Barnes652c3932009-08-17 13:31:43 -07009616 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7f1f3852013-04-02 11:22:20 -07009617 int i, j, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08009618
9619 drm_mode_config_init(dev);
9620
9621 dev->mode_config.min_width = 0;
9622 dev->mode_config.min_height = 0;
9623
Dave Airlie019d96c2011-09-29 16:20:42 +01009624 dev->mode_config.preferred_depth = 24;
9625 dev->mode_config.prefer_shadow = 1;
9626
Laurent Pincharte6ecefa2012-05-17 13:27:23 +02009627 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -08009628
Jesse Barnesb690e962010-07-19 13:53:12 -07009629 intel_init_quirks(dev);
9630
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009631 intel_init_pm(dev);
9632
Ben Widawskye3c74752013-04-05 13:12:39 -07009633 if (INTEL_INFO(dev)->num_pipes == 0)
9634 return;
9635
Jesse Barnese70236a2009-09-21 10:42:27 -07009636 intel_init_display(dev);
9637
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009638 if (IS_GEN2(dev)) {
9639 dev->mode_config.max_width = 2048;
9640 dev->mode_config.max_height = 2048;
9641 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -07009642 dev->mode_config.max_width = 4096;
9643 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -08009644 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009645 dev->mode_config.max_width = 8192;
9646 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -08009647 }
Ben Widawsky5d4545a2013-01-17 12:45:15 -08009648 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -08009649
Zhao Yakui28c97732009-10-09 11:39:41 +08009650 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -07009651 INTEL_INFO(dev)->num_pipes,
9652 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -08009653
Ben Widawsky7eb552a2013-03-13 14:05:41 -07009654 for (i = 0; i < INTEL_INFO(dev)->num_pipes; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009655 intel_crtc_init(dev, i);
Jesse Barnes7f1f3852013-04-02 11:22:20 -07009656 for (j = 0; j < dev_priv->num_plane; j++) {
9657 ret = intel_plane_init(dev, i, j);
9658 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +03009659 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
9660 pipe_name(i), sprite_name(i, j), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -07009661 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009662 }
9663
Paulo Zanoni79f689a2012-10-05 12:05:52 -03009664 intel_cpu_pll_init(dev);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02009665 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009666
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009667 /* Just disable it once at startup */
9668 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009669 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +00009670
9671 /* Just in case the BIOS is doing something questionable. */
9672 intel_disable_fbc(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01009673}
Jesse Barnesd5bb0812011-01-05 12:01:26 -08009674
Daniel Vetter24929352012-07-02 20:28:59 +02009675static void
9676intel_connector_break_all_links(struct intel_connector *connector)
9677{
9678 connector->base.dpms = DRM_MODE_DPMS_OFF;
9679 connector->base.encoder = NULL;
9680 connector->encoder->connectors_active = false;
9681 connector->encoder->base.crtc = NULL;
9682}
9683
Daniel Vetter7fad7982012-07-04 17:51:47 +02009684static void intel_enable_pipe_a(struct drm_device *dev)
9685{
9686 struct intel_connector *connector;
9687 struct drm_connector *crt = NULL;
9688 struct intel_load_detect_pipe load_detect_temp;
9689
9690 /* We can't just switch on the pipe A, we need to set things up with a
9691 * proper mode and output configuration. As a gross hack, enable pipe A
9692 * by enabling the load detect pipe once. */
9693 list_for_each_entry(connector,
9694 &dev->mode_config.connector_list,
9695 base.head) {
9696 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
9697 crt = &connector->base;
9698 break;
9699 }
9700 }
9701
9702 if (!crt)
9703 return;
9704
9705 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
9706 intel_release_load_detect_pipe(crt, &load_detect_temp);
9707
9708
9709}
9710
Daniel Vetterfa555832012-10-10 23:14:00 +02009711static bool
9712intel_check_plane_mapping(struct intel_crtc *crtc)
9713{
Ben Widawsky7eb552a2013-03-13 14:05:41 -07009714 struct drm_device *dev = crtc->base.dev;
9715 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +02009716 u32 reg, val;
9717
Ben Widawsky7eb552a2013-03-13 14:05:41 -07009718 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +02009719 return true;
9720
9721 reg = DSPCNTR(!crtc->plane);
9722 val = I915_READ(reg);
9723
9724 if ((val & DISPLAY_PLANE_ENABLE) &&
9725 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
9726 return false;
9727
9728 return true;
9729}
9730
Daniel Vetter24929352012-07-02 20:28:59 +02009731static void intel_sanitize_crtc(struct intel_crtc *crtc)
9732{
9733 struct drm_device *dev = crtc->base.dev;
9734 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +02009735 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +02009736
Daniel Vetter24929352012-07-02 20:28:59 +02009737 /* Clear any frame start delays used for debugging left by the BIOS */
Daniel Vetter3b117c82013-04-17 20:15:07 +02009738 reg = PIPECONF(crtc->config.cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +02009739 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
9740
9741 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +02009742 * disable the crtc (and hence change the state) if it is wrong. Note
9743 * that gen4+ has a fixed plane -> pipe mapping. */
9744 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +02009745 struct intel_connector *connector;
9746 bool plane;
9747
Daniel Vetter24929352012-07-02 20:28:59 +02009748 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
9749 crtc->base.base.id);
9750
9751 /* Pipe has the wrong plane attached and the plane is active.
9752 * Temporarily change the plane mapping and disable everything
9753 * ... */
9754 plane = crtc->plane;
9755 crtc->plane = !plane;
9756 dev_priv->display.crtc_disable(&crtc->base);
9757 crtc->plane = plane;
9758
9759 /* ... and break all links. */
9760 list_for_each_entry(connector, &dev->mode_config.connector_list,
9761 base.head) {
9762 if (connector->encoder->base.crtc != &crtc->base)
9763 continue;
9764
9765 intel_connector_break_all_links(connector);
9766 }
9767
9768 WARN_ON(crtc->active);
9769 crtc->base.enabled = false;
9770 }
Daniel Vetter24929352012-07-02 20:28:59 +02009771
Daniel Vetter7fad7982012-07-04 17:51:47 +02009772 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
9773 crtc->pipe == PIPE_A && !crtc->active) {
9774 /* BIOS forgot to enable pipe A, this mostly happens after
9775 * resume. Force-enable the pipe to fix this, the update_dpms
9776 * call below we restore the pipe to the right state, but leave
9777 * the required bits on. */
9778 intel_enable_pipe_a(dev);
9779 }
9780
Daniel Vetter24929352012-07-02 20:28:59 +02009781 /* Adjust the state of the output pipe according to whether we
9782 * have active connectors/encoders. */
9783 intel_crtc_update_dpms(&crtc->base);
9784
9785 if (crtc->active != crtc->base.enabled) {
9786 struct intel_encoder *encoder;
9787
9788 /* This can happen either due to bugs in the get_hw_state
9789 * functions or because the pipe is force-enabled due to the
9790 * pipe A quirk. */
9791 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
9792 crtc->base.base.id,
9793 crtc->base.enabled ? "enabled" : "disabled",
9794 crtc->active ? "enabled" : "disabled");
9795
9796 crtc->base.enabled = crtc->active;
9797
9798 /* Because we only establish the connector -> encoder ->
9799 * crtc links if something is active, this means the
9800 * crtc is now deactivated. Break the links. connector
9801 * -> encoder links are only establish when things are
9802 * actually up, hence no need to break them. */
9803 WARN_ON(crtc->active);
9804
9805 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
9806 WARN_ON(encoder->connectors_active);
9807 encoder->base.crtc = NULL;
9808 }
9809 }
9810}
9811
9812static void intel_sanitize_encoder(struct intel_encoder *encoder)
9813{
9814 struct intel_connector *connector;
9815 struct drm_device *dev = encoder->base.dev;
9816
9817 /* We need to check both for a crtc link (meaning that the
9818 * encoder is active and trying to read from a pipe) and the
9819 * pipe itself being active. */
9820 bool has_active_crtc = encoder->base.crtc &&
9821 to_intel_crtc(encoder->base.crtc)->active;
9822
9823 if (encoder->connectors_active && !has_active_crtc) {
9824 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
9825 encoder->base.base.id,
9826 drm_get_encoder_name(&encoder->base));
9827
9828 /* Connector is active, but has no active pipe. This is
9829 * fallout from our resume register restoring. Disable
9830 * the encoder manually again. */
9831 if (encoder->base.crtc) {
9832 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
9833 encoder->base.base.id,
9834 drm_get_encoder_name(&encoder->base));
9835 encoder->disable(encoder);
9836 }
9837
9838 /* Inconsistent output/port/pipe state happens presumably due to
9839 * a bug in one of the get_hw_state functions. Or someplace else
9840 * in our code, like the register restore mess on resume. Clamp
9841 * things to off as a safer default. */
9842 list_for_each_entry(connector,
9843 &dev->mode_config.connector_list,
9844 base.head) {
9845 if (connector->encoder != encoder)
9846 continue;
9847
9848 intel_connector_break_all_links(connector);
9849 }
9850 }
9851 /* Enabled encoders without active connectors will be fixed in
9852 * the crtc fixup. */
9853}
9854
Daniel Vetter44cec742013-01-25 17:53:21 +01009855void i915_redisable_vga(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01009856{
9857 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +02009858 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01009859
9860 if (I915_READ(vga_reg) != VGA_DISP_DISABLE) {
9861 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
Ville Syrjälä209d5212013-01-25 21:44:48 +02009862 i915_disable_vga(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01009863 }
9864}
9865
Daniel Vetter30e984d2013-06-05 13:34:17 +02009866static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +02009867{
9868 struct drm_i915_private *dev_priv = dev->dev_private;
9869 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +02009870 struct intel_crtc *crtc;
9871 struct intel_encoder *encoder;
9872 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +02009873 int i;
Daniel Vetter24929352012-07-02 20:28:59 +02009874
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009875 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9876 base.head) {
Daniel Vetter88adfff2013-03-28 10:42:01 +01009877 memset(&crtc->config, 0, sizeof(crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +02009878
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009879 crtc->active = dev_priv->display.get_pipe_config(crtc,
9880 &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +02009881
9882 crtc->base.enabled = crtc->active;
9883
9884 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
9885 crtc->base.base.id,
9886 crtc->active ? "enabled" : "disabled");
9887 }
9888
Daniel Vetter53589012013-06-05 13:34:16 +02009889 /* FIXME: Smash this into the new shared dpll infrastructure. */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009890 if (HAS_DDI(dev))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009891 intel_ddi_setup_hw_pll_state(dev);
9892
Daniel Vetter53589012013-06-05 13:34:16 +02009893 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
9894 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
9895
9896 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
9897 pll->active = 0;
9898 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9899 base.head) {
9900 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
9901 pll->active++;
9902 }
9903 pll->refcount = pll->active;
9904
9905 DRM_DEBUG_KMS("%s hw state readout: refcount %i\n",
9906 pll->name, pll->refcount);
9907 }
9908
Daniel Vetter24929352012-07-02 20:28:59 +02009909 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9910 base.head) {
9911 pipe = 0;
9912
9913 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -07009914 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
9915 encoder->base.crtc = &crtc->base;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009916 if (encoder->get_config &&
9917 dev_priv->display.get_clock) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -07009918 encoder->get_config(encoder, &crtc->config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009919 dev_priv->display.get_clock(crtc,
9920 &crtc->config);
9921 }
Daniel Vetter24929352012-07-02 20:28:59 +02009922 } else {
9923 encoder->base.crtc = NULL;
9924 }
9925
9926 encoder->connectors_active = false;
9927 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe=%i\n",
9928 encoder->base.base.id,
9929 drm_get_encoder_name(&encoder->base),
9930 encoder->base.crtc ? "enabled" : "disabled",
9931 pipe);
9932 }
9933
9934 list_for_each_entry(connector, &dev->mode_config.connector_list,
9935 base.head) {
9936 if (connector->get_hw_state(connector)) {
9937 connector->base.dpms = DRM_MODE_DPMS_ON;
9938 connector->encoder->connectors_active = true;
9939 connector->base.encoder = &connector->encoder->base;
9940 } else {
9941 connector->base.dpms = DRM_MODE_DPMS_OFF;
9942 connector->base.encoder = NULL;
9943 }
9944 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
9945 connector->base.base.id,
9946 drm_get_connector_name(&connector->base),
9947 connector->base.encoder ? "enabled" : "disabled");
9948 }
Daniel Vetter30e984d2013-06-05 13:34:17 +02009949}
9950
9951/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
9952 * and i915 state tracking structures. */
9953void intel_modeset_setup_hw_state(struct drm_device *dev,
9954 bool force_restore)
9955{
9956 struct drm_i915_private *dev_priv = dev->dev_private;
9957 enum pipe pipe;
9958 struct drm_plane *plane;
9959 struct intel_crtc *crtc;
9960 struct intel_encoder *encoder;
9961
9962 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +02009963
9964 /* HW state is read out, now we need to sanitize this mess. */
9965 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9966 base.head) {
9967 intel_sanitize_encoder(encoder);
9968 }
9969
9970 for_each_pipe(pipe) {
9971 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
9972 intel_sanitize_crtc(crtc);
Daniel Vetterc0b03412013-05-28 12:05:54 +02009973 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +02009974 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009975
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009976 if (force_restore) {
Daniel Vetterf30da182013-04-11 20:22:50 +02009977 /*
9978 * We need to use raw interfaces for restoring state to avoid
9979 * checking (bogus) intermediate states.
9980 */
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009981 for_each_pipe(pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -07009982 struct drm_crtc *crtc =
9983 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +02009984
9985 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
9986 crtc->fb);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009987 }
Jesse Barnesb5644d02013-03-26 13:25:27 -07009988 list_for_each_entry(plane, &dev->mode_config.plane_list, head)
9989 intel_plane_restore(plane);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01009990
9991 i915_redisable_vga(dev);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009992 } else {
9993 intel_modeset_update_staged_output_state(dev);
9994 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009995
9996 intel_modeset_check_state(dev);
Daniel Vetter2e938892012-10-11 20:08:24 +02009997
9998 drm_mode_config_reset(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01009999}
10000
10001void intel_modeset_gem_init(struct drm_device *dev)
10002{
Chris Wilson1833b132012-05-09 11:56:28 +010010003 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020010004
10005 intel_setup_overlay(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020010006
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010010007 intel_modeset_setup_hw_state(dev, false);
Jesse Barnes79e53942008-11-07 14:24:08 -080010008}
10009
10010void intel_modeset_cleanup(struct drm_device *dev)
10011{
Jesse Barnes652c3932009-08-17 13:31:43 -070010012 struct drm_i915_private *dev_priv = dev->dev_private;
10013 struct drm_crtc *crtc;
10014 struct intel_crtc *intel_crtc;
10015
Daniel Vetterfd0c0642013-04-24 11:13:35 +020010016 /*
10017 * Interrupts and polling as the first thing to avoid creating havoc.
10018 * Too much stuff here (turning of rps, connectors, ...) would
10019 * experience fancy races otherwise.
10020 */
10021 drm_irq_uninstall(dev);
10022 cancel_work_sync(&dev_priv->hotplug_work);
10023 /*
10024 * Due to the hpd irq storm handling the hotplug work can re-arm the
10025 * poll handlers. Hence disable polling after hpd handling is shut down.
10026 */
Keith Packardf87ea762010-10-03 19:36:26 -070010027 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020010028
Jesse Barnes652c3932009-08-17 13:31:43 -070010029 mutex_lock(&dev->struct_mutex);
10030
Jesse Barnes723bfd72010-10-07 16:01:13 -070010031 intel_unregister_dsm_handler();
10032
Jesse Barnes652c3932009-08-17 13:31:43 -070010033 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
10034 /* Skip inactive CRTCs */
10035 if (!crtc->fb)
10036 continue;
10037
10038 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3dec0092010-08-20 21:40:52 +020010039 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070010040 }
10041
Chris Wilson973d04f2011-07-08 12:22:37 +010010042 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070010043
Daniel Vetter8090c6b2012-06-24 16:42:32 +020010044 intel_disable_gt_powersave(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +000010045
Daniel Vetter930ebb42012-06-29 23:32:16 +020010046 ironlake_teardown_rc6(dev);
10047
Kristian Høgsberg69341a52009-11-11 12:19:17 -050010048 mutex_unlock(&dev->struct_mutex);
10049
Chris Wilson1630fe72011-07-08 12:22:42 +010010050 /* flush any delayed tasks or pending work */
10051 flush_scheduled_work();
10052
Jani Nikuladc652f92013-04-12 15:18:38 +030010053 /* destroy backlight, if any, before the connectors */
10054 intel_panel_destroy_backlight(dev);
10055
Jesse Barnes79e53942008-11-07 14:24:08 -080010056 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010010057
10058 intel_cleanup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010059}
10060
Dave Airlie28d52042009-09-21 14:33:58 +100010061/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080010062 * Return which encoder is currently attached for connector.
10063 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010010064struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080010065{
Chris Wilsondf0e9242010-09-09 16:20:55 +010010066 return &intel_attached_encoder(connector)->base;
10067}
Jesse Barnes79e53942008-11-07 14:24:08 -080010068
Chris Wilsondf0e9242010-09-09 16:20:55 +010010069void intel_connector_attach_encoder(struct intel_connector *connector,
10070 struct intel_encoder *encoder)
10071{
10072 connector->encoder = encoder;
10073 drm_mode_connector_attach_encoder(&connector->base,
10074 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080010075}
Dave Airlie28d52042009-09-21 14:33:58 +100010076
10077/*
10078 * set vga decode state - true == enable VGA decode
10079 */
10080int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
10081{
10082 struct drm_i915_private *dev_priv = dev->dev_private;
10083 u16 gmch_ctrl;
10084
10085 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
10086 if (state)
10087 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
10088 else
10089 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
10090 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
10091 return 0;
10092}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010093
10094#ifdef CONFIG_DEBUG_FS
10095#include <linux/seq_file.h>
10096
10097struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010098
10099 u32 power_well_driver;
10100
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010101 struct intel_cursor_error_state {
10102 u32 control;
10103 u32 position;
10104 u32 base;
10105 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010010106 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010107
10108 struct intel_pipe_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010109 enum transcoder cpu_transcoder;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010110 u32 conf;
10111 u32 source;
10112
10113 u32 htotal;
10114 u32 hblank;
10115 u32 hsync;
10116 u32 vtotal;
10117 u32 vblank;
10118 u32 vsync;
Damien Lespiau52331302012-08-15 19:23:25 +010010119 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010120
10121 struct intel_plane_error_state {
10122 u32 control;
10123 u32 stride;
10124 u32 size;
10125 u32 pos;
10126 u32 addr;
10127 u32 surface;
10128 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010010129 } plane[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010130};
10131
10132struct intel_display_error_state *
10133intel_display_capture_error_state(struct drm_device *dev)
10134{
Akshay Joshi0206e352011-08-16 15:34:10 -040010135 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010136 struct intel_display_error_state *error;
Paulo Zanoni702e7a52012-10-23 18:29:59 -020010137 enum transcoder cpu_transcoder;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010138 int i;
10139
10140 error = kmalloc(sizeof(*error), GFP_ATOMIC);
10141 if (error == NULL)
10142 return NULL;
10143
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010144 if (HAS_POWER_WELL(dev))
10145 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
10146
Damien Lespiau52331302012-08-15 19:23:25 +010010147 for_each_pipe(i) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -020010148 cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv, i);
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010149 error->pipe[i].cpu_transcoder = cpu_transcoder;
Paulo Zanoni702e7a52012-10-23 18:29:59 -020010150
Paulo Zanonia18c4c32013-03-06 20:03:12 -030010151 if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev)) {
10152 error->cursor[i].control = I915_READ(CURCNTR(i));
10153 error->cursor[i].position = I915_READ(CURPOS(i));
10154 error->cursor[i].base = I915_READ(CURBASE(i));
10155 } else {
10156 error->cursor[i].control = I915_READ(CURCNTR_IVB(i));
10157 error->cursor[i].position = I915_READ(CURPOS_IVB(i));
10158 error->cursor[i].base = I915_READ(CURBASE_IVB(i));
10159 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010160
10161 error->plane[i].control = I915_READ(DSPCNTR(i));
10162 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030010163 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030010164 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030010165 error->plane[i].pos = I915_READ(DSPPOS(i));
10166 }
Paulo Zanonica291362013-03-06 20:03:14 -030010167 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
10168 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010169 if (INTEL_INFO(dev)->gen >= 4) {
10170 error->plane[i].surface = I915_READ(DSPSURF(i));
10171 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
10172 }
10173
Paulo Zanoni702e7a52012-10-23 18:29:59 -020010174 error->pipe[i].conf = I915_READ(PIPECONF(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010175 error->pipe[i].source = I915_READ(PIPESRC(i));
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010176 error->pipe[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
10177 error->pipe[i].hblank = I915_READ(HBLANK(cpu_transcoder));
10178 error->pipe[i].hsync = I915_READ(HSYNC(cpu_transcoder));
10179 error->pipe[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
10180 error->pipe[i].vblank = I915_READ(VBLANK(cpu_transcoder));
10181 error->pipe[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010182 }
10183
Paulo Zanoni12d217c2013-05-03 12:15:38 -030010184 /* In the code above we read the registers without checking if the power
10185 * well was on, so here we have to clear the FPGA_DBG_RM_NOCLAIM bit to
10186 * prevent the next I915_WRITE from detecting it and printing an error
10187 * message. */
10188 if (HAS_POWER_WELL(dev))
10189 I915_WRITE_NOTRACE(FPGA_DBG, FPGA_DBG_RM_NOCLAIM);
10190
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010191 return error;
10192}
10193
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010194#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
10195
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010196void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010197intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010198 struct drm_device *dev,
10199 struct intel_display_error_state *error)
10200{
10201 int i;
10202
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010203 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010204 if (HAS_POWER_WELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010205 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010206 error->power_well_driver);
Damien Lespiau52331302012-08-15 19:23:25 +010010207 for_each_pipe(i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010208 err_printf(m, "Pipe [%d]:\n", i);
10209 err_printf(m, " CPU transcoder: %c\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030010210 transcoder_name(error->pipe[i].cpu_transcoder));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010211 err_printf(m, " CONF: %08x\n", error->pipe[i].conf);
10212 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
10213 err_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
10214 err_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
10215 err_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
10216 err_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
10217 err_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
10218 err_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010219
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010220 err_printf(m, "Plane [%d]:\n", i);
10221 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
10222 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030010223 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010224 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
10225 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030010226 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030010227 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010228 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010229 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010230 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
10231 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010232 }
10233
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030010234 err_printf(m, "Cursor [%d]:\n", i);
10235 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
10236 err_printf(m, " POS: %08x\n", error->cursor[i].position);
10237 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000010238 }
10239}
10240#endif